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