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 "common/locator_getters.hpp"
40 #include "net/dns_client.hpp"
41 #include "net/dns_types.hpp"
42
43 using namespace ot;
44
otDnsInitTxtEntryIterator(otDnsTxtEntryIterator * aIterator,const uint8_t * aTxtData,uint16_t aTxtDataLength)45 void otDnsInitTxtEntryIterator(otDnsTxtEntryIterator *aIterator, const uint8_t *aTxtData, uint16_t aTxtDataLength)
46 {
47 static_cast<Dns::TxtEntry::Iterator *>(aIterator)->Init(aTxtData, aTxtDataLength);
48 }
49
otDnsGetNextTxtEntry(otDnsTxtEntryIterator * aIterator,otDnsTxtEntry * aEntry)50 otError otDnsGetNextTxtEntry(otDnsTxtEntryIterator *aIterator, otDnsTxtEntry *aEntry)
51 {
52 return static_cast<Dns::TxtEntry::Iterator *>(aIterator)->GetNextEntry(*static_cast<Dns::TxtEntry *>(aEntry));
53 }
54
55 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
otDnsSetNameCompressionEnabled(bool aEnabled)56 void otDnsSetNameCompressionEnabled(bool aEnabled)
57 {
58 Instance::SetDnsNameCompressionEnabled(aEnabled);
59 }
60
otDnsIsNameCompressionEnabled(void)61 bool otDnsIsNameCompressionEnabled(void)
62 {
63 return Instance::IsDnsNameCompressionEnabled();
64 }
65 #endif
66
67 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
68
otDnsClientGetDefaultConfig(otInstance * aInstance)69 const otDnsQueryConfig *otDnsClientGetDefaultConfig(otInstance *aInstance)
70 {
71 Instance &instance = *static_cast<Instance *>(aInstance);
72
73 return &instance.Get<Dns::Client>().GetDefaultConfig();
74 }
75
otDnsClientSetDefaultConfig(otInstance * aInstance,const otDnsQueryConfig * aConfig)76 void otDnsClientSetDefaultConfig(otInstance *aInstance, const otDnsQueryConfig *aConfig)
77 {
78 Instance &instance = *static_cast<Instance *>(aInstance);
79
80 if (aConfig != nullptr)
81 {
82 instance.Get<Dns::Client>().SetDefaultConfig(*static_cast<const Dns::Client::QueryConfig *>(aConfig));
83 }
84 else
85 {
86 instance.Get<Dns::Client>().ResetDefaultConfig();
87 }
88 }
89
otDnsClientResolveAddress(otInstance * aInstance,const char * aHostName,otDnsAddressCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)90 otError otDnsClientResolveAddress(otInstance * aInstance,
91 const char * aHostName,
92 otDnsAddressCallback aCallback,
93 void * aContext,
94 const otDnsQueryConfig *aConfig)
95 {
96 Instance &instance = *static_cast<Instance *>(aInstance);
97
98 return instance.Get<Dns::Client>().ResolveAddress(aHostName, aCallback, aContext,
99 static_cast<const Dns::Client::QueryConfig *>(aConfig));
100 }
101
otDnsAddressResponseGetHostName(const otDnsAddressResponse * aResponse,char * aNameBuffer,uint16_t aNameBufferSize)102 otError otDnsAddressResponseGetHostName(const otDnsAddressResponse *aResponse,
103 char * aNameBuffer,
104 uint16_t aNameBufferSize)
105 {
106 const Dns::Client::AddressResponse &response = *static_cast<const Dns::Client::AddressResponse *>(aResponse);
107
108 return response.GetHostName(aNameBuffer, aNameBufferSize);
109 }
110
otDnsAddressResponseGetAddress(const otDnsAddressResponse * aResponse,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)111 otError otDnsAddressResponseGetAddress(const otDnsAddressResponse *aResponse,
112 uint16_t aIndex,
113 otIp6Address * aAddress,
114 uint32_t * aTtl)
115 {
116 const Dns::Client::AddressResponse &response = *static_cast<const Dns::Client::AddressResponse *>(aResponse);
117 uint32_t ttl;
118
119 return response.GetAddress(aIndex, *static_cast<Ip6::Address *>(aAddress), (aTtl != nullptr) ? *aTtl : ttl);
120 }
121
122 #if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
123
otDnsClientBrowse(otInstance * aInstance,const char * aServiceName,otDnsBrowseCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)124 otError otDnsClientBrowse(otInstance * aInstance,
125 const char * aServiceName,
126 otDnsBrowseCallback aCallback,
127 void * aContext,
128 const otDnsQueryConfig *aConfig)
129 {
130 Instance &instance = *static_cast<Instance *>(aInstance);
131
132 return instance.Get<Dns::Client>().Browse(aServiceName, aCallback, aContext,
133 static_cast<const Dns::Client::QueryConfig *>(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 const Dns::Client::BrowseResponse &response = *static_cast<const Dns::Client::BrowseResponse *>(aResponse);
141
142 return response.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 const Dns::Client::BrowseResponse &response = *static_cast<const Dns::Client::BrowseResponse *>(aResponse);
151
152 return response.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 const Dns::Client::BrowseResponse &response = *static_cast<const Dns::Client::BrowseResponse *>(aResponse);
160
161 return response.GetServiceInfo(aInstanceLabel, *static_cast<Dns::Client::ServiceInfo *>(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 const Dns::Client::BrowseResponse &response = *static_cast<const Dns::Client::BrowseResponse *>(aResponse);
171 uint32_t ttl;
172
173 return response.GetHostAddress(aHostName, aIndex, *static_cast<Ip6::Address *>(aAddress),
174 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 Instance &instance = *static_cast<Instance *>(aInstance);
185
186 return instance.Get<Dns::Client>().ResolveService(aInstanceLabel, aServiceName, aCallback, aContext,
187 static_cast<const Dns::Client::QueryConfig *>(aConfig));
188 }
189
otDnsServiceResponseGetServiceName(const otDnsServiceResponse * aResponse,char * aLabelBuffer,uint8_t aLabelBufferSize,char * aNameBuffer,uint16_t aNameBufferSize)190 otError otDnsServiceResponseGetServiceName(const otDnsServiceResponse *aResponse,
191 char * aLabelBuffer,
192 uint8_t aLabelBufferSize,
193 char * aNameBuffer,
194 uint16_t aNameBufferSize)
195 {
196 const Dns::Client::ServiceResponse &response = *static_cast<const Dns::Client::ServiceResponse *>(aResponse);
197
198 return response.GetServiceName(aLabelBuffer, aLabelBufferSize, aNameBuffer, aNameBufferSize);
199 }
200
otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse * aResponse,otDnsServiceInfo * aServiceInfo)201 otError otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse *aResponse, otDnsServiceInfo *aServiceInfo)
202 {
203 const Dns::Client::ServiceResponse &response = *static_cast<const Dns::Client::ServiceResponse *>(aResponse);
204
205 return response.GetServiceInfo(*static_cast<Dns::Client::ServiceInfo *>(aServiceInfo));
206 }
207
otDnsServiceResponseGetHostAddress(const otDnsServiceResponse * aResponse,const char * aHostName,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)208 otError otDnsServiceResponseGetHostAddress(const otDnsServiceResponse *aResponse,
209 const char * aHostName,
210 uint16_t aIndex,
211 otIp6Address * aAddress,
212 uint32_t * aTtl)
213 {
214 const Dns::Client::ServiceResponse &response = *static_cast<const Dns::Client::ServiceResponse *>(aResponse);
215 uint32_t ttl;
216
217 return response.GetHostAddress(aHostName, aIndex, *static_cast<Ip6::Address *>(aAddress),
218 (aTtl != nullptr) ? *aTtl : ttl);
219 }
220
221 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
222
223 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
224