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