blob: 746e25091874d87a41a7729c7fda5380c199bdf2 [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
Eric Andersenb0c39a82002-06-22 17:32:58 +000050static const char * const tcp_state[] =
Eric Andersen31a0ece2001-10-31 11:00:46 +000051{
Denis Vlasenko703e2022007-01-22 14:12:08 +000052 "",
53 "ESTABLISHED",
54 "SYN_SENT",
55 "SYN_RECV",
56 "FIN_WAIT1",
57 "FIN_WAIT2",
58 "TIME_WAIT",
59 "CLOSE",
60 "CLOSE_WAIT",
61 "LAST_ACK",
62 "LISTEN",
63 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +000064};
65
66typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000067 SS_FREE = 0, /* not allocated */
68 SS_UNCONNECTED, /* unconnected to any socket */
69 SS_CONNECTING, /* in process of connecting */
70 SS_CONNECTED, /* connected to socket */
71 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +000072} socket_state;
73
Denis Vlasenko703e2022007-01-22 14:12:08 +000074#define SO_ACCEPTCON (1<<16) /* performed a listen */
75#define SO_WAITDATA (1<<17) /* wait data to read */
76#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +000077
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000078/* Standard printout size */
79#define PRINT_IP_MAX_SIZE 23
80#define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s\n"
81#define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State\n"
82
83/* When there are IPv6 connections the IPv6 addresses will be
84 * truncated to none-recognition. The '-W' option makes the
85 * address columns wide enough to accomodate for longest possible
86 * IPv6 addresses, i.e. addresses of the form
87 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
88 */
89#define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
90#define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s\n"
91#define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State\n"
92
93static const char *net_conn_line = PRINT_NET_CONN;
94
95
96#if ENABLE_FEATURE_IPV6
97static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
98{
99 char addr6[INET6_ADDRSTRLEN];
100 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000101
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000102 sscanf(local_addr, "%08X%08X%08X%08X",
103 &in6.s6_addr32[0], &in6.s6_addr32[1],
104 &in6.s6_addr32[2], &in6.s6_addr32[3]);
105 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
106 inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000107
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000108 localaddr->sin6_family = AF_INET6;
109}
110#endif
111
112#if ENABLE_FEATURE_IPV6
113static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr)
114#else
115static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
116#endif
117{
118 sscanf(local_addr, "%X",
119 &((struct sockaddr_in *) localaddr)->sin_addr.s_addr);
120 ((struct sockaddr *) localaddr)->sa_family = AF_INET;
121}
122
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000123static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000124{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000125 if (!port)
126 return "*";
127 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000128 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000129 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000130 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000131 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000132 /* hummm, we may return static buffer here!! */
133 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000134}
135
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000136static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000137{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000138 enum { salen = USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) };
139 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000140
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000141 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6,
142 * while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000143
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000144 host = numeric ? xmalloc_sockaddr2dotted_noport(addr, salen)
145 : xmalloc_sockaddr2host_noport(addr, salen);
146
147 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
148 free(host);
149 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000150}
151
152static void tcp_do_one(int lnr, const char *line)
153{
154 char local_addr[64], rem_addr[64];
Eric Andersenb0c39a82002-06-22 17:32:58 +0000155 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000156 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000157#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000158 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000159#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000160 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000161#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000162 unsigned long rxq, txq, time_len, retr, inode;
163
164 if (lnr == 0)
165 return;
166
167 more[0] = '\0';
168 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000169 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
170 &d, local_addr, &local_port,
171 rem_addr, &rem_port, &state,
172 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000173
174 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000175#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000176 build_ipv6_addr(local_addr, &localaddr);
177 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000178#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000179 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000180 build_ipv4_addr(local_addr, &localaddr);
181 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000182 }
183
184 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000185 bb_error_msg("warning, got bogus tcp line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000186 return;
187 }
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000188
Denis Vlasenko703e2022007-01-22 14:12:08 +0000189 if ((rem_port && (flags & NETSTAT_CONNECTED))
190 || (!rem_port && (flags & NETSTAT_LISTENING))
191 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000192 char *l = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000193 (struct sockaddr *) &localaddr, local_port,
194 "tcp", flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000195 char *r = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000196 (struct sockaddr *) &remaddr, rem_port,
197 "tcp", flags & NETSTAT_NUMERIC);
198 printf(net_conn_line,
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000199 "tcp", rxq, txq, l, r, tcp_state[state]);
200 free(l);
201 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000202 }
203}
204
205static void udp_do_one(int lnr, const char *line)
206{
207 char local_addr[64], rem_addr[64];
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000208 const char *state_str;
209 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000210 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000211#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000212 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000213#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000214 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000215#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000216 unsigned long rxq, txq, time_len, retr, inode;
217
218 if (lnr == 0)
219 return;
220
221 more[0] = '\0';
222 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000223 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
224 &d, local_addr, &local_port,
225 rem_addr, &rem_port, &state,
226 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000227
228 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000229#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000230 /* Demangle what the kernel gives us */
231 build_ipv6_addr(local_addr, &localaddr);
232 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000233#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000234 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000235 build_ipv4_addr(local_addr, &localaddr);
236 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000237 }
238
239 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000240 bb_error_msg("warning, got bogus udp line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000241 return;
242 }
243 switch (state) {
244 case TCP_ESTABLISHED:
245 state_str = "ESTABLISHED";
246 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000247 case TCP_CLOSE:
248 state_str = "";
249 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000250 default:
251 state_str = "UNKNOWN";
252 break;
253 }
254
Denis Vlasenko703e2022007-01-22 14:12:08 +0000255#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000256# define notnull(A) ( \
257 ( (A.sin6_family == AF_INET6) \
258 && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
259 A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
260 ) || ( \
261 (A.sin6_family == AF_INET) \
262 && ((struct sockaddr_in*)&A)->sin_addr.s_addr \
263 ) \
264)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000265#else
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000266# define notnull(A) (A.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000267#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000268 {
269 int have_remaddr = notnull(remaddr);
270 if ((have_remaddr && (flags & NETSTAT_CONNECTED))
271 || (!have_remaddr && (flags & NETSTAT_LISTENING))
272 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000273 char *l = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000274 (struct sockaddr *) &localaddr, local_port,
275 "udp", flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000276 char *r = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000277 (struct sockaddr *) &remaddr, rem_port,
278 "udp", flags & NETSTAT_NUMERIC);
279 printf(net_conn_line,
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000280 "udp", rxq, txq, l, r, state_str);
281 free(l);
282 free(r);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000283 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000284 }
285}
286
287static void raw_do_one(int lnr, const char *line)
288{
289 char local_addr[64], rem_addr[64];
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000290 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000291 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000292#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000293 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000294#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000295 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000296#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000297 unsigned long rxq, txq, time_len, retr, inode;
298
299 if (lnr == 0)
300 return;
301
302 more[0] = '\0';
303 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000304 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
305 &d, local_addr, &local_port,
306 rem_addr, &rem_port, &state,
307 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000308
309 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000310#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000311 build_ipv6_addr(local_addr, &localaddr);
312 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000313#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000314 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000315 build_ipv4_addr(local_addr, &localaddr);
316 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000317 }
318
319 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000320 bb_error_msg("warning, got bogus raw line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000321 return;
322 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000323
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000324 {
325 int have_remaddr = notnull(remaddr);
326 if ((have_remaddr && (flags & NETSTAT_CONNECTED))
327 || (!have_remaddr && (flags & NETSTAT_LISTENING))
328 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000329 char *l = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000330 (struct sockaddr *) &localaddr, local_port,
331 "raw", flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000332 char *r = ip_port_str(
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000333 (struct sockaddr *) &remaddr, rem_port,
334 "raw", flags & NETSTAT_NUMERIC);
335 printf(net_conn_line,
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000336 "raw", rxq, txq, l, r, itoa(state));
337 free(l);
338 free(r);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000339 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000340 }
341}
342
Eric Andersen31a0ece2001-10-31 11:00:46 +0000343static void unix_do_one(int nr, const char *line)
344{
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000345 static smallint has_inode = 0;
346
Eric Andersen31a0ece2001-10-31 11:00:46 +0000347 char path[PATH_MAX], ss_flags[32];
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000348 const char *ss_proto, *ss_state, *ss_type;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000349 int num, state, type, inode;
350 void *d;
351 unsigned long refcnt, proto, unix_flags;
352
353 if (nr == 0) {
354 if (strstr(line, "Inode"))
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000355 has_inode = 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000356 return;
357 }
358 path[0] = '\0';
359 num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s",
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000360 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000361 if (num < 6) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000362 bb_error_msg("warning, got bogus unix line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000363 return;
364 }
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000365 if (!has_inode)
366 sprintf(path, "%d", inode);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000367
Denis Vlasenko703e2022007-01-22 14:12:08 +0000368 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000369 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000370 if (!(flags & NETSTAT_LISTENING))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000371 return;
372 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000373 if (!(flags & NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000374 return;
375 }
376 }
377
378 switch (proto) {
379 case 0:
380 ss_proto = "unix";
381 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000382 default:
383 ss_proto = "??";
384 }
385
386 switch (type) {
387 case SOCK_STREAM:
388 ss_type = "STREAM";
389 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000390 case SOCK_DGRAM:
391 ss_type = "DGRAM";
392 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000393 case SOCK_RAW:
394 ss_type = "RAW";
395 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000396 case SOCK_RDM:
397 ss_type = "RDM";
398 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000399 case SOCK_SEQPACKET:
400 ss_type = "SEQPACKET";
401 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000402 default:
403 ss_type = "UNKNOWN";
404 }
405
406 switch (state) {
407 case SS_FREE:
408 ss_state = "FREE";
409 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000410 case SS_UNCONNECTED:
411 /*
412 * Unconnected sockets may be listening
413 * for something.
414 */
415 if (unix_flags & SO_ACCEPTCON) {
416 ss_state = "LISTENING";
417 } else {
418 ss_state = "";
419 }
420 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000421 case SS_CONNECTING:
422 ss_state = "CONNECTING";
423 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000424 case SS_CONNECTED:
425 ss_state = "CONNECTED";
426 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000427 case SS_DISCONNECTING:
428 ss_state = "DISCONNECTING";
429 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000430 default:
431 ss_state = "UNKNOWN";
432 }
433
434 strcpy(ss_flags, "[ ");
435 if (unix_flags & SO_ACCEPTCON)
436 strcat(ss_flags, "ACC ");
437 if (unix_flags & SO_WAITDATA)
438 strcat(ss_flags, "W ");
439 if (unix_flags & SO_NOSPACE)
440 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000441 strcat(ss_flags, "]");
442
443 printf("%-5s %-6ld %-11s %-10s %-13s ",
444 ss_proto, refcnt, ss_flags, ss_type, ss_state);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000445 if (has_inode)
446 printf("%-6d ", inode);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000447 else
448 printf("- ");
449 puts(path);
450}
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
Eric Andersenb0c39a82002-06-22 17:32:58 +0000460static void do_info(const char *file, const char *name, void (*proc)(int, const char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000461{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000462 int lnr = 0;
463 FILE *procinfo;
464
Eric Andersenb0c39a82002-06-22 17:32:58 +0000465 procinfo = fopen(file, "r");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000466 if (procinfo == NULL) {
467 if (errno != ENOENT) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000468 bb_perror_msg("%s", file);
Eric Andersenb0c39a82002-06-22 17:32:58 +0000469 } else {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000470 bb_error_msg("no support for '%s' on this system", name);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000471 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000472 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000473 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000474 do {
475 char *buffer = xmalloc_fgets(procinfo);
476 if (buffer) {
477 (proc)(lnr++, buffer);
478 free(buffer);
479 }
480 } while (!feof(procinfo));
481 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000482}
483
484/*
485 * Our main function.
486 */
487
Denis Vlasenko06af2162007-02-03 17:28:39 +0000488int netstat_main(int argc, char **argv);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000489int netstat_main(int argc, char **argv)
490{
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000491 const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000492 unsigned opt;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000493#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000494 smallint inet = 1;
495 smallint inet6 = 1;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000496#else
Denis Vlasenko703e2022007-01-22 14:12:08 +0000497 enum { inet = 1, inet6 = 0 };
Eric Andersen51b8bd62002-07-03 11:46:38 +0000498#endif
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000499
500 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000501 opt = getopt32(argc, argv, NETSTAT_OPTS);
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000502 if (opt & 0x1) { // -l
503 flags &= ~NETSTAT_CONNECTED;
504 flags |= NETSTAT_LISTENING;
505 }
506 if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
507 //if (opt & 0x4) // -e
508 if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
509 //if (opt & 0x10) // -t: NETSTAT_TCP
510 //if (opt & 0x20) // -u: NETSTAT_UDP
511 //if (opt & 0x40) // -w: NETSTAT_RAW
512 //if (opt & 0x80) // -x: NETSTAT_UNIX
513 if (opt & OPT_showroute) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000514#if ENABLE_ROUTE
515 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000516 return 0;
Robert Griebl820098f2002-05-14 23:03:23 +0000517#else
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000518 bb_show_usage();
Robert Griebl820098f2002-05-14 23:03:23 +0000519#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000520 }
521
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000522 if (opt & OPT_widedisplay) { // -W
523 net_conn_line = PRINT_NET_CONN_WIDE;
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000524 net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000525 }
526
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000527 opt &= NETSTAT_ALLPROTO;
528 if (opt) {
529 flags &= ~NETSTAT_ALLPROTO;
530 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000531 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000532 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000533 printf("Active Internet connections "); /* xxx */
534
Denis Vlasenko703e2022007-01-22 14:12:08 +0000535 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000536 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000537 else if (flags & NETSTAT_LISTENING)
538 printf("(only servers)");
539 else
540 printf("(w/o servers)");
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000541 printf(net_conn_line_header, "Local Address", "Foreign Address");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000542 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000543 if (inet && flags & NETSTAT_TCP)
544 do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one);
545#if ENABLE_FEATURE_IPV6
546 if (inet6 && flags & NETSTAT_TCP)
547 do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000548#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000549 if (inet && flags & NETSTAT_UDP)
550 do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one);
551#if ENABLE_FEATURE_IPV6
552 if (inet6 && flags & NETSTAT_UDP)
553 do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000554#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000555 if (inet && flags & NETSTAT_RAW)
556 do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one);
557#if ENABLE_FEATURE_IPV6
558 if (inet6 && flags & NETSTAT_RAW)
559 do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000560#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000561 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000562 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000563 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000564 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000565 else if (flags & NETSTAT_LISTENING)
566 printf("(only servers)");
567 else
568 printf("(w/o servers)");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000569 printf("\nProto RefCnt Flags Type State I-Node Path\n");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000570 do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000571 }
572 return 0;
573}