1 /* netstat.c - Display Linux networking subsystem.
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com>
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
8 USE_NETSTAT(NEWTOY(netstat, "pWrxwutneal", TOYFLAG_BIN))
13 usage: netstat [-pWrxwutneal]
15 Display networking information. Default is netsat -tuwx
18 -a all sockets (not just connected)
19 -l listening server sockets
25 -n don't resolve names
27 -p PID/Program name of sockets
32 #include <net/route.h>
35 struct num_cache *inodes;
39 // convert address into text format.
40 static void addr2str(int af, void *addr, unsigned port, char *buf, int len,
44 struct servent *ser = 0;
46 // Convert to numeric address
47 if (!inet_ntop(af, addr, buf, 256)) {
55 // If there's no port number, it's a local :* binding, nothing to look up.
57 if (len-pos<2) pos = len-2;
58 strcpy(buf+pos, ":*");
63 if (!(toys.optflags & FLAG_n)) {
64 struct addrinfo hints, *result, *rp;
67 memset(&hints, 0, sizeof(struct addrinfo));
70 if (!getaddrinfo(buf, NULL, &hints, &result)) {
71 socklen_t sock_len = (af == AF_INET) ? sizeof(struct sockaddr_in)
72 : sizeof(struct sockaddr_in6);
74 // We assume that a failing getnameinfo dosn't stomp "buf" here.
75 for (rp = result; rp; rp = rp->ai_next)
76 if (!getnameinfo(rp->ai_addr, sock_len, buf, 256, 0, 0, 0)) break;
82 // Doesn't understand proto "tcp6", so truncate
83 memcpy(cut, proto, 3);
85 ser = getservbyport(htons(port), cut);
89 count = snprintf(0, 0, ":%u", port);
91 count = snprintf(0, 0, ":%s", ser->s_name);
95 ser->s_name[count] = 0;
98 if (len-pos<count) pos = len-count;
99 if (ser) sprintf(buf+pos, ":%s", ser->s_name);
100 else sprintf(buf+pos, ":%u", port);
103 // Display info for tcp/udp/raw
104 static void show_ip(char *fname)
106 char *ss_state = "UNKNOWN", buf[12], *s, *label = strrchr(fname, '/')+1;
107 char *state_label[] = {"", "ESTABLISHED", "SYN_SENT", "SYN_RECV", "FIN_WAIT1",
108 "FIN_WAIT2", "TIME_WAIT", "CLOSE", "CLOSE_WAIT",
109 "LAST_ACK", "LISTEN", "CLOSING", "UNKNOWN"};
111 FILE *fp = fopen(fname, "r");
114 perror_msg("'%s'", fname);
118 if(!fgets(toybuf, sizeof(toybuf), fp)) return; //skip header.
120 while (fgets(toybuf, sizeof(toybuf), fp)) {
121 char lip[256], rip[256];
123 struct {unsigned u; unsigned char b[4];} i4;
124 struct {struct {unsigned a, b, c, d;} u; unsigned char b[16];} i6;
126 unsigned lport, rport, state, txq, rxq, num, uid, nitems;
129 // Try ipv6, then try ipv4
130 nitems = sscanf(toybuf,
131 " %d: %8x%8x%8x%8x:%x %8x%8x%8x%8x:%x %x %x:%x %*X:%*X %*X %d %*d %ld",
132 &num, &laddr.i6.u.a, &laddr.i6.u.b, &laddr.i6.u.c,
133 &laddr.i6.u.d, &lport, &raddr.i6.u.a, &raddr.i6.u.b,
134 &raddr.i6.u.c, &raddr.i6.u.d, &rport, &state, &txq, &rxq,
138 nitems = sscanf(toybuf,
139 " %d: %x:%x %x:%x %x %x:%x %*X:%*X %*X %d %*d %ld",
140 &num, &laddr.i4.u, &lport, &raddr.i4.u, &rport, &state, &txq,
143 if (nitems!=10) continue;
145 } else nitems = AF_INET6;
147 // Should we display this? (listening or all or TCP/UDP/RAW)
148 if (!((toys.optflags & FLAG_l) && (!rport && (state & 0xA)))
149 && !(toys.optflags & FLAG_a) && !(rport & (0x10 | 0x20 | 0x40)))
152 addr2str(nitems, &laddr, lport, lip, TT.wpad, label);
153 addr2str(nitems, &raddr, rport, rip, TT.wpad, label);
157 if (strstart(&s, "tcp")) {
158 int sz = ARRAY_LEN(state_label);
159 if (!state || state >= sz) state = sz-1;
160 ss_state = state_label[state];
161 } else if (strstart(&s, "udp")) {
162 if (state == 1) ss_state = state_label[state];
163 else if (state == 7) ss_state = "";
164 } else if (strstart(&s, "raw")) sprintf(ss_state = buf, "%u", state);
166 if (!(toys.optflags & FLAG_n) && (pw = bufgetpwuid(uid)))
167 snprintf(toybuf, sizeof(toybuf), "%s", pw->pw_name);
168 else snprintf(toybuf, sizeof(toybuf), "%d", uid);
170 printf("%-6s%6d%7d ", label, rxq, txq);
171 printf("%*.*s %*.*s ", -TT.wpad, TT.wpad, lip, -TT.wpad, TT.wpad, rip);
172 printf("%-11s", ss_state);
173 if ((toys.optflags & FLAG_e)) printf(" %-10s %-11ld", toybuf, inode);
174 if ((toys.optflags & FLAG_p)) {
175 struct num_cache *nc = get_num_cache(TT.inodes, inode);
177 printf(" %s", nc ? nc->data : "-");
184 static void show_unix_sockets(void)
186 char *types[] = {"","STREAM","DGRAM","RAW","RDM","SEQPACKET","DCCP","PACKET"},
187 *states[] = {"","LISTENING","CONNECTING","CONNECTED","DISCONNECTING"},
189 unsigned long refcount, flags, type, state, inode;
190 FILE *fp = xfopen("/proc/net/unix", "r");
192 if(!fgets(toybuf, sizeof(toybuf), fp)) return; //skip header.
194 while (fgets(toybuf, sizeof(toybuf), fp)) {
197 // count = 6 or 7 (first field ignored, sockets don't always have filenames)
198 if (6<sscanf(toybuf, "%*p: %lX %*X %lX %lX %lX %lu %n",
199 &refcount, &flags, &type, &state, &inode, &offset))
202 // Linux exports only SO_ACCEPTCON since 2.3.15pre3 in 1999, but let's
203 // filter in case they add more someday.
206 // Only show unconnected listening sockets with -a
207 if (state==1 && flags && !(toys.optflags&FLAG_a)) continue;
209 if (type==10) type = 7; // move SOCK_PACKET into line
210 if (type>ARRAY_LEN(types)) type = 0;
211 if (state>ARRAY_LEN(states) || (state==1 && !flags)) state = 0;
212 sprintf(toybuf, "[ %s]", flags ? "ACC " : "");
214 printf("unix %-6ld %-11s %-10s %-13s %8lu ",
215 refcount, toybuf, types[type], states[state], inode);
216 if (toys.optflags & FLAG_p) {
217 struct num_cache *nc = get_num_cache(TT.inodes, inode);
219 printf("%-19.19s", nc ? nc->data : "-");
223 if ((ss = strrchr(s = toybuf+offset, '\n'))) *ss = 0;
232 static int scan_pids(struct dirtree *node)
234 char *s = toybuf+256;
235 struct dirent *entry;
239 if (!node->parent) return DIRTREE_RECURSE;
240 if (!(pid = atol(node->name))) return 0;
242 sprintf(toybuf, "/proc/%d/cmdline", pid);
243 if (!(readfile(toybuf, toybuf, 256))) return 0;
245 sprintf(s, "%d/fd", pid);
246 if (-1==(dirfd = openat(dirtree_parentfd(node), s, O_RDONLY))) return 0;
247 if (!(dp = fdopendir(dirfd))) {
253 while ((entry = readdir(dp))) {
255 if (!readlinkat0(dirfd, entry->d_name, s, sizeof(toybuf)-256)) continue;
256 // Can the "[0000]:" happen in a modern kernel?
257 if (strstart(&s, "socket:[") || strstart(&s, "[0000]:")) {
258 long long ll = atoll(s);
260 sprintf(s, "%d/%s", pid, getbasename(toybuf));
261 add_num_cache(&TT.inodes, ll, s, strlen(s)+1);
270 * extract inet4 route info from /proc/net/route file and display it.
272 static void display_routes(void)
274 static const char flagchars[] = "GHRDMDAC";
275 static const unsigned flagarray[] = {
276 RTF_GATEWAY, RTF_HOST, RTF_REINSTATE, RTF_DYNAMIC, RTF_MODIFIED
278 unsigned long dest, gate, mask;
279 int flags, ref, use, metric, mss, win, irtt;
280 char *out = toybuf, *flag_val;
282 FILE *fp = xfopen("/proc/net/route", "r");
284 if(!fgets(toybuf, sizeof(toybuf), fp)) return; //skip header.
286 printf("Kernel IP routing table\n"
287 "Destination\tGateway \tGenmask \tFlags %s Iface\n",
288 !(toys.optflags&FLAG_e) ? " MSS Window irtt" : "Metric Ref Use");
290 while (fgets(toybuf, sizeof(toybuf), fp)) {
291 char *destip = 0, *gateip = 0, *maskip = 0;
293 if (11 != sscanf(toybuf, "%63s%lx%lx%X%d%d%d%lx%d%d%d", iface, &dest,
294 &gate, &flags, &ref, &use, &metric, &mask, &mss, &win, &irtt))
297 // skip down interfaces.
298 if (!(flags & RTF_UP)) continue;
300 // TODO /proc/net/ipv6_route
303 if (inet_ntop(AF_INET, &dest, out, 16)) destip = out;
304 } else destip = (toys.optflags&FLAG_n) ? "0.0.0.0" : "default";
308 if (inet_ntop(AF_INET, &gate, out, 16)) gateip = out;
309 } else gateip = (toys.optflags&FLAG_n) ? "0.0.0.0" : "*";
314 if (inet_ntop(AF_INET, &mask, out, 16)) maskip = out;
321 for (dest = 0; dest < ARRAY_LEN(flagarray); dest++)
322 if (flags&flagarray[dest]) *out++ = flagchars[dest];
324 if (flags & RTF_REJECT) *flag_val = '!';
326 printf("%-15.15s %-15.15s %-16s%-6s", destip, gateip, maskip, flag_val);
327 if (!(toys.optflags & FLAG_e))
328 printf("%5d %-5d %6d %s\n", mss, win, irtt, iface);
329 else printf("%-6d %-2d %7d %s\n", metric, ref, use, iface);
335 void netstat_main(void)
337 int tuwx = FLAG_t|FLAG_u|FLAG_w|FLAG_x;
340 TT.wpad = (toys.optflags&FLAG_W) ? 51 : 23;
341 if (!(toys.optflags&(FLAG_r|tuwx))) toys.optflags |= tuwx;
342 if (toys.optflags & FLAG_r) display_routes();
343 if (!(toys.optflags&tuwx)) return;
345 if (toys.optflags & FLAG_a) type = "established and";
346 else if (toys.optflags & FLAG_l) type = "only";
348 if (toys.optflags & FLAG_p) dirtree_read("/proc", scan_pids);
350 if (toys.optflags&(FLAG_t|FLAG_u|FLAG_w)) {
351 printf("Active %s (%s servers)\n", "Internet connections", type);
352 printf("Proto Recv-Q Send-Q %*s %*s State ", -TT.wpad, "Local Address",
353 -TT.wpad, "Foreign Address");
354 if (toys.optflags & FLAG_e) printf(" User Inode ");
355 if (toys.optflags & FLAG_p) printf(" PID/Program Name");
358 if (toys.optflags & FLAG_t) {
359 show_ip("/proc/net/tcp");
360 show_ip("/proc/net/tcp6");
362 if (toys.optflags & FLAG_u) {
363 show_ip("/proc/net/udp");
364 show_ip("/proc/net/udp6");
366 if (toys.optflags & FLAG_w) {
367 show_ip("/proc/net/raw");
368 show_ip("/proc/net/raw6");
372 if (toys.optflags & FLAG_x) {
373 printf("Active %s (%s servers)\n", "UNIX domain sockets", type);
375 printf("Proto RefCnt Flags\t Type\t State\t %s Path\n",
376 (toys.optflags&FLAG_p) ? "PID/Program Name" : "I-Node");
380 if ((toys.optflags & FLAG_p) && CFG_TOYBOX_FREE)
381 llist_traverse(TT.inodes, free);