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