Philippe Bazot / Mbed 2 deprecated DU4SmartCities

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

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