+#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"
+/* PROGNAME_WIDTH chars: 12345678901234567890 */
+#define PROGNAME_BANNER "PID/Program name "
+
+struct prg_node {
+ struct prg_node *next;
+ long inode;
+ char name[PROGNAME_WIDTH];
+};
+
+#define PRG_HASH_SIZE 211
+
+struct globals {
+ smallint 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 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; \
+} while (0)
+
+
+#if ENABLE_FEATURE_NETSTAT_PRG
+
+/* Deliberately truncating long to unsigned *int* */
+#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
+
+static void prg_cache_add(long inode, char *name)
+{
+ unsigned hi = PRG_HASHIT(inode);
+ struct prg_node **pnp, *pn;
+
+ prg_cache_loaded = 2;
+ 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 */
+ return;
+ }
+ }
+ *pnp = xzalloc(sizeof(struct prg_node));
+ pn = *pnp;
+ pn->inode = inode;
+ safe_strncpy(pn->name, name, PROGNAME_WIDTH);
+}
+
+static const char *prg_cache_get(long inode)
+{
+ unsigned hi = PRG_HASHIT(inode);
+ struct prg_node *pn;
+
+ for (pn = prg_hash[hi]; pn; pn = pn->next)
+ if (pn->inode == inode)
+ return pn->name;
+ return "-";
+}
+
+#if ENABLE_FEATURE_CLEAN_UP
+static void prg_cache_clear(void)
+{
+ struct prg_node **pnp, *pn;
+
+ for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
+ while ((pn = *pnp) != NULL) {
+ *pnp = pn->next;
+ free(pn);
+ }
+ }
+}
+#else
+#define prg_cache_clear() ((void)0)
+#endif
+
+static long extract_socket_inode(const char *lname)
+{
+ long inode = -1;
+
+ if (is_prefixed_with(lname, "socket:[")) {
+ /* "socket:[12345]", extract the "12345" as inode */
+ inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
+ if (*lname != ']')
+ inode = -1;
+ } else if (is_prefixed_with(lname, "[0000]:")) {
+ /* "[0000]:12345", extract the "12345" as inode */
+ inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
+ if (errno) /* not NUL terminated? */
+ inode = -1;
+ }
+
+#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
+ if (errno == ERANGE)
+ inode = -1;
+#endif
+ return inode;
+}
+
+static int FAST_FUNC add_to_prg_cache_if_socket(const char *fileName,
+ struct stat *statbuf UNUSED_PARAM,
+ void *pid_slash_progname,
+ int depth UNUSED_PARAM)
+{
+ char *linkname;
+ long inode;
+
+ linkname = xmalloc_readlink(fileName);
+ if (linkname != NULL) {
+ inode = extract_socket_inode(linkname);
+ free(linkname);
+ if (inode >= 0)
+ prg_cache_add(inode, (char *)pid_slash_progname);
+ }
+ return TRUE;
+}
+
+static int FAST_FUNC dir_act(const char *fileName,
+ struct stat *statbuf UNUSED_PARAM,
+ void *userData UNUSED_PARAM,
+ int depth)
+{
+ const char *pid;
+ char *pid_slash_progname;
+ char proc_pid_fname[sizeof("/proc/%u/cmdline") + sizeof(long)*3];
+ char cmdline_buf[512];
+ int n, len;
+
+ if (depth == 0) /* "/proc" itself */
+ return TRUE; /* continue looking one level below /proc */
+
+ pid = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
+ if (!isdigit(pid[0])) /* skip /proc entries which aren't processes */
+ return SKIP;
+
+ 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[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)
+{
+ int load_ok;