ping[6]: accept and ignore -n. Code shrink
function old new delta print_stats_and_exit 270 232 -38 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
7b5d5c1bdb
commit
af4a07a244
@ -89,7 +89,9 @@
|
|||||||
//usage: "[OPTIONS] HOST"
|
//usage: "[OPTIONS] HOST"
|
||||||
//usage:# define ping_full_usage "\n\n"
|
//usage:# define ping_full_usage "\n\n"
|
||||||
//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
|
//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
|
||||||
|
//usage: IF_PING6(
|
||||||
//usage: "\n -4,-6 Force IP or IPv6 name resolution"
|
//usage: "\n -4,-6 Force IP or IPv6 name resolution"
|
||||||
|
//usage: )
|
||||||
//usage: "\n -c CNT Send only CNT pings"
|
//usage: "\n -c CNT Send only CNT pings"
|
||||||
//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
|
//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
|
||||||
//usage: "\n -t TTL Set TTL"
|
//usage: "\n -t TTL Set TTL"
|
||||||
@ -299,7 +301,7 @@ static int common_ping_main(sa_family_t af, char **argv)
|
|||||||
|
|
||||||
/* Full(er) version */
|
/* Full(er) version */
|
||||||
|
|
||||||
#define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6"))
|
#define OPT_STRING ("qvc:s:t:w:W:I:n4" IF_PING6("6"))
|
||||||
enum {
|
enum {
|
||||||
OPT_QUIET = 1 << 0,
|
OPT_QUIET = 1 << 0,
|
||||||
OPT_VERBOSE = 1 << 1,
|
OPT_VERBOSE = 1 << 1,
|
||||||
@ -309,8 +311,9 @@ enum {
|
|||||||
OPT_w = 1 << 5,
|
OPT_w = 1 << 5,
|
||||||
OPT_W = 1 << 6,
|
OPT_W = 1 << 6,
|
||||||
OPT_I = 1 << 7,
|
OPT_I = 1 << 7,
|
||||||
OPT_IPV4 = 1 << 8,
|
/*OPT_n = 1 << 8, - ignored */
|
||||||
OPT_IPV6 = (1 << 9) * ENABLE_PING6,
|
OPT_IPV4 = 1 << 9,
|
||||||
|
OPT_IPV6 = (1 << 10) * ENABLE_PING6,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -349,9 +352,6 @@ struct globals {
|
|||||||
#define source_lsa (G.source_lsa )
|
#define source_lsa (G.source_lsa )
|
||||||
#define str_I (G.str_I )
|
#define str_I (G.str_I )
|
||||||
#define datalen (G.datalen )
|
#define datalen (G.datalen )
|
||||||
#define ntransmitted (G.ntransmitted)
|
|
||||||
#define nreceived (G.nreceived )
|
|
||||||
#define nrepeats (G.nrepeats )
|
|
||||||
#define pingcount (G.pingcount )
|
#define pingcount (G.pingcount )
|
||||||
#define opt_ttl (G.opt_ttl )
|
#define opt_ttl (G.opt_ttl )
|
||||||
#define myid (G.myid )
|
#define myid (G.myid )
|
||||||
@ -387,33 +387,40 @@ void BUG_ping_globals_too_big(void);
|
|||||||
static void print_stats_and_exit(int junk) NORETURN;
|
static void print_stats_and_exit(int junk) NORETURN;
|
||||||
static void print_stats_and_exit(int junk UNUSED_PARAM)
|
static void print_stats_and_exit(int junk UNUSED_PARAM)
|
||||||
{
|
{
|
||||||
|
unsigned long ul;
|
||||||
|
unsigned long nrecv;
|
||||||
|
|
||||||
signal(SIGINT, SIG_IGN);
|
signal(SIGINT, SIG_IGN);
|
||||||
|
|
||||||
printf("\n--- %s ping statistics ---\n", hostname);
|
nrecv = G.nreceived;
|
||||||
printf("%lu packets transmitted, ", ntransmitted);
|
printf("\n--- %s ping statistics ---\n"
|
||||||
printf("%lu packets received, ", nreceived);
|
"%lu packets transmitted, "
|
||||||
if (nrepeats)
|
"%lu packets received, ",
|
||||||
printf("%lu duplicates, ", nrepeats);
|
hostname, G.ntransmitted, nrecv
|
||||||
if (ntransmitted)
|
);
|
||||||
ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
|
if (G.nrepeats)
|
||||||
printf("%lu%% packet loss\n", ntransmitted);
|
printf("%lu duplicates, ", G.nrepeats);
|
||||||
|
ul = G.ntransmitted;
|
||||||
|
if (ul != 0)
|
||||||
|
ul = (ul - nrecv) * 100 / ul;
|
||||||
|
printf("%lu%% packet loss\n", ul);
|
||||||
if (tmin != UINT_MAX) {
|
if (tmin != UINT_MAX) {
|
||||||
unsigned tavg = tsum / (nreceived + nrepeats);
|
unsigned tavg = tsum / (nrecv + G.nrepeats);
|
||||||
printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
|
printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
|
||||||
tmin / 1000, tmin % 1000,
|
tmin / 1000, tmin % 1000,
|
||||||
tavg / 1000, tavg % 1000,
|
tavg / 1000, tavg % 1000,
|
||||||
tmax / 1000, tmax % 1000);
|
tmax / 1000, tmax % 1000);
|
||||||
}
|
}
|
||||||
/* if condition is true, exit with 1 -- 'failure' */
|
/* if condition is true, exit with 1 -- 'failure' */
|
||||||
exit(nreceived == 0 || (deadline && nreceived < pingcount));
|
exit(nrecv == 0 || (deadline && nrecv < pingcount));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sendping_tail(void (*sp)(int), int size_pkt)
|
static void sendping_tail(void (*sp)(int), int size_pkt)
|
||||||
{
|
{
|
||||||
int sz;
|
int sz;
|
||||||
|
|
||||||
CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
|
CLR((uint16_t)G.ntransmitted % MAX_DUP_CHK);
|
||||||
ntransmitted++;
|
G.ntransmitted++;
|
||||||
|
|
||||||
size_pkt += datalen;
|
size_pkt += datalen;
|
||||||
|
|
||||||
@ -423,7 +430,7 @@ static void sendping_tail(void (*sp)(int), int size_pkt)
|
|||||||
if (sz != size_pkt)
|
if (sz != size_pkt)
|
||||||
bb_error_msg_and_die(bb_msg_write_error);
|
bb_error_msg_and_die(bb_msg_write_error);
|
||||||
|
|
||||||
if (pingcount == 0 || deadline || ntransmitted < pingcount) {
|
if (pingcount == 0 || deadline || G.ntransmitted < pingcount) {
|
||||||
/* Didn't send all pings yet - schedule next in 1s */
|
/* Didn't send all pings yet - schedule next in 1s */
|
||||||
signal(SIGALRM, sp);
|
signal(SIGALRM, sp);
|
||||||
if (deadline) {
|
if (deadline) {
|
||||||
@ -439,7 +446,7 @@ static void sendping_tail(void (*sp)(int), int size_pkt)
|
|||||||
* otherwise ping waits for two RTTs. */
|
* otherwise ping waits for two RTTs. */
|
||||||
unsigned expire = timeout;
|
unsigned expire = timeout;
|
||||||
|
|
||||||
if (nreceived) {
|
if (G.nreceived) {
|
||||||
/* approx. 2*tmax, in seconds (2 RTT) */
|
/* approx. 2*tmax, in seconds (2 RTT) */
|
||||||
expire = tmax / (512*1024);
|
expire = tmax / (512*1024);
|
||||||
if (expire == 0)
|
if (expire == 0)
|
||||||
@ -458,7 +465,7 @@ static void sendping4(int junk UNUSED_PARAM)
|
|||||||
pkt->icmp_type = ICMP_ECHO;
|
pkt->icmp_type = ICMP_ECHO;
|
||||||
/*pkt->icmp_code = 0;*/
|
/*pkt->icmp_code = 0;*/
|
||||||
pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
|
pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
|
||||||
pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
|
pkt->icmp_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
|
||||||
pkt->icmp_id = myid;
|
pkt->icmp_id = myid;
|
||||||
|
|
||||||
/* If datalen < 4, we store timestamp _past_ the packet,
|
/* If datalen < 4, we store timestamp _past_ the packet,
|
||||||
@ -481,7 +488,7 @@ static void sendping6(int junk UNUSED_PARAM)
|
|||||||
pkt->icmp6_type = ICMP6_ECHO_REQUEST;
|
pkt->icmp6_type = ICMP6_ECHO_REQUEST;
|
||||||
/*pkt->icmp6_code = 0;*/
|
/*pkt->icmp6_code = 0;*/
|
||||||
/*pkt->icmp6_cksum = 0;*/
|
/*pkt->icmp6_cksum = 0;*/
|
||||||
pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
|
pkt->icmp6_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
|
||||||
pkt->icmp6_id = myid;
|
pkt->icmp6_id = myid;
|
||||||
|
|
||||||
/*if (datalen >= 4)*/
|
/*if (datalen >= 4)*/
|
||||||
@ -548,7 +555,7 @@ static void unpack_tail(int sz, uint32_t *tp,
|
|||||||
const char *dupmsg = " (DUP!)";
|
const char *dupmsg = " (DUP!)";
|
||||||
unsigned triptime = triptime; /* for gcc */
|
unsigned triptime = triptime; /* for gcc */
|
||||||
|
|
||||||
++nreceived;
|
++G.nreceived;
|
||||||
|
|
||||||
if (tp) {
|
if (tp) {
|
||||||
/* (int32_t) cast is for hypothetical 64-bit unsigned */
|
/* (int32_t) cast is for hypothetical 64-bit unsigned */
|
||||||
@ -562,8 +569,8 @@ static void unpack_tail(int sz, uint32_t *tp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (TST(recv_seq % MAX_DUP_CHK)) {
|
if (TST(recv_seq % MAX_DUP_CHK)) {
|
||||||
++nrepeats;
|
++G.nrepeats;
|
||||||
--nreceived;
|
--G.nreceived;
|
||||||
} else {
|
} else {
|
||||||
SET(recv_seq % MAX_DUP_CHK);
|
SET(recv_seq % MAX_DUP_CHK);
|
||||||
dupmsg += 7;
|
dupmsg += 7;
|
||||||
@ -692,7 +699,7 @@ static void ping4(len_and_sockaddr *lsa)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
unpack4(G.rcv_packet, c, &from);
|
unpack4(G.rcv_packet, c, &from);
|
||||||
if (pingcount && nreceived >= pingcount)
|
if (pingcount && G.nreceived >= pingcount)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -784,7 +791,7 @@ static void ping6(len_and_sockaddr *lsa)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
unpack6(G.rcv_packet, c, &from, hoplimit);
|
unpack6(G.rcv_packet, c, &from, hoplimit);
|
||||||
if (pingcount && nreceived >= pingcount)
|
if (pingcount && G.nreceived >= pingcount)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user