X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fpscan.c;h=5fb6af0b3ba9a7767dfda50bb5f5ae5a113759ee;hb=c8d27334a0921d6ca76862e9c7befe12a4df6dc6;hp=10bf3b606f78883f736deac91d3ab2650073924a;hpb=ab47eeee6aeadd5afd78026d34dfcea129e63dce;p=oweals%2Fbusybox.git diff --git a/networking/pscan.c b/networking/pscan.c index 10bf3b606..5fb6af0b3 100644 --- a/networking/pscan.c +++ b/networking/pscan.c @@ -17,19 +17,6 @@ #define DERR(...) ((void)0) #endif -/* return time in usec */ -// TODO: move to libbb and use in traceroute, zcip, arping etc -// (maybe also use absolute monotonic clock - no time warps -// due to admin resetting date/time?) -static unsigned gettimeofday_us(void) -{ - struct timeval now; - - if (gettimeofday(&now, NULL)) - return 0; - return (now.tv_sec * 1000000 + now.tv_usec); -} - static const char *port_name(unsigned port) { struct servent *server; @@ -40,20 +27,25 @@ static const char *port_name(unsigned port) return "unknown"; } -int pscan_main(int argc, char **argv); -int pscan_main(int argc, char **argv) +/* We don't expect to see 1000+ seconds delay, unsigned is enough */ +#define MONOTONIC_US() ((unsigned)monotonic_us()) + +int pscan_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int pscan_main(int argc UNUSED_PARAM, char **argv) { const char *opt_max_port = "1024"; /* -P: default max port */ const char *opt_min_port = "1"; /* -p: default min port */ - const char *opt_timeout = "5000"; /* -t: default timeout */ - /* We estimate rtt and wait rtt*4 before concluding that port is + const char *opt_timeout = "5000"; /* -t: default timeout in msec */ + /* We estimate rtt and wait rtt*4 before concluding that port is * totally blocked. min rtt of 5 ms may be too low if you are * scanning an Internet host behind saturated/traffic shaped link. * Rule of thumb: with min_rtt of N msec, scanning 1000 ports * will take N seconds at absolute minimum */ - const char *opt_min_rtt = "5"; /* -T: default min rtt */ + const char *opt_min_rtt = "5"; /* -T: default min rtt in msec */ + const char *result_str; len_and_sockaddr *lsap; int s; + unsigned opt; unsigned port, max_port, nports; unsigned closed_ports = 0; unsigned open_ports = 0; @@ -61,18 +53,18 @@ int pscan_main(int argc, char **argv) unsigned timeout; unsigned min_rtt; unsigned rtt_4; - unsigned start; + unsigned start, diff; opt_complementary = "=1"; /* exactly one non-option */ - getopt32(argc, argv, "p:P:t:T:", &opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt); + opt = getopt32(argv, "cbp:P:t:T:", &opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt); argv += optind; max_port = xatou_range(opt_max_port, 1, 65535); - port = xatou_range(opt_min_port, 1, 65535); + port = xatou_range(opt_min_port, 1, max_port); nports = max_port - port + 1; - if ((int)nports <= 0) - bb_show_usage(); - rtt_4 = timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000; min_rtt = xatou_range(opt_min_rtt, 1, INT_MAX/1000 / 4) * 1000; + timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000; + /* Initial rtt is BIG: */ + rtt_4 = timeout; DMSG("min_rtt %u timeout %u", min_rtt, timeout); @@ -85,14 +77,15 @@ int pscan_main(int argc, char **argv) /* The SOCK_STREAM socket type is implemented on the TCP/IP protocol. */ set_nport(lsap, htons(port)); - s = xsocket(lsap->sa.sa_family, SOCK_STREAM, 0); - + s = xsocket(lsap->u.sa.sa_family, SOCK_STREAM, 0); /* We need unblocking socket so we don't need to wait for ETIMEOUT. */ /* Nonblocking connect typically "fails" with errno == EINPROGRESS */ ndelay_on(s); + DMSG("connect to port %u", port); - start = gettimeofday_us(); - if (connect(s, &lsap->sa, lsap->len) == 0) { + result_str = NULL; + start = MONOTONIC_US(); + if (connect(s, &lsap->u.sa, lsap->len) == 0) { /* Unlikely, for me even localhost fails :) */ DMSG("connect succeeded"); goto open; @@ -104,35 +97,46 @@ int pscan_main(int argc, char **argv) bb_perror_nomsg_and_die(); } + diff = 0; while (1) { if (errno == ECONNREFUSED) { - DMSG("port %u: ECONNREFUSED", port); + if (opt & 1) /* -c: show closed too */ + result_str = "closed"; closed_ports++; break; } - DERR("port %u errno %d @%u", port, errno, gettimeofday_us() - start); - if ((gettimeofday_us() - start) > rtt_4) + DERR("port %u errno %d @%u", port, errno, diff); + + if (diff > rtt_4) { + if (opt & 2) /* -b: show blocked too */ + result_str = "blocked"; break; + } /* Can sleep (much) longer than specified delay. * We check rtt BEFORE we usleep, otherwise - * on localhost we'll do zero writes done (!) + * on localhost we'll have no writes done (!) * before we exceed (rather small) rtt */ usleep(rtt_4/8); - DMSG("write to port %u @%u", port, gettimeofday_us() - start); - if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */ open: + diff = MONOTONIC_US() - start; + DMSG("write to port %u @%u", port, diff - start); + if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */ open_ports++; - printf("%5u\ttcp\topen\t%s\n", port, port_name(port)); + result_str = "open"; break; } } - DMSG("out of loop @%u", gettimeofday_us() - start); + DMSG("out of loop @%u", diff); + if (result_str) + printf("%5u" "\t" "tcp" "\t" "%s" "\t" "%s" "\n", + port, result_str, port_name(port)); /* Estimate new rtt - we don't want to wait entire timeout * for each port. *4 allows for rise in net delay. - * We increase rtt quickly (*4), decrease slowly (4/8 == 1/2) + * We increase rtt quickly (rtt_4*4), decrease slowly + * (diff is at least rtt_4/8, *4 == rtt_4/2) * because we don't want to accidentally miss ports. */ - rtt_4 = (gettimeofday_us() - start) * 4; + rtt_4 = diff * 4; if (rtt_4 < min_rtt) rtt_4 = min_rtt; if (rtt_4 > timeout) @@ -142,7 +146,7 @@ int pscan_main(int argc, char **argv) } if (ENABLE_FEATURE_CLEAN_UP) free(lsap); - printf("%d closed, %d open, %d timed out ports\n", + printf("%d closed, %d open, %d timed out (or blocked) ports\n", closed_ports, open_ports, nports - (closed_ports + open_ports));