netstat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* netstat.c - Display Linux networking subsystem.
  2. *
  3. * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com>
  4. * Copyright 2013 Kyungwan Han <asura321@gmail.com>
  5. *
  6. * Not in SUSv4.
  7. *
  8. USE_NETSTAT(NEWTOY(netstat, "pWrxwutneal", TOYFLAG_BIN))
  9. config NETSTAT
  10. bool "netstat"
  11. default y
  12. help
  13. usage: netstat [-pWrxwutneal]
  14. Display networking information. Default is netstat -tuwx
  15. -r Routing table
  16. -a All sockets (not just connected)
  17. -l Listening server sockets
  18. -t TCP sockets
  19. -u UDP sockets
  20. -w Raw sockets
  21. -x Unix sockets
  22. -e Extended info
  23. -n Don't resolve names
  24. -W Wide display
  25. -p Show PID/program name of sockets
  26. */
  27. #define FOR_netstat
  28. #include "toys.h"
  29. #include <net/route.h>
  30. GLOBALS(
  31. struct num_cache *inodes;
  32. int wpad;
  33. )
  34. static void addr2str(int af, void *addr, unsigned port, char *buf, int len,
  35. char *proto)
  36. {
  37. char pres[INET6_ADDRSTRLEN];
  38. struct servent *se = 0;
  39. int pos, count;
  40. if (!inet_ntop(af, addr, pres, sizeof(pres))) perror_exit("inet_ntop");
  41. if (FLAG(n) || !port) {
  42. strcpy(buf, pres);
  43. } else {
  44. struct addrinfo hints, *result, *rp;
  45. char cut[4];
  46. memset(&hints, 0, sizeof(struct addrinfo));
  47. hints.ai_family = af;
  48. if (!getaddrinfo(pres, NULL, &hints, &result)) {
  49. socklen_t sock_len = (af == AF_INET) ? sizeof(struct sockaddr_in)
  50. : sizeof(struct sockaddr_in6);
  51. // We assume that a failing getnameinfo dosn't stomp "buf" here.
  52. for (rp = result; rp; rp = rp->ai_next)
  53. if (!getnameinfo(rp->ai_addr, sock_len, buf, 256, 0, 0, 0)) break;
  54. freeaddrinfo(result);
  55. buf[len] = 0;
  56. }
  57. // getservbyport() doesn't understand proto "tcp6", so truncate
  58. memcpy(cut, proto, 3);
  59. cut[3] = 0;
  60. se = getservbyport(htons(port), cut);
  61. }
  62. if (!strcmp(buf, "::")) strcpy(buf, "[::]");
  63. // Append :service or :* if port == 0.
  64. if (se) {
  65. count = snprintf(0, 0, ":%s", se->s_name);
  66. // NI_MAXSERV == 32, which is greater than our minimum field width.
  67. // (Although the longest service name on Debian in 2021 is only 16 bytes.)
  68. if (count>=len) {
  69. count = len-1;
  70. se->s_name[count] = 0;
  71. }
  72. } else count = port ? snprintf(0, 0, ":%u", port) : 2;
  73. // We always show the port, even if that means clobbering the end of the host.
  74. pos = strlen(buf);
  75. if (len-pos<count) pos = len-count;
  76. if (se) sprintf(buf+pos, ":%s", se->s_name);
  77. else sprintf(buf+pos, port ? ":%u" : ":*", port);
  78. }
  79. // Display info for tcp/udp/raw
  80. static void show_ip(char *fname)
  81. {
  82. char *ss_state = "UNKNOWN", buf[12], *s, *label = strrchr(fname, '/')+1;
  83. char *state_label[] = {"", "ESTABLISHED", "SYN_SENT", "SYN_RECV", "FIN_WAIT1",
  84. "FIN_WAIT2", "TIME_WAIT", "CLOSE", "CLOSE_WAIT",
  85. "LAST_ACK", "LISTEN", "CLOSING", "UNKNOWN"};
  86. FILE *fp = xfopen(fname, "r");
  87. // Skip header.
  88. fgets(toybuf, sizeof(toybuf), fp);
  89. while (fgets(toybuf, sizeof(toybuf), fp)) {
  90. char lip[256], rip[256];
  91. union {
  92. struct {unsigned u; unsigned char b[4];} i4;
  93. struct {struct {unsigned a, b, c, d;} u; unsigned char b[16];} i6;
  94. } laddr, raddr;
  95. unsigned lport, rport, state, txq, rxq, num, uid, af = AF_INET6;
  96. unsigned long inode;
  97. // Try ipv6, then try ipv4
  98. if (16 != sscanf(toybuf,
  99. " %d: %8x%8x%8x%8x:%x %8x%8x%8x%8x:%x %x %x:%x %*X:%*X %*X %d %*d %ld",
  100. &num, &laddr.i6.u.a, &laddr.i6.u.b, &laddr.i6.u.c,
  101. &laddr.i6.u.d, &lport, &raddr.i6.u.a, &raddr.i6.u.b,
  102. &raddr.i6.u.c, &raddr.i6.u.d, &rport, &state, &txq, &rxq,
  103. &uid, &inode))
  104. {
  105. af = AF_INET;
  106. if (10 != sscanf(toybuf,
  107. " %d: %x:%x %x:%x %x %x:%x %*X:%*X %*X %d %*d %ld",
  108. &num, &laddr.i4.u, &lport, &raddr.i4.u, &rport, &state, &txq,
  109. &rxq, &uid, &inode)) continue;
  110. }
  111. // Should we display this? (listening or all or TCP/UDP/RAW)
  112. if (!(FLAG(l) && (!rport && (state&0xA))) && !FLAG(a) && !(rport&0x70))
  113. continue;
  114. addr2str(af, &laddr, lport, lip, TT.wpad, label);
  115. addr2str(af, &raddr, rport, rip, TT.wpad, label);
  116. // Display data
  117. s = label;
  118. if (strstart(&s, "tcp")) {
  119. int sz = ARRAY_LEN(state_label);
  120. if (!state || state >= sz) state = sz-1;
  121. ss_state = state_label[state];
  122. } else if (strstart(&s, "udp")) {
  123. if (state == 1) ss_state = state_label[state];
  124. else if (state == 7) ss_state = "";
  125. } else if (strstart(&s, "raw")) sprintf(ss_state = buf, "%u", state);
  126. printf("%-6s%6d%7d %*.*s %*.*s %-11s", label, rxq, txq, -TT.wpad, TT.wpad,
  127. lip, -TT.wpad, TT.wpad, rip, ss_state);
  128. if (FLAG(e)) {
  129. if (FLAG(n)) sprintf(s = toybuf, "%d", uid);
  130. else s = getusername(uid);
  131. printf(" %-10s %-11ld", s, inode);
  132. }
  133. if (FLAG(p)) {
  134. struct num_cache *nc = get_num_cache(TT.inodes, inode);
  135. printf(" %s", nc ? nc->data : "-");
  136. }
  137. xputc('\n');
  138. }
  139. fclose(fp);
  140. }
  141. static void show_unix_sockets(void)
  142. {
  143. char *types[] = {"","STREAM","DGRAM","RAW","RDM","SEQPACKET","DCCP","PACKET"},
  144. *states[] = {"","LISTENING","CONNECTING","CONNECTED","DISCONNECTING"},
  145. *filename = 0;
  146. unsigned long refcount, flags, type, state, inode;
  147. FILE *fp = xfopen("/proc/net/unix", "r");
  148. // Skip header.
  149. fgets(toybuf, sizeof(toybuf), fp);
  150. while (fscanf(fp, "%*p: %lX %*X %lX %lX %lX %lu%m[^\n]", &refcount, &flags,
  151. &type, &state, &inode, &filename) >= 5) {
  152. // Linux exports only SO_ACCEPTCON since 2.3.15pre3 in 1999, but let's
  153. // filter in case they add more someday.
  154. flags &= 1<<16;
  155. // Only show unconnected listening sockets with -a or -l.
  156. if (state==1 && flags && !(FLAG(a) || FLAG(l))) continue;
  157. if (type==10) type = 7; // move SOCK_PACKET into line
  158. if (type>=ARRAY_LEN(types)) type = 0;
  159. if (state>=ARRAY_LEN(states) || (state==1 && !flags)) state = 0;
  160. if (state!=1 && FLAG(l)) continue;
  161. sprintf(toybuf, "[ %s]", flags ? "ACC " : "");
  162. printf("unix %-6ld %-11s %-10s %-13s %-8lu ",
  163. refcount, toybuf, types[type], states[state], inode);
  164. if (FLAG(p)) {
  165. struct num_cache *nc = get_num_cache(TT.inodes, inode);
  166. printf("%-19.19s ", nc ? nc->data : "-");
  167. }
  168. if (filename) {
  169. printf("%s\n", filename+!FLAG(p));
  170. free(filename);
  171. filename = 0;
  172. } else xputc('\n');
  173. }
  174. fclose(fp);
  175. }
  176. static int scan_pids(struct dirtree *node)
  177. {
  178. char *s = toybuf+256;
  179. struct dirent *entry;
  180. DIR *dp;
  181. int pid, dirfd;
  182. if (!node->parent) return DIRTREE_RECURSE;
  183. if (!(pid = atol(node->name))) return 0;
  184. sprintf(toybuf, "/proc/%d/cmdline", pid);
  185. if (!(readfile(toybuf, toybuf, 256))) return 0;
  186. sprintf(s, "%d/fd", pid);
  187. if (-1==(dirfd = openat(dirtree_parentfd(node), s, O_RDONLY))) return 0;
  188. if (!(dp = fdopendir(dirfd))) close(dirfd);
  189. else while ((entry = readdir(dp))) {
  190. s = toybuf+256;
  191. if (!readlinkat0(dirfd, entry->d_name, s, sizeof(toybuf)-256)) continue;
  192. // Can the "[0000]:" happen in a modern kernel?
  193. if (strstart(&s, "socket:[") || strstart(&s, "[0000]:")) {
  194. long long ll = atoll(s);
  195. sprintf(s, "%d/%s", pid, getbasename(toybuf));
  196. add_num_cache(&TT.inodes, ll, s, strlen(s)+1);
  197. }
  198. }
  199. closedir(dp);
  200. return 0;
  201. }
  202. // extract inet4 route info from /proc/net/route file and display it.
  203. static void display_routes(void)
  204. {
  205. static const char flagchars[] = "GHRDMDAC";
  206. static const unsigned flagarray[] = {
  207. RTF_GATEWAY, RTF_HOST, RTF_REINSTATE, RTF_DYNAMIC, RTF_MODIFIED
  208. };
  209. unsigned dest, gate, mask;
  210. int flags, ref, use, metric, mss, win, irtt;
  211. char *out = toybuf, *flag_val;
  212. char iface[64]={0};
  213. FILE *fp = xfopen("/proc/net/route", "r");
  214. // Skip header.
  215. fgets(toybuf, sizeof(toybuf), fp);
  216. printf("Kernel IP routing table\n"
  217. "Destination\tGateway \tGenmask \tFlags %s Iface\n",
  218. !FLAG(e) ? " MSS Window irtt" : "Metric Ref Use");
  219. while (fscanf(fp, "%63s%x%x%X%d%d%d%x%d%d%d", iface, &dest, &gate, &flags,
  220. &ref, &use, &metric, &mask, &mss, &win, &irtt) == 11) {
  221. char *destip = 0, *gateip = 0, *maskip = 0;
  222. // skip down interfaces.
  223. if (!(flags & RTF_UP)) continue;
  224. // TODO /proc/net/ipv6_route
  225. if (dest) {
  226. if (inet_ntop(AF_INET, &dest, out, 16)) destip = out;
  227. } else destip = FLAG(n) ? "0.0.0.0" : "default";
  228. out += 16;
  229. if (gate) {
  230. if (inet_ntop(AF_INET, &gate, out, 16)) gateip = out;
  231. } else gateip = FLAG(n) ? "0.0.0.0" : "*";
  232. out += 16;
  233. // TODO /24
  234. //For Mask
  235. if (inet_ntop(AF_INET, &mask, out, 16)) maskip = out;
  236. else maskip = "?";
  237. out += 16;
  238. //Get flag Values
  239. flag_val = out;
  240. *out++ = 'U';
  241. for (dest = 0; dest < ARRAY_LEN(flagarray); dest++)
  242. if (flags&flagarray[dest]) *out++ = flagchars[dest];
  243. *out = 0;
  244. if (flags & RTF_REJECT) *flag_val = '!';
  245. printf("%-15.15s %-15.15s %-16s%-6s", destip, gateip, maskip, flag_val);
  246. if (!FLAG(e)) printf("%5d %-5d %6d %s\n", mss, win, irtt, iface);
  247. else printf("%-6d %-2d %7d %s\n", metric, ref, use, iface);
  248. }
  249. fclose(fp);
  250. }
  251. void netstat_main(void)
  252. {
  253. int tuwx = FLAG_t|FLAG_u|FLAG_w|FLAG_x;
  254. char *type = "w/o servers";
  255. TT.wpad = FLAG(W) ? 51 : 23;
  256. if (!(toys.optflags&(FLAG_r|tuwx))) toys.optflags |= tuwx;
  257. if (FLAG(r)) display_routes();
  258. if (!(toys.optflags&tuwx)) return;
  259. if (FLAG(a)) type = "servers and established";
  260. else if (FLAG(l)) type = "only servers";
  261. if (FLAG(p)) dirtree_read("/proc", scan_pids);
  262. if (toys.optflags&(FLAG_t|FLAG_u|FLAG_w)) {
  263. printf("Active Internet connections (%s)\n", type);
  264. printf("Proto Recv-Q Send-Q %*s %*s State ", -TT.wpad, "Local Address",
  265. -TT.wpad, "Foreign Address");
  266. if (FLAG(e)) printf(" User Inode ");
  267. if (FLAG(p)) printf(" PID/Program Name");
  268. xputc('\n');
  269. if (FLAG(t)) {
  270. show_ip("/proc/net/tcp");
  271. show_ip("/proc/net/tcp6");
  272. }
  273. if (FLAG(u)) {
  274. show_ip("/proc/net/udp");
  275. show_ip("/proc/net/udp6");
  276. }
  277. if (FLAG(w)) {
  278. show_ip("/proc/net/raw");
  279. show_ip("/proc/net/raw6");
  280. }
  281. }
  282. if (FLAG(x)) {
  283. printf("Active UNIX domain sockets (%s)\n", type);
  284. printf("Proto RefCnt Flags Type State I-Node%sPath\n",
  285. FLAG(p) ? " PID/Program Name " : " ");
  286. show_unix_sockets();
  287. }
  288. if (FLAG(p) && CFG_TOYBOX_FREE) llist_traverse(TT.inodes, free);
  289. toys.exitval = 0;
  290. }