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