3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 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 the AsyncDNSResolver, the object that performs
22 * Asynchronous Domain Name System (DNS) resolution in InetLayer.
27 #include <inet/IPAddress.h>
28 #include <inet/InetError.h>
30 #if INET_CONFIG_ENABLE_DNS_RESOLVER
31 #include <inet/DNSResolver.h>
32 #endif // INET_CONFIG_ENABLE_DNS_RESOLVER
34 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
35 #if INET_CONFIG_ENABLE_DNS_RESOLVER && INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
38 #include <semaphore.h>
44 * @class AsyncDNSResolverSockets
47 * This is an internal class to InetLayer that provides helper APIs for
48 * Asynchronous Domain Name System (DNS) resolution in InetLayer.
49 * There is no public interface available for the application layer.
52 class AsyncDNSResolverSockets
54 friend class InetLayer;
55 friend class DNSResolver;
58 INET_ERROR EnqueueRequest(DNSResolver & resolver);
60 INET_ERROR Init(InetLayer * inet);
62 INET_ERROR Cancel(DNSResolver & resolver);
64 INET_ERROR Shutdown();
66 INET_ERROR PrepareDNSResolver(DNSResolver & resolver, const char * hostName, uint16_t hostNameLen, uint8_t options,
67 uint8_t maxAddrs, IPAddress * addrArray, DNSResolver::OnResolveCompleteFunct onComplete,
71 pthread_t mAsyncDNSThreadHandle[INET_CONFIG_DNS_ASYNC_MAX_THREAD_COUNT];
72 pthread_mutex_t mAsyncDNSMutex; /* Mutex for accessing the DNSResolver queue. */
73 pthread_cond_t mAsyncDNSCondVar; /* Condition Variable for thread synchronization. */
74 volatile DNSResolver * mAsyncDNSQueueHead; /* The head of the asynchronous DNSResolver object queue. */
75 volatile DNSResolver * mAsyncDNSQueueTail; /* The tail of the asynchronous DNSResolver object queue. */
76 InetLayer * mInet; /* The pointer to the InetLayer. */
78 DNSResultEventHandler(chip::System::Layer * aLayer, void * aAppState,
79 chip::System::Error aError); /* Timer event handler function for asynchronous DNS notification */
81 INET_ERROR DequeueRequest(DNSResolver ** outResolver);
83 bool ShouldThreadShutdown();
85 void Resolve(DNSResolver & resolver);
87 void UpdateDNSResult(DNSResolver & resolver, struct addrinfo * lookupRes);
89 static void * AsyncDNSThreadRun(void * args);
91 static void NotifyChipThread(DNSResolver * resolver);
93 void AsyncMutexLock();
95 void AsyncMutexUnlock();
100 #endif // INET_CONFIG_ENABLE_DNS_RESOLVER && INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
101 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS