1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <command.h>
27#include <env.h>
28#include <log.h>
29#include <net.h>
30#include <asm/unaligned.h>
31
32#include "dns.h"
33
34char *net_dns_resolve;
35char *net_dns_env_var;
36
37static int dns_our_port;
38
39
40
41
42
43
44static unsigned int random_port(void)
45{
46 return 1024 + (get_timer(0) % 0x4000);
47}
48
49static void dns_send(void)
50{
51 struct header *header;
52 int n, name_len;
53 uchar *p, *pkt;
54 const char *s;
55 const char *name;
56 enum dns_query_type qtype = DNS_A_RECORD;
57
58 name = net_dns_resolve;
59 pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
60 p = pkt;
61
62
63 header = (struct header *)pkt;
64 header->tid = 1;
65 header->flags = htons(0x100);
66 header->nqueries = htons(1);
67 header->nanswers = 0;
68 header->nauth = 0;
69 header->nother = 0;
70
71
72 name_len = strlen(name);
73 p = (uchar *)&header->data;
74
75 do {
76 s = strchr(name, '.');
77 if (!s)
78 s = name + name_len;
79
80 n = s - name;
81 *p++ = n;
82 memcpy(p, name, n);
83 p += n;
84
85 if (*s == '.')
86 n++;
87
88 name += n;
89 name_len -= n;
90 } while (*s != '\0');
91
92 *p++ = 0;
93 *p++ = 0;
94 *p++ = (unsigned char) qtype;
95
96 *p++ = 0;
97 *p++ = 1;
98
99 n = p - pkt;
100 debug("Packet size %d\n", n);
101
102 dns_our_port = random_port();
103
104 net_send_udp_packet(net_server_ethaddr, net_dns_server,
105 DNS_SERVICE_PORT, dns_our_port, n);
106 debug("DNS packet sent\n");
107}
108
109static void dns_timeout_handler(void)
110{
111 puts("Timeout\n");
112 net_set_state(NETLOOP_FAIL);
113}
114
115static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
116 unsigned src, unsigned len)
117{
118 struct header *header;
119 const unsigned char *p, *e, *s;
120 u16 type, i;
121 int found, stop, dlen;
122 char ip_str[22];
123 struct in_addr ip_addr;
124
125
126 debug("%s\n", __func__);
127 if (dest != dns_our_port)
128 return;
129
130 for (i = 0; i < len; i += 4)
131 debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
132 pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
133
134
135 header = (struct header *)pkt;
136 if (ntohs(header->nqueries) != 1)
137 return;
138
139
140 if (header->nanswers == 0) {
141 puts("DNS: host not found\n");
142 net_set_state(NETLOOP_SUCCESS);
143 return;
144 }
145
146
147 s = &header->data[0];
148 e = pkt + len;
149 for (p = s; p < e && *p != '\0'; p++)
150 continue;
151
152
153 if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
154 puts("DNS: response was not an A record\n");
155 net_set_state(NETLOOP_SUCCESS);
156 return;
157 }
158
159
160 p += 5;
161
162
163 for (found = stop = 0; !stop && &p[12] < e; ) {
164
165 if (*p != 0xc0) {
166 while (*p && &p[12] < e)
167 p++;
168 p--;
169 }
170 debug("Name (Offset in header): %d\n", p[1]);
171
172 type = get_unaligned_be16(p+2);
173 debug("type = %d\n", type);
174 if (type == DNS_CNAME_RECORD) {
175
176 debug("Found canonical name\n");
177 dlen = get_unaligned_be16(p+10);
178 debug("dlen = %d\n", dlen);
179 p += 12 + dlen;
180 } else if (type == DNS_A_RECORD) {
181 debug("Found A-record\n");
182 found = 1;
183 stop = 1;
184 } else {
185 debug("Unknown type\n");
186 stop = 1;
187 }
188 }
189
190 if (found && &p[12] < e) {
191 dlen = get_unaligned_be16(p+10);
192 p += 12;
193 memcpy(&ip_addr, p, 4);
194
195 if (p + dlen <= e) {
196 ip_to_string(ip_addr, ip_str);
197 printf("%s\n", ip_str);
198 if (net_dns_env_var)
199 env_set(net_dns_env_var, ip_str);
200 } else {
201 puts("server responded with invalid IP number\n");
202 }
203 }
204
205 net_set_state(NETLOOP_SUCCESS);
206}
207
208void dns_start(void)
209{
210 debug("%s\n", __func__);
211
212 net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler);
213 net_set_udp_handler(dns_handler);
214
215
216 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
217
218 dns_send();
219}
220