Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Fri Jun 06 09:06:03 2014 +0000
Revision:
0:51f1ef89ec7b
06062014

Who changed what in which revision?

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