Skip to content

Commit cdf5811

Browse files
Cristib05cfriedt
authored andcommitted
net: dns: DNS resolver packet forwarding
This aims to implement a packet forwarding mechanism between DNS resolver and applications that install a callback, letting DNS resolver know that received UDP packet is also required by an application. Signed-off-by: Cristian Bulacu <[email protected]>
1 parent 52862dc commit cdf5811

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

include/zephyr/net/dns_resolve.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/*
88
* Copyright (c) 2017 Intel Corporation
9+
* Copyright 2025 NXP
910
*
1011
* SPDX-License-Identifier: Apache-2.0
1112
*/
@@ -395,6 +396,19 @@ typedef void (*dns_resolve_cb_t)(enum dns_resolve_status status,
395396
struct dns_addrinfo *info,
396397
void *user_data);
397398

399+
/**
400+
* @typedef dns_resolve_pkt_fw_cb_t
401+
* @brief DNS resolve callback which passes the received packet from DNS server to application
402+
*
403+
* @details The DNS resolve packet forwarding callback is called after a successful
404+
* DNS resolving.
405+
*
406+
* @param dns_data Pointer to data buffer containing the DNS message.
407+
* @param buf_len Length of the data.
408+
* @param user_data User data passed in dns_resolve function call.
409+
*/
410+
typedef void (*dns_resolve_pkt_fw_cb_t)(struct net_buf *dns_data, size_t buf_len, void *user_data);
411+
398412
/** @cond INTERNAL_HIDDEN */
399413

400414
enum dns_resolve_context_state {
@@ -506,6 +520,11 @@ struct dns_resolve_context {
506520

507521
/** Is this context in use */
508522
enum dns_resolve_context_state state;
523+
524+
#if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING) || defined(__DOXYGEN__)
525+
/** DNS packet forwarding callback. */
526+
dns_resolve_pkt_fw_cb_t pkt_fw_cb;
527+
#endif /* CONFIG_DNS_RESOLVER_PACKET_FORWARDING */
509528
};
510529

511530
/** @cond INTERNAL_HIDDEN */
@@ -774,6 +793,25 @@ static inline int dns_resolve_service(struct dns_resolve_context *ctx,
774793
*/
775794
struct dns_resolve_context *dns_resolve_get_default(void);
776795

796+
#if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING) || defined(__DOXYGEN__)
797+
/**
798+
* @brief Installs the packet forwarding callback to the DNS resolving context.
799+
*
800+
* @details When this callback is installed, a received message from DNS server
801+
* will be passed to callback.
802+
*
803+
* @param ctx Pointer to DNS resolver context.
804+
* If the application wants to use the default DNS context, pointer to default dns
805+
* context can be obtained by calling dns_resolve_get_default() function.
806+
* @param cb Callback to call when received DNS message is required by application.
807+
*/
808+
static inline void dns_resolve_enable_packet_forwarding(struct dns_resolve_context *ctx,
809+
dns_resolve_pkt_fw_cb_t cb)
810+
{
811+
ctx->pkt_fw_cb = cb;
812+
}
813+
#endif /* CONFIG_DNS_RESOLVER_PACKET_FORWARDING */
814+
777815
/**
778816
* @brief Get IP address info from DNS.
779817
*

subsys/net/lib/dns/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ config DNS_RESOLVER_CACHE_MAX_ENTRIES
194194

195195
endif # DNS_RESOLVER_CACHE
196196

197+
config DNS_RESOLVER_PACKET_FORWARDING
198+
bool "Forwards received DNS packets to application"
199+
depends on !DNS_RESOLVER_CACHE
200+
help
201+
This allows forwarding of received packets from DNS servers
202+
to applications that register the forwarding callback.
203+
197204
endif # DNS_RESOLVER
198205

199206
config MDNS_RESPONDER

subsys/net/lib/dns/resolve.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/*
88
* Copyright (c) 2017 Intel Corporation
99
* Copyright (c) 2024 Nordic Semiconductor
10+
* Copyright 2025 NXP
1011
*
1112
* SPDX-License-Identifier: Apache-2.0
1213
*/
@@ -1527,6 +1528,12 @@ static int dns_read(struct dns_resolve_context *ctx,
15271528
goto quit;
15281529
}
15291530

1531+
#if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING)
1532+
if (ctx->pkt_fw_cb != NULL) {
1533+
ctx->pkt_fw_cb(dns_data, data_len, ctx->queries[query_idx].user_data);
1534+
}
1535+
#endif /* CONFIG_DNS_RESOLVER_PACKET_FORWARDING */
1536+
15301537
invoke_query_callback(ret, NULL, &ctx->queries[query_idx]);
15311538

15321539
/* Marks the end of the results */

0 commit comments

Comments
 (0)