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