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

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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