3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2013-2017 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file defines DNSResolver, the object that abstracts
22 * Domain Name System (DNS) resolution in InetLayer.
28 #include <inet/IPAddress.h>
29 #include <inet/InetError.h>
30 #include <inet/InetLayerBasis.h>
32 #define NL_DNS_HOSTNAME_MAX_LEN (253)
42 * Options controlling how IP address resolution is performed.
46 kDNSOption_AddrFamily_Mask = 0x07, ///< Bits within a DNSOptions integer value representing the desired address family.
47 kDNSOption_Flags_Mask = 0xF8, ///< Bits within a DNSOptions integer value reserved for flags.
49 // Address Family Choices
50 kDNSOption_AddrFamily_Any = 0x00, ///< Return IPv4 and/or IPv6 addresses in the order returned by the nameserver.
51 #if INET_CONFIG_ENABLE_IPV4
52 kDNSOption_AddrFamily_IPv4Only = 0x01, ///< Return only IPv4 addresses.
53 kDNSOption_AddrFamily_IPv4Preferred = 0x02, ///< Return IPv4 and/or IPv6 addresses, with IPv4 addresses listed first.
55 kDNSOption_AddrFamily_IPv6Only = 0x03, ///< Return only IPv6 addresses.
56 kDNSOption_AddrFamily_IPv6Preferred = 0x04, ///< Return IPv4 and/or IPv6 addresses, with IPv6 addresses listed first.
58 // NOTE: At present there are no DNSOption flags define.
59 kDNSOption_ValidFlags = 0, ///< Set of all valid DNSOption flags.
61 kDNSOption_Default = kDNSOption_AddrFamily_Any
68 * This is an internal class to InetLayer that provides the abstraction of
69 * Domain Name System (DNS) resolution in InetLayer. There is no public
70 * interface available for the application layer.
73 class DNSResolver : public InetLayerBasis
76 friend class InetLayer;
78 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
79 #if INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
80 friend class AsyncDNSResolverSockets;
82 /// States of the DNSResolver object with respect to hostname resolution.
83 typedef enum DNSResolverState{
84 kState_Unused = 0, ///< Used to indicate that the DNSResolver object is not used.
85 kState_Active = 2, ///< Used to indicate that a DNS resolution is being performed on the DNSResolver object.
86 kState_Complete = 3, ///< Used to indicate that the DNS resolution on the DNSResolver object is complete.
87 kState_Canceled = 4, ///< Used to indicate that the DNS resolution on the DNSResolver has been canceled.
89 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
90 #endif // INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
93 * @brief Type of event handling function called when a DNS request completes.
95 * @param[in] appState Application state pointer.
96 * @param[in] err Error code.
97 * @param[in] addrCount Number of IP addresses in the \c addrArray list.
98 * @param[in] addrArray List of addresses in the answer.
101 * Provide a function of this type to the \c Resolve method to process
104 typedef void (*OnResolveCompleteFunct)(void * appState, INET_ERROR err, uint8_t addrCount, IPAddress * addrArray);
106 static chip::System::ObjectPool<DNSResolver, INET_CONFIG_NUM_DNS_RESOLVERS> sPool;
109 * A pointer to the callback function when a DNS request is complete.
111 OnResolveCompleteFunct OnComplete;
114 * A pointer to the DNS table that stores a list of resolved addresses.
116 IPAddress * AddrArray;
119 * The maximum number of addresses that could be stored in the DNS table.
124 * The actual number of addresses that are stored in the DNS table.
129 * DNS options for the current request.
133 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
135 void InitAddrInfoHints(struct addrinfo & hints);
136 INET_ERROR ProcessGetAddrInfoResult(int returnCode, struct addrinfo * results);
137 void CopyAddresses(int family, uint8_t maxAddrs, const struct addrinfo * addrs);
138 uint8_t CountAddresses(int family, const struct addrinfo * addrs);
140 #if INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
142 /* Hostname that requires resolution */
143 char asyncHostNameBuf[NL_DNS_HOSTNAME_MAX_LEN + 1]; // DNS limits hostnames to 253 max characters.
145 INET_ERROR asyncDNSResolveResult;
146 /* The next DNSResolver object in the asynchronous DNS resolution queue. */
147 DNSResolver * pNextAsyncDNSResolver;
149 DNSResolverState mState;
151 void HandleAsyncResolveComplete();
153 #endif // INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
155 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
157 INET_ERROR Resolve(const char * hostName, uint16_t hostNameLen, uint8_t options, uint8_t maxAddrs, IPAddress * addrArray,
158 OnResolveCompleteFunct onComplete, void * appState);
161 #if CHIP_SYSTEM_CONFIG_USE_LWIP
162 void HandleResolveComplete(void);
163 #if LWIP_VERSION_MAJOR > 1
164 static void LwIPHandleResolveComplete(const char * name, const ip_addr_t * ipaddr, void * callback_arg);
165 #else // LWIP_VERSION_MAJOR <= 1
166 static void LwIPHandleResolveComplete(const char * name, ip_addr_t * ipaddr, void * callback_arg);
167 #endif // LWIP_VERSION_MAJOR <= 1
168 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP