From f3a5aee7d7a5234c726c7bd066c79592f4128f62 Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Mon, 24 May 2021 22:02:44 +0800 Subject: [PATCH] libs/libc/netdb: add dns clear interface Signed-off-by: chao.an --- libs/libc/netdb/lib_dns.h | 15 +++++++++++++++ libs/libc/netdb/lib_dnsaddserver.c | 3 +++ libs/libc/netdb/lib_dnscache.c | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/libs/libc/netdb/lib_dns.h b/libs/libc/netdb/lib_dns.h index 95e2dcc81a..3ee8aee2ba 100644 --- a/libs/libc/netdb/lib_dns.h +++ b/libs/libc/netdb/lib_dns.h @@ -224,6 +224,21 @@ void dns_save_answer(FAR const char *hostname, FAR const union dns_addr_u *addr, int naddr); #endif +/**************************************************************************** + * Name: dns_clear_answer + * + * Description: + * Clear the resolved hostname in the DNS cache + * + * Returned Value: + * None + * + ****************************************************************************/ + +#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0 +void dns_clear_answer(void); +#endif + /**************************************************************************** * Name: dns_find_answer * diff --git a/libs/libc/netdb/lib_dnsaddserver.c b/libs/libc/netdb/lib_dnsaddserver.c index bcbc7ab888..20dc3891a0 100644 --- a/libs/libc/netdb/lib_dnsaddserver.c +++ b/libs/libc/netdb/lib_dnsaddserver.c @@ -270,6 +270,9 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen) g_dns_nservers = nservers; dns_semgive(); +#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0 + dns_clear_answer(); +#endif dns_notify_nameserver(addr, addrlen); return OK; } diff --git a/libs/libc/netdb/lib_dnscache.c b/libs/libc/netdb/lib_dnscache.c index d4ba396849..5f2ccafb57 100644 --- a/libs/libc/netdb/lib_dnscache.c +++ b/libs/libc/netdb/lib_dnscache.c @@ -164,6 +164,31 @@ void dns_save_answer(FAR const char *hostname, dns_semgive(); } +/**************************************************************************** + * Name: dns_clear_answer + * + * Description: + * Clear the resolved hostname in the DNS cache + * + * Returned Value: + * None + * + ****************************************************************************/ + +void dns_clear_answer(void) +{ + /* Get exclusive access to the DNS cache */ + + dns_semtake(); + + /* Reset the circular of DNS cache */ + + g_dns_head = 0; + g_dns_tail = 0; + + dns_semgive(); +} + /**************************************************************************** * Name: dns_find_answer *