Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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