netdb: Make NETDB_DNSSERVER_NAMESERVERS effective for NETDB_RESOLVCONF

prevent the server list in resolv.conf from increasing indefinitely
the one that is overwritten is always the one configured first.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2024-08-29 21:53:05 +08:00 committed by Xiang Xiao
parent 4e24eec7b6
commit 7ba125f7cd
3 changed files with 37 additions and 5 deletions

View file

@ -194,7 +194,6 @@ endif # NETDB_RESOLVCONF
config NETDB_DNSSERVER_NAMESERVERS config NETDB_DNSSERVER_NAMESERVERS
int "Max number of configured nameservers" int "Max number of configured nameservers"
default 1 default 1
depends on !NETDB_RESOLVCONF
---help--- ---help---
This setting determines how many nameservers there can be This setting determines how many nameservers there can be
in use concurrently. in use concurrently.

View file

@ -122,6 +122,11 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
union dns_addr_u dns_addr; union dns_addr_u dns_addr;
FAR uint16_t *pport; FAR uint16_t *pport;
size_t copylen; size_t copylen;
#if CONFIG_NETDB_DNSSERVER_NAMESERVERS > 1
char prev_addr[CONFIG_NETDB_DNSSERVER_NAMESERVERS - 1][DNS_MAX_LINE];
int prev_cnt;
int i;
#endif
int ret; int ret;
stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "a+"); stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "a+");
@ -223,7 +228,22 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
goto errout; goto errout;
} }
/* Write the new record to the end of the resolv.conf file. */ #if CONFIG_NETDB_DNSSERVER_NAMESERVERS > 1
fseek(stream, 0, SEEK_SET);
for (prev_cnt = 0; prev_cnt < CONFIG_NETDB_DNSSERVER_NAMESERVERS - 1;
prev_cnt++)
{
if (fgets(prev_addr[prev_cnt], DNS_MAX_LINE, stream) == NULL)
{
break;
}
}
#endif
fclose(stream);
stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "w");
/* Write the new record to the head of the resolv.conf file. */
#ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT #ifdef CONFIG_NETDB_RESOLVCONF_NONSTDPORT
/* The OpenBSD version supports a [host]:port syntax. When a non-standard /* The OpenBSD version supports a [host]:port syntax. When a non-standard
@ -237,8 +257,7 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
ret = fprintf(stream, "%s [%s]:%u\n", ret = fprintf(stream, "%s [%s]:%u\n",
NETDB_DNS_KEYWORD, addrstr, NTOHS(*pport)); NETDB_DNS_KEYWORD, addrstr, NTOHS(*pport));
#else #else
ret = fprintf(stream, "%s %s\n", ret = fprintf(stream, "%s %s\n", NETDB_DNS_KEYWORD, addrstr);
NETDB_DNS_KEYWORD, addrstr);
#endif #endif
if (ret < 0) if (ret < 0)
@ -249,6 +268,20 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
goto errout; goto errout;
} }
#if CONFIG_NETDB_DNSSERVER_NAMESERVERS > 1
for (i = 0; i < prev_cnt; i++)
{
ret = fputs(prev_addr[i], stream);
if (ret < 0)
{
ret = -get_errno();
nerr("ERROR: fprintf failed: %d\n", ret);
DEBUGASSERT(ret < 0);
goto errout;
}
}
#endif
ret = OK; ret = OK;
errout: errout:

View file

@ -83,7 +83,7 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg)
unsigned int count; unsigned int count;
uint16_t port; uint16_t port;
int keylen; int keylen;
int ret; int ret = OK;
/* Open the resolver configuration file */ /* Open the resolver configuration file */