]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - net/dns.c
net: cosmetic: Clean up CDP variables and functions
[karo-tx-uboot.git] / net / dns.c
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 /*
17  * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
18  *
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.
23  */
24
25 #include <common.h>
26 #include <command.h>
27 #include <net.h>
28 #include <asm/unaligned.h>
29
30 #include "dns.h"
31
32 char *NetDNSResolve;    /* The host to resolve  */
33 char *NetDNSenvvar;     /* The envvar to store the answer in */
34
35 static int DnsOurPort;
36
37 static void
38 DnsSend(void)
39 {
40         struct header *header;
41         int n, name_len;
42         uchar *p, *pkt;
43         const char *s;
44         const char *name;
45         enum dns_query_type qtype = DNS_A_RECORD;
46
47         name = NetDNSResolve;
48         pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
49         p = pkt;
50
51         /* Prepare DNS packet header */
52         header           = (struct header *) pkt;
53         header->tid      = 1;
54         header->flags    = htons(0x100);        /* standard query */
55         header->nqueries = htons(1);            /* Just one query */
56         header->nanswers = 0;
57         header->nauth    = 0;
58         header->nother   = 0;
59
60         /* Encode DNS name */
61         name_len = strlen(name);
62         p = (uchar *) &header->data;    /* For encoding host name into packet */
63
64         do {
65                 s = strchr(name, '.');
66                 if (!s)
67                         s = name + name_len;
68
69                 n = s - name;                   /* Chunk length */
70                 *p++ = n;                       /* Copy length  */
71                 memcpy(p, name, n);             /* Copy chunk   */
72                 p += n;
73
74                 if (*s == '.')
75                         n++;
76
77                 name += n;
78                 name_len -= n;
79         } while (*s != '\0');
80
81         *p++ = 0;                       /* Mark end of host name */
82         *p++ = 0;                       /* Some servers require double null */
83         *p++ = (unsigned char) qtype;   /* Query Type */
84
85         *p++ = 0;
86         *p++ = 1;                               /* Class: inet, 0x0001 */
87
88         n = p - pkt;                            /* Total packet length */
89         debug("Packet size %d\n", n);
90
91         DnsOurPort = random_port();
92
93         net_send_udp_packet(net_server_ethaddr, net_dns_server,
94                             DNS_SERVICE_PORT, DnsOurPort, n);
95         debug("DNS packet sent\n");
96 }
97
98 static void
99 DnsTimeout(void)
100 {
101         puts("Timeout\n");
102         net_set_state(NETLOOP_FAIL);
103 }
104
105 static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
106                         unsigned src, unsigned len)
107 {
108         struct header *header;
109         const unsigned char *p, *e, *s;
110         u16 type, i;
111         int found, stop, dlen;
112         char IPStr[22];
113         struct in_addr ip_addr;
114
115
116         debug("%s\n", __func__);
117         if (dest != DnsOurPort)
118                 return;
119
120         for (i = 0; i < len; i += 4)
121                 debug("0x%p - 0x%.2x  0x%.2x  0x%.2x  0x%.2x\n",
122                         pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
123
124         /* We sent one query. We want to have a single answer: */
125         header = (struct header *) pkt;
126         if (ntohs(header->nqueries) != 1)
127                 return;
128
129         /* Received 0 answers */
130         if (header->nanswers == 0) {
131                 puts("DNS: host not found\n");
132                 net_set_state(NETLOOP_SUCCESS);
133                 return;
134         }
135
136         /* Skip host name */
137         s = &header->data[0];
138         e = pkt + len;
139         for (p = s; p < e && *p != '\0'; p++)
140                 continue;
141
142         /* We sent query class 1, query type 1 */
143         if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
144                 puts("DNS: response was not an A record\n");
145                 net_set_state(NETLOOP_SUCCESS);
146                 return;
147         }
148
149         /* Go to the first answer section */
150         p += 5;
151
152         /* Loop through the answers, we want A type answer */
153         for (found = stop = 0; !stop && &p[12] < e; ) {
154
155                 /* Skip possible name in CNAME answer */
156                 if (*p != 0xc0) {
157                         while (*p && &p[12] < e)
158                                 p++;
159                         p--;
160                 }
161                 debug("Name (Offset in header): %d\n", p[1]);
162
163                 type = get_unaligned_be16(p+2);
164                 debug("type = %d\n", type);
165                 if (type == DNS_CNAME_RECORD) {
166                         /* CNAME answer. shift to the next section */
167                         debug("Found canonical name\n");
168                         dlen = get_unaligned_be16(p+10);
169                         debug("dlen = %d\n", dlen);
170                         p += 12 + dlen;
171                 } else if (type == DNS_A_RECORD) {
172                         debug("Found A-record\n");
173                         found = stop = 1;
174                 } else {
175                         debug("Unknown type\n");
176                         stop = 1;
177                 }
178         }
179
180         if (found && &p[12] < e) {
181
182                 dlen = get_unaligned_be16(p+10);
183                 p += 12;
184                 memcpy(&ip_addr, p, 4);
185
186                 if (p + dlen <= e) {
187                         ip_to_string(ip_addr, IPStr);
188                         printf("%s\n", IPStr);
189                         if (NetDNSenvvar)
190                                 setenv(NetDNSenvvar, IPStr);
191                 } else
192                         puts("server responded with invalid IP number\n");
193         }
194
195         net_set_state(NETLOOP_SUCCESS);
196 }
197
198 void
199 DnsStart(void)
200 {
201         debug("%s\n", __func__);
202
203         NetSetTimeout(DNS_TIMEOUT, DnsTimeout);
204         net_set_udp_handler(dns_handler);
205
206         /* Clear a previous MAC address, the server IP might have changed. */
207         memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
208
209         DnsSend();
210 }