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 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 14 | #include "busybox.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 | |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 17 | #ifdef CONFIG_ROUTE |
| 18 | extern void displayroutes(int noresolve, int netstatfmt); |
| 19 | #endif |
| 20 | |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 21 | #define NETSTAT_CONNECTED 0x01 |
| 22 | #define NETSTAT_LISTENING 0x02 |
| 23 | #define NETSTAT_NUMERIC 0x04 |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame^] | 24 | /* Must match getopt32 option string */ |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 25 | #define NETSTAT_TCP 0x10 |
| 26 | #define NETSTAT_UDP 0x20 |
| 27 | #define NETSTAT_RAW 0x40 |
| 28 | #define NETSTAT_UNIX 0x80 |
| 29 | #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 31 | static int flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 32 | |
| 33 | #define PROGNAME_WIDTHs PROGNAME_WIDTH1(PROGNAME_WIDTH) |
| 34 | #define PROGNAME_WIDTH1(s) PROGNAME_WIDTH2(s) |
| 35 | #define PROGNAME_WIDTH2(s) #s |
| 36 | |
| 37 | #define PRG_HASH_SIZE 211 |
| 38 | |
| 39 | enum { |
| 40 | TCP_ESTABLISHED = 1, |
| 41 | TCP_SYN_SENT, |
| 42 | TCP_SYN_RECV, |
| 43 | TCP_FIN_WAIT1, |
| 44 | TCP_FIN_WAIT2, |
| 45 | TCP_TIME_WAIT, |
| 46 | TCP_CLOSE, |
| 47 | TCP_CLOSE_WAIT, |
| 48 | TCP_LAST_ACK, |
| 49 | TCP_LISTEN, |
| 50 | TCP_CLOSING /* now a valid state */ |
| 51 | }; |
| 52 | |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 53 | static const char * const tcp_state[] = |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 54 | { |
| 55 | "", |
| 56 | "ESTABLISHED", |
| 57 | "SYN_SENT", |
| 58 | "SYN_RECV", |
| 59 | "FIN_WAIT1", |
| 60 | "FIN_WAIT2", |
| 61 | "TIME_WAIT", |
| 62 | "CLOSE", |
| 63 | "CLOSE_WAIT", |
| 64 | "LAST_ACK", |
| 65 | "LISTEN", |
| 66 | "CLOSING" |
| 67 | }; |
| 68 | |
| 69 | typedef enum { |
| 70 | SS_FREE = 0, /* not allocated */ |
| 71 | SS_UNCONNECTED, /* unconnected to any socket */ |
| 72 | SS_CONNECTING, /* in process of connecting */ |
| 73 | SS_CONNECTED, /* connected to socket */ |
| 74 | SS_DISCONNECTING /* in process of disconnecting */ |
| 75 | } socket_state; |
| 76 | |
| 77 | #define SO_ACCEPTCON (1<<16) /* performed a listen */ |
| 78 | #define SO_WAITDATA (1<<17) /* wait data to read */ |
| 79 | #define SO_NOSPACE (1<<18) /* no space to write */ |
| 80 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 81 | static char *get_sname(int port, const char *proto, int num) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 82 | { |
| 83 | char *str=itoa(ntohs(port)); |
| 84 | if (num) { |
| 85 | } else { |
| 86 | struct servent *se=getservbyport(port,proto); |
| 87 | if (se) |
| 88 | str=se->s_name; |
| 89 | } |
| 90 | if (!port) { |
| 91 | str="*"; |
| 92 | } |
| 93 | return str; |
| 94 | } |
| 95 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 96 | static void snprint_ip_port(char *ip_port, int size, struct sockaddr *addr, int port, char *proto, int numeric) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 97 | { |
| 98 | char *port_name; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 99 | |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 100 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 101 | if (addr->sa_family == AF_INET6) { |
| 102 | INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr, |
| 103 | (numeric&NETSTAT_NUMERIC) ? 0x0fff : 0); |
| 104 | } else |
| 105 | #endif |
| 106 | { |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 107 | INET_rresolve(ip_port, size, (struct sockaddr_in *)addr, |
| 108 | 0x4000 | ((numeric&NETSTAT_NUMERIC) ? 0x0fff : 0), |
| 109 | 0xffffffff); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 110 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 111 | port_name=get_sname(htons(port), proto, numeric); |
| 112 | if ((strlen(ip_port) + strlen(port_name)) > 22) |
| 113 | ip_port[22 - strlen(port_name)] = '\0'; |
| 114 | ip_port+=strlen(ip_port); |
| 115 | strcat(ip_port, ":"); |
| 116 | strcat(ip_port, port_name); |
| 117 | } |
| 118 | |
| 119 | static void tcp_do_one(int lnr, const char *line) |
| 120 | { |
| 121 | char local_addr[64], rem_addr[64]; |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 122 | const char *state_str; |
| 123 | char more[512]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 124 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 125 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 126 | struct sockaddr_in6 localaddr, remaddr; |
| 127 | char addr6[INET6_ADDRSTRLEN]; |
| 128 | struct in6_addr in6; |
| 129 | #else |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 130 | struct sockaddr_in localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 131 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 132 | unsigned long rxq, txq, time_len, retr, inode; |
| 133 | |
| 134 | if (lnr == 0) |
| 135 | return; |
| 136 | |
| 137 | more[0] = '\0'; |
| 138 | num = sscanf(line, |
| 139 | "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n", |
| 140 | &d, local_addr, &local_port, |
| 141 | rem_addr, &rem_port, &state, |
| 142 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
| 143 | |
| 144 | if (strlen(local_addr) > 8) { |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 145 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 146 | sscanf(local_addr, "%08X%08X%08X%08X", |
| 147 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 148 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 149 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
| 150 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr.sin6_addr); |
| 151 | sscanf(rem_addr, "%08X%08X%08X%08X", |
| 152 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 153 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 154 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
| 155 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &remaddr.sin6_addr); |
| 156 | localaddr.sin6_family = AF_INET6; |
| 157 | remaddr.sin6_family = AF_INET6; |
| 158 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 159 | } else { |
| 160 | sscanf(local_addr, "%X", |
| 161 | &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr); |
| 162 | sscanf(rem_addr, "%X", |
| 163 | &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr); |
| 164 | ((struct sockaddr *) &localaddr)->sa_family = AF_INET; |
| 165 | ((struct sockaddr *) &remaddr)->sa_family = AF_INET; |
| 166 | } |
| 167 | |
| 168 | if (num < 10) { |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 169 | bb_error_msg("warning, got bogus tcp line"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 170 | return; |
| 171 | } |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 172 | state_str = tcp_state[state]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 173 | if ((rem_port && (flags&NETSTAT_CONNECTED)) || |
| 174 | (!rem_port && (flags&NETSTAT_LISTENING))) |
| 175 | { |
| 176 | snprint_ip_port(local_addr, sizeof(local_addr), |
| 177 | (struct sockaddr *) &localaddr, local_port, |
| 178 | "tcp", flags&NETSTAT_NUMERIC); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 179 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 180 | snprint_ip_port(rem_addr, sizeof(rem_addr), |
| 181 | (struct sockaddr *) &remaddr, rem_port, |
| 182 | "tcp", flags&NETSTAT_NUMERIC); |
| 183 | |
| 184 | printf("tcp %6ld %6ld %-23s %-23s %-12s\n", |
| 185 | rxq, txq, local_addr, rem_addr, state_str); |
| 186 | |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static void udp_do_one(int lnr, const char *line) |
| 191 | { |
| 192 | char local_addr[64], rem_addr[64]; |
| 193 | char *state_str, more[512]; |
| 194 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 195 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 196 | struct sockaddr_in6 localaddr, remaddr; |
| 197 | char addr6[INET6_ADDRSTRLEN]; |
| 198 | struct in6_addr in6; |
| 199 | #else |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 200 | struct sockaddr_in localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 201 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 202 | unsigned long rxq, txq, time_len, retr, inode; |
| 203 | |
| 204 | if (lnr == 0) |
| 205 | return; |
| 206 | |
| 207 | more[0] = '\0'; |
| 208 | num = sscanf(line, |
| 209 | "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n", |
| 210 | &d, local_addr, &local_port, |
| 211 | rem_addr, &rem_port, &state, |
| 212 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
| 213 | |
| 214 | if (strlen(local_addr) > 8) { |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 215 | #ifdef CONFIG_FEATURE_IPV6 |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 216 | /* Demangle what the kernel gives us */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 217 | sscanf(local_addr, "%08X%08X%08X%08X", |
| 218 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 219 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 220 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
| 221 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr.sin6_addr); |
| 222 | sscanf(rem_addr, "%08X%08X%08X%08X", |
| 223 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 224 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 225 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
| 226 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &remaddr.sin6_addr); |
| 227 | localaddr.sin6_family = AF_INET6; |
| 228 | remaddr.sin6_family = AF_INET6; |
| 229 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 230 | } else { |
| 231 | sscanf(local_addr, "%X", |
| 232 | &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr); |
| 233 | sscanf(rem_addr, "%X", |
| 234 | &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr); |
| 235 | ((struct sockaddr *) &localaddr)->sa_family = AF_INET; |
| 236 | ((struct sockaddr *) &remaddr)->sa_family = AF_INET; |
| 237 | } |
| 238 | |
| 239 | if (num < 10) { |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 240 | bb_error_msg("warning, got bogus udp line"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | switch (state) { |
| 244 | case TCP_ESTABLISHED: |
| 245 | state_str = "ESTABLISHED"; |
| 246 | break; |
| 247 | |
| 248 | case TCP_CLOSE: |
| 249 | state_str = ""; |
| 250 | break; |
| 251 | |
| 252 | default: |
| 253 | state_str = "UNKNOWN"; |
| 254 | break; |
| 255 | } |
| 256 | |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 257 | #ifdef CONFIG_FEATURE_IPV6 |
| 258 | # define notnull(A) (((A.sin6_family == AF_INET6) && \ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 259 | ((A.sin6_addr.s6_addr32[0]) || \ |
| 260 | (A.sin6_addr.s6_addr32[1]) || \ |
| 261 | (A.sin6_addr.s6_addr32[2]) || \ |
| 262 | (A.sin6_addr.s6_addr32[3]))) || \ |
| 263 | ((A.sin6_family == AF_INET) && \ |
| 264 | ((struct sockaddr_in *) &A)->sin_addr.s_addr)) |
| 265 | #else |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 266 | # define notnull(A) (A.sin_addr.s_addr) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 267 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 268 | if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) || |
| 269 | (!notnull(remaddr) && (flags&NETSTAT_LISTENING))) |
| 270 | { |
| 271 | snprint_ip_port(local_addr, sizeof(local_addr), |
| 272 | (struct sockaddr *) &localaddr, local_port, |
| 273 | "udp", flags&NETSTAT_NUMERIC); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 274 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 275 | snprint_ip_port(rem_addr, sizeof(rem_addr), |
| 276 | (struct sockaddr *) &remaddr, rem_port, |
| 277 | "udp", flags&NETSTAT_NUMERIC); |
| 278 | |
| 279 | printf("udp %6ld %6ld %-23s %-23s %-12s\n", |
| 280 | rxq, txq, local_addr, rem_addr, state_str); |
| 281 | |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static void raw_do_one(int lnr, const char *line) |
| 286 | { |
| 287 | char local_addr[64], rem_addr[64]; |
| 288 | char *state_str, more[512]; |
| 289 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 290 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 291 | struct sockaddr_in6 localaddr, remaddr; |
| 292 | char addr6[INET6_ADDRSTRLEN]; |
| 293 | struct in6_addr in6; |
| 294 | #else |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 295 | struct sockaddr_in localaddr, remaddr; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 296 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 297 | unsigned long rxq, txq, time_len, retr, inode; |
| 298 | |
| 299 | if (lnr == 0) |
| 300 | return; |
| 301 | |
| 302 | more[0] = '\0'; |
| 303 | num = sscanf(line, |
| 304 | "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n", |
| 305 | &d, local_addr, &local_port, |
| 306 | rem_addr, &rem_port, &state, |
| 307 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
| 308 | |
| 309 | if (strlen(local_addr) > 8) { |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 310 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 311 | sscanf(local_addr, "%08X%08X%08X%08X", |
| 312 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 313 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 314 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
| 315 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr.sin6_addr); |
| 316 | sscanf(rem_addr, "%08X%08X%08X%08X", |
| 317 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
| 318 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
| 319 | inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6)); |
| 320 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &remaddr.sin6_addr); |
| 321 | localaddr.sin6_family = AF_INET6; |
| 322 | remaddr.sin6_family = AF_INET6; |
| 323 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 324 | } else { |
| 325 | sscanf(local_addr, "%X", |
| 326 | &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr); |
| 327 | sscanf(rem_addr, "%X", |
| 328 | &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr); |
| 329 | ((struct sockaddr *) &localaddr)->sa_family = AF_INET; |
| 330 | ((struct sockaddr *) &remaddr)->sa_family = AF_INET; |
| 331 | } |
| 332 | |
| 333 | if (num < 10) { |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 334 | bb_error_msg("warning, got bogus raw line"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | state_str=itoa(state); |
| 338 | |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 339 | #ifdef CONFIG_FEATURE_IPV6 |
| 340 | # define notnull(A) (((A.sin6_family == AF_INET6) && \ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 341 | ((A.sin6_addr.s6_addr32[0]) || \ |
| 342 | (A.sin6_addr.s6_addr32[1]) || \ |
| 343 | (A.sin6_addr.s6_addr32[2]) || \ |
| 344 | (A.sin6_addr.s6_addr32[3]))) || \ |
| 345 | ((A.sin6_family == AF_INET) && \ |
| 346 | ((struct sockaddr_in *) &A)->sin_addr.s_addr)) |
| 347 | #else |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 348 | # define notnull(A) (A.sin_addr.s_addr) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 349 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 350 | if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) || |
| 351 | (!notnull(remaddr) && (flags&NETSTAT_LISTENING))) |
| 352 | { |
| 353 | snprint_ip_port(local_addr, sizeof(local_addr), |
| 354 | (struct sockaddr *) &localaddr, local_port, |
| 355 | "raw", flags&NETSTAT_NUMERIC); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 356 | |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 357 | snprint_ip_port(rem_addr, sizeof(rem_addr), |
| 358 | (struct sockaddr *) &remaddr, rem_port, |
| 359 | "raw", flags&NETSTAT_NUMERIC); |
| 360 | |
| 361 | printf("raw %6ld %6ld %-23s %-23s %-12s\n", |
| 362 | rxq, txq, local_addr, rem_addr, state_str); |
| 363 | |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | #define HAS_INODE 1 |
| 368 | |
| 369 | static void unix_do_one(int nr, const char *line) |
| 370 | { |
| 371 | static int has = 0; |
| 372 | char path[PATH_MAX], ss_flags[32]; |
| 373 | char *ss_proto, *ss_state, *ss_type; |
| 374 | int num, state, type, inode; |
| 375 | void *d; |
| 376 | unsigned long refcnt, proto, unix_flags; |
| 377 | |
| 378 | if (nr == 0) { |
| 379 | if (strstr(line, "Inode")) |
| 380 | has |= HAS_INODE; |
| 381 | return; |
| 382 | } |
| 383 | path[0] = '\0'; |
| 384 | num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s", |
| 385 | &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path); |
| 386 | if (num < 6) { |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 387 | bb_error_msg("warning, got bogus unix line"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | if (!(has & HAS_INODE)) |
| 391 | snprintf(path,sizeof(path),"%d",inode); |
| 392 | |
| 393 | if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))!=(NETSTAT_LISTENING|NETSTAT_CONNECTED)) { |
| 394 | if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) { |
| 395 | if (!(flags&NETSTAT_LISTENING)) |
| 396 | return; |
| 397 | } else { |
| 398 | if (!(flags&NETSTAT_CONNECTED)) |
| 399 | return; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | switch (proto) { |
| 404 | case 0: |
| 405 | ss_proto = "unix"; |
| 406 | break; |
| 407 | |
| 408 | default: |
| 409 | ss_proto = "??"; |
| 410 | } |
| 411 | |
| 412 | switch (type) { |
| 413 | case SOCK_STREAM: |
| 414 | ss_type = "STREAM"; |
| 415 | break; |
| 416 | |
| 417 | case SOCK_DGRAM: |
| 418 | ss_type = "DGRAM"; |
| 419 | break; |
| 420 | |
| 421 | case SOCK_RAW: |
| 422 | ss_type = "RAW"; |
| 423 | break; |
| 424 | |
| 425 | case SOCK_RDM: |
| 426 | ss_type = "RDM"; |
| 427 | break; |
| 428 | |
| 429 | case SOCK_SEQPACKET: |
| 430 | ss_type = "SEQPACKET"; |
| 431 | break; |
| 432 | |
| 433 | default: |
| 434 | ss_type = "UNKNOWN"; |
| 435 | } |
| 436 | |
| 437 | switch (state) { |
| 438 | case SS_FREE: |
| 439 | ss_state = "FREE"; |
| 440 | break; |
| 441 | |
| 442 | case SS_UNCONNECTED: |
| 443 | /* |
| 444 | * Unconnected sockets may be listening |
| 445 | * for something. |
| 446 | */ |
| 447 | if (unix_flags & SO_ACCEPTCON) { |
| 448 | ss_state = "LISTENING"; |
| 449 | } else { |
| 450 | ss_state = ""; |
| 451 | } |
| 452 | break; |
| 453 | |
| 454 | case SS_CONNECTING: |
| 455 | ss_state = "CONNECTING"; |
| 456 | break; |
| 457 | |
| 458 | case SS_CONNECTED: |
| 459 | ss_state = "CONNECTED"; |
| 460 | break; |
| 461 | |
| 462 | case SS_DISCONNECTING: |
| 463 | ss_state = "DISCONNECTING"; |
| 464 | break; |
| 465 | |
| 466 | default: |
| 467 | ss_state = "UNKNOWN"; |
| 468 | } |
| 469 | |
| 470 | strcpy(ss_flags, "[ "); |
| 471 | if (unix_flags & SO_ACCEPTCON) |
| 472 | strcat(ss_flags, "ACC "); |
| 473 | if (unix_flags & SO_WAITDATA) |
| 474 | strcat(ss_flags, "W "); |
| 475 | if (unix_flags & SO_NOSPACE) |
| 476 | strcat(ss_flags, "N "); |
| 477 | |
| 478 | strcat(ss_flags, "]"); |
| 479 | |
| 480 | printf("%-5s %-6ld %-11s %-10s %-13s ", |
| 481 | ss_proto, refcnt, ss_flags, ss_type, ss_state); |
| 482 | if (has & HAS_INODE) |
| 483 | printf("%-6d ",inode); |
| 484 | else |
| 485 | printf("- "); |
| 486 | puts(path); |
| 487 | } |
| 488 | |
| 489 | #define _PATH_PROCNET_UDP "/proc/net/udp" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 490 | #define _PATH_PROCNET_UDP6 "/proc/net/udp6" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 491 | #define _PATH_PROCNET_TCP "/proc/net/tcp" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 492 | #define _PATH_PROCNET_TCP6 "/proc/net/tcp6" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 493 | #define _PATH_PROCNET_RAW "/proc/net/raw" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 494 | #define _PATH_PROCNET_RAW6 "/proc/net/raw6" |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 495 | #define _PATH_PROCNET_UNIX "/proc/net/unix" |
| 496 | |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 497 | static void do_info(const char *file, const char *name, void (*proc)(int, const char *)) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 498 | { |
| 499 | char buffer[8192]; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 500 | int lnr = 0; |
| 501 | FILE *procinfo; |
| 502 | |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 503 | procinfo = fopen(file, "r"); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 504 | if (procinfo == NULL) { |
| 505 | if (errno != ENOENT) { |
Eric Andersen | b0c39a8 | 2002-06-22 17:32:58 +0000 | [diff] [blame] | 506 | perror(file); |
| 507 | } else { |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 508 | bb_error_msg("no support for `%s' on this system", name); |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 509 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 510 | } else { |
| 511 | do { |
| 512 | if (fgets(buffer, sizeof(buffer), procinfo)) |
| 513 | (proc)(lnr++, buffer); |
| 514 | } while (!feof(procinfo)); |
| 515 | fclose(procinfo); |
| 516 | } |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Our main function. |
| 521 | */ |
| 522 | |
| 523 | int netstat_main(int argc, char **argv) |
| 524 | { |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 525 | enum { |
| 526 | OPT_extended = 0x4, |
| 527 | OPT_showroute = 0x100, |
| 528 | }; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame^] | 529 | unsigned opt; |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 530 | #ifdef CONFIG_FEATURE_IPV6 |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 531 | int inet = 1; |
| 532 | int inet6 = 1; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 533 | #else |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 534 | # define inet 1 |
| 535 | # define inet6 0 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 536 | #endif |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 537 | |
| 538 | /* Option string must match NETSTAT_xxx constants */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame^] | 539 | opt = getopt32(argc, argv, "laentuwxr"); |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 540 | if (opt & 0x1) { // -l |
| 541 | flags &= ~NETSTAT_CONNECTED; |
| 542 | flags |= NETSTAT_LISTENING; |
| 543 | } |
| 544 | if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a |
| 545 | //if (opt & 0x4) // -e |
| 546 | if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n |
| 547 | //if (opt & 0x10) // -t: NETSTAT_TCP |
| 548 | //if (opt & 0x20) // -u: NETSTAT_UDP |
| 549 | //if (opt & 0x40) // -w: NETSTAT_RAW |
| 550 | //if (opt & 0x80) // -x: NETSTAT_UNIX |
| 551 | if (opt & OPT_showroute) { // -r |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 552 | #ifdef CONFIG_ROUTE |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 553 | displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 554 | return 0; |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 555 | #else |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 556 | bb_error_msg_and_die("-r (display routing table) is not compiled in"); |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 557 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 560 | opt &= NETSTAT_ALLPROTO; |
| 561 | if (opt) { |
| 562 | flags &= ~NETSTAT_ALLPROTO; |
| 563 | flags |= opt; |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 564 | } |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 565 | if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) { |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 566 | printf("Active Internet connections "); /* xxx */ |
| 567 | |
| 568 | if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
| 569 | printf("(servers and established)"); |
| 570 | else { |
Denis Vlasenko | 754a88f | 2006-09-22 16:02:40 +0000 | [diff] [blame] | 571 | if (flags & NETSTAT_LISTENING) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 572 | printf("(only servers)"); |
| 573 | else |
| 574 | printf("(w/o servers)"); |
| 575 | } |
| 576 | printf("\nProto Recv-Q Send-Q Local Address Foreign Address State \n"); |
| 577 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 578 | if (inet && flags&NETSTAT_TCP) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 579 | do_info(_PATH_PROCNET_TCP,"AF INET (tcp)",tcp_do_one); |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 580 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 581 | if (inet6 && flags&NETSTAT_TCP) |
| 582 | do_info(_PATH_PROCNET_TCP6,"AF INET6 (tcp)",tcp_do_one); |
| 583 | #endif |
| 584 | if (inet && flags&NETSTAT_UDP) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 585 | do_info(_PATH_PROCNET_UDP,"AF INET (udp)",udp_do_one); |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 586 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 587 | if (inet6 && flags&NETSTAT_UDP) |
| 588 | do_info(_PATH_PROCNET_UDP6,"AF INET6 (udp)",udp_do_one); |
| 589 | #endif |
| 590 | if (inet && flags&NETSTAT_RAW) |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 591 | do_info(_PATH_PROCNET_RAW,"AF INET (raw)",raw_do_one); |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 592 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 593 | if (inet6 && flags&NETSTAT_RAW) |
| 594 | do_info(_PATH_PROCNET_RAW6,"AF INET6 (raw)",raw_do_one); |
| 595 | #endif |
Eric Andersen | 31a0ece | 2001-10-31 11:00:46 +0000 | [diff] [blame] | 596 | if (flags&NETSTAT_UNIX) { |
| 597 | printf("Active UNIX domain sockets "); |
| 598 | if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
| 599 | printf("(servers and established)"); |
| 600 | else { |
| 601 | if (flags&NETSTAT_LISTENING) |
| 602 | printf("(only servers)"); |
| 603 | else |
| 604 | printf("(w/o servers)"); |
| 605 | } |
| 606 | |
| 607 | printf("\nProto RefCnt Flags Type State I-Node Path\n"); |
| 608 | do_info(_PATH_PROCNET_UNIX,"AF UNIX",unix_do_one); |
| 609 | } |
| 610 | return 0; |
| 611 | } |