blob: 003c2ba6fa0eda81dd074dd59863be004995b664 [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
19//config: select PLATFORM_LINUX
20//config: help
21//config: netstat prints information about the Linux networking subsystem.
22//config:
23//config:config FEATURE_NETSTAT_WIDE
Denys Vlasenkof5604222017-01-10 14:58:54 +010024//config: bool "Enable wide output"
Denys Vlasenko47367e12016-11-23 09:05:14 +010025//config: default y
26//config: depends on NETSTAT
27//config: help
28//config: Add support for wide columns. Useful when displaying IPv6 addresses
29//config: (-W option).
30//config:
31//config:config FEATURE_NETSTAT_PRG
32//config: bool "Enable PID/Program name output"
33//config: default y
34//config: depends on NETSTAT
35//config: help
36//config: Add support for -p flag to print out PID and program name.
37//config: +700 bytes of code.
38
39//applet:IF_NETSTAT(APPLET(netstat, BB_DIR_BIN, BB_SUID_DROP))
40
41//kbuild:lib-$(CONFIG_NETSTAT) += netstat.o
Eric Andersen31a0ece2001-10-31 11:00:46 +000042
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000043#include "libbb.h"
Rob Landleyc9c1a412006-07-12 19:17:55 +000044#include "inet_common.h"
Eric Andersen31a0ece2001-10-31 11:00:46 +000045
Bob Dunlop55a046b2010-10-27 02:12:29 +020046//usage:#define netstat_trivial_usage
47//usage: "[-"IF_ROUTE("r")"al] [-tuwx] [-en"IF_FEATURE_NETSTAT_WIDE("W")IF_FEATURE_NETSTAT_PRG("p")"]"
48//usage:#define netstat_full_usage "\n\n"
49//usage: "Display networking information\n"
Bob Dunlop55a046b2010-10-27 02:12:29 +020050//usage: IF_ROUTE(
51//usage: "\n -r Routing table"
52//usage: )
53//usage: "\n -a All sockets"
54//usage: "\n -l Listening sockets"
55//usage: "\n Else: connected sockets"
56//usage: "\n -t TCP sockets"
57//usage: "\n -u UDP sockets"
58//usage: "\n -w Raw sockets"
59//usage: "\n -x Unix sockets"
60//usage: "\n Else: all socket types"
61//usage: "\n -e Other/more information"
62//usage: "\n -n Don't resolve names"
63//usage: IF_FEATURE_NETSTAT_WIDE(
64//usage: "\n -W Wide display"
65//usage: )
66//usage: IF_FEATURE_NETSTAT_PRG(
67//usage: "\n -p Show PID/program name for sockets"
68//usage: )
69
Denis Vlasenko6e69e422008-07-27 12:10:07 +000070#define NETSTAT_OPTS "laentuwx" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000071 IF_ROUTE( "r") \
72 IF_FEATURE_NETSTAT_WIDE("W") \
73 IF_FEATURE_NETSTAT_PRG( "p")
Denis Vlasenko6e69e422008-07-27 12:10:07 +000074
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000075enum {
Denis Vlasenko6e69e422008-07-27 12:10:07 +000076 OPT_sock_listen = 1 << 0, // l
77 OPT_sock_all = 1 << 1, // a
78 OPT_extended = 1 << 2, // e
79 OPT_noresolve = 1 << 3, // n
80 OPT_sock_tcp = 1 << 4, // t
81 OPT_sock_udp = 1 << 5, // u
82 OPT_sock_raw = 1 << 6, // w
83 OPT_sock_unix = 1 << 7, // x
Bob Dunlop55a046b2010-10-27 02:12:29 +020084 OPTBIT_x = 7,
85 IF_ROUTE( OPTBIT_ROUTE,)
86 IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
87 IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000088 OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
89 OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
90 OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000091};
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000092
Denis Vlasenko703e2022007-01-22 14:12:08 +000093#define NETSTAT_CONNECTED 0x01
94#define NETSTAT_LISTENING 0x02
95#define NETSTAT_NUMERIC 0x04
Denis Vlasenko67b23e62006-10-03 21:00:06 +000096/* Must match getopt32 option string */
Denis Vlasenko703e2022007-01-22 14:12:08 +000097#define NETSTAT_TCP 0x10
98#define NETSTAT_UDP 0x20
99#define NETSTAT_RAW 0x40
100#define NETSTAT_UNIX 0x80
101#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000102
Eric Andersen31a0ece2001-10-31 11:00:46 +0000103
Eric Andersen31a0ece2001-10-31 11:00:46 +0000104enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000105 TCP_ESTABLISHED = 1,
106 TCP_SYN_SENT,
107 TCP_SYN_RECV,
108 TCP_FIN_WAIT1,
109 TCP_FIN_WAIT2,
110 TCP_TIME_WAIT,
111 TCP_CLOSE,
112 TCP_CLOSE_WAIT,
113 TCP_LAST_ACK,
114 TCP_LISTEN,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000115 TCP_CLOSING, /* now a valid state */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000116};
117
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000118static const char *const tcp_state[] = {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000119 "",
120 "ESTABLISHED",
121 "SYN_SENT",
122 "SYN_RECV",
123 "FIN_WAIT1",
124 "FIN_WAIT2",
125 "TIME_WAIT",
126 "CLOSE",
127 "CLOSE_WAIT",
128 "LAST_ACK",
129 "LISTEN",
130 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000131};
132
133typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000134 SS_FREE = 0, /* not allocated */
135 SS_UNCONNECTED, /* unconnected to any socket */
136 SS_CONNECTING, /* in process of connecting */
137 SS_CONNECTED, /* connected to socket */
138 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000139} socket_state;
140
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200141#define SO_ACCEPTCON (1<<16) /* performed a listen */
142#define SO_WAITDATA (1<<17) /* wait data to read */
143#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000144
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200145#define ADDR_NORMAL_WIDTH 23
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000146/* When there are IPv6 connections the IPv6 addresses will be
147 * truncated to none-recognition. The '-W' option makes the
Maninder Singh97c64912015-05-25 13:46:36 +0200148 * address columns wide enough to accommodate for longest possible
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000149 * IPv6 addresses, i.e. addresses of the form
150 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
151 */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200152#define ADDR_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
153#if ENABLE_FEATURE_NETSTAT_WIDE
Denys Vlasenko327f5502013-11-29 16:45:45 +0100154# define FMT_NET_CONN_DATA "%s %6lu %6lu %-*s %-*s %-12s"
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200155# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-*s %-*s State %s\n"
156#else
Denys Vlasenko327f5502013-11-29 16:45:45 +0100157# define FMT_NET_CONN_DATA "%s %6lu %6lu %-23s %-23s %-12s"
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200158# define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State %s\n"
159#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000160
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000161#define PROGNAME_WIDTH 20
162#define PROGNAME_WIDTH_STR "20"
163/* PROGNAME_WIDTH chars: 12345678901234567890 */
164#define PROGNAME_BANNER "PID/Program name "
165
166struct prg_node {
167 struct prg_node *next;
168 long inode;
169 char name[PROGNAME_WIDTH];
170};
171
172#define PRG_HASH_SIZE 211
173
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000174struct globals {
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000175 smallint flags;
176#if ENABLE_FEATURE_NETSTAT_PRG
177 smallint prg_cache_loaded;
178 struct prg_node *prg_hash[PRG_HASH_SIZE];
179#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200180#if ENABLE_FEATURE_NETSTAT_PRG
181 const char *progname_banner;
182#endif
183#if ENABLE_FEATURE_NETSTAT_WIDE
184 unsigned addr_width;
185#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000186};
187#define G (*ptr_to_globals)
188#define flags (G.flags )
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000189#define prg_cache_loaded (G.prg_cache_loaded)
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200190#define prg_hash (G.prg_hash )
191#if ENABLE_FEATURE_NETSTAT_PRG
192# define progname_banner (G.progname_banner )
193#else
194# define progname_banner ""
195#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000196#define INIT_G() do { \
197 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
198 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000199} while (0)
200
201
202#if ENABLE_FEATURE_NETSTAT_PRG
203
204/* Deliberately truncating long to unsigned *int* */
205#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
206
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000207static void prg_cache_add(long inode, char *name)
208{
209 unsigned hi = PRG_HASHIT(inode);
210 struct prg_node **pnp, *pn;
211
212 prg_cache_loaded = 2;
213 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
214 if (pn->inode == inode) {
215 /* Some warning should be appropriate here
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100216 * as we got multiple processes for one i-node */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000217 return;
218 }
219 }
220 *pnp = xzalloc(sizeof(struct prg_node));
221 pn = *pnp;
222 pn->inode = inode;
223 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
224}
225
226static const char *prg_cache_get(long inode)
227{
228 unsigned hi = PRG_HASHIT(inode);
229 struct prg_node *pn;
230
231 for (pn = prg_hash[hi]; pn; pn = pn->next)
232 if (pn->inode == inode)
233 return pn->name;
234 return "-";
235}
236
237#if ENABLE_FEATURE_CLEAN_UP
238static void prg_cache_clear(void)
239{
240 struct prg_node **pnp, *pn;
241
242 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
243 while ((pn = *pnp) != NULL) {
244 *pnp = pn->next;
245 free(pn);
246 }
247 }
248}
249#else
250#define prg_cache_clear() ((void)0)
251#endif
252
Denis Vlasenkocf8b1ef2008-07-27 14:15:42 +0000253static long extract_socket_inode(const char *lname)
254{
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000255 long inode = -1;
256
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100257 if (is_prefixed_with(lname, "socket:[")) {
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000258 /* "socket:[12345]", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200259 inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000260 if (*lname != ']')
261 inode = -1;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100262 } else if (is_prefixed_with(lname, "[0000]:")) {
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000263 /* "[0000]:12345", extract the "12345" as inode */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200264 inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000265 if (errno) /* not NUL terminated? */
266 inode = -1;
267 }
268
269#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
270 if (errno == ERANGE)
271 inode = -1;
272#endif
273 return inode;
274}
275
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200276static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000277 struct stat *statbuf UNUSED_PARAM,
Bob Dunlop55a046b2010-10-27 02:12:29 +0200278 void *pid_slash_progname,
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000279 int depth UNUSED_PARAM)
280{
281 char *linkname;
282 long inode;
283
284 linkname = xmalloc_readlink(fileName);
285 if (linkname != NULL) {
286 inode = extract_socket_inode(linkname);
287 free(linkname);
288 if (inode >= 0)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200289 prg_cache_add(inode, (char *)pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000290 }
291 return TRUE;
292}
293
294static int FAST_FUNC dir_act(const char *fileName,
295 struct stat *statbuf UNUSED_PARAM,
296 void *userData UNUSED_PARAM,
297 int depth)
298{
Bob Dunlop55a046b2010-10-27 02:12:29 +0200299 const char *pid;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200300 char *pid_slash_progname;
301 char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000302 char cmdline_buf[512];
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200303 int n, len;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000304
305 if (depth == 0) /* "/proc" itself */
306 return TRUE; /* continue looking one level below /proc */
307
Bob Dunlop55a046b2010-10-27 02:12:29 +0200308 pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
309 if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000310 return SKIP;
311
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200312 len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName);
313 n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1);
314 if (n < 0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000315 return FALSE;
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200316 cmdline_buf[n] = '\0';
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000317
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200318 /* go through all files in /proc/PID/fd and check whether they are sockets */
319 strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
Bob Dunlop55a046b2010-10-27 02:12:29 +0200320 pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200321 n = recursive_action(proc_pid_fname,
322 ACTION_RECURSE | ACTION_QUIET,
323 add_to_prg_cache_if_socket,
324 NULL,
325 (void *)pid_slash_progname,
326 0);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200327 free(pid_slash_progname);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000328
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200329 if (!n)
Bob Dunlop55a046b2010-10-27 02:12:29 +0200330 return FALSE; /* signal permissions error to caller */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000331
Bob Dunlop55a046b2010-10-27 02:12:29 +0200332 return SKIP; /* caller should not recurse further into this dir */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000333}
334
335static void prg_cache_load(void)
336{
337 int load_ok;
338
339 prg_cache_loaded = 1;
340 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
341 NULL, dir_act, NULL, 0);
342 if (load_ok)
343 return;
344
345 if (prg_cache_loaded == 1)
346 bb_error_msg("can't scan /proc - are you root?");
347 else
348 bb_error_msg("showing only processes with your user ID");
349}
350
351#else
352
353#define prg_cache_clear() ((void)0)
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000354
355#endif //ENABLE_FEATURE_NETSTAT_PRG
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000356
357
358#if ENABLE_FEATURE_IPV6
359static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
360{
361 char addr6[INET6_ADDRSTRLEN];
362 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000363
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000364 sscanf(local_addr, "%08X%08X%08X%08X",
365 &in6.s6_addr32[0], &in6.s6_addr32[1],
366 &in6.s6_addr32[2], &in6.s6_addr32[3]);
367 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100368 inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000369
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000370 localaddr->sin6_family = AF_INET6;
371}
372#endif
373
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000374static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000375{
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100376 sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
377 localaddr->sin_family = AF_INET;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000378}
379
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000380static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000381{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000382 if (!port)
383 return "*";
384 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000385 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000386 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000387 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000388 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000389 /* hummm, we may return static buffer here!! */
390 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000391}
392
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000393static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000394{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000395 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000396
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000397 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
398 * in IPv6, while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000399
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000400 host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
401 : xmalloc_sockaddr2host_noport(addr);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000402
403 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
404 free(host);
405 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000406}
407
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000408struct inet_params {
409 int local_port, rem_port, state, uid;
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100410 union {
411 struct sockaddr sa;
412 struct sockaddr_in sin;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000413#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100414 struct sockaddr_in6 sin6;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000415#endif
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100416 } localaddr, remaddr;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000417 unsigned long rxq, txq, inode;
418};
Eric Andersen31a0ece2001-10-31 11:00:46 +0000419
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000420static int scan_inet_proc_line(struct inet_params *param, char *line)
421{
422 int num;
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200423 /* IPv6 /proc files use 32-char hex representation
424 * of IPv6 address, followed by :PORT_IN_HEX
425 */
426 char local_addr[33], rem_addr[33]; /* 32 + 1 for NUL */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000427
Eric Andersen31a0ece2001-10-31 11:00:46 +0000428 num = sscanf(line,
Denys Vlasenkodb9ccc52010-10-28 02:36:17 +0200429 "%*d: %32[0-9A-Fa-f]:%X "
430 "%32[0-9A-Fa-f]:%X %X "
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000431 "%lX:%lX %*X:%*X "
Denys Vlasenko327f5502013-11-29 16:45:45 +0100432 "%*X %d %*d %lu ",
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000433 local_addr, &param->local_port,
434 rem_addr, &param->rem_port, &param->state,
435 &param->txq, &param->rxq,
436 &param->uid, &param->inode);
437 if (num < 9) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000438 return 1; /* error */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000439 }
440
Eric Andersen31a0ece2001-10-31 11:00:46 +0000441 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000442#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100443 build_ipv6_addr(local_addr, &param->localaddr.sin6);
444 build_ipv6_addr(rem_addr, &param->remaddr.sin6);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000445#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000446 } else {
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100447 build_ipv4_addr(local_addr, &param->localaddr.sin);
448 build_ipv4_addr(rem_addr, &param->remaddr.sin);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000449 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000450 return 0;
451}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000452
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000453static void print_inet_line(struct inet_params *param,
454 const char *state_str, const char *proto, int is_connected)
455{
456 if ((is_connected && (flags & NETSTAT_CONNECTED))
457 || (!is_connected && (flags & NETSTAT_LISTENING))
Denis Vlasenko703e2022007-01-22 14:12:08 +0000458 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000459 char *l = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100460 &param->localaddr.sa, param->local_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000461 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000462 char *r = ip_port_str(
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100463 &param->remaddr.sa, param->rem_port,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000464 proto, flags & NETSTAT_NUMERIC);
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200465 printf(FMT_NET_CONN_DATA,
466 proto, param->rxq, param->txq,
467 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
468 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
469 state_str);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000470#if ENABLE_FEATURE_NETSTAT_PRG
471 if (option_mask32 & OPT_prg)
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000472 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000473#endif
474 bb_putchar('\n');
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000475 free(l);
476 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000477 }
478}
479
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000480static int FAST_FUNC tcp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000481{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000482 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000483
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200484 memset(&param, 0, sizeof(param));
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000485 if (scan_inet_proc_line(&param, line))
486 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000487
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000488 print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
489 return 0;
490}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000491
Denis Vlasenko703e2022007-01-22 14:12:08 +0000492#if ENABLE_FEATURE_IPV6
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100493# define NOT_NULL_ADDR(A) ( \
494 ( (A.sa.sa_family == AF_INET6) \
495 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
496 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
497 ) || ( \
498 (A.sa.sa_family == AF_INET) \
499 && A.sin.sin_addr.s_addr != 0 \
500 ) \
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000501)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000502#else
Denys Vlasenko45e97922010-02-19 08:29:32 +0100503# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000504#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000505
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000506static int FAST_FUNC udp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000507{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000508 int have_remaddr;
509 const char *state_str;
510 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000511
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200512 memset(&param, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000513 if (scan_inet_proc_line(&param, line))
514 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000515
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000516 state_str = "UNKNOWN";
517 switch (param.state) {
518 case TCP_ESTABLISHED:
519 state_str = "ESTABLISHED";
520 break;
521 case TCP_CLOSE:
522 state_str = "";
523 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000524 }
525
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100526 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000527 print_inet_line(&param, state_str, "udp", have_remaddr);
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000528 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000529}
530
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000531static int FAST_FUNC raw_do_one(char *line)
532{
533 int have_remaddr;
534 struct inet_params param;
535
536 if (scan_inet_proc_line(&param, line))
537 return 1;
538
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100539 have_remaddr = NOT_NULL_ADDR(param.remaddr);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000540 print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
541 return 0;
542}
543
544static int FAST_FUNC unix_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000545{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000546 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000547 unsigned long inode;
548 int type, state;
549 int num, path_ofs;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000550 const char *ss_proto, *ss_state, *ss_type;
551 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000552
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000553 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000554 * Other users report long lines filled by NUL bytes.
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000555 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000556 if (!line[0])
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000557 return 0;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000558
559 path_ofs = 0; /* paranoia */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000560 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
561 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
562 if (num < 6) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000563 return 1; /* error */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000564 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000565 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000566 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000567 if (!(flags & NETSTAT_LISTENING))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000568 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000569 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000570 if (!(flags & NETSTAT_CONNECTED))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000571 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000572 }
573 }
574
575 switch (proto) {
576 case 0:
577 ss_proto = "unix";
578 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000579 default:
580 ss_proto = "??";
581 }
582
583 switch (type) {
584 case SOCK_STREAM:
585 ss_type = "STREAM";
586 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000587 case SOCK_DGRAM:
588 ss_type = "DGRAM";
589 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000590 case SOCK_RAW:
591 ss_type = "RAW";
592 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000593 case SOCK_RDM:
594 ss_type = "RDM";
595 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000596 case SOCK_SEQPACKET:
597 ss_type = "SEQPACKET";
598 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000599 default:
600 ss_type = "UNKNOWN";
601 }
602
603 switch (state) {
604 case SS_FREE:
605 ss_state = "FREE";
606 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000607 case SS_UNCONNECTED:
608 /*
609 * Unconnected sockets may be listening
610 * for something.
611 */
612 if (unix_flags & SO_ACCEPTCON) {
613 ss_state = "LISTENING";
614 } else {
615 ss_state = "";
616 }
617 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000618 case SS_CONNECTING:
619 ss_state = "CONNECTING";
620 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000621 case SS_CONNECTED:
622 ss_state = "CONNECTED";
623 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000624 case SS_DISCONNECTING:
625 ss_state = "DISCONNECTING";
626 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000627 default:
628 ss_state = "UNKNOWN";
629 }
630
631 strcpy(ss_flags, "[ ");
632 if (unix_flags & SO_ACCEPTCON)
633 strcat(ss_flags, "ACC ");
634 if (unix_flags & SO_WAITDATA)
635 strcat(ss_flags, "W ");
636 if (unix_flags & SO_NOSPACE)
637 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000638 strcat(ss_flags, "]");
639
Denys Vlasenko327f5502013-11-29 16:45:45 +0100640 printf("%-5s %-6lu %-11s %-10s %-13s %6lu ",
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000641 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
642 );
643
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000644#if ENABLE_FEATURE_NETSTAT_PRG
645 if (option_mask32 & OPT_prg)
646 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
647#endif
648
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000649 /* TODO: currently we stop at first NUL byte. Is it a problem? */
650 line += path_ofs;
Ron Yorston8ec1ff32015-03-12 20:18:51 +0100651 chomp(line);
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000652 while (*line)
653 fputc_printable(*line++, stdout);
654 bb_putchar('\n');
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000655 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000656}
657
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000658static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000659{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000660 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000661 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000662 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000663
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000664 /* _stdin is just to save "r" param */
665 procinfo = fopen_or_warn_stdin(file);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000666 if (procinfo == NULL) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000667 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000668 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000669 lnr = 0;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000670 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000671 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000672 /* line 0 is skipped */
673 if (lnr && proc(buffer))
674 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
675 lnr++;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000676 free(buffer);
677 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000678 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000679}
680
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000681int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000682int netstat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000683{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000684 unsigned opt;
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000685
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000686 INIT_G();
687
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000688 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000689 opt = getopt32(argv, NETSTAT_OPTS);
Bob Dunlop55a046b2010-10-27 02:12:29 +0200690 if (opt & OPT_sock_listen) { // -l
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000691 flags &= ~NETSTAT_CONNECTED;
692 flags |= NETSTAT_LISTENING;
693 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200694 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
695 //if (opt & OPT_extended) // -e
696 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
697 //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
698 //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
699 //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
700 //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
Denis Vlasenko703e2022007-01-22 14:12:08 +0000701#if ENABLE_ROUTE
Bob Dunlop55a046b2010-10-27 02:12:29 +0200702 if (opt & OPT_route) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000703 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000704 return 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000705 }
Bob Dunlop55a046b2010-10-27 02:12:29 +0200706#endif
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200707#if ENABLE_FEATURE_NETSTAT_WIDE
708 G.addr_width = ADDR_NORMAL_WIDTH;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000709 if (opt & OPT_wide) { // -W
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200710 G.addr_width = ADDR_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000711 }
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200712#endif
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000713#if ENABLE_FEATURE_NETSTAT_PRG
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200714 progname_banner = "";
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000715 if (opt & OPT_prg) { // -p
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200716 progname_banner = PROGNAME_BANNER;
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000717 prg_cache_load();
718 }
719#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000720
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000721 opt &= NETSTAT_ALLPROTO;
722 if (opt) {
723 flags &= ~NETSTAT_ALLPROTO;
724 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000725 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000726 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200727 printf("Active Internet connections "); /* xxx */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000728
Denis Vlasenko703e2022007-01-22 14:12:08 +0000729 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000730 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000731 else if (flags & NETSTAT_LISTENING)
732 printf("(only servers)");
733 else
734 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200735 printf(FMT_NET_CONN_HEADER,
736 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
737 IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
738 progname_banner
739 );
Eric Andersen31a0ece2001-10-31 11:00:46 +0000740 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000741 if (flags & NETSTAT_TCP) {
742 do_info("/proc/net/tcp", tcp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000743#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000744 do_info("/proc/net/tcp6", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000745#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000746 }
747 if (flags & NETSTAT_UDP) {
748 do_info("/proc/net/udp", udp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000749#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000750 do_info("/proc/net/udp6", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000751#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000752 }
753 if (flags & NETSTAT_RAW) {
754 do_info("/proc/net/raw", raw_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000755#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000756 do_info("/proc/net/raw6", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000757#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000758 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000759 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000760 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000761 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000762 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000763 else if (flags & NETSTAT_LISTENING)
764 printf("(only servers)");
765 else
766 printf("(w/o servers)");
Denys Vlasenkodd88ba82010-10-27 13:10:29 +0200767 printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner);
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000768 do_info("/proc/net/unix", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000769 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000770 prg_cache_clear();
Eric Andersen31a0ece2001-10-31 11:00:46 +0000771 return 0;
772}