Skip to content

Commit 6efaf9d

Browse files
committed
netutils/netlib: support delete the DNS server address by index or address
- Modify the DHCP-client data structure to support multiple DNS addresses - Enhance DHCP-option parsing to extract all DNS-server addresses - Set all received DNS-server addresses Signed-off-by: nuttxs <[email protected]>
1 parent 099c8ff commit 6efaf9d

File tree

8 files changed

+259
-18
lines changed

8 files changed

+259
-18
lines changed

include/netutils/dhcpc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ struct dhcpc_state
5555
struct in_addr serverid;
5656
struct in_addr ipaddr;
5757
struct in_addr netmask;
58-
struct in_addr dnsaddr;
58+
struct in_addr dnsaddr[CONFIG_NETDB_DNSSERVER_NAMESERVERS];
59+
uint8_t num_dnsaddr; /* Number of DNS addresses received */
5960
struct in_addr default_router;
6061
uint32_t lease_time; /* Lease expires in this number of seconds */
6162
uint32_t renewal_time; /* Seconds to transition to RENEW state(T1) */

include/netutils/netlib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,14 @@ int netlib_ifdown(FAR const char *ifname);
482482

483483
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)
484484
int netlib_set_ipv4dnsaddr(FAR const struct in_addr *inaddr);
485+
int netlib_del_ipv4dnsaddr(FAR const struct in_addr *inaddr);
486+
int netlib_del_ipv4dnsaddr_by_index(int index);
485487
#endif
486488

487489
#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NETDB_DNSCLIENT)
488490
int netlib_set_ipv6dnsaddr(FAR const struct in6_addr *inaddr);
491+
int netlib_del_ipv6dnsaddr(FAR const struct in6_addr *inaddr);
492+
int netlib_del_ipv6dnsaddr_by_index(int index);
489493
#endif
490494

491495
int netlib_set_mtu(FAR const char *ifname, int mtu);

netutils/dhcpc/dhcpc.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,32 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
378378

379379
case DHCP_OPTION_DNS_SERVER:
380380

381-
/* Get the DNS server address in network order */
381+
/* Get the DNS server addresses in network order.
382+
* DHCP option 6 can contain multiple DNS server addresses,
383+
* each 4 bytes long.
384+
*/
382385

383-
if (optptr + 6 <= end)
386+
if (optptr + 2 <= end)
384387
{
385-
memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
388+
uint8_t optlen = *(optptr + 1);
389+
uint8_t num_dns = optlen / 4;
390+
uint8_t i;
391+
392+
/* Limit to configured maximum */
393+
394+
if (num_dns > CONFIG_NETDB_DNSSERVER_NAMESERVERS)
395+
{
396+
num_dns = CONFIG_NETDB_DNSSERVER_NAMESERVERS;
397+
}
398+
399+
presult->num_dnsaddr = 0;
400+
for (i = 0; i < num_dns && (optptr + 2 + i * 4 + 4) <= end;
401+
i++)
402+
{
403+
memcpy(&presult->dnsaddr[i].s_addr, optptr + 2 + i * 4,
404+
4);
405+
presult->num_dnsaddr++;
406+
}
386407
}
387408
else
388409
{
@@ -946,11 +967,22 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
946967
ip4_addr2(presult->netmask.s_addr),
947968
ip4_addr3(presult->netmask.s_addr),
948969
ip4_addr4(presult->netmask.s_addr));
949-
ninfo("Got DNS server %u.%u.%u.%u\n",
950-
ip4_addr1(presult->dnsaddr.s_addr),
951-
ip4_addr2(presult->dnsaddr.s_addr),
952-
ip4_addr3(presult->dnsaddr.s_addr),
953-
ip4_addr4(presult->dnsaddr.s_addr));
970+
971+
/* Print all DNS servers received */
972+
973+
if (presult->num_dnsaddr > 0)
974+
{
975+
uint8_t i;
976+
for (i = 0; i < presult->num_dnsaddr; i++)
977+
{
978+
ninfo("Got DNS server %d: %u.%u.%u.%u\n", i,
979+
ip4_addr1(presult->dnsaddr[i].s_addr),
980+
ip4_addr2(presult->dnsaddr[i].s_addr),
981+
ip4_addr3(presult->dnsaddr[i].s_addr),
982+
ip4_addr4(presult->dnsaddr[i].s_addr));
983+
}
984+
}
985+
954986
ninfo("Got default router %u.%u.%u.%u\n",
955987
ip4_addr1(presult->default_router.s_addr),
956988
ip4_addr2(presult->default_router.s_addr),

netutils/netlib/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if(CONFIG_NETUTILS_NETLIB)
4747
endif()
4848
endif()
4949
if(CONFIG_NETDB_DNSCLIENT)
50-
list(APPEND SRCS netlib_setipv4dnsaddr.c)
50+
list(APPEND SRCS netlib_setipv4dnsaddr.c netlib_delipv4dnsaddr.c)
5151
endif()
5252
if(CONFIG_NET_ROUTE)
5353
list(APPEND SRCS netlib_ipv4route.c netlib_ipv4router.c)
@@ -70,7 +70,7 @@ if(CONFIG_NETUTILS_NETLIB)
7070
list(APPEND SRCS netlib_autoconfig.c netlib_obtainipv6addr.c)
7171
endif()
7272
if(CONFIG_NETDB_DNSCLIENT)
73-
list(APPEND SRCS netlib_setipv6dnsaddr.c)
73+
list(APPEND SRCS netlib_setipv6dnsaddr.c netlib_delipv6dnsaddr.c)
7474
endif()
7575
if(CONFIG_NET_ROUTE)
7676
list(APPEND SRCS netlib_ipv6route.c netlib_ipv6router.c)

netutils/netlib/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CSRCS += netlib_getarptab.c
4747
endif
4848
endif
4949
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
50-
CSRCS += netlib_setipv4dnsaddr.c
50+
CSRCS += netlib_setipv4dnsaddr.c netlib_delipv4dnsaddr.c
5151
endif
5252
ifeq ($(CONFIG_NET_ROUTE),y)
5353
CSRCS += netlib_ipv4route.c netlib_ipv4router.c
@@ -71,7 +71,7 @@ CSRCS += netlib_autoconfig.c
7171
CSRCS += netlib_obtainipv6addr.c
7272
endif
7373
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
74-
CSRCS += netlib_setipv6dnsaddr.c
74+
CSRCS += netlib_setipv6dnsaddr.c netlib_delipv6dnsaddr.c
7575
endif
7676
ifeq ($(CONFIG_NET_ROUTE),y)
7777
CSRCS += netlib_ipv6route.c netlib_ipv6router.c
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/****************************************************************************
2+
* apps/netutils/netlib/netlib_delipv4dnsaddr.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 <sys/socket.h>
28+
#include <stdint.h>
29+
#include <string.h>
30+
#include <errno.h>
31+
32+
#include <netinet/in.h>
33+
#include <nuttx/net/dns.h>
34+
35+
#include "netutils/netlib.h"
36+
37+
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)
38+
39+
/****************************************************************************
40+
* Public Functions
41+
****************************************************************************/
42+
43+
/****************************************************************************
44+
* Name: netlib_del_ipv4dnsaddr
45+
*
46+
* Description:
47+
* Remove a DNS server IPv4 address from the list by address
48+
*
49+
* Parameters:
50+
* inaddr The address to remove
51+
*
52+
* Return:
53+
* Zero (OK) is returned on success; A negated errno value is returned
54+
* on failure.
55+
*
56+
****************************************************************************/
57+
58+
int netlib_del_ipv4dnsaddr(FAR const struct in_addr *inaddr)
59+
{
60+
struct sockaddr_in addr;
61+
int ret = -EINVAL;
62+
63+
if (inaddr)
64+
{
65+
addr.sin_family = AF_INET;
66+
addr.sin_port = 0;
67+
memcpy(&addr.sin_addr, inaddr, sizeof(struct in_addr));
68+
bzero(&addr.sin_zero, sizeof(addr.sin_zero));
69+
70+
ret = dns_del_nameserver((FAR const struct sockaddr *)&addr,
71+
sizeof(struct sockaddr_in));
72+
}
73+
74+
return ret;
75+
}
76+
77+
/****************************************************************************
78+
* Name: netlib_del_ipv4dnsaddr_by_index
79+
*
80+
* Description:
81+
* Remove a DNS server IPv4 address from the list by index (0-based)
82+
*
83+
* Parameters:
84+
* index: The index of the DNS server to remove (0=first, 1=second, etc.)
85+
*
86+
* Return:
87+
* Zero (OK) is returned on success; A negated errno value is returned
88+
* on failure.
89+
*
90+
****************************************************************************/
91+
92+
int netlib_del_ipv4dnsaddr_by_index(int index)
93+
{
94+
return dns_del_nameserver_by_index(index);
95+
}
96+
97+
#endif /* CONFIG_NET_IPv4 && CONFIG_NETDB_DNSCLIENT */
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/****************************************************************************
2+
* apps/netutils/netlib/netlib_delipv6dnsaddr.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 <sys/socket.h>
28+
#include <string.h>
29+
#include <errno.h>
30+
31+
#include <netinet/in.h>
32+
#include <nuttx/net/dns.h>
33+
34+
#include "netutils/netlib.h"
35+
36+
#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NETDB_DNSCLIENT)
37+
38+
/****************************************************************************
39+
* Public Functions
40+
****************************************************************************/
41+
42+
/****************************************************************************
43+
* Name: netlib_del_ipv6dnsaddr
44+
*
45+
* Description:
46+
* Remove a DNS server IPv6 address from the list by address
47+
*
48+
* Parameters:
49+
* inaddr The IPv6 address to remove
50+
*
51+
* Return:
52+
* Zero (OK) is returned on success; A negated errno value is returned
53+
* on failure.
54+
*
55+
****************************************************************************/
56+
57+
int netlib_del_ipv6dnsaddr(FAR const struct in6_addr *inaddr)
58+
{
59+
struct sockaddr_in6 addr;
60+
int ret = -EINVAL;
61+
62+
if (inaddr)
63+
{
64+
/* Del the IPv6 DNS server address */
65+
66+
addr.sin6_family = AF_INET6;
67+
addr.sin6_port = 0;
68+
memcpy(&addr.sin6_addr, inaddr, sizeof(struct in6_addr));
69+
70+
ret = dns_del_nameserver((FAR const struct sockaddr *)&addr,
71+
sizeof(struct sockaddr_in6));
72+
}
73+
74+
return ret;
75+
}
76+
77+
/****************************************************************************
78+
* Name: netlib_del_ipv6dnsaddr_by_index
79+
*
80+
* Description:
81+
* Remove a DNS server IPv6 address from the list by index (0-based)
82+
*
83+
* Parameters:
84+
* index:The index of the DNS server to remove (0=first, 1=second, etc.)
85+
*
86+
* Return:
87+
* Zero (OK) is returned on success; A negated errno value is returned
88+
* on failure.
89+
*
90+
****************************************************************************/
91+
92+
int netlib_del_ipv6dnsaddr_by_index(int index)
93+
{
94+
return dns_del_nameserver_by_index(index);
95+
}
96+
97+
#endif /* CONFIG_NET_IPv6 && CONFIG_NETDB_DNSCLIENT */

netutils/netlib/netlib_obtainipv4addr.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,23 @@ static int dhcp_setup_result(FAR const char *ifname,
8181
}
8282

8383
#ifdef CONFIG_NETDB_DNSCLIENT
84-
if (ds->dnsaddr.s_addr != 0)
84+
/* Set all received DNS server addresses */
85+
86+
if (ds->num_dnsaddr > 0)
8587
{
86-
ret = netlib_set_ipv4dnsaddr(&ds->dnsaddr);
87-
if (ret < 0)
88+
uint8_t i;
89+
for (i = 0; i < ds->num_dnsaddr; i++)
8890
{
89-
nerr("ERROR: set the DNS server address failed: %d\n", ret);
90-
return ret;
91+
if (ds->dnsaddr[i].s_addr != 0)
92+
{
93+
ret = netlib_set_ipv4dnsaddr(&ds->dnsaddr[i]);
94+
if (ret < 0)
95+
{
96+
nerr("ERROR: set DNS server %d address failed: %d\n",
97+
i, ret);
98+
return ret;
99+
}
100+
}
91101
}
92102
}
93103
#endif

0 commit comments

Comments
 (0)