blob: 10bf3b606f78883f736deac91d3ab2650073924a [file] [log] [blame]
Denis Vlasenkoab47eee2007-06-16 20:52:33 +00001/*
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
9#include "libbb.h"
10
11/* debugging */
12#ifdef DEBUG_PSCAN
13#define DMSG(...) bb_error_msg(__VA_ARGS__)
14#define DERR(...) bb_perror_msg(__VA_ARGS__)
15#else
16#define DMSG(...) ((void)0)
17#define DERR(...) ((void)0)
18#endif
19
20/* return time in usec */
21// TODO: move to libbb and use in traceroute, zcip, arping etc
22// (maybe also use absolute monotonic clock - no time warps
23// due to admin resetting date/time?)
24static unsigned gettimeofday_us(void)
25{
26 struct timeval now;
27
28 if (gettimeofday(&now, NULL))
29 return 0;
30 return (now.tv_sec * 1000000 + now.tv_usec);
31}
32
33static const char *port_name(unsigned port)
34{
35 struct servent *server;
36
37 server = getservbyport(htons(port), NULL);
38 if (server)
39 return server->s_name;
40 return "unknown";
41}
42
43int pscan_main(int argc, char **argv);
44int pscan_main(int argc, char **argv)
45{
46 const char *opt_max_port = "1024"; /* -P: default max port */
47 const char *opt_min_port = "1"; /* -p: default min port */
48 const char *opt_timeout = "5000"; /* -t: default timeout */
49 /* We estimate rtt and wait rtt*4 before concluding that port is
50 * totally blocked. min rtt of 5 ms may be too low if you are
51 * scanning an Internet host behind saturated/traffic shaped link.
52 * Rule of thumb: with min_rtt of N msec, scanning 1000 ports
53 * will take N seconds at absolute minimum */
54 const char *opt_min_rtt = "5"; /* -T: default min rtt */
55 len_and_sockaddr *lsap;
56 int s;
57 unsigned port, max_port, nports;
58 unsigned closed_ports = 0;
59 unsigned open_ports = 0;
60 /* all in usec */
61 unsigned timeout;
62 unsigned min_rtt;
63 unsigned rtt_4;
64 unsigned start;
65
66 opt_complementary = "=1"; /* exactly one non-option */
67 getopt32(argc, argv, "p:P:t:T:", &opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt);
68 argv += optind;
69 max_port = xatou_range(opt_max_port, 1, 65535);
70 port = xatou_range(opt_min_port, 1, 65535);
71 nports = max_port - port + 1;
72 if ((int)nports <= 0)
73 bb_show_usage();
74 rtt_4 = timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000;
75 min_rtt = xatou_range(opt_min_rtt, 1, INT_MAX/1000 / 4) * 1000;
76
77 DMSG("min_rtt %u timeout %u", min_rtt, timeout);
78
79 lsap = xhost2sockaddr(*argv, port);
80 printf("Scanning %s ports %u to %u\n Port\tProto\tState\tService\n",
81 *argv, port, max_port);
82
83 for (; port <= max_port; port++) {
84 DMSG("rtt %u", rtt_4);
85
86 /* The SOCK_STREAM socket type is implemented on the TCP/IP protocol. */
87 set_nport(lsap, htons(port));
88 s = xsocket(lsap->sa.sa_family, SOCK_STREAM, 0);
89
90 /* We need unblocking socket so we don't need to wait for ETIMEOUT. */
91 /* Nonblocking connect typically "fails" with errno == EINPROGRESS */
92 ndelay_on(s);
93 DMSG("connect to port %u", port);
94 start = gettimeofday_us();
95 if (connect(s, &lsap->sa, lsap->len) == 0) {
96 /* Unlikely, for me even localhost fails :) */
97 DMSG("connect succeeded");
98 goto open;
99 }
100 /* Check for untypical errors... */
101 if (errno != EAGAIN && errno != EINPROGRESS
102 && errno != ECONNREFUSED
103 ) {
104 bb_perror_nomsg_and_die();
105 }
106
107 while (1) {
108 if (errno == ECONNREFUSED) {
109 DMSG("port %u: ECONNREFUSED", port);
110 closed_ports++;
111 break;
112 }
113 DERR("port %u errno %d @%u", port, errno, gettimeofday_us() - start);
114 if ((gettimeofday_us() - start) > rtt_4)
115 break;
116 /* Can sleep (much) longer than specified delay.
117 * We check rtt BEFORE we usleep, otherwise
118 * on localhost we'll do zero writes done (!)
119 * before we exceed (rather small) rtt */
120 usleep(rtt_4/8);
121 DMSG("write to port %u @%u", port, gettimeofday_us() - start);
122 if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */
123 open:
124 open_ports++;
125 printf("%5u\ttcp\topen\t%s\n", port, port_name(port));
126 break;
127 }
128 }
129 DMSG("out of loop @%u", gettimeofday_us() - start);
130
131 /* Estimate new rtt - we don't want to wait entire timeout
132 * for each port. *4 allows for rise in net delay.
133 * We increase rtt quickly (*4), decrease slowly (4/8 == 1/2)
134 * because we don't want to accidentally miss ports. */
135 rtt_4 = (gettimeofday_us() - start) * 4;
136 if (rtt_4 < min_rtt)
137 rtt_4 = min_rtt;
138 if (rtt_4 > timeout)
139 rtt_4 = timeout;
140 /* Clean up */
141 close(s);
142 }
143 if (ENABLE_FEATURE_CLEAN_UP) free(lsap);
144
145 printf("%d closed, %d open, %d timed out ports\n",
146 closed_ports,
147 open_ports,
148 nports - (closed_ports + open_ports));
149 return EXIT_SUCCESS;
150}