3
0

pscan.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Pscan is a mini port scanner implementation for busybox
  3. *
  4. * Copyright 2007 Tito Ragusa <farmatito@tiscali.it>
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //usage:#define pscan_trivial_usage
  9. //usage: "[-cb] [-p MIN_PORT] [-P MAX_PORT] [-t TIMEOUT] [-T MIN_RTT] HOST"
  10. //usage:#define pscan_full_usage "\n\n"
  11. //usage: "Scan a host, print all open ports\n"
  12. //usage: "\n -c Show closed ports too"
  13. //usage: "\n -b Show blocked ports too"
  14. //usage: "\n -p Scan from this port (default 1)"
  15. //usage: "\n -P Scan up to this port (default 1024)"
  16. //usage: "\n -t Timeout (default 5000 ms)"
  17. //usage: "\n -T Minimum rtt (default 5 ms, increase for congested hosts)"
  18. #include "libbb.h"
  19. /* debugging */
  20. #ifdef DEBUG_PSCAN
  21. #define DMSG(...) bb_error_msg(__VA_ARGS__)
  22. #define DERR(...) bb_perror_msg(__VA_ARGS__)
  23. #else
  24. #define DMSG(...) ((void)0)
  25. #define DERR(...) ((void)0)
  26. #endif
  27. static const char *port_name(unsigned port)
  28. {
  29. struct servent *server;
  30. server = getservbyport(htons(port), NULL);
  31. if (server)
  32. return server->s_name;
  33. return "unknown";
  34. }
  35. /* We don't expect to see 1000+ seconds delay, unsigned is enough */
  36. #define MONOTONIC_US() ((unsigned)monotonic_us())
  37. int pscan_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int pscan_main(int argc UNUSED_PARAM, char **argv)
  39. {
  40. const char *opt_max_port = "1024"; /* -P: default max port */
  41. const char *opt_min_port = "1"; /* -p: default min port */
  42. const char *opt_timeout = "5000"; /* -t: default timeout in msec */
  43. /* We estimate rtt and wait rtt*4 before concluding that port is
  44. * totally blocked. min rtt of 5 ms may be too low if you are
  45. * scanning an Internet host behind saturated/traffic shaped link.
  46. * Rule of thumb: with min_rtt of N msec, scanning 1000 ports
  47. * will take N seconds at absolute minimum */
  48. const char *opt_min_rtt = "5"; /* -T: default min rtt in msec */
  49. const char *result_str;
  50. len_and_sockaddr *lsap;
  51. int s;
  52. unsigned opt;
  53. unsigned port, max_port, nports;
  54. unsigned closed_ports = 0;
  55. unsigned open_ports = 0;
  56. /* all in usec */
  57. unsigned timeout;
  58. unsigned min_rtt;
  59. unsigned rtt_4;
  60. unsigned start, diff;
  61. opt_complementary = "=1"; /* exactly one non-option */
  62. opt = getopt32(argv, "cbp:P:t:T:", &opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt);
  63. argv += optind;
  64. max_port = xatou_range(opt_max_port, 1, 65535);
  65. port = xatou_range(opt_min_port, 1, max_port);
  66. nports = max_port - port + 1;
  67. min_rtt = xatou_range(opt_min_rtt, 1, INT_MAX/1000 / 4) * 1000;
  68. timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000;
  69. /* Initial rtt is BIG: */
  70. rtt_4 = timeout;
  71. DMSG("min_rtt %u timeout %u", min_rtt, timeout);
  72. lsap = xhost2sockaddr(*argv, port);
  73. printf("Scanning %s ports %u to %u\n Port\tProto\tState\tService\n",
  74. *argv, port, max_port);
  75. for (; port <= max_port; port++) {
  76. DMSG("rtt %u", rtt_4);
  77. /* The SOCK_STREAM socket type is implemented on the TCP/IP protocol. */
  78. set_nport(&lsap->u.sa, htons(port));
  79. s = xsocket(lsap->u.sa.sa_family, SOCK_STREAM, 0);
  80. /* We need unblocking socket so we don't need to wait for ETIMEOUT. */
  81. /* Nonblocking connect typically "fails" with errno == EINPROGRESS */
  82. ndelay_on(s);
  83. DMSG("connect to port %u", port);
  84. result_str = NULL;
  85. start = MONOTONIC_US();
  86. if (connect(s, &lsap->u.sa, lsap->len) == 0) {
  87. /* Unlikely, for me even localhost fails :) */
  88. DMSG("connect succeeded");
  89. goto open;
  90. }
  91. /* Check for untypical errors... */
  92. if (errno != EAGAIN && errno != EINPROGRESS
  93. && errno != ECONNREFUSED
  94. ) {
  95. bb_perror_nomsg_and_die();
  96. }
  97. diff = 0;
  98. while (1) {
  99. if (errno == ECONNREFUSED) {
  100. if (opt & 1) /* -c: show closed too */
  101. result_str = "closed";
  102. closed_ports++;
  103. break;
  104. }
  105. DERR("port %u errno %d @%u", port, errno, diff);
  106. if (diff > rtt_4) {
  107. if (opt & 2) /* -b: show blocked too */
  108. result_str = "blocked";
  109. break;
  110. }
  111. /* Can sleep (much) longer than specified delay.
  112. * We check rtt BEFORE we usleep, otherwise
  113. * on localhost we'll have no writes done (!)
  114. * before we exceed (rather small) rtt */
  115. usleep(rtt_4/8);
  116. open:
  117. diff = MONOTONIC_US() - start;
  118. DMSG("write to port %u @%u", port, diff - start);
  119. if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */
  120. open_ports++;
  121. result_str = "open";
  122. break;
  123. }
  124. }
  125. DMSG("out of loop @%u", diff);
  126. if (result_str)
  127. printf("%5u" "\t" "tcp" "\t" "%s" "\t" "%s" "\n",
  128. port, result_str, port_name(port));
  129. /* Estimate new rtt - we don't want to wait entire timeout
  130. * for each port. *4 allows for rise in net delay.
  131. * We increase rtt quickly (rtt_4*4), decrease slowly
  132. * (diff is at least rtt_4/8, *4 == rtt_4/2)
  133. * because we don't want to accidentally miss ports. */
  134. rtt_4 = diff * 4;
  135. if (rtt_4 < min_rtt)
  136. rtt_4 = min_rtt;
  137. if (rtt_4 > timeout)
  138. rtt_4 = timeout;
  139. /* Clean up */
  140. close(s);
  141. }
  142. if (ENABLE_FEATURE_CLEAN_UP) free(lsap);
  143. printf("%u closed, %u open, %u timed out (or blocked) ports\n",
  144. closed_ports,
  145. open_ports,
  146. nports - (closed_ports + open_ports));
  147. return EXIT_SUCCESS;
  148. }