summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Habets <thomas@habets.se>2014-05-22 13:49:09 +0100
committerThomas Habets <thomas@habets.se>2014-05-22 13:49:09 +0100
commit850c5a4b81969956efef6ed30a0df47bd57afe7c (patch)
tree8a423eda1186bf173c953a64c997d0bc0d77f041
parent535e5d362704f39e3a9de19e9dc85bd04eb43d23 (diff)
downloadarping-850c5a4b81969956efef6ed30a0df47bd57afe7c.tar.gz
Add -C <n> to immediately exit after receiving <n> replies.
Thanks to tolysz <tolysz@gmail.com>
-rw-r--r--doc/arping.82
-rw-r--r--doc/arping.yodl1
-rwxr-xr-xextra/testing.ex24
-rw-r--r--src/arping.c23
4 files changed, 45 insertions, 5 deletions
diff --git a/doc/arping.8 b/doc/arping.8
index 4a17534..9036a3f 100644
--- a/doc/arping.8
+++ b/doc/arping.8
@@ -59,6 +59,8 @@ for a host\&.
Use instead of host if you want to address 255\&.255\&.255\&.255\&.
.IP "\-c \fIcount\fP"
Only send \fIcount\fP requests\&.
+.IP "\-C \fIcount\fP"
+Only wait for \fIcount\fP replies, regardless of \-c and \-w\&.
.IP "\-d"
Find duplicate replies\&. Exit with 1 if there are answers from
two different MAC addresses\&.
diff --git a/doc/arping.yodl b/doc/arping.yodl
index 6b111f7..630e93c 100644
--- a/doc/arping.yodl
+++ b/doc/arping.yodl
@@ -51,6 +51,7 @@ Note that this may get the arping unanswered since it's not normal behavior
for a host.
dit(-B) Use instead of host if you want to address 255.255.255.255.
dit(-c em(count)) Only send em(count) requests.
+ dit(-C em(count)) Only wait for em(count) replies, regardless of -c and -w.
dit(-d) Find duplicate replies. Exit with 1 if there are answers from
two different MAC addresses.
dit(-D) Display answers as exclamation points and missing packets as dots.
diff --git a/extra/testing.ex b/extra/testing.ex
index aa51b11..38c4a49 100755
--- a/extra/testing.ex
+++ b/extra/testing.ex
@@ -91,6 +91,18 @@ rtt min/avg/max/std-dev = \[0-9.\]+/\[0-9.\]+/\[0-9.\]+/0.000 ms\r
"
expect eof
+send_user -- "--------------- Ping IP max 2 (-C) ------------------\n"
+spawn $bin -C 2 -c 10 $ip
+expect -re "ARPING $ip\r
+60 bytes from $mac \\($ip\\): index=0 time=(.*)sec\r
+60 bytes from $mac \\($ip\\): index=1 time=(.*)sec\r
+\r
+--- $ip statistics ---\r
+2 packets transmitted, 2 packets received, 0% unanswered \\(0 extra\\)\r
+rtt min/avg/max/std-dev = \[0-9.\]+/\[0-9.\]+/\[0-9.\]+/\[0-9.\]+ ms\r
+"
+expect eof
+
send_user -- "--------------- Ping IP x 3 (-c) ------------------\n"
spawn $bin -c 3 $ip
expect -re "ARPING $ip\r
@@ -194,6 +206,18 @@ rtt min/avg/max/std-dev = \[0-9.\]+/\[0-9.\]+/\[0-9.\]+/0.000 ms\r
"
expect eof
+send_user -- "--------------- Ping MAC max 2 (-C) ------------------\n"
+spawn $bin -A -C 2 -c 10 $mac -T $ip
+expect -re "ARPING $mac\r
+60 bytes from $ip \\($mac\\): icmp_seq=0 time=(.*)sec\r
+60 bytes from $ip \\($mac\\): icmp_seq=1 time=(.*)sec\r
+\r
+--- $mac statistics ---\r
+2 packets transmitted, 2 packets received, 0% unanswered \\(0 extra\\)\r
+rtt min/avg/max/std-dev = \[0-9.\]+/\[0-9.\]+/\[0-9.\]+/\[0-9.\]+ ms\r
+"
+expect eof
+
send_user -- "--------------- Ping MAC cisco style (-D) ------------------\n"
spawn $bin -A -c 3 -D $mac -T $ip
expect "!!!\t 0% packet loss (0 extra)\r\n"
diff --git a/src/arping.c b/src/arping.c
index 407ce43..a42779c 100644
--- a/src/arping.c
+++ b/src/arping.c
@@ -15,7 +15,7 @@
*
*/
/*
- * Copyright (C) 2000-2011 Thomas Habets <thomas@habets.se>
+ * Copyright (C) 2000-2014 Thomas Habets <thomas@habets.se>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
@@ -154,9 +154,10 @@ static int finddup = 0; /* finddup mode. -d */
static int dupfound = 0; /* set to 1 if dup found */
static char lastreplymac[ETH_ALEN]; /* if last different from this then dup */
-static unsigned int numsent = 0; /* packets sent */
-static unsigned int numrecvd = 0; /* packets received */
-static unsigned int numdots = 0; /* dots that should be printed */
+static unsigned int numsent = 0; /* packets sent */
+static unsigned int numrecvd = 0; /* packets received */
+static unsigned int max_replies = UINT_MAX; /* exit after -C replies */
+static unsigned int numdots = 0; /* dots that should be printed */
static double stats_min_time = -1;
static double stats_max_time = -1;
@@ -336,6 +337,8 @@ extended_usage()
" -B Use instead of host if you want to address 255.255.255.255.\n"
" -c count\n"
" Only send count requests.\n"
+ " -C count\n"
+ " Only wait for this many replies, regardless of -c and -w.\n"
" -d Find duplicate replies. Exit with 1 if there are "
"answers from\n"
" two different MAC addresses.\n"
@@ -784,6 +787,9 @@ pingip_recv(const char *unused, struct pcap_pkthdr *h, uint8_t *packet)
memcpy(lastreplymac, heth->_802_3_shost, ETH_ALEN);
numrecvd++;
+ if (numrecvd >= max_replies) {
+ sigint(0);
+ }
}
/** handle incoming packet when pinging an MAC address.
@@ -880,6 +886,9 @@ pingmac_recv(const char *unused, struct pcap_pkthdr *h, uint8_t *packet)
printf("\n");
}
numrecvd++;
+ if (numrecvd >= max_replies) {
+ sigint(0);
+ }
}
/**
@@ -1048,7 +1057,8 @@ int main(int argc, char **argv)
dstip = 0xffffffff;
memcpy(dstmac, ethxmas, ETH_ALEN);
- while (EOF!=(c=getopt(argc,argv,"0aAbBc:dDeFhi:I:pqrRs:S:t:T:uUvw:"))) {
+ while (EOF != (c = getopt(argc, argv,
+ "0aAbBC:c:dDeFhi:I:pqrRs:S:t:T:uUvw:"))) {
switch(c) {
case '0':
srcip = 0;
@@ -1071,6 +1081,9 @@ int main(int argc, char **argv)
case 'c':
maxcount = atoi(optarg);
break;
+ case 'C':
+ max_replies = atoi(optarg);
+ break;
case 'd':
finddup = 1;
break;