blob: 1c78f9d19b77459ccefe6b717ebea6e1b1772890 [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 *
11 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen31a0ece2001-10-31 11:00:46 +000012 */
13
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Rob Landleyc9c1a412006-07-12 19:17:55 +000015#include "inet_common.h"
Eric Andersen31a0ece2001-10-31 11:00:46 +000016
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000017enum {
18 OPT_extended = 0x4,
19 OPT_showroute = 0x100,
20 OPT_widedisplay = 0x200 * ENABLE_FEATURE_NETSTAT_WIDE,
21};
22# define NETSTAT_OPTS "laentuwxr"USE_FEATURE_NETSTAT_WIDE("W")
23
Denis Vlasenko703e2022007-01-22 14:12:08 +000024#define NETSTAT_CONNECTED 0x01
25#define NETSTAT_LISTENING 0x02
26#define NETSTAT_NUMERIC 0x04
Denis Vlasenko67b23e62006-10-03 21:00:06 +000027/* Must match getopt32 option string */
Denis Vlasenko703e2022007-01-22 14:12:08 +000028#define NETSTAT_TCP 0x10
29#define NETSTAT_UDP 0x20
30#define NETSTAT_RAW 0x40
31#define NETSTAT_UNIX 0x80
32#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
Eric Andersen31a0ece2001-10-31 11:00:46 +000033
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000034static smallint flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO;
Eric Andersen31a0ece2001-10-31 11:00:46 +000035
Eric Andersen31a0ece2001-10-31 11:00:46 +000036enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000037 TCP_ESTABLISHED = 1,
38 TCP_SYN_SENT,
39 TCP_SYN_RECV,
40 TCP_FIN_WAIT1,
41 TCP_FIN_WAIT2,
42 TCP_TIME_WAIT,
43 TCP_CLOSE,
44 TCP_CLOSE_WAIT,
45 TCP_LAST_ACK,
46 TCP_LISTEN,
47 TCP_CLOSING /* now a valid state */
Eric Andersen31a0ece2001-10-31 11:00:46 +000048};
49
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000050static const char *const tcp_state[] = {
Denis Vlasenko703e2022007-01-22 14:12:08 +000051 "",
52 "ESTABLISHED",
53 "SYN_SENT",
54 "SYN_RECV",
55 "FIN_WAIT1",
56 "FIN_WAIT2",
57 "TIME_WAIT",
58 "CLOSE",
59 "CLOSE_WAIT",
60 "LAST_ACK",
61 "LISTEN",
62 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +000063};
64
65typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000066 SS_FREE = 0, /* not allocated */
67 SS_UNCONNECTED, /* unconnected to any socket */
68 SS_CONNECTING, /* in process of connecting */
69 SS_CONNECTED, /* connected to socket */
70 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +000071} socket_state;
72
Denis Vlasenko703e2022007-01-22 14:12:08 +000073#define SO_ACCEPTCON (1<<16) /* performed a listen */
74#define SO_WAITDATA (1<<17) /* wait data to read */
75#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +000076
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000077/* Standard printout size */
78#define PRINT_IP_MAX_SIZE 23
79#define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s\n"
80#define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State\n"
81
82/* When there are IPv6 connections the IPv6 addresses will be
83 * truncated to none-recognition. The '-W' option makes the
84 * address columns wide enough to accomodate for longest possible
85 * IPv6 addresses, i.e. addresses of the form
86 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
87 */
88#define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
89#define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s\n"
90#define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State\n"
91
92static const char *net_conn_line = PRINT_NET_CONN;
93
94
95#if ENABLE_FEATURE_IPV6
96static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
97{
98 char addr6[INET6_ADDRSTRLEN];
99 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000100
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000101 sscanf(local_addr, "%08X%08X%08X%08X",
102 &in6.s6_addr32[0], &in6.s6_addr32[1],
103 &in6.s6_addr32[2], &in6.s6_addr32[3]);
104 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
105 inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000106
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000107 localaddr->sin6_family = AF_INET6;
108}
109#endif
110
111#if ENABLE_FEATURE_IPV6
112static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr)
113#else
114static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
115#endif
116{
117 sscanf(local_addr, "%X",
118 &((struct sockaddr_in *) localaddr)->sin_addr.s_addr);
119 ((struct sockaddr *) localaddr)->sa_family = AF_INET;
120}
121
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000122static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000123{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000124 if (!port)
125 return "*";
126 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000127 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000128 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000129 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000130 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000131 /* hummm, we may return static buffer here!! */
132 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000133}
134
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000135static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000136{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000137 enum { salen = USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) };
138 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000139
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000140 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6,
141 * while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000142
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000143 host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
144 : xmalloc_sockaddr2host_noport(addr);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000145
146 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
147 free(host);
148 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000149}
150
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000151static void tcp_do_one(int lnr, char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000152{
153 char local_addr[64], rem_addr[64];
Eric Andersenb0c39a82002-06-22 17:32:58 +0000154 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000155 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000156#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000157 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000158#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000159 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000160#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000161 unsigned long rxq, txq, time_len, retr, inode;
162
163 if (lnr == 0)
164 return;
165
166 more[0] = '\0';
167 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000168 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
169 &d, local_addr, &local_port,
170 rem_addr, &rem_port, &state,
171 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000172
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000173 if (num < 10) {
174 bb_error_msg("warning, got bogus tcp line");
175 return;
176 }
177
Eric Andersen31a0ece2001-10-31 11:00:46 +0000178 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000179#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000180 build_ipv6_addr(local_addr, &localaddr);
181 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000182#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000183 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000184 build_ipv4_addr(local_addr, &localaddr);
185 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000186 }
187
Denis Vlasenko703e2022007-01-22 14:12:08 +0000188 if ((rem_port && (flags & NETSTAT_CONNECTED))
189 || (!rem_port && (flags & NETSTAT_LISTENING))
190 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000191 char *l = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000192 (struct sockaddr *) &localaddr, local_port,
193 "tcp", flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000194 char *r = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000195 (struct sockaddr *) &remaddr, rem_port,
196 "tcp", flags & NETSTAT_NUMERIC);
197 printf(net_conn_line,
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000198 "tcp", rxq, txq, l, r, tcp_state[state]);
199 free(l);
200 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000201 }
202}
203
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000204static void udp_do_one(int lnr, char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000205{
206 char local_addr[64], rem_addr[64];
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000207 const char *state_str;
208 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000209 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000210#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000211 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000212#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000213 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000214#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000215 unsigned long rxq, txq, time_len, retr, inode;
216
217 if (lnr == 0)
218 return;
219
220 more[0] = '\0';
221 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000222 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
223 &d, local_addr, &local_port,
224 rem_addr, &rem_port, &state,
225 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000226
227 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000228#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000229 /* Demangle what the kernel gives us */
230 build_ipv6_addr(local_addr, &localaddr);
231 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000232#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000233 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000234 build_ipv4_addr(local_addr, &localaddr);
235 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000236 }
237
238 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000239 bb_error_msg("warning, got bogus udp line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000240 return;
241 }
242 switch (state) {
243 case TCP_ESTABLISHED:
244 state_str = "ESTABLISHED";
245 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000246 case TCP_CLOSE:
247 state_str = "";
248 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000249 default:
250 state_str = "UNKNOWN";
251 break;
252 }
253
Denis Vlasenko703e2022007-01-22 14:12:08 +0000254#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000255# define notnull(A) ( \
256 ( (A.sin6_family == AF_INET6) \
257 && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
258 A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
259 ) || ( \
260 (A.sin6_family == AF_INET) \
261 && ((struct sockaddr_in*)&A)->sin_addr.s_addr \
262 ) \
263)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000264#else
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000265# define notnull(A) (A.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000266#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000267 {
268 int have_remaddr = notnull(remaddr);
269 if ((have_remaddr && (flags & NETSTAT_CONNECTED))
270 || (!have_remaddr && (flags & NETSTAT_LISTENING))
271 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000272 char *l = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000273 (struct sockaddr *) &localaddr, local_port,
274 "udp", flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000275 char *r = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000276 (struct sockaddr *) &remaddr, rem_port,
277 "udp", flags & NETSTAT_NUMERIC);
278 printf(net_conn_line,
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000279 "udp", rxq, txq, l, r, state_str);
280 free(l);
281 free(r);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000282 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000283 }
284}
285
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000286static void raw_do_one(int lnr, char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000287{
288 char local_addr[64], rem_addr[64];
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000289 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000290 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000291#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000292 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000293#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000294 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000295#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000296 unsigned long rxq, txq, time_len, retr, inode;
297
298 if (lnr == 0)
299 return;
300
301 more[0] = '\0';
302 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000303 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
304 &d, local_addr, &local_port,
305 rem_addr, &rem_port, &state,
306 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000307
308 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000309#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000310 build_ipv6_addr(local_addr, &localaddr);
311 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000312#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000313 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000314 build_ipv4_addr(local_addr, &localaddr);
315 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000316 }
317
318 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000319 bb_error_msg("warning, got bogus raw line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000320 return;
321 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000322
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000323 {
324 int have_remaddr = notnull(remaddr);
325 if ((have_remaddr && (flags & NETSTAT_CONNECTED))
326 || (!have_remaddr && (flags & NETSTAT_LISTENING))
327 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000328 char *l = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000329 (struct sockaddr *) &localaddr, local_port,
330 "raw", flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000331 char *r = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000332 (struct sockaddr *) &remaddr, rem_port,
333 "raw", flags & NETSTAT_NUMERIC);
334 printf(net_conn_line,
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000335 "raw", rxq, txq, l, r, itoa(state));
336 free(l);
337 free(r);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000338 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000339 }
340}
341
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000342static void unix_do_one(int nr, char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000343{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000344 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000345 unsigned long inode;
346 int type, state;
347 int num, path_ofs;
348 void *d;
349 const char *ss_proto, *ss_state, *ss_type;
350 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000351
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000352 /* TODO: currently we stop at first NUL byte. Is it a problem? */
353
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000354 if (nr == 0)
355 return; /* skip header */
356
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000357 *strchrnul(line, '\n') = '\0';
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000358
359 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000360 * Other users report long lines filled by NUL bytes.
361 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000362 if (!line[0])
363 return;
364
365 path_ofs = 0; /* paranoia */
366 num = sscanf(line, "%p: %lX %lX %lX %X %X %lu %n",
367 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
368 if (num < 7) {
369 bb_error_msg("got bogus unix line '%s'", line);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000370 return;
371 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000372 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000373 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000374 if (!(flags & NETSTAT_LISTENING))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000375 return;
376 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000377 if (!(flags & NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000378 return;
379 }
380 }
381
382 switch (proto) {
383 case 0:
384 ss_proto = "unix";
385 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000386 default:
387 ss_proto = "??";
388 }
389
390 switch (type) {
391 case SOCK_STREAM:
392 ss_type = "STREAM";
393 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000394 case SOCK_DGRAM:
395 ss_type = "DGRAM";
396 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000397 case SOCK_RAW:
398 ss_type = "RAW";
399 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000400 case SOCK_RDM:
401 ss_type = "RDM";
402 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000403 case SOCK_SEQPACKET:
404 ss_type = "SEQPACKET";
405 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000406 default:
407 ss_type = "UNKNOWN";
408 }
409
410 switch (state) {
411 case SS_FREE:
412 ss_state = "FREE";
413 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000414 case SS_UNCONNECTED:
415 /*
416 * Unconnected sockets may be listening
417 * for something.
418 */
419 if (unix_flags & SO_ACCEPTCON) {
420 ss_state = "LISTENING";
421 } else {
422 ss_state = "";
423 }
424 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000425 case SS_CONNECTING:
426 ss_state = "CONNECTING";
427 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000428 case SS_CONNECTED:
429 ss_state = "CONNECTED";
430 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000431 case SS_DISCONNECTING:
432 ss_state = "DISCONNECTING";
433 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000434 default:
435 ss_state = "UNKNOWN";
436 }
437
438 strcpy(ss_flags, "[ ");
439 if (unix_flags & SO_ACCEPTCON)
440 strcat(ss_flags, "ACC ");
441 if (unix_flags & SO_WAITDATA)
442 strcat(ss_flags, "W ");
443 if (unix_flags & SO_NOSPACE)
444 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000445 strcat(ss_flags, "]");
446
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000447 printf("%-5s %-6ld %-11s %-10s %-13s %6lu %s\n",
448 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode,
449 line + path_ofs);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000450}
451
452#define _PATH_PROCNET_UDP "/proc/net/udp"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000453#define _PATH_PROCNET_UDP6 "/proc/net/udp6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000454#define _PATH_PROCNET_TCP "/proc/net/tcp"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000455#define _PATH_PROCNET_TCP6 "/proc/net/tcp6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000456#define _PATH_PROCNET_RAW "/proc/net/raw"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000457#define _PATH_PROCNET_RAW6 "/proc/net/raw6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000458#define _PATH_PROCNET_UNIX "/proc/net/unix"
459
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000460static void do_info(const char *file, const char *name, void (*proc)(int, char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000461{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000462 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000463 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000464 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000465
Eric Andersenb0c39a82002-06-22 17:32:58 +0000466 procinfo = fopen(file, "r");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000467 if (procinfo == NULL) {
468 if (errno != ENOENT) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000469 bb_simple_perror_msg(file);
Eric Andersenb0c39a82002-06-22 17:32:58 +0000470 } else {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000471 bb_error_msg("no support for '%s' on this system", name);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000472 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000473 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000474 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000475 lnr = 0;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000476 /* Why? because xmalloc_fgets_str doesn't stop on NULs */
477 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
478 (proc)(lnr++, buffer);
479 free(buffer);
480 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000481 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000482}
483
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000484int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000485int netstat_main(int argc, char **argv)
486{
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000487 const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000488 unsigned opt;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000489#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000490 smallint inet = 1;
491 smallint inet6 = 1;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000492#else
Denis Vlasenko703e2022007-01-22 14:12:08 +0000493 enum { inet = 1, inet6 = 0 };
Eric Andersen51b8bd62002-07-03 11:46:38 +0000494#endif
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000495
496 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000497 opt = getopt32(argv, NETSTAT_OPTS);
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000498 if (opt & 0x1) { // -l
499 flags &= ~NETSTAT_CONNECTED;
500 flags |= NETSTAT_LISTENING;
501 }
502 if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
503 //if (opt & 0x4) // -e
504 if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
505 //if (opt & 0x10) // -t: NETSTAT_TCP
506 //if (opt & 0x20) // -u: NETSTAT_UDP
507 //if (opt & 0x40) // -w: NETSTAT_RAW
508 //if (opt & 0x80) // -x: NETSTAT_UNIX
509 if (opt & OPT_showroute) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000510#if ENABLE_ROUTE
511 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000512 return 0;
Robert Griebl820098f2002-05-14 23:03:23 +0000513#else
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000514 bb_show_usage();
Robert Griebl820098f2002-05-14 23:03:23 +0000515#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000516 }
517
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000518 if (opt & OPT_widedisplay) { // -W
519 net_conn_line = PRINT_NET_CONN_WIDE;
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000520 net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000521 }
522
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000523 opt &= NETSTAT_ALLPROTO;
524 if (opt) {
525 flags &= ~NETSTAT_ALLPROTO;
526 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000527 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000528 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000529 printf("Active Internet connections "); /* xxx */
530
Denis Vlasenko703e2022007-01-22 14:12:08 +0000531 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000532 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000533 else if (flags & NETSTAT_LISTENING)
534 printf("(only servers)");
535 else
536 printf("(w/o servers)");
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000537 printf(net_conn_line_header, "Local Address", "Foreign Address");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000538 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000539 if (inet && flags & NETSTAT_TCP)
540 do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one);
541#if ENABLE_FEATURE_IPV6
542 if (inet6 && flags & NETSTAT_TCP)
543 do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000544#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000545 if (inet && flags & NETSTAT_UDP)
546 do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one);
547#if ENABLE_FEATURE_IPV6
548 if (inet6 && flags & NETSTAT_UDP)
549 do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000550#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000551 if (inet && flags & NETSTAT_RAW)
552 do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one);
553#if ENABLE_FEATURE_IPV6
554 if (inet6 && flags & NETSTAT_RAW)
555 do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000556#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000557 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000558 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000559 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000560 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000561 else if (flags & NETSTAT_LISTENING)
562 printf("(only servers)");
563 else
564 printf("(w/o servers)");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000565 printf("\nProto RefCnt Flags Type State I-Node Path\n");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000566 do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000567 }
568 return 0;
569}