pscan.c 4.5 KB

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