Skip to content

Commit

Permalink
Print returned TTL value
Browse files Browse the repository at this point in the history
Co-authored-by: Luís Fonseca <[email protected]>
  • Loading branch information
nalves599 and luishfonseca committed Aug 24, 2024
1 parent 54f452c commit ddf417e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
29 changes: 28 additions & 1 deletion ci/test-04-options-a-b.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w

use Test::Command tests => 32;
use Test::Command tests => 41;
use Test::More;
use Time::HiRes qw(gettimeofday tv_interval);

Expand Down Expand Up @@ -68,6 +68,33 @@
$cmd->stderr_is_eq("");
}

# fping -a --print-ttl
{
my $cmd = Test::Command->new(cmd => "fping -a --print-ttl 127.0.0.1 127.0.0.2");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{127\.0\.0\.1 \(TTL \d+\)\n127\.0\.0\.2 \(TTL \d+\)\n});
$cmd->stderr_is_eq("");
}

# fping --print-ttl
{
my $cmd = Test::Command->new(cmd => "fping --print-ttl 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{127\.0\.0\.1 is alive \(TTL \d+\)});
$cmd->stderr_is_eq("");
}

# fping --print-ttl with IPv6
SKIP: {
if($ENV{SKIP_IPV6}) {
skip 'Skip IPv6 tests', 3;
}
my $cmd = Test::Command->new(cmd => "fping --print-ttl ::1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{::1 is alive \(TTL unknown\)\n});
$cmd->stderr_is_eq("");
}

# fping -A
{
my $cmd = Test::Command->new(cmd => "fping -4 -A localhost");
Expand Down
8 changes: 7 additions & 1 deletion ci/test-11-unpriv.pl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sub get_ping_gid_range {
}

sub test_unprivileged_works {
plan tests => 12;
plan tests => 15;

{
my $cmd = Test::Command->new(cmd => "$fping_copy 127.0.0.1");
Expand All @@ -54,6 +54,12 @@ sub test_unprivileged_works {
$cmd->stdout_is_eq("127.0.0.1 is alive (TOS unknown)\n");
$cmd->stderr_is_eq("");
}
{
my $cmd = Test::Command->new(cmd => "$fping_copy --print-ttl 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_is_eq("127.0.0.1 is alive (TTL unknown)\n");
$cmd->stderr_is_eq("");
}
SKIP: {
if($^O ne 'linux') {
skip '-k option is only supported on Linux', 3;
Expand Down
6 changes: 6 additions & 0 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ Print usage message.

Set the IP TTL field (time to live hops).

=item B<--print-ttl>

Displays the IPv4 TTL value from the IP Header in the output.
If B<fping> cannot read the TTL value, "(TTL unknown)" is returned.
IPv4 only, requires root privileges or cap_net_raw.

=item B<-i>, B<--interval>=I<MSEC>

The minimum amount of time (in milliseconds) between sending a ping packet
Expand Down
22 changes: 20 additions & 2 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ int random_data_flag = 0;
int cumulative_stats_flag = 0;
int check_source_flag = 0;
int print_tos_flag = 0;
int print_ttl_flag = 0;
#if defined(DEBUG) || defined(_DEBUG)
int randomly_lose_flag, trace_flag, print_per_system_flag;
int lose_factor;
Expand Down Expand Up @@ -560,6 +561,7 @@ int main(int argc, char **argv)
{ "fast-reachable", 'X', OPTPARSE_REQUIRED },
{ "check-source", '0', OPTPARSE_NONE },
{ "print-tos", '0', OPTPARSE_NONE },
{ "print-ttl", '0', OPTPARSE_NONE },
#if defined(DEBUG) || defined(_DEBUG)
{ NULL, 'z', OPTPARSE_REQUIRED },
#endif
Expand All @@ -584,6 +586,8 @@ int main(int argc, char **argv)
check_source_flag = 1;
} else if (strstr(optparse_state.optlongname, "print-tos") != NULL) {
print_tos_flag = 1;
} else if (strstr(optparse_state.optlongname, "print-ttl") != NULL) {
print_ttl_flag = 1;
} else {
usage(1);
}
Expand Down Expand Up @@ -2164,14 +2168,16 @@ int decode_icmp_ipv4(
size_t reply_buf_len,
unsigned short *id,
unsigned short *seq,
int *ip_header_tos)
int *ip_header_tos,
int *ip_header_ttl)
{
struct icmp *icp;
int hlen = 0;

if (!using_sock_dgram4) {
struct ip *ip = (struct ip *)reply_buf;
*ip_header_tos = ip->ip_tos;
*ip_header_ttl = ip->ip_ttl;

#if defined(__alpha__) && __STDC__ && !defined(__GLIBC__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
/* The alpha headers are decidedly broken.
Expand Down Expand Up @@ -2373,6 +2379,7 @@ int wait_for_reply(int64_t wait_time)
unsigned short id;
unsigned short seq;
int ip_header_tos = -1;
int ip_header_ttl = -1;

/* Receive packet */
result = receive_packet(wait_time, /* max. wait time, in ns */
Expand Down Expand Up @@ -2400,7 +2407,8 @@ int wait_for_reply(int64_t wait_time)
sizeof(buffer),
&id,
&seq,
&ip_header_tos);
&ip_header_tos,
&ip_header_ttl);
if (ip_hlen < 0) {
return 1;
}
Expand Down Expand Up @@ -2518,6 +2526,15 @@ int wait_for_reply(int64_t wait_time)
}
}

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

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

Expand Down Expand Up @@ -3088,5 +3105,6 @@ void usage(int is_error)
fprintf(out, " -x, --reachable=N shows if >=N hosts are reachable or not\n");
fprintf(out, " -X, --fast-reachable=N exits true immediately when N hosts are found\n");
fprintf(out, " --print-tos show tos value\n");
fprintf(out, " --print-ttl show IP TTL value\n");
exit(is_error);
}

0 comments on commit ddf417e

Please sign in to comment.