net: dns: add dns_resolve_reconfigure() API

So far there was no dedicated mechanism for replacing DNS servers with
new list. Add dns_resolve_reconfigure() API that allows to achieve that
in a thread-safe manner.

Introduce 3rd state in DNS context lifetime by converting from 'bool
is_used' to 'enum dns_resolve_context_state state'. This new
DEACTIVATING state allows to mark a DNS context as busy and safely close
context without holding lock. Closing DNS context with released lock
prevents deadlock in case net_context_close() has to synchronize with a
separate thread executing handler passed to net_context_recv() (which is
the case for example with ESP-AT WiFi driver).

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
This commit is contained in:
Marcin Niestroj 2021-03-18 12:31:07 +01:00 committed by Anas Nashif
commit 71c31c45c7
5 changed files with 174 additions and 36 deletions

View file

@ -157,6 +157,12 @@ typedef void (*dns_resolve_cb_t)(enum dns_resolve_status status,
struct dns_addrinfo *info,
void *user_data);
enum dns_resolve_context_state {
DNS_RESOLVE_CONTEXT_ACTIVE,
DNS_RESOLVE_CONTEXT_DEACTIVATING,
DNS_RESOLVE_CONTEXT_INACTIVE,
};
/**
* DNS resolve context structure.
*/
@ -237,7 +243,7 @@ struct dns_resolve_context {
} queries[CONFIG_DNS_NUM_CONCUR_QUERIES];
/** Is this context in use */
bool is_used;
enum dns_resolve_context_state state;
};
/**
@ -283,6 +289,29 @@ int dns_resolve_init(struct dns_resolve_context *ctx,
*/
int dns_resolve_close(struct dns_resolve_context *ctx);
/**
* @brief Reconfigure DNS resolving context.
*
* @details Reconfigures DNS context with new server list.
*
* @param ctx DNS context
* @param dns_servers_str DNS server addresses using textual strings. The
* array is NULL terminated. The port number can be given in the string.
* Syntax for the server addresses with or without port numbers:
* IPv4 : 10.0.9.1
* IPv4 + port : 10.0.9.1:5353
* IPv6 : 2001:db8::22:42
* IPv6 + port : [2001:db8::22:42]:5353
* @param dns_servers_sa DNS server addresses as struct sockaddr. The array
* is NULL terminated. Port numbers are optional in struct sockaddr, the
* default will be used if set to 0.
*
* @return 0 if ok, <0 if error.
*/
int dns_resolve_reconfigure(struct dns_resolve_context *ctx,
const char *servers_str[],
const struct sockaddr *servers_sa[]);
/**
* @brief Cancel a pending DNS query.
*