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