From: German Service Network Date: Sat, 27 Jul 2024 17:36:16 +0000 (+0200) Subject: Print returned TOS value X-Git-Url: https://git.gsnw.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04950d2841aceec24d19272d5061c0a170deccdc;p=fping.git Print returned TOS value --- diff --git a/ci/test-08-options-n-q.pl b/ci/test-08-options-n-q.pl index 32630d0..7e879ab 100755 --- a/ci/test-08-options-n-q.pl +++ b/ci/test-08-options-n-q.pl @@ -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 @@ -48,6 +48,15 @@ $cmd->stdout_is_eq("127.0.0.1 is alive\n"); $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"); diff --git a/doc/fping.pod b/doc/fping.pod index 6b591d9..678ac72 100644 --- a/doc/fping.pod +++ b/doc/fping.pod @@ -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 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 In looping or counting modes (B<-l>, B<-c>, or B<-C>), this parameter sets diff --git a/src/fping.c b/src/fping.c index 905a1f6..b74895f 100644 --- a/src/fping.c +++ b/src/fping.c @@ -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; @@ -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 @@ -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); } @@ -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. @@ -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 */ @@ -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; } @@ -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)); @@ -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); }