blob: d89d3aa5acc6993def7efac833a0a250894b5120 [file] [log] [blame]
Eric Andersen31a0ece2001-10-31 11:00:46 +00001/* 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 Andersen51b8bd62002-07-03 11:46:38 +00006 * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com>
Eric Andersen31a0ece2001-10-31 11:00:46 +00007 *
Eric Andersen51b8bd62002-07-03 11:46:38 +00008 * 2002-04-20
9 * IPV6 support added by Bart Visscher <magick@linux-fan.com>
"Robert P. J. Day"2819f752006-07-11 11:32:31 +000010 *
11 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen31a0ece2001-10-31 11:00:46 +000012 */
13
Eric Andersen31a0ece2001-10-31 11:00:46 +000014#include "busybox.h"
Rob Landleyc9c1a412006-07-12 19:17:55 +000015#include "inet_common.h"
Eric Andersen31a0ece2001-10-31 11:00:46 +000016
Denis Vlasenko703e2022007-01-22 14:12:08 +000017#define NETSTAT_CONNECTED 0x01
18#define NETSTAT_LISTENING 0x02
19#define NETSTAT_NUMERIC 0x04
Denis Vlasenko67b23e62006-10-03 21:00:06 +000020/* Must match getopt32 option string */
Denis Vlasenko703e2022007-01-22 14:12:08 +000021#define NETSTAT_TCP 0x10
22#define NETSTAT_UDP 0x20
23#define NETSTAT_RAW 0x40
24#define NETSTAT_UNIX 0x80
25#define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
Eric Andersen31a0ece2001-10-31 11:00:46 +000026
Denis Vlasenko754a88f2006-09-22 16:02:40 +000027static int flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO;
Eric Andersen31a0ece2001-10-31 11:00:46 +000028
Eric Andersen31a0ece2001-10-31 11:00:46 +000029enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000030 TCP_ESTABLISHED = 1,
31 TCP_SYN_SENT,
32 TCP_SYN_RECV,
33 TCP_FIN_WAIT1,
34 TCP_FIN_WAIT2,
35 TCP_TIME_WAIT,
36 TCP_CLOSE,
37 TCP_CLOSE_WAIT,
38 TCP_LAST_ACK,
39 TCP_LISTEN,
40 TCP_CLOSING /* now a valid state */
Eric Andersen31a0ece2001-10-31 11:00:46 +000041};
42
Eric Andersenb0c39a82002-06-22 17:32:58 +000043static const char * const tcp_state[] =
Eric Andersen31a0ece2001-10-31 11:00:46 +000044{
Denis Vlasenko703e2022007-01-22 14:12:08 +000045 "",
46 "ESTABLISHED",
47 "SYN_SENT",
48 "SYN_RECV",
49 "FIN_WAIT1",
50 "FIN_WAIT2",
51 "TIME_WAIT",
52 "CLOSE",
53 "CLOSE_WAIT",
54 "LAST_ACK",
55 "LISTEN",
56 "CLOSING"
Eric Andersen31a0ece2001-10-31 11:00:46 +000057};
58
59typedef enum {
Denis Vlasenko703e2022007-01-22 14:12:08 +000060 SS_FREE = 0, /* not allocated */
61 SS_UNCONNECTED, /* unconnected to any socket */
62 SS_CONNECTING, /* in process of connecting */
63 SS_CONNECTED, /* connected to socket */
64 SS_DISCONNECTING /* in process of disconnecting */
Eric Andersen31a0ece2001-10-31 11:00:46 +000065} socket_state;
66
Denis Vlasenko703e2022007-01-22 14:12:08 +000067#define SO_ACCEPTCON (1<<16) /* performed a listen */
68#define SO_WAITDATA (1<<17) /* wait data to read */
69#define SO_NOSPACE (1<<18) /* no space to write */
Eric Andersen31a0ece2001-10-31 11:00:46 +000070
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000071static const char *get_sname(int port, const char *proto, int num)
Eric Andersen31a0ece2001-10-31 11:00:46 +000072{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000073 /* hummm, we return static buffer here!! */
74 const char *str = itoa(ntohs(port));
Denis Vlasenko703e2022007-01-22 14:12:08 +000075 if (!num) {
76 struct servent *se = getservbyport(port, proto);
Eric Andersen31a0ece2001-10-31 11:00:46 +000077 if (se)
Denis Vlasenko703e2022007-01-22 14:12:08 +000078 str = se->s_name;
Eric Andersen31a0ece2001-10-31 11:00:46 +000079 }
80 if (!port) {
Denis Vlasenko703e2022007-01-22 14:12:08 +000081 str = "*";
Eric Andersen31a0ece2001-10-31 11:00:46 +000082 }
83 return str;
84}
85
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000086static void snprint_ip_port(char *ip_port, int size, struct sockaddr *addr, int port, const char *proto, int numeric)
Eric Andersen31a0ece2001-10-31 11:00:46 +000087{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000088 const char *port_name;
Eric Andersencd8c4362001-11-10 11:22:46 +000089
Denis Vlasenko703e2022007-01-22 14:12:08 +000090#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +000091 if (addr->sa_family == AF_INET6) {
92 INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr,
Denis Vlasenko703e2022007-01-22 14:12:08 +000093 (numeric & NETSTAT_NUMERIC) ? 0x0fff : 0);
Eric Andersen51b8bd62002-07-03 11:46:38 +000094 } else
95#endif
96 {
Denis Vlasenko703e2022007-01-22 14:12:08 +000097 INET_rresolve(ip_port, size, (struct sockaddr_in *)addr,
98 0x4000 | ((numeric & NETSTAT_NUMERIC) ? 0x0fff : 0),
99 0xffffffff);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000100 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000101 port_name = get_sname(htons(port), proto, numeric);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000102 if ((strlen(ip_port) + strlen(port_name)) > 22)
103 ip_port[22 - strlen(port_name)] = '\0';
Denis Vlasenko703e2022007-01-22 14:12:08 +0000104 ip_port += strlen(ip_port);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000105 strcat(ip_port, ":");
106 strcat(ip_port, port_name);
107}
108
109static void tcp_do_one(int lnr, const char *line)
110{
111 char local_addr[64], rem_addr[64];
Eric Andersenb0c39a82002-06-22 17:32:58 +0000112 const char *state_str;
113 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000114 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000115#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000116 struct sockaddr_in6 localaddr, remaddr;
117 char addr6[INET6_ADDRSTRLEN];
118 struct in6_addr in6;
119#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000120 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000121#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000122 unsigned long rxq, txq, time_len, retr, inode;
123
124 if (lnr == 0)
125 return;
126
127 more[0] = '\0';
128 num = sscanf(line,
129 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
130 &d, local_addr, &local_port,
131 rem_addr, &rem_port, &state,
132 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
133
134 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000135#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000136 sscanf(local_addr, "%08X%08X%08X%08X",
137 &in6.s6_addr32[0], &in6.s6_addr32[1],
138 &in6.s6_addr32[2], &in6.s6_addr32[3]);
139 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
140 inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr.sin6_addr);
141 sscanf(rem_addr, "%08X%08X%08X%08X",
142 &in6.s6_addr32[0], &in6.s6_addr32[1],
143 &in6.s6_addr32[2], &in6.s6_addr32[3]);
144 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
145 inet_pton(AF_INET6, addr6, (struct sockaddr *) &remaddr.sin6_addr);
146 localaddr.sin6_family = AF_INET6;
147 remaddr.sin6_family = AF_INET6;
148#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000149 } else {
150 sscanf(local_addr, "%X",
151 &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr);
152 sscanf(rem_addr, "%X",
153 &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr);
154 ((struct sockaddr *) &localaddr)->sa_family = AF_INET;
155 ((struct sockaddr *) &remaddr)->sa_family = AF_INET;
156 }
157
158 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000159 bb_error_msg("warning, got bogus tcp line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000160 return;
161 }
Eric Andersenb0c39a82002-06-22 17:32:58 +0000162 state_str = tcp_state[state];
Denis Vlasenko703e2022007-01-22 14:12:08 +0000163 if ((rem_port && (flags & NETSTAT_CONNECTED))
164 || (!rem_port && (flags & NETSTAT_LISTENING))
165 ) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000166 snprint_ip_port(local_addr, sizeof(local_addr),
167 (struct sockaddr *) &localaddr, local_port,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000168 "tcp", flags & NETSTAT_NUMERIC);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000169
Eric Andersen31a0ece2001-10-31 11:00:46 +0000170 snprint_ip_port(rem_addr, sizeof(rem_addr),
171 (struct sockaddr *) &remaddr, rem_port,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000172 "tcp", flags & NETSTAT_NUMERIC);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000173
174 printf("tcp %6ld %6ld %-23s %-23s %-12s\n",
175 rxq, txq, local_addr, rem_addr, state_str);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000176 }
177}
178
179static void udp_do_one(int lnr, const char *line)
180{
181 char local_addr[64], rem_addr[64];
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000182 const char *state_str;
183 char more[512];
Eric Andersen31a0ece2001-10-31 11:00:46 +0000184 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000185#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000186 struct sockaddr_in6 localaddr, remaddr;
187 char addr6[INET6_ADDRSTRLEN];
188 struct in6_addr in6;
189#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000190 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000191#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000192 unsigned long rxq, txq, time_len, retr, inode;
193
194 if (lnr == 0)
195 return;
196
197 more[0] = '\0';
198 num = sscanf(line,
199 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
200 &d, local_addr, &local_port,
201 rem_addr, &rem_port, &state,
202 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
203
204 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000205#if ENABLE_FEATURE_IPV6
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000206 /* Demangle what the kernel gives us */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000207 sscanf(local_addr, "%08X%08X%08X%08X",
208 &in6.s6_addr32[0], &in6.s6_addr32[1],
209 &in6.s6_addr32[2], &in6.s6_addr32[3]);
210 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
211 inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr.sin6_addr);
212 sscanf(rem_addr, "%08X%08X%08X%08X",
213 &in6.s6_addr32[0], &in6.s6_addr32[1],
214 &in6.s6_addr32[2], &in6.s6_addr32[3]);
215 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
216 inet_pton(AF_INET6, addr6, (struct sockaddr *) &remaddr.sin6_addr);
217 localaddr.sin6_family = AF_INET6;
218 remaddr.sin6_family = AF_INET6;
219#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000220 } else {
221 sscanf(local_addr, "%X",
222 &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr);
223 sscanf(rem_addr, "%X",
224 &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr);
225 ((struct sockaddr *) &localaddr)->sa_family = AF_INET;
226 ((struct sockaddr *) &remaddr)->sa_family = AF_INET;
227 }
228
229 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000230 bb_error_msg("warning, got bogus udp line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000231 return;
232 }
233 switch (state) {
234 case TCP_ESTABLISHED:
235 state_str = "ESTABLISHED";
236 break;
237
238 case TCP_CLOSE:
239 state_str = "";
240 break;
241
242 default:
243 state_str = "UNKNOWN";
244 break;
245 }
246
Denis Vlasenko703e2022007-01-22 14:12:08 +0000247#if ENABLE_FEATURE_IPV6
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000248# define notnull(A) (((A.sin6_family == AF_INET6) && \
Eric Andersen51b8bd62002-07-03 11:46:38 +0000249 ((A.sin6_addr.s6_addr32[0]) || \
250 (A.sin6_addr.s6_addr32[1]) || \
251 (A.sin6_addr.s6_addr32[2]) || \
252 (A.sin6_addr.s6_addr32[3]))) || \
253 ((A.sin6_family == AF_INET) && \
254 ((struct sockaddr_in *) &A)->sin_addr.s_addr))
255#else
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000256# define notnull(A) (A.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000257#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000258 if ((notnull(remaddr) && (flags & NETSTAT_CONNECTED))
259 || (!notnull(remaddr) && (flags & NETSTAT_LISTENING))
260 ) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000261 snprint_ip_port(local_addr, sizeof(local_addr),
262 (struct sockaddr *) &localaddr, local_port,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000263 "udp", flags & NETSTAT_NUMERIC);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000264
Eric Andersen31a0ece2001-10-31 11:00:46 +0000265 snprint_ip_port(rem_addr, sizeof(rem_addr),
266 (struct sockaddr *) &remaddr, rem_port,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000267 "udp", flags & NETSTAT_NUMERIC);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000268
269 printf("udp %6ld %6ld %-23s %-23s %-12s\n",
270 rxq, txq, local_addr, rem_addr, state_str);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000271 }
272}
273
274static void raw_do_one(int lnr, const char *line)
275{
276 char local_addr[64], rem_addr[64];
277 char *state_str, more[512];
278 int num, local_port, rem_port, d, state, timer_run, uid, timeout;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000279#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000280 struct sockaddr_in6 localaddr, remaddr;
281 char addr6[INET6_ADDRSTRLEN];
282 struct in6_addr in6;
283#else
Eric Andersen31a0ece2001-10-31 11:00:46 +0000284 struct sockaddr_in localaddr, remaddr;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000285#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000286 unsigned long rxq, txq, time_len, retr, inode;
287
288 if (lnr == 0)
289 return;
290
291 more[0] = '\0';
292 num = sscanf(line,
293 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
294 &d, local_addr, &local_port,
295 rem_addr, &rem_port, &state,
296 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
297
298 if (strlen(local_addr) > 8) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000299#if ENABLE_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000300 sscanf(local_addr, "%08X%08X%08X%08X",
301 &in6.s6_addr32[0], &in6.s6_addr32[1],
302 &in6.s6_addr32[2], &in6.s6_addr32[3]);
303 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
304 inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr.sin6_addr);
305 sscanf(rem_addr, "%08X%08X%08X%08X",
306 &in6.s6_addr32[0], &in6.s6_addr32[1],
307 &in6.s6_addr32[2], &in6.s6_addr32[3]);
308 inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
309 inet_pton(AF_INET6, addr6, (struct sockaddr *) &remaddr.sin6_addr);
310 localaddr.sin6_family = AF_INET6;
311 remaddr.sin6_family = AF_INET6;
312#endif
Eric Andersen31a0ece2001-10-31 11:00:46 +0000313 } else {
314 sscanf(local_addr, "%X",
315 &((struct sockaddr_in *) &localaddr)->sin_addr.s_addr);
316 sscanf(rem_addr, "%X",
317 &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr);
318 ((struct sockaddr *) &localaddr)->sa_family = AF_INET;
319 ((struct sockaddr *) &remaddr)->sa_family = AF_INET;
320 }
321
322 if (num < 10) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000323 bb_error_msg("warning, got bogus raw line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000324 return;
325 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000326 state_str = itoa(state);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000327
Denis Vlasenko703e2022007-01-22 14:12:08 +0000328#if ENABLE_FEATURE_IPV6
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000329# define notnull(A) (((A.sin6_family == AF_INET6) && \
Eric Andersen51b8bd62002-07-03 11:46:38 +0000330 ((A.sin6_addr.s6_addr32[0]) || \
331 (A.sin6_addr.s6_addr32[1]) || \
332 (A.sin6_addr.s6_addr32[2]) || \
333 (A.sin6_addr.s6_addr32[3]))) || \
334 ((A.sin6_family == AF_INET) && \
335 ((struct sockaddr_in *) &A)->sin_addr.s_addr))
336#else
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000337# define notnull(A) (A.sin_addr.s_addr)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000338#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000339 if ((notnull(remaddr) && (flags & NETSTAT_CONNECTED))
340 || (!notnull(remaddr) && (flags & NETSTAT_LISTENING))
341 ) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000342 snprint_ip_port(local_addr, sizeof(local_addr),
343 (struct sockaddr *) &localaddr, local_port,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000344 "raw", flags & NETSTAT_NUMERIC);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000345
Eric Andersen31a0ece2001-10-31 11:00:46 +0000346 snprint_ip_port(rem_addr, sizeof(rem_addr),
347 (struct sockaddr *) &remaddr, rem_port,
Denis Vlasenko703e2022007-01-22 14:12:08 +0000348 "raw", flags & NETSTAT_NUMERIC);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000349
350 printf("raw %6ld %6ld %-23s %-23s %-12s\n",
351 rxq, txq, local_addr, rem_addr, state_str);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000352 }
353}
354
355#define HAS_INODE 1
356
357static void unix_do_one(int nr, const char *line)
358{
359 static int has = 0;
360 char path[PATH_MAX], ss_flags[32];
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000361 const char *ss_proto, *ss_state, *ss_type;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000362 int num, state, type, inode;
363 void *d;
364 unsigned long refcnt, proto, unix_flags;
365
366 if (nr == 0) {
367 if (strstr(line, "Inode"))
368 has |= HAS_INODE;
369 return;
370 }
371 path[0] = '\0';
372 num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s",
373 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path);
374 if (num < 6) {
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000375 bb_error_msg("warning, got bogus unix line");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000376 return;
377 }
378 if (!(has & HAS_INODE))
379 snprintf(path,sizeof(path),"%d",inode);
380
Denis Vlasenko703e2022007-01-22 14:12:08 +0000381 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000382 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000383 if (!(flags & NETSTAT_LISTENING))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000384 return;
385 } else {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000386 if (!(flags & NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000387 return;
388 }
389 }
390
391 switch (proto) {
392 case 0:
393 ss_proto = "unix";
394 break;
395
396 default:
397 ss_proto = "??";
398 }
399
400 switch (type) {
401 case SOCK_STREAM:
402 ss_type = "STREAM";
403 break;
404
405 case SOCK_DGRAM:
406 ss_type = "DGRAM";
407 break;
408
409 case SOCK_RAW:
410 ss_type = "RAW";
411 break;
412
413 case SOCK_RDM:
414 ss_type = "RDM";
415 break;
416
417 case SOCK_SEQPACKET:
418 ss_type = "SEQPACKET";
419 break;
420
421 default:
422 ss_type = "UNKNOWN";
423 }
424
425 switch (state) {
426 case SS_FREE:
427 ss_state = "FREE";
428 break;
429
430 case SS_UNCONNECTED:
431 /*
432 * Unconnected sockets may be listening
433 * for something.
434 */
435 if (unix_flags & SO_ACCEPTCON) {
436 ss_state = "LISTENING";
437 } else {
438 ss_state = "";
439 }
440 break;
441
442 case SS_CONNECTING:
443 ss_state = "CONNECTING";
444 break;
445
446 case SS_CONNECTED:
447 ss_state = "CONNECTED";
448 break;
449
450 case SS_DISCONNECTING:
451 ss_state = "DISCONNECTING";
452 break;
453
454 default:
455 ss_state = "UNKNOWN";
456 }
457
458 strcpy(ss_flags, "[ ");
459 if (unix_flags & SO_ACCEPTCON)
460 strcat(ss_flags, "ACC ");
461 if (unix_flags & SO_WAITDATA)
462 strcat(ss_flags, "W ");
463 if (unix_flags & SO_NOSPACE)
464 strcat(ss_flags, "N ");
465
466 strcat(ss_flags, "]");
467
468 printf("%-5s %-6ld %-11s %-10s %-13s ",
469 ss_proto, refcnt, ss_flags, ss_type, ss_state);
470 if (has & HAS_INODE)
471 printf("%-6d ",inode);
472 else
473 printf("- ");
474 puts(path);
475}
476
477#define _PATH_PROCNET_UDP "/proc/net/udp"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000478#define _PATH_PROCNET_UDP6 "/proc/net/udp6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000479#define _PATH_PROCNET_TCP "/proc/net/tcp"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000480#define _PATH_PROCNET_TCP6 "/proc/net/tcp6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000481#define _PATH_PROCNET_RAW "/proc/net/raw"
Eric Andersen51b8bd62002-07-03 11:46:38 +0000482#define _PATH_PROCNET_RAW6 "/proc/net/raw6"
Eric Andersen31a0ece2001-10-31 11:00:46 +0000483#define _PATH_PROCNET_UNIX "/proc/net/unix"
484
Eric Andersenb0c39a82002-06-22 17:32:58 +0000485static void do_info(const char *file, const char *name, void (*proc)(int, const char *))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000486{
Eric Andersen31a0ece2001-10-31 11:00:46 +0000487 int lnr = 0;
488 FILE *procinfo;
489
Eric Andersenb0c39a82002-06-22 17:32:58 +0000490 procinfo = fopen(file, "r");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000491 if (procinfo == NULL) {
492 if (errno != ENOENT) {
Denis Vlasenko703e2022007-01-22 14:12:08 +0000493 bb_perror_msg("%s", file);
Eric Andersenb0c39a82002-06-22 17:32:58 +0000494 } else {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000495 bb_error_msg("no support for '%s' on this system", name);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000496 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000497 return;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000498 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000499 do {
500 char *buffer = xmalloc_fgets(procinfo);
501 if (buffer) {
502 (proc)(lnr++, buffer);
503 free(buffer);
504 }
505 } while (!feof(procinfo));
506 fclose(procinfo);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000507}
508
509/*
510 * Our main function.
511 */
512
Denis Vlasenko06af2162007-02-03 17:28:39 +0000513int netstat_main(int argc, char **argv);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000514int netstat_main(int argc, char **argv)
515{
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000516 enum {
517 OPT_extended = 0x4,
518 OPT_showroute = 0x100,
519 };
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000520 unsigned opt;
Denis Vlasenko703e2022007-01-22 14:12:08 +0000521#if ENABLE_FEATURE_IPV6
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000522 int inet = 1;
523 int inet6 = 1;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000524#else
Denis Vlasenko703e2022007-01-22 14:12:08 +0000525 enum { inet = 1, inet6 = 0 };
Eric Andersen51b8bd62002-07-03 11:46:38 +0000526#endif
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000527
528 /* Option string must match NETSTAT_xxx constants */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000529 opt = getopt32(argc, argv, "laentuwxr");
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000530 if (opt & 0x1) { // -l
531 flags &= ~NETSTAT_CONNECTED;
532 flags |= NETSTAT_LISTENING;
533 }
534 if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
535 //if (opt & 0x4) // -e
536 if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
537 //if (opt & 0x10) // -t: NETSTAT_TCP
538 //if (opt & 0x20) // -u: NETSTAT_UDP
539 //if (opt & 0x40) // -w: NETSTAT_RAW
540 //if (opt & 0x80) // -x: NETSTAT_UNIX
541 if (opt & OPT_showroute) { // -r
Denis Vlasenko703e2022007-01-22 14:12:08 +0000542#if ENABLE_ROUTE
543 bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000544 return 0;
Robert Griebl820098f2002-05-14 23:03:23 +0000545#else
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000546 bb_error_msg_and_die("-r (display routing table) is not compiled in");
Robert Griebl820098f2002-05-14 23:03:23 +0000547#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000548 }
549
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000550 opt &= NETSTAT_ALLPROTO;
551 if (opt) {
552 flags &= ~NETSTAT_ALLPROTO;
553 flags |= opt;
Eric Andersen31a0ece2001-10-31 11:00:46 +0000554 }
Denis Vlasenko754a88f2006-09-22 16:02:40 +0000555 if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000556 printf("Active Internet connections "); /* xxx */
557
Denis Vlasenko703e2022007-01-22 14:12:08 +0000558 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000559 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000560 else if (flags & NETSTAT_LISTENING)
561 printf("(only servers)");
562 else
563 printf("(w/o servers)");
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000564 printf("\nProto Recv-Q Send-Q Local Address Foreign Address State\n");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000565 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000566 if (inet && flags & NETSTAT_TCP)
567 do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one);
568#if ENABLE_FEATURE_IPV6
569 if (inet6 && flags & NETSTAT_TCP)
570 do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000571#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000572 if (inet && flags & NETSTAT_UDP)
573 do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one);
574#if ENABLE_FEATURE_IPV6
575 if (inet6 && flags & NETSTAT_UDP)
576 do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000577#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000578 if (inet && flags & NETSTAT_RAW)
579 do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one);
580#if ENABLE_FEATURE_IPV6
581 if (inet6 && flags & NETSTAT_RAW)
582 do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000583#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +0000584 if (flags & NETSTAT_UNIX) {
Eric Andersen31a0ece2001-10-31 11:00:46 +0000585 printf("Active UNIX domain sockets ");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000586 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
Eric Andersen31a0ece2001-10-31 11:00:46 +0000587 printf("(servers and established)");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000588 else if (flags & NETSTAT_LISTENING)
589 printf("(only servers)");
590 else
591 printf("(w/o servers)");
Eric Andersen31a0ece2001-10-31 11:00:46 +0000592 printf("\nProto RefCnt Flags Type State I-Node Path\n");
Denis Vlasenko703e2022007-01-22 14:12:08 +0000593 do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one);
Eric Andersen31a0ece2001-10-31 11:00:46 +0000594 }
595 return 0;
596}