I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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