-
Notifications
You must be signed in to change notification settings - Fork 36
/
uip_dns.c
456 lines (412 loc) · 12.5 KB
/
uip_dns.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* \addtogroup apps
* @{
*/
/**
* \defgroup resolv DNS resolver
* @{
*
* The uIP DNS resolver functions are used to lookup a hostname and
* map it to a numerical IP address. It maintains a list of resolved
* hostnames that can be queried with the uip_dns_lookup()
* function. New hostnames can be resolved using the uip_dns_query()
* function.
*
* When a hostname has been resolved (or found to be non-existant),
* the resolver code calls a callback function called uip_dns_callback()
* that must be implemented by the module that uses the resolver.
*/
/**
* \file
* DNS host name to IP address resolver.
* \author Adam Dunkels <adam@dunkels.com>
*
* This file implements a DNS host name to IP address resolver.
*/
/*
* Copyright (c) 2002-2003, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the uIP TCP/IP stack.
*
* $Id: resolv.c,v 1.5 2006/06/11 21:46:37 adam Exp $
*
*/
#include "uip.h"
#ifdef UIP_DNS
//#ifndef NULL
//#define NULL (void *)0
//#endif /* NULL */
/** \internal The maximum number of retries when asking for a name. */
#define MAX_RETRIES 8
/** \internal The DNS message header. */
struct dns_hdr {
u16_t id;
u8_t flags1, flags2;
#define DNS_FLAG1_RESPONSE 0x80
#define DNS_FLAG1_OPCODE_STATUS 0x10
#define DNS_FLAG1_OPCODE_INVERSE 0x08
#define DNS_FLAG1_OPCODE_STANDARD 0x00
#define DNS_FLAG1_AUTHORATIVE 0x04
#define DNS_FLAG1_TRUNC 0x02
#define DNS_FLAG1_RD 0x01
#define DNS_FLAG2_RA 0x80
#define DNS_FLAG2_ERR_MASK 0x0f
#define DNS_FLAG2_ERR_NONE 0x00
#define DNS_FLAG2_ERR_NAME 0x03
u16_t numquestions;
u16_t numanswers;
u16_t numauthrr;
u16_t numextrarr;
};
/** \internal The DNS answer message structure. */
struct dns_answer {
/* DNS answer record starts with either a domain name or a pointer
to a name already present somewhere in the packet. */
u16_t type;
u16_t class;
u16_t ttl[2];
u16_t len;
uip_ipaddr_t ipaddr;
};
struct namemap {
#define STATE_UNUSED 0
#define STATE_NEW 1
#define STATE_ASKING 2
#define STATE_DONE 3
#define STATE_ERROR 4
u8_t state;
u8_t tmr;
u8_t retries;
u8_t seqno;
u8_t err;
char name[32];
uip_ipaddr_t ipaddr;
};
#ifndef UIP_CONF_DNS_ENTRIES
#define DNS_ENTRIES 4
#else /* UIP_CONF_DNS_ENTRIES */
#define DNS_ENTRIES UIP_CONF_DNS_ENTRIES
#endif /* UIP_CONF_DNS_ENTRIES */
static struct namemap names[DNS_ENTRIES];
static u8_t seqno;
static struct uip_udp_conn *dns_conn = NULL;
/*---------------------------------------------------------------------------*/
/** \internal
* Walk through a compact encoded DNS name and return the end of it.
*
* \return The end of the name.
*/
/*---------------------------------------------------------------------------*/
static unsigned char *
parse_name(unsigned char *query)
{
unsigned char n;
do {
n = *query++;
while(n > 0) {
/* printf("%c", *query);*/
++query;
--n;
};
/* printf(".");*/
} while(*query != 0);
/* printf("\n");*/
return query + 1;
}
/*---------------------------------------------------------------------------*/
/** \internal
* Runs through the list of names to see if there are any that have
* not yet been queried and, if so, sends out a query.
*/
/*---------------------------------------------------------------------------*/
void
uip_dns_run(void)
{
register struct dns_hdr *hdr;
char *query, *nptr, *nameptr;
static u8_t i;
static u8_t n;
register struct namemap *namemapptr;
for(i = 0; i < DNS_ENTRIES; ++i) {
namemapptr = &names[i];
if(namemapptr->state == STATE_NEW ||
namemapptr->state == STATE_ASKING) {
if(namemapptr->state == STATE_ASKING) {
if(--namemapptr->tmr == 0) {
if(++namemapptr->retries == MAX_RETRIES) {
namemapptr->state = STATE_ERROR;
uip_dns_callback(namemapptr->name, NULL);
continue;
}
namemapptr->tmr = namemapptr->retries;
} else {
/* printf("Timer %d\n", namemapptr->tmr);*/
/* Its timer has not run out, so we move on to next
entry. */
continue;
}
} else {
namemapptr->state = STATE_ASKING;
namemapptr->tmr = 1;
namemapptr->retries = 0;
}
hdr = (struct dns_hdr *)uip_appdata;
memset(hdr, 0, sizeof(struct dns_hdr));
hdr->id = htons(i);
hdr->flags1 = DNS_FLAG1_RD;
hdr->numquestions = HTONS(1);
query = (char *)uip_appdata + 12;
nameptr = namemapptr->name;
--nameptr;
/* Convert hostname into suitable query format. */
do {
++nameptr;
nptr = query;
++query;
for(n = 0; *nameptr != '.' && *nameptr != 0; ++nameptr) {
*query = *nameptr;
++query;
++n;
}
*nptr = n;
} while(*nameptr != 0);
{
static unsigned char endquery[] =
{0,0,1,0,1};
memcpy(query, endquery, 5);
}
uip_udp_send((unsigned char)(query + 5 - (char *)uip_appdata));
break;
}
}
}
/*---------------------------------------------------------------------------*/
/** \internal
* Called when new UDP data arrives.
*/
/*---------------------------------------------------------------------------*/
void
uip_dns_newdata(void)
{
char *nameptr;
struct dns_answer *ans;
struct dns_hdr *hdr;
static u8_t nquestions, nanswers;
static u8_t i;
register struct namemap *namemapptr;
hdr = (struct dns_hdr *)uip_appdata;
/* printf("ID %d\n", htons(hdr->id));
printf("Query %d\n", hdr->flags1 & DNS_FLAG1_RESPONSE);
printf("Error %d\n", hdr->flags2 & DNS_FLAG2_ERR_MASK);
printf("Num questions %d, answers %d, authrr %d, extrarr %d\n",
htons(hdr->numquestions),
htons(hdr->numanswers),
htons(hdr->numauthrr),
htons(hdr->numextrarr));
*/
/* The ID in the DNS header should be our entry into the name
table. */
i = htons(hdr->id);
namemapptr = &names[i];
if(i < DNS_ENTRIES &&
namemapptr->state == STATE_ASKING) {
/* This entry is now finished. */
namemapptr->state = STATE_DONE;
namemapptr->err = hdr->flags2 & DNS_FLAG2_ERR_MASK;
/* Check for error. If so, call callback to inform. */
if(namemapptr->err != 0) {
namemapptr->state = STATE_ERROR;
uip_dns_callback(namemapptr->name, NULL);
return;
}
/* We only care about the question(s) and the answers. The authrr
and the extrarr are simply discarded. */
nquestions = htons(hdr->numquestions);
nanswers = htons(hdr->numanswers);
/* Skip the name in the question. XXX: This should really be
checked agains the name in the question, to be sure that they
match. */
nameptr = parse_name((char *)uip_appdata + 12) + 4;
while(nanswers > 0) {
/* The first byte in the answer resource record determines if it
is a compressed record or a normal one. */
if(*nameptr & 0xc0) {
/* Compressed name. */
nameptr +=2;
/* printf("Compressed anwser\n");*/
} else {
/* Not compressed name. */
nameptr = parse_name((char *)nameptr);
}
ans = (struct dns_answer *)nameptr;
/* printf("Answer: type %x, class %x, ttl %x, length %x\n",
htons(ans->type), htons(ans->class), (htons(ans->ttl[0])
<< 16) | htons(ans->ttl[1]), htons(ans->len));*/
/* Check for IP address type and Internet class. Others are
discarded. */
if(ans->type == HTONS(1) &&
ans->class == HTONS(1) &&
ans->len == HTONS(4)) {
/* printf("IP address %d.%d.%d.%d\n",
htons(ans->ipaddr[0]) >> 8,
htons(ans->ipaddr[0]) & 0xff,
htons(ans->ipaddr[1]) >> 8,
htons(ans->ipaddr[1]) & 0xff);*/
/* XXX: we should really check that this IP address is the one
we want. */
namemapptr->ipaddr[0] = ans->ipaddr[0];
namemapptr->ipaddr[1] = ans->ipaddr[1];
uip_dns_callback(namemapptr->name, namemapptr->ipaddr);
return;
} else {
nameptr = nameptr + 10 + htons(ans->len);
}
--nanswers;
}
}
}
/*---------------------------------------------------------------------------*/
void
uip_dns_query(char *name)
{
static u8_t i;
static u8_t lseq, lseqi;
register struct namemap *nameptr;
lseq = lseqi = 0;
for(i = 0; i < DNS_ENTRIES; ++i) {
nameptr = &names[i];
if(nameptr->state == STATE_UNUSED) {
break;
}
if(seqno - nameptr->seqno > lseq) {
lseq = seqno - nameptr->seqno;
lseqi = i;
}
}
if(i == DNS_ENTRIES) {
i = lseqi;
nameptr = &names[i];
}
/* printf("Using entry %d\n", i);*/
strcpy(nameptr->name, name);
nameptr->state = STATE_NEW;
nameptr->seqno = seqno;
++seqno;
}
/*---------------------------------------------------------------------------*/
/**
* Look up a hostname in the array of known hostnames.
*
* \note This function only looks in the internal array of known
* hostnames, it does not send out a query for the hostname if none
* was found. The function uip_dns_query() can be used to send a query
* for a hostname.
*
* \return A pointer to a 4-byte representation of the hostname's IP
* address, or NULL if the hostname was not found in the array of
* hostnames.
*/
/*---------------------------------------------------------------------------*/
u16_t *
uip_dns_lookup(char *name)
{
static u8_t i;
struct namemap *nameptr;
/* Walk through the list to see if the name is in there. If it is
not, we return NULL. */
for(i = 0; i < DNS_ENTRIES; ++i) {
nameptr = &names[i];
if(nameptr->state == STATE_DONE &&
strcmp(name, nameptr->name) == 0) {
return nameptr->ipaddr;
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/
/**
* Obtain the currently configured DNS server.
*
* \return A pointer to a 4-byte representation of the IP address of
* the currently configured DNS server or NULL if no DNS server has
* been configured.
*/
/*---------------------------------------------------------------------------*/
u16_t *
uip_dns_getserver(void)
{
if(dns_conn == NULL) {
return NULL;
}
return dns_conn->ripaddr;
}
/*---------------------------------------------------------------------------*/
/**
* Configure which DNS server to use for queries.
*
* \param dnsserver A pointer to a 4-byte representation of the IP
* address of the DNS server to be configured.
*/
/*---------------------------------------------------------------------------*/
void
uip_dns_conf(u16_t *dnsserver)
{
uip_ipaddr_t addr;
if(dns_conn != NULL) {
uip_udp_remove(dns_conn);
}
uip_ipaddr(&addr, dnsserver[0], dnsserver[1], dnsserver[2], dnsserver[3]);
dns_conn = uip_udp_new(addr, HTONS(53));
}
/*---------------------------------------------------------------------------*/
/**
* Shutdown the DNS resolver to deallocate the UDP socket.
*
/*---------------------------------------------------------------------------*/
void
uip_dns_shutdown()
{
uip_udp_remove(dns_conn);
}
/*---------------------------------------------------------------------------*/
/**
* Initalize the resolver.
*/
/*---------------------------------------------------------------------------*/
void
uip_dns_init(void)
{
static u8_t i;
for(i = 0; i < DNS_ENTRIES; ++i) {
names[i].state = STATE_DONE;
}
}
/*---------------------------------------------------------------------------*/
#endif // UIP_DNS
/** @} */
/** @} */