Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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