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 <liangchaozhong@xiaomi.com>
This commit is contained in:
liangchaozhong 2022-11-14 17:13:32 +08:00 committed by archer
parent cd3d3c272a
commit d8777fa77d
3 changed files with 20 additions and 7 deletions

View File

@ -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
/****************************************************************************

View File

@ -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.

View File

@ -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.