1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
13 /********************** DEFINITIONS & DECLARATIONS ***********************/
19 #include <sys/socket.h>
26 #define DNS_FLAG_MSGTYPE 0xF800 /**< Message type mask (opcode) */
27 #define DNS_FLAG_SQUERY 0x0000 /**< Standard query type */
28 #define DNS_FLAG_SRESPONSE 0x8000 /**< Standard response type */
29 #define DNS_FLAG_RD 0x0100 /**< Recursion desired flag */
30 #define DNS_FLAG_RCODE 0x000F /**< Response code mask
31 (stores err.cond.) code */
32 #define DNS_RCODE_NERROR 0 /**< "No errors" code */
34 #define DNS_QTYPE_A 1 /**< A 32-bit IP record type */
35 #define DNS_QTYPE_AAAA 0x1c /**< 128-bit IPv6 record type */
36 #define DNS_QTYPE_CNAME 5 /**< Canonical name record type */
38 #define DNS_QCLASS_IN 1 /**< Query class for internet msgs */
41 * A header for DNS-messages (see RFC 1035, paragraph 4.1.1).
43 * DNS-message consist of DNS-header and 4 optional sections,
44 * arranged in the following order:<ul>
46 * <li> question section
48 * <li> authority section
49 * <li> additional section
53 uint16_t id; /**< an identifier used to match up replies */
54 uint16_t flags; /**< contains op_code, err_code, etc. */
55 uint16_t qdcount; /**< specifies the number of entries in the
57 uint16_t ancount; /**< specifies the number of entries in the
59 uint16_t nscount; /**< specifies the number of entries in the
61 uint16_t arcount; /**< specifies the number of entries in the
66 /***************************** PROTOTYPES ********************************/
69 dns_send_query(int fd, int8_t * domain_name, uint8_t ip_version);
72 fill_dnshdr(uint8_t * packet, int8_t * domain_name, uint8_t ip_version);
75 dns_extract_name(uint8_t * dnsh, int8_t * head, int8_t * domain_name);
78 urltohost(char * url, char * host_name);
81 hosttodomain(char * host_name, char * domain_name);
83 /**************************** LOCAL VARIABLES ****************************/
85 static uint8_t ether_packet[ETH_MTU_SIZE];
86 static int32_t dns_server_ip = 0;
87 static uint8_t dns_server_ipv6[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
88 static int32_t dns_result_ip = 0;
89 static uint8_t dns_result_ipv6[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
90 static int8_t dns_error = 0; /**< Stores error code or 0 */
91 static int8_t dns_domain_name[0x100]; /**< Raw domain name */
92 static int8_t dns_domain_cname[0x100]; /**< Canonical domain name */
94 /**************************** IMPLEMENTATION *****************************/
97 * DNS: Initialize the environment for DNS client.
98 * To perfrom DNS-queries use the function dns_get_ip.
100 * @param device_socket a socket number used to send and receive packets
101 * @param server_ip DNS-server IPv4 address (e.g. 127.0.0.1)
102 * @return TRUE in case of successful initialization;
103 * FALSE in case of fault (e.g. can't obtain MAC).
107 dns_init(uint32_t _dns_server_ip, uint8_t _dns_server_ipv6[16], uint8_t ip_version)
110 memcpy(dns_server_ipv6, _dns_server_ipv6, 16);
112 dns_server_ip = _dns_server_ip;
117 * DNS: For given URL retrieves IPv4/IPv6 from DNS-server.
119 * URL can be given in one of the following form: <ul>
120 * <li> scheme with full path with (without) user and password
121 * <br>(e.g. "http://user:pass@www.host.org/url-path");
122 * <li> host name with url-path
123 * <br>(e.g. "www.host.org/url-path");
124 * <li> nothing but host name
125 * <br>(e.g. "www.host.org");
128 * @param fd socket descriptor
129 * @param url the URL to be resolved
130 * @param domain_ip In case of SUCCESS stores extracted IP.
131 * In case of FAULT stores zeros (0.0.0.0).
132 * @return TRUE - IP successfuly retrieved;
133 * FALSE - error condition occurs.
136 dns_get_ip(int fd, char* url, uint8_t * domain_ip, uint8_t ip_version)
138 /* this counter is used so that we abort after 30 DNS request */
140 /* this buffer stores host name retrieved from url */
141 static int8_t host_name[0x100];
145 // Retrieve host name from URL
146 if (!urltohost(url, (char *) host_name)) {
147 printf("\nERROR:\t\t\tBad URL!\n");
151 // Reformat host name into a series of labels
152 if (!hosttodomain((char *) host_name, (char *) dns_domain_name)) {
153 printf("\nERROR:\t\t\tBad host name!\n");
157 // Check if DNS server is presented and accessible
158 if (dns_server_ip == 0) {
159 printf("\nERROR:\t\t\tCan't resolve domain name "
160 "(DNS server is not presented)!\n");
164 // Use DNS-server to obtain IP
166 memset(dns_result_ipv6, 0, 16);
170 strcpy((char *) dns_domain_cname, "");
172 for(i = 0; i < 30; ++i) {
173 // Use canonical name in case we obtained it
174 if (strlen((char *) dns_domain_cname))
175 dns_send_query(fd, dns_domain_cname, ip_version);
177 dns_send_query(fd, dns_domain_name, ip_version);
179 // setting up a timer with a timeout of one seconds
180 set_timer(TICKS_SEC);
184 return 0; // FALSE - error
185 if ((dns_result_ip != 0) && (ip_version == 4)) {
186 memcpy(domain_ip, &dns_result_ip, 4);
187 return 1; // TRUE - success (domain IP retrieved)
189 else if ((dns_result_ipv6[0] != 0) && (ip_version == 6)) {
190 memcpy(domain_ip, dns_result_ipv6, 16);
191 return 1; // TRUE - success (domain IP retrieved)
193 } while (get_timer() > 0);
196 printf("\nGiving up after %d DNS requests\n", i);
197 return 0; // FALSE - domain name wasn't retrieved
201 * DNS: Handles DNS-messages according to Receive-handle diagram.
202 * Sets dns_result_ip for given dns_domain_name (see dns_get_ip)
203 * or signals error condition occurs during DNS-resolving process
204 * by setting dns_error flag.
206 * @param packet DNS-packet to be handled
207 * @param packetsize length of the packet
208 * @return ZERO - packet handled successfully;
209 * NON ZERO - packet was not handled (e.g. bad format)
215 handle_dns(uint8_t * packet, int32_t packetsize)
217 struct dnshdr * dnsh = (struct dnshdr *) packet;
218 uint8_t * resp_section = packet + sizeof(struct dnshdr);
219 /* This string stores domain name from DNS-packets */
220 static int8_t handle_domain_name[0x100];
223 // verify ID - is it response for our query?
224 if (dnsh -> id != htons(0x1234))
227 // Is it DNS response?
228 if ((dnsh -> flags & htons(DNS_FLAG_MSGTYPE)) != htons(DNS_FLAG_SRESPONSE))
231 // Is error condition occurs? (check error field in incoming packet)
232 if ((dnsh -> flags & htons(DNS_FLAG_RCODE)) != DNS_RCODE_NERROR) {
237 /* Pass all (qdcount) records in question section */
239 for (i = 0; i < htons(dnsh -> qdcount); i++) {
241 resp_section = dns_extract_name((uint8_t *) dnsh, (int8_t *) resp_section,
243 if (resp_section == NULL) {
244 return -1; // incorrect domain name (bad packet)
246 // pass QTYPE & QCLASS
250 /* Handle all (ancount) records in answer section */
252 for (i = 0; i < htons(dnsh -> ancount); i++) {
253 // retrieve domain name from the packet
254 resp_section = dns_extract_name((uint8_t *) dnsh, (int8_t *) resp_section,
257 if (resp_section == NULL) {
258 return -1; // incorrect domain name (bad packet)
261 // Check the class of the query (should be IN for Internet)
262 if (* (uint16_t *) (resp_section + 2) == htons(DNS_QCLASS_IN)) {
263 // check if retrieved name fit raw or canonical domain name
264 if (!strcmp((char *) handle_domain_name, (char *) dns_domain_name) ||
265 !strcmp((char *) handle_domain_name, (char *) dns_domain_cname)) {
266 switch (htons(* (uint16_t *) resp_section)) {
270 dns_result_ip = htonl(* (uint32_t *) (resp_section + 10));
271 return 0; // IP successfully obtained
273 case DNS_QTYPE_CNAME :
274 // rdata contains canonical name, store it for further requests
275 if (dns_extract_name((uint8_t *) dnsh, (int8_t *) resp_section + 10,
276 dns_domain_cname) == NULL) {
277 // incorrect domain name (bad packet)
281 case DNS_QTYPE_AAAA :
282 memcpy(dns_result_ipv6, (resp_section + 10), 16);
283 return 0; // IP successfully obtained
287 // continue with next record in answer section
288 resp_section += htons(* (uint16_t *) (resp_section + 8)) + 10;
291 return 0; // Packet successfully handled but IP wasn't obtained
295 * DNS: Sends a standard DNS-query (read request package) to a DNS-server.
296 * DNS-server respones with host IP or signals some error condition.
297 * Responses from the server are handled by handle_dns function.
299 * @param fd socket descriptor
300 * @param domain_name the domain name given as series of labels preceded
301 * with length(label) and terminated with 0
302 * <br>(e.g. "\3,w,w,w,\4,h,o,s,t,\3,o,r,g,\0")
306 dns_send_query(int fd, int8_t * domain_name, uint8_t ip_version)
308 int qry_len = strlen((char *) domain_name) + 5;
309 int iphdr_len = (ip_version == 4) ? sizeof(struct iphdr) : sizeof(struct ip6hdr);
310 ip6_addr_t server_ipv6;
312 uint32_t packetsize = iphdr_len +
313 sizeof(struct udphdr) + sizeof(struct dnshdr) +
316 memset(ether_packet, 0, packetsize);
317 fill_dnshdr(ðer_packet[
318 iphdr_len + sizeof(struct udphdr)],
321 fill_udphdr(ðer_packet[iphdr_len],
322 sizeof(struct dnshdr) +
323 sizeof(struct udphdr) + qry_len,
324 UDPPORT_DNSC, UDPPORT_DNSS);
325 if (ip_version == 4) {
326 fill_iphdr(ether_packet,
327 sizeof(struct dnshdr) + sizeof(struct udphdr) +
329 IPTYPE_UDP, 0, dns_server_ip);
331 memcpy(server_ipv6.addr, dns_server_ipv6, 16);
332 fill_ip6hdr(ether_packet,
333 sizeof(struct dnshdr) + sizeof(struct udphdr) + qry_len,
334 IPTYPE_UDP, get_ipv6_address(),
338 send_ip(fd, ether_packet, packetsize);
342 * DNS: Creates standard DNS-query package. Places DNS-header
343 * and question section in a packet and fills it with
344 * corresponding information.
346 * Use this function with similar functions for other network layers
347 * (fill_udphdr, fill_iphdr, fill_ethhdr).
349 * @param packet Points to the place where ARP-header must be placed.
350 * @param domain_name the domain name given as series of labels preceded
351 * with length(label) and terminated with 0
352 * <br>(e.g. "\3,w,w,w,\4,h,o,s,t,\3,o,r,g,\0")
358 fill_dnshdr(uint8_t * packet, int8_t * domain_name, uint8_t ip_version)
360 struct dnshdr * dnsh = (struct dnshdr *) packet;
361 uint8_t * qry_section = packet + sizeof(struct dnshdr);
363 dnsh -> id = htons(0x1234);
364 dnsh -> flags = htons(DNS_FLAG_SQUERY) | htons(DNS_FLAG_RD);
365 dnsh -> qdcount = htons(1);
367 strcpy((char *) qry_section, (char *) domain_name);
368 qry_section += strlen((char *) domain_name) + 1;
370 // fill QTYPE (ask for IP)
372 * (uint16_t *) qry_section = htons(DNS_QTYPE_A);
374 * (uint16_t *) qry_section = htons(DNS_QTYPE_AAAA);
376 // fill QCLASS (IN is a standard class for Internet)
377 * (uint16_t *) qry_section = htons(DNS_QCLASS_IN);
381 * DNS: Extracts domain name from the question or answer section of
382 * the DNS-message. This function is need to support message
383 * compression requirement (see RFC 1035, paragraph 4.1.4).
385 * @param dnsh Points at the DNS-header.
386 * @param head Points at the beginning of the domain_name
387 * which has to be extracted.
388 * @param domain_name In case of SUCCESS this string stores extracted name.
389 * In case of FAULT this string is empty.
390 * @return NULL in case of FAULT (domain name > 255 octets);
391 * otherwise pointer to the data following the name.
395 dns_extract_name(uint8_t * dnsh, int8_t * head, int8_t * domain_name)
397 int8_t * tail = domain_name;
399 int8_t * next_section = NULL;
402 if ((ptr[0] & 0xC0) == 0xC0) {
403 // message compressed (reference is used)
404 next_section = ptr + 2;
405 ptr = (int8_t *) dnsh + (htons(* (uint16_t *) ptr) & 0x3FFF);
409 // message termination
414 // maximum length for domain name is 255 octets w/o termination sym
415 if (tail - domain_name + ptr[0] + 1 > 255) {
416 strcpy((char *) domain_name, "");
419 memcpy(tail, ptr, ptr[0] + 1);
424 if (next_section == NULL)
427 return (uint8_t *) next_section;
431 * DNS: Parses URL and returns host name.
432 * Input string can be given as: <ul>
433 * <li> scheme with full path with (without) user and password
434 * <br>(e.g. "http://user:pass@www.host.org/url-path");
435 * <li> host name with url-path
436 * <br>(e.g. "www.host.org/url-path");
437 * <li> nothing but host name
438 * <br>(e.g. "www.host.org");
441 * @param url string that stores incoming URL
442 * @param host_name In case of SUCCESS this string stores the host name,
443 * In case of FAULT this string is empty.
444 * @return TRUE - host name retrieved,
445 * FALSE - host name > 255 octets or empty.
448 urltohost(char * url, char * host_name)
453 strcpy(host_name, "");
455 if (strstr(url, "://") != NULL)
456 url = strstr(url, "//") + 2; // URL
458 if (strstr(url, "@") != NULL) // truncate user & password
459 url = strstr(url, "@") + 1;
461 if (strstr(url, "/") != NULL) // truncate url path
462 length1 = strstr(url, "/") - url;
464 length1 = strlen(url);
466 if (strstr(url, ":") != NULL) // truncate port path
467 length2 = strstr(url, ":") - url;
469 length2 = strlen(url);
471 if(length1 > length2)
475 return 0; // string is empty
477 return 0; // host name is too big
479 strncpy(host_name, url, length1);
480 host_name[length1] = 0;
482 return 1; // Host name is retrieved
486 * DNS: Transforms host name string into a series of labels
487 * each of them preceded with length(label). 0 is a terminator.
488 * "www.domain.dom" -> "\3,w,w,w,\6,d,o,m,a,i,n,\3,c,o,m,\0"
490 * This format is used in DNS-messages.
492 * @param host_name incoming string with the host name
493 * @param domain_name resulting string with series of labels
494 * or empty string in case of FAULT
495 * @return TRUE - host name transformed,
496 * FALSE - host name > 255 octets or label > 63 octets.
499 hosttodomain(char * host_name, char * domain_name)
501 char * domain_iter = domain_name;
502 char * host_iter = host_name;
504 strcpy(domain_name, "");
506 if(strlen(host_name) > 255)
507 return 0; // invalid host name (refer to RFC 1035)
509 for(; 1; ++host_iter) {
510 if(*host_iter != '.' && *host_iter != 0)
512 *domain_iter = host_iter - host_name;
513 if (*domain_iter > 63) {
514 strcpy(domain_name, "");
515 return 0; // invalid host name (refer to RFC 1035)
518 strncpy(domain_iter, host_name, host_iter - host_name);
519 domain_iter += (host_iter - host_name);
520 if(*host_iter == 0) {
524 host_name = host_iter + 1;