3
0

netstat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 int tcp_do_one(int lnr, 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 0;
  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 (num < 10) {
  149. return 1; /* error */
  150. }
  151. if (strlen(local_addr) > 8) {
  152. #if ENABLE_FEATURE_IPV6
  153. build_ipv6_addr(local_addr, &localaddr);
  154. build_ipv6_addr(rem_addr, &remaddr);
  155. #endif
  156. } else {
  157. build_ipv4_addr(local_addr, &localaddr);
  158. build_ipv4_addr(rem_addr, &remaddr);
  159. }
  160. if ((rem_port && (flags & NETSTAT_CONNECTED))
  161. || (!rem_port && (flags & NETSTAT_LISTENING))
  162. ) {
  163. char *l = ip_port_str(
  164. (struct sockaddr *) &localaddr, local_port,
  165. "tcp", flags & NETSTAT_NUMERIC);
  166. char *r = ip_port_str(
  167. (struct sockaddr *) &remaddr, rem_port,
  168. "tcp", flags & NETSTAT_NUMERIC);
  169. printf(net_conn_line,
  170. "tcp", rxq, txq, l, r, tcp_state[state]);
  171. free(l);
  172. free(r);
  173. }
  174. return 0;
  175. }
  176. static int udp_do_one(int lnr, 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 0;
  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. return 1; /* error */
  208. }
  209. switch (state) {
  210. case TCP_ESTABLISHED:
  211. state_str = "ESTABLISHED";
  212. break;
  213. case TCP_CLOSE:
  214. state_str = "";
  215. break;
  216. default:
  217. state_str = "UNKNOWN";
  218. break;
  219. }
  220. #if ENABLE_FEATURE_IPV6
  221. # define notnull(A) ( \
  222. ( (A.sin6_family == AF_INET6) \
  223. && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
  224. A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
  225. ) || ( \
  226. (A.sin6_family == AF_INET) \
  227. && ((struct sockaddr_in*)&A)->sin_addr.s_addr \
  228. ) \
  229. )
  230. #else
  231. # define notnull(A) (A.sin_addr.s_addr)
  232. #endif
  233. {
  234. int have_remaddr = notnull(remaddr);
  235. if ((have_remaddr && (flags & NETSTAT_CONNECTED))
  236. || (!have_remaddr && (flags & NETSTAT_LISTENING))
  237. ) {
  238. char *l = ip_port_str(
  239. (struct sockaddr *) &localaddr, local_port,
  240. "udp", flags & NETSTAT_NUMERIC);
  241. char *r = ip_port_str(
  242. (struct sockaddr *) &remaddr, rem_port,
  243. "udp", flags & NETSTAT_NUMERIC);
  244. printf(net_conn_line,
  245. "udp", rxq, txq, l, r, state_str);
  246. free(l);
  247. free(r);
  248. }
  249. }
  250. return 0;
  251. }
  252. static int raw_do_one(int lnr, 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 0;
  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. return 1; /* error */
  282. }
  283. {
  284. int have_remaddr = notnull(remaddr);
  285. if ((have_remaddr && (flags & NETSTAT_CONNECTED))
  286. || (!have_remaddr && (flags & NETSTAT_LISTENING))
  287. ) {
  288. char *l = ip_port_str(
  289. (struct sockaddr *) &localaddr, local_port,
  290. "raw", flags & NETSTAT_NUMERIC);
  291. char *r = ip_port_str(
  292. (struct sockaddr *) &remaddr, rem_port,
  293. "raw", flags & NETSTAT_NUMERIC);
  294. printf(net_conn_line,
  295. "raw", rxq, txq, l, r, itoa(state));
  296. free(l);
  297. free(r);
  298. }
  299. }
  300. return 0;
  301. }
  302. static int unix_do_one(int nr, char *line)
  303. {
  304. unsigned long refcnt, proto, unix_flags;
  305. unsigned long inode;
  306. int type, state;
  307. int num, path_ofs;
  308. void *d;
  309. const char *ss_proto, *ss_state, *ss_type;
  310. char ss_flags[32];
  311. if (nr == 0)
  312. return 0; /* skip header */
  313. /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
  314. * Other users report long lines filled by NUL bytes.
  315. * (those ^@ are NUL bytes too). We see them as empty lines. */
  316. if (!line[0])
  317. return 0;
  318. path_ofs = 0; /* paranoia */
  319. num = sscanf(line, "%p: %lX %lX %lX %X %X %lu %n",
  320. &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
  321. if (num < 7) {
  322. return 1; /* error */
  323. }
  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 0;
  328. } else {
  329. if (!(flags & NETSTAT_CONNECTED))
  330. return 0;
  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 %6lu ",
  395. ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
  396. );
  397. /* TODO: currently we stop at first NUL byte. Is it a problem? */
  398. line += path_ofs;
  399. *strchrnul(line, '\n') = '\0';
  400. while (*line)
  401. fputc_printable(*line++, stdout);
  402. bb_putchar('\n');
  403. return 0;
  404. }
  405. #define _PATH_PROCNET_UDP "/proc/net/udp"
  406. #define _PATH_PROCNET_UDP6 "/proc/net/udp6"
  407. #define _PATH_PROCNET_TCP "/proc/net/tcp"
  408. #define _PATH_PROCNET_TCP6 "/proc/net/tcp6"
  409. #define _PATH_PROCNET_RAW "/proc/net/raw"
  410. #define _PATH_PROCNET_RAW6 "/proc/net/raw6"
  411. #define _PATH_PROCNET_UNIX "/proc/net/unix"
  412. static void do_info(const char *file, const char *name, int (*proc)(int, char *))
  413. {
  414. int lnr;
  415. FILE *procinfo;
  416. char *buffer;
  417. procinfo = fopen(file, "r");
  418. if (procinfo == NULL) {
  419. if (errno != ENOENT) {
  420. bb_simple_perror_msg(file);
  421. } else {
  422. bb_error_msg("no kernel support for %s", name);
  423. }
  424. return;
  425. }
  426. lnr = 0;
  427. /* Why? because xmalloc_fgets_str doesn't stop on NULs */
  428. while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
  429. if (proc(lnr++, buffer))
  430. bb_error_msg("%s: bogus data on line %d", file, lnr);
  431. free(buffer);
  432. }
  433. fclose(procinfo);
  434. }
  435. int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  436. int netstat_main(int argc ATTRIBUTE_UNUSED, char **argv)
  437. {
  438. const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
  439. unsigned opt;
  440. #if ENABLE_FEATURE_IPV6
  441. smallint inet = 1;
  442. smallint inet6 = 1;
  443. #else
  444. enum { inet = 1, inet6 = 0 };
  445. #endif
  446. /* Option string must match NETSTAT_xxx constants */
  447. opt = getopt32(argv, NETSTAT_OPTS);
  448. if (opt & 0x1) { // -l
  449. flags &= ~NETSTAT_CONNECTED;
  450. flags |= NETSTAT_LISTENING;
  451. }
  452. if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
  453. //if (opt & 0x4) // -e
  454. if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
  455. //if (opt & 0x10) // -t: NETSTAT_TCP
  456. //if (opt & 0x20) // -u: NETSTAT_UDP
  457. //if (opt & 0x40) // -w: NETSTAT_RAW
  458. //if (opt & 0x80) // -x: NETSTAT_UNIX
  459. if (opt & OPT_showroute) { // -r
  460. #if ENABLE_ROUTE
  461. bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
  462. return 0;
  463. #else
  464. bb_show_usage();
  465. #endif
  466. }
  467. if (opt & OPT_widedisplay) { // -W
  468. net_conn_line = PRINT_NET_CONN_WIDE;
  469. net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
  470. }
  471. opt &= NETSTAT_ALLPROTO;
  472. if (opt) {
  473. flags &= ~NETSTAT_ALLPROTO;
  474. flags |= opt;
  475. }
  476. if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
  477. printf("Active Internet connections "); /* xxx */
  478. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  479. printf("(servers and established)");
  480. else if (flags & NETSTAT_LISTENING)
  481. printf("(only servers)");
  482. else
  483. printf("(w/o servers)");
  484. printf(net_conn_line_header, "Local Address", "Foreign Address");
  485. }
  486. if (inet && flags & NETSTAT_TCP)
  487. do_info(_PATH_PROCNET_TCP, "AF INET (tcp)", tcp_do_one);
  488. #if ENABLE_FEATURE_IPV6
  489. if (inet6 && flags & NETSTAT_TCP)
  490. do_info(_PATH_PROCNET_TCP6, "AF INET6 (tcp)", tcp_do_one);
  491. #endif
  492. if (inet && flags & NETSTAT_UDP)
  493. do_info(_PATH_PROCNET_UDP, "AF INET (udp)", udp_do_one);
  494. #if ENABLE_FEATURE_IPV6
  495. if (inet6 && flags & NETSTAT_UDP)
  496. do_info(_PATH_PROCNET_UDP6, "AF INET6 (udp)", udp_do_one);
  497. #endif
  498. if (inet && flags & NETSTAT_RAW)
  499. do_info(_PATH_PROCNET_RAW, "AF INET (raw)", raw_do_one);
  500. #if ENABLE_FEATURE_IPV6
  501. if (inet6 && flags & NETSTAT_RAW)
  502. do_info(_PATH_PROCNET_RAW6, "AF INET6 (raw)", raw_do_one);
  503. #endif
  504. if (flags & NETSTAT_UNIX) {
  505. printf("Active UNIX domain sockets ");
  506. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  507. printf("(servers and established)");
  508. else if (flags & NETSTAT_LISTENING)
  509. printf("(only servers)");
  510. else
  511. printf("(w/o servers)");
  512. printf("\nProto RefCnt Flags Type State I-Node Path\n");
  513. do_info(_PATH_PROCNET_UNIX, "AF UNIX", unix_do_one);
  514. }
  515. return 0;
  516. }