322 lines
9.7 KiB
C
322 lines
9.7 KiB
C
/*
|
|
******************************************************************************
|
|
* SOURCE FILE
|
|
*
|
|
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: iplite_cmd_netstat.c,v $ $Revision: 1.21 $
|
|
* $Source: /home/interpeak/CVSRoot/iplite2/src/iplite_cmd_netstat.c,v $
|
|
* $Author: kenneth $
|
|
* $State: Exp $ $Locker: $
|
|
*
|
|
* INTERPEAK_COPYRIGHT_STRING
|
|
* Design and implementation by Kenneth Jonsson <kenneth@interpeak.se>
|
|
******************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 1 DESCRIPTION
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 2 CONFIGURATION
|
|
****************************************************************************
|
|
*/
|
|
|
|
#ifndef IPCOM_KERNEL
|
|
#define IPCOM_KERNEL
|
|
#endif
|
|
#include "iplite_config.h"
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 3 INCLUDE FILES
|
|
****************************************************************************
|
|
*/
|
|
|
|
#define IPCOM_USE_CLIB_PROTO
|
|
#include <ipcom_clib.h>
|
|
#include <ipcom_cmd.h>
|
|
#include <ipcom_getopt.h>
|
|
#include <ipcom_sock.h>
|
|
|
|
#include "iplite_cmd.h"
|
|
#include "iplite.h"
|
|
#include "iplite_h.h"
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 4 DEFINES
|
|
****************************************************************************
|
|
*/
|
|
#define IPLITE_NETSTAT_STRLEN 256
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 5 TYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 6 EXTERN PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 7 LOCAL PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
IP_PUBLIC int iplite_cmd_netstat(int argc, char **argv);
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 8 DATA
|
|
****************************************************************************
|
|
*/
|
|
|
|
IP_STATIC Iplite_cmd_int_str_map af_map[] =
|
|
{
|
|
{ IP_AF_INET, "INET" },
|
|
{ IP_AF_INET6, "INET6" },
|
|
{ -1, IP_NULL }
|
|
};
|
|
|
|
IP_STATIC Iplite_cmd_int_str_map proto_map[] =
|
|
{
|
|
{ IP_IPPROTO_TCP, "TCP" },
|
|
{ IP_IPPROTO_UDP, "UDP" },
|
|
{ IP_IPPROTO_ICMP, "ICMP" },
|
|
{ IP_IPPROTO_ICMPV6, "ICMP6" },
|
|
{ IP_IPPROTO_IP, "IP" },
|
|
{ IP_IPPROTO_IPV4, "IPv4" },
|
|
{ IP_IPPROTO_IPV6, "IPv6" },
|
|
{ -1, IP_NULL }
|
|
};
|
|
|
|
#ifdef IPCOM_USE_TCP
|
|
IP_STATIC IP_CONST char *tcp_state_list[] =
|
|
{
|
|
"",
|
|
"CLOSED",
|
|
"LISTEN",
|
|
"SYN_SENT",
|
|
"SYN_RCVD",
|
|
"ESTABLISHED",
|
|
"CLOSE_WAIT",
|
|
"FIN_WAIT_1",
|
|
"CLOSING",
|
|
"LAST_ACK",
|
|
"FIN_WAIT_2",
|
|
"TIME_WAIT"
|
|
};
|
|
#endif /* IPCOM_USE_TCP */
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 9 LOCAL FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* iplite_netstat_print_entries
|
|
*===========================================================================
|
|
* Description: Prints the status of IPLITE sockets.
|
|
* Parameters: af - The address family to print status for or -1 for all
|
|
* address family types.
|
|
* proto - The protocol to print status for or -1 for all
|
|
* protocols.
|
|
* show_unconnected - IP_TRUE if both connected and unconnected
|
|
* sockets status should be printed.
|
|
* no_dns - IP_TRUE if the addresses should not be resolved to
|
|
* there DNS names.
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC void
|
|
iplite_netstat_print_entries(int af, int proto, Ip_bool show_unconnected, Ip_bool no_dns)
|
|
{
|
|
int p, x, a, y;
|
|
char str[IPLITE_NETSTAT_STRLEN];
|
|
struct Iplite_tag_sock_data tag;
|
|
int ret;
|
|
Ip_u16 dport;
|
|
|
|
for (y = 0; af_map[y].key != -1; y++)
|
|
{
|
|
if (af != 0)
|
|
a = af;
|
|
else
|
|
a = af_map[y].key;
|
|
ipcom_printf("%s sockets\n", iplite_cmd_key_to_str(af_map, a));
|
|
ipcom_printf("%-6s%-4s%-30s%-30s%-5s\n",
|
|
"Prot", "Fd", "Local Address", "Foreign Address", "State");
|
|
|
|
for (x = 0; proto_map[x].key != -1; x++)
|
|
{
|
|
if (proto != -1)
|
|
p = proto;
|
|
else
|
|
p = proto_map[x].key;
|
|
|
|
tag.hdr.type = IPLITE_CTRL_NEW_ENUM_SOCK_DATA;
|
|
tag.hdr.size = sizeof(tag);
|
|
for (;;)
|
|
{
|
|
ret = iplite_ctrl(&tag.hdr);
|
|
if (ret == IPLITE_CTRL_ENUM_DONE)
|
|
break;
|
|
if (ret < 0)
|
|
{
|
|
ipcom_printf("iplite_ctrl() failed (%d)\n", -ret);
|
|
return;
|
|
}
|
|
tag.hdr.type = IPLITE_CTRL_CONT_ENUM_SOCK_DATA;
|
|
|
|
if (tag.protocol != p || tag.domain != a)
|
|
continue;
|
|
|
|
dport = ip_ntohs(((struct Ip_sockaddr_in *) &tag.daddr)->sin_port);
|
|
if (!show_unconnected && dport == 0)
|
|
/* Show only connected sockets */
|
|
continue;
|
|
|
|
ipcom_printf("%-6s", iplite_cmd_key_to_str(proto_map, tag.protocol));
|
|
ipcom_printf("%-4d", tag.fd);
|
|
ipcom_printf("%-30s",
|
|
ipcom_cmd_sockaddr_to_str(&tag.saddr.addr, IP_TRUE, no_dns, str, sizeof(str)));
|
|
ipcom_printf("%-30s",
|
|
ipcom_cmd_sockaddr_to_str(&tag.daddr.addr, IP_TRUE, no_dns, str, sizeof(str)));
|
|
|
|
#ifdef IPCOM_USE_TCP
|
|
if (tag.protocol == IP_IPPROTO_TCP)
|
|
ipcom_printf("%-11s", tcp_state_list[tag.tcp_state]);
|
|
|
|
#endif /* IPCOM_USE_TCP */
|
|
ipcom_printf("\n");
|
|
}
|
|
|
|
if (proto != -1)
|
|
break;
|
|
}
|
|
ipcom_printf("\n");
|
|
if (af != -1)
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 10 PUBLIC FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
*===========================================================================
|
|
* iplite_cmd_netstat
|
|
*===========================================================================
|
|
* Description:
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_PUBLIC int
|
|
iplite_cmd_netstat(int argc, char **argv)
|
|
{
|
|
int i, c;
|
|
int proto = -1;
|
|
int af = 0;
|
|
Ip_bool show_unconnected = IP_FALSE;
|
|
Ip_bool no_dns = IP_FALSE;
|
|
Ipcom_getopt opt;
|
|
|
|
ipcom_getopt_clear_r(&opt);
|
|
while ((c = ipcom_getopt_r(argc, argv, "?hHaf:p:nV:", &opt)) != -1)
|
|
{
|
|
switch (c)
|
|
{
|
|
case '?':
|
|
case 'h':
|
|
case 'H':
|
|
goto usage;
|
|
case 'a':
|
|
show_unconnected = IP_TRUE;
|
|
break;
|
|
case 'f':
|
|
for (i = 0; af_map[i].key != -1; i++)
|
|
if (ipcom_strcasecmp(opt.optarg, af_map[i].str) == 0)
|
|
{
|
|
af = af_map[i].key;
|
|
break;
|
|
}
|
|
if (af_map[i].key == -1)
|
|
goto usage;
|
|
break;
|
|
case 'p':
|
|
for (i = 0; proto_map[i].key != -1; i++)
|
|
if (ipcom_strcasecmp(opt.optarg, proto_map[i].str) == 0)
|
|
{
|
|
proto = proto_map[i].key;
|
|
break;
|
|
}
|
|
if (proto_map[i].key == -1)
|
|
goto usage;
|
|
break;
|
|
case 'n':
|
|
no_dns = IP_TRUE;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
iplite_netstat_print_entries(af, proto, show_unconnected, no_dns);
|
|
|
|
return 0;
|
|
|
|
usage:
|
|
ipcom_printf("\n"
|
|
"NAME\n"
|
|
" netstat - show network status\n"
|
|
"\n"
|
|
"SYNOPSIS\n");
|
|
ipcom_printf(" netstat [-anh] [-f address_family] [-p protocol]\n"
|
|
"\n"
|
|
"DESCRIPTION\n"
|
|
" The netstat program displays and modifies the Internet-to-Ethernet address\n"
|
|
" translation tables used by the address resolution protocol.\n"
|
|
"\n");
|
|
ipcom_printf(" -a Show the state of all sockets; normally sockets used by server\n"
|
|
" processes are not shown.\n"
|
|
" -f address_family\n"
|
|
" Limit statistics or address control block reports to those of the\n"
|
|
" specified address family. The following address families are\n"
|
|
" recognized: inet for IPv4 (default) and inet6 for IPv6\n");
|
|
ipcom_printf(" -h Help, this usage menu\n"
|
|
" -n Show network addresses as numbers. Default is to lookup symbolic\n"
|
|
" names for the addresses.\n"
|
|
" -p protocol\n"
|
|
" Show statistics about protocol, which is either a well-known name\n"
|
|
" for a protocol or an alias for it.\n"
|
|
"\n");
|
|
return 1;
|
|
}
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* END OF FILE
|
|
****************************************************************************
|
|
*/
|
|
|