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