Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/networking/ping.c


Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/networking/ping.c

    r3232 r3621  
    2929#include <netinet/ip_icmp.h>
    3030#include "libbb.h"
     31#include "common_bufsiz.h"
    3132
    3233#ifdef __BIONIC__
     
    9091//usage:# define ping_full_usage "\n\n"
    9192//usage:       "Send ICMP ECHO_REQUEST packets to network hosts\n"
     93//usage:    IF_PING6(
    9294//usage:     "\n    -4,-6       Force IP or IPv6 name resolution"
     95//usage:    )
    9396//usage:     "\n    -c CNT      Send only CNT pings"
    9497//usage:     "\n    -s SIZE     Send SIZE data bytes in packets (default:56)"
     
    99102//usage:     "\n    -w SEC      Seconds until ping exits (default:infinite)"
    100103//usage:     "\n            (can exit earlier with -c CNT)"
    101 //usage:     "\n    -q      Quiet, only displays output at start"
     104//usage:     "\n    -q      Quiet, only display output at start"
    102105//usage:     "\n            and when finished"
     106//usage:     "\n    -p      Pattern to use for payload"
    103107//usage:
    104108//usage:# define ping6_trivial_usage
     
    109113//usage:     "\n    -s SIZE     Send SIZE data bytes in packets (default:56)"
    110114//usage:     "\n    -I IFACE/IP Use interface or IP address as source"
    111 //usage:     "\n    -q      Quiet, only displays output at start"
     115//usage:     "\n    -q      Quiet, only display output at start"
    112116//usage:     "\n            and when finished"
     117//usage:     "\n    -p      Pattern to use for payload"
    113118//usage:
    114119//usage:#endif
     
    148153    MAXWAIT = 10,
    149154    PINGINTERVAL = 1, /* 1 second */
     155    pingsock = 0,
    150156};
     157
     158static void
     159#if ENABLE_PING6
     160create_icmp_socket(len_and_sockaddr *lsa)
     161#else
     162create_icmp_socket(void)
     163#define create_icmp_socket(lsa) create_icmp_socket()
     164#endif
     165{
     166    int sock;
     167#if ENABLE_PING6
     168    if (lsa->u.sa.sa_family == AF_INET6)
     169        sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
     170    else
     171#endif
     172        sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
     173    if (sock < 0) {
     174        if (errno == EPERM)
     175            bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
     176        bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
     177    }
     178
     179    xmove_fd(sock, pingsock);
     180}
    151181
    152182#if !ENABLE_FEATURE_FANCY_PING
     
    158188    char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
    159189} FIX_ALIASING;
    160 #define G (*(struct globals*)&bb_common_bufsiz1)
    161 #define INIT_G() do { } while (0)
     190#define G (*(struct globals*)bb_common_bufsiz1)
     191#define INIT_G() do { setup_common_bufsiz(); } while (0)
    162192
    163193static void noresp(int ign UNUSED_PARAM)
     
    170200{
    171201    struct icmp *pkt;
    172     int pingsock, c;
    173 
    174     pingsock = create_icmp_socket();
     202    int c;
    175203
    176204    pkt = (struct icmp *) G.packet;
    177     memset(pkt, 0, sizeof(G.packet));
     205    /*memset(pkt, 0, sizeof(G.packet)); already is */
    178206    pkt->icmp_type = ICMP_ECHO;
    179207    pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet));
     
    183211    /* listen for replies */
    184212    while (1) {
     213#if 0
    185214        struct sockaddr_in from;
    186215        socklen_t fromlen = sizeof(from);
     
    188217        c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
    189218                (struct sockaddr *) &from, &fromlen);
     219#else
     220        c = recv(pingsock, G.packet, sizeof(G.packet), 0);
     221#endif
    190222        if (c < 0) {
    191223            if (errno != EINTR)
     
    209241{
    210242    struct icmp6_hdr *pkt;
    211     int pingsock, c;
     243    int c;
    212244    int sockopt;
    213245
    214     pingsock = create_icmp6_socket();
    215 
    216246    pkt = (struct icmp6_hdr *) G.packet;
    217     memset(pkt, 0, sizeof(G.packet));
     247    /*memset(pkt, 0, sizeof(G.packet)); already is */
    218248    pkt->icmp6_type = ICMP6_ECHO_REQUEST;
    219249
    220250    sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
    221     setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
     251    setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt);
    222252
    223253    xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len);
     
    225255    /* listen for replies */
    226256    while (1) {
     257#if 0
    227258        struct sockaddr_in6 from;
    228259        socklen_t fromlen = sizeof(from);
     
    230261        c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
    231262                (struct sockaddr *) &from, &fromlen);
     263#else
     264        c = recv(pingsock, G.packet, sizeof(G.packet), 0);
     265#endif
    232266        if (c < 0) {
    233267            if (errno != EINTR)
     
    235269            continue;
    236270        }
    237         if (c >= ICMP_MINLEN) {         /* icmp6_hdr */
    238             pkt = (struct icmp6_hdr *) G.packet;
     271        if (c >= ICMP_MINLEN) { /* icmp6_hdr */
    239272            if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
    240273                break;
     
    284317    alarm(5); /* give the host 5000ms to respond */
    285318
     319    create_icmp_socket(lsa);
    286320#if ENABLE_PING6
    287321    if (lsa->u.sa.sa_family == AF_INET6)
     
    300334/* Full(er) version */
    301335
    302 #define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6"))
     336#define OPT_STRING ("qvc:s:t:w:W:I:np:4" IF_PING6("6"))
    303337enum {
    304338    OPT_QUIET = 1 << 0,
     
    310344    OPT_W = 1 << 6,
    311345    OPT_I = 1 << 7,
    312     OPT_IPV4 = 1 << 8,
    313     OPT_IPV6 = (1 << 9) * ENABLE_PING6,
     346    /*OPT_n = 1 << 8, - ignored */
     347    OPT_p = 1 << 9,
     348    OPT_IPV4 = 1 << 10,
     349    OPT_IPV6 = (1 << 11) * ENABLE_PING6,
    314350};
    315351
    316352
    317353struct globals {
    318     int pingsock;
    319354    int if_index;
    320355    char *str_I;
     
    325360    unsigned long ntransmitted, nreceived, nrepeats;
    326361    uint16_t myid;
     362    uint8_t pattern;
    327363    unsigned tmin, tmax; /* in us */
    328364    unsigned long long tsum; /* in us, sum of all times */
     
    342378#endif
    343379    } pingaddr;
    344     char rcvd_tbl[MAX_DUP_CHK / 8];
     380    unsigned char rcvd_tbl[MAX_DUP_CHK / 8];
    345381} FIX_ALIASING;
    346 #define G (*(struct globals*)&bb_common_bufsiz1)
    347 #define pingsock     (G.pingsock    )
     382#define G (*(struct globals*)bb_common_bufsiz1)
    348383#define if_index     (G.if_index    )
    349384#define source_lsa   (G.source_lsa  )
    350385#define str_I        (G.str_I       )
    351386#define datalen      (G.datalen     )
    352 #define ntransmitted (G.ntransmitted)
    353 #define nreceived    (G.nreceived   )
    354 #define nrepeats     (G.nrepeats    )
    355387#define pingcount    (G.pingcount   )
    356388#define opt_ttl      (G.opt_ttl     )
     
    366398#define pingaddr     (G.pingaddr    )
    367399#define rcvd_tbl     (G.rcvd_tbl    )
    368 void BUG_ping_globals_too_big(void);
    369400#define INIT_G() do { \
    370     if (sizeof(G) > COMMON_BUFSIZE) \
    371         BUG_ping_globals_too_big(); \
    372     pingsock = -1; \
     401    setup_common_bufsiz(); \
     402    BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
    373403    datalen = DEFDATALEN; \
    374404    timeout = MAXWAIT; \
     
    377407
    378408
    379 #define A(bit)      rcvd_tbl[(bit)>>3]  /* identify byte in array */
    380 #define B(bit)      (1 << ((bit) & 0x07))   /* identify bit in byte */
    381 #define SET(bit)    (A(bit) |= B(bit))
    382 #define CLR(bit)    (A(bit) &= (~B(bit)))
    383 #define TST(bit)    (A(bit) & B(bit))
    384 
    385 /**************************************************************************/
     409#define BYTE(bit)   rcvd_tbl[(bit)>>3]
     410#define MASK(bit)   (1 << ((bit) & 7))
     411#define SET(bit)    (BYTE(bit) |= MASK(bit))
     412#define CLR(bit)    (BYTE(bit) &= (~MASK(bit)))
     413#define TST(bit)    (BYTE(bit) & MASK(bit))
    386414
    387415static void print_stats_and_exit(int junk) NORETURN;
    388416static void print_stats_and_exit(int junk UNUSED_PARAM)
    389417{
     418    unsigned long ul;
     419    unsigned long nrecv;
     420
    390421    signal(SIGINT, SIG_IGN);
    391422
    392     printf("\n--- %s ping statistics ---\n", hostname);
    393     printf("%lu packets transmitted, ", ntransmitted);
    394     printf("%lu packets received, ", nreceived);
    395     if (nrepeats)
    396         printf("%lu duplicates, ", nrepeats);
    397     if (ntransmitted)
    398         ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
    399     printf("%lu%% packet loss\n", ntransmitted);
     423    nrecv = G.nreceived;
     424    printf("\n--- %s ping statistics ---\n"
     425        "%lu packets transmitted, "
     426        "%lu packets received, ",
     427        hostname, G.ntransmitted, nrecv
     428    );
     429    if (G.nrepeats)
     430        printf("%lu duplicates, ", G.nrepeats);
     431    ul = G.ntransmitted;
     432    if (ul != 0)
     433        ul = (ul - nrecv) * 100 / ul;
     434    printf("%lu%% packet loss\n", ul);
    400435    if (tmin != UINT_MAX) {
    401         unsigned tavg = tsum / (nreceived + nrepeats);
     436        unsigned tavg = tsum / (nrecv + G.nrepeats);
    402437        printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
    403438            tmin / 1000, tmin % 1000,
     
    406441    }
    407442    /* if condition is true, exit with 1 -- 'failure' */
    408     exit(nreceived == 0 || (deadline && nreceived < pingcount));
     443    exit(nrecv == 0 || (deadline && nrecv < pingcount));
    409444}
    410445
     
    413448    int sz;
    414449
    415     CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
    416     ntransmitted++;
     450    CLR((uint16_t)G.ntransmitted % MAX_DUP_CHK);
     451    G.ntransmitted++;
    417452
    418453    size_pkt += datalen;
     
    424459        bb_error_msg_and_die(bb_msg_write_error);
    425460
    426     if (pingcount == 0 || deadline || ntransmitted < pingcount) {
     461    if (pingcount == 0 || deadline || G.ntransmitted < pingcount) {
    427462        /* Didn't send all pings yet - schedule next in 1s */
    428463        signal(SIGALRM, sp);
     
    440475        unsigned expire = timeout;
    441476
    442         if (nreceived) {
     477        if (G.nreceived) {
    443478            /* approx. 2*tmax, in seconds (2 RTT) */
    444479            expire = tmax / (512*1024);
     
    455490    struct icmp *pkt = G.snd_packet;
    456491
    457     //memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced
     492    memset(pkt, G.pattern, datalen + ICMP_MINLEN + 4);
    458493    pkt->icmp_type = ICMP_ECHO;
    459494    /*pkt->icmp_code = 0;*/
    460495    pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
    461     pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
     496    pkt->icmp_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
    462497    pkt->icmp_id = myid;
    463498
     
    478513    struct icmp6_hdr *pkt = G.snd_packet;
    479514
    480     //memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4);
     515    memset(pkt, G.pattern, datalen + sizeof(struct icmp6_hdr) + 4);
    481516    pkt->icmp6_type = ICMP6_ECHO_REQUEST;
    482517    /*pkt->icmp6_code = 0;*/
    483518    /*pkt->icmp6_cksum = 0;*/
    484     pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
     519    pkt->icmp6_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
    485520    pkt->icmp6_id = myid;
    486521
    487522    /*if (datalen >= 4)*/
    488         *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
     523        *(bb__aliased_uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
    489524
    490525    //TODO? pkt->icmp_cksum = inet_cksum(...);
     
    546581        uint16_t recv_seq, int ttl)
    547582{
     583    unsigned char *b, m;
    548584    const char *dupmsg = " (DUP!)";
    549585    unsigned triptime = triptime; /* for gcc */
    550 
    551     ++nreceived;
    552586
    553587    if (tp) {
     
    562596    }
    563597
    564     if (TST(recv_seq % MAX_DUP_CHK)) {
    565         ++nrepeats;
    566         --nreceived;
     598    b = &BYTE(recv_seq % MAX_DUP_CHK);
     599    m = MASK(recv_seq % MAX_DUP_CHK);
     600    /*if TST(recv_seq % MAX_DUP_CHK):*/
     601    if (*b & m) {
     602        ++G.nrepeats;
    567603    } else {
    568         SET(recv_seq % MAX_DUP_CHK);
     604        /*SET(recv_seq % MAX_DUP_CHK):*/
     605        *b |= m;
     606        ++G.nreceived;
    569607        dupmsg += 7;
    570608    }
     
    649687    int sockopt;
    650688
    651     pingsock = create_icmp_socket();
    652689    pingaddr.sin = lsa->u.sin;
    653690    if (source_lsa) {
     
    657694        xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
    658695    }
    659     if (str_I)
    660         setsockopt_bindtodevice(pingsock, str_I);
    661696
    662697    /* enable broadcast pings */
     
    666701     * broadcast ping etc) */
    667702    sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
    668     setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
     703    setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt);
    669704
    670705    if (opt_ttl != 0) {
    671         setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl));
     706        setsockopt_int(pingsock, IPPROTO_IP, IP_TTL, opt_ttl);
    672707        /* above doesnt affect packets sent to bcast IP, so... */
    673         setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl));
     708        setsockopt_int(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, opt_ttl);
    674709    }
    675710
     
    693728        }
    694729        unpack4(G.rcv_packet, c, &from);
    695         if (pingcount && nreceived >= pingcount)
     730        if (pingcount && G.nreceived >= pingcount)
    696731            break;
    697732    }
    698733}
    699734#if ENABLE_PING6
    700 extern int BUG_bad_offsetof_icmp6_cksum(void);
    701735static void ping6(len_and_sockaddr *lsa)
    702736{
     
    707741    char control_buf[CMSG_SPACE(36)];
    708742
    709     pingsock = create_icmp6_socket();
    710743    pingaddr.sin6 = lsa->u.sin6;
    711     /* untested whether "-I addr" really works for IPv6: */
    712744    if (source_lsa)
    713745        xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
    714     if (str_I)
    715         setsockopt_bindtodevice(pingsock, str_I);
    716746
    717747#ifdef ICMP6_FILTER
     
    726756        if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
    727757                    sizeof(filt)) < 0)
    728             bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
     758            bb_error_msg_and_die("setsockopt(%s)", "ICMP6_FILTER");
    729759    }
    730760#endif /*ICMP6_FILTER*/
     
    736766     * broadcast ping etc) */
    737767    sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
    738     setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
     768    setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt);
    739769
    740770    sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
    741     if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
    742         BUG_bad_offsetof_icmp6_cksum();
    743     setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
     771    BUILD_BUG_ON(offsetof(struct icmp6_hdr, icmp6_cksum) != 2);
     772    setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt);
    744773
    745774    /* request ttl info to be returned in ancillary data */
    746     setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
     775    setsockopt_1(pingsock, SOL_IPV6, IPV6_HOPLIMIT);
    747776
    748777    if (if_index)
     
    785814        }
    786815        unpack6(G.rcv_packet, c, &from, hoplimit);
    787         if (pingcount && nreceived >= pingcount)
     816        if (pingcount && G.nreceived >= pingcount)
    788817            break;
    789818    }
     
    799828    }
    800829    printf(": %d data bytes\n", datalen);
     830
     831    create_icmp_socket(lsa);
     832    /* untested whether "-I addr" really works for IPv6: */
     833    if (str_I)
     834        setsockopt_bindtodevice(pingsock, str_I);
    801835
    802836    G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN;
     
    819853{
    820854    len_and_sockaddr *lsa;
    821     char *str_s;
     855    char *str_s, *str_p;
    822856
    823857    INIT_G();
     
    825859    /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
    826860    opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+";
    827     opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I);
     861    opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_p);
    828862    if (opt & OPT_s)
    829863        datalen = xatou16(str_s); // -s
     
    836870        }
    837871    }
     872    if (opt & OPT_p)
     873        G.pattern = xstrtou_range(str_p, 16, 0, 255);
     874
    838875    myid = (uint16_t) getpid();
    839876    hostname = argv[optind];
Note: See TracChangeset for help on using the changeset viewer.