blob: b246280688cdeb9a7217451e4cf186e0b36e8a85 [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 *
"Robert P. J. Day"2819f752006-07-11 11:32:31 +000014 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen31a0ece2001-10-31 11:00:46 +000015 */
16
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Rob Landleyc9c1a412006-07-12 19:17:55 +000018#include "inet_common.h"
Eric Andersen31a0ece2001-10-31 11:00:46 +000019
Denis Vlasenko6e69e422008-07-27 12:10:07 +000020#define NETSTAT_OPTS "laentuwx" \
21 USE_ROUTE( "r") \
22 USE_FEATURE_NETSTAT_WIDE("W") \
23 USE_FEATURE_NETSTAT_PRG( "p")
24
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000025enum {
Denis Vlasenko6e69e422008-07-27 12:10:07 +000026 OPTBIT_KEEP_OLD = 7,
27 USE_ROUTE( OPTBIT_ROUTE,)
28 USE_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
29 USE_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
30 OPT_sock_listen = 1 << 0, // l
31 OPT_sock_all = 1 << 1, // a
32 OPT_extended = 1 << 2, // e
33 OPT_noresolve = 1 << 3, // n
34 OPT_sock_tcp = 1 << 4, // t
35 OPT_sock_udp = 1 << 5, // u
36 OPT_sock_raw = 1 << 6, // w
37 OPT_sock_unix = 1 << 7, // x
38 OPT_route = USE_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
39 OPT_wide = USE_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
40 OPT_prg = USE_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000041};
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000042
Denis Vlasenko703e2022007-01-22 14:12:08 +000043#define NETSTAT_CONNECTED 0x01
44#define NETSTAT_LISTENING 0x02
45#define NETSTAT_NUMERIC 0x04
Denis Vlasenko67b23e62006-10-03 21:00:06 +000046/* Must match getopt32 option string */
Denis Vlasenko703e2022007-01-22 14:12:08 +000047#define NETSTAT_TCP 0x10
48#define NETSTAT_UDP 0x20
49#define NETSTAT_RAW 0x40
50#define NETSTAT_UNIX 0x80
51#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
Eric Andersen31a0ece2001-10-31 11:00:46 +000052
Eric Andersen31a0ece2001-10-31 11:00:46 +000053
Eric Andersen31a0ece2001-10-31 11:00:46 +000054enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000055 TCP_ESTABLISHED = 1,
56 TCP_SYN_SENT,
57 TCP_SYN_RECV,
58 TCP_FIN_WAIT1,
59 TCP_FIN_WAIT2,
60 TCP_TIME_WAIT,
61 TCP_CLOSE,
62 TCP_CLOSE_WAIT,
63 TCP_LAST_ACK,
64 TCP_LISTEN,
Denis Vlasenko6e69e422008-07-27 12:10:07 +000065 TCP_CLOSING, /* now a valid state */
Eric Andersen31a0ece2001-10-31 11:00:46 +000066};
67
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000068static const char *const tcp_state[] = {
Denis Vlasenko703e2022007-01-22 14:12:08 +000069 "",
70 "ESTABLISHED",
71 "SYN_SENT",
72 "SYN_RECV",
73 "FIN_WAIT1",
74 "FIN_WAIT2",
75 "TIME_WAIT",
76 "CLOSE",
77 "CLOSE_WAIT",
78 "LAST_ACK",
79 "LISTEN",
80 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +000081};
82
83typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000084 SS_FREE = 0, /* not allocated */
85 SS_UNCONNECTED, /* unconnected to any socket */
86 SS_CONNECTING, /* in process of connecting */
87 SS_CONNECTED, /* connected to socket */
88 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +000089} socket_state;
90
Denis Vlasenko703e2022007-01-22 14:12:08 +000091#define SO_ACCEPTCON (1<<16) /* performed a listen */
92#define SO_WAITDATA (1<<17) /* wait data to read */
93#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +000094
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000095/* Standard printout size */
96#define PRINT_IP_MAX_SIZE 23
Denis Vlasenko6e69e422008-07-27 12:10:07 +000097#define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s"
98#define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State "
Denis Vlasenko418a7fb2007-05-15 23:57:46 +000099
100/* When there are IPv6 connections the IPv6 addresses will be
101 * truncated to none-recognition. The '-W' option makes the
102 * address columns wide enough to accomodate for longest possible
103 * IPv6 addresses, i.e. addresses of the form
104 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
105 */
106#define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000107#define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s"
108#define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State "
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000109
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000110
111#define PROGNAME_WIDTH 20
112#define PROGNAME_WIDTH_STR "20"
113/* PROGNAME_WIDTH chars: 12345678901234567890 */
114#define PROGNAME_BANNER "PID/Program name "
115
116struct prg_node {
117 struct prg_node *next;
118 long inode;
119 char name[PROGNAME_WIDTH];
120};
121
122#define PRG_HASH_SIZE 211
123
124
125struct globals {
126 const char *net_conn_line;
127 smallint flags;
128#if ENABLE_FEATURE_NETSTAT_PRG
129 smallint prg_cache_loaded;
130 struct prg_node *prg_hash[PRG_HASH_SIZE];
131#endif
132};
133#define G (*ptr_to_globals)
134#define flags (G.flags )
135#define net_conn_line (G.net_conn_line )
136#define prg_hash (G.prg_hash )
137#define prg_cache_loaded (G.prg_cache_loaded)
138#define INIT_G() do { \
139 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
140 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
141 net_conn_line = PRINT_NET_CONN; \
142} while (0)
143
144
145#if ENABLE_FEATURE_NETSTAT_PRG
146
147/* Deliberately truncating long to unsigned *int* */
148#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
149
150#define print_progname_banner() do { \
151 if (option_mask32 & OPT_prg) printf(PROGNAME_BANNER); \
152} while (0)
153
154static void prg_cache_add(long inode, char *name)
155{
156 unsigned hi = PRG_HASHIT(inode);
157 struct prg_node **pnp, *pn;
158
159 prg_cache_loaded = 2;
160 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
161 if (pn->inode == inode) {
162 /* Some warning should be appropriate here
163 as we got multiple processes for one i-node */
164 return;
165 }
166 }
167 *pnp = xzalloc(sizeof(struct prg_node));
168 pn = *pnp;
169 pn->inode = inode;
170 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
171}
172
173static const char *prg_cache_get(long inode)
174{
175 unsigned hi = PRG_HASHIT(inode);
176 struct prg_node *pn;
177
178 for (pn = prg_hash[hi]; pn; pn = pn->next)
179 if (pn->inode == inode)
180 return pn->name;
181 return "-";
182}
183
184#if ENABLE_FEATURE_CLEAN_UP
185static void prg_cache_clear(void)
186{
187 struct prg_node **pnp, *pn;
188
189 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
190 while ((pn = *pnp) != NULL) {
191 *pnp = pn->next;
192 free(pn);
193 }
194 }
195}
196#else
197#define prg_cache_clear() ((void)0)
198#endif
199
Denis Vlasenkocf8b1ef2008-07-27 14:15:42 +0000200static long extract_socket_inode(const char *lname)
201{
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000202 long inode = -1;
203
204 if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
205 /* "socket:[12345]", extract the "12345" as inode */
206 inode = bb_strtol(lname + sizeof("socket:[")-1, (char**)&lname, 0);
207 if (*lname != ']')
208 inode = -1;
209 } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
210 /* "[0000]:12345", extract the "12345" as inode */
211 inode = bb_strtol(lname + sizeof("[0000]:")-1, NULL, 0);
212 if (errno) /* not NUL terminated? */
213 inode = -1;
214 }
215
216#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
217 if (errno == ERANGE)
218 inode = -1;
219#endif
220 return inode;
221}
222
223static int FAST_FUNC file_act(const char *fileName,
224 struct stat *statbuf UNUSED_PARAM,
225 void *userData,
226 int depth UNUSED_PARAM)
227{
228 char *linkname;
229 long inode;
230
231 linkname = xmalloc_readlink(fileName);
232 if (linkname != NULL) {
233 inode = extract_socket_inode(linkname);
234 free(linkname);
235 if (inode >= 0)
236 prg_cache_add(inode, (char *)userData);
237 }
238 return TRUE;
239}
240
241static int FAST_FUNC dir_act(const char *fileName,
242 struct stat *statbuf UNUSED_PARAM,
243 void *userData UNUSED_PARAM,
244 int depth)
245{
246 const char *shortName;
247 char *p, *q;
248 char cmdline_buf[512];
249 int i;
250
251 if (depth == 0) /* "/proc" itself */
252 return TRUE; /* continue looking one level below /proc */
253
254 shortName = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
255 if (!isdigit(shortName[0])) /* skip /proc entries whic aren't processes */
256 return SKIP;
257
258 p = concat_path_file(fileName, "cmdline"); /* "/proc/PID/cmdline" */
259 i = open_read_close(p, cmdline_buf, sizeof(cmdline_buf) - 1);
260 free(p);
261 if (i < 0)
262 return FALSE;
263 cmdline_buf[i] = '\0';
264 q = concat_path_file(shortName, bb_basename(cmdline_buf)); /* "PID/argv0" */
265
266 /* go through all files in /proc/PID/fd */
267 p = concat_path_file(fileName, "fd");
268 i = recursive_action(p, ACTION_RECURSE | ACTION_QUIET,
269 file_act, NULL, (void *)q, 0);
270
271 free(p);
272 free(q);
273
274 if (!i)
275 return FALSE; /* signal permissions error to caller */
276
277 return SKIP; /* caller should not recurse further into this dir. */
278}
279
280static void prg_cache_load(void)
281{
282 int load_ok;
283
284 prg_cache_loaded = 1;
285 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
286 NULL, dir_act, NULL, 0);
287 if (load_ok)
288 return;
289
290 if (prg_cache_loaded == 1)
291 bb_error_msg("can't scan /proc - are you root?");
292 else
293 bb_error_msg("showing only processes with your user ID");
294}
295
296#else
297
298#define prg_cache_clear() ((void)0)
299#define print_progname_banner() ((void)0)
300
301#endif //ENABLE_FEATURE_NETSTAT_PRG
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000302
303
304#if ENABLE_FEATURE_IPV6
305static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
306{
307 char addr6[INET6_ADDRSTRLEN];
308 struct in6_addr in6;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000309
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000310 sscanf(local_addr, "%08X%08X%08X%08X",
311 &in6.s6_addr32[0], &in6.s6_addr32[1],
312 &in6.s6_addr32[2], &in6.s6_addr32[3]);
313 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
314 inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr->sin6_addr);
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000315
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000316 localaddr->sin6_family = AF_INET6;
317}
318#endif
319
320#if ENABLE_FEATURE_IPV6
321static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr)
322#else
323static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
324#endif
325{
326 sscanf(local_addr, "%X",
327 &((struct sockaddr_in *) localaddr)->sin_addr.s_addr);
328 ((struct sockaddr *) localaddr)->sa_family = AF_INET;
329}
330
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000331static const char *get_sname(int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000332{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000333 if (!port)
334 return "*";
335 if (!numeric) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000336 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000337 if (se)
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000338 return se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000339 }
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000340 /* hummm, we may return static buffer here!! */
341 return itoa(ntohs(port));
Eric Andersen31a0ece2001-10-31 11:00:46 +0000342}
343
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000344static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000345{
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000346 char *host, *host_port;
Eric Andersencd8c4362001-11-10 11:22:46 +0000347
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000348 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
349 * in IPv6, while "0.0.0.0" is not. */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000350
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000351 host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
352 : xmalloc_sockaddr2host_noport(addr);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000353
354 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
355 free(host);
356 return host_port;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000357}
358
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000359struct inet_params {
360 int local_port, rem_port, state, uid;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000361#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000362 struct sockaddr_in6 localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000363#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000364 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000365#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000366 unsigned long rxq, txq, inode;
367};
Eric Andersen31a0ece2001-10-31 11:00:46 +0000368
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000369static int scan_inet_proc_line(struct inet_params *param, char *line)
370{
371 int num;
372 char local_addr[64], rem_addr[64];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000373
Eric Andersen31a0ece2001-10-31 11:00:46 +0000374 num = sscanf(line,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000375 "%*d: %64[0-9A-Fa-f]:%X "
376 "%64[0-9A-Fa-f]:%X %X "
377 "%lX:%lX %*X:%*X "
378 "%*X %d %*d %ld ",
379 local_addr, &param->local_port,
380 rem_addr, &param->rem_port, &param->state,
381 &param->txq, &param->rxq,
382 &param->uid, &param->inode);
383 if (num < 9) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000384 return 1; /* error */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000385 }
386
Eric Andersen31a0ece2001-10-31 11:00:46 +0000387 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000388#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000389 build_ipv6_addr(local_addr, &param->localaddr);
390 build_ipv6_addr(rem_addr, &param->remaddr);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000391#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000392 } else {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000393 build_ipv4_addr(local_addr, &param->localaddr);
394 build_ipv4_addr(rem_addr, &param->remaddr);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000395 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000396 return 0;
397}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000398
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000399static void print_inet_line(struct inet_params *param,
400 const char *state_str, const char *proto, int is_connected)
401{
402 if ((is_connected && (flags & NETSTAT_CONNECTED))
403 || (!is_connected && (flags & NETSTAT_LISTENING))
Denis Vlasenko703e2022007-01-22 14:12:08 +0000404 ) {
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000405 char *l = ip_port_str(
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000406 (struct sockaddr *) &param->localaddr, param->local_port,
407 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000408 char *r = ip_port_str(
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000409 (struct sockaddr *) &param->remaddr, param->rem_port,
410 proto, flags & NETSTAT_NUMERIC);
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000411 printf(net_conn_line,
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000412 proto, param->rxq, param->txq, l, r, state_str);
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000413#if ENABLE_FEATURE_NETSTAT_PRG
414 if (option_mask32 & OPT_prg)
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000415 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000416#endif
417 bb_putchar('\n');
Denis Vlasenko6d9ea242007-06-19 11:12:46 +0000418 free(l);
419 free(r);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000420 }
421}
422
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000423static int FAST_FUNC tcp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000424{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000425 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000426
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000427 if (scan_inet_proc_line(&param, line))
428 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000429
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000430 print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
431 return 0;
432}
Eric Andersen31a0ece2001-10-31 11:00:46 +0000433
Denis Vlasenko703e2022007-01-22 14:12:08 +0000434#if ENABLE_FEATURE_IPV6
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000435# define notnull(A) ( \
436 ( (A.sin6_family == AF_INET6) \
437 && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
438 A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
439 ) || ( \
440 (A.sin6_family == AF_INET) \
441 && ((struct sockaddr_in*)&A)->sin_addr.s_addr \
442 ) \
443)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000444#else
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000445# define notnull(A) (A.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000446#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000447
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000448static int FAST_FUNC udp_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000449{
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000450 int have_remaddr;
451 const char *state_str;
452 struct inet_params param;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000453
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000454 if (scan_inet_proc_line(&param, line))
455 return 1;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000456
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000457 state_str = "UNKNOWN";
458 switch (param.state) {
459 case TCP_ESTABLISHED:
460 state_str = "ESTABLISHED";
461 break;
462 case TCP_CLOSE:
463 state_str = "";
464 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000465 }
466
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000467 have_remaddr = notnull(param.remaddr);
468 print_inet_line(&param, state_str, "udp", have_remaddr);
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000469 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000470}
471
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000472static int FAST_FUNC raw_do_one(char *line)
473{
474 int have_remaddr;
475 struct inet_params param;
476
477 if (scan_inet_proc_line(&param, line))
478 return 1;
479
480 have_remaddr = notnull(param.remaddr);
481 print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
482 return 0;
483}
484
485static int FAST_FUNC unix_do_one(char *line)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000486{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000487 unsigned long refcnt, proto, unix_flags;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000488 unsigned long inode;
489 int type, state;
490 int num, path_ofs;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000491 const char *ss_proto, *ss_state, *ss_type;
492 char ss_flags[32];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000493
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000494 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
Denis Vlasenko474d1c52008-01-07 19:06:47 +0000495 * Other users report long lines filled by NUL bytes.
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000496 * (those ^@ are NUL bytes too). We see them as empty lines. */
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000497 if (!line[0])
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000498 return 0;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000499
500 path_ofs = 0; /* paranoia */
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000501 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
502 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
503 if (num < 6) {
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000504 return 1; /* error */
Eric Andersen31a0ece2001-10-31 11:00:46 +0000505 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000506 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000507 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000508 if (!(flags & NETSTAT_LISTENING))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000509 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000510 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000511 if (!(flags & NETSTAT_CONNECTED))
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000512 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000513 }
514 }
515
516 switch (proto) {
517 case 0:
518 ss_proto = "unix";
519 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000520 default:
521 ss_proto = "??";
522 }
523
524 switch (type) {
525 case SOCK_STREAM:
526 ss_type = "STREAM";
527 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000528 case SOCK_DGRAM:
529 ss_type = "DGRAM";
530 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000531 case SOCK_RAW:
532 ss_type = "RAW";
533 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000534 case SOCK_RDM:
535 ss_type = "RDM";
536 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000537 case SOCK_SEQPACKET:
538 ss_type = "SEQPACKET";
539 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000540 default:
541 ss_type = "UNKNOWN";
542 }
543
544 switch (state) {
545 case SS_FREE:
546 ss_state = "FREE";
547 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000548 case SS_UNCONNECTED:
549 /*
550 * Unconnected sockets may be listening
551 * for something.
552 */
553 if (unix_flags & SO_ACCEPTCON) {
554 ss_state = "LISTENING";
555 } else {
556 ss_state = "";
557 }
558 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000559 case SS_CONNECTING:
560 ss_state = "CONNECTING";
561 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000562 case SS_CONNECTED:
563 ss_state = "CONNECTED";
564 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000565 case SS_DISCONNECTING:
566 ss_state = "DISCONNECTING";
567 break;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000568 default:
569 ss_state = "UNKNOWN";
570 }
571
572 strcpy(ss_flags, "[ ");
573 if (unix_flags & SO_ACCEPTCON)
574 strcat(ss_flags, "ACC ");
575 if (unix_flags & SO_WAITDATA)
576 strcat(ss_flags, "W ");
577 if (unix_flags & SO_NOSPACE)
578 strcat(ss_flags, "N ");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000579 strcat(ss_flags, "]");
580
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000581 printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
582 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
583 );
584
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000585#if ENABLE_FEATURE_NETSTAT_PRG
586 if (option_mask32 & OPT_prg)
587 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
588#endif
589
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000590 /* TODO: currently we stop at first NUL byte. Is it a problem? */
591 line += path_ofs;
592 *strchrnul(line, '\n') = '\0';
593 while (*line)
594 fputc_printable(*line++, stdout);
595 bb_putchar('\n');
Denis Vlasenkoddc865f2007-12-26 21:49:33 +0000596 return 0;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000597}
598
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000599static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000600{
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000601 int lnr;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000602 FILE *procinfo;
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000603 char *buffer;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000604
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000605 /* _stdin is just to save "r" param */
606 procinfo = fopen_or_warn_stdin(file);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000607 if (procinfo == NULL) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000608 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000609 }
Denis Vlasenko7221c8c2007-12-03 10:45:14 +0000610 lnr = 0;
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000611 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000612 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000613 /* line 0 is skipped */
614 if (lnr && proc(buffer))
615 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
616 lnr++;
Denis Vlasenkoabee3d02007-12-26 20:44:45 +0000617 free(buffer);
618 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000619 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000620}
621
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000622int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000623int netstat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen31a0ece2001-10-31 11:00:46 +0000624{
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000625 const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000626 unsigned opt;
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000627
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000628 INIT_G();
629
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000630 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000631 opt = getopt32(argv, NETSTAT_OPTS);
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000632 if (opt & 0x1) { // -l
633 flags &= ~NETSTAT_CONNECTED;
634 flags |= NETSTAT_LISTENING;
635 }
636 if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
637 //if (opt & 0x4) // -e
638 if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
639 //if (opt & 0x10) // -t: NETSTAT_TCP
640 //if (opt & 0x20) // -u: NETSTAT_UDP
641 //if (opt & 0x40) // -w: NETSTAT_RAW
642 //if (opt & 0x80) // -x: NETSTAT_UNIX
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000643 if (opt & OPT_route) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000644#if ENABLE_ROUTE
645 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000646 return 0;
Robert Griebl820098f2002-05-14 23:03:23 +0000647#else
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000648 bb_show_usage();
Robert Griebl820098f2002-05-14 23:03:23 +0000649#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000650 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000651 if (opt & OPT_wide) { // -W
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000652 net_conn_line = PRINT_NET_CONN_WIDE;
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000653 net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000654 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000655#if ENABLE_FEATURE_NETSTAT_PRG
656 if (opt & OPT_prg) { // -p
657 prg_cache_load();
658 }
659#endif
Denis Vlasenko418a7fb2007-05-15 23:57:46 +0000660
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000661 opt &= NETSTAT_ALLPROTO;
662 if (opt) {
663 flags &= ~NETSTAT_ALLPROTO;
664 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000665 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000666 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000667 printf("Active Internet connections "); /* xxx */
668
Denis Vlasenko703e2022007-01-22 14:12:08 +0000669 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000670 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000671 else if (flags & NETSTAT_LISTENING)
672 printf("(only servers)");
673 else
674 printf("(w/o servers)");
Denis Vlasenkoa35958d2007-05-16 22:25:35 +0000675 printf(net_conn_line_header, "Local Address", "Foreign Address");
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000676 print_progname_banner();
677 bb_putchar('\n');
Eric Andersen31a0ece2001-10-31 11:00:46 +0000678 }
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000679 if (flags & NETSTAT_TCP) {
680 do_info("/proc/net/tcp", tcp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000681#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000682 do_info("/proc/net/tcp6", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000683#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000684 }
685 if (flags & NETSTAT_UDP) {
686 do_info("/proc/net/udp", udp_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000687#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000688 do_info("/proc/net/udp6", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000689#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000690 }
691 if (flags & NETSTAT_RAW) {
692 do_info("/proc/net/raw", raw_do_one);
Denis Vlasenko703e2022007-01-22 14:12:08 +0000693#if ENABLE_FEATURE_IPV6
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000694 do_info("/proc/net/raw6", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000695#endif
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000696 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000697 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000698 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000699 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000700 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000701 else if (flags & NETSTAT_LISTENING)
702 printf("(only servers)");
703 else
704 printf("(w/o servers)");
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000705 printf("\nProto RefCnt Flags Type State I-Node ");
706 print_progname_banner();
707 printf("Path\n");
Denis Vlasenko2c7a1fd2008-07-27 17:24:19 +0000708 do_info("/proc/net/unix", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000709 }
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000710 prg_cache_clear();
Eric Andersen31a0ece2001-10-31 11:00:46 +0000711 return 0;
712}