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