Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/networking/pscan.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/networking/pscan.c
r1765 r2725 4 4 * Copyright 2007 Tito Ragusa <farmatito@tiscali.it> 5 5 * 6 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.6 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 7 7 */ 8 8 … … 31 31 #define MONOTONIC_US() ((unsigned)monotonic_us()) 32 32 33 int pscan_main(int argc, char **argv) ;34 int pscan_main(int argc , char **argv)33 int pscan_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 34 int pscan_main(int argc UNUSED_PARAM, char **argv) 35 35 { 36 36 const char *opt_max_port = "1024"; /* -P: default max port */ … … 43 43 * will take N seconds at absolute minimum */ 44 44 const char *opt_min_rtt = "5"; /* -T: default min rtt in msec */ 45 const char *result_str; 45 46 len_and_sockaddr *lsap; 46 47 int s; 48 unsigned opt; 47 49 unsigned port, max_port, nports; 48 50 unsigned closed_ports = 0; … … 52 54 unsigned min_rtt; 53 55 unsigned rtt_4; 54 unsigned start ;56 unsigned start, diff; 55 57 56 58 opt_complementary = "=1"; /* exactly one non-option */ 57 getopt32(argv, "p:P:t:T:", &opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt);59 opt = getopt32(argv, "cbp:P:t:T:", &opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt); 58 60 argv += optind; 59 61 max_port = xatou_range(opt_max_port, 1, 65535); 60 62 port = xatou_range(opt_min_port, 1, max_port); 61 63 nports = max_port - port + 1; 62 rtt_4 = timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000;63 64 min_rtt = xatou_range(opt_min_rtt, 1, INT_MAX/1000 / 4) * 1000; 65 timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000; 66 /* Initial rtt is BIG: */ 67 rtt_4 = timeout; 64 68 65 69 DMSG("min_rtt %u timeout %u", min_rtt, timeout); … … 74 78 /* The SOCK_STREAM socket type is implemented on the TCP/IP protocol. */ 75 79 set_nport(lsap, htons(port)); 76 s = xsocket(lsap->sa.sa_family, SOCK_STREAM, 0); 77 80 s = xsocket(lsap->u.sa.sa_family, SOCK_STREAM, 0); 78 81 /* We need unblocking socket so we don't need to wait for ETIMEOUT. */ 79 82 /* Nonblocking connect typically "fails" with errno == EINPROGRESS */ 80 83 ndelay_on(s); 84 81 85 DMSG("connect to port %u", port); 86 result_str = NULL; 82 87 start = MONOTONIC_US(); 83 if (connect(s, &lsap-> sa, lsap->len) == 0) {88 if (connect(s, &lsap->u.sa, lsap->len) == 0) { 84 89 /* Unlikely, for me even localhost fails :) */ 85 90 DMSG("connect succeeded"); … … 93 98 } 94 99 100 diff = 0; 95 101 while (1) { 96 102 if (errno == ECONNREFUSED) { 97 DMSG("port %u: ECONNREFUSED", port); 103 if (opt & 1) /* -c: show closed too */ 104 result_str = "closed"; 98 105 closed_ports++; 99 106 break; 100 107 } 101 DERR("port %u errno %d @%u", port, errno, MONOTONIC_US() - start); 102 if ((MONOTONIC_US() - start) > rtt_4) 108 DERR("port %u errno %d @%u", port, errno, diff); 109 110 if (diff > rtt_4) { 111 if (opt & 2) /* -b: show blocked too */ 112 result_str = "blocked"; 103 113 break; 114 } 104 115 /* Can sleep (much) longer than specified delay. 105 116 * We check rtt BEFORE we usleep, otherwise 106 * on localhost we'll do zero writes done (!)117 * on localhost we'll have no writes done (!) 107 118 * before we exceed (rather small) rtt */ 108 119 usleep(rtt_4/8); 109 DMSG("write to port %u @%u", port, MONOTONIC_US() - start); 120 open: 121 diff = MONOTONIC_US() - start; 122 DMSG("write to port %u @%u", port, diff - start); 110 123 if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */ 111 open:112 124 open_ports++; 113 printf("%5u\ttcp\topen\t%s\n", port, port_name(port));125 result_str = "open"; 114 126 break; 115 127 } 116 128 } 117 DMSG("out of loop @%u", MONOTONIC_US() - start); 129 DMSG("out of loop @%u", diff); 130 if (result_str) 131 printf("%5u" "\t" "tcp" "\t" "%s" "\t" "%s" "\n", 132 port, result_str, port_name(port)); 118 133 119 134 /* Estimate new rtt - we don't want to wait entire timeout 120 135 * for each port. *4 allows for rise in net delay. 121 * We increase rtt quickly (*4), decrease slowly (4/8 == 1/2) 136 * We increase rtt quickly (rtt_4*4), decrease slowly 137 * (diff is at least rtt_4/8, *4 == rtt_4/2) 122 138 * because we don't want to accidentally miss ports. */ 123 rtt_4 = (MONOTONIC_US() - start)* 4;139 rtt_4 = diff * 4; 124 140 if (rtt_4 < min_rtt) 125 141 rtt_4 = min_rtt; … … 131 147 if (ENABLE_FEATURE_CLEAN_UP) free(lsap); 132 148 133 printf("%d closed, %d open, %d timed out ports\n",149 printf("%d closed, %d open, %d timed out (or blocked) ports\n", 134 150 closed_ports, 135 151 open_ports,
Note:
See TracChangeset
for help on using the changeset viewer.