• 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 <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)
55 {
56     Instance::SetDnsNameCompressionEnabled(aEnabled);
57 }
58 
otDnsIsNameCompressionEnabled(void)59 bool otDnsIsNameCompressionEnabled(void)
60 {
61     return Instance::IsDnsNameCompressionEnabled();
62 }
63 #endif
64 
65 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
66 
otDnsClientGetDefaultConfig(otInstance * aInstance)67 const otDnsQueryConfig *otDnsClientGetDefaultConfig(otInstance *aInstance)
68 {
69     return &AsCoreType(aInstance).Get<Dns::Client>().GetDefaultConfig();
70 }
71 
otDnsClientSetDefaultConfig(otInstance * aInstance,const otDnsQueryConfig * aConfig)72 void otDnsClientSetDefaultConfig(otInstance *aInstance, const otDnsQueryConfig *aConfig)
73 {
74     if (aConfig != nullptr)
75     {
76         AsCoreType(aInstance).Get<Dns::Client>().SetDefaultConfig(AsCoreType(aConfig));
77     }
78     else
79     {
80         AsCoreType(aInstance).Get<Dns::Client>().ResetDefaultConfig();
81     }
82 }
83 
otDnsClientResolveAddress(otInstance * aInstance,const char * aHostName,otDnsAddressCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)84 otError otDnsClientResolveAddress(otInstance *            aInstance,
85                                   const char *            aHostName,
86                                   otDnsAddressCallback    aCallback,
87                                   void *                  aContext,
88                                   const otDnsQueryConfig *aConfig)
89 {
90     return AsCoreType(aInstance).Get<Dns::Client>().ResolveAddress(aHostName, aCallback, aContext,
91                                                                    AsCoreTypePtr(aConfig));
92 }
93 
94 #if OPENTHREAD_CONFIG_DNS_CLIENT_NAT64_ENABLE
otDnsClientResolveIp4Address(otInstance * aInstance,const char * aHostName,otDnsAddressCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)95 otError otDnsClientResolveIp4Address(otInstance *            aInstance,
96                                      const char *            aHostName,
97                                      otDnsAddressCallback    aCallback,
98                                      void *                  aContext,
99                                      const otDnsQueryConfig *aConfig)
100 {
101     return AsCoreType(aInstance).Get<Dns::Client>().ResolveIp4Address(aHostName, aCallback, aContext,
102                                                                       AsCoreTypePtr(aConfig));
103 }
104 #endif
105 
otDnsAddressResponseGetHostName(const otDnsAddressResponse * aResponse,char * aNameBuffer,uint16_t aNameBufferSize)106 otError otDnsAddressResponseGetHostName(const otDnsAddressResponse *aResponse,
107                                         char *                      aNameBuffer,
108                                         uint16_t                    aNameBufferSize)
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     return AsCoreType(aInstance).Get<Dns::Client>().Browse(aServiceName, aCallback, aContext, AsCoreTypePtr(aConfig));
132 }
133 
otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse * aResponse,char * aNameBuffer,uint16_t aNameBufferSize)134 otError otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse *aResponse,
135                                           char *                     aNameBuffer,
136                                           uint16_t                   aNameBufferSize)
137 {
138     return AsCoreType(aResponse).GetServiceName(aNameBuffer, aNameBufferSize);
139 }
140 
otDnsBrowseResponseGetServiceInstance(const otDnsBrowseResponse * aResponse,uint16_t aIndex,char * aLabelBuffer,uint8_t aLabelBufferSize)141 otError otDnsBrowseResponseGetServiceInstance(const otDnsBrowseResponse *aResponse,
142                                               uint16_t                   aIndex,
143                                               char *                     aLabelBuffer,
144                                               uint8_t                    aLabelBufferSize)
145 {
146     return AsCoreType(aResponse).GetServiceInstance(aIndex, aLabelBuffer, aLabelBufferSize);
147 }
148 
otDnsBrowseResponseGetServiceInfo(const otDnsBrowseResponse * aResponse,const char * aInstanceLabel,otDnsServiceInfo * aServiceInfo)149 otError otDnsBrowseResponseGetServiceInfo(const otDnsBrowseResponse *aResponse,
150                                           const char *               aInstanceLabel,
151                                           otDnsServiceInfo *         aServiceInfo)
152 {
153     return AsCoreType(aResponse).GetServiceInfo(aInstanceLabel, AsCoreType(aServiceInfo));
154 }
155 
otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse * aResponse,const char * aHostName,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)156 otError otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse *aResponse,
157                                           const char *               aHostName,
158                                           uint16_t                   aIndex,
159                                           otIp6Address *             aAddress,
160                                           uint32_t *                 aTtl)
161 {
162     uint32_t ttl;
163 
164     return AsCoreType(aResponse).GetHostAddress(aHostName, aIndex, AsCoreType(aAddress), aTtl != nullptr ? *aTtl : ttl);
165 }
166 
otDnsClientResolveService(otInstance * aInstance,const char * aInstanceLabel,const char * aServiceName,otDnsServiceCallback aCallback,void * aContext,const otDnsQueryConfig * aConfig)167 otError otDnsClientResolveService(otInstance *            aInstance,
168                                   const char *            aInstanceLabel,
169                                   const char *            aServiceName,
170                                   otDnsServiceCallback    aCallback,
171                                   void *                  aContext,
172                                   const otDnsQueryConfig *aConfig)
173 {
174     return AsCoreType(aInstance).Get<Dns::Client>().ResolveService(aInstanceLabel, aServiceName, aCallback, aContext,
175                                                                    AsCoreTypePtr(aConfig));
176 }
177 
otDnsServiceResponseGetServiceName(const otDnsServiceResponse * aResponse,char * aLabelBuffer,uint8_t aLabelBufferSize,char * aNameBuffer,uint16_t aNameBufferSize)178 otError otDnsServiceResponseGetServiceName(const otDnsServiceResponse *aResponse,
179                                            char *                      aLabelBuffer,
180                                            uint8_t                     aLabelBufferSize,
181                                            char *                      aNameBuffer,
182                                            uint16_t                    aNameBufferSize)
183 {
184     return AsCoreType(aResponse).GetServiceName(aLabelBuffer, aLabelBufferSize, aNameBuffer, aNameBufferSize);
185 }
186 
otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse * aResponse,otDnsServiceInfo * aServiceInfo)187 otError otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse *aResponse, otDnsServiceInfo *aServiceInfo)
188 {
189     return AsCoreType(aResponse).GetServiceInfo(AsCoreType(aServiceInfo));
190 }
191 
otDnsServiceResponseGetHostAddress(const otDnsServiceResponse * aResponse,const char * aHostName,uint16_t aIndex,otIp6Address * aAddress,uint32_t * aTtl)192 otError otDnsServiceResponseGetHostAddress(const otDnsServiceResponse *aResponse,
193                                            const char *                aHostName,
194                                            uint16_t                    aIndex,
195                                            otIp6Address *              aAddress,
196                                            uint32_t *                  aTtl)
197 {
198     uint32_t ttl;
199 
200     return AsCoreType(aResponse).GetHostAddress(aHostName, aIndex, AsCoreType(aAddress),
201                                                 (aTtl != nullptr) ? *aTtl : ttl);
202 }
203 
204 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
205 
206 #endif // OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
207