blob: 3ab7b0d212c43e3f9d450e779299615de5ab9acc [file] [log] [blame]
Eric Andersen31a0ece2001-10-31 11:00:46 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini netstat implementation(s) for busybox
4 * based in part on the netstat implementation from net-tools.
5 *
Eric Andersen51b8bd62002-07-03 11:46:38 +00006 * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com>
Eric Andersen31a0ece2001-10-31 11:00:46 +00007 *
Eric Andersen51b8bd62002-07-03 11:46:38 +00008 * 2002-04-20
9 * IPV6 support added by Bart Visscher <magick@linux-fan.com>
"Robert P. J. Day"2819f752006-07-11 11:32:31 +000010 *
Denis Vlasenko6e69e422008-07-27 12:10:07 +000011 * 2008-07-10
12 * optional '-p' flag support ported from net-tools by G. Somlo <somlo@cmu.edu>
13 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen31a0ece2001-10-31 11:00:46 +000015 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010016//config:config NETSTAT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020017//config: bool "netstat (10 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010018//config: default y
Denys Vlasenko47367e12016-11-23 09:05:14 +010019//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: netstat prints information about the Linux networking subsystem.
Denys Vlasenko47367e12016-11-23 09:05:14 +010021//config:
22//config:config FEATURE_NETSTAT_WIDE
Denys Vlasenkof5604222017-01-10 14:58:54 +010023//config: bool "Enable wide output"
Denys Vlasenko47367e12016-11-23 09:05:14 +010024//config: default y
25//config: depends on NETSTAT
26//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: Add support for wide columns. Useful when displaying IPv6 addresses
28//config: (-W option).
Denys Vlasenko47367e12016-11-23 09:05:14 +010029//config:
30//config:config FEATURE_NETSTAT_PRG
31//config: bool "Enable PID/Program name output"
32//config: default y
33//config: depends on NETSTAT
34//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020035//config: Add support for -p flag to print out PID and program name.
36//config: +700 bytes of code.
Denys Vlasenko47367e12016-11-23 09:05:14 +010037
38//applet:IF_NETSTAT(APPLET(netstat, BB_DIR_BIN, BB_SUID_DROP))
39
40//kbuild:lib-$(CONFIG_NETSTAT) += netstat.o
Eric Andersen31a0ece2001-10-31 11:00:46 +000041
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000042#include "libbb.h"
Rob Landleyc9c1a412006-07-12 19:17:55 +000043#include "inet_common.h"
Eric Andersen31a0ece2001-10-31 11:00:46 +000044
Bob Dunlop55a046b2010-10-27 02:12:29 +020045//usage:#define netstat_trivial_usage
46//usage: "[-"IF_ROUTE("r")"al] [-tuwx] [-en"IF_FEATURE_NETSTAT_WIDE("W")IF_FEATURE_NETSTAT_PRG("p")"]"
47//usage:#define netstat_full_usage "\n\n"
48//usage: "Display networking information\n"
Bob Dunlop55a046b2010-10-27 02:12:29 +020049//usage: IF_ROUTE(
50//usage: "\n -r Routing table"
51//usage: )
52//usage: "\n -a All sockets"
53//usage: "\n -l Listening sockets"
54//usage: "\n Else: connected sockets"
55//usage: "\n -t TCP sockets"
56//usage: "\n -u UDP sockets"
57//usage: "\n -w Raw sockets"
58//usage: "\n -x Unix sockets"
59//usage: "\n Else: all socket types"
60//usage: "\n -e Other/more information"
61//usage: "\n -n Don't resolve names"
62//usage: IF_FEATURE_NETSTAT_WIDE(
63//usage: "\n -W Wide display"
64//usage: )
65//usage: IF_FEATURE_NETSTAT_PRG(
66//usage: "\n -p Show PID/program name for sockets"
67//usage: )
68
Denis Vlasenko6e69e422008-07-27 12:10:07 +000069#define NETSTAT_OPTS "laentuwx" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000070 IF_ROUTE( "r") \
71 IF_FEATURE_NETSTAT_WIDE("W") \
72 IF_FEATURE_NETSTAT_PRG( "p")
Denis Vlasenko6e69e422008-07-27 12:10:07 +000073
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000074enum {
Denis Vlasenko6e69e422008-07-27 12:10:07 +000075 OPT_sock_listen = 1 << 0, // l
76 OPT_sock_all = 1 << 1, // a
77 OPT_extended = 1 << 2, // e
78 OPT_noresolve = 1 << 3, // n
79 OPT_sock_tcp = 1 << 4, // t
80 OPT_sock_udp = 1 << 5, // u
81 OPT_sock_raw = 1 << 6, // w
82 OPT_sock_unix = 1 << 7, // x
Bob Dunlop55a046b2010-10-27 02:12:29 +020083 OPTBIT_x = 7,
84 IF_ROUTE( OPTBIT_ROUTE,)
85 IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
86 IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000087 OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
88 OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
89 OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000090};
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000091
Denis Vlasenko703e2022007-01-22 14:12:08 +000092#define NETSTAT_CONNECTED 0x01
93#define NETSTAT_LISTENING 0x02
94#define NETSTAT_NUMERIC 0x04
Denis Vlasenko67b23e62006-10-03 21:00:06 +000095/* Must match getopt32 option string */
Denis Vlasenko703e2022007-01-22 14:12:08 +000096#define NETSTAT_TCP 0x10
97#define NETSTAT_UDP 0x20
98#define NETSTAT_RAW 0x40
99#define NETSTAT_UNIX 0x80
100#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000101
Eric Andersen31a0ece2001-10-31 11:00:46 +0000102
Eric Andersen31a0ece2001-10-31 11:00:46 +0000103enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000104 TCP_ESTABLISHED = 1,
105 TCP_SYN_SENT,
106 TCP_SYN_RECV,
107 TCP_FIN_WAIT1,
108 TCP_FIN_WAIT2,
109 TCP_TIME_WAIT,
110 TCP_CLOSE,
111 TCP_CLOSE_WAIT,
112 TCP_LAST_ACK,
113 TCP_LISTEN,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000114 TCP_CLOSING, /* now a valid state */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000115};
116
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000117static const char *const tcp_state[] = {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000118 "",
119 "ESTABLISHED",
120 "SYN_SENT",
121 "SYN_RECV",
122 "FIN_WAIT1",
123 "FIN_WAIT2",
124 "TIME_WAIT",
125 "CLOSE",
126 "CLOSE_WAIT",
127 "LAST_ACK",
128 "LISTEN",
129 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000130};
131
132typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000133 SS_FREE = 0, /* not allocated */
134 SS_UNCONNECTED, /* unconnected to any socket */
135 SS_CONNECTING, /* in process of connecting */
136 SS_CONNECTED, /* connected to socket */
137 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000138} socket_state;
139
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200140#define SO_ACCEPTCON (1<<16) /* performed a listen */
141#define SO_WAITDATA (1<<17) /* wait data to read */
142#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000143
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200144#define ADDR_NORMAL_WIDTH 23
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000145/* When there are IPv6 connections the IPv6 addresses will be
146 * truncated to none-recognition. The '-W' option makes the
Maninder Singh97c64912015-05-25 13:46:36 +0200147 * address columns wide enough to accommodate for longest possible
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000148 * IPv6 addresses, i.e. addresses of the form
149 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
150 */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200151#define ADDR_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
152#if ENABLE_FEATURE_NETSTAT_WIDE
Denys Vlasenko327f5502013-11-29 16:45:45 +0100153# define FMT_NET_CONN_DATA "%s %6lu %6lu %-*s %-*s %-12s"
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200154# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-*s %-*s State %s\n"
155#else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100156# define FMT_NET_CONN_DATA "%s %6lu %6lu %-23s %-23s %-12s"
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200157# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State %s\n"
158#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000159
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000160#define PROGNAME_WIDTH 20
161#define PROGNAME_WIDTH_STR "20"
162/* PROGNAME_WIDTH chars: 12345678901234567890 */
163#define PROGNAME_BANNER "PID/Program name "
164
165struct prg_node {
166 struct prg_node *next;
167 long inode;
168 char name[PROGNAME_WIDTH];
169};
170
171#define PRG_HASH_SIZE 211
172
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000173struct globals {
Denys Vlasenko1f1492b2019-10-25 13:00:01 +0200174 smalluint flags;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000175#if ENABLE_FEATURE_NETSTAT_PRG
176 smallint prg_cache_loaded;
177 struct prg_node *prg_hash[PRG_HASH_SIZE];
178#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200179#if ENABLE_FEATURE_NETSTAT_PRG
180 const char *progname_banner;
181#endif
182#if ENABLE_FEATURE_NETSTAT_WIDE
183 unsigned addr_width;
184#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000185};
186#define G (*ptr_to_globals)
187#define flags (G.flags )
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000188#define prg_cache_loaded (G.prg_cache_loaded)
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200189#define prg_hash (G.prg_hash )
190#if ENABLE_FEATURE_NETSTAT_PRG
191# define progname_banner (G.progname_banner )
192#else
193# define progname_banner ""
194#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000195#define INIT_G() do { \
196 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
197 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000198} while (0)
199
200
201#if ENABLE_FEATURE_NETSTAT_PRG
202
203/* Deliberately truncating long to unsigned *int* */
204#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
205
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000206static void prg_cache_add(long inode, char *name)
207{
208 unsigned hi = PRG_HASHIT(inode);
209 struct prg_node **pnp, *pn;
210
211 prg_cache_loaded = 2;
212 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
213 if (pn->inode == inode) {
214 /* Some warning should be appropriate here
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100215 * as we got multiple processes for one i-node */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000216 return;
217 }
218 }
219 *pnp = xzalloc(sizeof(struct prg_node));
220 pn = *pnp;
221 pn->inode = inode;
222 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
223}
224
225static const char *prg_cache_get(long inode)
226{
227 unsigned hi = PRG_HASHIT(inode);
228 struct prg_node *pn;
229
230 for (pn = prg_hash[hi]; pn; pn = pn->next)
231 if (pn->inode == inode)
232 return pn->name;
233 return "-";
234}
235
236#if ENABLE_FEATURE_CLEAN_UP
237static void prg_cache_clear(void)
238{
239 struct prg_node **pnp, *pn;
240
241 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
242 while ((pn = *pnp) != NULL) {
243 *pnp = pn->next;
244 free(pn);
245 }
246 }
247}
248#else
249#define prg_cache_clear() ((void)0)
250#endif
251
Denis Vlasenkocf8b1ef2008-07-27 14:15:42 +0000252static long extract_socket_inode(const char *lname)
253{
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000254 long inode = -1;
255
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100256 if (is_prefixed_with(lname, "socket:[")) {
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000257 /* "socket:[12345]", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200258 inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000259 if (*lname != ']')
260 inode = -1;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100261 } else if (is_prefixed_with(lname, "[0000]:")) {
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000262 /* "[0000]:12345", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200263 inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000264 if (errno) /* not NUL terminated? */
265 inode = -1;
266 }
267
268#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
269 if (errno == ERANGE)
270 inode = -1;
271#endif
272 return inode;
273}
274
Denys Vlasenko689d0652020-10-01 21:52:16 +0200275static int FAST_FUNC add_to_prg_cache_if_socket(struct recursive_state *state,
276 const char *fileName,
277 struct stat *statbuf UNUSED_PARAM)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000278{
279 char *linkname;
280 long inode;
281
282 linkname = xmalloc_readlink(fileName);
283 if (linkname != NULL) {
284 inode = extract_socket_inode(linkname);
285 free(linkname);
Denys Vlasenko689d0652020-10-01 21:52:16 +0200286 if (inode >= 0) {
287 char *pid_slash_progname = state->userData;
288 prg_cache_add(inode, pid_slash_progname);
289 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000290 }
291 return TRUE;
292}
293
Denys Vlasenko689d0652020-10-01 21:52:16 +0200294static int FAST_FUNC dir_act(struct recursive_state *state,
295 const char *fileName,
296 struct stat *statbuf UNUSED_PARAM)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000297{
Bob Dunlop55a046b2010-10-27 02:12:29 +0200298 const char *pid;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200299 char *pid_slash_progname;
300 char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000301 char cmdline_buf[512];
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200302 int n, len;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000303
Denys Vlasenko689d0652020-10-01 21:52:16 +0200304 if (state->depth == 0) /* "/proc" itself */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000305 return TRUE; /* continue looking one level below /proc */
306
Bob Dunlop55a046b2010-10-27 02:12:29 +0200307 pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
308 if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000309 return SKIP;
310
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200311 len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName);
312 n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1);
313 if (n < 0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000314 return FALSE;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200315 cmdline_buf[n] = '\0';
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000316
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200317 /* go through all files in /proc/PID/fd and check whether they are sockets */
318 strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
Bob Dunlop55a046b2010-10-27 02:12:29 +0200319 pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200320 n = recursive_action(proc_pid_fname,
321 ACTION_RECURSE | ACTION_QUIET,
322 add_to_prg_cache_if_socket,
323 NULL,
Denys Vlasenko689d0652020-10-01 21:52:16 +0200324 (void *)pid_slash_progname
325 );
Bob Dunlop55a046b2010-10-27 02:12:29 +0200326 free(pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000327
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200328 if (!n)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200329 return FALSE; /* signal permissions error to caller */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000330
Bob Dunlop55a046b2010-10-27 02:12:29 +0200331 return SKIP; /* caller should not recurse further into this dir */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000332}
333
334static void prg_cache_load(void)
335{
336 int load_ok;
337
338 prg_cache_loaded = 1;
339 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
Denys Vlasenko689d0652020-10-01 21:52:16 +0200340 NULL, dir_act, NULL);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000341 if (load_ok)
342 return;
343
344 if (prg_cache_loaded == 1)
James Byrne69374872019-07-02 11:35:03 +0200345 bb_simple_error_msg("can't scan /proc - are you root?");
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000346 else
James Byrne69374872019-07-02 11:35:03 +0200347 bb_simple_error_msg("showing only processes with your user ID");
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000348}
349
350#else
351
352#define prg_cache_clear() ((void)0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000353
354#endif //ENABLE_FEATURE_NETSTAT_PRG
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000355
356
357#if ENABLE_FEATURE_IPV6
358static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
359{
360 char addr6[INET6_ADDRSTRLEN];
361 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000362
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000363 sscanf(local_addr, "%08X%08X%08X%08X",
364 &in6.s6_addr32[0], &in6.s6_addr32[1],
365 &in6.s6_addr32[2], &in6.s6_addr32[3]);
366 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100367 inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000368
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000369 localaddr->sin6_family = AF_INET6;
370}
371#endif
372
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000373static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000374{
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100375 sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
376 localaddr->sin_family = AF_INET;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000377}
378
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000379static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000380{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000381 if (!port)
382 return "*";
383 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000384 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000385 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000386 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000387 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000388 /* hummm, we may return static buffer here!! */
389 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000390}
391
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000392static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000393{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000394 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000395
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000396 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
397 * in IPv6, while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000398
Mark Marshall98592db2018-03-19 10:45:40 +0100399 host = NULL;
400 if (!numeric)
401 host = xmalloc_sockaddr2host_noport(addr);
402 if (!host)
403 host = xmalloc_sockaddr2dotted_noport(addr);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000404
405 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
406 free(host);
407 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000408}
409
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000410struct inet_params {
411 int local_port, rem_port, state, uid;
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100412 union {
413 struct sockaddr sa;
414 struct sockaddr_in sin;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000415#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100416 struct sockaddr_in6 sin6;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000417#endif
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100418 } localaddr, remaddr;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000419 unsigned long rxq, txq, inode;
420};
Eric Andersen31a0ece2001-10-31 11:00:46 +0000421
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000422static int scan_inet_proc_line(struct inet_params *param, char *line)
423{
424 int num;
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200425 /* IPv6 /proc files use 32-char hex representation
426 * of IPv6 address, followed by :PORT_IN_HEX
427 */
428 char local_addr[33], rem_addr[33]; /* 32 + 1 for NUL */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000429
Eric Andersen31a0ece2001-10-31 11:00:46 +0000430 num = sscanf(line,
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200431 "%*d: %32[0-9A-Fa-f]:%X "
432 "%32[0-9A-Fa-f]:%X %X "
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000433 "%lX:%lX %*X:%*X "
Denys Vlasenko327f5502013-11-29 16:45:45 +0100434 "%*X %d %*d %lu ",
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000435 local_addr, &param->local_port,
436 rem_addr, &param->rem_port, &param->state,
437 &param->txq, &param->rxq,
438 &param->uid, &param->inode);
439 if (num < 9) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000440 return 1; /* error */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000441 }
442
Eric Andersen31a0ece2001-10-31 11:00:46 +0000443 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000444#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100445 build_ipv6_addr(local_addr, &param->localaddr.sin6);
446 build_ipv6_addr(rem_addr, &param->remaddr.sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000447#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000448 } else {
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100449 build_ipv4_addr(local_addr, &param->localaddr.sin);
450 build_ipv4_addr(rem_addr, &param->remaddr.sin);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000451 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000452 return 0;
453}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000454
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000455static void print_inet_line(struct inet_params *param,
456 const char *state_str, const char *proto, int is_connected)
457{
458 if ((is_connected && (flags & NETSTAT_CONNECTED))
459 || (!is_connected && (flags & NETSTAT_LISTENING))
Denis Vlasenko703e2022007-01-22 14:12:08 +0000460 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000461 char *l = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100462 &param->localaddr.sa, param->local_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000463 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000464 char *r = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100465 &param->remaddr.sa, param->rem_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000466 proto, flags & NETSTAT_NUMERIC);
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200467 printf(FMT_NET_CONN_DATA,
468 proto, param->rxq, param->txq,
469 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
470 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
471 state_str);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000472#if ENABLE_FEATURE_NETSTAT_PRG
473 if (option_mask32 & OPT_prg)
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000474 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000475#endif
476 bb_putchar('\n');
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000477 free(l);
478 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000479 }
480}
481
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000482static int FAST_FUNC tcp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000483{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000484 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000485
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200486 memset(&param, 0, sizeof(param));
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000487 if (scan_inet_proc_line(&param, line))
488 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000489
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000490 print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
491 return 0;
492}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000493
Denis Vlasenko703e2022007-01-22 14:12:08 +0000494#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100495# define NOT_NULL_ADDR(A) ( \
496 ( (A.sa.sa_family == AF_INET6) \
497 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
498 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
499 ) || ( \
500 (A.sa.sa_family == AF_INET) \
501 && A.sin.sin_addr.s_addr != 0 \
502 ) \
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000503)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000504#else
Denys Vlasenko45e97922010-02-19 08:29:32 +0100505# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000506#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000507
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000508static int FAST_FUNC udp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000509{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000510 int have_remaddr;
511 const char *state_str;
512 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000513
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200514 memset(&param, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000515 if (scan_inet_proc_line(&param, line))
516 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000517
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000518 state_str = "UNKNOWN";
519 switch (param.state) {
520 case TCP_ESTABLISHED:
521 state_str = "ESTABLISHED";
522 break;
523 case TCP_CLOSE:
524 state_str = "";
525 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000526 }
527
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100528 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000529 print_inet_line(&param, state_str, "udp", have_remaddr);
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000530 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000531}
532
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000533static int FAST_FUNC raw_do_one(char *line)
534{
535 int have_remaddr;
536 struct inet_params param;
537
538 if (scan_inet_proc_line(&param, line))
539 return 1;
540
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100541 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000542 print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
543 return 0;
544}
545
546static int FAST_FUNC unix_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000547{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000548 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000549 unsigned long inode;
550 int type, state;
551 int num, path_ofs;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000552 const char *ss_proto, *ss_state, *ss_type;
553 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000554
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000555 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000556 * Other users report long lines filled by NUL bytes.
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000557 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000558 if (!line[0])
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000559 return 0;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000560
561 path_ofs = 0; /* paranoia */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000562 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
563 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
564 if (num < 6) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000565 return 1; /* error */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000566 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000567 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000568 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000569 if (!(flags & NETSTAT_LISTENING))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000570 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000571 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000572 if (!(flags & NETSTAT_CONNECTED))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000573 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000574 }
575 }
576
577 switch (proto) {
578 case 0:
579 ss_proto = "unix";
580 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000581 default:
582 ss_proto = "??";
583 }
584
585 switch (type) {
586 case SOCK_STREAM:
587 ss_type = "STREAM";
588 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000589 case SOCK_DGRAM:
590 ss_type = "DGRAM";
591 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000592 case SOCK_RAW:
593 ss_type = "RAW";
594 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000595 case SOCK_RDM:
596 ss_type = "RDM";
597 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000598 case SOCK_SEQPACKET:
599 ss_type = "SEQPACKET";
600 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000601 default:
602 ss_type = "UNKNOWN";
603 }
604
605 switch (state) {
606 case SS_FREE:
607 ss_state = "FREE";
608 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000609 case SS_UNCONNECTED:
610 /*
611 * Unconnected sockets may be listening
612 * for something.
613 */
614 if (unix_flags & SO_ACCEPTCON) {
615 ss_state = "LISTENING";
616 } else {
617 ss_state = "";
618 }
619 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000620 case SS_CONNECTING:
621 ss_state = "CONNECTING";
622 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000623 case SS_CONNECTED:
624 ss_state = "CONNECTED";
625 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000626 case SS_DISCONNECTING:
627 ss_state = "DISCONNECTING";
628 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000629 default:
630 ss_state = "UNKNOWN";
631 }
632
633 strcpy(ss_flags, "[ ");
634 if (unix_flags & SO_ACCEPTCON)
635 strcat(ss_flags, "ACC ");
636 if (unix_flags & SO_WAITDATA)
637 strcat(ss_flags, "W ");
638 if (unix_flags & SO_NOSPACE)
639 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000640 strcat(ss_flags, "]");
641
Denys Vlasenko327f5502013-11-29 16:45:45 +0100642 printf("%-5s %-6lu %-11s %-10s %-13s %6lu ",
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000643 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
644 );
645
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000646#if ENABLE_FEATURE_NETSTAT_PRG
647 if (option_mask32 & OPT_prg)
648 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
649#endif
650
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000651 /* TODO: currently we stop at first NUL byte. Is it a problem? */
652 line += path_ofs;
Ron Yorston8ec1ff32015-03-12 20:18:51 +0100653 chomp(line);
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000654 while (*line)
655 fputc_printable(*line++, stdout);
656 bb_putchar('\n');
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000657 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000658}
659
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000660static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000661{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000662 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000663 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000664 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000665
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000666 /* _stdin is just to save "r" param */
667 procinfo = fopen_or_warn_stdin(file);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000668 if (procinfo == NULL) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000669 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000670 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000671 lnr = 0;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000672 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000673 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000674 /* line 0 is skipped */
675 if (lnr && proc(buffer))
676 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
677 lnr++;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000678 free(buffer);
679 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000680 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000681}
682
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000683int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000684int netstat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000685{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000686 unsigned opt;
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000687
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000688 INIT_G();
689
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000690 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000691 opt = getopt32(argv, NETSTAT_OPTS);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200692 if (opt & OPT_sock_listen) { // -l
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000693 flags &= ~NETSTAT_CONNECTED;
694 flags |= NETSTAT_LISTENING;
695 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200696 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
697 //if (opt & OPT_extended) // -e
698 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
699 //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
700 //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
701 //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
702 //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
Denis Vlasenko703e2022007-01-22 14:12:08 +0000703#if ENABLE_ROUTE
Bob Dunlop55a046b2010-10-27 02:12:29 +0200704 if (opt & OPT_route) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000705 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000706 return 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000707 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200708#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200709#if ENABLE_FEATURE_NETSTAT_WIDE
710 G.addr_width = ADDR_NORMAL_WIDTH;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000711 if (opt & OPT_wide) { // -W
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200712 G.addr_width = ADDR_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000713 }
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200714#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000715#if ENABLE_FEATURE_NETSTAT_PRG
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200716 progname_banner = "";
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000717 if (opt & OPT_prg) { // -p
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200718 progname_banner = PROGNAME_BANNER;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000719 prg_cache_load();
720 }
721#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000722
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000723 opt &= NETSTAT_ALLPROTO;
724 if (opt) {
725 flags &= ~NETSTAT_ALLPROTO;
726 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000727 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000728 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200729 printf("Active Internet connections "); /* xxx */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000730
Denis Vlasenko703e2022007-01-22 14:12:08 +0000731 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000732 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000733 else if (flags & NETSTAT_LISTENING)
734 printf("(only servers)");
735 else
736 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200737 printf(FMT_NET_CONN_HEADER,
738 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
739 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
740 progname_banner
741 );
Eric Andersen31a0ece2001-10-31 11:00:46 +0000742 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000743 if (flags & NETSTAT_TCP) {
744 do_info("/proc/net/tcp", tcp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000745#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000746 do_info("/proc/net/tcp6", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000747#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000748 }
749 if (flags & NETSTAT_UDP) {
750 do_info("/proc/net/udp", udp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000751#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000752 do_info("/proc/net/udp6", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000753#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000754 }
755 if (flags & NETSTAT_RAW) {
756 do_info("/proc/net/raw", raw_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000757#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000758 do_info("/proc/net/raw6", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000759#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000760 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000761 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000762 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000763 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000764 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000765 else if (flags & NETSTAT_LISTENING)
766 printf("(only servers)");
767 else
768 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200769 printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000770 do_info("/proc/net/unix", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000771 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000772 prg_cache_clear();
Eric Andersen31a0ece2001-10-31 11:00:46 +0000773 return 0;
774}