This is an automated email from the ASF dual-hosted git repository.
jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
The following commit(s) were added to refs/heads/master by this push:
new 5ecc5daf8 net/lwip: Add netstat command
5ecc5daf8 is described below
commit 5ecc5daf830a1938b0fbc74482c107da91fa7989
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Wed Mar 11 10:29:25 2026 +0100
net/lwip: Add netstat command
This adds netstat command to check active
sockets similar to ux netstat command
compat> /lwip netstat
037131 TCP 0.0.0.0:5001 *.* LISTEN
037133 UDP 0.0.0.0:68 *.*
Signed-off-by: Jerzy Kasenberg <[email protected]>
---
net/ip/lwip_mn/pkg.yml | 5 +++
net/ip/lwip_mn/src/lwip_cli.c | 85 ++++++++++++++++++++++++++++++++++++++-----
2 files changed, 81 insertions(+), 9 deletions(-)
diff --git a/net/ip/lwip_mn/pkg.yml b/net/ip/lwip_mn/pkg.yml
index 3460dc91f..ccf93444b 100644
--- a/net/ip/lwip_mn/pkg.yml
+++ b/net/ip/lwip_mn/pkg.yml
@@ -37,3 +37,8 @@ pkg.deps.STD_LWIPOPTS:
pkg.init:
ip_init: 'MYNEWT_VAL(IP_SYSINIT_STAGE)'
+
+pkg.whole_archive: true
+
+pkg.link_tables:
+ - lwip_commands
diff --git a/net/ip/lwip_mn/src/lwip_cli.c b/net/ip/lwip_mn/src/lwip_cli.c
index b622f725c..7de4d8d98 100644
--- a/net/ip/lwip_mn/src/lwip_cli.c
+++ b/net/ip/lwip_mn/src/lwip_cli.c
@@ -29,7 +29,8 @@
#include <lwip/tcpip.h>
#include <lwip/dhcp.h>
-#include "ip_priv.h"
+#include <ip_priv.h>
+#include <lwip/priv/tcp_priv.h>
static void
lwip_nif_print(struct mn_itf *itf)
@@ -98,6 +99,22 @@ lwip_nif_up(const char *name)
return 0;
}
+static int
+lwip_lsif_cmd(int argc, char **argv)
+{
+ struct mn_itf itf = {};
+ int rc;
+
+ while (1) {
+ rc = mn_itf_getnext(&itf);
+ if (rc) {
+ break;
+ }
+ lwip_nif_print(&itf);
+ }
+ return 0;
+}
+
static int
lwip_cli(int argc, char **argv)
{
@@ -105,14 +122,7 @@ lwip_cli(int argc, char **argv)
struct mn_itf itf;
if (argc == 1 || !strcmp(argv[1], "listif")) {
- memset(&itf, 0, sizeof(itf));
- while (1) {
- rc = mn_itf_getnext(&itf);
- if (rc) {
- break;
- }
- lwip_nif_print(&itf);
- }
+ lwip_lsif_cmd(argc - 1, argv + 1);
} else if (mn_itf_get(argv[1], &itf) == 0) {
if (argc == 2) {
lwip_nif_print(&itf);
@@ -131,11 +141,68 @@ lwip_cli(int argc, char **argv)
return 0;
}
+static const char *const tcp_state_str[] = {
+ "CLOSED", "LISTEN", "SYN_SENT", "SYN_RCVD",
+ "ESTABLISHED", "FIN_WAIT_1", "FIN_WAIT_2", "CLOSE_WAIT",
+ "CLOSING", "LAST_ACK", "TIME_WAIT",
+};
+
+static void
+lwip_netstat_cmd(int argc, char **argv)
+{
+ char buf1[48];
+ char buf2[48];
+ const struct tcp_pcb *tcp_pcb;
+ const struct udp_pcb *udp_pcb;
+ int len;
+
+ for (int i = 0; i < 4; ++i) {
+ for (tcp_pcb = *tcp_pcb_lists[i]; tcp_pcb != NULL; tcp_pcb =
tcp_pcb->next) {
+ ipaddr_ntoa_r(&tcp_pcb->local_ip, buf1, sizeof(buf1));
+ len = strlen(buf1);
+ sprintf(buf1 + len, ":%d", tcp_pcb->local_port);
+ if (tcp_pcb->state >= ESTABLISHED) {
+ ipaddr_ntoa_r(&tcp_pcb->remote_ip, buf2, sizeof(buf2));
+ len = strlen(buf2);
+ sprintf(buf2 + len, ":%d", tcp_pcb->remote_port);
+ } else {
+ strcpy(buf2, "*.*");
+ }
+ if (IP_IS_V4_VAL(tcp_pcb->local_ip)) {
+ console_printf("TCP %-22s %-22s %s\n", buf1, buf2,
+ tcp_state_str[tcp_pcb->state]);
+ } else {
+ console_printf("TCP %s %s %s\n", buf1, buf2,
+ tcp_state_str[tcp_pcb->state]);
+ }
+ }
+ }
+
+ for (udp_pcb = udp_pcbs; udp_pcb != NULL; udp_pcb = udp_pcb->next) {
+ ipaddr_ntoa_r(&udp_pcb->local_ip, buf1, sizeof(buf1));
+ len = strlen(buf1);
+ sprintf(buf1 + len, ":%d", udp_pcb->local_port);
+ if (IP_IS_V4_VAL(udp_pcb->local_ip)) {
+ console_printf("UDP %-22s *.*\n", buf1);
+ } else {
+ console_printf("UDP %s\n", buf1);
+ }
+ }
+}
+
struct shell_cmd lwip_cli_cmd = {
.sc_cmd = "ip",
.sc_cmd_func = lwip_cli
};
+#if LWIP_IPV6 || LWIP_IPV4
+SHELL_MODULE_CMD(lwip, netstat, lwip_netstat_cmd, NULL);
+#endif
+SHELL_MODULE_CMD(lwip, lsif, lwip_lsif_cmd, NULL);
+SHELL_MODULE_CMD(lwip, ip, lwip_cli, NULL);
+
+SHELL_MODULE_WITH_LINK_TABLE(lwip);
+
void
lwip_cli_init(void)
{