netstat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. * 2008-07-10
  12. * optional '-p' flag support ported from net-tools by G. Somlo <somlo@cmu.edu>
  13. *
  14. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  15. */
  16. #include "libbb.h"
  17. #include "inet_common.h"
  18. #define NETSTAT_OPTS "laentuwx" \
  19. USE_ROUTE( "r") \
  20. USE_FEATURE_NETSTAT_WIDE("W") \
  21. USE_FEATURE_NETSTAT_PRG( "p")
  22. enum {
  23. OPTBIT_KEEP_OLD = 7,
  24. USE_ROUTE( OPTBIT_ROUTE,)
  25. USE_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
  26. USE_FEATURE_NETSTAT_PRG( OPTBIT_PRG ,)
  27. OPT_sock_listen = 1 << 0, // l
  28. OPT_sock_all = 1 << 1, // a
  29. OPT_extended = 1 << 2, // e
  30. OPT_noresolve = 1 << 3, // n
  31. OPT_sock_tcp = 1 << 4, // t
  32. OPT_sock_udp = 1 << 5, // u
  33. OPT_sock_raw = 1 << 6, // w
  34. OPT_sock_unix = 1 << 7, // x
  35. OPT_route = USE_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
  36. OPT_wide = USE_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
  37. OPT_prg = USE_FEATURE_NETSTAT_PRG( (1 << OPTBIT_PRG )) + 0, // p
  38. };
  39. #define NETSTAT_CONNECTED 0x01
  40. #define NETSTAT_LISTENING 0x02
  41. #define NETSTAT_NUMERIC 0x04
  42. /* Must match getopt32 option string */
  43. #define NETSTAT_TCP 0x10
  44. #define NETSTAT_UDP 0x20
  45. #define NETSTAT_RAW 0x40
  46. #define NETSTAT_UNIX 0x80
  47. #define NETSTAT_ALLPROTO (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW|NETSTAT_UNIX)
  48. enum {
  49. TCP_ESTABLISHED = 1,
  50. TCP_SYN_SENT,
  51. TCP_SYN_RECV,
  52. TCP_FIN_WAIT1,
  53. TCP_FIN_WAIT2,
  54. TCP_TIME_WAIT,
  55. TCP_CLOSE,
  56. TCP_CLOSE_WAIT,
  57. TCP_LAST_ACK,
  58. TCP_LISTEN,
  59. TCP_CLOSING, /* now a valid state */
  60. };
  61. static const char *const tcp_state[] = {
  62. "",
  63. "ESTABLISHED",
  64. "SYN_SENT",
  65. "SYN_RECV",
  66. "FIN_WAIT1",
  67. "FIN_WAIT2",
  68. "TIME_WAIT",
  69. "CLOSE",
  70. "CLOSE_WAIT",
  71. "LAST_ACK",
  72. "LISTEN",
  73. "CLOSING"
  74. };
  75. typedef enum {
  76. SS_FREE = 0, /* not allocated */
  77. SS_UNCONNECTED, /* unconnected to any socket */
  78. SS_CONNECTING, /* in process of connecting */
  79. SS_CONNECTED, /* connected to socket */
  80. SS_DISCONNECTING /* in process of disconnecting */
  81. } socket_state;
  82. #define SO_ACCEPTCON (1<<16) /* performed a listen */
  83. #define SO_WAITDATA (1<<17) /* wait data to read */
  84. #define SO_NOSPACE (1<<18) /* no space to write */
  85. /* Standard printout size */
  86. #define PRINT_IP_MAX_SIZE 23
  87. #define PRINT_NET_CONN "%s %6ld %6ld %-23s %-23s %-12s"
  88. #define PRINT_NET_CONN_HEADER "\nProto Recv-Q Send-Q %-23s %-23s State "
  89. /* When there are IPv6 connections the IPv6 addresses will be
  90. * truncated to none-recognition. The '-W' option makes the
  91. * address columns wide enough to accomodate for longest possible
  92. * IPv6 addresses, i.e. addresses of the form
  93. * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd
  94. */
  95. #define PRINT_IP_MAX_SIZE_WIDE 51 /* INET6_ADDRSTRLEN + 5 for the port number */
  96. #define PRINT_NET_CONN_WIDE "%s %6ld %6ld %-51s %-51s %-12s"
  97. #define PRINT_NET_CONN_HEADER_WIDE "\nProto Recv-Q Send-Q %-51s %-51s State "
  98. #define PROGNAME_WIDTH 20
  99. #define PROGNAME_WIDTH_STR "20"
  100. /* PROGNAME_WIDTH chars: 12345678901234567890 */
  101. #define PROGNAME_BANNER "PID/Program name "
  102. struct prg_node {
  103. struct prg_node *next;
  104. long inode;
  105. char name[PROGNAME_WIDTH];
  106. };
  107. #define PRG_HASH_SIZE 211
  108. struct globals {
  109. const char *net_conn_line;
  110. smallint flags;
  111. #if ENABLE_FEATURE_NETSTAT_PRG
  112. smallint prg_cache_loaded;
  113. struct prg_node *prg_hash[PRG_HASH_SIZE];
  114. #endif
  115. };
  116. #define G (*ptr_to_globals)
  117. #define flags (G.flags )
  118. #define net_conn_line (G.net_conn_line )
  119. #define prg_hash (G.prg_hash )
  120. #define prg_cache_loaded (G.prg_cache_loaded)
  121. #define INIT_G() do { \
  122. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  123. flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
  124. net_conn_line = PRINT_NET_CONN; \
  125. } while (0)
  126. #if ENABLE_FEATURE_NETSTAT_PRG
  127. /* Deliberately truncating long to unsigned *int* */
  128. #define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
  129. #define print_progname_banner() do { \
  130. if (option_mask32 & OPT_prg) printf(PROGNAME_BANNER); \
  131. } while (0)
  132. static void prg_cache_add(long inode, char *name)
  133. {
  134. unsigned hi = PRG_HASHIT(inode);
  135. struct prg_node **pnp, *pn;
  136. prg_cache_loaded = 2;
  137. for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
  138. if (pn->inode == inode) {
  139. /* Some warning should be appropriate here
  140. as we got multiple processes for one i-node */
  141. return;
  142. }
  143. }
  144. *pnp = xzalloc(sizeof(struct prg_node));
  145. pn = *pnp;
  146. pn->inode = inode;
  147. safe_strncpy(pn->name, name, PROGNAME_WIDTH);
  148. }
  149. static const char *prg_cache_get(long inode)
  150. {
  151. unsigned hi = PRG_HASHIT(inode);
  152. struct prg_node *pn;
  153. for (pn = prg_hash[hi]; pn; pn = pn->next)
  154. if (pn->inode == inode)
  155. return pn->name;
  156. return "-";
  157. }
  158. #if ENABLE_FEATURE_CLEAN_UP
  159. static void prg_cache_clear(void)
  160. {
  161. struct prg_node **pnp, *pn;
  162. for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
  163. while ((pn = *pnp) != NULL) {
  164. *pnp = pn->next;
  165. free(pn);
  166. }
  167. }
  168. }
  169. #else
  170. #define prg_cache_clear() ((void)0)
  171. #endif
  172. static long extract_socket_inode(const char *lname)
  173. {
  174. long inode = -1;
  175. if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
  176. /* "socket:[12345]", extract the "12345" as inode */
  177. inode = bb_strtol(lname + sizeof("socket:[")-1, (char**)&lname, 0);
  178. if (*lname != ']')
  179. inode = -1;
  180. } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
  181. /* "[0000]:12345", extract the "12345" as inode */
  182. inode = bb_strtol(lname + sizeof("[0000]:")-1, NULL, 0);
  183. if (errno) /* not NUL terminated? */
  184. inode = -1;
  185. }
  186. #if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
  187. if (errno == ERANGE)
  188. inode = -1;
  189. #endif
  190. return inode;
  191. }
  192. static int FAST_FUNC file_act(const char *fileName,
  193. struct stat *statbuf UNUSED_PARAM,
  194. void *userData,
  195. int depth UNUSED_PARAM)
  196. {
  197. char *linkname;
  198. long inode;
  199. linkname = xmalloc_readlink(fileName);
  200. if (linkname != NULL) {
  201. inode = extract_socket_inode(linkname);
  202. free(linkname);
  203. if (inode >= 0)
  204. prg_cache_add(inode, (char *)userData);
  205. }
  206. return TRUE;
  207. }
  208. static int FAST_FUNC dir_act(const char *fileName,
  209. struct stat *statbuf UNUSED_PARAM,
  210. void *userData UNUSED_PARAM,
  211. int depth)
  212. {
  213. const char *shortName;
  214. char *p, *q;
  215. char cmdline_buf[512];
  216. int i;
  217. if (depth == 0) /* "/proc" itself */
  218. return TRUE; /* continue looking one level below /proc */
  219. shortName = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
  220. if (!isdigit(shortName[0])) /* skip /proc entries whic aren't processes */
  221. return SKIP;
  222. p = concat_path_file(fileName, "cmdline"); /* "/proc/PID/cmdline" */
  223. i = open_read_close(p, cmdline_buf, sizeof(cmdline_buf) - 1);
  224. free(p);
  225. if (i < 0)
  226. return FALSE;
  227. cmdline_buf[i] = '\0';
  228. q = concat_path_file(shortName, bb_basename(cmdline_buf)); /* "PID/argv0" */
  229. /* go through all files in /proc/PID/fd */
  230. p = concat_path_file(fileName, "fd");
  231. i = recursive_action(p, ACTION_RECURSE | ACTION_QUIET,
  232. file_act, NULL, (void *)q, 0);
  233. free(p);
  234. free(q);
  235. if (!i)
  236. return FALSE; /* signal permissions error to caller */
  237. return SKIP; /* caller should not recurse further into this dir. */
  238. }
  239. static void prg_cache_load(void)
  240. {
  241. int load_ok;
  242. prg_cache_loaded = 1;
  243. load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
  244. NULL, dir_act, NULL, 0);
  245. if (load_ok)
  246. return;
  247. if (prg_cache_loaded == 1)
  248. bb_error_msg("can't scan /proc - are you root?");
  249. else
  250. bb_error_msg("showing only processes with your user ID");
  251. }
  252. #else
  253. #define prg_cache_clear() ((void)0)
  254. #define print_progname_banner() ((void)0)
  255. #endif //ENABLE_FEATURE_NETSTAT_PRG
  256. #if ENABLE_FEATURE_IPV6
  257. static void build_ipv6_addr(char* local_addr, struct sockaddr_in6* localaddr)
  258. {
  259. char addr6[INET6_ADDRSTRLEN];
  260. struct in6_addr in6;
  261. sscanf(local_addr, "%08X%08X%08X%08X",
  262. &in6.s6_addr32[0], &in6.s6_addr32[1],
  263. &in6.s6_addr32[2], &in6.s6_addr32[3]);
  264. inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
  265. inet_pton(AF_INET6, addr6, (struct sockaddr *) &localaddr->sin6_addr);
  266. localaddr->sin6_family = AF_INET6;
  267. }
  268. #endif
  269. #if ENABLE_FEATURE_IPV6
  270. static void build_ipv4_addr(char* local_addr, struct sockaddr_in6* localaddr)
  271. #else
  272. static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
  273. #endif
  274. {
  275. sscanf(local_addr, "%X",
  276. &((struct sockaddr_in *) localaddr)->sin_addr.s_addr);
  277. ((struct sockaddr *) localaddr)->sa_family = AF_INET;
  278. }
  279. static const char *get_sname(int port, const char *proto, int numeric)
  280. {
  281. if (!port)
  282. return "*";
  283. if (!numeric) {
  284. struct servent *se = getservbyport(port, proto);
  285. if (se)
  286. return se->s_name;
  287. }
  288. /* hummm, we may return static buffer here!! */
  289. return itoa(ntohs(port));
  290. }
  291. static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
  292. {
  293. char *host, *host_port;
  294. /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
  295. * in IPv6, while "0.0.0.0" is not. */
  296. host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
  297. : xmalloc_sockaddr2host_noport(addr);
  298. host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
  299. free(host);
  300. return host_port;
  301. }
  302. struct inet_params {
  303. int local_port, rem_port, state, uid;
  304. #if ENABLE_FEATURE_IPV6
  305. struct sockaddr_in6 localaddr, remaddr;
  306. #else
  307. struct sockaddr_in localaddr, remaddr;
  308. #endif
  309. unsigned long rxq, txq, inode;
  310. };
  311. static int scan_inet_proc_line(struct inet_params *param, char *line)
  312. {
  313. int num;
  314. char local_addr[64], rem_addr[64];
  315. num = sscanf(line,
  316. "%*d: %64[0-9A-Fa-f]:%X "
  317. "%64[0-9A-Fa-f]:%X %X "
  318. "%lX:%lX %*X:%*X "
  319. "%*X %d %*d %ld ",
  320. local_addr, &param->local_port,
  321. rem_addr, &param->rem_port, &param->state,
  322. &param->txq, &param->rxq,
  323. &param->uid, &param->inode);
  324. if (num < 9) {
  325. return 1; /* error */
  326. }
  327. if (strlen(local_addr) > 8) {
  328. #if ENABLE_FEATURE_IPV6
  329. build_ipv6_addr(local_addr, &param->localaddr);
  330. build_ipv6_addr(rem_addr, &param->remaddr);
  331. #endif
  332. } else {
  333. build_ipv4_addr(local_addr, &param->localaddr);
  334. build_ipv4_addr(rem_addr, &param->remaddr);
  335. }
  336. return 0;
  337. }
  338. static void print_inet_line(struct inet_params *param,
  339. const char *state_str, const char *proto, int is_connected)
  340. {
  341. if ((is_connected && (flags & NETSTAT_CONNECTED))
  342. || (!is_connected && (flags & NETSTAT_LISTENING))
  343. ) {
  344. char *l = ip_port_str(
  345. (struct sockaddr *) &param->localaddr, param->local_port,
  346. proto, flags & NETSTAT_NUMERIC);
  347. char *r = ip_port_str(
  348. (struct sockaddr *) &param->remaddr, param->rem_port,
  349. proto, flags & NETSTAT_NUMERIC);
  350. printf(net_conn_line,
  351. proto, param->rxq, param->txq, l, r, state_str);
  352. #if ENABLE_FEATURE_NETSTAT_PRG
  353. if (option_mask32 & OPT_prg)
  354. printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
  355. #endif
  356. bb_putchar('\n');
  357. free(l);
  358. free(r);
  359. }
  360. }
  361. static int FAST_FUNC tcp_do_one(char *line)
  362. {
  363. struct inet_params param;
  364. if (scan_inet_proc_line(&param, line))
  365. return 1;
  366. print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
  367. return 0;
  368. }
  369. #if ENABLE_FEATURE_IPV6
  370. # define notnull(A) ( \
  371. ( (A.sin6_family == AF_INET6) \
  372. && (A.sin6_addr.s6_addr32[0] | A.sin6_addr.s6_addr32[1] | \
  373. A.sin6_addr.s6_addr32[2] | A.sin6_addr.s6_addr32[3]) \
  374. ) || ( \
  375. (A.sin6_family == AF_INET) \
  376. && ((struct sockaddr_in*)&A)->sin_addr.s_addr \
  377. ) \
  378. )
  379. #else
  380. # define notnull(A) (A.sin_addr.s_addr)
  381. #endif
  382. static int FAST_FUNC udp_do_one(char *line)
  383. {
  384. int have_remaddr;
  385. const char *state_str;
  386. struct inet_params param;
  387. if (scan_inet_proc_line(&param, line))
  388. return 1;
  389. state_str = "UNKNOWN";
  390. switch (param.state) {
  391. case TCP_ESTABLISHED:
  392. state_str = "ESTABLISHED";
  393. break;
  394. case TCP_CLOSE:
  395. state_str = "";
  396. break;
  397. }
  398. have_remaddr = notnull(param.remaddr);
  399. print_inet_line(&param, state_str, "udp", have_remaddr);
  400. return 0;
  401. }
  402. static int FAST_FUNC raw_do_one(char *line)
  403. {
  404. int have_remaddr;
  405. struct inet_params param;
  406. if (scan_inet_proc_line(&param, line))
  407. return 1;
  408. have_remaddr = notnull(param.remaddr);
  409. print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
  410. return 0;
  411. }
  412. static int FAST_FUNC unix_do_one(char *line)
  413. {
  414. unsigned long refcnt, proto, unix_flags;
  415. unsigned long inode;
  416. int type, state;
  417. int num, path_ofs;
  418. const char *ss_proto, *ss_state, *ss_type;
  419. char ss_flags[32];
  420. /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
  421. * Other users report long lines filled by NUL bytes.
  422. * (those ^@ are NUL bytes too). We see them as empty lines. */
  423. if (!line[0])
  424. return 0;
  425. path_ofs = 0; /* paranoia */
  426. num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
  427. &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
  428. if (num < 6) {
  429. return 1; /* error */
  430. }
  431. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
  432. if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
  433. if (!(flags & NETSTAT_LISTENING))
  434. return 0;
  435. } else {
  436. if (!(flags & NETSTAT_CONNECTED))
  437. return 0;
  438. }
  439. }
  440. switch (proto) {
  441. case 0:
  442. ss_proto = "unix";
  443. break;
  444. default:
  445. ss_proto = "??";
  446. }
  447. switch (type) {
  448. case SOCK_STREAM:
  449. ss_type = "STREAM";
  450. break;
  451. case SOCK_DGRAM:
  452. ss_type = "DGRAM";
  453. break;
  454. case SOCK_RAW:
  455. ss_type = "RAW";
  456. break;
  457. case SOCK_RDM:
  458. ss_type = "RDM";
  459. break;
  460. case SOCK_SEQPACKET:
  461. ss_type = "SEQPACKET";
  462. break;
  463. default:
  464. ss_type = "UNKNOWN";
  465. }
  466. switch (state) {
  467. case SS_FREE:
  468. ss_state = "FREE";
  469. break;
  470. case SS_UNCONNECTED:
  471. /*
  472. * Unconnected sockets may be listening
  473. * for something.
  474. */
  475. if (unix_flags & SO_ACCEPTCON) {
  476. ss_state = "LISTENING";
  477. } else {
  478. ss_state = "";
  479. }
  480. break;
  481. case SS_CONNECTING:
  482. ss_state = "CONNECTING";
  483. break;
  484. case SS_CONNECTED:
  485. ss_state = "CONNECTED";
  486. break;
  487. case SS_DISCONNECTING:
  488. ss_state = "DISCONNECTING";
  489. break;
  490. default:
  491. ss_state = "UNKNOWN";
  492. }
  493. strcpy(ss_flags, "[ ");
  494. if (unix_flags & SO_ACCEPTCON)
  495. strcat(ss_flags, "ACC ");
  496. if (unix_flags & SO_WAITDATA)
  497. strcat(ss_flags, "W ");
  498. if (unix_flags & SO_NOSPACE)
  499. strcat(ss_flags, "N ");
  500. strcat(ss_flags, "]");
  501. printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
  502. ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
  503. );
  504. #if ENABLE_FEATURE_NETSTAT_PRG
  505. if (option_mask32 & OPT_prg)
  506. printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
  507. #endif
  508. /* TODO: currently we stop at first NUL byte. Is it a problem? */
  509. line += path_ofs;
  510. *strchrnul(line, '\n') = '\0';
  511. while (*line)
  512. fputc_printable(*line++, stdout);
  513. bb_putchar('\n');
  514. return 0;
  515. }
  516. static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
  517. {
  518. int lnr;
  519. FILE *procinfo;
  520. char *buffer;
  521. /* _stdin is just to save "r" param */
  522. procinfo = fopen_or_warn_stdin(file);
  523. if (procinfo == NULL) {
  524. return;
  525. }
  526. lnr = 0;
  527. /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
  528. while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
  529. /* line 0 is skipped */
  530. if (lnr && proc(buffer))
  531. bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
  532. lnr++;
  533. free(buffer);
  534. }
  535. fclose(procinfo);
  536. }
  537. int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  538. int netstat_main(int argc UNUSED_PARAM, char **argv)
  539. {
  540. const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
  541. unsigned opt;
  542. INIT_G();
  543. /* Option string must match NETSTAT_xxx constants */
  544. opt = getopt32(argv, NETSTAT_OPTS);
  545. if (opt & 0x1) { // -l
  546. flags &= ~NETSTAT_CONNECTED;
  547. flags |= NETSTAT_LISTENING;
  548. }
  549. if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
  550. //if (opt & 0x4) // -e
  551. if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
  552. //if (opt & 0x10) // -t: NETSTAT_TCP
  553. //if (opt & 0x20) // -u: NETSTAT_UDP
  554. //if (opt & 0x40) // -w: NETSTAT_RAW
  555. //if (opt & 0x80) // -x: NETSTAT_UNIX
  556. if (opt & OPT_route) { // -r
  557. #if ENABLE_ROUTE
  558. bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
  559. return 0;
  560. #else
  561. bb_show_usage();
  562. #endif
  563. }
  564. if (opt & OPT_wide) { // -W
  565. net_conn_line = PRINT_NET_CONN_WIDE;
  566. net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
  567. }
  568. #if ENABLE_FEATURE_NETSTAT_PRG
  569. if (opt & OPT_prg) { // -p
  570. prg_cache_load();
  571. }
  572. #endif
  573. opt &= NETSTAT_ALLPROTO;
  574. if (opt) {
  575. flags &= ~NETSTAT_ALLPROTO;
  576. flags |= opt;
  577. }
  578. if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
  579. printf("Active Internet connections "); /* xxx */
  580. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  581. printf("(servers and established)");
  582. else if (flags & NETSTAT_LISTENING)
  583. printf("(only servers)");
  584. else
  585. printf("(w/o servers)");
  586. printf(net_conn_line_header, "Local Address", "Foreign Address");
  587. print_progname_banner();
  588. bb_putchar('\n');
  589. }
  590. if (flags & NETSTAT_TCP) {
  591. do_info("/proc/net/tcp", tcp_do_one);
  592. #if ENABLE_FEATURE_IPV6
  593. do_info("/proc/net/tcp6", tcp_do_one);
  594. #endif
  595. }
  596. if (flags & NETSTAT_UDP) {
  597. do_info("/proc/net/udp", udp_do_one);
  598. #if ENABLE_FEATURE_IPV6
  599. do_info("/proc/net/udp6", udp_do_one);
  600. #endif
  601. }
  602. if (flags & NETSTAT_RAW) {
  603. do_info("/proc/net/raw", raw_do_one);
  604. #if ENABLE_FEATURE_IPV6
  605. do_info("/proc/net/raw6", raw_do_one);
  606. #endif
  607. }
  608. if (flags & NETSTAT_UNIX) {
  609. printf("Active UNIX domain sockets ");
  610. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  611. printf("(servers and established)");
  612. else if (flags & NETSTAT_LISTENING)
  613. printf("(only servers)");
  614. else
  615. printf("(w/o servers)");
  616. printf("\nProto RefCnt Flags Type State I-Node ");
  617. print_progname_banner();
  618. printf("Path\n");
  619. do_info("/proc/net/unix", unix_do_one);
  620. }
  621. prg_cache_clear();
  622. return 0;
  623. }