Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 1 | /* 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 Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 6 | * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com> |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 8 | * 2002-04-20 |
| 9 | * IPV6 support added by Bart Visscher <magick@linux-fan.com> |
"Robert P. J. Day" | 2819f75 | 2006-07-11 11:32:31 +0000 | [diff] [blame] | 10 | * |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 11 | * 2008-07-10 |
| 12 | * optional '-p' flag support ported from net-tools by G. Somlo <somlo@cmu.edu> |
| 13 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 14 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 18 | #include "inet_common.h" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 19 | |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 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" |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 24 | //usage: IF_ROUTE( |
| 25 | //usage: "\n -r Routing table" |
| 26 | //usage: ) |
| 27 | //usage: "\n -a All sockets" |
| 28 | //usage: "\n -l Listening sockets" |
| 29 | //usage: "\n Else: connected sockets" |
| 30 | //usage: "\n -t TCP sockets" |
| 31 | //usage: "\n -u UDP sockets" |
| 32 | //usage: "\n -w Raw sockets" |
| 33 | //usage: "\n -x Unix sockets" |
| 34 | //usage: "\n Else: all socket types" |
| 35 | //usage: "\n -e Other/more information" |
| 36 | //usage: "\n -n Don't resolve names" |
| 37 | //usage: IF_FEATURE_NETSTAT_WIDE( |
| 38 | //usage: "\n -W Wide display" |
| 39 | //usage: ) |
| 40 | //usage: IF_FEATURE_NETSTAT_PRG( |
| 41 | //usage: "\n -p Show PID/program name for sockets" |
| 42 | //usage: ) |
| 43 | |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 44 | #define NETSTAT_OPTS "laentuwx" \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 45 | IF_ROUTE( "r") \ |
| 46 | IF_FEATURE_NETSTAT_WIDE("W") \ |
| 47 | IF_FEATURE_NETSTAT_PRG( "p") |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 49 | enum { |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 50 | OPT_sock_listen = 1 << 0, // l |
| 51 | OPT_sock_all = 1 << 1, // a |
| 52 | OPT_extended = 1 << 2, // e |
| 53 | OPT_noresolve = 1 << 3, // n |
| 54 | OPT_sock_tcp = 1 << 4, // t |
| 55 | OPT_sock_udp = 1 << 5, // u |
| 56 | OPT_sock_raw = 1 << 6, // w |
| 57 | OPT_sock_unix = 1 << 7, // x |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 58 | OPTBIT_x = 7, |
| 59 | IF_ROUTE( OPTBIT_ROUTE,) |
| 60 | IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,) |
| 61 | IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 62 | OPT_route = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r |
| 63 | OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W |
| 64 | OPT_prg = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 65 | }; |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 67 | #define NETSTAT_CONNECTED 0x01 |
| 68 | #define NETSTAT_LISTENING 0x02 |
| 69 | #define NETSTAT_NUMERIC 0x04 |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 70 | /* Must match getopt32 option string */ |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 71 | #define NETSTAT_TCP 0x10 |
| 72 | #define NETSTAT_UDP 0x20 |
| 73 | #define NETSTAT_RAW 0x40 |
| 74 | #define NETSTAT_UNIX 0x80 |
| 75 | #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 76 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 77 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 78 | enum { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 79 | TCP_ESTABLISHED = 1, |
| 80 | TCP_SYN_SENT, |
| 81 | TCP_SYN_RECV, |
| 82 | TCP_FIN_WAIT1, |
| 83 | TCP_FIN_WAIT2, |
| 84 | TCP_TIME_WAIT, |
| 85 | TCP_CLOSE, |
| 86 | TCP_CLOSE_WAIT, |
| 87 | TCP_LAST_ACK, |
| 88 | TCP_LISTEN, |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 89 | TCP_CLOSING, /* now a valid state */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 92 | static const char *const tcp_state[] = { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 93 | "", |
| 94 | "ESTABLISHED", |
| 95 | "SYN_SENT", |
| 96 | "SYN_RECV", |
| 97 | "FIN_WAIT1", |
| 98 | "FIN_WAIT2", |
| 99 | "TIME_WAIT", |
| 100 | "CLOSE", |
| 101 | "CLOSE_WAIT", |
| 102 | "LAST_ACK", |
| 103 | "LISTEN", |
| 104 | "CLOSING" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | typedef enum { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 108 | SS_FREE = 0, /* not allocated */ |
| 109 | SS_UNCONNECTED, /* unconnected to any socket */ |
| 110 | SS_CONNECTING, /* in process of connecting */ |
| 111 | SS_CONNECTED, /* connected to socket */ |
| 112 | SS_DISCONNECTING /* in process of disconnecting */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 113 | } socket_state; |
| 114 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 115 | #define SO_ACCEPTCON (1<<16) /* performed a listen */ |
| 116 | #define SO_WAITDATA (1<<17) /* wait data to read */ |
| 117 | #define SO_NOSPACE (1<<18) /* no space to write */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 118 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 119 | #define ADDR_NORMAL_WIDTH 23 |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 120 | /* When there are IPv6 connections the IPv6 addresses will be |
| 121 | * truncated to none-recognition. The '-W' option makes the |
| 122 | * address columns wide enough to accomodate for longest possible |
| 123 | * IPv6 addresses, i.e. addresses of the form |
| 124 | * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd |
| 125 | */ |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 126 | #define ADDR_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */ |
| 127 | #if ENABLE_FEATURE_NETSTAT_WIDE |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 128 | # define FMT_NET_CONN_DATA "%s %6lu %6lu %-*s %-*s %-12s" |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 129 | # define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-*s %-*s State %s\n" |
| 130 | #else |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 131 | # define FMT_NET_CONN_DATA "%s %6lu %6lu %-23s %-23s %-12s" |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 132 | # define FMT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State %s\n" |
| 133 | #endif |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 134 | |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 135 | #define PROGNAME_WIDTH 20 |
| 136 | #define PROGNAME_WIDTH_STR "20" |
| 137 | /* PROGNAME_WIDTH chars: 12345678901234567890 */ |
| 138 | #define PROGNAME_BANNER "PID/Program name " |
| 139 | |
| 140 | struct prg_node { |
| 141 | struct prg_node *next; |
| 142 | long inode; |
| 143 | char name[PROGNAME_WIDTH]; |
| 144 | }; |
| 145 | |
| 146 | #define PRG_HASH_SIZE 211 |
| 147 | |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 148 | struct globals { |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 149 | smallint flags; |
| 150 | #if ENABLE_FEATURE_NETSTAT_PRG |
| 151 | smallint prg_cache_loaded; |
| 152 | struct prg_node *prg_hash[PRG_HASH_SIZE]; |
| 153 | #endif |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 154 | #if ENABLE_FEATURE_NETSTAT_PRG |
| 155 | const char *progname_banner; |
| 156 | #endif |
| 157 | #if ENABLE_FEATURE_NETSTAT_WIDE |
| 158 | unsigned addr_width; |
| 159 | #endif |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 160 | }; |
| 161 | #define G (*ptr_to_globals) |
| 162 | #define flags (G.flags ) |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 163 | #define prg_cache_loaded (G.prg_cache_loaded) |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 164 | #define prg_hash (G.prg_hash ) |
| 165 | #if ENABLE_FEATURE_NETSTAT_PRG |
| 166 | # define progname_banner (G.progname_banner ) |
| 167 | #else |
| 168 | # define progname_banner "" |
| 169 | #endif |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 170 | #define INIT_G() do { \ |
| 171 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 172 | flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \ |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 173 | } while (0) |
| 174 | |
| 175 | |
| 176 | #if ENABLE_FEATURE_NETSTAT_PRG |
| 177 | |
| 178 | /* Deliberately truncating long to unsigned *int* */ |
| 179 | #define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE) |
| 180 | |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 181 | static void prg_cache_add(long inode, char *name) |
| 182 | { |
| 183 | unsigned hi = PRG_HASHIT(inode); |
| 184 | struct prg_node **pnp, *pn; |
| 185 | |
| 186 | prg_cache_loaded = 2; |
| 187 | for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) { |
| 188 | if (pn->inode == inode) { |
| 189 | /* Some warning should be appropriate here |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 190 | * as we got multiple processes for one i-node */ |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | } |
| 194 | *pnp = xzalloc(sizeof(struct prg_node)); |
| 195 | pn = *pnp; |
| 196 | pn->inode = inode; |
| 197 | safe_strncpy(pn->name, name, PROGNAME_WIDTH); |
| 198 | } |
| 199 | |
| 200 | static const char *prg_cache_get(long inode) |
| 201 | { |
| 202 | unsigned hi = PRG_HASHIT(inode); |
| 203 | struct prg_node *pn; |
| 204 | |
| 205 | for (pn = prg_hash[hi]; pn; pn = pn->next) |
| 206 | if (pn->inode == inode) |
| 207 | return pn->name; |
| 208 | return "-"; |
| 209 | } |
| 210 | |
| 211 | #if ENABLE_FEATURE_CLEAN_UP |
| 212 | static void prg_cache_clear(void) |
| 213 | { |
| 214 | struct prg_node **pnp, *pn; |
| 215 | |
| 216 | for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) { |
| 217 | while ((pn = *pnp) != NULL) { |
| 218 | *pnp = pn->next; |
| 219 | free(pn); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | #else |
| 224 | #define prg_cache_clear() ((void)0) |
| 225 | #endif |
| 226 | |
Denis Vlasenko | cf8b1ef | 2008-07-27 14:15:42 +0000 | [diff] [blame] | 227 | static long extract_socket_inode(const char *lname) |
| 228 | { |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 229 | long inode = -1; |
| 230 | |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame^] | 231 | if (is_prefixed_with(lname, "socket:[")) { |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 232 | /* "socket:[12345]", extract the "12345" as inode */ |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 233 | inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0); |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 234 | if (*lname != ']') |
| 235 | inode = -1; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame^] | 236 | } else if (is_prefixed_with(lname, "[0000]:")) { |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 237 | /* "[0000]:12345", extract the "12345" as inode */ |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 238 | inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0); |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 239 | if (errno) /* not NUL terminated? */ |
| 240 | inode = -1; |
| 241 | } |
| 242 | |
| 243 | #if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */ |
| 244 | if (errno == ERANGE) |
| 245 | inode = -1; |
| 246 | #endif |
| 247 | return inode; |
| 248 | } |
| 249 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 250 | static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName, |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 251 | struct stat *statbuf UNUSED_PARAM, |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 252 | void *pid_slash_progname, |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 253 | int depth UNUSED_PARAM) |
| 254 | { |
| 255 | char *linkname; |
| 256 | long inode; |
| 257 | |
| 258 | linkname = xmalloc_readlink(fileName); |
| 259 | if (linkname != NULL) { |
| 260 | inode = extract_socket_inode(linkname); |
| 261 | free(linkname); |
| 262 | if (inode >= 0) |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 263 | prg_cache_add(inode, (char *)pid_slash_progname); |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 264 | } |
| 265 | return TRUE; |
| 266 | } |
| 267 | |
| 268 | static int FAST_FUNC dir_act(const char *fileName, |
| 269 | struct stat *statbuf UNUSED_PARAM, |
| 270 | void *userData UNUSED_PARAM, |
| 271 | int depth) |
| 272 | { |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 273 | const char *pid; |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 274 | char *pid_slash_progname; |
| 275 | char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3]; |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 276 | char cmdline_buf[512]; |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 277 | int n, len; |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 278 | |
| 279 | if (depth == 0) /* "/proc" itself */ |
| 280 | return TRUE; /* continue looking one level below /proc */ |
| 281 | |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 282 | pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */ |
| 283 | if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */ |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 284 | return SKIP; |
| 285 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 286 | len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName); |
| 287 | n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1); |
| 288 | if (n < 0) |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 289 | return FALSE; |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 290 | cmdline_buf[n] = '\0'; |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 291 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 292 | /* go through all files in /proc/PID/fd and check whether they are sockets */ |
| 293 | strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd"); |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 294 | pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */ |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 295 | n = recursive_action(proc_pid_fname, |
| 296 | ACTION_RECURSE | ACTION_QUIET, |
| 297 | add_to_prg_cache_if_socket, |
| 298 | NULL, |
| 299 | (void *)pid_slash_progname, |
| 300 | 0); |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 301 | free(pid_slash_progname); |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 302 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 303 | if (!n) |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 304 | return FALSE; /* signal permissions error to caller */ |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 305 | |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 306 | return SKIP; /* caller should not recurse further into this dir */ |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static void prg_cache_load(void) |
| 310 | { |
| 311 | int load_ok; |
| 312 | |
| 313 | prg_cache_loaded = 1; |
| 314 | load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET, |
| 315 | NULL, dir_act, NULL, 0); |
| 316 | if (load_ok) |
| 317 | return; |
| 318 | |
| 319 | if (prg_cache_loaded == 1) |
| 320 | bb_error_msg("can't scan /proc - are you root?"); |
| 321 | else |
| 322 | bb_error_msg("showing only processes with your user ID"); |
| 323 | } |
| 324 | |
| 325 | #else |
| 326 | |
| 327 | #define prg_cache_clear() ((void)0) |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 328 | |
| 329 | #endif //ENABLE_FEATURE_NETSTAT_PRG |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 330 | |
| 331 | |
| 332 | #if ENABLE_FEATURE_IPV6 |
| 333 | static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr) |
| 334 | { |
| 335 | char addr6[INET6_ADDRSTRLEN]; |
| 336 | struct in6_addr in6; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 338 | sscanf(local_addr, "%08X%08X%08X%08X", |
| 339 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 340 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 341 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 342 | inet_pton(AF_INET6, addr6, &localaddr->sin6_addr); |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 344 | localaddr->sin6_family = AF_INET6; |
| 345 | } |
| 346 | #endif |
| 347 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 348 | static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr) |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 349 | { |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 350 | sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr); |
| 351 | localaddr->sin_family = AF_INET; |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 354 | static const char *get_sname(int port, const char *proto, int numeric) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 355 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 356 | if (!port) |
| 357 | return "*"; |
| 358 | if (!numeric) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 359 | struct servent *se = getservbyport(port, proto); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 360 | if (se) |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 361 | return se->s_name; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 362 | } |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 363 | /* hummm, we may return static buffer here!! */ |
| 364 | return itoa(ntohs(port)); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 367 | static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 368 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 369 | char *host, *host_port; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 370 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 371 | /* Code which used "*" for INADDR_ANY is removed: it's ambiguous |
| 372 | * in IPv6, while "0.0.0.0" is not. */ |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 373 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 374 | host = numeric ? xmalloc_sockaddr2dotted_noport(addr) |
| 375 | : xmalloc_sockaddr2host_noport(addr); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 376 | |
| 377 | host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric)); |
| 378 | free(host); |
| 379 | return host_port; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 382 | struct inet_params { |
| 383 | int local_port, rem_port, state, uid; |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 384 | union { |
| 385 | struct sockaddr sa; |
| 386 | struct sockaddr_in sin; |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 387 | #if ENABLE_FEATURE_IPV6 |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 388 | struct sockaddr_in6 sin6; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 389 | #endif |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 390 | } localaddr, remaddr; |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 391 | unsigned long rxq, txq, inode; |
| 392 | }; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 393 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 394 | static int scan_inet_proc_line(struct inet_params *param, char *line) |
| 395 | { |
| 396 | int num; |
Denys Vlasenko | db9ccc5 | 2010-10-28 02:36:17 +0200 | [diff] [blame] | 397 | /* IPv6 /proc files use 32-char hex representation |
| 398 | * of IPv6 address, followed by :PORT_IN_HEX |
| 399 | */ |
| 400 | char local_addr[33], rem_addr[33]; /* 32 + 1 for NUL */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 401 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 402 | num = sscanf(line, |
Denys Vlasenko | db9ccc5 | 2010-10-28 02:36:17 +0200 | [diff] [blame] | 403 | "%*d: %32[0-9A-Fa-f]:%X " |
| 404 | "%32[0-9A-Fa-f]:%X %X " |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 405 | "%lX:%lX %*X:%*X " |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 406 | "%*X %d %*d %lu ", |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 407 | local_addr, ¶m->local_port, |
| 408 | rem_addr, ¶m->rem_port, ¶m->state, |
| 409 | ¶m->txq, ¶m->rxq, |
| 410 | ¶m->uid, ¶m->inode); |
| 411 | if (num < 9) { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame] | 412 | return 1; /* error */ |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 415 | if (strlen(local_addr) > 8) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 416 | #if ENABLE_FEATURE_IPV6 |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 417 | build_ipv6_addr(local_addr, ¶m->localaddr.sin6); |
| 418 | build_ipv6_addr(rem_addr, ¶m->remaddr.sin6); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 419 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 420 | } else { |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 421 | build_ipv4_addr(local_addr, ¶m->localaddr.sin); |
| 422 | build_ipv4_addr(rem_addr, ¶m->remaddr.sin); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 423 | } |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 424 | return 0; |
| 425 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 426 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 427 | static void print_inet_line(struct inet_params *param, |
| 428 | const char *state_str, const char *proto, int is_connected) |
| 429 | { |
| 430 | if ((is_connected && (flags & NETSTAT_CONNECTED)) |
| 431 | || (!is_connected && (flags & NETSTAT_LISTENING)) |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 432 | ) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 433 | char *l = ip_port_str( |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 434 | ¶m->localaddr.sa, param->local_port, |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 435 | proto, flags & NETSTAT_NUMERIC); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 436 | char *r = ip_port_str( |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 437 | ¶m->remaddr.sa, param->rem_port, |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 438 | proto, flags & NETSTAT_NUMERIC); |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 439 | printf(FMT_NET_CONN_DATA, |
| 440 | proto, param->rxq, param->txq, |
| 441 | IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l, |
| 442 | IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r, |
| 443 | state_str); |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 444 | #if ENABLE_FEATURE_NETSTAT_PRG |
| 445 | if (option_mask32 & OPT_prg) |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 446 | printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode)); |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 447 | #endif |
| 448 | bb_putchar('\n'); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 449 | free(l); |
| 450 | free(r); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 454 | static int FAST_FUNC tcp_do_one(char *line) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 455 | { |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 456 | struct inet_params param; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 457 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 458 | memset(¶m, 0, sizeof(param)); |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 459 | if (scan_inet_proc_line(¶m, line)) |
| 460 | return 1; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 461 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 462 | print_inet_line(¶m, tcp_state[param.state], "tcp", param.rem_port); |
| 463 | return 0; |
| 464 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 465 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 466 | #if ENABLE_FEATURE_IPV6 |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 467 | # define NOT_NULL_ADDR(A) ( \ |
| 468 | ( (A.sa.sa_family == AF_INET6) \ |
| 469 | && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \ |
| 470 | A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \ |
| 471 | ) || ( \ |
| 472 | (A.sa.sa_family == AF_INET) \ |
| 473 | && A.sin.sin_addr.s_addr != 0 \ |
| 474 | ) \ |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 475 | ) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 476 | #else |
Denys Vlasenko | 45e9792 | 2010-02-19 08:29:32 +0100 | [diff] [blame] | 477 | # define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 478 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 479 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 480 | static int FAST_FUNC udp_do_one(char *line) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 481 | { |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 482 | int have_remaddr; |
| 483 | const char *state_str; |
| 484 | struct inet_params param; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 485 | |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 486 | memset(¶m, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */ |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 487 | if (scan_inet_proc_line(¶m, line)) |
| 488 | return 1; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 489 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 490 | state_str = "UNKNOWN"; |
| 491 | switch (param.state) { |
| 492 | case TCP_ESTABLISHED: |
| 493 | state_str = "ESTABLISHED"; |
| 494 | break; |
| 495 | case TCP_CLOSE: |
| 496 | state_str = ""; |
| 497 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 500 | have_remaddr = NOT_NULL_ADDR(param.remaddr); |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 501 | print_inet_line(¶m, state_str, "udp", have_remaddr); |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame] | 502 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 505 | static int FAST_FUNC raw_do_one(char *line) |
| 506 | { |
| 507 | int have_remaddr; |
| 508 | struct inet_params param; |
| 509 | |
| 510 | if (scan_inet_proc_line(¶m, line)) |
| 511 | return 1; |
| 512 | |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 513 | have_remaddr = NOT_NULL_ADDR(param.remaddr); |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 514 | print_inet_line(¶m, itoa(param.state), "raw", have_remaddr); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int FAST_FUNC unix_do_one(char *line) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 519 | { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 520 | unsigned long refcnt, proto, unix_flags; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 521 | unsigned long inode; |
| 522 | int type, state; |
| 523 | int num, path_ofs; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 524 | const char *ss_proto, *ss_state, *ss_type; |
| 525 | char ss_flags[32]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 526 | |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 527 | /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..." |
Denis Vlasenko | 474d1c5 | 2008-01-07 19:06:47 +0000 | [diff] [blame] | 528 | * Other users report long lines filled by NUL bytes. |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 529 | * (those ^@ are NUL bytes too). We see them as empty lines. */ |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 530 | if (!line[0]) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame] | 531 | return 0; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 532 | |
| 533 | path_ofs = 0; /* paranoia */ |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 534 | num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n", |
| 535 | &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs); |
| 536 | if (num < 6) { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame] | 537 | return 1; /* error */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 538 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 539 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 540 | if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 541 | if (!(flags & NETSTAT_LISTENING)) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame] | 542 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 543 | } else { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 544 | if (!(flags & NETSTAT_CONNECTED)) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame] | 545 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | switch (proto) { |
| 550 | case 0: |
| 551 | ss_proto = "unix"; |
| 552 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 553 | default: |
| 554 | ss_proto = "??"; |
| 555 | } |
| 556 | |
| 557 | switch (type) { |
| 558 | case SOCK_STREAM: |
| 559 | ss_type = "STREAM"; |
| 560 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 561 | case SOCK_DGRAM: |
| 562 | ss_type = "DGRAM"; |
| 563 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 564 | case SOCK_RAW: |
| 565 | ss_type = "RAW"; |
| 566 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 567 | case SOCK_RDM: |
| 568 | ss_type = "RDM"; |
| 569 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 570 | case SOCK_SEQPACKET: |
| 571 | ss_type = "SEQPACKET"; |
| 572 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 573 | default: |
| 574 | ss_type = "UNKNOWN"; |
| 575 | } |
| 576 | |
| 577 | switch (state) { |
| 578 | case SS_FREE: |
| 579 | ss_state = "FREE"; |
| 580 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 581 | case SS_UNCONNECTED: |
| 582 | /* |
| 583 | * Unconnected sockets may be listening |
| 584 | * for something. |
| 585 | */ |
| 586 | if (unix_flags & SO_ACCEPTCON) { |
| 587 | ss_state = "LISTENING"; |
| 588 | } else { |
| 589 | ss_state = ""; |
| 590 | } |
| 591 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 592 | case SS_CONNECTING: |
| 593 | ss_state = "CONNECTING"; |
| 594 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 595 | case SS_CONNECTED: |
| 596 | ss_state = "CONNECTED"; |
| 597 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 598 | case SS_DISCONNECTING: |
| 599 | ss_state = "DISCONNECTING"; |
| 600 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 601 | default: |
| 602 | ss_state = "UNKNOWN"; |
| 603 | } |
| 604 | |
| 605 | strcpy(ss_flags, "[ "); |
| 606 | if (unix_flags & SO_ACCEPTCON) |
| 607 | strcat(ss_flags, "ACC "); |
| 608 | if (unix_flags & SO_WAITDATA) |
| 609 | strcat(ss_flags, "W "); |
| 610 | if (unix_flags & SO_NOSPACE) |
| 611 | strcat(ss_flags, "N "); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 612 | strcat(ss_flags, "]"); |
| 613 | |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 614 | printf("%-5s %-6lu %-11s %-10s %-13s %6lu ", |
Denis Vlasenko | d3c042f | 2007-12-30 01:59:53 +0000 | [diff] [blame] | 615 | ss_proto, refcnt, ss_flags, ss_type, ss_state, inode |
| 616 | ); |
| 617 | |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 618 | #if ENABLE_FEATURE_NETSTAT_PRG |
| 619 | if (option_mask32 & OPT_prg) |
| 620 | printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode)); |
| 621 | #endif |
| 622 | |
Denis Vlasenko | d3c042f | 2007-12-30 01:59:53 +0000 | [diff] [blame] | 623 | /* TODO: currently we stop at first NUL byte. Is it a problem? */ |
| 624 | line += path_ofs; |
| 625 | *strchrnul(line, '\n') = '\0'; |
| 626 | while (*line) |
| 627 | fputc_printable(*line++, stdout); |
| 628 | bb_putchar('\n'); |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame] | 629 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 632 | static void do_info(const char *file, int FAST_FUNC (*proc)(char *)) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 633 | { |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 634 | int lnr; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 635 | FILE *procinfo; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 636 | char *buffer; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 637 | |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 638 | /* _stdin is just to save "r" param */ |
| 639 | procinfo = fopen_or_warn_stdin(file); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 640 | if (procinfo == NULL) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 641 | return; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 642 | } |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 643 | lnr = 0; |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 644 | /* Why xmalloc_fgets_str? because it doesn't stop on NULs */ |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 645 | while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) { |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 646 | /* line 0 is skipped */ |
| 647 | if (lnr && proc(buffer)) |
| 648 | bb_error_msg("%s: bogus data on line %d", file, lnr + 1); |
| 649 | lnr++; |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 650 | free(buffer); |
| 651 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 652 | fclose(procinfo); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 655 | int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 656 | int netstat_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 657 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 658 | unsigned opt; |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 659 | |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 660 | INIT_G(); |
| 661 | |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 662 | /* Option string must match NETSTAT_xxx constants */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 663 | opt = getopt32(argv, NETSTAT_OPTS); |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 664 | if (opt & OPT_sock_listen) { // -l |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 665 | flags &= ~NETSTAT_CONNECTED; |
| 666 | flags |= NETSTAT_LISTENING; |
| 667 | } |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 668 | if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a |
| 669 | //if (opt & OPT_extended) // -e |
| 670 | if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n |
| 671 | //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP |
| 672 | //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP |
| 673 | //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW |
| 674 | //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 675 | #if ENABLE_ROUTE |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 676 | if (opt & OPT_route) { // -r |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 677 | bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 678 | return 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 679 | } |
Bob Dunlop | 55a046b | 2010-10-27 02:12:29 +0200 | [diff] [blame] | 680 | #endif |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 681 | #if ENABLE_FEATURE_NETSTAT_WIDE |
| 682 | G.addr_width = ADDR_NORMAL_WIDTH; |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 683 | if (opt & OPT_wide) { // -W |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 684 | G.addr_width = ADDR_WIDE; |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 685 | } |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 686 | #endif |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 687 | #if ENABLE_FEATURE_NETSTAT_PRG |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 688 | progname_banner = ""; |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 689 | if (opt & OPT_prg) { // -p |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 690 | progname_banner = PROGNAME_BANNER; |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 691 | prg_cache_load(); |
| 692 | } |
| 693 | #endif |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 694 | |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 695 | opt &= NETSTAT_ALLPROTO; |
| 696 | if (opt) { |
| 697 | flags &= ~NETSTAT_ALLPROTO; |
| 698 | flags |= opt; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 699 | } |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 700 | if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 701 | printf("Active Internet connections "); /* xxx */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 702 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 703 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 704 | printf("(servers and established)"); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 705 | else if (flags & NETSTAT_LISTENING) |
| 706 | printf("(only servers)"); |
| 707 | else |
| 708 | printf("(w/o servers)"); |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 709 | printf(FMT_NET_CONN_HEADER, |
| 710 | IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address", |
| 711 | IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address", |
| 712 | progname_banner |
| 713 | ); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 714 | } |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 715 | if (flags & NETSTAT_TCP) { |
| 716 | do_info("/proc/net/tcp", tcp_do_one); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 717 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 718 | do_info("/proc/net/tcp6", tcp_do_one); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 719 | #endif |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 720 | } |
| 721 | if (flags & NETSTAT_UDP) { |
| 722 | do_info("/proc/net/udp", udp_do_one); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 723 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 724 | do_info("/proc/net/udp6", udp_do_one); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 725 | #endif |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 726 | } |
| 727 | if (flags & NETSTAT_RAW) { |
| 728 | do_info("/proc/net/raw", raw_do_one); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 729 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 730 | do_info("/proc/net/raw6", raw_do_one); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 731 | #endif |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 732 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 733 | if (flags & NETSTAT_UNIX) { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 734 | printf("Active UNIX domain sockets "); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 735 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 736 | printf("(servers and established)"); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 737 | else if (flags & NETSTAT_LISTENING) |
| 738 | printf("(only servers)"); |
| 739 | else |
| 740 | printf("(w/o servers)"); |
Denys Vlasenko | dd88ba8 | 2010-10-27 13:10:29 +0200 | [diff] [blame] | 741 | printf("\nProto RefCnt Flags Type State I-Node %sPath\n", progname_banner); |
Denis Vlasenko | 2c7a1fd | 2008-07-27 17:24:19 +0000 | [diff] [blame] | 742 | do_info("/proc/net/unix", unix_do_one); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 743 | } |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 744 | prg_cache_clear(); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 745 | return 0; |
| 746 | } |