NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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