netstat.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. IF_ROUTE( "r") \
  20. IF_FEATURE_NETSTAT_WIDE("W") \
  21. IF_FEATURE_NETSTAT_PRG( "p")
  22. enum {
  23. OPTBIT_KEEP_OLD = 7,
  24. IF_ROUTE( OPTBIT_ROUTE,)
  25. IF_FEATURE_NETSTAT_WIDE(OPTBIT_WIDE ,)
  26. IF_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 = IF_ROUTE( (1 << OPTBIT_ROUTE)) + 0, // r
  36. OPT_wide = IF_FEATURE_NETSTAT_WIDE((1 << OPTBIT_WIDE )) + 0, // W
  37. OPT_prg = IF_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, &localaddr->sin6_addr);
  266. localaddr->sin6_family = AF_INET6;
  267. }
  268. #endif
  269. static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
  270. {
  271. sscanf(local_addr, "%X", &localaddr->sin_addr.s_addr);
  272. localaddr->sin_family = AF_INET;
  273. }
  274. static const char *get_sname(int port, const char *proto, int numeric)
  275. {
  276. if (!port)
  277. return "*";
  278. if (!numeric) {
  279. struct servent *se = getservbyport(port, proto);
  280. if (se)
  281. return se->s_name;
  282. }
  283. /* hummm, we may return static buffer here!! */
  284. return itoa(ntohs(port));
  285. }
  286. static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
  287. {
  288. char *host, *host_port;
  289. /* Code which used "*" for INADDR_ANY is removed: it's ambiguous
  290. * in IPv6, while "0.0.0.0" is not. */
  291. host = numeric ? xmalloc_sockaddr2dotted_noport(addr)
  292. : xmalloc_sockaddr2host_noport(addr);
  293. host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
  294. free(host);
  295. return host_port;
  296. }
  297. struct inet_params {
  298. int local_port, rem_port, state, uid;
  299. union {
  300. struct sockaddr sa;
  301. struct sockaddr_in sin;
  302. #if ENABLE_FEATURE_IPV6
  303. struct sockaddr_in6 sin6;
  304. #endif
  305. } localaddr, remaddr;
  306. unsigned long rxq, txq, inode;
  307. };
  308. static int scan_inet_proc_line(struct inet_params *param, char *line)
  309. {
  310. int num;
  311. char local_addr[64], rem_addr[64];
  312. num = sscanf(line,
  313. "%*d: %64[0-9A-Fa-f]:%X "
  314. "%64[0-9A-Fa-f]:%X %X "
  315. "%lX:%lX %*X:%*X "
  316. "%*X %d %*d %ld ",
  317. local_addr, &param->local_port,
  318. rem_addr, &param->rem_port, &param->state,
  319. &param->txq, &param->rxq,
  320. &param->uid, &param->inode);
  321. if (num < 9) {
  322. return 1; /* error */
  323. }
  324. if (strlen(local_addr) > 8) {
  325. #if ENABLE_FEATURE_IPV6
  326. build_ipv6_addr(local_addr, &param->localaddr.sin6);
  327. build_ipv6_addr(rem_addr, &param->remaddr.sin6);
  328. #endif
  329. } else {
  330. build_ipv4_addr(local_addr, &param->localaddr.sin);
  331. build_ipv4_addr(rem_addr, &param->remaddr.sin);
  332. }
  333. return 0;
  334. }
  335. static void print_inet_line(struct inet_params *param,
  336. const char *state_str, const char *proto, int is_connected)
  337. {
  338. if ((is_connected && (flags & NETSTAT_CONNECTED))
  339. || (!is_connected && (flags & NETSTAT_LISTENING))
  340. ) {
  341. char *l = ip_port_str(
  342. &param->localaddr.sa, param->local_port,
  343. proto, flags & NETSTAT_NUMERIC);
  344. char *r = ip_port_str(
  345. &param->remaddr.sa, param->rem_port,
  346. proto, flags & NETSTAT_NUMERIC);
  347. printf(net_conn_line,
  348. proto, param->rxq, param->txq, l, r, state_str);
  349. #if ENABLE_FEATURE_NETSTAT_PRG
  350. if (option_mask32 & OPT_prg)
  351. printf("%."PROGNAME_WIDTH_STR"s", prg_cache_get(param->inode));
  352. #endif
  353. bb_putchar('\n');
  354. free(l);
  355. free(r);
  356. }
  357. }
  358. static int FAST_FUNC tcp_do_one(char *line)
  359. {
  360. struct inet_params param;
  361. if (scan_inet_proc_line(&param, line))
  362. return 1;
  363. print_inet_line(&param, tcp_state[param.state], "tcp", param.rem_port);
  364. return 0;
  365. }
  366. #if ENABLE_FEATURE_IPV6
  367. # define NOT_NULL_ADDR(A) ( \
  368. ( (A.sa.sa_family == AF_INET6) \
  369. && (A.sin6.sin6_addr.s6_addr32[0] | A.sin6.sin6_addr.s6_addr32[1] | \
  370. A.sin6.sin6_addr.s6_addr32[2] | A.sin6.sin6_addr.s6_addr32[3]) \
  371. ) || ( \
  372. (A.sa.sa_family == AF_INET) \
  373. && A.sin.sin_addr.s_addr != 0 \
  374. ) \
  375. )
  376. #else
  377. # define NOT_NULL_ADDR(A) (A.sin.sin_addr.s_addr)
  378. #endif
  379. static int FAST_FUNC udp_do_one(char *line)
  380. {
  381. int have_remaddr;
  382. const char *state_str;
  383. struct inet_params param;
  384. if (scan_inet_proc_line(&param, line))
  385. return 1;
  386. state_str = "UNKNOWN";
  387. switch (param.state) {
  388. case TCP_ESTABLISHED:
  389. state_str = "ESTABLISHED";
  390. break;
  391. case TCP_CLOSE:
  392. state_str = "";
  393. break;
  394. }
  395. have_remaddr = NOT_NULL_ADDR(param.remaddr);
  396. print_inet_line(&param, state_str, "udp", have_remaddr);
  397. return 0;
  398. }
  399. static int FAST_FUNC raw_do_one(char *line)
  400. {
  401. int have_remaddr;
  402. struct inet_params param;
  403. if (scan_inet_proc_line(&param, line))
  404. return 1;
  405. have_remaddr = NOT_NULL_ADDR(param.remaddr);
  406. print_inet_line(&param, itoa(param.state), "raw", have_remaddr);
  407. return 0;
  408. }
  409. static int FAST_FUNC unix_do_one(char *line)
  410. {
  411. unsigned long refcnt, proto, unix_flags;
  412. unsigned long inode;
  413. int type, state;
  414. int num, path_ofs;
  415. const char *ss_proto, *ss_state, *ss_type;
  416. char ss_flags[32];
  417. /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
  418. * Other users report long lines filled by NUL bytes.
  419. * (those ^@ are NUL bytes too). We see them as empty lines. */
  420. if (!line[0])
  421. return 0;
  422. path_ofs = 0; /* paranoia */
  423. num = sscanf(line, "%*p: %lX %lX %lX %X %X %lu %n",
  424. &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
  425. if (num < 6) {
  426. return 1; /* error */
  427. }
  428. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
  429. if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
  430. if (!(flags & NETSTAT_LISTENING))
  431. return 0;
  432. } else {
  433. if (!(flags & NETSTAT_CONNECTED))
  434. return 0;
  435. }
  436. }
  437. switch (proto) {
  438. case 0:
  439. ss_proto = "unix";
  440. break;
  441. default:
  442. ss_proto = "??";
  443. }
  444. switch (type) {
  445. case SOCK_STREAM:
  446. ss_type = "STREAM";
  447. break;
  448. case SOCK_DGRAM:
  449. ss_type = "DGRAM";
  450. break;
  451. case SOCK_RAW:
  452. ss_type = "RAW";
  453. break;
  454. case SOCK_RDM:
  455. ss_type = "RDM";
  456. break;
  457. case SOCK_SEQPACKET:
  458. ss_type = "SEQPACKET";
  459. break;
  460. default:
  461. ss_type = "UNKNOWN";
  462. }
  463. switch (state) {
  464. case SS_FREE:
  465. ss_state = "FREE";
  466. break;
  467. case SS_UNCONNECTED:
  468. /*
  469. * Unconnected sockets may be listening
  470. * for something.
  471. */
  472. if (unix_flags & SO_ACCEPTCON) {
  473. ss_state = "LISTENING";
  474. } else {
  475. ss_state = "";
  476. }
  477. break;
  478. case SS_CONNECTING:
  479. ss_state = "CONNECTING";
  480. break;
  481. case SS_CONNECTED:
  482. ss_state = "CONNECTED";
  483. break;
  484. case SS_DISCONNECTING:
  485. ss_state = "DISCONNECTING";
  486. break;
  487. default:
  488. ss_state = "UNKNOWN";
  489. }
  490. strcpy(ss_flags, "[ ");
  491. if (unix_flags & SO_ACCEPTCON)
  492. strcat(ss_flags, "ACC ");
  493. if (unix_flags & SO_WAITDATA)
  494. strcat(ss_flags, "W ");
  495. if (unix_flags & SO_NOSPACE)
  496. strcat(ss_flags, "N ");
  497. strcat(ss_flags, "]");
  498. printf("%-5s %-6ld %-11s %-10s %-13s %6lu ",
  499. ss_proto, refcnt, ss_flags, ss_type, ss_state, inode
  500. );
  501. #if ENABLE_FEATURE_NETSTAT_PRG
  502. if (option_mask32 & OPT_prg)
  503. printf("%-"PROGNAME_WIDTH_STR"s", prg_cache_get(inode));
  504. #endif
  505. /* TODO: currently we stop at first NUL byte. Is it a problem? */
  506. line += path_ofs;
  507. *strchrnul(line, '\n') = '\0';
  508. while (*line)
  509. fputc_printable(*line++, stdout);
  510. bb_putchar('\n');
  511. return 0;
  512. }
  513. static void do_info(const char *file, int FAST_FUNC (*proc)(char *))
  514. {
  515. int lnr;
  516. FILE *procinfo;
  517. char *buffer;
  518. /* _stdin is just to save "r" param */
  519. procinfo = fopen_or_warn_stdin(file);
  520. if (procinfo == NULL) {
  521. return;
  522. }
  523. lnr = 0;
  524. /* Why xmalloc_fgets_str? because it doesn't stop on NULs */
  525. while ((buffer = xmalloc_fgets_str(procinfo, "\n")) != NULL) {
  526. /* line 0 is skipped */
  527. if (lnr && proc(buffer))
  528. bb_error_msg("%s: bogus data on line %d", file, lnr + 1);
  529. lnr++;
  530. free(buffer);
  531. }
  532. fclose(procinfo);
  533. }
  534. int netstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  535. int netstat_main(int argc UNUSED_PARAM, char **argv)
  536. {
  537. const char *net_conn_line_header = PRINT_NET_CONN_HEADER;
  538. unsigned opt;
  539. INIT_G();
  540. /* Option string must match NETSTAT_xxx constants */
  541. opt = getopt32(argv, NETSTAT_OPTS);
  542. if (opt & 0x1) { // -l
  543. flags &= ~NETSTAT_CONNECTED;
  544. flags |= NETSTAT_LISTENING;
  545. }
  546. if (opt & 0x2) flags |= NETSTAT_LISTENING | NETSTAT_CONNECTED; // -a
  547. //if (opt & 0x4) // -e
  548. if (opt & 0x8) flags |= NETSTAT_NUMERIC; // -n
  549. //if (opt & 0x10) // -t: NETSTAT_TCP
  550. //if (opt & 0x20) // -u: NETSTAT_UDP
  551. //if (opt & 0x40) // -w: NETSTAT_RAW
  552. //if (opt & 0x80) // -x: NETSTAT_UNIX
  553. if (opt & OPT_route) { // -r
  554. #if ENABLE_ROUTE
  555. bb_displayroutes(flags & NETSTAT_NUMERIC, !(opt & OPT_extended));
  556. return 0;
  557. #else
  558. bb_show_usage();
  559. #endif
  560. }
  561. if (opt & OPT_wide) { // -W
  562. net_conn_line = PRINT_NET_CONN_WIDE;
  563. net_conn_line_header = PRINT_NET_CONN_HEADER_WIDE;
  564. }
  565. #if ENABLE_FEATURE_NETSTAT_PRG
  566. if (opt & OPT_prg) { // -p
  567. prg_cache_load();
  568. }
  569. #endif
  570. opt &= NETSTAT_ALLPROTO;
  571. if (opt) {
  572. flags &= ~NETSTAT_ALLPROTO;
  573. flags |= opt;
  574. }
  575. if (flags & (NETSTAT_TCP|NETSTAT_UDP|NETSTAT_RAW)) {
  576. printf("Active Internet connections "); /* xxx */
  577. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  578. printf("(servers and established)");
  579. else if (flags & NETSTAT_LISTENING)
  580. printf("(only servers)");
  581. else
  582. printf("(w/o servers)");
  583. printf(net_conn_line_header, "Local Address", "Foreign Address");
  584. print_progname_banner();
  585. bb_putchar('\n');
  586. }
  587. if (flags & NETSTAT_TCP) {
  588. do_info("/proc/net/tcp", tcp_do_one);
  589. #if ENABLE_FEATURE_IPV6
  590. do_info("/proc/net/tcp6", tcp_do_one);
  591. #endif
  592. }
  593. if (flags & NETSTAT_UDP) {
  594. do_info("/proc/net/udp", udp_do_one);
  595. #if ENABLE_FEATURE_IPV6
  596. do_info("/proc/net/udp6", udp_do_one);
  597. #endif
  598. }
  599. if (flags & NETSTAT_RAW) {
  600. do_info("/proc/net/raw", raw_do_one);
  601. #if ENABLE_FEATURE_IPV6
  602. do_info("/proc/net/raw6", raw_do_one);
  603. #endif
  604. }
  605. if (flags & NETSTAT_UNIX) {
  606. printf("Active UNIX domain sockets ");
  607. if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) == (NETSTAT_LISTENING|NETSTAT_CONNECTED))
  608. printf("(servers and established)");
  609. else if (flags & NETSTAT_LISTENING)
  610. printf("(only servers)");
  611. else
  612. printf("(w/o servers)");
  613. printf("\nProto RefCnt Flags Type State I-Node ");
  614. print_progname_banner();
  615. printf("Path\n");
  616. do_info("/proc/net/unix", unix_do_one);
  617. }
  618. prg_cache_clear();
  619. return 0;
  620. }