Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1 /**
simon 0:350011bf8be7 2 * @file
simon 0:350011bf8be7 3 * DNS - host name to IP address resolver.
simon 0:350011bf8be7 4 *
simon 0:350011bf8be7 5 */
simon 0:350011bf8be7 6
simon 0:350011bf8be7 7 /**
simon 0:350011bf8be7 8
simon 0:350011bf8be7 9 * This file implements a DNS host name to IP address resolver.
simon 0:350011bf8be7 10
simon 0:350011bf8be7 11 * Port to lwIP from uIP
simon 0:350011bf8be7 12 * by Jim Pettinato April 2007
simon 0:350011bf8be7 13
simon 0:350011bf8be7 14 * uIP version Copyright (c) 2002-2003, Adam Dunkels.
simon 0:350011bf8be7 15 * All rights reserved.
simon 0:350011bf8be7 16 *
simon 0:350011bf8be7 17 * Redistribution and use in source and binary forms, with or without
simon 0:350011bf8be7 18 * modification, are permitted provided that the following conditions
simon 0:350011bf8be7 19 * are met:
simon 0:350011bf8be7 20 * 1. Redistributions of source code must retain the above copyright
simon 0:350011bf8be7 21 * notice, this list of conditions and the following disclaimer.
simon 0:350011bf8be7 22 * 2. Redistributions in binary form must reproduce the above copyright
simon 0:350011bf8be7 23 * notice, this list of conditions and the following disclaimer in the
simon 0:350011bf8be7 24 * documentation and/or other materials provided with the distribution.
simon 0:350011bf8be7 25 * 3. The name of the author may not be used to endorse or promote
simon 0:350011bf8be7 26 * products derived from this software without specific prior
simon 0:350011bf8be7 27 * written permission.
simon 0:350011bf8be7 28 *
simon 0:350011bf8be7 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
simon 0:350011bf8be7 30 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
simon 0:350011bf8be7 31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
simon 0:350011bf8be7 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
simon 0:350011bf8be7 33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
simon 0:350011bf8be7 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
simon 0:350011bf8be7 35 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
simon 0:350011bf8be7 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
simon 0:350011bf8be7 37 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
simon 0:350011bf8be7 38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
simon 0:350011bf8be7 39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
simon 0:350011bf8be7 40 *
simon 0:350011bf8be7 41 *
simon 0:350011bf8be7 42 * DNS.C
simon 0:350011bf8be7 43 *
simon 0:350011bf8be7 44 * The lwIP DNS resolver functions are used to lookup a host name and
simon 0:350011bf8be7 45 * map it to a numerical IP address. It maintains a list of resolved
simon 0:350011bf8be7 46 * hostnames that can be queried with the dns_lookup() function.
simon 0:350011bf8be7 47 * New hostnames can be resolved using the dns_query() function.
simon 0:350011bf8be7 48 *
simon 0:350011bf8be7 49 * The lwIP version of the resolver also adds a non-blocking version of
simon 0:350011bf8be7 50 * gethostbyname() that will work with a raw API application. This function
simon 0:350011bf8be7 51 * checks for an IP address string first and converts it if it is valid.
simon 0:350011bf8be7 52 * gethostbyname() then does a dns_lookup() to see if the name is
simon 0:350011bf8be7 53 * already in the table. If so, the IP is returned. If not, a query is
simon 0:350011bf8be7 54 * issued and the function returns with a ERR_INPROGRESS status. The app
simon 0:350011bf8be7 55 * using the dns client must then go into a waiting state.
simon 0:350011bf8be7 56 *
simon 0:350011bf8be7 57 * Once a hostname has been resolved (or found to be non-existent),
simon 0:350011bf8be7 58 * the resolver code calls a specified callback function (which
simon 0:350011bf8be7 59 * must be implemented by the module that uses the resolver).
simon 0:350011bf8be7 60 */
simon 0:350011bf8be7 61
simon 0:350011bf8be7 62 /*-----------------------------------------------------------------------------
simon 0:350011bf8be7 63 * RFC 1035 - Domain names - implementation and specification
simon 0:350011bf8be7 64 * RFC 2181 - Clarifications to the DNS Specification
simon 0:350011bf8be7 65 *----------------------------------------------------------------------------*/
simon 0:350011bf8be7 66
simon 0:350011bf8be7 67 /** @todo: define good default values (rfc compliance) */
simon 0:350011bf8be7 68 /** @todo: improve answer parsing, more checkings... */
simon 0:350011bf8be7 69 /** @todo: check RFC1035 - 7.3. Processing responses */
simon 0:350011bf8be7 70
simon 0:350011bf8be7 71 /*-----------------------------------------------------------------------------
simon 0:350011bf8be7 72 * Includes
simon 0:350011bf8be7 73 *----------------------------------------------------------------------------*/
simon 0:350011bf8be7 74
simon 0:350011bf8be7 75 #include "lwip/opt.h"
simon 0:350011bf8be7 76
simon 0:350011bf8be7 77 #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 78
simon 0:350011bf8be7 79 #include "lwip/udp.h"
simon 0:350011bf8be7 80 #include "lwip/mem.h"
simon 0:350011bf8be7 81 #include "lwip/memp.h"
simon 0:350011bf8be7 82 #include "lwip/dns.h"
simon 0:350011bf8be7 83
simon 0:350011bf8be7 84 #include <string.h>
simon 0:350011bf8be7 85
simon 0:350011bf8be7 86 /** DNS server IP address */
simon 0:350011bf8be7 87 #ifndef DNS_SERVER_ADDRESS
simon 0:350011bf8be7 88 #define DNS_SERVER_ADDRESS(ipaddr) (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */
simon 0:350011bf8be7 89 #endif
simon 0:350011bf8be7 90
simon 0:350011bf8be7 91 /** DNS server port address */
simon 0:350011bf8be7 92 #ifndef DNS_SERVER_PORT
simon 0:350011bf8be7 93 #define DNS_SERVER_PORT 53
simon 0:350011bf8be7 94 #endif
simon 0:350011bf8be7 95
simon 0:350011bf8be7 96 /** DNS maximum number of retries when asking for a name, before "timeout". */
simon 0:350011bf8be7 97 #ifndef DNS_MAX_RETRIES
simon 0:350011bf8be7 98 #define DNS_MAX_RETRIES 4
simon 0:350011bf8be7 99 #endif
simon 0:350011bf8be7 100
simon 0:350011bf8be7 101 /** DNS resource record max. TTL (one week as default) */
simon 0:350011bf8be7 102 #ifndef DNS_MAX_TTL
simon 0:350011bf8be7 103 #define DNS_MAX_TTL 604800
simon 0:350011bf8be7 104 #endif
simon 0:350011bf8be7 105
simon 0:350011bf8be7 106 /* DNS protocol flags */
simon 0:350011bf8be7 107 #define DNS_FLAG1_RESPONSE 0x80
simon 0:350011bf8be7 108 #define DNS_FLAG1_OPCODE_STATUS 0x10
simon 0:350011bf8be7 109 #define DNS_FLAG1_OPCODE_INVERSE 0x08
simon 0:350011bf8be7 110 #define DNS_FLAG1_OPCODE_STANDARD 0x00
simon 0:350011bf8be7 111 #define DNS_FLAG1_AUTHORATIVE 0x04
simon 0:350011bf8be7 112 #define DNS_FLAG1_TRUNC 0x02
simon 0:350011bf8be7 113 #define DNS_FLAG1_RD 0x01
simon 0:350011bf8be7 114 #define DNS_FLAG2_RA 0x80
simon 0:350011bf8be7 115 #define DNS_FLAG2_ERR_MASK 0x0f
simon 0:350011bf8be7 116 #define DNS_FLAG2_ERR_NONE 0x00
simon 0:350011bf8be7 117 #define DNS_FLAG2_ERR_NAME 0x03
simon 0:350011bf8be7 118
simon 0:350011bf8be7 119 /* DNS protocol states */
simon 0:350011bf8be7 120 #define DNS_STATE_UNUSED 0
simon 0:350011bf8be7 121 #define DNS_STATE_NEW 1
simon 0:350011bf8be7 122 #define DNS_STATE_ASKING 2
simon 0:350011bf8be7 123 #define DNS_STATE_DONE 3
simon 0:350011bf8be7 124
simon 0:350011bf8be7 125 #ifdef PACK_STRUCT_USE_INCLUDES
simon 0:350011bf8be7 126 # include "arch/bpstruct.h"
simon 0:350011bf8be7 127 #endif
simon 0:350011bf8be7 128 PACK_STRUCT_BEGIN
simon 0:350011bf8be7 129 /** DNS message header */
simon 0:350011bf8be7 130 struct dns_hdr {
simon 0:350011bf8be7 131 PACK_STRUCT_FIELD(u16_t id);
simon 0:350011bf8be7 132 PACK_STRUCT_FIELD(u8_t flags1);
simon 0:350011bf8be7 133 PACK_STRUCT_FIELD(u8_t flags2);
simon 0:350011bf8be7 134 PACK_STRUCT_FIELD(u16_t numquestions);
simon 0:350011bf8be7 135 PACK_STRUCT_FIELD(u16_t numanswers);
simon 0:350011bf8be7 136 PACK_STRUCT_FIELD(u16_t numauthrr);
simon 0:350011bf8be7 137 PACK_STRUCT_FIELD(u16_t numextrarr);
simon 0:350011bf8be7 138 } PACK_STRUCT_STRUCT;
simon 0:350011bf8be7 139 PACK_STRUCT_END
simon 0:350011bf8be7 140 #ifdef PACK_STRUCT_USE_INCLUDES
simon 0:350011bf8be7 141 # include "arch/epstruct.h"
simon 0:350011bf8be7 142 #endif
simon 0:350011bf8be7 143 #define SIZEOF_DNS_HDR 12
simon 0:350011bf8be7 144
simon 0:350011bf8be7 145 /** DNS query message structure.
simon 0:350011bf8be7 146 No packing needed: only used locally on the stack. */
simon 0:350011bf8be7 147 struct dns_query {
simon 0:350011bf8be7 148 /* DNS query record starts with either a domain name or a pointer
simon 0:350011bf8be7 149 to a name already present somewhere in the packet. */
simon 0:350011bf8be7 150 u16_t type;
simon 0:350011bf8be7 151 u16_t cls;
simon 0:350011bf8be7 152 };
simon 0:350011bf8be7 153 #define SIZEOF_DNS_QUERY 4
simon 0:350011bf8be7 154
simon 0:350011bf8be7 155 /** DNS answer message structure.
simon 0:350011bf8be7 156 No packing needed: only used locally on the stack. */
simon 0:350011bf8be7 157 struct dns_answer {
simon 0:350011bf8be7 158 /* DNS answer record starts with either a domain name or a pointer
simon 0:350011bf8be7 159 to a name already present somewhere in the packet. */
simon 0:350011bf8be7 160 u16_t type;
simon 0:350011bf8be7 161 u16_t cls;
simon 0:350011bf8be7 162 u32_t ttl;
simon 0:350011bf8be7 163 u16_t len;
simon 0:350011bf8be7 164 };
simon 0:350011bf8be7 165 #define SIZEOF_DNS_ANSWER 10
simon 0:350011bf8be7 166
simon 0:350011bf8be7 167 /** DNS table entry */
simon 0:350011bf8be7 168 struct dns_table_entry {
simon 0:350011bf8be7 169 u8_t state;
simon 0:350011bf8be7 170 u8_t numdns;
simon 0:350011bf8be7 171 u8_t tmr;
simon 0:350011bf8be7 172 u8_t retries;
simon 0:350011bf8be7 173 u8_t seqno;
simon 0:350011bf8be7 174 u8_t err;
simon 0:350011bf8be7 175 u32_t ttl;
simon 0:350011bf8be7 176 char name[DNS_MAX_NAME_LENGTH];
simon 0:350011bf8be7 177 ip_addr_t ipaddr;
simon 0:350011bf8be7 178 /* pointer to callback on DNS query done */
simon 0:350011bf8be7 179 dns_found_callback found;
simon 0:350011bf8be7 180 void *arg;
simon 0:350011bf8be7 181 };
simon 0:350011bf8be7 182
simon 0:350011bf8be7 183 #if DNS_LOCAL_HOSTLIST
simon 0:350011bf8be7 184
simon 0:350011bf8be7 185 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
simon 0:350011bf8be7 186 /** Local host-list. For hostnames in this list, no
simon 0:350011bf8be7 187 * external name resolution is performed */
simon 0:350011bf8be7 188 static struct local_hostlist_entry *local_hostlist_dynamic;
simon 0:350011bf8be7 189 #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
simon 0:350011bf8be7 190
simon 0:350011bf8be7 191 /** Defining this allows the local_hostlist_static to be placed in a different
simon 0:350011bf8be7 192 * linker section (e.g. FLASH) */
simon 0:350011bf8be7 193 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE
simon 0:350011bf8be7 194 #define DNS_LOCAL_HOSTLIST_STORAGE_PRE static
simon 0:350011bf8be7 195 #endif /* DNS_LOCAL_HOSTLIST_STORAGE_PRE */
simon 0:350011bf8be7 196 /** Defining this allows the local_hostlist_static to be placed in a different
simon 0:350011bf8be7 197 * linker section (e.g. FLASH) */
simon 0:350011bf8be7 198 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST
simon 0:350011bf8be7 199 #define DNS_LOCAL_HOSTLIST_STORAGE_POST
simon 0:350011bf8be7 200 #endif /* DNS_LOCAL_HOSTLIST_STORAGE_POST */
simon 0:350011bf8be7 201 DNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[]
simon 0:350011bf8be7 202 DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT;
simon 0:350011bf8be7 203
simon 0:350011bf8be7 204 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
simon 0:350011bf8be7 205
simon 0:350011bf8be7 206 static void dns_init_local();
simon 0:350011bf8be7 207 #endif /* DNS_LOCAL_HOSTLIST */
simon 0:350011bf8be7 208
simon 0:350011bf8be7 209
simon 0:350011bf8be7 210 /* forward declarations */
simon 0:350011bf8be7 211 static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
simon 0:350011bf8be7 212 static void dns_check_entries(void);
simon 0:350011bf8be7 213
simon 0:350011bf8be7 214 /*-----------------------------------------------------------------------------
simon 0:350011bf8be7 215 * Globales
simon 0:350011bf8be7 216 *----------------------------------------------------------------------------*/
simon 0:350011bf8be7 217
simon 0:350011bf8be7 218 /* DNS variables */
simon 0:350011bf8be7 219 static struct udp_pcb *dns_pcb;
simon 0:350011bf8be7 220 static u8_t dns_seqno;
simon 0:350011bf8be7 221 static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
simon 0:350011bf8be7 222 static ip_addr_t dns_servers[DNS_MAX_SERVERS];
simon 0:350011bf8be7 223 /** Contiguous buffer for processing responses */
simon 0:350011bf8be7 224 static u8_t dns_payload_buffer[LWIP_MEM_ALIGN_BUFFER(DNS_MSG_SIZE)];
simon 0:350011bf8be7 225 static u8_t* dns_payload;
simon 0:350011bf8be7 226
simon 0:350011bf8be7 227 /**
simon 0:350011bf8be7 228 * Initialize the resolver: set up the UDP pcb and configure the default server
simon 0:350011bf8be7 229 * (DNS_SERVER_ADDRESS).
simon 0:350011bf8be7 230 */
simon 0:350011bf8be7 231 void
simon 0:350011bf8be7 232 dns_init()
simon 0:350011bf8be7 233 {
simon 0:350011bf8be7 234 ip_addr_t dnsserver;
simon 0:350011bf8be7 235
simon 0:350011bf8be7 236 dns_payload = (u8_t *)LWIP_MEM_ALIGN(dns_payload_buffer);
simon 0:350011bf8be7 237
simon 0:350011bf8be7 238 /* initialize default DNS server address */
simon 0:350011bf8be7 239 DNS_SERVER_ADDRESS(&dnsserver);
simon 0:350011bf8be7 240
simon 0:350011bf8be7 241 LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n"));
simon 0:350011bf8be7 242
simon 0:350011bf8be7 243 /* if dns client not yet initialized... */
simon 0:350011bf8be7 244 if (dns_pcb == NULL) {
simon 0:350011bf8be7 245 dns_pcb = udp_new();
simon 0:350011bf8be7 246
simon 0:350011bf8be7 247 if (dns_pcb != NULL) {
simon 0:350011bf8be7 248 /* initialize DNS table not needed (initialized to zero since it is a
simon 0:350011bf8be7 249 * global variable) */
simon 0:350011bf8be7 250 LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0",
simon 0:350011bf8be7 251 DNS_STATE_UNUSED == 0);
simon 0:350011bf8be7 252
simon 0:350011bf8be7 253 /* initialize DNS client */
simon 0:350011bf8be7 254 udp_bind(dns_pcb, IP_ADDR_ANY, 0);
simon 0:350011bf8be7 255 udp_recv(dns_pcb, dns_recv, NULL);
simon 0:350011bf8be7 256
simon 0:350011bf8be7 257 /* initialize default DNS primary server */
simon 0:350011bf8be7 258 dns_setserver(0, &dnsserver);
simon 0:350011bf8be7 259 }
simon 0:350011bf8be7 260 }
simon 0:350011bf8be7 261 #if DNS_LOCAL_HOSTLIST
simon 0:350011bf8be7 262 dns_init_local();
simon 0:350011bf8be7 263 #endif
simon 0:350011bf8be7 264 }
simon 0:350011bf8be7 265
simon 0:350011bf8be7 266 /**
simon 0:350011bf8be7 267 * Initialize one of the DNS servers.
simon 0:350011bf8be7 268 *
simon 0:350011bf8be7 269 * @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS
simon 0:350011bf8be7 270 * @param dnsserver IP address of the DNS server to set
simon 0:350011bf8be7 271 */
simon 0:350011bf8be7 272 void
simon 0:350011bf8be7 273 dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
simon 0:350011bf8be7 274 {
simon 0:350011bf8be7 275 if ((numdns < DNS_MAX_SERVERS) && (dns_pcb != NULL) &&
simon 0:350011bf8be7 276 (dnsserver != NULL) && !ip_addr_isany(dnsserver)) {
simon 0:350011bf8be7 277 dns_servers[numdns] = (*dnsserver);
simon 0:350011bf8be7 278 }
simon 0:350011bf8be7 279 }
simon 0:350011bf8be7 280
simon 0:350011bf8be7 281 /**
simon 0:350011bf8be7 282 * Obtain one of the currently configured DNS server.
simon 0:350011bf8be7 283 *
simon 0:350011bf8be7 284 * @param numdns the index of the DNS server
simon 0:350011bf8be7 285 * @return IP address of the indexed DNS server or "ip_addr_any" if the DNS
simon 0:350011bf8be7 286 * server has not been configured.
simon 0:350011bf8be7 287 */
simon 0:350011bf8be7 288 ip_addr_t
simon 0:350011bf8be7 289 dns_getserver(u8_t numdns)
simon 0:350011bf8be7 290 {
simon 0:350011bf8be7 291 if (numdns < DNS_MAX_SERVERS) {
simon 0:350011bf8be7 292 return dns_servers[numdns];
simon 0:350011bf8be7 293 } else {
simon 0:350011bf8be7 294 return *IP_ADDR_ANY;
simon 0:350011bf8be7 295 }
simon 0:350011bf8be7 296 }
simon 0:350011bf8be7 297
simon 0:350011bf8be7 298 /**
simon 0:350011bf8be7 299 * The DNS resolver client timer - handle retries and timeouts and should
simon 0:350011bf8be7 300 * be called every DNS_TMR_INTERVAL milliseconds (every second by default).
simon 0:350011bf8be7 301 */
simon 0:350011bf8be7 302 void
simon 0:350011bf8be7 303 dns_tmr(void)
simon 0:350011bf8be7 304 {
simon 0:350011bf8be7 305 if (dns_pcb != NULL) {
simon 0:350011bf8be7 306 LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n"));
simon 0:350011bf8be7 307 dns_check_entries();
simon 0:350011bf8be7 308 }
simon 0:350011bf8be7 309 }
simon 0:350011bf8be7 310
simon 0:350011bf8be7 311 #if DNS_LOCAL_HOSTLIST
simon 0:350011bf8be7 312 static void
simon 0:350011bf8be7 313 dns_init_local()
simon 0:350011bf8be7 314 {
simon 0:350011bf8be7 315 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT)
simon 0:350011bf8be7 316 int i;
simon 0:350011bf8be7 317 struct local_hostlist_entry *entry;
simon 0:350011bf8be7 318 /* Dynamic: copy entries from DNS_LOCAL_HOSTLIST_INIT to list */
simon 0:350011bf8be7 319 struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT;
simon 0:350011bf8be7 320 size_t namelen;
simon 0:350011bf8be7 321 for (i = 0; i < sizeof(local_hostlist_init) / sizeof(struct local_hostlist_entry); i++) {
simon 0:350011bf8be7 322 struct local_hostlist_entry *init_entry = &local_hostlist_init[i];
simon 0:350011bf8be7 323 LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL);
simon 0:350011bf8be7 324 namelen = strlen(init_entry->name);
simon 0:350011bf8be7 325 LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
simon 0:350011bf8be7 326 entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
simon 0:350011bf8be7 327 LWIP_ASSERT("mem-error in dns_init_local", entry != NULL);
simon 0:350011bf8be7 328 if (entry != NULL) {
simon 0:350011bf8be7 329 entry->name = (char*)entry + sizeof(struct local_hostlist_entry);
simon 0:350011bf8be7 330 MEMCPY((char*)entry->name, init_entry->name, namelen);
simon 0:350011bf8be7 331 ((char*)entry->name)[namelen] = 0;
simon 0:350011bf8be7 332 entry->addr = init_entry->addr;
simon 0:350011bf8be7 333 entry->next = local_hostlist_dynamic;
simon 0:350011bf8be7 334 local_hostlist_dynamic = entry;
simon 0:350011bf8be7 335 }
simon 0:350011bf8be7 336 }
simon 0:350011bf8be7 337 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) */
simon 0:350011bf8be7 338 }
simon 0:350011bf8be7 339
simon 0:350011bf8be7 340 /**
simon 0:350011bf8be7 341 * Scans the local host-list for a hostname.
simon 0:350011bf8be7 342 *
simon 0:350011bf8be7 343 * @param hostname Hostname to look for in the local host-list
simon 0:350011bf8be7 344 * @return The first IP address for the hostname in the local host-list or
simon 0:350011bf8be7 345 * IPADDR_NONE if not found.
simon 0:350011bf8be7 346 */
simon 0:350011bf8be7 347 static u32_t
simon 0:350011bf8be7 348 dns_lookup_local(const char *hostname)
simon 0:350011bf8be7 349 {
simon 0:350011bf8be7 350 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
simon 0:350011bf8be7 351 struct local_hostlist_entry *entry = local_hostlist_dynamic;
simon 0:350011bf8be7 352 while(entry != NULL) {
simon 0:350011bf8be7 353 if(strcmp(entry->name, hostname) == 0) {
simon 0:350011bf8be7 354 return ip4_addr_get_u32(&entry->addr);
simon 0:350011bf8be7 355 }
simon 0:350011bf8be7 356 entry = entry->next;
simon 0:350011bf8be7 357 }
simon 0:350011bf8be7 358 #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
simon 0:350011bf8be7 359 int i;
simon 0:350011bf8be7 360 for (i = 0; i < sizeof(local_hostlist_static) / sizeof(struct local_hostlist_entry); i++) {
simon 0:350011bf8be7 361 if(strcmp(local_hostlist_static[i].name, hostname) == 0) {
simon 0:350011bf8be7 362 return ip4_addr_get_u32(&local_hostlist_static[i].addr);
simon 0:350011bf8be7 363 }
simon 0:350011bf8be7 364 }
simon 0:350011bf8be7 365 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
simon 0:350011bf8be7 366 return IPADDR_NONE;
simon 0:350011bf8be7 367 }
simon 0:350011bf8be7 368
simon 0:350011bf8be7 369 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
simon 0:350011bf8be7 370 /** Remove all entries from the local host-list for a specific hostname
simon 0:350011bf8be7 371 * and/or IP addess
simon 0:350011bf8be7 372 *
simon 0:350011bf8be7 373 * @param hostname hostname for which entries shall be removed from the local
simon 0:350011bf8be7 374 * host-list
simon 0:350011bf8be7 375 * @param addr address for which entries shall be removed from the local host-list
simon 0:350011bf8be7 376 * @return the number of removed entries
simon 0:350011bf8be7 377 */
simon 0:350011bf8be7 378 int
simon 0:350011bf8be7 379 dns_local_removehost(const char *hostname, const ip_addr_t *addr)
simon 0:350011bf8be7 380 {
simon 0:350011bf8be7 381 int removed = 0;
simon 0:350011bf8be7 382 struct local_hostlist_entry *entry = local_hostlist_dynamic;
simon 0:350011bf8be7 383 struct local_hostlist_entry *last_entry = NULL;
simon 0:350011bf8be7 384 while (entry != NULL) {
simon 0:350011bf8be7 385 if (((hostname == NULL) || !strcmp(entry->name, hostname)) &&
simon 0:350011bf8be7 386 ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) {
simon 0:350011bf8be7 387 struct local_hostlist_entry *free_entry;
simon 0:350011bf8be7 388 if (last_entry != NULL) {
simon 0:350011bf8be7 389 last_entry->next = entry->next;
simon 0:350011bf8be7 390 } else {
simon 0:350011bf8be7 391 local_hostlist_dynamic = entry->next;
simon 0:350011bf8be7 392 }
simon 0:350011bf8be7 393 free_entry = entry;
simon 0:350011bf8be7 394 entry = entry->next;
simon 0:350011bf8be7 395 memp_free(MEMP_LOCALHOSTLIST, free_entry);
simon 0:350011bf8be7 396 removed++;
simon 0:350011bf8be7 397 } else {
simon 0:350011bf8be7 398 last_entry = entry;
simon 0:350011bf8be7 399 entry = entry->next;
simon 0:350011bf8be7 400 }
simon 0:350011bf8be7 401 }
simon 0:350011bf8be7 402 return removed;
simon 0:350011bf8be7 403 }
simon 0:350011bf8be7 404
simon 0:350011bf8be7 405 /**
simon 0:350011bf8be7 406 * Add a hostname/IP address pair to the local host-list.
simon 0:350011bf8be7 407 * Duplicates are not checked.
simon 0:350011bf8be7 408 *
simon 0:350011bf8be7 409 * @param hostname hostname of the new entry
simon 0:350011bf8be7 410 * @param addr IP address of the new entry
simon 0:350011bf8be7 411 * @return ERR_OK if succeeded or ERR_MEM on memory error
simon 0:350011bf8be7 412 */
simon 0:350011bf8be7 413 err_t
simon 0:350011bf8be7 414 dns_local_addhost(const char *hostname, const ip_addr_t *addr)
simon 0:350011bf8be7 415 {
simon 0:350011bf8be7 416 struct local_hostlist_entry *entry;
simon 0:350011bf8be7 417 size_t namelen;
simon 0:350011bf8be7 418 LWIP_ASSERT("invalid host name (NULL)", hostname != NULL);
simon 0:350011bf8be7 419 namelen = strlen(hostname);
simon 0:350011bf8be7 420 LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
simon 0:350011bf8be7 421 entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
simon 0:350011bf8be7 422 if (entry == NULL) {
simon 0:350011bf8be7 423 return ERR_MEM;
simon 0:350011bf8be7 424 }
simon 0:350011bf8be7 425 entry->name = (char*)entry + sizeof(struct local_hostlist_entry);
simon 0:350011bf8be7 426 MEMCPY((char*)entry->name, hostname, namelen);
simon 0:350011bf8be7 427 ((char*)entry->name)[namelen] = 0;
simon 0:350011bf8be7 428 ip_addr_copy(entry->addr, *addr);
simon 0:350011bf8be7 429 entry->next = local_hostlist_dynamic;
simon 0:350011bf8be7 430 local_hostlist_dynamic = entry;
simon 0:350011bf8be7 431 return ERR_OK;
simon 0:350011bf8be7 432 }
simon 0:350011bf8be7 433 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC*/
simon 0:350011bf8be7 434 #endif /* DNS_LOCAL_HOSTLIST */
simon 0:350011bf8be7 435
simon 0:350011bf8be7 436 /**
simon 0:350011bf8be7 437 * Look up a hostname in the array of known hostnames.
simon 0:350011bf8be7 438 *
simon 0:350011bf8be7 439 * @note This function only looks in the internal array of known
simon 0:350011bf8be7 440 * hostnames, it does not send out a query for the hostname if none
simon 0:350011bf8be7 441 * was found. The function dns_enqueue() can be used to send a query
simon 0:350011bf8be7 442 * for a hostname.
simon 0:350011bf8be7 443 *
simon 0:350011bf8be7 444 * @param name the hostname to look up
simon 0:350011bf8be7 445 * @return the hostname's IP address, as u32_t (instead of ip_addr_t to
simon 0:350011bf8be7 446 * better check for failure: != IPADDR_NONE) or IPADDR_NONE if the hostname
simon 0:350011bf8be7 447 * was not found in the cached dns_table.
simon 0:350011bf8be7 448 */
simon 0:350011bf8be7 449 static u32_t
simon 0:350011bf8be7 450 dns_lookup(const char *name)
simon 0:350011bf8be7 451 {
simon 0:350011bf8be7 452 u8_t i;
simon 0:350011bf8be7 453 #if DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN)
simon 0:350011bf8be7 454 u32_t addr;
simon 0:350011bf8be7 455 #endif /* DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) */
simon 0:350011bf8be7 456 #if DNS_LOCAL_HOSTLIST
simon 0:350011bf8be7 457 if ((addr = dns_lookup_local(name)) != IPADDR_NONE) {
simon 0:350011bf8be7 458 return addr;
simon 0:350011bf8be7 459 }
simon 0:350011bf8be7 460 #endif /* DNS_LOCAL_HOSTLIST */
simon 0:350011bf8be7 461 #ifdef DNS_LOOKUP_LOCAL_EXTERN
simon 0:350011bf8be7 462 if((addr = DNS_LOOKUP_LOCAL_EXTERN(name)) != IPADDR_NONE) {
simon 0:350011bf8be7 463 return addr;
simon 0:350011bf8be7 464 }
simon 0:350011bf8be7 465 #endif /* DNS_LOOKUP_LOCAL_EXTERN */
simon 0:350011bf8be7 466
simon 0:350011bf8be7 467 /* Walk through name list, return entry if found. If not, return NULL. */
simon 0:350011bf8be7 468 for (i = 0; i < DNS_TABLE_SIZE; ++i) {
simon 0:350011bf8be7 469 if ((dns_table[i].state == DNS_STATE_DONE) &&
simon 0:350011bf8be7 470 (strcmp(name, dns_table[i].name) == 0)) {
simon 0:350011bf8be7 471 LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name));
simon 0:350011bf8be7 472 ip_addr_debug_print(DNS_DEBUG, &(dns_table[i].ipaddr));
simon 0:350011bf8be7 473 LWIP_DEBUGF(DNS_DEBUG, ("\n"));
simon 0:350011bf8be7 474 return ip4_addr_get_u32(&dns_table[i].ipaddr);
simon 0:350011bf8be7 475 }
simon 0:350011bf8be7 476 }
simon 0:350011bf8be7 477
simon 0:350011bf8be7 478 return IPADDR_NONE;
simon 0:350011bf8be7 479 }
simon 0:350011bf8be7 480
simon 0:350011bf8be7 481 #if DNS_DOES_NAME_CHECK
simon 0:350011bf8be7 482 /**
simon 0:350011bf8be7 483 * Compare the "dotted" name "query" with the encoded name "response"
simon 0:350011bf8be7 484 * to make sure an answer from the DNS server matches the current dns_table
simon 0:350011bf8be7 485 * entry (otherwise, answers might arrive late for hostname not on the list
simon 0:350011bf8be7 486 * any more).
simon 0:350011bf8be7 487 *
simon 0:350011bf8be7 488 * @param query hostname (not encoded) from the dns_table
simon 0:350011bf8be7 489 * @param response encoded hostname in the DNS response
simon 0:350011bf8be7 490 * @return 0: names equal; 1: names differ
simon 0:350011bf8be7 491 */
simon 0:350011bf8be7 492 static u8_t
simon 0:350011bf8be7 493 dns_compare_name(unsigned char *query, unsigned char *response)
simon 0:350011bf8be7 494 {
simon 0:350011bf8be7 495 unsigned char n;
simon 0:350011bf8be7 496
simon 0:350011bf8be7 497 do {
simon 0:350011bf8be7 498 n = *response++;
simon 0:350011bf8be7 499 /** @see RFC 1035 - 4.1.4. Message compression */
simon 0:350011bf8be7 500 if ((n & 0xc0) == 0xc0) {
simon 0:350011bf8be7 501 /* Compressed name */
simon 0:350011bf8be7 502 break;
simon 0:350011bf8be7 503 } else {
simon 0:350011bf8be7 504 /* Not compressed name */
simon 0:350011bf8be7 505 while (n > 0) {
simon 0:350011bf8be7 506 if ((*query) != (*response)) {
simon 0:350011bf8be7 507 return 1;
simon 0:350011bf8be7 508 }
simon 0:350011bf8be7 509 ++response;
simon 0:350011bf8be7 510 ++query;
simon 0:350011bf8be7 511 --n;
simon 0:350011bf8be7 512 };
simon 0:350011bf8be7 513 ++query;
simon 0:350011bf8be7 514 }
simon 0:350011bf8be7 515 } while (*response != 0);
simon 0:350011bf8be7 516
simon 0:350011bf8be7 517 return 0;
simon 0:350011bf8be7 518 }
simon 0:350011bf8be7 519 #endif /* DNS_DOES_NAME_CHECK */
simon 0:350011bf8be7 520
simon 0:350011bf8be7 521 /**
simon 0:350011bf8be7 522 * Walk through a compact encoded DNS name and return the end of the name.
simon 0:350011bf8be7 523 *
simon 0:350011bf8be7 524 * @param query encoded DNS name in the DNS server response
simon 0:350011bf8be7 525 * @return end of the name
simon 0:350011bf8be7 526 */
simon 0:350011bf8be7 527 static unsigned char *
simon 0:350011bf8be7 528 dns_parse_name(unsigned char *query)
simon 0:350011bf8be7 529 {
simon 0:350011bf8be7 530 unsigned char n;
simon 0:350011bf8be7 531
simon 0:350011bf8be7 532 do {
simon 0:350011bf8be7 533 n = *query++;
simon 0:350011bf8be7 534 /** @see RFC 1035 - 4.1.4. Message compression */
simon 0:350011bf8be7 535 if ((n & 0xc0) == 0xc0) {
simon 0:350011bf8be7 536 /* Compressed name */
simon 0:350011bf8be7 537 break;
simon 0:350011bf8be7 538 } else {
simon 0:350011bf8be7 539 /* Not compressed name */
simon 0:350011bf8be7 540 while (n > 0) {
simon 0:350011bf8be7 541 ++query;
simon 0:350011bf8be7 542 --n;
simon 0:350011bf8be7 543 };
simon 0:350011bf8be7 544 }
simon 0:350011bf8be7 545 } while (*query != 0);
simon 0:350011bf8be7 546
simon 0:350011bf8be7 547 return query + 1;
simon 0:350011bf8be7 548 }
simon 0:350011bf8be7 549
simon 0:350011bf8be7 550 /**
simon 0:350011bf8be7 551 * Send a DNS query packet.
simon 0:350011bf8be7 552 *
simon 0:350011bf8be7 553 * @param numdns index of the DNS server in the dns_servers table
simon 0:350011bf8be7 554 * @param name hostname to query
simon 0:350011bf8be7 555 * @param id index of the hostname in dns_table, used as transaction ID in the
simon 0:350011bf8be7 556 * DNS query packet
simon 0:350011bf8be7 557 * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise
simon 0:350011bf8be7 558 */
simon 0:350011bf8be7 559 static err_t
simon 0:350011bf8be7 560 dns_send(u8_t numdns, const char* name, u8_t id)
simon 0:350011bf8be7 561 {
simon 0:350011bf8be7 562 err_t err;
simon 0:350011bf8be7 563 struct dns_hdr *hdr;
simon 0:350011bf8be7 564 struct dns_query qry;
simon 0:350011bf8be7 565 struct pbuf *p;
simon 0:350011bf8be7 566 char *query, *nptr;
simon 0:350011bf8be7 567 const char *pHostname;
simon 0:350011bf8be7 568 u8_t n;
simon 0:350011bf8be7 569
simon 0:350011bf8be7 570 LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n",
simon 0:350011bf8be7 571 (u16_t)(numdns), name));
simon 0:350011bf8be7 572 LWIP_ASSERT("dns server out of array", numdns < DNS_MAX_SERVERS);
simon 0:350011bf8be7 573 LWIP_ASSERT("dns server has no IP address set", !ip_addr_isany(&dns_servers[numdns]));
simon 0:350011bf8be7 574
simon 0:350011bf8be7 575 /* if here, we have either a new query or a retry on a previous query to process */
simon 0:350011bf8be7 576 p = pbuf_alloc(PBUF_TRANSPORT, SIZEOF_DNS_HDR + DNS_MAX_NAME_LENGTH +
simon 0:350011bf8be7 577 SIZEOF_DNS_QUERY, PBUF_RAM);
simon 0:350011bf8be7 578 if (p != NULL) {
simon 0:350011bf8be7 579 LWIP_ASSERT("pbuf must be in one piece", p->next == NULL);
simon 0:350011bf8be7 580 /* fill dns header */
simon 0:350011bf8be7 581 hdr = (struct dns_hdr*)p->payload;
simon 0:350011bf8be7 582 memset(hdr, 0, SIZEOF_DNS_HDR);
simon 0:350011bf8be7 583 hdr->id = htons(id);
simon 0:350011bf8be7 584 hdr->flags1 = DNS_FLAG1_RD;
simon 0:350011bf8be7 585 hdr->numquestions = PP_HTONS(1);
simon 0:350011bf8be7 586 query = (char*)hdr + SIZEOF_DNS_HDR;
simon 0:350011bf8be7 587 pHostname = name;
simon 0:350011bf8be7 588 --pHostname;
simon 0:350011bf8be7 589
simon 0:350011bf8be7 590 /* convert hostname into suitable query format. */
simon 0:350011bf8be7 591 do {
simon 0:350011bf8be7 592 ++pHostname;
simon 0:350011bf8be7 593 nptr = query;
simon 0:350011bf8be7 594 ++query;
simon 0:350011bf8be7 595 for(n = 0; *pHostname != '.' && *pHostname != 0; ++pHostname) {
simon 0:350011bf8be7 596 *query = *pHostname;
simon 0:350011bf8be7 597 ++query;
simon 0:350011bf8be7 598 ++n;
simon 0:350011bf8be7 599 }
simon 0:350011bf8be7 600 *nptr = n;
simon 0:350011bf8be7 601 } while(*pHostname != 0);
simon 0:350011bf8be7 602 *query++='\0';
simon 0:350011bf8be7 603
simon 0:350011bf8be7 604 /* fill dns query */
simon 0:350011bf8be7 605 qry.type = PP_HTONS(DNS_RRTYPE_A);
simon 0:350011bf8be7 606 qry.cls = PP_HTONS(DNS_RRCLASS_IN);
simon 0:350011bf8be7 607 SMEMCPY(query, &qry, SIZEOF_DNS_QUERY);
simon 0:350011bf8be7 608
simon 0:350011bf8be7 609 /* resize pbuf to the exact dns query */
simon 0:350011bf8be7 610 pbuf_realloc(p, (u16_t)((query + SIZEOF_DNS_QUERY) - ((char*)(p->payload))));
simon 0:350011bf8be7 611
simon 0:350011bf8be7 612 /* connect to the server for faster receiving */
simon 0:350011bf8be7 613 udp_connect(dns_pcb, &dns_servers[numdns], DNS_SERVER_PORT);
simon 0:350011bf8be7 614 /* send dns packet */
simon 0:350011bf8be7 615 err = udp_sendto(dns_pcb, p, &dns_servers[numdns], DNS_SERVER_PORT);
simon 0:350011bf8be7 616
simon 0:350011bf8be7 617 /* free pbuf */
simon 0:350011bf8be7 618 pbuf_free(p);
simon 0:350011bf8be7 619 } else {
simon 0:350011bf8be7 620 err = ERR_MEM;
simon 0:350011bf8be7 621 }
simon 0:350011bf8be7 622
simon 0:350011bf8be7 623 return err;
simon 0:350011bf8be7 624 }
simon 0:350011bf8be7 625
simon 0:350011bf8be7 626 /**
simon 0:350011bf8be7 627 * dns_check_entry() - see if pEntry has not yet been queried and, if so, sends out a query.
simon 0:350011bf8be7 628 * Check an entry in the dns_table:
simon 0:350011bf8be7 629 * - send out query for new entries
simon 0:350011bf8be7 630 * - retry old pending entries on timeout (also with different servers)
simon 0:350011bf8be7 631 * - remove completed entries from the table if their TTL has expired
simon 0:350011bf8be7 632 *
simon 0:350011bf8be7 633 * @param i index of the dns_table entry to check
simon 0:350011bf8be7 634 */
simon 0:350011bf8be7 635 static void
simon 0:350011bf8be7 636 dns_check_entry(u8_t i)
simon 0:350011bf8be7 637 {
simon 0:350011bf8be7 638 err_t err;
simon 0:350011bf8be7 639 struct dns_table_entry *pEntry = &dns_table[i];
simon 0:350011bf8be7 640
simon 0:350011bf8be7 641 LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE);
simon 0:350011bf8be7 642
simon 0:350011bf8be7 643 switch(pEntry->state) {
simon 0:350011bf8be7 644
simon 0:350011bf8be7 645 case DNS_STATE_NEW: {
simon 0:350011bf8be7 646 /* initialize new entry */
simon 0:350011bf8be7 647 pEntry->state = DNS_STATE_ASKING;
simon 0:350011bf8be7 648 pEntry->numdns = 0;
simon 0:350011bf8be7 649 pEntry->tmr = 1;
simon 0:350011bf8be7 650 pEntry->retries = 0;
simon 0:350011bf8be7 651
simon 0:350011bf8be7 652 /* send DNS packet for this entry */
simon 0:350011bf8be7 653 err = dns_send(pEntry->numdns, pEntry->name, i);
simon 0:350011bf8be7 654 if (err != ERR_OK) {
simon 0:350011bf8be7 655 LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
simon 0:350011bf8be7 656 ("dns_send returned error: %d\n", err)); //FIX DG, lwip_strerr is part of lwip/api
simon 0:350011bf8be7 657 }
simon 0:350011bf8be7 658 break;
simon 0:350011bf8be7 659 }
simon 0:350011bf8be7 660
simon 0:350011bf8be7 661 case DNS_STATE_ASKING: {
simon 0:350011bf8be7 662 if (--pEntry->tmr == 0) {
simon 0:350011bf8be7 663 if (++pEntry->retries == DNS_MAX_RETRIES) {
simon 0:350011bf8be7 664 if ((pEntry->numdns+1<DNS_MAX_SERVERS) && !ip_addr_isany(&dns_servers[pEntry->numdns+1])) {
simon 0:350011bf8be7 665 /* change of server */
simon 0:350011bf8be7 666 pEntry->numdns++;
simon 0:350011bf8be7 667 pEntry->tmr = 1;
simon 0:350011bf8be7 668 pEntry->retries = 0;
simon 0:350011bf8be7 669 break;
simon 0:350011bf8be7 670 } else {
simon 0:350011bf8be7 671 LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", pEntry->name));
simon 0:350011bf8be7 672 /* call specified callback function if provided */
simon 0:350011bf8be7 673 if (pEntry->found)
simon 0:350011bf8be7 674 (*pEntry->found)(pEntry->name, NULL, pEntry->arg);
simon 0:350011bf8be7 675 /* flush this entry */
simon 0:350011bf8be7 676 pEntry->state = DNS_STATE_UNUSED;
simon 0:350011bf8be7 677 pEntry->found = NULL;
simon 0:350011bf8be7 678 break;
simon 0:350011bf8be7 679 }
simon 0:350011bf8be7 680 }
simon 0:350011bf8be7 681
simon 0:350011bf8be7 682 /* wait longer for the next retry */
simon 0:350011bf8be7 683 pEntry->tmr = pEntry->retries;
simon 0:350011bf8be7 684
simon 0:350011bf8be7 685 /* send DNS packet for this entry */
simon 0:350011bf8be7 686 err = dns_send(pEntry->numdns, pEntry->name, i);
simon 0:350011bf8be7 687 if (err != ERR_OK) {
simon 0:350011bf8be7 688 LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
simon 0:350011bf8be7 689 ("dns_send returned error: %d\n", err)); //FIX DG, lwip_strerr is part of lwip/api
simon 0:350011bf8be7 690 }
simon 0:350011bf8be7 691 }
simon 0:350011bf8be7 692 break;
simon 0:350011bf8be7 693 }
simon 0:350011bf8be7 694
simon 0:350011bf8be7 695 case DNS_STATE_DONE: {
simon 0:350011bf8be7 696 /* if the time to live is nul */
simon 0:350011bf8be7 697 if (--pEntry->ttl == 0) {
simon 0:350011bf8be7 698 LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name));
simon 0:350011bf8be7 699 /* flush this entry */
simon 0:350011bf8be7 700 pEntry->state = DNS_STATE_UNUSED;
simon 0:350011bf8be7 701 pEntry->found = NULL;
simon 0:350011bf8be7 702 }
simon 0:350011bf8be7 703 break;
simon 0:350011bf8be7 704 }
simon 0:350011bf8be7 705 case DNS_STATE_UNUSED:
simon 0:350011bf8be7 706 /* nothing to do */
simon 0:350011bf8be7 707 break;
simon 0:350011bf8be7 708 default:
simon 0:350011bf8be7 709 LWIP_ASSERT("unknown dns_table entry state:", 0);
simon 0:350011bf8be7 710 break;
simon 0:350011bf8be7 711 }
simon 0:350011bf8be7 712 }
simon 0:350011bf8be7 713
simon 0:350011bf8be7 714 /**
simon 0:350011bf8be7 715 * Call dns_check_entry for each entry in dns_table - check all entries.
simon 0:350011bf8be7 716 */
simon 0:350011bf8be7 717 static void
simon 0:350011bf8be7 718 dns_check_entries(void)
simon 0:350011bf8be7 719 {
simon 0:350011bf8be7 720 u8_t i;
simon 0:350011bf8be7 721
simon 0:350011bf8be7 722 for (i = 0; i < DNS_TABLE_SIZE; ++i) {
simon 0:350011bf8be7 723 dns_check_entry(i);
simon 0:350011bf8be7 724 }
simon 0:350011bf8be7 725 }
simon 0:350011bf8be7 726
simon 0:350011bf8be7 727 /**
simon 0:350011bf8be7 728 * Receive input function for DNS response packets arriving for the dns UDP pcb.
simon 0:350011bf8be7 729 *
simon 0:350011bf8be7 730 * @params see udp.h
simon 0:350011bf8be7 731 */
simon 0:350011bf8be7 732 static void
simon 0:350011bf8be7 733 dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
simon 0:350011bf8be7 734 {
simon 0:350011bf8be7 735 u16_t i;
simon 0:350011bf8be7 736 char *pHostname;
simon 0:350011bf8be7 737 struct dns_hdr *hdr;
simon 0:350011bf8be7 738 struct dns_answer ans;
simon 0:350011bf8be7 739 struct dns_table_entry *pEntry;
simon 0:350011bf8be7 740 u16_t nquestions, nanswers;
simon 0:350011bf8be7 741
simon 0:350011bf8be7 742 LWIP_UNUSED_ARG(arg);
simon 0:350011bf8be7 743 LWIP_UNUSED_ARG(pcb);
simon 0:350011bf8be7 744 LWIP_UNUSED_ARG(addr);
simon 0:350011bf8be7 745 LWIP_UNUSED_ARG(port);
simon 0:350011bf8be7 746
simon 0:350011bf8be7 747 /* is the dns message too big ? */
simon 0:350011bf8be7 748 if (p->tot_len > DNS_MSG_SIZE) {
simon 0:350011bf8be7 749 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too big\n"));
simon 0:350011bf8be7 750 /* free pbuf and return */
simon 0:350011bf8be7 751 goto memerr;
simon 0:350011bf8be7 752 }
simon 0:350011bf8be7 753
simon 0:350011bf8be7 754 /* is the dns message big enough ? */
simon 0:350011bf8be7 755 if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY + SIZEOF_DNS_ANSWER)) {
simon 0:350011bf8be7 756 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n"));
simon 0:350011bf8be7 757 /* free pbuf and return */
simon 0:350011bf8be7 758 goto memerr;
simon 0:350011bf8be7 759 }
simon 0:350011bf8be7 760
simon 0:350011bf8be7 761 /* copy dns payload inside static buffer for processing */
simon 0:350011bf8be7 762 if (pbuf_copy_partial(p, dns_payload, p->tot_len, 0) == p->tot_len) {
simon 0:350011bf8be7 763 /* The ID in the DNS header should be our entry into the name table. */
simon 0:350011bf8be7 764 hdr = (struct dns_hdr*)dns_payload;
simon 0:350011bf8be7 765 i = htons(hdr->id);
simon 0:350011bf8be7 766 if (i < DNS_TABLE_SIZE) {
simon 0:350011bf8be7 767 pEntry = &dns_table[i];
simon 0:350011bf8be7 768 if(pEntry->state == DNS_STATE_ASKING) {
simon 0:350011bf8be7 769 /* This entry is now completed. */
simon 0:350011bf8be7 770 pEntry->state = DNS_STATE_DONE;
simon 0:350011bf8be7 771 pEntry->err = hdr->flags2 & DNS_FLAG2_ERR_MASK;
simon 0:350011bf8be7 772
simon 0:350011bf8be7 773 /* We only care about the question(s) and the answers. The authrr
simon 0:350011bf8be7 774 and the extrarr are simply discarded. */
simon 0:350011bf8be7 775 nquestions = htons(hdr->numquestions);
simon 0:350011bf8be7 776 nanswers = htons(hdr->numanswers);
simon 0:350011bf8be7 777
simon 0:350011bf8be7 778 /* Check for error. If so, call callback to inform. */
simon 0:350011bf8be7 779 if (((hdr->flags1 & DNS_FLAG1_RESPONSE) == 0) || (pEntry->err != 0) || (nquestions != 1)) {
simon 0:350011bf8be7 780 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", pEntry->name));
simon 0:350011bf8be7 781 /* call callback to indicate error, clean up memory and return */
simon 0:350011bf8be7 782 goto responseerr;
simon 0:350011bf8be7 783 }
simon 0:350011bf8be7 784
simon 0:350011bf8be7 785 #if DNS_DOES_NAME_CHECK
simon 0:350011bf8be7 786 /* Check if the name in the "question" part match with the name in the entry. */
simon 0:350011bf8be7 787 if (dns_compare_name((unsigned char *)(pEntry->name), (unsigned char *)dns_payload + SIZEOF_DNS_HDR) != 0) {
simon 0:350011bf8be7 788 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", pEntry->name));
simon 0:350011bf8be7 789 /* call callback to indicate error, clean up memory and return */
simon 0:350011bf8be7 790 goto responseerr;
simon 0:350011bf8be7 791 }
simon 0:350011bf8be7 792 #endif /* DNS_DOES_NAME_CHECK */
simon 0:350011bf8be7 793
simon 0:350011bf8be7 794 /* Skip the name in the "question" part */
simon 0:350011bf8be7 795 pHostname = (char *) dns_parse_name((unsigned char *)dns_payload + SIZEOF_DNS_HDR) + SIZEOF_DNS_QUERY;
simon 0:350011bf8be7 796
simon 0:350011bf8be7 797 while (nanswers > 0) {
simon 0:350011bf8be7 798 /* skip answer resource record's host name */
simon 0:350011bf8be7 799 pHostname = (char *) dns_parse_name((unsigned char *)pHostname);
simon 0:350011bf8be7 800
simon 0:350011bf8be7 801 /* Check for IP address type and Internet class. Others are discarded. */
simon 0:350011bf8be7 802 SMEMCPY(&ans, pHostname, SIZEOF_DNS_ANSWER);
simon 0:350011bf8be7 803 if((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) &&
simon 0:350011bf8be7 804 (ans.len == PP_HTONS(sizeof(ip_addr_t))) ) {
simon 0:350011bf8be7 805 /* read the answer resource record's TTL, and maximize it if needed */
simon 0:350011bf8be7 806 pEntry->ttl = ntohl(ans.ttl);
simon 0:350011bf8be7 807 if (pEntry->ttl > DNS_MAX_TTL) {
simon 0:350011bf8be7 808 pEntry->ttl = DNS_MAX_TTL;
simon 0:350011bf8be7 809 }
simon 0:350011bf8be7 810 /* read the IP address after answer resource record's header */
simon 0:350011bf8be7 811 SMEMCPY(&(pEntry->ipaddr), (pHostname+SIZEOF_DNS_ANSWER), sizeof(ip_addr_t));
simon 0:350011bf8be7 812 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", pEntry->name));
simon 0:350011bf8be7 813 ip_addr_debug_print(DNS_DEBUG, (&(pEntry->ipaddr)));
simon 0:350011bf8be7 814 LWIP_DEBUGF(DNS_DEBUG, ("\n"));
simon 0:350011bf8be7 815 /* call specified callback function if provided */
simon 0:350011bf8be7 816 if (pEntry->found) {
simon 0:350011bf8be7 817 (*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg);
simon 0:350011bf8be7 818 }
simon 0:350011bf8be7 819 /* deallocate memory and return */
simon 0:350011bf8be7 820 goto memerr;
simon 0:350011bf8be7 821 } else {
simon 0:350011bf8be7 822 pHostname = pHostname + SIZEOF_DNS_ANSWER + htons(ans.len);
simon 0:350011bf8be7 823 }
simon 0:350011bf8be7 824 --nanswers;
simon 0:350011bf8be7 825 }
simon 0:350011bf8be7 826 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", pEntry->name));
simon 0:350011bf8be7 827 /* call callback to indicate error, clean up memory and return */
simon 0:350011bf8be7 828 goto responseerr;
simon 0:350011bf8be7 829 }
simon 0:350011bf8be7 830 }
simon 0:350011bf8be7 831 }
simon 0:350011bf8be7 832
simon 0:350011bf8be7 833 /* deallocate memory and return */
simon 0:350011bf8be7 834 goto memerr;
simon 0:350011bf8be7 835
simon 0:350011bf8be7 836 responseerr:
simon 0:350011bf8be7 837 /* ERROR: call specified callback function with NULL as name to indicate an error */
simon 0:350011bf8be7 838 if (pEntry->found) {
simon 0:350011bf8be7 839 (*pEntry->found)(pEntry->name, NULL, pEntry->arg);
simon 0:350011bf8be7 840 }
simon 0:350011bf8be7 841 /* flush this entry */
simon 0:350011bf8be7 842 pEntry->state = DNS_STATE_UNUSED;
simon 0:350011bf8be7 843 pEntry->found = NULL;
simon 0:350011bf8be7 844
simon 0:350011bf8be7 845 memerr:
simon 0:350011bf8be7 846 /* free pbuf */
simon 0:350011bf8be7 847 pbuf_free(p);
simon 0:350011bf8be7 848 return;
simon 0:350011bf8be7 849 }
simon 0:350011bf8be7 850
simon 0:350011bf8be7 851 /**
simon 0:350011bf8be7 852 * Queues a new hostname to resolve and sends out a DNS query for that hostname
simon 0:350011bf8be7 853 *
simon 0:350011bf8be7 854 * @param name the hostname that is to be queried
simon 0:350011bf8be7 855 * @param found a callback founction to be called on success, failure or timeout
simon 0:350011bf8be7 856 * @param callback_arg argument to pass to the callback function
simon 0:350011bf8be7 857 * @return @return a err_t return code.
simon 0:350011bf8be7 858 */
simon 0:350011bf8be7 859 static err_t
simon 0:350011bf8be7 860 dns_enqueue(const char *name, dns_found_callback found, void *callback_arg)
simon 0:350011bf8be7 861 {
simon 0:350011bf8be7 862 u8_t i;
simon 0:350011bf8be7 863 u8_t lseq, lseqi;
simon 0:350011bf8be7 864 struct dns_table_entry *pEntry = NULL;
simon 0:350011bf8be7 865 size_t namelen;
simon 0:350011bf8be7 866
simon 0:350011bf8be7 867 /* search an unused entry, or the oldest one */
simon 0:350011bf8be7 868 lseq = lseqi = 0;
simon 0:350011bf8be7 869 for (i = 0; i < DNS_TABLE_SIZE; ++i) {
simon 0:350011bf8be7 870 pEntry = &dns_table[i];
simon 0:350011bf8be7 871 /* is it an unused entry ? */
simon 0:350011bf8be7 872 if (pEntry->state == DNS_STATE_UNUSED)
simon 0:350011bf8be7 873 break;
simon 0:350011bf8be7 874
simon 0:350011bf8be7 875 /* check if this is the oldest completed entry */
simon 0:350011bf8be7 876 if (pEntry->state == DNS_STATE_DONE) {
simon 0:350011bf8be7 877 if ((dns_seqno - pEntry->seqno) > lseq) {
simon 0:350011bf8be7 878 lseq = dns_seqno - pEntry->seqno;
simon 0:350011bf8be7 879 lseqi = i;
simon 0:350011bf8be7 880 }
simon 0:350011bf8be7 881 }
simon 0:350011bf8be7 882 }
simon 0:350011bf8be7 883
simon 0:350011bf8be7 884 /* if we don't have found an unused entry, use the oldest completed one */
simon 0:350011bf8be7 885 if (i == DNS_TABLE_SIZE) {
simon 0:350011bf8be7 886 if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) {
simon 0:350011bf8be7 887 /* no entry can't be used now, table is full */
simon 0:350011bf8be7 888 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name));
simon 0:350011bf8be7 889 return ERR_MEM;
simon 0:350011bf8be7 890 } else {
simon 0:350011bf8be7 891 /* use the oldest completed one */
simon 0:350011bf8be7 892 i = lseqi;
simon 0:350011bf8be7 893 pEntry = &dns_table[i];
simon 0:350011bf8be7 894 }
simon 0:350011bf8be7 895 }
simon 0:350011bf8be7 896
simon 0:350011bf8be7 897 /* use this entry */
simon 0:350011bf8be7 898 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i)));
simon 0:350011bf8be7 899
simon 0:350011bf8be7 900 /* fill the entry */
simon 0:350011bf8be7 901 pEntry->state = DNS_STATE_NEW;
simon 0:350011bf8be7 902 pEntry->seqno = dns_seqno++;
simon 0:350011bf8be7 903 pEntry->found = found;
simon 0:350011bf8be7 904 pEntry->arg = callback_arg;
simon 0:350011bf8be7 905 namelen = LWIP_MIN(strlen(name), DNS_MAX_NAME_LENGTH-1);
simon 0:350011bf8be7 906 MEMCPY(pEntry->name, name, namelen);
simon 0:350011bf8be7 907 pEntry->name[namelen] = 0;
simon 0:350011bf8be7 908
simon 0:350011bf8be7 909 /* force to send query without waiting timer */
simon 0:350011bf8be7 910 dns_check_entry(i);
simon 0:350011bf8be7 911
simon 0:350011bf8be7 912 /* dns query is enqueued */
simon 0:350011bf8be7 913 return ERR_INPROGRESS;
simon 0:350011bf8be7 914 }
simon 0:350011bf8be7 915
simon 0:350011bf8be7 916 /**
simon 0:350011bf8be7 917 * Resolve a hostname (string) into an IP address.
simon 0:350011bf8be7 918 * NON-BLOCKING callback version for use with raw API!!!
simon 0:350011bf8be7 919 *
simon 0:350011bf8be7 920 * Returns immediately with one of err_t return codes:
simon 0:350011bf8be7 921 * - ERR_OK if hostname is a valid IP address string or the host
simon 0:350011bf8be7 922 * name is already in the local names table.
simon 0:350011bf8be7 923 * - ERR_INPROGRESS enqueue a request to be sent to the DNS server
simon 0:350011bf8be7 924 * for resolution if no errors are present.
simon 0:350011bf8be7 925 *
simon 0:350011bf8be7 926 * @param hostname the hostname that is to be queried
simon 0:350011bf8be7 927 * @param addr pointer to a ip_addr_t where to store the address if it is already
simon 0:350011bf8be7 928 * cached in the dns_table (only valid if ERR_OK is returned!)
simon 0:350011bf8be7 929 * @param found a callback function to be called on success, failure or timeout (only if
simon 0:350011bf8be7 930 * ERR_INPROGRESS is returned!)
simon 0:350011bf8be7 931 * @param callback_arg argument to pass to the callback function
simon 0:350011bf8be7 932 * @return a err_t return code.
simon 0:350011bf8be7 933 */
simon 0:350011bf8be7 934 err_t
simon 0:350011bf8be7 935 dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found,
simon 0:350011bf8be7 936 void *callback_arg)
simon 0:350011bf8be7 937 {
simon 0:350011bf8be7 938 u32_t ipaddr;
simon 0:350011bf8be7 939 /* not initialized or no valid server yet, or invalid addr pointer
simon 0:350011bf8be7 940 * or invalid hostname or invalid hostname length */
simon 0:350011bf8be7 941 if ((dns_pcb == NULL) || (addr == NULL) ||
simon 0:350011bf8be7 942 (!hostname) || (!hostname[0]) ||
simon 0:350011bf8be7 943 (strlen(hostname) >= DNS_MAX_NAME_LENGTH)) {
simon 0:350011bf8be7 944 return ERR_VAL;
simon 0:350011bf8be7 945 }
simon 0:350011bf8be7 946
simon 0:350011bf8be7 947 #if LWIP_HAVE_LOOPIF
simon 0:350011bf8be7 948 if (strcmp(hostname, "localhost")==0) {
simon 0:350011bf8be7 949 ip_addr_set_loopback(addr);
simon 0:350011bf8be7 950 return ERR_OK;
simon 0:350011bf8be7 951 }
simon 0:350011bf8be7 952 #endif /* LWIP_HAVE_LOOPIF */
simon 0:350011bf8be7 953
simon 0:350011bf8be7 954 /* host name already in octet notation? set ip addr and return ERR_OK */
simon 0:350011bf8be7 955 ipaddr = ipaddr_addr(hostname);
simon 0:350011bf8be7 956 if (ipaddr == IPADDR_NONE) {
simon 0:350011bf8be7 957 /* already have this address cached? */
simon 0:350011bf8be7 958 ipaddr = dns_lookup(hostname);
simon 0:350011bf8be7 959 }
simon 0:350011bf8be7 960 if (ipaddr != IPADDR_NONE) {
simon 0:350011bf8be7 961 ip4_addr_set_u32(addr, ipaddr);
simon 0:350011bf8be7 962 return ERR_OK;
simon 0:350011bf8be7 963 }
simon 0:350011bf8be7 964
simon 0:350011bf8be7 965 /* queue query with specified callback */
simon 0:350011bf8be7 966 return dns_enqueue(hostname, found, callback_arg);
simon 0:350011bf8be7 967 }
simon 0:350011bf8be7 968
simon 0:350011bf8be7 969 #endif /* LWIP_DNS */