From af758e51a26ade0946ff85401598e797a338ce89 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 23 Jan 2025 15:07:11 +0200 Subject: [PATCH] net: dns: Introduce resolver function that avoids using the cache Introduce dns_resolve_name_internal() that allows resolving a name and not use DNS cache if caching is enabled. This is needed in mDNS probing (RFC 6762 chapter 8.1) which needs to send a mDNS query and not get any results from cache. Signed-off-by: Jukka Rissanen --- subsys/net/ip/net_private.h | 12 ++++++++ subsys/net/lib/dns/resolve.c | 53 ++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/subsys/net/ip/net_private.h b/subsys/net/ip/net_private.h index 321e9b717b5..6a4099bfd85 100644 --- a/subsys/net/ip/net_private.h +++ b/subsys/net/ip/net_private.h @@ -154,6 +154,18 @@ extern void mdns_init_responder(void); static inline void mdns_init_responder(void) { } #endif /* CONFIG_MDNS_RESPONDER */ +#if defined(CONFIG_DNS_RESOLVER) +#include +extern int dns_resolve_name_internal(struct dns_resolve_context *ctx, + const char *query, + enum dns_query_type type, + uint16_t *dns_id, + dns_resolve_cb_t cb, + void *user_data, + int32_t timeout, + bool use_cache); +#endif /* CONFIG_DNS_RESOLVER */ + #if defined(CONFIG_NET_TEST) extern void loopback_enable_address_swap(bool swap_addresses); #endif /* CONFIG_NET_TEST */ diff --git a/subsys/net/lib/dns/resolve.c b/subsys/net/lib/dns/resolve.c index d976861baed..0fac9402df8 100644 --- a/subsys/net/lib/dns/resolve.c +++ b/subsys/net/lib/dns/resolve.c @@ -1340,13 +1340,14 @@ static void query_timeout(struct k_work *work) k_mutex_unlock(&pending_query->ctx->lock); } -int dns_resolve_name(struct dns_resolve_context *ctx, - const char *query, - enum dns_query_type type, - uint16_t *dns_id, - dns_resolve_cb_t cb, - void *user_data, - int32_t timeout) +int dns_resolve_name_internal(struct dns_resolve_context *ctx, + const char *query, + enum dns_query_type type, + uint16_t *dns_id, + dns_resolve_cb_t cb, + void *user_data, + int32_t timeout, + bool use_cache) { k_timeout_t tout; struct net_buf *dns_data = NULL; @@ -1421,18 +1422,24 @@ int dns_resolve_name(struct dns_resolve_context *ctx, try_resolve: #ifdef CONFIG_DNS_RESOLVER_CACHE - ret = dns_cache_find(&dns_cache, query, type, cached_info, ARRAY_SIZE(cached_info)); - if (ret > 0) { - /* The query was cached, no - * need to continue further. - */ - for (size_t cache_index = 0; cache_index < ret; cache_index++) { - cb(DNS_EAI_INPROGRESS, &cached_info[cache_index], user_data); - } - cb(DNS_EAI_ALLDONE, NULL, user_data); + if (use_cache) { + ret = dns_cache_find(&dns_cache, query, type, cached_info, + ARRAY_SIZE(cached_info)); + if (ret > 0) { + /* The query was cached, no + * need to continue further. + */ + for (size_t cache_index = 0; cache_index < ret; cache_index++) { + cb(DNS_EAI_INPROGRESS, &cached_info[cache_index], user_data); + } - return 0; + cb(DNS_EAI_ALLDONE, NULL, user_data); + + return 0; + } } +#else + ARG_UNUSED(use_cache); #endif /* CONFIG_DNS_RESOLVER_CACHE */ k_mutex_lock(&ctx->lock, K_FOREVER); @@ -1580,6 +1587,18 @@ fail: return ret; } +int dns_resolve_name(struct dns_resolve_context *ctx, + const char *query, + enum dns_query_type type, + uint16_t *dns_id, + dns_resolve_cb_t cb, + void *user_data, + int32_t timeout) +{ + return dns_resolve_name_internal(ctx, query, type, dns_id, cb, + user_data, timeout, true); +} + /* Must be invoked with context lock held */ static int dns_resolve_close_locked(struct dns_resolve_context *ctx) {