Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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