2001-10-31 16:30:46 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini netstat implementation(s) for busybox
|
|
|
|
* based in part on the netstat implementation from net-tools.
|
|
|
|
*
|
2002-07-03 17:16:38 +05:30
|
|
|
* Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com>
|
2001-10-31 16:30:46 +05:30
|
|
|
*
|
2002-07-03 17:16:38 +05:30
|
|
|
* 2002-04-20
|
|
|
|
* IPV6 support added by Bart Visscher <magick@linux-fan.com>
|
2006-07-11 17:02:31 +05:30
|
|
|
*
|
2008-07-27 17:40:07 +05:30
|
|
|
* 2008-07-10
|
|
|
|
* optional '-p' flag support ported from net-tools by G. Somlo <somlo@cmu.edu>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-10-31 16:30:46 +05:30
|
|
|
*/
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:config NETSTAT
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "netstat (10 kb)"
|
2016-11-23 13:35:14 +05:30
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: netstat prints information about the Linux networking subsystem.
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_NETSTAT_WIDE
|
2017-01-10 19:28:54 +05:30
|
|
|
//config: bool "Enable wide output"
|
2016-11-23 13:35:14 +05:30
|
|
|
//config: default y
|
|
|
|
//config: depends on NETSTAT
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Add support for wide columns. Useful when displaying IPv6 addresses
|
|
|
|
//config: (-W option).
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_NETSTAT_PRG
|
|
|
|
//config: bool "Enable PID/Program name output"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on NETSTAT
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Add support for -p flag to print out PID and program name.
|
|
|
|
//config: +700 bytes of code.
|
2016-11-23 13:35:14 +05:30
|
|
|
|
|
|
|
//applet:IF_NETSTAT(APPLET(netstat, BB_DIR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_NETSTAT) += netstat.o
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-07-13 00:47:55 +05:30
|
|
|
#include "inet_common.h"
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2010-10-27 05:42:29 +05:30
|
|
|
//usage:#define netstat_trivial_usage
|
|
|
|
//usage: "[-"IF_ROUTE("r")"al] [-tuwx] [-en"IF_FEATURE_NETSTAT_WIDE("W")IF_FEATURE_NETSTAT_PRG("p")"]"
|
|
|
|
//usage:#define netstat_full_usage "\n\n"
|
|
|
|
//usage: "Display networking information\n"
|
|
|
|
//usage: IF_ROUTE(
|
|
|
|
//usage: "\n -r Routing table"
|
|
|
|
//usage: )
|
|
|
|
//usage: "\n -a All sockets"
|
|
|
|
//usage: "\n -l Listening sockets"
|
|
|
|
//usage: "\n Else: connected sockets"
|
|
|
|
//usage: "\n -t TCP sockets"
|
|
|
|
//usage: "\n -u UDP sockets"
|
|
|
|
//usage: "\n -w Raw sockets"
|
|
|
|
//usage: "\n -x Unix sockets"
|
|
|
|
//usage: "\n Else: all socket types"
|
|
|
|
//usage: "\n -e Other/more information"
|
|
|
|
//usage: "\n -n Don't resolve names"
|
|
|
|
//usage: IF_FEATURE_NETSTAT_WIDE(
|
|
|
|
//usage: "\n -W Wide display"
|
|
|
|
//usage: )
|
|
|
|
//usage: IF_FEATURE_NETSTAT_PRG(
|
|
|
|
//usage: "\n -p Show PID/program name for sockets"
|
|
|
|
//usage: )
|
|
|
|
|
2008-07-27 17:40:07 +05:30
|
|
|
#define NETSTAT_OPTS "laentuwx" \
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_ROUTE( "r") \
|
|
|
|
IF_FEATURE_NETSTAT_WIDE("W") \
|
|
|
|
IF_FEATURE_NETSTAT_PRG( "p")
|
2008-07-27 17:40:07 +05:30
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
enum {
|
2008-07-27 17:40:07 +05:30
|
|
|
OPT_sock_listen = 1 << 0, // l
|
|
|
|
OPT_sock_all = 1 << 1, // a
|
|
|
|
OPT_extended = 1 << 2, // e
|
|
|
|
OPT_noresolve = 1 << 3, // n
|
|
|
|
OPT_sock_tcp = 1 << 4, // t
|
|
|
|
OPT_sock_udp = 1 << 5, // u
|
|
|
|
OPT_sock_raw = 1 << 6, // w
|
|
|
|
OPT_sock_unix = 1 << 7, // x
|
2010-10-27 05:42:29 +05:30
|
|
|
OPTBIT_x = 7,
|
|
|
|
IF_ROUTE( OPTBIT_ROUTE,)
|
|
|
|
IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
|
|
|
|
IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
|
2009-04-21 16:39:40 +05:30
|
|
|
OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
|
|
|
|
OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
|
|
|
|
OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
|
2007-05-16 05:27:46 +05:30
|
|
|
};
|
|
|
|
|
2007-01-22 19:42:08 +05:30
|
|
|
#define NETSTAT_CONNECTED 0x01
|
|
|
|
#define NETSTAT_LISTENING 0x02
|
|
|
|
#define NETSTAT_NUMERIC 0x04
|
2006-10-04 02:30:06 +05:30
|
|
|
/* Must match getopt32 option string */
|
2007-01-22 19:42:08 +05:30
|
|
|
#define NETSTAT_TCP 0x10
|
|
|
|
#define NETSTAT_UDP 0x20
|
|
|
|
#define NETSTAT_RAW 0x40
|
|
|
|
#define NETSTAT_UNIX 0x80
|
|
|
|
#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
|
2006-09-22 21:32:40 +05:30
|
|
|
|
2001-10-31 16:30:46 +05:30
|
|
|
|
|
|
|
enum {
|
2007-01-22 19:42:08 +05:30
|
|
|
TCP_ESTABLISHED = 1,
|
|
|
|
TCP_SYN_SENT,
|
|
|
|
TCP_SYN_RECV,
|
|
|
|
TCP_FIN_WAIT1,
|
|
|
|
TCP_FIN_WAIT2,
|
|
|
|
TCP_TIME_WAIT,
|
|
|
|
TCP_CLOSE,
|
|
|
|
TCP_CLOSE_WAIT,
|
|
|
|
TCP_LAST_ACK,
|
|
|
|
TCP_LISTEN,
|
2008-07-27 17:40:07 +05:30
|
|
|
TCP_CLOSING, /* now a valid state */
|
2001-10-31 16:30:46 +05:30
|
|
|
};
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char *const tcp_state[] = {
|
2007-01-22 19:42:08 +05:30
|
|
|
"",
|
|
|
|
"ESTABLISHED",
|
|
|
|
"SYN_SENT",
|
|
|
|
"SYN_RECV",
|
|
|
|
"FIN_WAIT1",
|
|
|
|
"FIN_WAIT2",
|
|
|
|
"TIME_WAIT",
|
|
|
|
"CLOSE",
|
|
|
|
"CLOSE_WAIT",
|
|
|
|
"LAST_ACK",
|
|
|
|
"LISTEN",
|
|
|
|
"CLOSING"
|
2001-10-31 16:30:46 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
2007-01-22 19:42:08 +05:30
|
|
|
SS_FREE = 0, /* not allocated */
|
|
|
|
SS_UNCONNECTED, /* unconnected to any socket */
|
|
|
|
SS_CONNECTING, /* in process of connecting */
|
|
|
|
SS_CONNECTED, /* connected to socket */
|
|
|
|
SS_DISCONNECTING /* in process of disconnecting */
|
2001-10-31 16:30:46 +05:30
|
|
|
} socket_state;
|
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
#define SO_ACCEPTCON (1<<16) /* performed a listen */
|
|
|
|
#define SO_WAITDATA (1<<17) /* wait data to read */
|
|
|
|
#define SO_NOSPACE (1<<18) /* no space to write */
|
2007-05-16 05:27:46 +05:30
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
#define ADDR_NORMAL_WIDTH 23
|
2007-05-16 05:27:46 +05:30
|
|
|
/* When there are IPv6 connections the IPv6 addresses will be
|
|
|
|
* truncated to none-recognition. The '-W' option makes the
|
2015-05-25 17:16:36 +05:30
|
|
|
* address columns wide enough to accommodate for longest possible
|
2007-05-16 05:27:46 +05:30
|
|
|
* IPv6 addresses, i.e. addresses of the form
|
|
|
|
* xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
|
|
|
|
*/
|
2010-10-27 16:40:29 +05:30
|
|
|
#define ADDR_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
|
|
|
|
#if ENABLE_FEATURE_NETSTAT_WIDE
|
2013-11-29 21:15:45 +05:30
|
|
|
# define FMT_NET_CONN_DATA "%s %6lu %6lu %-*s %-*s %-12s"
|
2010-10-27 16:40:29 +05:30
|
|
|
# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-*s %-*s State %s\n"
|
|
|
|
#else
|
2013-11-29 21:15:45 +05:30
|
|
|
# define FMT_NET_CONN_DATA "%s %6lu %6lu %-23s %-23s %-12s"
|
2010-10-27 16:40:29 +05:30
|
|
|
# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State %s\n"
|
|
|
|
#endif
|
2008-07-27 17:40:07 +05:30
|
|
|
|
|
|
|
#define PROGNAME_WIDTH 20
|
|
|
|
#define PROGNAME_WIDTH_STR "20"
|
|
|
|
/* PROGNAME_WIDTH chars: 12345678901234567890 */
|
|
|
|
#define PROGNAME_BANNER "PID/Program name "
|
|
|
|
|
|
|
|
struct prg_node {
|
|
|
|
struct prg_node *next;
|
|
|
|
long inode;
|
|
|
|
char name[PROGNAME_WIDTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PRG_HASH_SIZE 211
|
|
|
|
|
|
|
|
struct globals {
|
|
|
|
smallint flags;
|
|
|
|
#if ENABLE_FEATURE_NETSTAT_PRG
|
|
|
|
smallint prg_cache_loaded;
|
|
|
|
struct prg_node *prg_hash[PRG_HASH_SIZE];
|
|
|
|
#endif
|
2010-10-27 16:40:29 +05:30
|
|
|
#if ENABLE_FEATURE_NETSTAT_PRG
|
|
|
|
const char *progname_banner;
|
|
|
|
#endif
|
|
|
|
#if ENABLE_FEATURE_NETSTAT_WIDE
|
|
|
|
unsigned addr_width;
|
|
|
|
#endif
|
2008-07-27 17:40:07 +05:30
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define flags (G.flags )
|
|
|
|
#define prg_cache_loaded (G.prg_cache_loaded)
|
2010-10-27 16:40:29 +05:30
|
|
|
#define prg_hash (G.prg_hash )
|
|
|
|
#if ENABLE_FEATURE_NETSTAT_PRG
|
|
|
|
# define progname_banner (G.progname_banner )
|
|
|
|
#else
|
|
|
|
# define progname_banner ""
|
|
|
|
#endif
|
2008-07-27 17:40:07 +05:30
|
|
|
#define INIT_G() do { \
|
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
|
|
|
flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_NETSTAT_PRG
|
|
|
|
|
|
|
|
/* Deliberately truncating long to unsigned *int* */
|
|
|
|
#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
|
|
|
|
|
|
|
|
static void prg_cache_add(long inode, char *name)
|
|
|
|
{
|
|
|
|
unsigned hi = PRG_HASHIT(inode);
|
|
|
|
struct prg_node **pnp, *pn;
|
|
|
|
|
|
|
|
prg_cache_loaded = 2;
|
|
|
|
for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
|
|
|
|
if (pn->inode == inode) {
|
|
|
|
/* Some warning should be appropriate here
|
2013-01-14 20:27:44 +05:30
|
|
|
* as we got multiple processes for one i-node */
|
2008-07-27 17:40:07 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pnp = xzalloc(sizeof(struct prg_node));
|
|
|
|
pn = *pnp;
|
|
|
|
pn->inode = inode;
|
|
|
|
safe_strncpy(pn->name, name, PROGNAME_WIDTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *prg_cache_get(long inode)
|
|
|
|
{
|
|
|
|
unsigned hi = PRG_HASHIT(inode);
|
|
|
|
struct prg_node *pn;
|
|
|
|
|
|
|
|
for (pn = prg_hash[hi]; pn; pn = pn->next)
|
|
|
|
if (pn->inode == inode)
|
|
|
|
return pn->name;
|
|
|
|
return "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_CLEAN_UP
|
|
|
|
static void prg_cache_clear(void)
|
|
|
|
{
|
|
|
|
struct prg_node **pnp, *pn;
|
|
|
|
|
|
|
|
for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
|
|
|
|
while ((pn = *pnp) != NULL) {
|
|
|
|
*pnp = pn->next;
|
|
|
|
free(pn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define prg_cache_clear() ((void)0)
|
|
|
|
#endif
|
2007-05-16 05:27:46 +05:30
|
|
|
|
2008-07-27 19:45:42 +05:30
|
|
|
static long extract_socket_inode(const char *lname)
|
|
|
|
{
|
2008-07-27 17:40:07 +05:30
|
|
|
long inode = -1;
|
|
|
|
|
2015-03-12 22:18:34 +05:30
|
|
|
if (is_prefixed_with(lname, "socket:[")) {
|
2008-07-27 17:40:07 +05:30
|
|
|
/* "socket:[12345]", extract the "12345" as inode */
|
2010-10-27 16:40:29 +05:30
|
|
|
inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
|
2008-07-27 17:40:07 +05:30
|
|
|
if (*lname != ']')
|
|
|
|
inode = -1;
|
2015-03-12 22:18:34 +05:30
|
|
|
} else if (is_prefixed_with(lname, "[0000]:")) {
|
2008-07-27 17:40:07 +05:30
|
|
|
/* "[0000]:12345", extract the "12345" as inode */
|
2010-10-27 16:40:29 +05:30
|
|
|
inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
|
2008-07-27 17:40:07 +05:30
|
|
|
if (errno) /* not NUL terminated? */
|
|
|
|
inode = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
|
|
|
|
if (errno == ERANGE)
|
|
|
|
inode = -1;
|
|
|
|
#endif
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName,
|
2008-07-27 17:40:07 +05:30
|
|
|
struct stat *statbuf UNUSED_PARAM,
|
2010-10-27 05:42:29 +05:30
|
|
|
void *pid_slash_progname,
|
2008-07-27 17:40:07 +05:30
|
|
|
int depth UNUSED_PARAM)
|
|
|
|
{
|
|
|
|
char *linkname;
|
|
|
|
long inode;
|
|
|
|
|
|
|
|
linkname = xmalloc_readlink(fileName);
|
|
|
|
if (linkname != NULL) {
|
|
|
|
inode = extract_socket_inode(linkname);
|
|
|
|
free(linkname);
|
|
|
|
if (inode >= 0)
|
2010-10-27 05:42:29 +05:30
|
|
|
prg_cache_add(inode, (char *)pid_slash_progname);
|
2008-07-27 17:40:07 +05:30
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int FAST_FUNC dir_act(const char *fileName,
|
|
|
|
struct stat *statbuf UNUSED_PARAM,
|
|
|
|
void *userData UNUSED_PARAM,
|
|
|
|
int depth)
|
|
|
|
{
|
2010-10-27 05:42:29 +05:30
|
|
|
const char *pid;
|
2010-10-27 16:40:29 +05:30
|
|
|
char *pid_slash_progname;
|
|
|
|
char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
|
2008-07-27 17:40:07 +05:30
|
|
|
char cmdline_buf[512];
|
2010-10-27 16:40:29 +05:30
|
|
|
int n, len;
|
2008-07-27 17:40:07 +05:30
|
|
|
|
|
|
|
if (depth == 0) /* "/proc" itself */
|
|
|
|
return TRUE; /* continue looking one level below /proc */
|
|
|
|
|
2010-10-27 05:42:29 +05:30
|
|
|
pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
|
|
|
|
if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
|
2008-07-27 17:40:07 +05:30
|
|
|
return SKIP;
|
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName);
|
|
|
|
n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1);
|
|
|
|
if (n < 0)
|
2008-07-27 17:40:07 +05:30
|
|
|
return FALSE;
|
2010-10-27 16:40:29 +05:30
|
|
|
cmdline_buf[n] = '\0';
|
2008-07-27 17:40:07 +05:30
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
/* go through all files in /proc/PID/fd and check whether they are sockets */
|
|
|
|
strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
|
2010-10-27 05:42:29 +05:30
|
|
|
pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
|
2010-10-27 16:40:29 +05:30
|
|
|
n = recursive_action(proc_pid_fname,
|
|
|
|
ACTION_RECURSE | ACTION_QUIET,
|
|
|
|
add_to_prg_cache_if_socket,
|
|
|
|
NULL,
|
|
|
|
(void *)pid_slash_progname,
|
|
|
|
0);
|
2010-10-27 05:42:29 +05:30
|
|
|
free(pid_slash_progname);
|
2008-07-27 17:40:07 +05:30
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
if (!n)
|
2010-10-27 05:42:29 +05:30
|
|
|
return FALSE; /* signal permissions error to caller */
|
2008-07-27 17:40:07 +05:30
|
|
|
|
2010-10-27 05:42:29 +05:30
|
|
|
return SKIP; /* caller should not recurse further into this dir */
|
2008-07-27 17:40:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void prg_cache_load(void)
|
|
|
|
{
|
|
|
|
int load_ok;
|
|
|
|
|
|
|
|
prg_cache_loaded = 1;
|
|
|
|
load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
|
|
|
|
NULL, dir_act, NULL, 0);
|
|
|
|
if (load_ok)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (prg_cache_loaded == 1)
|
|
|
|
bb_error_msg("can't scan /proc - are you root?");
|
|
|
|
else
|
|
|
|
bb_error_msg("showing only processes with your user ID");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define prg_cache_clear() ((void)0)
|
|
|
|
|
|
|
|
#endif //ENABLE_FEATURE_NETSTAT_PRG
|
2007-05-16 05:27:46 +05:30
|
|
|
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
|
|
|
|
{
|
|
|
|
char addr6[INET6_ADDRSTRLEN];
|
|
|
|
struct in6_addr in6;
|
2007-05-30 05:59:55 +05:30
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
sscanf(local_addr, "%08X%08X%08X%08X",
|
|
|
|
&in6.s6_addr32[0], &in6.s6_addr32[1],
|
|
|
|
&in6.s6_addr32[2], &in6.s6_addr32[3]);
|
|
|
|
inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
|
2010-02-05 19:10:23 +05:30
|
|
|
inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
|
2007-05-30 05:59:55 +05:30
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
localaddr->sin6_family = AF_INET6;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
|
|
|
|
{
|
2010-02-05 19:10:23 +05:30
|
|
|
sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
|
|
|
|
localaddr->sin_family = AF_INET;
|
2007-05-16 05:27:46 +05:30
|
|
|
}
|
|
|
|
|
2007-06-19 16:42:46 +05:30
|
|
|
static const char *get_sname(int port, const char *proto, int numeric)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
2007-06-19 16:42:46 +05:30
|
|
|
if (!port)
|
|
|
|
return "*";
|
|
|
|
if (!numeric) {
|
2007-01-22 19:42:08 +05:30
|
|
|
struct servent *se = getservbyport(port, proto);
|
2001-10-31 16:30:46 +05:30
|
|
|
if (se)
|
2007-06-19 16:42:46 +05:30
|
|
|
return se->s_name;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2007-06-19 16:42:46 +05:30
|
|
|
/* hummm, we may return static buffer here!! */
|
|
|
|
return itoa(ntohs(port));
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2007-06-19 16:42:46 +05:30
|
|
|
static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
2007-06-19 16:42:46 +05:30
|
|
|
char *host, *host_port;
|
2001-11-10 16:52:46 +05:30
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
/* Code which used "*" for INADDR_ANY is removed: it's ambiguous
|
|
|
|
* in IPv6, while "0.0.0.0" is not. */
|
2007-06-19 16:42:46 +05:30
|
|
|
|
2007-08-18 19:46:39 +05:30
|
|
|
host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
|
|
|
|
: xmalloc_sockaddr2host_noport(addr);
|
2007-06-19 16:42:46 +05:30
|
|
|
|
|
|
|
host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
|
|
|
|
free(host);
|
|
|
|
return host_port;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
struct inet_params {
|
|
|
|
int local_port, rem_port, state, uid;
|
2010-02-05 19:10:23 +05:30
|
|
|
union {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_in sin;
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2010-02-05 19:10:23 +05:30
|
|
|
struct sockaddr_in6 sin6;
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2010-02-05 19:10:23 +05:30
|
|
|
} localaddr, remaddr;
|
2008-07-27 22:54:19 +05:30
|
|
|
unsigned long rxq, txq, inode;
|
|
|
|
};
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
static int scan_inet_proc_line(struct inet_params *param, char *line)
|
|
|
|
{
|
|
|
|
int num;
|
2010-10-28 06:06:17 +05:30
|
|
|
/* IPv6 /proc files use 32-char hex representation
|
|
|
|
* of IPv6 address, followed by :PORT_IN_HEX
|
|
|
|
*/
|
|
|
|
char local_addr[33], rem_addr[33]; /* 32 + 1 for NUL */
|
2001-10-31 16:30:46 +05:30
|
|
|
|
|
|
|
num = sscanf(line,
|
2010-10-28 06:06:17 +05:30
|
|
|
"%*d: %32[0-9A-Fa-f]:%X "
|
|
|
|
"%32[0-9A-Fa-f]:%X %X "
|
2008-07-27 22:54:19 +05:30
|
|
|
"%lX:%lX %*X:%*X "
|
2013-11-29 21:15:45 +05:30
|
|
|
"%*X %d %*d %lu ",
|
2008-07-27 22:54:19 +05:30
|
|
|
local_addr, ¶m->local_port,
|
|
|
|
rem_addr, ¶m->rem_port, ¶m->state,
|
|
|
|
¶m->txq, ¶m->rxq,
|
|
|
|
¶m->uid, ¶m->inode);
|
|
|
|
if (num < 9) {
|
2007-12-27 03:19:33 +05:30
|
|
|
return 1; /* error */
|
2007-12-27 02:14:45 +05:30
|
|
|
}
|
|
|
|
|
2001-10-31 16:30:46 +05:30
|
|
|
if (strlen(local_addr) > 8) {
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2010-02-05 19:10:23 +05:30
|
|
|
build_ipv6_addr(local_addr, ¶m->localaddr.sin6);
|
|
|
|
build_ipv6_addr(rem_addr, ¶m->remaddr.sin6);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2001-10-31 16:30:46 +05:30
|
|
|
} else {
|
2010-02-05 19:10:23 +05:30
|
|
|
build_ipv4_addr(local_addr, ¶m->localaddr.sin);
|
|
|
|
build_ipv4_addr(rem_addr, ¶m->remaddr.sin);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2008-07-27 22:54:19 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
static void print_inet_line(struct inet_params *param,
|
|
|
|
const char *state_str, const char *proto, int is_connected)
|
|
|
|
{
|
|
|
|
if ((is_connected && (flags & NETSTAT_CONNECTED))
|
|
|
|
|| (!is_connected && (flags & NETSTAT_LISTENING))
|
2007-01-22 19:42:08 +05:30
|
|
|
) {
|
2007-06-19 16:42:46 +05:30
|
|
|
char *l = ip_port_str(
|
2010-02-05 19:10:23 +05:30
|
|
|
¶m->localaddr.sa, param->local_port,
|
2008-07-27 22:54:19 +05:30
|
|
|
proto, flags & NETSTAT_NUMERIC);
|
2007-06-19 16:42:46 +05:30
|
|
|
char *r = ip_port_str(
|
2010-02-05 19:10:23 +05:30
|
|
|
¶m->remaddr.sa, param->rem_port,
|
2008-07-27 22:54:19 +05:30
|
|
|
proto, flags & NETSTAT_NUMERIC);
|
2010-10-27 16:40:29 +05:30
|
|
|
printf(FMT_NET_CONN_DATA,
|
|
|
|
proto, param->rxq, param->txq,
|
|
|
|
IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
|
|
|
|
IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
|
|
|
|
state_str);
|
2008-07-27 17:40:07 +05:30
|
|
|
#if ENABLE_FEATURE_NETSTAT_PRG
|
|
|
|
if (option_mask32 & OPT_prg)
|
2008-07-27 22:54:19 +05:30
|
|
|
printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
|
2008-07-27 17:40:07 +05:30
|
|
|
#endif
|
|
|
|
bb_putchar('\n');
|
2007-06-19 16:42:46 +05:30
|
|
|
free(l);
|
|
|
|
free(r);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
static int FAST_FUNC tcp_do_one(char *line)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
2008-07-27 22:54:19 +05:30
|
|
|
struct inet_params param;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
memset(¶m, 0, sizeof(param));
|
2008-07-27 22:54:19 +05:30
|
|
|
if (scan_inet_proc_line(¶m, line))
|
|
|
|
return 1;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
print_inet_line(¶m, tcp_state[param.state], "tcp", param.rem_port);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2010-02-05 19:10:23 +05:30
|
|
|
# define NOT_NULL_ADDR(A) ( \
|
|
|
|
( (A.sa.sa_family == AF_INET6) \
|
|
|
|
&& (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
|
|
|
|
A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
|
|
|
|
) || ( \
|
|
|
|
(A.sa.sa_family == AF_INET) \
|
|
|
|
&& A.sin.sin_addr.s_addr != 0 \
|
|
|
|
) \
|
2007-05-16 05:27:46 +05:30
|
|
|
)
|
2002-07-03 17:16:38 +05:30
|
|
|
#else
|
2010-02-19 12:59:32 +05:30
|
|
|
# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2008-07-27 22:54:19 +05:30
|
|
|
|
|
|
|
static int FAST_FUNC udp_do_one(char *line)
|
|
|
|
{
|
|
|
|
int have_remaddr;
|
|
|
|
const char *state_str;
|
|
|
|
struct inet_params param;
|
|
|
|
|
2010-10-27 16:40:29 +05:30
|
|
|
memset(¶m, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */
|
2008-07-27 22:54:19 +05:30
|
|
|
if (scan_inet_proc_line(¶m, line))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
state_str = "UNKNOWN";
|
|
|
|
switch (param.state) {
|
|
|
|
case TCP_ESTABLISHED:
|
|
|
|
state_str = "ESTABLISHED";
|
|
|
|
break;
|
|
|
|
case TCP_CLOSE:
|
|
|
|
state_str = "";
|
|
|
|
break;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2008-07-27 22:54:19 +05:30
|
|
|
|
2010-02-05 19:10:23 +05:30
|
|
|
have_remaddr = NOT_NULL_ADDR(param.remaddr);
|
2008-07-27 22:54:19 +05:30
|
|
|
print_inet_line(¶m, state_str, "udp", have_remaddr);
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
static int FAST_FUNC raw_do_one(char *line)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
2008-07-27 22:54:19 +05:30
|
|
|
int have_remaddr;
|
|
|
|
struct inet_params param;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
if (scan_inet_proc_line(¶m, line))
|
|
|
|
return 1;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2010-02-05 19:10:23 +05:30
|
|
|
have_remaddr = NOT_NULL_ADDR(param.remaddr);
|
2008-07-27 22:54:19 +05:30
|
|
|
print_inet_line(¶m, itoa(param.state), "raw", have_remaddr);
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
static int FAST_FUNC unix_do_one(char *line)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
|
|
|
unsigned long refcnt, proto, unix_flags;
|
2007-12-03 16:15:14 +05:30
|
|
|
unsigned long inode;
|
|
|
|
int type, state;
|
|
|
|
int num, path_ofs;
|
|
|
|
const char *ss_proto, *ss_state, *ss_type;
|
|
|
|
char ss_flags[32];
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2007-12-03 16:15:14 +05:30
|
|
|
/* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
|
2008-01-08 00:36:47 +05:30
|
|
|
* Other users report long lines filled by NUL bytes.
|
2007-12-27 02:14:45 +05:30
|
|
|
* (those ^@ are NUL bytes too). We see them as empty lines. */
|
2007-12-03 16:15:14 +05:30
|
|
|
if (!line[0])
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2007-12-03 16:15:14 +05:30
|
|
|
path_ofs = 0; /* paranoia */
|
2008-07-27 22:54:19 +05:30
|
|
|
num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
|
|
|
|
&refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
|
|
|
|
if (num < 6) {
|
2007-12-27 03:19:33 +05:30
|
|
|
return 1; /* error */
|
2007-12-03 16:15:14 +05:30
|
|
|
}
|
2007-01-22 19:42:08 +05:30
|
|
|
if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
|
2001-10-31 16:30:46 +05:30
|
|
|
if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
|
2007-01-22 19:42:08 +05:30
|
|
|
if (!(flags & NETSTAT_LISTENING))
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
} else {
|
2007-01-22 19:42:08 +05:30
|
|
|
if (!(flags & NETSTAT_CONNECTED))
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case 0:
|
|
|
|
ss_proto = "unix";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ss_proto = "??";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case SOCK_STREAM:
|
|
|
|
ss_type = "STREAM";
|
|
|
|
break;
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
ss_type = "DGRAM";
|
|
|
|
break;
|
|
|
|
case SOCK_RAW:
|
|
|
|
ss_type = "RAW";
|
|
|
|
break;
|
|
|
|
case SOCK_RDM:
|
|
|
|
ss_type = "RDM";
|
|
|
|
break;
|
|
|
|
case SOCK_SEQPACKET:
|
|
|
|
ss_type = "SEQPACKET";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ss_type = "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SS_FREE:
|
|
|
|
ss_state = "FREE";
|
|
|
|
break;
|
|
|
|
case SS_UNCONNECTED:
|
|
|
|
/*
|
|
|
|
* Unconnected sockets may be listening
|
|
|
|
* for something.
|
|
|
|
*/
|
|
|
|
if (unix_flags & SO_ACCEPTCON) {
|
|
|
|
ss_state = "LISTENING";
|
|
|
|
} else {
|
|
|
|
ss_state = "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SS_CONNECTING:
|
|
|
|
ss_state = "CONNECTING";
|
|
|
|
break;
|
|
|
|
case SS_CONNECTED:
|
|
|
|
ss_state = "CONNECTED";
|
|
|
|
break;
|
|
|
|
case SS_DISCONNECTING:
|
|
|
|
ss_state = "DISCONNECTING";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ss_state = "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(ss_flags, "[ ");
|
|
|
|
if (unix_flags & SO_ACCEPTCON)
|
|
|
|
strcat(ss_flags, "ACC ");
|
|
|
|
if (unix_flags & SO_WAITDATA)
|
|
|
|
strcat(ss_flags, "W ");
|
|
|
|
if (unix_flags & SO_NOSPACE)
|
|
|
|
strcat(ss_flags, "N ");
|
|
|
|
strcat(ss_flags, "]");
|
|
|
|
|
2013-11-29 21:15:45 +05:30
|
|
|
printf("%-5s %-6lu %-11s %-10s %-13s %6lu ",
|
2007-12-30 07:29:53 +05:30
|
|
|
ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
|
|
|
|
);
|
|
|
|
|
2008-07-27 17:40:07 +05:30
|
|
|
#if ENABLE_FEATURE_NETSTAT_PRG
|
|
|
|
if (option_mask32 & OPT_prg)
|
|
|
|
printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 07:29:53 +05:30
|
|
|
/* TODO: currently we stop at first NUL byte. Is it a problem? */
|
|
|
|
line += path_ofs;
|
2015-03-13 00:48:51 +05:30
|
|
|
chomp(line);
|
2007-12-30 07:29:53 +05:30
|
|
|
while (*line)
|
|
|
|
fputc_printable(*line++, stdout);
|
|
|
|
bb_putchar('\n');
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
2007-12-03 16:15:14 +05:30
|
|
|
int lnr;
|
2001-10-31 16:30:46 +05:30
|
|
|
FILE *procinfo;
|
2007-12-03 16:15:14 +05:30
|
|
|
char *buffer;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2008-07-27 22:54:19 +05:30
|
|
|
/* _stdin is just to save "r" param */
|
|
|
|
procinfo = fopen_or_warn_stdin(file);
|
2001-10-31 16:30:46 +05:30
|
|
|
if (procinfo == NULL) {
|
2007-01-22 19:42:08 +05:30
|
|
|
return;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2007-12-03 16:15:14 +05:30
|
|
|
lnr = 0;
|
2008-07-27 22:54:19 +05:30
|
|
|
/* Why xmalloc_fgets_str? because it doesn't stop on NULs */
|
2007-12-27 02:14:45 +05:30
|
|
|
while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
|
2008-07-27 22:54:19 +05:30
|
|
|
/* line 0 is skipped */
|
|
|
|
if (lnr && proc(buffer))
|
|
|
|
bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
|
|
|
|
lnr++;
|
2007-12-27 02:14:45 +05:30
|
|
|
free(buffer);
|
|
|
|
}
|
2007-01-22 19:42:08 +05:30
|
|
|
fclose(procinfo);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int netstat_main(int argc UNUSED_PARAM, char **argv)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
2006-10-04 02:30:06 +05:30
|
|
|
unsigned opt;
|
2006-09-22 21:32:40 +05:30
|
|
|
|
2008-07-27 17:40:07 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2006-09-22 21:32:40 +05:30
|
|
|
/* Option string must match NETSTAT_xxx constants */
|
2007-08-18 21:02:12 +05:30
|
|
|
opt = getopt32(argv, NETSTAT_OPTS);
|
2010-10-27 05:42:29 +05:30
|
|
|
if (opt & OPT_sock_listen) { // -l
|
2006-09-22 21:32:40 +05:30
|
|
|
flags &= ~NETSTAT_CONNECTED;
|
|
|
|
flags |= NETSTAT_LISTENING;
|
|
|
|
}
|
2010-10-27 05:42:29 +05:30
|
|
|
if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
|
|
|
|
//if (opt & OPT_extended) // -e
|
|
|
|
if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
|
|
|
|
//if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
|
|
|
|
//if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
|
|
|
|
//if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
|
|
|
|
//if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_ROUTE
|
2010-10-27 05:42:29 +05:30
|
|
|
if (opt & OPT_route) { // -r
|
2007-01-22 19:42:08 +05:30
|
|
|
bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
|
2004-03-15 13:59:22 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2010-10-27 05:42:29 +05:30
|
|
|
#endif
|
2010-10-27 16:40:29 +05:30
|
|
|
#if ENABLE_FEATURE_NETSTAT_WIDE
|
|
|
|
G.addr_width = ADDR_NORMAL_WIDTH;
|
2008-07-27 17:40:07 +05:30
|
|
|
if (opt & OPT_wide) { // -W
|
2010-10-27 16:40:29 +05:30
|
|
|
G.addr_width = ADDR_WIDE;
|
2007-05-16 05:27:46 +05:30
|
|
|
}
|
2010-10-27 16:40:29 +05:30
|
|
|
#endif
|
2008-07-27 17:40:07 +05:30
|
|
|
#if ENABLE_FEATURE_NETSTAT_PRG
|
2010-10-27 16:40:29 +05:30
|
|
|
progname_banner = "";
|
2008-07-27 17:40:07 +05:30
|
|
|
if (opt & OPT_prg) { // -p
|
2010-10-27 16:40:29 +05:30
|
|
|
progname_banner = PROGNAME_BANNER;
|
2008-07-27 17:40:07 +05:30
|
|
|
prg_cache_load();
|
|
|
|
}
|
|
|
|
#endif
|
2007-05-16 05:27:46 +05:30
|
|
|
|
2006-09-22 21:32:40 +05:30
|
|
|
opt &= NETSTAT_ALLPROTO;
|
|
|
|
if (opt) {
|
|
|
|
flags &= ~NETSTAT_ALLPROTO;
|
|
|
|
flags |= opt;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2006-09-22 21:32:40 +05:30
|
|
|
if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
|
2010-10-29 15:16:52 +05:30
|
|
|
printf("Active Internet connections "); /* xxx */
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2007-01-22 19:42:08 +05:30
|
|
|
if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
|
2001-10-31 16:30:46 +05:30
|
|
|
printf("(servers and established)");
|
2007-01-22 19:42:08 +05:30
|
|
|
else if (flags & NETSTAT_LISTENING)
|
|
|
|
printf("(only servers)");
|
|
|
|
else
|
|
|
|
printf("(w/o servers)");
|
2010-10-27 16:40:29 +05:30
|
|
|
printf(FMT_NET_CONN_HEADER,
|
|
|
|
IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
|
|
|
|
IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
|
|
|
|
progname_banner
|
|
|
|
);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2008-07-27 22:54:19 +05:30
|
|
|
if (flags & NETSTAT_TCP) {
|
|
|
|
do_info("/proc/net/tcp", tcp_do_one);
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2008-07-27 22:54:19 +05:30
|
|
|
do_info("/proc/net/tcp6", tcp_do_one);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2008-07-27 22:54:19 +05:30
|
|
|
}
|
|
|
|
if (flags & NETSTAT_UDP) {
|
|
|
|
do_info("/proc/net/udp", udp_do_one);
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2008-07-27 22:54:19 +05:30
|
|
|
do_info("/proc/net/udp6", udp_do_one);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2008-07-27 22:54:19 +05:30
|
|
|
}
|
|
|
|
if (flags & NETSTAT_RAW) {
|
|
|
|
do_info("/proc/net/raw", raw_do_one);
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2008-07-27 22:54:19 +05:30
|
|
|
do_info("/proc/net/raw6", raw_do_one);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2008-07-27 22:54:19 +05:30
|
|
|
}
|
2007-01-22 19:42:08 +05:30
|
|
|
if (flags & NETSTAT_UNIX) {
|
2001-10-31 16:30:46 +05:30
|
|
|
printf("Active UNIX domain sockets ");
|
2007-01-22 19:42:08 +05:30
|
|
|
if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
|
2001-10-31 16:30:46 +05:30
|
|
|
printf("(servers and established)");
|
2007-01-22 19:42:08 +05:30
|
|
|
else if (flags & NETSTAT_LISTENING)
|
|
|
|
printf("(only servers)");
|
|
|
|
else
|
|
|
|
printf("(w/o servers)");
|
2010-10-27 16:40:29 +05:30
|
|
|
printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner);
|
2008-07-27 22:54:19 +05:30
|
|
|
do_info("/proc/net/unix", unix_do_one);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2008-07-27 17:40:07 +05:30
|
|
|
prg_cache_clear();
|
2001-10-31 16:30:46 +05:30
|
|
|
return 0;
|
|
|
|
}
|