From d8777fa77d3bf03cc435a3f9d9c611e5564e68e3 Mon Sep 17 00:00:00 2001 From: liangchaozhong Date: Mon, 14 Nov 2022 17:13:32 +0800 Subject: [PATCH] invalid dns cache entry after ttl expires Log ttl after receive dns query and invalid dns cache entry when ttl expires. Signed-off-by: liangchaozhong --- libs/libc/netdb/lib_dns.h | 4 +++- libs/libc/netdb/lib_dnscache.c | 9 +++++++-- libs/libc/netdb/lib_dnsquery.c | 14 ++++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/libs/libc/netdb/lib_dns.h b/libs/libc/netdb/lib_dns.h index 996bc8683f..3a05b797a0 100644 --- a/libs/libc/netdb/lib_dns.h +++ b/libs/libc/netdb/lib_dns.h @@ -222,6 +222,7 @@ int dns_query(FAR const char *hostname, FAR union dns_addr_u *addr, * hostname - The hostname string to be cached. * addr - The IP addresses associated with the hostname. * naddr - The count of the IP addresses. + * ttl - The TTL of the IP addresses. * * Returned Value: * None @@ -230,7 +231,8 @@ int dns_query(FAR const char *hostname, FAR union dns_addr_u *addr, #if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0 void dns_save_answer(FAR const char *hostname, - FAR const union dns_addr_u *addr, int naddr); + FAR const union dns_addr_u *addr, int naddr, + uint32_t ttl); #endif /**************************************************************************** diff --git a/libs/libc/netdb/lib_dnscache.c b/libs/libc/netdb/lib_dnscache.c index 3cccf36198..bb1ee03aa1 100644 --- a/libs/libc/netdb/lib_dnscache.c +++ b/libs/libc/netdb/lib_dnscache.c @@ -53,6 +53,7 @@ struct dns_cache_s char name[CONFIG_NETDB_DNSCLIENT_NAMESIZE]; uint8_t naddr; /* How many addresses per name */ union dns_addr_u addr[CONFIG_NETDB_MAX_IPADDR]; + uint32_t ttl; }; /**************************************************************************** @@ -84,6 +85,7 @@ static struct dns_cache_s g_dns_cache[CONFIG_NETDB_DNSCLIENT_ENTRIES]; * hostname - The hostname string to be cached. * addr - The IP addresses associated with the hostname. * naddr - The count of the IP addresses. + * ttl - The TTL of the IP addresses. * * Returned Value: * None @@ -91,7 +93,8 @@ static struct dns_cache_s g_dns_cache[CONFIG_NETDB_DNSCLIENT_ENTRIES]; ****************************************************************************/ void dns_save_answer(FAR const char *hostname, - FAR const union dns_addr_u *addr, int naddr) + FAR const union dns_addr_u *addr, int naddr, + uint32_t ttl) { FAR struct dns_cache_s *entry; #if CONFIG_NETDB_DNSCLIENT_LIFESEC > 0 @@ -145,6 +148,7 @@ void dns_save_answer(FAR const char *hostname, strlcpy(entry->name, hostname, CONFIG_NETDB_DNSCLIENT_NAMESIZE); memcpy(&entry->addr, addr, naddr * sizeof(*addr)); entry->naddr = naddr; + entry->ttl = ttl; /* Save the updated head index */ @@ -242,7 +246,8 @@ int dns_find_answer(FAR const char *hostname, FAR union dns_addr_u *addr, */ elapsed = (uint32_t)now.tv_sec - (uint32_t)entry->ctime; - if (ret >= 0 && elapsed > CONFIG_NETDB_DNSCLIENT_LIFESEC) + if (ret >= 0 && + (elapsed > CONFIG_NETDB_DNSCLIENT_LIFESEC || elapsed > entry->ttl)) { /* This entry has expired. Increment the tail index to exclude * this entry on future traversals. diff --git a/libs/libc/netdb/lib_dnsquery.c b/libs/libc/netdb/lib_dnsquery.c index 9c8040f79c..a68371866b 100644 --- a/libs/libc/netdb/lib_dnsquery.c +++ b/libs/libc/netdb/lib_dnsquery.c @@ -90,6 +90,7 @@ struct dns_query_s FAR const char *hostname; /* Hostname to lookup */ FAR union dns_addr_u *addr; /* Location to return host address */ FAR int *naddr; /* Number of returned addresses */ + uint32_t ttl; /* Time to Live, unit:s */ }; /* Query info to check response against. */ @@ -325,7 +326,8 @@ static int dns_send_query(int sd, FAR const char *name, ****************************************************************************/ static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr, - FAR struct dns_query_info_s *qinfo) + FAR struct dns_query_info_s *qinfo, + uint32_t *ttl) { FAR uint8_t *nameptr; FAR uint8_t *namestart; @@ -475,6 +477,10 @@ static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr, NTOHS(ans->type), NTOHS(ans->class), (NTOHS(ans->ttl[0]) << 16) | NTOHS(ans->ttl[1]), NTOHS(ans->len)); + if (ttl) + { + *ttl = (NTOHS(ans->ttl[0]) << 16) | NTOHS(ans->ttl[1]); + } /* Check for IPv4/6 address type and Internet class. Others are * discarded. @@ -612,7 +618,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr, /* Obtain the IPv6 response */ ret = dns_recv_response(sd, &query->addr[next], - *query->naddr - next, &qinfo); + *query->naddr - next, &qinfo, &query->ttl); if (ret >= 0) { next += ret; @@ -641,7 +647,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr, /* Obtain the IPv4 response */ ret = dns_recv_response(sd, &query->addr[next], - *query->naddr - next, &qinfo); + *query->naddr - next, &qinfo, &query->ttl); if (ret >= 0) { next += ret; @@ -659,7 +665,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr, #if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0 /* Save the answer in the DNS cache */ - dns_save_answer(query->hostname, query->addr, next); + dns_save_answer(query->hostname, query->addr, next, query->ttl); #endif /* Return 1 to indicate to (1) stop the traversal, and (2) * indicate that the address was found.