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