Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
lwip_dns.c
00001 /** 00002 * @file 00003 * DNS - host name to IP address resolver. 00004 * 00005 * @defgroup dns DNS 00006 * @ingroup callbackstyle_api 00007 * 00008 * Implements a DNS host name to IP address resolver. 00009 * 00010 * The lwIP DNS resolver functions are used to lookup a host name and 00011 * map it to a numerical IP address. It maintains a list of resolved 00012 * hostnames that can be queried with the dns_lookup() function. 00013 * New hostnames can be resolved using the dns_query() function. 00014 * 00015 * The lwIP version of the resolver also adds a non-blocking version of 00016 * gethostbyname() that will work with a raw API application. This function 00017 * checks for an IP address string first and converts it if it is valid. 00018 * gethostbyname() then does a dns_lookup() to see if the name is 00019 * already in the table. If so, the IP is returned. If not, a query is 00020 * issued and the function returns with a ERR_INPROGRESS status. The app 00021 * using the dns client must then go into a waiting state. 00022 * 00023 * Once a hostname has been resolved (or found to be non-existent), 00024 * the resolver code calls a specified callback function (which 00025 * must be implemented by the module that uses the resolver). 00026 * 00027 * Multicast DNS queries are supported for names ending on ".local". 00028 * However, only "One-Shot Multicast DNS Queries" are supported (RFC 6762 00029 * chapter 5.1), this is not a fully compliant implementation of continuous 00030 * mDNS querying! 00031 * 00032 * All functions must be called from TCPIP thread. 00033 * 00034 * @see @ref netconn_common for thread-safe access. 00035 */ 00036 00037 /* 00038 * Port to lwIP from uIP 00039 * by Jim Pettinato April 2007 00040 * 00041 * security fixes and more by Simon Goldschmidt 00042 * 00043 * uIP version Copyright (c) 2002-2003, Adam Dunkels. 00044 * All rights reserved. 00045 * 00046 * Redistribution and use in source and binary forms, with or without 00047 * modification, are permitted provided that the following conditions 00048 * are met: 00049 * 1. Redistributions of source code must retain the above copyright 00050 * notice, this list of conditions and the following disclaimer. 00051 * 2. Redistributions in binary form must reproduce the above copyright 00052 * notice, this list of conditions and the following disclaimer in the 00053 * documentation and/or other materials provided with the distribution. 00054 * 3. The name of the author may not be used to endorse or promote 00055 * products derived from this software without specific prior 00056 * written permission. 00057 * 00058 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 00059 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00060 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00061 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00062 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00063 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00064 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00065 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00066 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00067 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00068 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00069 */ 00070 00071 /*----------------------------------------------------------------------------- 00072 * RFC 1035 - Domain names - implementation and specification 00073 * RFC 2181 - Clarifications to the DNS Specification 00074 *----------------------------------------------------------------------------*/ 00075 00076 /** @todo: define good default values (rfc compliance) */ 00077 /** @todo: improve answer parsing, more checkings... */ 00078 /** @todo: check RFC1035 - 7.3. Processing responses */ 00079 /** @todo: one-shot mDNS: dual-stack fallback to another IP version */ 00080 00081 /*----------------------------------------------------------------------------- 00082 * Includes 00083 *----------------------------------------------------------------------------*/ 00084 00085 #include "lwip/opt.h" 00086 00087 #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ 00088 00089 #include "lwip/def.h" 00090 #include "lwip/udp.h" 00091 #include "lwip/mem.h" 00092 #include "lwip/memp.h" 00093 #include "lwip/dns.h" 00094 #include "lwip/prot/dns.h" 00095 00096 #include <string.h> 00097 00098 /** Random generator function to create random TXIDs and source ports for queries */ 00099 #ifndef DNS_RAND_TXID 00100 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0) 00101 #define DNS_RAND_TXID LWIP_RAND 00102 #else 00103 static u16_t dns_txid; 00104 #define DNS_RAND_TXID() (++dns_txid) 00105 #endif 00106 #endif 00107 00108 /** Limits the source port to be >= 1024 by default */ 00109 #ifndef DNS_PORT_ALLOWED 00110 #define DNS_PORT_ALLOWED(port) ((port) >= 1024) 00111 #endif 00112 00113 /** DNS maximum number of retries when asking for a name, before "timeout". */ 00114 #ifndef DNS_MAX_RETRIES 00115 #define DNS_MAX_RETRIES 4 00116 #endif 00117 00118 /** DNS resource record max. TTL (one week as default) */ 00119 #ifndef DNS_MAX_TTL 00120 #define DNS_MAX_TTL 604800 00121 #elif DNS_MAX_TTL > 0x7FFFFFFF 00122 #error DNS_MAX_TTL must be a positive 32-bit value 00123 #endif 00124 00125 #if DNS_TABLE_SIZE > 255 00126 #error DNS_TABLE_SIZE must fit into an u8_t 00127 #endif 00128 #if DNS_MAX_SERVERS > 255 00129 #error DNS_MAX_SERVERS must fit into an u8_t 00130 #endif 00131 00132 /* The number of parallel requests (i.e. calls to dns_gethostbyname 00133 * that cannot be answered from the DNS table. 00134 * This is set to the table size by default. 00135 */ 00136 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 00137 #ifndef DNS_MAX_REQUESTS 00138 #define DNS_MAX_REQUESTS DNS_TABLE_SIZE 00139 #else 00140 #if DNS_MAX_REQUESTS > 255 00141 #error DNS_MAX_REQUESTS must fit into an u8_t 00142 #endif 00143 #endif 00144 #else 00145 /* In this configuration, both arrays have to have the same size and are used 00146 * like one entry (used/free) */ 00147 #define DNS_MAX_REQUESTS DNS_TABLE_SIZE 00148 #endif 00149 00150 /* The number of UDP source ports used in parallel */ 00151 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 00152 #ifndef DNS_MAX_SOURCE_PORTS 00153 #define DNS_MAX_SOURCE_PORTS DNS_MAX_REQUESTS 00154 #else 00155 #if DNS_MAX_SOURCE_PORTS > 255 00156 #error DNS_MAX_SOURCE_PORTS must fit into an u8_t 00157 #endif 00158 #endif 00159 #else 00160 #ifdef DNS_MAX_SOURCE_PORTS 00161 #undef DNS_MAX_SOURCE_PORTS 00162 #endif 00163 #define DNS_MAX_SOURCE_PORTS 1 00164 #endif 00165 00166 #if LWIP_IPV4 && LWIP_IPV6 00167 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) (((t) == LWIP_DNS_ADDRTYPE_IPV6_IPV4) || ((t) == LWIP_DNS_ADDRTYPE_IPV6)) 00168 #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))) 00169 #define LWIP_DNS_ADDRTYPE_ARG(x) , x 00170 #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) x 00171 #define LWIP_DNS_SET_ADDRTYPE(x, y) do { x = y; } while(0) 00172 #else 00173 #if LWIP_IPV6 00174 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 1 00175 #else 00176 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 0 00177 #endif 00178 #define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) 1 00179 #define LWIP_DNS_ADDRTYPE_ARG(x) 00180 #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) 0 00181 #define LWIP_DNS_SET_ADDRTYPE(x, y) 00182 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00183 00184 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 00185 #define LWIP_DNS_ISMDNS_ARG(x) , x 00186 #else 00187 #define LWIP_DNS_ISMDNS_ARG(x) 00188 #endif 00189 00190 /** DNS query message structure. 00191 No packing needed: only used locally on the stack. */ 00192 struct dns_query { 00193 /* DNS query record starts with either a domain name or a pointer 00194 to a name already present somewhere in the packet. */ 00195 u16_t type; 00196 u16_t cls; 00197 }; 00198 #define SIZEOF_DNS_QUERY 4 00199 00200 /** DNS answer message structure. 00201 No packing needed: only used locally on the stack. */ 00202 struct dns_answer { 00203 /* DNS answer record starts with either a domain name or a pointer 00204 to a name already present somewhere in the packet. */ 00205 u16_t type; 00206 u16_t cls; 00207 u32_t ttl; 00208 u16_t len; 00209 }; 00210 #define SIZEOF_DNS_ANSWER 10 00211 /* maximum allowed size for the struct due to non-packed */ 00212 #define SIZEOF_DNS_ANSWER_ASSERT 12 00213 00214 /* DNS table entry states */ 00215 typedef enum { 00216 DNS_STATE_UNUSED = 0, 00217 DNS_STATE_NEW = 1, 00218 DNS_STATE_ASKING = 2, 00219 DNS_STATE_DONE = 3 00220 } dns_state_enum_t; 00221 00222 /** DNS table entry */ 00223 struct dns_table_entry { 00224 u32_t ttl; 00225 ip_addr_t ipaddr; 00226 u16_t txid; 00227 u8_t state; 00228 u8_t server_idx; 00229 u8_t tmr; 00230 u8_t retries; 00231 u8_t seqno; 00232 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 00233 u8_t pcb_idx; 00234 #endif 00235 char name[DNS_MAX_NAME_LENGTH]; 00236 #if LWIP_IPV4 && LWIP_IPV6 00237 u8_t reqaddrtype; 00238 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00239 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 00240 u8_t is_mdns; 00241 #endif 00242 }; 00243 00244 /** DNS request table entry: used when dns_gehostbyname cannot answer the 00245 * request from the DNS table */ 00246 struct dns_req_entry { 00247 /* pointer to callback on DNS query done */ 00248 dns_found_callback found; 00249 /* argument passed to the callback function */ 00250 void *arg; 00251 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 00252 u8_t dns_table_idx; 00253 #endif 00254 #if LWIP_IPV4 && LWIP_IPV6 00255 u8_t reqaddrtype; 00256 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00257 }; 00258 00259 #if DNS_LOCAL_HOSTLIST 00260 00261 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 00262 /** Local host-list. For hostnames in this list, no 00263 * external name resolution is performed */ 00264 static struct local_hostlist_entry *local_hostlist_dynamic; 00265 #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 00266 00267 /** Defining this allows the local_hostlist_static to be placed in a different 00268 * linker section (e.g. FLASH) */ 00269 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE 00270 #define DNS_LOCAL_HOSTLIST_STORAGE_PRE static 00271 #endif /* DNS_LOCAL_HOSTLIST_STORAGE_PRE */ 00272 /** Defining this allows the local_hostlist_static to be placed in a different 00273 * linker section (e.g. FLASH) */ 00274 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST 00275 #define DNS_LOCAL_HOSTLIST_STORAGE_POST 00276 #endif /* DNS_LOCAL_HOSTLIST_STORAGE_POST */ 00277 DNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[] 00278 DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT; 00279 00280 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 00281 00282 static void dns_init_local(void); 00283 #endif /* DNS_LOCAL_HOSTLIST */ 00284 00285 00286 /* forward declarations */ 00287 static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); 00288 static void dns_check_entries(void); 00289 static void dns_call_found(u8_t idx, ip_addr_t* addr); 00290 00291 /*----------------------------------------------------------------------------- 00292 * Globals 00293 *----------------------------------------------------------------------------*/ 00294 00295 /* DNS variables */ 00296 static struct udp_pcb *dns_pcbs[DNS_MAX_SOURCE_PORTS]; 00297 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 00298 static u8_t dns_last_pcb_idx; 00299 #endif 00300 static u8_t dns_seqno; 00301 static struct dns_table_entry dns_table[DNS_TABLE_SIZE]; 00302 static struct dns_req_entry dns_requests[DNS_MAX_REQUESTS]; 00303 static ip_addr_t dns_servers[DNS_MAX_SERVERS]; 00304 00305 #if LWIP_IPV4 00306 const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT; 00307 #endif /* LWIP_IPV4 */ 00308 #if LWIP_IPV6 00309 const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT; 00310 #endif /* LWIP_IPV6 */ 00311 00312 /** 00313 * Initialize the resolver: set up the UDP pcb and configure the default server 00314 * (if DNS_SERVER_ADDRESS is set). 00315 */ 00316 void 00317 dns_init(void) 00318 { 00319 #ifdef DNS_SERVER_ADDRESS 00320 /* initialize default DNS server address */ 00321 ip_addr_t dnsserver; 00322 DNS_SERVER_ADDRESS(&dnsserver); 00323 dns_setserver(0, &dnsserver); 00324 #endif /* DNS_SERVER_ADDRESS */ 00325 00326 LWIP_ASSERT("sanity check SIZEOF_DNS_QUERY", 00327 sizeof(struct dns_query) == SIZEOF_DNS_QUERY); 00328 LWIP_ASSERT("sanity check SIZEOF_DNS_ANSWER", 00329 sizeof(struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT); 00330 00331 LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n")); 00332 00333 /* if dns client not yet initialized... */ 00334 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) 00335 if (dns_pcbs[0] == NULL) { 00336 dns_pcbs[0] = udp_new_ip_type(IPADDR_TYPE_ANY); 00337 LWIP_ASSERT("dns_pcbs[0] != NULL", dns_pcbs[0] != NULL); 00338 00339 /* initialize DNS table not needed (initialized to zero since it is a 00340 * global variable) */ 00341 LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0", 00342 DNS_STATE_UNUSED == 0); 00343 00344 /* initialize DNS client */ 00345 udp_bind(dns_pcbs[0], IP_ANY_TYPE, 0); 00346 udp_recv(dns_pcbs[0], dns_recv, NULL); 00347 } 00348 #endif 00349 00350 #if DNS_LOCAL_HOSTLIST 00351 dns_init_local(); 00352 #endif 00353 } 00354 00355 /** 00356 * @ingroup dns 00357 * Initialize one of the DNS servers. 00358 * 00359 * @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS 00360 * @param dnsserver IP address of the DNS server to set 00361 */ 00362 void 00363 dns_setserver(u8_t numdns, const ip_addr_t *dnsserver) 00364 { 00365 if (numdns < DNS_MAX_SERVERS) { 00366 if (dnsserver != NULL) { 00367 dns_servers[numdns] = (*dnsserver); 00368 } else { 00369 dns_servers[numdns] = *IP_ADDR_ANY; 00370 } 00371 } 00372 } 00373 00374 /** 00375 * @ingroup dns 00376 * Obtain one of the currently configured DNS server. 00377 * 00378 * @param numdns the index of the DNS server 00379 * @return IP address of the indexed DNS server or "ip_addr_any" if the DNS 00380 * server has not been configured. 00381 */ 00382 const ip_addr_t* 00383 dns_getserver(u8_t numdns) 00384 { 00385 if (numdns < DNS_MAX_SERVERS) { 00386 return &dns_servers[numdns]; 00387 } else { 00388 return IP_ADDR_ANY; 00389 } 00390 } 00391 00392 /** 00393 * The DNS resolver client timer - handle retries and timeouts and should 00394 * be called every DNS_TMR_INTERVAL milliseconds (every second by default). 00395 */ 00396 void 00397 dns_tmr(void) 00398 { 00399 LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n")); 00400 dns_check_entries(); 00401 } 00402 00403 #if DNS_LOCAL_HOSTLIST 00404 static void 00405 dns_init_local(void) 00406 { 00407 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) 00408 size_t i; 00409 struct local_hostlist_entry *entry; 00410 /* Dynamic: copy entries from DNS_LOCAL_HOSTLIST_INIT to list */ 00411 struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT; 00412 size_t namelen; 00413 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) { 00414 struct local_hostlist_entry *init_entry = &local_hostlist_init[i]; 00415 LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL); 00416 namelen = strlen(init_entry->name); 00417 LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); 00418 entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); 00419 LWIP_ASSERT("mem-error in dns_init_local", entry != NULL); 00420 if (entry != NULL) { 00421 char* entry_name = (char*)entry + sizeof(struct local_hostlist_entry); 00422 MEMCPY(entry_name, init_entry->name, namelen); 00423 entry_name[namelen] = 0; 00424 entry->name = entry_name; 00425 entry->addr = init_entry->addr; 00426 entry->next = local_hostlist_dynamic; 00427 local_hostlist_dynamic = entry; 00428 } 00429 } 00430 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) */ 00431 } 00432 00433 /** 00434 * @ingroup dns 00435 * Scans the local host-list for a hostname. 00436 * 00437 * @param hostname Hostname to look for in the local host-list 00438 * @param addr the first IP address for the hostname in the local host-list or 00439 * IPADDR_NONE if not found. 00440 * @return ERR_OK if found, ERR_ARG if not found 00441 */ 00442 static err_t 00443 dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype)) 00444 { 00445 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 00446 struct local_hostlist_entry *entry = local_hostlist_dynamic; 00447 while (entry != NULL) { 00448 if ((lwip_stricmp(entry->name, hostname) == 0) && 00449 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) { 00450 if (addr) { 00451 ip_addr_copy(*addr, entry->addr); 00452 } 00453 return ERR_OK; 00454 } 00455 entry = entry->next; 00456 } 00457 #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 00458 size_t i; 00459 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) { 00460 if ((lwip_stricmp(local_hostlist_static[i].name, hostname) == 0) && 00461 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) { 00462 if (addr) { 00463 ip_addr_copy(*addr, local_hostlist_static[i].addr); 00464 } 00465 return ERR_OK; 00466 } 00467 } 00468 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 00469 return ERR_ARG; 00470 } 00471 00472 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 00473 /** 00474 * @ingroup dns 00475 * Remove all entries from the local host-list for a specific hostname 00476 * and/or IP address 00477 * 00478 * @param hostname hostname for which entries shall be removed from the local 00479 * host-list 00480 * @param addr address for which entries shall be removed from the local host-list 00481 * @return the number of removed entries 00482 */ 00483 int 00484 dns_local_removehost(const char *hostname, const ip_addr_t *addr) 00485 { 00486 int removed = 0; 00487 struct local_hostlist_entry *entry = local_hostlist_dynamic; 00488 struct local_hostlist_entry *last_entry = NULL; 00489 while (entry != NULL) { 00490 if (((hostname == NULL) || !lwip_stricmp(entry->name, hostname)) && 00491 ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) { 00492 struct local_hostlist_entry *free_entry; 00493 if (last_entry != NULL) { 00494 last_entry->next = entry->next; 00495 } else { 00496 local_hostlist_dynamic = entry->next; 00497 } 00498 free_entry = entry; 00499 entry = entry->next; 00500 memp_free(MEMP_LOCALHOSTLIST, free_entry); 00501 removed++; 00502 } else { 00503 last_entry = entry; 00504 entry = entry->next; 00505 } 00506 } 00507 return removed; 00508 } 00509 00510 /** 00511 * @ingroup dns 00512 * Add a hostname/IP address pair to the local host-list. 00513 * Duplicates are not checked. 00514 * 00515 * @param hostname hostname of the new entry 00516 * @param addr IP address of the new entry 00517 * @return ERR_OK if succeeded or ERR_MEM on memory error 00518 */ 00519 err_t 00520 dns_local_addhost(const char *hostname, const ip_addr_t *addr) 00521 { 00522 struct local_hostlist_entry *entry; 00523 size_t namelen; 00524 char* entry_name; 00525 LWIP_ASSERT("invalid host name (NULL)", hostname != NULL); 00526 namelen = strlen(hostname); 00527 LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); 00528 entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); 00529 if (entry == NULL) { 00530 return ERR_MEM; 00531 } 00532 entry_name = (char*)entry + sizeof(struct local_hostlist_entry); 00533 MEMCPY(entry_name, hostname, namelen); 00534 entry_name[namelen] = 0; 00535 entry->name = entry_name; 00536 ip_addr_copy(entry->addr, *addr); 00537 entry->next = local_hostlist_dynamic; 00538 local_hostlist_dynamic = entry; 00539 return ERR_OK; 00540 } 00541 #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC*/ 00542 #endif /* DNS_LOCAL_HOSTLIST */ 00543 00544 /** 00545 * @ingroup dns 00546 * Look up a hostname in the array of known hostnames. 00547 * 00548 * @note This function only looks in the internal array of known 00549 * hostnames, it does not send out a query for the hostname if none 00550 * was found. The function dns_enqueue() can be used to send a query 00551 * for a hostname. 00552 * 00553 * @param name the hostname to look up 00554 * @param addr the hostname's IP address, as u32_t (instead of ip_addr_t to 00555 * better check for failure: != IPADDR_NONE) or IPADDR_NONE if the hostname 00556 * was not found in the cached dns_table. 00557 * @return ERR_OK if found, ERR_ARG if not found 00558 */ 00559 static err_t 00560 dns_lookup(const char *name, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype)) 00561 { 00562 u8_t i; 00563 #if DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) 00564 #endif /* DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) */ 00565 #if DNS_LOCAL_HOSTLIST 00566 if (dns_lookup_local(name, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) { 00567 return ERR_OK; 00568 } 00569 #endif /* DNS_LOCAL_HOSTLIST */ 00570 #ifdef DNS_LOOKUP_LOCAL_EXTERN 00571 if (DNS_LOOKUP_LOCAL_EXTERN(name, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) == ERR_OK) { 00572 return ERR_OK; 00573 } 00574 #endif /* DNS_LOOKUP_LOCAL_EXTERN */ 00575 00576 /* Walk through name list, return entry if found. If not, return NULL. */ 00577 for (i = 0; i < DNS_TABLE_SIZE; ++i) { 00578 if ((dns_table[i].state == DNS_STATE_DONE) && 00579 (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0) && 00580 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) { 00581 LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name)); 00582 ip_addr_debug_print(DNS_DEBUG, &(dns_table[i].ipaddr)); 00583 LWIP_DEBUGF(DNS_DEBUG, ("\n")); 00584 if (addr) { 00585 ip_addr_copy(*addr, dns_table[i].ipaddr); 00586 } 00587 return ERR_OK; 00588 } 00589 } 00590 00591 return ERR_ARG; 00592 } 00593 00594 /** 00595 * Compare the "dotted" name "query" with the encoded name "response" 00596 * to make sure an answer from the DNS server matches the current dns_table 00597 * entry (otherwise, answers might arrive late for hostname not on the list 00598 * any more). 00599 * 00600 * @param query hostname (not encoded) from the dns_table 00601 * @param p pbuf containing the encoded hostname in the DNS response 00602 * @param start_offset offset into p where the name starts 00603 * @return 0xFFFF: names differ, other: names equal -> offset behind name 00604 */ 00605 static u16_t 00606 dns_compare_name(const char *query, struct pbuf* p, u16_t start_offset) 00607 { 00608 int n; 00609 u16_t response_offset = start_offset; 00610 00611 do { 00612 n = pbuf_try_get_at(p, response_offset++); 00613 if (n < 0) { 00614 return 0xFFFF; 00615 } 00616 /** @see RFC 1035 - 4.1.4. Message compression */ 00617 if ((n & 0xc0) == 0xc0) { 00618 /* Compressed name: cannot be equal since we don't send them */ 00619 return 0xFFFF; 00620 } else { 00621 /* Not compressed name */ 00622 while (n > 0) { 00623 int c = pbuf_try_get_at(p, response_offset); 00624 if (c < 0) { 00625 return 0xFFFF; 00626 } 00627 if ((*query) != (u8_t)c) { 00628 return 0xFFFF; 00629 } 00630 ++response_offset; 00631 ++query; 00632 --n; 00633 } 00634 ++query; 00635 } 00636 n = pbuf_try_get_at(p, response_offset); 00637 if (n < 0) { 00638 return 0xFFFF; 00639 } 00640 } while (n != 0); 00641 00642 return response_offset + 1; 00643 } 00644 00645 /** 00646 * Walk through a compact encoded DNS name and return the end of the name. 00647 * 00648 * @param p pbuf containing the name 00649 * @param query_idx start index into p pointing to encoded DNS name in the DNS server response 00650 * @return index to end of the name 00651 */ 00652 static u16_t 00653 dns_skip_name(struct pbuf* p, u16_t query_idx) 00654 { 00655 int n; 00656 u16_t offset = query_idx; 00657 00658 do { 00659 n = pbuf_try_get_at(p, offset++); 00660 if (n < 0) { 00661 return 0xFFFF; 00662 } 00663 /** @see RFC 1035 - 4.1.4. Message compression */ 00664 if ((n & 0xc0) == 0xc0) { 00665 /* Compressed name: since we only want to skip it (not check it), stop here */ 00666 break; 00667 } else { 00668 /* Not compressed name */ 00669 if (offset + n >= p->tot_len) { 00670 return 0xFFFF; 00671 } 00672 offset = (u16_t)(offset + n); 00673 } 00674 n = pbuf_try_get_at(p, offset); 00675 if (n < 0) { 00676 return 0xFFFF; 00677 } 00678 } while (n != 0); 00679 00680 return offset + 1; 00681 } 00682 00683 /** 00684 * Send a DNS query packet. 00685 * 00686 * @param idx the DNS table entry index for which to send a request 00687 * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise 00688 */ 00689 static err_t 00690 dns_send(u8_t idx) 00691 { 00692 err_t err; 00693 struct dns_hdr hdr; 00694 struct dns_query qry; 00695 struct pbuf *p; 00696 u16_t query_idx, copy_len; 00697 const char *hostname, *hostname_part; 00698 u8_t n; 00699 u8_t pcb_idx; 00700 struct dns_table_entry* entry = &dns_table[idx]; 00701 00702 LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n", 00703 (u16_t)(entry->server_idx), entry->name)); 00704 LWIP_ASSERT("dns server out of array", entry->server_idx < DNS_MAX_SERVERS); 00705 if (ip_addr_isany_val(dns_servers[entry->server_idx]) 00706 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 00707 && !entry->is_mdns 00708 #endif 00709 ) { 00710 /* DNS server not valid anymore, e.g. PPP netif has been shut down */ 00711 /* call specified callback function if provided */ 00712 dns_call_found(idx, NULL); 00713 /* flush this entry */ 00714 entry->state = DNS_STATE_UNUSED; 00715 return ERR_OK; 00716 } 00717 00718 /* if here, we have either a new query or a retry on a previous query to process */ 00719 p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(SIZEOF_DNS_HDR + strlen(entry->name) + 2 + 00720 SIZEOF_DNS_QUERY), PBUF_RAM); 00721 if (p != NULL) { 00722 const ip_addr_t* dst; 00723 u16_t dst_port; 00724 /* fill dns header */ 00725 memset(&hdr, 0, SIZEOF_DNS_HDR); 00726 hdr.id = lwip_htons(entry->txid); 00727 hdr.flags1 = DNS_FLAG1_RD; 00728 hdr.numquestions = PP_HTONS(1); 00729 pbuf_take(p, &hdr, SIZEOF_DNS_HDR); 00730 hostname = entry->name; 00731 --hostname; 00732 00733 /* convert hostname into suitable query format. */ 00734 query_idx = SIZEOF_DNS_HDR; 00735 do { 00736 ++hostname; 00737 hostname_part = hostname; 00738 for (n = 0; *hostname != '.' && *hostname != 0; ++hostname) { 00739 ++n; 00740 } 00741 copy_len = (u16_t)(hostname - hostname_part); 00742 pbuf_put_at(p, query_idx, n); 00743 pbuf_take_at(p, hostname_part, copy_len, query_idx + 1); 00744 query_idx += n + 1; 00745 } while (*hostname != 0); 00746 pbuf_put_at(p, query_idx, 0); 00747 query_idx++; 00748 00749 /* fill dns query */ 00750 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) { 00751 qry.type = PP_HTONS(DNS_RRTYPE_AAAA); 00752 } else { 00753 qry.type = PP_HTONS(DNS_RRTYPE_A); 00754 } 00755 qry.cls = PP_HTONS(DNS_RRCLASS_IN); 00756 pbuf_take_at(p, &qry, SIZEOF_DNS_QUERY, query_idx); 00757 00758 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 00759 pcb_idx = entry->pcb_idx; 00760 #else 00761 pcb_idx = 0; 00762 #endif 00763 /* send dns packet */ 00764 LWIP_DEBUGF(DNS_DEBUG, ("sending DNS request ID %d for name \"%s\" to server %d\r\n", 00765 entry->txid, entry->name, entry->server_idx)); 00766 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 00767 if (entry->is_mdns) { 00768 dst_port = DNS_MQUERY_PORT; 00769 #if LWIP_IPV6 00770 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) 00771 { 00772 dst = &dns_mquery_v6group; 00773 } 00774 #endif 00775 #if LWIP_IPV4 && LWIP_IPV6 00776 else 00777 #endif 00778 #if LWIP_IPV4 00779 { 00780 dst = &dns_mquery_v4group; 00781 } 00782 #endif 00783 } else 00784 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 00785 { 00786 dst_port = DNS_SERVER_PORT; 00787 dst = &dns_servers[entry->server_idx]; 00788 } 00789 err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port); 00790 00791 /* free pbuf */ 00792 pbuf_free(p); 00793 } else { 00794 err = ERR_MEM; 00795 } 00796 00797 return err; 00798 } 00799 00800 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 00801 static struct udp_pcb* 00802 dns_alloc_random_port(void) 00803 { 00804 err_t err; 00805 struct udp_pcb* ret; 00806 00807 ret = udp_new_ip_type(IPADDR_TYPE_ANY); 00808 if (ret == NULL) { 00809 /* out of memory, have to reuse an existing pcb */ 00810 return NULL; 00811 } 00812 do { 00813 u16_t port = (u16_t)DNS_RAND_TXID(); 00814 if (!DNS_PORT_ALLOWED(port)) { 00815 /* this port is not allowed, try again */ 00816 err = ERR_USE; 00817 continue; 00818 } 00819 err = udp_bind(ret, IP_ANY_TYPE, port); 00820 } while (err == ERR_USE); 00821 if (err != ERR_OK) { 00822 udp_remove(ret); 00823 return NULL; 00824 } 00825 udp_recv(ret, dns_recv, NULL); 00826 return ret; 00827 } 00828 00829 /** 00830 * dns_alloc_pcb() - allocates a new pcb (or reuses an existing one) to be used 00831 * for sending a request 00832 * 00833 * @return an index into dns_pcbs 00834 */ 00835 static u8_t 00836 dns_alloc_pcb(void) 00837 { 00838 u8_t i; 00839 u8_t idx; 00840 00841 for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) { 00842 if (dns_pcbs[i] == NULL) { 00843 break; 00844 } 00845 } 00846 if (i < DNS_MAX_SOURCE_PORTS) { 00847 dns_pcbs[i] = dns_alloc_random_port(); 00848 if (dns_pcbs[i] != NULL) { 00849 /* succeeded */ 00850 dns_last_pcb_idx = i; 00851 return i; 00852 } 00853 } 00854 /* if we come here, creating a new UDP pcb failed, so we have to use 00855 an already existing one */ 00856 for (i = 0, idx = dns_last_pcb_idx + 1; i < DNS_MAX_SOURCE_PORTS; i++, idx++) { 00857 if (idx >= DNS_MAX_SOURCE_PORTS) { 00858 idx = 0; 00859 } 00860 if (dns_pcbs[idx] != NULL) { 00861 dns_last_pcb_idx = idx; 00862 return idx; 00863 } 00864 } 00865 return DNS_MAX_SOURCE_PORTS; 00866 } 00867 #endif /* ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) */ 00868 00869 /** 00870 * dns_call_found() - call the found callback and check if there are duplicate 00871 * entries for the given hostname. If there are any, their found callback will 00872 * be called and they will be removed. 00873 * 00874 * @param idx dns table index of the entry that is resolved or removed 00875 * @param addr IP address for the hostname (or NULL on error or memory shortage) 00876 */ 00877 static void 00878 dns_call_found(u8_t idx, ip_addr_t* addr) 00879 { 00880 #if ((LWIP_DNS_SECURE & (LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT)) != 0) 00881 u8_t i; 00882 #endif 00883 00884 #if LWIP_IPV4 && LWIP_IPV6 00885 if (addr != NULL) { 00886 /* check that address type matches the request and adapt the table entry */ 00887 if (IP_IS_V6_VAL(*addr)) { 00888 LWIP_ASSERT("invalid response", LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype)); 00889 dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6; 00890 } else { 00891 LWIP_ASSERT("invalid response", !LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype)); 00892 dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4; 00893 } 00894 } 00895 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00896 00897 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 00898 for (i = 0; i < DNS_MAX_REQUESTS; i++) { 00899 if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) { 00900 (*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg); 00901 /* flush this entry */ 00902 dns_requests[i].found = NULL; 00903 } 00904 } 00905 #else 00906 if (dns_requests[idx].found) { 00907 (*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg); 00908 } 00909 dns_requests[idx].found = NULL; 00910 #endif 00911 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 00912 /* close the pcb used unless other request are using it */ 00913 for (i = 0; i < DNS_MAX_REQUESTS; i++) { 00914 if (i == idx) { 00915 continue; /* only check other requests */ 00916 } 00917 if (dns_table[i].state == DNS_STATE_ASKING) { 00918 if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) { 00919 /* another request is still using the same pcb */ 00920 dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS; 00921 break; 00922 } 00923 } 00924 } 00925 if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) { 00926 /* if we come here, the pcb is not used any more and can be removed */ 00927 udp_remove(dns_pcbs[dns_table[idx].pcb_idx]); 00928 dns_pcbs[dns_table[idx].pcb_idx] = NULL; 00929 dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS; 00930 } 00931 #endif 00932 } 00933 00934 /* Create a query transmission ID that is unique for all outstanding queries */ 00935 static u16_t 00936 dns_create_txid(void) 00937 { 00938 u16_t txid; 00939 u8_t i; 00940 00941 again: 00942 txid = (u16_t)DNS_RAND_TXID(); 00943 00944 /* check whether the ID is unique */ 00945 for (i = 0; i < DNS_TABLE_SIZE; i++) { 00946 if ((dns_table[i].state == DNS_STATE_ASKING) && 00947 (dns_table[i].txid == txid)) { 00948 /* ID already used by another pending query */ 00949 goto again; 00950 } 00951 } 00952 00953 return txid; 00954 } 00955 00956 /** 00957 * dns_check_entry() - see if entry has not yet been queried and, if so, sends out a query. 00958 * Check an entry in the dns_table: 00959 * - send out query for new entries 00960 * - retry old pending entries on timeout (also with different servers) 00961 * - remove completed entries from the table if their TTL has expired 00962 * 00963 * @param i index of the dns_table entry to check 00964 */ 00965 static void 00966 dns_check_entry(u8_t i) 00967 { 00968 err_t err; 00969 struct dns_table_entry *entry = &dns_table[i]; 00970 00971 LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE); 00972 00973 switch (entry->state) { 00974 case DNS_STATE_NEW: 00975 /* initialize new entry */ 00976 entry->txid = dns_create_txid(); 00977 entry->state = DNS_STATE_ASKING; 00978 entry->server_idx = 0; 00979 entry->tmr = 1; 00980 entry->retries = 0; 00981 00982 /* send DNS packet for this entry */ 00983 err = dns_send(i); 00984 if (err != ERR_OK) { 00985 LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, 00986 ("dns_send returned error: %s\n", lwip_strerr(err))); 00987 } 00988 break; 00989 case DNS_STATE_ASKING: 00990 if (--entry->tmr == 0) { 00991 if (++entry->retries == DNS_MAX_RETRIES) { 00992 if ((entry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[entry->server_idx + 1]) 00993 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 00994 && !entry->is_mdns 00995 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 00996 ) { 00997 /* change of server */ 00998 entry->server_idx++; 00999 entry->tmr = 1; 01000 entry->retries = 0; 01001 } else { 01002 LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", entry->name)); 01003 /* call specified callback function if provided */ 01004 dns_call_found(i, NULL); 01005 /* flush this entry */ 01006 entry->state = DNS_STATE_UNUSED; 01007 break; 01008 } 01009 } else { 01010 /* wait longer for the next retry */ 01011 entry->tmr = entry->retries; 01012 } 01013 01014 /* send DNS packet for this entry */ 01015 err = dns_send(i); 01016 if (err != ERR_OK) { 01017 LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, 01018 ("dns_send returned error: %s\n", lwip_strerr(err))); 01019 } 01020 } 01021 break; 01022 case DNS_STATE_DONE: 01023 /* if the time to live is nul */ 01024 if ((entry->ttl == 0) || (--entry->ttl == 0)) { 01025 LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", entry->name)); 01026 /* flush this entry, there cannot be any related pending entries in this state */ 01027 entry->state = DNS_STATE_UNUSED; 01028 } 01029 break; 01030 case DNS_STATE_UNUSED: 01031 /* nothing to do */ 01032 break; 01033 default: 01034 LWIP_ASSERT("unknown dns_table entry state:", 0); 01035 break; 01036 } 01037 } 01038 01039 /** 01040 * Call dns_check_entry for each entry in dns_table - check all entries. 01041 */ 01042 static void 01043 dns_check_entries(void) 01044 { 01045 u8_t i; 01046 01047 for (i = 0; i < DNS_TABLE_SIZE; ++i) { 01048 dns_check_entry(i); 01049 } 01050 } 01051 01052 /** 01053 * Save TTL and call dns_call_found for correct response. 01054 */ 01055 static void 01056 dns_correct_response(u8_t idx, u32_t ttl) 01057 { 01058 struct dns_table_entry *entry = &dns_table[idx]; 01059 01060 entry->state = DNS_STATE_DONE; 01061 01062 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", entry->name)); 01063 ip_addr_debug_print(DNS_DEBUG, (&(entry->ipaddr))); 01064 LWIP_DEBUGF(DNS_DEBUG, ("\n")); 01065 01066 /* read the answer resource record's TTL, and maximize it if needed */ 01067 entry->ttl = ttl; 01068 if (entry->ttl > DNS_MAX_TTL) { 01069 entry->ttl = DNS_MAX_TTL; 01070 } 01071 dns_call_found(idx, &entry->ipaddr); 01072 01073 if (entry->ttl == 0) { 01074 /* RFC 883, page 29: "Zero values are 01075 interpreted to mean that the RR can only be used for the 01076 transaction in progress, and should not be cached." 01077 -> flush this entry now */ 01078 /* entry reused during callback? */ 01079 if (entry->state == DNS_STATE_DONE) { 01080 entry->state = DNS_STATE_UNUSED; 01081 } 01082 } 01083 } 01084 /** 01085 * Receive input function for DNS response packets arriving for the dns UDP pcb. 01086 */ 01087 static void 01088 dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) 01089 { 01090 u8_t i; 01091 u16_t txid; 01092 u16_t res_idx; 01093 struct dns_hdr hdr; 01094 struct dns_answer ans; 01095 struct dns_query qry; 01096 u16_t nquestions, nanswers; 01097 01098 LWIP_UNUSED_ARG(arg); 01099 LWIP_UNUSED_ARG(pcb); 01100 LWIP_UNUSED_ARG(port); 01101 01102 /* is the dns message big enough ? */ 01103 if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) { 01104 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n")); 01105 /* free pbuf and return */ 01106 goto memerr; 01107 } 01108 01109 /* copy dns payload inside static buffer for processing */ 01110 if (pbuf_copy_partial(p, &hdr, SIZEOF_DNS_HDR, 0) == SIZEOF_DNS_HDR) { 01111 /* Match the ID in the DNS header with the name table. */ 01112 txid = lwip_htons(hdr.id); 01113 for (i = 0; i < DNS_TABLE_SIZE; i++) { 01114 const struct dns_table_entry *entry = &dns_table[i]; 01115 if ((entry->state == DNS_STATE_ASKING) && 01116 (entry->txid == txid)) { 01117 01118 /* We only care about the question(s) and the answers. The authrr 01119 and the extrarr are simply discarded. */ 01120 nquestions = lwip_htons(hdr.numquestions); 01121 nanswers = lwip_htons(hdr.numanswers); 01122 01123 /* Check for correct response. */ 01124 if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) { 01125 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": not a response\n", entry->name)); 01126 goto memerr; /* ignore this packet */ 01127 } 01128 if (nquestions != 1) { 01129 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); 01130 goto memerr; /* ignore this packet */ 01131 } 01132 01133 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 01134 if (!entry->is_mdns) 01135 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 01136 { 01137 /* Check whether response comes from the same network address to which the 01138 question was sent. (RFC 5452) */ 01139 if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) { 01140 goto memerr; /* ignore this packet */ 01141 } 01142 } 01143 01144 /* Check if the name in the "question" part match with the name in the entry and 01145 skip it if equal. */ 01146 res_idx = dns_compare_name(entry->name, p, SIZEOF_DNS_HDR); 01147 if (res_idx == 0xFFFF) { 01148 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); 01149 goto memerr; /* ignore this packet */ 01150 } 01151 01152 /* check if "question" part matches the request */ 01153 if (pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) { 01154 goto memerr; /* ignore this packet */ 01155 } 01156 if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) || 01157 (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) || 01158 (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) { 01159 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); 01160 goto memerr; /* ignore this packet */ 01161 } 01162 /* skip the rest of the "question" part */ 01163 res_idx += SIZEOF_DNS_QUERY; 01164 01165 /* Check for error. If so, call callback to inform. */ 01166 if (hdr.flags2 & DNS_FLAG2_ERR_MASK) { 01167 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", entry->name)); 01168 } else { 01169 while ((nanswers > 0) && (res_idx < p->tot_len)) { 01170 /* skip answer resource record's host name */ 01171 res_idx = dns_skip_name(p, res_idx); 01172 if (res_idx == 0xFFFF) { 01173 goto memerr; /* ignore this packet */ 01174 } 01175 01176 /* Check for IP address type and Internet class. Others are discarded. */ 01177 if (pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) { 01178 goto memerr; /* ignore this packet */ 01179 } 01180 res_idx += SIZEOF_DNS_ANSWER; 01181 01182 if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) { 01183 #if LWIP_IPV4 01184 if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(sizeof(ip4_addr_t)))) { 01185 #if LWIP_IPV4 && LWIP_IPV6 01186 if (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) 01187 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01188 { 01189 ip4_addr_t ip4addr; 01190 /* read the IP address after answer resource record's header */ 01191 if (pbuf_copy_partial(p, &ip4addr, sizeof(ip4_addr_t), res_idx) != sizeof(ip4_addr_t)) { 01192 goto memerr; /* ignore this packet */ 01193 } 01194 ip_addr_copy_from_ip4(dns_table[i].ipaddr, ip4addr); 01195 pbuf_free(p); 01196 /* handle correct response */ 01197 dns_correct_response(i, lwip_ntohl(ans.ttl)); 01198 return; 01199 } 01200 } 01201 #endif /* LWIP_IPV4 */ 01202 #if LWIP_IPV6 01203 if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(sizeof(ip6_addr_t)))) { 01204 #if LWIP_IPV4 && LWIP_IPV6 01205 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) 01206 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01207 { 01208 ip6_addr_t ip6addr; 01209 /* read the IP address after answer resource record's header */ 01210 if (pbuf_copy_partial(p, &ip6addr, sizeof(ip6_addr_t), res_idx) != sizeof(ip6_addr_t)) { 01211 goto memerr; /* ignore this packet */ 01212 } 01213 ip_addr_copy_from_ip6(dns_table[i].ipaddr, ip6addr); 01214 pbuf_free(p); 01215 /* handle correct response */ 01216 dns_correct_response(i, lwip_ntohl(ans.ttl)); 01217 return; 01218 } 01219 } 01220 #endif /* LWIP_IPV6 */ 01221 } 01222 /* skip this answer */ 01223 if ((int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) { 01224 goto memerr; /* ignore this packet */ 01225 } 01226 res_idx += lwip_htons(ans.len); 01227 --nanswers; 01228 } 01229 #if LWIP_IPV4 && LWIP_IPV6 01230 if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || 01231 (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) { 01232 if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) { 01233 /* IPv4 failed, try IPv6 */ 01234 dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6; 01235 } else { 01236 /* IPv6 failed, try IPv4 */ 01237 dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4; 01238 } 01239 pbuf_free(p); 01240 dns_table[i].state = DNS_STATE_NEW; 01241 dns_check_entry(i); 01242 return; 01243 } 01244 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01245 LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", entry->name)); 01246 } 01247 /* call callback to indicate error, clean up memory and return */ 01248 pbuf_free(p); 01249 dns_call_found(i, NULL); 01250 dns_table[i].state = DNS_STATE_UNUSED; 01251 return; 01252 } 01253 } 01254 } 01255 01256 memerr: 01257 /* deallocate memory and return */ 01258 pbuf_free(p); 01259 return; 01260 } 01261 01262 /** 01263 * Queues a new hostname to resolve and sends out a DNS query for that hostname 01264 * 01265 * @param name the hostname that is to be queried 01266 * @param hostnamelen length of the hostname 01267 * @param found a callback function to be called on success, failure or timeout 01268 * @param callback_arg argument to pass to the callback function 01269 * @return err_t return code. 01270 */ 01271 static err_t 01272 dns_enqueue(const char *name, size_t hostnamelen, dns_found_callback found, 01273 void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns)) 01274 { 01275 u8_t i; 01276 u8_t lseq, lseqi; 01277 struct dns_table_entry *entry = NULL; 01278 size_t namelen; 01279 struct dns_req_entry* req; 01280 01281 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 01282 u8_t r; 01283 /* check for duplicate entries */ 01284 for (i = 0; i < DNS_TABLE_SIZE; i++) { 01285 if ((dns_table[i].state == DNS_STATE_ASKING) && 01286 (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0)) { 01287 #if LWIP_IPV4 && LWIP_IPV6 01288 if (dns_table[i].reqaddrtype != dns_addrtype) { 01289 /* requested address types don't match 01290 this can lead to 2 concurrent requests, but mixing the address types 01291 for the same host should not be that common */ 01292 continue; 01293 } 01294 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01295 /* this is a duplicate entry, find a free request entry */ 01296 for (r = 0; r < DNS_MAX_REQUESTS; r++) { 01297 if (dns_requests[r].found == 0) { 01298 dns_requests[r].found = found; 01299 dns_requests[r].arg = callback_arg; 01300 dns_requests[r].dns_table_idx = i; 01301 LWIP_DNS_SET_ADDRTYPE(dns_requests[r].reqaddrtype, dns_addrtype); 01302 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": duplicate request\n", name)); 01303 return ERR_INPROGRESS; 01304 } 01305 } 01306 } 01307 } 01308 /* no duplicate entries found */ 01309 #endif 01310 01311 /* search an unused entry, or the oldest one */ 01312 lseq = 0; 01313 lseqi = DNS_TABLE_SIZE; 01314 for (i = 0; i < DNS_TABLE_SIZE; ++i) { 01315 entry = &dns_table[i]; 01316 /* is it an unused entry ? */ 01317 if (entry->state == DNS_STATE_UNUSED) { 01318 break; 01319 } 01320 /* check if this is the oldest completed entry */ 01321 if (entry->state == DNS_STATE_DONE) { 01322 u8_t age = dns_seqno - entry->seqno; 01323 if (age > lseq) { 01324 lseq = age; 01325 lseqi = i; 01326 } 01327 } 01328 } 01329 01330 /* if we don't have found an unused entry, use the oldest completed one */ 01331 if (i == DNS_TABLE_SIZE) { 01332 if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) { 01333 /* no entry can be used now, table is full */ 01334 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name)); 01335 return ERR_MEM; 01336 } else { 01337 /* use the oldest completed one */ 01338 i = lseqi; 01339 entry = &dns_table[i]; 01340 } 01341 } 01342 01343 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 01344 /* find a free request entry */ 01345 req = NULL; 01346 for (r = 0; r < DNS_MAX_REQUESTS; r++) { 01347 if (dns_requests[r].found == NULL) { 01348 req = &dns_requests[r]; 01349 break; 01350 } 01351 } 01352 if (req == NULL) { 01353 /* no request entry can be used now, table is full */ 01354 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS request entries table is full\n", name)); 01355 return ERR_MEM; 01356 } 01357 req->dns_table_idx = i; 01358 #else 01359 /* in this configuration, the entry index is the same as the request index */ 01360 req = &dns_requests[i]; 01361 #endif 01362 01363 /* use this entry */ 01364 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i))); 01365 01366 /* fill the entry */ 01367 entry->state = DNS_STATE_NEW; 01368 entry->seqno = dns_seqno; 01369 LWIP_DNS_SET_ADDRTYPE(entry->reqaddrtype, dns_addrtype); 01370 LWIP_DNS_SET_ADDRTYPE(req->reqaddrtype, dns_addrtype); 01371 req->found = found; 01372 req->arg = callback_arg; 01373 namelen = LWIP_MIN(hostnamelen, DNS_MAX_NAME_LENGTH-1); 01374 MEMCPY(entry->name, name, namelen); 01375 entry->name[namelen] = 0; 01376 01377 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 01378 entry->pcb_idx = dns_alloc_pcb(); 01379 if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) { 01380 /* failed to get a UDP pcb */ 01381 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": failed to allocate a pcb\n", name)); 01382 entry->state = DNS_STATE_UNUSED; 01383 req->found = NULL; 01384 return ERR_MEM; 01385 } 01386 LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS pcb %"U16_F"\n", name, (u16_t)(entry->pcb_idx))); 01387 #endif 01388 01389 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 01390 entry->is_mdns = is_mdns; 01391 #endif 01392 01393 dns_seqno++; 01394 01395 /* force to send query without waiting timer */ 01396 dns_check_entry(i); 01397 01398 /* dns query is enqueued */ 01399 return ERR_INPROGRESS; 01400 } 01401 01402 /** 01403 * @ingroup dns 01404 * Resolve a hostname (string) into an IP address. 01405 * NON-BLOCKING callback version for use with raw API!!! 01406 * 01407 * Returns immediately with one of err_t return codes: 01408 * - ERR_OK if hostname is a valid IP address string or the host 01409 * name is already in the local names table. 01410 * - ERR_INPROGRESS enqueue a request to be sent to the DNS server 01411 * for resolution if no errors are present. 01412 * - ERR_ARG: dns client not initialized or invalid hostname 01413 * 01414 * @param hostname the hostname that is to be queried 01415 * @param addr pointer to a ip_addr_t where to store the address if it is already 01416 * cached in the dns_table (only valid if ERR_OK is returned!) 01417 * @param found a callback function to be called on success, failure or timeout (only if 01418 * ERR_INPROGRESS is returned!) 01419 * @param callback_arg argument to pass to the callback function 01420 * @return a err_t return code. 01421 */ 01422 err_t 01423 dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, 01424 void *callback_arg) 01425 { 01426 return dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, LWIP_DNS_ADDRTYPE_DEFAULT); 01427 } 01428 01429 /** 01430 * @ingroup dns 01431 * Like dns_gethostbyname, but returned address type can be controlled: 01432 * @param hostname the hostname that is to be queried 01433 * @param addr pointer to a ip_addr_t where to store the address if it is already 01434 * cached in the dns_table (only valid if ERR_OK is returned!) 01435 * @param found a callback function to be called on success, failure or timeout (only if 01436 * ERR_INPROGRESS is returned!) 01437 * @param callback_arg argument to pass to the callback function 01438 * @param dns_addrtype - LWIP_DNS_ADDRTYPE_IPV4_IPV6: try to resolve IPv4 first, try IPv6 if IPv4 fails only 01439 * - LWIP_DNS_ADDRTYPE_IPV6_IPV4: try to resolve IPv6 first, try IPv4 if IPv6 fails only 01440 * - LWIP_DNS_ADDRTYPE_IPV4: try to resolve IPv4 only 01441 * - LWIP_DNS_ADDRTYPE_IPV6: try to resolve IPv6 only 01442 */ 01443 err_t 01444 dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found, 01445 void *callback_arg, u8_t dns_addrtype) 01446 { 01447 size_t hostnamelen; 01448 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 01449 u8_t is_mdns; 01450 #endif 01451 /* not initialized or no valid server yet, or invalid addr pointer 01452 * or invalid hostname or invalid hostname length */ 01453 if ((addr == NULL) || 01454 (!hostname) || (!hostname[0])) { 01455 return ERR_ARG; 01456 } 01457 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) 01458 if (dns_pcbs[0] == NULL) { 01459 return ERR_ARG; 01460 } 01461 #endif 01462 hostnamelen = strlen(hostname); 01463 if (hostnamelen >= DNS_MAX_NAME_LENGTH) { 01464 LWIP_DEBUGF(DNS_DEBUG, ("dns_gethostbyname: name too long to resolve")); 01465 return ERR_ARG; 01466 } 01467 01468 01469 #if LWIP_HAVE_LOOPIF 01470 if (strcmp(hostname, "localhost") == 0) { 01471 ip_addr_set_loopback(LWIP_DNS_ADDRTYPE_IS_IPV6(dns_addrtype), addr); 01472 return ERR_OK; 01473 } 01474 #endif /* LWIP_HAVE_LOOPIF */ 01475 01476 /* host name already in octet notation? set ip addr and return ERR_OK */ 01477 if (ipaddr_aton(hostname, addr)) { 01478 #if LWIP_IPV4 && LWIP_IPV6 01479 if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) || 01480 (IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6))) 01481 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01482 { 01483 return ERR_OK; 01484 } 01485 } 01486 /* already have this address cached? */ 01487 if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) { 01488 return ERR_OK; 01489 } 01490 #if LWIP_IPV4 && LWIP_IPV6 01491 if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) { 01492 /* fallback to 2nd IP type and try again to lookup */ 01493 u8_t fallback; 01494 if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) { 01495 fallback = LWIP_DNS_ADDRTYPE_IPV6; 01496 } else { 01497 fallback = LWIP_DNS_ADDRTYPE_IPV4; 01498 } 01499 if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) == ERR_OK) { 01500 return ERR_OK; 01501 } 01502 } 01503 #else /* LWIP_IPV4 && LWIP_IPV6 */ 01504 LWIP_UNUSED_ARG(dns_addrtype); 01505 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01506 01507 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 01508 if (strstr(hostname, ".local") == &hostname[hostnamelen] - 6) { 01509 is_mdns = 1; 01510 } else { 01511 is_mdns = 0; 01512 } 01513 01514 if (!is_mdns) 01515 #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 01516 { 01517 /* prevent calling found callback if no server is set, return error instead */ 01518 if (ip_addr_isany_val(dns_servers[0])) { 01519 return ERR_VAL; 01520 } 01521 } 01522 01523 /* queue query with specified callback */ 01524 return dns_enqueue(hostname, hostnamelen, found, callback_arg LWIP_DNS_ADDRTYPE_ARG(dns_addrtype) 01525 LWIP_DNS_ISMDNS_ARG(is_mdns)); 01526 } 01527 01528 #endif /* LWIP_DNS */
Generated on Tue Jul 12 2022 11:02:26 by
1.7.2