Skip to content

Commit

Permalink
Print returned TOS value
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnw-sebast authored and auerswal committed Jul 28, 2024
1 parent 8ecda7f commit 04950d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
11 changes: 10 additions & 1 deletion ci/test-08-options-n-q.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w

use Test::Command tests => 36;
use Test::Command tests => 39;

# -n show targets by name (-d is equivalent)
# -O n set the type of service (tos) flag on the ICMP packets
Expand Down Expand Up @@ -48,6 +48,15 @@
$cmd->stderr_is_eq("");
}

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

# fping -q
{
my $cmd = Test::Command->new(cmd => "fping -q -p 100 -c 3 127.0.0.1");
Expand Down
5 changes: 5 additions & 0 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ Calculate "outage time" based on the number of lost pings and the interval used
Set the typ of service flag (TOS). I<N> can be either decimal or hexadecimal
(0xh) format.

=item B<--print-tos>

Displays the TOS value in the output, if TOS is not present, "(TOS unknown)" is returned.
IPv4 only, requires root privileges or cap_net_raw.

=item B<-p>, B<--period>=I<MSEC>

In looping or counting modes (B<-l>, B<-c>, or B<-C>), this parameter sets
Expand Down
42 changes: 30 additions & 12 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ int timestamp_format_flag = 0;
int random_data_flag = 0;
int cumulative_stats_flag = 0;
int check_source_flag = 0;
int print_tos_flag = 0;
#if defined(DEBUG) || defined(_DEBUG)
int randomly_lose_flag, trace_flag, print_per_system_flag;
int lose_factor;
Expand Down Expand Up @@ -558,6 +559,7 @@ int main(int argc, char **argv)
{ "reachable", 'x', OPTPARSE_REQUIRED },
{ "fast-reachable", 'X', OPTPARSE_REQUIRED },
{ "check-source", '0', OPTPARSE_NONE },
{ "print-tos", '0', OPTPARSE_NONE },
#if defined(DEBUG) || defined(_DEBUG)
{ NULL, 'z', OPTPARSE_REQUIRED },
#endif
Expand All @@ -569,17 +571,19 @@ int main(int argc, char **argv)
switch (c) {
case '0':
if(strstr(optparse_state.optlongname, "timestamp-format") != NULL) {
if(strcmp(optparse_state.optarg, "ctime") == 0) {
timestamp_format_flag = 1;
}else if(strcmp(optparse_state.optarg, "iso") == 0) {
timestamp_format_flag = 2;
}else if(strcmp(optparse_state.optarg, "rfc3339") == 0) {
timestamp_format_flag = 3;
}else{
usage(1);
}
if(strcmp(optparse_state.optarg, "ctime") == 0) {
timestamp_format_flag = 1;
}else if(strcmp(optparse_state.optarg, "iso") == 0) {
timestamp_format_flag = 2;
}else if(strcmp(optparse_state.optarg, "rfc3339") == 0) {
timestamp_format_flag = 3;
}else{
usage(1);
}
} else if (strstr(optparse_state.optlongname, "check-source") != NULL) {
check_source_flag = 1;
} else if (strstr(optparse_state.optlongname, "print-tos") != NULL) {
print_tos_flag = 1;
} else {
usage(1);
}
Expand Down Expand Up @@ -2159,13 +2163,15 @@ int decode_icmp_ipv4(
char *reply_buf,
size_t reply_buf_len,
unsigned short *id,
unsigned short *seq)
unsigned short *seq,
int *ip_header_tos)
{
struct icmp *icp;
int hlen = 0;

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

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

/* Receive packet */
result = receive_packet(wait_time, /* max. wait time, in ns */
Expand All @@ -2392,7 +2399,8 @@ int wait_for_reply(int64_t wait_time)
buffer,
sizeof(buffer),
&id,
&seq);
&seq,
&ip_header_tos);
if (ip_hlen < 0) {
return 1;
}
Expand Down Expand Up @@ -2498,8 +2506,17 @@ int wait_for_reply(int64_t wait_time)
if (verbose_flag || alive_flag) {
printf("%s", h->host);

if (verbose_flag)
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 (elapsed_flag)
printf(" (%s ms)", sprint_tm(this_reply));
Expand Down Expand Up @@ -3070,5 +3087,6 @@ void usage(int is_error)
fprintf(out, " -v, --version show version\n");
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");
exit(is_error);
}

0 comments on commit 04950d2

Please sign in to comment.