dns.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <net.h>
  26. #include <asm/unaligned.h>
  27. #include "dns.h"
  28. char *net_dns_resolve; /* The host to resolve */
  29. char *net_dns_env_var; /* The envvar to store the answer in */
  30. static int dns_our_port;
  31. static void dns_send(void)
  32. {
  33. struct header *header;
  34. int n, name_len;
  35. uchar *p, *pkt;
  36. const char *s;
  37. const char *name;
  38. enum dns_query_type qtype = DNS_A_RECORD;
  39. name = net_dns_resolve;
  40. pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
  41. p = pkt;
  42. /* Prepare DNS packet header */
  43. header = (struct header *)pkt;
  44. header->tid = 1;
  45. header->flags = htons(0x100); /* standard query */
  46. header->nqueries = htons(1); /* Just one query */
  47. header->nanswers = 0;
  48. header->nauth = 0;
  49. header->nother = 0;
  50. /* Encode DNS name */
  51. name_len = strlen(name);
  52. p = (uchar *)&header->data; /* For encoding host name into packet */
  53. do {
  54. s = strchr(name, '.');
  55. if (!s)
  56. s = name + name_len;
  57. n = s - name; /* Chunk length */
  58. *p++ = n; /* Copy length */
  59. memcpy(p, name, n); /* Copy chunk */
  60. p += n;
  61. if (*s == '.')
  62. n++;
  63. name += n;
  64. name_len -= n;
  65. } while (*s != '\0');
  66. *p++ = 0; /* Mark end of host name */
  67. *p++ = 0; /* Some servers require double null */
  68. *p++ = (unsigned char) qtype; /* Query Type */
  69. *p++ = 0;
  70. *p++ = 1; /* Class: inet, 0x0001 */
  71. n = p - pkt; /* Total packet length */
  72. debug("Packet size %d\n", n);
  73. dns_our_port = random_port();
  74. net_send_udp_packet(net_server_ethaddr, net_dns_server,
  75. DNS_SERVICE_PORT, dns_our_port, n);
  76. debug("DNS packet sent\n");
  77. }
  78. static void dns_timeout_handler(void)
  79. {
  80. puts("Timeout\n");
  81. net_set_state(NETLOOP_FAIL);
  82. }
  83. static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  84. unsigned src, unsigned len)
  85. {
  86. struct header *header;
  87. const unsigned char *p, *e, *s;
  88. u16 type, i;
  89. int found, stop, dlen;
  90. char ip_str[22];
  91. struct in_addr ip_addr;
  92. debug("%s\n", __func__);
  93. if (dest != dns_our_port)
  94. return;
  95. for (i = 0; i < len; i += 4)
  96. debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
  97. pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
  98. /* We sent one query. We want to have a single answer: */
  99. header = (struct header *)pkt;
  100. if (ntohs(header->nqueries) != 1)
  101. return;
  102. /* Received 0 answers */
  103. if (header->nanswers == 0) {
  104. puts("DNS: host not found\n");
  105. net_set_state(NETLOOP_SUCCESS);
  106. return;
  107. }
  108. /* Skip host name */
  109. s = &header->data[0];
  110. e = pkt + len;
  111. for (p = s; p < e && *p != '\0'; p++)
  112. continue;
  113. /* We sent query class 1, query type 1 */
  114. if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
  115. puts("DNS: response was not an A record\n");
  116. net_set_state(NETLOOP_SUCCESS);
  117. return;
  118. }
  119. /* Go to the first answer section */
  120. p += 5;
  121. /* Loop through the answers, we want A type answer */
  122. for (found = stop = 0; !stop && &p[12] < e; ) {
  123. /* Skip possible name in CNAME answer */
  124. if (*p != 0xc0) {
  125. while (*p && &p[12] < e)
  126. p++;
  127. p--;
  128. }
  129. debug("Name (Offset in header): %d\n", p[1]);
  130. type = get_unaligned_be16(p+2);
  131. debug("type = %d\n", type);
  132. if (type == DNS_CNAME_RECORD) {
  133. /* CNAME answer. shift to the next section */
  134. debug("Found canonical name\n");
  135. dlen = get_unaligned_be16(p+10);
  136. debug("dlen = %d\n", dlen);
  137. p += 12 + dlen;
  138. } else if (type == DNS_A_RECORD) {
  139. debug("Found A-record\n");
  140. found = 1;
  141. stop = 1;
  142. } else {
  143. debug("Unknown type\n");
  144. stop = 1;
  145. }
  146. }
  147. if (found && &p[12] < e) {
  148. dlen = get_unaligned_be16(p+10);
  149. p += 12;
  150. memcpy(&ip_addr, p, 4);
  151. if (p + dlen <= e) {
  152. ip_to_string(ip_addr, ip_str);
  153. printf("%s\n", ip_str);
  154. if (net_dns_env_var)
  155. setenv(net_dns_env_var, ip_str);
  156. } else {
  157. puts("server responded with invalid IP number\n");
  158. }
  159. }
  160. net_set_state(NETLOOP_SUCCESS);
  161. }
  162. void dns_start(void)
  163. {
  164. debug("%s\n", __func__);
  165. net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler);
  166. net_set_udp_handler(dns_handler);
  167. /* Clear a previous MAC address, the server IP might have changed. */
  168. memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
  169. dns_send();
  170. }