From cb2b53a6a57376b00f38f75c9777278fecf24b03 Mon Sep 17 00:00:00 2001 From: krzysztof-cabaj Date: Thu, 27 Jul 2023 12:46:47 -0400 Subject: [PATCH] sys/shell/lwip: add gateway configuration --- sys/shell/cmds/lwip_netif.c | 184 ++++++++++++++++++------------------ 1 file changed, 94 insertions(+), 90 deletions(-) diff --git a/sys/shell/cmds/lwip_netif.c b/sys/shell/cmds/lwip_netif.c index 6c3a61c24f812..29cff17ffeb80 100644 --- a/sys/shell/cmds/lwip_netif.c +++ b/sys/shell/cmds/lwip_netif.c @@ -31,7 +31,6 @@ #include "arch/sys_arch.h" #include -//#include "net/ipv4/addr.h" #include "net/netif.h" @@ -118,18 +117,18 @@ static void _netif_list(struct netif *netif) #ifdef MODULE_LWIP_IPV4 static void _usage_add4(char *cmd) { - printf("usage: %s add4 /\n", cmd); - printf("usage: %s add4 / gw \n", cmd); + printf("usage: %s add4 /\n", cmd); + printf("usage: %s add4 / gw \n", cmd); } static void _lwip_prefix_to_subnet(int prefix, ip4_addr_t *subnet) { - uint32_t value = 0; - uint32_t tmp = 0x80000000; + uint32_t value = 0; + uint32_t tmp = 0x80000000; - for(int i = 0; i < prefix; i++) - { - value += tmp; + for (int i = 0; i < prefix; i++) + { + value += tmp; tmp = tmp >> 1; } subnet->addr = htonl(value); @@ -137,92 +136,97 @@ static void _lwip_prefix_to_subnet(int prefix, ip4_addr_t *subnet) static int _lwip_netif_add4(int argc, char **argv) { - if(argc != 4 && argc != 6) - { - printf("error: invalid number of parameters\n"); - _usage_add4(argv[0]); - return 1; - } - - struct netif *iface; - - sys_lock_tcpip_core(); - iface = netif_find(argv[2]); - - if(iface == NULL) - { - printf("error: invalid interface name (names are case sensitive)\n"); - sys_unlock_tcpip_core(); - return 1; - } - - char *ip_ptr, *prefix_ptr = NULL; - - ip_ptr = argv[3]; + struct netif *iface; + char *ip_ptr, *prefix_ptr = NULL; + ip4_addr_t ip, subnet, gw; + int prefix; + + if (argc != 4 && argc != 6) { + printf("error: invalid number of parameters\n"); + _usage_add4(argv[0]); + return 1; + } - while((*ip_ptr) != 0) - { - if((*ip_ptr) == '/') - { - *ip_ptr = 0; - prefix_ptr = ip_ptr + 1; - } + sys_lock_tcpip_core(); + iface = netif_find(argv[2]); - ip_ptr++; + if (iface == NULL) { + printf("error: invalid interface name (names are case sensitive)\n"); + sys_unlock_tcpip_core(); + return 1; } - ip_ptr = argv[3]; + ip_ptr = argv[3]; + while ((*ip_ptr) != 0) { + if((*ip_ptr) == '/') + { + *ip_ptr = 0; + prefix_ptr = ip_ptr + 1; + } - if(prefix_ptr == NULL) - { - printf("error: ivalid IPv4 prefix notation\n"); - _usage_add4(argv[0]); - sys_unlock_tcpip_core(); - return 1; - } + ip_ptr++; + } + ip_ptr = argv[3]; - ip4_addr_t ip, subnet; + if (prefix_ptr == NULL) { + printf("error: invalid IPv4 prefix notation\n"); + _usage_add4(argv[0]); + sys_unlock_tcpip_core(); + return 1; + } - subnet.addr = 0x00ffffff; + if(inet_pton(AF_INET, ip_ptr, &ip.addr) != 1) { + printf("error:invalid IPv4 address\n"); + sys_unlock_tcpip_core(); + return 1; + } - if(inet_pton(AF_INET, ip_ptr, &ip.addr) != 1) - { - printf("error:invalid IPv4 address\n"); - sys_unlock_tcpip_core(); - return 1; - } + prefix = atoi(prefix_ptr); - int prefix = atoi(prefix_ptr); + if ( prefix < 0 || prefix > 32) { + printf("error:invalid prefix, should be in range <0, 32>\n"); + sys_unlock_tcpip_core(); + return 1; + } - if( prefix < 0 || prefix > 32) - { - printf("error:invalid prefix, should be in range <0, 32>\n"); - sys_unlock_tcpip_core(); - return 1; - } + _lwip_prefix_to_subnet(prefix, &subnet); - _lwip_prefix_to_subnet(prefix, &subnet); + if (argc == 4) + netif_set_addr(iface, &ip, &subnet, NULL); + else { + if (strcmp("gw", argv[4]) != 0) { + printf("error: invalid subcommand \"%s\"\n", argv[4]); + _usage_add4(argv[0]); + sys_unlock_tcpip_core(); + return 1; + } - netif_set_addr(iface, &ip, &subnet, NULL); + if (inet_pton(AF_INET, argv[5], &gw.addr) != 1) { + printf("error: invalid gateway address\n"); + sys_unlock_tcpip_core(); + return 1; + } + netif_set_addr(iface, &ip, &subnet, &gw); + } - sys_unlock_tcpip_core(); - return 0; + sys_unlock_tcpip_core(); + return 0; } #endif #ifdef MODULE_LWIP_IPV6 static void _usage_add6(char *cmd) { - printf("usage: %s add6 - currently not implemented\n", cmd); + printf("usage: %s add6 - currently not implemented\n", cmd); } static int _lwip_netif_add6(int argc, char **argv) { - (void)argc; - (void)argv; - printf("error: currently not implemented\n"); + (void)argc; + (void)argv; + printf("error: currently not implemented\n"); - return 0; + return 0; } #endif @@ -301,35 +305,35 @@ static int _lwip_netif_set_u32_from_ip4(netif_t *iface, netopt_t opt, static int _lwip_netif_set(char *cmd, netif_t *iface, char *key, char *value) { - (void)cmd; - (void)iface; - (void)key; - (void)value; + (void)cmd; + (void)iface; + (void)key; + (void)value; #ifdef MODULE_LWIP_IPV4 - if (strcmp("ip4", key) == 0) { - return _lwip_netif_set_u32_from_ip4(iface, NETOPT_IPV4_ADDR, 0, value); - } - if (strcmp("mask", key) == 0) { - return _lwip_netif_set_u32_from_ip4(iface, NETOPT_IPV4_MASK, 0, value); - } + if (strcmp("ip4", key) == 0) { + return _lwip_netif_set_u32_from_ip4(iface, NETOPT_IPV4_ADDR, 0, value); + } + if (strcmp("mask", key) == 0) { + return _lwip_netif_set_u32_from_ip4(iface, NETOPT_IPV4_MASK, 0, value); + } #endif - else - _set_usage(cmd); - return 1; + else + _set_usage(cmd); + return 1; } static void _lwip_netif_help(char *cmd) { - printf("usage: %s\n", cmd); - printf("usage: %s help\n", cmd); + printf("usage: %s\n", cmd); + printf("usage: %s help\n", cmd); #ifdef MODULE_LWIP_IPV4 - _usage_add4(cmd); + _usage_add4(cmd); #endif #ifdef MODULE_LWIP_IPV6 - _usage_add6(cmd); + _usage_add6(cmd); #endif - _set_usage(cmd); + _set_usage(cmd); } static int _lwip_netif_config(int argc, char **argv) @@ -376,9 +380,9 @@ static int _lwip_netif_config(int argc, char **argv) iface = netif_get_by_name(argv[1]); if (!iface) { printf("error: invalid interface given\n"); - return 1; + return 1; } - + _lwip_netif_set(argv[0], iface, argv[3], argv[4]); } else