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
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
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
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
enum {
|
|
|
|
OPT_extended = 0x4,
|
|
|
|
OPT_showroute = 0x100,
|
|
|
|
OPT_widedisplay = 0x200 * ENABLE_FEATURE_NETSTAT_WIDE,
|
|
|
|
};
|
|
|
|
# define NETSTAT_OPTS "laentuwxr"USE_FEATURE_NETSTAT_WIDE("W")
|
|
|
|
|
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
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
static smallint flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO;
|
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,
|
|
|
|
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;
|
|
|
|
|
2007-01-22 19:42:08 +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 */
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
/* Standard printout size */
|
|
|
|
#define PRINT_IP_MAX_SIZE 23
|
|
|
|
#define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s\n"
|
|
|
|
#define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State\n"
|
|
|
|
|
|
|
|
/* When there are IPv6 connections the IPv6 addresses will be
|
|
|
|
* truncated to none-recognition. The '-W' option makes the
|
|
|
|
* address columns wide enough to accomodate for longest possible
|
|
|
|
* IPv6 addresses, i.e. addresses of the form
|
|
|
|
* xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
|
|
|
|
*/
|
|
|
|
#define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
|
|
|
|
#define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s\n"
|
|
|
|
#define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State\n"
|
|
|
|
|
|
|
|
static const char *net_conn_line = PRINT_NET_CONN;
|
|
|
|
|
|
|
|
|
|
|
|
#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));
|
|
|
|
inet_pton(AF_INET6, addr6, (struct sockaddr *) &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
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr)
|
|
|
|
#else
|
|
|
|
static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sscanf(local_addr, "%X",
|
|
|
|
&((struct sockaddr_in *) localaddr)->sin_addr.s_addr);
|
|
|
|
((struct sockaddr *) localaddr)->sa_family = AF_INET;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
enum { salen = USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) };
|
|
|
|
char *host, *host_port;
|
2001-11-10 16:52:46 +05:30
|
|
|
|
2007-06-19 16:42:46 +05:30
|
|
|
/* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6,
|
|
|
|
* while "0.0.0.0" is not. */
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-12-27 03:19:33 +05:30
|
|
|
static int tcp_do_one(int lnr, char *line)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
|
|
|
char local_addr[64], rem_addr[64];
|
2002-06-22 23:02:58 +05:30
|
|
|
char more[512];
|
2001-10-31 16:30:46 +05:30
|
|
|
int num, local_port, rem_port, d, state, timer_run, uid, timeout;
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2002-07-03 17:16:38 +05:30
|
|
|
struct sockaddr_in6 localaddr, remaddr;
|
|
|
|
#else
|
2001-10-31 16:30:46 +05:30
|
|
|
struct sockaddr_in localaddr, remaddr;
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2001-10-31 16:30:46 +05:30
|
|
|
unsigned long rxq, txq, time_len, retr, inode;
|
|
|
|
|
|
|
|
if (lnr == 0)
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
|
|
|
more[0] = '\0';
|
|
|
|
num = sscanf(line,
|
2007-05-16 05:27:46 +05:30
|
|
|
"%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
|
|
|
|
&d, local_addr, &local_port,
|
|
|
|
rem_addr, &rem_port, &state,
|
|
|
|
&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
|
2001-10-31 16:30:46 +05:30
|
|
|
|
2007-12-27 02:14:45 +05:30
|
|
|
if (num < 10) {
|
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
|
2007-05-16 05:27:46 +05:30
|
|
|
build_ipv6_addr(local_addr, &localaddr);
|
|
|
|
build_ipv6_addr(rem_addr, &remaddr);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2001-10-31 16:30:46 +05:30
|
|
|
} else {
|
2007-05-16 05:27:46 +05:30
|
|
|
build_ipv4_addr(local_addr, &localaddr);
|
|
|
|
build_ipv4_addr(rem_addr, &remaddr);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2007-01-22 19:42:08 +05:30
|
|
|
if ((rem_port && (flags & NETSTAT_CONNECTED))
|
|
|
|
|| (!rem_port && (flags & NETSTAT_LISTENING))
|
|
|
|
) {
|
2007-06-19 16:42:46 +05:30
|
|
|
char *l = ip_port_str(
|
2007-05-16 05:27:46 +05:30
|
|
|
(struct sockaddr *) &localaddr, local_port,
|
|
|
|
"tcp", flags & NETSTAT_NUMERIC);
|
2007-06-19 16:42:46 +05:30
|
|
|
char *r = ip_port_str(
|
2007-05-16 05:27:46 +05:30
|
|
|
(struct sockaddr *) &remaddr, rem_port,
|
|
|
|
"tcp", flags & NETSTAT_NUMERIC);
|
|
|
|
printf(net_conn_line,
|
2007-06-19 16:42:46 +05:30
|
|
|
"tcp", rxq, txq, l, r, tcp_state[state]);
|
|
|
|
free(l);
|
|
|
|
free(r);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2007-12-27 03:19:33 +05:30
|
|
|
static int udp_do_one(int lnr, char *line)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
|
|
|
char local_addr[64], rem_addr[64];
|
2007-01-30 04:21:25 +05:30
|
|
|
const char *state_str;
|
|
|
|
char more[512];
|
2001-10-31 16:30:46 +05:30
|
|
|
int num, local_port, rem_port, d, state, timer_run, uid, timeout;
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2002-07-03 17:16:38 +05:30
|
|
|
struct sockaddr_in6 localaddr, remaddr;
|
|
|
|
#else
|
2001-10-31 16:30:46 +05:30
|
|
|
struct sockaddr_in localaddr, remaddr;
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2001-10-31 16:30:46 +05:30
|
|
|
unsigned long rxq, txq, time_len, retr, inode;
|
|
|
|
|
|
|
|
if (lnr == 0)
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
|
|
|
more[0] = '\0';
|
|
|
|
num = sscanf(line,
|
2007-05-16 05:27:46 +05:30
|
|
|
"%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
|
|
|
|
&d, local_addr, &local_port,
|
|
|
|
rem_addr, &rem_port, &state,
|
|
|
|
&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
|
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
|
2007-05-16 05:27:46 +05:30
|
|
|
/* Demangle what the kernel gives us */
|
|
|
|
build_ipv6_addr(local_addr, &localaddr);
|
|
|
|
build_ipv6_addr(rem_addr, &remaddr);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2001-10-31 16:30:46 +05:30
|
|
|
} else {
|
2007-05-16 05:27:46 +05:30
|
|
|
build_ipv4_addr(local_addr, &localaddr);
|
|
|
|
build_ipv4_addr(rem_addr, &remaddr);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (num < 10) {
|
2007-12-27 03:19:33 +05:30
|
|
|
return 1; /* error */
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
switch (state) {
|
|
|
|
case TCP_ESTABLISHED:
|
|
|
|
state_str = "ESTABLISHED";
|
|
|
|
break;
|
|
|
|
case TCP_CLOSE:
|
|
|
|
state_str = "";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state_str = "UNKNOWN";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2007-05-16 05:27:46 +05:30
|
|
|
# define notnull(A) ( \
|
|
|
|
( (A.sin6_family == AF_INET6) \
|
|
|
|
&& (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
|
|
|
|
A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
|
|
|
|
) || ( \
|
|
|
|
(A.sin6_family == AF_INET) \
|
|
|
|
&& ((struct sockaddr_in*)&A)->sin_addr.s_addr \
|
|
|
|
) \
|
|
|
|
)
|
2002-07-03 17:16:38 +05:30
|
|
|
#else
|
2002-11-26 08:10:56 +05:30
|
|
|
# define notnull(A) (A.sin_addr.s_addr)
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2007-05-16 05:27:46 +05:30
|
|
|
{
|
|
|
|
int have_remaddr = notnull(remaddr);
|
|
|
|
if ((have_remaddr && (flags & NETSTAT_CONNECTED))
|
|
|
|
|| (!have_remaddr && (flags & NETSTAT_LISTENING))
|
|
|
|
) {
|
2007-06-19 16:42:46 +05:30
|
|
|
char *l = ip_port_str(
|
2007-05-16 05:27:46 +05:30
|
|
|
(struct sockaddr *) &localaddr, local_port,
|
|
|
|
"udp", flags & NETSTAT_NUMERIC);
|
2007-06-19 16:42:46 +05:30
|
|
|
char *r = ip_port_str(
|
2007-05-16 05:27:46 +05:30
|
|
|
(struct sockaddr *) &remaddr, rem_port,
|
|
|
|
"udp", flags & NETSTAT_NUMERIC);
|
|
|
|
printf(net_conn_line,
|
2007-06-19 16:42:46 +05:30
|
|
|
"udp", rxq, txq, l, r, state_str);
|
|
|
|
free(l);
|
|
|
|
free(r);
|
2007-05-16 05:27:46 +05:30
|
|
|
}
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2007-12-27 03:19:33 +05:30
|
|
|
static int raw_do_one(int lnr, char *line)
|
2001-10-31 16:30:46 +05:30
|
|
|
{
|
|
|
|
char local_addr[64], rem_addr[64];
|
2007-05-16 05:27:46 +05:30
|
|
|
char more[512];
|
2001-10-31 16:30:46 +05:30
|
|
|
int num, local_port, rem_port, d, state, timer_run, uid, timeout;
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2002-07-03 17:16:38 +05:30
|
|
|
struct sockaddr_in6 localaddr, remaddr;
|
|
|
|
#else
|
2001-10-31 16:30:46 +05:30
|
|
|
struct sockaddr_in localaddr, remaddr;
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2001-10-31 16:30:46 +05:30
|
|
|
unsigned long rxq, txq, time_len, retr, inode;
|
|
|
|
|
|
|
|
if (lnr == 0)
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
|
|
|
|
more[0] = '\0';
|
|
|
|
num = sscanf(line,
|
2007-05-16 05:27:46 +05:30
|
|
|
"%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
|
|
|
|
&d, local_addr, &local_port,
|
|
|
|
rem_addr, &rem_port, &state,
|
|
|
|
&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
|
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
|
2007-05-16 05:27:46 +05:30
|
|
|
build_ipv6_addr(local_addr, &localaddr);
|
|
|
|
build_ipv6_addr(rem_addr, &remaddr);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2001-10-31 16:30:46 +05:30
|
|
|
} else {
|
2007-05-16 05:27:46 +05:30
|
|
|
build_ipv4_addr(local_addr, &localaddr);
|
|
|
|
build_ipv4_addr(rem_addr, &remaddr);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (num < 10) {
|
2007-12-27 03:19:33 +05:30
|
|
|
return 1; /* error */
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
{
|
|
|
|
int have_remaddr = notnull(remaddr);
|
|
|
|
if ((have_remaddr && (flags & NETSTAT_CONNECTED))
|
|
|
|
|| (!have_remaddr && (flags & NETSTAT_LISTENING))
|
|
|
|
) {
|
2007-06-19 16:42:46 +05:30
|
|
|
char *l = ip_port_str(
|
2007-05-16 05:27:46 +05:30
|
|
|
(struct sockaddr *) &localaddr, local_port,
|
|
|
|
"raw", flags & NETSTAT_NUMERIC);
|
2007-06-19 16:42:46 +05:30
|
|
|
char *r = ip_port_str(
|
2007-05-16 05:27:46 +05:30
|
|
|
(struct sockaddr *) &remaddr, rem_port,
|
|
|
|
"raw", flags & NETSTAT_NUMERIC);
|
|
|
|
printf(net_conn_line,
|
2007-06-19 16:42:46 +05:30
|
|
|
"raw", rxq, txq, l, r, itoa(state));
|
|
|
|
free(l);
|
|
|
|
free(r);
|
2007-05-16 05:27:46 +05:30
|
|
|
}
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0;
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
|
2007-12-27 03:19:33 +05:30
|
|
|
static int unix_do_one(int nr, 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;
|
|
|
|
void *d;
|
|
|
|
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
|
|
|
if (nr == 0)
|
2007-12-27 03:19:33 +05:30
|
|
|
return 0; /* skip header */
|
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 */
|
|
|
|
num = sscanf(line, "%p: %lX %lX %lX %X %X %lu %n",
|
|
|
|
&d, &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
|
|
|
|
if (num < 7) {
|
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, "]");
|
|
|
|
|
2007-12-30 07:29:53 +05:30
|
|
|
printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
|
|
|
|
ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
|
|
|
|
);
|
|
|
|
|
|
|
|
/* TODO: currently we stop at first NUL byte. Is it a problem? */
|
|
|
|
line += path_ofs;
|
|
|
|
*strchrnul(line, '\n') = '\0';
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#define _PATH_PROCNET_UDP "/proc/net/udp"
|
2002-07-03 17:16:38 +05:30
|
|
|
#define _PATH_PROCNET_UDP6 "/proc/net/udp6"
|
2001-10-31 16:30:46 +05:30
|
|
|
#define _PATH_PROCNET_TCP "/proc/net/tcp"
|
2002-07-03 17:16:38 +05:30
|
|
|
#define _PATH_PROCNET_TCP6 "/proc/net/tcp6"
|
2001-10-31 16:30:46 +05:30
|
|
|
#define _PATH_PROCNET_RAW "/proc/net/raw"
|
2002-07-03 17:16:38 +05:30
|
|
|
#define _PATH_PROCNET_RAW6 "/proc/net/raw6"
|
2001-10-31 16:30:46 +05:30
|
|
|
#define _PATH_PROCNET_UNIX "/proc/net/unix"
|
|
|
|
|
2007-12-27 03:19:33 +05:30
|
|
|
static void do_info(const char *file, const char *name, int (*proc)(int, 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
|
|
|
|
2002-06-22 23:02:58 +05:30
|
|
|
procinfo = fopen(file, "r");
|
2001-10-31 16:30:46 +05:30
|
|
|
if (procinfo == NULL) {
|
|
|
|
if (errno != ENOENT) {
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg(file);
|
2002-06-22 23:02:58 +05:30
|
|
|
} else {
|
2007-12-27 03:19:33 +05:30
|
|
|
bb_error_msg("no kernel support for %s", name);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
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;
|
2007-12-27 02:14:45 +05:30
|
|
|
/* Why? because xmalloc_fgets_str doesn't stop on NULs */
|
|
|
|
while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
|
2007-12-27 03:19:33 +05:30
|
|
|
if (proc(lnr++, buffer))
|
|
|
|
bb_error_msg("%s: bogus data on line %d", file, 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
|
|
|
{
|
2007-05-17 03:55:35 +05:30
|
|
|
const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
|
2006-10-04 02:30:06 +05:30
|
|
|
unsigned opt;
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2007-05-16 05:27:46 +05:30
|
|
|
smallint inet = 1;
|
|
|
|
smallint inet6 = 1;
|
2002-07-03 17:16:38 +05:30
|
|
|
#else
|
2007-01-22 19:42:08 +05:30
|
|
|
enum { inet = 1, inet6 = 0 };
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
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);
|
2006-09-22 21:32:40 +05:30
|
|
|
if (opt & 0x1) { // -l
|
|
|
|
flags &= ~NETSTAT_CONNECTED;
|
|
|
|
flags |= NETSTAT_LISTENING;
|
|
|
|
}
|
|
|
|
if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
|
|
|
|
//if (opt & 0x4) // -e
|
|
|
|
if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
|
|
|
|
//if (opt & 0x10) // -t: NETSTAT_TCP
|
|
|
|
//if (opt & 0x20) // -u: NETSTAT_UDP
|
|
|
|
//if (opt & 0x40) // -w: NETSTAT_RAW
|
|
|
|
//if (opt & 0x80) // -x: NETSTAT_UNIX
|
|
|
|
if (opt & OPT_showroute) { // -r
|
2007-01-22 19:42:08 +05:30
|
|
|
#if ENABLE_ROUTE
|
|
|
|
bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
|
2004-03-15 13:59:22 +05:30
|
|
|
return 0;
|
2002-05-15 04:33:23 +05:30
|
|
|
#else
|
2007-05-16 05:27:46 +05:30
|
|
|
bb_show_usage();
|
2002-05-15 04:33:23 +05:30
|
|
|
#endif
|
2004-03-15 13:59:22 +05:30
|
|
|
}
|
|
|
|
|
2007-05-16 05:27:46 +05:30
|
|
|
if (opt & OPT_widedisplay) { // -W
|
|
|
|
net_conn_line = PRINT_NET_CONN_WIDE;
|
2007-05-17 03:55:35 +05:30
|
|
|
net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
|
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)) {
|
2001-10-31 16:30:46 +05:30
|
|
|
printf("Active Internet connections "); /* xxx */
|
|
|
|
|
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)");
|
2007-05-17 03:55:35 +05:30
|
|
|
printf(net_conn_line_header, "Local Address", "Foreign Address");
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
2007-01-22 19:42:08 +05:30
|
|
|
if (inet && flags & NETSTAT_TCP)
|
|
|
|
do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one);
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
if (inet6 && flags & NETSTAT_TCP)
|
|
|
|
do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2007-01-22 19:42:08 +05:30
|
|
|
if (inet && flags & NETSTAT_UDP)
|
|
|
|
do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one);
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
if (inet6 && flags & NETSTAT_UDP)
|
|
|
|
do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
2007-01-22 19:42:08 +05:30
|
|
|
if (inet && flags & NETSTAT_RAW)
|
|
|
|
do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one);
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
if (inet6 && flags & NETSTAT_RAW)
|
|
|
|
do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one);
|
2002-07-03 17:16:38 +05:30
|
|
|
#endif
|
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)");
|
2001-10-31 16:30:46 +05:30
|
|
|
printf("\nProto RefCnt Flags Type State I-Node Path\n");
|
2007-01-22 19:42:08 +05:30
|
|
|
do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one);
|
2001-10-31 16:30:46 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|