dns.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * DNS support driver
  3. *
  4. * Copyright (c) 2008 Pieter Voorthuijsen <pieter.voorthuijsen@prodrive.nl>
  5. * Copyright (c) 2009 Robin Getz <rgetz@blackfin.uclinux.org>
  6. *
  7. * This is a simple DNS implementation for U-Boot. It will use the first IP
  8. * in the DNS response as net_server_ip. This can then be used for any other
  9. * network related activities.
  10. *
  11. * The packet handling is partly based on TADNS, original copyrights
  12. * follow below.
  13. *
  14. */
  15. /*
  16. * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
  17. *
  18. * "THE BEER-WARE LICENSE" (Revision 42):
  19. * Sergey Lyubka wrote this file. As long as you retain this notice you
  20. * can do whatever you want with this stuff. If we meet some day, and you think
  21. * this stuff is worth it, you can buy me a beer in return.
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <env.h>
  26. #include <net.h>
  27. #include <asm/unaligned.h>
  28. #include "dns.h"
  29. char *net_dns_resolve; /* The host to resolve */
  30. char *net_dns_env_var; /* The envvar to store the answer in */
  31. static int dns_our_port;
  32. static void dns_send(void)
  33. {
  34. struct header *header;
  35. int n, name_len;
  36. uchar *p, *pkt;
  37. const char *s;
  38. const char *name;
  39. enum dns_query_type qtype = DNS_A_RECORD;
  40. name = net_dns_resolve;
  41. pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
  42. p = pkt;
  43. /* Prepare DNS packet header */
  44. header = (struct header *)pkt;
  45. header->tid = 1;
  46. header->flags = htons(0x100); /* standard query */
  47. header->nqueries = htons(1); /* Just one query */
  48. header->nanswers = 0;
  49. header->nauth = 0;
  50. header->nother = 0;
  51. /* Encode DNS name */
  52. name_len = strlen(name);
  53. p = (uchar *)&header->data; /* For encoding host name into packet */
  54. do {
  55. s = strchr(name, '.');
  56. if (!s)
  57. s = name + name_len;
  58. n = s - name; /* Chunk length */
  59. *p++ = n; /* Copy length */
  60. memcpy(p, name, n); /* Copy chunk */
  61. p += n;
  62. if (*s == '.')
  63. n++;
  64. name += n;
  65. name_len -= n;
  66. } while (*s != '\0');
  67. *p++ = 0; /* Mark end of host name */
  68. *p++ = 0; /* Some servers require double null */
  69. *p++ = (unsigned char) qtype; /* Query Type */
  70. *p++ = 0;
  71. *p++ = 1; /* Class: inet, 0x0001 */
  72. n = p - pkt; /* Total packet length */
  73. debug("Packet size %d\n", n);
  74. dns_our_port = random_port();
  75. net_send_udp_packet(net_server_ethaddr, net_dns_server,
  76. DNS_SERVICE_PORT, dns_our_port, n);
  77. debug("DNS packet sent\n");
  78. }
  79. static void dns_timeout_handler(void)
  80. {
  81. puts("Timeout\n");
  82. net_set_state(NETLOOP_FAIL);
  83. }
  84. static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  85. unsigned src, unsigned len)
  86. {
  87. struct header *header;
  88. const unsigned char *p, *e, *s;
  89. u16 type, i;
  90. int found, stop, dlen;
  91. char ip_str[22];
  92. struct in_addr ip_addr;
  93. debug("%s\n", __func__);
  94. if (dest != dns_our_port)
  95. return;
  96. for (i = 0; i < len; i += 4)
  97. debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
  98. pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
  99. /* We sent one query. We want to have a single answer: */
  100. header = (struct header *)pkt;
  101. if (ntohs(header->nqueries) != 1)
  102. return;
  103. /* Received 0 answers */
  104. if (header->nanswers == 0) {
  105. puts("DNS: host not found\n");
  106. net_set_state(NETLOOP_SUCCESS);
  107. return;
  108. }
  109. /* Skip host name */
  110. s = &header->data[0];
  111. e = pkt + len;
  112. for (p = s; p < e && *p != '\0'; p++)
  113. continue;
  114. /* We sent query class 1, query type 1 */
  115. if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
  116. puts("DNS: response was not an A record\n");
  117. net_set_state(NETLOOP_SUCCESS);
  118. return;
  119. }
  120. /* Go to the first answer section */
  121. p += 5;
  122. /* Loop through the answers, we want A type answer */
  123. for (found = stop = 0; !stop && &p[12] < e; ) {
  124. /* Skip possible name in CNAME answer */
  125. if (*p != 0xc0) {
  126. while (*p && &p[12] < e)
  127. p++;
  128. p--;
  129. }
  130. debug("Name (Offset in header): %d\n", p[1]);
  131. type = get_unaligned_be16(p+2);
  132. debug("type = %d\n", type);
  133. if (type == DNS_CNAME_RECORD) {
  134. /* CNAME answer. shift to the next section */
  135. debug("Found canonical name\n");
  136. dlen = get_unaligned_be16(p+10);
  137. debug("dlen = %d\n", dlen);
  138. p += 12 + dlen;
  139. } else if (type == DNS_A_RECORD) {
  140. debug("Found A-record\n");
  141. found = 1;
  142. stop = 1;
  143. } else {
  144. debug("Unknown type\n");
  145. stop = 1;
  146. }
  147. }
  148. if (found && &p[12] < e) {
  149. dlen = get_unaligned_be16(p+10);
  150. p += 12;
  151. memcpy(&ip_addr, p, 4);
  152. if (p + dlen <= e) {
  153. ip_to_string(ip_addr, ip_str);
  154. printf("%s\n", ip_str);
  155. if (net_dns_env_var)
  156. env_set(net_dns_env_var, ip_str);
  157. } else {
  158. puts("server responded with invalid IP number\n");
  159. }
  160. }
  161. net_set_state(NETLOOP_SUCCESS);
  162. }
  163. void dns_start(void)
  164. {
  165. debug("%s\n", __func__);
  166. net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler);
  167. net_set_udp_handler(dns_handler);
  168. /* Clear a previous MAC address, the server IP might have changed. */
  169. memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
  170. dns_send();
  171. }