Skip to content

Commit

Permalink
support --print-{tos,ttl} with -cN, -CN, and -l
Browse files Browse the repository at this point in the history
This addresses GitHub issue #367.
  • Loading branch information
auerswal authored and schweikert committed Dec 14, 2024
1 parent 0037ba9 commit dd04b77
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 39 deletions.
32 changes: 31 additions & 1 deletion ci/test-05-options-c-e.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w

use Test::Command tests => 78;
use Test::Command tests => 84;
use Test::More;

# -c n count of pings to send to each target (default 1)
Expand All @@ -25,6 +25,21 @@
});
}

# fping -c n --print-tos --print-ttl
{
my $cmd = Test::Command->new(cmd => "fping -4 -c 2 -p 100 --print-tos --print-ttl localhost 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{localhost : \[0\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
127\.0\.0\.1 : \[0\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
localhost : \[1\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
127\.0\.0\.1 : \[1\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
});

$cmd->stderr_like(qr{localhost : xmt/rcv/%loss = 2/2/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
127\.0\.0\.1 : xmt/rcv/%loss = 2/2/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
});
}

# fping -c n -q
{
my $cmd = Test::Command->new(cmd => "fping -q -c 2 -p 100 localhost 127.0.0.1");
Expand Down Expand Up @@ -107,6 +122,21 @@
});
}

# fping -C n --print-tos --print-ttl
{
my $cmd = Test::Command->new(cmd => "fping -4 -C 2 -p 100 --print-tos --print-ttl localhost 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{localhost : \[0\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
127\.0\.0\.1 : \[0\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
localhost : \[1\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
127\.0\.0\.1 : \[1\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
});

$cmd->stderr_like(qr{localhost : \d\.\d+ \d\.\d+
127\.0\.0\.1 : \d\.\d+ \d\.\d+
});
}

# fping -C n -q
{
my $cmd = Test::Command->new(cmd => "fping -C 5 -q -p 100 localhost");
Expand Down
10 changes: 9 additions & 1 deletion ci/test-07-options-i-m.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w

use Test::Command tests => 15;
use Test::Command tests => 16;
use Test::More;

# -i n interval between sending ping packets (in millisec) (default 25)
Expand All @@ -25,6 +25,14 @@
});
}

# fping -l --print-tos --print-ttl
{
my $cmd = Test::Command->new(cmd => '(sleep 2; pkill fping)& fping -p 900 --print-ttl --print-tos -l 127.0.0.1');
$cmd->stdout_like(qr{127\.0\.0\.1 : \[0\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
127\.0\.0\.1 : \[1\], 64 bytes, \d\.\d+ ms \(\d\.\d+ avg, 0% loss\) \(TOS \d+\) \(TTL \d+\)
});
}

# fping -k
SKIP: {
if($^O ne 'linux') {
Expand Down
66 changes: 29 additions & 37 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -2574,41 +2574,6 @@ int wait_for_reply(int64_t wait_time)

if (verbose_flag)
printf(" is alive");

if(print_tos_flag) {
if(ip_header_tos != -1) {
printf(" (TOS %d)", ip_header_tos);
}
else {
printf(" (TOS unknown)");
}
}

if (print_ttl_flag) {
if(ip_header_ttl != -1) {
printf(" (TTL %d)", ip_header_ttl);
}
else {
printf(" (TTL unknown)");
}
}

if (icmp_request_typ == 13) {
printf(" (Timestamp Originate=%u Receive=%u Transmit=%u Localreceive=%u)",
ip_header_otime_ms, ip_header_rtime_ms, ip_header_ttime_ms,
ms_since_midnight_utc(recv_time));
}

if (elapsed_flag)
printf(" (%s ms)", sprint_tm(this_reply));

if (addr_cmp((struct sockaddr *)&response_addr, (struct sockaddr *)&h->saddr)) {
char buf[INET6_ADDRSTRLEN];
getnameinfo((struct sockaddr *)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
fprintf(stderr, " [<- %s]", buf);
}

printf("\n");
}
}

Expand All @@ -2630,19 +2595,46 @@ int wait_for_reply(int64_t wait_time)
printf("%d%% return)",
(h->num_recv_total * 100) / h->num_sent);
}
}

if (verbose_flag || alive_flag || per_recv_flag) {

if (addr_cmp((struct sockaddr *)&response_addr, (struct sockaddr *)&h->saddr)) {
char buf[INET6_ADDRSTRLEN];
getnameinfo((struct sockaddr *)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
fprintf(stderr, " [<- %s]", buf);
}

if(print_tos_flag) {
if(ip_header_tos != -1) {
printf(" (TOS %d)", ip_header_tos);
}
else {
printf(" (TOS unknown)");
}
}

if (print_ttl_flag) {
if(ip_header_ttl != -1) {
printf(" (TTL %d)", ip_header_ttl);
}
else {
printf(" (TTL unknown)");
}
}

if (icmp_request_typ == 13) {
printf(", ICMP timestamp: Originate=%u Receive=%u Transmit=%u Localreceive=%u",
printf("%simestamp%s Originate=%u Receive=%u Transmit=%u Localreceive=%u%s",
per_recv_flag ? ", ICMP t" : " (T",
per_recv_flag ? ":" : "",
ip_header_otime_ms, ip_header_rtime_ms, ip_header_ttime_ms,
ms_since_midnight_utc(recv_time));
ms_since_midnight_utc(recv_time),
per_recv_flag ? "" : ")");
}

if (elapsed_flag && !per_recv_flag)
printf(" (%s ms)", sprint_tm(this_reply));

printf("\n");
}

Expand Down

0 comments on commit dd04b77

Please sign in to comment.