Mercurial > hg > toybox
annotate toys/pending/netstat.c @ 1215:4eaac3e63fa7 draft
Cleanup freeramdisk: tabs to 2 spaces, square brackets for option name, do optional cleanup under if (CFG_TOYBOX_FREE) guard.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 09 Mar 2014 14:38:51 -0500 |
parents | 0752b2d58909 |
children | c0c91437138b |
rev | line source |
---|---|
961 | 1 /* netstat.c - Display Linux networking subsystem. |
2 * | |
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com> | |
997
8b1814e4c987
Ashwini Sharma said that Kyungwan Han should be in the contact info for the commands he sent recently.
Rob Landley <rob@landley.net>
parents:
961
diff
changeset
|
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> |
961 | 5 * |
6 * Not in SUSv4. | |
7 * | |
8 USE_NETSTAT(NEWTOY(netstat, "pWrxwutneal", TOYFLAG_BIN)) | |
9 config NETSTAT | |
10 bool "netstat" | |
11 default n | |
12 help | |
13 usage: netstat [-pWrxwutneal] | |
14 | |
15 Display networking information. | |
16 | |
17 -r Display routing table. | |
18 -a Display all sockets (Default: Connected). | |
19 -l Display listening server sockets. | |
20 -t Display TCP sockets. | |
21 -u Display UDP sockets. | |
22 -w Display Raw sockets. | |
23 -x Display Unix sockets. | |
24 -e Display other/more information. | |
25 -n Don't resolve names. | |
26 -W Wide Display. | |
27 -p Display PID/Program name for sockets. | |
28 */ | |
29 | |
30 #define FOR_netstat | |
31 #include "toys.h" | |
32 #include <net/route.h> | |
33 | |
34 typedef union _iaddr { | |
35 unsigned u; | |
36 unsigned char b[4]; | |
37 } iaddr; | |
38 | |
39 typedef union _iaddr6 { | |
40 struct { | |
41 unsigned a; | |
42 unsigned b; | |
43 unsigned c; | |
44 unsigned d; | |
45 } u; | |
46 unsigned char b[16]; | |
47 } iaddr6; | |
48 | |
49 #define ADDR_LEN (INET6_ADDRSTRLEN + 1 + 5 + 1) //IPv6 addr len + : + port + '\0' | |
50 | |
51 //For unix states | |
52 enum { | |
53 SOCK_ACCEPTCON = (1 << 16), //performed a listen. | |
54 SOCK_WAIT_DATA = (1 << 17), //wait data to read. | |
55 SOCK_NO_SPACE = (1 << 18), //no space to write. | |
56 }; | |
57 | |
58 #define SOCK_NOT_CONNECTED 1 | |
59 //For PID/Progrma Name | |
60 #define PROGRAM_NAME "PID/Program Name" | |
61 #define PROGNAME_LEN 21 | |
62 | |
63 typedef struct _pidlist { | |
64 struct _pidlist *next; | |
65 long inode; | |
66 char name[PROGNAME_LEN]; | |
67 } PID_LIST; | |
68 PID_LIST *pid_list = NULL; | |
69 | |
70 /* | |
71 * Get base name from the input name. | |
72 */ | |
73 static const char *get_basename(char *name) | |
74 { | |
75 const char *c = strrchr(name, '/'); | |
76 if (c) return c + 1; | |
77 return name; | |
78 } | |
79 /* | |
80 * copy string from src to dest -> only number of bytes. | |
81 */ | |
82 static char *safe_strncpy(char *dst, char *src, size_t size) | |
83 { | |
84 if(!size) return dst; | |
85 dst[--size] = '\0'; | |
86 return strncpy(dst, src, size); | |
87 } | |
88 | |
89 /* | |
90 * locate character in string. | |
91 */ | |
92 static char *strchr_nul(char *s, int c) | |
93 { | |
94 while(*s != '\0' && *s != c) s++; | |
95 return (char*)s; | |
96 } | |
97 | |
98 // Find out if the last character of a string matches with the given one. | |
99 // Don't underrun the buffer if the string length is 0. | |
100 static char *find_last_char(char *str, int c) | |
101 { | |
102 if (str && *str) { | |
103 size_t sz = strlen(str) - 1; | |
104 str += sz; | |
105 if ( (unsigned char)*str == c) return (char*)str; | |
106 } | |
107 return NULL; | |
108 } | |
109 /* | |
110 * Concat path and the file name. | |
111 */ | |
112 static char *append_pathandfile(char *path, char *fname) | |
113 { | |
114 char *c; | |
115 if (!path) path = ""; | |
116 c = find_last_char(path, '/'); | |
117 while (*fname == '/') fname++; | |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
118 return xmprintf("%s%s%s", path, (c)? "" : "/", fname); |
961 | 119 } |
120 /* | |
121 * Concat sub-path and the file name. | |
122 */ | |
123 static char *append_subpathandfile(char *path, char *fname) | |
124 { | |
125 #define ISDOTORDOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2]))) | |
126 if(!fname) return NULL; | |
127 if(ISDOTORDOTDOT(fname)) return NULL; | |
128 return append_pathandfile(path, fname); | |
129 #undef ISDOTORDOTDOT | |
130 } | |
131 /* | |
132 * used to converts string into int and validate the input str for invalid int value or out-of-range. | |
133 */ | |
134 static unsigned get_strtou(char *str, char **endp, int base) | |
135 { | |
136 unsigned long uli; | |
137 char *endptr; | |
138 | |
139 if (!isalnum(str[0])) { | |
140 errno = ERANGE; | |
141 return UINT_MAX; | |
142 } | |
143 errno = 0; | |
144 uli = strtoul(str, &endptr, base); | |
145 if (uli > UINT_MAX) { | |
146 errno = ERANGE; | |
147 return UINT_MAX; | |
148 } | |
149 | |
150 if (endp) *endp = endptr; | |
151 if (endptr[0]) { | |
152 if (isalnum(endptr[0]) || errno) { //"123abc" or out-of-range | |
153 errno = ERANGE; | |
154 return UINT_MAX; | |
155 } | |
156 errno = EINVAL; | |
157 } | |
158 return uli; | |
159 } | |
160 /* | |
161 * used to retrive pid name from pid list. | |
162 */ | |
163 static const char *get_pid_name(unsigned long inode) | |
164 { | |
165 PID_LIST *tmp; | |
166 for (tmp = pid_list; tmp; tmp = tmp->next) | |
167 if (tmp->inode == inode) return tmp->name; | |
168 return "-"; | |
169 } | |
170 /* | |
171 * For TCP/UDP/RAW display data. | |
172 */ | |
173 static void display_data(unsigned rport, char *label, unsigned rxq, unsigned txq, char *lip, char *rip, unsigned state, unsigned long inode) | |
174 { | |
1042
cbc467592b2e
Remove itoa/utoa, let libc do this with sprintf.
Rob Landley <rob@landley.net>
parents:
997
diff
changeset
|
175 char *ss_state = "UNKNOWN", buf[12]; |
961 | 176 char *state_label[] = {"", "ESTABLISHED", "SYN_SENT", "SYN_RECV", "FIN_WAIT1", "FIN_WAIT2", |
177 "TIME_WAIT", "CLOSE", "CLOSE_WAIT", "LAST_ACK", "LISTEN", "CLOSING", "UNKNOWN"}; | |
178 if (!strcmp(label, "tcp")) { | |
179 int sz = ARRAY_LEN(state_label); | |
180 if (!state || state >= sz) state = sz-1; | |
181 ss_state = state_label[state]; | |
182 } | |
183 else if (!strcmp(label, "udp")) { | |
184 if (state == 1) ss_state = state_label[state]; | |
185 else if (state == 7) ss_state = ""; | |
186 } | |
1042
cbc467592b2e
Remove itoa/utoa, let libc do this with sprintf.
Rob Landley <rob@landley.net>
parents:
997
diff
changeset
|
187 else if (!strcmp(label, "raw")) sprintf(ss_state = buf, "%u", state); |
961 | 188 |
189 if ( (toys.optflags & FLAG_W) && (toys.optflags & FLAG_p)) | |
190 xprintf("%3s %6d %6d %-51s %-51s %-12s%s\n", label, rxq, txq, lip, rip, ss_state, get_pid_name(inode)); | |
191 else if (toys.optflags & FLAG_W) | |
192 xprintf("%3s %6d %6d %-51s %-51s %-12s\n", label, rxq, txq, lip, rip, ss_state); | |
193 else if (toys.optflags & FLAG_p) | |
194 xprintf("%3s %6d %6d %-23s %-23s %-12s%s\n", label, rxq, txq, lip, rip, ss_state, get_pid_name(inode)); | |
195 else xprintf("%3s %6d %6d %-23s %-23s %-12s\n", label, rxq, txq, lip, rip, ss_state); | |
196 } | |
197 /* | |
198 * For TCP/UDP/RAW show data. | |
199 */ | |
200 static void show_data(unsigned rport, char *label, unsigned rxq, unsigned txq, char *lip, char *rip, unsigned state, unsigned long inode) | |
201 { | |
202 if (toys.optflags & FLAG_l) { | |
203 if (!rport && (state && 0xA)) display_data(rport, label, rxq, txq, lip, rip, state, inode); | |
204 } else if (toys.optflags & FLAG_a) display_data(rport, label, rxq, txq, lip, rip, state, inode); | |
205 //rport && (TCP | UDP | RAW) | |
206 else if (rport && (0x10 | 0x20 | 0x40)) display_data(rport, label, rxq, txq, lip, rip, state, inode); | |
207 } | |
208 /* | |
209 * used to get service name. | |
210 */ | |
211 static char *get_servname(int port, char *label) | |
212 { | |
213 int lport = htons(port); | |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
214 if (!lport) return xmprintf("%s", "*"); |
961 | 215 struct servent *ser = getservbyport(lport, label); |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
216 if (ser) return xmprintf("%s", ser->s_name); |
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
217 return xmprintf("%u", (unsigned)ntohs(lport)); |
961 | 218 } |
219 /* | |
220 * used to convert address into text format. | |
221 */ | |
222 static void addr2str(int af, void *addr, unsigned port, char *buf, char *label) | |
223 { | |
224 char ip[ADDR_LEN] = {0,}; | |
225 if (!inet_ntop(af, addr, ip, ADDR_LEN)) { | |
226 *buf = '\0'; | |
227 return; | |
228 } | |
229 size_t iplen = strlen(ip); | |
230 if (!port) { | |
231 strncat(ip+iplen, ":*", ADDR_LEN-iplen-1); | |
232 memcpy(buf, ip, ADDR_LEN); | |
233 return; | |
234 } | |
235 | |
236 if (!(toys.optflags & FLAG_n)) { | |
237 struct addrinfo hints, *result, *rp; | |
238 | |
239 memset(&hints, 0, sizeof(struct addrinfo)); | |
240 hints.ai_family = af; | |
241 | |
242 if (!getaddrinfo(ip, NULL, &hints, &result)) { | |
243 char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,}; | |
244 socklen_t sock_len; | |
245 char *sname = NULL; | |
246 int plen = 0; | |
247 | |
248 if (af == AF_INET) sock_len = sizeof(struct sockaddr_in); | |
249 else sock_len = sizeof(struct sockaddr_in6); | |
250 | |
251 for (rp = result; rp; rp = rp->ai_next) | |
252 if (!getnameinfo(rp->ai_addr, sock_len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICSERV)) | |
253 break; | |
254 | |
255 freeaddrinfo(result); | |
256 sname = get_servname(port, label); | |
257 plen = strlen(sname); | |
258 if (*hbuf) { | |
259 memset(ip, 0, ADDR_LEN); | |
260 memcpy(ip, hbuf, (ADDR_LEN - plen - 2)); | |
261 iplen = strlen(ip); | |
262 } | |
263 snprintf(ip + iplen, ADDR_LEN-iplen, ":%s", sname); | |
264 free(sname); | |
265 } | |
266 } | |
267 else snprintf(ip+iplen, ADDR_LEN-iplen, ":%d", port); | |
268 memcpy(buf, ip, ADDR_LEN); | |
269 } | |
270 /* | |
271 * display ipv4 info for TCP/UDP/RAW. | |
272 */ | |
273 static void show_ipv4(char *fname, char *label) | |
274 { | |
275 FILE *fp = fopen((char *)fname, "r"); | |
276 if (!fp) { | |
277 perror_msg("'%s'", fname); | |
278 return; | |
279 } | |
280 fgets(toybuf, sizeof(toybuf), fp); //skip header. | |
281 while (fgets(toybuf, sizeof(toybuf), fp)) { | |
282 char lip[ADDR_LEN] = {0,}, rip[ADDR_LEN] = {0,}; | |
283 iaddr laddr, raddr; | |
284 unsigned lport, rport, state, txq, rxq, num, uid; | |
285 unsigned long inode; | |
286 | |
287 int nitems = sscanf(toybuf, " %d: %x:%x %x:%x %x %x:%x %*X:%*X %*X %d %*d %ld", | |
288 &num, &laddr.u, &lport, &raddr.u, &rport, &state, &txq, &rxq, &uid, &inode); | |
289 if (nitems == 10) { | |
290 addr2str(AF_INET, &laddr, lport, lip, label); | |
291 addr2str(AF_INET, &raddr, rport, rip, label); | |
292 show_data(rport, label, rxq, txq, lip, rip, state, inode); | |
293 } | |
294 }//End of While | |
295 fclose(fp); | |
296 } | |
297 /* | |
298 * display ipv6 info for TCP/UDP/RAW. | |
299 */ | |
300 static void show_ipv6(char *fname, char *label) | |
301 { | |
302 FILE *fp = fopen((char *)fname, "r"); | |
303 if (!fp) { | |
304 perror_msg("'%s'", fname); | |
305 return; | |
306 } | |
307 fgets(toybuf, sizeof(toybuf), fp); //skip header. | |
308 while (fgets(toybuf, sizeof(toybuf), fp)) { | |
309 char lip[ADDR_LEN] = {0,}, rip[ADDR_LEN] = {0,}; | |
310 iaddr6 laddr6, raddr6; | |
311 unsigned lport, rport, state, txq, rxq, num, uid; | |
312 unsigned long inode; | |
313 int nitems = sscanf(toybuf, " %d: %8x%8x%8x%8x:%x %8x%8x%8x%8x:%x %x %x:%x %*X:%*X %*X %d %*d %ld", | |
314 &num, &laddr6.u.a, &laddr6.u.b, &laddr6.u.c, &laddr6.u.d, &lport, &raddr6.u.a, &raddr6.u.b, | |
315 &raddr6.u.c, &raddr6.u.d, &rport, &state, &txq, &rxq, &uid, &inode); | |
316 if (nitems == 16) { | |
317 addr2str(AF_INET6, &laddr6, lport, lip, label); | |
318 addr2str(AF_INET6, &raddr6, rport, rip, label); | |
319 show_data(rport, label, rxq, txq, lip, rip, state, inode); | |
320 } | |
321 }//End of While | |
322 fclose(fp); | |
323 } | |
324 /* | |
325 * display unix socket info. | |
326 */ | |
327 static void show_unix_sockets(char *fname, char *label) | |
328 { | |
329 FILE *fp = fopen((char *)fname, "r"); | |
330 if (!fp) { | |
331 perror_msg("'%s'", fname); | |
332 return; | |
333 } | |
334 fgets(toybuf, sizeof(toybuf), fp); //skip header. | |
335 while (fgets(toybuf, sizeof(toybuf), fp)) { | |
336 unsigned long int refcount, label, flags, inode; | |
337 int nitems = 0, path_offset = 0, type, state; | |
338 char sock_flags[32] = {0,}, *sock_type, *sock_state, *bptr = toybuf; | |
339 | |
340 if (!toybuf[0]) continue; | |
341 nitems = sscanf(toybuf, "%*p: %lX %lX %lX %X %X %lu %n", | |
342 &refcount, &label, &flags, &type, &state, &inode, &path_offset); | |
343 //for state one less | |
344 if (nitems < 6) break; | |
345 if (toys.optflags & FLAG_l) { | |
346 if ( !((state == SOCK_NOT_CONNECTED) && (flags & SOCK_ACCEPTCON)) ) continue; | |
347 } else if (!(toys.optflags & FLAG_a)) { | |
348 if ((state == SOCK_NOT_CONNECTED) && (flags & SOCK_ACCEPTCON)) continue; | |
349 } | |
350 | |
351 //prepare socket type, state and flags. | |
352 { | |
353 char *ss_type[] = { "", "STREAM", "DGRAM", "RAW", "RDM", "SEQPACKET", "UNKNOWN"}; | |
354 char *ss_state[] = { "FREE", "LISTENING", "CONNECTING", "CONNECTED", "DISCONNECTING", "UNKNOWN"}; | |
355 | |
356 int sz = ARRAY_LEN(ss_type);//sizeof(ss_type)/sizeof(ss_type[0]); | |
357 if ( (type < SOCK_STREAM) || (type > SOCK_SEQPACKET) ) sock_type = ss_type[sz-1]; | |
358 else sock_type = ss_type[type]; | |
359 | |
360 sz = ARRAY_LEN(ss_state);//sizeof(ss_state)/sizeof(ss_state[0]); | |
361 if ((state < 0) || (state > sz-2)) sock_state = ss_state[sz-1]; | |
362 else if (state == SOCK_NOT_CONNECTED) { | |
363 if (flags & SOCK_ACCEPTCON) sock_state = ss_state[state]; | |
364 else sock_state = " "; | |
365 } else sock_state = ss_state[state]; | |
366 | |
367 strcpy(sock_flags, "[ "); | |
368 if (flags & SOCK_ACCEPTCON) strcat(sock_flags, "ACC "); | |
369 if (flags & SOCK_WAIT_DATA) strcat(sock_flags, "W "); | |
370 if (flags & SOCK_NO_SPACE) strcat(sock_flags, "N "); | |
371 strcat(sock_flags, "]"); | |
372 } | |
373 xprintf("%-5s %-6ld %-11s %-10s %-13s %6lu ", (!label ? "unix" : "??"), refcount, sock_flags, sock_type, sock_state, inode); | |
374 if (toys.optflags & FLAG_p) xprintf("%-20s", get_pid_name(inode)); | |
375 | |
376 bptr += path_offset; | |
377 *strchr_nul(bptr, '\n') = '\0'; | |
378 xprintf("%s\n", bptr); | |
379 }//End of while | |
380 fclose(fp); | |
381 } | |
382 /* | |
383 * extract inode value from the link. | |
384 */ | |
385 static long ss_inode(char *link) | |
386 { | |
387 long inode = -1; | |
388 //"link = socket:[12345]", get "12345" as inode. | |
389 if (!strncmp(link, "socket:[", sizeof("socket:[")-1)) { | |
390 inode = get_strtou(link + sizeof("socket:[")-1, (char**)&link, 0); | |
391 if (*link != ']') inode = -1; | |
392 } | |
393 //"link = [0000]:12345", get "12345" as inode. | |
394 else if (!strncmp(link, "[0000]:", sizeof("[0000]:")-1)) { | |
395 inode = get_strtou(link + sizeof("[0000]:")-1, NULL, 0); | |
396 //if not NULL terminated. | |
397 if (errno) inode = -1; | |
398 } | |
399 return inode; | |
400 } | |
401 /* | |
402 * add inode and progname in the pid list. | |
403 */ | |
404 static void add2list(long inode, char *progname) | |
405 { | |
406 PID_LIST *node = pid_list; | |
407 for(; node; node = node->next) { | |
408 if(node->inode == inode) | |
409 return; | |
410 } | |
411 PID_LIST *new = (PID_LIST *)xzalloc(sizeof(PID_LIST)); | |
412 new->inode = inode; | |
413 safe_strncpy(new->name, progname, PROGNAME_LEN-1); | |
414 new->next = pid_list; | |
415 pid_list = new; | |
416 } | |
417 /* | |
418 * add pid info in the list. | |
419 */ | |
420 static void extract_inode(char *path, char *progname) | |
421 { | |
422 DIR *dp; | |
423 struct dirent *entry; | |
424 | |
425 if (!(dp = opendir(path))) { | |
426 if (errno == EACCES) return; | |
427 else perror_exit("%s", path); | |
428 } | |
429 while ((entry = readdir(dp))) { | |
430 char *link = NULL, *fname = append_subpathandfile(path, entry->d_name); | |
431 if (!fname) continue; | |
432 link = xreadlink(fname); | |
433 if (link) { | |
434 long inode = ss_inode(link); | |
435 free(link); | |
436 if (inode != -1) add2list(inode, progname); | |
437 } | |
438 free(fname); | |
439 }//end of while. | |
440 closedir(dp); | |
441 } | |
442 /* | |
443 * prepare the list for all pids in /proc directory. | |
444 */ | |
445 static void get_pid_list(void) | |
446 { | |
447 DIR *dp; | |
448 struct dirent *entry; | |
449 char path[64] = {0,}; | |
450 uid_t uid = geteuid(); | |
451 | |
452 if (!(dp = opendir("/proc"))) perror_exit("opendir"); | |
453 | |
454 while ((entry = readdir(dp))) { | |
455 int fd, nitems = 0, length = 0; | |
456 char *pid, *progname; | |
457 | |
458 if (!isdigit(*entry->d_name)) continue; | |
459 pid = entry->d_name; | |
460 length = snprintf(path, sizeof(path), "/proc/%s/cmdline", entry->d_name); | |
461 if (sizeof(path) <= length) continue; | |
462 | |
463 fd = xopen(path, O_RDONLY); | |
464 nitems = readall(fd, toybuf, sizeof(toybuf) - 1); | |
465 xclose(fd); | |
466 if (nitems < 1) continue; | |
467 toybuf[nitems] = '\0'; | |
468 strcpy(path + length - (sizeof("cmdline")-1), "fd"); | |
469 progname = append_pathandfile(pid, (char *)get_basename(toybuf)); //e.g. progname = 2054/gnome-keyring-daemon | |
470 extract_inode(path, progname); | |
471 }//end of while. | |
472 closedir(dp); | |
473 | |
474 if (uid) fprintf(stderr, "(Not all processes could be identified, non-owned process info " | |
475 "will not be shown, you would have to be root to see it all.)\n"); | |
476 } | |
477 /* | |
478 * Dealloc pid list. | |
479 */ | |
480 static void clean_pid_list(void) | |
481 { | |
482 PID_LIST *tmp; | |
483 while (pid_list) { | |
484 tmp = pid_list->next; | |
485 free(pid_list); | |
486 pid_list = tmp; | |
487 } | |
488 } | |
489 /* | |
490 * For TCP/UDP/RAW show the header. | |
491 */ | |
492 static void show_header(void) | |
493 { | |
494 if ((toys.optflags & FLAG_W) && (toys.optflags & FLAG_p)) | |
495 xprintf("\nProto Recv-Q Send-Q %-51s %-51s %-12s%s\n", "Local Address", "Foreign Address", "State", PROGRAM_NAME); | |
496 else if (toys.optflags & FLAG_p) | |
497 xprintf("\nProto Recv-Q Send-Q %-23s %-23s %-12s%s\n", "Local Address", "Foreign Address", "State", PROGRAM_NAME); | |
498 else if (toys.optflags & FLAG_W) | |
499 xprintf("\nProto Recv-Q Send-Q %-51s %-51s State \n", "Local Address", "Foreign Address"); | |
500 else xprintf("\nProto Recv-Q Send-Q %-23s %-23s State \n", "Local Address", "Foreign Address"); | |
501 } | |
502 /* | |
503 * used to get the flag values for route command. | |
504 */ | |
505 static void get_flag_value(char **flagstr, int flags) | |
506 { | |
507 int i = 0; | |
508 char *str = *flagstr; | |
509 static const char flagchars[] = "GHRDMDAC"; | |
510 static const unsigned flagarray[] = { | |
511 RTF_GATEWAY, | |
512 RTF_HOST, | |
513 RTF_REINSTATE, | |
514 RTF_DYNAMIC, | |
515 RTF_MODIFIED, | |
516 RTF_DEFAULT, | |
517 RTF_ADDRCONF, | |
518 RTF_CACHE | |
519 }; | |
520 *str++ = 'U'; | |
521 while ( (*str = flagchars[i]) ) { | |
522 if (flags & flagarray[i++]) ++str; | |
523 } | |
524 } | |
525 /* | |
526 * extract inet4 route info from /proc/net/route file and display it. | |
527 */ | |
528 static void display_routes(int is_more_info, int notresolve) | |
529 { | |
530 #define IPV4_MASK (RTF_GATEWAY|RTF_HOST|RTF_REINSTATE|RTF_DYNAMIC|RTF_MODIFIED) | |
531 unsigned long dest, gate, mask; | |
532 int flags, ref, use, metric, mss, win, irtt; | |
533 char iface[64]={0,}; | |
534 char *flag_val = xzalloc(10); //there are 9 flags "UGHRDMDAC" for route. | |
535 | |
536 FILE *fp = xfopen("/proc/net/route", "r"); | |
537 xprintf("Kernel IP routing table\n" | |
538 "Destination Gateway Genmask Flags %s Iface\n", | |
539 is_more_info ? " MSS Window irtt" : "Metric Ref Use"); | |
540 fgets(toybuf, sizeof(toybuf), fp); //skip 1st line. | |
541 while (fgets(toybuf, sizeof(toybuf), fp)) { | |
542 int nitems = 0; | |
543 char *destip = NULL, *gateip = NULL, *maskip = NULL; | |
544 memset(flag_val, 0, 10); | |
545 | |
546 nitems = sscanf(toybuf, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n", | |
547 iface, &dest, &gate, &flags, &ref, &use, &metric, &mask, &mss, &win, &irtt); | |
548 if (nitems != 11) {//EOF with no (nonspace) chars read. | |
549 if ((nitems < 0) && feof(fp)) break; | |
550 perror_exit("sscanf"); | |
551 } | |
552 //skip down interfaces. | |
553 if (!(flags & RTF_UP)) continue; | |
554 | |
555 if (dest) {//For Destination | |
556 if (inet_ntop(AF_INET, &dest, toybuf, sizeof(toybuf)) ) destip = xstrdup(toybuf); | |
557 } else { | |
558 if (!notresolve) destip = xstrdup("default"); | |
559 else destip = xstrdup("0.0.0.0"); | |
560 } | |
561 if (gate) {//For Gateway | |
562 if (inet_ntop(AF_INET, &gate, toybuf, sizeof(toybuf)) ) gateip = xstrdup(toybuf); | |
563 } else { | |
564 if (!notresolve) gateip = xstrdup("*"); | |
565 else gateip = xstrdup("0.0.0.0"); | |
566 } | |
567 //For Mask | |
568 if (inet_ntop(AF_INET, &mask, toybuf, sizeof(toybuf)) ) maskip = xstrdup(toybuf); | |
569 | |
570 //Get flag Values | |
571 get_flag_value(&flag_val, (flags & IPV4_MASK)); | |
572 if (flags & RTF_REJECT) flag_val[0] = '!'; | |
573 xprintf("%-15.15s %-15.15s %-16s%-6s", destip, gateip, maskip, flag_val); | |
574 if (destip) free(destip); | |
575 if (gateip) free(gateip); | |
576 if (maskip) free(maskip); | |
577 if (is_more_info) xprintf("%5d %-5d %6d %s\n", mss, win, irtt, iface); | |
578 else xprintf("%-6d %-2d %7d %s\n", metric, ref, use, iface); | |
579 }//end of while. | |
580 fclose(fp); | |
581 if (flag_val) free(flag_val); | |
582 #undef IPV4_MASK | |
583 return; | |
584 } | |
585 /* | |
586 * netstat utily main function. | |
587 */ | |
588 void netstat_main(void) | |
589 { | |
590 #define IS_NETSTAT_PROTO_FLAGS_UP (toys.optflags & (FLAG_t | FLAG_u | FLAG_w | FLAG_x)) | |
591 int flag_listen_and_all = 0; | |
592 if (!toys.optflags) toys.optflags = FLAG_t | FLAG_u | FLAG_w | FLAG_x; | |
593 | |
594 //When a is set | |
595 if (toys.optflags & FLAG_a) flag_listen_and_all = 1; | |
596 //when a and l both are set | |
597 if ( (toys.optflags & FLAG_a) && (toys.optflags & FLAG_l) ) | |
598 toys.optflags &= ~FLAG_l; | |
599 //when only a is set | |
600 if ( (toys.optflags & FLAG_a) && (!IS_NETSTAT_PROTO_FLAGS_UP) ) | |
601 toys.optflags |= FLAG_t | FLAG_u | FLAG_w | FLAG_x; | |
602 //when only l is set | |
603 if ( (toys.optflags & FLAG_l) && (!IS_NETSTAT_PROTO_FLAGS_UP) ) | |
604 toys.optflags |= FLAG_t | FLAG_u | FLAG_w | FLAG_x; | |
605 //when only e/n is set | |
606 if( ((toys.optflags & FLAG_e) || (toys.optflags & FLAG_n)) && (!IS_NETSTAT_PROTO_FLAGS_UP) ) | |
607 toys.optflags |= FLAG_t | FLAG_u | FLAG_w | FLAG_x; | |
608 //when W is set | |
609 if ( (toys.optflags & FLAG_W) && (!IS_NETSTAT_PROTO_FLAGS_UP) ) | |
610 toys.optflags |= FLAG_t | FLAG_u | FLAG_w | FLAG_x; | |
611 //when p is set | |
612 if ( (toys.optflags & FLAG_p) && (!IS_NETSTAT_PROTO_FLAGS_UP) ) | |
613 toys.optflags |= FLAG_t | FLAG_u | FLAG_w | FLAG_x; | |
614 | |
615 //Display routing table. | |
616 if (toys.optflags & FLAG_r) { | |
617 display_routes(!(toys.optflags & FLAG_e), (toys.optflags & FLAG_n)); | |
618 return; | |
619 } | |
620 | |
621 if (toys.optflags & FLAG_p) get_pid_list(); | |
622 | |
623 //For TCP/UDP/RAW. | |
624 if ( (toys.optflags & FLAG_t) || (toys.optflags & FLAG_u) || (toys.optflags & FLAG_w) ) { | |
625 xprintf("Active Internet connections "); | |
626 | |
627 if (flag_listen_and_all) xprintf("(servers and established)"); | |
628 else if (toys.optflags & FLAG_l) xprintf("(only servers)"); | |
629 else xprintf("(w/o servers)"); | |
630 | |
631 show_header(); | |
632 if (toys.optflags & FLAG_t) {//For TCP | |
633 show_ipv4("/proc/net/tcp", "tcp"); | |
634 show_ipv6("/proc/net/tcp6", "tcp"); | |
635 } | |
636 if (toys.optflags & FLAG_u) {//For UDP | |
637 show_ipv4("/proc/net/udp", "udp"); | |
638 show_ipv6("/proc/net/udp6", "udp"); | |
639 } | |
640 if (toys.optflags & FLAG_w) {//For raw | |
641 show_ipv4("/proc/net/raw", "raw"); | |
642 show_ipv6("/proc/net/raw6", "raw"); | |
643 } | |
644 } | |
645 if (toys.optflags & FLAG_x) {//For UNIX. | |
646 xprintf("Active UNIX domain sockets "); | |
647 if (flag_listen_and_all) xprintf("(servers and established)"); | |
648 else if (toys.optflags & FLAG_l) xprintf("(only servers)"); | |
649 else xprintf("(w/o servers)"); | |
650 | |
651 if (toys.optflags & FLAG_p) xprintf("\nProto RefCnt Flags Type State I-Node %s Path\n", PROGRAM_NAME); | |
652 else xprintf("\nProto RefCnt Flags Type State I-Node Path\n"); | |
653 show_unix_sockets("/proc/net/unix", "unix"); | |
654 } | |
655 if (toys.optflags & FLAG_p) clean_pid_list(); | |
656 if (toys.exitval) toys.exitval = 0; | |
657 #undef IS_NETSTAT_PROTO_FLAGS_UP | |
658 } |