Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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