A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

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