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