Skip to content

Commit 9995892

Browse files
committed
netdb/lib_dnsdelserver.c: support delete the DNS server address
by index or address - dns_del_nameserver() - dns_del_nameserver_by_index() Signed-off-by: nuttxs <zhaoqing.zhang@sony.com>
1 parent 2ecd272 commit 9995892

File tree

4 files changed

+257
-2
lines changed

4 files changed

+257
-2
lines changed

include/nuttx/net/dns.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ extern "C"
203203

204204
int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen);
205205

206+
/****************************************************************************
207+
* Name: dns_del_nameserver
208+
*
209+
* Description:
210+
* Remove a DNS server from the list by address.
211+
*
212+
****************************************************************************/
213+
214+
int dns_del_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen);
215+
216+
/****************************************************************************
217+
* Name: dns_del_nameserver_by_index
218+
*
219+
* Description:
220+
* Remove a DNS server from the list by index (0-based).
221+
*
222+
****************************************************************************/
223+
224+
int dns_del_nameserver_by_index(int index);
225+
206226
/****************************************************************************
207227
* Name: dns_default_nameserver
208228
*

libs/libc/netdb/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ if(CONFIG_LIBC_NETDB)
5959

6060
if(CONFIG_NETDB_DNSCLIENT)
6161
list(APPEND SRCS lib_dnsinit.c lib_dnsbind.c lib_dnsquery.c)
62-
list(APPEND SRCS lib_dnsaddserver.c lib_dnsdefaultserver.c)
62+
list(APPEND SRCS lib_dnsaddserver.c lib_dnsdelserver.c
63+
lib_dnsdefaultserver.c)
6364
list(APPEND SRCS lib_dnsforeach.c lib_dnsnotify.c)
6465
list(APPEND SRCS lib_dnsqueryfamily.c)
6566

libs/libc/netdb/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ endif
4444

4545
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
4646
CSRCS += lib_dnsinit.c lib_dnsbind.c lib_dnsquery.c
47-
CSRCS += lib_dnsaddserver.c lib_dnsdefaultserver.c
47+
CSRCS += lib_dnsaddserver.c lib_dnsdelserver.c lib_dnsdefaultserver.c
4848
CSRCS += lib_dnsforeach.c lib_dnsnotify.c
4949
CSRCS += lib_dnsqueryfamily.c
5050

libs/libc/netdb/lib_dnsdelserver.c

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/****************************************************************************
2+
* libs/libc/netdb/lib_dnsdelserver.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
27+
#include <stdbool.h>
28+
#include <stdio.h>
29+
#include <string.h>
30+
#include <errno.h>
31+
#include <debug.h>
32+
33+
#include <nuttx/net/ip.h>
34+
#include <nuttx/net/dns.h>
35+
36+
#include "netdb/lib_dns.h"
37+
38+
#ifdef CONFIG_NETDB_DNSCLIENT
39+
40+
/****************************************************************************
41+
* Private Functions
42+
****************************************************************************/
43+
44+
static int dns_find_nameserver_index(FAR const struct sockaddr *addr,
45+
socklen_t addrlen)
46+
{
47+
size_t cmplen = 0;
48+
#ifndef CONFIG_NETDB_RESOLVCONF
49+
int i;
50+
#endif
51+
52+
#ifdef CONFIG_NET_IPv4
53+
if (addr->sa_family == AF_INET)
54+
{
55+
cmplen = sizeof(struct sockaddr_in);
56+
}
57+
#endif
58+
59+
#ifdef CONFIG_NET_IPv6
60+
if (addr->sa_family == AF_INET6)
61+
{
62+
cmplen = sizeof(struct sockaddr_in6);
63+
}
64+
#endif
65+
66+
if (cmplen == 0)
67+
{
68+
/* Unsupported address family */
69+
70+
return -ENOSYS;
71+
}
72+
73+
if (addrlen < cmplen)
74+
{
75+
return -EINVAL;
76+
}
77+
78+
dns_lock();
79+
80+
#ifdef CONFIG_NETDB_RESOLVCONF
81+
82+
/* Not implemented for CONFIG_NETDB_RESOLVCONF */
83+
84+
dns_unlock();
85+
return -ENOSYS;
86+
87+
#else
88+
/* Search for the matching nameserver in the array */
89+
90+
for (i = 0; i < g_dns_nservers; i++)
91+
{
92+
if (g_dns_servers[i].addr.sa_family == addr->sa_family)
93+
{
94+
#ifdef CONFIG_NET_IPv4
95+
if (addr->sa_family == AF_INET)
96+
{
97+
FAR struct sockaddr_in *in1 =
98+
(FAR struct sockaddr_in *)&g_dns_servers[i].addr;
99+
FAR struct sockaddr_in *in2 =
100+
(FAR struct sockaddr_in *)addr;
101+
102+
/* Compare only the IP address part, ignore port and padding */
103+
104+
if (net_ipv4addr_cmp(in1->sin_addr.s_addr,
105+
in2->sin_addr.s_addr))
106+
{
107+
dns_unlock();
108+
return i;
109+
}
110+
}
111+
#endif
112+
113+
#ifdef CONFIG_NET_IPv6
114+
if (addr->sa_family == AF_INET6)
115+
{
116+
FAR struct sockaddr_in6 *in6_1 =
117+
(FAR struct sockaddr_in6 *)&g_dns_servers[i].addr;
118+
FAR struct sockaddr_in6 *in6_2 =
119+
(FAR struct sockaddr_in6 *)addr;
120+
121+
/* Compare only the IPv6 address part */
122+
123+
if (net_ipv6addr_cmp(in6_1->sin6_addr.s6_addr,
124+
in6_2->sin6_addr.s6_addr))
125+
{
126+
dns_unlock();
127+
return i;
128+
}
129+
}
130+
#endif
131+
}
132+
}
133+
134+
dns_unlock();
135+
return -ENOENT;
136+
#endif
137+
}
138+
139+
/****************************************************************************
140+
* Public Functions
141+
****************************************************************************/
142+
143+
/****************************************************************************
144+
* Name: dns_del_nameserver
145+
*
146+
* Description:
147+
* Remove a DNS server from the list by matching the address
148+
*
149+
****************************************************************************/
150+
151+
#ifdef CONFIG_NETDB_RESOLVCONF
152+
153+
int dns_del_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
154+
{
155+
/* Not implemented for CONFIG_NETDB_RESOLVCONF */
156+
157+
return -ENOSYS;
158+
}
159+
160+
int dns_del_nameserver_by_index(int index)
161+
{
162+
/* For resolv.conf mode, removing requires rewriting the file */
163+
164+
return -ENOSYS;
165+
}
166+
167+
#else /* !CONFIG_NETDB_RESOLVCONF */
168+
169+
int dns_del_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
170+
{
171+
int index;
172+
173+
if (addr == NULL)
174+
{
175+
return -EINVAL;
176+
}
177+
178+
/* Find the nameserver in the array */
179+
180+
index = dns_find_nameserver_index(addr, addrlen);
181+
if (index < 0)
182+
{
183+
return index; /* Return the error code */
184+
}
185+
186+
/* Remove the server by index */
187+
188+
return dns_del_nameserver_by_index(index);
189+
}
190+
191+
int dns_del_nameserver_by_index(int index)
192+
{
193+
int i;
194+
195+
if (index < 0 || index >= CONFIG_NETDB_DNSSERVER_NAMESERVERS)
196+
{
197+
return -EINVAL;
198+
}
199+
200+
dns_lock();
201+
202+
if (index >= g_dns_nservers)
203+
{
204+
dns_unlock();
205+
return -ENOENT;
206+
}
207+
208+
/* Shift all subsequent entries down by one position */
209+
210+
for (i = index; i < g_dns_nservers - 1; i++)
211+
{
212+
memcpy(&g_dns_servers[i], &g_dns_servers[i + 1],
213+
sizeof(union dns_addr_u));
214+
}
215+
216+
memset(&g_dns_servers[g_dns_nservers - 1], 0, sizeof(union dns_addr_u));
217+
218+
/* Decrement the server count */
219+
220+
g_dns_nservers--;
221+
222+
dns_unlock();
223+
224+
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
225+
/* Clear the DNS cache after removing a server */
226+
227+
dns_clear_answer();
228+
#endif
229+
230+
return OK;
231+
}
232+
233+
#endif /* CONFIG_NETDB_RESOLVCONF */
234+
#endif /* CONFIG_NETDB_DNSCLIENT */

0 commit comments

Comments
 (0)