1 /*
2  *  Copyright (c) 2017-2021, 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  * @brief
32  *  This file defines the top-level DNS functions for the OpenThread library.
33  */
34 
35 #ifndef OPENTHREAD_DNS_CLIENT_H_
36 #define OPENTHREAD_DNS_CLIENT_H_
37 
38 #include <openthread/dns.h>
39 #include <openthread/instance.h>
40 #include <openthread/ip6.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /**
47  * @addtogroup api-dns
48  *
49  * @brief
50  *   This module includes functions that control DNS communication.
51  *
52  *   The functions in this module are available only if feature `OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE` is enabled.
53  *
54  * @{
55  *
56  */
57 
58 /**
59  * This enumeration type represents the "Recursion Desired" (RD) flag in an `otDnsQueryConfig`.
60  *
61  */
62 typedef enum
63 {
64     OT_DNS_FLAG_UNSPECIFIED       = 0, ///< Indicates the flag is not specified.
65     OT_DNS_FLAG_RECURSION_DESIRED = 1, ///< Indicates DNS name server can resolve the query recursively.
66     OT_DNS_FLAG_NO_RECURSION      = 2, ///< Indicates DNS name server can not resolve the query recursively.
67 } otDnsRecursionFlag;
68 
69 /**
70  * This enumeration type represents the NAT64 mode in an `otDnsQueryConfig`.
71  *
72  * The NAT64 mode indicates whether to allow or disallow NAT64 address translation during DNS client address resolution.
73  * This mode is only used when `OPENTHREAD_CONFIG_DNS_CLIENT_NAT64_ENABLE` is enabled.
74  *
75  */
76 typedef enum
77 {
78     OT_DNS_NAT64_UNSPECIFIED = 0, ///< NAT64 mode is not specified. Use default NAT64 mode.
79     OT_DNS_NAT64_ALLOW       = 1, ///< Allow NAT64 address translation during DNS client address resolution.
80     OT_DNS_NAT64_DISALLOW    = 2, ///< Do not allow NAT64 address translation during DNS client address resolution.
81 } otDnsNat64Mode;
82 
83 /**
84  * This structure represents a DNS query configuration.
85  *
86  * Any of the fields in this structure can be set to zero to indicate that it is not specified. How the unspecified
87  * fields are treated is determined by the function which uses the instance of `otDnsQueryConfig`.
88  *
89  */
90 typedef struct otDnsQueryConfig
91 {
92     otSockAddr         mServerSockAddr;  ///< Server address (IPv6 address/port). All zero or zero port for unspecified.
93     uint32_t           mResponseTimeout; ///< Wait time (in msec) to rx response. Zero indicates unspecified value.
94     uint8_t            mMaxTxAttempts;   ///< Maximum tx attempts before reporting failure. Zero for unspecified value.
95     otDnsRecursionFlag mRecursionFlag;   ///< Indicates whether the server can resolve the query recursively or not.
96     otDnsNat64Mode     mNat64Mode;       ///< Allow/Disallow NAT64 address translation during address resolution.
97 } otDnsQueryConfig;
98 
99 /**
100  * This function gets the current default query config used by DNS client.
101  *
102  * When OpenThread stack starts, the default DNS query config is determined from a set of OT config options such as
103  * `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS`, `_DEFAULT_SERVER_PORT`, `_DEFAULT_RESPONSE_TIMEOUT`, etc.
104  * (see `config/dns_client.h` for all related config options).
105  *
106  * @param[in]  aInstance        A pointer to an OpenThread instance.
107  *
108  * @returns A pointer to the current default config being used by DNS client.
109  *
110  */
111 const otDnsQueryConfig *otDnsClientGetDefaultConfig(otInstance *aInstance);
112 
113 /**
114  * This function sets the default query config on DNS client.
115  *
116  * @note Any ongoing query will continue to use the config from when it was started. The new default config will be
117  * used for any future DNS queries.
118  *
119  * The @p aConfig can be NULL. In this case the default config will be set to the defaults from OT config options
120  * `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_{}`. This resets the default query config back to to the config when the
121  * OpenThread stack starts.
122  *
123  * In a non-NULL @p aConfig, caller can choose to leave some of the fields in `otDnsQueryConfig` instance unspecified
124  * (value zero). The unspecified fields are replaced by the corresponding OT config option definitions
125  * `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_{}` to form the default query config.
126  *
127  * When `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_ADDRESS_AUTO_SET_ENABLE` is enabled, the server's IPv6 address in
128  * the default config is automatically set and updated by DNS client. This is done only when user does not explicitly
129  * set or specify it. This behavior requires SRP client and its auto-start feature to be enabled. SRP client will then
130  * monitor the Thread Network Data for DNS/SRP Service entries to select an SRP server. The selected SRP server address
131  * is also set as the DNS server address in the default config.
132  *
133  * @param[in]  aInstance   A pointer to an OpenThread instance.
134  * @param[in]  aConfig     A pointer to the new query config to use as default.
135  *
136  */
137 void otDnsClientSetDefaultConfig(otInstance *aInstance, const otDnsQueryConfig *aConfig);
138 
139 /**
140  * This type is an opaque representation of a response to an address resolution DNS query.
141  *
142  * Pointers to instance of this type are provided from callback `otDnsAddressCallback`.
143  *
144  */
145 typedef struct otDnsAddressResponse otDnsAddressResponse;
146 
147 /**
148  * This function pointer is called when a DNS response is received for an address resolution query.
149  *
150  * Within this callback the user can use `otDnsAddressResponseGet{Item}()` functions along with the @p aResponse
151  * pointer to get more info about the response.
152  *
153  * The @p aResponse pointer can only be used within this callback and after returning from this function it will not
154  * stay valid, so the user MUST NOT retain the @p aResponse pointer for later use.
155  *
156  * @param[in]  aError     The result of the DNS transaction.
157  * @param[in]  aResponse  A pointer to the response (it is always non-NULL).
158  * @param[in]  aContext   A pointer to application-specific context.
159  *
160  * The @p aError can have the following:
161  *
162  *  - OT_ERROR_NONE              A response was received successfully.
163  *  - OT_ERROR_ABORT             A DNS transaction was aborted by stack.
164  *  - OT_ERROR_RESPONSE_TIMEOUT  No DNS response has been received within timeout.
165  *
166  * If the server rejects the address resolution request the error code from server is mapped as follow:
167  *
168  *  - (0)  NOERROR   Success (no error condition)                    -> OT_ERROR_NONE
169  *  - (1)  FORMERR   Server unable to interpret due to format error  -> OT_ERROR_PARSE
170  *  - (2)  SERVFAIL  Server encountered an internal failure          -> OT_ERROR_FAILED
171  *  - (3)  NXDOMAIN  Name that ought to exist, does not exist        -> OT_ERROR_NOT_FOUND
172  *  - (4)  NOTIMP    Server does not support the query type (OpCode) -> OT_ERROR_NOT_IMPLEMENTED
173  *  - (5)  REFUSED   Server refused for policy/security reasons      -> OT_ERROR_SECURITY
174  *  - (6)  YXDOMAIN  Some name that ought not to exist, does exist   -> OT_ERROR_DUPLICATED
175  *  - (7)  YXRRSET   Some RRset that ought not to exist, does exist  -> OT_ERROR_DUPLICATED
176  *  - (8)  NXRRSET   Some RRset that ought to exist, does not exist  -> OT_ERROR_NOT_FOUND
177  *  - (9)  NOTAUTH   Service is not authoritative for zone           -> OT_ERROR_SECURITY
178  *  - (10) NOTZONE   A name is not in the zone                       -> OT_ERROR_PARSE
179  *  - (20) BADNAME   Bad name                                        -> OT_ERROR_PARSE
180  *  - (21) BADALG    Bad algorithm                                   -> OT_ERROR_SECURITY
181  *  - (22) BADTRUN   Bad truncation                                  -> OT_ERROR_PARSE
182  *  - Other response codes                                           -> OT_ERROR_FAILED
183  *
184  */
185 typedef void (*otDnsAddressCallback)(otError aError, const otDnsAddressResponse *aResponse, void *aContext);
186 
187 /**
188  * This function sends an address resolution DNS query for AAAA (IPv6) record(s) for a given host name.
189  *
190  * The @p aConfig can be NULL. In this case the default config (from `otDnsClientGetDefaultConfig()`) will be used as
191  * the config for this query. In a non-NULL @p aConfig, some of the fields can be left unspecified (value zero). The
192  * unspecified fields are then replaced by the values from the default config.
193  *
194  * @param[in]  aInstance        A pointer to an OpenThread instance.
195  * @param[in]  aHostName        The host name for which to query the address (MUST NOT be NULL).
196  * @param[in]  aCallback        A function pointer that shall be called on response reception or time-out.
197  * @param[in]  aContext         A pointer to arbitrary context information.
198  * @param[in]  aConfig          A pointer to the config to use for this query.
199  *
200  * @retval OT_ERROR_NONE          Query sent successfully. @p aCallback will be invoked to report the status.
201  * @retval OT_ERROR_NO_BUFS       Insufficient buffer to prepare and send query.
202  *
203  */
204 otError otDnsClientResolveAddress(otInstance *            aInstance,
205                                   const char *            aHostName,
206                                   otDnsAddressCallback    aCallback,
207                                   void *                  aContext,
208                                   const otDnsQueryConfig *aConfig);
209 
210 /**
211  * This function gets the full host name associated with an address resolution DNS response.
212  *
213  * This function MUST only be used from `otDnsAddressCallback`.
214  *
215  * @param[in]  aResponse         A pointer to the response.
216  * @param[out] aNameBuffer       A buffer to char array to output the full host name (MUST NOT be NULL).
217  * @param[in]  aNameBufferSize   The size of @p aNameBuffer.
218  *
219  * @retval OT_ERROR_NONE     The full host name was read successfully.
220  * @retval OT_ERROR_NO_BUFS  The name does not fit in @p aNameBuffer.
221  *
222  */
223 otError otDnsAddressResponseGetHostName(const otDnsAddressResponse *aResponse,
224                                         char *                      aNameBuffer,
225                                         uint16_t                    aNameBufferSize);
226 
227 /**
228  * This function gets an IPv6 address associated with an address resolution DNS response.
229  *
230  * This function MUST only be used from `otDnsAddressCallback`.
231  *
232  * The response may include multiple IPv6 address records. @p aIndex can be used to iterate through the list of
233  * addresses. Index zero gets the first address and so on. When we reach end of the list, `OT_ERROR_NOT_FOUND` is
234  * returned.
235  *
236  * @param[in]  aResponse     A pointer to the response.
237  * @param[in]  aIndex        The address record index to retrieve.
238  * @param[out] aAddress      A pointer to a IPv6 address to output the address (MUST NOT be NULL).
239  * @param[out] aTtl          A pointer to an `uint32_t` to output TTL for the address. It can be NULL if caller does not
240  *                           want to get the TTL.
241  *
242  * @retval OT_ERROR_NONE       The address was read successfully.
243  * @retval OT_ERROR_NOT_FOUND  No address record in @p aResponse at @p aIndex.
244  * @retval OT_ERROR_PARSE      Could not parse the records in the @p aResponse.
245  *
246  */
247 otError otDnsAddressResponseGetAddress(const otDnsAddressResponse *aResponse,
248                                        uint16_t                    aIndex,
249                                        otIp6Address *              aAddress,
250                                        uint32_t *                  aTtl);
251 
252 /**
253  * This type is an opaque representation of a response to a browse (service instance enumeration) DNS query.
254  *
255  * Pointers to instance of this type are provided from callback `otDnsBrowseCallback`.
256  *
257  */
258 typedef struct otDnsBrowseResponse otDnsBrowseResponse;
259 
260 /**
261  * This function pointer is called when a DNS response is received for a browse (service instance enumeration) query.
262  *
263  * Within this callback the user can use `otDnsBrowseResponseGet{Item}()` functions along with the @p aResponse
264  * pointer to get more info about the response.
265  *
266  * The @p aResponse pointer can only be used within this callback and after returning from this function it will not
267  * stay valid, so the user MUST NOT retain the @p aResponse pointer for later use.
268  *
269  * @param[in]  aError     The result of the DNS transaction.
270  * @param[in]  aResponse  A pointer to the response (it is always non-NULL).
271  * @param[in]  aContext   A pointer to application-specific context.
272  *
273  * For the full list of possible values for @p aError, please see `otDnsAddressCallback()`.
274  *
275  */
276 typedef void (*otDnsBrowseCallback)(otError aError, const otDnsBrowseResponse *aResponse, void *aContext);
277 
278 /**
279  * This structure provides info for a DNS service instance.
280  *
281  */
282 typedef struct otDnsServiceInfo
283 {
284     uint32_t     mTtl;                ///< Service record TTL (in seconds).
285     uint16_t     mPort;               ///< Service port number.
286     uint16_t     mPriority;           ///< Service priority.
287     uint16_t     mWeight;             ///< Service weight.
288     char *       mHostNameBuffer;     ///< Buffer to output the service host name (can be NULL if not needed).
289     uint16_t     mHostNameBufferSize; ///< Size of `mHostNameBuffer`.
290     otIp6Address mHostAddress;        ///< The host IPv6 address. Set to all zero if not available.
291     uint32_t     mHostAddressTtl;     ///< The host address TTL.
292     uint8_t *    mTxtData;            ///< Buffer to output TXT data (can be NULL if not needed).
293     uint16_t     mTxtDataSize;        ///< On input, size of `mTxtData` buffer. On output `mTxtData` length.
294     uint32_t     mTxtDataTtl;         ///< The TXT data TTL.
295 } otDnsServiceInfo;
296 
297 /**
298  * This function sends a DNS browse (service instance enumeration) query for a given service name.
299  *
300  * This function is available when `OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE` is enabled.
301  *
302  * The @p aConfig can be NULL. In this case the default config (from `otDnsClientGetDefaultConfig()`) will be used as
303  * the config for this query. In a non-NULL @p aConfig, some of the fields can be left unspecified (value zero). The
304  * unspecified fields are then replaced by the values from the default config.
305  *
306  * @param[in]  aInstance        A pointer to an OpenThread instance.
307  * @param[in]  aServiceName     The service name to query for (MUST NOT be NULL).
308  * @param[in]  aCallback        A function pointer that shall be called on response reception or time-out.
309  * @param[in]  aContext         A pointer to arbitrary context information.
310  * @param[in]  aConfig          A pointer to the config to use for this query.
311  *
312  * @retval OT_ERROR_NONE        Query sent successfully. @p aCallback will be invoked to report the status.
313  * @retval OT_ERROR_NO_BUFS     Insufficient buffer to prepare and send query.
314  *
315  */
316 otError otDnsClientBrowse(otInstance *            aInstance,
317                           const char *            aServiceName,
318                           otDnsBrowseCallback     aCallback,
319                           void *                  aContext,
320                           const otDnsQueryConfig *aConfig);
321 
322 /**
323  * This function gets the service name associated with a DNS browse (service instance enumeration) response.
324  *
325  * This function MUST only be used from `otDnsBrowseCallback`.
326  *
327  * @param[in]  aResponse         A pointer to the response.
328  * @param[out] aNameBuffer       A buffer to char array to output the service name (MUST NOT be NULL).
329  * @param[in]  aNameBufferSize   The size of @p aNameBuffer.
330  *
331  * @retval OT_ERROR_NONE     The service name was read successfully.
332  * @retval OT_ERROR_NO_BUFS  The name does not fit in @p aNameBuffer.
333  *
334  */
335 otError otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse *aResponse,
336                                           char *                     aNameBuffer,
337                                           uint16_t                   aNameBufferSize);
338 
339 /**
340  * This function gets a service instance associated with a DNS browse (service instance enumeration) response.
341  *
342  * This function MUST only be used from `otDnsBrowseCallback`.
343  *
344  * The response may include multiple service instance records. @p aIndex can be used to iterate through the list. Index
345  * zero gives the the first record. When we reach end of the list, `OT_ERROR_NOT_FOUND` is returned.
346  *
347  * Note that this function gets the service instance label and not the full service instance name which is of the form
348  * `<Instance>.<Service>.<Domain>`.
349  *
350  * @param[in]  aResponse          A pointer to the response.
351  * @param[in]  aIndex             The service instance record index to retrieve.
352  * @param[out] aLabelBuffer       A buffer to char array to output the service instance label (MUST NOT be NULL).
353  * @param[in]  aLabelBufferSize   The size of @p aLabelBuffer.
354  *
355  * @retval OT_ERROR_NONE          The service instance was read successfully.
356  * @retval OT_ERROR_NO_BUFS       The name does not fit in @p aNameBuffer.
357  * @retval OT_ERROR_NOT_FOUND     No service instance record in @p aResponse at @p aIndex.
358  * @retval OT_ERROR_PARSE         Could not parse the records in the @p aResponse.
359  *
360  */
361 otError otDnsBrowseResponseGetServiceInstance(const otDnsBrowseResponse *aResponse,
362                                               uint16_t                   aIndex,
363                                               char *                     aLabelBuffer,
364                                               uint8_t                    aLabelBufferSize);
365 
366 /**
367  * This function gets info for a service instance from a DNS browse (service instance enumeration) response.
368  *
369  * This function MUST only be used from `otDnsBrowseCallback`.
370  *
371  * A browse DNS response should include the SRV, TXT, and AAAA records for the service instances that are enumerated
372  * (note that it is a SHOULD and not a MUST requirement). This function tries to retrieve this info for a given service
373  * instance when available.
374  *
375  * - If no matching SRV record is found in @p aResponse, `OT_ERROR_NOT_FOUND` is returned.
376  * - If a matching SRV record is found in @p aResponse, @p aServiceInfo is updated and `OT_ERROR_NONE` is returned.
377  * - If no matching TXT record is found in @p aResponse, `mTxtDataSize` in @p aServiceInfo is set to zero.
378  * - If no matching AAAA record is found in @p aResponse, `mHostAddress is set to all zero or unspecified address.
379  * - If there are multiple AAAA records for the host name in @p aResponse, `mHostAddress` is set to the first one. The
380  *   other addresses can be retrieved using `otDnsBrowseResponseGetHostAddress()`.
381  *
382  * @param[in]  aResponse          A pointer to the response.
383  * @param[in]  aInstanceLabel     The service instance label (MUST NOT be NULL).
384  * @param[out] aServiceInfo       A `ServiceInfo` to output the service instance information (MUST NOT be NULL).
385  *
386  * @retval OT_ERROR_NONE          The service instance info was read. @p aServiceInfo is updated.
387  * @retval OT_ERROR_NOT_FOUND     Could not find a matching SRV record for @p aInstanceLabel.
388  * @retval OT_ERROR_NO_BUFS       The host name and/or TXT data could not fit in the given buffers.
389  * @retval OT_ERROR_PARSE         Could not parse the records in the @p aResponse.
390  *
391  */
392 otError otDnsBrowseResponseGetServiceInfo(const otDnsBrowseResponse *aResponse,
393                                           const char *               aInstanceLabel,
394                                           otDnsServiceInfo *         aServiceInfo);
395 
396 /**
397  * This function gets the host IPv6 address from a DNS browse (service instance enumeration) response.
398  *
399  * This function MUST only be used from `otDnsBrowseCallback`.
400  *
401  * The response can include zero or more IPv6 address records. @p aIndex can be used to iterate through the list of
402  * addresses. Index zero gets the first address and so on. When we reach end of the list, `OT_ERROR_NOT_FOUND` is
403  * returned.
404  *
405  * @param[in]  aResponse     A pointer to the response.
406  * @param[in]  aHostName     The host name to get the address (MUST NOT be NULL).
407  * @param[in]  aIndex        The address record index to retrieve.
408  * @param[out] aAddress      A pointer to a IPv6 address to output the address (MUST NOT be NULL).
409  * @param[out] aTtl          A pointer to an `uint32_t` to output TTL for the address. It can be NULL if caller does
410  *                           not want to get the TTL.
411  *
412  * @retval OT_ERROR_NONE       The address was read successfully.
413  * @retval OT_ERROR_NOT_FOUND  No address record for @p aHostname in @p aResponse at @p aIndex.
414  * @retval OT_ERROR_PARSE      Could not parse the records in the @p aResponse.
415  *
416  */
417 otError otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse *aResponse,
418                                           const char *               aHostName,
419                                           uint16_t                   aIndex,
420                                           otIp6Address *             aAddress,
421                                           uint32_t *                 aTtl);
422 
423 /**
424  * This type is an opaque representation of a response to a service instance resolution DNS query.
425  *
426  * Pointers to instance of this type are provided from callback `otDnsAddressCallback`.
427  *
428  */
429 typedef struct otDnsServiceResponse otDnsServiceResponse;
430 
431 /**
432  * This function pointer is called when a DNS response is received for a service instance resolution query.
433  *
434  * Within this callback the user can use `otDnsServiceResponseGet{Item}()` functions along with the @p aResponse
435  * pointer to get more info about the response.
436  *
437  * The @p aResponse pointer can only be used within this callback and after returning from this function it will not
438  * stay valid, so the user MUST NOT retain the @p aResponse pointer for later use.
439  *
440  * @param[in]  aError     The result of the DNS transaction.
441  * @param[in]  aResponse  A pointer to the response (it is always non-NULL).
442  * @param[in]  aContext   A pointer to application-specific context.
443  *
444  * For the full list of possible values for @p aError, please see `otDnsAddressCallback()`.
445  *
446  */
447 typedef void (*otDnsServiceCallback)(otError aError, const otDnsServiceResponse *aResponse, void *aContext);
448 
449 /**
450  * This function sends a DNS service instance resolution query for a given service instance.
451  *
452  * This function is available when `OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE` is enabled.
453  *
454  * The @p aConfig can be NULL. In this case the default config (from `otDnsClientGetDefaultConfig()`) will be used as
455  * the config for this query. In a non-NULL @p aConfig, some of the fields can be left unspecified (value zero). The
456  * unspecified fields are then replaced by the values from the default config.
457  *
458  * @param[in]  aInstance          A pointer to an OpenThread instance.
459  * @param[in]  aInstanceLabel     The service instance label.
460  * @param[in]  aServiceName       The service name (together with @p aInstanceLabel form full instance name).
461  * @param[in]  aCallback          A function pointer that shall be called on response reception or time-out.
462  * @param[in]  aContext           A pointer to arbitrary context information.
463  * @param[in]  aConfig            A pointer to the config to use for this query.
464  *
465  * @retval OT_ERROR_NONE          Query sent successfully. @p aCallback will be invoked to report the status.
466  * @retval OT_ERROR_NO_BUFS       Insufficient buffer to prepare and send query.
467  * @retval OT_ERROR_INVALID_ARGS  @p aInstanceLabel is NULL.
468  *
469  */
470 otError otDnsClientResolveService(otInstance *            aInstance,
471                                   const char *            aInstanceLabel,
472                                   const char *            aServiceName,
473                                   otDnsServiceCallback    aCallback,
474                                   void *                  aContext,
475                                   const otDnsQueryConfig *aConfig);
476 
477 /**
478  * This function gets the service instance name associated with a DNS service instance resolution response.
479  *
480  * This function MUST only be used from `otDnsServiceCallback`.
481  *
482  * @param[in]  aResponse         A pointer to the response.
483  * @param[out] aLabelBuffer      A buffer to char array to output the service instance label (MUST NOT be NULL).
484  * @param[in]  aLabelBufferSize  The size of @p aLabelBuffer.
485  * @param[out] aNameBuffer       A buffer to char array to output the rest of service name (can be NULL if user is
486  *                               not interested in getting the name.
487  * @param[in]  aNameBufferSize   The size of @p aNameBuffer.
488  *
489  * @retval OT_ERROR_NONE     The service name was read successfully.
490  * @retval OT_ERROR_NO_BUFS  Either the label or name does not fit in the given buffers.
491  *
492  */
493 otError otDnsServiceResponseGetServiceName(const otDnsServiceResponse *aResponse,
494                                            char *                      aLabelBuffer,
495                                            uint8_t                     aLabelBufferSize,
496                                            char *                      aNameBuffer,
497                                            uint16_t                    aNameBufferSize);
498 
499 /**
500  * This function gets info for a service instance from a DNS service instance resolution response.
501  *
502  * This function MUST only be used from `otDnsServiceCallback`.
503  *
504  * - If no matching SRV record is found in @p aResponse, `OT_ERROR_NOT_FOUND` is returned.
505  * - If a matching SRV record is found in @p aResponse, @p aServiceInfo is updated and `OT_ERROR_NONE` is returned.
506  * - If no matching TXT record is found in @p aResponse, `mTxtDataSize` in @p aServiceInfo is set to zero.
507  * - If no matching AAAA record is found in @p aResponse, `mHostAddress is set to all zero or unspecified address.
508  * - If there are multiple AAAA records for the host name in @p aResponse, `mHostAddress` is set to the first one. The
509  *   other addresses can be retrieved using `otDnsServiceResponseGetHostAddress()`.
510  *
511  * @param[in]  aResponse          A pointer to the response.
512  * @param[out] aServiceInfo       A `ServiceInfo` to output the service instance information (MUST NOT be NULL).
513  *
514  * @retval OT_ERROR_NONE          The service instance info was read. @p aServiceInfo is updated.
515  * @retval OT_ERROR_NOT_FOUND     Could not find a matching SRV record in @p aResponse.
516  * @retval OT_ERROR_NO_BUFS       The host name and/or TXT data could not fit in the given buffers.
517  * @retval OT_ERROR_PARSE         Could not parse the records in the @p aResponse.
518  *
519  */
520 otError otDnsServiceResponseGetServiceInfo(const otDnsServiceResponse *aResponse, otDnsServiceInfo *aServiceInfo);
521 
522 /**
523  * This function gets the host IPv6 address from a DNS service instance resolution response.
524  *
525  * This function MUST only be used from `otDnsServiceCallback`.
526  *
527  * The response can include zero or more IPv6 address records. @p aIndex can be used to iterate through the list of
528  * addresses. Index zero gets the first address and so on. When we reach end of the list, `OT_ERROR_NOT_FOUND` is
529  * returned.
530  *
531  * @param[in]  aResponse     A pointer to the response.
532  * @param[in]  aHostName     The host name to get the address (MUST NOT be NULL).
533  * @param[in]  aIndex        The address record index to retrieve.
534  * @param[out] aAddress      A pointer to a IPv6 address to output the address (MUST NOT be NULL).
535  * @param[out] aTtl          A pointer to an `uint32_t` to output TTL for the address. It can be NULL if caller does
536  *                           not want to get the TTL.
537  *
538  * @retval OT_ERROR_NONE       The address was read successfully.
539  * @retval OT_ERROR_NOT_FOUND  No address record for @p aHostname in @p aResponse at @p aIndex.
540  * @retval OT_ERROR_PARSE      Could not parse the records in the @p aResponse.
541  *
542  */
543 otError otDnsServiceResponseGetHostAddress(const otDnsServiceResponse *aResponse,
544                                            const char *                aHostName,
545                                            uint16_t                    aIndex,
546                                            otIp6Address *              aAddress,
547                                            uint32_t *                  aTtl);
548 
549 /**
550  * @}
551  *
552  */
553 
554 #ifdef __cplusplus
555 } // extern "C"
556 #endif
557 
558 #endif // OPENTHREAD_DNS_CLIENT_H_
559