1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/shell/commands/sc_lwip_netif.c
Erik Ekman eab317749f sys/shell: Add lwIP ifconfig shell command
Lists state, link type, v4/v6 addresses.
Currently read-only.

Using lwIP debug system to print addresses, to limit dependencies
and work with dual stack setup. Most other code seems to only
allow either v4 or v6 networking. For that to compile I
had to change the `SZT_F` format string due to this error:
```
error: format '%lu' expects argument of type 'long unsigned int',
but argument 2 has type 'size_t {aka unsigned int}'
```
Switching to the lwIP default format string here.

Outputs the following on my ESP32 board with Ethernet,
when both v4 and v6 are enabled in examples/paho-mqtt:
```
> ifconfig
Iface ET0 HWaddr: 24:0a:c4:e6:0e:9f Link: up State: up
        Link type: wired
        inet addr: 10.4.4.81 mask: 255.255.254.0 gw: 10.4.4.1
        inet6 addr: fe80:0:0:0:260a:c4ff:fee6:e9f scope: link
        inet6 addr: 2001:db8:1000:0:260a:c4ff:fee6:e9f scope: global
Iface ET1 HWaddr: 24:0a:c4:e6:0e:9c Link: up State: up
        Link type: wireless
        inet addr: 10.4.4.82 mask: 255.255.254.0 gw: 10.4.4.1
        inet6 addr: fe80:0:0:0:260a:c4ff:fee6:e9c scope: link
        inet6 addr: 2001:db8:1000:0:260a:c4ff:fee6:e9c scope: global
>
```
2021-02-23 11:47:45 +01:00

102 lines
2.8 KiB
C

/*
* Copyright (C) 2021 Google LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_shell_commands
* @{
*
* @file
* @brief Shell command for printing lwIP network interface status
*
* @author Erik Ekman <eekman@google.com>
*
* @}
*/
#include <stdio.h>
#include "lwip/netif.h"
#include "net/netdev.h"
#include "net/netopt.h"
#ifdef MODULE_LWIP_IPV6
static void _netif_list_ipv6(struct netif *netif, int addr_index) {
printf(" inet6 addr: ");
ip_addr_debug_print(LWIP_DBG_ON, netif_ip_addr6(netif, addr_index));
printf(" scope: ");
if (ip6_addr_isglobal(netif_ip6_addr(netif, addr_index))) {
printf("global");
} else if (ip6_addr_islinklocal(netif_ip6_addr(netif, addr_index))) {
printf("link");
} else if (ip6_addr_issitelocal(netif_ip6_addr(netif, addr_index))) {
printf("site");
} else {
printf("unknown");
}
printf("\n");
}
#endif
static void _netif_list(struct netif *netif) {
int i;
struct netdev *dev = netif->state;
printf("Iface %c%c%u ", netif->name[0], netif->name[1], netif->num);
printf("HWaddr: ");
for (i = 0; i < netif->hwaddr_len; i++) {
printf("%02x", netif->hwaddr[i]);
if ((i+1) < netif->hwaddr_len) {
printf(":");
}
}
printf(" Link: %s State: %s\n",
netif_is_link_up(netif) ? "up" : "down",
netif_is_up(netif) ? "up" : "down");
printf(" Link type: %s\n",
(dev->driver->get(dev, NETOPT_IS_WIRED, &i, sizeof(i)) > 0) ?
"wired" : "wireless");
#ifdef MODULE_LWIP_IPV4
printf(" inet addr: ");
ip_addr_debug_print(LWIP_DBG_ON, netif_ip_addr4(netif));
printf(" mask: ");
ip_addr_debug_print(LWIP_DBG_ON, netif_ip_netmask4(netif));
printf(" gw: ");
ip_addr_debug_print(LWIP_DBG_ON, netif_ip_gw4(netif));
printf("\n");
#endif
#ifdef MODULE_LWIP_IPV6
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (netif_ip6_addr_state(netif, i) != IP6_ADDR_INVALID) {
_netif_list_ipv6(netif, i);
}
}
#endif
}
int _lwip_netif_config(int argc, char **argv)
{
if (argc < 2) {
/* List in interface order, which is normally reverse of list order */
struct netif *netif;
int netifs = 0;
int listed = 0;
u8_t i;
NETIF_FOREACH(netif) netifs++;
for (i = 0; listed < netifs; i++) {
NETIF_FOREACH(netif) {
if (i == netif->num) {
_netif_list(netif);
listed++;
}
}
}
return 0;
}
printf("%s takes no arguments.\n", argv[0]);
return 1;
}