adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by Adam Dunkels. |
| 16 | * 4. The name of the author may not be used to endorse or promote |
| 17 | * products derived from this software without specific prior |
| 18 | * written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 21 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 26 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * This file is part of the uIP TCP/IP stack. |
| 33 | * |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 34 | * $Id: resolv.c,v 1.4 2003/08/05 13:51:50 adamdunkels Exp $ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 35 | * |
| 36 | */ |
| 37 | |
| 38 | #include "resolv.h" |
| 39 | #include "dispatcher.h" |
| 40 | |
| 41 | #ifndef NULL |
| 42 | #define NULL (void *)0 |
| 43 | #endif /* NULL */ |
| 44 | |
| 45 | #define MAX_RETRIES 8 |
| 46 | |
| 47 | struct dns_hdr { |
| 48 | u16_t id; |
| 49 | u8_t flags1, flags2; |
| 50 | #define DNS_FLAG1_RESPONSE 0x80 |
| 51 | #define DNS_FLAG1_OPCODE_STATUS 0x10 |
| 52 | #define DNS_FLAG1_OPCODE_INVERSE 0x08 |
| 53 | #define DNS_FLAG1_OPCODE_STANDARD 0x00 |
| 54 | #define DNS_FLAG1_AUTHORATIVE 0x04 |
| 55 | #define DNS_FLAG1_TRUNC 0x02 |
| 56 | #define DNS_FLAG1_RD 0x01 |
| 57 | #define DNS_FLAG2_RA 0x80 |
| 58 | #define DNS_FLAG2_ERR_MASK 0x0f |
| 59 | #define DNS_FLAG2_ERR_NONE 0x00 |
| 60 | #define DNS_FLAG2_ERR_NAME 0x03 |
| 61 | u16_t numquestions; |
| 62 | u16_t numanswers; |
| 63 | u16_t numauthrr; |
| 64 | u16_t numextrarr; |
| 65 | }; |
| 66 | |
| 67 | struct dns_answer { |
| 68 | /* DNS answer record starts with either a domain name or a pointer |
| 69 | to a name already present somewhere in the packet. */ |
| 70 | u16_t type; |
| 71 | u16_t class; |
| 72 | u16_t ttl[2]; |
| 73 | u16_t len; |
| 74 | u16_t ipaddr[2]; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | #define STATE_UNUSED 0 |
| 79 | #define STATE_NEW 1 |
| 80 | #define STATE_ASKING 2 |
| 81 | #define STATE_DONE 3 |
| 82 | #define STATE_ERROR 4 |
| 83 | struct namemap { |
| 84 | u8_t state; |
| 85 | u8_t tmr; |
| 86 | u8_t retries; |
| 87 | u8_t seqno; |
| 88 | u8_t err; |
| 89 | char name[64]; |
| 90 | u16_t ipaddr[2]; |
| 91 | }; |
| 92 | |
| 93 | #define RESOLV_ENTRIES 8 |
| 94 | |
| 95 | static struct namemap names[RESOLV_ENTRIES]; |
| 96 | |
| 97 | static u8_t seqno; |
| 98 | |
| 99 | static struct uip_udp_conn *resolv_conn = NULL; |
| 100 | |
| 101 | ek_signal_t resolv_signal_found = EK_SIGNAL_NONE; |
| 102 | |
| 103 | /*-----------------------------------------------------------------------------------*/ |
| 104 | /* parse_name(name): |
| 105 | * |
| 106 | * Returns the end of the name. |
| 107 | */ |
| 108 | static unsigned char * |
| 109 | parse_name(unsigned char *query) |
| 110 | { |
| 111 | unsigned char n; |
| 112 | |
| 113 | do { |
| 114 | n = *query++; |
| 115 | |
| 116 | while(n > 0) { |
| 117 | /* printf("%c", *query);*/ |
| 118 | ++query; |
| 119 | --n; |
| 120 | }; |
| 121 | /* printf(".");*/ |
| 122 | } while(*query != 0); |
| 123 | /* printf("\n");*/ |
| 124 | return query + 1; |
| 125 | } |
| 126 | /*-----------------------------------------------------------------------------------*/ |
| 127 | /* check_entries(void): |
| 128 | * |
| 129 | * Runs through the list of names to see if there are any that have |
| 130 | * not been queried yet. If so, a query is sent out. |
| 131 | */ |
| 132 | static void |
| 133 | check_entries(void) |
| 134 | { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 135 | register struct dns_hdr *hdr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 136 | char *query, *nptr, *nameptr; |
| 137 | u8_t i; |
| 138 | u8_t n; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 139 | register struct namemap *namemapptr; |
| 140 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 141 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 142 | namemapptr = &names[i]; |
| 143 | if(namemapptr->state == STATE_NEW || |
| 144 | namemapptr->state == STATE_ASKING) { |
| 145 | if(namemapptr->state == STATE_ASKING) { |
| 146 | if(--namemapptr->tmr == 0) { |
| 147 | if(++namemapptr->retries == MAX_RETRIES) { |
| 148 | namemapptr->state = STATE_ERROR; |
| 149 | resolv_found(namemapptr->name, NULL); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 150 | continue; |
| 151 | } |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 152 | namemapptr->tmr = namemapptr->retries; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 153 | } else { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 154 | /* printf("Timer %d\n", namemapptr->tmr);*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 155 | /* Its timer has not run out, so we move on to next |
| 156 | entry. */ |
| 157 | continue; |
| 158 | } |
| 159 | } else { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 160 | namemapptr->state = STATE_ASKING; |
| 161 | namemapptr->tmr = 1; |
| 162 | namemapptr->retries = 0; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 163 | } |
| 164 | hdr = (struct dns_hdr *)uip_appdata; |
| 165 | hdr->id = htons(i); |
| 166 | hdr->flags1 = DNS_FLAG1_RD; |
| 167 | hdr->flags2 = 0; |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 168 | hdr->numquestions = HTONS(1); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 169 | hdr->numanswers = hdr->numauthrr = hdr->numextrarr = 0; |
| 170 | query = (char *)uip_appdata + 12; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 171 | nameptr = namemapptr->name; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 172 | --nameptr; |
| 173 | /* Convert hostname into suitable query format. */ |
| 174 | do { |
| 175 | ++nameptr; |
| 176 | nptr = query; |
| 177 | ++query; |
| 178 | for(n = 0; *nameptr != '.' && *nameptr != 0; ++nameptr) { |
| 179 | *query = *nameptr; |
| 180 | ++query; |
| 181 | ++n; |
| 182 | } |
| 183 | *nptr = n; |
| 184 | } while(*nameptr != 0); |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 185 | { |
| 186 | static unsigned char endquery[] = |
| 187 | {0,0,1,0,1}; |
| 188 | memcpy(query, endquery, 5); |
| 189 | } |
| 190 | #if 0 |
| 191 | nptr = query; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 192 | *nptr = 0; /* End of query name. */ |
| 193 | ++nptr; |
| 194 | *nptr = 0; /* High byte of query type. */ |
| 195 | ++nptr; |
| 196 | *nptr = 1; /* Low byte of query type. 1 == IP address query. */ |
| 197 | ++nptr; |
| 198 | *nptr = 0; /* High byte of query class. */ |
| 199 | ++nptr; |
| 200 | *nptr = 1; /* Low byte of query class. */ |
| 201 | ++nptr; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 202 | #endif |
| 203 | uip_udp_send((unsigned char)(query + 5 - (char *)uip_appdata)); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | /*-----------------------------------------------------------------------------------*/ |
| 209 | static void |
| 210 | newdata(void) |
| 211 | { |
| 212 | char *nameptr; |
| 213 | struct dns_answer *ans; |
| 214 | struct dns_hdr *hdr; |
| 215 | u8_t nquestions, nanswers; |
| 216 | u8_t i; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 217 | register struct namemap *namemapptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 218 | |
| 219 | hdr = (struct dns_hdr *)uip_appdata; |
| 220 | /* printf("ID %d\n", htons(hdr->id)); |
| 221 | printf("Query %d\n", hdr->flags1 & DNS_FLAG1_RESPONSE); |
| 222 | printf("Error %d\n", hdr->flags2 & DNS_FLAG2_ERR_MASK); |
| 223 | printf("Num questions %d, answers %d, authrr %d, extrarr %d\n", |
| 224 | htons(hdr->numquestions), |
| 225 | htons(hdr->numanswers), |
| 226 | htons(hdr->numauthrr), |
| 227 | htons(hdr->numextrarr)); |
| 228 | */ |
| 229 | |
| 230 | /* The ID in the DNS header should be our entry into the name |
| 231 | table. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 232 | i = htons(hdr->id); |
| 233 | namemapptr = &names[i]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 234 | if(i < RESOLV_ENTRIES && |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 235 | namemapptr->state == STATE_ASKING) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 236 | |
| 237 | /* This entry is now finished. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 238 | namemapptr->state = STATE_DONE; |
| 239 | namemapptr->err = hdr->flags2 & DNS_FLAG2_ERR_MASK; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 240 | |
| 241 | /* Check for error. If so, call callback to inform. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 242 | if(namemapptr->err != 0) { |
| 243 | namemapptr->state = STATE_ERROR; |
| 244 | resolv_found(namemapptr->name, NULL); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | |
| 248 | /* We only care about the question(s) and the answers. The authrr |
| 249 | and the extrarr are simply discarded. */ |
| 250 | nquestions = htons(hdr->numquestions); |
| 251 | nanswers = htons(hdr->numanswers); |
| 252 | |
| 253 | /* Skip the name in the question. XXX: This should really be |
| 254 | checked agains the name in the question, to be sure that they |
| 255 | match. */ |
| 256 | nameptr = parse_name((char *)uip_appdata + 12) + 4; |
| 257 | |
| 258 | while(nanswers > 0) { |
| 259 | /* The first byte in the answer resource record determines if it |
| 260 | is a compressed record or a normal one. */ |
| 261 | if(*nameptr & 0xc0) { |
| 262 | /* Compressed name. */ |
| 263 | nameptr +=2; |
| 264 | /* printf("Compressed anwser\n");*/ |
| 265 | } else { |
| 266 | /* Not compressed name. */ |
| 267 | nameptr = parse_name((char *)nameptr); |
| 268 | } |
| 269 | |
| 270 | ans = (struct dns_answer *)nameptr; |
| 271 | /* printf("Answer: type %x, class %x, ttl %x, length %x\n", |
| 272 | htons(ans->type), htons(ans->class), |
| 273 | (htons(ans->ttl[0]) << 16) | htons(ans->ttl[1]), |
| 274 | htons(ans->len));*/ |
| 275 | |
| 276 | /* Check for IP address type and Internet class. Others are |
| 277 | discarded. */ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 278 | if(ans->type == HTONS(1) && |
| 279 | ans->class == HTONS(1) && |
| 280 | ans->len == HTONS(4)) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 281 | /* printf("IP address %d.%d.%d.%d\n", |
| 282 | htons(ans->ipaddr[0]) >> 8, |
| 283 | htons(ans->ipaddr[0]) & 0xff, |
| 284 | htons(ans->ipaddr[1]) >> 8, |
| 285 | htons(ans->ipaddr[1]) & 0xff);*/ |
| 286 | /* XXX: we should really check that this IP address is the one |
| 287 | we want. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 288 | namemapptr->ipaddr[0] = ans->ipaddr[0]; |
| 289 | namemapptr->ipaddr[1] = ans->ipaddr[1]; |
| 290 | resolv_found(namemapptr->name, namemapptr->ipaddr); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 291 | return; |
| 292 | } else { |
| 293 | nameptr = nameptr + 10 + htons(ans->len); |
| 294 | } |
| 295 | --nanswers; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | } |
| 300 | /*-----------------------------------------------------------------------------------*/ |
| 301 | /* udp_appcall(): |
| 302 | * |
| 303 | * The main UDP function. |
| 304 | */ |
| 305 | void |
| 306 | udp_appcall(void) |
| 307 | { |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 308 | if(uip_udp_conn->rport == HTONS(53)) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 309 | if(uip_poll()) { |
| 310 | check_entries(); |
| 311 | } |
| 312 | if(uip_newdata()) { |
| 313 | newdata(); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | /*-----------------------------------------------------------------------------------*/ |
| 318 | /* resolv_query(name): |
| 319 | * |
| 320 | * Queues a name so that a question for the name will be sent out the |
| 321 | * next time the udp_appcall is polled. |
| 322 | */ |
| 323 | void |
| 324 | resolv_query(char *name) |
| 325 | { |
| 326 | u8_t i; |
| 327 | u8_t lseq, lseqi; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 328 | register struct namemap *nameptr; |
| 329 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 330 | lseq = lseqi = 0; |
| 331 | |
| 332 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 333 | nameptr = &names[i]; |
| 334 | if(nameptr->state == STATE_UNUSED) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 337 | if(seqno - nameptr->seqno > lseq) { |
| 338 | lseq = seqno - nameptr->seqno; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 339 | lseqi = i; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if(i == RESOLV_ENTRIES) { |
| 344 | i = lseqi; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 345 | nameptr = &names[i]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* printf("Using entry %d\n", i);*/ |
| 349 | |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 350 | strcpy(nameptr->name, name); |
| 351 | nameptr->state = STATE_NEW; |
| 352 | nameptr->seqno = seqno; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 353 | ++seqno; |
| 354 | |
| 355 | } |
| 356 | /*-----------------------------------------------------------------------------------*/ |
| 357 | u16_t * |
| 358 | resolv_lookup(char *name) |
| 359 | { |
| 360 | u8_t i; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 361 | struct namemap *nameptr; |
| 362 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 363 | /* Walk through the list to see if the name is in there. If it is |
| 364 | not, we return NULL. */ |
| 365 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame^] | 366 | nameptr = &names[i]; |
| 367 | if(nameptr->state == STATE_DONE && |
| 368 | strcmp(name, nameptr->name) == 0) { |
| 369 | return nameptr->ipaddr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | return NULL; |
| 373 | } |
| 374 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 66c6af6 | 2003-04-16 18:28:16 +0000 | [diff] [blame] | 375 | u16_t * |
| 376 | resolv_getserver(void) |
| 377 | { |
| 378 | if(resolv_conn == NULL) { |
| 379 | return NULL; |
| 380 | } |
| 381 | return resolv_conn->ripaddr; |
| 382 | } |
| 383 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 384 | void |
| 385 | resolv_conf(u16_t *dnsserver) |
| 386 | { |
| 387 | if(resolv_conn != NULL) { |
| 388 | uip_udp_remove(resolv_conn); |
| 389 | } |
| 390 | |
| 391 | resolv_conn = uip_udp_new(dnsserver, 53); |
| 392 | |
| 393 | } |
| 394 | /*-----------------------------------------------------------------------------------*/ |
| 395 | void |
| 396 | resolv_init(void) |
| 397 | { |
| 398 | u8_t i; |
| 399 | |
| 400 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
| 401 | names[i].state = STATE_DONE; |
| 402 | } |
| 403 | |
| 404 | resolv_signal_found = dispatcher_sigalloc(); |
| 405 | } |
| 406 | /*-----------------------------------------------------------------------------------*/ |
| 407 | void |
| 408 | resolv_found(char *name, u16_t *ipaddr) |
| 409 | { |
| 410 | dispatcher_emit(resolv_signal_found, name, DISPATCHER_BROADCAST); |
| 411 | } |
adamdunkels | 66c6af6 | 2003-04-16 18:28:16 +0000 | [diff] [blame] | 412 | /*-----------------------------------------------------------------------------------*/ |