1195972f6Sopenharmony_ci/** 2195972f6Sopenharmony_ci * @file 3195972f6Sopenharmony_ci * DNS - host name to IP address resolver. 4195972f6Sopenharmony_ci * 5195972f6Sopenharmony_ci * @defgroup dns DNS 6195972f6Sopenharmony_ci * @ingroup callbackstyle_api 7195972f6Sopenharmony_ci * 8195972f6Sopenharmony_ci * Implements a DNS host name to IP address resolver. 9195972f6Sopenharmony_ci * 10195972f6Sopenharmony_ci * The lwIP DNS resolver functions are used to lookup a host name and 11195972f6Sopenharmony_ci * map it to a numerical IP address. It maintains a list of resolved 12195972f6Sopenharmony_ci * hostnames that can be queried with the dns_lookup() function. 13195972f6Sopenharmony_ci * New hostnames can be resolved using the dns_query() function. 14195972f6Sopenharmony_ci * 15195972f6Sopenharmony_ci * The lwIP version of the resolver also adds a non-blocking version of 16195972f6Sopenharmony_ci * gethostbyname() that will work with a raw API application. This function 17195972f6Sopenharmony_ci * checks for an IP address string first and converts it if it is valid. 18195972f6Sopenharmony_ci * gethostbyname() then does a dns_lookup() to see if the name is 19195972f6Sopenharmony_ci * already in the table. If so, the IP is returned. If not, a query is 20195972f6Sopenharmony_ci * issued and the function returns with a ERR_INPROGRESS status. The app 21195972f6Sopenharmony_ci * using the dns client must then go into a waiting state. 22195972f6Sopenharmony_ci * 23195972f6Sopenharmony_ci * Once a hostname has been resolved (or found to be non-existent), 24195972f6Sopenharmony_ci * the resolver code calls a specified callback function (which 25195972f6Sopenharmony_ci * must be implemented by the module that uses the resolver). 26195972f6Sopenharmony_ci * 27195972f6Sopenharmony_ci * Multicast DNS queries are supported for names ending on ".local". 28195972f6Sopenharmony_ci * However, only "One-Shot Multicast DNS Queries" are supported (RFC 6762 29195972f6Sopenharmony_ci * chapter 5.1), this is not a fully compliant implementation of continuous 30195972f6Sopenharmony_ci * mDNS querying! 31195972f6Sopenharmony_ci * 32195972f6Sopenharmony_ci * All functions must be called from TCPIP thread. 33195972f6Sopenharmony_ci * 34195972f6Sopenharmony_ci * @see DNS_MAX_SERVERS 35195972f6Sopenharmony_ci * @see LWIP_DHCP_MAX_DNS_SERVERS 36195972f6Sopenharmony_ci * @see @ref netconn_common for thread-safe access. 37195972f6Sopenharmony_ci */ 38195972f6Sopenharmony_ci 39195972f6Sopenharmony_ci/* 40195972f6Sopenharmony_ci * Port to lwIP from uIP 41195972f6Sopenharmony_ci * by Jim Pettinato April 2007 42195972f6Sopenharmony_ci * 43195972f6Sopenharmony_ci * security fixes and more by Simon Goldschmidt 44195972f6Sopenharmony_ci * 45195972f6Sopenharmony_ci * uIP version Copyright (c) 2002-2003, Adam Dunkels. 46195972f6Sopenharmony_ci * All rights reserved. 47195972f6Sopenharmony_ci * 48195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 49195972f6Sopenharmony_ci * modification, are permitted provided that the following conditions 50195972f6Sopenharmony_ci * are met: 51195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 52195972f6Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 53195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 54195972f6Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 55195972f6Sopenharmony_ci * documentation and/or other materials provided with the distribution. 56195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote 57195972f6Sopenharmony_ci * products derived from this software without specific prior 58195972f6Sopenharmony_ci * written permission. 59195972f6Sopenharmony_ci * 60195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 61195972f6Sopenharmony_ci * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 62195972f6Sopenharmony_ci * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63195972f6Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 64195972f6Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65195972f6Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 66195972f6Sopenharmony_ci * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 67195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 68195972f6Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 69195972f6Sopenharmony_ci * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 70195972f6Sopenharmony_ci * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 71195972f6Sopenharmony_ci */ 72195972f6Sopenharmony_ci 73195972f6Sopenharmony_ci/*----------------------------------------------------------------------------- 74195972f6Sopenharmony_ci * RFC 1035 - Domain names - implementation and specification 75195972f6Sopenharmony_ci * RFC 2181 - Clarifications to the DNS Specification 76195972f6Sopenharmony_ci *----------------------------------------------------------------------------*/ 77195972f6Sopenharmony_ci 78195972f6Sopenharmony_ci/** @todo: define good default values (rfc compliance) */ 79195972f6Sopenharmony_ci/** @todo: improve answer parsing, more checkings... */ 80195972f6Sopenharmony_ci/** @todo: check RFC1035 - 7.3. Processing responses */ 81195972f6Sopenharmony_ci/** @todo: one-shot mDNS: dual-stack fallback to another IP version */ 82195972f6Sopenharmony_ci 83195972f6Sopenharmony_ci/*----------------------------------------------------------------------------- 84195972f6Sopenharmony_ci * Includes 85195972f6Sopenharmony_ci *----------------------------------------------------------------------------*/ 86195972f6Sopenharmony_ci 87195972f6Sopenharmony_ci#include "lwip/opt.h" 88195972f6Sopenharmony_ci 89195972f6Sopenharmony_ci#if LWIP_ENABLE_DISTRIBUTED_NET && !LWIP_USE_GET_HOST_BY_NAME_EXTERNAL 90195972f6Sopenharmony_ci#include "lwip/distributed_net/distributed_net.h" 91195972f6Sopenharmony_ci#include "lwip/distributed_net/udp_transmit.h" 92195972f6Sopenharmony_ci#endif 93195972f6Sopenharmony_ci 94195972f6Sopenharmony_ci#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ 95195972f6Sopenharmony_ci 96195972f6Sopenharmony_ci#include "lwip/def.h" 97195972f6Sopenharmony_ci#include "lwip/udp.h" 98195972f6Sopenharmony_ci#include "lwip/mem.h" 99195972f6Sopenharmony_ci#include "lwip/memp.h" 100195972f6Sopenharmony_ci#include "lwip/dns.h" 101195972f6Sopenharmony_ci#include "lwip/prot/dns.h" 102195972f6Sopenharmony_ci 103195972f6Sopenharmony_ci#include <string.h> 104195972f6Sopenharmony_ci 105195972f6Sopenharmony_ci/** Random generator function to create random TXIDs and source ports for queries */ 106195972f6Sopenharmony_ci#ifndef DNS_RAND_TXID 107195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0) 108195972f6Sopenharmony_ci#define DNS_RAND_TXID LWIP_RAND 109195972f6Sopenharmony_ci#else 110195972f6Sopenharmony_cistatic u16_t dns_txid; 111195972f6Sopenharmony_ci#define DNS_RAND_TXID() (++dns_txid) 112195972f6Sopenharmony_ci#endif 113195972f6Sopenharmony_ci#endif 114195972f6Sopenharmony_ci 115195972f6Sopenharmony_ci/** Limits the source port to be >= 1024 by default */ 116195972f6Sopenharmony_ci#ifndef DNS_PORT_ALLOWED 117195972f6Sopenharmony_ci#define DNS_PORT_ALLOWED(port) ((port) >= 1024) 118195972f6Sopenharmony_ci#endif 119195972f6Sopenharmony_ci 120195972f6Sopenharmony_ci/** DNS resource record max. TTL (one week as default) */ 121195972f6Sopenharmony_ci#ifndef DNS_MAX_TTL 122195972f6Sopenharmony_ci#define DNS_MAX_TTL 604800 123195972f6Sopenharmony_ci#elif DNS_MAX_TTL > 0x7FFFFFFF 124195972f6Sopenharmony_ci#error DNS_MAX_TTL must be a positive 32-bit value 125195972f6Sopenharmony_ci#endif 126195972f6Sopenharmony_ci 127195972f6Sopenharmony_ci#if DNS_TABLE_SIZE > 255 128195972f6Sopenharmony_ci#error DNS_TABLE_SIZE must fit into an u8_t 129195972f6Sopenharmony_ci#endif 130195972f6Sopenharmony_ci#if DNS_MAX_SERVERS > 255 131195972f6Sopenharmony_ci#error DNS_MAX_SERVERS must fit into an u8_t 132195972f6Sopenharmony_ci#endif 133195972f6Sopenharmony_ci 134195972f6Sopenharmony_ci/* The number of parallel requests (i.e. calls to dns_gethostbyname 135195972f6Sopenharmony_ci * that cannot be answered from the DNS table. 136195972f6Sopenharmony_ci * This is set to the table size by default. 137195972f6Sopenharmony_ci */ 138195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 139195972f6Sopenharmony_ci#ifndef DNS_MAX_REQUESTS 140195972f6Sopenharmony_ci#define DNS_MAX_REQUESTS DNS_TABLE_SIZE 141195972f6Sopenharmony_ci#else 142195972f6Sopenharmony_ci#if DNS_MAX_REQUESTS > 255 143195972f6Sopenharmony_ci#error DNS_MAX_REQUESTS must fit into an u8_t 144195972f6Sopenharmony_ci#endif 145195972f6Sopenharmony_ci#endif 146195972f6Sopenharmony_ci#else 147195972f6Sopenharmony_ci/* In this configuration, both arrays have to have the same size and are used 148195972f6Sopenharmony_ci * like one entry (used/free) */ 149195972f6Sopenharmony_ci#define DNS_MAX_REQUESTS DNS_TABLE_SIZE 150195972f6Sopenharmony_ci#endif 151195972f6Sopenharmony_ci 152195972f6Sopenharmony_ci/* The number of UDP source ports used in parallel */ 153195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 154195972f6Sopenharmony_ci#ifndef DNS_MAX_SOURCE_PORTS 155195972f6Sopenharmony_ci#define DNS_MAX_SOURCE_PORTS DNS_MAX_REQUESTS 156195972f6Sopenharmony_ci#else 157195972f6Sopenharmony_ci#if DNS_MAX_SOURCE_PORTS > 255 158195972f6Sopenharmony_ci#error DNS_MAX_SOURCE_PORTS must fit into an u8_t 159195972f6Sopenharmony_ci#endif 160195972f6Sopenharmony_ci#endif 161195972f6Sopenharmony_ci#else 162195972f6Sopenharmony_ci#ifdef DNS_MAX_SOURCE_PORTS 163195972f6Sopenharmony_ci#undef DNS_MAX_SOURCE_PORTS 164195972f6Sopenharmony_ci#endif 165195972f6Sopenharmony_ci#define DNS_MAX_SOURCE_PORTS 1 166195972f6Sopenharmony_ci#endif 167195972f6Sopenharmony_ci 168195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 169195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) (((t) == LWIP_DNS_ADDRTYPE_IPV6_IPV4) || ((t) == LWIP_DNS_ADDRTYPE_IPV6)) 170195972f6Sopenharmony_ci#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))) 171195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_ARG(x) , x 172195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) x 173195972f6Sopenharmony_ci#define LWIP_DNS_SET_ADDRTYPE(x, y) do { x = y; } while(0) 174195972f6Sopenharmony_ci#else 175195972f6Sopenharmony_ci#if LWIP_IPV6 176195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 1 177195972f6Sopenharmony_ci#else 178195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 0 179195972f6Sopenharmony_ci#endif 180195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) 1 181195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_ARG(x) 182195972f6Sopenharmony_ci#define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) 0 183195972f6Sopenharmony_ci#define LWIP_DNS_SET_ADDRTYPE(x, y) 184195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 185195972f6Sopenharmony_ci 186195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 187195972f6Sopenharmony_ci#define LWIP_DNS_ISMDNS_ARG(x) , x 188195972f6Sopenharmony_ci#else 189195972f6Sopenharmony_ci#define LWIP_DNS_ISMDNS_ARG(x) 190195972f6Sopenharmony_ci#endif 191195972f6Sopenharmony_ci 192195972f6Sopenharmony_ci/** DNS query message structure. 193195972f6Sopenharmony_ci No packing needed: only used locally on the stack. */ 194195972f6Sopenharmony_cistruct dns_query { 195195972f6Sopenharmony_ci /* DNS query record starts with either a domain name or a pointer 196195972f6Sopenharmony_ci to a name already present somewhere in the packet. */ 197195972f6Sopenharmony_ci u16_t type; 198195972f6Sopenharmony_ci u16_t cls; 199195972f6Sopenharmony_ci}; 200195972f6Sopenharmony_ci#define SIZEOF_DNS_QUERY 4 201195972f6Sopenharmony_ci 202195972f6Sopenharmony_ci/** DNS answer message structure. 203195972f6Sopenharmony_ci No packing needed: only used locally on the stack. */ 204195972f6Sopenharmony_cistruct dns_answer { 205195972f6Sopenharmony_ci /* DNS answer record starts with either a domain name or a pointer 206195972f6Sopenharmony_ci to a name already present somewhere in the packet. */ 207195972f6Sopenharmony_ci u16_t type; 208195972f6Sopenharmony_ci u16_t cls; 209195972f6Sopenharmony_ci u32_t ttl; 210195972f6Sopenharmony_ci u16_t len; 211195972f6Sopenharmony_ci}; 212195972f6Sopenharmony_ci#define SIZEOF_DNS_ANSWER 10 213195972f6Sopenharmony_ci/* maximum allowed size for the struct due to non-packed */ 214195972f6Sopenharmony_ci#define SIZEOF_DNS_ANSWER_ASSERT 12 215195972f6Sopenharmony_ci 216195972f6Sopenharmony_ci/* DNS table entry states */ 217195972f6Sopenharmony_citypedef enum { 218195972f6Sopenharmony_ci DNS_STATE_UNUSED = 0, 219195972f6Sopenharmony_ci DNS_STATE_NEW = 1, 220195972f6Sopenharmony_ci DNS_STATE_ASKING = 2, 221195972f6Sopenharmony_ci DNS_STATE_DONE = 3 222195972f6Sopenharmony_ci} dns_state_enum_t; 223195972f6Sopenharmony_ci 224195972f6Sopenharmony_ci/** DNS table entry */ 225195972f6Sopenharmony_cistruct dns_table_entry { 226195972f6Sopenharmony_ci u32_t ttl; 227195972f6Sopenharmony_ci ip_addr_t ipaddr; 228195972f6Sopenharmony_ci u16_t txid; 229195972f6Sopenharmony_ci u8_t state; 230195972f6Sopenharmony_ci u8_t server_idx; 231195972f6Sopenharmony_ci u8_t tmr; 232195972f6Sopenharmony_ci u8_t retries; 233195972f6Sopenharmony_ci u8_t seqno; 234195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 235195972f6Sopenharmony_ci u8_t pcb_idx; 236195972f6Sopenharmony_ci#endif 237195972f6Sopenharmony_ci char name[DNS_MAX_NAME_LENGTH]; 238195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 239195972f6Sopenharmony_ci u8_t reqaddrtype; 240195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 241195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 242195972f6Sopenharmony_ci u8_t is_mdns; 243195972f6Sopenharmony_ci#endif 244195972f6Sopenharmony_ci}; 245195972f6Sopenharmony_ci 246195972f6Sopenharmony_ci/** DNS request table entry: used when dns_gehostbyname cannot answer the 247195972f6Sopenharmony_ci * request from the DNS table */ 248195972f6Sopenharmony_cistruct dns_req_entry { 249195972f6Sopenharmony_ci /* pointer to callback on DNS query done */ 250195972f6Sopenharmony_ci dns_found_callback found; 251195972f6Sopenharmony_ci /* argument passed to the callback function */ 252195972f6Sopenharmony_ci void *arg; 253195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 254195972f6Sopenharmony_ci u8_t dns_table_idx; 255195972f6Sopenharmony_ci#endif 256195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 257195972f6Sopenharmony_ci u8_t reqaddrtype; 258195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 259195972f6Sopenharmony_ci}; 260195972f6Sopenharmony_ci 261195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST 262195972f6Sopenharmony_ci 263195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 264195972f6Sopenharmony_ci/** Local host-list. For hostnames in this list, no 265195972f6Sopenharmony_ci * external name resolution is performed */ 266195972f6Sopenharmony_cistatic struct local_hostlist_entry *local_hostlist_dynamic; 267195972f6Sopenharmony_ci#else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 268195972f6Sopenharmony_ci 269195972f6Sopenharmony_ci/** Defining this allows the local_hostlist_static to be placed in a different 270195972f6Sopenharmony_ci * linker section (e.g. FLASH) */ 271195972f6Sopenharmony_ci#ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE 272195972f6Sopenharmony_ci#define DNS_LOCAL_HOSTLIST_STORAGE_PRE static 273195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST_STORAGE_PRE */ 274195972f6Sopenharmony_ci/** Defining this allows the local_hostlist_static to be placed in a different 275195972f6Sopenharmony_ci * linker section (e.g. FLASH) */ 276195972f6Sopenharmony_ci#ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST 277195972f6Sopenharmony_ci#define DNS_LOCAL_HOSTLIST_STORAGE_POST 278195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST_STORAGE_POST */ 279195972f6Sopenharmony_ciDNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[] 280195972f6Sopenharmony_ci DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT; 281195972f6Sopenharmony_ci 282195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 283195972f6Sopenharmony_ci 284195972f6Sopenharmony_cistatic void dns_init_local(void); 285195972f6Sopenharmony_cistatic err_t dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype)); 286195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST */ 287195972f6Sopenharmony_ci 288195972f6Sopenharmony_ci 289195972f6Sopenharmony_ci/* forward declarations */ 290195972f6Sopenharmony_cistatic void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); 291195972f6Sopenharmony_cistatic void dns_check_entries(void); 292195972f6Sopenharmony_cistatic void dns_call_found(u8_t idx, ip_addr_t *addr); 293195972f6Sopenharmony_ci 294195972f6Sopenharmony_ci/*----------------------------------------------------------------------------- 295195972f6Sopenharmony_ci * Globals 296195972f6Sopenharmony_ci *----------------------------------------------------------------------------*/ 297195972f6Sopenharmony_ci 298195972f6Sopenharmony_ci/* DNS variables */ 299195972f6Sopenharmony_cistatic struct udp_pcb *dns_pcbs[DNS_MAX_SOURCE_PORTS]; 300195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 301195972f6Sopenharmony_cistatic u8_t dns_last_pcb_idx; 302195972f6Sopenharmony_ci#endif 303195972f6Sopenharmony_cistatic u8_t dns_seqno; 304195972f6Sopenharmony_cistatic struct dns_table_entry dns_table[DNS_TABLE_SIZE]; 305195972f6Sopenharmony_cistatic struct dns_req_entry dns_requests[DNS_MAX_REQUESTS]; 306195972f6Sopenharmony_cistatic ip_addr_t dns_servers[DNS_MAX_SERVERS]; 307195972f6Sopenharmony_ci 308195972f6Sopenharmony_ci#if LWIP_IPV4 309195972f6Sopenharmony_ciconst ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT; 310195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */ 311195972f6Sopenharmony_ci#if LWIP_IPV6 312195972f6Sopenharmony_ciconst ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT; 313195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */ 314195972f6Sopenharmony_ci 315195972f6Sopenharmony_ci/** 316195972f6Sopenharmony_ci * Initialize the resolver: set up the UDP pcb and configure the default server 317195972f6Sopenharmony_ci * (if DNS_SERVER_ADDRESS is set). 318195972f6Sopenharmony_ci */ 319195972f6Sopenharmony_civoid 320195972f6Sopenharmony_cidns_init(void) 321195972f6Sopenharmony_ci{ 322195972f6Sopenharmony_ci#ifdef DNS_SERVER_ADDRESS 323195972f6Sopenharmony_ci /* initialize default DNS server address */ 324195972f6Sopenharmony_ci ip_addr_t dnsserver; 325195972f6Sopenharmony_ci DNS_SERVER_ADDRESS(&dnsserver); 326195972f6Sopenharmony_ci dns_setserver(0, &dnsserver); 327195972f6Sopenharmony_ci#ifdef DNS_SERVER_ADDRESS_SECONDARY 328195972f6Sopenharmony_ci DNS_SERVER_ADDRESS_SECONDARY(&dnsserver); 329195972f6Sopenharmony_ci dns_setserver(1, &dnsserver); 330195972f6Sopenharmony_ci#endif 331195972f6Sopenharmony_ci#endif /* DNS_SERVER_ADDRESS */ 332195972f6Sopenharmony_ci 333195972f6Sopenharmony_ci LWIP_ASSERT("sanity check SIZEOF_DNS_QUERY", 334195972f6Sopenharmony_ci sizeof(struct dns_query) == SIZEOF_DNS_QUERY); 335195972f6Sopenharmony_ci LWIP_ASSERT("sanity check SIZEOF_DNS_ANSWER", 336195972f6Sopenharmony_ci sizeof(struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT); 337195972f6Sopenharmony_ci 338195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n")); 339195972f6Sopenharmony_ci 340195972f6Sopenharmony_ci /* if dns client not yet initialized... */ 341195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) 342195972f6Sopenharmony_ci if (dns_pcbs[0] == NULL) { 343195972f6Sopenharmony_ci dns_pcbs[0] = udp_new_ip_type(IPADDR_TYPE_ANY); 344195972f6Sopenharmony_ci LWIP_ASSERT("dns_pcbs[0] != NULL", dns_pcbs[0] != NULL); 345195972f6Sopenharmony_ci 346195972f6Sopenharmony_ci /* initialize DNS table not needed (initialized to zero since it is a 347195972f6Sopenharmony_ci * global variable) */ 348195972f6Sopenharmony_ci LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0", 349195972f6Sopenharmony_ci DNS_STATE_UNUSED == 0); 350195972f6Sopenharmony_ci 351195972f6Sopenharmony_ci /* initialize DNS client */ 352195972f6Sopenharmony_ci udp_bind(dns_pcbs[0], IP_ANY_TYPE, 0); 353195972f6Sopenharmony_ci udp_recv(dns_pcbs[0], dns_recv, NULL); 354195972f6Sopenharmony_ci } 355195972f6Sopenharmony_ci#endif 356195972f6Sopenharmony_ci 357195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST 358195972f6Sopenharmony_ci dns_init_local(); 359195972f6Sopenharmony_ci#endif 360195972f6Sopenharmony_ci} 361195972f6Sopenharmony_ci 362195972f6Sopenharmony_ci/** 363195972f6Sopenharmony_ci * @ingroup dns 364195972f6Sopenharmony_ci * Initialize one of the DNS servers. 365195972f6Sopenharmony_ci * 366195972f6Sopenharmony_ci * @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS 367195972f6Sopenharmony_ci * @param dnsserver IP address of the DNS server to set 368195972f6Sopenharmony_ci */ 369195972f6Sopenharmony_civoid 370195972f6Sopenharmony_cidns_setserver(u8_t numdns, const ip_addr_t *dnsserver) 371195972f6Sopenharmony_ci{ 372195972f6Sopenharmony_ci if (numdns < DNS_MAX_SERVERS) { 373195972f6Sopenharmony_ci if (dnsserver != NULL) { 374195972f6Sopenharmony_ci dns_servers[numdns] = (*dnsserver); 375195972f6Sopenharmony_ci } else { 376195972f6Sopenharmony_ci dns_servers[numdns] = *IP_ADDR_ANY; 377195972f6Sopenharmony_ci } 378195972f6Sopenharmony_ci } 379195972f6Sopenharmony_ci} 380195972f6Sopenharmony_ci 381195972f6Sopenharmony_ci/** 382195972f6Sopenharmony_ci * @ingroup dns 383195972f6Sopenharmony_ci * Obtain one of the currently configured DNS server. 384195972f6Sopenharmony_ci * 385195972f6Sopenharmony_ci * @param numdns the index of the DNS server 386195972f6Sopenharmony_ci * @return IP address of the indexed DNS server or "ip_addr_any" if the DNS 387195972f6Sopenharmony_ci * server has not been configured. 388195972f6Sopenharmony_ci */ 389195972f6Sopenharmony_ciconst ip_addr_t * 390195972f6Sopenharmony_cidns_getserver(u8_t numdns) 391195972f6Sopenharmony_ci{ 392195972f6Sopenharmony_ci if (numdns < DNS_MAX_SERVERS) { 393195972f6Sopenharmony_ci return &dns_servers[numdns]; 394195972f6Sopenharmony_ci } else { 395195972f6Sopenharmony_ci return IP_ADDR_ANY; 396195972f6Sopenharmony_ci } 397195972f6Sopenharmony_ci} 398195972f6Sopenharmony_ci 399195972f6Sopenharmony_ci/** 400195972f6Sopenharmony_ci * The DNS resolver client timer - handle retries and timeouts and should 401195972f6Sopenharmony_ci * be called every DNS_TMR_INTERVAL milliseconds (every second by default). 402195972f6Sopenharmony_ci */ 403195972f6Sopenharmony_civoid 404195972f6Sopenharmony_cidns_tmr(void) 405195972f6Sopenharmony_ci{ 406195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n")); 407195972f6Sopenharmony_ci dns_check_entries(); 408195972f6Sopenharmony_ci} 409195972f6Sopenharmony_ci 410195972f6Sopenharmony_ci#if LWIP_LOWPOWER 411195972f6Sopenharmony_ci#include "lwip/lowpower.h" 412195972f6Sopenharmony_ciu32_t 413195972f6Sopenharmony_cidns_tmr_tick(void) 414195972f6Sopenharmony_ci{ 415195972f6Sopenharmony_ci u32_t tick = 0; 416195972f6Sopenharmony_ci u32_t val; 417195972f6Sopenharmony_ci s32_t i; 418195972f6Sopenharmony_ci 419195972f6Sopenharmony_ci for (i = 0; i < DNS_TABLE_SIZE; i++) { 420195972f6Sopenharmony_ci if ((dns_table[i].state == DNS_STATE_NEW) || 421195972f6Sopenharmony_ci (dns_table[i].state == DNS_STATE_ASKING)) { 422195972f6Sopenharmony_ci LWIP_DEBUGF(LOWPOWER_DEBUG, ("%s tmr tick: 1\n", "dns_tmr_tick")); 423195972f6Sopenharmony_ci return 1; 424195972f6Sopenharmony_ci } 425195972f6Sopenharmony_ci if (dns_table[i].state == DNS_STATE_DONE) { 426195972f6Sopenharmony_ci val = dns_table[i].ttl; 427195972f6Sopenharmony_ci SET_TMR_TICK(tick, val); 428195972f6Sopenharmony_ci } 429195972f6Sopenharmony_ci } 430195972f6Sopenharmony_ci LWIP_DEBUGF(LOWPOWER_DEBUG, ("%s tmr tick: %u\n", "dns_tmr_tick", tick)); 431195972f6Sopenharmony_ci return tick; 432195972f6Sopenharmony_ci} 433195972f6Sopenharmony_ci#endif 434195972f6Sopenharmony_ci 435195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST 436195972f6Sopenharmony_cistatic void 437195972f6Sopenharmony_cidns_init_local(void) 438195972f6Sopenharmony_ci{ 439195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) 440195972f6Sopenharmony_ci size_t i; 441195972f6Sopenharmony_ci struct local_hostlist_entry *entry; 442195972f6Sopenharmony_ci /* Dynamic: copy entries from DNS_LOCAL_HOSTLIST_INIT to list */ 443195972f6Sopenharmony_ci struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT; 444195972f6Sopenharmony_ci size_t namelen; 445195972f6Sopenharmony_ci for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) { 446195972f6Sopenharmony_ci struct local_hostlist_entry *init_entry = &local_hostlist_init[i]; 447195972f6Sopenharmony_ci LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL); 448195972f6Sopenharmony_ci namelen = strlen(init_entry->name); 449195972f6Sopenharmony_ci LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); 450195972f6Sopenharmony_ci entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); 451195972f6Sopenharmony_ci LWIP_ASSERT("mem-error in dns_init_local", entry != NULL); 452195972f6Sopenharmony_ci if (entry != NULL) { 453195972f6Sopenharmony_ci char *entry_name = (char *)entry + sizeof(struct local_hostlist_entry); 454195972f6Sopenharmony_ci MEMCPY(entry_name, init_entry->name, namelen); 455195972f6Sopenharmony_ci entry_name[namelen] = 0; 456195972f6Sopenharmony_ci entry->name = entry_name; 457195972f6Sopenharmony_ci entry->addr = init_entry->addr; 458195972f6Sopenharmony_ci entry->next = local_hostlist_dynamic; 459195972f6Sopenharmony_ci local_hostlist_dynamic = entry; 460195972f6Sopenharmony_ci } 461195972f6Sopenharmony_ci } 462195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) */ 463195972f6Sopenharmony_ci} 464195972f6Sopenharmony_ci 465195972f6Sopenharmony_ci/** 466195972f6Sopenharmony_ci * @ingroup dns 467195972f6Sopenharmony_ci * Iterate the local host-list for a hostname. 468195972f6Sopenharmony_ci * 469195972f6Sopenharmony_ci * @param iterator_fn a function that is called for every entry in the local host-list 470195972f6Sopenharmony_ci * @param iterator_arg 3rd argument passed to iterator_fn 471195972f6Sopenharmony_ci * @return the number of entries in the local host-list 472195972f6Sopenharmony_ci */ 473195972f6Sopenharmony_cisize_t 474195972f6Sopenharmony_cidns_local_iterate(dns_found_callback iterator_fn, void *iterator_arg) 475195972f6Sopenharmony_ci{ 476195972f6Sopenharmony_ci size_t i; 477195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 478195972f6Sopenharmony_ci struct local_hostlist_entry *entry = local_hostlist_dynamic; 479195972f6Sopenharmony_ci i = 0; 480195972f6Sopenharmony_ci while (entry != NULL) { 481195972f6Sopenharmony_ci if (iterator_fn != NULL) { 482195972f6Sopenharmony_ci iterator_fn(entry->name, &entry->addr, iterator_arg); 483195972f6Sopenharmony_ci } 484195972f6Sopenharmony_ci i++; 485195972f6Sopenharmony_ci entry = entry->next; 486195972f6Sopenharmony_ci } 487195972f6Sopenharmony_ci#else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 488195972f6Sopenharmony_ci for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) { 489195972f6Sopenharmony_ci if (iterator_fn != NULL) { 490195972f6Sopenharmony_ci iterator_fn(local_hostlist_static[i].name, &local_hostlist_static[i].addr, iterator_arg); 491195972f6Sopenharmony_ci } 492195972f6Sopenharmony_ci } 493195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 494195972f6Sopenharmony_ci return i; 495195972f6Sopenharmony_ci} 496195972f6Sopenharmony_ci 497195972f6Sopenharmony_ci/** 498195972f6Sopenharmony_ci * @ingroup dns 499195972f6Sopenharmony_ci * Scans the local host-list for a hostname. 500195972f6Sopenharmony_ci * 501195972f6Sopenharmony_ci * @param hostname Hostname to look for in the local host-list 502195972f6Sopenharmony_ci * @param addr the first IP address for the hostname in the local host-list or 503195972f6Sopenharmony_ci * IPADDR_NONE if not found. 504195972f6Sopenharmony_ci * @param dns_addrtype - LWIP_DNS_ADDRTYPE_IPV4_IPV6: try to resolve IPv4 (ATTENTION: no fallback here!) 505195972f6Sopenharmony_ci * - LWIP_DNS_ADDRTYPE_IPV6_IPV4: try to resolve IPv6 (ATTENTION: no fallback here!) 506195972f6Sopenharmony_ci * - LWIP_DNS_ADDRTYPE_IPV4: try to resolve IPv4 only 507195972f6Sopenharmony_ci * - LWIP_DNS_ADDRTYPE_IPV6: try to resolve IPv6 only 508195972f6Sopenharmony_ci * @return ERR_OK if found, ERR_ARG if not found 509195972f6Sopenharmony_ci */ 510195972f6Sopenharmony_cierr_t 511195972f6Sopenharmony_cidns_local_lookup(const char *hostname, ip_addr_t *addr, u8_t dns_addrtype) 512195972f6Sopenharmony_ci{ 513195972f6Sopenharmony_ci LWIP_UNUSED_ARG(dns_addrtype); 514195972f6Sopenharmony_ci return dns_lookup_local(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)); 515195972f6Sopenharmony_ci} 516195972f6Sopenharmony_ci 517195972f6Sopenharmony_ci/* Internal implementation for dns_local_lookup and dns_lookup */ 518195972f6Sopenharmony_cistatic err_t 519195972f6Sopenharmony_cidns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype)) 520195972f6Sopenharmony_ci{ 521195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 522195972f6Sopenharmony_ci struct local_hostlist_entry *entry = local_hostlist_dynamic; 523195972f6Sopenharmony_ci while (entry != NULL) { 524195972f6Sopenharmony_ci if ((lwip_stricmp(entry->name, hostname) == 0) && 525195972f6Sopenharmony_ci LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) { 526195972f6Sopenharmony_ci if (addr) { 527195972f6Sopenharmony_ci ip_addr_copy(*addr, entry->addr); 528195972f6Sopenharmony_ci } 529195972f6Sopenharmony_ci return ERR_OK; 530195972f6Sopenharmony_ci } 531195972f6Sopenharmony_ci entry = entry->next; 532195972f6Sopenharmony_ci } 533195972f6Sopenharmony_ci#else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 534195972f6Sopenharmony_ci size_t i; 535195972f6Sopenharmony_ci for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) { 536195972f6Sopenharmony_ci if ((lwip_stricmp(local_hostlist_static[i].name, hostname) == 0) && 537195972f6Sopenharmony_ci LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) { 538195972f6Sopenharmony_ci if (addr) { 539195972f6Sopenharmony_ci ip_addr_copy(*addr, local_hostlist_static[i].addr); 540195972f6Sopenharmony_ci } 541195972f6Sopenharmony_ci return ERR_OK; 542195972f6Sopenharmony_ci } 543195972f6Sopenharmony_ci } 544195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ 545195972f6Sopenharmony_ci return ERR_ARG; 546195972f6Sopenharmony_ci} 547195972f6Sopenharmony_ci 548195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 549195972f6Sopenharmony_ci/** 550195972f6Sopenharmony_ci * @ingroup dns 551195972f6Sopenharmony_ci * Remove all entries from the local host-list for a specific hostname 552195972f6Sopenharmony_ci * and/or IP address 553195972f6Sopenharmony_ci * 554195972f6Sopenharmony_ci * @param hostname hostname for which entries shall be removed from the local 555195972f6Sopenharmony_ci * host-list 556195972f6Sopenharmony_ci * @param addr address for which entries shall be removed from the local host-list 557195972f6Sopenharmony_ci * @return the number of removed entries 558195972f6Sopenharmony_ci */ 559195972f6Sopenharmony_ciint 560195972f6Sopenharmony_cidns_local_removehost(const char *hostname, const ip_addr_t *addr) 561195972f6Sopenharmony_ci{ 562195972f6Sopenharmony_ci int removed = 0; 563195972f6Sopenharmony_ci struct local_hostlist_entry *entry = local_hostlist_dynamic; 564195972f6Sopenharmony_ci struct local_hostlist_entry *last_entry = NULL; 565195972f6Sopenharmony_ci while (entry != NULL) { 566195972f6Sopenharmony_ci if (((hostname == NULL) || !lwip_stricmp(entry->name, hostname)) && 567195972f6Sopenharmony_ci ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) { 568195972f6Sopenharmony_ci struct local_hostlist_entry *free_entry; 569195972f6Sopenharmony_ci if (last_entry != NULL) { 570195972f6Sopenharmony_ci last_entry->next = entry->next; 571195972f6Sopenharmony_ci } else { 572195972f6Sopenharmony_ci local_hostlist_dynamic = entry->next; 573195972f6Sopenharmony_ci } 574195972f6Sopenharmony_ci free_entry = entry; 575195972f6Sopenharmony_ci entry = entry->next; 576195972f6Sopenharmony_ci memp_free(MEMP_LOCALHOSTLIST, free_entry); 577195972f6Sopenharmony_ci removed++; 578195972f6Sopenharmony_ci } else { 579195972f6Sopenharmony_ci last_entry = entry; 580195972f6Sopenharmony_ci entry = entry->next; 581195972f6Sopenharmony_ci } 582195972f6Sopenharmony_ci } 583195972f6Sopenharmony_ci return removed; 584195972f6Sopenharmony_ci} 585195972f6Sopenharmony_ci 586195972f6Sopenharmony_ci/** 587195972f6Sopenharmony_ci * @ingroup dns 588195972f6Sopenharmony_ci * Add a hostname/IP address pair to the local host-list. 589195972f6Sopenharmony_ci * Duplicates are not checked. 590195972f6Sopenharmony_ci * 591195972f6Sopenharmony_ci * @param hostname hostname of the new entry 592195972f6Sopenharmony_ci * @param addr IP address of the new entry 593195972f6Sopenharmony_ci * @return ERR_OK if succeeded or ERR_MEM on memory error 594195972f6Sopenharmony_ci */ 595195972f6Sopenharmony_cierr_t 596195972f6Sopenharmony_cidns_local_addhost(const char *hostname, const ip_addr_t *addr) 597195972f6Sopenharmony_ci{ 598195972f6Sopenharmony_ci struct local_hostlist_entry *entry; 599195972f6Sopenharmony_ci size_t namelen; 600195972f6Sopenharmony_ci char *entry_name; 601195972f6Sopenharmony_ci LWIP_ASSERT("invalid host name (NULL)", hostname != NULL); 602195972f6Sopenharmony_ci namelen = strlen(hostname); 603195972f6Sopenharmony_ci LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); 604195972f6Sopenharmony_ci entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); 605195972f6Sopenharmony_ci if (entry == NULL) { 606195972f6Sopenharmony_ci return ERR_MEM; 607195972f6Sopenharmony_ci } 608195972f6Sopenharmony_ci entry_name = (char *)entry + sizeof(struct local_hostlist_entry); 609195972f6Sopenharmony_ci MEMCPY(entry_name, hostname, namelen); 610195972f6Sopenharmony_ci entry_name[namelen] = 0; 611195972f6Sopenharmony_ci entry->name = entry_name; 612195972f6Sopenharmony_ci ip_addr_copy(entry->addr, *addr); 613195972f6Sopenharmony_ci entry->next = local_hostlist_dynamic; 614195972f6Sopenharmony_ci local_hostlist_dynamic = entry; 615195972f6Sopenharmony_ci return ERR_OK; 616195972f6Sopenharmony_ci} 617195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC*/ 618195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST */ 619195972f6Sopenharmony_ci 620195972f6Sopenharmony_ci/** 621195972f6Sopenharmony_ci * @ingroup dns 622195972f6Sopenharmony_ci * Look up a hostname in the array of known hostnames. 623195972f6Sopenharmony_ci * 624195972f6Sopenharmony_ci * @note This function only looks in the internal array of known 625195972f6Sopenharmony_ci * hostnames, it does not send out a query for the hostname if none 626195972f6Sopenharmony_ci * was found. The function dns_enqueue() can be used to send a query 627195972f6Sopenharmony_ci * for a hostname. 628195972f6Sopenharmony_ci * 629195972f6Sopenharmony_ci * @param name the hostname to look up 630195972f6Sopenharmony_ci * @param addr the hostname's IP address, as u32_t (instead of ip_addr_t to 631195972f6Sopenharmony_ci * better check for failure: != IPADDR_NONE) or IPADDR_NONE if the hostname 632195972f6Sopenharmony_ci * was not found in the cached dns_table. 633195972f6Sopenharmony_ci * @return ERR_OK if found, ERR_ARG if not found 634195972f6Sopenharmony_ci */ 635195972f6Sopenharmony_cistatic err_t 636195972f6Sopenharmony_cidns_lookup(const char *name, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype)) 637195972f6Sopenharmony_ci{ 638195972f6Sopenharmony_ci u8_t i; 639195972f6Sopenharmony_ci#if DNS_LOCAL_HOSTLIST 640195972f6Sopenharmony_ci if (dns_lookup_local(name, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) { 641195972f6Sopenharmony_ci return ERR_OK; 642195972f6Sopenharmony_ci } 643195972f6Sopenharmony_ci#endif /* DNS_LOCAL_HOSTLIST */ 644195972f6Sopenharmony_ci#ifdef DNS_LOOKUP_LOCAL_EXTERN 645195972f6Sopenharmony_ci if (DNS_LOOKUP_LOCAL_EXTERN(name, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) == ERR_OK) { 646195972f6Sopenharmony_ci return ERR_OK; 647195972f6Sopenharmony_ci } 648195972f6Sopenharmony_ci#endif /* DNS_LOOKUP_LOCAL_EXTERN */ 649195972f6Sopenharmony_ci 650195972f6Sopenharmony_ci /* Walk through name list, return entry if found. If not, return NULL. */ 651195972f6Sopenharmony_ci for (i = 0; i < DNS_TABLE_SIZE; ++i) { 652195972f6Sopenharmony_ci if ((dns_table[i].state == DNS_STATE_DONE) && 653195972f6Sopenharmony_ci (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0) && 654195972f6Sopenharmony_ci LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) { 655195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name)); 656195972f6Sopenharmony_ci ip_addr_debug_print_val(DNS_DEBUG, dns_table[i].ipaddr); 657195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("\n")); 658195972f6Sopenharmony_ci if (addr) { 659195972f6Sopenharmony_ci ip_addr_copy(*addr, dns_table[i].ipaddr); 660195972f6Sopenharmony_ci } 661195972f6Sopenharmony_ci return ERR_OK; 662195972f6Sopenharmony_ci } 663195972f6Sopenharmony_ci } 664195972f6Sopenharmony_ci 665195972f6Sopenharmony_ci return ERR_ARG; 666195972f6Sopenharmony_ci} 667195972f6Sopenharmony_ci 668195972f6Sopenharmony_ci/** 669195972f6Sopenharmony_ci * Compare the "dotted" name "query" with the encoded name "response" 670195972f6Sopenharmony_ci * to make sure an answer from the DNS server matches the current dns_table 671195972f6Sopenharmony_ci * entry (otherwise, answers might arrive late for hostname not on the list 672195972f6Sopenharmony_ci * any more). 673195972f6Sopenharmony_ci * 674195972f6Sopenharmony_ci * For now, this function compares case-insensitive to cope with all kinds of 675195972f6Sopenharmony_ci * servers. This also means that "dns 0x20 bit encoding" must be checked 676195972f6Sopenharmony_ci * externally, if we want to implement it. 677195972f6Sopenharmony_ci * Currently, the request is sent exactly as passed in by he user request. 678195972f6Sopenharmony_ci * 679195972f6Sopenharmony_ci * @param query hostname (not encoded) from the dns_table 680195972f6Sopenharmony_ci * @param p pbuf containing the encoded hostname in the DNS response 681195972f6Sopenharmony_ci * @param start_offset offset into p where the name starts 682195972f6Sopenharmony_ci * @return 0xFFFF: names differ, other: names equal -> offset behind name 683195972f6Sopenharmony_ci */ 684195972f6Sopenharmony_cistatic u16_t 685195972f6Sopenharmony_cidns_compare_name(const char *query, struct pbuf *p, u16_t start_offset) 686195972f6Sopenharmony_ci{ 687195972f6Sopenharmony_ci int n; 688195972f6Sopenharmony_ci u16_t response_offset = start_offset; 689195972f6Sopenharmony_ci 690195972f6Sopenharmony_ci do { 691195972f6Sopenharmony_ci n = pbuf_try_get_at(p, response_offset); 692195972f6Sopenharmony_ci if ((n < 0) || (response_offset == 0xFFFF)) { 693195972f6Sopenharmony_ci /* error or overflow */ 694195972f6Sopenharmony_ci return 0xFFFF; 695195972f6Sopenharmony_ci } 696195972f6Sopenharmony_ci response_offset++; 697195972f6Sopenharmony_ci /** @see RFC 1035 - 4.1.4. Message compression */ 698195972f6Sopenharmony_ci if ((n & 0xc0) == 0xc0) { 699195972f6Sopenharmony_ci /* Compressed name: cannot be equal since we don't send them */ 700195972f6Sopenharmony_ci return 0xFFFF; 701195972f6Sopenharmony_ci } else { 702195972f6Sopenharmony_ci /* Not compressed name */ 703195972f6Sopenharmony_ci while (n > 0) { 704195972f6Sopenharmony_ci int c = pbuf_try_get_at(p, response_offset); 705195972f6Sopenharmony_ci if (c < 0) { 706195972f6Sopenharmony_ci return 0xFFFF; 707195972f6Sopenharmony_ci } 708195972f6Sopenharmony_ci if (lwip_tolower((*query)) != lwip_tolower((u8_t)c)) { 709195972f6Sopenharmony_ci return 0xFFFF; 710195972f6Sopenharmony_ci } 711195972f6Sopenharmony_ci if (response_offset == 0xFFFF) { 712195972f6Sopenharmony_ci /* would overflow */ 713195972f6Sopenharmony_ci return 0xFFFF; 714195972f6Sopenharmony_ci } 715195972f6Sopenharmony_ci response_offset++; 716195972f6Sopenharmony_ci ++query; 717195972f6Sopenharmony_ci --n; 718195972f6Sopenharmony_ci } 719195972f6Sopenharmony_ci ++query; 720195972f6Sopenharmony_ci } 721195972f6Sopenharmony_ci n = pbuf_try_get_at(p, response_offset); 722195972f6Sopenharmony_ci if (n < 0) { 723195972f6Sopenharmony_ci return 0xFFFF; 724195972f6Sopenharmony_ci } 725195972f6Sopenharmony_ci } while (n != 0); 726195972f6Sopenharmony_ci 727195972f6Sopenharmony_ci if (response_offset == 0xFFFF) { 728195972f6Sopenharmony_ci /* would overflow */ 729195972f6Sopenharmony_ci return 0xFFFF; 730195972f6Sopenharmony_ci } 731195972f6Sopenharmony_ci return (u16_t)(response_offset + 1); 732195972f6Sopenharmony_ci} 733195972f6Sopenharmony_ci 734195972f6Sopenharmony_ci/** 735195972f6Sopenharmony_ci * Walk through a compact encoded DNS name and return the end of the name. 736195972f6Sopenharmony_ci * 737195972f6Sopenharmony_ci * @param p pbuf containing the name 738195972f6Sopenharmony_ci * @param query_idx start index into p pointing to encoded DNS name in the DNS server response 739195972f6Sopenharmony_ci * @return index to end of the name 740195972f6Sopenharmony_ci */ 741195972f6Sopenharmony_cistatic u16_t 742195972f6Sopenharmony_cidns_skip_name(struct pbuf *p, u16_t query_idx) 743195972f6Sopenharmony_ci{ 744195972f6Sopenharmony_ci int n; 745195972f6Sopenharmony_ci u16_t offset = query_idx; 746195972f6Sopenharmony_ci 747195972f6Sopenharmony_ci do { 748195972f6Sopenharmony_ci n = pbuf_try_get_at(p, offset++); 749195972f6Sopenharmony_ci if ((n < 0) || (offset == 0)) { 750195972f6Sopenharmony_ci return 0xFFFF; 751195972f6Sopenharmony_ci } 752195972f6Sopenharmony_ci /** @see RFC 1035 - 4.1.4. Message compression */ 753195972f6Sopenharmony_ci if ((n & 0xc0) == 0xc0) { 754195972f6Sopenharmony_ci /* Compressed name: since we only want to skip it (not check it), stop here */ 755195972f6Sopenharmony_ci break; 756195972f6Sopenharmony_ci } else { 757195972f6Sopenharmony_ci /* Not compressed name */ 758195972f6Sopenharmony_ci if (offset + n >= p->tot_len) { 759195972f6Sopenharmony_ci return 0xFFFF; 760195972f6Sopenharmony_ci } 761195972f6Sopenharmony_ci offset = (u16_t)(offset + n); 762195972f6Sopenharmony_ci } 763195972f6Sopenharmony_ci n = pbuf_try_get_at(p, offset); 764195972f6Sopenharmony_ci if (n < 0) { 765195972f6Sopenharmony_ci return 0xFFFF; 766195972f6Sopenharmony_ci } 767195972f6Sopenharmony_ci } while (n != 0); 768195972f6Sopenharmony_ci 769195972f6Sopenharmony_ci if (offset == 0xFFFF) { 770195972f6Sopenharmony_ci return 0xFFFF; 771195972f6Sopenharmony_ci } 772195972f6Sopenharmony_ci return (u16_t)(offset + 1); 773195972f6Sopenharmony_ci} 774195972f6Sopenharmony_ci 775195972f6Sopenharmony_ci/** 776195972f6Sopenharmony_ci * Send a DNS query packet. 777195972f6Sopenharmony_ci * 778195972f6Sopenharmony_ci * @param idx the DNS table entry index for which to send a request 779195972f6Sopenharmony_ci * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise 780195972f6Sopenharmony_ci */ 781195972f6Sopenharmony_cistatic err_t 782195972f6Sopenharmony_cidns_send(u8_t idx) 783195972f6Sopenharmony_ci{ 784195972f6Sopenharmony_ci err_t err; 785195972f6Sopenharmony_ci struct dns_hdr hdr; 786195972f6Sopenharmony_ci struct dns_query qry; 787195972f6Sopenharmony_ci struct pbuf *p; 788195972f6Sopenharmony_ci u16_t query_idx, copy_len; 789195972f6Sopenharmony_ci const char *hostname, *hostname_part; 790195972f6Sopenharmony_ci u8_t n; 791195972f6Sopenharmony_ci u8_t pcb_idx; 792195972f6Sopenharmony_ci struct dns_table_entry *entry = &dns_table[idx]; 793195972f6Sopenharmony_ci 794195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n", 795195972f6Sopenharmony_ci (u16_t)(entry->server_idx), entry->name)); 796195972f6Sopenharmony_ci LWIP_ASSERT("dns server out of array", entry->server_idx < DNS_MAX_SERVERS); 797195972f6Sopenharmony_ci if (ip_addr_isany_val(dns_servers[entry->server_idx]) 798195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 799195972f6Sopenharmony_ci && !entry->is_mdns 800195972f6Sopenharmony_ci#endif 801195972f6Sopenharmony_ci ) { 802195972f6Sopenharmony_ci /* DNS server not valid anymore, e.g. PPP netif has been shut down */ 803195972f6Sopenharmony_ci /* call specified callback function if provided */ 804195972f6Sopenharmony_ci dns_call_found(idx, NULL); 805195972f6Sopenharmony_ci /* flush this entry */ 806195972f6Sopenharmony_ci entry->state = DNS_STATE_UNUSED; 807195972f6Sopenharmony_ci return ERR_OK; 808195972f6Sopenharmony_ci } 809195972f6Sopenharmony_ci 810195972f6Sopenharmony_ci /* if here, we have either a new query or a retry on a previous query to process */ 811195972f6Sopenharmony_ci#if LWIP_ENABLE_DISTRIBUTED_NET && !LWIP_USE_GET_HOST_BY_NAME_EXTERNAL 812195972f6Sopenharmony_ci if (is_distributed_net_enabled()) { 813195972f6Sopenharmony_ci p = pbuf_alloc(PBUF_TRANSPORT, 814195972f6Sopenharmony_ci (u16_t)(sizeof(udp_data) + SIZEOF_DNS_HDR + strlen(entry->name) + 2 + SIZEOF_DNS_QUERY), PBUF_RAM); 815195972f6Sopenharmony_ci } else { 816195972f6Sopenharmony_ci p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(SIZEOF_DNS_HDR + strlen(entry->name) + 2 + SIZEOF_DNS_QUERY), PBUF_RAM); 817195972f6Sopenharmony_ci } 818195972f6Sopenharmony_ci#else 819195972f6Sopenharmony_ci p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(SIZEOF_DNS_HDR + strlen(entry->name) + 2 + 820195972f6Sopenharmony_ci SIZEOF_DNS_QUERY), PBUF_RAM); 821195972f6Sopenharmony_ci#endif 822195972f6Sopenharmony_ci 823195972f6Sopenharmony_ci if (p != NULL) { 824195972f6Sopenharmony_ci const ip_addr_t *dst; 825195972f6Sopenharmony_ci u16_t dst_port; 826195972f6Sopenharmony_ci /* fill dns header */ 827195972f6Sopenharmony_ci memset(&hdr, 0, SIZEOF_DNS_HDR); 828195972f6Sopenharmony_ci hdr.id = lwip_htons(entry->txid); 829195972f6Sopenharmony_ci hdr.flags1 = DNS_FLAG1_RD; 830195972f6Sopenharmony_ci hdr.numquestions = PP_HTONS(1); 831195972f6Sopenharmony_ci#if LWIP_ENABLE_DISTRIBUTED_NET && !LWIP_USE_GET_HOST_BY_NAME_EXTERNAL 832195972f6Sopenharmony_ci if (is_distributed_net_enabled()) { 833195972f6Sopenharmony_ci udp_data udp_data_hdr = {0}; 834195972f6Sopenharmony_ci (void)memset_s(&udp_data_hdr, sizeof(udp_data_hdr), 0, sizeof(udp_data_hdr)); 835195972f6Sopenharmony_ci dst = &dns_servers[entry->server_idx]; 836195972f6Sopenharmony_ci 837195972f6Sopenharmony_ci#if LWIP_IPV6 838195972f6Sopenharmony_ci (void)strcpy_s(udp_data_hdr.dest_addr, sizeof(udp_data_hdr.dest_addr), ip4addr_ntoa(&dst->u_addr.ip4)); 839195972f6Sopenharmony_ci#else 840195972f6Sopenharmony_ci (void)strcpy_s(udp_data_hdr.dest_addr, sizeof(udp_data_hdr.dest_addr), ip4addr_ntoa(dst)); 841195972f6Sopenharmony_ci#endif 842195972f6Sopenharmony_ci 843195972f6Sopenharmony_ci udp_data_hdr.dest_port = DNS_SERVER_PORT; 844195972f6Sopenharmony_ci 845195972f6Sopenharmony_ci pbuf_take(p, &udp_data_hdr, sizeof(udp_data_hdr)); 846195972f6Sopenharmony_ci pbuf_take_at(p, &hdr, SIZEOF_DNS_HDR, sizeof(udp_data_hdr)); 847195972f6Sopenharmony_ci } else { 848195972f6Sopenharmony_ci pbuf_take(p, &hdr, SIZEOF_DNS_HDR); 849195972f6Sopenharmony_ci } 850195972f6Sopenharmony_ci#else 851195972f6Sopenharmony_ci pbuf_take(p, &hdr, SIZEOF_DNS_HDR); 852195972f6Sopenharmony_ci#endif 853195972f6Sopenharmony_ci hostname = entry->name; 854195972f6Sopenharmony_ci --hostname; 855195972f6Sopenharmony_ci 856195972f6Sopenharmony_ci /* convert hostname into suitable query format. */ 857195972f6Sopenharmony_ci#if LWIP_ENABLE_DISTRIBUTED_NET && !LWIP_USE_GET_HOST_BY_NAME_EXTERNAL 858195972f6Sopenharmony_ci if (is_distributed_net_enabled()) { 859195972f6Sopenharmony_ci query_idx = sizeof(udp_data) + SIZEOF_DNS_HDR; 860195972f6Sopenharmony_ci } else { 861195972f6Sopenharmony_ci query_idx = SIZEOF_DNS_HDR; 862195972f6Sopenharmony_ci } 863195972f6Sopenharmony_ci#else 864195972f6Sopenharmony_ci query_idx = SIZEOF_DNS_HDR; 865195972f6Sopenharmony_ci#endif 866195972f6Sopenharmony_ci do { 867195972f6Sopenharmony_ci ++hostname; 868195972f6Sopenharmony_ci hostname_part = hostname; 869195972f6Sopenharmony_ci for (n = 0; *hostname != '.' && *hostname != 0; ++hostname) { 870195972f6Sopenharmony_ci ++n; 871195972f6Sopenharmony_ci } 872195972f6Sopenharmony_ci copy_len = (u16_t)(hostname - hostname_part); 873195972f6Sopenharmony_ci if (query_idx + n + 1 > 0xFFFF) { 874195972f6Sopenharmony_ci /* u16_t overflow */ 875195972f6Sopenharmony_ci goto overflow_return; 876195972f6Sopenharmony_ci } 877195972f6Sopenharmony_ci pbuf_put_at(p, query_idx, n); 878195972f6Sopenharmony_ci pbuf_take_at(p, hostname_part, copy_len, (u16_t)(query_idx + 1)); 879195972f6Sopenharmony_ci query_idx = (u16_t)(query_idx + n + 1); 880195972f6Sopenharmony_ci } while (*hostname != 0); 881195972f6Sopenharmony_ci pbuf_put_at(p, query_idx, 0); 882195972f6Sopenharmony_ci query_idx++; 883195972f6Sopenharmony_ci 884195972f6Sopenharmony_ci /* fill dns query */ 885195972f6Sopenharmony_ci if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) { 886195972f6Sopenharmony_ci qry.type = PP_HTONS(DNS_RRTYPE_AAAA); 887195972f6Sopenharmony_ci } else { 888195972f6Sopenharmony_ci qry.type = PP_HTONS(DNS_RRTYPE_A); 889195972f6Sopenharmony_ci } 890195972f6Sopenharmony_ci qry.cls = PP_HTONS(DNS_RRCLASS_IN); 891195972f6Sopenharmony_ci pbuf_take_at(p, &qry, SIZEOF_DNS_QUERY, query_idx); 892195972f6Sopenharmony_ci 893195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 894195972f6Sopenharmony_ci pcb_idx = entry->pcb_idx; 895195972f6Sopenharmony_ci#else 896195972f6Sopenharmony_ci pcb_idx = 0; 897195972f6Sopenharmony_ci#endif 898195972f6Sopenharmony_ci /* send dns packet */ 899195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("sending DNS request ID %d for name \"%s\" to server %d\r\n", 900195972f6Sopenharmony_ci entry->txid, entry->name, entry->server_idx)); 901195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 902195972f6Sopenharmony_ci if (entry->is_mdns) { 903195972f6Sopenharmony_ci dst_port = DNS_MQUERY_PORT; 904195972f6Sopenharmony_ci#if LWIP_IPV6 905195972f6Sopenharmony_ci if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) { 906195972f6Sopenharmony_ci dst = &dns_mquery_v6group; 907195972f6Sopenharmony_ci } 908195972f6Sopenharmony_ci#endif 909195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 910195972f6Sopenharmony_ci else 911195972f6Sopenharmony_ci#endif 912195972f6Sopenharmony_ci#if LWIP_IPV4 913195972f6Sopenharmony_ci { 914195972f6Sopenharmony_ci dst = &dns_mquery_v4group; 915195972f6Sopenharmony_ci } 916195972f6Sopenharmony_ci#endif 917195972f6Sopenharmony_ci } else 918195972f6Sopenharmony_ci#endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 919195972f6Sopenharmony_ci { 920195972f6Sopenharmony_ci dst_port = DNS_SERVER_PORT; 921195972f6Sopenharmony_ci dst = &dns_servers[entry->server_idx]; 922195972f6Sopenharmony_ci } 923195972f6Sopenharmony_ci#if LWIP_ENABLE_DISTRIBUTED_NET && !LWIP_USE_GET_HOST_BY_NAME_EXTERNAL 924195972f6Sopenharmony_ci if (is_distributed_net_enabled()) { 925195972f6Sopenharmony_ci ip_addr_t local_addr = {0}; 926195972f6Sopenharmony_ci dst_port = get_local_udp_server_port(); 927195972f6Sopenharmony_ci 928195972f6Sopenharmony_ci#if LWIP_IPV6 929195972f6Sopenharmony_ci local_addr.u_addr.ip4.addr = ipaddr_addr(LOCAL_SERVER_IP); 930195972f6Sopenharmony_ci local_addr.type = IPADDR_TYPE_V4; 931195972f6Sopenharmony_ci#else 932195972f6Sopenharmony_ci local_addr.addr = ipaddr_addr(LOCAL_SERVER_IP); 933195972f6Sopenharmony_ci#endif 934195972f6Sopenharmony_ci#if (defined(EMUI_WEB_CLIENT)) 935195972f6Sopenharmony_ci DISTRIBUTED_NET_START_UDP_SERVER(); 936195972f6Sopenharmony_ci#endif 937195972f6Sopenharmony_ci err = udp_sendto(dns_pcbs[pcb_idx], p, &local_addr, dst_port); 938195972f6Sopenharmony_ci } else { 939195972f6Sopenharmony_ci err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port); 940195972f6Sopenharmony_ci } 941195972f6Sopenharmony_ci#else 942195972f6Sopenharmony_ci err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port); 943195972f6Sopenharmony_ci#endif 944195972f6Sopenharmony_ci 945195972f6Sopenharmony_ci /* free pbuf */ 946195972f6Sopenharmony_ci pbuf_free(p); 947195972f6Sopenharmony_ci } else { 948195972f6Sopenharmony_ci err = ERR_MEM; 949195972f6Sopenharmony_ci } 950195972f6Sopenharmony_ci 951195972f6Sopenharmony_ci return err; 952195972f6Sopenharmony_cioverflow_return: 953195972f6Sopenharmony_ci pbuf_free(p); 954195972f6Sopenharmony_ci return ERR_VAL; 955195972f6Sopenharmony_ci} 956195972f6Sopenharmony_ci 957195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 958195972f6Sopenharmony_cistatic struct udp_pcb * 959195972f6Sopenharmony_cidns_alloc_random_port(void) 960195972f6Sopenharmony_ci{ 961195972f6Sopenharmony_ci err_t err; 962195972f6Sopenharmony_ci struct udp_pcb *pcb; 963195972f6Sopenharmony_ci 964195972f6Sopenharmony_ci pcb = udp_new_ip_type(IPADDR_TYPE_ANY); 965195972f6Sopenharmony_ci if (pcb == NULL) { 966195972f6Sopenharmony_ci /* out of memory, have to reuse an existing pcb */ 967195972f6Sopenharmony_ci return NULL; 968195972f6Sopenharmony_ci } 969195972f6Sopenharmony_ci do { 970195972f6Sopenharmony_ci u16_t port = (u16_t)DNS_RAND_TXID(); 971195972f6Sopenharmony_ci if (DNS_PORT_ALLOWED(port)) { 972195972f6Sopenharmony_ci err = udp_bind(pcb, IP_ANY_TYPE, port); 973195972f6Sopenharmony_ci } else { 974195972f6Sopenharmony_ci /* this port is not allowed, try again */ 975195972f6Sopenharmony_ci err = ERR_USE; 976195972f6Sopenharmony_ci } 977195972f6Sopenharmony_ci } while (err == ERR_USE); 978195972f6Sopenharmony_ci if (err != ERR_OK) { 979195972f6Sopenharmony_ci udp_remove(pcb); 980195972f6Sopenharmony_ci return NULL; 981195972f6Sopenharmony_ci } 982195972f6Sopenharmony_ci udp_recv(pcb, dns_recv, NULL); 983195972f6Sopenharmony_ci return pcb; 984195972f6Sopenharmony_ci} 985195972f6Sopenharmony_ci 986195972f6Sopenharmony_ci/** 987195972f6Sopenharmony_ci * dns_alloc_pcb() - allocates a new pcb (or reuses an existing one) to be used 988195972f6Sopenharmony_ci * for sending a request 989195972f6Sopenharmony_ci * 990195972f6Sopenharmony_ci * @return an index into dns_pcbs 991195972f6Sopenharmony_ci */ 992195972f6Sopenharmony_cistatic u8_t 993195972f6Sopenharmony_cidns_alloc_pcb(void) 994195972f6Sopenharmony_ci{ 995195972f6Sopenharmony_ci u8_t i; 996195972f6Sopenharmony_ci u8_t idx; 997195972f6Sopenharmony_ci 998195972f6Sopenharmony_ci for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) { 999195972f6Sopenharmony_ci if (dns_pcbs[i] == NULL) { 1000195972f6Sopenharmony_ci break; 1001195972f6Sopenharmony_ci } 1002195972f6Sopenharmony_ci } 1003195972f6Sopenharmony_ci if (i < DNS_MAX_SOURCE_PORTS) { 1004195972f6Sopenharmony_ci dns_pcbs[i] = dns_alloc_random_port(); 1005195972f6Sopenharmony_ci if (dns_pcbs[i] != NULL) { 1006195972f6Sopenharmony_ci /* succeeded */ 1007195972f6Sopenharmony_ci dns_last_pcb_idx = i; 1008195972f6Sopenharmony_ci return i; 1009195972f6Sopenharmony_ci } 1010195972f6Sopenharmony_ci } 1011195972f6Sopenharmony_ci /* if we come here, creating a new UDP pcb failed, so we have to use 1012195972f6Sopenharmony_ci an already existing one (so overflow is no issue) */ 1013195972f6Sopenharmony_ci for (i = 0, idx = (u8_t)(dns_last_pcb_idx + 1); i < DNS_MAX_SOURCE_PORTS; i++, idx++) { 1014195972f6Sopenharmony_ci if (idx >= DNS_MAX_SOURCE_PORTS) { 1015195972f6Sopenharmony_ci idx = 0; 1016195972f6Sopenharmony_ci } 1017195972f6Sopenharmony_ci if (dns_pcbs[idx] != NULL) { 1018195972f6Sopenharmony_ci dns_last_pcb_idx = idx; 1019195972f6Sopenharmony_ci return idx; 1020195972f6Sopenharmony_ci } 1021195972f6Sopenharmony_ci } 1022195972f6Sopenharmony_ci return DNS_MAX_SOURCE_PORTS; 1023195972f6Sopenharmony_ci} 1024195972f6Sopenharmony_ci#endif /* ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) */ 1025195972f6Sopenharmony_ci 1026195972f6Sopenharmony_ci/** 1027195972f6Sopenharmony_ci * dns_call_found() - call the found callback and check if there are duplicate 1028195972f6Sopenharmony_ci * entries for the given hostname. If there are any, their found callback will 1029195972f6Sopenharmony_ci * be called and they will be removed. 1030195972f6Sopenharmony_ci * 1031195972f6Sopenharmony_ci * @param idx dns table index of the entry that is resolved or removed 1032195972f6Sopenharmony_ci * @param addr IP address for the hostname (or NULL on error or memory shortage) 1033195972f6Sopenharmony_ci */ 1034195972f6Sopenharmony_cistatic void 1035195972f6Sopenharmony_cidns_call_found(u8_t idx, ip_addr_t *addr) 1036195972f6Sopenharmony_ci{ 1037195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & (LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT)) != 0) 1038195972f6Sopenharmony_ci u8_t i; 1039195972f6Sopenharmony_ci#endif 1040195972f6Sopenharmony_ci 1041195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 1042195972f6Sopenharmony_ci if (addr != NULL) { 1043195972f6Sopenharmony_ci /* check that address type matches the request and adapt the table entry */ 1044195972f6Sopenharmony_ci if (IP_IS_V6_VAL(*addr)) { 1045195972f6Sopenharmony_ci LWIP_ASSERT("invalid response", LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype)); 1046195972f6Sopenharmony_ci dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6; 1047195972f6Sopenharmony_ci } else { 1048195972f6Sopenharmony_ci LWIP_ASSERT("invalid response", !LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype)); 1049195972f6Sopenharmony_ci dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4; 1050195972f6Sopenharmony_ci } 1051195972f6Sopenharmony_ci } 1052195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 1053195972f6Sopenharmony_ci 1054195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 1055195972f6Sopenharmony_ci for (i = 0; i < DNS_MAX_REQUESTS; i++) { 1056195972f6Sopenharmony_ci if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) { 1057195972f6Sopenharmony_ci (*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg); 1058195972f6Sopenharmony_ci /* flush this entry */ 1059195972f6Sopenharmony_ci dns_requests[i].found = NULL; 1060195972f6Sopenharmony_ci } 1061195972f6Sopenharmony_ci } 1062195972f6Sopenharmony_ci#else 1063195972f6Sopenharmony_ci if (dns_requests[idx].found) { 1064195972f6Sopenharmony_ci (*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg); 1065195972f6Sopenharmony_ci } 1066195972f6Sopenharmony_ci dns_requests[idx].found = NULL; 1067195972f6Sopenharmony_ci#endif 1068195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 1069195972f6Sopenharmony_ci /* close the pcb used unless other request are using it */ 1070195972f6Sopenharmony_ci for (i = 0; i < DNS_MAX_REQUESTS; i++) { 1071195972f6Sopenharmony_ci if (i == idx) { 1072195972f6Sopenharmony_ci continue; /* only check other requests */ 1073195972f6Sopenharmony_ci } 1074195972f6Sopenharmony_ci if (dns_table[i].state == DNS_STATE_ASKING) { 1075195972f6Sopenharmony_ci if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) { 1076195972f6Sopenharmony_ci /* another request is still using the same pcb */ 1077195972f6Sopenharmony_ci dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS; 1078195972f6Sopenharmony_ci break; 1079195972f6Sopenharmony_ci } 1080195972f6Sopenharmony_ci } 1081195972f6Sopenharmony_ci } 1082195972f6Sopenharmony_ci if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) { 1083195972f6Sopenharmony_ci /* if we come here, the pcb is not used any more and can be removed */ 1084195972f6Sopenharmony_ci udp_remove(dns_pcbs[dns_table[idx].pcb_idx]); 1085195972f6Sopenharmony_ci dns_pcbs[dns_table[idx].pcb_idx] = NULL; 1086195972f6Sopenharmony_ci dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS; 1087195972f6Sopenharmony_ci } 1088195972f6Sopenharmony_ci#endif 1089195972f6Sopenharmony_ci} 1090195972f6Sopenharmony_ci 1091195972f6Sopenharmony_ci/* Create a query transmission ID that is unique for all outstanding queries */ 1092195972f6Sopenharmony_cistatic u16_t 1093195972f6Sopenharmony_cidns_create_txid(void) 1094195972f6Sopenharmony_ci{ 1095195972f6Sopenharmony_ci u16_t txid; 1096195972f6Sopenharmony_ci u8_t i; 1097195972f6Sopenharmony_ci 1098195972f6Sopenharmony_ciagain: 1099195972f6Sopenharmony_ci txid = (u16_t)DNS_RAND_TXID(); 1100195972f6Sopenharmony_ci 1101195972f6Sopenharmony_ci /* check whether the ID is unique */ 1102195972f6Sopenharmony_ci for (i = 0; i < DNS_TABLE_SIZE; i++) { 1103195972f6Sopenharmony_ci if ((dns_table[i].state == DNS_STATE_ASKING) && 1104195972f6Sopenharmony_ci (dns_table[i].txid == txid)) { 1105195972f6Sopenharmony_ci /* ID already used by another pending query */ 1106195972f6Sopenharmony_ci goto again; 1107195972f6Sopenharmony_ci } 1108195972f6Sopenharmony_ci } 1109195972f6Sopenharmony_ci 1110195972f6Sopenharmony_ci return txid; 1111195972f6Sopenharmony_ci} 1112195972f6Sopenharmony_ci 1113195972f6Sopenharmony_ci/** 1114195972f6Sopenharmony_ci * Check whether there are other backup DNS servers available to try 1115195972f6Sopenharmony_ci */ 1116195972f6Sopenharmony_cistatic u8_t 1117195972f6Sopenharmony_cidns_backupserver_available(struct dns_table_entry *pentry) 1118195972f6Sopenharmony_ci{ 1119195972f6Sopenharmony_ci u8_t ret = 0; 1120195972f6Sopenharmony_ci 1121195972f6Sopenharmony_ci if (pentry) { 1122195972f6Sopenharmony_ci if ((pentry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[pentry->server_idx + 1])) { 1123195972f6Sopenharmony_ci ret = 1; 1124195972f6Sopenharmony_ci } 1125195972f6Sopenharmony_ci } 1126195972f6Sopenharmony_ci 1127195972f6Sopenharmony_ci return ret; 1128195972f6Sopenharmony_ci} 1129195972f6Sopenharmony_ci 1130195972f6Sopenharmony_ci/** 1131195972f6Sopenharmony_ci * dns_check_entry() - see if entry has not yet been queried and, if so, sends out a query. 1132195972f6Sopenharmony_ci * Check an entry in the dns_table: 1133195972f6Sopenharmony_ci * - send out query for new entries 1134195972f6Sopenharmony_ci * - retry old pending entries on timeout (also with different servers) 1135195972f6Sopenharmony_ci * - remove completed entries from the table if their TTL has expired 1136195972f6Sopenharmony_ci * 1137195972f6Sopenharmony_ci * @param i index of the dns_table entry to check 1138195972f6Sopenharmony_ci */ 1139195972f6Sopenharmony_cistatic void 1140195972f6Sopenharmony_cidns_check_entry(u8_t i) 1141195972f6Sopenharmony_ci{ 1142195972f6Sopenharmony_ci err_t err; 1143195972f6Sopenharmony_ci struct dns_table_entry *entry = &dns_table[i]; 1144195972f6Sopenharmony_ci 1145195972f6Sopenharmony_ci LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE); 1146195972f6Sopenharmony_ci 1147195972f6Sopenharmony_ci switch (entry->state) { 1148195972f6Sopenharmony_ci case DNS_STATE_NEW: 1149195972f6Sopenharmony_ci /* initialize new entry */ 1150195972f6Sopenharmony_ci entry->txid = dns_create_txid(); 1151195972f6Sopenharmony_ci entry->state = DNS_STATE_ASKING; 1152195972f6Sopenharmony_ci entry->server_idx = 0; 1153195972f6Sopenharmony_ci entry->tmr = 1; 1154195972f6Sopenharmony_ci entry->retries = 0; 1155195972f6Sopenharmony_ci 1156195972f6Sopenharmony_ci /* send DNS packet for this entry */ 1157195972f6Sopenharmony_ci err = dns_send(i); 1158195972f6Sopenharmony_ci if (err != ERR_OK) { 1159195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, 1160195972f6Sopenharmony_ci ("dns_send returned error: %s\n", lwip_strerr(err))); 1161195972f6Sopenharmony_ci } 1162195972f6Sopenharmony_ci break; 1163195972f6Sopenharmony_ci case DNS_STATE_ASKING: 1164195972f6Sopenharmony_ci if (--entry->tmr == 0) { 1165195972f6Sopenharmony_ci if (++entry->retries == DNS_MAX_RETRIES) { 1166195972f6Sopenharmony_ci if (dns_backupserver_available(entry) 1167195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 1168195972f6Sopenharmony_ci && !entry->is_mdns 1169195972f6Sopenharmony_ci#endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 1170195972f6Sopenharmony_ci ) { 1171195972f6Sopenharmony_ci /* change of server */ 1172195972f6Sopenharmony_ci entry->server_idx++; 1173195972f6Sopenharmony_ci entry->tmr = 1; 1174195972f6Sopenharmony_ci entry->retries = 0; 1175195972f6Sopenharmony_ci } else { 1176195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", entry->name)); 1177195972f6Sopenharmony_ci /* call specified callback function if provided */ 1178195972f6Sopenharmony_ci dns_call_found(i, NULL); 1179195972f6Sopenharmony_ci /* flush this entry */ 1180195972f6Sopenharmony_ci entry->state = DNS_STATE_UNUSED; 1181195972f6Sopenharmony_ci break; 1182195972f6Sopenharmony_ci } 1183195972f6Sopenharmony_ci } else { 1184195972f6Sopenharmony_ci /* wait longer for the next retry */ 1185195972f6Sopenharmony_ci entry->tmr = entry->retries; 1186195972f6Sopenharmony_ci } 1187195972f6Sopenharmony_ci 1188195972f6Sopenharmony_ci /* send DNS packet for this entry */ 1189195972f6Sopenharmony_ci err = dns_send(i); 1190195972f6Sopenharmony_ci if (err != ERR_OK) { 1191195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING, 1192195972f6Sopenharmony_ci ("dns_send returned error: %s\n", lwip_strerr(err))); 1193195972f6Sopenharmony_ci } 1194195972f6Sopenharmony_ci } 1195195972f6Sopenharmony_ci break; 1196195972f6Sopenharmony_ci case DNS_STATE_DONE: 1197195972f6Sopenharmony_ci /* if the time to live is nul */ 1198195972f6Sopenharmony_ci if ((entry->ttl == 0) || (--entry->ttl == 0)) { 1199195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", entry->name)); 1200195972f6Sopenharmony_ci /* flush this entry, there cannot be any related pending entries in this state */ 1201195972f6Sopenharmony_ci entry->state = DNS_STATE_UNUSED; 1202195972f6Sopenharmony_ci } 1203195972f6Sopenharmony_ci break; 1204195972f6Sopenharmony_ci case DNS_STATE_UNUSED: 1205195972f6Sopenharmony_ci /* nothing to do */ 1206195972f6Sopenharmony_ci break; 1207195972f6Sopenharmony_ci default: 1208195972f6Sopenharmony_ci LWIP_ASSERT("unknown dns_table entry state:", 0); 1209195972f6Sopenharmony_ci break; 1210195972f6Sopenharmony_ci } 1211195972f6Sopenharmony_ci} 1212195972f6Sopenharmony_ci 1213195972f6Sopenharmony_ci/** 1214195972f6Sopenharmony_ci * Call dns_check_entry for each entry in dns_table - check all entries. 1215195972f6Sopenharmony_ci */ 1216195972f6Sopenharmony_cistatic void 1217195972f6Sopenharmony_cidns_check_entries(void) 1218195972f6Sopenharmony_ci{ 1219195972f6Sopenharmony_ci u8_t i; 1220195972f6Sopenharmony_ci 1221195972f6Sopenharmony_ci for (i = 0; i < DNS_TABLE_SIZE; ++i) { 1222195972f6Sopenharmony_ci dns_check_entry(i); 1223195972f6Sopenharmony_ci } 1224195972f6Sopenharmony_ci} 1225195972f6Sopenharmony_ci 1226195972f6Sopenharmony_ci/** 1227195972f6Sopenharmony_ci * Save TTL and call dns_call_found for correct response. 1228195972f6Sopenharmony_ci */ 1229195972f6Sopenharmony_cistatic void 1230195972f6Sopenharmony_cidns_correct_response(u8_t idx, u32_t ttl) 1231195972f6Sopenharmony_ci{ 1232195972f6Sopenharmony_ci struct dns_table_entry *entry = &dns_table[idx]; 1233195972f6Sopenharmony_ci 1234195972f6Sopenharmony_ci entry->state = DNS_STATE_DONE; 1235195972f6Sopenharmony_ci 1236195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", entry->name)); 1237195972f6Sopenharmony_ci ip_addr_debug_print_val(DNS_DEBUG, entry->ipaddr); 1238195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("\n")); 1239195972f6Sopenharmony_ci 1240195972f6Sopenharmony_ci /* read the answer resource record's TTL, and maximize it if needed */ 1241195972f6Sopenharmony_ci entry->ttl = ttl; 1242195972f6Sopenharmony_ci if (entry->ttl > DNS_MAX_TTL) { 1243195972f6Sopenharmony_ci entry->ttl = DNS_MAX_TTL; 1244195972f6Sopenharmony_ci } 1245195972f6Sopenharmony_ci dns_call_found(idx, &entry->ipaddr); 1246195972f6Sopenharmony_ci 1247195972f6Sopenharmony_ci if (entry->ttl == 0) { 1248195972f6Sopenharmony_ci /* RFC 883, page 29: "Zero values are 1249195972f6Sopenharmony_ci interpreted to mean that the RR can only be used for the 1250195972f6Sopenharmony_ci transaction in progress, and should not be cached." 1251195972f6Sopenharmony_ci -> flush this entry now */ 1252195972f6Sopenharmony_ci /* entry reused during callback? */ 1253195972f6Sopenharmony_ci if (entry->state == DNS_STATE_DONE) { 1254195972f6Sopenharmony_ci entry->state = DNS_STATE_UNUSED; 1255195972f6Sopenharmony_ci } 1256195972f6Sopenharmony_ci } 1257195972f6Sopenharmony_ci} 1258195972f6Sopenharmony_ci 1259195972f6Sopenharmony_ci/** 1260195972f6Sopenharmony_ci * Receive input function for DNS response packets arriving for the dns UDP pcb. 1261195972f6Sopenharmony_ci */ 1262195972f6Sopenharmony_cistatic void 1263195972f6Sopenharmony_cidns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) 1264195972f6Sopenharmony_ci{ 1265195972f6Sopenharmony_ci u8_t i; 1266195972f6Sopenharmony_ci u16_t txid; 1267195972f6Sopenharmony_ci u16_t res_idx; 1268195972f6Sopenharmony_ci struct dns_hdr hdr; 1269195972f6Sopenharmony_ci struct dns_answer ans; 1270195972f6Sopenharmony_ci struct dns_query qry; 1271195972f6Sopenharmony_ci u16_t nquestions, nanswers; 1272195972f6Sopenharmony_ci 1273195972f6Sopenharmony_ci LWIP_UNUSED_ARG(arg); 1274195972f6Sopenharmony_ci LWIP_UNUSED_ARG(pcb); 1275195972f6Sopenharmony_ci LWIP_UNUSED_ARG(port); 1276195972f6Sopenharmony_ci 1277195972f6Sopenharmony_ci /* is the dns message big enough ? */ 1278195972f6Sopenharmony_ci if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) { 1279195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n")); 1280195972f6Sopenharmony_ci /* free pbuf and return */ 1281195972f6Sopenharmony_ci goto ignore_packet; 1282195972f6Sopenharmony_ci } 1283195972f6Sopenharmony_ci 1284195972f6Sopenharmony_ci /* copy dns payload inside static buffer for processing */ 1285195972f6Sopenharmony_ci if (pbuf_copy_partial(p, &hdr, SIZEOF_DNS_HDR, 0) == SIZEOF_DNS_HDR) { 1286195972f6Sopenharmony_ci /* Match the ID in the DNS header with the name table. */ 1287195972f6Sopenharmony_ci txid = lwip_htons(hdr.id); 1288195972f6Sopenharmony_ci for (i = 0; i < DNS_TABLE_SIZE; i++) { 1289195972f6Sopenharmony_ci struct dns_table_entry *entry = &dns_table[i]; 1290195972f6Sopenharmony_ci if ((entry->state == DNS_STATE_ASKING) && 1291195972f6Sopenharmony_ci (entry->txid == txid)) { 1292195972f6Sopenharmony_ci 1293195972f6Sopenharmony_ci /* We only care about the question(s) and the answers. The authrr 1294195972f6Sopenharmony_ci and the extrarr are simply discarded. */ 1295195972f6Sopenharmony_ci nquestions = lwip_htons(hdr.numquestions); 1296195972f6Sopenharmony_ci nanswers = lwip_htons(hdr.numanswers); 1297195972f6Sopenharmony_ci 1298195972f6Sopenharmony_ci /* Check for correct response. */ 1299195972f6Sopenharmony_ci if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) { 1300195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": not a response\n", entry->name)); 1301195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1302195972f6Sopenharmony_ci } 1303195972f6Sopenharmony_ci if (nquestions != 1) { 1304195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); 1305195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1306195972f6Sopenharmony_ci } 1307195972f6Sopenharmony_ci 1308195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 1309195972f6Sopenharmony_ci if (!entry->is_mdns) 1310195972f6Sopenharmony_ci#endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 1311195972f6Sopenharmony_ci { 1312195972f6Sopenharmony_ci /* Check whether response comes from the same network address to which the 1313195972f6Sopenharmony_ci question was sent. (RFC 5452) */ 1314195972f6Sopenharmony_ci#if LWIP_ENABLE_DISTRIBUTED_NET && !LWIP_USE_GET_HOST_BY_NAME_EXTERNAL 1315195972f6Sopenharmony_ci if (is_distributed_net_enabled()) { 1316195972f6Sopenharmony_ci#if LWIP_IPV6 1317195972f6Sopenharmony_ci if (addr->type != IPADDR_TYPE_V4 || addr->u_addr.ip4.addr != ipaddr_addr(LOCAL_SERVER_IP) || 1318195972f6Sopenharmony_ci port != get_local_udp_server_port()) { 1319195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1320195972f6Sopenharmony_ci } 1321195972f6Sopenharmony_ci#else 1322195972f6Sopenharmony_ci if (addr->addr != ipaddr_addr(LOCAL_SERVER_IP) || port != get_local_udp_server_port()) { 1323195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1324195972f6Sopenharmony_ci } 1325195972f6Sopenharmony_ci#endif 1326195972f6Sopenharmony_ci } else { 1327195972f6Sopenharmony_ci if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) { 1328195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1329195972f6Sopenharmony_ci } 1330195972f6Sopenharmony_ci } 1331195972f6Sopenharmony_ci#else 1332195972f6Sopenharmony_ci if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) { 1333195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1334195972f6Sopenharmony_ci } 1335195972f6Sopenharmony_ci#endif 1336195972f6Sopenharmony_ci } 1337195972f6Sopenharmony_ci 1338195972f6Sopenharmony_ci /* Check if the name in the "question" part match with the name in the entry and 1339195972f6Sopenharmony_ci skip it if equal. */ 1340195972f6Sopenharmony_ci res_idx = dns_compare_name(entry->name, p, SIZEOF_DNS_HDR); 1341195972f6Sopenharmony_ci if (res_idx == 0xFFFF) { 1342195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); 1343195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1344195972f6Sopenharmony_ci } 1345195972f6Sopenharmony_ci 1346195972f6Sopenharmony_ci /* check if "question" part matches the request */ 1347195972f6Sopenharmony_ci if (pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) { 1348195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1349195972f6Sopenharmony_ci } 1350195972f6Sopenharmony_ci if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) || 1351195972f6Sopenharmony_ci (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) || 1352195972f6Sopenharmony_ci (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) { 1353195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name)); 1354195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1355195972f6Sopenharmony_ci } 1356195972f6Sopenharmony_ci /* skip the rest of the "question" part */ 1357195972f6Sopenharmony_ci if (res_idx + SIZEOF_DNS_QUERY > 0xFFFF) { 1358195972f6Sopenharmony_ci goto ignore_packet; 1359195972f6Sopenharmony_ci } 1360195972f6Sopenharmony_ci res_idx = (u16_t)(res_idx + SIZEOF_DNS_QUERY); 1361195972f6Sopenharmony_ci 1362195972f6Sopenharmony_ci /* Check for error. If so, call callback to inform. */ 1363195972f6Sopenharmony_ci if (hdr.flags2 & DNS_FLAG2_ERR_MASK) { 1364195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", entry->name)); 1365195972f6Sopenharmony_ci 1366195972f6Sopenharmony_ci /* if there is another backup DNS server to try 1367195972f6Sopenharmony_ci * then don't stop the DNS request 1368195972f6Sopenharmony_ci */ 1369195972f6Sopenharmony_ci if (dns_backupserver_available(entry)) { 1370195972f6Sopenharmony_ci /* avoid retrying the same server */ 1371195972f6Sopenharmony_ci entry->retries = DNS_MAX_RETRIES-1; 1372195972f6Sopenharmony_ci entry->tmr = 1; 1373195972f6Sopenharmony_ci 1374195972f6Sopenharmony_ci /* contact next available server for this entry */ 1375195972f6Sopenharmony_ci dns_check_entry(i); 1376195972f6Sopenharmony_ci 1377195972f6Sopenharmony_ci goto ignore_packet; 1378195972f6Sopenharmony_ci } 1379195972f6Sopenharmony_ci } else { 1380195972f6Sopenharmony_ci while ((nanswers > 0) && (res_idx < p->tot_len)) { 1381195972f6Sopenharmony_ci /* skip answer resource record's host name */ 1382195972f6Sopenharmony_ci res_idx = dns_skip_name(p, res_idx); 1383195972f6Sopenharmony_ci if (res_idx == 0xFFFF) { 1384195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1385195972f6Sopenharmony_ci } 1386195972f6Sopenharmony_ci 1387195972f6Sopenharmony_ci /* Check for IP address type and Internet class. Others are discarded. */ 1388195972f6Sopenharmony_ci if (pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) { 1389195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1390195972f6Sopenharmony_ci } 1391195972f6Sopenharmony_ci if (res_idx + SIZEOF_DNS_ANSWER > 0xFFFF) { 1392195972f6Sopenharmony_ci goto ignore_packet; 1393195972f6Sopenharmony_ci } 1394195972f6Sopenharmony_ci res_idx = (u16_t)(res_idx + SIZEOF_DNS_ANSWER); 1395195972f6Sopenharmony_ci 1396195972f6Sopenharmony_ci if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) { 1397195972f6Sopenharmony_ci#if LWIP_IPV4 1398195972f6Sopenharmony_ci if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(sizeof(ip4_addr_t)))) { 1399195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 1400195972f6Sopenharmony_ci if (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) 1401195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 1402195972f6Sopenharmony_ci { 1403195972f6Sopenharmony_ci ip4_addr_t ip4addr; 1404195972f6Sopenharmony_ci /* read the IP address after answer resource record's header */ 1405195972f6Sopenharmony_ci if (pbuf_copy_partial(p, &ip4addr, sizeof(ip4_addr_t), res_idx) != sizeof(ip4_addr_t)) { 1406195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1407195972f6Sopenharmony_ci } 1408195972f6Sopenharmony_ci ip_addr_copy_from_ip4(dns_table[i].ipaddr, ip4addr); 1409195972f6Sopenharmony_ci pbuf_free(p); 1410195972f6Sopenharmony_ci /* handle correct response */ 1411195972f6Sopenharmony_ci dns_correct_response(i, lwip_ntohl(ans.ttl)); 1412195972f6Sopenharmony_ci return; 1413195972f6Sopenharmony_ci } 1414195972f6Sopenharmony_ci } 1415195972f6Sopenharmony_ci#endif /* LWIP_IPV4 */ 1416195972f6Sopenharmony_ci#if LWIP_IPV6 1417195972f6Sopenharmony_ci if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(sizeof(ip6_addr_p_t)))) { 1418195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 1419195972f6Sopenharmony_ci if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) 1420195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 1421195972f6Sopenharmony_ci { 1422195972f6Sopenharmony_ci ip6_addr_p_t ip6addr; 1423195972f6Sopenharmony_ci /* read the IP address after answer resource record's header */ 1424195972f6Sopenharmony_ci if (pbuf_copy_partial(p, &ip6addr, sizeof(ip6_addr_p_t), res_idx) != sizeof(ip6_addr_p_t)) { 1425195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1426195972f6Sopenharmony_ci } 1427195972f6Sopenharmony_ci /* @todo: scope ip6addr? Might be required for link-local addresses at least? */ 1428195972f6Sopenharmony_ci ip_addr_copy_from_ip6_packed(dns_table[i].ipaddr, ip6addr); 1429195972f6Sopenharmony_ci pbuf_free(p); 1430195972f6Sopenharmony_ci /* handle correct response */ 1431195972f6Sopenharmony_ci dns_correct_response(i, lwip_ntohl(ans.ttl)); 1432195972f6Sopenharmony_ci return; 1433195972f6Sopenharmony_ci } 1434195972f6Sopenharmony_ci } 1435195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */ 1436195972f6Sopenharmony_ci } 1437195972f6Sopenharmony_ci /* skip this answer */ 1438195972f6Sopenharmony_ci if ((int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) { 1439195972f6Sopenharmony_ci goto ignore_packet; /* ignore this packet */ 1440195972f6Sopenharmony_ci } 1441195972f6Sopenharmony_ci res_idx = (u16_t)(res_idx + lwip_htons(ans.len)); 1442195972f6Sopenharmony_ci --nanswers; 1443195972f6Sopenharmony_ci } 1444195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 1445195972f6Sopenharmony_ci if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || 1446195972f6Sopenharmony_ci (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) { 1447195972f6Sopenharmony_ci if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) { 1448195972f6Sopenharmony_ci /* IPv4 failed, try IPv6 */ 1449195972f6Sopenharmony_ci dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6; 1450195972f6Sopenharmony_ci } else { 1451195972f6Sopenharmony_ci /* IPv6 failed, try IPv4 */ 1452195972f6Sopenharmony_ci dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4; 1453195972f6Sopenharmony_ci } 1454195972f6Sopenharmony_ci pbuf_free(p); 1455195972f6Sopenharmony_ci dns_table[i].state = DNS_STATE_NEW; 1456195972f6Sopenharmony_ci dns_check_entry(i); 1457195972f6Sopenharmony_ci return; 1458195972f6Sopenharmony_ci } 1459195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 1460195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", entry->name)); 1461195972f6Sopenharmony_ci } 1462195972f6Sopenharmony_ci /* call callback to indicate error, clean up memory and return */ 1463195972f6Sopenharmony_ci pbuf_free(p); 1464195972f6Sopenharmony_ci dns_call_found(i, NULL); 1465195972f6Sopenharmony_ci dns_table[i].state = DNS_STATE_UNUSED; 1466195972f6Sopenharmony_ci return; 1467195972f6Sopenharmony_ci } 1468195972f6Sopenharmony_ci } 1469195972f6Sopenharmony_ci } 1470195972f6Sopenharmony_ci 1471195972f6Sopenharmony_ciignore_packet: 1472195972f6Sopenharmony_ci /* deallocate memory and return */ 1473195972f6Sopenharmony_ci pbuf_free(p); 1474195972f6Sopenharmony_ci return; 1475195972f6Sopenharmony_ci} 1476195972f6Sopenharmony_ci 1477195972f6Sopenharmony_ci/** 1478195972f6Sopenharmony_ci * Queues a new hostname to resolve and sends out a DNS query for that hostname 1479195972f6Sopenharmony_ci * 1480195972f6Sopenharmony_ci * @param name the hostname that is to be queried 1481195972f6Sopenharmony_ci * @param hostnamelen length of the hostname 1482195972f6Sopenharmony_ci * @param found a callback function to be called on success, failure or timeout 1483195972f6Sopenharmony_ci * @param callback_arg argument to pass to the callback function 1484195972f6Sopenharmony_ci * @return err_t return code. 1485195972f6Sopenharmony_ci */ 1486195972f6Sopenharmony_cistatic err_t 1487195972f6Sopenharmony_cidns_enqueue(const char *name, size_t hostnamelen, dns_found_callback found, 1488195972f6Sopenharmony_ci void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns)) 1489195972f6Sopenharmony_ci{ 1490195972f6Sopenharmony_ci u8_t i; 1491195972f6Sopenharmony_ci u8_t lseq, lseqi; 1492195972f6Sopenharmony_ci struct dns_table_entry *entry = NULL; 1493195972f6Sopenharmony_ci size_t namelen; 1494195972f6Sopenharmony_ci struct dns_req_entry *req; 1495195972f6Sopenharmony_ci 1496195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 1497195972f6Sopenharmony_ci u8_t r; 1498195972f6Sopenharmony_ci /* check for duplicate entries */ 1499195972f6Sopenharmony_ci for (i = 0; i < DNS_TABLE_SIZE; i++) { 1500195972f6Sopenharmony_ci if ((dns_table[i].state == DNS_STATE_ASKING) && 1501195972f6Sopenharmony_ci (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0)) { 1502195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 1503195972f6Sopenharmony_ci if (dns_table[i].reqaddrtype != dns_addrtype) { 1504195972f6Sopenharmony_ci /* requested address types don't match 1505195972f6Sopenharmony_ci this can lead to 2 concurrent requests, but mixing the address types 1506195972f6Sopenharmony_ci for the same host should not be that common */ 1507195972f6Sopenharmony_ci continue; 1508195972f6Sopenharmony_ci } 1509195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 1510195972f6Sopenharmony_ci /* this is a duplicate entry, find a free request entry */ 1511195972f6Sopenharmony_ci for (r = 0; r < DNS_MAX_REQUESTS; r++) { 1512195972f6Sopenharmony_ci if (dns_requests[r].found == 0) { 1513195972f6Sopenharmony_ci dns_requests[r].found = found; 1514195972f6Sopenharmony_ci dns_requests[r].arg = callback_arg; 1515195972f6Sopenharmony_ci dns_requests[r].dns_table_idx = i; 1516195972f6Sopenharmony_ci LWIP_DNS_SET_ADDRTYPE(dns_requests[r].reqaddrtype, dns_addrtype); 1517195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": duplicate request\n", name)); 1518195972f6Sopenharmony_ci return ERR_INPROGRESS; 1519195972f6Sopenharmony_ci } 1520195972f6Sopenharmony_ci } 1521195972f6Sopenharmony_ci } 1522195972f6Sopenharmony_ci } 1523195972f6Sopenharmony_ci /* no duplicate entries found */ 1524195972f6Sopenharmony_ci#endif 1525195972f6Sopenharmony_ci 1526195972f6Sopenharmony_ci /* search an unused entry, or the oldest one */ 1527195972f6Sopenharmony_ci lseq = 0; 1528195972f6Sopenharmony_ci lseqi = DNS_TABLE_SIZE; 1529195972f6Sopenharmony_ci for (i = 0; i < DNS_TABLE_SIZE; ++i) { 1530195972f6Sopenharmony_ci entry = &dns_table[i]; 1531195972f6Sopenharmony_ci /* is it an unused entry ? */ 1532195972f6Sopenharmony_ci if (entry->state == DNS_STATE_UNUSED) { 1533195972f6Sopenharmony_ci break; 1534195972f6Sopenharmony_ci } 1535195972f6Sopenharmony_ci /* check if this is the oldest completed entry */ 1536195972f6Sopenharmony_ci if (entry->state == DNS_STATE_DONE) { 1537195972f6Sopenharmony_ci u8_t age = (u8_t)(dns_seqno - entry->seqno); 1538195972f6Sopenharmony_ci if (age > lseq) { 1539195972f6Sopenharmony_ci lseq = age; 1540195972f6Sopenharmony_ci lseqi = i; 1541195972f6Sopenharmony_ci } 1542195972f6Sopenharmony_ci } 1543195972f6Sopenharmony_ci } 1544195972f6Sopenharmony_ci 1545195972f6Sopenharmony_ci /* if we don't have found an unused entry, use the oldest completed one */ 1546195972f6Sopenharmony_ci if (i == DNS_TABLE_SIZE) { 1547195972f6Sopenharmony_ci if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) { 1548195972f6Sopenharmony_ci /* no entry can be used now, table is full */ 1549195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name)); 1550195972f6Sopenharmony_ci return ERR_MEM; 1551195972f6Sopenharmony_ci } else { 1552195972f6Sopenharmony_ci /* use the oldest completed one */ 1553195972f6Sopenharmony_ci i = lseqi; 1554195972f6Sopenharmony_ci entry = &dns_table[i]; 1555195972f6Sopenharmony_ci } 1556195972f6Sopenharmony_ci } 1557195972f6Sopenharmony_ci 1558195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 1559195972f6Sopenharmony_ci /* find a free request entry */ 1560195972f6Sopenharmony_ci req = NULL; 1561195972f6Sopenharmony_ci for (r = 0; r < DNS_MAX_REQUESTS; r++) { 1562195972f6Sopenharmony_ci if (dns_requests[r].found == NULL) { 1563195972f6Sopenharmony_ci req = &dns_requests[r]; 1564195972f6Sopenharmony_ci break; 1565195972f6Sopenharmony_ci } 1566195972f6Sopenharmony_ci } 1567195972f6Sopenharmony_ci if (req == NULL) { 1568195972f6Sopenharmony_ci /* no request entry can be used now, table is full */ 1569195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS request entries table is full\n", name)); 1570195972f6Sopenharmony_ci return ERR_MEM; 1571195972f6Sopenharmony_ci } 1572195972f6Sopenharmony_ci req->dns_table_idx = i; 1573195972f6Sopenharmony_ci#else 1574195972f6Sopenharmony_ci /* in this configuration, the entry index is the same as the request index */ 1575195972f6Sopenharmony_ci req = &dns_requests[i]; 1576195972f6Sopenharmony_ci#endif 1577195972f6Sopenharmony_ci 1578195972f6Sopenharmony_ci /* use this entry */ 1579195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i))); 1580195972f6Sopenharmony_ci 1581195972f6Sopenharmony_ci /* fill the entry */ 1582195972f6Sopenharmony_ci entry->state = DNS_STATE_NEW; 1583195972f6Sopenharmony_ci entry->seqno = dns_seqno; 1584195972f6Sopenharmony_ci LWIP_DNS_SET_ADDRTYPE(entry->reqaddrtype, dns_addrtype); 1585195972f6Sopenharmony_ci LWIP_DNS_SET_ADDRTYPE(req->reqaddrtype, dns_addrtype); 1586195972f6Sopenharmony_ci req->found = found; 1587195972f6Sopenharmony_ci req->arg = callback_arg; 1588195972f6Sopenharmony_ci namelen = LWIP_MIN(hostnamelen, DNS_MAX_NAME_LENGTH - 1); 1589195972f6Sopenharmony_ci MEMCPY(entry->name, name, namelen); 1590195972f6Sopenharmony_ci entry->name[namelen] = 0; 1591195972f6Sopenharmony_ci 1592195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 1593195972f6Sopenharmony_ci entry->pcb_idx = dns_alloc_pcb(); 1594195972f6Sopenharmony_ci if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) { 1595195972f6Sopenharmony_ci /* failed to get a UDP pcb */ 1596195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": failed to allocate a pcb\n", name)); 1597195972f6Sopenharmony_ci entry->state = DNS_STATE_UNUSED; 1598195972f6Sopenharmony_ci req->found = NULL; 1599195972f6Sopenharmony_ci return ERR_MEM; 1600195972f6Sopenharmony_ci } 1601195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS pcb %"U16_F"\n", name, (u16_t)(entry->pcb_idx))); 1602195972f6Sopenharmony_ci#endif 1603195972f6Sopenharmony_ci 1604195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 1605195972f6Sopenharmony_ci entry->is_mdns = is_mdns; 1606195972f6Sopenharmony_ci#endif 1607195972f6Sopenharmony_ci 1608195972f6Sopenharmony_ci dns_seqno++; 1609195972f6Sopenharmony_ci 1610195972f6Sopenharmony_ci /* force to send query without waiting timer */ 1611195972f6Sopenharmony_ci dns_check_entry(i); 1612195972f6Sopenharmony_ci 1613195972f6Sopenharmony_ci /* dns query is enqueued */ 1614195972f6Sopenharmony_ci return ERR_INPROGRESS; 1615195972f6Sopenharmony_ci} 1616195972f6Sopenharmony_ci 1617195972f6Sopenharmony_ci/** 1618195972f6Sopenharmony_ci * @ingroup dns 1619195972f6Sopenharmony_ci * Resolve a hostname (string) into an IP address. 1620195972f6Sopenharmony_ci * NON-BLOCKING callback version for use with raw API!!! 1621195972f6Sopenharmony_ci * 1622195972f6Sopenharmony_ci * Returns immediately with one of err_t return codes: 1623195972f6Sopenharmony_ci * - ERR_OK if hostname is a valid IP address string or the host 1624195972f6Sopenharmony_ci * name is already in the local names table. 1625195972f6Sopenharmony_ci * - ERR_INPROGRESS enqueue a request to be sent to the DNS server 1626195972f6Sopenharmony_ci * for resolution if no errors are present. 1627195972f6Sopenharmony_ci * - ERR_ARG: dns client not initialized or invalid hostname 1628195972f6Sopenharmony_ci * 1629195972f6Sopenharmony_ci * @param hostname the hostname that is to be queried 1630195972f6Sopenharmony_ci * @param addr pointer to a ip_addr_t where to store the address if it is already 1631195972f6Sopenharmony_ci * cached in the dns_table (only valid if ERR_OK is returned!) 1632195972f6Sopenharmony_ci * @param found a callback function to be called on success, failure or timeout (only if 1633195972f6Sopenharmony_ci * ERR_INPROGRESS is returned!) 1634195972f6Sopenharmony_ci * @param callback_arg argument to pass to the callback function 1635195972f6Sopenharmony_ci * @return a err_t return code. 1636195972f6Sopenharmony_ci */ 1637195972f6Sopenharmony_cierr_t 1638195972f6Sopenharmony_cidns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, 1639195972f6Sopenharmony_ci void *callback_arg) 1640195972f6Sopenharmony_ci{ 1641195972f6Sopenharmony_ci return dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, LWIP_DNS_ADDRTYPE_DEFAULT); 1642195972f6Sopenharmony_ci} 1643195972f6Sopenharmony_ci 1644195972f6Sopenharmony_ci/** 1645195972f6Sopenharmony_ci * @ingroup dns 1646195972f6Sopenharmony_ci * Like dns_gethostbyname, but returned address type can be controlled: 1647195972f6Sopenharmony_ci * @param hostname the hostname that is to be queried 1648195972f6Sopenharmony_ci * @param addr pointer to a ip_addr_t where to store the address if it is already 1649195972f6Sopenharmony_ci * cached in the dns_table (only valid if ERR_OK is returned!) 1650195972f6Sopenharmony_ci * @param found a callback function to be called on success, failure or timeout (only if 1651195972f6Sopenharmony_ci * ERR_INPROGRESS is returned!) 1652195972f6Sopenharmony_ci * @param callback_arg argument to pass to the callback function 1653195972f6Sopenharmony_ci * @param dns_addrtype - LWIP_DNS_ADDRTYPE_IPV4_IPV6: try to resolve IPv4 first, try IPv6 if IPv4 fails only 1654195972f6Sopenharmony_ci * - LWIP_DNS_ADDRTYPE_IPV6_IPV4: try to resolve IPv6 first, try IPv4 if IPv6 fails only 1655195972f6Sopenharmony_ci * - LWIP_DNS_ADDRTYPE_IPV4: try to resolve IPv4 only 1656195972f6Sopenharmony_ci * - LWIP_DNS_ADDRTYPE_IPV6: try to resolve IPv6 only 1657195972f6Sopenharmony_ci */ 1658195972f6Sopenharmony_cierr_t 1659195972f6Sopenharmony_cidns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found, 1660195972f6Sopenharmony_ci void *callback_arg, u8_t dns_addrtype) 1661195972f6Sopenharmony_ci{ 1662195972f6Sopenharmony_ci size_t hostnamelen; 1663195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 1664195972f6Sopenharmony_ci u8_t is_mdns; 1665195972f6Sopenharmony_ci#endif 1666195972f6Sopenharmony_ci /* not initialized or no valid server yet, or invalid addr pointer 1667195972f6Sopenharmony_ci * or invalid hostname or invalid hostname length */ 1668195972f6Sopenharmony_ci if ((addr == NULL) || 1669195972f6Sopenharmony_ci (!hostname) || (!hostname[0])) { 1670195972f6Sopenharmony_ci return ERR_ARG; 1671195972f6Sopenharmony_ci } 1672195972f6Sopenharmony_ci#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) 1673195972f6Sopenharmony_ci if (dns_pcbs[0] == NULL) { 1674195972f6Sopenharmony_ci return ERR_ARG; 1675195972f6Sopenharmony_ci } 1676195972f6Sopenharmony_ci#endif 1677195972f6Sopenharmony_ci hostnamelen = strlen(hostname); 1678195972f6Sopenharmony_ci if (hostnamelen >= DNS_MAX_NAME_LENGTH) { 1679195972f6Sopenharmony_ci LWIP_DEBUGF(DNS_DEBUG, ("dns_gethostbyname: name too long to resolve")); 1680195972f6Sopenharmony_ci return ERR_ARG; 1681195972f6Sopenharmony_ci } 1682195972f6Sopenharmony_ci 1683195972f6Sopenharmony_ci 1684195972f6Sopenharmony_ci#if LWIP_HAVE_LOOPIF 1685195972f6Sopenharmony_ci if (strcmp(hostname, "localhost") == 0) { 1686195972f6Sopenharmony_ci ip_addr_set_loopback(LWIP_DNS_ADDRTYPE_IS_IPV6(dns_addrtype), addr); 1687195972f6Sopenharmony_ci return ERR_OK; 1688195972f6Sopenharmony_ci } 1689195972f6Sopenharmony_ci#endif /* LWIP_HAVE_LOOPIF */ 1690195972f6Sopenharmony_ci 1691195972f6Sopenharmony_ci /* host name already in octet notation? set ip addr and return ERR_OK */ 1692195972f6Sopenharmony_ci if (ipaddr_aton(hostname, addr)) { 1693195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 1694195972f6Sopenharmony_ci if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) || 1695195972f6Sopenharmony_ci (IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6))) 1696195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 1697195972f6Sopenharmony_ci { 1698195972f6Sopenharmony_ci return ERR_OK; 1699195972f6Sopenharmony_ci } 1700195972f6Sopenharmony_ci } 1701195972f6Sopenharmony_ci /* already have this address cached? */ 1702195972f6Sopenharmony_ci if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) { 1703195972f6Sopenharmony_ci return ERR_OK; 1704195972f6Sopenharmony_ci } 1705195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_IPV6 1706195972f6Sopenharmony_ci if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) { 1707195972f6Sopenharmony_ci /* fallback to 2nd IP type and try again to lookup */ 1708195972f6Sopenharmony_ci u8_t fallback; 1709195972f6Sopenharmony_ci if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) { 1710195972f6Sopenharmony_ci fallback = LWIP_DNS_ADDRTYPE_IPV6; 1711195972f6Sopenharmony_ci } else { 1712195972f6Sopenharmony_ci fallback = LWIP_DNS_ADDRTYPE_IPV4; 1713195972f6Sopenharmony_ci } 1714195972f6Sopenharmony_ci if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) == ERR_OK) { 1715195972f6Sopenharmony_ci return ERR_OK; 1716195972f6Sopenharmony_ci } 1717195972f6Sopenharmony_ci } 1718195972f6Sopenharmony_ci#else /* LWIP_IPV4 && LWIP_IPV6 */ 1719195972f6Sopenharmony_ci LWIP_UNUSED_ARG(dns_addrtype); 1720195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_IPV6 */ 1721195972f6Sopenharmony_ci 1722195972f6Sopenharmony_ci#if LWIP_DNS_SUPPORT_MDNS_QUERIES 1723195972f6Sopenharmony_ci if (strstr(hostname, ".local") == &hostname[hostnamelen] - 6) { 1724195972f6Sopenharmony_ci is_mdns = 1; 1725195972f6Sopenharmony_ci } else { 1726195972f6Sopenharmony_ci is_mdns = 0; 1727195972f6Sopenharmony_ci } 1728195972f6Sopenharmony_ci 1729195972f6Sopenharmony_ci if (!is_mdns) 1730195972f6Sopenharmony_ci#endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */ 1731195972f6Sopenharmony_ci { 1732195972f6Sopenharmony_ci /* prevent calling found callback if no server is set, return error instead */ 1733195972f6Sopenharmony_ci if (ip_addr_isany_val(dns_servers[0])) { 1734195972f6Sopenharmony_ci return ERR_VAL; 1735195972f6Sopenharmony_ci } 1736195972f6Sopenharmony_ci } 1737195972f6Sopenharmony_ci 1738195972f6Sopenharmony_ci /* queue query with specified callback */ 1739195972f6Sopenharmony_ci return dns_enqueue(hostname, hostnamelen, found, callback_arg LWIP_DNS_ADDRTYPE_ARG(dns_addrtype) 1740195972f6Sopenharmony_ci LWIP_DNS_ISMDNS_ARG(is_mdns)); 1741195972f6Sopenharmony_ci} 1742195972f6Sopenharmony_ci 1743195972f6Sopenharmony_ci#endif /* LWIP_DNS */ 1744