Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/networking/ping.c


Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

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

    r2725 r3232  
    2929#include <netinet/ip_icmp.h>
    3030#include "libbb.h"
     31
     32#ifdef __BIONIC__
     33/* should be in netinet/ip_icmp.h */
     34# define ICMP_DEST_UNREACH    3  /* Destination Unreachable  */
     35# define ICMP_SOURCE_QUENCH   4  /* Source Quench    */
     36# define ICMP_REDIRECT        5  /* Redirect (change route)  */
     37# define ICMP_ECHO            8  /* Echo Request      */
     38# define ICMP_TIME_EXCEEDED  11  /* Time Exceeded    */
     39# define ICMP_PARAMETERPROB  12  /* Parameter Problem    */
     40# define ICMP_TIMESTAMP      13  /* Timestamp Request    */
     41# define ICMP_TIMESTAMPREPLY 14  /* Timestamp Reply    */
     42# define ICMP_INFO_REQUEST   15  /* Information Request    */
     43# define ICMP_INFO_REPLY     16  /* Information Reply    */
     44# define ICMP_ADDRESS        17  /* Address Mask Request    */
     45# define ICMP_ADDRESSREPLY   18  /* Address Mask Reply    */
     46#endif
     47
     48//config:config PING
     49//config:   bool "ping"
     50//config:   default y
     51//config:   select PLATFORM_LINUX
     52//config:   help
     53//config:     ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to
     54//config:     elicit an ICMP ECHO_RESPONSE from a host or gateway.
     55//config:
     56//config:config PING6
     57//config:   bool "ping6"
     58//config:   default y
     59//config:   depends on FEATURE_IPV6 && PING
     60//config:   help
     61//config:     This will give you a ping that can talk IPv6.
     62//config:
     63//config:config FEATURE_FANCY_PING
     64//config:   bool "Enable fancy ping output"
     65//config:   default y
     66//config:   depends on PING
     67//config:   help
     68//config:     Make the output from the ping applet include statistics, and at the
     69//config:     same time provide full support for ICMP packets.
     70
     71/* Needs socket(AF_INET, SOCK_RAW, IPPROTO_ICMP), therefore BB_SUID_MAYBE: */
     72//applet:IF_PING(APPLET(ping, BB_DIR_BIN, BB_SUID_MAYBE))
     73//applet:IF_PING6(APPLET(ping6, BB_DIR_BIN, BB_SUID_MAYBE))
     74
     75//kbuild:lib-$(CONFIG_PING)  += ping.o
     76//kbuild:lib-$(CONFIG_PING6) += ping.o
     77
     78//usage:#if !ENABLE_FEATURE_FANCY_PING
     79//usage:# define ping_trivial_usage
     80//usage:       "HOST"
     81//usage:# define ping_full_usage "\n\n"
     82//usage:       "Send ICMP ECHO_REQUEST packets to network hosts"
     83//usage:# define ping6_trivial_usage
     84//usage:       "HOST"
     85//usage:# define ping6_full_usage "\n\n"
     86//usage:       "Send ICMP ECHO_REQUEST packets to network hosts"
     87//usage:#else
     88//usage:# define ping_trivial_usage
     89//usage:       "[OPTIONS] HOST"
     90//usage:# define ping_full_usage "\n\n"
     91//usage:       "Send ICMP ECHO_REQUEST packets to network hosts\n"
     92//usage:     "\n    -4,-6       Force IP or IPv6 name resolution"
     93//usage:     "\n    -c CNT      Send only CNT pings"
     94//usage:     "\n    -s SIZE     Send SIZE data bytes in packets (default:56)"
     95//usage:     "\n    -t TTL      Set TTL"
     96//usage:     "\n    -I IFACE/IP Use interface or IP address as source"
     97//usage:     "\n    -W SEC      Seconds to wait for the first response (default:10)"
     98//usage:     "\n            (after all -c CNT packets are sent)"
     99//usage:     "\n    -w SEC      Seconds until ping exits (default:infinite)"
     100//usage:     "\n            (can exit earlier with -c CNT)"
     101//usage:     "\n    -q      Quiet, only displays output at start"
     102//usage:     "\n            and when finished"
     103//usage:
     104//usage:# define ping6_trivial_usage
     105//usage:       "[OPTIONS] HOST"
     106//usage:# define ping6_full_usage "\n\n"
     107//usage:       "Send ICMP ECHO_REQUEST packets to network hosts\n"
     108//usage:     "\n    -c CNT      Send only CNT pings"
     109//usage:     "\n    -s SIZE     Send SIZE data bytes in packets (default:56)"
     110//usage:     "\n    -I IFACE/IP Use interface or IP address as source"
     111//usage:     "\n    -q      Quiet, only displays output at start"
     112//usage:     "\n            and when finished"
     113//usage:
     114//usage:#endif
     115//usage:
     116//usage:#define ping_example_usage
     117//usage:       "$ ping localhost\n"
     118//usage:       "PING slag (127.0.0.1): 56 data bytes\n"
     119//usage:       "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n"
     120//usage:       "\n"
     121//usage:       "--- debian ping statistics ---\n"
     122//usage:       "1 packets transmitted, 1 packets received, 0% packet loss\n"
     123//usage:       "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
     124//usage:#define ping6_example_usage
     125//usage:       "$ ping6 ip6-localhost\n"
     126//usage:       "PING ip6-localhost (::1): 56 data bytes\n"
     127//usage:       "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n"
     128//usage:       "\n"
     129//usage:       "--- ip6-localhost ping statistics ---\n"
     130//usage:       "1 packets transmitted, 1 packets received, 0% packet loss\n"
     131//usage:       "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
    31132
    32133#if ENABLE_PING6
     
    49150};
    50151
    51 /* Common routines */
    52 
    53 static int in_cksum(unsigned short *buf, int sz)
    54 {
    55     int nleft = sz;
    56     int sum = 0;
    57     unsigned short *w = buf;
    58     unsigned short ans = 0;
    59 
    60     while (nleft > 1) {
    61         sum += *w++;
    62         nleft -= 2;
    63     }
    64 
    65     if (nleft == 1) {
    66         *(unsigned char *) (&ans) = *(unsigned char *) w;
    67         sum += ans;
    68     }
    69 
    70     sum = (sum >> 16) + (sum & 0xFFFF);
    71     sum += (sum >> 16);
    72     ans = ~sum;
    73     return ans;
    74 }
    75 
    76152#if !ENABLE_FEATURE_FANCY_PING
    77153
     
    101177    memset(pkt, 0, sizeof(G.packet));
    102178    pkt->icmp_type = ICMP_ECHO;
    103     pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(G.packet));
     179    pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet));
    104180
    105181    xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len);
     
    224300/* Full(er) version */
    225301
    226 #define OPT_STRING ("qvc:s:w:W:I:4" IF_PING6("6"))
     302#define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6"))
    227303enum {
    228304    OPT_QUIET = 1 << 0,
     
    230306    OPT_c = 1 << 2,
    231307    OPT_s = 1 << 3,
    232     OPT_w = 1 << 4,
    233     OPT_W = 1 << 5,
    234     OPT_I = 1 << 6,
    235     OPT_IPV4 = 1 << 7,
    236     OPT_IPV6 = (1 << 8) * ENABLE_PING6,
     308    OPT_t = 1 << 4,
     309    OPT_w = 1 << 5,
     310    OPT_W = 1 << 6,
     311    OPT_I = 1 << 7,
     312    OPT_IPV4 = 1 << 8,
     313    OPT_IPV6 = (1 << 9) * ENABLE_PING6,
    237314};
    238315
     
    245322    unsigned datalen;
    246323    unsigned pingcount; /* must be int-sized */
     324    unsigned opt_ttl;
    247325    unsigned long ntransmitted, nreceived, nrepeats;
    248326    uint16_t myid;
     
    276354#define nrepeats     (G.nrepeats    )
    277355#define pingcount    (G.pingcount   )
     356#define opt_ttl      (G.opt_ttl     )
    278357#define myid         (G.myid        )
    279358#define tmin         (G.tmin        )
     
    330409}
    331410
    332 static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt)
     411static void sendping_tail(void (*sp)(int), int size_pkt)
    333412{
    334413    int sz;
     
    337416    ntransmitted++;
    338417
     418    size_pkt += datalen;
     419
    339420    /* sizeof(pingaddr) can be larger than real sa size, but I think
    340421     * it doesn't matter */
    341     sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));
     422    sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr));
    342423    if (sz != size_pkt)
    343424        bb_error_msg_and_die(bb_msg_write_error);
     
    388469        *(uint32_t*)&pkt->icmp_dun = monotonic_us();
    389470
    390     pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
    391 
    392     sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN);
     471    pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, datalen + ICMP_MINLEN);
     472
     473    sendping_tail(sendping4, ICMP_MINLEN);
    393474}
    394475#if ENABLE_PING6
     
    407488        *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
    408489
    409     //TODO? pkt->icmp_cksum = in_cksum(...);
    410 
    411     sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr));
     490    //TODO? pkt->icmp_cksum = inet_cksum(...);
     491
     492    sendping_tail(sendping6, sizeof(struct icmp6_hdr));
    412493}
    413494#endif
     
    533614}
    534615#if ENABLE_PING6
    535 static void unpack6(char *packet, int sz, /*struct sockaddr_in6 *from,*/ int hoplimit)
     616static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
    536617{
    537618    struct icmp6_hdr *icmppkt;
     
    553634            tp = (uint32_t *) &icmppkt->icmp6_data8[4];
    554635        unpack_tail(sz, tp,
    555             inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
     636            inet_ntop(AF_INET6, &from->sin6_addr,
    556637                    buf, sizeof(buf)),
    557638            recv_seq, hoplimit);
     
    586667    sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
    587668    setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
     669
     670    if (opt_ttl != 0) {
     671        setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl));
     672        /* above doesnt affect packets sent to bcast IP, so... */
     673        setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl));
     674    }
    588675
    589676    signal(SIGINT, print_stats_and_exit);
     
    638725        }
    639726        if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
    640                        sizeof(filt)) < 0)
     727                    sizeof(filt)) < 0)
    641728            bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
    642729    }
     
    697784            }
    698785        }
    699         unpack6(G.rcv_packet, c, /*&from,*/ hoplimit);
     786        unpack6(G.rcv_packet, c, &from, hoplimit);
    700787        if (pingcount && nreceived >= pingcount)
    701788            break;
     
    736823    INIT_G();
    737824
    738     /* exactly one argument needed; -v and -q don't mix; -c NUM, -w NUM, -W NUM */
    739     opt_complementary = "=1:q--v:v--q:c+:w+:W+";
    740     opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &deadline, &timeout, &str_I);
     825    /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
     826    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);
    741828    if (opt & OPT_s)
    742829        datalen = xatou16(str_s); // -s
Note: See TracChangeset for help on using the changeset viewer.