A modified version of http://mbed.org/users/segundo/libraries/NetServices/ljhqix to add HTTP proxy feature (based on http://mbed.org/users/igorsk/programs/NetServicesSource/ltjpag)

Dependents:   SenseClient

Committer:
mimil
Date:
Tue Sep 06 13:26:45 2011 +0000
Revision:
0:308f83189a3f

        

Who changed what in which revision?

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