diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-22 17:12:08 +0300 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-22 17:12:08 +0300 |
commit | 703e20235aa0624d3ff4335c1e86edaa6e21efe2 (patch) | |
tree | 49e8451efad93b75c0be74ee553d3b3f8c04a25d /networking/netstat.c | |
parent | 6cd2d2bcba37a13d0d73326dd7bca64bbccce4f8 (diff) |
cleanups: unnecessary casts, unified const_1, eliminate cross-.c file
prototypes (heresy!), add spaces in places like "flags&NETSTAT_CONNECTED",
removed unused #defines, #ifdef -> #if, use uint32_t for ipv4 addrs.
Diffstat (limited to 'networking/netstat.c')
-rw-r--r-- | networking/netstat.c | 258 |
1 files changed, 119 insertions, 139 deletions
diff --git a/networking/netstat.c b/networking/netstat.c index 3dad57a40..16ee52d9a 100644 --- a/networking/netstat.c +++ b/networking/netstat.c @@ -14,81 +14,70 @@ #include "busybox.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_CONNECTED 0x01 +#define NETSTAT_LISTENING 0x02 +#define NETSTAT_NUMERIC 0x04 /* Must match getopt32 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) +#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_ALLPROTO; -#define PROGNAME_WIDTHs PROGNAME_WIDTH1(PROGNAME_WIDTH) -#define PROGNAME_WIDTH1(s) PROGNAME_WIDTH2(s) -#define PROGNAME_WIDTH2(s) #s - -#define PRG_HASH_SIZE 211 - enum { - TCP_ESTABLISHED = 1, - TCP_SYN_SENT, - TCP_SYN_RECV, - TCP_FIN_WAIT1, - TCP_FIN_WAIT2, - TCP_TIME_WAIT, - TCP_CLOSE, - TCP_CLOSE_WAIT, - TCP_LAST_ACK, - TCP_LISTEN, - TCP_CLOSING /* now a valid state */ + TCP_ESTABLISHED = 1, + TCP_SYN_SENT, + TCP_SYN_RECV, + TCP_FIN_WAIT1, + TCP_FIN_WAIT2, + TCP_TIME_WAIT, + TCP_CLOSE, + TCP_CLOSE_WAIT, + TCP_LAST_ACK, + TCP_LISTEN, + TCP_CLOSING /* now a valid state */ }; static const char * const tcp_state[] = { - "", - "ESTABLISHED", - "SYN_SENT", - "SYN_RECV", - "FIN_WAIT1", - "FIN_WAIT2", - "TIME_WAIT", - "CLOSE", - "CLOSE_WAIT", - "LAST_ACK", - "LISTEN", - "CLOSING" + "", + "ESTABLISHED", + "SYN_SENT", + "SYN_RECV", + "FIN_WAIT1", + "FIN_WAIT2", + "TIME_WAIT", + "CLOSE", + "CLOSE_WAIT", + "LAST_ACK", + "LISTEN", + "CLOSING" }; typedef enum { - SS_FREE = 0, /* not allocated */ - SS_UNCONNECTED, /* unconnected to any socket */ - SS_CONNECTING, /* in process of connecting */ - SS_CONNECTED, /* connected to socket */ - SS_DISCONNECTING /* in process of disconnecting */ + SS_FREE = 0, /* not allocated */ + SS_UNCONNECTED, /* unconnected to any socket */ + SS_CONNECTING, /* in process of connecting */ + SS_CONNECTED, /* connected to socket */ + 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 */ +#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 */ static char *get_sname(int port, const char *proto, int num) { - char *str=itoa(ntohs(port)); - if (num) { - } else { - struct servent *se=getservbyport(port,proto); + char *str = itoa(ntohs(port)); + if (!num) { + struct servent *se = getservbyport(port, proto); if (se) - str=se->s_name; + str = se->s_name; } if (!port) { - str="*"; + str = "*"; } return str; } @@ -97,21 +86,21 @@ static void snprint_ip_port(char *ip_port, int size, struct sockaddr *addr, int { char *port_name; -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_FEATURE_IPV6 if (addr->sa_family == AF_INET6) { INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr, - (numeric&NETSTAT_NUMERIC) ? 0x0fff : 0); + (numeric & NETSTAT_NUMERIC) ? 0x0fff : 0); } else #endif { - INET_rresolve(ip_port, size, (struct sockaddr_in *)addr, - 0x4000 | ((numeric&NETSTAT_NUMERIC) ? 0x0fff : 0), - 0xffffffff); + INET_rresolve(ip_port, size, (struct sockaddr_in *)addr, + 0x4000 | ((numeric & NETSTAT_NUMERIC) ? 0x0fff : 0), + 0xffffffff); } - port_name=get_sname(htons(port), proto, numeric); + port_name = get_sname(htons(port), proto, numeric); if ((strlen(ip_port) + strlen(port_name)) > 22) ip_port[22 - strlen(port_name)] = '\0'; - ip_port+=strlen(ip_port); + ip_port += strlen(ip_port); strcat(ip_port, ":"); strcat(ip_port, port_name); } @@ -122,7 +111,7 @@ static void tcp_do_one(int lnr, const char *line) const char *state_str; char more[512]; int num, local_port, rem_port, d, state, timer_run, uid, timeout; -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_FEATURE_IPV6 struct sockaddr_in6 localaddr, remaddr; char addr6[INET6_ADDRSTRLEN]; struct in6_addr in6; @@ -142,7 +131,7 @@ static void tcp_do_one(int lnr, const char *line) &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); if (strlen(local_addr) > 8) { -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_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]); @@ -170,20 +159,19 @@ static void tcp_do_one(int lnr, const char *line) return; } state_str = tcp_state[state]; - if ((rem_port && (flags&NETSTAT_CONNECTED)) || - (!rem_port && (flags&NETSTAT_LISTENING))) - { + if ((rem_port && (flags & NETSTAT_CONNECTED)) + || (!rem_port && (flags & NETSTAT_LISTENING)) + ) { snprint_ip_port(local_addr, sizeof(local_addr), (struct sockaddr *) &localaddr, local_port, - "tcp", flags&NETSTAT_NUMERIC); + "tcp", flags & NETSTAT_NUMERIC); snprint_ip_port(rem_addr, sizeof(rem_addr), (struct sockaddr *) &remaddr, rem_port, - "tcp", flags&NETSTAT_NUMERIC); + "tcp", flags & NETSTAT_NUMERIC); printf("tcp %6ld %6ld %-23s %-23s %-12s\n", rxq, txq, local_addr, rem_addr, state_str); - } } @@ -192,7 +180,7 @@ static void udp_do_one(int lnr, const char *line) 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 +#if ENABLE_FEATURE_IPV6 struct sockaddr_in6 localaddr, remaddr; char addr6[INET6_ADDRSTRLEN]; struct in6_addr in6; @@ -212,7 +200,7 @@ static void udp_do_one(int lnr, const char *line) &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); if (strlen(local_addr) > 8) { -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_FEATURE_IPV6 /* Demangle what the kernel gives us */ sscanf(local_addr, "%08X%08X%08X%08X", &in6.s6_addr32[0], &in6.s6_addr32[1], @@ -254,7 +242,7 @@ static void udp_do_one(int lnr, const char *line) break; } -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_FEATURE_IPV6 # define notnull(A) (((A.sin6_family == AF_INET6) && \ ((A.sin6_addr.s6_addr32[0]) || \ (A.sin6_addr.s6_addr32[1]) || \ @@ -265,20 +253,19 @@ static void udp_do_one(int lnr, const char *line) #else # define notnull(A) (A.sin_addr.s_addr) #endif - if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) || - (!notnull(remaddr) && (flags&NETSTAT_LISTENING))) - { + 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); + "udp", flags & NETSTAT_NUMERIC); snprint_ip_port(rem_addr, sizeof(rem_addr), (struct sockaddr *) &remaddr, rem_port, - "udp", flags&NETSTAT_NUMERIC); + "udp", flags & NETSTAT_NUMERIC); printf("udp %6ld %6ld %-23s %-23s %-12s\n", rxq, txq, local_addr, rem_addr, state_str); - } } @@ -287,7 +274,7 @@ static void raw_do_one(int lnr, const char *line) 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 +#if ENABLE_FEATURE_IPV6 struct sockaddr_in6 localaddr, remaddr; char addr6[INET6_ADDRSTRLEN]; struct in6_addr in6; @@ -307,7 +294,7 @@ static void raw_do_one(int lnr, const char *line) &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); if (strlen(local_addr) > 8) { -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_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]); @@ -334,9 +321,9 @@ static void raw_do_one(int lnr, const char *line) bb_error_msg("warning, got bogus raw line"); return; } - state_str=itoa(state); + state_str = itoa(state); -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_FEATURE_IPV6 # define notnull(A) (((A.sin6_family == AF_INET6) && \ ((A.sin6_addr.s6_addr32[0]) || \ (A.sin6_addr.s6_addr32[1]) || \ @@ -347,20 +334,19 @@ static void raw_do_one(int lnr, const char *line) #else # define notnull(A) (A.sin_addr.s_addr) #endif - if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) || - (!notnull(remaddr) && (flags&NETSTAT_LISTENING))) - { + 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); + "raw", flags & NETSTAT_NUMERIC); snprint_ip_port(rem_addr, sizeof(rem_addr), (struct sockaddr *) &remaddr, rem_port, - "raw", flags&NETSTAT_NUMERIC); + "raw", flags & NETSTAT_NUMERIC); printf("raw %6ld %6ld %-23s %-23s %-12s\n", rxq, txq, local_addr, rem_addr, state_str); - } } @@ -390,12 +376,12 @@ static void unix_do_one(int nr, const char *line) if (!(has & HAS_INODE)) snprintf(path,sizeof(path),"%d",inode); - if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))!=(NETSTAT_LISTENING|NETSTAT_CONNECTED)) { + if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) { if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) { - if (!(flags&NETSTAT_LISTENING)) + if (!(flags & NETSTAT_LISTENING)) return; } else { - if (!(flags&NETSTAT_CONNECTED)) + if (!(flags & NETSTAT_CONNECTED)) return; } } @@ -502,20 +488,20 @@ static void do_info(const char *file, const char *name, void (*proc)(int, const procinfo = fopen(file, "r"); if (procinfo == NULL) { if (errno != ENOENT) { - perror(file); + bb_perror_msg("%s", file); } else { bb_error_msg("no support for '%s' on this system", name); } - } else { - do { - char *buffer = xmalloc_fgets(procinfo); - if (buffer) { - (proc)(lnr++, buffer); - free(buffer); - } - } while (!feof(procinfo)); - fclose(procinfo); + return; } + do { + char *buffer = xmalloc_fgets(procinfo); + if (buffer) { + (proc)(lnr++, buffer); + free(buffer); + } + } while (!feof(procinfo)); + fclose(procinfo); } /* @@ -529,12 +515,11 @@ int netstat_main(int argc, char **argv) OPT_showroute = 0x100, }; unsigned opt; -#ifdef CONFIG_FEATURE_IPV6 +#if ENABLE_FEATURE_IPV6 int inet = 1; int inet6 = 1; #else -# define inet 1 -# define inet6 0 + enum { inet = 1, inet6 = 0 }; #endif /* Option string must match NETSTAT_xxx constants */ @@ -551,8 +536,8 @@ int netstat_main(int argc, char **argv) //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)); +#if ENABLE_ROUTE + bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended)); return 0; #else bb_error_msg_and_die("-r (display routing table) is not compiled in"); @@ -567,47 +552,42 @@ int netstat_main(int argc, char **argv) if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) { printf("Active Internet connections "); /* xxx */ - if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED)) + if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) printf("(servers and established)"); - else { - if (flags & NETSTAT_LISTENING) - printf("(only servers)"); - else - printf("(w/o servers)"); - } + else 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 (inet && flags&NETSTAT_TCP) - do_info(_PATH_PROCNET_TCP,"AF INET (tcp)",tcp_do_one); -#ifdef CONFIG_FEATURE_IPV6 - if (inet6 && flags&NETSTAT_TCP) - do_info(_PATH_PROCNET_TCP6,"AF INET6 (tcp)",tcp_do_one); + if (inet && flags & NETSTAT_TCP) + do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one); +#if ENABLE_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); -#ifdef CONFIG_FEATURE_IPV6 - if (inet6 && flags&NETSTAT_UDP) - do_info(_PATH_PROCNET_UDP6,"AF INET6 (udp)",udp_do_one); + if (inet && flags & NETSTAT_UDP) + do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one); +#if ENABLE_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); + if (inet && flags & NETSTAT_RAW) + do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one); +#if ENABLE_FEATURE_IPV6 + if (inet6 && flags & NETSTAT_RAW) + do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one); #endif - if (flags&NETSTAT_UNIX) { + if (flags & NETSTAT_UNIX) { printf("Active UNIX domain sockets "); - if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED)) + if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) printf("(servers and established)"); - else { - if (flags&NETSTAT_LISTENING) - printf("(only servers)"); - else - printf("(w/o servers)"); - } - + else if (flags & NETSTAT_LISTENING) + printf("(only servers)"); + else + printf("(w/o servers)"); printf("\nProto RefCnt Flags Type State I-Node Path\n"); - do_info(_PATH_PROCNET_UNIX,"AF UNIX",unix_do_one); + do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one); } return 0; } |