SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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