4 * Copyright (c) 2008 Pieter Voorthuijsen <pieter.voorthuijsen@prodrive.nl>
5 * Copyright (c) 2009 Robin Getz <rgetz@blackfin.uclinux.org>
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.
11 * The packet handling is partly based on TADNS, original copyrights
17 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
19 * "THE BEER-WARE LICENSE" (Revision 42):
20 * Sergey Lyubka wrote this file. As long as you retain this notice you
21 * can do whatever you want with this stuff. If we meet some day, and you think
22 * this stuff is worth it, you can buy me a beer in return.
28 #include <asm/unaligned.h>
32 char *NetDNSResolve; /* The host to resolve */
33 char *NetDNSenvvar; /* The envvar to store the answer in */
35 static int DnsOurPort;
40 struct header *header;
45 enum dns_query_type qtype = DNS_A_RECORD;
48 pkt = p = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE);
50 /* Prepare DNS packet header */
51 header = (struct header *) pkt;
53 header->flags = htons(0x100); /* standard query */
54 header->nqueries = htons(1); /* Just one query */
60 name_len = strlen(name);
61 p = (uchar *) &header->data; /* For encoding host name into packet */
64 s = strchr(name, '.');
68 n = s - name; /* Chunk length */
69 *p++ = n; /* Copy length */
70 memcpy(p, name, n); /* Copy chunk */
80 *p++ = 0; /* Mark end of host name */
81 *p++ = 0; /* Some servers require double null */
82 *p++ = (unsigned char) qtype; /* Query Type */
85 *p++ = 1; /* Class: inet, 0x0001 */
87 n = p - pkt; /* Total packet length */
88 debug("Packet size %d\n", n);
90 DnsOurPort = random_port();
92 NetSendUDPPacket(NetServerEther, NetOurDNSIP, DNS_SERVICE_PORT,
94 debug("DNS packet sent\n");
101 NetState = NETLOOP_FAIL;
105 DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
107 struct header *header;
108 const unsigned char *p, *e, *s;
110 int found, stop, dlen;
115 debug("%s\n", __func__);
116 if (dest != DnsOurPort)
119 for (i = 0; i < len; i += 4)
120 debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
121 pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
123 /* We sent one query. We want to have a single answer: */
124 header = (struct header *) pkt;
125 if (ntohs(header->nqueries) != 1)
128 /* Received 0 answers */
129 if (header->nanswers == 0) {
130 puts("DNS: host not found\n");
131 NetState = NETLOOP_SUCCESS;
136 s = &header->data[0];
138 for (p = s; p < e && *p != '\0'; p++)
141 /* We sent query class 1, query type 1 */
142 if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
143 puts("DNS: response was not an A record\n");
144 NetState = NETLOOP_SUCCESS;
148 /* Go to the first answer section */
151 /* Loop through the answers, we want A type answer */
152 for (found = stop = 0; !stop && &p[12] < e; ) {
154 /* Skip possible name in CNAME answer */
156 while (*p && &p[12] < e)
160 debug("Name (Offset in header): %d\n", p[1]);
162 type = get_unaligned_be16(p+2);
163 debug("type = %d\n", type);
164 if (type == DNS_CNAME_RECORD) {
165 /* CNAME answer. shift to the next section */
166 debug("Found canonical name\n");
167 dlen = get_unaligned_be16(p+10);
168 debug("dlen = %d\n", dlen);
170 } else if (type == DNS_A_RECORD) {
171 debug("Found A-record\n");
174 debug("Unknown type\n");
179 if (found && &p[12] < e) {
181 dlen = get_unaligned_be16(p+10);
183 memcpy(&IPAddress, p, 4);
186 ip_to_string(IPAddress, IPStr);
187 printf("%s\n", IPStr);
189 setenv(NetDNSenvvar, IPStr);
191 puts("server responded with invalid IP number\n");
194 NetState = NETLOOP_SUCCESS;
200 debug("%s\n", __func__);
202 NetSetTimeout(DNS_TIMEOUT, DnsTimeout);
203 NetSetHandler(DnsHandler);