From eafee9c3f91879afa82749fa1d8cd2b0b53a5d5c Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 28 Apr 2025 22:01:01 +0200 Subject: WIP: check_icmp refactor --- plugins-root/check_icmp.c | 1287 +++++++++++++++++++++++---------------------- 1 file changed, 659 insertions(+), 628 deletions(-) (limited to 'plugins-root/check_icmp.c') diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c index 061e7d82..87dac21d 100644 --- a/plugins-root/check_icmp.c +++ b/plugins-root/check_icmp.c @@ -67,6 +67,10 @@ const char *email = "devel@monitoring-plugins.org"; #include #include +#include "../lib/states.h" +#include "./check_icmp.d/config.h" +#include "./check_icmp.d/check_icmp_helpers.h" + /** sometimes undefined system macros (quite a few, actually) **/ #ifndef MAXTTL # define MAXTTL 255 @@ -97,56 +101,8 @@ const char *email = "devel@monitoring-plugins.org"; # define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 #endif -typedef unsigned short range_t; /* type for get_range() -- unimplemented */ - -typedef struct rta_host { - unsigned short id; /* id in **table, and icmp pkts */ - char *name; /* arg used for adding this host */ - char *msg; /* icmp error message, if any */ - struct sockaddr_storage saddr_in; /* the address of this host */ - struct sockaddr_storage error_addr; /* stores address of error replies */ - unsigned long long time_waited; /* total time waited, in usecs */ - unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */ - unsigned char icmp_type, icmp_code; /* type and code from errors */ - unsigned short flags; /* control/status flags */ - double rta; /* measured RTA */ - int rta_status; // check result for RTA checks - double rtmax; /* max rtt */ - double rtmin; /* min rtt */ - double jitter; /* measured jitter */ - int jitter_status; // check result for Jitter checks - double jitter_max; /* jitter rtt maximum */ - double jitter_min; /* jitter rtt minimum */ - double EffectiveLatency; - double mos; /* Mean opnion score */ - int mos_status; // check result for MOS checks - double score; /* score */ - int score_status; // check result for score checks - u_int last_tdiff; - u_int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */ - unsigned char pl; /* measured packet loss */ - int pl_status; // check result for packet loss checks - struct rta_host *next; /* linked list */ - int order_status; // check result for packet order checks -} rta_host; - #define FLAG_LOST_CAUSE 0x01 /* decidedly dead target. */ -/* threshold structure. all values are maximum allowed, exclusive */ -typedef struct threshold { - unsigned char pl; /* max allowed packet loss in percent */ - unsigned int rta; /* roundtrip time average, microseconds */ - double jitter; /* jitter time average, microseconds */ - double mos; /* MOS */ - double score; /* Score */ -} threshold; - -/* the data structure */ -typedef struct icmp_ping_data { - struct timeval stime; /* timestamp (saved in protocol struct as well) */ - unsigned short ping_id; -} icmp_ping_data; - typedef union ip_hdr { struct ip ip; struct ip6_hdr ip6; @@ -159,24 +115,6 @@ typedef union icmp_packet { u_short *cksum_in; } icmp_packet; -/* the different modes of this program are as follows: - * MODE_RTA: send all packets no matter what (mimic check_icmp and check_ping) - * MODE_HOSTCHECK: Return immediately upon any sign of life - * In addition, sends packets to ALL addresses assigned - * to this host (as returned by gethostbyname() or - * gethostbyaddr() and expects one host only to be checked at - * a time. Therefore, any packet response what so ever will - * count as a sign of life, even when received outside - * crit.rta limit. Do not misspell any additional IP's. - * MODE_ALL: Requires packets from ALL requested IP to return OK (default). - * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without - * tcp and udp args does this) - */ -#define MODE_RTA 0 -#define MODE_HOSTCHECK 1 -#define MODE_ALL 2 -#define MODE_ICMP 3 - enum enum_threshold_mode { const_rta_mode, const_packet_loss_mode, @@ -187,78 +125,325 @@ enum enum_threshold_mode { typedef enum enum_threshold_mode threshold_mode; -/* the different ping types we can do - * TODO: investigate ARP ping as well */ -#define HAVE_ICMP 1 -#define HAVE_UDP 2 -#define HAVE_TCP 4 -#define HAVE_ARP 8 - -#define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data) -#define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */ -#define IP_HDR_SIZE 20 -#define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN) -#define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44) - -/* various target states */ -#define TSTATE_INACTIVE 0x01 /* don't ping this host anymore */ -#define TSTATE_WAITING 0x02 /* unanswered packets on the wire */ -#define TSTATE_ALIVE 0x04 /* target is alive (has answered something) */ -#define TSTATE_UNREACH 0x08 - /** prototypes **/ -void print_help(void); +void print_help(); void print_usage(void); -static u_int get_timevar(const char * /*str*/); -static u_int get_timevaldiff(struct timeval * /*early*/, struct timeval * /*later*/); + +/* Time related */ +static unsigned int get_timevar(const char * /*str*/); +static time_t get_timevaldiff(struct timeval * /*early*/, struct timeval * /*later*/, + struct timeval *prog_start); + static in_addr_t get_ip_address(const char * /*ifname*/); -static int wait_for_reply(int /*sock*/, u_int /*t*/); -static int recvfrom_wto(int /*sock*/, void * /*buf*/, unsigned int /*len*/, struct sockaddr * /*saddr*/, u_int * /*timo*/, - struct timeval * /*tv*/); -static int send_icmp_ping(int /*sock*/, struct rta_host * /*host*/); +static void set_source_ip(char * /*arg*/, const int icmp_sock); + +/* Receiving data */ +static int wait_for_reply(int /*sock*/, unsigned int /*t*/, bool order_mode, bool mos_mode, + bool rta_mode, bool pl_mode, bool jitter_mode, bool score_mode, + int min_hosts_alive, unsigned short icmp_pkt_size, + unsigned int *pkt_interval, unsigned int *target_interval, threshold warn, + threshold crit, pid_t pid, int mode, + unsigned long long max_completion_time, struct timeval *prog_start, + struct rta_host **table, const unsigned short packets, + const int icmp_sock, const unsigned short number_of_targets, + check_icmp_state *program_state); + +static int recvfrom_wto(int /*sock*/, void * /*buf*/, unsigned int /*len*/, + struct sockaddr * /*saddr*/, unsigned int * /*timo*/, + struct timeval * /*tv*/, struct timeval *prog_start); +static int handle_random_icmp(unsigned char * /*packet*/, struct sockaddr_storage * /*addr*/, + unsigned int *pkt_interval, unsigned int *target_interval, pid_t pid, + struct rta_host **table, unsigned short packets, + const unsigned short number_of_targets, + check_icmp_state *program_state); + +/* Sending data */ +static int send_icmp_ping(int /*sock*/, struct rta_host * /*host*/, unsigned short icmp_pkt_size, + pid_t pid, check_icmp_state *program_state); + +/* Threshold related */ static int get_threshold(char *str, threshold *th); -static bool get_threshold2(char *str, size_t length, threshold * /*warn*/, threshold * /*crit*/, threshold_mode mode); +static bool get_threshold2(char *str, size_t length, threshold * /*warn*/, threshold * /*crit*/, + threshold_mode mode); static bool parse_threshold2_helper(char *s, size_t length, threshold *thr, threshold_mode mode); -static void run_checks(void); -static void set_source_ip(char * /*arg*/); -static int add_target(char * /*arg*/); + +/* main test function */ +static void run_checks(bool order_mode, bool mos_mode, bool rta_mode, bool pl_mode, + bool jitter_mode, bool score_mode, int min_hosts_alive, + unsigned short icmp_pkt_size, unsigned int *pkt_interval, + unsigned int *target_interval, threshold warn, threshold crit, pid_t pid, + int mode, unsigned int max_completion_time, const struct timeval *prog_start, + struct rta_host **table, const unsigned short packets, const int icmp_sock, + const unsigned short number_of_targets, check_icmp_state *program_state); + +/* Target aquisition */ +static int add_target(char * /*arg*/, int mode); static int add_target_ip(char * /*arg*/, struct sockaddr_storage * /*in*/); -static int handle_random_icmp(unsigned char * /*packet*/, struct sockaddr_storage * /*addr*/); + static void parse_address(struct sockaddr_storage * /*addr*/, char * /*address*/, int /*size*/); + static unsigned short icmp_checksum(uint16_t * /*p*/, size_t /*n*/); -static void finish(int /*sig*/); + +/* End of run function */ +static void finish(int /*sig*/, bool order_mode, bool mos_mode, bool rta_mode, bool pl_mode, + bool jitter_mode, bool score_mode, int min_hosts_alive, threshold warn, + threshold crit, const int icmp_sock, const unsigned short number_of_targets, + check_icmp_state *program_state); + +/* Error exit */ static void crash(const char * /*fmt*/, ...); /** global variables **/ -static struct rta_host **table, *cursor, *list; - -static threshold crit = {.pl = 80, .rta = 500000, .jitter = 0.0, .mos = 0.0, .score = 0.0}; -static threshold warn = {.pl = 40, .rta = 200000, .jitter = 0.0, .mos = 0.0, .score = 0.0}; - -static int mode, protocols, sockets, debug = 0, timeout = 10; -static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE; -static unsigned short icmp_pkt_size = DEFAULT_PING_DATA_SIZE + ICMP_MINLEN; - -static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0, ttl = 0; -#define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost)) -static unsigned short targets_down = 0, targets = 0, packets = 0; -#define targets_alive (targets - targets_down) -static unsigned int retry_interval, pkt_interval, target_interval; -static int icmp_sock, tcp_sock, udp_sock, status = STATE_OK; -static pid_t pid; -static struct timezone tz; -static struct timeval prog_start; -static unsigned long long max_completion_time = 0; -static unsigned int warn_down = 1, crit_down = 1; /* host down threshold values */ -static int min_hosts_alive = -1; -static float pkt_backoff_factor = 1.5; -static float target_backoff_factor = 1.5; -static bool rta_mode = false; -static bool pl_mode = false; -static bool jitter_mode = false; -static bool score_mode = false; -static bool mos_mode = false; -static bool order_mode = false; +static struct rta_host *cursor = NULL; +static struct rta_host *host_list = NULL; + +static int debug = 0; + +/** the working code **/ + +static inline unsigned short targets_alive(unsigned short targets, unsigned short targets_down) { + return targets - targets_down; +} +static inline unsigned int icmp_pkts_en_route(unsigned int icmp_sent, unsigned int icmp_recv, + unsigned int icmp_lost) { + return icmp_sent - (icmp_recv + icmp_lost); +} + +// Create configuration from cli parameters +typedef struct { + int errorcode; + check_icmp_config config; +} check_icmp_config_wrapper; +check_icmp_config_wrapper process_arguments(int argc, char **argv); + +check_icmp_config_wrapper process_arguments(int argc, char **argv) { + /* get calling name the old-fashioned way for portability instead + * of relying on the glibc-ism __progname */ + char *ptr = strrchr(argv[0], '/'); + if (ptr) { + progname = &ptr[1]; + } else { + progname = argv[0]; + } + + check_icmp_config_wrapper result = { + .errorcode = OK, + .config = check_icmp_config_init(), + }; + + /* use the pid to mark packets as ours */ + /* Some systems have 32-bit pid_t so mask off only 16 bits */ + result.config.pid = getpid() & 0xffff; + + if (!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) { + result.config.mode = MODE_ICMP; + } else if (!strcmp(progname, "check_host")) { + result.config.mode = MODE_HOSTCHECK; + result.config.pkt_interval = 1000000; + result.config.packets = 5; + result.config.crit.rta = result.config.warn.rta = 1000000; + result.config.crit.pl = result.config.warn.pl = 100; + } else if (!strcmp(progname, "check_rta_multi")) { + result.config.mode = MODE_ALL; + result.config.target_interval = 0; + result.config.pkt_interval = 50000; + result.config.packets = 5; + } + /* support "--help" and "--version" */ + if (argc == 2) { + if (!strcmp(argv[1], "--help")) { + strcpy(argv[1], "-h"); + } + if (!strcmp(argv[1], "--version")) { + strcpy(argv[1], "-V"); + } + } + + /* Parse protocol arguments first */ + char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64"; + for (int i = 1; i < argc; i++) { + long int arg; + while ((arg = getopt(argc, argv, opts_str)) != EOF) { + switch (arg) { + case '4': + if (address_family != -1) { + crash("Multiple protocol versions not supported"); + } + address_family = AF_INET; + break; + case '6': +#ifdef USE_IPV6 + if (address_family != -1) { + crash("Multiple protocol versions not supported"); + } + address_family = AF_INET6; +#else + usage(_("IPv6 support not available\n")); +#endif + break; + } + } + } + + /* Reset argument scanning */ + optind = 1; + + bool err; + /* parse the arguments */ + for (int i = 1; i < argc; i++) { + long int arg; + while ((arg = getopt(argc, argv, opts_str)) != EOF) { + switch (arg) { + case 'v': + debug++; + break; + case 'b': { + long size = strtol(optarg, NULL, 0); + if ((unsigned long)size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) && + size < MAX_PING_DATA) { + result.config.icmp_data_size = size; + result.config.icmp_pkt_size = size + ICMP_MINLEN; + } else { + usage_va("ICMP data length must be between: %lu and %lu", + sizeof(struct icmp) + sizeof(struct icmp_ping_data), + MAX_PING_DATA - 1); + } + } break; + case 'i': + result.config.pkt_interval = get_timevar(optarg); + break; + case 'I': + result.config.target_interval = get_timevar(optarg); + break; + case 'w': + get_threshold(optarg, &result.config.warn); + break; + case 'c': + get_threshold(optarg, &result.config.crit); + break; + case 'n': + case 'p': + result.config.packets = strtoul(optarg, NULL, 0); + if (result.config.packets > 20) { + errno = 0; + crash("packets is > 20 (%d)", result.config.packets); + } + break; + case 't': + result.config.timeout = strtoul(optarg, NULL, 0); + // TODO die here and complain about wrong input + // instead of: + if (!result.config.timeout) { + result.config.timeout = 10; + } + break; + case 'H': { + int add_result = add_target(optarg, result.config.mode); + if (add_result == 0) { + result.config.number_of_targets++; + } + } break; + case 'l': + result.config.ttl = strtoul(optarg, NULL, 0); + break; + case 'm': + result.config.min_hosts_alive = (int)strtoul(optarg, NULL, 0); + break; + case 's': /* specify source IP address */ + result.config.source_ip = optarg; + break; + case 'V': /* version */ + print_revision(progname, NP_VERSION); + exit(STATE_UNKNOWN); + case 'h': /* help */ + print_help(); + exit(STATE_UNKNOWN); + break; + case 'R': /* RTA mode */ + err = get_threshold2(optarg, strlen(optarg), &result.config.warn, + &result.config.crit, const_rta_mode); + if (!err) { + crash("Failed to parse RTA threshold"); + } + + result.config.rta_mode = true; + break; + case 'P': /* packet loss mode */ + err = get_threshold2(optarg, strlen(optarg), &result.config.warn, + &result.config.crit, const_packet_loss_mode); + if (!err) { + crash("Failed to parse packet loss threshold"); + } + + result.config.pl_mode = true; + break; + case 'J': /* jitter mode */ + err = get_threshold2(optarg, strlen(optarg), &result.config.warn, + &result.config.crit, const_jitter_mode); + if (!err) { + crash("Failed to parse jitter threshold"); + } + + result.config.jitter_mode = true; + break; + case 'M': /* MOS mode */ + err = get_threshold2(optarg, strlen(optarg), &result.config.warn, + &result.config.crit, const_mos_mode); + if (!err) { + crash("Failed to parse MOS threshold"); + } + + result.config.mos_mode = true; + break; + case 'S': /* score mode */ + err = get_threshold2(optarg, strlen(optarg), &result.config.warn, + &result.config.crit, const_score_mode); + if (!err) { + crash("Failed to parse score threshold"); + } + + result.config.score_mode = true; + break; + case 'O': /* out of order mode */ + result.config.order_mode = true; + break; + } + } + } + + argv = &argv[optind]; + while (*argv) { + add_target(*argv, result.config.mode); + argv++; + } + + if (!result.config.number_of_targets) { + errno = 0; + crash("No hosts to check"); + } + + /* stupid users should be able to give whatever thresholds they want + * (nothing will break if they do), but some anal plugin maintainer + * will probably add some printf() thing here later, so it might be + * best to at least show them where to do it. ;) */ + if (result.config.warn.pl > result.config.crit.pl) { + result.config.warn.pl = result.config.crit.pl; + } + if (result.config.warn.rta > result.config.crit.rta) { + result.config.warn.rta = result.config.crit.rta; + } + if (result.config.warn.jitter > result.config.crit.jitter) { + result.config.crit.jitter = result.config.warn.jitter; + } + if (result.config.warn.mos < result.config.crit.mos) { + result.config.warn.mos = result.config.crit.mos; + } + if (result.config.warn.score < result.config.crit.score) { + result.config.warn.score = result.config.crit.score; + } + + return result; +} /** code start **/ static void crash(const char *fmt, ...) { @@ -382,7 +567,11 @@ static const char *get_icmp_error_msg(unsigned char icmp_type, unsigned char icm return msg; } -static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr) { +static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr, + unsigned int *pkt_interval, unsigned int *target_interval, + const pid_t pid, struct rta_host **table, unsigned short packets, + const unsigned short number_of_targets, + check_icmp_state *program_state) { struct icmp p; memcpy(&p, packet, sizeof(p)); if (p.icmp_type == ICMP_ECHO && ntohs(p.icmp_id) == pid) { @@ -404,7 +593,8 @@ static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *ad * TIMXCEED actually sends a proper icmp response we will have passed * too many hops to have a hope of reaching it later, in which case it * indicates overconfidence in the network, poor routing or both. */ - if (p.icmp_type != ICMP_UNREACH && p.icmp_type != ICMP_TIMXCEED && p.icmp_type != ICMP_SOURCEQUENCH && p.icmp_type != ICMP_PARAMPROB) { + if (p.icmp_type != ICMP_UNREACH && p.icmp_type != ICMP_TIMXCEED && + p.icmp_type != ICMP_SOURCEQUENCH && p.icmp_type != ICMP_PARAMPROB) { return 0; } @@ -412,7 +602,8 @@ static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *ad * to RFC 792). If it isn't, just ignore it */ struct icmp sent_icmp; memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp)); - if (sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != pid || ntohs(sent_icmp.icmp_seq) >= targets * packets) { + if (sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != pid || + ntohs(sent_icmp.icmp_seq) >= number_of_targets * packets) { if (debug) { printf("Packet is no response to a packet we sent\n"); } @@ -424,10 +615,11 @@ static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *ad if (debug) { char address[INET6_ADDRSTRLEN]; parse_address(addr, address, sizeof(address)); - printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n", get_icmp_error_msg(p.icmp_type, p.icmp_code), address, host->name); + printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n", + get_icmp_error_msg(p.icmp_type, p.icmp_code), address, host->name); } - icmp_lost++; + program_state->icmp_lost++; host->icmp_lost++; /* don't spend time on lost hosts any more */ if (host->flags & FLAG_LOST_CAUSE) { @@ -437,10 +629,10 @@ static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *ad /* source quench means we're sending too fast, so increase the * interval and mark this packet lost */ if (p.icmp_type == ICMP_SOURCEQUENCH) { - pkt_interval *= pkt_backoff_factor; - target_interval *= target_backoff_factor; + *pkt_interval *= PACKET_BACKOFF_FACTOR; + *target_interval *= TARGET_BACKOFF_FACTOR; } else { - targets_down++; + program_state->targets_down++; host->flags |= FLAG_LOST_CAUSE; } host->icmp_type = p.icmp_type; @@ -466,240 +658,23 @@ int main(int argc, char **argv) { bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - /* we only need to be setsuid when we get the sockets, so do - * that before pointer magic (esp. on network data) */ - int icmp_sockerrno; - int udp_sockerrno; - int tcp_sockerrno; - icmp_sockerrno = udp_sockerrno = tcp_sockerrno = sockets = 0; - address_family = -1; int icmp_proto = IPPROTO_ICMP; - /* get calling name the old-fashioned way for portability instead - * of relying on the glibc-ism __progname */ - char *ptr = strrchr(argv[0], '/'); - if (ptr) { - progname = &ptr[1]; - } else { - progname = argv[0]; - } - - /* now set defaults. Use progname to set them initially (allows for - * superfast check_host program when target host is up */ - cursor = list = NULL; - table = NULL; - - mode = MODE_RTA; - /* Default critical thresholds */ - crit.rta = 500000; - crit.pl = 80; - crit.jitter = 50; - crit.mos = 3; - crit.score = 70; - /* Default warning thresholds */ - warn.rta = 200000; - warn.pl = 40; - warn.jitter = 40; - warn.mos = 3.5; - warn.score = 80; - - protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP; - pkt_interval = 80000; /* 80 msec packet interval by default */ - packets = 5; - - if (!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) { - mode = MODE_ICMP; - protocols = HAVE_ICMP; - } else if (!strcmp(progname, "check_host")) { - mode = MODE_HOSTCHECK; - pkt_interval = 1000000; - packets = 5; - crit.rta = warn.rta = 1000000; - crit.pl = warn.pl = 100; - } else if (!strcmp(progname, "check_rta_multi")) { - mode = MODE_ALL; - target_interval = 0; - pkt_interval = 50000; - packets = 5; - } - - /* support "--help" and "--version" */ - if (argc == 2) { - if (!strcmp(argv[1], "--help")) { - strcpy(argv[1], "-h"); - } - if (!strcmp(argv[1], "--version")) { - strcpy(argv[1], "-V"); - } - } - - /* Parse protocol arguments first */ - char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64"; - for (int i = 1; i < argc; i++) { - long int arg; - while ((arg = getopt(argc, argv, opts_str)) != EOF) { - switch (arg) { - case '4': - if (address_family != -1) { - crash("Multiple protocol versions not supported"); - } - address_family = AF_INET; - break; - case '6': -#ifdef USE_IPV6 - if (address_family != -1) { - crash("Multiple protocol versions not supported"); - } - address_family = AF_INET6; -#else - usage(_("IPv6 support not available\n")); -#endif - break; - } - } - } - - /* Reset argument scanning */ - optind = 1; - - unsigned long size; - bool err; - /* parse the arguments */ - char *source_ip = NULL; - for (int i = 1; i < argc; i++) { - long int arg; - while ((arg = getopt(argc, argv, opts_str)) != EOF) { - switch (arg) { - case 'v': - debug++; - break; - case 'b': - size = strtol(optarg, NULL, 0); - if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) && size < MAX_PING_DATA) { - icmp_data_size = size; - icmp_pkt_size = size + ICMP_MINLEN; - } else { - usage_va("ICMP data length must be between: %lu and %lu", sizeof(struct icmp) + sizeof(struct icmp_ping_data), - MAX_PING_DATA - 1); - } - break; - case 'i': - pkt_interval = get_timevar(optarg); - break; - case 'I': - target_interval = get_timevar(optarg); - break; - case 'w': - get_threshold(optarg, &warn); - break; - case 'c': - get_threshold(optarg, &crit); - break; - case 'n': - case 'p': - packets = strtoul(optarg, NULL, 0); - break; - case 't': - timeout = strtoul(optarg, NULL, 0); - if (!timeout) { - timeout = 10; - } - break; - case 'H': - add_target(optarg); - break; - case 'l': - ttl = (int)strtoul(optarg, NULL, 0); - break; - case 'm': - min_hosts_alive = (int)strtoul(optarg, NULL, 0); - break; - case 'd': /* implement later, for cluster checks */ - warn_down = (unsigned char)strtoul(optarg, &ptr, 0); - if (ptr) { - crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0); - } - break; - case 's': /* specify source IP address */ - source_ip = optarg; - break; - case 'V': /* version */ - print_revision(progname, NP_VERSION); - exit(STATE_UNKNOWN); - case 'h': /* help */ - print_help(); - exit(STATE_UNKNOWN); - break; - case 'R': /* RTA mode */ - err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_rta_mode); - if (!err) { - crash("Failed to parse RTA threshold"); - } - - rta_mode = true; - break; - case 'P': /* packet loss mode */ - err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_packet_loss_mode); - if (!err) { - crash("Failed to parse packet loss threshold"); - } - - pl_mode = true; - break; - case 'J': /* jitter mode */ - err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_jitter_mode); - if (!err) { - crash("Failed to parse jitter threshold"); - } - - jitter_mode = true; - break; - case 'M': /* MOS mode */ - err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_mos_mode); - if (!err) { - crash("Failed to parse MOS threshold"); - } - - mos_mode = true; - break; - case 'S': /* score mode */ - err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_score_mode); - if (!err) { - crash("Failed to parse score threshold"); - } - - score_mode = true; - break; - case 'O': /* out of order mode */ - order_mode = true; - break; - } - } - } - /* POSIXLY_CORRECT might break things, so unset it (the portable way) */ environ = NULL; - /* use the pid to mark packets as ours */ - /* Some systems have 32-bit pid_t so mask off only 16 bits */ - pid = getpid() & 0xffff; - /* printf("pid = %u\n", pid); */ - /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - argv = &argv[optind]; - while (*argv) { - add_target(*argv); - argv++; - } + check_icmp_config_wrapper tmp_config = process_arguments(argc, argv); - if (!targets) { - errno = 0; - crash("No hosts to check"); + if (tmp_config.errorcode != OK) { + crash("failed to parse config"); } + const check_icmp_config config = tmp_config.config; + // add_target might change address_family switch (address_family) { case AF_INET: @@ -711,14 +686,14 @@ int main(int argc, char **argv) { default: crash("Address family not supported"); } - if ((icmp_sock = socket(address_family, SOCK_RAW, icmp_proto)) != -1) { - sockets |= HAVE_ICMP; - } else { - icmp_sockerrno = errno; + + int icmp_sock = socket(address_family, SOCK_RAW, icmp_proto); + if (icmp_sock == -1) { + crash("Failed to obtain ICMP socket"); } - if (source_ip) { - set_source_ip(source_ip); + if (config.source_ip) { + set_source_ip(config.source_ip, icmp_sock); } #ifdef SO_TIMESTAMP @@ -736,184 +711,179 @@ int main(int argc, char **argv) { return 1; } - if (!sockets) { - if (icmp_sock == -1) { - errno = icmp_sockerrno; - crash("Failed to obtain ICMP socket"); - return -1; - } - /* if(udp_sock == -1) { */ - /* errno = icmp_sockerrno; */ - /* crash("Failed to obtain UDP socket"); */ - /* return -1; */ - /* } */ - /* if(tcp_sock == -1) { */ - /* errno = icmp_sockerrno; */ - /* crash("Failed to obtain TCP socker"); */ - /* return -1; */ - /* } */ - } - if (!ttl) { - ttl = 64; - } - if (icmp_sock) { - int result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl)); + int result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &config.ttl, sizeof(config.ttl)); if (debug) { if (result == -1) { printf("setsockopt failed\n"); } else { - printf("ttl set to %u\n", ttl); + printf("ttl set to %lu\n", config.ttl); } } } - /* stupid users should be able to give whatever thresholds they want - * (nothing will break if they do), but some anal plugin maintainer - * will probably add some printf() thing here later, so it might be - * best to at least show them where to do it. ;) */ - if (warn.pl > crit.pl) { - warn.pl = crit.pl; - } - if (warn.rta > crit.rta) { - warn.rta = crit.rta; - } - if (warn_down > crit_down) { - crit_down = warn_down; - } - if (warn.jitter > crit.jitter) { - crit.jitter = warn.jitter; - } - if (warn.mos < crit.mos) { - warn.mos = crit.mos; - } - if (warn.score < crit.score) { - warn.score = crit.score; - } - #ifdef HAVE_SIGACTION struct sigaction sig_action; sig_action.sa_sigaction = NULL; sig_action.sa_handler = finish; sigfillset(&sig_action.sa_mask); sig_action.sa_flags = SA_NODEFER | SA_RESTART; + sigaction(SIGINT, &sig_action, NULL); sigaction(SIGHUP, &sig_action, NULL); sigaction(SIGTERM, &sig_action, NULL); sigaction(SIGALRM, &sig_action, NULL); #else /* HAVE_SIGACTION */ - signal(SIGINT, finish); - signal(SIGHUP, finish); - signal(SIGTERM, finish); - signal(SIGALRM, finish); + // signal(SIGINT, finish); + // signal(SIGHUP, finish); + // signal(SIGTERM, finish); + // signal(SIGALRM, finish); #endif /* HAVE_SIGACTION */ if (debug) { - printf("Setting alarm timeout to %u seconds\n", timeout); + printf("Setting alarm timeout to %u seconds\n", config.timeout); } - alarm(timeout); + alarm(config.timeout); /* make sure we don't wait any longer than necessary */ - gettimeofday(&prog_start, &tz); - max_completion_time = ((targets * packets * pkt_interval) + (targets * target_interval)) + (targets * packets * crit.rta) + crit.rta; + struct timezone time_zone_dummy; + struct timeval prog_start; + gettimeofday(&prog_start, &time_zone_dummy); + + unsigned int max_completion_time = + ((config.number_of_targets * config.packets * config.pkt_interval) + + (config.number_of_targets * config.target_interval)) + + (config.number_of_targets * config.packets * config.crit.rta) + config.crit.rta; if (debug) { printf("packets: %u, targets: %u\n" "target_interval: %0.3f, pkt_interval %0.3f\n" "crit.rta: %0.3f\n" "max_completion_time: %0.3f\n", - packets, targets, (float)target_interval / 1000, (float)pkt_interval / 1000, (float)crit.rta / 1000, + config.packets, config.number_of_targets, (float)config.target_interval / 1000, + (float)config.pkt_interval / 1000, (float)config.crit.rta / 1000, (float)max_completion_time / 1000); } if (debug) { - if (max_completion_time > (u_int)timeout * 1000000) { - printf("max_completion_time: %llu timeout: %u\n", max_completion_time, timeout); - printf("Timeout must be at least %llu\n", max_completion_time / 1000000 + 1); + if (max_completion_time > (unsigned int)config.timeout * 1000000) { + printf("max_completion_time: %u timeout: %u\n", max_completion_time, config.timeout); + printf("Timeout must be at least %u\n", (max_completion_time / 1000000) + 1); } } if (debug) { - printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n", crit.rta, crit.pl, warn.rta, warn.pl); - printf("pkt_interval: %u target_interval: %u retry_interval: %u\n", pkt_interval, target_interval, retry_interval); - printf("icmp_pkt_size: %u timeout: %u\n", icmp_pkt_size, timeout); + printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n", config.crit.rta, config.crit.pl, + config.warn.rta, config.warn.pl); + printf("pkt_interval: %u target_interval: %u\n", config.pkt_interval, + config.target_interval); + printf("icmp_pkt_size: %u timeout: %u\n", config.icmp_pkt_size, config.timeout); } - if (packets > 20) { + if (config.min_hosts_alive < -1) { errno = 0; - crash("packets is > 20 (%d)", packets); + crash("minimum alive hosts is negative (%i)", config.min_hosts_alive); } - if (min_hosts_alive < -1) { - errno = 0; - crash("minimum alive hosts is negative (%i)", min_hosts_alive); - } - - struct rta_host *host = list; - table = malloc(sizeof(struct rta_host *) * targets); + struct rta_host *host = host_list; + struct rta_host **table = malloc(sizeof(struct rta_host *) * config.number_of_targets); if (!table) { crash("main(): malloc failed for host table"); } - int i = 0; + unsigned short i = 0; while (host) { - host->id = i * packets; + host->id = i * config.packets; table[i] = host; host = host->next; i++; } - run_checks(); + unsigned int pkt_interval = config.pkt_interval; + unsigned int target_interval = config.target_interval; + + check_icmp_state program_state = check_icmp_state_init(); + + run_checks(config.order_mode, config.mos_mode, config.rta_mode, config.pl_mode, + config.jitter_mode, config.score_mode, config.min_hosts_alive, config.icmp_data_size, + &pkt_interval, &target_interval, config.warn, config.crit, config.pid, config.mode, + max_completion_time, &prog_start, table, config.packets, icmp_sock, + config.number_of_targets, &program_state); errno = 0; - finish(0); + finish(0, config.order_mode, config.mos_mode, config.rta_mode, config.pl_mode, + config.jitter_mode, config.score_mode, config.min_hosts_alive, config.warn, config.crit, + icmp_sock, config.number_of_targets, &program_state); return (0); } -static void run_checks(void) { +static void run_checks(bool order_mode, bool mos_mode, bool rta_mode, bool pl_mode, + bool jitter_mode, bool score_mode, int min_hosts_alive, + unsigned short icmp_pkt_size, unsigned int *pkt_interval, + unsigned int *target_interval, threshold warn, threshold crit, + const pid_t pid, const int mode, const unsigned int max_completion_time, + const struct timeval *prog_start, struct rta_host **table, + const unsigned short packets, const int icmp_sock, + const unsigned short number_of_targets, check_icmp_state *program_state) { /* this loop might actually violate the pkt_interval or target_interval * settings, but only if there aren't any packets on the wire which * indicates that the target can handle an increased packet rate */ - for (u_int i = 0; i < packets; i++) { - for (u_int t = 0; t < targets; t++) { + for (unsigned int packet_index = 0; packet_index < packets; packet_index++) { + for (unsigned int target_index = 0; target_index < number_of_targets; target_index++) { /* don't send useless packets */ - if (!targets_alive) { - finish(0); + if (!targets_alive(number_of_targets, program_state->targets_down)) { + finish(0, order_mode, mos_mode, rta_mode, pl_mode, jitter_mode, score_mode, + min_hosts_alive, warn, crit, icmp_sock, number_of_targets, program_state); } - if (table[t]->flags & FLAG_LOST_CAUSE) { + if (table[target_index]->flags & FLAG_LOST_CAUSE) { if (debug) { - printf("%s is a lost cause. not sending any more\n", table[t]->name); + printf("%s is a lost cause. not sending any more\n", table[target_index]->name); } continue; } /* we're still in the game, so send next packet */ - (void)send_icmp_ping(icmp_sock, table[t]); - wait_for_reply(icmp_sock, target_interval); + (void)send_icmp_ping(icmp_sock, table[target_index], icmp_pkt_size, pid, program_state); + + wait_for_reply(icmp_sock, *target_interval, order_mode, mos_mode, rta_mode, pl_mode, + jitter_mode, score_mode, min_hosts_alive, icmp_pkt_size, pkt_interval, + target_interval, warn, crit, pid, mode, max_completion_time, prog_start, + table, packets, icmp_sock, number_of_targets, program_state); } - wait_for_reply(icmp_sock, pkt_interval * targets); + wait_for_reply(icmp_sock, *pkt_interval * number_of_targets, order_mode, mos_mode, rta_mode, + pl_mode, jitter_mode, score_mode, min_hosts_alive, icmp_pkt_size, + pkt_interval, target_interval, warn, crit, pid, mode, max_completion_time, + prog_start, table, packets, icmp_sock, number_of_targets, program_state); } - if (icmp_pkts_en_route && targets_alive) { - u_int time_passed = get_timevaldiff(NULL, NULL); - u_int final_wait = max_completion_time - time_passed; + if (icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv, + program_state->icmp_lost) && + targets_alive(number_of_targets, program_state->targets_down)) { + unsigned int time_passed = get_timevaldiff(NULL, NULL, prog_start); + unsigned int final_wait = max_completion_time - time_passed; if (debug) { - printf("time_passed: %u final_wait: %u max_completion_time: %llu\n", time_passed, final_wait, max_completion_time); + printf("time_passed: %u final_wait: %u max_completion_time: %u\n", time_passed, + final_wait, max_completion_time); } if (time_passed > max_completion_time) { if (debug) { printf("Time passed. Finishing up\n"); } - finish(0); + finish(0, order_mode, mos_mode, rta_mode, pl_mode, jitter_mode, score_mode, + min_hosts_alive, warn, crit, icmp_sock, number_of_targets, program_state); } /* catch the packets that might come in within the timeframe, but * haven't yet */ if (debug) { - printf("Waiting for %u micro-seconds (%0.3f msecs)\n", final_wait, (float)final_wait / 1000); + printf("Waiting for %u micro-seconds (%0.3f msecs)\n", final_wait, + (float)final_wait / 1000); } - wait_for_reply(icmp_sock, final_wait); + wait_for_reply(icmp_sock, final_wait, order_mode, mos_mode, rta_mode, pl_mode, jitter_mode, + score_mode, min_hosts_alive, icmp_pkt_size, pkt_interval, target_interval, + warn, crit, pid, mode, max_completion_time, prog_start, table, packets, + icmp_sock, number_of_targets, program_state); } } @@ -927,7 +897,15 @@ static void run_checks(void) { * both: * icmp echo reply : the rest */ -static int wait_for_reply(int sock, u_int t) { +static int wait_for_reply(int sock, const unsigned int time_interval, bool order_mode, + bool mos_mode, bool rta_mode, bool pl_mode, bool jitter_mode, + bool score_mode, int min_hosts_alive, unsigned short icmp_pkt_size, + unsigned int *pkt_interval, unsigned int *target_interval, threshold warn, + threshold crit, const pid_t pid, const int mode, + const unsigned long long max_completion_time, struct timeval *prog_start, + struct rta_host **table, const unsigned short packets, + const int icmp_sock, const unsigned short number_of_targets, + check_icmp_state *program_state) { union icmp_packet packet; if (!(packet.buf = malloc(icmp_pkt_size))) { crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", icmp_pkt_size); @@ -937,30 +915,39 @@ static int wait_for_reply(int sock, u_int t) { memset(packet.buf, 0, icmp_pkt_size); /* if we can't listen or don't have anything to listen to, just return */ - if (!t || !icmp_pkts_en_route) { + if (!time_interval || !icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv, + program_state->icmp_lost)) { free(packet.buf); return 0; } struct timeval wait_start; - gettimeofday(&wait_start, &tz); + struct timezone time_zone_dummy; + gettimeofday(&wait_start, &time_zone_dummy); - u_int i = t; struct sockaddr_storage resp_addr; - u_int per_pkt_wait = t / icmp_pkts_en_route; + unsigned int per_pkt_wait = + time_interval / icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv, + program_state->icmp_lost); static unsigned char buf[65536]; union ip_hdr *ip; struct timeval now; - while (icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) { - t = per_pkt_wait; + while (icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv, + program_state->icmp_lost) && + get_timevaldiff(&wait_start, NULL, prog_start) < time_interval) { + unsigned int loop_time_interval = per_pkt_wait; /* wrap up if all targets are declared dead */ - if (!targets_alive || get_timevaldiff(&prog_start, NULL) >= max_completion_time || (mode == MODE_HOSTCHECK && targets_down)) { - finish(0); + if (!targets_alive(number_of_targets, program_state->targets_down) || + get_timevaldiff(prog_start, NULL, prog_start) >= max_completion_time || + (mode == MODE_HOSTCHECK && program_state->targets_down)) { + finish(0, order_mode, mos_mode, rta_mode, pl_mode, jitter_mode, score_mode, + min_hosts_alive, warn, crit, icmp_sock, number_of_targets, program_state); } /* reap responses until we hit a timeout */ - int n = recvfrom_wto(sock, buf, sizeof(buf), (struct sockaddr *)&resp_addr, &t, &now); + int n = recvfrom_wto(sock, buf, sizeof(buf), (struct sockaddr *)&resp_addr, + &loop_time_interval, &now, prog_start); if (!n) { if (debug > 1) { printf("recvfrom_wto() timed out during a %u usecs wait\n", per_pkt_wait); @@ -982,7 +969,9 @@ static int wait_for_reply(int sock, u_int t) { if (debug > 1) { char address[INET6_ADDRSTRLEN]; parse_address(&resp_addr, address, sizeof(address)); - printf("received %u bytes from %s\n", address_family == AF_INET6 ? ntohs(ip->ip6.ip6_plen) : ntohs(ip->ip.ip_len), address); + printf("received %u bytes from %s\n", + address_family == AF_INET6 ? ntohs(ip->ip6.ip6_plen) : ntohs(ip->ip.ip_len), + address); } } @@ -999,7 +988,8 @@ static int wait_for_reply(int sock, u_int t) { if (n < (hlen + ICMP_MINLEN)) { char address[INET6_ADDRSTRLEN]; parse_address(&resp_addr, address, sizeof(address)); - crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n", n, hlen + icmp_pkt_size, address); + crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n", n, + hlen + icmp_pkt_size, address); } /* else if(debug) { */ /* printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */ @@ -1013,14 +1003,19 @@ static int wait_for_reply(int sock, u_int t) { /* address_family == AF_INET6 ? sizeof(struct icmp6_hdr) : sizeof(struct icmp));*/ - if ((address_family == PF_INET && (ntohs(packet.icp->icmp_id) != pid || packet.icp->icmp_type != ICMP_ECHOREPLY || - ntohs(packet.icp->icmp_seq) >= targets * packets)) || - (address_family == PF_INET6 && (ntohs(packet.icp6->icmp6_id) != pid || packet.icp6->icmp6_type != ICMP6_ECHO_REPLY || - ntohs(packet.icp6->icmp6_seq) >= targets * packets))) { + if ((address_family == PF_INET && + (ntohs(packet.icp->icmp_id) != pid || packet.icp->icmp_type != ICMP_ECHOREPLY || + ntohs(packet.icp->icmp_seq) >= number_of_targets * packets)) || + (address_family == PF_INET6 && + (ntohs(packet.icp6->icmp6_id) != pid || packet.icp6->icmp6_type != ICMP6_ECHO_REPLY || + ntohs(packet.icp6->icmp6_seq) >= number_of_targets * packets))) { if (debug > 2) { printf("not a proper ICMP_ECHOREPLY\n"); } - handle_random_icmp(buf + hlen, &resp_addr); + + handle_random_icmp(buf + hlen, &resp_addr, pkt_interval, target_interval, pid, table, + packets, number_of_targets, program_state); + continue; } @@ -1030,20 +1025,22 @@ static int wait_for_reply(int sock, u_int t) { if (address_family == PF_INET) { memcpy(&data, packet.icp->icmp_data, sizeof(data)); if (debug > 2) { - printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", sizeof(data), ntohs(packet.icp->icmp_id), - ntohs(packet.icp->icmp_seq), packet.icp->icmp_cksum); + printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", sizeof(data), + ntohs(packet.icp->icmp_id), ntohs(packet.icp->icmp_seq), + packet.icp->icmp_cksum); } host = table[ntohs(packet.icp->icmp_seq) / packets]; } else { memcpy(&data, &packet.icp6->icmp6_dataun.icmp6_un_data8[4], sizeof(data)); if (debug > 2) { - printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", sizeof(data), ntohs(packet.icp6->icmp6_id), - ntohs(packet.icp6->icmp6_seq), packet.icp6->icmp6_cksum); + printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", sizeof(data), + ntohs(packet.icp6->icmp6_id), ntohs(packet.icp6->icmp6_seq), + packet.icp6->icmp6_cksum); } host = table[ntohs(packet.icp6->icmp6_seq) / packets]; } - u_int tdiff = get_timevaldiff(&data.stime, &now); + unsigned int tdiff = get_timevaldiff(&data.stime, &now, prog_start); if (host->last_tdiff > 0) { /* Calculate jitter */ @@ -1081,7 +1078,7 @@ static int wait_for_reply(int sock, u_int t) { host->time_waited += tdiff; host->icmp_recv++; - icmp_recv++; + program_state->icmp_recv++; if (tdiff > (unsigned int)host->rtmax) { host->rtmax = tdiff; @@ -1097,13 +1094,14 @@ static int wait_for_reply(int sock, u_int t) { switch (address_family) { case AF_INET: { - printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000, address, - ttl, ip->ip.ip_ttl, (float)host->rtmax / 1000, (float)host->rtmin / 1000); + printf("%0.3f ms rtt from %s, incoming ttl: %u, max: %0.3f, min: %0.3f\n", + (float)tdiff / 1000, address, ip->ip.ip_ttl, (float)host->rtmax / 1000, + (float)host->rtmin / 1000); break; }; case AF_INET6: { - printf("%0.3f ms rtt from %s, outgoing ttl: %u, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000, address, ttl, - (float)host->rtmax / 1000, (float)host->rtmin / 1000); + printf("%0.3f ms rtt from %s, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000, + address, (float)host->rtmax / 1000, (float)host->rtmin / 1000); }; } } @@ -1112,7 +1110,8 @@ static int wait_for_reply(int sock, u_int t) { if (mode == MODE_HOSTCHECK) { printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|" "pkt=%u;;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n", - host->name, icmp_recv, (float)tdiff / 1000, icmp_recv, packets, (float)tdiff / 1000, (float)warn.rta / 1000, + host->name, program_state->icmp_recv, (float)tdiff / 1000, + program_state->icmp_recv, packets, (float)tdiff / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000); exit(STATE_OK); } @@ -1123,7 +1122,8 @@ static int wait_for_reply(int sock, u_int t) { } /* the ping functions */ -static int send_icmp_ping(int sock, struct rta_host *host) { +static int send_icmp_ping(const int sock, struct rta_host *host, const unsigned short icmp_pkt_size, + const pid_t pid, check_icmp_state *program_state) { if (sock == -1) { errno = 0; crash("Attempt to send on bogus socket"); @@ -1141,6 +1141,7 @@ static int send_icmp_ping(int sock, struct rta_host *host) { memset(buf, 0, icmp_pkt_size); struct timeval tv; + struct timezone tz; if ((gettimeofday(&tv, &tz)) == -1) { free(buf); return -1; @@ -1166,8 +1167,9 @@ static int send_icmp_ping(int sock, struct rta_host *host) { icp->icmp_cksum = icmp_checksum((uint16_t *)buf, (size_t)icmp_pkt_size); if (debug > 2) { - printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", sizeof(data), ntohs(icp->icmp_id), - ntohs(icp->icmp_seq), icp->icmp_cksum, host->name); + printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", + sizeof(data), ntohs(icp->icmp_id), ntohs(icp->icmp_seq), icp->icmp_cksum, + host->name); } } else { struct icmp6_hdr *icp6 = (struct icmp6_hdr *)buf; @@ -1183,8 +1185,9 @@ static int send_icmp_ping(int sock, struct rta_host *host) { // let checksum be calculated automatically if (debug > 2) { - printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", sizeof(data), ntohs(icp6->icmp6_id), - ntohs(icp6->icmp6_seq), icp6->icmp6_cksum, host->name); + printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", + sizeof(data), ntohs(icp6->icmp6_id), ntohs(icp6->icmp6_seq), icp6->icmp6_cksum, + host->name); } } @@ -1222,13 +1225,14 @@ static int send_icmp_ping(int sock, struct rta_host *host) { return -1; } - icmp_sent++; + program_state->icmp_sent++; host->icmp_sent++; return 0; } -static int recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr, u_int *timo, struct timeval *tv) { +static int recvfrom_wto(const int sock, void *buf, const unsigned int len, struct sockaddr *saddr, + unsigned int *timeout, struct timeval *tv, struct timeval *prog_start) { #ifdef HAVE_MSGHDR_MSG_CONTROL char ans_data[4096]; #endif // HAVE_MSGHDR_MSG_CONTROL @@ -1236,16 +1240,16 @@ static int recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr * struct cmsghdr *chdr; #endif - if (!*timo) { + if (!*timeout) { if (debug) { - printf("*timo is not\n"); + printf("*timeout is not\n"); } return 0; } struct timeval to; - to.tv_sec = *timo / 1000000; - to.tv_usec = (*timo - (to.tv_sec * 1000000)); + to.tv_sec = *timeout / 1000000; + to.tv_usec = (*timeout - (to.tv_sec * 1000000)); fd_set rd; fd_set wr; @@ -1255,54 +1259,68 @@ static int recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr * errno = 0; struct timeval then; - gettimeofday(&then, &tz); + struct timezone time_zone_dummy; + gettimeofday(&then, &time_zone_dummy); + int n = select(sock + 1, &rd, &wr, NULL, &to); if (n < 0) { crash("select() in recvfrom_wto"); } + struct timeval now; - gettimeofday(&now, &tz); - *timo = get_timevaldiff(&then, &now); + gettimeofday(&now, &time_zone_dummy); + *timeout = get_timevaldiff(&then, &now, prog_start); if (!n) { return 0; /* timeout */ } - u_int slen = sizeof(struct sockaddr_storage); + unsigned int slen = sizeof(struct sockaddr_storage); - struct iovec iov; - memset(&iov, 0, sizeof(iov)); - iov.iov_base = buf; - iov.iov_len = len; + struct iovec iov = { + .iov_base = buf, + .iov_len = len, + }; - struct msghdr hdr; - memset(&hdr, 0, sizeof(hdr)); - hdr.msg_name = saddr; - hdr.msg_namelen = slen; - hdr.msg_iov = &iov; - hdr.msg_iovlen = 1; + struct msghdr hdr = { + .msg_name = saddr, + .msg_namelen = slen, + .msg_iov = &iov, + .msg_iovlen = 1, #ifdef HAVE_MSGHDR_MSG_CONTROL - hdr.msg_control = ans_data; - hdr.msg_controllen = sizeof(ans_data); + .msg_control = ans_data, + .msg_controllen = sizeof(ans_data), #endif + }; + + ssize_t ret = recvmsg(sock, &hdr, 0); - int ret = recvmsg(sock, &hdr, 0); #ifdef SO_TIMESTAMP for (chdr = CMSG_FIRSTHDR(&hdr); chdr; chdr = CMSG_NXTHDR(&hdr, chdr)) { - if (chdr->cmsg_level == SOL_SOCKET && chdr->cmsg_type == SO_TIMESTAMP && chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) { + if (chdr->cmsg_level == SOL_SOCKET && chdr->cmsg_type == SO_TIMESTAMP && + chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) { memcpy(tv, CMSG_DATA(chdr), sizeof(*tv)); break; } } - if (!chdr) + if (!chdr) { + gettimeofday(tv, &time_zone_dummy); + } +#else + gettimeofday(tv, &time_zone_dummy); #endif // SO_TIMESTAMP - gettimeofday(tv, &tz); + return (ret); } -static void finish(int sig) { +static void finish(int sig, bool order_mode, bool mos_mode, bool rta_mode, bool pl_mode, + bool jitter_mode, bool score_mode, int min_hosts_alive, threshold warn, + threshold crit, const int icmp_sock, const unsigned short number_of_targets, + check_icmp_state *program_state) { + // Deactivate alarm alarm(0); + if (debug > 1) { printf("finish(%d) called\n", sig); } @@ -1310,57 +1328,56 @@ static void finish(int sig) { if (icmp_sock != -1) { close(icmp_sock); } - if (udp_sock != -1) { - close(udp_sock); - } - if (tcp_sock != -1) { - close(tcp_sock); - } if (debug) { - printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n", icmp_sent, icmp_recv, icmp_lost); - printf("targets: %u targets_alive: %u\n", targets, targets_alive); + printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n", program_state->icmp_sent, + program_state->icmp_recv, program_state->icmp_lost); + printf("targets: %u targets_alive: %u\n", number_of_targets, + targets_alive(number_of_targets, program_state->targets_down)); } /* iterate thrice to calculate values, give output, and print perfparse */ - status = STATE_OK; - struct rta_host *host = list; + mp_state_enum status = STATE_OK; + struct rta_host *host = host_list; - u_int i = 0; + unsigned int target_counter = 0; const char *status_string[] = {"OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPENDENT"}; int hosts_ok = 0; int hosts_warn = 0; while (host) { - int this_status = STATE_OK; + mp_state_enum this_status = STATE_OK; - unsigned char pl; + unsigned char packet_loss; double rta; if (!host->icmp_recv) { /* rta 0 is ofcourse not entirely correct, but will still show up * conspicuously as missing entries in perfparse and cacti */ - pl = 100; + packet_loss = 100; rta = 0; status = STATE_CRITICAL; /* up the down counter if not already counted */ - if (!(host->flags & FLAG_LOST_CAUSE) && targets_alive) { - targets_down++; + if (!(host->flags & FLAG_LOST_CAUSE) && + targets_alive(number_of_targets, program_state->targets_down)) { + program_state->targets_down++; } } else { - pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent; + packet_loss = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent; rta = (double)host->time_waited / host->icmp_recv; } if (host->icmp_recv > 1) { /* * This algorithm is probably pretty much blindly copied from - * locations like this one: https://www.slac.stanford.edu/comp/net/wan-mon/tutorial.html#mos - * It calculates a MOS value (range of 1 to 5, where 1 is bad and 5 really good). - * According to some quick research MOS originates from the Audio/Video transport network area. - * Whether it can and should be computed from ICMP data, I can not say. + * locations like this one: + * https://www.slac.stanford.edu/comp/net/wan-mon/tutorial.html#mos It calculates a MOS + * value (range of 1 to 5, where 1 is bad and 5 really good). According to some quick + * research MOS originates from the Audio/Video transport network area. Whether it can + * and should be computed from ICMP data, I can not say. * * Anyway the basic idea is to map a value "R" with a range of 0-100 to the MOS value * - * MOS stands likely for Mean Opinion Score ( https://en.wikipedia.org/wiki/Mean_Opinion_Score ) + * MOS stands likely for Mean Opinion Score ( + * https://en.wikipedia.org/wiki/Mean_Opinion_Score ) * * More links: * - https://confluence.slac.stanford.edu/display/IEPM/MOS @@ -1383,7 +1400,7 @@ static void finish(int sig) { // Now, let us deduct 2.5 R values per percentage of packet loss (i.e. a // loss of 5% will be entered as 5). - R = R - (pl * 2.5); + R = R - (packet_loss * 2.5); if (R < 0) { R = 0; @@ -1398,7 +1415,7 @@ static void finish(int sig) { host->mos = 0; } - host->pl = pl; + host->pl = packet_loss; host->rta = rta; /* if no new mode selected, use old schema */ @@ -1421,11 +1438,11 @@ static void finish(int sig) { } if (pl_mode) { - if (pl >= crit.pl) { + if (packet_loss >= crit.pl) { this_status = STATE_CRITICAL; status = STATE_CRITICAL; host->pl_status = STATE_CRITICAL; - } else if (status != STATE_CRITICAL && (pl >= warn.pl)) { + } else if (status != STATE_CRITICAL && (packet_loss >= warn.pl)) { this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status); status = STATE_WARNING; host->pl_status = STATE_WARNING; @@ -1478,7 +1495,7 @@ static void finish(int sig) { } /* this is inevitable */ - if (!targets_alive) { + if (!targets_alive(number_of_targets, program_state->targets_down)) { status = STATE_CRITICAL; } if (min_hosts_alive > -1) { @@ -1490,21 +1507,21 @@ static void finish(int sig) { } printf("%s - ", status_string[status]); - host = list; + host = host_list; while (host) { if (debug) { puts(""); } - if (i) { - if (i < targets) { + if (target_counter) { + if (target_counter < number_of_targets) { printf(" :: "); } else { printf("\n"); } } - i++; + target_counter++; if (!host->icmp_recv) { status = STATE_CRITICAL; @@ -1514,7 +1531,8 @@ static void finish(int sig) { if (host->flags & FLAG_LOST_CAUSE) { char address[INET6_ADDRSTRLEN]; parse_address(&host->error_addr, address, sizeof(address)); - printf("%s: %s @ %s. rta nan, lost %d%%", host->name, get_icmp_error_msg(host->icmp_type, host->icmp_code), address, 100); + printf("%s: %s @ %s. rta nan, lost %d%%", host->name, + get_icmp_error_msg(host->icmp_type, host->icmp_code), address, 100); } else { /* not marked as lost cause, so we have no flags for it */ printf("%s: rta nan, lost 100%%", host->name); } @@ -1525,9 +1543,11 @@ static void finish(int sig) { if (status == STATE_OK) { printf(" rta %0.3fms", host->rta / 1000); } else if (status == STATE_WARNING && host->rta_status == status) { - printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)warn.rta / 1000); + printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, + (float)warn.rta / 1000); } else if (status == STATE_CRITICAL && host->rta_status == status) { - printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)crit.rta / 1000); + printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, + (float)crit.rta / 1000); } } @@ -1591,8 +1611,8 @@ static void finish(int sig) { if (!(!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode && order_mode)) { printf("|"); } - i = 0; - host = list; + target_counter = 0; + host = host_list; while (host) { if (debug) { puts(""); @@ -1600,45 +1620,56 @@ static void finish(int sig) { if (rta_mode) { if (host->pl < 100) { - printf("%srta=%0.3fms;%0.3f;%0.3f;0; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ", (targets > 1) ? host->name : "", - host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000, (targets > 1) ? host->name : "", - (float)host->rtmax / 1000, (targets > 1) ? host->name : "", + printf("%srta=%0.3fms;%0.3f;%0.3f;0; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ", + (number_of_targets > 1) ? host->name : "", host->rta / 1000, + (float)warn.rta / 1000, (float)crit.rta / 1000, + (number_of_targets > 1) ? host->name : "", (float)host->rtmax / 1000, + (number_of_targets > 1) ? host->name : "", (host->rtmin < INFINITY) ? (float)host->rtmin / 1000 : (float)0); } else { - printf("%srta=U;;;; %srtmax=U;;;; %srtmin=U;;;; ", (targets > 1) ? host->name : "", (targets > 1) ? host->name : "", - (targets > 1) ? host->name : ""); + printf("%srta=U;;;; %srtmax=U;;;; %srtmin=U;;;; ", + (number_of_targets > 1) ? host->name : "", + (number_of_targets > 1) ? host->name : "", + (number_of_targets > 1) ? host->name : ""); } } if (pl_mode) { - printf("%spl=%u%%;%u;%u;0;100 ", (targets > 1) ? host->name : "", host->pl, warn.pl, crit.pl); + printf("%spl=%u%%;%u;%u;0;100 ", (number_of_targets > 1) ? host->name : "", host->pl, + warn.pl, crit.pl); } if (jitter_mode) { if (host->pl < 100) { - printf("%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %sjitter_max=%0.3fms;;;; %sjitter_min=%0.3fms;;;; ", - (targets > 1) ? host->name : "", (float)host->jitter, (float)warn.jitter, (float)crit.jitter, - (targets > 1) ? host->name : "", (float)host->jitter_max / 1000, (targets > 1) ? host->name : "", - (float)host->jitter_min / 1000); + printf("%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %sjitter_max=%0.3fms;;;; " + "%sjitter_min=%0.3fms;;;; ", + (number_of_targets > 1) ? host->name : "", (float)host->jitter, + (float)warn.jitter, (float)crit.jitter, + (number_of_targets > 1) ? host->name : "", (float)host->jitter_max / 1000, + (number_of_targets > 1) ? host->name : "", (float)host->jitter_min / 1000); } else { - printf("%sjitter_avg=U;;;; %sjitter_max=U;;;; %sjitter_min=U;;;; ", (targets > 1) ? host->name : "", - (targets > 1) ? host->name : "", (targets > 1) ? host->name : ""); + printf("%sjitter_avg=U;;;; %sjitter_max=U;;;; %sjitter_min=U;;;; ", + (number_of_targets > 1) ? host->name : "", + (number_of_targets > 1) ? host->name : "", + (number_of_targets > 1) ? host->name : ""); } } if (mos_mode) { if (host->pl < 100) { - printf("%smos=%0.1f;%0.1f;%0.1f;0;5 ", (targets > 1) ? host->name : "", (float)host->mos, (float)warn.mos, (float)crit.mos); + printf("%smos=%0.1f;%0.1f;%0.1f;0;5 ", (number_of_targets > 1) ? host->name : "", + (float)host->mos, (float)warn.mos, (float)crit.mos); } else { - printf("%smos=U;;;; ", (targets > 1) ? host->name : ""); + printf("%smos=U;;;; ", (number_of_targets > 1) ? host->name : ""); } } if (score_mode) { if (host->pl < 100) { - printf("%sscore=%u;%u;%u;0;100 ", (targets > 1) ? host->name : "", (int)host->score, (int)warn.score, (int)crit.score); + printf("%sscore=%u;%u;%u;0;100 ", (number_of_targets > 1) ? host->name : "", + (int)host->score, (int)warn.score, (int)crit.score); } else { - printf("%sscore=U;;;; ", (targets > 1) ? host->name : ""); + printf("%sscore=U;;;; ", (number_of_targets > 1) ? host->name : ""); } } @@ -1656,46 +1687,53 @@ static void finish(int sig) { /* finish with an empty line */ puts(""); if (debug) { - printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n", targets, targets_alive, hosts_ok, - hosts_warn, min_hosts_alive); + printf( + "targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n", + number_of_targets, targets_alive(number_of_targets, program_state->targets_down), + hosts_ok, hosts_warn, min_hosts_alive); } exit(status); } -static u_int get_timevaldiff(struct timeval *early, struct timeval *later) { +static time_t get_timevaldiff(struct timeval *earlier, struct timeval *later, + struct timeval *prog_start) { struct timeval now; if (!later) { - gettimeofday(&now, &tz); + struct timezone time_zone_dummy; + gettimeofday(&now, &time_zone_dummy); later = &now; } - if (!early) { - early = &prog_start; + if (!earlier) { + earlier = prog_start; } /* if early > later we return 0 so as to indicate a timeout */ - if (early->tv_sec > later->tv_sec || (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec)) { + if (earlier->tv_sec > later->tv_sec || + (earlier->tv_sec == later->tv_sec && earlier->tv_usec > later->tv_usec)) { return 0; } - u_int ret = (later->tv_sec - early->tv_sec) * 1000000; - ret += later->tv_usec - early->tv_usec; + + time_t ret = (later->tv_sec - earlier->tv_sec) * 1000000; + ret += later->tv_usec - earlier->tv_usec; return ret; } -static int add_target_ip(char *arg, struct sockaddr_storage *in) { +static int add_target_ip(char *arg, struct sockaddr_storage *address) { struct sockaddr_in *sin; struct sockaddr_in6 *sin6; if (address_family == AF_INET) { - sin = (struct sockaddr_in *)in; + sin = (struct sockaddr_in *)address; } else { - sin6 = (struct sockaddr_in6 *)in; + sin6 = (struct sockaddr_in6 *)address; } /* disregard obviously stupid addresses * (I didn't find an ipv6 equivalent to INADDR_NONE) */ - if (((address_family == AF_INET && (sin->sin_addr.s_addr == INADDR_NONE || sin->sin_addr.s_addr == INADDR_ANY))) || + if (((address_family == AF_INET && + (sin->sin_addr.s_addr == INADDR_NONE || sin->sin_addr.s_addr == INADDR_ANY))) || (address_family == AF_INET6 && (sin6->sin6_addr.s6_addr == in6addr_any.s6_addr))) { return -1; } @@ -1703,13 +1741,14 @@ static int add_target_ip(char *arg, struct sockaddr_storage *in) { /* no point in adding two identical IP's, so don't. ;) */ struct sockaddr_in *host_sin; struct sockaddr_in6 *host_sin6; - struct rta_host *host = list; + struct rta_host *host = host_list; while (host) { host_sin = (struct sockaddr_in *)&host->saddr_in; host_sin6 = (struct sockaddr_in6 *)&host->saddr_in; if ((address_family == AF_INET && host_sin->sin_addr.s_addr == sin->sin_addr.s_addr) || - (address_family == AF_INET6 && host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) { + (address_family == AF_INET6 && + host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) { if (debug) { printf("Identical IP already exists. Not adding %s\n", arg); } @@ -1722,10 +1761,11 @@ static int add_target_ip(char *arg, struct sockaddr_storage *in) { host = (struct rta_host *)malloc(sizeof(struct rta_host)); if (!host) { char straddr[INET6_ADDRSTRLEN]; - parse_address((struct sockaddr_storage *)&in, straddr, sizeof(straddr)); + parse_address((struct sockaddr_storage *)&address, straddr, sizeof(straddr)); crash("add_target_ip(%s, %s): malloc(%lu) failed", arg, straddr, sizeof(struct rta_host)); } - memset(host, 0, sizeof(struct rta_host)); + + *host = ping_target_init(); /* set the values. use calling name for output */ host->name = strdup(arg); @@ -1738,39 +1778,23 @@ static int add_target_ip(char *arg, struct sockaddr_storage *in) { } else { host_sin6 = (struct sockaddr_in6 *)&host->saddr_in; host_sin6->sin6_family = AF_INET6; - memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr, sizeof host_sin6->sin6_addr.s6_addr); - } - - /* fill out the sockaddr_in struct */ - host->rtmin = INFINITY; - host->rtmax = 0; - host->jitter = 0; - host->jitter_max = 0; - host->jitter_min = INFINITY; - host->last_tdiff = 0; - host->order_status = STATE_OK; - host->last_icmp_seq = 0; - host->rta_status = 0; - host->pl_status = 0; - host->jitter_status = 0; - host->mos_status = 0; - host->score_status = 0; - host->pl_status = 0; - - if (!list) { - list = cursor = host; + memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr, + sizeof host_sin6->sin6_addr.s6_addr); + } + + if (!host_list) { + host_list = cursor = host; } else { cursor->next = host; } cursor = host; - targets++; return 0; } /* wrapper for add_target_ip */ -static int add_target(char *arg) { +static int add_target(char *arg, const int mode) { struct sockaddr_storage ip; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; @@ -1850,7 +1874,7 @@ static int add_target(char *arg) { return 0; } -static void set_source_ip(char *arg) { +static void set_source_ip(char *arg, const int icmp_sock) { struct sockaddr_in src; memset(&src, 0, sizeof(src)); @@ -1894,7 +1918,7 @@ static in_addr_t get_ip_address(const char *ifname) { * s = seconds * return value is in microseconds */ -static u_int get_timevar(const char *str) { +static unsigned int get_timevar(const char *str) { if (!str) { return 0; } @@ -1920,7 +1944,7 @@ static u_int get_timevar(const char *str) { printf("evaluating %s, u: %c, p: %c\n", str, u, p); } - u_int factor = 1000; /* default to milliseconds */ + unsigned int factor = 1000; /* default to milliseconds */ if (u == 'u') { factor = 1; /* microseconds */ } else if (u == 'm') { @@ -1933,7 +1957,7 @@ static u_int get_timevar(const char *str) { } char *ptr; - u_int i; + unsigned int i; i = strtoul(str, &ptr, 0); if (!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1) { return i * factor; @@ -1945,7 +1969,7 @@ static u_int get_timevar(const char *str) { } /* integer and decimal, respectively */ - u_int d = strtoul(ptr + 1, NULL, 0); + unsigned int d = strtoul(ptr + 1, NULL, 0); /* d is decimal, so get rid of excess digits */ while (d >= factor) { @@ -2000,9 +2024,11 @@ static int get_threshold(char *str, threshold *th) { * @param[in] length strlen(str) * @param[out] warn Pointer to the warn threshold struct to which the values should be assigned * @param[out] crit Pointer to the crit threshold struct to which the values should be assigned - * @param[in] mode Determines whether this a threshold for rta, packet_loss, jitter, mos or score (exclusively) + * @param[in] mode Determines whether this a threshold for rta, packet_loss, jitter, mos or score + * (exclusively) */ -static bool get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, threshold_mode mode) { +static bool get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, + threshold_mode mode) { if (!str || !length || !warn || !crit) { return false; } @@ -2108,13 +2134,14 @@ void print_help(void) { printf(" %s\n", _("Use IPv4 (default) or IPv6 to communicate with the targets")); printf(" %s\n", "-w"); printf(" %s", _("warning threshold (currently ")); - printf("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl); + printf("%0.3fms,%u%%)\n", (float)DEFAULT_WARN_RTA / 1000, DEFAULT_WARN_PL); printf(" %s\n", "-c"); printf(" %s", _("critical threshold (currently ")); - printf("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl); + printf("%0.3fms,%u%%)\n", (float)DEFAULT_CRIT_RTA / 1000, DEFAULT_CRIT_PL); printf(" %s\n", "-R"); - printf(" %s\n", _("RTA, round trip average, mode warning,critical, ex. 100ms,200ms unit in ms")); + printf(" %s\n", + _("RTA, round trip average, mode warning,critical, ex. 100ms,200ms unit in ms")); printf(" %s\n", "-P"); printf(" %s\n", _("packet loss mode, ex. 40%,50% , unit in %")); printf(" %s\n", "-J"); @@ -2131,28 +2158,29 @@ void print_help(void) { printf(" %s\n", _("specify a source IP address or device name")); printf(" %s\n", "-n"); printf(" %s", _("number of packets to send (currently ")); - printf("%u)\n", packets); + printf("%u)\n", DEFAULT_NUMBER_OF_PACKETS); printf(" %s\n", "-p"); printf(" %s", _("number of packets to send (currently ")); - printf("%u)\n", packets); + printf("%u)\n", DEFAULT_NUMBER_OF_PACKETS); printf(" %s\n", "-i"); printf(" %s", _("max packet interval (currently ")); - printf("%0.3fms)\n", (float)pkt_interval / 1000); + printf("%0.3fms)\n", (float)DEFAULT_PKT_INTERVAL / 1000); printf(" %s\n", "-I"); printf(" %s", _("max target interval (currently ")); - printf("%0.3fms)\n", (float)target_interval / 1000); + printf("%0.3fms)\n", (float)DEFAULT_TARGET_INTERVAL / 1000); printf(" %s\n", "-m"); printf(" %s", _("number of alive hosts required for success")); printf("\n"); printf(" %s\n", "-l"); printf(" %s", _("TTL on outgoing packets (currently ")); - printf("%u)\n", ttl); + printf("%u)\n", DEFAULT_TTL); printf(" %s\n", "-t"); printf(" %s", _("timeout value (seconds, currently ")); - printf("%u)\n", timeout); + printf("%u)\n", DEFAULT_TIMEOUT); printf(" %s\n", "-b"); printf(" %s\n", _("Number of icmp data bytes to send")); - printf(" %s %u + %d)\n", _("Packet size will be data bytes + icmp header (currently"), icmp_data_size, ICMP_MINLEN); + printf(" %s %lu + %d)\n", _("Packet size will be data bytes + icmp header (currently"), + DEFAULT_PING_DATA_SIZE, ICMP_MINLEN); printf(" %s\n", "-v"); printf(" %s\n", _("verbose")); printf("\n"); @@ -2162,12 +2190,15 @@ void print_help(void) { printf("\n"); printf(" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%")); printf(" %s\n", _("packet loss. The default values should work well for most users.")); - printf(" %s\n", _("You can specify different RTA factors using the standardized abbreviations")); - printf(" %s\n", _("us (microseconds), ms (milliseconds, default) or just plain s for seconds.")); + printf(" %s\n", + _("You can specify different RTA factors using the standardized abbreviations")); + printf(" %s\n", + _("us (microseconds), ms (milliseconds, default) or just plain s for seconds.")); /* -d not yet implemented */ - /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means WARNING if >= 12 hops")); - printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent.")); - printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/ + /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means WARNING if >= 12 + hops")); printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent.")); printf + ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do + not."));*/ printf("\n"); printf(" %s\n", _("The -v switch can be specified several times for increased verbosity.")); /* printf ("%s\n", _("Long options are currently unsupported.")); -- cgit v1.2.3-74-g34f1