mirror of
https://github.com/espressif/openthread.git
synced 2026-08-29 21:39:54 +00:00
[dns-client] add 'QueryConfig' and default config (#6172)
This commit adds a new structure `otDnsQueryConfig` which represents a
DNS query configuration defining a set of parameters:
- The DNS server socket address (IPv6 address and port number).
- Response timeout interval (in msec).
- Maximum number of transmit attempts before reporting failure.
- "Recursion desired" flag (if server can resolve recursively).
There is a default query config used by DNS client. When OpenThread
stack starts, the default DNS query config is determined from a set of
OpenThread config options defined in `config/dns_clinet.h` such as
`OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS`, etc. New
APIs `otDnsClient{Get}/{Set}DefaultConfig()` are added to allow user
to get/set the current default config during operation.
When issuing a browse, address, or service resolution query, the user
can choose not to provide a query config in which case the current
default config will be used. The user can also provide a full or
partially specified `otDnsQueryConfig` instance, e.g., some of some of
the fields may be left unspecified (value zero). The corresponding
value from the current default config is then used for any unspecified
field in the given config.
This commit also updates the CLI to add `dns config` sub-command and
update other `dns` commands to follow the new API model.
This commit is contained in:
@@ -55,9 +55,65 @@ extern "C" {
|
||||
*
|
||||
*/
|
||||
|
||||
#define OT_DNS_DEFAULT_SERVER_PORT 53 ///< The default DNS Server port.
|
||||
/**
|
||||
* This enumeration type represents the "Recursion Desired" (RD) flag in an `otDnsQueryConfig`.
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
OT_DNS_FLAG_UNSPECIFIED = 0, ///< Indicates the flag is not specified.
|
||||
OT_DNS_FLAG_RECURSION_DESIRED = 1, ///< Indicates DNS name server can resolve the query recursively.
|
||||
OT_DNS_FLAG_NO_RECURSION = 2, ///< Indicates DNS name server can not resolve the query recursively.
|
||||
} otDnsRecursionFlag;
|
||||
|
||||
#define OT_DNS_DEFAULT_SERVER_IP "2001:4860:4860::8888" ///< Defines default DNS Server address - Google DNS.
|
||||
/**
|
||||
* This structure represents a DNS query configuration.
|
||||
*
|
||||
* Any of the fields in this structure can be set to zero to indicate that it is not specified. How the unspecified
|
||||
* fields are treated is determined by the function which uses the instance of `otDnsQueryConfig`.
|
||||
*
|
||||
*/
|
||||
typedef struct otDnsQueryConfig
|
||||
{
|
||||
otSockAddr mServerSockAddr; ///< Server address (IPv6 address/port). All zero or zero port for unspecified.
|
||||
uint32_t mResponseTimeout; ///< Wait time (in msec) to rx response. Zero indicates unspecified value.
|
||||
uint8_t mMaxTxAttempts; ///< Maximum tx attempts before reporting failure. Zero for unspecified value.
|
||||
otDnsRecursionFlag mRecursionFlag; ///< Indicates whether the server can resolve the query recursively or not.
|
||||
} otDnsQueryConfig;
|
||||
|
||||
/**
|
||||
* This function gets the current default query config used by DNS client.
|
||||
*
|
||||
* When OpenThread stack starts, the default DNS query config is determined from a set of OT config options such as
|
||||
* `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS`, `_DEFAULT_SERVER_PORT`, `_DEFAULT_RESPONSE_TIMEOUT`, etc.
|
||||
* (see `config/dns_clinet.h` for all related config options).
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
*
|
||||
* @returns A pointer to the current default config being used by DNS client.
|
||||
*
|
||||
*/
|
||||
const otDnsQueryConfig *otDnsClientGetDefaultConfig(otInstance *aInstance);
|
||||
|
||||
/**
|
||||
* This function sets the default query config on DNS client.
|
||||
*
|
||||
* @note Any ongoing query will continue to use the config from when it was started. The new default config will be
|
||||
* used for any future DNS queries.
|
||||
*
|
||||
* The @p aConfig can be NULL. In this case the default config will be set to the defaults from OT config options
|
||||
* `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_{}`. This resets the default query config back to to the config when the
|
||||
* OpenThread stack starts.
|
||||
*
|
||||
* In a non-NULL @p aConfig, caller can choose to leave some of the fields in `otDnsQueryConfig` instance unspecified
|
||||
* (value zero). The unspecified fields are replaced by the corresponding OT config option definitions
|
||||
* `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_{}` to form the default query config.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[in] aConfig A pointer to the new query config to use as default.
|
||||
*
|
||||
*/
|
||||
void otDnsClientSetDefaultConfig(otInstance *aInstance, const otDnsQueryConfig *aConfig);
|
||||
|
||||
/**
|
||||
* This type is an opaque representation of a response to an address resolution DNS query.
|
||||
@@ -110,23 +166,25 @@ typedef void (*otDnsAddressCallback)(otError aError, const otDnsAddressResponse
|
||||
/**
|
||||
* This function sends an address resolution DNS query for AAAA (IPv6) record(s) for a given host name.
|
||||
*
|
||||
* The @p aConfig can be NULL. In this case the default config (from `otDnsClientGetDefaultConfig()`) will be used as
|
||||
* the config for this query. In a non-NULL @p aConfig, some of the fields can be left unspecified (value zero). The
|
||||
* unspecified fields are then replaced by the values from the default config.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[in] aServerSockAddr A pointer to the server socket address.
|
||||
* @param[in] aHostName The host name for which to query the address (MUST NOT be NULL).
|
||||
* @param[in] aNoRecursion Indicates whether name server can resolve the query recursively or not.
|
||||
* @param[in] aCallback A function pointer that shall be called on response reception or time-out.
|
||||
* @param[in] aContext A pointer to arbitrary context information.
|
||||
* @param[in] aConfig A pointer to the config to use for this query.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Query sent successfully. @p aCallback will be invoked to report the status.
|
||||
* @retval OT_ERROR_NO_BUFS Insufficient buffer to prepare and send query.
|
||||
*
|
||||
*/
|
||||
otError otDnsClientResolveAddress(otInstance * aInstance,
|
||||
const otSockAddr * aServerSockAddr,
|
||||
const char * aHostName,
|
||||
bool aNoRecursion,
|
||||
otDnsAddressCallback aCallback,
|
||||
void * aContext);
|
||||
otError otDnsClientResolveAddress(otInstance * aInstance,
|
||||
const char * aHostName,
|
||||
otDnsAddressCallback aCallback,
|
||||
void * aContext,
|
||||
const otDnsQueryConfig *aConfig);
|
||||
|
||||
/**
|
||||
* This function gets the full host name associated with an address resolution DNS response.
|
||||
@@ -220,21 +278,25 @@ typedef struct otDnsServiceInfo
|
||||
*
|
||||
* This function is available when `OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE` is enabled.
|
||||
*
|
||||
* The @p aConfig can be NULL. In this case the default config (from `otDnsClientGetDefaultConfig()`) will be used as
|
||||
* the config for this query. In a non-NULL @p aConfig, some of the fields can be left unspecified (value zero). The
|
||||
* unspecified fields are then replaced by the values from the default config.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[in] aServerSockAddr A pointer to the server socket address.
|
||||
* @param[in] aServiceName The service name to query for (MUST NOT be NULL).
|
||||
* @param[in] aCallback A function pointer that shall be called on response reception or time-out.
|
||||
* @param[in] aContext A pointer to arbitrary context information.
|
||||
* @param[in] aConfig A pointer to the config to use for this query.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Query sent successfully. @p aCallback will be invoked to report the status.
|
||||
* @retval OT_ERROR_NO_BUFS Insufficient buffer to prepare and send query.
|
||||
*
|
||||
*/
|
||||
otError otDnsClientBrowse(otInstance * aInstance,
|
||||
const otSockAddr * aServerSockAddr,
|
||||
const char * aServiceName,
|
||||
otDnsBrowseCallback aCallback,
|
||||
void * aContext);
|
||||
otError otDnsClientBrowse(otInstance * aInstance,
|
||||
const char * aServiceName,
|
||||
otDnsBrowseCallback aCallback,
|
||||
void * aContext,
|
||||
const otDnsQueryConfig *aConfig);
|
||||
|
||||
/**
|
||||
* This function gets the service name associated with a DNS browse (service instance enumeration) response.
|
||||
@@ -368,23 +430,27 @@ typedef void (*otDnsServiceCallback)(otError aError, const otDnsServiceResponse
|
||||
*
|
||||
* This function is available when `OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE` is enabled.
|
||||
*
|
||||
* The @p aConfig can be NULL. In this case the default config (from `otDnsClientGetDefaultConfig()`) will be used as
|
||||
* the config for this query. In a non-NULL @p aConfig, some of the fields can be left unspecified (value zero). The
|
||||
* unspecified fields are then replaced by the values from the default config.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[in] aServerSockAddr A pointer to the server socket address.
|
||||
* @param[in] aInstanceLabel The service instance label.
|
||||
* @param[in] aServiceName The service name (together with @p aInstanceLabel form full instance name).
|
||||
* @param[in] aCallback A function pointer that shall be called on response reception or time-out.
|
||||
* @param[in] aContext A pointer to arbitrary context information.
|
||||
* @param[in] aConfig A pointer to the config to use for this query.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Query sent successfully. @p aCallback will be invoked to report the status.
|
||||
* @retval OT_ERROR_NO_BUFS Insufficient buffer to prepare and send query.
|
||||
*
|
||||
*/
|
||||
otError otDnsClientResolveService(otInstance * aInstance,
|
||||
const otSockAddr * aServerSockAddr,
|
||||
const char * aInstanceLabel,
|
||||
const char * aServiceName,
|
||||
otDnsServiceCallback aCallback,
|
||||
void * aContext);
|
||||
otError otDnsClientResolveService(otInstance * aInstance,
|
||||
const char * aInstanceLabel,
|
||||
const char * aServiceName,
|
||||
otDnsServiceCallback aCallback,
|
||||
void * aContext,
|
||||
const otDnsQueryConfig *aConfig);
|
||||
|
||||
/**
|
||||
* This function gets the service instance name associated with a DNS service instance resolution response.
|
||||
|
||||
@@ -53,7 +53,7 @@ extern "C" {
|
||||
* @note This number versions both OpenThread platform and user APIs.
|
||||
*
|
||||
*/
|
||||
#define OPENTHREAD_API_VERSION (74)
|
||||
#define OPENTHREAD_API_VERSION (76)
|
||||
|
||||
/**
|
||||
* @addtogroup api-instance
|
||||
|
||||
+56
-7
@@ -41,7 +41,7 @@ Done
|
||||
- [delaytimermin](#delaytimermin)
|
||||
- [diag](#diag)
|
||||
- [discover](#discover-channel)
|
||||
- [dns](#dns-resolve-hostname-dns-server-ip-dns-server-port)
|
||||
- [dns](#dns-config)
|
||||
- [domainname](#domainname)
|
||||
- [dua](#dua-iid)
|
||||
- [eidcache](#eidcache)
|
||||
@@ -747,22 +747,69 @@ Perform an MLE Discovery operation.
|
||||
Done
|
||||
```
|
||||
|
||||
### dns resolve \<hostname\> \[DNS server IP\] \[DNS server port\]
|
||||
### dns config
|
||||
|
||||
Send DNS Query to obtain IPv6 address for given hostname. The latter two parameters have following default values:
|
||||
Get the default query config used by DNS client.
|
||||
|
||||
- DNS server IP: 2001:4860:4860::8888 (Google DNS Server)
|
||||
- DNS server port: 53
|
||||
The config includes the server IPv6 address and port, response timeout in msec (wait time to rx response), maximum tx attempts before reporting failure, boolean flag to indicate whether the server can resolve the query recursively or not.
|
||||
|
||||
```bash
|
||||
> dns config
|
||||
Server: [fd00:0:0:0:0:0:0:1]:1234
|
||||
ResponseTimeout: 5000 ms
|
||||
MaxTxAttempts: 2
|
||||
RecursionDesired: no
|
||||
Done
|
||||
>
|
||||
```
|
||||
|
||||
### dns config \[DNS server IP\] \[DNS server port\] \[response timeout (ms)\] \[max tx attempts\] \[recursion desired (boolean)\]
|
||||
|
||||
Set the default query config.
|
||||
|
||||
```bash
|
||||
> dns config fd00::1 1234 5000 2 0
|
||||
Done
|
||||
|
||||
> dns config
|
||||
Server: [fd00:0:0:0:0:0:0:1]:1234
|
||||
ResponseTimeout: 5000 ms
|
||||
MaxTxAttempts: 2
|
||||
RecursionDesired: no
|
||||
Done
|
||||
```
|
||||
|
||||
We can leave some of the fields as unspecified (or use value zero). The unspecified fields are replaced by the corresponding OT config option definitions `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_{}` to form the default query config.
|
||||
|
||||
```bash
|
||||
> dns config fd00::2
|
||||
Done
|
||||
|
||||
> dns config
|
||||
Server: [fd00:0:0:0:0:0:0:2]:53
|
||||
ResponseTimeout: 3000 ms
|
||||
MaxTxAttempts: 3
|
||||
RecursionDesired: yes
|
||||
Done
|
||||
```
|
||||
|
||||
### dns resolve \<hostname\> \[DNS server IP\] \[DNS server port\] \[response timeout (ms)\] \[max tx attempts\] \[recursion desired (boolean)\]
|
||||
|
||||
Send DNS Query to obtain IPv6 address for given hostname.
|
||||
|
||||
The parameters after `hostname` are optional. Any unspecified (or zero) value for these optional parameters is replaced by the value from the current default config (`dns config`).
|
||||
|
||||
```bash
|
||||
> dns resolve ipv6.google.com
|
||||
> DNS response for ipv6.google.com - 2a00:1450:401b:801:0:0:0:200e TTL: 300
|
||||
```
|
||||
|
||||
### dns browse \<service-name\> \[DNS server IP\] \[DNS server port\]
|
||||
### dns browse \<service-name\> \[DNS server IP\] \[DNS server port\] \[response timeout (ms)\] \[max tx attempts\] \[recursion desired (boolean)\]
|
||||
|
||||
Send a browse (service instance enumeration) DNS query to get the list of services for given service-name.
|
||||
|
||||
The parameters after `service-name` are optional. Any unspecified (or zero) value for these optional parameters is replaced by the value from the current default config (`dns config`).
|
||||
|
||||
```bash
|
||||
> dns browse _service._udp.example.com
|
||||
DNS browse response for _service._udp.example.com.
|
||||
@@ -779,10 +826,12 @@ instance2
|
||||
Done
|
||||
```
|
||||
|
||||
### dns service \<service-instance-label\> \<service-name\> \[DNS server IP\] \[DNS server port\]
|
||||
### dns service \<service-instance-label\> \<service-name\> \[DNS server IP\] \[DNS server port\] \[response timeout (ms)\] \[max tx attempts\] \[recursion desired (boolean)\]
|
||||
|
||||
Send a service instance resolution DNS query for a given service instance. Service instance label is provided first, followed by the service name (note that service instance label can contain dot '.' character).
|
||||
|
||||
The parameters after `service-name` are optional. Any unspecified (or zero) value for these optional parameters is replaced by the value from the current default config (`dns config`).
|
||||
|
||||
### domainname
|
||||
|
||||
Get the Thread Domain Name for Thread 1.2 device.
|
||||
|
||||
+60
-39
@@ -1336,37 +1336,36 @@ exit:
|
||||
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
|
||||
|
||||
otError Interpreter::GetDnsServerAddress(uint8_t aArgsLength,
|
||||
char * aArgs[],
|
||||
otSockAddr &aAddress,
|
||||
uint8_t aStartArgsIndex)
|
||||
otError Interpreter::GetDnsConfig(uint8_t aArgsLength,
|
||||
char * aArgs[],
|
||||
otDnsQueryConfig *&aConfig,
|
||||
uint8_t aStartArgsIndex)
|
||||
{
|
||||
// This method gets the optional server address from given `aArgs`
|
||||
// after the `aStartArgsIndex`. The format `[server IPv6 address]
|
||||
// [server port]`.
|
||||
// This method gets the optional config from given `aArgs` after the
|
||||
// `aStartArgsIndex`. The format: `[server IPv6 address] [server
|
||||
// port] [timeout] [max tx attempt] [recursion desired]`.
|
||||
|
||||
otError error = OT_ERROR_NONE;
|
||||
bool recursionDesired;
|
||||
|
||||
VerifyOrExit(aArgsLength >= aStartArgsIndex, error = OT_ERROR_INVALID_ARGS);
|
||||
memset(aConfig, 0, sizeof(otDnsQueryConfig));
|
||||
|
||||
if (aArgsLength > aStartArgsIndex)
|
||||
{
|
||||
SuccessOrExit(error = ParseAsIp6Address(aArgs[aStartArgsIndex], aAddress.mAddress));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use IPv6 address of default DNS server.
|
||||
SuccessOrExit(error = otIp6AddressFromString(OT_DNS_DEFAULT_SERVER_IP, &aAddress.mAddress));
|
||||
}
|
||||
VerifyOrExit(aArgsLength > aStartArgsIndex, aConfig = nullptr);
|
||||
|
||||
if (aArgsLength > aStartArgsIndex + 1)
|
||||
{
|
||||
SuccessOrExit(error = ParseAsUint16(aArgs[aStartArgsIndex + 1], aAddress.mPort));
|
||||
}
|
||||
else
|
||||
{
|
||||
aAddress.mPort = OT_DNS_DEFAULT_SERVER_PORT;
|
||||
}
|
||||
SuccessOrExit(error = ParseAsIp6Address(aArgs[aStartArgsIndex], aConfig->mServerSockAddr.mAddress));
|
||||
|
||||
VerifyOrExit(aArgsLength > aStartArgsIndex + 1);
|
||||
SuccessOrExit(error = ParseAsUint16(aArgs[aStartArgsIndex + 1], aConfig->mServerSockAddr.mPort));
|
||||
|
||||
VerifyOrExit(aArgsLength > aStartArgsIndex + 2);
|
||||
SuccessOrExit(error = ParseAsUint32(aArgs[aStartArgsIndex + 2], aConfig->mResponseTimeout));
|
||||
|
||||
VerifyOrExit(aArgsLength > aStartArgsIndex + 3);
|
||||
SuccessOrExit(error = ParseAsUint8(aArgs[aStartArgsIndex + 3], aConfig->mMaxTxAttempts));
|
||||
|
||||
VerifyOrExit(aArgsLength > aStartArgsIndex + 4);
|
||||
SuccessOrExit(error = ParseAsBool(aArgs[aStartArgsIndex + 4], recursionDesired));
|
||||
aConfig->mRecursionFlag = recursionDesired ? OT_DNS_FLAG_RECURSION_DESIRED : OT_DNS_FLAG_NO_RECURSION;
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -1374,29 +1373,53 @@ exit:
|
||||
|
||||
otError Interpreter::ProcessDns(uint8_t aArgsLength, char *aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otSockAddr serverSockAddr;
|
||||
otError error = OT_ERROR_NONE;
|
||||
otDnsQueryConfig queryConfig;
|
||||
otDnsQueryConfig *config = &queryConfig;
|
||||
|
||||
VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
if (strcmp(aArgs[0], "resolve") == 0)
|
||||
if (strcmp(aArgs[0], "config") == 0)
|
||||
{
|
||||
SuccessOrExit(error = GetDnsServerAddress(aArgsLength, aArgs, serverSockAddr, 2));
|
||||
SuccessOrExit(error = otDnsClientResolveAddress(mInstance, &serverSockAddr, aArgs[1], /* aNoRecursion */ false,
|
||||
&Interpreter::HandleDnsAddressResponse, this));
|
||||
if (aArgsLength == 1)
|
||||
{
|
||||
const otDnsQueryConfig *defaultConfig = otDnsClientGetDefaultConfig(mInstance);
|
||||
|
||||
OutputFormat("Server: [");
|
||||
OutputIp6Address(defaultConfig->mServerSockAddr.mAddress);
|
||||
OutputLine("]:%d", defaultConfig->mServerSockAddr.mPort);
|
||||
OutputLine("ResponseTimeout: %u ms", defaultConfig->mResponseTimeout);
|
||||
OutputLine("MaxTxAttempts: %u", defaultConfig->mMaxTxAttempts);
|
||||
OutputLine("RecursionDesired: %s",
|
||||
(defaultConfig->mRecursionFlag == OT_DNS_FLAG_RECURSION_DESIRED) ? "yes" : "no");
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 1));
|
||||
otDnsClientSetDefaultConfig(mInstance, config);
|
||||
}
|
||||
}
|
||||
else if (strcmp(aArgs[0], "resolve") == 0)
|
||||
{
|
||||
SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 2));
|
||||
SuccessOrExit(error = otDnsClientResolveAddress(mInstance, aArgs[1], &Interpreter::HandleDnsAddressResponse,
|
||||
this, config));
|
||||
error = OT_ERROR_PENDING;
|
||||
}
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
else if (strcmp(aArgs[0], "browse") == 0)
|
||||
{
|
||||
SuccessOrExit(error = GetDnsServerAddress(aArgsLength, aArgs, serverSockAddr, 2));
|
||||
SuccessOrExit(error = otDnsClientBrowse(mInstance, &serverSockAddr, aArgs[1],
|
||||
&Interpreter::HandleDnsBrowseResponse, this));
|
||||
SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 2));
|
||||
SuccessOrExit(error =
|
||||
otDnsClientBrowse(mInstance, aArgs[1], &Interpreter::HandleDnsBrowseResponse, this, config));
|
||||
error = OT_ERROR_PENDING;
|
||||
}
|
||||
else if (strcmp(aArgs[0], "service") == 0)
|
||||
{
|
||||
SuccessOrExit(error = GetDnsServerAddress(aArgsLength, aArgs, serverSockAddr, 3));
|
||||
SuccessOrExit(error = otDnsClientResolveService(mInstance, &serverSockAddr, aArgs[1], aArgs[2],
|
||||
&Interpreter::HandleDnsServiceResponse, this));
|
||||
SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 3));
|
||||
SuccessOrExit(error = otDnsClientResolveService(mInstance, aArgs[1], aArgs[2],
|
||||
&Interpreter::HandleDnsServiceResponse, this, config));
|
||||
error = OT_ERROR_PENDING;
|
||||
}
|
||||
#endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
else
|
||||
@@ -1404,8 +1427,6 @@ otError Interpreter::ProcessDns(uint8_t aArgsLength, char *aArgs[])
|
||||
ExitNow(error = OT_ERROR_INVALID_COMMAND);
|
||||
}
|
||||
|
||||
error = OT_ERROR_PENDING;
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
+1
-1
@@ -558,7 +558,7 @@ private:
|
||||
#endif
|
||||
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
|
||||
otError GetDnsServerAddress(uint8_t aArgsLength, char *aArgs[], otSockAddr &aAddress, uint8_t aStartArgsIndex);
|
||||
otError GetDnsConfig(uint8_t aArgsLength, char *aArgs[], otDnsQueryConfig *&aConfig, uint8_t aStartArgsIndex);
|
||||
static void HandleDnsAddressResponse(otError aError, const otDnsAddressResponse *aResponse, void *aContext);
|
||||
void HandleDnsAddressResponse(otError aError, const otDnsAddressResponse *aResponse);
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
|
||||
+43
-23
@@ -43,17 +43,37 @@ using namespace ot;
|
||||
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
|
||||
|
||||
otError otDnsClientResolveAddress(otInstance * aInstance,
|
||||
const otSockAddr * aServerSockAddr,
|
||||
const char * aHostName,
|
||||
bool aNoRecursion,
|
||||
otDnsAddressCallback aCallback,
|
||||
void * aContext)
|
||||
const otDnsQueryConfig *otDnsClientGetDefaultConfig(otInstance *aInstance)
|
||||
{
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
return instance.Get<Dns::Client>().ResolveAddress(*static_cast<const Ip6::SockAddr *>(aServerSockAddr), aHostName,
|
||||
aNoRecursion, aCallback, aContext);
|
||||
return &instance.Get<Dns::Client>().GetDefaultConfig();
|
||||
}
|
||||
|
||||
void otDnsClientSetDefaultConfig(otInstance *aInstance, const otDnsQueryConfig *aConfig)
|
||||
{
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
if (aConfig != nullptr)
|
||||
{
|
||||
instance.Get<Dns::Client>().SetDefaultConfig(*static_cast<const Dns::Client::QueryConfig *>(aConfig));
|
||||
}
|
||||
else
|
||||
{
|
||||
instance.Get<Dns::Client>().ResetDefaultConfig();
|
||||
}
|
||||
}
|
||||
|
||||
otError otDnsClientResolveAddress(otInstance * aInstance,
|
||||
const char * aHostName,
|
||||
otDnsAddressCallback aCallback,
|
||||
void * aContext,
|
||||
const otDnsQueryConfig *aConfig)
|
||||
{
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
return instance.Get<Dns::Client>().ResolveAddress(aHostName, aCallback, aContext,
|
||||
static_cast<const Dns::Client::QueryConfig *>(aConfig));
|
||||
}
|
||||
|
||||
otError otDnsAddressResponseGetHostName(const otDnsAddressResponse *aResponse,
|
||||
@@ -78,16 +98,16 @@ otError otDnsAddressResponseGetAddress(const otDnsAddressResponse *aResponse,
|
||||
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
|
||||
otError otDnsClientBrowse(otInstance * aInstance,
|
||||
const otSockAddr * aServerSockAddr,
|
||||
const char * aServiceName,
|
||||
otDnsBrowseCallback aCallback,
|
||||
void * aContext)
|
||||
otError otDnsClientBrowse(otInstance * aInstance,
|
||||
const char * aServiceName,
|
||||
otDnsBrowseCallback aCallback,
|
||||
void * aContext,
|
||||
const otDnsQueryConfig *aConfig)
|
||||
{
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
return instance.Get<Dns::Client>().Browse(*static_cast<const Ip6::SockAddr *>(aServerSockAddr), aServiceName,
|
||||
aCallback, aContext);
|
||||
return instance.Get<Dns::Client>().Browse(aServiceName, aCallback, aContext,
|
||||
static_cast<const Dns::Client::QueryConfig *>(aConfig));
|
||||
}
|
||||
|
||||
otError otDnsBrowseResponseGetServiceName(const otDnsBrowseResponse *aResponse,
|
||||
@@ -131,17 +151,17 @@ otError otDnsBrowseResponseGetHostAddress(const otDnsBrowseResponse *aResponse,
|
||||
aTtl != nullptr ? *aTtl : ttl);
|
||||
}
|
||||
|
||||
otError otDnsClientResolveService(otInstance * aInstance,
|
||||
const otSockAddr * aServerSockAddr,
|
||||
const char * aInstanceLabel,
|
||||
const char * aServiceName,
|
||||
otDnsServiceCallback aCallback,
|
||||
void * aContext)
|
||||
otError otDnsClientResolveService(otInstance * aInstance,
|
||||
const char * aInstanceLabel,
|
||||
const char * aServiceName,
|
||||
otDnsServiceCallback aCallback,
|
||||
void * aContext,
|
||||
const otDnsQueryConfig *aConfig)
|
||||
{
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
return instance.Get<Dns::Client>().ResolveService(*static_cast<const Ip6::SockAddr *>(aServerSockAddr),
|
||||
aInstanceLabel, aServiceName, aCallback, aContext);
|
||||
return instance.Get<Dns::Client>().ResolveService(aInstanceLabel, aServiceName, aCallback, aContext,
|
||||
static_cast<const Dns::Client::QueryConfig *>(aConfig));
|
||||
}
|
||||
|
||||
otError otDnsServiceResponseGetServiceName(const otDnsServiceResponse *aResponse,
|
||||
|
||||
@@ -45,26 +45,6 @@
|
||||
#define OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE 0
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_RESPONSE_TIMEOUT
|
||||
*
|
||||
* Maximum time that DNS Client waits for response in milliseconds.
|
||||
*
|
||||
*/
|
||||
#ifndef OPENTHREAD_CONFIG_DNS_RESPONSE_TIMEOUT
|
||||
#define OPENTHREAD_CONFIG_DNS_RESPONSE_TIMEOUT 3000
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_MAX_RETRANSMIT
|
||||
*
|
||||
* Maximum number of retransmissions for DNS client.
|
||||
*
|
||||
*/
|
||||
#ifndef OPENTHREAD_CONFIG_DNS_MAX_RETRANSMIT
|
||||
#define OPENTHREAD_CONFIG_DNS_MAX_RETRANSMIT 2
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
*
|
||||
@@ -75,4 +55,59 @@
|
||||
#define OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS
|
||||
*
|
||||
* Specifies the default DNS server IPv6 address.
|
||||
*
|
||||
* It MUST be a C string representation of the server IPv6 address.
|
||||
*
|
||||
* Default value is set to "2001:4860:4860::8888" which is the Google Public DNS IPv6 address.
|
||||
*
|
||||
*/
|
||||
#ifndef OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS
|
||||
#define OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS "2001:4860:4860::8888"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_PORT
|
||||
*
|
||||
* Specifies the default DNS server port number.
|
||||
*
|
||||
*/
|
||||
#ifndef OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_PORT
|
||||
#define OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_PORT 53
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RESPONSE_TIMEOUT
|
||||
*
|
||||
* Specifies the default wait time that DNS client waits for a response from server (in milliseconds).
|
||||
*
|
||||
*/
|
||||
#ifndef OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RESPONSE_TIMEOUT
|
||||
#define OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RESPONSE_TIMEOUT 6000
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_MAX_TX_ATTEMPTS
|
||||
*
|
||||
* Specifies the default maximum number of DNS query tx attempts with no response before reporting failure.
|
||||
*
|
||||
*/
|
||||
#ifndef OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_MAX_TX_ATTEMPTS
|
||||
#define OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_MAX_TX_ATTEMPTS 3
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_NO_RECURSION_FLAG
|
||||
*
|
||||
* Specifies the default "recursion desired" flag (indicates whether the server can resolve the query recursively or
|
||||
* not).
|
||||
*
|
||||
*/
|
||||
#ifndef OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RECURSION_DESIRED_FLAG
|
||||
#define OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RECURSION_DESIRED_FLAG 1
|
||||
#endif
|
||||
|
||||
#endif // CONFIG_DNS_CLIENT_H_
|
||||
|
||||
@@ -563,4 +563,12 @@
|
||||
#error "OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE conflicts with OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT."
|
||||
#endif
|
||||
|
||||
#ifdef OPENTHREAD_CONFIG_DNS_RESPONSE_TIMEOUT
|
||||
#error "OPENTHREAD_CONFIG_DNS_RESPONSE_TIMEOUT was replaced by OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RESPONSE_TIMEOUT"
|
||||
#endif
|
||||
|
||||
#ifdef OPENTHREAD_CONFIG_DNS_MAX_RETRANSMIT
|
||||
#error "OPENTHREAD_CONFIG_DNS_MAX_RETRANSMIT was replaced by OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_MAX_TX_ATTEMPTS"
|
||||
#endif
|
||||
|
||||
#endif // OPENTHREAD_CORE_CONFIG_CHECK_H_
|
||||
|
||||
+100
-30
@@ -46,6 +46,56 @@
|
||||
namespace ot {
|
||||
namespace Dns {
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
// Client::QueryConfig
|
||||
|
||||
const char Client::QueryConfig::kDefaultServerAddressString[] = OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS;
|
||||
|
||||
Client::QueryConfig::QueryConfig(InitMode aMode)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aMode);
|
||||
|
||||
IgnoreError(GetServerSockAddr().GetAddress().FromString(kDefaultServerAddressString));
|
||||
GetServerSockAddr().SetPort(kDefaultServerPort);
|
||||
SetResponseTimeout(kDefaultResponseTimeout);
|
||||
SetMaxTxAttempts(kDefaultMaxTxAttempts);
|
||||
SetRecursionFlag(kDefaultRecursionDesired ? kFlagRecursionDesired : kFlagNoRecursion);
|
||||
}
|
||||
|
||||
void Client::QueryConfig::SetFrom(const QueryConfig &aConfig, const QueryConfig &aDefaultConfig)
|
||||
{
|
||||
// This method sets the config from `aConfig` replacing any
|
||||
// unspecified fields (value zero) with the fields from
|
||||
// `aDefaultConfig`.
|
||||
|
||||
*this = aConfig;
|
||||
|
||||
if (GetServerSockAddr().GetAddress().IsUnspecified())
|
||||
{
|
||||
GetServerSockAddr().GetAddress() = aDefaultConfig.GetServerSockAddr().GetAddress();
|
||||
}
|
||||
|
||||
if (GetServerSockAddr().GetPort() == 0)
|
||||
{
|
||||
GetServerSockAddr().SetPort(aDefaultConfig.GetServerSockAddr().GetPort());
|
||||
}
|
||||
|
||||
if (GetResponseTimeout() == 0)
|
||||
{
|
||||
SetResponseTimeout(aDefaultConfig.GetResponseTimeout());
|
||||
}
|
||||
|
||||
if (GetMaxTxAttempts() == 0)
|
||||
{
|
||||
SetMaxTxAttempts(aDefaultConfig.GetMaxTxAttempts());
|
||||
}
|
||||
|
||||
if (GetRecursionFlag() == kFlagUnspecified)
|
||||
{
|
||||
SetRecursionFlag(aDefaultConfig.GetRecursionFlag());
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
// Client::Response
|
||||
|
||||
@@ -386,6 +436,7 @@ Client::Client(Instance &aInstance)
|
||||
: InstanceLocator(aInstance)
|
||||
, mSocket(aInstance)
|
||||
, mTimer(aInstance, Client::HandleTimer)
|
||||
, mDefaultConfig(QueryConfig::kInitFromDefaults)
|
||||
{
|
||||
static_assert(kAddressQuery == 0, "kAddressQuery value is not correct");
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
@@ -417,28 +468,35 @@ void Client::Stop(void)
|
||||
IgnoreError(mSocket.Close());
|
||||
}
|
||||
|
||||
otError Client::ResolveAddress(const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aHostName,
|
||||
bool aNoRecursion,
|
||||
AddressCallback aCallback,
|
||||
void * aContext)
|
||||
void Client::SetDefaultConfig(const QueryConfig &aQueryConfig)
|
||||
{
|
||||
QueryConfig startingDefault(QueryConfig::kInitFromDefaults);
|
||||
|
||||
mDefaultConfig.SetFrom(aQueryConfig, startingDefault);
|
||||
}
|
||||
|
||||
void Client::ResetDefaultConfig(void)
|
||||
{
|
||||
mDefaultConfig = QueryConfig(QueryConfig::kInitFromDefaults);
|
||||
}
|
||||
|
||||
otError Client::ResolveAddress(const char * aHostName,
|
||||
AddressCallback aCallback,
|
||||
void * aContext,
|
||||
const QueryConfig *aConfig)
|
||||
{
|
||||
QueryInfo info;
|
||||
|
||||
info.Clear();
|
||||
info.mQueryType = kAddressQuery;
|
||||
info.mNoRecursion = aNoRecursion;
|
||||
info.mCallback.mAddressCallback = aCallback;
|
||||
|
||||
return StartQuery(info, aServerSockAddr, nullptr, aHostName, aContext);
|
||||
return StartQuery(info, aConfig, nullptr, aHostName, aContext);
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
|
||||
otError Client::Browse(const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aServiceName,
|
||||
BrowseCallback aCallback,
|
||||
void * aContext)
|
||||
otError Client::Browse(const char *aServiceName, BrowseCallback aCallback, void *aContext, const QueryConfig *aConfig)
|
||||
{
|
||||
QueryInfo info;
|
||||
|
||||
@@ -446,14 +504,14 @@ otError Client::Browse(const Ip6::SockAddr &aServerSockAddr,
|
||||
info.mQueryType = kBrowseQuery;
|
||||
info.mCallback.mBrowseCallback = aCallback;
|
||||
|
||||
return StartQuery(info, aServerSockAddr, nullptr, aServiceName, aContext);
|
||||
return StartQuery(info, aConfig, nullptr, aServiceName, aContext);
|
||||
}
|
||||
|
||||
otError Client::ResolveService(const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aInstanceLabel,
|
||||
const char * aServiceName,
|
||||
ServiceCallback aCallback,
|
||||
void * aContext)
|
||||
otError Client::ResolveService(const char * aInstanceLabel,
|
||||
const char * aServiceName,
|
||||
ServiceCallback aCallback,
|
||||
void * aContext,
|
||||
const QueryConfig *aConfig)
|
||||
{
|
||||
QueryInfo info;
|
||||
|
||||
@@ -461,16 +519,16 @@ otError Client::ResolveService(const Ip6::SockAddr &aServerSockAddr,
|
||||
info.mQueryType = kServiceQuery;
|
||||
info.mCallback.mServiceCallback = aCallback;
|
||||
|
||||
return StartQuery(info, aServerSockAddr, aInstanceLabel, aServiceName, aContext);
|
||||
return StartQuery(info, aConfig, aInstanceLabel, aServiceName, aContext);
|
||||
}
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
|
||||
otError Client::StartQuery(QueryInfo & aInfo,
|
||||
const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aLabel,
|
||||
const char * aName,
|
||||
void * aContext)
|
||||
otError Client::StartQuery(QueryInfo & aInfo,
|
||||
const QueryConfig *aConfig,
|
||||
const char * aLabel,
|
||||
const char * aName,
|
||||
void * aContext)
|
||||
{
|
||||
// This method assumes that `mQueryType` and `mCallback` to be
|
||||
// already set by caller on `aInfo`. The `aLabel` can be `nullptr`
|
||||
@@ -482,7 +540,19 @@ otError Client::StartQuery(QueryInfo & aInfo,
|
||||
|
||||
VerifyOrExit(mSocket.IsBound(), error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
aInfo.mServerSockAddr = aServerSockAddr;
|
||||
if (aConfig == nullptr)
|
||||
{
|
||||
aInfo.mConfig = mDefaultConfig;
|
||||
}
|
||||
else
|
||||
{
|
||||
// To form the config for this query, replace any unspecified
|
||||
// fields (zero value) in the given `aConfig` with the fields
|
||||
// from `mDefaultConfig`.
|
||||
|
||||
aInfo.mConfig.SetFrom(*aConfig, mDefaultConfig);
|
||||
}
|
||||
|
||||
aInfo.mCallbackContext = aContext;
|
||||
|
||||
SuccessOrExit(error = AllocateQuery(aInfo, aLabel, aName, query));
|
||||
@@ -544,7 +614,8 @@ void Client::SendQuery(Query &aQuery, QueryInfo &aInfo, bool aUpdateTimer)
|
||||
Header header;
|
||||
Ip6::MessageInfo messageInfo;
|
||||
|
||||
aInfo.mRetransmissionTime = TimerMilli::GetNow() + kResponseTimeout;
|
||||
aInfo.mTransmissionCount++;
|
||||
aInfo.mRetransmissionTime = TimerMilli::GetNow() + aInfo.mConfig.GetResponseTimeout();
|
||||
|
||||
if (aInfo.mMessageId == 0)
|
||||
{
|
||||
@@ -563,7 +634,7 @@ void Client::SendQuery(Query &aQuery, QueryInfo &aInfo, bool aUpdateTimer)
|
||||
header.SetType(Header::kTypeQuery);
|
||||
header.SetQueryType(Header::kQueryTypeStandard);
|
||||
|
||||
if (!aInfo.mNoRecursion)
|
||||
if (aInfo.mConfig.GetRecursionFlag() == QueryConfig::kFlagRecursionDesired)
|
||||
{
|
||||
header.SetRecursionDesiredFlag();
|
||||
}
|
||||
@@ -583,8 +654,8 @@ void Client::SendQuery(Query &aQuery, QueryInfo &aInfo, bool aUpdateTimer)
|
||||
SuccessOrExit(error = message->Append(Question(kQuestionRecordTypes[aInfo.mQueryType][num])));
|
||||
}
|
||||
|
||||
messageInfo.SetPeerAddr(aInfo.mServerSockAddr.GetAddress());
|
||||
messageInfo.SetPeerPort(aInfo.mServerSockAddr.GetPort());
|
||||
messageInfo.SetPeerAddr(aInfo.mConfig.GetServerSockAddr().GetAddress());
|
||||
messageInfo.SetPeerPort(aInfo.mConfig.GetServerSockAddr().GetPort());
|
||||
|
||||
SuccessOrExit(error = mSocket.SendTo(*message, messageInfo));
|
||||
|
||||
@@ -794,13 +865,12 @@ void Client::HandleTimer(void)
|
||||
|
||||
if (now >= info.mRetransmissionTime)
|
||||
{
|
||||
if (info.mRetransmissionCount >= kMaxRetransmit)
|
||||
if (info.mTransmissionCount >= info.mConfig.GetMaxTxAttempts())
|
||||
{
|
||||
FinalizeQuery(*query, OT_ERROR_RESPONSE_TIMEOUT);
|
||||
continue;
|
||||
}
|
||||
|
||||
info.mRetransmissionCount++;
|
||||
SendQuery(*query, info, /* aUpdateTimer */ false);
|
||||
}
|
||||
|
||||
|
||||
+167
-35
@@ -86,6 +86,106 @@ class Client : public InstanceLocator, private NonCopyable
|
||||
typedef Message Query; // `Message` is used to save `Query` related info.
|
||||
|
||||
public:
|
||||
/**
|
||||
* This type represents a DNS query configuration (e.g., server address, response wait timeout, etc).
|
||||
*
|
||||
*/
|
||||
class QueryConfig : public otDnsQueryConfig, public Clearable<QueryConfig>
|
||||
{
|
||||
friend class Client;
|
||||
|
||||
public:
|
||||
/**
|
||||
* This enumeration type represents the "Recursion Desired" (RD) flag in a `otDnsQueryConfig`.
|
||||
*
|
||||
*/
|
||||
enum RecursionFlag
|
||||
{
|
||||
kFlagUnspecified = OT_DNS_FLAG_UNSPECIFIED, ///< The flag is not specified.
|
||||
kFlagRecursionDesired = OT_DNS_FLAG_RECURSION_DESIRED, ///< Server can resolve the query recursively.
|
||||
kFlagNoRecursion = OT_DNS_FLAG_NO_RECURSION, ///< Server can not resolve the query recursively.
|
||||
};
|
||||
|
||||
/**
|
||||
* This is the default constructor for `QueryConfig` object.
|
||||
*
|
||||
*/
|
||||
QueryConfig(void) = default;
|
||||
|
||||
/**
|
||||
* This method gets the server socket address (IPv6 address and port number).
|
||||
*
|
||||
* @returns The server socket address.
|
||||
*
|
||||
*/
|
||||
const Ip6::SockAddr &GetServerSockAddr(void) const
|
||||
{
|
||||
return static_cast<const Ip6::SockAddr &>(mServerSockAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method gets the wait time to receive response from server (in msec).
|
||||
*
|
||||
* @returns The timeout interval in msec.
|
||||
*
|
||||
*/
|
||||
uint32_t GetResponseTimeout(void) const { return mResponseTimeout; }
|
||||
|
||||
/**
|
||||
* This method gets the maximum number of query transmit attempts before reporting failure.
|
||||
*
|
||||
* @returns The maximum number of query transmit attempts.
|
||||
*
|
||||
*/
|
||||
uint8_t GetMaxTxAttempts(void) const { return mMaxTxAttempts; }
|
||||
|
||||
/**
|
||||
* This method gets the recursion flag indicating whether the server can resolve the query recursively or not.
|
||||
*
|
||||
* @returns The recursion flag.
|
||||
*
|
||||
*/
|
||||
RecursionFlag GetRecursionFlag(void) const { return static_cast<RecursionFlag>(mRecursionFlag); }
|
||||
|
||||
private:
|
||||
enum : uint32_t
|
||||
{
|
||||
kDefaultResponseTimeout = OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RESPONSE_TIMEOUT, // in msec
|
||||
};
|
||||
|
||||
enum : uint16_t
|
||||
{
|
||||
kDefaultServerPort = OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_PORT,
|
||||
};
|
||||
|
||||
enum : uint8_t
|
||||
{
|
||||
kDefaultMaxTxAttempts = OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_MAX_TX_ATTEMPTS,
|
||||
};
|
||||
|
||||
enum : bool
|
||||
{
|
||||
kDefaultRecursionDesired = OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_RECURSION_DESIRED_FLAG,
|
||||
};
|
||||
|
||||
enum InitMode : uint8_t
|
||||
{
|
||||
kInitFromDefaults,
|
||||
};
|
||||
|
||||
static const char kDefaultServerAddressString[];
|
||||
|
||||
explicit QueryConfig(InitMode aMode);
|
||||
|
||||
Ip6::SockAddr &GetServerSockAddr(void) { return static_cast<Ip6::SockAddr &>(mServerSockAddr); }
|
||||
|
||||
void SetResponseTimeout(uint32_t aResponseTimeout) { mResponseTimeout = aResponseTimeout; }
|
||||
void SetMaxTxAttempts(uint8_t aMaxTxAttempts) { mMaxTxAttempts = aMaxTxAttempts; }
|
||||
void SetRecursionFlag(RecursionFlag aFlag) { mRecursionFlag = static_cast<otDnsRecursionFlag>(aFlag); }
|
||||
|
||||
void SetFrom(const QueryConfig &aConfig, const QueryConfig &aDefaultConfig);
|
||||
};
|
||||
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
/**
|
||||
* This structure provides info for a DNS service instance.
|
||||
@@ -408,14 +508,43 @@ public:
|
||||
*/
|
||||
void Stop(void);
|
||||
|
||||
/**
|
||||
* This method gets the current default query config being used by DNS client.
|
||||
*
|
||||
* @returns The current default query config.
|
||||
*
|
||||
*/
|
||||
const QueryConfig &GetDefaultConfig(void) const { return mDefaultConfig; }
|
||||
|
||||
/**
|
||||
* This method sets the default query config.
|
||||
*
|
||||
* @param[in] aQueryConfig The new default query config.
|
||||
*
|
||||
*/
|
||||
void SetDefaultConfig(const QueryConfig &aQueryConfig);
|
||||
|
||||
/**
|
||||
* This method resets the default config to the config used when the OpenThread stack starts.
|
||||
*
|
||||
* When OpenThread stack starts, the default DNS query config is determined from a set of OT config options such as
|
||||
* `OPENTHREAD_CONFIG_DNS_CLIENT_DEFAULT_SERVER_IP6_ADDRESS`, `_DEFAULT_SERVER_PORT`, or `_DEFAULT_RESPONSE_TIMEOUT`
|
||||
* etc. (see `config/dns_clinet.h` for all related config options).
|
||||
*
|
||||
*/
|
||||
void ResetDefaultConfig(void);
|
||||
|
||||
/**
|
||||
* This method sends an address resolution DNS query for AAAA (IPv6) record for a given host name.
|
||||
*
|
||||
* @param[in] aServerSockAddr The server socket address.
|
||||
* The @p aConfig can be nullptr. In this case the default config (from `GetDefaultConfig()`) will be used as
|
||||
* the config for this query. In a non-nullptr @p aConfig, some of the fields can be left unspecified (value zero).
|
||||
* The unspecified fields are then replaced by the values from the default config.
|
||||
*
|
||||
* @param[in] aHostName The host name for which to query the address (MUST NOT be `nullptr`).
|
||||
* @param[in] aNoRecursion Indicates whether name server can resolve the query recursively or not.
|
||||
* @param[in] aCallback A callback function pointer to report the result of query.
|
||||
* @param[in] aContext A pointer to arbitrary context information passed to @p aCallback.
|
||||
* @param[in] aConfig The config to use for this query.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully sent DNS query.
|
||||
* @retval OT_ERROR_NO_BUFS Failed to allocate retransmission data.
|
||||
@@ -423,59 +552,62 @@ public:
|
||||
* @retval OT_ERROR_INVALID_STATE Cannot send query since Thread interface is not up.
|
||||
*
|
||||
*/
|
||||
otError ResolveAddress(const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aHostName,
|
||||
bool aNoRecursion,
|
||||
AddressCallback aCallback,
|
||||
void * aContext);
|
||||
otError ResolveAddress(const char * aHostName,
|
||||
AddressCallback aCallback,
|
||||
void * aContext,
|
||||
const QueryConfig *aConfig = nullptr);
|
||||
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
|
||||
/**
|
||||
* This method sends a browse (service instance enumeration) DNS query for a given service name.
|
||||
*
|
||||
* @param[in] aServerSockAddr The server socket address.
|
||||
* The @p aConfig can be nullptr. In this case the default config (from `GetDefaultConfig()`) will be used as
|
||||
* the config for this query. In a non-nullptr @p aConfig, some of the fields can be left unspecified (value zero).
|
||||
* The unspecified fields are then replaced by the values from the default config.
|
||||
*
|
||||
* @param[in] aServiceName The service name to query for (MUST NOT be `nullptr`).
|
||||
* @param[in] aCallback The callback to report the response or errors (such as time-out).
|
||||
* @param[in] aContext A pointer to arbitrary context information.
|
||||
* @param[in] aConfig The config to use for this query.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Query sent successfully. @p aCallback will be invoked to report the status.
|
||||
* @retval OT_ERROR_NO_BUFS Insufficient buffer to prepare and send query.
|
||||
*
|
||||
*/
|
||||
otError Browse(const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aServiceName,
|
||||
BrowseCallback aCallback,
|
||||
void * aContext);
|
||||
otError Browse(const char * aServiceName,
|
||||
BrowseCallback aCallback,
|
||||
void * aContext,
|
||||
const QueryConfig *aConfig = nullptr);
|
||||
|
||||
/**
|
||||
* This function sends a DNS service instance resolution query for a given service instance.
|
||||
* This method sends a DNS service instance resolution query for a given service instance.
|
||||
*
|
||||
* The @p aConfig can be nullptr. In this case the default config (from `GetDefaultConfig()`) will be used as
|
||||
* the config for this query. In a non-nullptr @p aConfig, some of the fields can be left unspecified (value zero).
|
||||
* The unspecified fields are then replaced by the values from the default config.
|
||||
|
||||
*
|
||||
* @param[in] aServerSockAddr The server socket address.
|
||||
* @param[in] aInstanceLabel The service instance label.
|
||||
* @param[in] aServiceName The service name (together with @p aInstanceLabel form full instance name).
|
||||
* @param[in] aCallback A function pointer that shall be called on response reception or time-out.
|
||||
* @param[in] aContext A pointer to arbitrary context information.
|
||||
* @param[in] aConfig The config to use for this query.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Query sent successfully. @p aCallback will be invoked to report the status.
|
||||
* @retval OT_ERROR_NO_BUFS Insufficient buffer to prepare and send query.
|
||||
*
|
||||
*/
|
||||
otError ResolveService(const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aInstanceLabel,
|
||||
otError ResolveService(const char * aInstanceLabel,
|
||||
const char * aServiceName,
|
||||
otDnsServiceCallback aCallback,
|
||||
void * aContext);
|
||||
void * aContext,
|
||||
const QueryConfig * aConfig = nullptr);
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
kResponseTimeout = OPENTHREAD_CONFIG_DNS_RESPONSE_TIMEOUT, // in msec
|
||||
kMaxRetransmit = OPENTHREAD_CONFIG_DNS_MAX_RETRANSMIT,
|
||||
};
|
||||
|
||||
enum QueryType : uint8_t
|
||||
{
|
||||
kAddressQuery, // Address resolution.
|
||||
@@ -500,14 +632,13 @@ private:
|
||||
{
|
||||
void ReadFrom(const Query &aQuery) { IgnoreError(aQuery.Read(0, *this)); }
|
||||
|
||||
QueryType mQueryType;
|
||||
uint16_t mMessageId;
|
||||
Ip6::SockAddr mServerSockAddr;
|
||||
Callback mCallback;
|
||||
void * mCallbackContext;
|
||||
TimeMilli mRetransmissionTime;
|
||||
uint8_t mRetransmissionCount;
|
||||
bool mNoRecursion;
|
||||
QueryType mQueryType;
|
||||
uint16_t mMessageId;
|
||||
Callback mCallback;
|
||||
void * mCallbackContext;
|
||||
TimeMilli mRetransmissionTime;
|
||||
QueryConfig mConfig;
|
||||
uint8_t mTransmissionCount;
|
||||
// Followed by the name (service, host, instance) encoded as a `Dns::Name`.
|
||||
};
|
||||
|
||||
@@ -516,11 +647,11 @@ private:
|
||||
kNameOffsetInQuery = sizeof(QueryInfo),
|
||||
};
|
||||
|
||||
otError StartQuery(QueryInfo & aInfo,
|
||||
const Ip6::SockAddr &aServerSockAddr,
|
||||
const char * aLabel,
|
||||
const char * aName,
|
||||
void * aContext);
|
||||
otError StartQuery(QueryInfo & aInfo,
|
||||
const QueryConfig *aConfig,
|
||||
const char * aLabel,
|
||||
const char * aName,
|
||||
void * aContext);
|
||||
otError AllocateQuery(const QueryInfo &aInfo, const char *aLabel, const char *aName, Query *&aQuery);
|
||||
void FreeQuery(Query &aQuery);
|
||||
void UpdateQuery(Query &aQuery, const QueryInfo &aInfo) { aQuery.Write(0, aInfo); }
|
||||
@@ -549,6 +680,7 @@ private:
|
||||
Ip6::Udp::Socket mSocket;
|
||||
QueryList mQueries;
|
||||
TimerMilli mTimer;
|
||||
QueryConfig mDefaultConfig;
|
||||
};
|
||||
|
||||
} // namespace Dns
|
||||
|
||||
Reference in New Issue
Block a user