Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

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