netstat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini netstat implementation(s) for busybox
  4. * based in part on the netstat implementation from net-tools.
  5. *
  6. * Copyright (C) 2002 by Bart Visscher <magick@linux-fan.com>
  7. *
  8. * 2002-04-20
  9. * IPV6 support added by Bart Visscher <magick@linux-fan.com>
  10. *
  11. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  12. */
  13. #include "libbb.h"
  14. #include "inet_common.h"
  15. enum {
  16. OPT_extended = 0x4,
  17. OPT_showroute = 0x100,
  18. OPT_widedisplay = 0x200 * ENABLE_FEATURE_NETSTAT_WIDE,
  19. };
  20. # define NETSTAT_OPTS "laentuwxr"USE_FEATURE_NETSTAT_WIDE("W")
  21. #define NETSTAT_CONNECTED 0x01
  22. #define NETSTAT_LISTENING 0x02
  23. #define NETSTAT_NUMERIC 0x04
  24. /* Must match getopt32 option string */
  25. #define NETSTAT_TCP 0x10
  26. #define NETSTAT_UDP 0x20
  27. #define NETSTAT_RAW 0x40
  28. #define NETSTAT_UNIX 0x80
  29. #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
  30. static smallint flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO;
  31. enum {
  32. TCP_ESTABLISHED = 1,
  33. TCP_SYN_SENT,
  34. TCP_SYN_RECV,
  35. TCP_FIN_WAIT1,
  36. TCP_FIN_WAIT2,
  37. TCP_TIME_WAIT,
  38. TCP_CLOSE,
  39. TCP_CLOSE_WAIT,
  40. TCP_LAST_ACK,
  41. TCP_LISTEN,
  42. TCP_CLOSING /* now a valid state */
  43. };
  44. static const char *const tcp_state[] = {
  45. "",
  46. "ESTABLISHED",
  47. "SYN_SENT",
  48. "SYN_RECV",
  49. "FIN_WAIT1",
  50. "FIN_WAIT2",
  51. "TIME_WAIT",
  52. "CLOSE",
  53. "CLOSE_WAIT",
  54. "LAST_ACK",
  55. "LISTEN",
  56. "CLOSING"
  57. };
  58. typedef enum {
  59. SS_FREE = 0, /* not allocated */
  60. SS_UNCONNECTED, /* unconnected to any socket */
  61. SS_CONNECTING, /* in process of connecting */
  62. SS_CONNECTED, /* connected to socket */
  63. SS_DISCONNECTING /* in process of disconnecting */
  64. } socket_state;
  65. #define SO_ACCEPTCON (1<<16) /* performed a listen */
  66. #define SO_WAITDATA (1<<17) /* wait data to read */
  67. #define SO_NOSPACE (1<<18) /* no space to write */
  68. /* Standard printout size */
  69. #define PRINT_IP_MAX_SIZE 23
  70. #define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s\n"
  71. #define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State\n"
  72. /* When there are IPv6 connections the IPv6 addresses will be
  73. * truncated to none-recognition. The '-W' option makes the
  74. * address columns wide enough to accomodate for longest possible
  75. * IPv6 addresses, i.e. addresses of the form
  76. * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
  77. */
  78. #define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
  79. #define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s\n"
  80. #define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State\n"
  81. static const char *net_conn_line = PRINT_NET_CONN;
  82. #if ENABLE_FEATURE_IPV6
  83. static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
  84. {
  85. char addr6[INET6_ADDRSTRLEN];
  86. struct in6_addr in6;
  87. sscanf(local_addr, "%08X%08X%08X%08X",
  88. &in6.s6_addr32[0], &in6.s6_addr32[1],
  89. &in6.s6_addr32[2], &in6.s6_addr32[3]);
  90. inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
  91. inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr->sin6_addr);
  92. localaddr->sin6_family = AF_INET6;
  93. }
  94. #endif
  95. #if ENABLE_FEATURE_IPV6
  96. static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr)
  97. #else
  98. static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
  99. #endif
  100. {
  101. sscanf(local_addr, "%X",
  102. &((struct sockaddr_in *) localaddr)->sin_addr.s_addr);
  103. ((struct sockaddr *) localaddr)->sa_family = AF_INET;
  104. }
  105. static const char *get_sname(int port, const char *proto, int numeric)
  106. {
  107. if (!port)
  108. return "*";
  109. if (!numeric) {
  110. struct servent *se = getservbyport(port, proto);
  111. if (se)
  112. return se->s_name;
  113. }
  114. /* hummm, we may return static buffer here!! */
  115. return itoa(ntohs(port));
  116. }
  117. static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
  118. {
  119. enum { salen = USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) };
  120. char *host, *host_port;
  121. /* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6,
  122. * while "0.0.0.0" is not. */
  123. host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
  124. : xmalloc_sockaddr2host_noport(addr);
  125. host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
  126. free(host);
  127. return host_port;
  128. }
  129. static void tcp_do_one(int lnr, const char *line)
  130. {
  131. char local_addr[64], rem_addr[64];
  132. char more[512];
  133. int num, local_port, rem_port, d, state, timer_run, uid, timeout;
  134. #if ENABLE_FEATURE_IPV6
  135. struct sockaddr_in6 localaddr, remaddr;
  136. #else
  137. struct sockaddr_in localaddr, remaddr;
  138. #endif
  139. unsigned long rxq, txq, time_len, retr, inode;
  140. if (lnr == 0)
  141. return;
  142. more[0] = '\0';
  143. num = sscanf(line,
  144. "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
  145. &d, local_addr, &local_port,
  146. rem_addr, &rem_port, &state,
  147. &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
  148. if (strlen(local_addr) > 8) {
  149. #if ENABLE_FEATURE_IPV6
  150. build_ipv6_addr(local_addr, &localaddr);
  151. build_ipv6_addr(rem_addr, &remaddr);
  152. #endif
  153. } else {
  154. build_ipv4_addr(local_addr, &localaddr);
  155. build_ipv4_addr(rem_addr, &remaddr);
  156. }
  157. if (num < 10) {
  158. bb_error_msg("warning, got bogus tcp line");
  159. return;
  160. }
  161. if ((rem_port && (flags & NETSTAT_CONNECTED))
  162. || (!rem_port && (flags & NETSTAT_LISTENING))
  163. ) {
  164. char *l = ip_port_str(
  165. (struct sockaddr *) &localaddr, local_port,
  166. "tcp", flags & NETSTAT_NUMERIC);
  167. char *r = ip_port_str(
  168. (struct sockaddr *) &remaddr, rem_port,
  169. "tcp", flags & NETSTAT_NUMERIC);
  170. printf(net_conn_line,
  171. "tcp", rxq, txq, l, r, tcp_state[state]);
  172. free(l);
  173. free(r);
  174. }
  175. }
  176. static void udp_do_one(int lnr, const char *line)
  177. {
  178. char local_addr[64], rem_addr[64];
  179. const char *state_str;
  180. char more[512];
  181. int num, local_port, rem_port, d, state, timer_run, uid, timeout;
  182. #if ENABLE_FEATURE_IPV6
  183. struct sockaddr_in6 localaddr, remaddr;
  184. #else
  185. struct sockaddr_in localaddr, remaddr;
  186. #endif
  187. unsigned long rxq, txq, time_len, retr, inode;
  188. if (lnr == 0)
  189. return;
  190. more[0] = '\0';
  191. num = sscanf(line,
  192. "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
  193. &d, local_addr, &local_port,
  194. rem_addr, &rem_port, &state,
  195. &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
  196. if (strlen(local_addr) > 8) {
  197. #if ENABLE_FEATURE_IPV6
  198. /* Demangle what the kernel gives us */
  199. build_ipv6_addr(local_addr, &localaddr);
  200. build_ipv6_addr(rem_addr, &remaddr);
  201. #endif
  202. } else {
  203. build_ipv4_addr(local_addr, &localaddr);
  204. build_ipv4_addr(rem_addr, &remaddr);
  205. }
  206. if (num < 10) {
  207. bb_error_msg("warning, got bogus udp line");
  208. return;
  209. }
  210. switch (state) {
  211. case TCP_ESTABLISHED:
  212. state_str = "ESTABLISHED";
  213. break;
  214. case TCP_CLOSE:
  215. state_str = "";
  216. break;
  217. default:
  218. state_str = "UNKNOWN";
  219. break;
  220. }
  221. #if ENABLE_FEATURE_IPV6
  222. # define notnull(A) ( \
  223. ( (A.sin6_family == AF_INET6) \
  224. && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
  225. A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
  226. ) || ( \
  227. (A.sin6_family == AF_INET) \
  228. && ((struct sockaddr_in*)&A)->sin_addr.s_addr \
  229. ) \
  230. )
  231. #else
  232. # define notnull(A) (A.sin_addr.s_addr)
  233. #endif
  234. {
  235. int have_remaddr = notnull(remaddr);
  236. if ((have_remaddr && (flags & NETSTAT_CONNECTED))
  237. || (!have_remaddr && (flags & NETSTAT_LISTENING))
  238. ) {
  239. char *l = ip_port_str(
  240. (struct sockaddr *) &localaddr, local_port,
  241. "udp", flags & NETSTAT_NUMERIC);
  242. char *r = ip_port_str(
  243. (struct sockaddr *) &remaddr, rem_port,
  244. "udp", flags & NETSTAT_NUMERIC);
  245. printf(net_conn_line,
  246. "udp", rxq, txq, l, r, state_str);
  247. free(l);
  248. free(r);
  249. }
  250. }
  251. }
  252. static void raw_do_one(int lnr, const char *line)
  253. {
  254. char local_addr[64], rem_addr[64];
  255. char more[512];
  256. int num, local_port, rem_port, d, state, timer_run, uid, timeout;
  257. #if ENABLE_FEATURE_IPV6
  258. struct sockaddr_in6 localaddr, remaddr;
  259. #else
  260. struct sockaddr_in localaddr, remaddr;
  261. #endif
  262. unsigned long rxq, txq, time_len, retr, inode;
  263. if (lnr == 0)
  264. return;
  265. more[0] = '\0';
  266. num = sscanf(line,
  267. "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
  268. &d, local_addr, &local_port,
  269. rem_addr, &rem_port, &state,
  270. &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
  271. if (strlen(local_addr) > 8) {
  272. #if ENABLE_FEATURE_IPV6
  273. build_ipv6_addr(local_addr, &localaddr);
  274. build_ipv6_addr(rem_addr, &remaddr);
  275. #endif
  276. } else {
  277. build_ipv4_addr(local_addr, &localaddr);
  278. build_ipv4_addr(rem_addr, &remaddr);
  279. }
  280. if (num < 10) {
  281. bb_error_msg("warning, got bogus raw line");
  282. return;
  283. }
  284. {
  285. int have_remaddr = notnull(remaddr);
  286. if ((have_remaddr && (flags & NETSTAT_CONNECTED))
  287. || (!have_remaddr && (flags & NETSTAT_LISTENING))
  288. ) {
  289. char *l = ip_port_str(
  290. (struct sockaddr *) &localaddr, local_port,
  291. "raw", flags & NETSTAT_NUMERIC);
  292. char *r = ip_port_str(
  293. (struct sockaddr *) &remaddr, rem_port,
  294. "raw", flags & NETSTAT_NUMERIC);
  295. printf(net_conn_line,
  296. "raw", rxq, txq, l, r, itoa(state));
  297. free(l);
  298. free(r);
  299. }
  300. }
  301. }
  302. static void unix_do_one(int nr, const char *line)
  303. {
  304. static smallint has_inode = 0;
  305. char path[PATH_MAX], ss_flags[32];
  306. const char *ss_proto, *ss_state, *ss_type;
  307. int num, state, type, inode;
  308. void *d;
  309. unsigned long refcnt, proto, unix_flags;
  310. if (nr == 0) {
  311. if (strstr(line, "Inode"))
  312. has_inode = 1;
  313. return;
  314. }
  315. path[0] = '\0';
  316. num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s",
  317. &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path);
  318. if (num < 6) {
  319. bb_error_msg("warning, got bogus unix line");
  320. return;
  321. }
  322. if (!has_inode)
  323. sprintf(path, "%d", inode);
  324. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
  325. if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
  326. if (!(flags & NETSTAT_LISTENING))
  327. return;
  328. } else {
  329. if (!(flags & NETSTAT_CONNECTED))
  330. return;
  331. }
  332. }
  333. switch (proto) {
  334. case 0:
  335. ss_proto = "unix";
  336. break;
  337. default:
  338. ss_proto = "??";
  339. }
  340. switch (type) {
  341. case SOCK_STREAM:
  342. ss_type = "STREAM";
  343. break;
  344. case SOCK_DGRAM:
  345. ss_type = "DGRAM";
  346. break;
  347. case SOCK_RAW:
  348. ss_type = "RAW";
  349. break;
  350. case SOCK_RDM:
  351. ss_type = "RDM";
  352. break;
  353. case SOCK_SEQPACKET:
  354. ss_type = "SEQPACKET";
  355. break;
  356. default:
  357. ss_type = "UNKNOWN";
  358. }
  359. switch (state) {
  360. case SS_FREE:
  361. ss_state = "FREE";
  362. break;
  363. case SS_UNCONNECTED:
  364. /*
  365. * Unconnected sockets may be listening
  366. * for something.
  367. */
  368. if (unix_flags & SO_ACCEPTCON) {
  369. ss_state = "LISTENING";
  370. } else {
  371. ss_state = "";
  372. }
  373. break;
  374. case SS_CONNECTING:
  375. ss_state = "CONNECTING";
  376. break;
  377. case SS_CONNECTED:
  378. ss_state = "CONNECTED";
  379. break;
  380. case SS_DISCONNECTING:
  381. ss_state = "DISCONNECTING";
  382. break;
  383. default:
  384. ss_state = "UNKNOWN";
  385. }
  386. strcpy(ss_flags, "[ ");
  387. if (unix_flags & SO_ACCEPTCON)
  388. strcat(ss_flags, "ACC ");
  389. if (unix_flags & SO_WAITDATA)
  390. strcat(ss_flags, "W ");
  391. if (unix_flags & SO_NOSPACE)
  392. strcat(ss_flags, "N ");
  393. strcat(ss_flags, "]");
  394. printf("%-5s %-6ld %-11s %-10s %-13s ",
  395. ss_proto, refcnt, ss_flags, ss_type, ss_state);
  396. if (has_inode)
  397. printf("%-6d ", inode);
  398. else
  399. printf("- ");
  400. puts(path);
  401. }
  402. #define _PATH_PROCNET_UDP "/proc/net/udp"
  403. #define _PATH_PROCNET_UDP6 "/proc/net/udp6"
  404. #define _PATH_PROCNET_TCP "/proc/net/tcp"
  405. #define _PATH_PROCNET_TCP6 "/proc/net/tcp6"
  406. #define _PATH_PROCNET_RAW "/proc/net/raw"
  407. #define _PATH_PROCNET_RAW6 "/proc/net/raw6"
  408. #define _PATH_PROCNET_UNIX "/proc/net/unix"
  409. static void do_info(const char *file, const char *name, void (*proc)(int, const char *))
  410. {
  411. int lnr = 0;
  412. FILE *procinfo;
  413. procinfo = fopen(file, "r");
  414. if (procinfo == NULL) {
  415. if (errno != ENOENT) {
  416. bb_simple_perror_msg(file);
  417. } else {
  418. bb_error_msg("no support for '%s' on this system", name);
  419. }
  420. return;
  421. }
  422. do {
  423. char *buffer = xmalloc_fgets(procinfo);
  424. if (buffer) {
  425. (proc)(lnr++, buffer);
  426. free(buffer);
  427. }
  428. } while (!feof(procinfo));
  429. fclose(procinfo);
  430. }
  431. /*
  432. * Our main function.
  433. */
  434. int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  435. int netstat_main(int argc, char **argv)
  436. {
  437. const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
  438. unsigned opt;
  439. #if ENABLE_FEATURE_IPV6
  440. smallint inet = 1;
  441. smallint inet6 = 1;
  442. #else
  443. enum { inet = 1, inet6 = 0 };
  444. #endif
  445. /* Option string must match NETSTAT_xxx constants */
  446. opt = getopt32(argv, NETSTAT_OPTS);
  447. if (opt & 0x1) { // -l
  448. flags &= ~NETSTAT_CONNECTED;
  449. flags |= NETSTAT_LISTENING;
  450. }
  451. if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
  452. //if (opt & 0x4) // -e
  453. if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
  454. //if (opt & 0x10) // -t: NETSTAT_TCP
  455. //if (opt & 0x20) // -u: NETSTAT_UDP
  456. //if (opt & 0x40) // -w: NETSTAT_RAW
  457. //if (opt & 0x80) // -x: NETSTAT_UNIX
  458. if (opt & OPT_showroute) { // -r
  459. #if ENABLE_ROUTE
  460. bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
  461. return 0;
  462. #else
  463. bb_show_usage();
  464. #endif
  465. }
  466. if (opt & OPT_widedisplay) { // -W
  467. net_conn_line = PRINT_NET_CONN_WIDE;
  468. net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
  469. }
  470. opt &= NETSTAT_ALLPROTO;
  471. if (opt) {
  472. flags &= ~NETSTAT_ALLPROTO;
  473. flags |= opt;
  474. }
  475. if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
  476. printf("Active Internet connections "); /* xxx */
  477. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  478. printf("(servers and established)");
  479. else if (flags & NETSTAT_LISTENING)
  480. printf("(only servers)");
  481. else
  482. printf("(w/o servers)");
  483. printf(net_conn_line_header, "Local Address", "Foreign Address");
  484. }
  485. if (inet && flags & NETSTAT_TCP)
  486. do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one);
  487. #if ENABLE_FEATURE_IPV6
  488. if (inet6 && flags & NETSTAT_TCP)
  489. do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one);
  490. #endif
  491. if (inet && flags & NETSTAT_UDP)
  492. do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one);
  493. #if ENABLE_FEATURE_IPV6
  494. if (inet6 && flags & NETSTAT_UDP)
  495. do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one);
  496. #endif
  497. if (inet && flags & NETSTAT_RAW)
  498. do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one);
  499. #if ENABLE_FEATURE_IPV6
  500. if (inet6 && flags & NETSTAT_RAW)
  501. do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one);
  502. #endif
  503. if (flags & NETSTAT_UNIX) {
  504. printf("Active UNIX domain sockets ");
  505. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  506. printf("(servers and established)");
  507. else if (flags & NETSTAT_LISTENING)
  508. printf("(only servers)");
  509. else
  510. printf("(w/o servers)");
  511. printf("\nProto RefCnt Flags Type State I-Node Path\n");
  512. do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one);
  513. }
  514. return 0;
  515. }