• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * DNS - host name to IP address resolver.
4  *
5  * @defgroup dns DNS
6  * @ingroup callbackstyle_api
7  *
8  * Implements a DNS host name to IP address resolver.
9  *
10  * The lwIP DNS resolver functions are used to lookup a host name and
11  * map it to a numerical IP address. It maintains a list of resolved
12  * hostnames that can be queried with the dns_lookup() function.
13  * New hostnames can be resolved using the dns_query() function.
14  *
15  * The lwIP version of the resolver also adds a non-blocking version of
16  * gethostbyname() that will work with a raw API application. This function
17  * checks for an IP address string first and converts it if it is valid.
18  * gethostbyname() then does a dns_lookup() to see if the name is
19  * already in the table. If so, the IP is returned. If not, a query is
20  * issued and the function returns with a ERR_INPROGRESS status. The app
21  * using the dns client must then go into a waiting state.
22  *
23  * Once a hostname has been resolved (or found to be non-existent),
24  * the resolver code calls a specified callback function (which
25  * must be implemented by the module that uses the resolver).
26  *
27  * Multicast DNS queries are supported for names ending on ".local".
28  * However, only "One-Shot Multicast DNS Queries" are supported (RFC 6762
29  * chapter 5.1), this is not a fully compliant implementation of continuous
30  * mDNS querying!
31  *
32  * All functions must be called from TCPIP thread.
33  *
34  * @see DNS_MAX_SERVERS
35  * @see LWIP_DHCP_MAX_DNS_SERVERS
36  * @see @ref netconn_common for thread-safe access.
37  */
38 
39 /*
40  * Port to lwIP from uIP
41  * by Jim Pettinato April 2007
42  *
43  * security fixes and more by Simon Goldschmidt
44  *
45  * uIP version Copyright (c) 2002-2003, Adam Dunkels.
46  * All rights reserved.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. The name of the author may not be used to endorse or promote
57  *    products derived from this software without specific prior
58  *    written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
61  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
62  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
64  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
66  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
67  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
68  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
69  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
70  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71  */
72 
73 /*-----------------------------------------------------------------------------
74  * RFC 1035 - Domain names - implementation and specification
75  * RFC 2181 - Clarifications to the DNS Specification
76  *----------------------------------------------------------------------------*/
77 
78 /** @todo: define good default values (rfc compliance) */
79 /** @todo: improve answer parsing, more checkings... */
80 /** @todo: check RFC1035 - 7.3. Processing responses */
81 /** @todo: one-shot mDNS: dual-stack fallback to another IP version */
82 
83 /*-----------------------------------------------------------------------------
84  * Includes
85  *----------------------------------------------------------------------------*/
86 
87 #include "lwip/opt.h"
88 
89 #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
90 
91 #include "lwip/def.h"
92 #include "lwip/udp.h"
93 #include "lwip/mem.h"
94 #include "lwip/memp.h"
95 #include "lwip/dns.h"
96 #include "lwip/prot/dns.h"
97 
98 #include <string.h>
99 
100 /** Random generator function to create random TXIDs and source ports for queries */
101 #ifndef DNS_RAND_TXID
102 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0)
103 #define DNS_RAND_TXID LWIP_RAND
104 #else
105 static u16_t dns_txid;
106 #define DNS_RAND_TXID() (++dns_txid)
107 #endif
108 #endif
109 
110 /** Limits the source port to be >= 1024 by default */
111 #ifndef DNS_PORT_ALLOWED
112 #define DNS_PORT_ALLOWED(port) ((port) >= 1024)
113 #endif
114 
115 /** DNS resource record max. TTL (one week as default) */
116 #ifndef DNS_MAX_TTL
117 #define DNS_MAX_TTL               604800
118 #elif DNS_MAX_TTL > 0x7FFFFFFF
119 #error DNS_MAX_TTL must be a positive 32-bit value
120 #endif
121 
122 #if DNS_TABLE_SIZE > 255
123 #error DNS_TABLE_SIZE must fit into an u8_t
124 #endif
125 #if DNS_MAX_SERVERS > 255
126 #error DNS_MAX_SERVERS must fit into an u8_t
127 #endif
128 
129 /* The number of parallel requests (i.e. calls to dns_gethostbyname
130  * that cannot be answered from the DNS table.
131  * This is set to the table size by default.
132  */
133 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
134 #ifndef DNS_MAX_REQUESTS
135 #define DNS_MAX_REQUESTS          DNS_TABLE_SIZE
136 #else
137 #if DNS_MAX_REQUESTS > 255
138 #error DNS_MAX_REQUESTS must fit into an u8_t
139 #endif
140 #endif
141 #else
142 /* In this configuration, both arrays have to have the same size and are used
143  * like one entry (used/free) */
144 #define DNS_MAX_REQUESTS          DNS_TABLE_SIZE
145 #endif
146 
147 /* The number of UDP source ports used in parallel */
148 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
149 #ifndef DNS_MAX_SOURCE_PORTS
150 #define DNS_MAX_SOURCE_PORTS      DNS_MAX_REQUESTS
151 #else
152 #if DNS_MAX_SOURCE_PORTS > 255
153 #error DNS_MAX_SOURCE_PORTS must fit into an u8_t
154 #endif
155 #endif
156 #else
157 #ifdef DNS_MAX_SOURCE_PORTS
158 #undef DNS_MAX_SOURCE_PORTS
159 #endif
160 #define DNS_MAX_SOURCE_PORTS      1
161 #endif
162 
163 #if LWIP_IPV4 && LWIP_IPV6
164 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) (((t) == LWIP_DNS_ADDRTYPE_IPV6_IPV4) || ((t) == LWIP_DNS_ADDRTYPE_IPV6))
165 #define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) (IP_IS_V6_VAL(ip) ? LWIP_DNS_ADDRTYPE_IS_IPV6(t) : (!LWIP_DNS_ADDRTYPE_IS_IPV6(t)))
166 #define LWIP_DNS_ADDRTYPE_ARG(x) , x
167 #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) x
168 #define LWIP_DNS_SET_ADDRTYPE(x, y) do { x = y; } while(0)
169 #else
170 #if LWIP_IPV6
171 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 1
172 #else
173 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 0
174 #endif
175 #define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) 1
176 #define LWIP_DNS_ADDRTYPE_ARG(x)
177 #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) 0
178 #define LWIP_DNS_SET_ADDRTYPE(x, y)
179 #endif /* LWIP_IPV4 && LWIP_IPV6 */
180 
181 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
182 #define LWIP_DNS_ISMDNS_ARG(x) , x
183 #else
184 #define LWIP_DNS_ISMDNS_ARG(x)
185 #endif
186 
187 /** DNS query message structure.
188     No packing needed: only used locally on the stack. */
189 struct dns_query {
190   /* DNS query record starts with either a domain name or a pointer
191      to a name already present somewhere in the packet. */
192   u16_t type;
193   u16_t cls;
194 };
195 #define SIZEOF_DNS_QUERY 4
196 
197 /** DNS answer message structure.
198     No packing needed: only used locally on the stack. */
199 struct dns_answer {
200   /* DNS answer record starts with either a domain name or a pointer
201      to a name already present somewhere in the packet. */
202   u16_t type;
203   u16_t cls;
204   u32_t ttl;
205   u16_t len;
206 };
207 #define SIZEOF_DNS_ANSWER 10
208 /* maximum allowed size for the struct due to non-packed */
209 #define SIZEOF_DNS_ANSWER_ASSERT 12
210 
211 /* DNS table entry states */
212 typedef enum {
213   DNS_STATE_UNUSED           = 0,
214   DNS_STATE_NEW              = 1,
215   DNS_STATE_ASKING           = 2,
216   DNS_STATE_DONE             = 3
217 } dns_state_enum_t;
218 
219 /** DNS table entry */
220 struct dns_table_entry {
221   u32_t ttl;
222   ip_addr_t ipaddr;
223   u16_t txid;
224   u8_t  state;
225   u8_t  server_idx;
226   u8_t  tmr;
227   u8_t  retries;
228   u8_t  seqno;
229 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
230   u8_t pcb_idx;
231 #endif
232   char name[DNS_MAX_NAME_LENGTH];
233 #if LWIP_IPV4 && LWIP_IPV6
234   u8_t reqaddrtype;
235 #endif /* LWIP_IPV4 && LWIP_IPV6 */
236 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
237   u8_t is_mdns;
238 #endif
239 };
240 
241 /** DNS request table entry: used when dns_gehostbyname cannot answer the
242  * request from the DNS table */
243 struct dns_req_entry {
244   /* pointer to callback on DNS query done */
245   dns_found_callback found;
246   /* argument passed to the callback function */
247   void *arg;
248 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
249   u8_t dns_table_idx;
250 #endif
251 #if LWIP_IPV4 && LWIP_IPV6
252   u8_t reqaddrtype;
253 #endif /* LWIP_IPV4 && LWIP_IPV6 */
254 };
255 
256 #if DNS_LOCAL_HOSTLIST
257 
258 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
259 /** Local host-list. For hostnames in this list, no
260  *  external name resolution is performed */
261 static struct local_hostlist_entry *local_hostlist_dynamic;
262 #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
263 
264 /** Defining this allows the local_hostlist_static to be placed in a different
265  * linker section (e.g. FLASH) */
266 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE
267 #define DNS_LOCAL_HOSTLIST_STORAGE_PRE static
268 #endif /* DNS_LOCAL_HOSTLIST_STORAGE_PRE */
269 /** Defining this allows the local_hostlist_static to be placed in a different
270  * linker section (e.g. FLASH) */
271 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST
272 #define DNS_LOCAL_HOSTLIST_STORAGE_POST
273 #endif /* DNS_LOCAL_HOSTLIST_STORAGE_POST */
274 DNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[]
275   DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT;
276 
277 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
278 
279 static void dns_init_local(void);
280 static err_t dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype));
281 #endif /* DNS_LOCAL_HOSTLIST */
282 
283 
284 /* forward declarations */
285 static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
286 static void dns_check_entries(void);
287 static void dns_call_found(u8_t idx, ip_addr_t *addr);
288 
289 /*-----------------------------------------------------------------------------
290  * Globals
291  *----------------------------------------------------------------------------*/
292 
293 /* DNS variables */
294 static struct udp_pcb        *dns_pcbs[DNS_MAX_SOURCE_PORTS];
295 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
296 static u8_t                   dns_last_pcb_idx;
297 #endif
298 static u8_t                   dns_seqno;
299 static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
300 static struct dns_req_entry   dns_requests[DNS_MAX_REQUESTS];
301 static ip_addr_t              dns_servers[DNS_MAX_SERVERS];
302 
303 #if LWIP_IPV4
304 const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT;
305 #endif /* LWIP_IPV4 */
306 #if LWIP_IPV6
307 const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
308 #endif /* LWIP_IPV6 */
309 
310 /**
311  * Initialize the resolver: set up the UDP pcb and configure the default server
312  * (if DNS_SERVER_ADDRESS is set).
313  */
314 void
dns_init(void)315 dns_init(void)
316 {
317 #ifdef DNS_SERVER_ADDRESS
318   /* initialize default DNS server address */
319   ip_addr_t dnsserver;
320   DNS_SERVER_ADDRESS(&dnsserver);
321   dns_setserver(0, &dnsserver);
322 #ifdef DNS_SERVER_ADDRESS_SECONDARY
323   DNS_SERVER_ADDRESS_SECONDARY(&dnsserver);
324   dns_setserver(1, &dnsserver);
325 #endif
326 #endif /* DNS_SERVER_ADDRESS */
327 
328   LWIP_ASSERT("sanity check SIZEOF_DNS_QUERY",
329               sizeof(struct dns_query) == SIZEOF_DNS_QUERY);
330   LWIP_ASSERT("sanity check SIZEOF_DNS_ANSWER",
331               sizeof(struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT);
332 
333   LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n"));
334 
335   /* if dns client not yet initialized... */
336 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
337   if (dns_pcbs[0] == NULL) {
338     dns_pcbs[0] = udp_new_ip_type(IPADDR_TYPE_ANY);
339     LWIP_ASSERT("dns_pcbs[0] != NULL", dns_pcbs[0] != NULL);
340 
341     /* initialize DNS table not needed (initialized to zero since it is a
342      * global variable) */
343     LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0",
344                 DNS_STATE_UNUSED == 0);
345 
346     /* initialize DNS client */
347     udp_bind(dns_pcbs[0], IP_ANY_TYPE, 0);
348     udp_recv(dns_pcbs[0], dns_recv, NULL);
349   }
350 #endif
351 
352 #if DNS_LOCAL_HOSTLIST
353   dns_init_local();
354 #endif
355 }
356 
357 /**
358  * @ingroup dns
359  * Initialize one of the DNS servers.
360  *
361  * @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS
362  * @param dnsserver IP address of the DNS server to set
363  */
364 void
dns_setserver(u8_t numdns,const ip_addr_t * dnsserver)365 dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
366 {
367   if (numdns < DNS_MAX_SERVERS) {
368     if (dnsserver != NULL) {
369       dns_servers[numdns] = (*dnsserver);
370     } else {
371       dns_servers[numdns] = *IP_ADDR_ANY;
372     }
373   }
374 }
375 
376 /**
377  * @ingroup dns
378  * Obtain one of the currently configured DNS server.
379  *
380  * @param numdns the index of the DNS server
381  * @return IP address of the indexed DNS server or "ip_addr_any" if the DNS
382  *         server has not been configured.
383  */
384 const ip_addr_t *
dns_getserver(u8_t numdns)385 dns_getserver(u8_t numdns)
386 {
387   if (numdns < DNS_MAX_SERVERS) {
388     return &dns_servers[numdns];
389   } else {
390     return IP_ADDR_ANY;
391   }
392 }
393 
394 /**
395  * The DNS resolver client timer - handle retries and timeouts and should
396  * be called every DNS_TMR_INTERVAL milliseconds (every second by default).
397  */
398 void
dns_tmr(void)399 dns_tmr(void)
400 {
401   LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n"));
402   dns_check_entries();
403 }
404 
405 #if DNS_LOCAL_HOSTLIST
406 static void
dns_init_local(void)407 dns_init_local(void)
408 {
409 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT)
410   size_t i;
411   struct local_hostlist_entry *entry;
412   /* Dynamic: copy entries from DNS_LOCAL_HOSTLIST_INIT to list */
413   struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT;
414   size_t namelen;
415   for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) {
416     struct local_hostlist_entry *init_entry = &local_hostlist_init[i];
417     LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL);
418     namelen = strlen(init_entry->name);
419     LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
420     entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
421     LWIP_ASSERT("mem-error in dns_init_local", entry != NULL);
422     if (entry != NULL) {
423       char *entry_name = (char *)entry + sizeof(struct local_hostlist_entry);
424       MEMCPY(entry_name, init_entry->name, namelen);
425       entry_name[namelen] = 0;
426       entry->name = entry_name;
427       entry->addr = init_entry->addr;
428       entry->next = local_hostlist_dynamic;
429       local_hostlist_dynamic = entry;
430     }
431   }
432 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) */
433 }
434 
435 /**
436  * @ingroup dns
437  * Iterate the local host-list for a hostname.
438  *
439  * @param iterator_fn a function that is called for every entry in the local host-list
440  * @param iterator_arg 3rd argument passed to iterator_fn
441  * @return the number of entries in the local host-list
442  */
443 size_t
dns_local_iterate(dns_found_callback iterator_fn,void * iterator_arg)444 dns_local_iterate(dns_found_callback iterator_fn, void *iterator_arg)
445 {
446   size_t i;
447 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
448   struct local_hostlist_entry *entry = local_hostlist_dynamic;
449   i = 0;
450   while (entry != NULL) {
451     if (iterator_fn != NULL) {
452       iterator_fn(entry->name, &entry->addr, iterator_arg);
453     }
454     i++;
455     entry = entry->next;
456   }
457 #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
458   for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
459     if (iterator_fn != NULL) {
460       iterator_fn(local_hostlist_static[i].name, &local_hostlist_static[i].addr, iterator_arg);
461     }
462   }
463 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
464   return i;
465 }
466 
467 /**
468  * @ingroup dns
469  * Scans the local host-list for a hostname.
470  *
471  * @param hostname Hostname to look for in the local host-list
472  * @param addr the first IP address for the hostname in the local host-list or
473  *         IPADDR_NONE if not found.
474  * @param dns_addrtype - LWIP_DNS_ADDRTYPE_IPV4_IPV6: try to resolve IPv4 (ATTENTION: no fallback here!)
475  *                     - LWIP_DNS_ADDRTYPE_IPV6_IPV4: try to resolve IPv6 (ATTENTION: no fallback here!)
476  *                     - LWIP_DNS_ADDRTYPE_IPV4: try to resolve IPv4 only
477  *                     - LWIP_DNS_ADDRTYPE_IPV6: try to resolve IPv6 only
478  * @return ERR_OK if found, ERR_ARG if not found
479  */
480 err_t
dns_local_lookup(const char * hostname,ip_addr_t * addr,u8_t dns_addrtype)481 dns_local_lookup(const char *hostname, ip_addr_t *addr, u8_t dns_addrtype)
482 {
483   LWIP_UNUSED_ARG(dns_addrtype);
484   return dns_lookup_local(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype));
485 }
486 
487 /* Internal implementation for dns_local_lookup and dns_lookup */
488 static err_t
dns_lookup_local(const char * hostname,ip_addr_t * addr LWIP_DNS_ADDRTYPE_ARG (u8_t dns_addrtype))489 dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
490 {
491 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
492   struct local_hostlist_entry *entry = local_hostlist_dynamic;
493   while (entry != NULL) {
494     if ((lwip_stricmp(entry->name, hostname) == 0) &&
495         LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) {
496       if (addr) {
497         ip_addr_copy(*addr, entry->addr);
498       }
499       return ERR_OK;
500     }
501     entry = entry->next;
502   }
503 #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
504   size_t i;
505   for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
506     if ((lwip_stricmp(local_hostlist_static[i].name, hostname) == 0) &&
507         LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) {
508       if (addr) {
509         ip_addr_copy(*addr, local_hostlist_static[i].addr);
510       }
511       return ERR_OK;
512     }
513   }
514 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
515   return ERR_ARG;
516 }
517 
518 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
519 /**
520  * @ingroup dns
521  * Remove all entries from the local host-list for a specific hostname
522  * and/or IP address
523  *
524  * @param hostname hostname for which entries shall be removed from the local
525  *                 host-list
526  * @param addr address for which entries shall be removed from the local host-list
527  * @return the number of removed entries
528  */
529 int
dns_local_removehost(const char * hostname,const ip_addr_t * addr)530 dns_local_removehost(const char *hostname, const ip_addr_t *addr)
531 {
532   int removed = 0;
533   struct local_hostlist_entry *entry = local_hostlist_dynamic;
534   struct local_hostlist_entry *last_entry = NULL;
535   while (entry != NULL) {
536     if (((hostname == NULL) || !lwip_stricmp(entry->name, hostname)) &&
537         ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) {
538       struct local_hostlist_entry *free_entry;
539       if (last_entry != NULL) {
540         last_entry->next = entry->next;
541       } else {
542         local_hostlist_dynamic = entry->next;
543       }
544       free_entry = entry;
545       entry = entry->next;
546       memp_free(MEMP_LOCALHOSTLIST, free_entry);
547       removed++;
548     } else {
549       last_entry = entry;
550       entry = entry->next;
551     }
552   }
553   return removed;
554 }
555 
556 /**
557  * @ingroup dns
558  * Add a hostname/IP address pair to the local host-list.
559  * Duplicates are not checked.
560  *
561  * @param hostname hostname of the new entry
562  * @param addr IP address of the new entry
563  * @return ERR_OK if succeeded or ERR_MEM on memory error
564  */
565 err_t
dns_local_addhost(const char * hostname,const ip_addr_t * addr)566 dns_local_addhost(const char *hostname, const ip_addr_t *addr)
567 {
568   struct local_hostlist_entry *entry;
569   size_t namelen;
570   char *entry_name;
571   LWIP_ASSERT("invalid host name (NULL)", hostname != NULL);
572   namelen = strlen(hostname);
573   LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
574   entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
575   if (entry == NULL) {
576     return ERR_MEM;
577   }
578   entry_name = (char *)entry + sizeof(struct local_hostlist_entry);
579   MEMCPY(entry_name, hostname, namelen);
580   entry_name[namelen] = 0;
581   entry->name = entry_name;
582   ip_addr_copy(entry->addr, *addr);
583   entry->next = local_hostlist_dynamic;
584   local_hostlist_dynamic = entry;
585   return ERR_OK;
586 }
587 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC*/
588 #endif /* DNS_LOCAL_HOSTLIST */
589 
590 /**
591  * @ingroup dns
592  * Look up a hostname in the array of known hostnames.
593  *
594  * @note This function only looks in the internal array of known
595  * hostnames, it does not send out a query for the hostname if none
596  * was found. The function dns_enqueue() can be used to send a query
597  * for a hostname.
598  *
599  * @param name the hostname to look up
600  * @param addr the hostname's IP address, as u32_t (instead of ip_addr_t to
601  *         better check for failure: != IPADDR_NONE) or IPADDR_NONE if the hostname
602  *         was not found in the cached dns_table.
603  * @return ERR_OK if found, ERR_ARG if not found
604  */
605 static err_t
dns_lookup(const char * name,ip_addr_t * addr LWIP_DNS_ADDRTYPE_ARG (u8_t dns_addrtype))606 dns_lookup(const char *name, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
607 {
608   u8_t i;
609 #if DNS_LOCAL_HOSTLIST
610   if (dns_lookup_local(name, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) {
611     return ERR_OK;
612   }
613 #endif /* DNS_LOCAL_HOSTLIST */
614 #ifdef DNS_LOOKUP_LOCAL_EXTERN
615   if (DNS_LOOKUP_LOCAL_EXTERN(name, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) == ERR_OK) {
616     return ERR_OK;
617   }
618 #endif /* DNS_LOOKUP_LOCAL_EXTERN */
619 
620   /* Walk through name list, return entry if found. If not, return NULL. */
621   for (i = 0; i < DNS_TABLE_SIZE; ++i) {
622     if ((dns_table[i].state == DNS_STATE_DONE) &&
623         (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0) &&
624         LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) {
625       LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name));
626       ip_addr_debug_print_val(DNS_DEBUG, dns_table[i].ipaddr);
627       LWIP_DEBUGF(DNS_DEBUG, ("\n"));
628       if (addr) {
629         ip_addr_copy(*addr, dns_table[i].ipaddr);
630       }
631       return ERR_OK;
632     }
633   }
634 
635   return ERR_ARG;
636 }
637 
638 /**
639  * Compare the "dotted" name "query" with the encoded name "response"
640  * to make sure an answer from the DNS server matches the current dns_table
641  * entry (otherwise, answers might arrive late for hostname not on the list
642  * any more).
643  *
644  * For now, this function compares case-insensitive to cope with all kinds of
645  * servers. This also means that "dns 0x20 bit encoding" must be checked
646  * externally, if we want to implement it.
647  * Currently, the request is sent exactly as passed in by he user request.
648  *
649  * @param query hostname (not encoded) from the dns_table
650  * @param p pbuf containing the encoded hostname in the DNS response
651  * @param start_offset offset into p where the name starts
652  * @return 0xFFFF: names differ, other: names equal -> offset behind name
653  */
654 static u16_t
dns_compare_name(const char * query,struct pbuf * p,u16_t start_offset)655 dns_compare_name(const char *query, struct pbuf *p, u16_t start_offset)
656 {
657   int n;
658   u16_t response_offset = start_offset;
659 
660   do {
661     n = pbuf_try_get_at(p, response_offset);
662     if ((n < 0) || (response_offset == 0xFFFF)) {
663       /* error or overflow */
664       return 0xFFFF;
665     }
666     response_offset++;
667     /** @see RFC 1035 - 4.1.4. Message compression */
668     if ((n & 0xc0) == 0xc0) {
669       /* Compressed name: cannot be equal since we don't send them */
670       return 0xFFFF;
671     } else {
672       /* Not compressed name */
673       while (n > 0) {
674         int c = pbuf_try_get_at(p, response_offset);
675         if (c < 0) {
676           return 0xFFFF;
677         }
678         if (lwip_tolower((*query)) != lwip_tolower((u8_t)c)) {
679           return 0xFFFF;
680         }
681         if (response_offset == 0xFFFF) {
682           /* would overflow */
683           return 0xFFFF;
684         }
685         response_offset++;
686         ++query;
687         --n;
688       }
689       ++query;
690     }
691     n = pbuf_try_get_at(p, response_offset);
692     if (n < 0) {
693       return 0xFFFF;
694     }
695   } while (n != 0);
696 
697   if (response_offset == 0xFFFF) {
698     /* would overflow */
699     return 0xFFFF;
700   }
701   return (u16_t)(response_offset + 1);
702 }
703 
704 /**
705  * Walk through a compact encoded DNS name and return the end of the name.
706  *
707  * @param p pbuf containing the name
708  * @param query_idx start index into p pointing to encoded DNS name in the DNS server response
709  * @return index to end of the name
710  */
711 static u16_t
dns_skip_name(struct pbuf * p,u16_t query_idx)712 dns_skip_name(struct pbuf *p, u16_t query_idx)
713 {
714   int n;
715   u16_t offset = query_idx;
716 
717   do {
718     n = pbuf_try_get_at(p, offset++);
719     if ((n < 0) || (offset == 0)) {
720       return 0xFFFF;
721     }
722     /** @see RFC 1035 - 4.1.4. Message compression */
723     if ((n & 0xc0) == 0xc0) {
724       /* Compressed name: since we only want to skip it (not check it), stop here */
725       break;
726     } else {
727       /* Not compressed name */
728       if (offset + n >= p->tot_len) {
729         return 0xFFFF;
730       }
731       offset = (u16_t)(offset + n);
732     }
733     n = pbuf_try_get_at(p, offset);
734     if (n < 0) {
735       return 0xFFFF;
736     }
737   } while (n != 0);
738 
739   if (offset == 0xFFFF) {
740     return 0xFFFF;
741   }
742   return (u16_t)(offset + 1);
743 }
744 
745 /**
746  * Send a DNS query packet.
747  *
748  * @param idx the DNS table entry index for which to send a request
749  * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise
750  */
751 static err_t
dns_send(u8_t idx)752 dns_send(u8_t idx)
753 {
754   err_t err;
755   struct dns_hdr hdr;
756   struct dns_query qry;
757   struct pbuf *p;
758   u16_t query_idx, copy_len;
759   const char *hostname, *hostname_part;
760   u8_t n;
761   u8_t pcb_idx;
762   struct dns_table_entry *entry = &dns_table[idx];
763 
764   LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n",
765                           (u16_t)(entry->server_idx), entry->name));
766   LWIP_ASSERT("dns server out of array", entry->server_idx < DNS_MAX_SERVERS);
767   if (ip_addr_isany_val(dns_servers[entry->server_idx])
768 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
769       && !entry->is_mdns
770 #endif
771      ) {
772     /* DNS server not valid anymore, e.g. PPP netif has been shut down */
773     /* call specified callback function if provided */
774     dns_call_found(idx, NULL);
775     /* flush this entry */
776     entry->state = DNS_STATE_UNUSED;
777     return ERR_OK;
778   }
779 
780   /* if here, we have either a new query or a retry on a previous query to process */
781   p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(SIZEOF_DNS_HDR + strlen(entry->name) + 2 +
782                                          SIZEOF_DNS_QUERY), PBUF_RAM);
783   if (p != NULL) {
784     const ip_addr_t *dst;
785     u16_t dst_port;
786     /* fill dns header */
787     memset(&hdr, 0, SIZEOF_DNS_HDR);
788     hdr.id = lwip_htons(entry->txid);
789     hdr.flags1 = DNS_FLAG1_RD;
790     hdr.numquestions = PP_HTONS(1);
791     pbuf_take(p, &hdr, SIZEOF_DNS_HDR);
792     hostname = entry->name;
793     --hostname;
794 
795     /* convert hostname into suitable query format. */
796     query_idx = SIZEOF_DNS_HDR;
797     do {
798       ++hostname;
799       hostname_part = hostname;
800       for (n = 0; *hostname != '.' && *hostname != 0; ++hostname) {
801         ++n;
802       }
803       copy_len = (u16_t)(hostname - hostname_part);
804       if (query_idx + n + 1 > 0xFFFF) {
805         /* u16_t overflow */
806         goto overflow_return;
807       }
808       pbuf_put_at(p, query_idx, n);
809       pbuf_take_at(p, hostname_part, copy_len, (u16_t)(query_idx + 1));
810       query_idx = (u16_t)(query_idx + n + 1);
811     } while (*hostname != 0);
812     pbuf_put_at(p, query_idx, 0);
813     query_idx++;
814 
815     /* fill dns query */
816     if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
817       qry.type = PP_HTONS(DNS_RRTYPE_AAAA);
818     } else {
819       qry.type = PP_HTONS(DNS_RRTYPE_A);
820     }
821     qry.cls = PP_HTONS(DNS_RRCLASS_IN);
822     pbuf_take_at(p, &qry, SIZEOF_DNS_QUERY, query_idx);
823 
824 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
825     pcb_idx = entry->pcb_idx;
826 #else
827     pcb_idx = 0;
828 #endif
829     /* send dns packet */
830     LWIP_DEBUGF(DNS_DEBUG, ("sending DNS request ID %d for name \"%s\" to server %d\r\n",
831                             entry->txid, entry->name, entry->server_idx));
832 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
833     if (entry->is_mdns) {
834       dst_port = DNS_MQUERY_PORT;
835 #if LWIP_IPV6
836       if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
837         dst = &dns_mquery_v6group;
838       }
839 #endif
840 #if LWIP_IPV4 && LWIP_IPV6
841       else
842 #endif
843 #if LWIP_IPV4
844       {
845         dst = &dns_mquery_v4group;
846       }
847 #endif
848     } else
849 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
850     {
851       dst_port = DNS_SERVER_PORT;
852       dst = &dns_servers[entry->server_idx];
853     }
854     err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port);
855 
856     /* free pbuf */
857     pbuf_free(p);
858   } else {
859     err = ERR_MEM;
860   }
861 
862   return err;
863 overflow_return:
864   pbuf_free(p);
865   return ERR_VAL;
866 }
867 
868 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
869 static struct udp_pcb *
dns_alloc_random_port(void)870 dns_alloc_random_port(void)
871 {
872   err_t err;
873   struct udp_pcb *pcb;
874 
875   pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
876   if (pcb == NULL) {
877     /* out of memory, have to reuse an existing pcb */
878     return NULL;
879   }
880   do {
881     u16_t port = (u16_t)DNS_RAND_TXID();
882     if (DNS_PORT_ALLOWED(port)) {
883       err = udp_bind(pcb, IP_ANY_TYPE, port);
884     } else {
885       /* this port is not allowed, try again */
886       err = ERR_USE;
887     }
888   } while (err == ERR_USE);
889   if (err != ERR_OK) {
890     udp_remove(pcb);
891     return NULL;
892   }
893   udp_recv(pcb, dns_recv, NULL);
894   return pcb;
895 }
896 
897 /**
898  * dns_alloc_pcb() - allocates a new pcb (or reuses an existing one) to be used
899  * for sending a request
900  *
901  * @return an index into dns_pcbs
902  */
903 static u8_t
dns_alloc_pcb(void)904 dns_alloc_pcb(void)
905 {
906   u8_t i;
907   u8_t idx;
908 
909   for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) {
910     if (dns_pcbs[i] == NULL) {
911       break;
912     }
913   }
914   if (i < DNS_MAX_SOURCE_PORTS) {
915     dns_pcbs[i] = dns_alloc_random_port();
916     if (dns_pcbs[i] != NULL) {
917       /* succeeded */
918       dns_last_pcb_idx = i;
919       return i;
920     }
921   }
922   /* if we come here, creating a new UDP pcb failed, so we have to use
923      an already existing one (so overflow is no issue) */
924   for (i = 0, idx = (u8_t)(dns_last_pcb_idx + 1); i < DNS_MAX_SOURCE_PORTS; i++, idx++) {
925     if (idx >= DNS_MAX_SOURCE_PORTS) {
926       idx = 0;
927     }
928     if (dns_pcbs[idx] != NULL) {
929       dns_last_pcb_idx = idx;
930       return idx;
931     }
932   }
933   return DNS_MAX_SOURCE_PORTS;
934 }
935 #endif /* ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) */
936 
937 /**
938  * dns_call_found() - call the found callback and check if there are duplicate
939  * entries for the given hostname. If there are any, their found callback will
940  * be called and they will be removed.
941  *
942  * @param idx dns table index of the entry that is resolved or removed
943  * @param addr IP address for the hostname (or NULL on error or memory shortage)
944  */
945 static void
dns_call_found(u8_t idx,ip_addr_t * addr)946 dns_call_found(u8_t idx, ip_addr_t *addr)
947 {
948 #if ((LWIP_DNS_SECURE & (LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT)) != 0)
949   u8_t i;
950 #endif
951 
952 #if LWIP_IPV4 && LWIP_IPV6
953   if (addr != NULL) {
954     /* check that address type matches the request and adapt the table entry */
955     if (IP_IS_V6_VAL(*addr)) {
956       LWIP_ASSERT("invalid response", LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
957       dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
958     } else {
959       LWIP_ASSERT("invalid response", !LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
960       dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
961     }
962   }
963 #endif /* LWIP_IPV4 && LWIP_IPV6 */
964 
965 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
966   for (i = 0; i < DNS_MAX_REQUESTS; i++) {
967     if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) {
968       (*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg);
969       /* flush this entry */
970       dns_requests[i].found = NULL;
971     }
972   }
973 #else
974   if (dns_requests[idx].found) {
975     (*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg);
976   }
977   dns_requests[idx].found = NULL;
978 #endif
979 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
980   /* close the pcb used unless other request are using it */
981   for (i = 0; i < DNS_MAX_REQUESTS; i++) {
982     if (i == idx) {
983       continue; /* only check other requests */
984     }
985     if (dns_table[i].state == DNS_STATE_ASKING) {
986       if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) {
987         /* another request is still using the same pcb */
988         dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
989         break;
990       }
991     }
992   }
993   if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) {
994     /* if we come here, the pcb is not used any more and can be removed */
995     udp_remove(dns_pcbs[dns_table[idx].pcb_idx]);
996     dns_pcbs[dns_table[idx].pcb_idx] = NULL;
997     dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
998   }
999 #endif
1000 }
1001 
1002 /* Create a query transmission ID that is unique for all outstanding queries */
1003 static u16_t
dns_create_txid(void)1004 dns_create_txid(void)
1005 {
1006   u16_t txid;
1007   u8_t i;
1008 
1009 again:
1010   txid = (u16_t)DNS_RAND_TXID();
1011 
1012   /* check whether the ID is unique */
1013   for (i = 0; i < DNS_TABLE_SIZE; i++) {
1014     if ((dns_table[i].state == DNS_STATE_ASKING) &&
1015         (dns_table[i].txid == txid)) {
1016       /* ID already used by another pending query */
1017       goto again;
1018     }
1019   }
1020 
1021   return txid;
1022 }
1023 
1024 /**
1025  * Check whether there are other backup DNS servers available to try
1026  */
1027 static u8_t
dns_backupserver_available(struct dns_table_entry * pentry)1028 dns_backupserver_available(struct dns_table_entry *pentry)
1029 {
1030   u8_t ret = 0;
1031 
1032   if (pentry) {
1033     if ((pentry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[pentry->server_idx + 1])) {
1034       ret = 1;
1035     }
1036   }
1037 
1038   return ret;
1039 }
1040 
1041 /**
1042  * dns_check_entry() - see if entry has not yet been queried and, if so, sends out a query.
1043  * Check an entry in the dns_table:
1044  * - send out query for new entries
1045  * - retry old pending entries on timeout (also with different servers)
1046  * - remove completed entries from the table if their TTL has expired
1047  *
1048  * @param i index of the dns_table entry to check
1049  */
1050 static void
dns_check_entry(u8_t i)1051 dns_check_entry(u8_t i)
1052 {
1053   err_t err;
1054   struct dns_table_entry *entry = &dns_table[i];
1055 
1056   LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE);
1057 
1058   switch (entry->state) {
1059     case DNS_STATE_NEW:
1060       /* initialize new entry */
1061       entry->txid = dns_create_txid();
1062       entry->state = DNS_STATE_ASKING;
1063       entry->server_idx = 0;
1064       entry->tmr = 1;
1065       entry->retries = 0;
1066 
1067       /* send DNS packet for this entry */
1068       err = dns_send(i);
1069       if (err != ERR_OK) {
1070         LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
1071                     ("dns_send returned error: %s\n", lwip_strerr(err)));
1072       }
1073       break;
1074     case DNS_STATE_ASKING:
1075       if (--entry->tmr == 0) {
1076         if (++entry->retries == DNS_MAX_RETRIES) {
1077           if (dns_backupserver_available(entry)
1078 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
1079               && !entry->is_mdns
1080 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
1081              ) {
1082             /* change of server */
1083             entry->server_idx++;
1084             entry->tmr = 1;
1085             entry->retries = 0;
1086           } else {
1087             LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", entry->name));
1088             /* call specified callback function if provided */
1089             dns_call_found(i, NULL);
1090             /* flush this entry */
1091             entry->state = DNS_STATE_UNUSED;
1092             break;
1093           }
1094         } else {
1095           /* wait longer for the next retry */
1096           entry->tmr = entry->retries;
1097         }
1098 
1099         /* send DNS packet for this entry */
1100         err = dns_send(i);
1101         if (err != ERR_OK) {
1102           LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
1103                       ("dns_send returned error: %s\n", lwip_strerr(err)));
1104         }
1105       }
1106       break;
1107     case DNS_STATE_DONE:
1108       /* if the time to live is nul */
1109       if ((entry->ttl == 0) || (--entry->ttl == 0)) {
1110         LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", entry->name));
1111         /* flush this entry, there cannot be any related pending entries in this state */
1112         entry->state = DNS_STATE_UNUSED;
1113       }
1114       break;
1115     case DNS_STATE_UNUSED:
1116       /* nothing to do */
1117       break;
1118     default:
1119       LWIP_ASSERT("unknown dns_table entry state:", 0);
1120       break;
1121   }
1122 }
1123 
1124 /**
1125  * Call dns_check_entry for each entry in dns_table - check all entries.
1126  */
1127 static void
dns_check_entries(void)1128 dns_check_entries(void)
1129 {
1130   u8_t i;
1131 
1132   for (i = 0; i < DNS_TABLE_SIZE; ++i) {
1133     dns_check_entry(i);
1134   }
1135 }
1136 
1137 /**
1138  * Save TTL and call dns_call_found for correct response.
1139  */
1140 static void
dns_correct_response(u8_t idx,u32_t ttl)1141 dns_correct_response(u8_t idx, u32_t ttl)
1142 {
1143   struct dns_table_entry *entry = &dns_table[idx];
1144 
1145   entry->state = DNS_STATE_DONE;
1146 
1147   LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", entry->name));
1148   ip_addr_debug_print_val(DNS_DEBUG, entry->ipaddr);
1149   LWIP_DEBUGF(DNS_DEBUG, ("\n"));
1150 
1151   /* read the answer resource record's TTL, and maximize it if needed */
1152   entry->ttl = ttl;
1153   if (entry->ttl > DNS_MAX_TTL) {
1154     entry->ttl = DNS_MAX_TTL;
1155   }
1156   dns_call_found(idx, &entry->ipaddr);
1157 
1158   if (entry->ttl == 0) {
1159     /* RFC 883, page 29: "Zero values are
1160        interpreted to mean that the RR can only be used for the
1161        transaction in progress, and should not be cached."
1162        -> flush this entry now */
1163     /* entry reused during callback? */
1164     if (entry->state == DNS_STATE_DONE) {
1165       entry->state = DNS_STATE_UNUSED;
1166     }
1167   }
1168 }
1169 
1170 /**
1171  * Receive input function for DNS response packets arriving for the dns UDP pcb.
1172  */
1173 static void
dns_recv(void * arg,struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * addr,u16_t port)1174 dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
1175 {
1176   u8_t i;
1177   u16_t txid;
1178   u16_t res_idx;
1179   struct dns_hdr hdr;
1180   struct dns_answer ans;
1181   struct dns_query qry;
1182   u16_t nquestions, nanswers;
1183 
1184   LWIP_UNUSED_ARG(arg);
1185   LWIP_UNUSED_ARG(pcb);
1186   LWIP_UNUSED_ARG(port);
1187 
1188   /* is the dns message big enough ? */
1189   if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) {
1190     LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n"));
1191     /* free pbuf and return */
1192     goto ignore_packet;
1193   }
1194 
1195   /* copy dns payload inside static buffer for processing */
1196   if (pbuf_copy_partial(p, &hdr, SIZEOF_DNS_HDR, 0) == SIZEOF_DNS_HDR) {
1197     /* Match the ID in the DNS header with the name table. */
1198     txid = lwip_htons(hdr.id);
1199     for (i = 0; i < DNS_TABLE_SIZE; i++) {
1200       struct dns_table_entry *entry = &dns_table[i];
1201       if ((entry->state == DNS_STATE_ASKING) &&
1202           (entry->txid == txid)) {
1203 
1204         /* We only care about the question(s) and the answers. The authrr
1205            and the extrarr are simply discarded. */
1206         nquestions = lwip_htons(hdr.numquestions);
1207         nanswers   = lwip_htons(hdr.numanswers);
1208 
1209         /* Check for correct response. */
1210         if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) {
1211           LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": not a response\n", entry->name));
1212           goto ignore_packet; /* ignore this packet */
1213         }
1214         if (nquestions != 1) {
1215           LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
1216           goto ignore_packet; /* ignore this packet */
1217         }
1218 
1219 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
1220         if (!entry->is_mdns)
1221 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
1222         {
1223           /* Check whether response comes from the same network address to which the
1224              question was sent. (RFC 5452) */
1225           if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) {
1226             goto ignore_packet; /* ignore this packet */
1227           }
1228         }
1229 
1230         /* Check if the name in the "question" part match with the name in the entry and
1231            skip it if equal. */
1232         res_idx = dns_compare_name(entry->name, p, SIZEOF_DNS_HDR);
1233         if (res_idx == 0xFFFF) {
1234           LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
1235           goto ignore_packet; /* ignore this packet */
1236         }
1237 
1238         /* check if "question" part matches the request */
1239         if (pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) {
1240           goto ignore_packet; /* ignore this packet */
1241         }
1242         if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) ||
1243             (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) ||
1244             (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) {
1245           LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
1246           goto ignore_packet; /* ignore this packet */
1247         }
1248         /* skip the rest of the "question" part */
1249         if (res_idx + SIZEOF_DNS_QUERY > 0xFFFF) {
1250           goto ignore_packet;
1251         }
1252         res_idx = (u16_t)(res_idx + SIZEOF_DNS_QUERY);
1253 
1254         /* Check for error. If so, call callback to inform. */
1255         if (hdr.flags2 & DNS_FLAG2_ERR_MASK) {
1256           LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", entry->name));
1257 
1258           /* if there is another backup DNS server to try
1259            * then don't stop the DNS request
1260            */
1261           if (dns_backupserver_available(entry)) {
1262             /* avoid retrying the same server */
1263             entry->retries = DNS_MAX_RETRIES-1;
1264             entry->tmr     = 1;
1265 
1266             /* contact next available server for this entry */
1267             dns_check_entry(i);
1268 
1269             goto ignore_packet;
1270           }
1271         } else {
1272           while ((nanswers > 0) && (res_idx < p->tot_len)) {
1273             /* skip answer resource record's host name */
1274             res_idx = dns_skip_name(p, res_idx);
1275             if (res_idx == 0xFFFF) {
1276               goto ignore_packet; /* ignore this packet */
1277             }
1278 
1279             /* Check for IP address type and Internet class. Others are discarded. */
1280             if (pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) {
1281               goto ignore_packet; /* ignore this packet */
1282             }
1283             if (res_idx + SIZEOF_DNS_ANSWER > 0xFFFF) {
1284               goto ignore_packet;
1285             }
1286             res_idx = (u16_t)(res_idx + SIZEOF_DNS_ANSWER);
1287 
1288             if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) {
1289 #if LWIP_IPV4
1290               if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(sizeof(ip4_addr_t)))) {
1291 #if LWIP_IPV4 && LWIP_IPV6
1292                 if (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
1293 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1294                 {
1295                   ip4_addr_t ip4addr;
1296                   /* read the IP address after answer resource record's header */
1297                   if (pbuf_copy_partial(p, &ip4addr, sizeof(ip4_addr_t), res_idx) != sizeof(ip4_addr_t)) {
1298                     goto ignore_packet; /* ignore this packet */
1299                   }
1300                   ip_addr_copy_from_ip4(dns_table[i].ipaddr, ip4addr);
1301                   pbuf_free(p);
1302                   /* handle correct response */
1303                   dns_correct_response(i, lwip_ntohl(ans.ttl));
1304                   return;
1305                 }
1306               }
1307 #endif /* LWIP_IPV4 */
1308 #if LWIP_IPV6
1309               if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(sizeof(ip6_addr_p_t)))) {
1310 #if LWIP_IPV4 && LWIP_IPV6
1311                 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
1312 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1313                 {
1314                   ip6_addr_p_t ip6addr;
1315                   /* read the IP address after answer resource record's header */
1316                   if (pbuf_copy_partial(p, &ip6addr, sizeof(ip6_addr_p_t), res_idx) != sizeof(ip6_addr_p_t)) {
1317                     goto ignore_packet; /* ignore this packet */
1318                   }
1319                   /* @todo: scope ip6addr? Might be required for link-local addresses at least? */
1320                   ip_addr_copy_from_ip6_packed(dns_table[i].ipaddr, ip6addr);
1321                   pbuf_free(p);
1322                   /* handle correct response */
1323                   dns_correct_response(i, lwip_ntohl(ans.ttl));
1324                   return;
1325                 }
1326               }
1327 #endif /* LWIP_IPV6 */
1328             }
1329             /* skip this answer */
1330             if ((int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) {
1331               goto ignore_packet; /* ignore this packet */
1332             }
1333             res_idx = (u16_t)(res_idx + lwip_htons(ans.len));
1334             --nanswers;
1335           }
1336 #if LWIP_IPV4 && LWIP_IPV6
1337           if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) ||
1338               (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
1339             if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
1340               /* IPv4 failed, try IPv6 */
1341               dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
1342             } else {
1343               /* IPv6 failed, try IPv4 */
1344               dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
1345             }
1346             pbuf_free(p);
1347             dns_table[i].state = DNS_STATE_NEW;
1348             dns_check_entry(i);
1349             return;
1350           }
1351 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1352           LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", entry->name));
1353         }
1354         /* call callback to indicate error, clean up memory and return */
1355         pbuf_free(p);
1356         dns_call_found(i, NULL);
1357         dns_table[i].state = DNS_STATE_UNUSED;
1358         return;
1359       }
1360     }
1361   }
1362 
1363 ignore_packet:
1364   /* deallocate memory and return */
1365   pbuf_free(p);
1366   return;
1367 }
1368 
1369 /**
1370  * Queues a new hostname to resolve and sends out a DNS query for that hostname
1371  *
1372  * @param name the hostname that is to be queried
1373  * @param hostnamelen length of the hostname
1374  * @param found a callback function to be called on success, failure or timeout
1375  * @param callback_arg argument to pass to the callback function
1376  * @return err_t return code.
1377  */
1378 static err_t
dns_enqueue(const char * name,size_t hostnamelen,dns_found_callback found,void * callback_arg LWIP_DNS_ADDRTYPE_ARG (u8_t dns_addrtype)LWIP_DNS_ISMDNS_ARG (u8_t is_mdns))1379 dns_enqueue(const char *name, size_t hostnamelen, dns_found_callback found,
1380             void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns))
1381 {
1382   u8_t i;
1383   u8_t lseq, lseqi;
1384   struct dns_table_entry *entry = NULL;
1385   size_t namelen;
1386   struct dns_req_entry *req;
1387 
1388 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
1389   u8_t r;
1390   /* check for duplicate entries */
1391   for (i = 0; i < DNS_TABLE_SIZE; i++) {
1392     if ((dns_table[i].state == DNS_STATE_ASKING) &&
1393         (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0)) {
1394 #if LWIP_IPV4 && LWIP_IPV6
1395       if (dns_table[i].reqaddrtype != dns_addrtype) {
1396         /* requested address types don't match
1397            this can lead to 2 concurrent requests, but mixing the address types
1398            for the same host should not be that common */
1399         continue;
1400       }
1401 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1402       /* this is a duplicate entry, find a free request entry */
1403       for (r = 0; r < DNS_MAX_REQUESTS; r++) {
1404         if (dns_requests[r].found == 0) {
1405           dns_requests[r].found = found;
1406           dns_requests[r].arg = callback_arg;
1407           dns_requests[r].dns_table_idx = i;
1408           LWIP_DNS_SET_ADDRTYPE(dns_requests[r].reqaddrtype, dns_addrtype);
1409           LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": duplicate request\n", name));
1410           return ERR_INPROGRESS;
1411         }
1412       }
1413     }
1414   }
1415   /* no duplicate entries found */
1416 #endif
1417 
1418   /* search an unused entry, or the oldest one */
1419   lseq = 0;
1420   lseqi = DNS_TABLE_SIZE;
1421   for (i = 0; i < DNS_TABLE_SIZE; ++i) {
1422     entry = &dns_table[i];
1423     /* is it an unused entry ? */
1424     if (entry->state == DNS_STATE_UNUSED) {
1425       break;
1426     }
1427     /* check if this is the oldest completed entry */
1428     if (entry->state == DNS_STATE_DONE) {
1429       u8_t age = (u8_t)(dns_seqno - entry->seqno);
1430       if (age > lseq) {
1431         lseq = age;
1432         lseqi = i;
1433       }
1434     }
1435   }
1436 
1437   /* if we don't have found an unused entry, use the oldest completed one */
1438   if (i == DNS_TABLE_SIZE) {
1439     if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) {
1440       /* no entry can be used now, table is full */
1441       LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name));
1442       return ERR_MEM;
1443     } else {
1444       /* use the oldest completed one */
1445       i = lseqi;
1446       entry = &dns_table[i];
1447     }
1448   }
1449 
1450 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
1451   /* find a free request entry */
1452   req = NULL;
1453   for (r = 0; r < DNS_MAX_REQUESTS; r++) {
1454     if (dns_requests[r].found == NULL) {
1455       req = &dns_requests[r];
1456       break;
1457     }
1458   }
1459   if (req == NULL) {
1460     /* no request entry can be used now, table is full */
1461     LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS request entries table is full\n", name));
1462     return ERR_MEM;
1463   }
1464   req->dns_table_idx = i;
1465 #else
1466   /* in this configuration, the entry index is the same as the request index */
1467   req = &dns_requests[i];
1468 #endif
1469 
1470   /* use this entry */
1471   LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i)));
1472 
1473   /* fill the entry */
1474   entry->state = DNS_STATE_NEW;
1475   entry->seqno = dns_seqno;
1476   LWIP_DNS_SET_ADDRTYPE(entry->reqaddrtype, dns_addrtype);
1477   LWIP_DNS_SET_ADDRTYPE(req->reqaddrtype, dns_addrtype);
1478   req->found = found;
1479   req->arg   = callback_arg;
1480   namelen = LWIP_MIN(hostnamelen, DNS_MAX_NAME_LENGTH - 1);
1481   MEMCPY(entry->name, name, namelen);
1482   entry->name[namelen] = 0;
1483 
1484 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
1485   entry->pcb_idx = dns_alloc_pcb();
1486   if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) {
1487     /* failed to get a UDP pcb */
1488     LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": failed to allocate a pcb\n", name));
1489     entry->state = DNS_STATE_UNUSED;
1490     req->found = NULL;
1491     return ERR_MEM;
1492   }
1493   LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS pcb %"U16_F"\n", name, (u16_t)(entry->pcb_idx)));
1494 #endif
1495 
1496 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
1497   entry->is_mdns = is_mdns;
1498 #endif
1499 
1500   dns_seqno++;
1501 
1502   /* force to send query without waiting timer */
1503   dns_check_entry(i);
1504 
1505   /* dns query is enqueued */
1506   return ERR_INPROGRESS;
1507 }
1508 
1509 /**
1510  * @ingroup dns
1511  * Resolve a hostname (string) into an IP address.
1512  * NON-BLOCKING callback version for use with raw API!!!
1513  *
1514  * Returns immediately with one of err_t return codes:
1515  * - ERR_OK if hostname is a valid IP address string or the host
1516  *   name is already in the local names table.
1517  * - ERR_INPROGRESS enqueue a request to be sent to the DNS server
1518  *   for resolution if no errors are present.
1519  * - ERR_ARG: dns client not initialized or invalid hostname
1520  *
1521  * @param hostname the hostname that is to be queried
1522  * @param addr pointer to a ip_addr_t where to store the address if it is already
1523  *             cached in the dns_table (only valid if ERR_OK is returned!)
1524  * @param found a callback function to be called on success, failure or timeout (only if
1525  *              ERR_INPROGRESS is returned!)
1526  * @param callback_arg argument to pass to the callback function
1527  * @return a err_t return code.
1528  */
1529 err_t
dns_gethostbyname(const char * hostname,ip_addr_t * addr,dns_found_callback found,void * callback_arg)1530 dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found,
1531                   void *callback_arg)
1532 {
1533   return dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, LWIP_DNS_ADDRTYPE_DEFAULT);
1534 }
1535 
1536 /**
1537  * @ingroup dns
1538  * Like dns_gethostbyname, but returned address type can be controlled:
1539  * @param hostname the hostname that is to be queried
1540  * @param addr pointer to a ip_addr_t where to store the address if it is already
1541  *             cached in the dns_table (only valid if ERR_OK is returned!)
1542  * @param found a callback function to be called on success, failure or timeout (only if
1543  *              ERR_INPROGRESS is returned!)
1544  * @param callback_arg argument to pass to the callback function
1545  * @param dns_addrtype - LWIP_DNS_ADDRTYPE_IPV4_IPV6: try to resolve IPv4 first, try IPv6 if IPv4 fails only
1546  *                     - LWIP_DNS_ADDRTYPE_IPV6_IPV4: try to resolve IPv6 first, try IPv4 if IPv6 fails only
1547  *                     - LWIP_DNS_ADDRTYPE_IPV4: try to resolve IPv4 only
1548  *                     - LWIP_DNS_ADDRTYPE_IPV6: try to resolve IPv6 only
1549  */
1550 err_t
dns_gethostbyname_addrtype(const char * hostname,ip_addr_t * addr,dns_found_callback found,void * callback_arg,u8_t dns_addrtype)1551 dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found,
1552                            void *callback_arg, u8_t dns_addrtype)
1553 {
1554   size_t hostnamelen;
1555 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
1556   u8_t is_mdns;
1557 #endif
1558   /* not initialized or no valid server yet, or invalid addr pointer
1559    * or invalid hostname or invalid hostname length */
1560   if ((addr == NULL) ||
1561       (!hostname) || (!hostname[0])) {
1562     return ERR_ARG;
1563   }
1564 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
1565   if (dns_pcbs[0] == NULL) {
1566     return ERR_ARG;
1567   }
1568 #endif
1569   hostnamelen = strlen(hostname);
1570   if (hostnamelen >= DNS_MAX_NAME_LENGTH) {
1571     LWIP_DEBUGF(DNS_DEBUG, ("dns_gethostbyname: name too long to resolve"));
1572     return ERR_ARG;
1573   }
1574 
1575 
1576 #if LWIP_HAVE_LOOPIF
1577   if (strcmp(hostname, "localhost") == 0) {
1578     ip_addr_set_loopback(LWIP_DNS_ADDRTYPE_IS_IPV6(dns_addrtype), addr);
1579     return ERR_OK;
1580   }
1581 #endif /* LWIP_HAVE_LOOPIF */
1582 
1583   /* host name already in octet notation? set ip addr and return ERR_OK */
1584   if (ipaddr_aton(hostname, addr)) {
1585 #if LWIP_IPV4 && LWIP_IPV6
1586     if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) ||
1587         (IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6)))
1588 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1589     {
1590       return ERR_OK;
1591     }
1592   }
1593   /* already have this address cached? */
1594   if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) {
1595     return ERR_OK;
1596   }
1597 #if LWIP_IPV4 && LWIP_IPV6
1598   if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
1599     /* fallback to 2nd IP type and try again to lookup */
1600     u8_t fallback;
1601     if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
1602       fallback = LWIP_DNS_ADDRTYPE_IPV6;
1603     } else {
1604       fallback = LWIP_DNS_ADDRTYPE_IPV4;
1605     }
1606     if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) == ERR_OK) {
1607       return ERR_OK;
1608     }
1609   }
1610 #else /* LWIP_IPV4 && LWIP_IPV6 */
1611   LWIP_UNUSED_ARG(dns_addrtype);
1612 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1613 
1614 #if LWIP_DNS_SUPPORT_MDNS_QUERIES
1615   if (strstr(hostname, ".local") == &hostname[hostnamelen] - 6) {
1616     is_mdns = 1;
1617   } else {
1618     is_mdns = 0;
1619   }
1620 
1621   if (!is_mdns)
1622 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
1623   {
1624     /* prevent calling found callback if no server is set, return error instead */
1625     if (ip_addr_isany_val(dns_servers[0])) {
1626       return ERR_VAL;
1627     }
1628   }
1629 
1630   /* queue query with specified callback */
1631   return dns_enqueue(hostname, hostnamelen, found, callback_arg LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)
1632                      LWIP_DNS_ISMDNS_ARG(is_mdns));
1633 }
1634 
1635 #endif /* LWIP_DNS */
1636