Skip to content

Commit f3746b2

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 - fix some test case build error Signed-off-by: nuttxs <zhaoqing.zhang@sony.com>
1 parent 099c8ff commit f3746b2

File tree

13 files changed

+325
-30
lines changed

13 files changed

+325
-30
lines changed

examples/bridge/bridge_main.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static int briget_net1_setup(void)
8181
struct dhcpc_state ds;
8282
void *handle;
8383
char inetaddr[INET_ADDRSTRLEN];
84+
int ret;
8485
#endif
8586

8687
printf("NET1: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME);
@@ -162,9 +163,17 @@ static int briget_net1_setup(void)
162163
&ds.default_router);
163164
}
164165

165-
if (ds.dnsaddr.s_addr != 0)
166+
for (int i = 0; i < ds.num_dnsaddr; i++)
166167
{
167-
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
168+
if (ds.dnsaddr[i].s_addr != 0)
169+
{
170+
ret = netlib_set_ipv4dnsaddr(&ds.dnsaddr[i]);
171+
if (ret < 0)
172+
{
173+
nerr("NET1 ERROR: Set DNS server %d:%s address failed: %d\n",
174+
i, inet_ntoa(ds.dnsaddr[i]), ret);
175+
}
176+
}
168177
}
169178

170179
dhcpc_close(handle);
@@ -213,6 +222,7 @@ static int briget_net2_setup(void)
213222
struct dhcpc_state ds;
214223
void *handle;
215224
char inetaddr[INET_ADDRSTRLEN];
225+
int ret;
216226
#endif
217227

218228
printf("NET2: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME);
@@ -288,9 +298,17 @@ static int briget_net2_setup(void)
288298
&ds.default_router);
289299
}
290300

291-
if (ds.dnsaddr.s_addr != 0)
301+
for (int i = 0; i < ds.num_dnsaddr; i++)
292302
{
293-
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
303+
if (ds.dnsaddr[i].s_addr != 0)
304+
{
305+
ret = netlib_set_ipv4dnsaddr(&ds.dnsaddr[i]);
306+
if (ret < 0)
307+
{
308+
nerr("NET2 ERROR: Set DNS server %d:%s address failed: %d\n",
309+
i, inet_ntoa(ds.dnsaddr[i]), ret);
310+
}
311+
}
294312
}
295313

296314
dhcpc_close(handle);

examples/discover/discover_main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ int main(int argc, FAR char *argv[])
138138
{
139139
struct dhcpc_state ds;
140140
char inetaddr[INET_ADDRSTRLEN];
141+
int ret;
141142

142143
dhcpc_request(handle, &ds);
143144
netlib_set_ipv4addr("eth0", &ds.ipaddr);
@@ -152,9 +153,17 @@ int main(int argc, FAR char *argv[])
152153
netlib_set_dripv4addr("eth0", &ds.default_router);
153154
}
154155

155-
if (ds.dnsaddr.s_addr != 0)
156+
for (int i = 0; i < ds.num_dnsaddr; i++)
156157
{
157-
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
158+
if (ds.dnsaddr[i].s_addr != 0)
159+
{
160+
ret = netlib_set_ipv4dnsaddr(&ds.dnsaddr[i]);
161+
if (ret < 0)
162+
{
163+
nerr("ERROR: Set DNS server %d:%s address failed: %d\n",
164+
i, inet_ntoa(ds.dnsaddr[i]), ret);
165+
}
166+
}
158167
}
159168

160169
dhcpc_close(handle);

examples/tcpecho/tcpecho_main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static int tcpecho_netsetup(void)
9191
struct dhcpc_state ds;
9292
void *handle;
9393
char inetaddr[INET_ADDRSTRLEN];
94+
int ret;
9495
#endif
9596

9697
/* Many embedded network interfaces must have a software assigned MAC */
@@ -166,9 +167,17 @@ static int tcpecho_netsetup(void)
166167
netlib_set_dripv4addr("eth0", &ds.default_router);
167168
}
168169

169-
if (ds.dnsaddr.s_addr != 0)
170+
for (int i = 0; i < ds.num_dnsaddr; i++)
170171
{
171-
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
172+
if (ds.dnsaddr[i].s_addr != 0)
173+
{
174+
ret = netlib_set_ipv4dnsaddr(&ds.dnsaddr[i]);
175+
if (ret < 0)
176+
{
177+
nerr("ERROR: Set DNS server %d:%s address failed: %d\n",
178+
i, inet_ntoa(ds.dnsaddr[i]), ret);
179+
}
180+
}
172181
}
173182

174183
dhcpc_close(handle);

examples/webserver/webserver_main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ int main(int argc, FAR char *argv[])
169169
{
170170
struct dhcpc_state ds;
171171
char inetaddr[INET_ADDRSTRLEN];
172+
int ret;
172173

173174
dhcpc_request(handle, &ds);
174175
netlib_set_ipv4addr("eth0", &ds.ipaddr);
@@ -183,9 +184,17 @@ int main(int argc, FAR char *argv[])
183184
netlib_set_dripv4addr("eth0", &ds.default_router);
184185
}
185186

186-
if (ds.dnsaddr.s_addr != 0)
187+
for (int i = 0; i < ds.num_dnsaddr; i++)
187188
{
188-
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
189+
if (ds.dnsaddr[i].s_addr != 0)
190+
{
191+
ret = netlib_set_ipv4dnsaddr(&ds.dnsaddr[i]);
192+
if (ret < 0)
193+
{
194+
nerr("ERROR: Set DNS server %d:%s address failed: %d\n",
195+
i, inet_ntoa(ds.dnsaddr[i]), ret);
196+
}
197+
}
189198
}
190199

191200
dhcpc_close(handle);

examples/xmlrpc/xmlrpc_main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ static int xmlrpc_netinit(void)
334334
{
335335
struct dhcpc_state ds;
336336
char inetaddr[INET_ADDRSTRLEN];
337+
int ret;
337338

338339
dhcpc_request(handle, &ds);
339340
netlib_set_ipv4addr("eth0", &ds.ipaddr);
@@ -348,9 +349,17 @@ static int xmlrpc_netinit(void)
348349
netlib_set_dripv4addr("eth0", &ds.default_router);
349350
}
350351

351-
if (ds.dnsaddr.s_addr != 0)
352+
for (int i = 0; i < ds.num_dnsaddr; i++)
352353
{
353-
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
354+
if (ds.dnsaddr[i].s_addr != 0)
355+
{
356+
ret = netlib_set_ipv4dnsaddr(&ds.dnsaddr[i]);
357+
if (ret < 0)
358+
{
359+
nerr("ERROR: Set DNS server %d:%s address failed: %d\n",
360+
i, inet_ntoa(ds.dnsaddr[i]), ret);
361+
}
362+
}
354363
}
355364

356365
dhcpc_close(handle);

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

0 commit comments

Comments
 (0)