Skip to content

Commit 71d47cd

Browse files
sys/shell/lwip: merge ifconfig add4 and add6 options to one-ifconfig add
1 parent 67abda0 commit 71d47cd

1 file changed

Lines changed: 42 additions & 10 deletions

File tree

sys/shell/cmds/lwip_netif.c

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2021 Google LLC
3-
* 2023 Krzysztof Cabaj <kcabaj@gmail.com>
3+
* 2024 Krzysztof Cabaj <kcabaj@gmail.com>
44
*
55
* This file is subject to the terms and conditions of the GNU Lesser
66
* General Public License v2.1. See the file LICENSE in the top level
@@ -120,8 +120,8 @@ static void _netif_list(struct netif *netif)
120120
#ifdef MODULE_LWIP_IPV4
121121
static void _usage_add4(char *cmd)
122122
{
123-
printf("usage: %s add4 <interface> <IPv4>/<prefix>\n", cmd);
124-
printf("usage: %s add4 <interface> <IPv4>/<prefix> gw <IPv4>\n", cmd);
123+
printf("usage: %s add <interface> <IPv4>/<prefix>\n", cmd);
124+
printf("usage: %s add <interface> <IPv4>/<prefix> gw <IPv4>\n", cmd);
125125
}
126126

127127
static void _lwip_prefix_to_subnet(int prefix, ip4_addr_t *subnet)
@@ -219,14 +219,14 @@ static int _lwip_netif_add4(int argc, char **argv)
219219
#ifdef MODULE_LWIP_IPV6
220220
static void _usage_add6(char *cmd)
221221
{
222-
printf("usage: %s add6 - currently not implemented\n", cmd);
222+
printf("usage: %s add for LWIP IPv6 currently not implemented\n", cmd);
223223
}
224224

225225
static int _lwip_netif_add6(int argc, char **argv)
226226
{
227227
(void)argc;
228228
(void)argv;
229-
printf("error: currently not implemented\n");
229+
printf("error: LWIP IPv6 configuration currently not implemented\n");
230230

231231
return 0;
232232
}
@@ -267,14 +267,46 @@ static int _lwip_netif_config(int argc, char **argv)
267267
if (strcmp("help", argv[1]) == 0) {
268268
_lwip_netif_help(argv[0]);
269269
}
270+
#if defined(MODULE_LWIP_IPV4) || defined(MODULE_LWIP_IPV6)
271+
else if (strcmp("add", argv[1]) == 0) {
272+
if (argc != 4 && argc != 6) {
273+
printf("error: invalid number of parameters\n");
270274
#ifdef MODULE_LWIP_IPV4
271-
else if (strcmp("add4", argv[1]) == 0) {
272-
_lwip_netif_add4(argc, argv);
273-
}
275+
_usage_add4(argv[0]);
274276
#endif
275277
#ifdef MODULE_LWIP_IPV6
276-
else if (strcmp("add6", argv[1]) == 0) {
277-
_lwip_netif_add6(argc, argv);
278+
_usage_add6(argv[0]);
279+
#endif
280+
return 0;
281+
}
282+
283+
char *prefix_ptr = strchr(argv[3], '/');
284+
285+
if (prefix_ptr == NULL) {
286+
printf("error: provide IP address with prefix\n");
287+
return 0;
288+
}
289+
290+
*prefix_ptr = 0;
291+
292+
#ifdef MODULE_LWIP_IPV4
293+
ipv4_addr_t ip4;
294+
295+
if (ipv4_addr_from_buf(&ip4, argv[3], strlen(argv[3])) != NULL) {
296+
*prefix_ptr = '/';
297+
_lwip_netif_add4(argc, argv);
298+
return 1;
299+
}
300+
#endif
301+
#ifdef MODULE_LWIP_IPV6
302+
ipv6_addr_t ip6;
303+
if (ipv6_addr_from_buf(&ip6, argv[3], strlen(argv[3])) != NULL) {
304+
*prefix_ptr = '/';
305+
_lwip_netif_add6(argc, argv);
306+
return 1;
307+
}
308+
#endif
309+
printf("error: use proper IPv4 or IPv6 address\n");
278310
}
279311
#endif
280312
else {

0 commit comments

Comments
 (0)