1 /* vi: set sw=4 ts=4: */
3 * Mini netstat implementation(s) for busybox
4 * based in part on the netstat implementation from net-tools.
6 * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com>
9 * IPV6 support added by Bart Visscher <magick@linux-fan.com>
12 * optional '-p' flag support ported from net-tools by G. Somlo <somlo@cmu.edu>
14 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
18 #include "inet_common.h"
20 //usage:#define netstat_trivial_usage
21 //usage: "[-"IF_ROUTE("r")"al] [-tuwx] [-en"IF_FEATURE_NETSTAT_WIDE("W")IF_FEATURE_NETSTAT_PRG("p")"]"
22 //usage:#define netstat_full_usage "\n\n"
23 //usage: "Display networking information\n"
26 //usage: "\n -r Routing table"
28 //usage: "\n -a All sockets"
29 //usage: "\n -l Listening sockets"
30 //usage: "\n Else: connected sockets"
31 //usage: "\n -t TCP sockets"
32 //usage: "\n -u UDP sockets"
33 //usage: "\n -w Raw sockets"
34 //usage: "\n -x Unix sockets"
35 //usage: "\n Else: all socket types"
36 //usage: "\n -e Other/more information"
37 //usage: "\n -n Don't resolve names"
38 //usage: IF_FEATURE_NETSTAT_WIDE(
39 //usage: "\n -W Wide display"
41 //usage: IF_FEATURE_NETSTAT_PRG(
42 //usage: "\n -p Show PID/program name for sockets"
45 #define NETSTAT_OPTS "laentuwx" \
47 IF_FEATURE_NETSTAT_WIDE("W") \
48 IF_FEATURE_NETSTAT_PRG( "p")
51 OPT_sock_listen = 1 << 0, // l
52 OPT_sock_all = 1 << 1, // a
53 OPT_extended = 1 << 2, // e
54 OPT_noresolve = 1 << 3, // n
55 OPT_sock_tcp = 1 << 4, // t
56 OPT_sock_udp = 1 << 5, // u
57 OPT_sock_raw = 1 << 6, // w
58 OPT_sock_unix = 1 << 7, // x
60 IF_ROUTE( OPTBIT_ROUTE,)
61 IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
62 IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
63 OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
64 OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
65 OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
68 #define NETSTAT_CONNECTED 0x01
69 #define NETSTAT_LISTENING 0x02
70 #define NETSTAT_NUMERIC 0x04
71 /* Must match getopt32 option string */
72 #define NETSTAT_TCP 0x10
73 #define NETSTAT_UDP 0x20
74 #define NETSTAT_RAW 0x40
75 #define NETSTAT_UNIX 0x80
76 #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
90 TCP_CLOSING, /* now a valid state */
93 static const char *const tcp_state[] = {
109 SS_FREE = 0, /* not allocated */
110 SS_UNCONNECTED, /* unconnected to any socket */
111 SS_CONNECTING, /* in process of connecting */
112 SS_CONNECTED, /* connected to socket */
113 SS_DISCONNECTING /* in process of disconnecting */
116 #define SO_ACCEPTCON (1<<16) /* performed a listen */
117 #define SO_WAITDATA (1<<17) /* wait data to read */
118 #define SO_NOSPACE (1<<18) /* no space to write */
120 /* Standard printout size */
121 #define PRINT_IP_MAX_SIZE 23
122 #define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s"
123 #define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State "
125 /* When there are IPv6 connections the IPv6 addresses will be
126 * truncated to none-recognition. The '-W' option makes the
127 * address columns wide enough to accomodate for longest possible
128 * IPv6 addresses, i.e. addresses of the form
129 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
131 #define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
132 #define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s"
133 #define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State "
135 #define PROGNAME_WIDTH 20
136 #define PROGNAME_WIDTH_STR "20"
137 /* PROGNAME_WIDTH chars: 12345678901234567890 */
138 #define PROGNAME_BANNER "PID/Program name "
141 struct prg_node *next;
143 char name[PROGNAME_WIDTH];
146 #define PRG_HASH_SIZE 211
149 const char *net_conn_line;
151 #if ENABLE_FEATURE_NETSTAT_PRG
152 smallint prg_cache_loaded;
153 struct prg_node *prg_hash[PRG_HASH_SIZE];
156 #define G (*ptr_to_globals)
157 #define flags (G.flags )
158 #define net_conn_line (G.net_conn_line )
159 #define prg_hash (G.prg_hash )
160 #define prg_cache_loaded (G.prg_cache_loaded)
161 #define INIT_G() do { \
162 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
163 flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
164 net_conn_line = PRINT_NET_CONN; \
168 #if ENABLE_FEATURE_NETSTAT_PRG
170 /* Deliberately truncating long to unsigned *int* */
171 #define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
173 #define print_progname_banner() do { \
174 if (option_mask32 & OPT_prg) printf(PROGNAME_BANNER); \
177 static void prg_cache_add(long inode, char *name)
179 unsigned hi = PRG_HASHIT(inode);
180 struct prg_node **pnp, *pn;
182 prg_cache_loaded = 2;
183 for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
184 if (pn->inode == inode) {
185 /* Some warning should be appropriate here
186 as we got multiple processes for one i-node */
190 *pnp = xzalloc(sizeof(struct prg_node));
193 safe_strncpy(pn->name, name, PROGNAME_WIDTH);
196 static const char *prg_cache_get(long inode)
198 unsigned hi = PRG_HASHIT(inode);
201 for (pn = prg_hash[hi]; pn; pn = pn->next)
202 if (pn->inode == inode)
207 #if ENABLE_FEATURE_CLEAN_UP
208 static void prg_cache_clear(void)
210 struct prg_node **pnp, *pn;
212 for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
213 while ((pn = *pnp) != NULL) {
220 #define prg_cache_clear() ((void)0)
223 static long extract_socket_inode(const char *lname)
227 if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
228 /* "socket:[12345]", extract the "12345" as inode */
229 inode = bb_strtol(lname + sizeof("socket:[")-1, (char**)&lname, 0);
232 } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
233 /* "[0000]:12345", extract the "12345" as inode */
234 inode = bb_strtol(lname + sizeof("[0000]:")-1, NULL, 0);
235 if (errno) /* not NUL terminated? */
239 #if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
246 static int FAST_FUNC file_act(const char *fileName,
247 struct stat *statbuf UNUSED_PARAM,
248 void *pid_slash_progname,
249 int depth UNUSED_PARAM)
254 linkname = xmalloc_readlink(fileName);
255 if (linkname != NULL) {
256 inode = extract_socket_inode(linkname);
259 prg_cache_add(inode, (char *)pid_slash_progname);
264 static int FAST_FUNC dir_act(const char *fileName,
265 struct stat *statbuf UNUSED_PARAM,
266 void *userData UNUSED_PARAM,
270 char *p, *pid_slash_progname;
271 char cmdline_buf[512];
274 if (depth == 0) /* "/proc" itself */
275 return TRUE; /* continue looking one level below /proc */
277 pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
278 if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
281 p = concat_path_file(fileName, "cmdline"); /* "/proc/PID/cmdline" */
282 i = open_read_close(p, cmdline_buf, sizeof(cmdline_buf) - 1);
286 cmdline_buf[i] = '\0';
288 /* go through all files in /proc/PID/fd */
289 pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
290 p = concat_path_file(fileName, "fd");
291 i = recursive_action(p, ACTION_RECURSE | ACTION_QUIET,
292 file_act, NULL, (void *)pid_slash_progname, 0);
294 free(pid_slash_progname);
297 return FALSE; /* signal permissions error to caller */
299 return SKIP; /* caller should not recurse further into this dir */
302 static void prg_cache_load(void)
306 prg_cache_loaded = 1;
307 load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
308 NULL, dir_act, NULL, 0);
312 if (prg_cache_loaded == 1)
313 bb_error_msg("can't scan /proc - are you root?");
315 bb_error_msg("showing only processes with your user ID");
320 #define prg_cache_clear() ((void)0)
321 #define print_progname_banner() ((void)0)
323 #endif //ENABLE_FEATURE_NETSTAT_PRG
326 #if ENABLE_FEATURE_IPV6
327 static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
329 char addr6[INET6_ADDRSTRLEN];
332 sscanf(local_addr, "%08X%08X%08X%08X",
333 &in6.s6_addr32[0], &in6.s6_addr32[1],
334 &in6.s6_addr32[2], &in6.s6_addr32[3]);
335 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
336 inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
338 localaddr->sin6_family = AF_INET6;
342 static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
344 sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
345 localaddr->sin_family = AF_INET;
348 static const char *get_sname(int port, const char *proto, int numeric)
353 struct servent *se = getservbyport(port, proto);
357 /* hummm, we may return static buffer here!! */
358 return itoa(ntohs(port));
361 static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
363 char *host, *host_port;
365 /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
366 * in IPv6, while "0.0.0.0" is not. */
368 host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
369 : xmalloc_sockaddr2host_noport(addr);
371 host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
377 int local_port, rem_port, state, uid;
380 struct sockaddr_in sin;
381 #if ENABLE_FEATURE_IPV6
382 struct sockaddr_in6 sin6;
384 } localaddr, remaddr;
385 unsigned long rxq, txq, inode;
388 static int scan_inet_proc_line(struct inet_params *param, char *line)
391 char local_addr[64], rem_addr[64];
394 "%*d: %64[0-9A-Fa-f]:%X "
395 "%64[0-9A-Fa-f]:%X %X "
398 local_addr, ¶m->local_port,
399 rem_addr, ¶m->rem_port, ¶m->state,
400 ¶m->txq, ¶m->rxq,
401 ¶m->uid, ¶m->inode);
403 return 1; /* error */
406 if (strlen(local_addr) > 8) {
407 #if ENABLE_FEATURE_IPV6
408 build_ipv6_addr(local_addr, ¶m->localaddr.sin6);
409 build_ipv6_addr(rem_addr, ¶m->remaddr.sin6);
412 build_ipv4_addr(local_addr, ¶m->localaddr.sin);
413 build_ipv4_addr(rem_addr, ¶m->remaddr.sin);
418 static void print_inet_line(struct inet_params *param,
419 const char *state_str, const char *proto, int is_connected)
421 if ((is_connected && (flags & NETSTAT_CONNECTED))
422 || (!is_connected && (flags & NETSTAT_LISTENING))
424 char *l = ip_port_str(
425 ¶m->localaddr.sa, param->local_port,
426 proto, flags & NETSTAT_NUMERIC);
427 char *r = ip_port_str(
428 ¶m->remaddr.sa, param->rem_port,
429 proto, flags & NETSTAT_NUMERIC);
430 printf(net_conn_line,
431 proto, param->rxq, param->txq, l, r, state_str);
432 #if ENABLE_FEATURE_NETSTAT_PRG
433 if (option_mask32 & OPT_prg)
434 printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
442 static int FAST_FUNC tcp_do_one(char *line)
444 struct inet_params param;
446 if (scan_inet_proc_line(¶m, line))
449 print_inet_line(¶m, tcp_state[param.state], "tcp", param.rem_port);
453 #if ENABLE_FEATURE_IPV6
454 # define NOT_NULL_ADDR(A) ( \
455 ( (A.sa.sa_family == AF_INET6) \
456 && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
457 A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
459 (A.sa.sa_family == AF_INET) \
460 && A.sin.sin_addr.s_addr != 0 \
464 # define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
467 static int FAST_FUNC udp_do_one(char *line)
470 const char *state_str;
471 struct inet_params param;
473 if (scan_inet_proc_line(¶m, line))
476 state_str = "UNKNOWN";
477 switch (param.state) {
478 case TCP_ESTABLISHED:
479 state_str = "ESTABLISHED";
486 have_remaddr = NOT_NULL_ADDR(param.remaddr);
487 print_inet_line(¶m, state_str, "udp", have_remaddr);
491 static int FAST_FUNC raw_do_one(char *line)
494 struct inet_params param;
496 if (scan_inet_proc_line(¶m, line))
499 have_remaddr = NOT_NULL_ADDR(param.remaddr);
500 print_inet_line(¶m, itoa(param.state), "raw", have_remaddr);
504 static int FAST_FUNC unix_do_one(char *line)
506 unsigned long refcnt, proto, unix_flags;
510 const char *ss_proto, *ss_state, *ss_type;
513 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
514 * Other users report long lines filled by NUL bytes.
515 * (those ^@ are NUL bytes too). We see them as empty lines. */
519 path_ofs = 0; /* paranoia */
520 num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
521 &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
523 return 1; /* error */
525 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
526 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
527 if (!(flags & NETSTAT_LISTENING))
530 if (!(flags & NETSTAT_CONNECTED))
557 ss_type = "SEQPACKET";
569 * Unconnected sockets may be listening
572 if (unix_flags & SO_ACCEPTCON) {
573 ss_state = "LISTENING";
579 ss_state = "CONNECTING";
582 ss_state = "CONNECTED";
584 case SS_DISCONNECTING:
585 ss_state = "DISCONNECTING";
588 ss_state = "UNKNOWN";
591 strcpy(ss_flags, "[ ");
592 if (unix_flags & SO_ACCEPTCON)
593 strcat(ss_flags, "ACC ");
594 if (unix_flags & SO_WAITDATA)
595 strcat(ss_flags, "W ");
596 if (unix_flags & SO_NOSPACE)
597 strcat(ss_flags, "N ");
598 strcat(ss_flags, "]");
600 printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
601 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
604 #if ENABLE_FEATURE_NETSTAT_PRG
605 if (option_mask32 & OPT_prg)
606 printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
609 /* TODO: currently we stop at first NUL byte. Is it a problem? */
611 *strchrnul(line, '\n') = '\0';
613 fputc_printable(*line++, stdout);
618 static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
624 /* _stdin is just to save "r" param */
625 procinfo = fopen_or_warn_stdin(file);
626 if (procinfo == NULL) {
630 /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
631 while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
632 /* line 0 is skipped */
633 if (lnr && proc(buffer))
634 bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
641 int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
642 int netstat_main(int argc UNUSED_PARAM, char **argv)
644 const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
649 /* Option string must match NETSTAT_xxx constants */
650 opt = getopt32(argv, NETSTAT_OPTS);
651 if (opt & OPT_sock_listen) { // -l
652 flags &= ~NETSTAT_CONNECTED;
653 flags |= NETSTAT_LISTENING;
655 if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
656 //if (opt & OPT_extended) // -e
657 if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
658 //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
659 //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
660 //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
661 //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
663 if (opt & OPT_route) { // -r
664 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
668 if (opt & OPT_wide) { // -W
669 net_conn_line = PRINT_NET_CONN_WIDE;
670 net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
672 #if ENABLE_FEATURE_NETSTAT_PRG
673 if (opt & OPT_prg) { // -p
678 opt &= NETSTAT_ALLPROTO;
680 flags &= ~NETSTAT_ALLPROTO;
683 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
684 printf("Active Internet connections "); /* xxx */
686 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
687 printf("(servers and established)");
688 else if (flags & NETSTAT_LISTENING)
689 printf("(only servers)");
691 printf("(w/o servers)");
692 printf(net_conn_line_header, "Local Address", "Foreign Address");
693 print_progname_banner();
696 if (flags & NETSTAT_TCP) {
697 do_info("/proc/net/tcp", tcp_do_one);
698 #if ENABLE_FEATURE_IPV6
699 do_info("/proc/net/tcp6", tcp_do_one);
702 if (flags & NETSTAT_UDP) {
703 do_info("/proc/net/udp", udp_do_one);
704 #if ENABLE_FEATURE_IPV6
705 do_info("/proc/net/udp6", udp_do_one);
708 if (flags & NETSTAT_RAW) {
709 do_info("/proc/net/raw", raw_do_one);
710 #if ENABLE_FEATURE_IPV6
711 do_info("/proc/net/raw6", raw_do_one);
714 if (flags & NETSTAT_UNIX) {
715 printf("Active UNIX domain sockets ");
716 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
717 printf("(servers and established)");
718 else if (flags & NETSTAT_LISTENING)
719 printf("(only servers)");
721 printf("(w/o servers)");
722 printf("\nProto RefCnt Flags Type State I-Node ");
723 print_progname_banner();
725 do_info("/proc/net/unix", unix_do_one);