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