Skip to content

Commit 53a4fdf

Browse files
committed
net: shell: Print info about websocket
Add "net websocket" command that displays websocket information. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent e4bf7f6 commit 53a4fdf

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

subsys/net/ip/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ zephyr_library_sources_ifdef(CONFIG_NET_PROMISCUOUS_MODE promiscuous.c)
3535

3636
if(CONFIG_NET_SHELL)
3737
zephyr_library_include_directories(. ${ZEPHYR_BASE}/subsys/net/l2)
38+
zephyr_library_include_directories(. ${ZEPHYR_BASE}/subsys/net/lib)
3839
zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)
3940
endif()

subsys/net/ip/net_shell.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
6565
#include "net_shell.h"
6666
#include "net_stats.h"
6767

68+
#include <sys/fdtable.h>
69+
#include "websocket/websocket_internal.h"
70+
6871
#define PR(fmt, ...) \
6972
shell_fprintf(shell, SHELL_NORMAL, fmt, ##__VA_ARGS__)
7073

@@ -3906,6 +3909,71 @@ static int cmd_net_vlan_del(const struct shell *shell, size_t argc,
39063909
return 0;
39073910
}
39083911

3912+
static void websocket_context_cb(struct websocket_context *context,
3913+
void *user_data)
3914+
{
3915+
#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4)
3916+
#define ADDR_LEN NET_IPV6_ADDR_LEN
3917+
#elif defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6)
3918+
#define ADDR_LEN NET_IPV4_ADDR_LEN
3919+
#else
3920+
#define ADDR_LEN NET_IPV6_ADDR_LEN
3921+
#endif
3922+
struct net_shell_user_data *data = user_data;
3923+
const struct shell *shell = data->shell;
3924+
struct net_context *net_ctx;
3925+
int *count = data->user_data;
3926+
/* +7 for []:port */
3927+
char addr_local[ADDR_LEN + 7];
3928+
char addr_remote[ADDR_LEN + 7] = "";
3929+
3930+
net_ctx = z_get_fd_obj(context->real_sock, NULL, 0);
3931+
if (net_ctx == NULL) {
3932+
PR_ERROR("Invalid fd %d");
3933+
return;
3934+
}
3935+
3936+
get_addresses(net_ctx, addr_local, sizeof(addr_local),
3937+
addr_remote, sizeof(addr_remote));
3938+
3939+
PR("[%2d] %p/%p\t%p %c%c%c %16s\t%16s\n",
3940+
(*count) + 1, context, net_ctx,
3941+
net_context_get_iface(net_ctx),
3942+
net_context_get_family(net_ctx) == AF_INET6 ? '6' :
3943+
(net_context_get_family(net_ctx) == AF_INET ? '4' : ' '),
3944+
net_context_get_type(net_ctx) == SOCK_DGRAM ? 'D' :
3945+
(net_context_get_type(net_ctx) == SOCK_STREAM ? 'S' :
3946+
(net_context_get_type(net_ctx) == SOCK_RAW ? 'R' : ' ')),
3947+
net_context_get_ip_proto(net_ctx) == IPPROTO_UDP ? 'U' :
3948+
(net_context_get_ip_proto(net_ctx) == IPPROTO_TCP ? 'T' : ' '),
3949+
addr_local, addr_remote);
3950+
3951+
(*count)++;
3952+
}
3953+
3954+
static int cmd_net_websocket(const struct shell *shell, size_t argc,
3955+
char *argv[])
3956+
{
3957+
struct net_shell_user_data user_data;
3958+
int count = 0;
3959+
3960+
ARG_UNUSED(argc);
3961+
ARG_UNUSED(argv);
3962+
3963+
PR(" websocket/net_ctx\tIface Local \tRemote\n");
3964+
3965+
user_data.shell = shell;
3966+
user_data.user_data = &count;
3967+
3968+
websocket_context_foreach(websocket_context_cb, &user_data);
3969+
3970+
if (count == 0) {
3971+
PR("No connections\n");
3972+
}
3973+
3974+
return 0;
3975+
}
3976+
39093977
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_arp,
39103978
SHELL_CMD(flush, NULL, "Remove all entries from ARP cache.",
39113979
cmd_net_arp_flush),
@@ -4221,6 +4289,9 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_commands,
42214289
SHELL_CMD(tcp, &net_cmd_tcp, "Connect/send/close TCP connection.",
42224290
cmd_net_tcp),
42234291
SHELL_CMD(vlan, &net_cmd_vlan, "Show VLAN information.", cmd_net_vlan),
4292+
SHELL_CMD(websocket, NULL, "Print information about WebSocket "
4293+
"connections.",
4294+
cmd_net_websocket),
42244295
SHELL_SUBCMD_SET_END
42254296
);
42264297

0 commit comments

Comments
 (0)