Skip to content

Commit

Permalink
sys/shell/lwip: add gateway configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-cabaj committed Jul 27, 2023
1 parent 6cb7892 commit 39dfd21
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions sys/shell/cmds/lwip_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include "arch/sys_arch.h"
#include <arpa/inet.h>
//#include "net/ipv4/addr.h"

#ifdef MODULE_LWIP_IPV6
static void _netif_list_ipv6(struct netif *netif, int addr_index, uint8_t state)
Expand Down Expand Up @@ -135,15 +134,18 @@ static void _lwip_prefix_to_subnet(int prefix, ip4_addr_t *subnet)

static int _lwip_netif_add4(int argc, char **argv)
{
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;
}

struct netif *iface;

sys_lock_tcpip_core();
iface = netif_find(argv[2]);

Expand All @@ -154,10 +156,7 @@ static int _lwip_netif_add4(int argc, char **argv)
return 1;
}

char *ip_ptr, *prefix_ptr = NULL;

ip_ptr = argv[3];

while((*ip_ptr) != 0)
{
if((*ip_ptr) == '/')
Expand All @@ -168,29 +167,24 @@ static int _lwip_netif_add4(int argc, char **argv)

ip_ptr++;
}

ip_ptr = argv[3];

if(prefix_ptr == NULL)
{
printf("error: ivalid IPv4 prefix notation\n");
printf("error: invalid IPv4 prefix notation\n");
_usage_add4(argv[0]);
sys_unlock_tcpip_core();
return 1;
}

ip4_addr_t ip, subnet;

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;
}

int prefix = atoi(prefix_ptr);
prefix = atoi(prefix_ptr);

if( prefix < 0 || prefix > 32)
{
Expand All @@ -201,7 +195,26 @@ static int _lwip_netif_add4(int argc, char **argv)

_lwip_prefix_to_subnet(prefix, &subnet);

netif_set_addr(iface, &ip, &subnet, NULL);
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;
}

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;
Expand Down

0 comments on commit 39dfd21

Please sign in to comment.