dns.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <asm/unaligned.h>
  27. #include "dns.h"
  28. char *NetDNSResolve; /* The host to resolve */
  29. char *NetDNSenvvar; /* The envvar to store the answer in */
  30. static int DnsOurPort;
  31. static void
  32. DnsSend(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 = NetDNSResolve;
  41. pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE);
  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. DnsOurPort = random_port();
  74. NetSendUDPPacket(NetServerEther, NetOurDNSIP, DNS_SERVICE_PORT,
  75. DnsOurPort, n);
  76. debug("DNS packet sent\n");
  77. }
  78. static void
  79. DnsTimeout(void)
  80. {
  81. puts("Timeout\n");
  82. net_set_state(NETLOOP_FAIL);
  83. }
  84. static void
  85. DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, 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 IPStr[22];
  92. IPaddr_t IPAddress;
  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 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 = 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(&IPAddress, p, 4);
  151. if (p + dlen <= e) {
  152. ip_to_string(IPAddress, IPStr);
  153. printf("%s\n", IPStr);
  154. if (NetDNSenvvar)
  155. setenv(NetDNSenvvar, IPStr);
  156. } else
  157. puts("server responded with invalid IP number\n");
  158. }
  159. net_set_state(NETLOOP_SUCCESS);
  160. }
  161. void
  162. DnsStart(void)
  163. {
  164. debug("%s\n", __func__);
  165. NetSetTimeout(DNS_TIMEOUT, DnsTimeout);
  166. net_set_udp_handler(DnsHandler);
  167. DnsSend();
  168. }