Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Thu Feb 23 21:38:39 2012 +0000
Revision:
0:616601bde9fb

        

Who changed what in which revision?

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