diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-22 14:12:08 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-22 14:12:08 +0000 |
commit | 703e20235aa0624d3ff4335c1e86edaa6e21efe2 (patch) | |
tree | 49e8451efad93b75c0be74ee553d3b3f8c04a25d /networking/netstat.c | |
parent | 6cd2d2bcba37a13d0d73326dd7bca64bbccce4f8 (diff) | |
download | busybox-w32-703e20235aa0624d3ff4335c1e86edaa6e21efe2.tar.gz busybox-w32-703e20235aa0624d3ff4335c1e86edaa6e21efe2.tar.bz2 busybox-w32-703e20235aa0624d3ff4335c1e86edaa6e21efe2.zip |
cleanups: unnecessary casts, unified const_1, eliminate cross-.c file
prototypes (heresy!), add spaces in places like "flags&NETSTAT_CONNECTED",
removed unused #defines, #ifdef -> #if, use uint32_t for ipv4 addrs.
Diffstat (limited to 'networking/netstat.c')
-rw-r--r-- | networking/netstat.c | 258 |
1 files changed, 119 insertions, 139 deletions
diff --git a/networking/netstat.c b/networking/netstat.c index 3dad57a40..16ee52d9a 100644 --- a/networking/netstat.c +++ b/networking/netstat.c | |||
@@ -14,81 +14,70 @@ | |||
14 | #include "busybox.h" | 14 | #include "busybox.h" |
15 | #include "inet_common.h" | 15 | #include "inet_common.h" |
16 | 16 | ||
17 | #ifdef CONFIG_ROUTE | 17 | #define NETSTAT_CONNECTED 0x01 |
18 | extern void displayroutes(int noresolve, int netstatfmt); | 18 | #define NETSTAT_LISTENING 0x02 |
19 | #endif | 19 | #define NETSTAT_NUMERIC 0x04 |
20 | |||
21 | #define NETSTAT_CONNECTED 0x01 | ||
22 | #define NETSTAT_LISTENING 0x02 | ||
23 | #define NETSTAT_NUMERIC 0x04 | ||
24 | /* Must match getopt32 option string */ | 20 | /* Must match getopt32 option string */ |
25 | #define NETSTAT_TCP 0x10 | 21 | #define NETSTAT_TCP 0x10 |
26 | #define NETSTAT_UDP 0x20 | 22 | #define NETSTAT_UDP 0x20 |
27 | #define NETSTAT_RAW 0x40 | 23 | #define NETSTAT_RAW 0x40 |
28 | #define NETSTAT_UNIX 0x80 | 24 | #define NETSTAT_UNIX 0x80 |
29 | #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX) | 25 | #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX) |
30 | 26 | ||
31 | static int flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; | 27 | static int flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; |
32 | 28 | ||
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 { | 29 | enum { |
40 | TCP_ESTABLISHED = 1, | 30 | TCP_ESTABLISHED = 1, |
41 | TCP_SYN_SENT, | 31 | TCP_SYN_SENT, |
42 | TCP_SYN_RECV, | 32 | TCP_SYN_RECV, |
43 | TCP_FIN_WAIT1, | 33 | TCP_FIN_WAIT1, |
44 | TCP_FIN_WAIT2, | 34 | TCP_FIN_WAIT2, |
45 | TCP_TIME_WAIT, | 35 | TCP_TIME_WAIT, |
46 | TCP_CLOSE, | 36 | TCP_CLOSE, |
47 | TCP_CLOSE_WAIT, | 37 | TCP_CLOSE_WAIT, |
48 | TCP_LAST_ACK, | 38 | TCP_LAST_ACK, |
49 | TCP_LISTEN, | 39 | TCP_LISTEN, |
50 | TCP_CLOSING /* now a valid state */ | 40 | TCP_CLOSING /* now a valid state */ |
51 | }; | 41 | }; |
52 | 42 | ||
53 | static const char * const tcp_state[] = | 43 | static const char * const tcp_state[] = |
54 | { | 44 | { |
55 | "", | 45 | "", |
56 | "ESTABLISHED", | 46 | "ESTABLISHED", |
57 | "SYN_SENT", | 47 | "SYN_SENT", |
58 | "SYN_RECV", | 48 | "SYN_RECV", |
59 | "FIN_WAIT1", | 49 | "FIN_WAIT1", |
60 | "FIN_WAIT2", | 50 | "FIN_WAIT2", |
61 | "TIME_WAIT", | 51 | "TIME_WAIT", |
62 | "CLOSE", | 52 | "CLOSE", |
63 | "CLOSE_WAIT", | 53 | "CLOSE_WAIT", |
64 | "LAST_ACK", | 54 | "LAST_ACK", |
65 | "LISTEN", | 55 | "LISTEN", |
66 | "CLOSING" | 56 | "CLOSING" |
67 | }; | 57 | }; |
68 | 58 | ||
69 | typedef enum { | 59 | typedef enum { |
70 | SS_FREE = 0, /* not allocated */ | 60 | SS_FREE = 0, /* not allocated */ |
71 | SS_UNCONNECTED, /* unconnected to any socket */ | 61 | SS_UNCONNECTED, /* unconnected to any socket */ |
72 | SS_CONNECTING, /* in process of connecting */ | 62 | SS_CONNECTING, /* in process of connecting */ |
73 | SS_CONNECTED, /* connected to socket */ | 63 | SS_CONNECTED, /* connected to socket */ |
74 | SS_DISCONNECTING /* in process of disconnecting */ | 64 | SS_DISCONNECTING /* in process of disconnecting */ |
75 | } socket_state; | 65 | } socket_state; |
76 | 66 | ||
77 | #define SO_ACCEPTCON (1<<16) /* performed a listen */ | 67 | #define SO_ACCEPTCON (1<<16) /* performed a listen */ |
78 | #define SO_WAITDATA (1<<17) /* wait data to read */ | 68 | #define SO_WAITDATA (1<<17) /* wait data to read */ |
79 | #define SO_NOSPACE (1<<18) /* no space to write */ | 69 | #define SO_NOSPACE (1<<18) /* no space to write */ |
80 | 70 | ||
81 | static char *get_sname(int port, const char *proto, int num) | 71 | static char *get_sname(int port, const char *proto, int num) |
82 | { | 72 | { |
83 | char *str=itoa(ntohs(port)); | 73 | char *str = itoa(ntohs(port)); |
84 | if (num) { | 74 | if (!num) { |
85 | } else { | 75 | struct servent *se = getservbyport(port, proto); |
86 | struct servent *se=getservbyport(port,proto); | ||
87 | if (se) | 76 | if (se) |
88 | str=se->s_name; | 77 | str = se->s_name; |
89 | } | 78 | } |
90 | if (!port) { | 79 | if (!port) { |
91 | str="*"; | 80 | str = "*"; |
92 | } | 81 | } |
93 | return str; | 82 | return str; |
94 | } | 83 | } |
@@ -97,21 +86,21 @@ static void snprint_ip_port(char *ip_port, int size, struct sockaddr *addr, int | |||
97 | { | 86 | { |
98 | char *port_name; | 87 | char *port_name; |
99 | 88 | ||
100 | #ifdef CONFIG_FEATURE_IPV6 | 89 | #if ENABLE_FEATURE_IPV6 |
101 | if (addr->sa_family == AF_INET6) { | 90 | if (addr->sa_family == AF_INET6) { |
102 | INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr, | 91 | INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr, |
103 | (numeric&NETSTAT_NUMERIC) ? 0x0fff : 0); | 92 | (numeric & NETSTAT_NUMERIC) ? 0x0fff : 0); |
104 | } else | 93 | } else |
105 | #endif | 94 | #endif |
106 | { | 95 | { |
107 | INET_rresolve(ip_port, size, (struct sockaddr_in *)addr, | 96 | INET_rresolve(ip_port, size, (struct sockaddr_in *)addr, |
108 | 0x4000 | ((numeric&NETSTAT_NUMERIC) ? 0x0fff : 0), | 97 | 0x4000 | ((numeric & NETSTAT_NUMERIC) ? 0x0fff : 0), |
109 | 0xffffffff); | 98 | 0xffffffff); |
110 | } | 99 | } |
111 | port_name=get_sname(htons(port), proto, numeric); | 100 | port_name = get_sname(htons(port), proto, numeric); |
112 | if ((strlen(ip_port) + strlen(port_name)) > 22) | 101 | if ((strlen(ip_port) + strlen(port_name)) > 22) |
113 | ip_port[22 - strlen(port_name)] = '\0'; | 102 | ip_port[22 - strlen(port_name)] = '\0'; |
114 | ip_port+=strlen(ip_port); | 103 | ip_port += strlen(ip_port); |
115 | strcat(ip_port, ":"); | 104 | strcat(ip_port, ":"); |
116 | strcat(ip_port, port_name); | 105 | strcat(ip_port, port_name); |
117 | } | 106 | } |
@@ -122,7 +111,7 @@ static void tcp_do_one(int lnr, const char *line) | |||
122 | const char *state_str; | 111 | const char *state_str; |
123 | char more[512]; | 112 | char more[512]; |
124 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; | 113 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
125 | #ifdef CONFIG_FEATURE_IPV6 | 114 | #if ENABLE_FEATURE_IPV6 |
126 | struct sockaddr_in6 localaddr, remaddr; | 115 | struct sockaddr_in6 localaddr, remaddr; |
127 | char addr6[INET6_ADDRSTRLEN]; | 116 | char addr6[INET6_ADDRSTRLEN]; |
128 | struct in6_addr in6; | 117 | struct in6_addr in6; |
@@ -142,7 +131,7 @@ static void tcp_do_one(int lnr, const char *line) | |||
142 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); | 131 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
143 | 132 | ||
144 | if (strlen(local_addr) > 8) { | 133 | if (strlen(local_addr) > 8) { |
145 | #ifdef CONFIG_FEATURE_IPV6 | 134 | #if ENABLE_FEATURE_IPV6 |
146 | sscanf(local_addr, "%08X%08X%08X%08X", | 135 | sscanf(local_addr, "%08X%08X%08X%08X", |
147 | &in6.s6_addr32[0], &in6.s6_addr32[1], | 136 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
148 | &in6.s6_addr32[2], &in6.s6_addr32[3]); | 137 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
@@ -170,20 +159,19 @@ static void tcp_do_one(int lnr, const char *line) | |||
170 | return; | 159 | return; |
171 | } | 160 | } |
172 | state_str = tcp_state[state]; | 161 | state_str = tcp_state[state]; |
173 | if ((rem_port && (flags&NETSTAT_CONNECTED)) || | 162 | if ((rem_port && (flags & NETSTAT_CONNECTED)) |
174 | (!rem_port && (flags&NETSTAT_LISTENING))) | 163 | || (!rem_port && (flags & NETSTAT_LISTENING)) |
175 | { | 164 | ) { |
176 | snprint_ip_port(local_addr, sizeof(local_addr), | 165 | snprint_ip_port(local_addr, sizeof(local_addr), |
177 | (struct sockaddr *) &localaddr, local_port, | 166 | (struct sockaddr *) &localaddr, local_port, |
178 | "tcp", flags&NETSTAT_NUMERIC); | 167 | "tcp", flags & NETSTAT_NUMERIC); |
179 | 168 | ||
180 | snprint_ip_port(rem_addr, sizeof(rem_addr), | 169 | snprint_ip_port(rem_addr, sizeof(rem_addr), |
181 | (struct sockaddr *) &remaddr, rem_port, | 170 | (struct sockaddr *) &remaddr, rem_port, |
182 | "tcp", flags&NETSTAT_NUMERIC); | 171 | "tcp", flags & NETSTAT_NUMERIC); |
183 | 172 | ||
184 | printf("tcp %6ld %6ld %-23s %-23s %-12s\n", | 173 | printf("tcp %6ld %6ld %-23s %-23s %-12s\n", |
185 | rxq, txq, local_addr, rem_addr, state_str); | 174 | rxq, txq, local_addr, rem_addr, state_str); |
186 | |||
187 | } | 175 | } |
188 | } | 176 | } |
189 | 177 | ||
@@ -192,7 +180,7 @@ static void udp_do_one(int lnr, const char *line) | |||
192 | char local_addr[64], rem_addr[64]; | 180 | char local_addr[64], rem_addr[64]; |
193 | char *state_str, more[512]; | 181 | char *state_str, more[512]; |
194 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; | 182 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
195 | #ifdef CONFIG_FEATURE_IPV6 | 183 | #if ENABLE_FEATURE_IPV6 |
196 | struct sockaddr_in6 localaddr, remaddr; | 184 | struct sockaddr_in6 localaddr, remaddr; |
197 | char addr6[INET6_ADDRSTRLEN]; | 185 | char addr6[INET6_ADDRSTRLEN]; |
198 | struct in6_addr in6; | 186 | struct in6_addr in6; |
@@ -212,7 +200,7 @@ static void udp_do_one(int lnr, const char *line) | |||
212 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); | 200 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
213 | 201 | ||
214 | if (strlen(local_addr) > 8) { | 202 | if (strlen(local_addr) > 8) { |
215 | #ifdef CONFIG_FEATURE_IPV6 | 203 | #if ENABLE_FEATURE_IPV6 |
216 | /* Demangle what the kernel gives us */ | 204 | /* Demangle what the kernel gives us */ |
217 | sscanf(local_addr, "%08X%08X%08X%08X", | 205 | sscanf(local_addr, "%08X%08X%08X%08X", |
218 | &in6.s6_addr32[0], &in6.s6_addr32[1], | 206 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
@@ -254,7 +242,7 @@ static void udp_do_one(int lnr, const char *line) | |||
254 | break; | 242 | break; |
255 | } | 243 | } |
256 | 244 | ||
257 | #ifdef CONFIG_FEATURE_IPV6 | 245 | #if ENABLE_FEATURE_IPV6 |
258 | # define notnull(A) (((A.sin6_family == AF_INET6) && \ | 246 | # define notnull(A) (((A.sin6_family == AF_INET6) && \ |
259 | ((A.sin6_addr.s6_addr32[0]) || \ | 247 | ((A.sin6_addr.s6_addr32[0]) || \ |
260 | (A.sin6_addr.s6_addr32[1]) || \ | 248 | (A.sin6_addr.s6_addr32[1]) || \ |
@@ -265,20 +253,19 @@ static void udp_do_one(int lnr, const char *line) | |||
265 | #else | 253 | #else |
266 | # define notnull(A) (A.sin_addr.s_addr) | 254 | # define notnull(A) (A.sin_addr.s_addr) |
267 | #endif | 255 | #endif |
268 | if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) || | 256 | if ((notnull(remaddr) && (flags & NETSTAT_CONNECTED)) |
269 | (!notnull(remaddr) && (flags&NETSTAT_LISTENING))) | 257 | || (!notnull(remaddr) && (flags & NETSTAT_LISTENING)) |
270 | { | 258 | ) { |
271 | snprint_ip_port(local_addr, sizeof(local_addr), | 259 | snprint_ip_port(local_addr, sizeof(local_addr), |
272 | (struct sockaddr *) &localaddr, local_port, | 260 | (struct sockaddr *) &localaddr, local_port, |
273 | "udp", flags&NETSTAT_NUMERIC); | 261 | "udp", flags & NETSTAT_NUMERIC); |
274 | 262 | ||
275 | snprint_ip_port(rem_addr, sizeof(rem_addr), | 263 | snprint_ip_port(rem_addr, sizeof(rem_addr), |
276 | (struct sockaddr *) &remaddr, rem_port, | 264 | (struct sockaddr *) &remaddr, rem_port, |
277 | "udp", flags&NETSTAT_NUMERIC); | 265 | "udp", flags & NETSTAT_NUMERIC); |
278 | 266 | ||
279 | printf("udp %6ld %6ld %-23s %-23s %-12s\n", | 267 | printf("udp %6ld %6ld %-23s %-23s %-12s\n", |
280 | rxq, txq, local_addr, rem_addr, state_str); | 268 | rxq, txq, local_addr, rem_addr, state_str); |
281 | |||
282 | } | 269 | } |
283 | } | 270 | } |
284 | 271 | ||
@@ -287,7 +274,7 @@ static void raw_do_one(int lnr, const char *line) | |||
287 | char local_addr[64], rem_addr[64]; | 274 | char local_addr[64], rem_addr[64]; |
288 | char *state_str, more[512]; | 275 | char *state_str, more[512]; |
289 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; | 276 | int num, local_port, rem_port, d, state, timer_run, uid, timeout; |
290 | #ifdef CONFIG_FEATURE_IPV6 | 277 | #if ENABLE_FEATURE_IPV6 |
291 | struct sockaddr_in6 localaddr, remaddr; | 278 | struct sockaddr_in6 localaddr, remaddr; |
292 | char addr6[INET6_ADDRSTRLEN]; | 279 | char addr6[INET6_ADDRSTRLEN]; |
293 | struct in6_addr in6; | 280 | struct in6_addr in6; |
@@ -307,7 +294,7 @@ static void raw_do_one(int lnr, const char *line) | |||
307 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); | 294 | &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more); |
308 | 295 | ||
309 | if (strlen(local_addr) > 8) { | 296 | if (strlen(local_addr) > 8) { |
310 | #ifdef CONFIG_FEATURE_IPV6 | 297 | #if ENABLE_FEATURE_IPV6 |
311 | sscanf(local_addr, "%08X%08X%08X%08X", | 298 | sscanf(local_addr, "%08X%08X%08X%08X", |
312 | &in6.s6_addr32[0], &in6.s6_addr32[1], | 299 | &in6.s6_addr32[0], &in6.s6_addr32[1], |
313 | &in6.s6_addr32[2], &in6.s6_addr32[3]); | 300 | &in6.s6_addr32[2], &in6.s6_addr32[3]); |
@@ -334,9 +321,9 @@ static void raw_do_one(int lnr, const char *line) | |||
334 | bb_error_msg("warning, got bogus raw line"); | 321 | bb_error_msg("warning, got bogus raw line"); |
335 | return; | 322 | return; |
336 | } | 323 | } |
337 | state_str=itoa(state); | 324 | state_str = itoa(state); |
338 | 325 | ||
339 | #ifdef CONFIG_FEATURE_IPV6 | 326 | #if ENABLE_FEATURE_IPV6 |
340 | # define notnull(A) (((A.sin6_family == AF_INET6) && \ | 327 | # define notnull(A) (((A.sin6_family == AF_INET6) && \ |
341 | ((A.sin6_addr.s6_addr32[0]) || \ | 328 | ((A.sin6_addr.s6_addr32[0]) || \ |
342 | (A.sin6_addr.s6_addr32[1]) || \ | 329 | (A.sin6_addr.s6_addr32[1]) || \ |
@@ -347,20 +334,19 @@ static void raw_do_one(int lnr, const char *line) | |||
347 | #else | 334 | #else |
348 | # define notnull(A) (A.sin_addr.s_addr) | 335 | # define notnull(A) (A.sin_addr.s_addr) |
349 | #endif | 336 | #endif |
350 | if ((notnull(remaddr) && (flags&NETSTAT_CONNECTED)) || | 337 | if ((notnull(remaddr) && (flags & NETSTAT_CONNECTED)) |
351 | (!notnull(remaddr) && (flags&NETSTAT_LISTENING))) | 338 | || (!notnull(remaddr) && (flags & NETSTAT_LISTENING)) |
352 | { | 339 | ) { |
353 | snprint_ip_port(local_addr, sizeof(local_addr), | 340 | snprint_ip_port(local_addr, sizeof(local_addr), |
354 | (struct sockaddr *) &localaddr, local_port, | 341 | (struct sockaddr *) &localaddr, local_port, |
355 | "raw", flags&NETSTAT_NUMERIC); | 342 | "raw", flags & NETSTAT_NUMERIC); |
356 | 343 | ||
357 | snprint_ip_port(rem_addr, sizeof(rem_addr), | 344 | snprint_ip_port(rem_addr, sizeof(rem_addr), |
358 | (struct sockaddr *) &remaddr, rem_port, | 345 | (struct sockaddr *) &remaddr, rem_port, |
359 | "raw", flags&NETSTAT_NUMERIC); | 346 | "raw", flags & NETSTAT_NUMERIC); |
360 | 347 | ||
361 | printf("raw %6ld %6ld %-23s %-23s %-12s\n", | 348 | printf("raw %6ld %6ld %-23s %-23s %-12s\n", |
362 | rxq, txq, local_addr, rem_addr, state_str); | 349 | rxq, txq, local_addr, rem_addr, state_str); |
363 | |||
364 | } | 350 | } |
365 | } | 351 | } |
366 | 352 | ||
@@ -390,12 +376,12 @@ static void unix_do_one(int nr, const char *line) | |||
390 | if (!(has & HAS_INODE)) | 376 | if (!(has & HAS_INODE)) |
391 | snprintf(path,sizeof(path),"%d",inode); | 377 | snprintf(path,sizeof(path),"%d",inode); |
392 | 378 | ||
393 | if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))!=(NETSTAT_LISTENING|NETSTAT_CONNECTED)) { | 379 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) { |
394 | if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) { | 380 | if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) { |
395 | if (!(flags&NETSTAT_LISTENING)) | 381 | if (!(flags & NETSTAT_LISTENING)) |
396 | return; | 382 | return; |
397 | } else { | 383 | } else { |
398 | if (!(flags&NETSTAT_CONNECTED)) | 384 | if (!(flags & NETSTAT_CONNECTED)) |
399 | return; | 385 | return; |
400 | } | 386 | } |
401 | } | 387 | } |
@@ -502,20 +488,20 @@ static void do_info(const char *file, const char *name, void (*proc)(int, const | |||
502 | procinfo = fopen(file, "r"); | 488 | procinfo = fopen(file, "r"); |
503 | if (procinfo == NULL) { | 489 | if (procinfo == NULL) { |
504 | if (errno != ENOENT) { | 490 | if (errno != ENOENT) { |
505 | perror(file); | 491 | bb_perror_msg("%s", file); |
506 | } else { | 492 | } else { |
507 | bb_error_msg("no support for '%s' on this system", name); | 493 | bb_error_msg("no support for '%s' on this system", name); |
508 | } | 494 | } |
509 | } else { | 495 | return; |
510 | do { | ||
511 | char *buffer = xmalloc_fgets(procinfo); | ||
512 | if (buffer) { | ||
513 | (proc)(lnr++, buffer); | ||
514 | free(buffer); | ||
515 | } | ||
516 | } while (!feof(procinfo)); | ||
517 | fclose(procinfo); | ||
518 | } | 496 | } |
497 | do { | ||
498 | char *buffer = xmalloc_fgets(procinfo); | ||
499 | if (buffer) { | ||
500 | (proc)(lnr++, buffer); | ||
501 | free(buffer); | ||
502 | } | ||
503 | } while (!feof(procinfo)); | ||
504 | fclose(procinfo); | ||
519 | } | 505 | } |
520 | 506 | ||
521 | /* | 507 | /* |
@@ -529,12 +515,11 @@ int netstat_main(int argc, char **argv) | |||
529 | OPT_showroute = 0x100, | 515 | OPT_showroute = 0x100, |
530 | }; | 516 | }; |
531 | unsigned opt; | 517 | unsigned opt; |
532 | #ifdef CONFIG_FEATURE_IPV6 | 518 | #if ENABLE_FEATURE_IPV6 |
533 | int inet = 1; | 519 | int inet = 1; |
534 | int inet6 = 1; | 520 | int inet6 = 1; |
535 | #else | 521 | #else |
536 | # define inet 1 | 522 | enum { inet = 1, inet6 = 0 }; |
537 | # define inet6 0 | ||
538 | #endif | 523 | #endif |
539 | 524 | ||
540 | /* Option string must match NETSTAT_xxx constants */ | 525 | /* Option string must match NETSTAT_xxx constants */ |
@@ -551,8 +536,8 @@ int netstat_main(int argc, char **argv) | |||
551 | //if (opt & 0x40) // -w: NETSTAT_RAW | 536 | //if (opt & 0x40) // -w: NETSTAT_RAW |
552 | //if (opt & 0x80) // -x: NETSTAT_UNIX | 537 | //if (opt & 0x80) // -x: NETSTAT_UNIX |
553 | if (opt & OPT_showroute) { // -r | 538 | if (opt & OPT_showroute) { // -r |
554 | #ifdef CONFIG_ROUTE | 539 | #if ENABLE_ROUTE |
555 | displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended)); | 540 | bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended)); |
556 | return 0; | 541 | return 0; |
557 | #else | 542 | #else |
558 | bb_error_msg_and_die("-r (display routing table) is not compiled in"); | 543 | bb_error_msg_and_die("-r (display routing table) is not compiled in"); |
@@ -567,47 +552,42 @@ int netstat_main(int argc, char **argv) | |||
567 | if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) { | 552 | if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) { |
568 | printf("Active Internet connections "); /* xxx */ | 553 | printf("Active Internet connections "); /* xxx */ |
569 | 554 | ||
570 | if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED)) | 555 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
571 | printf("(servers and established)"); | 556 | printf("(servers and established)"); |
572 | else { | 557 | else if (flags & NETSTAT_LISTENING) |
573 | if (flags & NETSTAT_LISTENING) | 558 | printf("(only servers)"); |
574 | printf("(only servers)"); | 559 | else |
575 | else | 560 | printf("(w/o servers)"); |
576 | printf("(w/o servers)"); | ||
577 | } | ||
578 | printf("\nProto Recv-Q Send-Q Local Address Foreign Address State\n"); | 561 | printf("\nProto Recv-Q Send-Q Local Address Foreign Address State\n"); |
579 | } | 562 | } |
580 | if (inet && flags&NETSTAT_TCP) | 563 | if (inet && flags & NETSTAT_TCP) |
581 | do_info(_PATH_PROCNET_TCP,"AF INET (tcp)",tcp_do_one); | 564 | do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one); |
582 | #ifdef CONFIG_FEATURE_IPV6 | 565 | #if ENABLE_FEATURE_IPV6 |
583 | if (inet6 && flags&NETSTAT_TCP) | 566 | if (inet6 && flags & NETSTAT_TCP) |
584 | do_info(_PATH_PROCNET_TCP6,"AF INET6 (tcp)",tcp_do_one); | 567 | do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one); |
585 | #endif | 568 | #endif |
586 | if (inet && flags&NETSTAT_UDP) | 569 | if (inet && flags & NETSTAT_UDP) |
587 | do_info(_PATH_PROCNET_UDP,"AF INET (udp)",udp_do_one); | 570 | do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one); |
588 | #ifdef CONFIG_FEATURE_IPV6 | 571 | #if ENABLE_FEATURE_IPV6 |
589 | if (inet6 && flags&NETSTAT_UDP) | 572 | if (inet6 && flags & NETSTAT_UDP) |
590 | do_info(_PATH_PROCNET_UDP6,"AF INET6 (udp)",udp_do_one); | 573 | do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one); |
591 | #endif | 574 | #endif |
592 | if (inet && flags&NETSTAT_RAW) | 575 | if (inet && flags & NETSTAT_RAW) |
593 | do_info(_PATH_PROCNET_RAW,"AF INET (raw)",raw_do_one); | 576 | do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one); |
594 | #ifdef CONFIG_FEATURE_IPV6 | 577 | #if ENABLE_FEATURE_IPV6 |
595 | if (inet6 && flags&NETSTAT_RAW) | 578 | if (inet6 && flags & NETSTAT_RAW) |
596 | do_info(_PATH_PROCNET_RAW6,"AF INET6 (raw)",raw_do_one); | 579 | do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one); |
597 | #endif | 580 | #endif |
598 | if (flags&NETSTAT_UNIX) { | 581 | if (flags & NETSTAT_UNIX) { |
599 | printf("Active UNIX domain sockets "); | 582 | printf("Active UNIX domain sockets "); |
600 | if ((flags&(NETSTAT_LISTENING|NETSTAT_CONNECTED))==(NETSTAT_LISTENING|NETSTAT_CONNECTED)) | 583 | if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED)) |
601 | printf("(servers and established)"); | 584 | printf("(servers and established)"); |
602 | else { | 585 | else if (flags & NETSTAT_LISTENING) |
603 | if (flags&NETSTAT_LISTENING) | 586 | printf("(only servers)"); |
604 | printf("(only servers)"); | 587 | else |
605 | else | 588 | printf("(w/o servers)"); |
606 | printf("(w/o servers)"); | ||
607 | } | ||
608 | |||
609 | printf("\nProto RefCnt Flags Type State I-Node Path\n"); | 589 | printf("\nProto RefCnt Flags Type State I-Node Path\n"); |
610 | do_info(_PATH_PROCNET_UNIX,"AF UNIX",unix_do_one); | 590 | do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one); |
611 | } | 591 | } |
612 | return 0; | 592 | return 0; |
613 | } | 593 | } |