net/procfs: Support printing multiple IPv6 address per netdev

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-10-23 17:18:09 +08:00 committed by Xiang Xiao
parent 2b9633e652
commit 7f421a46ca
2 changed files with 36 additions and 8 deletions

View File

@ -61,6 +61,14 @@
# define CONFIG_C99_BOOL 1
#endif
/* ISO C99 supports Designated initializers */
#undef CONFIG_DESIGNATED_INITIALIZERS
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define CONFIG_DESIGNATED_INITIALIZERS 1
#endif
/* ISO C/C++11 atomic types support */
#undef CONFIG_HAVE_ATOMICS

View File

@ -36,6 +36,7 @@
#include <nuttx/net/netdev.h>
#include <nuttx/net/sixlowpan.h>
#include "inet/inet.h"
#include "netdev/netdev.h"
#include "utils/utils.h"
#include "procfs/procfs.h"
@ -43,6 +44,16 @@
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
!defined(CONFIG_FS_PROCFS_EXCLUDE_NET)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_NET_IPv4
# define NETSTAT_IPv6_IDX 2
#else
# define NETSTAT_IPv6_IDX 1
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@ -83,7 +94,13 @@ static const linegen_t g_netstat_linegen[] =
, netprocfs_inet4addresses
#endif
#ifdef CONFIG_NET_IPv6
# if defined(CONFIG_NETDEV_MULTIPLE_IPv6) && \
defined(CONFIG_DESIGNATED_INITIALIZERS)
, [NETSTAT_IPv6_IDX ... NETSTAT_IPv6_IDX + CONFIG_NETDEV_MAX_IPv6_ADDR - 1]
= netprocfs_inet6address
# else
, netprocfs_inet6address
# endif
, netprocfs_inet6draddress
#endif
#if !defined(CONFIG_NET_IPv4) && !defined(CONFIG_NET_IPv6)
@ -309,18 +326,26 @@ static int netprocfs_inet6address(FAR struct netprocfs_file_s *netfile)
FAR struct net_driver_s *dev;
char addrstr[INET6_ADDRSTRLEN];
uint8_t preflen;
int idx = netfile->lineno - NETSTAT_IPv6_IDX;
int len = 0;
DEBUGASSERT(netfile != NULL && netfile->dev != NULL);
dev = netfile->dev;
#ifdef CONFIG_NETDEV_MULTIPLE_IPv6
if (net_ipv6addr_cmp(dev->d_ipv6[idx].addr, g_ipv6_unspecaddr))
{
return 0;
}
#endif
/* Convert the 128 network mask to a human friendly prefix length */
preflen = net_ipv6_mask2pref(dev->d_ipv6netmask);
preflen = net_ipv6_mask2pref(dev->d_ipv6[idx].mask);
/* Show the assigned IPv6 address */
if (inet_ntop(AF_INET6, dev->d_ipv6addr, addrstr, INET6_ADDRSTRLEN))
if (inet_ntop(AF_INET6, dev->d_ipv6[idx].addr, addrstr, INET6_ADDRSTRLEN))
{
len += snprintf(&netfile->line[len], NET_LINELEN - len,
"\tinet6 addr: %s/%d\n", addrstr, preflen);
@ -339,22 +364,17 @@ static int netprocfs_inet6draddress(FAR struct netprocfs_file_s *netfile)
{
FAR struct net_driver_s *dev;
char addrstr[INET6_ADDRSTRLEN];
uint8_t preflen;
int len = 0;
DEBUGASSERT(netfile != NULL && netfile->dev != NULL);
dev = netfile->dev;
/* Convert the 128 network mask to a human friendly prefix length */
preflen = net_ipv6_mask2pref(dev->d_ipv6netmask);
/* Show the IPv6 default router address */
if (inet_ntop(AF_INET6, dev->d_ipv6draddr, addrstr, INET6_ADDRSTRLEN))
{
len += snprintf(&netfile->line[len], NET_LINELEN - len,
"\tinet6 DRaddr: %s/%d\n\n", addrstr, preflen);
"\tinet6 DRaddr: %s\n\n", addrstr);
}
return len;