ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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