• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2017, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements the OpenThread DNSv6 API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #include "instance/instance.hpp"
37 #include "net/dns_types.hpp"
38 
39 using namespace ot;
40 
otDnsInitTxtEntryIterator(otDnsTxtEntryIterator * aIterator,const uint8_t * aTxtData,uint16_t aTxtDataLength)41 void otDnsInitTxtEntryIterator(otDnsTxtEntryIterator *aIterator, const uint8_t *aTxtData, uint16_t aTxtDataLength)
42 {
43     AsCoreType(aIterator).Init(aTxtData, aTxtDataLength);
44 }
45 
otDnsGetNextTxtEntry(otDnsTxtEntryIterator * aIterator,otDnsTxtEntry * aEntry)46 otError otDnsGetNextTxtEntry(otDnsTxtEntryIterator *aIterator, otDnsTxtEntry *aEntry)
47 {
48     return AsCoreType(aIterator).GetNextEntry(AsCoreType(aEntry));
49 }
50 
otDnsEncodeTxtData(const otDnsTxtEntry * aTxtEntries,uint8_t aNumTxtEntries,uint8_t * aTxtData,uint16_t * aTxtDataLength)51 otError otDnsEncodeTxtData(const otDnsTxtEntry *aTxtEntries,
52                            uint8_t              aNumTxtEntries,
53                            uint8_t             *aTxtData,
54                            uint16_t            *aTxtDataLength)
55 {
56     Error                          error;
57     MutableData<kWithUint16Length> data;
58 
59     AssertPointerIsNotNull(aTxtEntries);
60     AssertPointerIsNotNull(aTxtData);
61     AssertPointerIsNotNull(aTxtDataLength);
62 
63     data.Init(aTxtData, *aTxtDataLength);
64     SuccessOrExit(error = Dns::TxtEntry::AppendEntries(AsCoreTypePtr(aTxtEntries), aNumTxtEntries, data));
65     *aTxtDataLength = data.GetLength();
66 
67 exit:
68     return error;
69 }
70 
71 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
otDnsSetNameCompressionEnabled(bool aEnabled)72 void otDnsSetNameCompressionEnabled(bool aEnabled) { Instance::SetDnsNameCompressionEnabled(aEnabled); }
73 
otDnsIsNameCompressionEnabled(void)74 bool otDnsIsNameCompressionEnabled(void) { return Instance::IsDnsNameCompressionEnabled(); }
75 #endif
76 
77 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
78 
otDnsClientGetDefaultConfig(otInstance * aInstance)79 const otDnsQueryConfig *otDnsClientGetDefaultConfig(otInstance *aInstance)
80 {
81     return &AsCoreType(aInstance).Get<Dns::Client>().GetDefaultConfig();
82 }
83 
otDnsClientSetDefaultConfig(otInstance * aInstance,const otDnsQueryConfig * aConfig)84 void otDnsClientSetDefaultConfig(otInstance *aInstance, const otDnsQueryConfig *aConfig)
85 {
86     if (aConfig != nullptr)
87     {
88         AsCoreType(aInstance).Get<Dns::Client>().SetDefaultConfig(AsCoreType(aConfig));
89     }
90     else
91     {
92         AsCoreType(aInstance).Get<Dns::Client>().ResetDefaultConfig();
93     }
94 }
95 
otDnsClientResolveAddress(otInstance * aInstance,const char * aHostName,otDnsAddressCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)96 otError otDnsClientResolveAddress(otInstance             *aInstance,
97                                   const char             *aHostName,
98                                   otDnsAddressCallback    aCallback,
99                                   void                   *aContext,
100                                   const otDnsQueryConfig *aConfig)
101 {
102     AssertPointerIsNotNull(aHostName);
103 
104     return AsCoreType(aInstance).Get<Dns::Client>().ResolveAddress(aHostName, aCallback, aContext,
105                                                                    AsCoreTypePtr(aConfig));
106 }
107 
108 #if OPENTHREAD_CONFIG_DNS_CLIENT_NAT64_ENABLE
otDnsClientResolveIp4Address(otInstance * aInstance,const char * aHostName,otDnsAddressCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)109 otError otDnsClientResolveIp4Address(otInstance             *aInstance,
110                                      const char             *aHostName,
111                                      otDnsAddressCallback    aCallback,
112                                      void                   *aContext,
113                                      const otDnsQueryConfig *aConfig)
114 {
115     AssertPointerIsNotNull(aHostName);
116 
117     return AsCoreType(aInstance).Get<Dns::Client>().ResolveIp4Address(aHostName, aCallback, aContext,
118                                                                       AsCoreTypePtr(aConfig));
119 }
120 #endif
121 
otDnsAddressResponseGetHostName(const otDnsAddressResponse * aResponse,char * aNameBuffer,uint16_t aNameBufferSize)122 otError otDnsAddressResponseGetHostName(const otDnsAddressResponse *aResponse,
123                                         char                       *aNameBuffer,
124                                         uint16_t                    aNameBufferSize)
125 {
126     AssertPointerIsNotNull(aNameBuffer);
127 
128     return AsCoreType(aResponse).GetHostName(aNameBuffer, aNameBufferSize);
129 }
130 
otDnsAddressResponseGetAddress(const otDnsAddressResponse * aResponse,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)131 otError otDnsAddressResponseGetAddress(const otDnsAddressResponse *aResponse,
132                                        uint16_t                    aIndex,
133                                        otIp6Address               *aAddress,
134                                        uint32_t                   *aTtl)
135 {
136     uint32_t ttl;
137 
138     return AsCoreType(aResponse).GetAddress(aIndex, AsCoreType(aAddress), (aTtl != nullptr) ? *aTtl : ttl);
139 }
140 
141 #if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
142 
otDnsClientBrowse(otInstance * aInstance,const char * aServiceName,otDnsBrowseCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)143 otError otDnsClientBrowse(otInstance             *aInstance,
144                           const char             *aServiceName,
145                           otDnsBrowseCallback     aCallback,
146                           void                   *aContext,
147                           const otDnsQueryConfig *aConfig)
148 {
149     AssertPointerIsNotNull(aServiceName);
150 
151     return AsCoreType(aInstance).Get<Dns::Client>().Browse(aServiceName, aCallback, aContext, AsCoreTypePtr(aConfig));
152 }
153 
otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse * aResponse,char * aNameBuffer,uint16_t aNameBufferSize)154 otError otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse *aResponse,
155                                           char                      *aNameBuffer,
156                                           uint16_t                   aNameBufferSize)
157 {
158     AssertPointerIsNotNull(aNameBuffer);
159 
160     return AsCoreType(aResponse).GetServiceName(aNameBuffer, aNameBufferSize);
161 }
162 
otDnsBrowseResponseGetServiceInstance(const otDnsBrowseResponse * aResponse,uint16_t aIndex,char * aLabelBuffer,uint8_t aLabelBufferSize)163 otError otDnsBrowseResponseGetServiceInstance(const otDnsBrowseResponse *aResponse,
164                                               uint16_t                   aIndex,
165                                               char                      *aLabelBuffer,
166                                               uint8_t                    aLabelBufferSize)
167 {
168     AssertPointerIsNotNull(aLabelBuffer);
169 
170     return AsCoreType(aResponse).GetServiceInstance(aIndex, aLabelBuffer, aLabelBufferSize);
171 }
172 
otDnsBrowseResponseGetServiceInfo(const otDnsBrowseResponse * aResponse,const char * aInstanceLabel,otDnsServiceInfo * aServiceInfo)173 otError otDnsBrowseResponseGetServiceInfo(const otDnsBrowseResponse *aResponse,
174                                           const char                *aInstanceLabel,
175                                           otDnsServiceInfo          *aServiceInfo)
176 {
177     AssertPointerIsNotNull(aInstanceLabel);
178 
179     return AsCoreType(aResponse).GetServiceInfo(aInstanceLabel, AsCoreType(aServiceInfo));
180 }
181 
otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse * aResponse,const char * aHostName,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)182 otError otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse *aResponse,
183                                           const char                *aHostName,
184                                           uint16_t                   aIndex,
185                                           otIp6Address              *aAddress,
186                                           uint32_t                  *aTtl)
187 {
188     uint32_t ttl;
189 
190     AssertPointerIsNotNull(aHostName);
191 
192     return AsCoreType(aResponse).GetHostAddress(aHostName, aIndex, AsCoreType(aAddress), aTtl != nullptr ? *aTtl : ttl);
193 }
194 
otDnsClientResolveService(otInstance * aInstance,const char * aInstanceLabel,const char * aServiceName,otDnsServiceCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)195 otError otDnsClientResolveService(otInstance             *aInstance,
196                                   const char             *aInstanceLabel,
197                                   const char             *aServiceName,
198                                   otDnsServiceCallback    aCallback,
199                                   void                   *aContext,
200                                   const otDnsQueryConfig *aConfig)
201 {
202     AssertPointerIsNotNull(aInstanceLabel);
203     AssertPointerIsNotNull(aServiceName);
204 
205     return AsCoreType(aInstance).Get<Dns::Client>().ResolveService(aInstanceLabel, aServiceName, aCallback, aContext,
206                                                                    AsCoreTypePtr(aConfig));
207 }
208 
otDnsClientResolveServiceAndHostAddress(otInstance * aInstance,const char * aInstanceLabel,const char * aServiceName,otDnsServiceCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)209 otError otDnsClientResolveServiceAndHostAddress(otInstance             *aInstance,
210                                                 const char             *aInstanceLabel,
211                                                 const char             *aServiceName,
212                                                 otDnsServiceCallback    aCallback,
213                                                 void                   *aContext,
214                                                 const otDnsQueryConfig *aConfig)
215 {
216     AssertPointerIsNotNull(aInstanceLabel);
217     AssertPointerIsNotNull(aServiceName);
218 
219     return AsCoreType(aInstance).Get<Dns::Client>().ResolveServiceAndHostAddress(
220         aInstanceLabel, aServiceName, aCallback, aContext, AsCoreTypePtr(aConfig));
221 }
222 
otDnsServiceResponseGetServiceName(const otDnsServiceResponse * aResponse,char * aLabelBuffer,uint8_t aLabelBufferSize,char * aNameBuffer,uint16_t aNameBufferSize)223 otError otDnsServiceResponseGetServiceName(const otDnsServiceResponse *aResponse,
224                                            char                       *aLabelBuffer,
225                                            uint8_t                     aLabelBufferSize,
226                                            char                       *aNameBuffer,
227                                            uint16_t                    aNameBufferSize)
228 {
229     AssertPointerIsNotNull(aLabelBuffer);
230     AssertPointerIsNotNull(aNameBuffer);
231 
232     return AsCoreType(aResponse).GetServiceName(aLabelBuffer, aLabelBufferSize, aNameBuffer, aNameBufferSize);
233 }
234 
otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse * aResponse,otDnsServiceInfo * aServiceInfo)235 otError otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse *aResponse, otDnsServiceInfo *aServiceInfo)
236 {
237     return AsCoreType(aResponse).GetServiceInfo(AsCoreType(aServiceInfo));
238 }
239 
otDnsServiceResponseGetHostAddress(const otDnsServiceResponse * aResponse,const char * aHostName,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)240 otError otDnsServiceResponseGetHostAddress(const otDnsServiceResponse *aResponse,
241                                            const char                 *aHostName,
242                                            uint16_t                    aIndex,
243                                            otIp6Address               *aAddress,
244                                            uint32_t                   *aTtl)
245 {
246     uint32_t ttl;
247 
248     AssertPointerIsNotNull(aHostName);
249 
250     return AsCoreType(aResponse).GetHostAddress(aHostName, aIndex, AsCoreType(aAddress),
251                                                 (aTtl != nullptr) ? *aTtl : ttl);
252 }
253 
254 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
255 
256 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
257