Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/networking/pscan.c

    r1765 r2725  
    44 * Copyright 2007 Tito Ragusa <farmatito@tiscali.it>
    55 *
    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.
    77 */
    88
     
    3131#define MONOTONIC_US() ((unsigned)monotonic_us())
    3232
    33 int pscan_main(int argc, char **argv);
    34 int pscan_main(int argc, char **argv)
     33int pscan_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     34int pscan_main(int argc UNUSED_PARAM, char **argv)
    3535{
    3636    const char *opt_max_port = "1024";      /* -P: default max port */
     
    4343     * will take N seconds at absolute minimum */
    4444    const char *opt_min_rtt = "5";          /* -T: default min rtt in msec */
     45    const char *result_str;
    4546    len_and_sockaddr *lsap;
    4647    int s;
     48    unsigned opt;
    4749    unsigned port, max_port, nports;
    4850    unsigned closed_ports = 0;
     
    5254    unsigned min_rtt;
    5355    unsigned rtt_4;
    54     unsigned start;
     56    unsigned start, diff;
    5557
    5658    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);
    5860    argv += optind;
    5961    max_port = xatou_range(opt_max_port, 1, 65535);
    6062    port = xatou_range(opt_min_port, 1, max_port);
    6163    nports = max_port - port + 1;
    62     rtt_4 = timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000;
    6364    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;
    6468
    6569    DMSG("min_rtt %u timeout %u", min_rtt, timeout);
     
    7478        /* The SOCK_STREAM socket type is implemented on the TCP/IP protocol. */
    7579        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);
    7881        /* We need unblocking socket so we don't need to wait for ETIMEOUT. */
    7982        /* Nonblocking connect typically "fails" with errno == EINPROGRESS */
    8083        ndelay_on(s);
     84
    8185        DMSG("connect to port %u", port);
     86        result_str = NULL;
    8287        start = MONOTONIC_US();
    83         if (connect(s, &lsap->sa, lsap->len) == 0) {
     88        if (connect(s, &lsap->u.sa, lsap->len) == 0) {
    8489            /* Unlikely, for me even localhost fails :) */
    8590            DMSG("connect succeeded");
     
    9398        }
    9499
     100        diff = 0;
    95101        while (1) {
    96102            if (errno == ECONNREFUSED) {
    97                 DMSG("port %u: ECONNREFUSED", port);
     103                if (opt & 1) /* -c: show closed too */
     104                    result_str = "closed";
    98105                closed_ports++;
    99106                break;
    100107            }
    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";
    103113                break;
     114            }
    104115            /* Can sleep (much) longer than specified delay.
    105116             * 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 (!)
    107118             * before we exceed (rather small) rtt */
    108119            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);
    110123            if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */
    111  open:
    112124                open_ports++;
    113                 printf("%5u\ttcp\topen\t%s\n", port, port_name(port));
     125                result_str = "open";
    114126                break;
    115127            }
    116128        }
    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));
    118133
    119134        /* Estimate new rtt - we don't want to wait entire timeout
    120135         * 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)
    122138         * because we don't want to accidentally miss ports. */
    123         rtt_4 = (MONOTONIC_US() - start) * 4;
     139        rtt_4 = diff * 4;
    124140        if (rtt_4 < min_rtt)
    125141            rtt_4 = min_rtt;
     
    131147    if (ENABLE_FEATURE_CLEAN_UP) free(lsap);
    132148
    133     printf("%d closed, %d open, %d timed out ports\n",
     149    printf("%d closed, %d open, %d timed out (or blocked) ports\n",
    134150                    closed_ports,
    135151                    open_ports,
Note: See TracChangeset for help on using the changeset viewer.