Skip to content

Commit

Permalink
net: shell: Print info about websocket
Browse files Browse the repository at this point in the history
Add "net websocket" command that displays websocket information.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information
jukkar committed Sep 9, 2019
1 parent e4bf7f6 commit 53a4fdf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions subsys/net/ip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ zephyr_library_sources_ifdef(CONFIG_NET_PROMISCUOUS_MODE promiscuous.c)

if(CONFIG_NET_SHELL)
zephyr_library_include_directories(. ${ZEPHYR_BASE}/subsys/net/l2)
zephyr_library_include_directories(. ${ZEPHYR_BASE}/subsys/net/lib)
zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)
endif()
71 changes: 71 additions & 0 deletions subsys/net/ip/net_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
#include "net_shell.h"
#include "net_stats.h"

#include <sys/fdtable.h>
#include "websocket/websocket_internal.h"

#define PR(fmt, ...) \
shell_fprintf(shell, SHELL_NORMAL, fmt, ##__VA_ARGS__)

Expand Down Expand Up @@ -3906,6 +3909,71 @@ static int cmd_net_vlan_del(const struct shell *shell, size_t argc,
return 0;
}

static void websocket_context_cb(struct websocket_context *context,
void *user_data)
{
#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4)
#define ADDR_LEN NET_IPV6_ADDR_LEN
#elif defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6)
#define ADDR_LEN NET_IPV4_ADDR_LEN
#else
#define ADDR_LEN NET_IPV6_ADDR_LEN
#endif
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
struct net_context *net_ctx;
int *count = data->user_data;
/* +7 for []:port */
char addr_local[ADDR_LEN + 7];
char addr_remote[ADDR_LEN + 7] = "";

net_ctx = z_get_fd_obj(context->real_sock, NULL, 0);
if (net_ctx == NULL) {
PR_ERROR("Invalid fd %d");
return;
}

get_addresses(net_ctx, addr_local, sizeof(addr_local),
addr_remote, sizeof(addr_remote));

PR("[%2d] %p/%p\t%p %c%c%c %16s\t%16s\n",
(*count) + 1, context, net_ctx,
net_context_get_iface(net_ctx),
net_context_get_family(net_ctx) == AF_INET6 ? '6' :
(net_context_get_family(net_ctx) == AF_INET ? '4' : ' '),
net_context_get_type(net_ctx) == SOCK_DGRAM ? 'D' :
(net_context_get_type(net_ctx) == SOCK_STREAM ? 'S' :
(net_context_get_type(net_ctx) == SOCK_RAW ? 'R' : ' ')),
net_context_get_ip_proto(net_ctx) == IPPROTO_UDP ? 'U' :
(net_context_get_ip_proto(net_ctx) == IPPROTO_TCP ? 'T' : ' '),
addr_local, addr_remote);

(*count)++;
}

static int cmd_net_websocket(const struct shell *shell, size_t argc,
char *argv[])
{
struct net_shell_user_data user_data;
int count = 0;

ARG_UNUSED(argc);
ARG_UNUSED(argv);

PR(" websocket/net_ctx\tIface Local \tRemote\n");

user_data.shell = shell;
user_data.user_data = &count;

websocket_context_foreach(websocket_context_cb, &user_data);

if (count == 0) {
PR("No connections\n");
}

return 0;
}

SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_arp,
SHELL_CMD(flush, NULL, "Remove all entries from ARP cache.",
cmd_net_arp_flush),
Expand Down Expand Up @@ -4221,6 +4289,9 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_commands,
SHELL_CMD(tcp, &net_cmd_tcp, "Connect/send/close TCP connection.",
cmd_net_tcp),
SHELL_CMD(vlan, &net_cmd_vlan, "Show VLAN information.", cmd_net_vlan),
SHELL_CMD(websocket, NULL, "Print information about WebSocket "
"connections.",
cmd_net_websocket),
SHELL_SUBCMD_SET_END
);

Expand Down

0 comments on commit 53a4fdf

Please sign in to comment.