netdb/lib_dnsaddserver.c: Do not insert the duplicate DNS address

Signed-off-by: songlinzhang <songlinzhang@xiaomi.com>
This commit is contained in:
songlinzhang 2022-04-11 14:38:21 +08:00 committed by Xiang Xiao
parent c9f3b3a7f1
commit 8dd5d0d510

View File

@ -49,6 +49,26 @@ union dns_addr_u g_dns_servers[CONFIG_NETDB_DNSSERVER_NAMESERVERS];
uint8_t g_dns_nservers; /* Number of currently configured nameservers */
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
static int dns_check_nameserver(FAR void *arg, FAR struct sockaddr *addr,
socklen_t addrlen)
{
FAR struct sockaddr *newaddr = (FAR struct sockaddr *)arg;
if (addr->sa_family == newaddr->sa_family)
{
if (memcmp(addr, newaddr, addrlen) == 0)
{
return -EEXIST;
}
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
@ -66,9 +86,9 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
{
FAR FILE *stream;
char addrstr[DNS_MAX_ADDRSTR];
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
uint16_t port;
#endif
union dns_addr_u dns_addr;
FAR uint16_t *pport;
size_t copylen;
int ret;
stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "a+");
@ -97,6 +117,8 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
{
FAR struct sockaddr_in *in4 = (FAR struct sockaddr_in *)addr;
copylen = sizeof(struct sockaddr_in);
pport = &dns_addr.ipv4.sin_port;
if (inet_ntop(AF_INET, &in4->sin_addr, addrstr,
DNS_MAX_ADDRSTR) == NULL)
{
@ -105,12 +127,6 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
DEBUGASSERT(ret < 0);
goto errout;
}
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
/* Get the port number */
port = NTOHS(in4->sin_port);
#endif
}
}
else
@ -130,6 +146,8 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
{
FAR struct sockaddr_in6 *in6 = (FAR struct sockaddr_in6 *)addr;
copylen = sizeof(struct sockaddr_in6);
pport = &dns_addr.ipv6.sin6_port;
if (inet_ntop(AF_INET6, &in6->sin6_addr, addrstr,
DNS_MAX_ADDRSTR) == NULL)
{
@ -138,12 +156,6 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
DEBUGASSERT(ret < 0);
goto errout;
}
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
/* Get the port number */
port = NTOHS(in6->sin6_port);
#endif
}
}
else
@ -154,6 +166,21 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
goto errout;
}
memcpy(&dns_addr.addr, addr, copylen);
/* A port number of zero means to use the default DNS server port number */
if (*pport == 0)
{
*pport = HTONS(DNS_DEFAULT_PORT);
}
ret = dns_foreach_nameserver(dns_check_nameserver, &dns_addr.addr);
if (ret < 0)
{
goto errout;
}
/* Write the new record to the end of the resolv.conf file. */
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
@ -165,17 +192,12 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
* nameserver [::1]:5353
*/
if (port != 0 && port != DNS_DEFAULT_PORT)
{
ret = fprintf(stream, "%s [%s]:%u\n",
NETDB_DNS_KEYWORD, addrstr, port);
}
else
ret = fprintf(stream, "%s [%s]:%u\n",
NETDB_DNS_KEYWORD, addrstr, NTOHS(*pport));
#else
ret = fprintf(stream, "%s %s\n",
NETDB_DNS_KEYWORD, addrstr);
#endif
{
ret = fprintf(stream, "%s %s\n",
NETDB_DNS_KEYWORD, addrstr);
}
if (ret < 0)
{
@ -193,7 +215,7 @@ errout:
if (ret == OK)
{
dns_notify_nameserver(addr, addrlen);
dns_notify_nameserver(&dns_addr.addr, addrlen);
}
return ret;
@ -203,10 +225,12 @@ errout:
int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
{
union dns_addr_u dns_addr;
FAR uint16_t *pport;
size_t copylen;
int nservers;
int idx;
int ret;
DEBUGASSERT(addr != NULL);
@ -232,7 +256,7 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
/* Set up for the IPv4 address copy */
copylen = sizeof(struct sockaddr_in);
pport = &g_dns_servers[idx].ipv4.sin_port;
pport = &dns_addr.ipv4.sin_port;
}
else
#endif
@ -245,7 +269,7 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
/* Set up for the IPv6 address copy */
copylen = sizeof(struct sockaddr_in6);
pport = &g_dns_servers[idx].ipv6.sin6_port;
pport = &dns_addr.ipv6.sin6_port;
}
else
#endif
@ -265,7 +289,7 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
return -EINVAL;
}
memcpy(&g_dns_servers[idx].addr, addr, copylen);
memcpy(&dns_addr.addr, addr, copylen);
/* A port number of zero means to use the default DNS server port number */
@ -274,6 +298,15 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
*pport = HTONS(DNS_DEFAULT_PORT);
}
ret = dns_foreach_nameserver(dns_check_nameserver, &dns_addr.addr);
if (ret < 0)
{
dns_semgive();
return ret;
}
memcpy(&g_dns_servers[idx].addr, &dns_addr.addr, copylen);
/* We now have a valid DNS address */
g_dns_nservers = nservers;
@ -281,7 +314,7 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
dns_clear_answer();
#endif
dns_notify_nameserver(addr, addrlen);
dns_notify_nameserver(&dns_addr.addr, addrlen);
return OK;
}