123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #include "libbb.h"
- #ifdef DEBUG_PSCAN
- #define DMSG(...) bb_error_msg(__VA_ARGS__)
- #define DERR(...) bb_perror_msg(__VA_ARGS__)
- #else
- #define DMSG(...) ((void)0)
- #define DERR(...) ((void)0)
- #endif
- static const char *port_name(unsigned port)
- {
- struct servent *server;
- server = getservbyport(htons(port), NULL);
- if (server)
- return server->s_name;
- return "unknown";
- }
- #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";
- const char *opt_min_port = "1";
- const char *opt_timeout = "5000";
-
- const char *opt_min_rtt = "5";
- 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;
-
- unsigned timeout;
- unsigned min_rtt;
- unsigned rtt_4;
- unsigned start, diff;
- opt_complementary = "=1";
- 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, max_port);
- nports = max_port - port + 1;
- min_rtt = xatou_range(opt_min_rtt, 1, INT_MAX/1000 / 4) * 1000;
- timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000;
-
- rtt_4 = timeout;
- DMSG("min_rtt %u timeout %u", min_rtt, timeout);
- lsap = xhost2sockaddr(*argv, port);
- printf("Scanning %s ports %u to %u\n Port\tProto\tState\tService\n",
- *argv, port, max_port);
- for (; port <= max_port; port++) {
- DMSG("rtt %u", rtt_4);
-
- set_nport(&lsap->u.sa, htons(port));
- s = xsocket(lsap->u.sa.sa_family, SOCK_STREAM, 0);
-
-
- ndelay_on(s);
- DMSG("connect to port %u", port);
- result_str = NULL;
- start = MONOTONIC_US();
- if (connect(s, &lsap->u.sa, lsap->len) == 0) {
-
- DMSG("connect succeeded");
- goto open;
- }
-
- if (errno != EAGAIN && errno != EINPROGRESS
- && errno != ECONNREFUSED
- ) {
- bb_perror_nomsg_and_die();
- }
- diff = 0;
- while (1) {
- if (errno == ECONNREFUSED) {
- if (opt & 1)
- result_str = "closed";
- closed_ports++;
- break;
- }
- DERR("port %u errno %d @%u", port, errno, diff);
- if (diff > rtt_4) {
- if (opt & 2)
- result_str = "blocked";
- break;
- }
-
- usleep(rtt_4/8);
- open:
- diff = MONOTONIC_US() - start;
- DMSG("write to port %u @%u", port, diff - start);
- if (write(s, " ", 1) >= 0) {
- open_ports++;
- result_str = "open";
- break;
- }
- }
- DMSG("out of loop @%u", diff);
- if (result_str)
- printf("%5u" "\t" "tcp" "\t" "%s" "\t" "%s" "\n",
- port, result_str, port_name(port));
-
- rtt_4 = diff * 4;
- if (rtt_4 < min_rtt)
- rtt_4 = min_rtt;
- if (rtt_4 > timeout)
- rtt_4 = timeout;
-
- close(s);
- }
- if (ENABLE_FEATURE_CLEAN_UP) free(lsap);
- printf("%d closed, %d open, %d timed out (or blocked) ports\n",
- closed_ports,
- open_ports,
- nports - (closed_ports + open_ports));
- return EXIT_SUCCESS;
- }
|