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