Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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