dns.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 NetServerIP. 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 "dns.h"
  27. char *NetDNSResolve; /* The host to resolve */
  28. char *NetDNSenvvar; /* The envvar to store the answer in */
  29. static int DnsOurPort;
  30. static void
  31. DnsSend(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 = NetDNSResolve;
  40. pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE);
  41. /* Prepare DNS packet header */
  42. header = (struct header *) pkt;
  43. header->tid = 1;
  44. header->flags = htons(0x100); /* standard query */
  45. header->nqueries = htons(1); /* Just one query */
  46. header->nanswers = 0;
  47. header->nauth = 0;
  48. header->nother = 0;
  49. /* Encode DNS name */
  50. name_len = strlen(name);
  51. p = (uchar *) &header->data; /* For encoding host name into packet */
  52. do {
  53. s = strchr(name, '.');
  54. if (!s)
  55. s = name + name_len;
  56. n = s - name; /* Chunk length */
  57. *p++ = n; /* Copy length */
  58. memcpy(p, name, n); /* Copy chunk */
  59. p += n;
  60. if (*s == '.')
  61. n++;
  62. name += n;
  63. name_len -= n;
  64. } while (*s != '\0');
  65. *p++ = 0; /* Mark end of host name */
  66. *p++ = 0; /* Some servers require double null */
  67. *p++ = (unsigned char) qtype; /* Query Type */
  68. *p++ = 0;
  69. *p++ = 1; /* Class: inet, 0x0001 */
  70. n = p - pkt; /* Total packet length */
  71. debug("Packet size %d\n", n);
  72. DnsOurPort = random_port();
  73. NetSendUDPPacket(NetServerEther, NetOurDNSIP, DNS_SERVICE_PORT,
  74. DnsOurPort, n);
  75. debug("DNS packet sent\n");
  76. }
  77. static void
  78. DnsTimeout(void)
  79. {
  80. puts("Timeout\n");
  81. NetState = NETLOOP_FAIL;
  82. }
  83. static void
  84. DnsHandler(uchar *pkt, unsigned dest, 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 IPStr[22];
  91. IPaddr_t IPAddress;
  92. short tmp;
  93. debug("%s\n", __func__);
  94. if (dest != DnsOurPort)
  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 1 query. We want to see more that 1 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 server returned no answers\n");
  106. NetState = 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. tmp = p[1] | (p[2] << 8);
  116. if (&p[5] > e || ntohs(tmp) != DNS_A_RECORD) {
  117. puts("DNS response was not A record\n");
  118. NetState = NETLOOP_SUCCESS;
  119. return;
  120. }
  121. /* Go to the first answer section */
  122. p += 5;
  123. /* Loop through the answers, we want A type answer */
  124. for (found = stop = 0; !stop && &p[12] < e; ) {
  125. /* Skip possible name in CNAME answer */
  126. if (*p != 0xc0) {
  127. while (*p && &p[12] < e)
  128. p++;
  129. p--;
  130. }
  131. debug("Name (Offset in header): %d\n", p[1]);
  132. tmp = p[2] | (p[3] << 8);
  133. type = ntohs(tmp);
  134. debug("type = %d\n", type);
  135. if (type == DNS_CNAME_RECORD) {
  136. /* CNAME answer. shift to the next section */
  137. debug("Found canonical name\n");
  138. tmp = p[10] | (p[11] << 8);
  139. dlen = ntohs(tmp);
  140. debug("dlen = %d\n", dlen);
  141. p += 12 + dlen;
  142. } else if (type == DNS_A_RECORD) {
  143. debug("Found A-record\n");
  144. found = stop = 1;
  145. } else {
  146. debug("Unknown type\n");
  147. stop = 1;
  148. }
  149. }
  150. if (found && &p[12] < e) {
  151. tmp = p[10] | (p[11] << 8);
  152. dlen = ntohs(tmp);
  153. p += 12;
  154. memcpy(&IPAddress, p, 4);
  155. if (p + dlen <= e) {
  156. ip_to_string(IPAddress, IPStr);
  157. printf("%s\n", IPStr);
  158. if (NetDNSenvvar)
  159. setenv(NetDNSenvvar, IPStr);
  160. } else
  161. puts("server responded with invalid IP number\n");
  162. }
  163. NetState = NETLOOP_SUCCESS;
  164. }
  165. void
  166. DnsStart(void)
  167. {
  168. debug("%s\n", __func__);
  169. NetSetTimeout(DNS_TIMEOUT, DnsTimeout);
  170. NetSetHandler(DnsHandler);
  171. DnsSend();
  172. }