Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 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 | * |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 6 | * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com> |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 8 | * 2002-04-20 |
| 9 | * IPV6 support added by Bart Visscher <magick@linux-fan.com> |
"Robert P. J. Day" | 2819f75 | 2006-07-11 11:32:31 +0000 | [diff] [blame] | 10 | * |
| 11 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 15 | #include "inet_common.h" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 17 | enum { |
| 18 | OPT_extended = 0x4, |
| 19 | OPT_showroute = 0x100, |
| 20 | OPT_widedisplay = 0x200 * ENABLE_FEATURE_NETSTAT_WIDE, |
| 21 | }; |
| 22 | # define NETSTAT_OPTS "laentuwxr"USE_FEATURE_NETSTAT_WIDE("W") |
| 23 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 24 | #define NETSTAT_CONNECTED 0x01 |
| 25 | #define NETSTAT_LISTENING 0x02 |
| 26 | #define NETSTAT_NUMERIC 0x04 |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 27 | /* Must match getopt32 option string */ |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 28 | #define NETSTAT_TCP 0x10 |
| 29 | #define NETSTAT_UDP 0x20 |
| 30 | #define NETSTAT_RAW 0x40 |
| 31 | #define NETSTAT_UNIX 0x80 |
| 32 | #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 34 | static smallint flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 36 | enum { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 37 | TCP_ESTABLISHED = 1, |
| 38 | TCP_SYN_SENT, |
| 39 | TCP_SYN_RECV, |
| 40 | TCP_FIN_WAIT1, |
| 41 | TCP_FIN_WAIT2, |
| 42 | TCP_TIME_WAIT, |
| 43 | TCP_CLOSE, |
| 44 | TCP_CLOSE_WAIT, |
| 45 | TCP_LAST_ACK, |
| 46 | TCP_LISTEN, |
| 47 | TCP_CLOSING /* now a valid state */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 50 | static const char *const tcp_state[] = { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 51 | "", |
| 52 | "ESTABLISHED", |
| 53 | "SYN_SENT", |
| 54 | "SYN_RECV", |
| 55 | "FIN_WAIT1", |
| 56 | "FIN_WAIT2", |
| 57 | "TIME_WAIT", |
| 58 | "CLOSE", |
| 59 | "CLOSE_WAIT", |
| 60 | "LAST_ACK", |
| 61 | "LISTEN", |
| 62 | "CLOSING" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | typedef enum { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 66 | SS_FREE = 0, /* not allocated */ |
| 67 | SS_UNCONNECTED, /* unconnected to any socket */ |
| 68 | SS_CONNECTING, /* in process of connecting */ |
| 69 | SS_CONNECTED, /* connected to socket */ |
| 70 | SS_DISCONNECTING /* in process of disconnecting */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 71 | } socket_state; |
| 72 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 73 | #define SO_ACCEPTCON (1<<16) /* performed a listen */ |
| 74 | #define SO_WAITDATA (1<<17) /* wait data to read */ |
| 75 | #define SO_NOSPACE (1<<18) /* no space to write */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 77 | /* Standard printout size */ |
| 78 | #define PRINT_IP_MAX_SIZE 23 |
| 79 | #define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s\n" |
| 80 | #define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State\n" |
| 81 | |
| 82 | /* When there are IPv6 connections the IPv6 addresses will be |
| 83 | * truncated to none-recognition. The '-W' option makes the |
| 84 | * address columns wide enough to accomodate for longest possible |
| 85 | * IPv6 addresses, i.e. addresses of the form |
| 86 | * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd |
| 87 | */ |
| 88 | #define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */ |
| 89 | #define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s\n" |
| 90 | #define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State\n" |
| 91 | |
| 92 | static const char *net_conn_line = PRINT_NET_CONN; |
| 93 | |
| 94 | |
| 95 | #if ENABLE_FEATURE_IPV6 |
| 96 | static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr) |
| 97 | { |
| 98 | char addr6[INET6_ADDRSTRLEN]; |
| 99 | struct in6_addr in6; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 101 | sscanf(local_addr, "%08X%08X%08X%08X", |
| 102 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 103 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 104 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
| 105 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr->sin6_addr); |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 107 | localaddr->sin6_family = AF_INET6; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | #if ENABLE_FEATURE_IPV6 |
| 112 | static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr) |
| 113 | #else |
| 114 | static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr) |
| 115 | #endif |
| 116 | { |
| 117 | sscanf(local_addr, "%X", |
| 118 | &((struct sockaddr_in *) localaddr)->sin_addr.s_addr); |
| 119 | ((struct sockaddr *) localaddr)->sa_family = AF_INET; |
| 120 | } |
| 121 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 122 | static const char *get_sname(int port, const char *proto, int numeric) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 123 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 124 | if (!port) |
| 125 | return "*"; |
| 126 | if (!numeric) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 127 | struct servent *se = getservbyport(port, proto); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 128 | if (se) |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 129 | return se->s_name; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 130 | } |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 131 | /* hummm, we may return static buffer here!! */ |
| 132 | return itoa(ntohs(port)); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 135 | static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 136 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 137 | enum { salen = USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) }; |
| 138 | char *host, *host_port; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 140 | /* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6, |
| 141 | * while "0.0.0.0" is not. */ |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 143 | host = numeric ? xmalloc_sockaddr2dotted_noport(addr) |
| 144 | : xmalloc_sockaddr2host_noport(addr); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 145 | |
| 146 | host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric)); |
| 147 | free(host); |
| 148 | return host_port; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 151 | static int tcp_do_one(int lnr, char *line) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 152 | { |
| 153 | char local_addr[64], rem_addr[64]; |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 154 | char more[512]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 155 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 156 | #if ENABLE_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 157 | struct sockaddr_in6 localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 158 | #else |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 159 | struct sockaddr_in localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 160 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 161 | unsigned long rxq, txq, time_len, retr, inode; |
| 162 | |
| 163 | if (lnr == 0) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 164 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 165 | |
| 166 | more[0] = '\0'; |
| 167 | num = sscanf(line, |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 168 | "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n", |
| 169 | &d, local_addr, &local_port, |
| 170 | rem_addr, &rem_port, &state, |
| 171 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 172 | |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 173 | if (num < 10) { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 174 | return 1; /* error */ |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 177 | if (strlen(local_addr) > 8) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 178 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 179 | build_ipv6_addr(local_addr, &localaddr); |
| 180 | build_ipv6_addr(rem_addr, &remaddr); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 181 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 182 | } else { |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 183 | build_ipv4_addr(local_addr, &localaddr); |
| 184 | build_ipv4_addr(rem_addr, &remaddr); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 187 | if ((rem_port && (flags & NETSTAT_CONNECTED)) |
| 188 | || (!rem_port && (flags & NETSTAT_LISTENING)) |
| 189 | ) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 190 | char *l = ip_port_str( |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 191 | (struct sockaddr *) &localaddr, local_port, |
| 192 | "tcp", flags & NETSTAT_NUMERIC); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 193 | char *r = ip_port_str( |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 194 | (struct sockaddr *) &remaddr, rem_port, |
| 195 | "tcp", flags & NETSTAT_NUMERIC); |
| 196 | printf(net_conn_line, |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 197 | "tcp", rxq, txq, l, r, tcp_state[state]); |
| 198 | free(l); |
| 199 | free(r); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 200 | } |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 201 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 204 | static int udp_do_one(int lnr, char *line) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 205 | { |
| 206 | char local_addr[64], rem_addr[64]; |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 207 | const char *state_str; |
| 208 | char more[512]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 209 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 210 | #if ENABLE_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 211 | struct sockaddr_in6 localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 212 | #else |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 213 | struct sockaddr_in localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 214 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 215 | unsigned long rxq, txq, time_len, retr, inode; |
| 216 | |
| 217 | if (lnr == 0) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 218 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 219 | |
| 220 | more[0] = '\0'; |
| 221 | num = sscanf(line, |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 222 | "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n", |
| 223 | &d, local_addr, &local_port, |
| 224 | rem_addr, &rem_port, &state, |
| 225 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 226 | |
| 227 | if (strlen(local_addr) > 8) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 228 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 229 | /* Demangle what the kernel gives us */ |
| 230 | build_ipv6_addr(local_addr, &localaddr); |
| 231 | build_ipv6_addr(rem_addr, &remaddr); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 232 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 233 | } else { |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 234 | build_ipv4_addr(local_addr, &localaddr); |
| 235 | build_ipv4_addr(rem_addr, &remaddr); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if (num < 10) { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 239 | return 1; /* error */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 240 | } |
| 241 | switch (state) { |
| 242 | case TCP_ESTABLISHED: |
| 243 | state_str = "ESTABLISHED"; |
| 244 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 245 | case TCP_CLOSE: |
| 246 | state_str = ""; |
| 247 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 248 | default: |
| 249 | state_str = "UNKNOWN"; |
| 250 | break; |
| 251 | } |
| 252 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 253 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 254 | # define notnull(A) ( \ |
| 255 | ( (A.sin6_family == AF_INET6) \ |
| 256 | && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \ |
| 257 | A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \ |
| 258 | ) || ( \ |
| 259 | (A.sin6_family == AF_INET) \ |
| 260 | && ((struct sockaddr_in*)&A)->sin_addr.s_addr \ |
| 261 | ) \ |
| 262 | ) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 263 | #else |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 264 | # define notnull(A) (A.sin_addr.s_addr) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 265 | #endif |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 266 | { |
| 267 | int have_remaddr = notnull(remaddr); |
| 268 | if ((have_remaddr && (flags & NETSTAT_CONNECTED)) |
| 269 | || (!have_remaddr && (flags & NETSTAT_LISTENING)) |
| 270 | ) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 271 | char *l = ip_port_str( |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 272 | (struct sockaddr *) &localaddr, local_port, |
| 273 | "udp", flags & NETSTAT_NUMERIC); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 274 | char *r = ip_port_str( |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 275 | (struct sockaddr *) &remaddr, rem_port, |
| 276 | "udp", flags & NETSTAT_NUMERIC); |
| 277 | printf(net_conn_line, |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 278 | "udp", rxq, txq, l, r, state_str); |
| 279 | free(l); |
| 280 | free(r); |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 281 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 282 | } |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 283 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 286 | static int raw_do_one(int lnr, char *line) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 287 | { |
| 288 | char local_addr[64], rem_addr[64]; |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 289 | char more[512]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 290 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 291 | #if ENABLE_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 292 | struct sockaddr_in6 localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 293 | #else |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 294 | struct sockaddr_in localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 295 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 296 | unsigned long rxq, txq, time_len, retr, inode; |
| 297 | |
| 298 | if (lnr == 0) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 299 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 300 | |
| 301 | more[0] = '\0'; |
| 302 | num = sscanf(line, |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 303 | "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n", |
| 304 | &d, local_addr, &local_port, |
| 305 | rem_addr, &rem_port, &state, |
| 306 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 307 | |
| 308 | if (strlen(local_addr) > 8) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 309 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 310 | build_ipv6_addr(local_addr, &localaddr); |
| 311 | build_ipv6_addr(rem_addr, &remaddr); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 312 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 313 | } else { |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 314 | build_ipv4_addr(local_addr, &localaddr); |
| 315 | build_ipv4_addr(rem_addr, &remaddr); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if (num < 10) { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 319 | return 1; /* error */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 320 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 321 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 322 | { |
| 323 | int have_remaddr = notnull(remaddr); |
| 324 | if ((have_remaddr && (flags & NETSTAT_CONNECTED)) |
| 325 | || (!have_remaddr && (flags & NETSTAT_LISTENING)) |
| 326 | ) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 327 | char *l = ip_port_str( |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 328 | (struct sockaddr *) &localaddr, local_port, |
| 329 | "raw", flags & NETSTAT_NUMERIC); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 330 | char *r = ip_port_str( |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 331 | (struct sockaddr *) &remaddr, rem_port, |
| 332 | "raw", flags & NETSTAT_NUMERIC); |
| 333 | printf(net_conn_line, |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 334 | "raw", rxq, txq, l, r, itoa(state)); |
| 335 | free(l); |
| 336 | free(r); |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 337 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 338 | } |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 339 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 342 | static int unix_do_one(int nr, char *line) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 343 | { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 344 | unsigned long refcnt, proto, unix_flags; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 345 | unsigned long inode; |
| 346 | int type, state; |
| 347 | int num, path_ofs; |
| 348 | void *d; |
| 349 | const char *ss_proto, *ss_state, *ss_type; |
| 350 | char ss_flags[32]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 351 | |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 352 | /* TODO: currently we stop at first NUL byte. Is it a problem? */ |
| 353 | |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 354 | if (nr == 0) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 355 | return 0; /* skip header */ |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 356 | |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 357 | *strchrnul(line, '\n') = '\0'; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 358 | |
| 359 | /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..." |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 360 | * Other users report long lines filled by NUL bytes. |
| 361 | * (those ^@ are NUL bytes too). We see them as empty lines. */ |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 362 | if (!line[0]) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 363 | return 0; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 364 | |
| 365 | path_ofs = 0; /* paranoia */ |
| 366 | num = sscanf(line, "%p: %lX %lX %lX %X %X %lu %n", |
| 367 | &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs); |
| 368 | if (num < 7) { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 369 | return 1; /* error */ |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 370 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 371 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 372 | if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 373 | if (!(flags & NETSTAT_LISTENING)) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 374 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 375 | } else { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 376 | if (!(flags & NETSTAT_CONNECTED)) |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 377 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | switch (proto) { |
| 382 | case 0: |
| 383 | ss_proto = "unix"; |
| 384 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 385 | default: |
| 386 | ss_proto = "??"; |
| 387 | } |
| 388 | |
| 389 | switch (type) { |
| 390 | case SOCK_STREAM: |
| 391 | ss_type = "STREAM"; |
| 392 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 393 | case SOCK_DGRAM: |
| 394 | ss_type = "DGRAM"; |
| 395 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 396 | case SOCK_RAW: |
| 397 | ss_type = "RAW"; |
| 398 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 399 | case SOCK_RDM: |
| 400 | ss_type = "RDM"; |
| 401 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 402 | case SOCK_SEQPACKET: |
| 403 | ss_type = "SEQPACKET"; |
| 404 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 405 | default: |
| 406 | ss_type = "UNKNOWN"; |
| 407 | } |
| 408 | |
| 409 | switch (state) { |
| 410 | case SS_FREE: |
| 411 | ss_state = "FREE"; |
| 412 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 413 | case SS_UNCONNECTED: |
| 414 | /* |
| 415 | * Unconnected sockets may be listening |
| 416 | * for something. |
| 417 | */ |
| 418 | if (unix_flags & SO_ACCEPTCON) { |
| 419 | ss_state = "LISTENING"; |
| 420 | } else { |
| 421 | ss_state = ""; |
| 422 | } |
| 423 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 424 | case SS_CONNECTING: |
| 425 | ss_state = "CONNECTING"; |
| 426 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 427 | case SS_CONNECTED: |
| 428 | ss_state = "CONNECTED"; |
| 429 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 430 | case SS_DISCONNECTING: |
| 431 | ss_state = "DISCONNECTING"; |
| 432 | break; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 433 | default: |
| 434 | ss_state = "UNKNOWN"; |
| 435 | } |
| 436 | |
| 437 | strcpy(ss_flags, "[ "); |
| 438 | if (unix_flags & SO_ACCEPTCON) |
| 439 | strcat(ss_flags, "ACC "); |
| 440 | if (unix_flags & SO_WAITDATA) |
| 441 | strcat(ss_flags, "W "); |
| 442 | if (unix_flags & SO_NOSPACE) |
| 443 | strcat(ss_flags, "N "); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 444 | strcat(ss_flags, "]"); |
| 445 | |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 446 | printf("%-5s %-6ld %-11s %-10s %-13s %6lu %s\n", |
| 447 | ss_proto, refcnt, ss_flags, ss_type, ss_state, inode, |
| 448 | line + path_ofs); |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 449 | return 0; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | #define _PATH_PROCNET_UDP "/proc/net/udp" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 453 | #define _PATH_PROCNET_UDP6 "/proc/net/udp6" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 454 | #define _PATH_PROCNET_TCP "/proc/net/tcp" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 455 | #define _PATH_PROCNET_TCP6 "/proc/net/tcp6" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 456 | #define _PATH_PROCNET_RAW "/proc/net/raw" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 457 | #define _PATH_PROCNET_RAW6 "/proc/net/raw6" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 458 | #define _PATH_PROCNET_UNIX "/proc/net/unix" |
| 459 | |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 460 | static void do_info(const char *file, const char *name, int (*proc)(int, char *)) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 461 | { |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 462 | int lnr; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 463 | FILE *procinfo; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 464 | char *buffer; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 465 | |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 466 | procinfo = fopen(file, "r"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 467 | if (procinfo == NULL) { |
| 468 | if (errno != ENOENT) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 469 | bb_simple_perror_msg(file); |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 470 | } else { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 471 | bb_error_msg("no kernel support for %s", name); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 472 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 473 | return; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 474 | } |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 475 | lnr = 0; |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 476 | /* Why? because xmalloc_fgets_str doesn't stop on NULs */ |
| 477 | while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) { |
Denis Vlasenko | ddc865f | 2007-12-26 21:49:33 +0000 | [diff] [blame^] | 478 | if (proc(lnr++, buffer)) |
| 479 | bb_error_msg("%s: bogus data on line %d", file, lnr); |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 480 | free(buffer); |
| 481 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 482 | fclose(procinfo); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 485 | int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 486 | int netstat_main(int argc, char **argv) |
| 487 | { |
Denis Vlasenko | a35958d | 2007-05-16 22:25:35 +0000 | [diff] [blame] | 488 | const char *net_conn_line_header = PRINT_NET_CONN_HEADER; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 489 | unsigned opt; |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 490 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 491 | smallint inet = 1; |
| 492 | smallint inet6 = 1; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 493 | #else |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 494 | enum { inet = 1, inet6 = 0 }; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 495 | #endif |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 496 | |
| 497 | /* Option string must match NETSTAT_xxx constants */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 498 | opt = getopt32(argv, NETSTAT_OPTS); |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 499 | if (opt & 0x1) { // -l |
| 500 | flags &= ~NETSTAT_CONNECTED; |
| 501 | flags |= NETSTAT_LISTENING; |
| 502 | } |
| 503 | if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a |
| 504 | //if (opt & 0x4) // -e |
| 505 | if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n |
| 506 | //if (opt & 0x10) // -t: NETSTAT_TCP |
| 507 | //if (opt & 0x20) // -u: NETSTAT_UDP |
| 508 | //if (opt & 0x40) // -w: NETSTAT_RAW |
| 509 | //if (opt & 0x80) // -x: NETSTAT_UNIX |
| 510 | if (opt & OPT_showroute) { // -r |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 511 | #if ENABLE_ROUTE |
| 512 | bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 513 | return 0; |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 514 | #else |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 515 | bb_show_usage(); |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 516 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 519 | if (opt & OPT_widedisplay) { // -W |
| 520 | net_conn_line = PRINT_NET_CONN_WIDE; |
Denis Vlasenko | a35958d | 2007-05-16 22:25:35 +0000 | [diff] [blame] | 521 | net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE; |
Denis Vlasenko | 418a7fb | 2007-05-15 23:57:46 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 524 | opt &= NETSTAT_ALLPROTO; |
| 525 | if (opt) { |
| 526 | flags &= ~NETSTAT_ALLPROTO; |
| 527 | flags |= opt; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 528 | } |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 529 | if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 530 | printf("Active Internet connections "); /* xxx */ |
| 531 | |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 532 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 533 | printf("(servers and established)"); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 534 | else if (flags & NETSTAT_LISTENING) |
| 535 | printf("(only servers)"); |
| 536 | else |
| 537 | printf("(w/o servers)"); |
Denis Vlasenko | a35958d | 2007-05-16 22:25:35 +0000 | [diff] [blame] | 538 | printf(net_conn_line_header, "Local Address", "Foreign Address"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 539 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 540 | if (inet && flags & NETSTAT_TCP) |
| 541 | do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one); |
| 542 | #if ENABLE_FEATURE_IPV6 |
| 543 | if (inet6 && flags & NETSTAT_TCP) |
| 544 | do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 545 | #endif |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 546 | if (inet && flags & NETSTAT_UDP) |
| 547 | do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one); |
| 548 | #if ENABLE_FEATURE_IPV6 |
| 549 | if (inet6 && flags & NETSTAT_UDP) |
| 550 | do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 551 | #endif |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 552 | if (inet && flags & NETSTAT_RAW) |
| 553 | do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one); |
| 554 | #if ENABLE_FEATURE_IPV6 |
| 555 | if (inet6 && flags & NETSTAT_RAW) |
| 556 | do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 557 | #endif |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 558 | if (flags & NETSTAT_UNIX) { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 559 | printf("Active UNIX domain sockets "); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 560 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 561 | printf("(servers and established)"); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 562 | else if (flags & NETSTAT_LISTENING) |
| 563 | printf("(only servers)"); |
| 564 | else |
| 565 | printf("(w/o servers)"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 566 | printf("\nProto RefCnt Flags Type State I-Node Path\n"); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 567 | do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 568 | } |
| 569 | return 0; |
| 570 | } |