Skip to content

Commit 1f75639

Browse files
committed
[nrf fromtree] net: sockets_offload: Allow to enable/disable DNS offload at runtime
Add new socket offloading functions, allowing to enable/disable offloaded DNS implementation at runtime. This may be useful if there is a mix of offloaded/native network interfaces in the system, so the application can choose which DNS backend to use. Signed-off-by: Robert Lubos <[email protected]> (cherry picked from commit b18bc7c)
1 parent 34bef2b commit 1f75639

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

include/zephyr/net/socket_offload.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ struct socket_dns_offload {
4141
*/
4242
void socket_offload_dns_register(const struct socket_dns_offload *ops);
4343

44+
/**
45+
* @brief Deregister an offloaded socket DNS API interface.
46+
*
47+
* @param ops A pointer to the offloaded socket DNS API interface.
48+
*
49+
* @retval 0 On success
50+
* @retval -EINVAL Offloaded DNS API was not regsitered.
51+
*/
52+
int socket_offload_dns_deregister(const struct socket_dns_offload *ops);
53+
54+
/**
55+
* @brief Enable/disable DNS offloading at runtime.
56+
*
57+
* @param enable Whether to enable or disable the DNS offloading.
58+
*/
59+
void socket_offload_dns_enable(bool enable);
60+
61+
/**
62+
* @brief Check if DNS offloading is enabled.
63+
*
64+
* @retval true DNS offloaded API is registered and enabled.
65+
* @retval false DNS offloading is disabled.
66+
*/
67+
#if defined(CONFIG_NET_SOCKETS_OFFLOAD)
68+
bool socket_offload_dns_is_enabled(void);
69+
#else
70+
static inline bool socket_offload_dns_is_enabled(void)
71+
{
72+
return false;
73+
}
74+
#endif /* defined(CONFIG_NET_SOCKETS_OFFLOAD) */
75+
76+
4477
/** @cond INTERNAL_HIDDEN */
4578

4679
int socket_offload_getaddrinfo(const char *node, const char *service,

subsys/net/lib/sockets/getaddrinfo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ int zsock_getaddrinfo(const char *host, const char *service,
433433
const struct zsock_addrinfo *hints,
434434
struct zsock_addrinfo **res)
435435
{
436-
if (IS_ENABLED(CONFIG_NET_SOCKETS_OFFLOAD)) {
436+
if (socket_offload_dns_is_enabled()) {
437437
return socket_offload_getaddrinfo(host, service, hints, res);
438438
}
439439

@@ -471,7 +471,7 @@ int zsock_getaddrinfo(const char *host, const char *service,
471471

472472
void zsock_freeaddrinfo(struct zsock_addrinfo *ai)
473473
{
474-
if (IS_ENABLED(CONFIG_NET_SOCKETS_OFFLOAD)) {
474+
if (socket_offload_dns_is_enabled()) {
475475
socket_offload_freeaddrinfo(ai);
476476
return;
477477
}

subsys/net/lib/sockets/socket_offload.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,42 @@ LOG_MODULE_REGISTER(net_socket_offload, CONFIG_NET_SOCKETS_LOG_LEVEL);
1212

1313
#include "sockets_internal.h"
1414

15-
const struct socket_dns_offload *dns_offload;
15+
static const struct socket_dns_offload *dns_offload;
16+
static bool dns_offload_enabled;
1617

1718
void socket_offload_dns_register(const struct socket_dns_offload *ops)
1819
{
1920
__ASSERT_NO_MSG(ops);
2021
__ASSERT_NO_MSG(dns_offload == NULL);
2122

2223
dns_offload = ops;
24+
25+
socket_offload_dns_enable(true);
26+
}
27+
28+
int socket_offload_dns_deregister(const struct socket_dns_offload *ops)
29+
{
30+
__ASSERT_NO_MSG(ops != NULL);
31+
32+
if (dns_offload != ops) {
33+
return -EINVAL;
34+
}
35+
36+
dns_offload = NULL;
37+
38+
socket_offload_dns_enable(false);
39+
40+
return 0;
41+
}
42+
43+
void socket_offload_dns_enable(bool enable)
44+
{
45+
dns_offload_enabled = enable;
46+
}
47+
48+
bool socket_offload_dns_is_enabled(void)
49+
{
50+
return (dns_offload != NULL) && dns_offload_enabled;
2351
}
2452

2553
int socket_offload_getaddrinfo(const char *node, const char *service,

0 commit comments

Comments
 (0)