Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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