Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / inet / AsyncDNSResolverSockets.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2017 Nest Labs, Inc.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      This file defines the AsyncDNSResolver, the object that performs
22  *      Asynchronous Domain Name System (DNS) resolution in InetLayer.
23  *
24  */
25 #pragma once
26
27 #include <inet/IPAddress.h>
28 #include <inet/InetError.h>
29
30 #if INET_CONFIG_ENABLE_DNS_RESOLVER
31 #include <inet/DNSResolver.h>
32 #endif // INET_CONFIG_ENABLE_DNS_RESOLVER
33
34 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
35 #if INET_CONFIG_ENABLE_DNS_RESOLVER && INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
36 #include <netdb.h>
37 #include <pthread.h>
38 #include <semaphore.h>
39
40 namespace chip {
41 namespace Inet {
42
43 /**
44  *  @class AsyncDNSResolverSockets
45  *
46  *  @brief
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.
50  *
51  */
52 class AsyncDNSResolverSockets
53 {
54     friend class InetLayer;
55     friend class DNSResolver;
56
57 public:
58     INET_ERROR EnqueueRequest(DNSResolver & resolver);
59
60     INET_ERROR Init(InetLayer * inet);
61
62     INET_ERROR Cancel(DNSResolver & resolver);
63
64     INET_ERROR Shutdown();
65
66     INET_ERROR PrepareDNSResolver(DNSResolver & resolver, const char * hostName, uint16_t hostNameLen, uint8_t options,
67                                   uint8_t maxAddrs, IPAddress * addrArray, DNSResolver::OnResolveCompleteFunct onComplete,
68                                   void * appState);
69
70 private:
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. */
77     static void
78     DNSResultEventHandler(chip::System::Layer * aLayer, void * aAppState,
79                           chip::System::Error aError); /* Timer event handler function for asynchronous DNS notification */
80
81     INET_ERROR DequeueRequest(DNSResolver ** outResolver);
82
83     bool ShouldThreadShutdown();
84
85     void Resolve(DNSResolver & resolver);
86
87     void UpdateDNSResult(DNSResolver & resolver, struct addrinfo * lookupRes);
88
89     static void * AsyncDNSThreadRun(void * args);
90
91     static void NotifyChipThread(DNSResolver * resolver);
92
93     void AsyncMutexLock();
94
95     void AsyncMutexUnlock();
96 };
97
98 } // namespace Inet
99 } // namespace chip
100 #endif // INET_CONFIG_ENABLE_DNS_RESOLVER && INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
101 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS