00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include "resolv.h"
00064 #include "uip.h"
00065
00066 #include <string.h>
00067
00068 #ifndef NULL
00069 #define NULL (void *)0
00070 #endif
00071
00072
00073 #define MAX_RETRIES 8
00074
00075
00076 struct dns_hdr {
00077 u16_t id;
00078 u8_t flags1, flags2;
00079 #define DNS_FLAG1_RESPONSE 0x80
00080 #define DNS_FLAG1_OPCODE_STATUS 0x10
00081 #define DNS_FLAG1_OPCODE_INVERSE 0x08
00082 #define DNS_FLAG1_OPCODE_STANDARD 0x00
00083 #define DNS_FLAG1_AUTHORATIVE 0x04
00084 #define DNS_FLAG1_TRUNC 0x02
00085 #define DNS_FLAG1_RD 0x01
00086 #define DNS_FLAG2_RA 0x80
00087 #define DNS_FLAG2_ERR_MASK 0x0f
00088 #define DNS_FLAG2_ERR_NONE 0x00
00089 #define DNS_FLAG2_ERR_NAME 0x03
00090 u16_t numquestions;
00091 u16_t numanswers;
00092 u16_t numauthrr;
00093 u16_t numextrarr;
00094 };
00095
00096
00097 struct dns_answer {
00098
00099
00100 u16_t type;
00101 u16_t class;
00102 u16_t ttl[2];
00103 u16_t len;
00104 uip_ipaddr_t ipaddr;
00105 };
00106
00107 struct namemap {
00108 #define STATE_UNUSED 0
00109 #define STATE_NEW 1
00110 #define STATE_ASKING 2
00111 #define STATE_DONE 3
00112 #define STATE_ERROR 4
00113 u8_t state;
00114 u8_t tmr;
00115 u8_t retries;
00116 u8_t seqno;
00117 u8_t err;
00118 char name[32];
00119 uip_ipaddr_t ipaddr;
00120 };
00121
00122 #ifndef UIP_CONF_RESOLV_ENTRIES
00123 #define RESOLV_ENTRIES 4
00124 #else
00125 #define RESOLV_ENTRIES UIP_CONF_RESOLV_ENTRIES
00126 #endif
00127
00128
00129 static struct namemap names[RESOLV_ENTRIES];
00130
00131 static u8_t seqno;
00132
00133 static struct uip_udp_conn *resolv_conn = NULL;
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 static unsigned char *
00144 parse_name(unsigned char *query)
00145 {
00146 unsigned char n;
00147
00148 do {
00149 n = *query++;
00150
00151 while(n > 0) {
00152
00153 ++query;
00154 --n;
00155 };
00156
00157 } while(*query != 0);
00158
00159 return query + 1;
00160 }
00161
00162
00163
00164
00165
00166
00167 static void
00168 check_entries(void)
00169 {
00170 register struct dns_hdr *hdr;
00171 char *query, *nptr, *nameptr;
00172 static u8_t i;
00173 static u8_t n;
00174 register struct namemap *namemapptr;
00175
00176 for(i = 0; i < RESOLV_ENTRIES; ++i) {
00177 namemapptr = &names[i];
00178 if(namemapptr->state == STATE_NEW ||
00179 namemapptr->state == STATE_ASKING) {
00180 if(namemapptr->state == STATE_ASKING) {
00181 if(--namemapptr->tmr == 0) {
00182 if(++namemapptr->retries == MAX_RETRIES) {
00183 namemapptr->state = STATE_ERROR;
00184 resolv_found(namemapptr->name, NULL);
00185 continue;
00186 }
00187 namemapptr->tmr = namemapptr->retries;
00188 } else {
00189
00190
00191
00192 continue;
00193 }
00194 } else {
00195 namemapptr->state = STATE_ASKING;
00196 namemapptr->tmr = 1;
00197 namemapptr->retries = 0;
00198 }
00199 hdr = (struct dns_hdr *)uip_appdata;
00200 memset(hdr, 0, sizeof(struct dns_hdr));
00201 hdr->id = htons(i);
00202 hdr->flags1 = DNS_FLAG1_RD;
00203 hdr->numquestions = HTONS(1);
00204 query = (char *)uip_appdata + 12;
00205 nameptr = namemapptr->name;
00206 --nameptr;
00207
00208 do {
00209 ++nameptr;
00210 nptr = query;
00211 ++query;
00212 for(n = 0; *nameptr != '.' && *nameptr != 0; ++nameptr) {
00213 *query = *nameptr;
00214 ++query;
00215 ++n;
00216 }
00217 *nptr = n;
00218 } while(*nameptr != 0);
00219 {
00220 static unsigned char endquery[] =
00221 {0,0,1,0,1};
00222 memcpy(query, endquery, 5);
00223 }
00224 uip_udp_send((unsigned char)(query + 5 - (char *)uip_appdata));
00225 break;
00226 }
00227 }
00228 }
00229
00230
00231
00232
00233
00234 static void
00235 newdata(void)
00236 {
00237 char *nameptr;
00238 struct dns_answer *ans;
00239 struct dns_hdr *hdr;
00240 static u8_t nquestions, nanswers;
00241 static u8_t i;
00242 register struct namemap *namemapptr;
00243
00244 hdr = (struct dns_hdr *)uip_appdata;
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 i = htons(hdr->id);
00258 namemapptr = &names[i];
00259 if(i < RESOLV_ENTRIES &&
00260 namemapptr->state == STATE_ASKING) {
00261
00262
00263 namemapptr->state = STATE_DONE;
00264 namemapptr->err = hdr->flags2 & DNS_FLAG2_ERR_MASK;
00265
00266
00267 if(namemapptr->err != 0) {
00268 namemapptr->state = STATE_ERROR;
00269 resolv_found(namemapptr->name, NULL);
00270 return;
00271 }
00272
00273
00274
00275 nquestions = htons(hdr->numquestions);
00276 nanswers = htons(hdr->numanswers);
00277
00278
00279
00280
00281 nameptr = parse_name((char *)uip_appdata + 12) + 4;
00282
00283 while(nanswers > 0) {
00284
00285
00286 if(*nameptr & 0xc0) {
00287
00288 nameptr +=2;
00289
00290 } else {
00291
00292 nameptr = parse_name((char *)nameptr);
00293 }
00294
00295 ans = (struct dns_answer *)nameptr;
00296
00297
00298
00299
00300
00301
00302 if(ans->type == HTONS(1) &&
00303 ans->class == HTONS(1) &&
00304 ans->len == HTONS(4)) {
00305
00306
00307
00308
00309
00310
00311
00312 namemapptr->ipaddr[0] = ans->ipaddr[0];
00313 namemapptr->ipaddr[1] = ans->ipaddr[1];
00314
00315 resolv_found(namemapptr->name, namemapptr->ipaddr);
00316 return;
00317 } else {
00318 nameptr = nameptr + 10 + htons(ans->len);
00319 }
00320 --nanswers;
00321 }
00322 }
00323
00324 }
00325
00326
00327
00328
00329
00330 void
00331 resolv_appcall(void)
00332 {
00333 if(uip_udp_conn->rport == HTONS(53)) {
00334 if(uip_poll()) {
00335 check_entries();
00336 }
00337 if(uip_newdata()) {
00338 newdata();
00339 }
00340 }
00341 }
00342
00343
00344
00345
00346
00347
00348
00349 void
00350 resolv_query(char *name)
00351 {
00352 static u8_t i;
00353 static u8_t lseq, lseqi;
00354 register struct namemap *nameptr;
00355
00356 lseq = lseqi = 0;
00357
00358 for(i = 0; i < RESOLV_ENTRIES; ++i) {
00359 nameptr = &names[i];
00360 if(nameptr->state == STATE_UNUSED) {
00361 break;
00362 }
00363 if(seqno - nameptr->seqno > lseq) {
00364 lseq = seqno - nameptr->seqno;
00365 lseqi = i;
00366 }
00367 }
00368
00369 if(i == RESOLV_ENTRIES) {
00370 i = lseqi;
00371 nameptr = &names[i];
00372 }
00373
00374
00375
00376 strcpy(nameptr->name, name);
00377 nameptr->state = STATE_NEW;
00378 nameptr->seqno = seqno;
00379 ++seqno;
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 u16_t *
00396 resolv_lookup(char *name)
00397 {
00398 static u8_t i;
00399 struct namemap *nameptr;
00400
00401
00402
00403 for(i = 0; i < RESOLV_ENTRIES; ++i) {
00404 nameptr = &names[i];
00405 if(nameptr->state == STATE_DONE &&
00406 strcmp(name, nameptr->name) == 0) {
00407 return nameptr->ipaddr;
00408 }
00409 }
00410 return NULL;
00411 }
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 u16_t *
00422 resolv_getserver(void)
00423 {
00424 if(resolv_conn == NULL) {
00425 return NULL;
00426 }
00427 return resolv_conn->ripaddr;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437 void
00438 resolv_conf(u16_t *dnsserver)
00439 {
00440 if(resolv_conn != NULL) {
00441 uip_udp_remove(resolv_conn);
00442 }
00443
00444 resolv_conn = uip_udp_new(dnsserver, HTONS(53));
00445 }
00446
00447
00448
00449
00450
00451 void
00452 resolv_init(void)
00453 {
00454 static u8_t i;
00455
00456 for(i = 0; i < RESOLV_ENTRIES; ++i) {
00457 names[i].state = STATE_DONE;
00458 }
00459
00460 }
00461
00462
00463
00464