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 <openthread/dns_client.h>
37 
38 #include "common/instance.hpp"
39 #include "net/dns_types.hpp"
40 
41 using namespace ot;
42 
otDnsInitTxtEntryIterator(otDnsTxtEntryIterator * aIterator,const uint8_t * aTxtData,uint16_t aTxtDataLength)43 void otDnsInitTxtEntryIterator(otDnsTxtEntryIterator *aIterator, const uint8_t *aTxtData, uint16_t aTxtDataLength)
44 {
45     AsCoreType(aIterator).Init(aTxtData, aTxtDataLength);
46 }
47 
otDnsGetNextTxtEntry(otDnsTxtEntryIterator * aIterator,otDnsTxtEntry * aEntry)48 otError otDnsGetNextTxtEntry(otDnsTxtEntryIterator *aIterator, otDnsTxtEntry *aEntry)
49 {
50     return AsCoreType(aIterator).GetNextEntry(AsCoreType(aEntry));
51 }
52 
53 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
otDnsSetNameCompressionEnabled(bool aEnabled)54 void otDnsSetNameCompressionEnabled(bool aEnabled) { Instance::SetDnsNameCompressionEnabled(aEnabled); }
55 
otDnsIsNameCompressionEnabled(void)56 bool otDnsIsNameCompressionEnabled(void) { return Instance::IsDnsNameCompressionEnabled(); }
57 #endif
58 
59 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
60 
otDnsClientGetDefaultConfig(otInstance * aInstance)61 const otDnsQueryConfig *otDnsClientGetDefaultConfig(otInstance *aInstance)
62 {
63     return &AsCoreType(aInstance).Get<Dns::Client>().GetDefaultConfig();
64 }
65 
otDnsClientSetDefaultConfig(otInstance * aInstance,const otDnsQueryConfig * aConfig)66 void otDnsClientSetDefaultConfig(otInstance *aInstance, const otDnsQueryConfig *aConfig)
67 {
68     if (aConfig != nullptr)
69     {
70         AsCoreType(aInstance).Get<Dns::Client>().SetDefaultConfig(AsCoreType(aConfig));
71     }
72     else
73     {
74         AsCoreType(aInstance).Get<Dns::Client>().ResetDefaultConfig();
75     }
76 }
77 
otDnsClientResolveAddress(otInstance * aInstance,const char * aHostName,otDnsAddressCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)78 otError otDnsClientResolveAddress(otInstance             *aInstance,
79                                   const char             *aHostName,
80                                   otDnsAddressCallback    aCallback,
81                                   void                   *aContext,
82                                   const otDnsQueryConfig *aConfig)
83 {
84     AssertPointerIsNotNull(aHostName);
85 
86     return AsCoreType(aInstance).Get<Dns::Client>().ResolveAddress(aHostName, aCallback, aContext,
87                                                                    AsCoreTypePtr(aConfig));
88 }
89 
90 #if OPENTHREAD_CONFIG_DNS_CLIENT_NAT64_ENABLE
otDnsClientResolveIp4Address(otInstance * aInstance,const char * aHostName,otDnsAddressCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)91 otError otDnsClientResolveIp4Address(otInstance             *aInstance,
92                                      const char             *aHostName,
93                                      otDnsAddressCallback    aCallback,
94                                      void                   *aContext,
95                                      const otDnsQueryConfig *aConfig)
96 {
97     AssertPointerIsNotNull(aHostName);
98 
99     return AsCoreType(aInstance).Get<Dns::Client>().ResolveIp4Address(aHostName, aCallback, aContext,
100                                                                       AsCoreTypePtr(aConfig));
101 }
102 #endif
103 
otDnsAddressResponseGetHostName(const otDnsAddressResponse * aResponse,char * aNameBuffer,uint16_t aNameBufferSize)104 otError otDnsAddressResponseGetHostName(const otDnsAddressResponse *aResponse,
105                                         char                       *aNameBuffer,
106                                         uint16_t                    aNameBufferSize)
107 {
108     AssertPointerIsNotNull(aNameBuffer);
109 
110     return AsCoreType(aResponse).GetHostName(aNameBuffer, aNameBufferSize);
111 }
112 
otDnsAddressResponseGetAddress(const otDnsAddressResponse * aResponse,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)113 otError otDnsAddressResponseGetAddress(const otDnsAddressResponse *aResponse,
114                                        uint16_t                    aIndex,
115                                        otIp6Address               *aAddress,
116                                        uint32_t                   *aTtl)
117 {
118     uint32_t ttl;
119 
120     return AsCoreType(aResponse).GetAddress(aIndex, AsCoreType(aAddress), (aTtl != nullptr) ? *aTtl : ttl);
121 }
122 
123 #if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
124 
otDnsClientBrowse(otInstance * aInstance,const char * aServiceName,otDnsBrowseCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)125 otError otDnsClientBrowse(otInstance             *aInstance,
126                           const char             *aServiceName,
127                           otDnsBrowseCallback     aCallback,
128                           void                   *aContext,
129                           const otDnsQueryConfig *aConfig)
130 {
131     AssertPointerIsNotNull(aServiceName);
132 
133     return AsCoreType(aInstance).Get<Dns::Client>().Browse(aServiceName, aCallback, aContext, AsCoreTypePtr(aConfig));
134 }
135 
otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse * aResponse,char * aNameBuffer,uint16_t aNameBufferSize)136 otError otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse *aResponse,
137                                           char                      *aNameBuffer,
138                                           uint16_t                   aNameBufferSize)
139 {
140     AssertPointerIsNotNull(aNameBuffer);
141 
142     return AsCoreType(aResponse).GetServiceName(aNameBuffer, aNameBufferSize);
143 }
144 
otDnsBrowseResponseGetServiceInstance(const otDnsBrowseResponse * aResponse,uint16_t aIndex,char * aLabelBuffer,uint8_t aLabelBufferSize)145 otError otDnsBrowseResponseGetServiceInstance(const otDnsBrowseResponse *aResponse,
146                                               uint16_t                   aIndex,
147                                               char                      *aLabelBuffer,
148                                               uint8_t                    aLabelBufferSize)
149 {
150     AssertPointerIsNotNull(aLabelBuffer);
151 
152     return AsCoreType(aResponse).GetServiceInstance(aIndex, aLabelBuffer, aLabelBufferSize);
153 }
154 
otDnsBrowseResponseGetServiceInfo(const otDnsBrowseResponse * aResponse,const char * aInstanceLabel,otDnsServiceInfo * aServiceInfo)155 otError otDnsBrowseResponseGetServiceInfo(const otDnsBrowseResponse *aResponse,
156                                           const char                *aInstanceLabel,
157                                           otDnsServiceInfo          *aServiceInfo)
158 {
159     AssertPointerIsNotNull(aInstanceLabel);
160 
161     return AsCoreType(aResponse).GetServiceInfo(aInstanceLabel, AsCoreType(aServiceInfo));
162 }
163 
otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse * aResponse,const char * aHostName,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)164 otError otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse *aResponse,
165                                           const char                *aHostName,
166                                           uint16_t                   aIndex,
167                                           otIp6Address              *aAddress,
168                                           uint32_t                  *aTtl)
169 {
170     uint32_t ttl;
171 
172     AssertPointerIsNotNull(aHostName);
173 
174     return AsCoreType(aResponse).GetHostAddress(aHostName, aIndex, AsCoreType(aAddress), aTtl != nullptr ? *aTtl : ttl);
175 }
176 
otDnsClientResolveService(otInstance * aInstance,const char * aInstanceLabel,const char * aServiceName,otDnsServiceCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)177 otError otDnsClientResolveService(otInstance             *aInstance,
178                                   const char             *aInstanceLabel,
179                                   const char             *aServiceName,
180                                   otDnsServiceCallback    aCallback,
181                                   void                   *aContext,
182                                   const otDnsQueryConfig *aConfig)
183 {
184     AssertPointerIsNotNull(aInstanceLabel);
185     AssertPointerIsNotNull(aServiceName);
186 
187     return AsCoreType(aInstance).Get<Dns::Client>().ResolveService(aInstanceLabel, aServiceName, aCallback, aContext,
188                                                                    AsCoreTypePtr(aConfig));
189 }
190 
otDnsClientResolveServiceAndHostAddress(otInstance * aInstance,const char * aInstanceLabel,const char * aServiceName,otDnsServiceCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)191 otError otDnsClientResolveServiceAndHostAddress(otInstance             *aInstance,
192                                                 const char             *aInstanceLabel,
193                                                 const char             *aServiceName,
194                                                 otDnsServiceCallback    aCallback,
195                                                 void                   *aContext,
196                                                 const otDnsQueryConfig *aConfig)
197 {
198     AssertPointerIsNotNull(aInstanceLabel);
199     AssertPointerIsNotNull(aServiceName);
200 
201     return AsCoreType(aInstance).Get<Dns::Client>().ResolveServiceAndHostAddress(
202         aInstanceLabel, aServiceName, aCallback, aContext, AsCoreTypePtr(aConfig));
203 }
204 
otDnsServiceResponseGetServiceName(const otDnsServiceResponse * aResponse,char * aLabelBuffer,uint8_t aLabelBufferSize,char * aNameBuffer,uint16_t aNameBufferSize)205 otError otDnsServiceResponseGetServiceName(const otDnsServiceResponse *aResponse,
206                                            char                       *aLabelBuffer,
207                                            uint8_t                     aLabelBufferSize,
208                                            char                       *aNameBuffer,
209                                            uint16_t                    aNameBufferSize)
210 {
211     AssertPointerIsNotNull(aLabelBuffer);
212     AssertPointerIsNotNull(aNameBuffer);
213 
214     return AsCoreType(aResponse).GetServiceName(aLabelBuffer, aLabelBufferSize, aNameBuffer, aNameBufferSize);
215 }
216 
otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse * aResponse,otDnsServiceInfo * aServiceInfo)217 otError otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse *aResponse, otDnsServiceInfo *aServiceInfo)
218 {
219     return AsCoreType(aResponse).GetServiceInfo(AsCoreType(aServiceInfo));
220 }
221 
otDnsServiceResponseGetHostAddress(const otDnsServiceResponse * aResponse,const char * aHostName,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)222 otError otDnsServiceResponseGetHostAddress(const otDnsServiceResponse *aResponse,
223                                            const char                 *aHostName,
224                                            uint16_t                    aIndex,
225                                            otIp6Address               *aAddress,
226                                            uint32_t                   *aTtl)
227 {
228     uint32_t ttl;
229 
230     AssertPointerIsNotNull(aHostName);
231 
232     return AsCoreType(aResponse).GetHostAddress(aHostName, aIndex, AsCoreType(aAddress),
233                                                 (aTtl != nullptr) ? *aTtl : ttl);
234 }
235 
236 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
237 
238 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
239