blob: d6c10b07bb4cd2dccca3351c1b1c43d4acacf2ea [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 Vlasenkob6aae0f2007-01-29 22:51:25 +0000123static const char *get_sname(int port, const char *proto, int num)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000124{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000125 /* hummm, we return static buffer here!! */
126 const char *str = itoa(ntohs(port));
Denis Vlasenko703e2022007-01-22 14:12:08 +0000127 if (!num) {
128 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000129 if (se)
Denis Vlasenko703e2022007-01-22 14:12:08 +0000130 str = se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000131 }
132 if (!port) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000133 str = "*";
Eric Andersen31a0ece2001-10-31 11:00:46 +0000134 }
135 return str;
136}
137
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000138static void snprint_ip_port(char *ip_port, int size, struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000139{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000140 const char *port_name;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000141 int max_len;
142 int port_name_len;
Eric Andersencd8c4362001-11-10 11:22:46 +0000143
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000144// TODO: replace by xmalloc_sockaddr2host?
Denis Vlasenko703e2022007-01-22 14:12:08 +0000145#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000146 if (addr->sa_family == AF_INET6) {
147 INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000148 (numeric & NETSTAT_NUMERIC) ? 0x0fff : 0);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000149 } else
150#endif
151 {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000152 INET_rresolve(ip_port, size, (struct sockaddr_in *)addr,
153 0x4000 | ((numeric & NETSTAT_NUMERIC) ? 0x0fff : 0),
154 0xffffffff);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000155 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000156 port_name = get_sname(htons(port), proto, numeric);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000157
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000158 max_len = (option_mask32 & OPT_widedisplay)
159 ? (PRINT_IP_MAX_SIZE_WIDE - 1)
160 : (PRINT_IP_MAX_SIZE - 1);
161 port_name_len = strlen(port_name);
162 if ((strlen(ip_port) + port_name_len) > max_len)
163 ip_port[max_len - port_name_len] = '\0';
Denis Vlasenko703e2022007-01-22 14:12:08 +0000164 ip_port += strlen(ip_port);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000165 *ip_port++ = ':';
166 strcpy(ip_port, port_name);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000167}
168
169static void tcp_do_one(int lnr, const char *line)
170{
171 char local_addr[64], rem_addr[64];
Eric Andersenb0c39a82002-06-22 17:32:58 +0000172 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000173 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000174#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000175 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000176#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000177 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000178#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000179 unsigned long rxq, txq, time_len, retr, inode;
180
181 if (lnr == 0)
182 return;
183
184 more[0] = '\0';
185 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000186 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
187 &d, local_addr, &local_port,
188 rem_addr, &rem_port, &state,
189 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000190
191 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000192#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000193 build_ipv6_addr(local_addr, &localaddr);
194 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000195#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000196 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000197 build_ipv4_addr(local_addr, &localaddr);
198 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000199 }
200
201 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000202 bb_error_msg("warning, got bogus tcp line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000203 return;
204 }
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000205
Denis Vlasenko703e2022007-01-22 14:12:08 +0000206 if ((rem_port && (flags & NETSTAT_CONNECTED))
207 || (!rem_port && (flags & NETSTAT_LISTENING))
208 ) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000209 snprint_ip_port(local_addr, sizeof(local_addr),
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000210 (struct sockaddr *) &localaddr, local_port,
211 "tcp", flags & NETSTAT_NUMERIC);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000212 snprint_ip_port(rem_addr, sizeof(rem_addr),
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000213 (struct sockaddr *) &remaddr, rem_port,
214 "tcp", flags & NETSTAT_NUMERIC);
215 printf(net_conn_line,
216 "tcp", rxq, txq, local_addr, rem_addr, tcp_state[state]);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000217 }
218}
219
220static void udp_do_one(int lnr, const char *line)
221{
222 char local_addr[64], rem_addr[64];
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000223 const char *state_str;
224 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000225 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000226#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000227 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000228#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000229 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000230#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000231 unsigned long rxq, txq, time_len, retr, inode;
232
233 if (lnr == 0)
234 return;
235
236 more[0] = '\0';
237 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000238 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
239 &d, local_addr, &local_port,
240 rem_addr, &rem_port, &state,
241 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000242
243 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000244#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000245 /* Demangle what the kernel gives us */
246 build_ipv6_addr(local_addr, &localaddr);
247 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000248#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000249 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000250 build_ipv4_addr(local_addr, &localaddr);
251 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000252 }
253
254 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000255 bb_error_msg("warning, got bogus udp line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000256 return;
257 }
258 switch (state) {
259 case TCP_ESTABLISHED:
260 state_str = "ESTABLISHED";
261 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000262 case TCP_CLOSE:
263 state_str = "";
264 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000265 default:
266 state_str = "UNKNOWN";
267 break;
268 }
269
Denis Vlasenko703e2022007-01-22 14:12:08 +0000270#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000271# define notnull(A) ( \
272 ( (A.sin6_family == AF_INET6) \
273 && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
274 A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
275 ) || ( \
276 (A.sin6_family == AF_INET) \
277 && ((struct sockaddr_in*)&A)->sin_addr.s_addr \
278 ) \
279)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000280#else
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000281# define notnull(A) (A.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000282#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000283 {
284 int have_remaddr = notnull(remaddr);
285 if ((have_remaddr && (flags & NETSTAT_CONNECTED))
286 || (!have_remaddr && (flags & NETSTAT_LISTENING))
287 ) {
288 snprint_ip_port(local_addr, sizeof(local_addr),
289 (struct sockaddr *) &localaddr, local_port,
290 "udp", flags & NETSTAT_NUMERIC);
291 snprint_ip_port(rem_addr, sizeof(rem_addr),
292 (struct sockaddr *) &remaddr, rem_port,
293 "udp", flags & NETSTAT_NUMERIC);
294 printf(net_conn_line,
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000295 "udp", rxq, txq, local_addr, rem_addr, state_str);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000296 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000297 }
298}
299
300static void raw_do_one(int lnr, const char *line)
301{
302 char local_addr[64], rem_addr[64];
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000303 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000304 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000305#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000306 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000307#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000308 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000309#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000310 unsigned long rxq, txq, time_len, retr, inode;
311
312 if (lnr == 0)
313 return;
314
315 more[0] = '\0';
316 num = sscanf(line,
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000317 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
318 &d, local_addr, &local_port,
319 rem_addr, &rem_port, &state,
320 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000321
322 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000323#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000324 build_ipv6_addr(local_addr, &localaddr);
325 build_ipv6_addr(rem_addr, &remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000326#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000327 } else {
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000328 build_ipv4_addr(local_addr, &localaddr);
329 build_ipv4_addr(rem_addr, &remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000330 }
331
332 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000333 bb_error_msg("warning, got bogus raw line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000334 return;
335 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000336
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000337 {
338 int have_remaddr = notnull(remaddr);
339 if ((have_remaddr && (flags & NETSTAT_CONNECTED))
340 || (!have_remaddr && (flags & NETSTAT_LISTENING))
341 ) {
342 snprint_ip_port(local_addr, sizeof(local_addr),
343 (struct sockaddr *) &localaddr, local_port,
344 "raw", flags & NETSTAT_NUMERIC);
345 snprint_ip_port(rem_addr, sizeof(rem_addr),
346 (struct sockaddr *) &remaddr, rem_port,
347 "raw", flags & NETSTAT_NUMERIC);
348 printf(net_conn_line,
349 "raw", rxq, txq, local_addr, rem_addr, itoa(state));
350 }
Eric Andersen31a0ece2001-10-31 11:00:46 +0000351 }
352}
353
Eric Andersen31a0ece2001-10-31 11:00:46 +0000354static void unix_do_one(int nr, const char *line)
355{
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000356 static smallint has_inode = 0;
357
Eric Andersen31a0ece2001-10-31 11:00:46 +0000358 char path[PATH_MAX], ss_flags[32];
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000359 const char *ss_proto, *ss_state, *ss_type;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000360 int num, state, type, inode;
361 void *d;
362 unsigned long refcnt, proto, unix_flags;
363
364 if (nr == 0) {
365 if (strstr(line, "Inode"))
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000366 has_inode = 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000367 return;
368 }
369 path[0] = '\0';
370 num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s",
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000371 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000372 if (num < 6) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000373 bb_error_msg("warning, got bogus unix line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000374 return;
375 }
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000376 if (!has_inode)
377 sprintf(path, "%d", inode);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000378
Denis Vlasenko703e2022007-01-22 14:12:08 +0000379 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000380 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000381 if (!(flags & NETSTAT_LISTENING))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000382 return;
383 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000384 if (!(flags & NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000385 return;
386 }
387 }
388
389 switch (proto) {
390 case 0:
391 ss_proto = "unix";
392 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000393 default:
394 ss_proto = "??";
395 }
396
397 switch (type) {
398 case SOCK_STREAM:
399 ss_type = "STREAM";
400 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000401 case SOCK_DGRAM:
402 ss_type = "DGRAM";
403 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000404 case SOCK_RAW:
405 ss_type = "RAW";
406 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000407 case SOCK_RDM:
408 ss_type = "RDM";
409 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000410 case SOCK_SEQPACKET:
411 ss_type = "SEQPACKET";
412 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000413 default:
414 ss_type = "UNKNOWN";
415 }
416
417 switch (state) {
418 case SS_FREE:
419 ss_state = "FREE";
420 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000421 case SS_UNCONNECTED:
422 /*
423 * Unconnected sockets may be listening
424 * for something.
425 */
426 if (unix_flags & SO_ACCEPTCON) {
427 ss_state = "LISTENING";
428 } else {
429 ss_state = "";
430 }
431 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000432 case SS_CONNECTING:
433 ss_state = "CONNECTING";
434 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000435 case SS_CONNECTED:
436 ss_state = "CONNECTED";
437 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000438 case SS_DISCONNECTING:
439 ss_state = "DISCONNECTING";
440 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000441 default:
442 ss_state = "UNKNOWN";
443 }
444
445 strcpy(ss_flags, "[ ");
446 if (unix_flags & SO_ACCEPTCON)
447 strcat(ss_flags, "ACC ");
448 if (unix_flags & SO_WAITDATA)
449 strcat(ss_flags, "W ");
450 if (unix_flags & SO_NOSPACE)
451 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000452 strcat(ss_flags, "]");
453
454 printf("%-5s %-6ld %-11s %-10s %-13s ",
455 ss_proto, refcnt, ss_flags, ss_type, ss_state);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000456 if (has_inode)
457 printf("%-6d ", inode);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000458 else
459 printf("- ");
460 puts(path);
461}
462
463#define _PATH_PROCNET_UDP "/proc/net/udp"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000464#define _PATH_PROCNET_UDP6 "/proc/net/udp6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000465#define _PATH_PROCNET_TCP "/proc/net/tcp"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000466#define _PATH_PROCNET_TCP6 "/proc/net/tcp6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000467#define _PATH_PROCNET_RAW "/proc/net/raw"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000468#define _PATH_PROCNET_RAW6 "/proc/net/raw6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000469#define _PATH_PROCNET_UNIX "/proc/net/unix"
470
Eric Andersenb0c39a82002-06-22 17:32:58 +0000471static void do_info(const char *file, const char *name, void (*proc)(int, const char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000472{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000473 int lnr = 0;
474 FILE *procinfo;
475
Eric Andersenb0c39a82002-06-22 17:32:58 +0000476 procinfo = fopen(file, "r");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000477 if (procinfo == NULL) {
478 if (errno != ENOENT) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000479 bb_perror_msg("%s", file);
Eric Andersenb0c39a82002-06-22 17:32:58 +0000480 } else {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000481 bb_error_msg("no support for '%s' on this system", name);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000482 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000483 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000484 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000485 do {
486 char *buffer = xmalloc_fgets(procinfo);
487 if (buffer) {
488 (proc)(lnr++, buffer);
489 free(buffer);
490 }
491 } while (!feof(procinfo));
492 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000493}
494
495/*
496 * Our main function.
497 */
498
Denis Vlasenko06af2162007-02-03 17:28:39 +0000499int netstat_main(int argc, char **argv);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000500int netstat_main(int argc, char **argv)
501{
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000502 const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000503 unsigned opt;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000504#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000505 smallint inet = 1;
506 smallint inet6 = 1;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000507#else
Denis Vlasenko703e2022007-01-22 14:12:08 +0000508 enum { inet = 1, inet6 = 0 };
Eric Andersen51b8bd62002-07-03 11:46:38 +0000509#endif
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000510
511 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000512 opt = getopt32(argc, argv, NETSTAT_OPTS);
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000513 if (opt & 0x1) { // -l
514 flags &= ~NETSTAT_CONNECTED;
515 flags |= NETSTAT_LISTENING;
516 }
517 if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
518 //if (opt & 0x4) // -e
519 if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
520 //if (opt & 0x10) // -t: NETSTAT_TCP
521 //if (opt & 0x20) // -u: NETSTAT_UDP
522 //if (opt & 0x40) // -w: NETSTAT_RAW
523 //if (opt & 0x80) // -x: NETSTAT_UNIX
524 if (opt & OPT_showroute) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000525#if ENABLE_ROUTE
526 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000527 return 0;
Robert Griebl820098f2002-05-14 23:03:23 +0000528#else
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000529 bb_show_usage();
Robert Griebl820098f2002-05-14 23:03:23 +0000530#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000531 }
532
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000533 if (opt & OPT_widedisplay) { // -W
534 net_conn_line = PRINT_NET_CONN_WIDE;
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000535 net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000536 }
537
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000538 opt &= NETSTAT_ALLPROTO;
539 if (opt) {
540 flags &= ~NETSTAT_ALLPROTO;
541 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000542 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000543 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000544 printf("Active Internet connections "); /* xxx */
545
Denis Vlasenko703e2022007-01-22 14:12:08 +0000546 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000547 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000548 else if (flags & NETSTAT_LISTENING)
549 printf("(only servers)");
550 else
551 printf("(w/o servers)");
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000552 printf(net_conn_line_header, "Local Address", "Foreign Address");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000553 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000554 if (inet && flags & NETSTAT_TCP)
555 do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one);
556#if ENABLE_FEATURE_IPV6
557 if (inet6 && flags & NETSTAT_TCP)
558 do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000559#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000560 if (inet && flags & NETSTAT_UDP)
561 do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one);
562#if ENABLE_FEATURE_IPV6
563 if (inet6 && flags & NETSTAT_UDP)
564 do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000565#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000566 if (inet && flags & NETSTAT_RAW)
567 do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one);
568#if ENABLE_FEATURE_IPV6
569 if (inet6 && flags & NETSTAT_RAW)
570 do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000571#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000572 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000573 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000574 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000575 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000576 else if (flags & NETSTAT_LISTENING)
577 printf("(only servers)");
578 else
579 printf("(w/o servers)");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000580 printf("\nProto RefCnt Flags Type State I-Node Path\n");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000581 do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000582 }
583 return 0;
584}