3
0

pscan.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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, 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. len_and_sockaddr *lsap;
  40. int s;
  41. unsigned port, max_port, nports;
  42. unsigned closed_ports = 0;
  43. unsigned open_ports = 0;
  44. /* all in usec */
  45. unsigned timeout;
  46. unsigned min_rtt;
  47. unsigned rtt_4;
  48. unsigned start;
  49. opt_complementary = "=1"; /* exactly one non-option */
  50. getopt32(argv, "p:P:t:T:", &opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt);
  51. argv += optind;
  52. max_port = xatou_range(opt_max_port, 1, 65535);
  53. port = xatou_range(opt_min_port, 1, max_port);
  54. nports = max_port - port + 1;
  55. rtt_4 = timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000;
  56. min_rtt = xatou_range(opt_min_rtt, 1, INT_MAX/1000 / 4) * 1000;
  57. DMSG("min_rtt %u timeout %u", min_rtt, timeout);
  58. lsap = xhost2sockaddr(*argv, port);
  59. printf("Scanning %s ports %u to %u\n Port\tProto\tState\tService\n",
  60. *argv, port, max_port);
  61. for (; port <= max_port; port++) {
  62. DMSG("rtt %u", rtt_4);
  63. /* The SOCK_STREAM socket type is implemented on the TCP/IP protocol. */
  64. set_nport(lsap, htons(port));
  65. s = xsocket(lsap->sa.sa_family, SOCK_STREAM, 0);
  66. /* We need unblocking socket so we don't need to wait for ETIMEOUT. */
  67. /* Nonblocking connect typically "fails" with errno == EINPROGRESS */
  68. ndelay_on(s);
  69. DMSG("connect to port %u", port);
  70. start = MONOTONIC_US();
  71. if (connect(s, &lsap->sa, lsap->len) == 0) {
  72. /* Unlikely, for me even localhost fails :) */
  73. DMSG("connect succeeded");
  74. goto open;
  75. }
  76. /* Check for untypical errors... */
  77. if (errno != EAGAIN && errno != EINPROGRESS
  78. && errno != ECONNREFUSED
  79. ) {
  80. bb_perror_nomsg_and_die();
  81. }
  82. while (1) {
  83. if (errno == ECONNREFUSED) {
  84. DMSG("port %u: ECONNREFUSED", port);
  85. closed_ports++;
  86. break;
  87. }
  88. DERR("port %u errno %d @%u", port, errno, MONOTONIC_US() - start);
  89. if ((MONOTONIC_US() - start) > rtt_4)
  90. break;
  91. /* Can sleep (much) longer than specified delay.
  92. * We check rtt BEFORE we usleep, otherwise
  93. * on localhost we'll do zero writes done (!)
  94. * before we exceed (rather small) rtt */
  95. usleep(rtt_4/8);
  96. DMSG("write to port %u @%u", port, MONOTONIC_US() - start);
  97. if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */
  98. open:
  99. open_ports++;
  100. printf("%5u\ttcp\topen\t%s\n", port, port_name(port));
  101. break;
  102. }
  103. }
  104. DMSG("out of loop @%u", MONOTONIC_US() - start);
  105. /* Estimate new rtt - we don't want to wait entire timeout
  106. * for each port. *4 allows for rise in net delay.
  107. * We increase rtt quickly (*4), decrease slowly (4/8 == 1/2)
  108. * because we don't want to accidentally miss ports. */
  109. rtt_4 = (MONOTONIC_US() - start) * 4;
  110. if (rtt_4 < min_rtt)
  111. rtt_4 = min_rtt;
  112. if (rtt_4 > timeout)
  113. rtt_4 = timeout;
  114. /* Clean up */
  115. close(s);
  116. }
  117. if (ENABLE_FEATURE_CLEAN_UP) free(lsap);
  118. printf("%d closed, %d open, %d timed out ports\n",
  119. closed_ports,
  120. open_ports,
  121. nports - (closed_ports + open_ports));
  122. return EXIT_SUCCESS;
  123. }