mirror of
https://github.com/apache/nuttx.git
synced 2026-05-27 11:26:12 +08:00
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:
@@ -222,6 +222,7 @@ int dns_query(FAR const char *hostname, FAR union dns_addr_u *addr,
|
|||||||
* hostname - The hostname string to be cached.
|
* hostname - The hostname string to be cached.
|
||||||
* addr - The IP addresses associated with the hostname.
|
* addr - The IP addresses associated with the hostname.
|
||||||
* naddr - The count of the IP addresses.
|
* naddr - The count of the IP addresses.
|
||||||
|
* ttl - The TTL of the IP addresses.
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
* None
|
* None
|
||||||
@@ -230,7 +231,8 @@ int dns_query(FAR const char *hostname, FAR union dns_addr_u *addr,
|
|||||||
|
|
||||||
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
||||||
void dns_save_answer(FAR const char *hostname,
|
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
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ struct dns_cache_s
|
|||||||
char name[CONFIG_NETDB_DNSCLIENT_NAMESIZE];
|
char name[CONFIG_NETDB_DNSCLIENT_NAMESIZE];
|
||||||
uint8_t naddr; /* How many addresses per name */
|
uint8_t naddr; /* How many addresses per name */
|
||||||
union dns_addr_u addr[CONFIG_NETDB_MAX_IPADDR];
|
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.
|
* hostname - The hostname string to be cached.
|
||||||
* addr - The IP addresses associated with the hostname.
|
* addr - The IP addresses associated with the hostname.
|
||||||
* naddr - The count of the IP addresses.
|
* naddr - The count of the IP addresses.
|
||||||
|
* ttl - The TTL of the IP addresses.
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
* None
|
* 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,
|
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;
|
FAR struct dns_cache_s *entry;
|
||||||
#if CONFIG_NETDB_DNSCLIENT_LIFESEC > 0
|
#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);
|
strlcpy(entry->name, hostname, CONFIG_NETDB_DNSCLIENT_NAMESIZE);
|
||||||
memcpy(&entry->addr, addr, naddr * sizeof(*addr));
|
memcpy(&entry->addr, addr, naddr * sizeof(*addr));
|
||||||
entry->naddr = naddr;
|
entry->naddr = naddr;
|
||||||
|
entry->ttl = ttl;
|
||||||
|
|
||||||
/* Save the updated head index */
|
/* 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;
|
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 has expired. Increment the tail index to exclude
|
||||||
* this entry on future traversals.
|
* this entry on future traversals.
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ struct dns_query_s
|
|||||||
FAR const char *hostname; /* Hostname to lookup */
|
FAR const char *hostname; /* Hostname to lookup */
|
||||||
FAR union dns_addr_u *addr; /* Location to return host address */
|
FAR union dns_addr_u *addr; /* Location to return host address */
|
||||||
FAR int *naddr; /* Number of returned addresses */
|
FAR int *naddr; /* Number of returned addresses */
|
||||||
|
uint32_t ttl; /* Time to Live, unit:s */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Query info to check response against. */
|
/* 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,
|
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 *nameptr;
|
||||||
FAR uint8_t *namestart;
|
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->type), NTOHS(ans->class),
|
||||||
(NTOHS(ans->ttl[0]) << 16) | NTOHS(ans->ttl[1]),
|
(NTOHS(ans->ttl[0]) << 16) | NTOHS(ans->ttl[1]),
|
||||||
NTOHS(ans->len));
|
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
|
/* Check for IPv4/6 address type and Internet class. Others are
|
||||||
* discarded.
|
* discarded.
|
||||||
@@ -612,7 +618,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
|
|||||||
/* Obtain the IPv6 response */
|
/* Obtain the IPv6 response */
|
||||||
|
|
||||||
ret = dns_recv_response(sd, &query->addr[next],
|
ret = dns_recv_response(sd, &query->addr[next],
|
||||||
*query->naddr - next, &qinfo);
|
*query->naddr - next, &qinfo, &query->ttl);
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
next += ret;
|
next += ret;
|
||||||
@@ -641,7 +647,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
|
|||||||
/* Obtain the IPv4 response */
|
/* Obtain the IPv4 response */
|
||||||
|
|
||||||
ret = dns_recv_response(sd, &query->addr[next],
|
ret = dns_recv_response(sd, &query->addr[next],
|
||||||
*query->naddr - next, &qinfo);
|
*query->naddr - next, &qinfo, &query->ttl);
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
next += ret;
|
next += ret;
|
||||||
@@ -659,7 +665,7 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
|
|||||||
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
||||||
/* Save the answer in the DNS cache */
|
/* 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
|
#endif
|
||||||
/* Return 1 to indicate to (1) stop the traversal, and (2)
|
/* Return 1 to indicate to (1) stop the traversal, and (2)
|
||||||
* indicate that the address was found.
|
* indicate that the address was found.
|
||||||
|
|||||||
Reference in New Issue
Block a user