* Mini netstat implementation(s) for busybox
* based in part on the netstat implementation from net-tools.
*
- * Copyright (C) 2001 by Bart Visscher <magick@linux-fan.com>
+ * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * 2002-04-20
+ * IPV6 support added by Bart Visscher <magick@linux-fan.com>
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <dirent.h>
-#include <unistd.h>
-#include "inet_common.h"
#include "busybox.h"
-#include "pwd.h"
+#include "inet_common.h"
#ifdef CONFIG_ROUTE
extern void displayroutes(int noresolve, int netstatfmt);
#endif
-#define NETSTAT_CONNECTED 0x01
-#define NETSTAT_LISTENING 0x02
-#define NETSTAT_NUMERIC 0x04
-#define NETSTAT_TCP 0x10
-#define NETSTAT_UDP 0x20
-#define NETSTAT_RAW 0x40
-#define NETSTAT_UNIX 0x80
+#define NETSTAT_CONNECTED 0x01
+#define NETSTAT_LISTENING 0x02
+#define NETSTAT_NUMERIC 0x04
+/* Must match getopt_ulflags option string */
+#define NETSTAT_TCP 0x10
+#define NETSTAT_UDP 0x20
+#define NETSTAT_RAW 0x40
+#define NETSTAT_UNIX 0x80
+#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
-static int flags = NETSTAT_CONNECTED |
- NETSTAT_TCP | NETSTAT_UDP | NETSTAT_RAW | NETSTAT_UNIX;
+static int flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO;
#define PROGNAME_WIDTHs PROGNAME_WIDTH1(PROGNAME_WIDTH)
#define PROGNAME_WIDTH1(s) PROGNAME_WIDTH2(s)
#define SO_WAITDATA (1<<17) /* wait data to read */
#define SO_NOSPACE (1<<18) /* no space to write */
-static char *itoa(unsigned int i)
-{
- /* 21 digits plus null terminator, good for 64-bit or smaller ints */
- static char local[22];
- char *p = &local[21];
- *p-- = '\0';
- do {
- *p-- = '0' + i % 10;
- i /= 10;
- } while (i > 0);
- return p + 1;
-}
-
static char *get_sname(int port, const char *proto, int num)
{
char *str=itoa(ntohs(port));
{
char *port_name;
+#ifdef CONFIG_FEATURE_IPV6
+ if (addr->sa_family == AF_INET6) {
+ INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr,
+ (numeric&NETSTAT_NUMERIC) ? 0x0fff : 0);
+ } else
+#endif
+ {
INET_rresolve(ip_port, size, (struct sockaddr_in *)addr,
0x4000 | ((numeric&NETSTAT_NUMERIC) ? 0x0fff : 0),
0xffffffff);
+ }
port_name=get_sname(htons(port), proto, numeric);
if ((strlen(ip_port) + strlen(port_name)) > 22)
ip_port[22 - strlen(port_name)] = '\0';
const char *state_str;
char more[512];
int num, local_port, rem_port, d, state, timer_run, uid, timeout;
+#ifdef CONFIG_FEATURE_IPV6
+ struct sockaddr_in6 localaddr, remaddr;
+ char addr6[INET6_ADDRSTRLEN];
+ struct in6_addr in6;
+#else
struct sockaddr_in localaddr, remaddr;
+#endif
unsigned long rxq, txq, time_len, retr, inode;
if (lnr == 0)
&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
if (strlen(local_addr) > 8) {
+#ifdef CONFIG_FEATURE_IPV6
+ sscanf(local_addr, "%08X%08X%08X%08X",
+ &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);
+ sscanf(rem_addr, "%08X%08X%08X%08X",
+ &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 *) &remaddr.sin6_addr);
+ localaddr.sin6_family = AF_INET6;
+ remaddr.sin6_family = AF_INET6;
+#endif
} else {
sscanf(local_addr, "%X",
&((struct sockaddr_in *) &localaddr)->sin_addr.s_addr);
}
if (num < 10) {
- error_msg("warning, got bogus tcp line.");
+ bb_error_msg("warning, got bogus tcp line");
return;
}
state_str = tcp_state[state];
snprint_ip_port(local_addr, sizeof(local_addr),
(struct sockaddr *) &localaddr, local_port,
"tcp", flags&NETSTAT_NUMERIC);
-
+
snprint_ip_port(rem_addr, sizeof(rem_addr),
(struct sockaddr *) &remaddr, rem_port,
"tcp", flags&NETSTAT_NUMERIC);
char local_addr[64], rem_addr[64];
char *state_str, more[512];
int num, local_port, rem_port, d, state, timer_run, uid, timeout;
+#ifdef CONFIG_FEATURE_IPV6
+ struct sockaddr_in6 localaddr, remaddr;
+ char addr6[INET6_ADDRSTRLEN];
+ struct in6_addr in6;
+#else
struct sockaddr_in localaddr, remaddr;
+#endif
unsigned long rxq, txq, time_len, retr, inode;
if (lnr == 0)
&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
if (strlen(local_addr) > 8) {
+#ifdef CONFIG_FEATURE_IPV6
+ /* Demangle what the kernel gives us */
+ sscanf(local_addr, "%08X%08X%08X%08X",
+ &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);
+ sscanf(rem_addr, "%08X%08X%08X%08X",
+ &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 *) &remaddr.sin6_addr);
+ localaddr.sin6_family = AF_INET6;
+ remaddr.sin6_family = AF_INET6;
+#endif
} else {
sscanf(local_addr, "%X",
&((struct sockaddr_in *) &localaddr)->sin_addr.s_addr);
}
if (num < 10) {
- error_msg("warning, got bogus udp line.");
+ bb_error_msg("warning, got bogus udp line");
return;
}
switch (state) {
break;
}
-#define notnull(A) (A.sin_addr.s_addr)
+#ifdef CONFIG_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))
+#else
+# define notnull(A) (A.sin_addr.s_addr)
+#endif
if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) ||
(!notnull(remaddr) && (flags&NETSTAT_LISTENING)))
{
snprint_ip_port(local_addr, sizeof(local_addr),
(struct sockaddr *) &localaddr, local_port,
"udp", flags&NETSTAT_NUMERIC);
-
+
snprint_ip_port(rem_addr, sizeof(rem_addr),
(struct sockaddr *) &remaddr, rem_port,
"udp", flags&NETSTAT_NUMERIC);
char local_addr[64], rem_addr[64];
char *state_str, more[512];
int num, local_port, rem_port, d, state, timer_run, uid, timeout;
+#ifdef CONFIG_FEATURE_IPV6
+ struct sockaddr_in6 localaddr, remaddr;
+ char addr6[INET6_ADDRSTRLEN];
+ struct in6_addr in6;
+#else
struct sockaddr_in localaddr, remaddr;
+#endif
unsigned long rxq, txq, time_len, retr, inode;
if (lnr == 0)
&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
if (strlen(local_addr) > 8) {
+#ifdef CONFIG_FEATURE_IPV6
+ sscanf(local_addr, "%08X%08X%08X%08X",
+ &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);
+ sscanf(rem_addr, "%08X%08X%08X%08X",
+ &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 *) &remaddr.sin6_addr);
+ localaddr.sin6_family = AF_INET6;
+ remaddr.sin6_family = AF_INET6;
+#endif
} else {
sscanf(local_addr, "%X",
&((struct sockaddr_in *) &localaddr)->sin_addr.s_addr);
}
if (num < 10) {
- error_msg("warning, got bogus raw line.");
+ bb_error_msg("warning, got bogus raw line");
return;
}
state_str=itoa(state);
-#define notnull(A) (A.sin_addr.s_addr)
+#ifdef CONFIG_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))
+#else
+# define notnull(A) (A.sin_addr.s_addr)
+#endif
if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) ||
(!notnull(remaddr) && (flags&NETSTAT_LISTENING)))
{
snprint_ip_port(local_addr, sizeof(local_addr),
(struct sockaddr *) &localaddr, local_port,
"raw", flags&NETSTAT_NUMERIC);
-
+
snprint_ip_port(rem_addr, sizeof(rem_addr),
(struct sockaddr *) &remaddr, rem_port,
"raw", flags&NETSTAT_NUMERIC);
num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s",
&d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path);
if (num < 6) {
- error_msg("warning, got bogus unix line.");
+ bb_error_msg("warning, got bogus unix line");
return;
}
if (!(has & HAS_INODE))
}
#define _PATH_PROCNET_UDP "/proc/net/udp"
+#define _PATH_PROCNET_UDP6 "/proc/net/udp6"
#define _PATH_PROCNET_TCP "/proc/net/tcp"
+#define _PATH_PROCNET_TCP6 "/proc/net/tcp6"
#define _PATH_PROCNET_RAW "/proc/net/raw"
+#define _PATH_PROCNET_RAW6 "/proc/net/raw6"
#define _PATH_PROCNET_UNIX "/proc/net/unix"
static void do_info(const char *file, const char *name, void (*proc)(int, const char *))
if (errno != ENOENT) {
perror(file);
} else {
- error_msg("no support for `%s' on this system.", name);
+ bb_error_msg("no support for `%s' on this system", name);
}
} else {
do {
int netstat_main(int argc, char **argv)
{
- int opt;
- int new_flags=0;
- int showroute = 0, extended = 0;
- while ((opt = getopt(argc, argv, "laenrtuwx")) != -1)
- switch (opt) {
- case 'l':
- flags &= ~NETSTAT_CONNECTED;
- flags |= NETSTAT_LISTENING;
- break;
- case 'a':
- flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED;
- break;
- case 'n':
- flags |= NETSTAT_NUMERIC;
- break;
- case 'r':
- showroute = 1;
- break;
- case 'e':
- extended = 1;
- break;
- case 't':
- new_flags |= NETSTAT_TCP;
- break;
- case 'u':
- new_flags |= NETSTAT_UDP;
- break;
- case 'w':
- new_flags |= NETSTAT_RAW;
- break;
- case 'x':
- new_flags |= NETSTAT_UNIX;
- break;
- default:
- show_usage();
- }
- if ( showroute ) {
-#ifdef CONFIG_ROUTE
- displayroutes ( flags & NETSTAT_NUMERIC, !extended );
- return 0;
+ enum {
+ OPT_extended = 0x4,
+ OPT_showroute = 0x100,
+ };
+ unsigned long opt;
+#ifdef CONFIG_FEATURE_IPV6
+ int inet = 1;
+ int inet6 = 1;
#else
- error_msg_and_die( "-r (display routing table) is not compiled in." );
+# define inet 1
+# define inet6 0
+#endif
+
+ /* Option string must match NETSTAT_xxx constants */
+ opt = bb_getopt_ulflags(argc, argv, "laentuwxr");
+ if (opt & 0x1) { // -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_showroute) { // -r
+#ifdef CONFIG_ROUTE
+ displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
+ return 0;
+#else
+ bb_error_msg_and_die("-r (display routing table) is not compiled in");
#endif
- }
-
- if (new_flags) {
- flags &= ~(NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX);
- flags |= new_flags;
}
- if (flags&(NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
+
+ opt &= NETSTAT_ALLPROTO;
+ if (opt) {
+ flags &= ~NETSTAT_ALLPROTO;
+ flags |= opt;
+ }
+ if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
printf("Active Internet connections "); /* xxx */
if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED))
printf("(servers and established)");
else {
- if (flags&NETSTAT_LISTENING)
+ if (flags & NETSTAT_LISTENING)
printf("(only servers)");
else
printf("(w/o servers)");
}
printf("\nProto Recv-Q Send-Q Local Address Foreign Address State \n");
}
- if (flags&NETSTAT_TCP)
+ if (inet && flags&NETSTAT_TCP)
do_info(_PATH_PROCNET_TCP,"AF INET (tcp)",tcp_do_one);
- if (flags&NETSTAT_UDP)
+#ifdef CONFIG_FEATURE_IPV6
+ if (inet6 && flags&NETSTAT_TCP)
+ do_info(_PATH_PROCNET_TCP6,"AF INET6 (tcp)",tcp_do_one);
+#endif
+ if (inet && flags&NETSTAT_UDP)
do_info(_PATH_PROCNET_UDP,"AF INET (udp)",udp_do_one);
- if (flags&NETSTAT_RAW)
+#ifdef CONFIG_FEATURE_IPV6
+ if (inet6 && flags&NETSTAT_UDP)
+ do_info(_PATH_PROCNET_UDP6,"AF INET6 (udp)",udp_do_one);
+#endif
+ if (inet && flags&NETSTAT_RAW)
do_info(_PATH_PROCNET_RAW,"AF INET (raw)",raw_do_one);
+#ifdef CONFIG_FEATURE_IPV6
+ if (inet6 && flags&NETSTAT_RAW)
+ do_info(_PATH_PROCNET_RAW6,"AF INET6 (raw)",raw_do_one);
+#endif
if (flags&NETSTAT_UNIX) {
printf("Active UNIX domain sockets ");
if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED))