diff --git a/ChangeLog b/ChangeLog index b883189..aa657ac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,18 +1,28 @@ Unreleased * INCOMPATIBILITY WARNING: - fping and fping6 are now unified into one binary. This means that for - example doing 'fping www.google.com' is going to ping the IPv6 IP of - www.google.com on IPv6-enabled hosts. If you need exact compatibility with - old versions, you can: - - compile fping with --disable-ipv6 (or use a wrapper, and call 'fping -4') - - compile fping with --enable-ipv6 and rename it to fping6 (same as 'fping -6') + fping and fping6 are now unified into one binary. This means that, for + example, doing 'fping www.google.com' is going to ping the IPv6 IP of + www.google.com on IPv6-enabled hosts. + If you need exact compatibility with old versions, you can configure, + compile, and install fping twice: once for ipv4 and once for ipv6: + - ./configure --disable-ipv6; make clean install + - ./configure --disable-ipv4 --program-suffix=6; make clean install + + Or, alternatively, you could write two wrappers 'fping' and 'fping6', + that set respectively the options '-4' and '-6' when calling the original + fping binary. + + * Version 4.0 * (feature) Unified 'fping' and 'fping6' into one binary (#80) + * (feature) --enable-ipv6 is now default * (feature) New option '-4' to force IPv4 * (feature) New option '-6' to force IPv6 + +2017-02-09 David Schweikert + * Version 3.16 * (feature) Support kernel-timestamping of received packets (#46) * (feature) Simplify restrictions: only -i >= 1 and -p >= 10 are enforced now - * (feature) --enable-ipv6 is now default (you can use --disable-ipv6 to disable IPv6 support) * (bugfix) Fix option -m to return all IPs of a hostname * (bugfix) Fix option -H (ttl) for IPv6 * (bugfix) Fix option -M (don't fragment) for IPv6 diff --git a/TODO b/TODO deleted file mode 100644 index c7e06b0..0000000 --- a/TODO +++ /dev/null @@ -1,5 +0,0 @@ -- implement -4 -- implement -6 -- test -4 -- test -6 -- fix -m diff --git a/ci/deploy-bintray.sh b/ci/deploy-bintray.sh index dcb53d1..95219a3 100755 --- a/ci/deploy-bintray.sh +++ b/ci/deploy-bintray.sh @@ -10,9 +10,9 @@ set -e # exit 0 #fi -# do this only for the master branch -if [ "$TRAVIS_BRANCH" != "master" ]; then - echo "skipped upload branch $TRAVIS_BRANCH isn't master" +# do this only for the master and version3 branch +if [ "$TRAVIS_BRANCH" != "master" -a "$TRAVIS_BRANCH" != "version3" ]; then + echo "skipped upload branch $TRAVIS_BRANCH isn't master/version3" exit 0 fi diff --git a/ci/test-04-options-a-b.pl b/ci/test-04-options-a-b.pl index fa716b6..7958a3c 100755 --- a/ci/test-04-options-a-b.pl +++ b/ci/test-04-options-a-b.pl @@ -31,7 +31,7 @@ $cmd->stderr_is_eq(""); my $cmd = Test::Command->new(cmd => "fping -4 ::1"); $cmd->exit_is_num(2); $cmd->stdout_is_eq(""); -$cmd->stderr_is_eq("::1: Address family for hostname not supported\n"); +$cmd->stderr_like(qr{^::1:.*(not supported|not known)}); } # fping -6 @@ -49,7 +49,7 @@ SKIP: { my $cmd = Test::Command->new(cmd => "fping -6 127.0.0.1"); $cmd->exit_is_num(2); $cmd->stdout_is_eq(""); -$cmd->stderr_is_eq("127.0.0.1: Address family for hostname not supported\n"); +$cmd->stderr_like(qr{127\.0\.0\.1:.*(not supported|not known)}); } # fping -a diff --git a/ci/test-07-options-i-m.pl b/ci/test-07-options-i-m.pl index 9a22d13..06e2ece 100755 --- a/ci/test-07-options-i-m.pl +++ b/ci/test-07-options-i-m.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl -w use Test::Command tests => 7; +use Test::More; # -i n interval between sending ping packets (in millisec) (default 25) # -l loop sending pings forever @@ -24,11 +25,14 @@ $cmd->stdout_like(qr{127\.0\.0\.1 : \[0\], 84 bytes, 0\.\d+ ms \(0.\d+ avg, 0% l } # fping -M -{ -my $cmd = Test::Command->new(cmd => "fping -M 127.0.0.1"); -$cmd->exit_is_num(0); -$cmd->stdout_is_eq("127.0.0.1 is alive\n"); -$cmd->stderr_is_eq(""); +SKIP: { + if($^O eq 'darwin') { + skip '-M option not supported on macOS', 3; + } + my $cmd = Test::Command->new(cmd => "fping -M 127.0.0.1"); + $cmd->exit_is_num(0); + $cmd->stdout_is_eq("127.0.0.1 is alive\n"); + $cmd->stderr_is_eq(""); } # fping -m -> test-14-internet-hosts diff --git a/ci/test-14-ping-internet-hosts.pl b/ci/test-14-ping-internet-hosts.pl index 4f3c4b2..ed6cfcd 100755 --- a/ci/test-14-ping-internet-hosts.pl +++ b/ci/test-14-ping-internet-hosts.pl @@ -72,9 +72,12 @@ $cmd->stderr_is_eq(""); } # fping -M -{ -my $cmd = Test::Command->new(cmd => "fping -r 0 -b 10000 -M 8.8.8.8"); -$cmd->exit_is_num(1); -$cmd->stdout_is_eq("8.8.8.8 is unreachable\n"); -$cmd->stderr_is_eq("8.8.8.8: error while sending ping: Message too long\n"); +SKIP: { + if($^O eq 'darwin') { + skip '-M option not supported on macOS', 3; + } + my $cmd = Test::Command->new(cmd => "fping -r 0 -b 10000 -M 8.8.8.8"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq("8.8.8.8 is unreachable\n"); + $cmd->stderr_is_eq("8.8.8.8: error while sending ping: Message too long\n"); } diff --git a/configure.ac b/configure.ac index 2a178da..94a6db7 100644 --- a/configure.ac +++ b/configure.ac @@ -5,23 +5,36 @@ AC_PREREQ(2.59) AC_INIT([fping],[3.16-rc2]) -dnl make ipv4 and ipv6 options +dnl --disable-ipv4 AC_ARG_ENABLE([ipv4], - AS_HELP_STRING([--enable-ipv4], [(ignored for compatibility with previous versions)])) + AS_HELP_STRING([--disable-ipv4], [Disable support for pinging IPv4 hosts])) +AM_CONDITIONAL([IPV4], [test "x$enable_ipv4" != "xno"]) +AM_COND_IF([IPV4], [AC_DEFINE([IPV4], [1], [IPv4 enabled])]) +dnl --disable-ipv6 AC_ARG_ENABLE([ipv6], - AS_HELP_STRING([--enable-ipv6], [Build IPv6 capable fping6]), - [case "${enableval}" in - yes) ipv6=true ;; - no) ipv6=false ;; - *) AC_MSG_ERROR([bad value ${enableval} for --enable-ipv6]) ;; - esac],[ipv6=true]) -AM_CONDITIONAL([IPV6], [test x$ipv6 = xtrue]) + AS_HELP_STRING([--disable-ipv6], [Disable support for pinging IPv6 hosts])) +AS_IF([test "x$enable_ipv6" != "xno"], [ + dnl Test if IPv6 is supported + AC_CHECK_HEADERS([netinet/icmp6.h], [have_ipv6="yes"], [], [[ + #include + ]]) +]) +dnl Can't disable both IPv4 and IPv6 +AS_IF([test "x$enable_ipv4" = "xno" -a "x$enable_ipv6" = "xno"], [ + AC_MSG_ERROR([Need to enable IPv4 or IPv6. Can't disable both!)]) +]) +dnl IPv6 required, but not supported? +AS_IF([test \( "x$enable_ipv6" = "xyes" -o "x$enable_ipv4" = "xno" \) -a "x$have_ipv6" != "xyes" ], [ + AC_MSG_ERROR([IPv6 not supported on this platform (netinet/icmp6.h header not found)]) +]) +AM_CONDITIONAL([IPV6], [test "x$have_ipv6" = "xyes"]) +AM_COND_IF([IPV6], [AC_DEFINE([IPV6], [1], [IPv6 enabled])]) AC_ARG_ENABLE([timestamp], AS_HELP_STRING([--disable-timestamp], [Disable kernel-based packet timestaping (SO_TIMESTAMP)])) AS_IF([test "x$enable_timestamp" != "xno"], [ - AC_CHECK_DECL([SO_TIMESTAMP], [AC_DEFINE(HAVE_SO_TIMESTAMP, [1], [set define])], [have_so_timestamp="no"], [#include + AC_CHECK_DECL([SO_TIMESTAMP], [AC_DEFINE(HAVE_SO_TIMESTAMP, [1], [SO_TIMESTAMP is defined])], [have_so_timestamp="no"], [#include #include ]) ]) dnl Test if --enable-timestamp is explicitely enabled and make an error if this platform doesn't support it @@ -70,7 +83,7 @@ AH_BOTTOM([ ]) dnl Checks for header files. -AC_CHECK_HEADERS(unistd.h sys/file.h stdlib.h sys/select.h) +AC_CHECK_HEADERS([unistd.h sys/file.h stdlib.h sys/select.h]) AC_CONFIG_FILES([Makefile doc/Makefile diff --git a/doc/Makefile.am b/doc/Makefile.am index 33e4cff..450e158 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -1,15 +1,6 @@ -man_MANS = +man_MANS = fping.8 -man_MANS += fping.8 - -if IPV6 -man_MANS += fping6.8 -endif - -EXTRA_DIST = fping.8 fping6.8 fping.pod README.1992 +EXTRA_DIST = fping.8 fping.pod README.1992 fping.8: fping.pod pod2man -c "" -s 8 -r "fping" $< >$@ - -fping6.8: fping.pod - pod2man -c "" -s 8 -r "fping" -n fping6 $< >$@ diff --git a/src/.clang-format b/src/.clang-format new file mode 100644 index 0000000..11d0dcd --- /dev/null +++ b/src/.clang-format @@ -0,0 +1 @@ +BasedOnStyle: WebKit diff --git a/src/Makefile.am b/src/Makefile.am index da2299f..c518d34 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,4 @@ fping_DEPENDENCIES = ../config.h if IPV6 fping_SOURCES += socket6.c fping_CFLAGS = $(AM_CFLAGS) -DIPV6 -install-exec-hook: - ln -s fping$(EXEEXT) $(DESTDIR)$(sbindir)/fping6 endif diff --git a/src/fping.c b/src/fping.c index d2e1c35..f00a13e 100644 --- a/src/fping.c +++ b/src/fping.c @@ -31,8 +31,7 @@ */ #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif /* __cplusplus */ #include "fping.h" @@ -40,12 +39,11 @@ extern "C" /*** autoconf includes ***/ - -#include #include -#include #include #include +#include +#include #include "config.h" #include "seqmap.h" @@ -58,12 +56,12 @@ extern "C" #include #endif /* HAVE_STDLIB_H */ -#include #include +#include -#include -#include #include +#include +#include #if HAVE_SYS_FILE_H #include @@ -78,8 +76,8 @@ extern "C" #include #include -#include #include +#include #include @@ -87,8 +85,8 @@ extern "C" /*** externals ***/ -extern char *optarg; -extern int optind,opterr; +extern char* optarg; +extern int optind, opterr; #ifndef h_errno extern int h_errno; #endif @@ -99,141 +97,138 @@ extern int h_errno; /*** Constants ***/ -#define EMAIL "david@schweikert.ch" +#define EMAIL "david@schweikert.ch" /*** Ping packet defines ***/ -#define MAX_IP_PACKET 65536 /* (theoretical) max IP packet size */ -#define SIZE_IP_HDR 40 -#define SIZE_ICMP_HDR 8 /* from ip_icmp.h */ -#define MAX_PING_DATA ( MAX_IP_PACKET - SIZE_IP_HDR - SIZE_ICMP_HDR ) +#define MAX_IP_PACKET 65536 /* (theoretical) max IP packet size */ +#define SIZE_IP_HDR 40 +#define SIZE_ICMP_HDR 8 /* from ip_icmp.h */ +#define MAX_PING_DATA (MAX_IP_PACKET - SIZE_IP_HDR - SIZE_ICMP_HDR) -#define MAX_GENERATE 100000 /* maximum number of hosts that -g can generate */ +#define MAX_GENERATE 100000 /* maximum number of hosts that -g can generate */ /* sized so as to be like traditional ping */ -#define DEFAULT_PING_DATA_SIZE 56 +#define DEFAULT_PING_DATA_SIZE 56 /* maxima and minima */ -#define MIN_INTERVAL 1 /* in millisec */ -#define MIN_PERHOST_INTERVAL 10 /* in millisec */ +#define MIN_INTERVAL 1 /* in millisec */ +#define MIN_PERHOST_INTERVAL 10 /* in millisec */ /* response time array flags */ -#define RESP_WAITING -1 -#define RESP_UNUSED -2 -#define RESP_ERROR -3 +#define RESP_WAITING -1 +#define RESP_UNUSED -2 +#define RESP_ERROR -3 /* debugging flags */ -#if defined( DEBUG ) || defined( _DEBUG ) -#define DBG_TRACE 1 -#define DBG_SENT_TIMES 2 -#define DBG_RANDOM_LOSE_FEW 4 -#define DBG_RANDOM_LOSE_MANY 8 -#define DBG_PRINT_PER_SYSTEM 16 -#define DBG_REPORT_ALL_RTTS 32 +#if defined(DEBUG) || defined(_DEBUG) +#define DBG_TRACE 1 +#define DBG_SENT_TIMES 2 +#define DBG_RANDOM_LOSE_FEW 4 +#define DBG_RANDOM_LOSE_MANY 8 +#define DBG_PRINT_PER_SYSTEM 16 +#define DBG_REPORT_ALL_RTTS 32 #endif /* DEBUG || _DEBUG */ /* Long names for ICMP packet types */ #define ICMP_TYPE_STR_MAX 18 -char *icmp_type_str[19] = -{ - "ICMP Echo Reply", /* 0 */ +char* icmp_type_str[19] = { + "ICMP Echo Reply", /* 0 */ "", "", - "ICMP Unreachable", /* 3 */ - "ICMP Source Quench", /* 4 */ - "ICMP Redirect", /* 5 */ + "ICMP Unreachable", /* 3 */ + "ICMP Source Quench", /* 4 */ + "ICMP Redirect", /* 5 */ "", "", - "ICMP Echo", /* 8 */ + "ICMP Echo", /* 8 */ "", "", - "ICMP Time Exceeded", /* 11 */ - "ICMP Parameter Problem", /* 12 */ - "ICMP Timestamp Request", /* 13 */ - "ICMP Timestamp Reply", /* 14 */ + "ICMP Time Exceeded", /* 11 */ + "ICMP Parameter Problem", /* 12 */ + "ICMP Timestamp Request", /* 13 */ + "ICMP Timestamp Reply", /* 14 */ "ICMP Information Request", /* 15 */ - "ICMP Information Reply", /* 16 */ - "ICMP Mask Request", /* 17 */ - "ICMP Mask Reply" /* 18 */ + "ICMP Information Reply", /* 16 */ + "ICMP Mask Request", /* 17 */ + "ICMP Mask Reply" /* 18 */ }; -char *icmp_unreach_str[16] = -{ - "ICMP Network Unreachable", /* 0 */ - "ICMP Host Unreachable", /* 1 */ - "ICMP Protocol Unreachable", /* 2 */ - "ICMP Port Unreachable", /* 3 */ - "ICMP Unreachable (Fragmentation Needed)", /* 4 */ - "ICMP Unreachable (Source Route Failed)", /* 5 */ - "ICMP Unreachable (Destination Network Unknown)", /* 6 */ - "ICMP Unreachable (Destination Host Unknown)", /* 7 */ - "ICMP Unreachable (Source Host Isolated)", /* 8 */ - "ICMP Unreachable (Communication with Network Prohibited)", /* 9 */ - "ICMP Unreachable (Communication with Host Prohibited)", /* 10 */ - "ICMP Unreachable (Network Unreachable For Type Of Service)", /* 11 */ - "ICMP Unreachable (Host Unreachable For Type Of Service)", /* 12 */ +char* icmp_unreach_str[16] = { + "ICMP Network Unreachable", /* 0 */ + "ICMP Host Unreachable", /* 1 */ + "ICMP Protocol Unreachable", /* 2 */ + "ICMP Port Unreachable", /* 3 */ + "ICMP Unreachable (Fragmentation Needed)", /* 4 */ + "ICMP Unreachable (Source Route Failed)", /* 5 */ + "ICMP Unreachable (Destination Network Unknown)", /* 6 */ + "ICMP Unreachable (Destination Host Unknown)", /* 7 */ + "ICMP Unreachable (Source Host Isolated)", /* 8 */ + "ICMP Unreachable (Communication with Network Prohibited)", /* 9 */ + "ICMP Unreachable (Communication with Host Prohibited)", /* 10 */ + "ICMP Unreachable (Network Unreachable For Type Of Service)", /* 11 */ + "ICMP Unreachable (Host Unreachable For Type Of Service)", /* 12 */ "ICMP Unreachable (Communication Administratively Prohibited)", /* 13 */ - "ICMP Unreachable (Host Precedence Violation)", /* 14 */ - "ICMP Unreachable (Precedence cutoff in effect)" /* 15 */ + "ICMP Unreachable (Host Precedence Violation)", /* 14 */ + "ICMP Unreachable (Precedence cutoff in effect)" /* 15 */ }; -#define ICMP_UNREACH_MAXTYPE 15 +#define ICMP_UNREACH_MAXTYPE 15 /* entry used to keep track of each host we are pinging */ -#define EV_TYPE_PING 1 -#define EV_TYPE_TIMEOUT 2 +#define EV_TYPE_PING 1 +#define EV_TYPE_TIMEOUT 2 -typedef struct host_entry -{ - /* Each host can have an event attached: either the next time that a ping needs +typedef struct host_entry { + /* Each host can have an event attached: either the next time that a ping needs * to be sent, or the timeout, if the last ping was sent */ - struct host_entry *ev_prev; /* double linked list for the event-queue */ - struct host_entry *ev_next; /* double linked list for the event-queue */ - struct timeval ev_time; /* time, after which this event should happen */ - int ev_type; /* event type */ - - int i; /* index into array */ - char *name; /* name as given by user */ - char *host; /* text description of host */ - char *pad; /* pad to align print names */ - struct sockaddr_storage saddr; /* internet address */ - socklen_t saddr_len; - int timeout; /* time to wait for response */ - unsigned char running; /* unset when through sending */ - unsigned char waiting; /* waiting for response */ - struct timeval last_send_time; /* time of last packet sent */ - int num_sent; /* number of ping packets sent */ - int num_recv; /* number of pings received (duplicates ignored) */ - int num_recv_total; /* number of pings received, including duplicates */ - int max_reply; /* longest response time */ - int min_reply; /* shortest response time */ - int total_time; /* sum of response times */ - /* _i -> splits (reset on every report interval) */ - int num_sent_i; /* number of ping packets sent */ - int num_recv_i; /* number of pings received */ - int max_reply_i; /* longest response time */ - int min_reply_i; /* shortest response time */ - int total_time_i; /* sum of response times */ - int discard_next_recv_i;/* don't count next received reply for split reporting */ - int *resp_times; /* individual response times */ -#if defined( DEBUG ) || defined( _DEBUG ) - int *sent_times; /* per-sent-ping timestamp */ + struct host_entry* ev_prev; /* double linked list for the event-queue */ + struct host_entry* ev_next; /* double linked list for the event-queue */ + struct timeval ev_time; /* time, after which this event should happen */ + int ev_type; /* event type */ + + int i; /* index into array */ + char* name; /* name as given by user */ + char* host; /* text description of host */ + char* pad; /* pad to align print names */ + struct sockaddr_storage saddr; /* internet address */ + socklen_t saddr_len; + int timeout; /* time to wait for response */ + unsigned char running; /* unset when through sending */ + unsigned char waiting; /* waiting for response */ + struct timeval last_send_time; /* time of last packet sent */ + int num_sent; /* number of ping packets sent */ + int num_recv; /* number of pings received (duplicates ignored) */ + int num_recv_total; /* number of pings received, including duplicates */ + int max_reply; /* longest response time */ + int min_reply; /* shortest response time */ + int total_time; /* sum of response times */ + /* _i -> splits (reset on every report interval) */ + int num_sent_i; /* number of ping packets sent */ + int num_recv_i; /* number of pings received */ + int max_reply_i; /* longest response time */ + int min_reply_i; /* shortest response time */ + int total_time_i; /* sum of response times */ + int discard_next_recv_i; /* don't count next received reply for split reporting */ + int* resp_times; /* individual response times */ +#if defined(DEBUG) || defined(_DEBUG) + int* sent_times; /* per-sent-ping timestamp */ #endif /* DEBUG || _DEBUG */ } HOST_ENTRY; /*** globals ***/ -HOST_ENTRY **table = NULL; /* array of pointers to items in the list */ +HOST_ENTRY** table = NULL; /* array of pointers to items in the list */ /* event queue (ev): This, together with the ev_next / ev_prev elements are used * to track the next event happening for each host. This can be either a new ping * that needs to be sent or a timeout */ -HOST_ENTRY *ev_first; -HOST_ENTRY *ev_last; +HOST_ENTRY* ev_first; +HOST_ENTRY* ev_last; -char *prog; -int ident; /* our pid */ +char* prog; +int ident; /* our pid */ int socket4 = 0; #ifndef IPV6 int hints_ai_family = AF_INET; @@ -268,25 +263,25 @@ long min_reply = 0; int total_replies = 0; double sum_replies = 0; int max_hostname_len = 0; -int num_jobs = 0; /* number of hosts still to do */ -int num_hosts; /* total number of hosts */ -int num_alive = 0, /* total number alive */ - num_unreachable = 0, /* total number unreachable */ - num_noaddress = 0; /* total number of addresses not found */ -int num_timeout = 0, /* number of times select timed out */ - num_pingsent = 0, /* total pings sent */ - num_pingreceived = 0, /* total pings received */ - num_othericmprcvd = 0; /* total non-echo-reply ICMP received */ - -struct timeval current_time; /* current time (pseudo) */ +int num_jobs = 0; /* number of hosts still to do */ +int num_hosts; /* total number of hosts */ +int num_alive = 0, /* total number alive */ + num_unreachable = 0, /* total number unreachable */ + num_noaddress = 0; /* total number of addresses not found */ +int num_timeout = 0, /* number of times select timed out */ + num_pingsent = 0, /* total pings sent */ + num_pingreceived = 0, /* total pings received */ + num_othericmprcvd = 0; /* total non-echo-reply ICMP received */ + +struct timeval current_time; /* current time (pseudo) */ struct timeval start_time; struct timeval end_time; -struct timeval last_send_time; /* time last ping was sent */ -struct timeval next_report_time; /* time next -Q report is expected */ +struct timeval last_send_time; /* time last ping was sent */ +struct timeval next_report_time; /* time next -Q report is expected */ struct timezone tz; /* switches */ -int generate_flag = 0; /* flag for IP list generation */ +int generate_flag = 0; /* flag for IP list generation */ int verbose_flag, quiet_flag, stats_flag, unreachable_flag, alive_flag; int elapsed_flag, version_flag, count_flag, loop_flag, netdata_flag; int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag; @@ -294,41 +289,41 @@ int multif_flag; int outage_flag = 0; int timestamp_flag = 0; int random_data_flag = 0; -#if defined( DEBUG ) || defined( _DEBUG ) +#if defined(DEBUG) || defined(_DEBUG) int randomly_lose_flag, sent_times_flag, trace_flag, print_per_system_flag; int lose_factor; #endif /* DEBUG || _DEBUG */ -char *filename = NULL; /* file containing hosts to ping */ +char* filename = NULL; /* file containing hosts to ping */ /*** forward declarations ***/ -void add_name( char *name ); -void add_addr( char *name, char *host, struct sockaddr *ipaddr, socklen_t ipaddr_len); -char *na_cat( char *name, struct in_addr ipaddr ); -void crash_and_burn( char *message ); -void errno_crash_and_burn( char *message ); -char *get_host_by_address( struct in_addr in ); -void remove_job( HOST_ENTRY *h ); -int send_ping( HOST_ENTRY *h ); -long timeval_diff( struct timeval *a, struct timeval *b ); -void timeval_add(struct timeval *a, long t_10u); -void usage( int ); -int wait_for_reply( long ); -void print_per_system_stats( void ); -void print_per_system_splits( void ); -void print_netdata( void ); -void print_global_stats( void ); +void add_name(char* name); +void add_addr(char* name, char* host, struct sockaddr* ipaddr, socklen_t ipaddr_len); +char* na_cat(char* name, struct in_addr ipaddr); +void crash_and_burn(char* message); +void errno_crash_and_burn(char* message); +char* get_host_by_address(struct in_addr in); +void remove_job(HOST_ENTRY* h); +int send_ping(HOST_ENTRY* h); +long timeval_diff(struct timeval* a, struct timeval* b); +void timeval_add(struct timeval* a, long t_10u); +void usage(int); +int wait_for_reply(long); +void print_per_system_stats(void); +void print_per_system_splits(void); +void print_netdata(void); +void print_global_stats(void); void main_loop(); void finish(); -char *sprint_tm( int t ); -void ev_enqueue(HOST_ENTRY *h); -HOST_ENTRY *ev_dequeue(); -void ev_remove(HOST_ENTRY *h); -void add_cidr(char *); -void add_range(char *, char *); -void print_warning(char *fmt, ...); -int addr_cmp(struct sockaddr *a, struct sockaddr *b); +char* sprint_tm(int t); +void ev_enqueue(HOST_ENTRY* h); +HOST_ENTRY* ev_dequeue(); +void ev_remove(HOST_ENTRY* h); +void add_cidr(char*); +void add_range(char*, char*); +void print_warning(char* fmt, ...); +int addr_cmp(struct sockaddr* a, struct sockaddr* b); /*** function definitions ***/ @@ -346,13 +341,13 @@ int addr_cmp(struct sockaddr *a, struct sockaddr *b); ************************************************************/ -int main( int argc, char **argv ) +int main(int argc, char** argv) { int c, i, n; - char *buf; + char* buf; uid_t uid; int tos = 0; - HOST_ENTRY *cursor; + HOST_ENTRY* cursor; prog = argv[0]; @@ -361,9 +356,9 @@ int main( int argc, char **argv ) socket6 = open_ping_socket_ipv6(ping_data_size); #endif - if((uid = getuid())) { + if ((uid = getuid())) { /* drop privileges */ - if(setuid( getuid() ) == -1) + if (setuid(getuid()) == -1) perror("cannot setuid"); } @@ -374,12 +369,10 @@ int main( int argc, char **argv ) /* get command line options */ - while( ( c = getopt( argc, argv, "46ADMNRadeghlmnoqsuvzB:C:H:I:O:Q:S:T:b:c:f:i:p:r:t:" ) ) != EOF ) - { - switch( c ) - { + while ((c = getopt(argc, argv, "46ADMNRadeghlmnoqsuvzB:C:H:I:O:Q:S:T:b:c:f:i:p:r:t:")) != EOF) { + switch (c) { case '4': - if(hints_ai_family != AF_UNSPEC) { + if (hints_ai_family != AF_UNSPEC) { fprintf(stderr, "%s: can't specify both -4 and -6\n", prog); exit(1); } @@ -387,7 +380,7 @@ int main( int argc, char **argv ) break; case '6': #ifdef IPV6 - if(hints_ai_family != AF_UNSPEC) { + if (hints_ai_family != AF_UNSPEC) { fprintf(stderr, "%s: can't specify both -4 and -6\n", prog); exit(1); } @@ -399,14 +392,14 @@ int main( int argc, char **argv ) break; case 'M': #ifdef IP_MTU_DISCOVER - if(socket4) { + if (socket4) { int val = IP_PMTUDISC_DO; if (setsockopt(socket4, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val))) { perror("setsockopt IP_MTU_DISCOVER"); } } #ifdef IPV6 - if(socket6) { + if (socket6) { int val = IPV6_PMTUDISC_DO; if (setsockopt(socket6, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, sizeof(val))) { perror("setsockopt IPV6_MTU_DISCOVER"); @@ -420,37 +413,37 @@ int main( int argc, char **argv ) break; case 't': - if( !( timeout = ( unsigned int )atoi( optarg ) * 100 ) ) + if (!(timeout = (unsigned int)atoi(optarg) * 100)) usage(1); break; case 'r': - if (!sscanf(optarg,"%u",&retry)) + if (!sscanf(optarg, "%u", &retry)) usage(1); break; case 'i': - if (!sscanf(optarg,"%u",&interval)) + if (!sscanf(optarg, "%u", &interval)) usage(1); interval *= 100; break; case 'p': - if( !( perhost_interval = ( unsigned int )atoi( optarg ) * 100 ) ) + if (!(perhost_interval = (unsigned int)atoi(optarg) * 100)) usage(1); break; case 'c': - if( !( count = ( unsigned int )atoi( optarg ) ) ) + if (!(count = (unsigned int)atoi(optarg))) usage(1); count_flag = 1; break; case 'C': - if( !( count = ( unsigned int )atoi( optarg ) ) ) + if (!(count = (unsigned int)atoi(optarg))) usage(1); count_flag = 1; @@ -458,7 +451,7 @@ int main( int argc, char **argv ) break; case 'b': - if (!sscanf(optarg,"%u",&ping_data_size)) + if (!sscanf(optarg, "%u", &ping_data_size)) usage(1); break; @@ -475,7 +468,7 @@ int main( int argc, char **argv ) case 'Q': verbose_flag = 0; quiet_flag = 1; - if( !( report_interval = ( unsigned int )atoi( optarg ) * 100000 ) ) + if (!(report_interval = (unsigned int)atoi(optarg) * 100000)) usage(1); break; @@ -502,7 +495,7 @@ int main( int argc, char **argv ) break; case 'B': - if( !( backoff = atof( optarg ) ) ) + if (!(backoff = atof(optarg))) usage(1); break; @@ -533,22 +526,22 @@ int main( int argc, char **argv ) break; case 'H': - if( !( ttl = ( u_int )atoi( optarg ) )) + if (!(ttl = (u_int)atoi(optarg))) usage(1); break; -#if defined( DEBUG ) || defined( _DEBUG ) +#if defined(DEBUG) || defined(_DEBUG) case 'z': - if( ! ( debugging = ( unsigned int )atoi( optarg ) ) ) + if (!(debugging = (unsigned int)atoi(optarg))) usage(1); break; #endif /* DEBUG || _DEBUG */ case 'v': - printf( "%s: Version %s\n", argv[0], VERSION); - printf( "%s: comments to %s\n", argv[0], EMAIL ); - exit( 0 ); + printf("%s: Version %s\n", argv[0], VERSION); + printf("%s: comments to %s\n", argv[0], EMAIL); + exit(0); case 'f': filename = optarg; @@ -561,14 +554,12 @@ int main( int argc, char **argv ) break; case 'S': - if(inet_pton(AF_INET, optarg, &src_addr)) - { + if (inet_pton(AF_INET, optarg, &src_addr)) { src_addr_set = 1; break; } #ifdef IPV6 - if(inet_pton(AF_INET6, optarg, &src_addr6)) - { + if (inet_pton(AF_INET6, optarg, &src_addr6)) { src_addr6_set = 1; break; } @@ -578,21 +569,22 @@ int main( int argc, char **argv ) case 'I': #ifdef SO_BINDTODEVICE - if(socket4) { + if (socket4) { if (setsockopt(socket4, SOL_SOCKET, SO_BINDTODEVICE, optarg, strlen(optarg))) { perror("binding to specific interface (SO_BINTODEVICE)"); } } #ifdef IPV6 - if(socket6) { + if (socket6) { if (setsockopt(socket6, SOL_SOCKET, SO_BINDTODEVICE, optarg, strlen(optarg))) { perror("binding to specific interface (SO_BINTODEVICE), IPV6"); } } #endif #else - printf( "%s: cant bind to a particular net interface since SO_BINDTODEVICE is not supported on your os.\n", argv[0] ); - exit(3);; + printf("%s: cant bind to a particular net interface since SO_BINDTODEVICE is not supported on your os.\n", argv[0]); + exit(3); + ; #endif break; @@ -601,21 +593,20 @@ int main( int argc, char **argv ) break; case 'O': - if (sscanf(optarg,"%i",&tos)){ - if(socket4) { - if ( setsockopt(socket4, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) { + if (sscanf(optarg, "%i", &tos)) { + if (socket4) { + if (setsockopt(socket4, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) { perror("setting type of service octet IP_TOS"); } } #ifdef IPV6 - if(socket6) { - if ( setsockopt(socket6, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos))) { + if (socket6) { + if (setsockopt(socket6, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos))) { perror("setting type of service octet IPV6_TCLASS"); } } #endif - } - else { + } else { usage(1); } break; @@ -629,11 +620,11 @@ int main( int argc, char **argv ) exit(1); break; - }/* SWITCH */ - }/* WHILE */ + } + } /* if we are called 'fping6', assume '-6' */ - if(strstr(argv[0], "fping6")) { + if (strstr(argv[0], "fping6")) { hints_ai_family = AF_INET6; } @@ -644,139 +635,137 @@ int main( int argc, char **argv ) exit(1); } - if( unreachable_flag && alive_flag ) - { - fprintf( stderr, "%s: specify only one of a, u\n", prog); + if (unreachable_flag && alive_flag) { + fprintf(stderr, "%s: specify only one of a, u\n", prog); exit(1); + } - }/* IF */ - - if( count_flag && loop_flag ) - { - fprintf( stderr, "%s: specify only one of c, l\n", prog); + if (count_flag && loop_flag) { + fprintf(stderr, "%s: specify only one of c, l\n", prog); exit(1); + } - }/* IF */ - - if( ( interval < MIN_INTERVAL * 100 || - perhost_interval < MIN_PERHOST_INTERVAL * 100 ) - && getuid() ) - { - fprintf( stderr, "%s: these options are too risky for mere mortals.\n", prog ); - fprintf( stderr, "%s: You need -i >= %u and -p >= %u\n", - prog, MIN_INTERVAL, MIN_PERHOST_INTERVAL ); + if ((interval < MIN_INTERVAL * 100 || perhost_interval < MIN_PERHOST_INTERVAL * 100) + && getuid()) { + fprintf(stderr, "%s: these options are too risky for mere mortals.\n", prog); + fprintf(stderr, "%s: You need -i >= %u and -p >= %u\n", + prog, MIN_INTERVAL, MIN_PERHOST_INTERVAL); exit(1); - }/* IF */ + } - if( ping_data_size > MAX_PING_DATA ) - { - fprintf( stderr, "%s: data size %u not valid, must be lower than %u\n", - prog, ping_data_size, (unsigned int) MAX_PING_DATA ); + if (ping_data_size > MAX_PING_DATA) { + fprintf(stderr, "%s: data size %u not valid, must be lower than %u\n", + prog, ping_data_size, (unsigned int)MAX_PING_DATA); exit(1); + } - }/* IF */ - - if( ( backoff > MAX_BACKOFF_FACTOR ) || ( backoff < MIN_BACKOFF_FACTOR ) ) - { - fprintf( stderr, "%s: backoff factor %.1f not valid, must be between %.1f and %.1f\n", - prog, backoff, MIN_BACKOFF_FACTOR, MAX_BACKOFF_FACTOR ); + if ((backoff > MAX_BACKOFF_FACTOR) || (backoff < MIN_BACKOFF_FACTOR)) { + fprintf(stderr, "%s: backoff factor %.1f not valid, must be between %.1f and %.1f\n", + prog, backoff, MIN_BACKOFF_FACTOR, MAX_BACKOFF_FACTOR); exit(1); + } - }/* IF */ - - if( alive_flag || unreachable_flag ) + if (alive_flag || unreachable_flag) verbose_flag = 0; - if( count_flag ) - { - if( verbose_flag ) + if (count_flag) { + if (verbose_flag) per_recv_flag = 1; alive_flag = unreachable_flag = verbose_flag = 0; + } - }/* IF */ - - if( loop_flag ) - { - if( !report_interval ) + if (loop_flag) { + if (!report_interval) per_recv_flag = 1; alive_flag = unreachable_flag = verbose_flag = 0; + } - }/* IF */ - - - trials = ( count > retry + 1 ) ? count : retry + 1; + trials = (count > retry + 1) ? count : retry + 1; -#if defined( DEBUG ) || defined( _DEBUG ) - if( debugging & DBG_TRACE ) +#if defined(DEBUG) || defined(_DEBUG) + if (debugging & DBG_TRACE) trace_flag = 1; - if( ( debugging & DBG_SENT_TIMES ) && !loop_flag ) + if ((debugging & DBG_SENT_TIMES) && !loop_flag) sent_times_flag = 1; - if( debugging & DBG_RANDOM_LOSE_FEW ) - { + if (debugging & DBG_RANDOM_LOSE_FEW) { randomly_lose_flag = 1; - lose_factor = 1; /* ie, 1/4 */ - - }/* IF */ + lose_factor = 1; /* ie, 1/4 */ + } - if( debugging & DBG_RANDOM_LOSE_MANY ) - { + if (debugging & DBG_RANDOM_LOSE_MANY) { randomly_lose_flag = 1; - lose_factor = 5; /* ie, 3/4 */ - - }/* IF */ + lose_factor = 5; /* ie, 3/4 */ + } - if( debugging & DBG_PRINT_PER_SYSTEM ) + if (debugging & DBG_PRINT_PER_SYSTEM) print_per_system_flag = 1; - if( ( debugging & DBG_REPORT_ALL_RTTS ) && !loop_flag ) + if ((debugging & DBG_REPORT_ALL_RTTS) && !loop_flag) report_all_rtts_flag = 1; - if( trace_flag ) - { - fprintf( stderr, "%s:\n count: %u, retry: %u, interval: %u\n", - prog, count, retry, interval / 10 ); - fprintf( stderr, " perhost_interval: %u, timeout: %u\n", - perhost_interval / 10, timeout / 10 ); - fprintf( stderr, " ping_data_size = %u, trials = %u\n", - ping_data_size, trials ); - - if( verbose_flag ) fprintf( stderr, " verbose_flag set\n" ); - if( multif_flag ) fprintf( stderr, " multif_flag set\n" ); - if( name_flag ) fprintf( stderr, " name_flag set\n" ); - if( addr_flag ) fprintf( stderr, " addr_flag set\n" ); - if( stats_flag ) fprintf( stderr, " stats_flag set\n" ); - if( unreachable_flag ) fprintf( stderr, " unreachable_flag set\n" ); - if( alive_flag ) fprintf( stderr, " alive_flag set\n" ); - if( elapsed_flag ) fprintf( stderr, " elapsed_flag set\n" ); - if( version_flag ) fprintf( stderr, " version_flag set\n" ); - if( count_flag ) fprintf( stderr, " count_flag set\n" ); - if( loop_flag ) fprintf( stderr, " loop_flag set\n" ); - if( backoff_flag ) fprintf( stderr, " backoff_flag set\n" ); - if( per_recv_flag ) fprintf( stderr, " per_recv_flag set\n" ); - if( report_all_rtts_flag ) fprintf( stderr, " report_all_rtts_flag set\n" ); - if( randomly_lose_flag ) fprintf( stderr, " randomly_lose_flag set\n" ); - if( sent_times_flag ) fprintf( stderr, " sent_times_flag set\n" ); - if( print_per_system_flag ) fprintf( stderr, " print_per_system_flag set\n" ); - if( outage_flag ) fprintf( stderr, " outage_flag set\n" ); - if( netdata_flag ) fprintf( stderr, " netdata_flag set\n" ); - - }/* IF */ + if (trace_flag) { + fprintf(stderr, "%s:\n count: %u, retry: %u, interval: %u\n", + prog, count, retry, interval / 10); + fprintf(stderr, " perhost_interval: %u, timeout: %u\n", + perhost_interval / 10, timeout / 10); + fprintf(stderr, " ping_data_size = %u, trials = %u\n", + ping_data_size, trials); + + if (verbose_flag) + fprintf(stderr, " verbose_flag set\n"); + if (multif_flag) + fprintf(stderr, " multif_flag set\n"); + if (name_flag) + fprintf(stderr, " name_flag set\n"); + if (addr_flag) + fprintf(stderr, " addr_flag set\n"); + if (stats_flag) + fprintf(stderr, " stats_flag set\n"); + if (unreachable_flag) + fprintf(stderr, " unreachable_flag set\n"); + if (alive_flag) + fprintf(stderr, " alive_flag set\n"); + if (elapsed_flag) + fprintf(stderr, " elapsed_flag set\n"); + if (version_flag) + fprintf(stderr, " version_flag set\n"); + if (count_flag) + fprintf(stderr, " count_flag set\n"); + if (loop_flag) + fprintf(stderr, " loop_flag set\n"); + if (backoff_flag) + fprintf(stderr, " backoff_flag set\n"); + if (per_recv_flag) + fprintf(stderr, " per_recv_flag set\n"); + if (report_all_rtts_flag) + fprintf(stderr, " report_all_rtts_flag set\n"); + if (randomly_lose_flag) + fprintf(stderr, " randomly_lose_flag set\n"); + if (sent_times_flag) + fprintf(stderr, " sent_times_flag set\n"); + if (print_per_system_flag) + fprintf(stderr, " print_per_system_flag set\n"); + if (outage_flag) + fprintf(stderr, " outage_flag set\n"); + if (netdata_flag) + fprintf(stderr, " netdata_flag set\n"); + } #endif /* DEBUG || _DEBUG */ /* set the TTL, if the -H option was set (otherwise ttl will be = 0) */ - if(ttl > 0) { - if(socket4) { - if (setsockopt(socket4, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl))) { + if (ttl > 0) { + if (socket4) { + if (setsockopt(socket4, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl))) { perror("setting time to live"); } } #ifdef IPV6 - if(socket6) { - if (setsockopt(socket6, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl))) { + if (socket6) { + if (setsockopt(socket6, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl))) { perror("setting time to live"); } } @@ -786,14 +775,14 @@ int main( int argc, char **argv ) #if HAVE_SO_TIMESTAMP { int opt = 1; - if(socket4) { - if (setsockopt(socket4, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { + if (socket4) { + if (setsockopt(socket4, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { perror("setting SO_TIMESTAMP option"); } } #ifdef IPV6 - if(socket6) { - if (setsockopt(socket6, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { + if (socket6) { + if (setsockopt(socket6, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) { perror("setting SO_TIMESTAMP option (IPv6)"); } } @@ -812,133 +801,121 @@ int main( int argc, char **argv ) /* file and generate are mutually exclusive */ /* file and command line are mutually exclusive */ /* generate requires command line parameters beyond the switches */ - if( ( *argv && filename ) || ( filename && generate_flag ) || ( generate_flag && !*argv ) ) + if ((*argv && filename) || (filename && generate_flag) || (generate_flag && !*argv)) usage(1); /* if no conditions are specified, then assume input from stdin */ - if( !*argv && !filename && !generate_flag ) + if (!*argv && !filename && !generate_flag) filename = "-"; - if( *argv && !generate_flag ) - { - while( *argv ) - { - add_name( *argv ); + if (*argv && !generate_flag) { + while (*argv) { + add_name(*argv); ++argv; - }/* WHILE */ - }/* IF */ - else if( filename ) - { - FILE *ping_file; + } + } + else if (filename) { + FILE* ping_file; char line[132]; char host[132]; - if( strcmp( filename, "-" ) == 0 ) - ping_file = fdopen( 0, "r" ); + if (strcmp(filename, "-") == 0) + ping_file = fdopen(0, "r"); else - ping_file = fopen( filename, "r" ); - - if( !ping_file ) - errno_crash_and_burn( "fopen" ); + ping_file = fopen(filename, "r"); + if (!ping_file) + errno_crash_and_burn("fopen"); - while( fgets( line, sizeof(line), ping_file ) ) - { - if( sscanf( line, "%s", host ) != 1 ) + while (fgets(line, sizeof(line), ping_file)) { + if (sscanf(line, "%s", host) != 1) continue; - if( ( !*host ) || ( host[0] == '#' ) ) /* magic to avoid comments */ + if ((!*host) || (host[0] == '#')) /* magic to avoid comments */ continue; add_name(host); - }/* WHILE */ + } - fclose( ping_file ); - } - else if( *argv && generate_flag ) { - if(argc == 1) { + fclose(ping_file); + } else if (*argv && generate_flag) { + if (argc == 1) { /* one target: we expect a cidr range (n.n.n.n/m) */ add_cidr(argv[0]); - } - else if(argc == 2) { + } else if (argc == 2) { add_range(argv[0], argv[1]); - } - else { + } else { usage(1); } - } - else { + } else { usage(1); } - if(!num_hosts) { + if (!num_hosts) { exit(num_noaddress ? 2 : 1); } #ifndef IPV6 - if(src_addr_set) { + if (src_addr_set) { socket_set_src_addr_ipv4(socket4, &src_addr); } #else - if(src_addr6_set) { + if (src_addr6_set) { socket_set_src_addr_ipv6(socket6, &src_addr6); } #endif /* allocate array to hold outstanding ping requests */ - table = ( HOST_ENTRY** )malloc( sizeof( HOST_ENTRY* ) * num_hosts ); - if( !table ) - crash_and_burn( "Can't malloc array of hosts" ); + table = (HOST_ENTRY**)malloc(sizeof(HOST_ENTRY*) * num_hosts); + if (!table) + crash_and_burn("Can't malloc array of hosts"); cursor = ev_first; - for( num_jobs = 0; num_jobs < num_hosts; num_jobs++ ) - { + for (num_jobs = 0; num_jobs < num_hosts; num_jobs++) { table[num_jobs] = cursor; cursor->i = num_jobs; /* as long as we're here, put this in so names print out nicely */ - if( count_flag || loop_flag ) - { - n = max_hostname_len - strlen( cursor->host ); - buf = ( char* ) malloc( n + 1 ); - if( !buf ) - crash_and_burn( "can't malloc host pad" ); - - for ( i = 0; i < n; i++ ) + if (count_flag || loop_flag) { + n = max_hostname_len - strlen(cursor->host); + buf = (char*)malloc(n + 1); + if (!buf) + crash_and_burn("can't malloc host pad"); + + for (i = 0; i < n; i++) buf[i] = ' '; buf[n] = '\0'; cursor->pad = buf; - }/* IF */ - - cursor=cursor->ev_next; + } - }/* FOR */ + cursor = cursor->ev_next; + } init_ping_buffer_ipv4(ping_data_size); #ifdef IPV6 init_ping_buffer_ipv6(ping_data_size); #endif - signal( SIGINT, finish ); + signal(SIGINT, finish); - gettimeofday( &start_time, &tz ); + gettimeofday(&start_time, &tz); current_time = start_time; - if( report_interval ) { + if (report_interval) { next_report_time = start_time; timeval_add(&next_report_time, report_interval); } last_send_time.tv_sec = current_time.tv_sec - 10000; -#if defined( DEBUG ) || defined( _DEBUG ) - if( randomly_lose_flag ) - srandom( start_time.tv_usec ); +#if defined(DEBUG) || defined(_DEBUG) + if (randomly_lose_flag) + srandom(start_time.tv_usec); #endif /* DEBUG || _DEBUG */ seqmap_init(); @@ -949,23 +926,23 @@ int main( int argc, char **argv ) finish(); return 0; -} /* main() */ +} -void add_cidr(char *addr) +void add_cidr(char* addr) { - char *addr_end; - char *mask_str; + char* addr_end; + char* mask_str; unsigned long mask; unsigned long bitmask; int ret; struct addrinfo addr_hints; - struct addrinfo *addr_res; + struct addrinfo* addr_res; unsigned long net_addr; unsigned long net_last; /* Split address from mask */ addr_end = strchr(addr, '/'); - if(addr_end == NULL) { + if (addr_end == NULL) { usage(1); } *addr_end = '\0'; @@ -977,28 +954,28 @@ void add_cidr(char *addr) addr_hints.ai_family = AF_UNSPEC; addr_hints.ai_flags = AI_NUMERICHOST; ret = getaddrinfo(addr, NULL, &addr_hints, &addr_res); - if(ret) { + if (ret) { fprintf(stderr, "%s, can't parse address %s: %s\n", prog, addr, gai_strerror(ret)); exit(1); } - if(addr_res->ai_family != AF_INET) { + if (addr_res->ai_family != AF_INET) { fprintf(stderr, "%s: -g works only with IPv4 addresses\n", prog); exit(1); } - net_addr = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr); + net_addr = ntohl(((struct sockaddr_in*)addr_res->ai_addr)->sin_addr.s_addr); /* check mask */ - if(mask < 1 || mask > 32) { + if (mask < 1 || mask > 32) { fprintf(stderr, "%s: netmask must be between 1 and 32 (is: %s)\n", prog, mask_str); exit(1); } /* convert mask integer from 1 to 32 to a bitmask */ - bitmask = ((unsigned long) 0xFFFFFFFF) << (32-mask); + bitmask = ((unsigned long)0xFFFFFFFF) << (32 - mask); /* calculate network range */ net_addr &= bitmask; - net_last = net_addr + ((unsigned long) 0x1 << (32-mask)) - 1; + net_last = net_addr + ((unsigned long)0x1 << (32 - mask)) - 1; /* exclude network and broadcast address for regular prefixes */ if (mask < 31) { @@ -1007,7 +984,7 @@ void add_cidr(char *addr) } /* add all hosts in that network (net_addr and net_last inclusive) */ - for(; net_addr <= net_last; net_addr++) { + for (; net_addr <= net_last; net_addr++) { struct in_addr in_addr_tmp; char buffer[20]; in_addr_tmp.s_addr = htonl(net_addr); @@ -1015,14 +992,13 @@ void add_cidr(char *addr) add_name(buffer); } - freeaddrinfo(addr_res); } -void add_range(char *start, char *end) +void add_range(char* start, char* end) { struct addrinfo addr_hints; - struct addrinfo *addr_res; + struct addrinfo* addr_res; unsigned long start_long; unsigned long end_long; int ret; @@ -1032,41 +1008,41 @@ void add_range(char *start, char *end) addr_hints.ai_family = AF_UNSPEC; addr_hints.ai_flags = AI_NUMERICHOST; ret = getaddrinfo(start, NULL, &addr_hints, &addr_res); - if(ret) { + if (ret) { fprintf(stderr, "%s: can't parse address %s: %s\n", prog, start, gai_strerror(ret)); exit(1); } - if(addr_res->ai_family != AF_INET) { + if (addr_res->ai_family != AF_INET) { freeaddrinfo(addr_res); fprintf(stderr, "%s: -g works only with IPv4 addresses\n", prog); exit(1); } - start_long = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr); + start_long = ntohl(((struct sockaddr_in*)addr_res->ai_addr)->sin_addr.s_addr); /* parse end address (IPv4 only) */ memset(&addr_hints, 0, sizeof(struct addrinfo)); addr_hints.ai_family = AF_UNSPEC; addr_hints.ai_flags = AI_NUMERICHOST; ret = getaddrinfo(end, NULL, &addr_hints, &addr_res); - if(ret) { + if (ret) { fprintf(stderr, "%s: can't parse address %s: %s\n", prog, end, gai_strerror(ret)); exit(1); } - if(addr_res->ai_family != AF_INET) { + if (addr_res->ai_family != AF_INET) { freeaddrinfo(addr_res); fprintf(stderr, "%s: -g works only with IPv4 addresses\n", prog); exit(1); } - end_long = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr); + end_long = ntohl(((struct sockaddr_in*)addr_res->ai_addr)->sin_addr.s_addr); freeaddrinfo(addr_res); - if(end_long > start_long + MAX_GENERATE) { - fprintf(stderr, "%s: -g parameter generates too many addresses\n", prog); - exit(1); + if (end_long > start_long + MAX_GENERATE) { + fprintf(stderr, "%s: -g parameter generates too many addresses\n", prog); + exit(1); } /* generate */ - for(; start_long <= end_long; start_long++) { + for (; start_long <= end_long; start_long++) { struct in_addr in_addr_tmp; char buffer[20]; in_addr_tmp.s_addr = htonl(start_long); @@ -1075,25 +1051,22 @@ void add_range(char *start, char *end) } } - void main_loop() { long lt; long wait_time; long wait_time_next_report; - HOST_ENTRY *h; + HOST_ENTRY* h; - while(ev_first) { + while (ev_first) { /* Any event that can be processed now ? */ - if(ev_first->ev_time.tv_sec < current_time.tv_sec || - (ev_first->ev_time.tv_sec == current_time.tv_sec && - ev_first->ev_time.tv_usec < current_time.tv_usec)) - { + if (ev_first->ev_time.tv_sec < current_time.tv_sec || (ev_first->ev_time.tv_sec == current_time.tv_sec && ev_first->ev_time.tv_usec < current_time.tv_usec)) { /* Event type: ping */ - if(ev_first->ev_type == EV_TYPE_PING) { + if (ev_first->ev_type == EV_TYPE_PING) { /* Make sure that we don't ping more than once every "interval" */ - lt = timeval_diff( ¤t_time, &last_send_time ); - if(lt < interval) goto wait_for_reply; + lt = timeval_diff(¤t_time, &last_send_time); + if (lt < interval) + goto wait_for_reply; /* Dequeue the event */ h = ev_dequeue(); @@ -1102,16 +1075,16 @@ void main_loop() send_ping(h); /* Check what needs to be done next */ - if(!loop_flag && !count_flag) { + if (!loop_flag && !count_flag) { /* Normal mode: schedule retry */ - if(h->waiting < retry + 1) { + if (h->waiting < retry + 1) { h->ev_type = EV_TYPE_PING; h->ev_time.tv_sec = last_send_time.tv_sec; h->ev_time.tv_usec = last_send_time.tv_usec; timeval_add(&h->ev_time, h->timeout); ev_enqueue(h); - if(backoff_flag) { + if (backoff_flag) { h->timeout *= backoff; } } @@ -1125,8 +1098,7 @@ void main_loop() } } /* Loop and count mode: schedule next ping */ - else if(loop_flag || (count_flag && h->num_sent < count)) - { + else if (loop_flag || (count_flag && h->num_sent < count)) { h->ev_type = EV_TYPE_PING; h->ev_time.tv_sec = last_send_time.tv_sec; h->ev_time.tv_usec = last_send_time.tv_usec; @@ -1134,7 +1106,7 @@ void main_loop() ev_enqueue(h); } /* Count mode: schedule timeout after last ping */ - else if(count_flag && h->num_sent >= count) { + else if (count_flag && h->num_sent >= count) { h->ev_type = EV_TYPE_TIMEOUT; h->ev_time.tv_sec = last_send_time.tv_sec; h->ev_time.tv_usec = last_send_time.tv_usec; @@ -1143,74 +1115,73 @@ void main_loop() } } /* Event type: timeout */ - else if(ev_first->ev_type == EV_TYPE_TIMEOUT) { + else if (ev_first->ev_type == EV_TYPE_TIMEOUT) { num_timeout++; remove_job(ev_first); } } - wait_for_reply: + wait_for_reply: /* When can we expect the next event? */ - if(ev_first) { - if(ev_first->ev_time.tv_sec == 0) { + if (ev_first) { + if (ev_first->ev_time.tv_sec == 0) { wait_time = 0; - } - else { + } else { wait_time = timeval_diff(&ev_first->ev_time, ¤t_time); - if(wait_time < 0) wait_time = 0; + if (wait_time < 0) + wait_time = 0; } - if(ev_first->ev_type == EV_TYPE_PING) { + if (ev_first->ev_type == EV_TYPE_PING) { /* make sure that we wait enough, so that the inter-ping delay is * bigger than 'interval' */ - if(wait_time < interval) { + if (wait_time < interval) { lt = timeval_diff(¤t_time, &last_send_time); - if(lt < interval) { - wait_time = interval-lt; - } - else { + if (lt < interval) { + wait_time = interval - lt; + } else { wait_time = 0; } } } -#if defined( DEBUG ) || defined( _DEBUG ) - if( trace_flag ) { +#if defined(DEBUG) || defined(_DEBUG) + if (trace_flag) { fprintf(stderr, "next event in %d ms (%s)\n", wait_time / 100, ev_first->host); } #endif - } - else { + } else { wait_time = interval; } /* Make sure we don't wait too long, in case a report is expected */ - if( report_interval && ( loop_flag || count_flag ) ) { - wait_time_next_report = timeval_diff ( &next_report_time, ¤t_time ); - if(wait_time_next_report < wait_time) { - wait_time = wait_time_next_report; - if(wait_time < 0) { wait_time = 0; } - } + if (report_interval && (loop_flag || count_flag)) { + wait_time_next_report = timeval_diff(&next_report_time, ¤t_time); + if (wait_time_next_report < wait_time) { + wait_time = wait_time_next_report; + if (wait_time < 0) { + wait_time = 0; + } + } } /* Receive replies */ /* (this is what sleeps during each loop iteration) */ - if(wait_for_reply(wait_time)) { - while(wait_for_reply(0)); /* process other replies in the queue */ + if (wait_for_reply(wait_time)) { + while (wait_for_reply(0)) + ; /* process other replies in the queue */ } - gettimeofday( ¤t_time, &tz ); + gettimeofday(¤t_time, &tz); /* Print report */ - if( report_interval && ( loop_flag || count_flag ) && - ( timeval_diff ( ¤t_time, &next_report_time ) >= 0 ) ) - { - if(netdata_flag) + if (report_interval && (loop_flag || count_flag) && (timeval_diff(¤t_time, &next_report_time) >= 0)) { + if (netdata_flag) print_netdata(); else print_per_system_splits(); - while(timeval_diff ( ¤t_time, &next_report_time ) >= 0 ) + while (timeval_diff(¤t_time, &next_report_time) >= 0) timeval_add(&next_report_time, report_interval); } } @@ -1233,51 +1204,46 @@ void main_loop() void finish() { int i; - HOST_ENTRY *h; + HOST_ENTRY* h; - gettimeofday( &end_time, &tz ); + gettimeofday(&end_time, &tz); /* tot up unreachables */ - for( i = 0; i < num_hosts; i++ ) - { + for (i = 0; i < num_hosts; i++) { h = table[i]; - if( !h->num_recv ) - { + if (!h->num_recv) { num_unreachable++; - if( verbose_flag || unreachable_flag ) - { - printf( "%s", h->host ); + if (verbose_flag || unreachable_flag) { + printf("%s", h->host); - if( verbose_flag ) - printf( " is unreachable" ); + if (verbose_flag) + printf(" is unreachable"); - printf( "\n" ); + printf("\n"); - }/* IF */ - }/* IF */ - }/* FOR */ + } + } + } - if( count_flag || loop_flag ) + if (count_flag || loop_flag) print_per_system_stats(); -#if defined( DEBUG ) || defined( _DEBUG ) - else if( print_per_system_flag ) +#if defined(DEBUG) || defined(_DEBUG) + else if (print_per_system_flag) print_per_system_stats(); #endif /* DEBUG || _DEBUG */ - if( stats_flag ) + if (stats_flag) print_global_stats(); - if( num_noaddress ) - exit( 2 ); - else if( num_alive != num_hosts ) - exit( 1 ); + if (num_noaddress) + exit(2); + else if (num_alive != num_hosts) + exit(1); exit(0); - -} /* finish() */ - +} /************************************************************ @@ -1292,83 +1258,72 @@ void finish() ************************************************************/ -void print_per_system_stats( void ) +void print_per_system_stats(void) { int i, j, avg, outage_ms; - HOST_ENTRY *h; + HOST_ENTRY* h; int resp; - fflush( stdout ); + fflush(stdout); - if( verbose_flag || per_recv_flag ) - fprintf( stderr, "\n" ); + if (verbose_flag || per_recv_flag) + fprintf(stderr, "\n"); - for( i = 0; i < num_hosts; i++ ) - { + for (i = 0; i < num_hosts; i++) { h = table[i]; - fprintf( stderr, "%s%s :", h->host, h->pad ); + fprintf(stderr, "%s%s :", h->host, h->pad); - if( report_all_rtts_flag ) { - for( j = 0; j < h->num_sent; j++ ) { - if( ( resp = h->resp_times[j] ) >= 0 ) - fprintf( stderr, " %d.%02d", resp / 100, resp % 100 ); + if (report_all_rtts_flag) { + for (j = 0; j < h->num_sent; j++) { + if ((resp = h->resp_times[j]) >= 0) + fprintf(stderr, " %d.%02d", resp / 100, resp % 100); else - fprintf( stderr, " -" ); + fprintf(stderr, " -"); } - fprintf( stderr, "\n" ); - } - else { - if( h->num_recv <= h->num_sent ) - { - fprintf( stderr, " xmt/rcv/%%loss = %d/%d/%d%%", - h->num_sent, h->num_recv, h->num_sent > 0 ? - ( ( h->num_sent - h->num_recv ) * 100 ) / h->num_sent : 0 ); + fprintf(stderr, "\n"); + } else { + if (h->num_recv <= h->num_sent) { + fprintf(stderr, " xmt/rcv/%%loss = %d/%d/%d%%", + h->num_sent, h->num_recv, h->num_sent > 0 ? ((h->num_sent - h->num_recv) * 100) / h->num_sent : 0); if (outage_flag) { /* Time outage total */ - outage_ms = (h->num_sent - h->num_recv) * perhost_interval/100; - fprintf( stderr, ", outage(ms) = %d", outage_ms ); + outage_ms = (h->num_sent - h->num_recv) * perhost_interval / 100; + fprintf(stderr, ", outage(ms) = %d", outage_ms); } - } - else { - fprintf( stderr, " xmt/rcv/%%return = %d/%d/%d%%", + } else { + fprintf(stderr, " xmt/rcv/%%return = %d/%d/%d%%", h->num_sent, h->num_recv, - ( ( h->num_recv * 100 ) / h->num_sent ) ); - + ((h->num_recv * 100) / h->num_sent)); } - if( h->num_recv ) { + if (h->num_recv) { avg = h->total_time / h->num_recv; - fprintf( stderr, ", min/avg/max = %s", sprint_tm( h->min_reply ) ); - fprintf( stderr, "/%s", sprint_tm( avg ) ); - fprintf( stderr, "/%s", sprint_tm( h->max_reply ) ); - + fprintf(stderr, ", min/avg/max = %s", sprint_tm(h->min_reply)); + fprintf(stderr, "/%s", sprint_tm(avg)); + fprintf(stderr, "/%s", sprint_tm(h->max_reply)); } fprintf(stderr, "\n"); } -#if defined( DEBUG ) || defined( _DEBUG ) - if( sent_times_flag ) - { - for( j = 0; j < h->num_sent; j++ ) - { - if( ( resp = h->sent_times[j] ) >= 0 ) - fprintf( stderr, " %s", sprint_tm( resp ) ); +#if defined(DEBUG) || defined(_DEBUG) + if (sent_times_flag) { + for (j = 0; j < h->num_sent; j++) { + if ((resp = h->sent_times[j]) >= 0) + fprintf(stderr, " %s", sprint_tm(resp)); else - fprintf( stderr, " -" ); - - fprintf( stderr, "\n" ); + fprintf(stderr, " -"); + fprintf(stderr, "\n"); } } #endif } } - /************************************************************ Function: print_netdata @@ -1382,20 +1337,19 @@ void print_per_system_stats( void ) ************************************************************/ -void print_netdata( void ) +void print_netdata(void) { static int sent_charts = 0; int i, avg; - HOST_ENTRY *h; + HOST_ENTRY* h; - fflush( stdout ); + fflush(stdout); - for( i = 0; i < num_hosts; i++ ) - { + for (i = 0; i < num_hosts; i++) { h = table[i]; - if(!sent_charts) { + if (!sent_charts) { printf("CHART fping.%s_packets '' 'FPing Packets for host %s' packets '%s' fping.packets line 110020 %d\n", h->name, h->host, h->name, report_interval / 100000); printf("DIMENSION xmt sent absolute 1 1\n"); printf("DIMENSION rcv received absolute 1 1\n"); @@ -1406,34 +1360,33 @@ void print_netdata( void ) printf("SET rcv = %d\n", h->num_recv_i); printf("END\n"); - if(!sent_charts) { + if (!sent_charts) { printf("CHART fping.%s_quality '' 'FPing Quality for host %s' percentage '%s' fping.quality area 110010 %d\n", h->name, h->host, h->name, report_interval / 100000); printf("DIMENSION returned '' absolute 1 1\n"); /* printf("DIMENSION lost '' absolute 1 1\n"); */ } /* if we just sent the probe and didn't receive a reply, we shouldn't count it */ - h->discard_next_recv_i=0; - if( h->waiting && timeval_diff(¤t_time, &h->last_send_time) < h->timeout) { - if(h->num_sent_i) { + h->discard_next_recv_i = 0; + if (h->waiting && timeval_diff(¤t_time, &h->last_send_time) < h->timeout) { + if (h->num_sent_i) { h->num_sent_i--; - h->discard_next_recv_i=1; + h->discard_next_recv_i = 1; } } - printf("BEGIN fping.%s_quality\n", h->name); -/* + /* if( h->num_recv_i <= h->num_sent_i ) printf("SET lost = %d\n", h->num_sent_i > 0 ? ( ( h->num_sent_i - h->num_recv_i ) * 100 ) / h->num_sent_i : 0 ); else printf("SET lost = 0\n"); */ - printf("SET returned = %d\n", h->num_sent_i > 0 ? ( ( h->num_recv_i * 100 ) / h->num_sent_i ) : 0 ); + printf("SET returned = %d\n", h->num_sent_i > 0 ? ((h->num_recv_i * 100) / h->num_sent_i) : 0); printf("END\n"); - if(!sent_charts) { + if (!sent_charts) { printf("CHART fping.%s_latency '' 'FPing Latency for host %s' ms '%s' fping.latency area 110000 %d\n", h->name, h->host, h->name, report_interval / 100000); printf("DIMENSION min minimum absolute 10 1000\n"); printf("DIMENSION max maximum absolute 10 1000\n"); @@ -1441,23 +1394,21 @@ void print_netdata( void ) } printf("BEGIN fping.%s_latency\n", h->name); - if( h->num_recv_i ) - { + if (h->num_recv_i) { avg = h->total_time_i / h->num_recv_i; printf("SET min = %d\n", h->min_reply_i); printf("SET avg = %d\n", avg); printf("SET max = %d\n", h->max_reply_i); - }/* IF */ + } printf("END\n"); - h->num_sent_i = h->num_recv_i = h->max_reply_i = - h->min_reply_i = h->total_time_i = 0; + h->num_sent_i = h->num_recv_i = h->max_reply_i = h->min_reply_i = h->total_time_i = 0; - }/* FOR */ + } sent_charts = 1; -} /* print_per_system_splits() */ +} /************************************************************ @@ -1472,67 +1423,60 @@ void print_netdata( void ) ************************************************************/ -void print_per_system_splits( void ) +void print_per_system_splits(void) { int i, avg, outage_ms_i; - HOST_ENTRY *h; - struct tm *curr_tm; + HOST_ENTRY* h; + struct tm* curr_tm; - fflush( stdout ); + fflush(stdout); - if( verbose_flag || per_recv_flag ) - fprintf( stderr, "\n" ); + if (verbose_flag || per_recv_flag) + fprintf(stderr, "\n"); - gettimeofday( ¤t_time, &tz ); - curr_tm = localtime( ( time_t* )¤t_time.tv_sec ); - fprintf( stderr, "[%2.2d:%2.2d:%2.2d]\n", curr_tm->tm_hour, - curr_tm->tm_min, curr_tm->tm_sec ); + gettimeofday(¤t_time, &tz); + curr_tm = localtime((time_t*)¤t_time.tv_sec); + fprintf(stderr, "[%2.2d:%2.2d:%2.2d]\n", curr_tm->tm_hour, + curr_tm->tm_min, curr_tm->tm_sec); - for( i = 0; i < num_hosts; i++ ) { + for (i = 0; i < num_hosts; i++) { h = table[i]; - fprintf( stderr, "%s%s :", h->host, h->pad ); + fprintf(stderr, "%s%s :", h->host, h->pad); /* if we just sent the probe and didn't receive a reply, we shouldn't count it */ - h->discard_next_recv_i=0; - if( h->waiting && timeval_diff(¤t_time, &h->last_send_time) < h->timeout) { - if(h->num_sent_i) { + h->discard_next_recv_i = 0; + if (h->waiting && timeval_diff(¤t_time, &h->last_send_time) < h->timeout) { + if (h->num_sent_i) { h->num_sent_i--; - h->discard_next_recv_i=1; + h->discard_next_recv_i = 1; } } - if( h->num_recv_i <= h->num_sent_i ) { - fprintf( stderr, " xmt/rcv/%%loss = %d/%d/%d%%", - h->num_sent_i, h->num_recv_i, h->num_sent_i > 0 ? - ( ( h->num_sent_i - h->num_recv_i ) * 100 ) / h->num_sent_i : 0 ); + if (h->num_recv_i <= h->num_sent_i) { + fprintf(stderr, " xmt/rcv/%%loss = %d/%d/%d%%", + h->num_sent_i, h->num_recv_i, h->num_sent_i > 0 ? ((h->num_sent_i - h->num_recv_i) * 100) / h->num_sent_i : 0); if (outage_flag) { /* Time outage */ - outage_ms_i = (h->num_sent_i - h->num_recv_i) * perhost_interval/100; - fprintf( stderr, ", outage(ms) = %d", outage_ms_i ); + outage_ms_i = (h->num_sent_i - h->num_recv_i) * perhost_interval / 100; + fprintf(stderr, ", outage(ms) = %d", outage_ms_i); } - } - else { - fprintf( stderr, " xmt/rcv/%%return = %d/%d/%d%%", - h->num_sent_i, h->num_recv_i, h->num_sent_i > 0 ? - ( ( h->num_recv_i * 100 ) / h->num_sent_i ) : 0 ); + } else { + fprintf(stderr, " xmt/rcv/%%return = %d/%d/%d%%", + h->num_sent_i, h->num_recv_i, h->num_sent_i > 0 ? ((h->num_recv_i * 100) / h->num_sent_i) : 0); } - if( h->num_recv_i ) { + if (h->num_recv_i) { avg = h->total_time_i / h->num_recv_i; - fprintf( stderr, ", min/avg/max = %s", sprint_tm( h->min_reply_i ) ); - fprintf( stderr, "/%s", sprint_tm( avg ) ); - fprintf( stderr, "/%s", sprint_tm( h->max_reply_i ) ); - + fprintf(stderr, ", min/avg/max = %s", sprint_tm(h->min_reply_i)); + fprintf(stderr, "/%s", sprint_tm(avg)); + fprintf(stderr, "/%s", sprint_tm(h->max_reply_i)); } - fprintf( stderr, "\n" ); - h->num_sent_i = h->num_recv_i = h->max_reply_i = - h->min_reply_i = h->total_time_i = 0; - + fprintf(stderr, "\n"); + h->num_sent_i = h->num_recv_i = h->max_reply_i = h->min_reply_i = h->total_time_i = 0; } -} /* print_per_system_splits() */ - +} /************************************************************ @@ -1547,40 +1491,38 @@ void print_per_system_splits( void ) ************************************************************/ -void print_global_stats( void ) +void print_global_stats(void) { - fflush( stdout ); - fprintf( stderr, "\n" ); - fprintf( stderr, " %7d targets\n", num_hosts ); - fprintf( stderr, " %7d alive\n", num_alive ); - fprintf( stderr, " %7d unreachable\n" ,num_unreachable ); - fprintf( stderr, " %7d unknown addresses\n", num_noaddress ); - fprintf( stderr, "\n" ); - fprintf( stderr, " %7d timeouts (waiting for response)\n", num_timeout ); - fprintf( stderr, " %7d ICMP Echos sent\n", num_pingsent ); - fprintf( stderr, " %7d ICMP Echo Replies received\n", num_pingreceived ); - fprintf( stderr, " %7d other ICMP received\n", num_othericmprcvd ); - fprintf( stderr, "\n" ); - - if( total_replies == 0 ) - { + fflush(stdout); + fprintf(stderr, "\n"); + fprintf(stderr, " %7d targets\n", num_hosts); + fprintf(stderr, " %7d alive\n", num_alive); + fprintf(stderr, " %7d unreachable\n", num_unreachable); + fprintf(stderr, " %7d unknown addresses\n", num_noaddress); + fprintf(stderr, "\n"); + fprintf(stderr, " %7d timeouts (waiting for response)\n", num_timeout); + fprintf(stderr, " %7d ICMP Echos sent\n", num_pingsent); + fprintf(stderr, " %7d ICMP Echo Replies received\n", num_pingreceived); + fprintf(stderr, " %7d other ICMP received\n", num_othericmprcvd); + fprintf(stderr, "\n"); + + if (total_replies == 0) { min_reply = 0; max_reply = 0; total_replies = 1; sum_replies = 0; - }/* IF */ - - fprintf( stderr, " %s ms (min round trip time)\n", sprint_tm( min_reply ) ); - fprintf( stderr, " %s ms (avg round trip time)\n", - sprint_tm( ( int )( sum_replies / total_replies ) ) ); - fprintf( stderr, " %s ms (max round trip time)\n", sprint_tm( max_reply ) ); - fprintf( stderr, " %12.3f sec (elapsed real time)\n", - timeval_diff( &end_time, &start_time ) / 100000.0 ); - fprintf( stderr, "\n" ); + } -} /* print_global_stats() */ + fprintf(stderr, " %s ms (min round trip time)\n", sprint_tm(min_reply)); + fprintf(stderr, " %s ms (avg round trip time)\n", + sprint_tm((int)(sum_replies / total_replies))); + fprintf(stderr, " %s ms (max round trip time)\n", sprint_tm(max_reply)); + fprintf(stderr, " %12.3f sec (elapsed real time)\n", + timeval_diff(&end_time, &start_time) / 100000.0); + fprintf(stderr, "\n"); +} /************************************************************ @@ -1600,55 +1542,54 @@ void print_global_stats( void ) ************************************************************/ -int send_ping( HOST_ENTRY *h ) +int send_ping(HOST_ENTRY* h) { int n; int myseq; int ret = 1; - gettimeofday( &h->last_send_time, &tz ); + gettimeofday(&h->last_send_time, &tz); myseq = seqmap_add(h->i, h->num_sent, &h->last_send_time); #if defined(DEBUG) || defined(_DEBUG) - if( trace_flag ) - printf( "sending [%d] to %s\n", h->num_sent, h->host ); + if (trace_flag) + printf("sending [%d] to %s\n", h->num_sent, h->host); #endif /* DEBUG || _DEBUG */ - if(h->saddr.ss_family == AF_INET) { - n = socket_sendto_ping_ipv4(socket4, (struct sockaddr *) &h->saddr, h->saddr_len, myseq, ident); + if (h->saddr.ss_family == AF_INET) { + n = socket_sendto_ping_ipv4(socket4, (struct sockaddr*)&h->saddr, h->saddr_len, myseq, ident); } #ifdef IPV6 - else if(h->saddr.ss_family == AF_INET6) { - n = socket_sendto_ping_ipv6(socket6, (struct sockaddr *) &h->saddr, h->saddr_len, myseq, ident); + else if (h->saddr.ss_family == AF_INET6) { + n = socket_sendto_ping_ipv6(socket6, (struct sockaddr*)&h->saddr, h->saddr_len, myseq, ident); } #endif else { return 0; } - if( + if ( (n < 0) -#if defined( EHOSTDOWN ) +#if defined(EHOSTDOWN) && errno != EHOSTDOWN #endif - ) { - if( verbose_flag ) { - print_warning( "%s: error while sending ping: %s\n", h->host, strerror( errno ) ); + ) { + if (verbose_flag) { + print_warning("%s: error while sending ping: %s\n", h->host, strerror(errno)); } - if( !loop_flag ) + if (!loop_flag) h->resp_times[h->num_sent] = RESP_ERROR; ret = 0; - } - else { + } else { /* mark this trial as outstanding */ - if( !loop_flag ) + if (!loop_flag) h->resp_times[h->num_sent] = RESP_WAITING; -#if defined( DEBUG ) || defined( _DEBUG ) - if( sent_times_flag ) - h->sent_times[h->num_sent] = timeval_diff( &h->last_send_time, &start_time ); +#if defined(DEBUG) || defined(_DEBUG) + if (sent_times_flag) + h->sent_times[h->num_sent] = timeval_diff(&h->last_send_time, &start_time); #endif } @@ -1657,12 +1598,12 @@ int send_ping( HOST_ENTRY *h ) h->waiting++; num_pingsent++; last_send_time = h->last_send_time; - h->discard_next_recv_i=0; + h->discard_next_recv_i = 0; - return(ret); + return (ret); } -int socket_can_read(struct timeval *timeout) +int socket_can_read(struct timeval* timeout) { int nfound; fd_set readset, writeset; @@ -1675,30 +1616,29 @@ int socket_can_read(struct timeval *timeout) #endif select_again: - FD_ZERO( &readset ); - FD_ZERO( &writeset ); - FD_SET( socket4, &readset ); + FD_ZERO(&readset); + FD_ZERO(&writeset); + FD_SET(socket4, &readset); #ifdef IPV6 - FD_SET( socket6, &readset ); + FD_SET(socket6, &readset); #endif nfound = select(socketmax + 1, &readset, &writeset, NULL, timeout); - if(nfound < 0) { - if(errno == EINTR) { + if (nfound < 0) { + if (errno == EINTR) { /* interrupted system call: redo the select */ goto select_again; - } - else { + } else { perror("select"); } } - if(nfound > 0) { - if(FD_ISSET(socket4, &readset)) { + if (nfound > 0) { + if (FD_ISSET(socket4, &readset)) { return socket4; } #ifdef IPV6 - if(FD_ISSET(socket6, &readset)) { + if (FD_ISSET(socket6, &readset)) { return socket6; } #endif @@ -1708,11 +1648,11 @@ select_again: } int receive_packet(int socket, - struct timeval *reply_timestamp, - struct sockaddr *reply_src_addr, - size_t reply_src_addr_len, - char *reply_buf, - size_t reply_buf_len) + struct timeval* reply_timestamp, + struct sockaddr* reply_src_addr, + size_t reply_src_addr_len, + char* reply_buf, + size_t reply_buf_len) { int recv_len; static unsigned char msg_control[40]; @@ -1730,36 +1670,32 @@ int receive_packet(int socket, 0 }; int timestamp_set = 0; - struct cmsghdr *cmsg; + struct cmsghdr* cmsg; recv_len = recvmsg(socket, &recv_msghdr, 0); - if(recv_len <= 0) { + if (recv_len <= 0) { return 0; } #if HAVE_SO_TIMESTAMP /* ancilliary data */ - for(cmsg = CMSG_FIRSTHDR(&recv_msghdr); - cmsg != NULL; - cmsg = CMSG_NXTHDR(&recv_msghdr, cmsg)) - { - if(cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_TIMESTAMP) - { + for (cmsg = CMSG_FIRSTHDR(&recv_msghdr); + cmsg != NULL; + cmsg = CMSG_NXTHDR(&recv_msghdr, cmsg)) { + if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMP) { memcpy(reply_timestamp, CMSG_DATA(cmsg), sizeof(*reply_timestamp)); timestamp_set = 1; } } #endif - if(! timestamp_set) { + if (!timestamp_set) { gettimeofday(reply_timestamp, NULL); } -#if defined( DEBUG ) || defined( _DEBUG ) - if( randomly_lose_flag ) - { - if( ( random() & 0x07 ) <= lose_factor ) +#if defined(DEBUG) || defined(_DEBUG) + if (randomly_lose_flag) { + if ((random() & 0x07) <= lose_factor) return 0; } #endif @@ -1768,107 +1704,102 @@ int receive_packet(int socket, } int decode_icmp_ipv4( - struct sockaddr *response_addr, - size_t response_addr_len, - char *reply_buf, - size_t reply_buf_len, - unsigned short *id, - unsigned short *seq - ) + struct sockaddr* response_addr, + size_t response_addr_len, + char* reply_buf, + size_t reply_buf_len, + unsigned short* id, + unsigned short* seq) { - struct ip *ip = (struct ip *) reply_buf; - struct icmp *icp; + struct ip* ip = (struct ip*)reply_buf; + struct icmp* icp; int hlen = 0; -#if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) +#if defined(__alpha__) && __STDC__ && !defined(__GLIBC__) /* The alpha headers are decidedly broken. * Using an ANSI compiler, it provides ip_vhl instead of ip_hl and * ip_v. So, to get ip_hl, we mask off the bottom four bits. */ - hlen = ( ip->ip_vhl & 0x0F ) << 2; + hlen = (ip->ip_vhl & 0x0F) << 2; #else hlen = ip->ip_hl << 2; #endif - if( reply_buf_len < hlen + ICMP_MINLEN ) { + if (reply_buf_len < hlen + ICMP_MINLEN) { /* too short */ - if( verbose_flag ) - { + if (verbose_flag) { char buf[INET6_ADDRSTRLEN]; - getnameinfo((struct sockaddr *)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); - printf( "received packet too short for ICMP (%d bytes from %s)\n", (int) reply_buf_len, buf); + getnameinfo((struct sockaddr*)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); + printf("received packet too short for ICMP (%d bytes from %s)\n", (int)reply_buf_len, buf); } return 0; } - icp = (struct icmp *)(reply_buf + hlen); + icp = (struct icmp*)(reply_buf + hlen); - if( icp->icmp_type != ICMP_ECHOREPLY ) - { + if (icp->icmp_type != ICMP_ECHOREPLY) { /* Handle other ICMP packets */ - struct icmp *sent_icmp; - SEQMAP_VALUE *seqmap_value; + struct icmp* sent_icmp; + SEQMAP_VALUE* seqmap_value; char addr_ascii[INET6_ADDRSTRLEN]; - HOST_ENTRY *h; + HOST_ENTRY* h; /* reply icmp packet (hlen + ICMP_MINLEN) followed by "sent packet" (ip + icmp headers) */ - if(reply_buf_len < hlen + ICMP_MINLEN + sizeof(struct ip) + ICMP_MINLEN) { + if (reply_buf_len < hlen + ICMP_MINLEN + sizeof(struct ip) + ICMP_MINLEN) { /* discard ICMP message if we can't tell that it was caused by us (i.e. if the "sent packet" is not included). */ return 0; } - sent_icmp = (struct icmp *) (reply_buf + hlen + ICMP_MINLEN + sizeof(struct ip)); + sent_icmp = (struct icmp*)(reply_buf + hlen + ICMP_MINLEN + sizeof(struct ip)); - if(sent_icmp->icmp_type != ICMP_ECHO || ntohs(sent_icmp->icmp_id) != ident) { + if (sent_icmp->icmp_type != ICMP_ECHO || ntohs(sent_icmp->icmp_id) != ident) { /* not caused by us */ return 0; } seqmap_value = seqmap_fetch(ntohs(sent_icmp->icmp_seq), ¤t_time); - if(seqmap_value == NULL) { + if (seqmap_value == NULL) { return 0; } getnameinfo(response_addr, response_addr_len, addr_ascii, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); - switch(icp->icmp_type) { - case ICMP_UNREACH: - h = table[seqmap_value->host_nr]; - if( icp->icmp_code > ICMP_UNREACH_MAXTYPE ) { - print_warning("ICMP Unreachable (Invalid Code) from %s for ICMP Echo sent to %s", - addr_ascii, h->host ); - } - else { - print_warning("%s from %s for ICMP Echo sent to %s", - icmp_unreach_str[icp->icmp_code], addr_ascii, h->host); - } + switch (icp->icmp_type) { + case ICMP_UNREACH: + h = table[seqmap_value->host_nr]; + if (icp->icmp_code > ICMP_UNREACH_MAXTYPE) { + print_warning("ICMP Unreachable (Invalid Code) from %s for ICMP Echo sent to %s", + addr_ascii, h->host); + } else { + print_warning("%s from %s for ICMP Echo sent to %s", + icmp_unreach_str[icp->icmp_code], addr_ascii, h->host); + } - print_warning("\n" ); - num_othericmprcvd++; - break; + print_warning("\n"); + num_othericmprcvd++; + break; - case ICMP_SOURCEQUENCH: - case ICMP_REDIRECT: - case ICMP_TIMXCEED: - case ICMP_PARAMPROB: - h = table[seqmap_value->host_nr]; - if(icp->icmp_type <= ICMP_TYPE_STR_MAX) { - print_warning("%s from %s for ICMP Echo sent to %s", - icmp_type_str[icp->icmp_type], addr_ascii, h->host ); - } - else { - print_warning("ICMP %d from %s for ICMP Echo sent to %s", - icp->icmp_type, addr_ascii, h->host ); - } - print_warning( "\n" ); - num_othericmprcvd++; - break; + case ICMP_SOURCEQUENCH: + case ICMP_REDIRECT: + case ICMP_TIMXCEED: + case ICMP_PARAMPROB: + h = table[seqmap_value->host_nr]; + if (icp->icmp_type <= ICMP_TYPE_STR_MAX) { + print_warning("%s from %s for ICMP Echo sent to %s", + icmp_type_str[icp->icmp_type], addr_ascii, h->host); + } else { + print_warning("ICMP %d from %s for ICMP Echo sent to %s", + icp->icmp_type, addr_ascii, h->host); + } + print_warning("\n"); + num_othericmprcvd++; + break; } return 0; } - *id = ntohs(icp->icmp_id); + *id = ntohs(icp->icmp_id); *seq = ntohs(icp->icmp_seq); return 1; @@ -1876,177 +1807,166 @@ int decode_icmp_ipv4( #ifdef IPV6 int decode_icmp_ipv6( - struct sockaddr *response_addr, - size_t response_addr_len, - char *reply_buf, - size_t reply_buf_len, - unsigned short *id, - unsigned short *seq - ) + struct sockaddr* response_addr, + size_t response_addr_len, + char* reply_buf, + size_t reply_buf_len, + unsigned short* id, + unsigned short* seq) { - struct icmp6_hdr *icp; + struct icmp6_hdr* icp; - if( reply_buf_len < sizeof(struct icmp6_hdr) ) - { - if( verbose_flag ) - { + if (reply_buf_len < sizeof(struct icmp6_hdr)) { + if (verbose_flag) { char buf[INET6_ADDRSTRLEN]; - getnameinfo((struct sockaddr *)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); - printf( "received packet too short for ICMP (%d bytes from %s)\n", (int) reply_buf_len, buf); + getnameinfo((struct sockaddr*)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); + printf("received packet too short for ICMP (%d bytes from %s)\n", (int)reply_buf_len, buf); } return 0; /* too short */ } - icp = (struct icmp6_hdr *) reply_buf; + icp = (struct icmp6_hdr*)reply_buf; - if( icp->icmp6_type != ICMP6_ECHO_REPLY ) - { + if (icp->icmp6_type != ICMP6_ECHO_REPLY) { /* Handle other ICMP packets */ - struct icmp6_hdr *sent_icmp; - SEQMAP_VALUE *seqmap_value; + struct icmp6_hdr* sent_icmp; + SEQMAP_VALUE* seqmap_value; char addr_ascii[INET6_ADDRSTRLEN]; - HOST_ENTRY *h; + HOST_ENTRY* h; /* reply icmp packet (ICMP_MINLEN) followed by "sent packet" (ip + icmp headers) */ - if(reply_buf_len < ICMP_MINLEN + sizeof(struct ip) + ICMP_MINLEN) { + if (reply_buf_len < ICMP_MINLEN + sizeof(struct ip) + ICMP_MINLEN) { /* discard ICMP message if we can't tell that it was caused by us (i.e. if the "sent packet" is not included). */ return 0; } - sent_icmp = (struct icmp6_hdr *) (reply_buf + sizeof(struct icmp6_hdr) + sizeof(struct ip)); + sent_icmp = (struct icmp6_hdr*)(reply_buf + sizeof(struct icmp6_hdr) + sizeof(struct ip)); - if(sent_icmp->icmp6_type != ICMP_ECHO || ntohs(sent_icmp->icmp6_id) != ident) { + if (sent_icmp->icmp6_type != ICMP_ECHO || ntohs(sent_icmp->icmp6_id) != ident) { /* not caused by us */ return 0; } seqmap_value = seqmap_fetch(ntohs(sent_icmp->icmp6_seq), ¤t_time); - if(seqmap_value == NULL) { + if (seqmap_value == NULL) { return 0; } getnameinfo(response_addr, response_addr_len, addr_ascii, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); - switch(icp->icmp6_type) { - case ICMP_UNREACH: - h = table[seqmap_value->host_nr]; - if( icp->icmp6_code > ICMP_UNREACH_MAXTYPE ) { - print_warning("ICMP Unreachable (Invalid Code) from %s for ICMP Echo sent to %s", - addr_ascii, h->host ); - } - else { - print_warning("%s from %s for ICMP Echo sent to %s", - icmp_unreach_str[icp->icmp6_code], addr_ascii, h->host); - } + switch (icp->icmp6_type) { + case ICMP_UNREACH: + h = table[seqmap_value->host_nr]; + if (icp->icmp6_code > ICMP_UNREACH_MAXTYPE) { + print_warning("ICMP Unreachable (Invalid Code) from %s for ICMP Echo sent to %s", + addr_ascii, h->host); + } else { + print_warning("%s from %s for ICMP Echo sent to %s", + icmp_unreach_str[icp->icmp6_code], addr_ascii, h->host); + } - print_warning("\n" ); - num_othericmprcvd++; - break; + print_warning("\n"); + num_othericmprcvd++; + break; - case ICMP_SOURCEQUENCH: - case ICMP_REDIRECT: - case ICMP_TIMXCEED: - case ICMP_PARAMPROB: - h = table[seqmap_value->host_nr]; - if(icp->icmp6_type <= ICMP_TYPE_STR_MAX) { - print_warning("%s from %s for ICMP Echo sent to %s", - icmp_type_str[icp->icmp6_type], addr_ascii, h->host ); - } - else { - print_warning("ICMP %d from %s for ICMP Echo sent to %s", - icp->icmp6_type, addr_ascii, h->host ); - } - print_warning( "\n" ); - num_othericmprcvd++; - break; + case ICMP_SOURCEQUENCH: + case ICMP_REDIRECT: + case ICMP_TIMXCEED: + case ICMP_PARAMPROB: + h = table[seqmap_value->host_nr]; + if (icp->icmp6_type <= ICMP_TYPE_STR_MAX) { + print_warning("%s from %s for ICMP Echo sent to %s", + icmp_type_str[icp->icmp6_type], addr_ascii, h->host); + } else { + print_warning("ICMP %d from %s for ICMP Echo sent to %s", + icp->icmp6_type, addr_ascii, h->host); + } + print_warning("\n"); + num_othericmprcvd++; + break; } return 0; } - *id = ntohs(icp->icmp6_id); + *id = ntohs(icp->icmp6_id); *seq = ntohs(icp->icmp6_seq); return 1; } #endif - int wait_for_reply(long wait_time) { int result; static char buffer[4096]; struct sockaddr_storage response_addr; int n, avg; - HOST_ENTRY *h; + HOST_ENTRY* h; long this_reply; int this_count; - struct timeval *sent_time; + struct timeval* sent_time; struct timeval recv_time; - SEQMAP_VALUE *seqmap_value; + SEQMAP_VALUE* seqmap_value; unsigned short id; unsigned short seq; struct timeval to; int s = 0; /* Wait for a socket to become ready */ - if(wait_time) { - if(wait_time < 100000) { + if (wait_time) { + if (wait_time < 100000) { to.tv_sec = 0; to.tv_usec = wait_time * 10; + } else { + to.tv_sec = wait_time / 100000; + to.tv_usec = (wait_time % 100000) * 10; } - else { - to.tv_sec = wait_time / 100000 ; - to.tv_usec = (wait_time % 100000) * 10 ; - } - } - else { + } else { to.tv_sec = 0; to.tv_usec = 0; } s = socket_can_read(&to); - if(s == 0) { - return 0; /* timeout */ + if (s == 0) { + return 0; /* timeout */ } /* Receive packet */ - result = receive_packet(s, /* socket */ - &recv_time, /* reply_timestamp */ - (struct sockaddr *) &response_addr, /* reply_src_addr */ - sizeof(response_addr), /* reply_src_addr_len */ - buffer, /* reply_buf */ - sizeof(buffer) /* reply_buf_len */ - ); - - if(result <= 0) { + result = receive_packet(s, /* socket */ + &recv_time, /* reply_timestamp */ + (struct sockaddr*)&response_addr, /* reply_src_addr */ + sizeof(response_addr), /* reply_src_addr_len */ + buffer, /* reply_buf */ + sizeof(buffer) /* reply_buf_len */ + ); + + if (result <= 0) { return 0; } - gettimeofday( ¤t_time, &tz ); + gettimeofday(¤t_time, &tz); /* Process ICMP packet and retrieve id/seq */ - if(response_addr.ss_family == AF_INET) { - if(!decode_icmp_ipv4( - (struct sockaddr *) &response_addr, - sizeof(response_addr), - buffer, - sizeof(buffer), - &id, - &seq) - ) { + if (response_addr.ss_family == AF_INET) { + if (!decode_icmp_ipv4( + (struct sockaddr*)&response_addr, + sizeof(response_addr), + buffer, + sizeof(buffer), + &id, + &seq)) { return 1; } } #ifdef IPV6 - else if(response_addr.ss_family == AF_INET6) { - if(!decode_icmp_ipv6( - (struct sockaddr *) &response_addr, - sizeof(response_addr), - buffer, - sizeof(buffer), - &id, - &seq) - ) { + else if (response_addr.ss_family == AF_INET6) { + if (!decode_icmp_ipv6( + (struct sockaddr*)&response_addr, + sizeof(response_addr), + buffer, + sizeof(buffer), + &id, + &seq)) { return 1; } } @@ -2055,12 +1975,12 @@ int wait_for_reply(long wait_time) return 1; } - if( id != ident ) { + if (id != ident) { return 1; /* packet received, but not the one we are looking for! */ } seqmap_value = seqmap_fetch(seq, ¤t_time); - if(seqmap_value == NULL) { + if (seqmap_value == NULL) { return 1; } @@ -2068,31 +1988,35 @@ int wait_for_reply(long wait_time) n = seqmap_value->host_nr; h = table[n]; - sent_time = &seqmap_value->ping_ts; + sent_time = &seqmap_value->ping_ts; this_count = seqmap_value->ping_count; - this_reply = timeval_diff( &recv_time, sent_time ); + this_reply = timeval_diff(&recv_time, sent_time); - if( loop_flag || h->resp_times[this_count] == RESP_WAITING ) - { + if (loop_flag || h->resp_times[this_count] == RESP_WAITING) { /* only for non-duplicates: */ h->waiting = 0; h->timeout = timeout; h->num_recv++; - if(h->discard_next_recv_i) { - h->discard_next_recv_i=0; - } - else { + if (h->discard_next_recv_i) { + h->discard_next_recv_i = 0; + } else { h->num_recv_i++; - if( !h->max_reply_i || this_reply > h->max_reply_i ) h->max_reply_i = this_reply; - if( !h->min_reply_i || this_reply < h->min_reply_i ) h->min_reply_i = this_reply; + if (!h->max_reply_i || this_reply > h->max_reply_i) + h->max_reply_i = this_reply; + if (!h->min_reply_i || this_reply < h->min_reply_i) + h->min_reply_i = this_reply; h->total_time_i += this_reply; } - if( !max_reply || this_reply > max_reply ) max_reply = this_reply; - if( !min_reply || this_reply < min_reply ) min_reply = this_reply; - if( !h->max_reply || this_reply > h->max_reply ) h->max_reply = this_reply; - if( !h->min_reply || this_reply < h->min_reply ) h->min_reply = this_reply; + if (!max_reply || this_reply > max_reply) + max_reply = this_reply; + if (!min_reply || this_reply < min_reply) + min_reply = this_reply; + if (!h->max_reply || this_reply > h->max_reply) + h->max_reply = this_reply; + if (!h->min_reply || this_reply < h->min_reply) + h->min_reply = this_reply; sum_replies += this_reply; h->total_time += this_reply; total_replies++; @@ -2101,113 +2025,99 @@ int wait_for_reply(long wait_time) /* received ping is cool, so process it */ h->num_recv_total++; -#if defined( DEBUG ) || defined( _DEBUG ) - if( trace_flag ) - printf( "received [%d] from %s\n", this_count, h->host ); +#if defined(DEBUG) || defined(_DEBUG) + if (trace_flag) + printf("received [%d] from %s\n", this_count, h->host); #endif /* DEBUG || _DEBUG */ /* note reply time in array, probably */ - if( !loop_flag ) - { - if( ( this_count >= 0 ) && ( this_count < trials ) ) - { - if( h->resp_times[this_count] >= 0 ) - { - if( !per_recv_flag ) - { - fprintf( stderr, "%s : duplicate for [%d], %d bytes, %s ms", - h->host, this_count, result, sprint_tm( this_reply ) ); - - if(addr_cmp((struct sockaddr *)&response_addr, (struct sockaddr *)&h->saddr)) { + if (!loop_flag) { + if ((this_count >= 0) && (this_count < trials)) { + if (h->resp_times[this_count] >= 0) { + if (!per_recv_flag) { + fprintf(stderr, "%s : duplicate for [%d], %d bytes, %s ms", + h->host, this_count, result, 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); + getnameinfo((struct sockaddr*)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); + fprintf(stderr, " [<- %s]", buf); } - fprintf( stderr, "\n" ); + fprintf(stderr, "\n"); - }/* IF */ - }/* IF */ + } + } else h->resp_times[this_count] = this_reply; - }/* IF */ - else - { + } + else { /* count is out of bounds?? */ - fprintf( stderr, "%s : duplicate for [%d], %d bytes, %s ms\n", - h->host, this_count, result, sprint_tm( this_reply ) ); + fprintf(stderr, "%s : duplicate for [%d], %d bytes, %s ms\n", + h->host, this_count, result, sprint_tm(this_reply)); - }/* ELSE */ - }/* IF */ + } + } - if( h->num_recv == 1 ) - { + if (h->num_recv == 1) { num_alive++; - if( verbose_flag || alive_flag ) - { - printf( "%s", h->host ); + if (verbose_flag || alive_flag) { + printf("%s", h->host); - if( verbose_flag ) - printf( " is alive" ); + if (verbose_flag) + printf(" is alive"); - if( elapsed_flag ) - printf( " (%s ms)", sprint_tm( this_reply ) ); + if (elapsed_flag) + printf(" (%s ms)", sprint_tm(this_reply)); - if(addr_cmp((struct sockaddr *)&response_addr, (struct sockaddr *)&h->saddr)) { + 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); + getnameinfo((struct sockaddr*)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); + fprintf(stderr, " [<- %s]", buf); } - printf( "\n" ); - - }/* IF */ - }/* IF */ + printf("\n"); + } + } - if( per_recv_flag ) - { - if(timestamp_flag) { + if (per_recv_flag) { + if (timestamp_flag) { printf("[%lu.%06lu] ", - (unsigned long)recv_time.tv_sec, - (unsigned long)recv_time.tv_usec); + (unsigned long)recv_time.tv_sec, + (unsigned long)recv_time.tv_usec); } avg = h->total_time / h->num_recv; - printf( "%s%s : [%d], %d bytes, %s ms", - h->host, h->pad, this_count, result, sprint_tm( this_reply ) ); - printf( " (%s avg, ", sprint_tm( avg ) ); + printf("%s%s : [%d], %d bytes, %s ms", + h->host, h->pad, this_count, result, sprint_tm(this_reply)); + printf(" (%s avg, ", sprint_tm(avg)); - if( h->num_recv <= h->num_sent ) { - printf( "%d%% loss)", - ( ( h->num_sent - h->num_recv ) * 100 ) / h->num_sent ); - - } - else { - printf( "%d%% return)", - ( h->num_recv_total * 100 ) / h->num_sent ); + if (h->num_recv <= h->num_sent) { + printf("%d%% loss)", + ((h->num_sent - h->num_recv) * 100) / h->num_sent); + } else { + printf("%d%% return)", + (h->num_recv_total * 100) / h->num_sent); } - if(addr_cmp((struct sockaddr *)&response_addr, (struct sockaddr *)&h->saddr)) { + 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); + getnameinfo((struct sockaddr*)&response_addr, sizeof(response_addr), buf, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); + fprintf(stderr, " [<- %s]", buf); } - printf( "\n" ); + printf("\n"); - }/* IF */ + } /* remove this job, if we are done */ - if((count_flag && h->num_recv >= count) || - (!loop_flag && !count_flag && h->num_recv)) - { + if ((count_flag && h->num_recv >= count) || (!loop_flag && !count_flag && h->num_recv)) { remove_job(h); } - fflush( stdout ); + fflush(stdout); return num_jobs; - -} /* wait_for_reply() */ +} /************************************************************ @@ -2225,24 +2135,24 @@ int wait_for_reply(long wait_time) ************************************************************/ -void add_name( char *name ) +void add_name(char* name) { - struct addrinfo *res0, *res, hints; - int ret_ga; - char *printname; - char namebuf[256]; - char addrbuf[256]; + struct addrinfo *res0, *res, hints; + int ret_ga; + char* printname; + char namebuf[256]; + char addrbuf[256]; /* getaddrinfo */ bzero(&hints, sizeof(struct addrinfo)); hints.ai_flags = 0; hints.ai_socktype = SOCK_RAW; hints.ai_family = hints_ai_family; - if(hints_ai_family == AF_INET) { + if (hints_ai_family == AF_INET) { hints.ai_protocol = IPPROTO_ICMP; } #ifdef IPV6 - else if(hints_ai_family == AF_INET6) { + else if (hints_ai_family == AF_INET6) { hints.ai_protocol = IPPROTO_ICMPV6; } #endif @@ -2251,7 +2161,7 @@ void add_name( char *name ) } ret_ga = getaddrinfo(name, NULL, &hints, &res0); if (ret_ga) { - if(!quiet_flag) + if (!quiet_flag) print_warning("%s: %s\n", name, gai_strerror(ret_ga)); num_noaddress++; return; @@ -2264,52 +2174,46 @@ void add_name( char *name ) */ for (res = res0; res; res = res->ai_next) { /* name_flag: addr -> name lookup requested) */ - if(!name_flag) { + if (!name_flag) { printname = name; - } - else { + } else { int ret; ret = getnameinfo(res->ai_addr, res->ai_addrlen, namebuf, - sizeof(namebuf)/sizeof(char), NULL, 0, 0); + sizeof(namebuf) / sizeof(char), NULL, 0, 0); if (ret) { - if(!quiet_flag) { - ret = getnameinfo(res->ai_addr, res->ai_addrlen, namebuf, - sizeof(namebuf)/sizeof(char), NULL, 0, 0); - print_warning("%s: can't reverse-lookup (%s)\n", name, gai_strerror(ret_ga)); + if (!quiet_flag) { + print_warning("%s: can't reverse-lookup (%s)\n", name, gai_strerror(ret)); } printname = name; - } - else { + } else { printname = namebuf; } } /* addr_flag: name -> addr lookup requested */ - if(addr_flag) { + if (addr_flag) { int ret; ret = getnameinfo(res->ai_addr, res->ai_addrlen, addrbuf, - sizeof(addrbuf)/sizeof(char), NULL, 0, NI_NUMERICHOST); + sizeof(addrbuf) / sizeof(char), NULL, 0, NI_NUMERICHOST); if (ret) { - if(!quiet_flag) { - print_warning("%s: can't forward-lookup address (%s)\n", name, gai_strerror(ret_ga)); + if (!quiet_flag) { + print_warning("%s: can't forward-lookup address (%s)\n", name, gai_strerror(ret)); } continue; } - if(name_flag) { + if (name_flag) { char nameaddrbuf[512]; - snprintf(nameaddrbuf, sizeof(nameaddrbuf)/sizeof(char), "%s (%s)", printname, addrbuf); + snprintf(nameaddrbuf, sizeof(nameaddrbuf) / sizeof(char), "%s (%s)", printname, addrbuf); add_addr(name, nameaddrbuf, res->ai_addr, res->ai_addrlen); - } - else { + } else { add_addr(name, addrbuf, res->ai_addr, res->ai_addrlen); } - } - else { + } else { add_addr(name, printname, res->ai_addr, res->ai_addrlen); } - if(!multif_flag) { + if (!multif_flag) { break; } } @@ -2330,16 +2234,16 @@ void add_name( char *name ) ************************************************************/ -void add_addr( char *name, char *host, struct sockaddr *ipaddr, socklen_t ipaddr_len ) +void add_addr(char* name, char* host, struct sockaddr* ipaddr, socklen_t ipaddr_len) { - HOST_ENTRY *p; + HOST_ENTRY* p; int n, *i; - p = ( HOST_ENTRY* )malloc( sizeof( HOST_ENTRY ) ); - if( !p ) - crash_and_burn( "can't allocate HOST_ENTRY" ); + p = (HOST_ENTRY*)malloc(sizeof(HOST_ENTRY)); + if (!p) + crash_and_burn("can't allocate HOST_ENTRY"); - memset( ( char* ) p, 0, sizeof( HOST_ENTRY ) ); + memset((char*)p, 0, sizeof(HOST_ENTRY)); p->name = strdup(name); p->host = strdup(host); @@ -2349,57 +2253,52 @@ void add_addr( char *name, char *host, struct sockaddr *ipaddr, socklen_t ipaddr p->running = 1; p->min_reply = 0; - if(netdata_flag) { - char *s = p->name; - while(*s) { - if(!isalnum(*s)) *s = '_'; + if (netdata_flag) { + char* s = p->name; + while (*s) { + if (!isalnum(*s)) + *s = '_'; s++; } } - if( strlen( p->host ) > max_hostname_len ) - max_hostname_len = strlen( p->host ); + if (strlen(p->host) > max_hostname_len) + max_hostname_len = strlen(p->host); /* array for response time results */ - if( !loop_flag ) - { - i = ( int* )malloc( trials * sizeof( int ) ); - if( !i ) - crash_and_burn( "can't allocate resp_times array" ); + if (!loop_flag) { + i = (int*)malloc(trials * sizeof(int)); + if (!i) + crash_and_burn("can't allocate resp_times array"); - for( n = 1; n < trials; n++ ) + for (n = 1; n < trials; n++) i[n] = RESP_UNUSED; p->resp_times = i; + } - }/* IF */ - -#if defined( DEBUG ) || defined( _DEBUG ) +#if defined(DEBUG) || defined(_DEBUG) /* likewise for sent times */ - if( sent_times_flag ) - { - i = ( int* )malloc( trials * sizeof( int ) ); - if( !i ) - crash_and_burn( "can't allocate sent_times array" ); + if (sent_times_flag) { + i = (int*)malloc(trials * sizeof(int)); + if (!i) + crash_and_burn("can't allocate sent_times array"); - for( n = 1; n < trials; n++ ) + for (n = 1; n < trials; n++) i[n] = RESP_UNUSED; p->sent_times = i; - - }/* IF */ + } #endif /* DEBUG || _DEBUG */ /* schedule first ping */ p->ev_type = EV_TYPE_PING; - p->ev_time.tv_sec = 0; + p->ev_time.tv_sec = 0; p->ev_time.tv_usec = 0; ev_enqueue(p); num_hosts++; - -} /* add_addr() */ - +} /************************************************************ @@ -2413,11 +2312,11 @@ void add_addr( char *name, char *host, struct sockaddr *ipaddr, socklen_t ipaddr ************************************************************/ -void remove_job( HOST_ENTRY *h ) +void remove_job(HOST_ENTRY* h) { -#if defined( DEBUG ) || defined( _DEBUG ) - if( trace_flag ) - printf( "removing job for %s\n", h->host ); +#if defined(DEBUG) || defined(_DEBUG) + if (trace_flag) + printf("removing job for %s\n", h->host); #endif /* DEBUG || _DEBUG */ h->running = 0; @@ -2425,8 +2324,7 @@ void remove_job( HOST_ENTRY *h ) --num_jobs; ev_remove(h); - -} /* remove_job() */ +} /************************************************************ @@ -2440,15 +2338,13 @@ void remove_job( HOST_ENTRY *h ) ************************************************************/ -void crash_and_burn( char *message ) +void crash_and_burn(char* message) { - if( verbose_flag ) - fprintf( stderr, "%s: %s\n", prog, message ); - - exit( 4 ); - -} /* crash_and_burn() */ + if (verbose_flag) + fprintf(stderr, "%s: %s\n", prog, message); + exit(4); +} /************************************************************ @@ -2462,12 +2358,11 @@ void crash_and_burn( char *message ) ************************************************************/ -void errno_crash_and_burn( char *message ) +void errno_crash_and_burn(char* message) { - fprintf( stderr, "%s: %s : %s\n", prog, message, strerror( errno ) ); - exit( 4 ); -} /* errno_crash_and_burn() */ - + fprintf(stderr, "%s: %s : %s\n", prog, message, strerror(errno)); + exit(4); +} /************************************************************ @@ -2477,17 +2372,16 @@ void errno_crash_and_burn( char *message ) *************************************************************/ -void print_warning(char *format, ...) { +void print_warning(char* format, ...) +{ va_list args; - if(!quiet_flag) { - va_start(args, format ); + if (!quiet_flag) { + va_start(args, format); vfprintf(stderr, format, args); va_end(args); } } - - /************************************************************ Function: timeval_diff @@ -2505,27 +2399,25 @@ void print_warning(char *format, ...) { ************************************************************/ -long timeval_diff( struct timeval *a, struct timeval *b ) +long timeval_diff(struct timeval* a, struct timeval* b) { long sec_diff = a->tv_sec - b->tv_sec; - if(sec_diff == 0) { + if (sec_diff == 0) { return (a->tv_usec - b->tv_usec) / 10; - } - else if(sec_diff < 100) { + } else if (sec_diff < 100) { return (sec_diff * 1000000 + a->tv_usec - b->tv_usec) / 10; - } - else { + } else { /* For such large differences, we don't really care about the microseconds... */ return sec_diff * 100000; } -} /* timeval_diff() */ +} /************************************************************ Function: timeval_add *************************************************************/ -void timeval_add(struct timeval *a, long t_10u) +void timeval_add(struct timeval* a, long t_10u) { t_10u *= 10; a->tv_sec += (t_10u + a->tv_usec) / 1000000; @@ -2549,35 +2441,30 @@ void timeval_add(struct timeval *a, long t_10u) ************************************************************/ -char * sprint_tm( int t ) +char* sprint_tm(int t) { static char buf[10]; - if( t < 0 ) { + if (t < 0) { /* negative (unexpected) */ - sprintf( buf, "%.2g", (double) t / 100 ); - } - else if( t < 100 ) { + sprintf(buf, "%.2g", (double)t / 100); + } else if (t < 100) { /* <= 0.99 ms */ - sprintf( buf, "0.%02d", t ); - } - else if( t < 1000 ) { + sprintf(buf, "0.%02d", t); + } else if (t < 1000) { /* 1.00 - 9.99 ms */ - sprintf( buf, "%d.%02d", t / 100, t % 100 ); - } - else if( t < 10000 ) { + sprintf(buf, "%d.%02d", t / 100, t % 100); + } else if (t < 10000) { /* 10.0 - 99.9 ms */ - sprintf( buf, "%d.%d", t / 100, ( t % 100 ) / 10 ); - } - else if( t < 100000000 ) { + sprintf(buf, "%d.%d", t / 100, (t % 100) / 10); + } else if (t < 100000000) { /* 100 - 1'000'000 ms */ - sprintf( buf, "%d", t / 100 ); - } - else { - sprintf( buf, "%.2e", (double) (t / 100) ); + sprintf(buf, "%d", t / 100); + } else { + sprintf(buf, "%.2e", (double)(t / 100)); } - return( buf ); + return (buf); } /************************************************************ @@ -2585,19 +2472,17 @@ char * sprint_tm( int t ) Function: addr_cmp *************************************************************/ -int addr_cmp(struct sockaddr *a, struct sockaddr *b) +int addr_cmp(struct sockaddr* a, struct sockaddr* b) { - if(a->sa_family != b->sa_family) { + if (a->sa_family != b->sa_family) { return a->sa_family - b->sa_family; - } - else { - if(a->sa_family == AF_INET) { - return ((struct sockaddr_in *) a)->sin_addr.s_addr - ((struct sockaddr_in *) b)->sin_addr.s_addr; - } - else if(a->sa_family == AF_INET6) { - return memcmp(&((struct sockaddr_in6 *) a)->sin6_addr, - &((struct sockaddr_in6 *) b)->sin6_addr, - sizeof(((struct sockaddr_in6 *) a)->sin6_addr)); + } else { + if (a->sa_family == AF_INET) { + return ((struct sockaddr_in*)a)->sin_addr.s_addr - ((struct sockaddr_in*)b)->sin_addr.s_addr; + } else if (a->sa_family == AF_INET6) { + return memcmp(&((struct sockaddr_in6*)a)->sin6_addr, + &((struct sockaddr_in6*)b)->sin6_addr, + sizeof(((struct sockaddr_in6*)a)->sin6_addr)); } } @@ -2619,20 +2504,20 @@ int addr_cmp(struct sockaddr *a, struct sockaddr *b) than the others. *************************************************************/ -void ev_enqueue(HOST_ENTRY *h) +void ev_enqueue(HOST_ENTRY* h) { - HOST_ENTRY *i; - HOST_ENTRY *i_prev; + HOST_ENTRY* i; + HOST_ENTRY* i_prev; -#if defined( DEBUG ) || defined( _DEBUG ) - if( trace_flag ) { +#if defined(DEBUG) || defined(_DEBUG) + if (trace_flag) { long st = timeval_diff(&h->ev_time, ¤t_time); - fprintf(stderr, "Enqueue: host=%s, when=%d ms (%d, %d)\n", h->host, st/100, h->ev_time.tv_sec, h->ev_time.tv_usec); + fprintf(stderr, "Enqueue: host=%s, when=%d ms (%d, %d)\n", h->host, st / 100, h->ev_time.tv_sec, h->ev_time.tv_usec); } #endif /* Empty list */ - if(ev_last == NULL) { + if (ev_last == NULL) { h->ev_next = NULL; h->ev_prev = NULL; ev_first = h; @@ -2641,10 +2526,7 @@ void ev_enqueue(HOST_ENTRY *h) } /* Insert on tail? */ - if(h->ev_time.tv_sec > ev_last->ev_time.tv_sec || - (h->ev_time.tv_sec == ev_last->ev_time.tv_sec && - h->ev_time.tv_usec >= ev_last->ev_time.tv_usec)) - { + if (h->ev_time.tv_sec > ev_last->ev_time.tv_sec || (h->ev_time.tv_sec == ev_last->ev_time.tv_sec && h->ev_time.tv_usec >= ev_last->ev_time.tv_usec)) { h->ev_next = NULL; h->ev_prev = ev_last; ev_last->ev_next = h; @@ -2654,20 +2536,15 @@ void ev_enqueue(HOST_ENTRY *h) /* Find insertion point */ i = ev_last; - while(1) { + while (1) { i_prev = i->ev_prev; - if(i_prev == NULL || - h->ev_time.tv_sec > i_prev->ev_time.tv_sec || - (h->ev_time.tv_sec == i_prev->ev_time.tv_sec && - h->ev_time.tv_usec >= i_prev->ev_time.tv_usec)) - { + if (i_prev == NULL || h->ev_time.tv_sec > i_prev->ev_time.tv_sec || (h->ev_time.tv_sec == i_prev->ev_time.tv_sec && h->ev_time.tv_usec >= i_prev->ev_time.tv_usec)) { h->ev_prev = i_prev; h->ev_next = i; i->ev_prev = h; - if(i_prev != NULL) { + if (i_prev != NULL) { i_prev->ev_next = h; - } - else { + } else { ev_first = h; } return; @@ -2681,11 +2558,11 @@ void ev_enqueue(HOST_ENTRY *h) Function: ev_dequeue *************************************************************/ -HOST_ENTRY *ev_dequeue() +HOST_ENTRY* ev_dequeue() { - HOST_ENTRY *dequeued; + HOST_ENTRY* dequeued; - if(ev_first == NULL) { + if (ev_first == NULL) { return NULL; } dequeued = ev_first; @@ -2699,25 +2576,24 @@ HOST_ENTRY *ev_dequeue() Function: ev_remove *************************************************************/ -void ev_remove(HOST_ENTRY *h) +void ev_remove(HOST_ENTRY* h) { - if(ev_first == h) { + if (ev_first == h) { ev_first = h->ev_next; } - if(ev_last == h) { + if (ev_last == h) { ev_last = h->ev_prev; } - if(h->ev_prev) { + if (h->ev_prev) { h->ev_prev->ev_next = h->ev_next; } - if(h->ev_next) { + if (h->ev_next) { h->ev_next->ev_prev = h->ev_prev; } h->ev_prev = NULL; h->ev_next = NULL; } - /************************************************************ Function: usage @@ -2732,48 +2608,48 @@ void ev_remove(HOST_ENTRY *h) void usage(int is_error) { - FILE *out = is_error ? stderr : stdout; - fprintf(out, "\n" ); - fprintf(out, "Usage: %s [options] [targets...]\n", prog ); - fprintf(out, " -4 only use IPv4 addresses\n" ); - fprintf(out, " -6 only use IPv6 addresses\n" ); - fprintf(out, " -a show targets that are alive\n" ); - fprintf(out, " -A show targets by address\n" ); + FILE* out = is_error ? stderr : stdout; + fprintf(out, "\n"); + fprintf(out, "Usage: %s [options] [targets...]\n", prog); + fprintf(out, " -4 only use IPv4 addresses\n"); + fprintf(out, " -6 only use IPv6 addresses\n"); + fprintf(out, " -a show targets that are alive\n"); + fprintf(out, " -A show targets by address\n"); fprintf(out, " -b n amount of ping data to send, in bytes (default %d)\n", DEFAULT_PING_DATA_SIZE); - fprintf(out, " -B f set exponential backoff factor to f\n" ); - fprintf(out, " -c n count of pings to send to each target (default %d)\n", count ); - fprintf(out, " -C n same as -c, report results in verbose format\n" ); - fprintf(out, " -D print timestamp before each output line\n" ); - fprintf(out, " -e show elapsed time on return packets\n" ); - fprintf(out, " -f file read list of targets from a file ( - means stdin) (only if no -g specified)\n" ); - fprintf(out, " -g generate target list (only if no -f specified)\n" ); - fprintf(out, " (specify the start and end IP in the target list, or supply a IP netmask)\n" ); - fprintf(out, " (ex. %s -g 192.168.1.0 192.168.1.255 or %s -g 192.168.1.0/24)\n", prog, prog ); + fprintf(out, " -B f set exponential backoff factor to f\n"); + fprintf(out, " -c n count of pings to send to each target (default %d)\n", count); + fprintf(out, " -C n same as -c, report results in verbose format\n"); + fprintf(out, " -D print timestamp before each output line\n"); + fprintf(out, " -e show elapsed time on return packets\n"); + fprintf(out, " -f file read list of targets from a file ( - means stdin) (only if no -g specified)\n"); + fprintf(out, " -g generate target list (only if no -f specified)\n"); + fprintf(out, " (specify the start and end IP in the target list, or supply a IP netmask)\n"); + fprintf(out, " (ex. %s -g 192.168.1.0 192.168.1.255 or %s -g 192.168.1.0/24)\n", prog, prog); fprintf(out, " -H n Set the IP TTL value (Time To Live hops)\n"); - fprintf(out, " -i n interval between sending ping packets (in millisec) (default %d)\n", interval / 100 ); + fprintf(out, " -i n interval between sending ping packets (in millisec) (default %d)\n", interval / 100); #ifdef SO_BINDTODEVICE - fprintf(out, " -I if bind to a particular interface\n"); + fprintf(out, " -I if bind to a particular interface\n"); #endif - fprintf(out, " -l loop sending pings forever\n" ); - fprintf(out, " -m use all IPs of provided hostnames (e.g. IPv4 and IPv6), use with -A\n" ); - fprintf(out, " -M set the Don't Fragment flag\n" ); - fprintf(out, " -n show targets by name (-d is equivalent)\n" ); - fprintf(out, " -N output compatible for netdata (-l -Q are required)\n" ); - fprintf(out, " -o show the accumulated outage time (lost packets * packet interval)\n" ); - fprintf(out, " -O n set the type of service (tos) flag on the ICMP packets\n" ); - fprintf(out, " -p n interval between ping packets to one target (in millisec)\n" ); - fprintf(out, " (in looping and counting modes, default %d)\n", perhost_interval / 100 ); - fprintf(out, " -q quiet (don't show per-target/per-ping results)\n" ); - fprintf(out, " -Q n same as -q, but show summary every n seconds\n" ); - fprintf(out, " -r n number of retries (default %d)\n", DEFAULT_RETRY ); - fprintf(out, " -R random packet data (to foil link data compression)\n" ); - fprintf(out, " -s print final stats\n" ); - fprintf(out, " -S addr set source address\n" ); - fprintf(out, " -t n individual target initial timeout (in millisec) (default %d)\n", timeout / 100 ); + fprintf(out, " -l loop sending pings forever\n"); + fprintf(out, " -m use all IPs of provided hostnames (e.g. IPv4 and IPv6), use with -A\n"); + fprintf(out, " -M set the Don't Fragment flag\n"); + fprintf(out, " -n show targets by name (-d is equivalent)\n"); + fprintf(out, " -N output compatible for netdata (-l -Q are required)\n"); + fprintf(out, " -o show the accumulated outage time (lost packets * packet interval)\n"); + fprintf(out, " -O n set the type of service (tos) flag on the ICMP packets\n"); + fprintf(out, " -p n interval between ping packets to one target (in millisec)\n"); + fprintf(out, " (in looping and counting modes, default %d)\n", perhost_interval / 100); + fprintf(out, " -q quiet (don't show per-target/per-ping results)\n"); + fprintf(out, " -Q n same as -q, but show summary every n seconds\n"); + fprintf(out, " -r n number of retries (default %d)\n", DEFAULT_RETRY); + fprintf(out, " -R random packet data (to foil link data compression)\n"); + fprintf(out, " -s print final stats\n"); + fprintf(out, " -S addr set source address\n"); + fprintf(out, " -t n individual target initial timeout (in millisec) (default %d)\n", timeout / 100); fprintf(out, " -T n ignored (for compatibility with fping 2.4)\n"); - fprintf(out, " -u show targets that are unreachable\n" ); - fprintf(out, " -v show version\n" ); - fprintf(out, " targets list of targets to check (if no -f specified)\n" ); + fprintf(out, " -u show targets that are unreachable\n"); + fprintf(out, " -v show version\n"); + fprintf(out, " targets list of targets to check (if no -f specified)\n"); fprintf(out, "\n"); exit(is_error); -} /* usage() */ +} diff --git a/src/seqmap.c b/src/seqmap.c index 20f9825..3cff4c0 100644 --- a/src/seqmap.c +++ b/src/seqmap.c @@ -1,4 +1,4 @@ -/* +/* * fping: fast-ping, file-ping, favorite-ping, funky-ping * * Ping a list of target hosts in a round robin fashion. @@ -22,8 +22,8 @@ * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed - * by Stanford University. The name of the University may not be used - * to endorse or promote products derived from this software without + * by Stanford University. The name of the University may not be used + * to endorse or promote products derived from this software without * specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED @@ -36,11 +36,11 @@ */ #include "seqmap.h" -#include "options.h" #include "limits.h" +#include "options.h" -#include #include +#include /* description of the data structure used: * @@ -51,26 +51,26 @@ * - when entering a value, we check that the current entry is expired */ -static SEQMAP_VALUE *seqmap_map = NULL; +static SEQMAP_VALUE* seqmap_map = NULL; static unsigned int seqmap_next_id = 0; -#define SEQMAP_TIMEOUT_IN_S 10 +#define SEQMAP_TIMEOUT_IN_S 10 #define SEQMAP_UNASSIGNED_HOST_NR UINT_MAX void seqmap_init() { seqmap_map = calloc(SEQMAP_MAXSEQ, sizeof(SEQMAP_VALUE)); - if(seqmap_map == NULL) { + if (seqmap_map == NULL) { perror("malloc error (can't allocate seqmap_map)"); } } -unsigned int seqmap_add(unsigned int host_nr, unsigned int ping_count, struct timeval *now) +unsigned int seqmap_add(unsigned int host_nr, unsigned int ping_count, struct timeval* now) { unsigned int current_id; - SEQMAP_VALUE *next_value; + SEQMAP_VALUE* next_value; - if(!seqmap_map) { + if (!seqmap_map) { fprintf(stderr, "fping internal error: seqmap not initialized.\n"); exit(4); } @@ -78,7 +78,7 @@ unsigned int seqmap_add(unsigned int host_nr, unsigned int ping_count, struct ti /* check if expired (note that unused seqmap values will have fields set to * 0, so will be seen as expired */ next_value = &seqmap_map[seqmap_next_id]; - if(next_value->ping_ts.tv_sec != 0 && (now->tv_sec - next_value->ping_ts.tv_sec) < SEQMAP_TIMEOUT_IN_S) { + if (next_value->ping_ts.tv_sec != 0 && (now->tv_sec - next_value->ping_ts.tv_sec) < SEQMAP_TIMEOUT_IN_S) { fprintf(stderr, "fping error: not enough sequence numbers available! (expire_timeout=%d, host_nr=%d, ping_count=%d, seqmap_next_id=%d)\n", SEQMAP_TIMEOUT_IN_S, host_nr, ping_count, seqmap_next_id); exit(4); @@ -97,18 +97,18 @@ unsigned int seqmap_add(unsigned int host_nr, unsigned int ping_count, struct ti return current_id; } -SEQMAP_VALUE *seqmap_fetch(unsigned int id, struct timeval *now) +SEQMAP_VALUE* seqmap_fetch(unsigned int id, struct timeval* now) { - SEQMAP_VALUE *value; + SEQMAP_VALUE* value; - if(id > SEQMAP_MAXSEQ) { + if (id > SEQMAP_MAXSEQ) { return NULL; } value = &seqmap_map[id]; /* verify that value is not expired */ - if(now->tv_sec - value->ping_ts.tv_sec >= SEQMAP_TIMEOUT_IN_S) { + if (now->tv_sec - value->ping_ts.tv_sec >= SEQMAP_TIMEOUT_IN_S) { return NULL; } diff --git a/src/socket4.c b/src/socket4.c index 6d8782b..d613059 100644 --- a/src/socket4.c +++ b/src/socket4.c @@ -1,4 +1,4 @@ -/* +/* * fping: fast-ping, file-ping, favorite-ping, funky-ping * * Ping a list of target hosts in a round robin fashion. @@ -22,47 +22,47 @@ * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed - * by Stanford University. The name of the University may not be used - * to endorse or promote products derived from this software without + * by Stanford University. The name of the University may not be used + * to endorse or promote products derived from this software without * specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ -#include "fping.h" #include "config.h" +#include "fping.h" -#include +#include +#include #include -#include #include +#include #include -#include #include -#include #include -#include +#include +#include -char *ping_buffer_ipv4 = 0; +char* ping_buffer_ipv4 = 0; size_t ping_pkt_size_ipv4; int open_ping_socket_ipv4() { - struct protoent *proto; + struct protoent* proto; int s; /* confirm that ICMP is available on this machine */ - if( ( proto = getprotobyname( "icmp" ) ) == NULL ) - crash_and_burn( "icmp: unknown protocol" ); + if ((proto = getprotobyname("icmp")) == NULL) + crash_and_burn("icmp: unknown protocol"); /* create raw socket for ICMP calls (ping) */ - s = socket( AF_INET, SOCK_RAW, proto->p_proto ); - if( s < 0 ) { + s = socket(AF_INET, SOCK_RAW, proto->p_proto); + if (s < 0) { /* try non-privileged icmp (works on Mac OSX without privileges, for example) */ - s = socket( AF_INET, SOCK_DGRAM, proto->p_proto ); - if( s < 0 ) { - errno_crash_and_burn( "can't create socket (must run as root?)" ); + s = socket(AF_INET, SOCK_DGRAM, proto->p_proto); + if (s < 0) { + errno_crash_and_burn("can't create socket (must run as root?)"); } } @@ -70,10 +70,10 @@ int open_ping_socket_ipv4() { int flags; - if((flags = fcntl(s, F_GETFL, 0)) < 0) + if ((flags = fcntl(s, F_GETFL, 0)) < 0) perror("fcntl"); - if(fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) + if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) perror("fcntl"); } @@ -84,45 +84,45 @@ void init_ping_buffer_ipv4(size_t ping_data_size) { /* allocate ping buffer */ ping_pkt_size_ipv4 = ping_data_size + ICMP_MINLEN; - ping_buffer_ipv4 = (char *) calloc(1, ping_pkt_size_ipv4); - if(!ping_buffer_ipv4) - crash_and_burn( "can't malloc ping packet" ); + ping_buffer_ipv4 = (char*)calloc(1, ping_pkt_size_ipv4); + if (!ping_buffer_ipv4) + crash_and_burn("can't malloc ping packet"); } -void socket_set_src_addr_ipv4(int s, struct in_addr *src_addr) +void socket_set_src_addr_ipv4(int s, struct in_addr* src_addr) { struct sockaddr_in sa; - memset( &sa, 0, sizeof( sa ) ); + memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_addr = *src_addr; - if ( bind( s, (struct sockaddr *)&sa, sizeof( sa ) ) < 0 ) - errno_crash_and_burn( "cannot bind source address" ); + if (bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0) + errno_crash_and_burn("cannot bind source address"); } -unsigned short calcsum(unsigned short *buffer, int length) +unsigned short calcsum(unsigned short* buffer, int length) { unsigned long sum; /* initialize sum to zero and loop until length (in words) is 0 */ - for (sum=0; length>1; length-=2) /* sizeof() returns number of bytes, we're interested in number of words */ - sum += *buffer++; /* add 1 word of buffer to sum and proceed to the next */ + for (sum = 0; length > 1; length -= 2) /* sizeof() returns number of bytes, we're interested in number of words */ + sum += *buffer++; /* add 1 word of buffer to sum and proceed to the next */ /* we may have an extra byte */ - if (length==1) - sum += (char)*buffer; + if (length == 1) + sum += (char)*buffer; sum = (sum >> 16) + (sum & 0xFFFF); /* add high 16 to low 16 */ - sum += (sum >> 16); /* add carry */ + sum += (sum >> 16); /* add carry */ return ~sum; } -int socket_sendto_ping_ipv4(int s, struct sockaddr *saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr) +int socket_sendto_ping_ipv4(int s, struct sockaddr* saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr) { - struct icmp *icp; + struct icmp* icp; int n; - icp = (struct icmp *) ping_buffer_ipv4; + icp = (struct icmp*)ping_buffer_ipv4; icp->icmp_type = ICMP_ECHO; icp->icmp_code = 0; @@ -131,12 +131,12 @@ int socket_sendto_ping_ipv4(int s, struct sockaddr *saddr, socklen_t saddr_len, icp->icmp_id = htons(icmp_id_nr); if (random_data_flag) { - for (n = ((char*)&icp->icmp_data - (char *)icp); n < ping_pkt_size_ipv4; ++n) { + for (n = ((char*)&icp->icmp_data - (char*)icp); n < ping_pkt_size_ipv4; ++n) { ping_buffer_ipv4[n] = random() & 0xFF; } } - icp->icmp_cksum = calcsum((unsigned short *) icp, ping_pkt_size_ipv4); + icp->icmp_cksum = calcsum((unsigned short*)icp, ping_pkt_size_ipv4); n = sendto(s, icp, ping_pkt_size_ipv4, 0, saddr, saddr_len); diff --git a/src/socket6.c b/src/socket6.c index 1a2f27f..03f2ade 100644 --- a/src/socket6.c +++ b/src/socket6.c @@ -1,4 +1,4 @@ -/* +/* * fping: fast-ping, file-ping, favorite-ping, funky-ping * * Ping a list of target hosts in a round robin fashion. @@ -22,46 +22,46 @@ * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed - * by Stanford University. The name of the University may not be used - * to endorse or promote products derived from this software without + * by Stanford University. The name of the University may not be used + * to endorse or promote products derived from this software without * specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ -#include "fping.h" #include "config.h" +#include "fping.h" -#include -#include +#include #include +#include #include -#include #include -#include +#include +#include -#include +#include -char *ping_buffer_ipv6 = 0; +char* ping_buffer_ipv6 = 0; size_t ping_pkt_size_ipv6; int open_ping_socket_ipv6() { - struct protoent *proto; + struct protoent* proto; int s; /* confirm that ICMP is available on this machine */ - if( ( proto = getprotobyname( "ipv6-icmp" ) ) == NULL ) - crash_and_burn( "icmp: unknown protocol" ); + if ((proto = getprotobyname("ipv6-icmp")) == NULL) + crash_and_burn("icmp: unknown protocol"); /* create raw socket for ICMP calls (ping) */ - s = socket( AF_INET6, SOCK_RAW, proto->p_proto ); - if( s < 0 ) { + s = socket(AF_INET6, SOCK_RAW, proto->p_proto); + if (s < 0) { /* try non-privileged icmp (works on Mac OSX without privileges, for example) */ - s = socket( AF_INET6, SOCK_DGRAM, proto->p_proto ); - if( s < 0 ) { - errno_crash_and_burn( "can't create raw socket (must run as root?)" ); + s = socket(AF_INET6, SOCK_DGRAM, proto->p_proto); + if (s < 0) { + errno_crash_and_burn("can't create raw socket (must run as root?)"); } } @@ -69,10 +69,10 @@ int open_ping_socket_ipv6() { int flags; - if((flags = fcntl(s, F_GETFL, 0)) < 0) + if ((flags = fcntl(s, F_GETFL, 0)) < 0) perror("fcntl"); - if(fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) + if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) perror("fcntl"); } @@ -83,32 +83,32 @@ void init_ping_buffer_ipv6(size_t ping_data_size) { /* allocate ping buffer */ ping_pkt_size_ipv6 = ping_data_size + sizeof(struct icmp6_hdr); - ping_buffer_ipv6 = (char *) calloc(1, ping_pkt_size_ipv6); - if(!ping_buffer_ipv6) - crash_and_burn( "can't malloc ping packet" ); + ping_buffer_ipv6 = (char*)calloc(1, ping_pkt_size_ipv6); + if (!ping_buffer_ipv6) + crash_and_burn("can't malloc ping packet"); } -void socket_set_src_addr_ipv6(int s, struct in6_addr *src_addr) +void socket_set_src_addr_ipv6(int s, struct in6_addr* src_addr) { struct sockaddr_in6 sa; - memset( &sa, 0, sizeof( sa ) ); + memset(&sa, 0, sizeof(sa)); sa.sin6_family = AF_INET6; sa.sin6_addr = *src_addr; - if ( bind( s, (struct sockaddr *)&sa, sizeof( sa ) ) < 0 ) - errno_crash_and_burn( "cannot bind source address" ); + if (bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0) + errno_crash_and_burn("cannot bind source address"); } -int socket_sendto_ping_ipv6(int s, struct sockaddr *saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr) +int socket_sendto_ping_ipv6(int s, struct sockaddr* saddr, socklen_t saddr_len, uint16_t icmp_seq_nr, uint16_t icmp_id_nr) { - struct icmp6_hdr *icp; + struct icmp6_hdr* icp; int n; - icp = (struct icmp6_hdr *) ping_buffer_ipv6; - icp->icmp6_type = ICMP6_ECHO_REQUEST; - icp->icmp6_code = 0; - icp->icmp6_seq = htons(icmp_seq_nr); - icp->icmp6_id = htons(icmp_id_nr); + icp = (struct icmp6_hdr*)ping_buffer_ipv6; + icp->icmp6_type = ICMP6_ECHO_REQUEST; + icp->icmp6_code = 0; + icp->icmp6_seq = htons(icmp_seq_nr); + icp->icmp6_id = htons(icmp_id_nr); if (random_data_flag) { for (n = sizeof(struct icmp6_hdr); n < ping_pkt_size_ipv6; ++n) { @@ -116,7 +116,7 @@ int socket_sendto_ping_ipv6(int s, struct sockaddr *saddr, socklen_t saddr_len, } } - icp->icmp6_cksum = 0; /* The IPv6 stack calculates the checksum for us... */ + icp->icmp6_cksum = 0; /* The IPv6 stack calculates the checksum for us... */ n = sendto(s, icp, ping_pkt_size_ipv6, 0, saddr, saddr_len);