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 | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame^] | 61 | * $Id: resolv.c,v 1.9 2003/10/01 11:25:37 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 | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 138 | |
| 139 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 140 | /** \internal |
| 141 | * Walk through a compact encoded DNS name and return the end of it. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 142 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 143 | * \return The end of the name. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 144 | */ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 145 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 146 | static unsigned char * |
| 147 | parse_name(unsigned char *query) |
| 148 | { |
| 149 | unsigned char n; |
| 150 | |
| 151 | do { |
| 152 | n = *query++; |
| 153 | |
| 154 | while(n > 0) { |
| 155 | /* printf("%c", *query);*/ |
| 156 | ++query; |
| 157 | --n; |
| 158 | }; |
| 159 | /* printf(".");*/ |
| 160 | } while(*query != 0); |
| 161 | /* printf("\n");*/ |
| 162 | return query + 1; |
| 163 | } |
| 164 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 165 | /** \internal |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 166 | * 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] | 167 | * not yet been queried and, if so, sends out a query. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 168 | */ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 169 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 170 | static void |
| 171 | check_entries(void) |
| 172 | { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 173 | register struct dns_hdr *hdr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 174 | char *query, *nptr, *nameptr; |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 175 | static u8_t i; |
| 176 | static u8_t n; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 177 | register struct namemap *namemapptr; |
| 178 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 179 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 180 | namemapptr = &names[i]; |
| 181 | if(namemapptr->state == STATE_NEW || |
| 182 | namemapptr->state == STATE_ASKING) { |
| 183 | if(namemapptr->state == STATE_ASKING) { |
| 184 | if(--namemapptr->tmr == 0) { |
| 185 | if(++namemapptr->retries == MAX_RETRIES) { |
| 186 | namemapptr->state = STATE_ERROR; |
| 187 | resolv_found(namemapptr->name, NULL); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 188 | continue; |
| 189 | } |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 190 | namemapptr->tmr = namemapptr->retries; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 191 | } else { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 192 | /* printf("Timer %d\n", namemapptr->tmr);*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 193 | /* Its timer has not run out, so we move on to next |
| 194 | entry. */ |
| 195 | continue; |
| 196 | } |
| 197 | } else { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 198 | namemapptr->state = STATE_ASKING; |
| 199 | namemapptr->tmr = 1; |
| 200 | namemapptr->retries = 0; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 201 | } |
| 202 | hdr = (struct dns_hdr *)uip_appdata; |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 203 | memset(hdr, 0, sizeof(struct dns_hdr)); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 204 | hdr->id = htons(i); |
| 205 | hdr->flags1 = DNS_FLAG1_RD; |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 206 | hdr->numquestions = HTONS(1); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 207 | query = (char *)uip_appdata + 12; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 208 | nameptr = namemapptr->name; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 209 | --nameptr; |
| 210 | /* Convert hostname into suitable query format. */ |
| 211 | do { |
| 212 | ++nameptr; |
| 213 | nptr = query; |
| 214 | ++query; |
| 215 | for(n = 0; *nameptr != '.' && *nameptr != 0; ++nameptr) { |
| 216 | *query = *nameptr; |
| 217 | ++query; |
| 218 | ++n; |
| 219 | } |
| 220 | *nptr = n; |
| 221 | } while(*nameptr != 0); |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 222 | { |
| 223 | static unsigned char endquery[] = |
| 224 | {0,0,1,0,1}; |
| 225 | memcpy(query, endquery, 5); |
| 226 | } |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 227 | uip_udp_send((unsigned char)(query + 5 - (char *)uip_appdata)); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 233 | /** \internal |
| 234 | * Called when new UDP data arrives. |
| 235 | */ |
| 236 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 237 | static void |
| 238 | newdata(void) |
| 239 | { |
| 240 | char *nameptr; |
| 241 | struct dns_answer *ans; |
| 242 | struct dns_hdr *hdr; |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 243 | static u8_t nquestions, nanswers; |
| 244 | static u8_t i; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 245 | register struct namemap *namemapptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 246 | |
| 247 | hdr = (struct dns_hdr *)uip_appdata; |
| 248 | /* printf("ID %d\n", htons(hdr->id)); |
| 249 | printf("Query %d\n", hdr->flags1 & DNS_FLAG1_RESPONSE); |
| 250 | printf("Error %d\n", hdr->flags2 & DNS_FLAG2_ERR_MASK); |
| 251 | printf("Num questions %d, answers %d, authrr %d, extrarr %d\n", |
| 252 | htons(hdr->numquestions), |
| 253 | htons(hdr->numanswers), |
| 254 | htons(hdr->numauthrr), |
| 255 | htons(hdr->numextrarr)); |
| 256 | */ |
| 257 | |
| 258 | /* The ID in the DNS header should be our entry into the name |
| 259 | table. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 260 | i = htons(hdr->id); |
| 261 | namemapptr = &names[i]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 262 | if(i < RESOLV_ENTRIES && |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 263 | namemapptr->state == STATE_ASKING) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 264 | |
| 265 | /* This entry is now finished. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 266 | namemapptr->state = STATE_DONE; |
| 267 | namemapptr->err = hdr->flags2 & DNS_FLAG2_ERR_MASK; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 268 | |
| 269 | /* Check for error. If so, call callback to inform. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 270 | if(namemapptr->err != 0) { |
| 271 | namemapptr->state = STATE_ERROR; |
| 272 | resolv_found(namemapptr->name, NULL); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
| 276 | /* We only care about the question(s) and the answers. The authrr |
| 277 | and the extrarr are simply discarded. */ |
| 278 | nquestions = htons(hdr->numquestions); |
| 279 | nanswers = htons(hdr->numanswers); |
| 280 | |
| 281 | /* Skip the name in the question. XXX: This should really be |
| 282 | checked agains the name in the question, to be sure that they |
| 283 | match. */ |
| 284 | nameptr = parse_name((char *)uip_appdata + 12) + 4; |
| 285 | |
| 286 | while(nanswers > 0) { |
| 287 | /* The first byte in the answer resource record determines if it |
| 288 | is a compressed record or a normal one. */ |
| 289 | if(*nameptr & 0xc0) { |
| 290 | /* Compressed name. */ |
| 291 | nameptr +=2; |
| 292 | /* printf("Compressed anwser\n");*/ |
| 293 | } else { |
| 294 | /* Not compressed name. */ |
| 295 | nameptr = parse_name((char *)nameptr); |
| 296 | } |
| 297 | |
| 298 | ans = (struct dns_answer *)nameptr; |
| 299 | /* printf("Answer: type %x, class %x, ttl %x, length %x\n", |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 300 | htons(ans->type), htons(ans->class), (htons(ans->ttl[0]) |
| 301 | << 16) | htons(ans->ttl[1]), htons(ans->len));*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 302 | |
| 303 | /* Check for IP address type and Internet class. Others are |
| 304 | discarded. */ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 305 | if(ans->type == HTONS(1) && |
| 306 | ans->class == HTONS(1) && |
| 307 | ans->len == HTONS(4)) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 308 | /* printf("IP address %d.%d.%d.%d\n", |
| 309 | htons(ans->ipaddr[0]) >> 8, |
| 310 | htons(ans->ipaddr[0]) & 0xff, |
| 311 | htons(ans->ipaddr[1]) >> 8, |
| 312 | htons(ans->ipaddr[1]) & 0xff);*/ |
| 313 | /* XXX: we should really check that this IP address is the one |
| 314 | we want. */ |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 315 | namemapptr->ipaddr[0] = ans->ipaddr[0]; |
| 316 | namemapptr->ipaddr[1] = ans->ipaddr[1]; |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 317 | |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 318 | resolv_found(namemapptr->name, namemapptr->ipaddr); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 319 | return; |
| 320 | } else { |
| 321 | nameptr = nameptr + 10 + htons(ans->len); |
| 322 | } |
| 323 | --nanswers; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 329 | /** \internal |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 330 | * The main UDP function. |
| 331 | */ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 332 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 333 | void |
| 334 | udp_appcall(void) |
| 335 | { |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 336 | if(uip_udp_conn->rport == HTONS(53)) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 337 | if(uip_poll()) { |
| 338 | check_entries(); |
| 339 | } |
| 340 | if(uip_newdata()) { |
| 341 | newdata(); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 346 | /** |
| 347 | * 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] | 348 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 349 | * \param name The hostname that is to be queried. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 350 | */ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 351 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 352 | void |
| 353 | resolv_query(char *name) |
| 354 | { |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 355 | static u8_t i; |
| 356 | static u8_t lseq, lseqi; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 357 | register struct namemap *nameptr; |
| 358 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 359 | lseq = lseqi = 0; |
| 360 | |
| 361 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 362 | nameptr = &names[i]; |
| 363 | if(nameptr->state == STATE_UNUSED) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 366 | if(seqno - nameptr->seqno > lseq) { |
| 367 | lseq = seqno - nameptr->seqno; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 368 | lseqi = i; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if(i == RESOLV_ENTRIES) { |
| 373 | i = lseqi; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 374 | nameptr = &names[i]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* printf("Using entry %d\n", i);*/ |
| 378 | |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 379 | strcpy(nameptr->name, name); |
| 380 | nameptr->state = STATE_NEW; |
| 381 | nameptr->seqno = seqno; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 382 | ++seqno; |
| 383 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 384 | if(resolv_conn != NULL) { |
adamdunkels | bb2e93c | 2003-09-04 19:38:46 +0000 | [diff] [blame] | 385 | dispatcher_emit(uip_signal_poll_udp, resolv_conn, DISPATCHER_BROADCAST); |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 386 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 387 | } |
| 388 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 389 | /** |
| 390 | * Look up a hostname in the array of known hostnames. |
| 391 | * |
| 392 | * \note This function only looks in the internal array of known |
| 393 | * hostnames, it does not send out a query for the hostname if none |
| 394 | * was found. The function resolv_query() can be used to send a query |
| 395 | * for a hostname. |
| 396 | * |
| 397 | * \return A pointer to a 4-byte representation of the hostname's IP |
| 398 | * address, or NULL if the hostname was not found in the array of |
| 399 | * hostnames. |
| 400 | */ |
| 401 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 402 | u16_t * |
| 403 | resolv_lookup(char *name) |
| 404 | { |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 405 | static u8_t i; |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 406 | struct namemap *nameptr; |
| 407 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 408 | /* Walk through the list to see if the name is in there. If it is |
| 409 | not, we return NULL. */ |
| 410 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 411 | nameptr = &names[i]; |
| 412 | if(nameptr->state == STATE_DONE && |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 413 | strcmp(name, nameptr->name) == 0) { |
adamdunkels | 2366402 | 2003-08-05 13:51:50 +0000 | [diff] [blame] | 414 | return nameptr->ipaddr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | return NULL; |
| 418 | } |
| 419 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 420 | /** |
| 421 | * Obtain the currently configured DNS server. |
| 422 | * |
| 423 | * \return A pointer to a 4-byte representation of the IP address of |
| 424 | * the currently configured DNS server or NULL if no DNS server has |
| 425 | * been configured. |
| 426 | */ |
| 427 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 66c6af6 | 2003-04-16 18:28:16 +0000 | [diff] [blame] | 428 | u16_t * |
| 429 | resolv_getserver(void) |
| 430 | { |
| 431 | if(resolv_conn == NULL) { |
| 432 | return NULL; |
| 433 | } |
| 434 | return resolv_conn->ripaddr; |
| 435 | } |
| 436 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 437 | /** |
| 438 | * Configure a DNS server. |
| 439 | * |
| 440 | * \param dnsserver A pointer to a 4-byte representation of the IP |
| 441 | * address of the DNS server to be configured. |
| 442 | */ |
| 443 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 444 | void |
| 445 | resolv_conf(u16_t *dnsserver) |
| 446 | { |
| 447 | if(resolv_conn != NULL) { |
| 448 | uip_udp_remove(resolv_conn); |
| 449 | } |
| 450 | |
| 451 | resolv_conn = uip_udp_new(dnsserver, 53); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 452 | } |
| 453 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 454 | /** |
| 455 | * Initalize the resolver. |
| 456 | */ |
| 457 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 458 | void |
| 459 | resolv_init(void) |
| 460 | { |
adamdunkels | 99dcf45 | 2003-08-15 18:50:36 +0000 | [diff] [blame] | 461 | static u8_t i; |
| 462 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 463 | for(i = 0; i < RESOLV_ENTRIES; ++i) { |
| 464 | names[i].state = STATE_DONE; |
| 465 | } |
| 466 | |
| 467 | resolv_signal_found = dispatcher_sigalloc(); |
| 468 | } |
| 469 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 470 | /** \internal |
| 471 | * Callback function which is called when a hostname is found. |
| 472 | * |
| 473 | */ |
| 474 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 475 | void |
| 476 | resolv_found(char *name, u16_t *ipaddr) |
| 477 | { |
| 478 | dispatcher_emit(resolv_signal_found, name, DISPATCHER_BROADCAST); |
| 479 | } |
adamdunkels | 66c6af6 | 2003-04-16 18:28:16 +0000 | [diff] [blame] | 480 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame^] | 481 | |
| 482 | /** @} */ |
| 483 | /** @} */ |