grep: add proper support for pattern_list
[oweals/busybox.git] / networking / netstat.c
index b246280688cdeb9a7217451e4cf186e0b36e8a85..c7934423ba0da9b08db41d338375b1e519e86929 100644 (file)
  * 2008-07-10
  * optional '-p' flag support ported from net-tools by G. Somlo <somlo@cmu.edu>
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+//config:config NETSTAT
+//config:      bool "netstat (10 kb)"
+//config:      default y
+//config:      select PLATFORM_LINUX
+//config:      help
+//config:      netstat prints information about the Linux networking subsystem.
+//config:
+//config:config FEATURE_NETSTAT_WIDE
+//config:      bool "Enable wide output"
+//config:      default y
+//config:      depends on NETSTAT
+//config:      help
+//config:      Add support for wide columns. Useful when displaying IPv6 addresses
+//config:      (-W option).
+//config:
+//config:config FEATURE_NETSTAT_PRG
+//config:      bool "Enable PID/Program name output"
+//config:      default y
+//config:      depends on NETSTAT
+//config:      help
+//config:      Add support for -p flag to print out PID and program name.
+//config:      +700 bytes of code.
+
+//applet:IF_NETSTAT(APPLET(netstat, BB_DIR_BIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_NETSTAT) += netstat.o
 
 #include "libbb.h"
 #include "inet_common.h"
 
+//usage:#define netstat_trivial_usage
+//usage:       "[-"IF_ROUTE("r")"al] [-tuwx] [-en"IF_FEATURE_NETSTAT_WIDE("W")IF_FEATURE_NETSTAT_PRG("p")"]"
+//usage:#define netstat_full_usage "\n\n"
+//usage:       "Display networking information\n"
+//usage:       IF_ROUTE(
+//usage:     "\n       -r      Routing table"
+//usage:       )
+//usage:     "\n       -a      All sockets"
+//usage:     "\n       -l      Listening sockets"
+//usage:     "\n               Else: connected sockets"
+//usage:     "\n       -t      TCP sockets"
+//usage:     "\n       -u      UDP sockets"
+//usage:     "\n       -w      Raw sockets"
+//usage:     "\n       -x      Unix sockets"
+//usage:     "\n               Else: all socket types"
+//usage:     "\n       -e      Other/more information"
+//usage:     "\n       -n      Don't resolve names"
+//usage:       IF_FEATURE_NETSTAT_WIDE(
+//usage:     "\n       -W      Wide display"
+//usage:       )
+//usage:       IF_FEATURE_NETSTAT_PRG(
+//usage:     "\n       -p      Show PID/program name for sockets"
+//usage:       )
+
 #define NETSTAT_OPTS "laentuwx" \
-       USE_ROUTE(               "r") \
-       USE_FEATURE_NETSTAT_WIDE("W") \
-       USE_FEATURE_NETSTAT_PRG( "p")
+       IF_ROUTE(               "r") \
+       IF_FEATURE_NETSTAT_WIDE("W") \
+       IF_FEATURE_NETSTAT_PRG( "p")
 
 enum {
-       OPTBIT_KEEP_OLD = 7,
-       USE_ROUTE(               OPTBIT_ROUTE,)
-       USE_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
-       USE_FEATURE_NETSTAT_PRG( OPTBIT_PRG  ,)
        OPT_sock_listen = 1 << 0, // l
        OPT_sock_all    = 1 << 1, // a
        OPT_extended    = 1 << 2, // e
@@ -35,9 +81,13 @@ enum {
        OPT_sock_udp    = 1 << 5, // u
        OPT_sock_raw    = 1 << 6, // w
        OPT_sock_unix   = 1 << 7, // x
-       OPT_route       = USE_ROUTE(               (1 << OPTBIT_ROUTE)) + 0, // r
-       OPT_wide        = USE_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
-       OPT_prg         = USE_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG  )) + 0, // p
+       OPTBIT_x        = 7,
+       IF_ROUTE(               OPTBIT_ROUTE,)
+       IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
+       IF_FEATURE_NETSTAT_PRG( OPTBIT_PRG  ,)
+       OPT_route       = IF_ROUTE(               (1 << OPTBIT_ROUTE)) + 0, // r
+       OPT_wide        = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
+       OPT_prg         = IF_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG  )) + 0, // p
 };
 
 #define NETSTAT_CONNECTED 0x01
@@ -88,25 +138,25 @@ typedef enum {
        SS_DISCONNECTING /* in process of disconnecting  */
 } socket_state;
 
-#define SO_ACCEPTCON (1<<16)   /* performed a listen           */
-#define SO_WAITDATA  (1<<17)   /* wait data to read            */
-#define SO_NOSPACE   (1<<18)   /* no space to write            */
-
-/* Standard printout size */
-#define PRINT_IP_MAX_SIZE           23
-#define PRINT_NET_CONN              "%s   %6ld %6ld %-23s %-23s %-12s"
-#define PRINT_NET_CONN_HEADER       "\nProto Recv-Q Send-Q %-23s %-23s State       "
+#define SO_ACCEPTCON (1<<16)  /* performed a listen           */
+#define SO_WAITDATA  (1<<17)  /* wait data to read            */
+#define SO_NOSPACE   (1<<18)  /* no space to write            */
 
+#define ADDR_NORMAL_WIDTH        23
 /* When there are IPv6 connections the IPv6 addresses will be
  * truncated to none-recognition. The '-W' option makes the
- * address columns wide enough to accomodate for longest possible
+ * address columns wide enough to accommodate for longest possible
  * IPv6 addresses, i.e. addresses of the form
  * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
  */
-#define PRINT_IP_MAX_SIZE_WIDE      51  /* INET6_ADDRSTRLEN + 5 for the port number */
-#define PRINT_NET_CONN_WIDE         "%s   %6ld %6ld %-51s %-51s %-12s"
-#define PRINT_NET_CONN_HEADER_WIDE  "\nProto Recv-Q Send-Q %-51s %-51s State       "
-
+#define ADDR_WIDE                51  /* INET6_ADDRSTRLEN + 5 for the port number */
+#if ENABLE_FEATURE_NETSTAT_WIDE
+# define FMT_NET_CONN_DATA       "%s   %6lu %6lu %-*s %-*s %-12s"
+# define FMT_NET_CONN_HEADER     "\nProto Recv-Q Send-Q %-*s %-*s State       %s\n"
+#else
+# define FMT_NET_CONN_DATA       "%s   %6lu %6lu %-23s %-23s %-12s"
+# define FMT_NET_CONN_HEADER     "\nProto Recv-Q Send-Q %-23s %-23s State       %s\n"
+#endif
 
 #define PROGNAME_WIDTH     20
 #define PROGNAME_WIDTH_STR "20"
@@ -121,24 +171,31 @@ struct prg_node {
 
 #define PRG_HASH_SIZE 211
 
-
 struct globals {
-       const char *net_conn_line;
-       smallint flags;
+       smalluint flags;
 #if ENABLE_FEATURE_NETSTAT_PRG
        smallint prg_cache_loaded;
        struct prg_node *prg_hash[PRG_HASH_SIZE];
 #endif
+#if ENABLE_FEATURE_NETSTAT_PRG
+       const char *progname_banner;
+#endif
+#if ENABLE_FEATURE_NETSTAT_WIDE
+       unsigned addr_width;
+#endif
 };
 #define G (*ptr_to_globals)
 #define flags            (G.flags           )
-#define net_conn_line    (G.net_conn_line   )
-#define prg_hash         (G.prg_hash        )
 #define prg_cache_loaded (G.prg_cache_loaded)
+#define prg_hash         (G.prg_hash        )
+#if ENABLE_FEATURE_NETSTAT_PRG
+# define progname_banner (G.progname_banner )
+#else
+# define progname_banner ""
+#endif
 #define INIT_G() do { \
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
        flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
-       net_conn_line = PRINT_NET_CONN; \
 } while (0)
 
 
@@ -147,10 +204,6 @@ struct globals {
 /* Deliberately truncating long to unsigned *int* */
 #define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
 
-#define print_progname_banner() do { \
-       if (option_mask32 & OPT_prg) printf(PROGNAME_BANNER); \
-} while (0)
-
 static void prg_cache_add(long inode, char *name)
 {
        unsigned hi = PRG_HASHIT(inode);
@@ -160,7 +213,7 @@ static void prg_cache_add(long inode, char *name)
        for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
                if (pn->inode == inode) {
                        /* Some warning should be appropriate here
-                          as we got multiple processes for one i-node */
+                        * as we got multiple processes for one i-node */
                        return;
                }
        }
@@ -201,14 +254,14 @@ static long extract_socket_inode(const char *lname)
 {
        long inode = -1;
 
-       if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
+       if (is_prefixed_with(lname, "socket:[")) {
                /* "socket:[12345]", extract the "12345" as inode */
-               inode = bb_strtol(lname + sizeof("socket:[")-1, (char**)&lname, 0);
+               inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
                if (*lname != ']')
                        inode = -1;
-       } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
+       } else if (is_prefixed_with(lname, "[0000]:")) {
                /* "[0000]:12345", extract the "12345" as inode */
-               inode = bb_strtol(lname + sizeof("[0000]:")-1, NULL, 0);
+               inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
                if (errno) /* not NUL terminated? */
                        inode = -1;
        }
@@ -220,9 +273,9 @@ static long extract_socket_inode(const char *lname)
        return inode;
 }
 
-static int FAST_FUNC file_act(const char *fileName,
+static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName,
                struct stat *statbuf UNUSED_PARAM,
-               void *userData,
+               void *pid_slash_progname,
                int depth UNUSED_PARAM)
 {
        char *linkname;
@@ -233,7 +286,7 @@ static int FAST_FUNC file_act(const char *fileName,
                inode = extract_socket_inode(linkname);
                free(linkname);
                if (inode >= 0)
-                       prg_cache_add(inode, (char *)userData);
+                       prg_cache_add(inode, (char *)pid_slash_progname);
        }
        return TRUE;
 }
@@ -243,38 +296,40 @@ static int FAST_FUNC dir_act(const char *fileName,
                void *userData UNUSED_PARAM,
                int depth)
 {
-       const char *shortName;
-       char *p, *q;
+       const char *pid;
+       char *pid_slash_progname;
+       char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
        char cmdline_buf[512];
-       int i;
+       int n, len;
 
        if (depth == 0) /* "/proc" itself */
                return TRUE; /* continue looking one level below /proc */
 
-       shortName = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
-       if (!isdigit(shortName[0])) /* skip /proc entries whic aren't processes */
+       pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
+       if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
                return SKIP;
 
-       p = concat_path_file(fileName, "cmdline"); /* "/proc/PID/cmdline" */
-       i = open_read_close(p, cmdline_buf, sizeof(cmdline_buf) - 1);
-       free(p);
-       if (i < 0)
+       len = snprintf(proc_pid_fname, sizeof(proc_pid_fname), "%s/cmdline", fileName);
+       n = open_read_close(proc_pid_fname, cmdline_buf, sizeof(cmdline_buf) - 1);
+       if (n < 0)
                return FALSE;
-       cmdline_buf[i] = '\0';
-       q = concat_path_file(shortName, bb_basename(cmdline_buf)); /* "PID/argv0" */
-
-       /* go through all files in /proc/PID/fd */
-       p = concat_path_file(fileName, "fd");
-       i = recursive_action(p, ACTION_RECURSE | ACTION_QUIET,
-                               file_act, NULL, (void *)q, 0);
-
-       free(p);
-       free(q);
-
-       if (!i)
-               return FALSE;   /* signal permissions error to caller */
-
-       return SKIP;            /* caller should not recurse further into this dir. */
+       cmdline_buf[n] = '\0';
+
+       /* go through all files in /proc/PID/fd and check whether they are sockets */
+       strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
+       pid_slash_progname = concat_path_file(pid, bb_basename(cmdline_buf)); /* "PID/argv0" */
+       n = recursive_action(proc_pid_fname,
+                       ACTION_RECURSE | ACTION_QUIET,
+                       add_to_prg_cache_if_socket,
+                       NULL,
+                       (void *)pid_slash_progname,
+                       0);
+       free(pid_slash_progname);
+
+       if (!n)
+               return FALSE; /* signal permissions error to caller */
+
+       return SKIP; /* caller should not recurse further into this dir */
 }
 
 static void prg_cache_load(void)
@@ -288,15 +343,14 @@ static void prg_cache_load(void)
                return;
 
        if (prg_cache_loaded == 1)
-               bb_error_msg("can't scan /proc - are you root?");
+               bb_simple_error_msg("can't scan /proc - are you root?");
        else
-               bb_error_msg("showing only processes with your user ID");
+               bb_simple_error_msg("showing only processes with your user ID");
 }
 
 #else
 
 #define prg_cache_clear()       ((void)0)
-#define print_progname_banner() ((void)0)
 
 #endif //ENABLE_FEATURE_NETSTAT_PRG
 
@@ -311,21 +365,16 @@ static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
                   &in6.s6_addr32[0], &in6.s6_addr32[1],
                   &in6.s6_addr32[2], &in6.s6_addr32[3]);
        inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
-       inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr->sin6_addr);
+       inet_pton(AF_INET6, addr6, &localaddr->sin6_addr);
 
        localaddr->sin6_family = AF_INET6;
 }
 #endif
 
-#if ENABLE_FEATURE_IPV6
-static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr)
-#else
 static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
-#endif
 {
-       sscanf(local_addr, "%X",
-                  &((struct sockaddr_in *) localaddr)->sin_addr.s_addr);
-       ((struct sockaddr *) localaddr)->sa_family = AF_INET;
+       sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
+       localaddr->sin_family = AF_INET;
 }
 
 static const char *get_sname(int port, const char *proto, int numeric)
@@ -348,8 +397,11 @@ static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int
        /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
         * in IPv6, while "0.0.0.0" is not. */
 
-       host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
-                      : xmalloc_sockaddr2host_noport(addr);
+       host = NULL;
+       if (!numeric)
+               host = xmalloc_sockaddr2host_noport(addr);
+       if (!host)
+               host = xmalloc_sockaddr2dotted_noport(addr);
 
        host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
        free(host);
@@ -358,24 +410,29 @@ static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int
 
 struct inet_params {
        int local_port, rem_port, state, uid;
+       union {
+               struct sockaddr     sa;
+               struct sockaddr_in  sin;
 #if ENABLE_FEATURE_IPV6
-       struct sockaddr_in6 localaddr, remaddr;
-#else
-       struct sockaddr_in localaddr, remaddr;
+               struct sockaddr_in6 sin6;
 #endif
+       } localaddr, remaddr;
        unsigned long rxq, txq, inode;
 };
 
 static int scan_inet_proc_line(struct inet_params *param, char *line)
 {
        int num;
-       char local_addr[64], rem_addr[64];
+       /* IPv6 /proc files use 32-char hex representation
+        * of IPv6 address, followed by :PORT_IN_HEX
+        */
+       char local_addr[33], rem_addr[33]; /* 32 + 1 for NUL */
 
        num = sscanf(line,
-                       "%*d: %64[0-9A-Fa-f]:%X "
-                       "%64[0-9A-Fa-f]:%X %X "
+                       "%*d: %32[0-9A-Fa-f]:%X "
+                       "%32[0-9A-Fa-f]:%X %X "
                        "%lX:%lX %*X:%*X "
-                       "%*X %d %*d %ld ",
+                       "%*X %d %*d %lu ",
                        local_addr, &param->local_port,
                        rem_addr, &param->rem_port, &param->state,
                        &param->txq, &param->rxq,
@@ -386,12 +443,12 @@ static int scan_inet_proc_line(struct inet_params *param, char *line)
 
        if (strlen(local_addr) > 8) {
 #if ENABLE_FEATURE_IPV6
-               build_ipv6_addr(local_addr, &param->localaddr);
-               build_ipv6_addr(rem_addr, &param->remaddr);
+               build_ipv6_addr(local_addr, &param->localaddr.sin6);
+               build_ipv6_addr(rem_addr, &param->remaddr.sin6);
 #endif
        } else {
-               build_ipv4_addr(local_addr, &param->localaddr);
-               build_ipv4_addr(rem_addr, &param->remaddr);
+               build_ipv4_addr(local_addr, &param->localaddr.sin);
+               build_ipv4_addr(rem_addr, &param->remaddr.sin);
        }
        return 0;
 }
@@ -403,13 +460,16 @@ static void print_inet_line(struct inet_params *param,
         || (!is_connected && (flags & NETSTAT_LISTENING))
        ) {
                char *l = ip_port_str(
-                               (struct sockaddr *) &param->localaddr, param->local_port,
+                               &param->localaddr.sa, param->local_port,
                                proto, flags & NETSTAT_NUMERIC);
                char *r = ip_port_str(
-                               (struct sockaddr *) &param->remaddr, param->rem_port,
+                               &param->remaddr.sa, param->rem_port,
                                proto, flags & NETSTAT_NUMERIC);
-               printf(net_conn_line,
-                       proto, param->rxq, param->txq, l, r, state_str);
+               printf(FMT_NET_CONN_DATA,
+                       proto, param->rxq, param->txq,
+                       IF_FEATURE_NETSTAT_WIDE(G.addr_width,) l,
+                       IF_FEATURE_NETSTAT_WIDE(G.addr_width,) r,
+                       state_str);
 #if ENABLE_FEATURE_NETSTAT_PRG
                if (option_mask32 & OPT_prg)
                        printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
@@ -424,6 +484,7 @@ static int FAST_FUNC tcp_do_one(char *line)
 {
        struct inet_params param;
 
+       memset(&param, 0, sizeof(param));
        if (scan_inet_proc_line(&param, line))
                return 1;
 
@@ -432,17 +493,17 @@ static int FAST_FUNC tcp_do_one(char *line)
 }
 
 #if ENABLE_FEATURE_IPV6
-# define notnull(A) ( \
-       ( (A.sin6_family == AF_INET6)                               \
-         && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
-             A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3])  \
-       ) || (                                                      \
-         (A.sin6_family == AF_INET)                                \
-         && ((struct sockaddr_in*)&A)->sin_addr.s_addr             \
-       )                                                           \
+# define NOT_NULL_ADDR(A) ( \
+       ( (A.sa.sa_family == AF_INET6) \
+         && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
+             A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3])  \
+       ) || ( \
+         (A.sa.sa_family == AF_INET) \
+         && A.sin.sin_addr.s_addr != 0 \
+       ) \
 )
 #else
-# define notnull(A) (A.sin_addr.s_addr)
+# define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
 #endif
 
 static int FAST_FUNC udp_do_one(char *line)
@@ -451,6 +512,7 @@ static int FAST_FUNC udp_do_one(char *line)
        const char *state_str;
        struct inet_params param;
 
+       memset(&param, 0, sizeof(param)); /* otherwise we display garbage IPv6 scope_ids */
        if (scan_inet_proc_line(&param, line))
                return 1;
 
@@ -464,7 +526,7 @@ static int FAST_FUNC udp_do_one(char *line)
                break;
        }
 
-       have_remaddr = notnull(param.remaddr);
+       have_remaddr = NOT_NULL_ADDR(param.remaddr);
        print_inet_line(&param, state_str, "udp", have_remaddr);
        return 0;
 }
@@ -477,7 +539,7 @@ static int FAST_FUNC raw_do_one(char *line)
        if (scan_inet_proc_line(&param, line))
                return 1;
 
-       have_remaddr = notnull(param.remaddr);
+       have_remaddr = NOT_NULL_ADDR(param.remaddr);
        print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
        return 0;
 }
@@ -578,7 +640,7 @@ static int FAST_FUNC unix_do_one(char *line)
                strcat(ss_flags, "N ");
        strcat(ss_flags, "]");
 
-       printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
+       printf("%-5s %-6lu %-11s %-10s %-13s %6lu ",
                ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
                );
 
@@ -589,7 +651,7 @@ static int FAST_FUNC unix_do_one(char *line)
 
        /* TODO: currently we stop at first NUL byte. Is it a problem? */
        line += path_ofs;
-       *strchrnul(line, '\n') = '\0';
+       chomp(line);
        while (*line)
                fputc_printable(*line++, stdout);
        bb_putchar('\n');
@@ -622,38 +684,39 @@ static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
 int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int netstat_main(int argc UNUSED_PARAM, char **argv)
 {
-       const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
        unsigned opt;
 
        INIT_G();
 
        /* Option string must match NETSTAT_xxx constants */
        opt = getopt32(argv, NETSTAT_OPTS);
-       if (opt & 0x1) { // -l
+       if (opt & OPT_sock_listen) { // -l
                flags &= ~NETSTAT_CONNECTED;
                flags |= NETSTAT_LISTENING;
        }
-       if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
-       //if (opt & 0x4) // -e
-       if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
-       //if (opt & 0x10) // -t: NETSTAT_TCP
-       //if (opt & 0x20) // -u: NETSTAT_UDP
-       //if (opt & 0x40) // -w: NETSTAT_RAW
-       //if (opt & 0x80) // -x: NETSTAT_UNIX
-       if (opt & OPT_route) { // -r
+       if (opt & OPT_sock_all) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
+       //if (opt & OPT_extended) // -e
+       if (opt & OPT_noresolve) flags |= NETSTAT_NUMERIC; // -n
+       //if (opt & OPT_sock_tcp) // -t: NETSTAT_TCP
+       //if (opt & OPT_sock_udp) // -u: NETSTAT_UDP
+       //if (opt & OPT_sock_raw) // -w: NETSTAT_RAW
+       //if (opt & OPT_sock_unix) // -x: NETSTAT_UNIX
 #if ENABLE_ROUTE
+       if (opt & OPT_route) { // -r
                bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
                return 0;
-#else
-               bb_show_usage();
-#endif
        }
+#endif
+#if ENABLE_FEATURE_NETSTAT_WIDE
+       G.addr_width = ADDR_NORMAL_WIDTH;
        if (opt & OPT_wide) { // -W
-               net_conn_line = PRINT_NET_CONN_WIDE;
-               net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
+               G.addr_width = ADDR_WIDE;
        }
+#endif
 #if ENABLE_FEATURE_NETSTAT_PRG
+       progname_banner = "";
        if (opt & OPT_prg) { // -p
+               progname_banner = PROGNAME_BANNER;
                prg_cache_load();
        }
 #endif
@@ -664,7 +727,7 @@ int netstat_main(int argc UNUSED_PARAM, char **argv)
                flags |= opt;
        }
        if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
-               printf("Active Internet connections "); /* xxx */
+               printf("Active Internet connections "); /* xxx */
 
                if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
                        printf("(servers and established)");
@@ -672,9 +735,11 @@ int netstat_main(int argc UNUSED_PARAM, char **argv)
                        printf("(only servers)");
                else
                        printf("(w/o servers)");
-               printf(net_conn_line_header, "Local Address", "Foreign Address");
-               print_progname_banner();
-               bb_putchar('\n');
+               printf(FMT_NET_CONN_HEADER,
+                               IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Local Address",
+                               IF_FEATURE_NETSTAT_WIDE(G.addr_width,) "Foreign Address",
+                               progname_banner
+               );
        }
        if (flags & NETSTAT_TCP) {
                do_info("/proc/net/tcp", tcp_do_one);
@@ -702,9 +767,7 @@ int netstat_main(int argc UNUSED_PARAM, char **argv)
                        printf("(only servers)");
                else
                        printf("(w/o servers)");
-               printf("\nProto RefCnt Flags       Type       State         I-Node ");
-               print_progname_banner();
-               printf("Path\n");
+               printf("\nProto RefCnt Flags       Type       State         I-Node %sPath\n", progname_banner);
                do_info("/proc/net/unix", unix_do_one);
        }
        prg_cache_clear();