summaryrefslogtreecommitdiffstats
path: root/plugins-root/check_icmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins-root/check_icmp.c')
-rw-r--r--plugins-root/check_icmp.c2938
1 files changed, 1616 insertions, 1322 deletions
diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c
index dcaceddb..e536e31c 100644
--- a/plugins-root/check_icmp.c
+++ b/plugins-root/check_icmp.c
@@ -46,12 +46,17 @@ const char *email = "devel@monitoring-plugins.org";
46#include "../plugins/common.h" 46#include "../plugins/common.h"
47#include "netutils.h" 47#include "netutils.h"
48#include "utils.h" 48#include "utils.h"
49#include "output.h"
50#include "perfdata.h"
49 51
50#if HAVE_SYS_SOCKIO_H 52#if HAVE_SYS_SOCKIO_H
51# include <sys/sockio.h> 53# include <sys/sockio.h>
52#endif 54#endif
53 55
54#include <sys/time.h> 56#include <sys/time.h>
57#if defined(SIOCGIFADDR)
58# include <sys/ioctl.h>
59#endif /* SIOCGIFADDR */
55#include <errno.h> 60#include <errno.h>
56#include <signal.h> 61#include <signal.h>
57#include <ctype.h> 62#include <ctype.h>
@@ -65,6 +70,17 @@ const char *email = "devel@monitoring-plugins.org";
65#include <netinet/icmp6.h> 70#include <netinet/icmp6.h>
66#include <arpa/inet.h> 71#include <arpa/inet.h>
67#include <math.h> 72#include <math.h>
73#include <netdb.h>
74#include <sys/types.h>
75#include <unistd.h>
76#include <stdint.h>
77#include <sys/socket.h>
78#include <assert.h>
79#include <sys/select.h>
80
81#include "../lib/states.h"
82#include "./check_icmp.d/config.h"
83#include "./check_icmp.d/check_icmp_helpers.h"
68 84
69/** sometimes undefined system macros (quite a few, actually) **/ 85/** sometimes undefined system macros (quite a few, actually) **/
70#ifndef MAXTTL 86#ifndef MAXTTL
@@ -96,56 +112,8 @@ const char *email = "devel@monitoring-plugins.org";
96# define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 112# define ICMP_UNREACH_PRECEDENCE_CUTOFF 15
97#endif 113#endif
98 114
99typedef unsigned short range_t; /* type for get_range() -- unimplemented */
100
101typedef struct rta_host {
102 unsigned short id; /* id in **table, and icmp pkts */
103 char *name; /* arg used for adding this host */
104 char *msg; /* icmp error message, if any */
105 struct sockaddr_storage saddr_in; /* the address of this host */
106 struct sockaddr_storage error_addr; /* stores address of error replies */
107 unsigned long long time_waited; /* total time waited, in usecs */
108 unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */
109 unsigned char icmp_type, icmp_code; /* type and code from errors */
110 unsigned short flags; /* control/status flags */
111 double rta; /* measured RTA */
112 int rta_status; // check result for RTA checks
113 double rtmax; /* max rtt */
114 double rtmin; /* min rtt */
115 double jitter; /* measured jitter */
116 int jitter_status; // check result for Jitter checks
117 double jitter_max; /* jitter rtt maximum */
118 double jitter_min; /* jitter rtt minimum */
119 double EffectiveLatency;
120 double mos; /* Mean opnion score */
121 int mos_status; // check result for MOS checks
122 double score; /* score */
123 int score_status; // check result for score checks
124 u_int last_tdiff;
125 u_int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */
126 unsigned char pl; /* measured packet loss */
127 int pl_status; // check result for packet loss checks
128 struct rta_host *next; /* linked list */
129 int order_status; // check result for packet order checks
130} rta_host;
131
132#define FLAG_LOST_CAUSE 0x01 /* decidedly dead target. */ 115#define FLAG_LOST_CAUSE 0x01 /* decidedly dead target. */
133 116
134/* threshold structure. all values are maximum allowed, exclusive */
135typedef struct threshold {
136 unsigned char pl; /* max allowed packet loss in percent */
137 unsigned int rta; /* roundtrip time average, microseconds */
138 double jitter; /* jitter time average, microseconds */
139 double mos; /* MOS */
140 double score; /* Score */
141} threshold;
142
143/* the data structure */
144typedef struct icmp_ping_data {
145 struct timeval stime; /* timestamp (saved in protocol struct as well) */
146 unsigned short ping_id;
147} icmp_ping_data;
148
149typedef union ip_hdr { 117typedef union ip_hdr {
150 struct ip ip; 118 struct ip ip;
151 struct ip6_hdr ip6; 119 struct ip6_hdr ip6;
@@ -158,24 +126,6 @@ typedef union icmp_packet {
158 u_short *cksum_in; 126 u_short *cksum_in;
159} icmp_packet; 127} icmp_packet;
160 128
161/* the different modes of this program are as follows:
162 * MODE_RTA: send all packets no matter what (mimic check_icmp and check_ping)
163 * MODE_HOSTCHECK: Return immediately upon any sign of life
164 * In addition, sends packets to ALL addresses assigned
165 * to this host (as returned by gethostbyname() or
166 * gethostbyaddr() and expects one host only to be checked at
167 * a time. Therefore, any packet response what so ever will
168 * count as a sign of life, even when received outside
169 * crit.rta limit. Do not misspell any additional IP's.
170 * MODE_ALL: Requires packets from ALL requested IP to return OK (default).
171 * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without
172 * tcp and udp args does this)
173 */
174#define MODE_RTA 0
175#define MODE_HOSTCHECK 1
176#define MODE_ALL 2
177#define MODE_ICMP 3
178
179enum enum_threshold_mode { 129enum enum_threshold_mode {
180 const_rta_mode, 130 const_rta_mode,
181 const_packet_loss_mode, 131 const_packet_loss_mode,
@@ -186,89 +136,478 @@ enum enum_threshold_mode {
186 136
187typedef enum enum_threshold_mode threshold_mode; 137typedef enum enum_threshold_mode threshold_mode;
188 138
189/* the different ping types we can do
190 * TODO: investigate ARP ping as well */
191#define HAVE_ICMP 1
192#define HAVE_UDP 2
193#define HAVE_TCP 4
194#define HAVE_ARP 8
195
196#define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data)
197#define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */
198#define IP_HDR_SIZE 20
199#define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN)
200#define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44)
201
202/* various target states */
203#define TSTATE_INACTIVE 0x01 /* don't ping this host anymore */
204#define TSTATE_WAITING 0x02 /* unanswered packets on the wire */
205#define TSTATE_ALIVE 0x04 /* target is alive (has answered something) */
206#define TSTATE_UNREACH 0x08
207
208/** prototypes **/ 139/** prototypes **/
209void print_help(void); 140void print_help();
210void print_usage(void); 141void print_usage(void);
211static u_int get_timevar(const char *); 142
212static u_int get_timevaldiff(struct timeval *, struct timeval *); 143/* Time related */
213static in_addr_t get_ip_address(const char *); 144typedef struct {
214static int wait_for_reply(int, u_int); 145 int error_code;
215static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *, struct timeval *); 146 time_t time_range;
216static int send_icmp_ping(int, struct rta_host *); 147} get_timevar_wrapper;
217static int get_threshold(char *str, threshold *th); 148static get_timevar_wrapper get_timevar(const char *str);
218static bool get_threshold2(char *str, size_t length, threshold *, threshold *, threshold_mode mode); 149static time_t get_timevaldiff(struct timeval earlier, struct timeval later);
219static bool parse_threshold2_helper(char *s, size_t length, threshold *thr, threshold_mode mode); 150static time_t get_timevaldiff_to_now(struct timeval earlier);
220static void run_checks(void); 151
221static void set_source_ip(char *); 152static in_addr_t get_ip_address(const char *ifname, const int icmp_sock);
222static int add_target(char *); 153static void set_source_ip(char *arg, int icmp_sock, sa_family_t addr_family);
223static int add_target_ip(char *, struct sockaddr_storage *); 154
224static int handle_random_icmp(unsigned char *, struct sockaddr_storage *); 155/* Receiving data */
225static void parse_address(struct sockaddr_storage *, char *, int); 156static int wait_for_reply(check_icmp_socket_set sockset, time_t time_interval,
226static unsigned short icmp_checksum(uint16_t *, size_t); 157 unsigned short icmp_pkt_size, time_t *target_interval, uint16_t sender_id,
227static void finish(int); 158 ping_target **table, unsigned short packets,
228static void crash(const char *, ...); 159 unsigned short number_of_targets, check_icmp_state *program_state);
229 160
230/** external **/ 161typedef struct {
231extern int optind; 162 sa_family_t recv_proto;
232extern char *optarg; 163 ssize_t received;
233extern char **environ; 164} recvfrom_wto_wrapper;
165static recvfrom_wto_wrapper recvfrom_wto(check_icmp_socket_set sockset, void *buf, unsigned int len,
166 struct sockaddr *saddr, time_t *timeout,
167 struct timeval *received_timestamp);
168static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr,
169 time_t *target_interval, uint16_t sender_id, ping_target **table,
170 unsigned short packets, unsigned short number_of_targets,
171 check_icmp_state *program_state);
172
173/* Sending data */
174static int send_icmp_ping(check_icmp_socket_set sockset, ping_target *host,
175 unsigned short icmp_pkt_size, uint16_t sender_id,
176 check_icmp_state *program_state);
177
178/* Threshold related */
179typedef struct {
180 int errorcode;
181 check_icmp_threshold threshold;
182} get_threshold_wrapper;
183static get_threshold_wrapper get_threshold(char *str, check_icmp_threshold threshold);
184
185typedef struct {
186 int errorcode;
187 check_icmp_threshold warn;
188 check_icmp_threshold crit;
189} get_threshold2_wrapper;
190static get_threshold2_wrapper get_threshold2(char *str, size_t length, check_icmp_threshold warn,
191 check_icmp_threshold crit, threshold_mode mode);
192
193typedef struct {
194 int errorcode;
195 check_icmp_threshold result;
196} parse_threshold2_helper_wrapper;
197static parse_threshold2_helper_wrapper parse_threshold2_helper(char *threshold_string,
198 size_t length,
199 check_icmp_threshold thr,
200 threshold_mode mode);
201
202/* main test function */
203static void run_checks(unsigned short icmp_pkt_size, time_t *target_interval, uint16_t sender_id,
204 check_icmp_execution_mode mode, time_t max_completion_time,
205 struct timeval prog_start, ping_target **table, unsigned short packets,
206 check_icmp_socket_set sockset, unsigned short number_of_targets,
207 check_icmp_state *program_state);
208mp_subcheck evaluate_target(ping_target target, check_icmp_mode_switches modes,
209 check_icmp_threshold warn, check_icmp_threshold crit);
210
211typedef struct {
212 int targets_ok;
213 int targets_warn;
214 mp_subcheck sc_host;
215} evaluate_host_wrapper;
216evaluate_host_wrapper evaluate_host(check_icmp_target_container host,
217 check_icmp_mode_switches modes, check_icmp_threshold warn,
218 check_icmp_threshold crit);
219
220/* Target acquisition */
221typedef struct {
222 int error_code;
223 check_icmp_target_container host;
224 bool has_v4;
225 bool has_v6;
226} add_host_wrapper;
227static add_host_wrapper add_host(char *arg, check_icmp_execution_mode mode,
228 sa_family_t enforced_proto);
229
230typedef struct {
231 int error_code;
232 ping_target *targets;
233 unsigned int number_of_targets;
234 bool has_v4;
235 bool has_v6;
236} add_target_wrapper;
237static add_target_wrapper add_target(char *arg, check_icmp_execution_mode mode,
238 sa_family_t enforced_proto);
239
240typedef struct {
241 int error_code;
242 ping_target *target;
243} add_target_ip_wrapper;
244static add_target_ip_wrapper add_target_ip(struct sockaddr_storage address);
245
246static void parse_address(const struct sockaddr_storage *addr, char *dst, socklen_t size);
247
248static unsigned short icmp_checksum(uint16_t *packet, size_t packet_size);
249
250/* End of run function */
251static void finish(int sign, check_icmp_mode_switches modes, int min_hosts_alive,
252 check_icmp_threshold warn, check_icmp_threshold crit,
253 unsigned short number_of_targets, check_icmp_state *program_state,
254 check_icmp_target_container host_list[], unsigned short number_of_hosts,
255 mp_check overall[static 1]);
256
257/* Error exit */
258static void crash(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
234 259
235/** global variables **/ 260/** global variables **/
236static struct rta_host **table, *cursor, *list; 261static int debug = 0;
237 262
238static threshold crit = {.pl = 80, .rta = 500000, .jitter = 0.0, .mos = 0.0, .score = 0.0}; 263extern unsigned int timeout;
239static threshold warn = {.pl = 40, .rta = 200000, .jitter = 0.0, .mos = 0.0, .score = 0.0}; 264
240 265/** the working code **/
241static int mode, protocols, sockets, debug = 0, timeout = 10; 266static inline unsigned short targets_alive(unsigned short targets, unsigned short targets_down) {
242static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE; 267 return targets - targets_down;
243static unsigned short icmp_pkt_size = DEFAULT_PING_DATA_SIZE + ICMP_MINLEN; 268}
244 269static inline unsigned int icmp_pkts_en_route(unsigned int icmp_sent, unsigned int icmp_recv,
245static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0, ttl = 0; 270 unsigned int icmp_lost) {
246#define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost)) 271 return icmp_sent - (icmp_recv + icmp_lost);
247static unsigned short targets_down = 0, targets = 0, packets = 0; 272}
248#define targets_alive (targets - targets_down) 273
249static unsigned int retry_interval, pkt_interval, target_interval; 274// Create configuration from cli parameters
250static int icmp_sock, tcp_sock, udp_sock, status = STATE_OK; 275typedef struct {
251static pid_t pid; 276 int errorcode;
252static struct timezone tz; 277 check_icmp_config config;
253static struct timeval prog_start; 278} check_icmp_config_wrapper;
254static unsigned long long max_completion_time = 0; 279check_icmp_config_wrapper process_arguments(int argc, char **argv) {
255static unsigned int warn_down = 1, crit_down = 1; /* host down threshold values */ 280 check_icmp_config_wrapper result = {
256static int min_hosts_alive = -1; 281 .errorcode = OK,
257static float pkt_backoff_factor = 1.5; 282 .config = check_icmp_config_init(),
258static float target_backoff_factor = 1.5; 283 };
259static bool rta_mode = false; 284
260static bool pl_mode = false; 285 /* use the pid to mark packets as ours */
261static bool jitter_mode = false; 286 /* Some systems have 32-bit pid_t so mask off only 16 bits */
262static bool score_mode = false; 287 result.config.sender_id = getpid() & 0xffff;
263static bool mos_mode = false; 288
264static bool order_mode = false; 289 if (!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) {
290 result.config.mode = MODE_ICMP;
291 } else if (!strcmp(progname, "check_host")) {
292 result.config.mode = MODE_HOSTCHECK;
293 result.config.number_of_packets = 5;
294 result.config.crit.rta = result.config.warn.rta = 1000000;
295 result.config.crit.pl = result.config.warn.pl = 100;
296 } else if (!strcmp(progname, "check_rta_multi")) {
297 result.config.mode = MODE_ALL;
298 result.config.target_interval = 0;
299 result.config.number_of_packets = 5;
300 }
301 /* support "--help" and "--version" */
302 if (argc == 2) {
303 if (!strcmp(argv[1], "--help")) {
304 strcpy(argv[1], "-h");
305 }
306 if (!strcmp(argv[1], "--version")) {
307 strcpy(argv[1], "-V");
308 }
309 }
310
311 sa_family_t enforced_ai_family = AF_UNSPEC;
312
313 enum {
314 output_format_index = CHAR_MAX + 1,
315 };
316
317 struct option longopts[] = {
318 {"version", no_argument, 0, 'V'},
319 {"help", no_argument, 0, 'h'},
320 {"verbose", no_argument, 0, 'v'},
321 {"Host", required_argument, 0, 'H'},
322 {"ipv4-only", no_argument, 0, '4'},
323 {"ipv6-only", no_argument, 0, '6'},
324 {"warning", required_argument, 0, 'w'},
325 {"critical", required_argument, 0, 'c'},
326 {"rta-mode-thresholds", required_argument, 0, 'R'},
327 {"packet-loss-mode-thresholds", required_argument, 0, 'P'},
328 {"jitter-mode-thresholds", required_argument, 0, 'J'},
329 {"mos-mode-thresholds", required_argument, 0, 'M'},
330 {"score-mode-thresholds", required_argument, 0, 'S'},
331 {"out-of-order-packets", no_argument, 0, 'O'},
332 {"number-of-packets", required_argument, 0, 'n'},
333 {"number-of-packets", required_argument, 0, 'p'},
334 {"packet-interval", required_argument, 0, 'i'},
335 {"target-interval", required_argument, 0, 'I'},
336 {"minimal-host-alive", required_argument, 0, 'm'},
337 {"outgoing-ttl", required_argument, 0, 'l'},
338 {"size", required_argument, 0, 'b'},
339 {"output-format", required_argument, 0, output_format_index},
340 {},
341 };
342
343 // Parse protocol arguments first
344 // and count hosts here
345 char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64";
346 for (int i = 1; i < argc; i++) {
347 long int arg;
348 while ((arg = getopt_long(argc, argv, opts_str, longopts, NULL)) != EOF) {
349 switch (arg) {
350
351 case '4':
352 if (enforced_ai_family != AF_UNSPEC) {
353 crash("Multiple protocol versions not supported");
354 }
355 enforced_ai_family = AF_INET;
356 break;
357 case '6':
358 if (enforced_ai_family != AF_UNSPEC) {
359 crash("Multiple protocol versions not supported");
360 }
361 enforced_ai_family = AF_INET6;
362 break;
363 case 'H': {
364 result.config.number_of_hosts++;
365 break;
366 }
367 case 'h': /* help */
368 // Trigger help here to avoid adding hosts before that (and doing DNS queries)
369 print_help();
370 exit(STATE_UNKNOWN);
371 break;
372 case 'v':
373 debug++;
374 break;
375 }
376 }
377 }
378
379 char **tmp = &argv[optind];
380 while (*tmp) {
381 result.config.number_of_hosts++;
382 tmp++;
383 }
384
385 // Sanity check: if hostmode is selected,only a single host is allowed
386 if (result.config.mode == MODE_HOSTCHECK && result.config.number_of_hosts > 1) {
387 usage("check_host only allows a single host");
388 }
389
390 // Allocate hosts
391 result.config.hosts =
392 calloc(result.config.number_of_hosts, sizeof(check_icmp_target_container));
393 if (result.config.hosts == NULL) {
394 crash("failed to allocate memory");
395 }
396
397 /* Reset argument scanning */
398 optind = 1;
399
400 int host_counter = 0;
401 /* parse the arguments */
402 for (int i = 1; i < argc; i++) {
403 long int arg;
404 while ((arg = getopt_long(argc, argv, opts_str, longopts, NULL)) != EOF) {
405 switch (arg) {
406 case 'b': {
407 long size = strtol(optarg, NULL, 0);
408 if ((unsigned long)size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) &&
409 size < MAX_PING_DATA) {
410 result.config.icmp_data_size = (unsigned short)size;
411 } else {
412 usage_va("ICMP data length must be between: %lu and %lu",
413 sizeof(struct icmp) + sizeof(struct icmp_ping_data),
414 MAX_PING_DATA - 1);
415 }
416 } break;
417 case 'i': {
418 // packet_interval was unused and is now removed
419 } break;
420 case 'I': {
421 get_timevar_wrapper parsed_time = get_timevar(optarg);
422
423 if (parsed_time.error_code == OK) {
424 result.config.target_interval = parsed_time.time_range;
425 } else {
426 crash("failed to parse target interval");
427 }
428 } break;
429 case 'w': {
430 get_threshold_wrapper warn = get_threshold(optarg, result.config.warn);
431 if (warn.errorcode == OK) {
432 result.config.warn = warn.threshold;
433 } else {
434 crash("failed to parse warning threshold");
435 }
436 } break;
437 case 'c': {
438 get_threshold_wrapper crit = get_threshold(optarg, result.config.crit);
439 if (crit.errorcode == OK) {
440 result.config.crit = crit.threshold;
441 } else {
442 crash("failed to parse critical threshold");
443 }
444 } break;
445 case 'n':
446 case 'p':
447 result.config.number_of_packets = (unsigned short)strtoul(optarg, NULL, 0);
448 if (result.config.number_of_packets > 20) {
449 errno = 0;
450 crash("packets is > 20 (%d)", result.config.number_of_packets);
451 }
452 break;
453 case 't':
454 // WARNING Deprecated since execution time is determined by the other factors
455 break;
456 case 'H': {
457 add_host_wrapper host_add_result =
458 add_host(optarg, result.config.mode, enforced_ai_family);
459 if (host_add_result.error_code == OK) {
460 result.config.hosts[host_counter] = host_add_result.host;
461 host_counter++;
462
463 if (result.config.targets != NULL) {
464 result.config.number_of_targets += ping_target_list_append(
465 result.config.targets, host_add_result.host.target_list);
466 } else {
467 result.config.targets = host_add_result.host.target_list;
468 result.config.number_of_targets += host_add_result.host.number_of_targets;
469 }
470
471 if (host_add_result.has_v4) {
472 result.config.need_v4 = true;
473 }
474 if (host_add_result.has_v6) {
475 result.config.need_v6 = true;
476 }
477 } else {
478 crash("Failed to add host, unable to parse it correctly");
479 }
480 } break;
481 case 'l':
482 result.config.ttl = strtoul(optarg, NULL, 0);
483 break;
484 case 'm':
485 result.config.min_hosts_alive = (int)strtoul(optarg, NULL, 0);
486 break;
487 case 's': /* specify source IP address */
488 result.config.source_ip = optarg;
489 break;
490 case 'V': /* version */
491 print_revision(progname, NP_VERSION);
492 exit(STATE_UNKNOWN);
493 case 'R': /* RTA mode */ {
494 get_threshold2_wrapper rta_th = get_threshold2(
495 optarg, strlen(optarg), result.config.warn, result.config.crit, const_rta_mode);
496
497 if (rta_th.errorcode != OK) {
498 crash("Failed to parse RTA threshold");
499 }
500
501 result.config.warn = rta_th.warn;
502 result.config.crit = rta_th.crit;
503 result.config.modes.rta_mode = true;
504 } break;
505 case 'P': /* packet loss mode */ {
506 get_threshold2_wrapper pl_th =
507 get_threshold2(optarg, strlen(optarg), result.config.warn, result.config.crit,
508 const_packet_loss_mode);
509 if (pl_th.errorcode != OK) {
510 crash("Failed to parse packet loss threshold");
511 }
512
513 result.config.warn = pl_th.warn;
514 result.config.crit = pl_th.crit;
515 result.config.modes.pl_mode = true;
516 } break;
517 case 'J': /* jitter mode */ {
518 get_threshold2_wrapper jitter_th =
519 get_threshold2(optarg, strlen(optarg), result.config.warn, result.config.crit,
520 const_jitter_mode);
521 if (jitter_th.errorcode != OK) {
522 crash("Failed to parse jitter threshold");
523 }
524
525 result.config.warn = jitter_th.warn;
526 result.config.crit = jitter_th.crit;
527 result.config.modes.jitter_mode = true;
528 } break;
529 case 'M': /* MOS mode */ {
530 get_threshold2_wrapper mos_th = get_threshold2(
531 optarg, strlen(optarg), result.config.warn, result.config.crit, const_mos_mode);
532 if (mos_th.errorcode != OK) {
533 crash("Failed to parse MOS threshold");
534 }
535
536 result.config.warn = mos_th.warn;
537 result.config.crit = mos_th.crit;
538 result.config.modes.mos_mode = true;
539 } break;
540 case 'S': /* score mode */ {
541 get_threshold2_wrapper score_th =
542 get_threshold2(optarg, strlen(optarg), result.config.warn, result.config.crit,
543 const_score_mode);
544 if (score_th.errorcode != OK) {
545 crash("Failed to parse score threshold");
546 }
547
548 result.config.warn = score_th.warn;
549 result.config.crit = score_th.crit;
550 result.config.modes.score_mode = true;
551 } break;
552 case 'O': /* out of order mode */
553 result.config.modes.order_mode = true;
554 break;
555 case output_format_index: {
556 parsed_output_format parser = mp_parse_output_format(optarg);
557 if (!parser.parsing_success) {
558 // TODO List all available formats here, maybe add anothoer usage function
559 printf("Invalid output format: %s\n", optarg);
560 exit(STATE_UNKNOWN);
561 }
562
563 result.config.output_format_is_set = true;
564 result.config.output_format = parser.output_format;
565 break;
566 }
567 }
568 }
569 }
570
571 argv = &argv[optind];
572 while (*argv) {
573 add_target(*argv, result.config.mode, enforced_ai_family);
574 argv++;
575 }
576
577 if (!result.config.number_of_targets) {
578 errno = 0;
579 crash("No hosts to check");
580 }
581
582 /* stupid users should be able to give whatever thresholds they want
583 * (nothing will break if they do), but some anal plugin maintainer
584 * will probably add some printf() thing here later, so it might be
585 * best to at least show them where to do it. ;) */
586 if (result.config.warn.pl > result.config.crit.pl) {
587 result.config.warn.pl = result.config.crit.pl;
588 }
589 if (result.config.warn.rta > result.config.crit.rta) {
590 result.config.warn.rta = result.config.crit.rta;
591 }
592 if (result.config.warn.jitter > result.config.crit.jitter) {
593 result.config.crit.jitter = result.config.warn.jitter;
594 }
595 if (result.config.warn.mos < result.config.crit.mos) {
596 result.config.warn.mos = result.config.crit.mos;
597 }
598 if (result.config.warn.score < result.config.crit.score) {
599 result.config.warn.score = result.config.crit.score;
600 }
601
602 return result;
603}
265 604
266/** code start **/ 605/** code start **/
267static void crash(const char *fmt, ...) { 606static void crash(const char *fmt, ...) {
268 va_list ap;
269 607
270 printf("%s: ", progname); 608 printf("%s: ", progname);
271 609
610 va_list ap;
272 va_start(ap, fmt); 611 va_start(ap, fmt);
273 vprintf(fmt, ap); 612 vprintf(fmt, ap);
274 va_end(ap); 613 va_end(ap);
@@ -385,18 +724,20 @@ static const char *get_icmp_error_msg(unsigned char icmp_type, unsigned char icm
385 return msg; 724 return msg;
386} 725}
387 726
388static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr) { 727static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr,
389 struct icmp p, sent_icmp; 728 time_t *target_interval, const uint16_t sender_id,
390 struct rta_host *host = NULL; 729 ping_target **table, unsigned short packets,
391 730 const unsigned short number_of_targets,
392 memcpy(&p, packet, sizeof(p)); 731 check_icmp_state *program_state) {
393 if (p.icmp_type == ICMP_ECHO && ntohs(p.icmp_id) == pid) { 732 struct icmp icmp_packet;
733 memcpy(&icmp_packet, packet, sizeof(icmp_packet));
734 if (icmp_packet.icmp_type == ICMP_ECHO && ntohs(icmp_packet.icmp_id) == sender_id) {
394 /* echo request from us to us (pinging localhost) */ 735 /* echo request from us to us (pinging localhost) */
395 return 0; 736 return 0;
396 } 737 }
397 738
398 if (debug) { 739 if (debug) {
399 printf("handle_random_icmp(%p, %p)\n", (void *)&p, (void *)addr); 740 printf("handle_random_icmp(%p, %p)\n", (void *)&icmp_packet, (void *)addr);
400 } 741 }
401 742
402 /* only handle a few types, since others can't possibly be replies to 743 /* only handle a few types, since others can't possibly be replies to
@@ -409,14 +750,17 @@ static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *ad
409 * TIMXCEED actually sends a proper icmp response we will have passed 750 * TIMXCEED actually sends a proper icmp response we will have passed
410 * too many hops to have a hope of reaching it later, in which case it 751 * too many hops to have a hope of reaching it later, in which case it
411 * indicates overconfidence in the network, poor routing or both. */ 752 * indicates overconfidence in the network, poor routing or both. */
412 if (p.icmp_type != ICMP_UNREACH && p.icmp_type != ICMP_TIMXCEED && p.icmp_type != ICMP_SOURCEQUENCH && p.icmp_type != ICMP_PARAMPROB) { 753 if (icmp_packet.icmp_type != ICMP_UNREACH && icmp_packet.icmp_type != ICMP_TIMXCEED &&
754 icmp_packet.icmp_type != ICMP_SOURCEQUENCH && icmp_packet.icmp_type != ICMP_PARAMPROB) {
413 return 0; 755 return 0;
414 } 756 }
415 757
416 /* might be for us. At least it holds the original package (according 758 /* might be for us. At least it holds the original package (according
417 * to RFC 792). If it isn't, just ignore it */ 759 * to RFC 792). If it isn't, just ignore it */
760 struct icmp sent_icmp;
418 memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp)); 761 memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
419 if (sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != pid || ntohs(sent_icmp.icmp_seq) >= targets * packets) { 762 if (sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != sender_id ||
763 ntohs(sent_icmp.icmp_seq) >= number_of_targets * packets) {
420 if (debug) { 764 if (debug) {
421 printf("Packet is no response to a packet we sent\n"); 765 printf("Packet is no response to a packet we sent\n");
422 } 766 }
@@ -424,14 +768,15 @@ static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *ad
424 } 768 }
425 769
426 /* it is indeed a response for us */ 770 /* it is indeed a response for us */
427 host = table[ntohs(sent_icmp.icmp_seq) / packets]; 771 ping_target *host = table[ntohs(sent_icmp.icmp_seq) / packets];
428 if (debug) { 772 if (debug) {
429 char address[INET6_ADDRSTRLEN]; 773 char address[INET6_ADDRSTRLEN];
430 parse_address(addr, address, sizeof(address)); 774 parse_address(addr, address, sizeof(address));
431 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); 775 printf("Received \"%s\" from %s for ICMP ECHO sent.\n",
776 get_icmp_error_msg(icmp_packet.icmp_type, icmp_packet.icmp_code), address);
432 } 777 }
433 778
434 icmp_lost++; 779 program_state->icmp_lost++;
435 host->icmp_lost++; 780 host->icmp_lost++;
436 /* don't spend time on lost hosts any more */ 781 /* don't spend time on lost hosts any more */
437 if (host->flags & FLAG_LOST_CAUSE) { 782 if (host->flags & FLAG_LOST_CAUSE) {
@@ -440,305 +785,112 @@ static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *ad
440 785
441 /* source quench means we're sending too fast, so increase the 786 /* source quench means we're sending too fast, so increase the
442 * interval and mark this packet lost */ 787 * interval and mark this packet lost */
443 if (p.icmp_type == ICMP_SOURCEQUENCH) { 788 if (icmp_packet.icmp_type == ICMP_SOURCEQUENCH) {
444 pkt_interval *= pkt_backoff_factor; 789 *target_interval = (unsigned int)((double)*target_interval * TARGET_BACKOFF_FACTOR);
445 target_interval *= target_backoff_factor;
446 } else { 790 } else {
447 targets_down++; 791 program_state->targets_down++;
448 host->flags |= FLAG_LOST_CAUSE; 792 host->flags |= FLAG_LOST_CAUSE;
449 } 793 }
450 host->icmp_type = p.icmp_type; 794 host->icmp_type = icmp_packet.icmp_type;
451 host->icmp_code = p.icmp_code; 795 host->icmp_code = icmp_packet.icmp_code;
452 host->error_addr = *addr; 796 host->error_addr = *addr;
453 797
454 return 0; 798 return 0;
455} 799}
456 800
457void parse_address(struct sockaddr_storage *addr, char *address, int size) { 801void parse_address(const struct sockaddr_storage *addr, char *dst, socklen_t size) {
458 switch (address_family) { 802 switch (addr->ss_family) {
459 case AF_INET: 803 case AF_INET:
460 inet_ntop(address_family, &((struct sockaddr_in *)addr)->sin_addr, address, size); 804 inet_ntop(AF_INET, &((struct sockaddr_in *)addr)->sin_addr, dst, size);
461 break; 805 break;
462 case AF_INET6: 806 case AF_INET6:
463 inet_ntop(address_family, &((struct sockaddr_in6 *)addr)->sin6_addr, address, size); 807 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)addr)->sin6_addr, dst, size);
464 break; 808 break;
809 default:
810 assert(false);
465 } 811 }
466} 812}
467 813
468int main(int argc, char **argv) { 814int main(int argc, char **argv) {
469 int i;
470 char *ptr;
471 long int arg;
472 int icmp_sockerrno, udp_sockerrno, tcp_sockerrno;
473 int result;
474 struct rta_host *host;
475#ifdef HAVE_SIGACTION
476 struct sigaction sig_action;
477#endif
478#ifdef SO_TIMESTAMP
479 int on = 1;
480#endif
481 char *source_ip = NULL;
482 char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64";
483 setlocale(LC_ALL, ""); 815 setlocale(LC_ALL, "");
484 bindtextdomain(PACKAGE, LOCALEDIR); 816 bindtextdomain(PACKAGE, LOCALEDIR);
485 textdomain(PACKAGE); 817 textdomain(PACKAGE);
486 818
487 /* we only need to be setsuid when we get the sockets, so do 819 /* POSIXLY_CORRECT might break things, so unset it (the portable way) */
488 * that before pointer magic (esp. on network data) */ 820 environ = NULL;
489 icmp_sockerrno = udp_sockerrno = tcp_sockerrno = sockets = 0;
490
491 address_family = -1;
492 int icmp_proto = IPPROTO_ICMP;
493 821
494 /* get calling name the old-fashioned way for portability instead 822 /* determine program- and service-name quickly */
495 * of relying on the glibc-ism __progname */ 823 progname = strrchr(argv[0], '/');
496 ptr = strrchr(argv[0], '/'); 824 if (progname != NULL) {
497 if (ptr) { 825 progname++;
498 progname = &ptr[1];
499 } else { 826 } else {
500 progname = argv[0]; 827 progname = argv[0];
501 } 828 }
502 829
503 /* now set defaults. Use progname to set them initially (allows for 830 /* Parse extra opts if any */
504 * superfast check_host program when target host is up */ 831 argv = np_extra_opts(&argc, argv, progname);
505 cursor = list = NULL;
506 table = NULL;
507
508 mode = MODE_RTA;
509 /* Default critical thresholds */
510 crit.rta = 500000;
511 crit.pl = 80;
512 crit.jitter = 50;
513 crit.mos = 3;
514 crit.score = 70;
515 /* Default warning thresholds */
516 warn.rta = 200000;
517 warn.pl = 40;
518 warn.jitter = 40;
519 warn.mos = 3.5;
520 warn.score = 80;
521
522 protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP;
523 pkt_interval = 80000; /* 80 msec packet interval by default */
524 packets = 5;
525 832
526 if (!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) { 833 check_icmp_config_wrapper tmp_config = process_arguments(argc, argv);
527 mode = MODE_ICMP;
528 protocols = HAVE_ICMP;
529 } else if (!strcmp(progname, "check_host")) {
530 mode = MODE_HOSTCHECK;
531 pkt_interval = 1000000;
532 packets = 5;
533 crit.rta = warn.rta = 1000000;
534 crit.pl = warn.pl = 100;
535 } else if (!strcmp(progname, "check_rta_multi")) {
536 mode = MODE_ALL;
537 target_interval = 0;
538 pkt_interval = 50000;
539 packets = 5;
540 }
541 834
542 /* support "--help" and "--version" */ 835 if (tmp_config.errorcode != OK) {
543 if (argc == 2) { 836 crash("failed to parse config");
544 if (!strcmp(argv[1], "--help")) {
545 strcpy(argv[1], "-h");
546 }
547 if (!strcmp(argv[1], "--version")) {
548 strcpy(argv[1], "-V");
549 }
550 } 837 }
551 838
552 /* Parse protocol arguments first */ 839 const check_icmp_config config = tmp_config.config;
553 for (i = 1; i < argc; i++) { 840
554 while ((arg = getopt(argc, argv, opts_str)) != EOF) { 841 if (config.output_format_is_set) {
555 switch (arg) { 842 mp_set_format(config.output_format);
556 case '4':
557 if (address_family != -1) {
558 crash("Multiple protocol versions not supported");
559 }
560 address_family = AF_INET;
561 break;
562 case '6':
563#ifdef USE_IPV6
564 if (address_family != -1) {
565 crash("Multiple protocol versions not supported");
566 }
567 address_family = AF_INET6;
568#else
569 usage(_("IPv6 support not available\n"));
570#endif
571 break;
572 }
573 }
574 } 843 }
575 844
576 /* Reset argument scanning */ 845 check_icmp_socket_set sockset = {
577 optind = 1; 846 .socket4 = -1,
847 .socket6 = -1,
848 };
578 849
579 unsigned long size; 850 if (config.need_v4) {
580 bool err; 851 sockset.socket4 = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
581 /* parse the arguments */ 852 if (sockset.socket4 == -1) {
582 for (i = 1; i < argc; i++) { 853 crash("Failed to obtain ICMP v4 socket");
583 while ((arg = getopt(argc, argv, opts_str)) != EOF) { 854 }
584 switch (arg) {
585 case 'v':
586 debug++;
587 break;
588 case 'b':
589 size = strtol(optarg, NULL, 0);
590 if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) && size < MAX_PING_DATA) {
591 icmp_data_size = size;
592 icmp_pkt_size = size + ICMP_MINLEN;
593 } else {
594 usage_va("ICMP data length must be between: %lu and %lu", sizeof(struct icmp) + sizeof(struct icmp_ping_data),
595 MAX_PING_DATA - 1);
596 }
597 break;
598 case 'i':
599 pkt_interval = get_timevar(optarg);
600 break;
601 case 'I':
602 target_interval = get_timevar(optarg);
603 break;
604 case 'w':
605 get_threshold(optarg, &warn);
606 break;
607 case 'c':
608 get_threshold(optarg, &crit);
609 break;
610 case 'n':
611 case 'p':
612 packets = strtoul(optarg, NULL, 0);
613 break;
614 case 't':
615 timeout = strtoul(optarg, NULL, 0);
616 if (!timeout) {
617 timeout = 10;
618 }
619 break;
620 case 'H':
621 add_target(optarg);
622 break;
623 case 'l':
624 ttl = (int)strtoul(optarg, NULL, 0);
625 break;
626 case 'm':
627 min_hosts_alive = (int)strtoul(optarg, NULL, 0);
628 break;
629 case 'd': /* implement later, for cluster checks */
630 warn_down = (unsigned char)strtoul(optarg, &ptr, 0);
631 if (ptr) {
632 crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0);
633 }
634 break;
635 case 's': /* specify source IP address */
636 source_ip = optarg;
637 break;
638 case 'V': /* version */
639 print_revision(progname, NP_VERSION);
640 exit(STATE_UNKNOWN);
641 case 'h': /* help */
642 print_help();
643 exit(STATE_UNKNOWN);
644 break;
645 case 'R': /* RTA mode */
646 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_rta_mode);
647 if (!err) {
648 crash("Failed to parse RTA threshold");
649 }
650 855
651 rta_mode = true; 856 if (config.source_ip) {
652 break;
653 case 'P': /* packet loss mode */
654 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_packet_loss_mode);
655 if (!err) {
656 crash("Failed to parse packet loss threshold");
657 }
658 857
659 pl_mode = true; 858 struct in_addr tmp = {};
660 break; 859 int error_code = inet_pton(AF_INET, config.source_ip, &tmp);
661 case 'J': /* jitter mode */ 860 if (error_code == 1) {
662 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_jitter_mode); 861 set_source_ip(config.source_ip, sockset.socket4, AF_INET);
663 if (!err) { 862 } else {
664 crash("Failed to parse jitter threshold"); 863 // just try this mindlessly if it's not a v4 address
665 } 864 set_source_ip(config.source_ip, sockset.socket6, AF_INET6);
865 }
866 }
666 867
667 jitter_mode = true; 868#ifdef SO_TIMESTAMP
668 break; 869 if (sockset.socket4 != -1) {
669 case 'M': /* MOS mode */ 870 int on = 1;
670 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_mos_mode); 871 if (setsockopt(sockset.socket4, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on))) {
671 if (!err) { 872 if (debug) {
672 crash("Failed to parse MOS threshold"); 873 printf("Warning: no SO_TIMESTAMP support\n");
673 } 874 }
674 875 }
675 mos_mode = true; 876 }
676 break; 877 if (sockset.socket6 != -1) {
677 case 'S': /* score mode */ 878 int on = 1;
678 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_score_mode); 879 if (setsockopt(sockset.socket6, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on))) {
679 if (!err) { 880 if (debug) {
680 crash("Failed to parse score threshold"); 881 printf("Warning: no SO_TIMESTAMP support\n");
681 } 882 }
682
683 score_mode = true;
684 break;
685 case 'O': /* out of order mode */
686 order_mode = true;
687 break;
688 } 883 }
689 } 884 }
885#endif // SO_TIMESTAMP
690 } 886 }
691 887
692 /* POSIXLY_CORRECT might break things, so unset it (the portable way) */ 888 if (config.need_v6) {
693 environ = NULL; 889 sockset.socket6 = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
694 890 if (sockset.socket6 == -1) {
695 /* use the pid to mark packets as ours */ 891 crash("Failed to obtain ICMP v6 socket");
696 /* Some systems have 32-bit pid_t so mask off only 16 bits */
697 pid = getpid() & 0xffff;
698 /* printf("pid = %u\n", pid); */
699
700 /* Parse extra opts if any */
701 argv = np_extra_opts(&argc, argv, progname);
702
703 argv = &argv[optind];
704 while (*argv) {
705 add_target(*argv);
706 argv++;
707 }
708
709 if (!targets) {
710 errno = 0;
711 crash("No hosts to check");
712 }
713
714 // add_target might change address_family
715 switch (address_family) {
716 case AF_INET:
717 icmp_proto = IPPROTO_ICMP;
718 break;
719 case AF_INET6:
720 icmp_proto = IPPROTO_ICMPV6;
721 break;
722 default:
723 crash("Address family not supported");
724 }
725 if ((icmp_sock = socket(address_family, SOCK_RAW, icmp_proto)) != -1) {
726 sockets |= HAVE_ICMP;
727 } else {
728 icmp_sockerrno = errno;
729 }
730
731 if (source_ip) {
732 set_source_ip(source_ip);
733 }
734
735#ifdef SO_TIMESTAMP
736 if (setsockopt(icmp_sock, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on))) {
737 if (debug) {
738 printf("Warning: no SO_TIMESTAMP support\n");
739 } 892 }
740 } 893 }
741#endif // SO_TIMESTAMP
742 894
743 /* now drop privileges (no effect if not setsuid or geteuid() == 0) */ 895 /* now drop privileges (no effect if not setsuid or geteuid() == 0) */
744 if (setuid(getuid()) == -1) { 896 if (setuid(getuid()) == -1) {
@@ -746,186 +898,179 @@ int main(int argc, char **argv) {
746 return 1; 898 return 1;
747 } 899 }
748 900
749 if (!sockets) { 901 if (sockset.socket4) {
750 if (icmp_sock == -1) { 902 int result = setsockopt(sockset.socket4, SOL_IP, IP_TTL, &config.ttl, sizeof(config.ttl));
751 errno = icmp_sockerrno;
752 crash("Failed to obtain ICMP socket");
753 return -1;
754 }
755 /* if(udp_sock == -1) { */
756 /* errno = icmp_sockerrno; */
757 /* crash("Failed to obtain UDP socket"); */
758 /* return -1; */
759 /* } */
760 /* if(tcp_sock == -1) { */
761 /* errno = icmp_sockerrno; */
762 /* crash("Failed to obtain TCP socker"); */
763 /* return -1; */
764 /* } */
765 }
766 if (!ttl) {
767 ttl = 64;
768 }
769
770 if (icmp_sock) {
771 result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
772 if (debug) { 903 if (debug) {
773 if (result == -1) { 904 if (result == -1) {
774 printf("setsockopt failed\n"); 905 printf("setsockopt failed\n");
775 } else { 906 } else {
776 printf("ttl set to %u\n", ttl); 907 printf("ttl set to %lu\n", config.ttl);
777 } 908 }
778 } 909 }
779 } 910 }
780 911
781 /* stupid users should be able to give whatever thresholds they want 912 if (sockset.socket6) {
782 * (nothing will break if they do), but some anal plugin maintainer 913 int result = setsockopt(sockset.socket6, SOL_IP, IP_TTL, &config.ttl, sizeof(config.ttl));
783 * will probably add some printf() thing here later, so it might be 914 if (debug) {
784 * best to at least show them where to do it. ;) */ 915 if (result == -1) {
785 if (warn.pl > crit.pl) { 916 printf("setsockopt failed\n");
786 warn.pl = crit.pl; 917 } else {
787 } 918 printf("ttl set to %lu\n", config.ttl);
788 if (warn.rta > crit.rta) { 919 }
789 warn.rta = crit.rta; 920 }
790 }
791 if (warn_down > crit_down) {
792 crit_down = warn_down;
793 }
794 if (warn.jitter > crit.jitter) {
795 crit.jitter = warn.jitter;
796 }
797 if (warn.mos < crit.mos) {
798 warn.mos = crit.mos;
799 }
800 if (warn.score < crit.score) {
801 warn.score = crit.score;
802 }
803
804#ifdef HAVE_SIGACTION
805 sig_action.sa_sigaction = NULL;
806 sig_action.sa_handler = finish;
807 sigfillset(&sig_action.sa_mask);
808 sig_action.sa_flags = SA_NODEFER | SA_RESTART;
809 sigaction(SIGINT, &sig_action, NULL);
810 sigaction(SIGHUP, &sig_action, NULL);
811 sigaction(SIGTERM, &sig_action, NULL);
812 sigaction(SIGALRM, &sig_action, NULL);
813#else /* HAVE_SIGACTION */
814 signal(SIGINT, finish);
815 signal(SIGHUP, finish);
816 signal(SIGTERM, finish);
817 signal(SIGALRM, finish);
818#endif /* HAVE_SIGACTION */
819 if (debug) {
820 printf("Setting alarm timeout to %u seconds\n", timeout);
821 } 921 }
822 alarm(timeout);
823 922
824 /* make sure we don't wait any longer than necessary */ 923 /* make sure we don't wait any longer than necessary */
825 gettimeofday(&prog_start, &tz); 924 struct timeval prog_start;
826 max_completion_time = ((targets * packets * pkt_interval) + (targets * target_interval)) + (targets * packets * crit.rta) + crit.rta; 925 gettimeofday(&prog_start, NULL);
926
927 time_t max_completion_time =
928 (config.target_interval * config.number_of_targets) +
929 (config.crit.rta * config.number_of_targets * config.number_of_packets) + config.crit.rta;
827 930
828 if (debug) { 931 if (debug) {
829 printf("packets: %u, targets: %u\n" 932 printf("packets: %u, targets: %u\n"
830 "target_interval: %0.3f, pkt_interval %0.3f\n" 933 "target_interval: %0.3f\n"
831 "crit.rta: %0.3f\n" 934 "crit.rta: %0.3f\n"
832 "max_completion_time: %0.3f\n", 935 "max_completion_time: %0.3f\n",
833 packets, targets, (float)target_interval / 1000, (float)pkt_interval / 1000, (float)crit.rta / 1000, 936 config.number_of_packets, config.number_of_targets,
937 (float)config.target_interval / 1000, (float)config.crit.rta / 1000,
834 (float)max_completion_time / 1000); 938 (float)max_completion_time / 1000);
835 } 939 }
836 940
837 if (debug) { 941 if (debug) {
838 if (max_completion_time > (u_int)timeout * 1000000) { 942 if (max_completion_time > (timeout * 1000000)) {
839 printf("max_completion_time: %llu timeout: %u\n", max_completion_time, timeout); 943 printf("max_completion_time: %ld timeout: %u\n", max_completion_time, timeout);
840 printf("Timeout must be at least %llu\n", max_completion_time / 1000000 + 1); 944 printf("Timeout must be at least %ld\n", (max_completion_time / 1000000) + 1);
841 } 945 }
842 } 946 }
843 947
844 if (debug) { 948 if (debug) {
845 printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n", crit.rta, crit.pl, warn.rta, warn.pl); 949 printf("crit = {%ld, %u%%}, warn = {%ld, %u%%}\n", config.crit.rta, config.crit.pl,
846 printf("pkt_interval: %u target_interval: %u retry_interval: %u\n", pkt_interval, target_interval, retry_interval); 950 config.warn.rta, config.warn.pl);
847 printf("icmp_pkt_size: %u timeout: %u\n", icmp_pkt_size, timeout); 951 printf("target_interval: %ld\n", config.target_interval);
952 printf("icmp_pkt_size: %u timeout: %u\n", config.icmp_data_size + ICMP_MINLEN, timeout);
848 } 953 }
849 954
850 if (packets > 20) { 955 if (config.min_hosts_alive < -1) {
851 errno = 0; 956 errno = 0;
852 crash("packets is > 20 (%d)", packets); 957 crash("minimum alive hosts is negative (%i)", config.min_hosts_alive);
853 } 958 }
854 959
855 if (min_hosts_alive < -1) { 960 // Build an index table of all targets
856 errno = 0; 961 ping_target *host = config.targets;
857 crash("minimum alive hosts is negative (%i)", min_hosts_alive); 962 ping_target **table = malloc(sizeof(ping_target *) * config.number_of_targets);
858 }
859
860 host = list;
861 table = malloc(sizeof(struct rta_host *) * targets);
862 if (!table) { 963 if (!table) {
863 crash("main(): malloc failed for host table"); 964 crash("main(): malloc failed for host table");
864 } 965 }
865 966
866 i = 0; 967 unsigned short target_index = 0;
867 while (host) { 968 while (host) {
868 host->id = i * packets; 969 host->id = target_index * config.number_of_packets;
869 table[i] = host; 970 table[target_index] = host;
870 host = host->next; 971 host = host->next;
871 i++; 972 target_index++;
872 } 973 }
873 974
874 run_checks(); 975 time_t target_interval = config.target_interval;
976
977 check_icmp_state program_state = check_icmp_state_init();
978
979 run_checks(config.icmp_data_size, &target_interval, config.sender_id, config.mode,
980 max_completion_time, prog_start, table, config.number_of_packets, sockset,
981 config.number_of_targets, &program_state);
875 982
876 errno = 0; 983 errno = 0;
877 finish(0);
878 984
879 return (0); 985 mp_check overall = mp_check_init();
880} 986 finish(0, config.modes, config.min_hosts_alive, config.warn, config.crit,
987 config.number_of_targets, &program_state, config.hosts, config.number_of_hosts,
988 &overall);
881 989
882static void run_checks(void) { 990 if (sockset.socket4) {
883 u_int i, t; 991 close(sockset.socket4);
884 u_int final_wait, time_passed; 992 }
993 if (sockset.socket6) {
994 close(sockset.socket6);
995 }
996
997 mp_exit(overall);
998}
885 999
1000static void run_checks(unsigned short icmp_pkt_size, time_t *target_interval,
1001 const uint16_t sender_id, const check_icmp_execution_mode mode,
1002 const time_t max_completion_time, const struct timeval prog_start,
1003 ping_target **table, const unsigned short packets,
1004 const check_icmp_socket_set sockset, const unsigned short number_of_targets,
1005 check_icmp_state *program_state) {
886 /* this loop might actually violate the pkt_interval or target_interval 1006 /* this loop might actually violate the pkt_interval or target_interval
887 * settings, but only if there aren't any packets on the wire which 1007 * settings, but only if there aren't any packets on the wire which
888 * indicates that the target can handle an increased packet rate */ 1008 * indicates that the target can handle an increased packet rate */
889 for (i = 0; i < packets; i++) { 1009 for (unsigned int packet_index = 0; packet_index < packets; packet_index++) {
890 for (t = 0; t < targets; t++) { 1010 for (unsigned int target_index = 0; target_index < number_of_targets; target_index++) {
891 /* don't send useless packets */ 1011 /* don't send useless packets */
892 if (!targets_alive) { 1012 if (!targets_alive(number_of_targets, program_state->targets_down)) {
893 finish(0); 1013 return;
894 } 1014 }
895 if (table[t]->flags & FLAG_LOST_CAUSE) { 1015 if (table[target_index]->flags & FLAG_LOST_CAUSE) {
896 if (debug) { 1016 if (debug) {
897 printf("%s is a lost cause. not sending any more\n", table[t]->name); 1017
1018 char address[INET6_ADDRSTRLEN];
1019 parse_address(&table[target_index]->address, address, sizeof(address));
1020 printf("%s is a lost cause. not sending any more\n", address);
898 } 1021 }
899 continue; 1022 continue;
900 } 1023 }
901 1024
902 /* we're still in the game, so send next packet */ 1025 /* we're still in the game, so send next packet */
903 (void)send_icmp_ping(icmp_sock, table[t]); 1026 (void)send_icmp_ping(sockset, table[target_index], icmp_pkt_size, sender_id,
904 wait_for_reply(icmp_sock, target_interval); 1027 program_state);
1028
1029 /* wrap up if all targets are declared dead */
1030 if (targets_alive(number_of_targets, program_state->targets_down) ||
1031 get_timevaldiff(prog_start, prog_start) < max_completion_time ||
1032 !(mode == MODE_HOSTCHECK && program_state->targets_down)) {
1033 wait_for_reply(sockset, *target_interval, icmp_pkt_size, target_interval, sender_id,
1034 table, packets, number_of_targets, program_state);
1035 }
1036 }
1037 if (targets_alive(number_of_targets, program_state->targets_down) ||
1038 get_timevaldiff_to_now(prog_start) < max_completion_time ||
1039 !(mode == MODE_HOSTCHECK && program_state->targets_down)) {
1040 wait_for_reply(sockset, number_of_targets, icmp_pkt_size, target_interval, sender_id,
1041 table, packets, number_of_targets, program_state);
905 } 1042 }
906 wait_for_reply(icmp_sock, pkt_interval * targets);
907 } 1043 }
908 1044
909 if (icmp_pkts_en_route && targets_alive) { 1045 if (icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv,
910 time_passed = get_timevaldiff(NULL, NULL); 1046 program_state->icmp_lost) &&
911 final_wait = max_completion_time - time_passed; 1047 targets_alive(number_of_targets, program_state->targets_down)) {
1048 time_t time_passed = get_timevaldiff_to_now(prog_start);
1049 time_t final_wait = max_completion_time - time_passed;
912 1050
913 if (debug) { 1051 if (debug) {
914 printf("time_passed: %u final_wait: %u max_completion_time: %llu\n", time_passed, final_wait, max_completion_time); 1052 printf("time_passed: %ld final_wait: %ld max_completion_time: %ld\n", time_passed,
1053 final_wait, max_completion_time);
915 } 1054 }
916 if (time_passed > max_completion_time) { 1055 if (time_passed > max_completion_time) {
917 if (debug) { 1056 if (debug) {
918 printf("Time passed. Finishing up\n"); 1057 printf("Time passed. Finishing up\n");
919 } 1058 }
920 finish(0); 1059 return;
921 } 1060 }
922 1061
923 /* catch the packets that might come in within the timeframe, but 1062 /* catch the packets that might come in within the timeframe, but
924 * haven't yet */ 1063 * haven't yet */
925 if (debug) { 1064 if (debug) {
926 printf("Waiting for %u micro-seconds (%0.3f msecs)\n", final_wait, (float)final_wait / 1000); 1065 printf("Waiting for %ld micro-seconds (%0.3f msecs)\n", final_wait,
1066 (float)final_wait / 1000);
1067 }
1068 if (targets_alive(number_of_targets, program_state->targets_down) ||
1069 get_timevaldiff_to_now(prog_start) < max_completion_time ||
1070 !(mode == MODE_HOSTCHECK && program_state->targets_down)) {
1071 wait_for_reply(sockset, final_wait, icmp_pkt_size, target_interval, sender_id, table,
1072 packets, number_of_targets, program_state);
927 } 1073 }
928 wait_for_reply(icmp_sock, final_wait);
929 } 1074 }
930} 1075}
931 1076
@@ -939,18 +1084,11 @@ static void run_checks(void) {
939 * both: 1084 * both:
940 * icmp echo reply : the rest 1085 * icmp echo reply : the rest
941 */ 1086 */
942static int wait_for_reply(int sock, u_int t) { 1087static int wait_for_reply(check_icmp_socket_set sockset, const time_t time_interval,
943 int n, hlen; 1088 unsigned short icmp_pkt_size, time_t *target_interval, uint16_t sender_id,
944 static unsigned char buf[65536]; 1089 ping_target **table, const unsigned short packets,
945 struct sockaddr_storage resp_addr; 1090 const unsigned short number_of_targets, check_icmp_state *program_state) {
946 union ip_hdr *ip;
947 union icmp_packet packet; 1091 union icmp_packet packet;
948 struct rta_host *host;
949 struct icmp_ping_data data;
950 struct timeval wait_start, now;
951 u_int tdiff, i, per_pkt_wait;
952 double jitter_tmp;
953
954 if (!(packet.buf = malloc(icmp_pkt_size))) { 1092 if (!(packet.buf = malloc(icmp_pkt_size))) {
955 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", icmp_pkt_size); 1093 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", icmp_pkt_size);
956 return -1; /* might be reached if we're in debug mode */ 1094 return -1; /* might be reached if we're in debug mode */
@@ -959,177 +1097,174 @@ static int wait_for_reply(int sock, u_int t) {
959 memset(packet.buf, 0, icmp_pkt_size); 1097 memset(packet.buf, 0, icmp_pkt_size);
960 1098
961 /* if we can't listen or don't have anything to listen to, just return */ 1099 /* if we can't listen or don't have anything to listen to, just return */
962 if (!t || !icmp_pkts_en_route) { 1100 if (!time_interval || !icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv,
1101 program_state->icmp_lost)) {
963 free(packet.buf); 1102 free(packet.buf);
964 return 0; 1103 return 0;
965 } 1104 }
966 1105
967 gettimeofday(&wait_start, &tz); 1106 // Get current time stamp
1107 struct timeval wait_start;
1108 gettimeofday(&wait_start, NULL);
968 1109
969 i = t; 1110 struct sockaddr_storage resp_addr;
970 per_pkt_wait = t / icmp_pkts_en_route; 1111 time_t per_pkt_wait =
971 while (icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) { 1112 time_interval / icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv,
972 t = per_pkt_wait; 1113 program_state->icmp_lost);
973 1114 static unsigned char buf[65536];
974 /* wrap up if all targets are declared dead */ 1115 union ip_hdr *ip_header;
975 if (!targets_alive || get_timevaldiff(&prog_start, NULL) >= max_completion_time || (mode == MODE_HOSTCHECK && targets_down)) { 1116 struct timeval packet_received_timestamp;
976 finish(0); 1117 while (icmp_pkts_en_route(program_state->icmp_sent, program_state->icmp_recv,
977 } 1118 program_state->icmp_lost) &&
1119 get_timevaldiff_to_now(wait_start) < time_interval) {
1120 time_t loop_time_interval = per_pkt_wait;
978 1121
979 /* reap responses until we hit a timeout */ 1122 /* reap responses until we hit a timeout */
980 n = recvfrom_wto(sock, buf, sizeof(buf), (struct sockaddr *)&resp_addr, &t, &now); 1123 recvfrom_wto_wrapper recv_foo =
981 if (!n) { 1124 recvfrom_wto(sockset, buf, sizeof(buf), (struct sockaddr *)&resp_addr,
1125 &loop_time_interval, &packet_received_timestamp);
1126 if (!recv_foo.received) {
982 if (debug > 1) { 1127 if (debug > 1) {
983 printf("recvfrom_wto() timed out during a %u usecs wait\n", per_pkt_wait); 1128 printf("recvfrom_wto() timed out during a %ld usecs wait\n", per_pkt_wait);
984 } 1129 }
985 continue; /* timeout for this one, so keep trying */ 1130 continue; /* timeout for this one, so keep trying */
986 } 1131 }
987 if (n < 0) { 1132
1133 if (recv_foo.received < 0) {
988 if (debug) { 1134 if (debug) {
989 printf("recvfrom_wto() returned errors\n"); 1135 printf("recvfrom_wto() returned errors\n");
990 } 1136 }
991 free(packet.buf); 1137 free(packet.buf);
992 return n; 1138 return (int)recv_foo.received;
993 } 1139 }
994 1140
995 // FIXME: with ipv6 we don't have an ip header here 1141 if (recv_foo.recv_proto != AF_INET6) {
996 if (address_family != AF_INET6) { 1142 ip_header = (union ip_hdr *)buf;
997 ip = (union ip_hdr *)buf;
998 1143
999 if (debug > 1) { 1144 if (debug > 1) {
1000 char address[INET6_ADDRSTRLEN]; 1145 char address[INET6_ADDRSTRLEN];
1001 parse_address(&resp_addr, address, sizeof(address)); 1146 parse_address(&resp_addr, address, sizeof(address));
1002 printf("received %u bytes from %s\n", address_family == AF_INET6 ? ntohs(ip->ip6.ip6_plen) : ntohs(ip->ip.ip_len), address); 1147 printf("received %u bytes from %s\n",
1148 address_family == AF_INET6 ? ntohs(ip_header->ip6.ip6_plen)
1149 : ntohs(ip_header->ip.ip_len),
1150 address);
1003 } 1151 }
1004 } 1152 }
1005 1153
1006 /* obsolete. alpha on tru64 provides the necessary defines, but isn't broken */ 1154 int hlen = (recv_foo.recv_proto == AF_INET6) ? 0 : ip_header->ip.ip_hl << 2;
1007 /* #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) */ 1155
1008 /* alpha headers are decidedly broken. Using an ansi compiler, 1156 if (recv_foo.received < (hlen + ICMP_MINLEN)) {
1009 * they provide ip_vhl instead of ip_hl and ip_v, so we mask
1010 * off the bottom 4 bits */
1011 /* hlen = (ip->ip_vhl & 0x0f) << 2; */
1012 /* #else */
1013 hlen = (address_family == AF_INET6) ? 0 : ip->ip.ip_hl << 2;
1014 /* #endif */
1015
1016 if (n < (hlen + ICMP_MINLEN)) {
1017 char address[INET6_ADDRSTRLEN]; 1157 char address[INET6_ADDRSTRLEN];
1018 parse_address(&resp_addr, address, sizeof(address)); 1158 parse_address(&resp_addr, address, sizeof(address));
1019 crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n", n, hlen + icmp_pkt_size, address); 1159 crash("received packet too short for ICMP (%ld bytes, expected %d) from %s\n",
1160 recv_foo.received, hlen + icmp_pkt_size, address);
1020 } 1161 }
1021 /* else if(debug) { */
1022 /* printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */
1023 /* hlen, ntohs(ip->ip_len) - hlen, */
1024 /* sizeof(struct ip), icmp_pkt_size); */
1025 /* } */
1026
1027 /* check the response */ 1162 /* check the response */
1028
1029 memcpy(packet.buf, buf + hlen, icmp_pkt_size); 1163 memcpy(packet.buf, buf + hlen, icmp_pkt_size);
1030 /* address_family == AF_INET6 ? sizeof(struct icmp6_hdr)
1031 : sizeof(struct icmp));*/
1032 1164
1033 if ((address_family == PF_INET && (ntohs(packet.icp->icmp_id) != pid || packet.icp->icmp_type != ICMP_ECHOREPLY || 1165 if ((recv_foo.recv_proto == AF_INET &&
1034 ntohs(packet.icp->icmp_seq) >= targets * packets)) || 1166 (ntohs(packet.icp->icmp_id) != sender_id || packet.icp->icmp_type != ICMP_ECHOREPLY ||
1035 (address_family == PF_INET6 && (ntohs(packet.icp6->icmp6_id) != pid || packet.icp6->icmp6_type != ICMP6_ECHO_REPLY || 1167 ntohs(packet.icp->icmp_seq) >= number_of_targets * packets)) ||
1036 ntohs(packet.icp6->icmp6_seq) >= targets * packets))) { 1168 (recv_foo.recv_proto == AF_INET6 &&
1169 (ntohs(packet.icp6->icmp6_id) != sender_id ||
1170 packet.icp6->icmp6_type != ICMP6_ECHO_REPLY ||
1171 ntohs(packet.icp6->icmp6_seq) >= number_of_targets * packets))) {
1037 if (debug > 2) { 1172 if (debug > 2) {
1038 printf("not a proper ICMP_ECHOREPLY\n"); 1173 printf("not a proper ICMP_ECHOREPLY\n");
1039 } 1174 }
1040 handle_random_icmp(buf + hlen, &resp_addr); 1175
1176 handle_random_icmp(buf + hlen, &resp_addr, target_interval, sender_id, table, packets,
1177 number_of_targets, program_state);
1178
1041 continue; 1179 continue;
1042 } 1180 }
1043 1181
1044 /* this is indeed a valid response */ 1182 /* this is indeed a valid response */
1045 if (address_family == PF_INET) { 1183 ping_target *target;
1184 struct icmp_ping_data data;
1185 if (address_family == AF_INET) {
1046 memcpy(&data, packet.icp->icmp_data, sizeof(data)); 1186 memcpy(&data, packet.icp->icmp_data, sizeof(data));
1047 if (debug > 2) { 1187 if (debug > 2) {
1048 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", (unsigned long)sizeof(data), ntohs(packet.icp->icmp_id), 1188 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", sizeof(data),
1049 ntohs(packet.icp->icmp_seq), packet.icp->icmp_cksum); 1189 ntohs(packet.icp->icmp_id), ntohs(packet.icp->icmp_seq),
1190 packet.icp->icmp_cksum);
1050 } 1191 }
1051 host = table[ntohs(packet.icp->icmp_seq) / packets]; 1192 target = table[ntohs(packet.icp->icmp_seq) / packets];
1052 } else { 1193 } else {
1053 memcpy(&data, &packet.icp6->icmp6_dataun.icmp6_un_data8[4], sizeof(data)); 1194 memcpy(&data, &packet.icp6->icmp6_dataun.icmp6_un_data8[4], sizeof(data));
1054 if (debug > 2) { 1195 if (debug > 2) {
1055 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", (unsigned long)sizeof(data), ntohs(packet.icp6->icmp6_id), 1196 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", sizeof(data),
1056 ntohs(packet.icp6->icmp6_seq), packet.icp6->icmp6_cksum); 1197 ntohs(packet.icp6->icmp6_id), ntohs(packet.icp6->icmp6_seq),
1198 packet.icp6->icmp6_cksum);
1057 } 1199 }
1058 host = table[ntohs(packet.icp6->icmp6_seq) / packets]; 1200 target = table[ntohs(packet.icp6->icmp6_seq) / packets];
1059 } 1201 }
1060 1202
1061 tdiff = get_timevaldiff(&data.stime, &now); 1203 time_t tdiff = get_timevaldiff(data.stime, packet_received_timestamp);
1062 1204
1063 if (host->last_tdiff > 0) { 1205 if (target->last_tdiff > 0) {
1064 /* Calculate jitter */ 1206 /* Calculate jitter */
1065 if (host->last_tdiff > tdiff) { 1207 double jitter_tmp;
1066 jitter_tmp = host->last_tdiff - tdiff; 1208 if (target->last_tdiff > tdiff) {
1209 jitter_tmp = (double)(target->last_tdiff - tdiff);
1067 } else { 1210 } else {
1068 jitter_tmp = tdiff - host->last_tdiff; 1211 jitter_tmp = (double)(tdiff - target->last_tdiff);
1069 } 1212 }
1070 1213
1071 if (host->jitter == 0) { 1214 if (target->jitter == 0) {
1072 host->jitter = jitter_tmp; 1215 target->jitter = jitter_tmp;
1073 host->jitter_max = jitter_tmp; 1216 target->jitter_max = jitter_tmp;
1074 host->jitter_min = jitter_tmp; 1217 target->jitter_min = jitter_tmp;
1075 } else { 1218 } else {
1076 host->jitter += jitter_tmp; 1219 target->jitter += jitter_tmp;
1077 1220
1078 if (jitter_tmp < host->jitter_min) { 1221 if (jitter_tmp < target->jitter_min) {
1079 host->jitter_min = jitter_tmp; 1222 target->jitter_min = jitter_tmp;
1080 } 1223 }
1081 1224
1082 if (jitter_tmp > host->jitter_max) { 1225 if (jitter_tmp > target->jitter_max) {
1083 host->jitter_max = jitter_tmp; 1226 target->jitter_max = jitter_tmp;
1084 } 1227 }
1085 } 1228 }
1086 1229
1087 /* Check if packets in order */ 1230 /* Check if packets in order */
1088 if (host->last_icmp_seq >= packet.icp->icmp_seq) { 1231 if (target->last_icmp_seq >= packet.icp->icmp_seq) {
1089 host->order_status = STATE_CRITICAL; 1232 target->found_out_of_order_packets = true;
1090 } 1233 }
1091 } 1234 }
1092 host->last_tdiff = tdiff; 1235 target->last_tdiff = tdiff;
1093 1236
1094 host->last_icmp_seq = packet.icp->icmp_seq; 1237 target->last_icmp_seq = packet.icp->icmp_seq;
1095 1238
1096 host->time_waited += tdiff; 1239 target->time_waited += tdiff;
1097 host->icmp_recv++; 1240 target->icmp_recv++;
1098 icmp_recv++; 1241 program_state->icmp_recv++;
1099 1242
1100 if (tdiff > (unsigned int)host->rtmax) { 1243 if (tdiff > (unsigned int)target->rtmax) {
1101 host->rtmax = tdiff; 1244 target->rtmax = (double)tdiff;
1102 } 1245 }
1103 1246
1104 if ((host->rtmin == INFINITY) || (tdiff < (unsigned int)host->rtmin)) { 1247 if ((target->rtmin == INFINITY) || (tdiff < (unsigned int)target->rtmin)) {
1105 host->rtmin = tdiff; 1248 target->rtmin = (double)tdiff;
1106 } 1249 }
1107 1250
1108 if (debug) { 1251 if (debug) {
1109 char address[INET6_ADDRSTRLEN]; 1252 char address[INET6_ADDRSTRLEN];
1110 parse_address(&resp_addr, address, sizeof(address)); 1253 parse_address(&resp_addr, address, sizeof(address));
1111 1254
1112 switch (address_family) { 1255 switch (recv_foo.recv_proto) {
1113 case AF_INET: { 1256 case AF_INET: {
1114 printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000, address, 1257 printf("%0.3f ms rtt from %s, incoming ttl: %u, max: %0.3f, min: %0.3f\n",
1115 ttl, ip->ip.ip_ttl, (float)host->rtmax / 1000, (float)host->rtmin / 1000); 1258 (float)tdiff / 1000, address, ip_header->ip.ip_ttl,
1259 (float)target->rtmax / 1000, (float)target->rtmin / 1000);
1116 break; 1260 break;
1117 }; 1261 };
1118 case AF_INET6: { 1262 case AF_INET6: {
1119 printf("%0.3f ms rtt from %s, outgoing ttl: %u, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000, address, ttl, 1263 printf("%0.3f ms rtt from %s, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000,
1120 (float)host->rtmax / 1000, (float)host->rtmin / 1000); 1264 address, (float)target->rtmax / 1000, (float)target->rtmin / 1000);
1121 }; 1265 };
1122 } 1266 }
1123 } 1267 }
1124
1125 /* if we're in hostcheck mode, exit with limited printouts */
1126 if (mode == MODE_HOSTCHECK) {
1127 printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|"
1128 "pkt=%u;;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n",
1129 host->name, icmp_recv, (float)tdiff / 1000, icmp_recv, packets, (float)tdiff / 1000, (float)warn.rta / 1000,
1130 (float)crit.rta / 1000);
1131 exit(STATE_OK);
1132 }
1133 } 1268 }
1134 1269
1135 free(packet.buf); 1270 free(packet.buf);
@@ -1137,38 +1272,28 @@ static int wait_for_reply(int sock, u_int t) {
1137} 1272}
1138 1273
1139/* the ping functions */ 1274/* the ping functions */
1140static int send_icmp_ping(int sock, struct rta_host *host) { 1275static int send_icmp_ping(const check_icmp_socket_set sockset, ping_target *host,
1141 long int len; 1276 const unsigned short icmp_pkt_size, const uint16_t sender_id,
1142 size_t addrlen; 1277 check_icmp_state *program_state) {
1143 struct icmp_ping_data data; 1278 void *buf = calloc(1, icmp_pkt_size);
1144 struct msghdr hdr;
1145 struct iovec iov;
1146 struct timeval tv;
1147 void *buf = NULL;
1148
1149 if (sock == -1) {
1150 errno = 0;
1151 crash("Attempt to send on bogus socket");
1152 return -1;
1153 }
1154
1155 if (!buf) { 1279 if (!buf) {
1156 if (!(buf = malloc(icmp_pkt_size))) { 1280 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", icmp_pkt_size);
1157 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", icmp_pkt_size); 1281 return -1; /* might be reached if we're in debug mode */
1158 return -1; /* might be reached if we're in debug mode */
1159 }
1160 } 1282 }
1161 memset(buf, 0, icmp_pkt_size);
1162 1283
1163 if ((gettimeofday(&tv, &tz)) == -1) { 1284 struct timeval current_time;
1285 if ((gettimeofday(&current_time, NULL)) == -1) {
1164 free(buf); 1286 free(buf);
1165 return -1; 1287 return -1;
1166 } 1288 }
1167 1289
1290 struct icmp_ping_data data;
1168 data.ping_id = 10; /* host->icmp.icmp_sent; */ 1291 data.ping_id = 10; /* host->icmp.icmp_sent; */
1169 memcpy(&data.stime, &tv, sizeof(tv)); 1292 memcpy(&data.stime, &current_time, sizeof(current_time));
1293
1294 socklen_t addrlen = 0;
1170 1295
1171 if (address_family == AF_INET) { 1296 if (host->address.ss_family == AF_INET) {
1172 struct icmp *icp = (struct icmp *)buf; 1297 struct icmp *icp = (struct icmp *)buf;
1173 addrlen = sizeof(struct sockaddr_in); 1298 addrlen = sizeof(struct sockaddr_in);
1174 1299
@@ -1177,15 +1302,19 @@ static int send_icmp_ping(int sock, struct rta_host *host) {
1177 icp->icmp_type = ICMP_ECHO; 1302 icp->icmp_type = ICMP_ECHO;
1178 icp->icmp_code = 0; 1303 icp->icmp_code = 0;
1179 icp->icmp_cksum = 0; 1304 icp->icmp_cksum = 0;
1180 icp->icmp_id = htons(pid); 1305 icp->icmp_id = htons((uint16_t)sender_id);
1181 icp->icmp_seq = htons(host->id++); 1306 icp->icmp_seq = htons(host->id++);
1182 icp->icmp_cksum = icmp_checksum((uint16_t *)buf, (size_t)icmp_pkt_size); 1307 icp->icmp_cksum = icmp_checksum((uint16_t *)buf, (size_t)icmp_pkt_size);
1183 1308
1184 if (debug > 2) { 1309 if (debug > 2) {
1185 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", (unsigned long)sizeof(data), 1310 char address[INET6_ADDRSTRLEN];
1186 ntohs(icp->icmp_id), ntohs(icp->icmp_seq), icp->icmp_cksum, host->name); 1311 parse_address((&host->address), address, sizeof(address));
1312
1313 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n",
1314 sizeof(data), ntohs(icp->icmp_id), ntohs(icp->icmp_seq), icp->icmp_cksum,
1315 address);
1187 } 1316 }
1188 } else { 1317 } else if (host->address.ss_family == AF_INET6) {
1189 struct icmp6_hdr *icp6 = (struct icmp6_hdr *)buf; 1318 struct icmp6_hdr *icp6 = (struct icmp6_hdr *)buf;
1190 addrlen = sizeof(struct sockaddr_in6); 1319 addrlen = sizeof(struct sockaddr_in6);
1191 1320
@@ -1194,659 +1323,434 @@ static int send_icmp_ping(int sock, struct rta_host *host) {
1194 icp6->icmp6_type = ICMP6_ECHO_REQUEST; 1323 icp6->icmp6_type = ICMP6_ECHO_REQUEST;
1195 icp6->icmp6_code = 0; 1324 icp6->icmp6_code = 0;
1196 icp6->icmp6_cksum = 0; 1325 icp6->icmp6_cksum = 0;
1197 icp6->icmp6_id = htons(pid); 1326 icp6->icmp6_id = htons((uint16_t)sender_id);
1198 icp6->icmp6_seq = htons(host->id++); 1327 icp6->icmp6_seq = htons(host->id++);
1199 // let checksum be calculated automatically 1328 // let checksum be calculated automatically
1200 1329
1201 if (debug > 2) { 1330 if (debug > 2) {
1202 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", (unsigned long)sizeof(data), 1331 char address[INET6_ADDRSTRLEN];
1203 ntohs(icp6->icmp6_id), ntohs(icp6->icmp6_seq), icp6->icmp6_cksum, host->name); 1332 parse_address((&host->address), address, sizeof(address));
1333
1334 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to target %s\n",
1335 sizeof(data), ntohs(icp6->icmp6_id), ntohs(icp6->icmp6_seq), icp6->icmp6_cksum,
1336 address);
1204 } 1337 }
1338 } else {
1339 // unknown address family
1340 crash("unknown address family in %s", __func__);
1205 } 1341 }
1206 1342
1343 struct iovec iov;
1207 memset(&iov, 0, sizeof(iov)); 1344 memset(&iov, 0, sizeof(iov));
1208 iov.iov_base = buf; 1345 iov.iov_base = buf;
1209 iov.iov_len = icmp_pkt_size; 1346 iov.iov_len = icmp_pkt_size;
1210 1347
1348 struct msghdr hdr;
1211 memset(&hdr, 0, sizeof(hdr)); 1349 memset(&hdr, 0, sizeof(hdr));
1212 hdr.msg_name = (struct sockaddr *)&host->saddr_in; 1350 hdr.msg_name = (struct sockaddr *)&host->address;
1213 hdr.msg_namelen = addrlen; 1351 hdr.msg_namelen = addrlen;
1214 hdr.msg_iov = &iov; 1352 hdr.msg_iov = &iov;
1215 hdr.msg_iovlen = 1; 1353 hdr.msg_iovlen = 1;
1216 1354
1217 errno = 0; 1355 errno = 0;
1218 1356
1219/* MSG_CONFIRM is a linux thing and only available on linux kernels >= 2.3.15, see send(2) */ 1357 long int len;
1358 /* MSG_CONFIRM is a linux thing and only available on linux kernels >= 2.3.15, see send(2) */
1359 if (host->address.ss_family == AF_INET) {
1360#ifdef MSG_CONFIRM
1361 len = sendmsg(sockset.socket4, &hdr, MSG_CONFIRM);
1362#else
1363 len = sendmsg(sockset.socket4, &hdr, 0);
1364#endif
1365 } else if (host->address.ss_family == AF_INET6) {
1220#ifdef MSG_CONFIRM 1366#ifdef MSG_CONFIRM
1221 len = sendmsg(sock, &hdr, MSG_CONFIRM); 1367 len = sendmsg(sockset.socket6, &hdr, MSG_CONFIRM);
1222#else 1368#else
1223 len = sendmsg(sock, &hdr, 0); 1369 len = sendmsg(sockset.socket6, &hdr, 0);
1224#endif 1370#endif
1371 } else {
1372 assert(false);
1373 }
1225 1374
1226 free(buf); 1375 free(buf);
1227 1376
1228 if (len < 0 || (unsigned int)len != icmp_pkt_size) { 1377 if (len < 0 || (unsigned int)len != icmp_pkt_size) {
1229 if (debug) { 1378 if (debug) {
1230 char address[INET6_ADDRSTRLEN]; 1379 char address[INET6_ADDRSTRLEN];
1231 parse_address((struct sockaddr_storage *)&host->saddr_in, address, sizeof(address)); 1380 parse_address((&host->address), address, sizeof(address));
1232 printf("Failed to send ping to %s: %s\n", address, strerror(errno)); 1381 printf("Failed to send ping to %s: %s\n", address, strerror(errno));
1233 } 1382 }
1234 errno = 0; 1383 errno = 0;
1235 return -1; 1384 return -1;
1236 } 1385 }
1237 1386
1238 icmp_sent++; 1387 program_state->icmp_sent++;
1239 host->icmp_sent++; 1388 host->icmp_sent++;
1240 1389
1241 return 0; 1390 return 0;
1242} 1391}
1243 1392
1244static int recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr, u_int *timo, struct timeval *tv) { 1393static recvfrom_wto_wrapper recvfrom_wto(const check_icmp_socket_set sockset, void *buf,
1245 u_int slen; 1394 const unsigned int len, struct sockaddr *saddr,
1246 int n, ret; 1395 time_t *timeout, struct timeval *received_timestamp) {
1247 struct timeval to, then, now;
1248 fd_set rd, wr;
1249#ifdef HAVE_MSGHDR_MSG_CONTROL 1396#ifdef HAVE_MSGHDR_MSG_CONTROL
1250 char ans_data[4096]; 1397 char ans_data[4096];
1251#endif // HAVE_MSGHDR_MSG_CONTROL 1398#endif // HAVE_MSGHDR_MSG_CONTROL
1252 struct msghdr hdr;
1253 struct iovec iov;
1254#ifdef SO_TIMESTAMP 1399#ifdef SO_TIMESTAMP
1255 struct cmsghdr *chdr; 1400 struct cmsghdr *chdr;
1256#endif 1401#endif
1257 1402
1258 if (!*timo) { 1403 recvfrom_wto_wrapper result = {
1404 .received = 0,
1405 .recv_proto = AF_UNSPEC,
1406 };
1407
1408 if (!*timeout) {
1259 if (debug) { 1409 if (debug) {
1260 printf("*timo is not\n"); 1410 printf("*timeout is not\n");
1261 } 1411 }
1262 return 0; 1412 return result;
1413 }
1414
1415 struct timeval real_timeout;
1416 real_timeout.tv_sec = *timeout / 1000000;
1417 real_timeout.tv_usec = (*timeout - (real_timeout.tv_sec * 1000000));
1418
1419 // Dummy fds for select
1420 fd_set dummy_write_fds;
1421 FD_ZERO(&dummy_write_fds);
1422
1423 // Read fds for select with the socket
1424 fd_set read_fds;
1425 FD_ZERO(&read_fds);
1426
1427 if (sockset.socket4 != -1) {
1428 FD_SET(sockset.socket4, &read_fds);
1263 } 1429 }
1430 if (sockset.socket6 != -1) {
1431 FD_SET(sockset.socket6, &read_fds);
1432 }
1433
1434 int nfds = (sockset.socket4 > sockset.socket6 ? sockset.socket4 : sockset.socket6) + 1;
1264 1435
1265 to.tv_sec = *timo / 1000000; 1436 struct timeval then;
1266 to.tv_usec = (*timo - (to.tv_sec * 1000000)); 1437 gettimeofday(&then, NULL);
1267 1438
1268 FD_ZERO(&rd);
1269 FD_ZERO(&wr);
1270 FD_SET(sock, &rd);
1271 errno = 0; 1439 errno = 0;
1272 gettimeofday(&then, &tz); 1440 int select_return = select(nfds, &read_fds, &dummy_write_fds, NULL, &real_timeout);
1273 n = select(sock + 1, &rd, &wr, NULL, &to); 1441 if (select_return < 0) {
1274 if (n < 0) {
1275 crash("select() in recvfrom_wto"); 1442 crash("select() in recvfrom_wto");
1276 } 1443 }
1277 gettimeofday(&now, &tz);
1278 *timo = get_timevaldiff(&then, &now);
1279 1444
1280 if (!n) { 1445 struct timeval now;
1281 return 0; /* timeout */ 1446 gettimeofday(&now, NULL);
1447 *timeout = get_timevaldiff(then, now);
1448
1449 if (!select_return) {
1450 return result; /* timeout */
1282 } 1451 }
1283 1452
1284 slen = sizeof(struct sockaddr_storage); 1453 unsigned int slen = sizeof(struct sockaddr_storage);
1285 1454
1286 memset(&iov, 0, sizeof(iov)); 1455 struct iovec iov = {
1287 iov.iov_base = buf; 1456 .iov_base = buf,
1288 iov.iov_len = len; 1457 .iov_len = len,
1458 };
1289 1459
1290 memset(&hdr, 0, sizeof(hdr)); 1460 struct msghdr hdr = {
1291 hdr.msg_name = saddr; 1461 .msg_name = saddr,
1292 hdr.msg_namelen = slen; 1462 .msg_namelen = slen,
1293 hdr.msg_iov = &iov; 1463 .msg_iov = &iov,
1294 hdr.msg_iovlen = 1; 1464 .msg_iovlen = 1,
1295#ifdef HAVE_MSGHDR_MSG_CONTROL 1465#ifdef HAVE_MSGHDR_MSG_CONTROL
1296 hdr.msg_control = ans_data; 1466 .msg_control = ans_data,
1297 hdr.msg_controllen = sizeof(ans_data); 1467 .msg_controllen = sizeof(ans_data),
1298#endif 1468#endif
1469 };
1470
1471 ssize_t ret;
1472
1473 // Test explicitly whether sockets are in use
1474 // this is necessary at least on OpenBSD where FD_ISSET will segfault otherwise
1475 if ((sockset.socket4 != -1) && FD_ISSET(sockset.socket4, &read_fds)) {
1476 ret = recvmsg(sockset.socket4, &hdr, 0);
1477 result.recv_proto = AF_INET;
1478 } else if ((sockset.socket6 != -1) && FD_ISSET(sockset.socket6, &read_fds)) {
1479 ret = recvmsg(sockset.socket6, &hdr, 0);
1480 result.recv_proto = AF_INET6;
1481 } else {
1482 assert(false);
1483 }
1484
1485 result.received = ret;
1299 1486
1300 ret = recvmsg(sock, &hdr, 0);
1301#ifdef SO_TIMESTAMP 1487#ifdef SO_TIMESTAMP
1302 for (chdr = CMSG_FIRSTHDR(&hdr); chdr; chdr = CMSG_NXTHDR(&hdr, chdr)) { 1488 for (chdr = CMSG_FIRSTHDR(&hdr); chdr; chdr = CMSG_NXTHDR(&hdr, chdr)) {
1303 if (chdr->cmsg_level == SOL_SOCKET && chdr->cmsg_type == SO_TIMESTAMP && chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) { 1489 if (chdr->cmsg_level == SOL_SOCKET && chdr->cmsg_type == SO_TIMESTAMP &&
1304 memcpy(tv, CMSG_DATA(chdr), sizeof(*tv)); 1490 chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) {
1491 memcpy(received_timestamp, CMSG_DATA(chdr), sizeof(*received_timestamp));
1305 break; 1492 break;
1306 } 1493 }
1307 } 1494 }
1308 1495
1309 if (!chdr) 1496 if (!chdr) {
1497 gettimeofday(received_timestamp, NULL);
1498 }
1499#else
1500 gettimeofday(tv, NULL);
1310#endif // SO_TIMESTAMP 1501#endif // SO_TIMESTAMP
1311 gettimeofday(tv, &tz);
1312 return (ret);
1313}
1314 1502
1315static void finish(int sig) { 1503 return (result);
1316 u_int i = 0; 1504}
1317 unsigned char pl;
1318 double rta;
1319 struct rta_host *host;
1320 const char *status_string[] = {"OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPENDENT"};
1321 int hosts_ok = 0;
1322 int hosts_warn = 0;
1323 int this_status;
1324 double R;
1325 1505
1506static void finish(int sig, check_icmp_mode_switches modes, int min_hosts_alive,
1507 check_icmp_threshold warn, check_icmp_threshold crit,
1508 const unsigned short number_of_targets, check_icmp_state *program_state,
1509 check_icmp_target_container host_list[], unsigned short number_of_hosts,
1510 mp_check overall[static 1]) {
1511 // Deactivate alarm
1326 alarm(0); 1512 alarm(0);
1513
1327 if (debug > 1) { 1514 if (debug > 1) {
1328 printf("finish(%d) called\n", sig); 1515 printf("finish(%d) called\n", sig);
1329 } 1516 }
1330 1517
1331 if (icmp_sock != -1) {
1332 close(icmp_sock);
1333 }
1334 if (udp_sock != -1) {
1335 close(udp_sock);
1336 }
1337 if (tcp_sock != -1) {
1338 close(tcp_sock);
1339 }
1340
1341 if (debug) { 1518 if (debug) {
1342 printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n", icmp_sent, icmp_recv, icmp_lost); 1519 printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n", program_state->icmp_sent,
1343 printf("targets: %u targets_alive: %u\n", targets, targets_alive); 1520 program_state->icmp_recv, program_state->icmp_lost);
1521 printf("targets: %u targets_alive: %u\n", number_of_targets,
1522 targets_alive(number_of_targets, program_state->targets_down));
1344 } 1523 }
1345 1524
1346 /* iterate thrice to calculate values, give output, and print perfparse */ 1525 // loop over targets to evaluate each one
1347 status = STATE_OK; 1526 int targets_ok = 0;
1348 host = list; 1527 int targets_warn = 0;
1349 1528 for (unsigned short i = 0; i < number_of_hosts; i++) {
1350 while (host) { 1529 evaluate_host_wrapper host_check = evaluate_host(host_list[i], modes, warn, crit);
1351 this_status = STATE_OK;
1352
1353 if (!host->icmp_recv) {
1354 /* rta 0 is ofcourse not entirely correct, but will still show up
1355 * conspicuously as missing entries in perfparse and cacti */
1356 pl = 100;
1357 rta = 0;
1358 status = STATE_CRITICAL;
1359 /* up the down counter if not already counted */
1360 if (!(host->flags & FLAG_LOST_CAUSE) && targets_alive) {
1361 targets_down++;
1362 }
1363 } else {
1364 pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent;
1365 rta = (double)host->time_waited / host->icmp_recv;
1366 }
1367
1368 if (host->icmp_recv > 1) {
1369 /*
1370 * This algorithm is probably pretty much blindly copied from
1371 * locations like this one: https://www.slac.stanford.edu/comp/net/wan-mon/tutorial.html#mos
1372 * It calculates a MOS value (range of 1 to 5, where 1 is bad and 5 really good).
1373 * According to some quick research MOS originates from the Audio/Video transport network area.
1374 * Whether it can and should be computed from ICMP data, I can not say.
1375 *
1376 * Anyway the basic idea is to map a value "R" with a range of 0-100 to the MOS value
1377 *
1378 * MOS stands likely for Mean Opinion Score ( https://en.wikipedia.org/wiki/Mean_Opinion_Score )
1379 *
1380 * More links:
1381 * - https://confluence.slac.stanford.edu/display/IEPM/MOS
1382 */
1383 host->jitter = (host->jitter / (host->icmp_recv - 1) / 1000);
1384
1385 /*
1386 * Take the average round trip latency (in milliseconds), add
1387 * round trip jitter, but double the impact to latency
1388 * then add 10 for protocol latencies (in milliseconds).
1389 */
1390 host->EffectiveLatency = (rta / 1000) + host->jitter * 2 + 10;
1391
1392 if (host->EffectiveLatency < 160) {
1393 R = 93.2 - (host->EffectiveLatency / 40);
1394 } else {
1395 R = 93.2 - ((host->EffectiveLatency - 120) / 10);
1396 }
1397
1398 // Now, let us deduct 2.5 R values per percentage of packet loss (i.e. a
1399 // loss of 5% will be entered as 5).
1400 R = R - (pl * 2.5);
1401
1402 if (R < 0) {
1403 R = 0;
1404 }
1405 1530
1406 host->score = R; 1531 targets_ok += host_check.targets_ok;
1407 host->mos = 1 + ((0.035) * R) + ((.000007) * R * (R - 60) * (100 - R)); 1532 targets_warn += host_check.targets_warn;
1408 } else {
1409 host->jitter = 0;
1410 host->jitter_min = 0;
1411 host->jitter_max = 0;
1412 host->mos = 0;
1413 }
1414
1415 host->pl = pl;
1416 host->rta = rta;
1417
1418 /* if no new mode selected, use old schema */
1419 if (!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode && !order_mode) {
1420 rta_mode = true;
1421 pl_mode = true;
1422 }
1423 1533
1424 /* Check which mode is on and do the warn / Crit stuff */ 1534 mp_add_subcheck_to_check(overall, host_check.sc_host);
1425 if (rta_mode) {
1426 if (rta >= crit.rta) {
1427 this_status = STATE_CRITICAL;
1428 status = STATE_CRITICAL;
1429 host->rta_status = STATE_CRITICAL;
1430 } else if (status != STATE_CRITICAL && (rta >= warn.rta)) {
1431 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1432 status = STATE_WARNING;
1433 host->rta_status = STATE_WARNING;
1434 }
1435 }
1436
1437 if (pl_mode) {
1438 if (pl >= crit.pl) {
1439 this_status = STATE_CRITICAL;
1440 status = STATE_CRITICAL;
1441 host->pl_status = STATE_CRITICAL;
1442 } else if (status != STATE_CRITICAL && (pl >= warn.pl)) {
1443 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1444 status = STATE_WARNING;
1445 host->pl_status = STATE_WARNING;
1446 }
1447 }
1448
1449 if (jitter_mode) {
1450 if (host->jitter >= crit.jitter) {
1451 this_status = STATE_CRITICAL;
1452 status = STATE_CRITICAL;
1453 host->jitter_status = STATE_CRITICAL;
1454 } else if (status != STATE_CRITICAL && (host->jitter >= warn.jitter)) {
1455 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1456 status = STATE_WARNING;
1457 host->jitter_status = STATE_WARNING;
1458 }
1459 }
1460
1461 if (mos_mode) {
1462 if (host->mos <= crit.mos) {
1463 this_status = STATE_CRITICAL;
1464 status = STATE_CRITICAL;
1465 host->mos_status = STATE_CRITICAL;
1466 } else if (status != STATE_CRITICAL && (host->mos <= warn.mos)) {
1467 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1468 status = STATE_WARNING;
1469 host->mos_status = STATE_WARNING;
1470 }
1471 }
1472
1473 if (score_mode) {
1474 if (host->score <= crit.score) {
1475 this_status = STATE_CRITICAL;
1476 status = STATE_CRITICAL;
1477 host->score_status = STATE_CRITICAL;
1478 } else if (status != STATE_CRITICAL && (host->score <= warn.score)) {
1479 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1480 status = STATE_WARNING;
1481 host->score_status = STATE_WARNING;
1482 }
1483 }
1484
1485 if (this_status == STATE_WARNING) {
1486 hosts_warn++;
1487 } else if (this_status == STATE_OK) {
1488 hosts_ok++;
1489 }
1490
1491 host = host->next;
1492 } 1535 }
1493 1536
1494 /* this is inevitable */
1495 if (!targets_alive) {
1496 status = STATE_CRITICAL;
1497 }
1498 if (min_hosts_alive > -1) { 1537 if (min_hosts_alive > -1) {
1499 if (hosts_ok >= min_hosts_alive) { 1538 mp_subcheck sc_min_targets_alive = mp_subcheck_init();
1500 status = STATE_OK; 1539 sc_min_targets_alive = mp_set_subcheck_default_state(sc_min_targets_alive, STATE_OK);
1501 } else if ((hosts_ok + hosts_warn) >= min_hosts_alive) { 1540
1502 status = STATE_WARNING; 1541 if (targets_ok >= min_hosts_alive) {
1503 } 1542 sc_min_targets_alive = mp_set_subcheck_state(sc_min_targets_alive, STATE_OK);
1504 } 1543 xasprintf(&sc_min_targets_alive.output, "%u targets OK of a minimum of %u", targets_ok,
1505 printf("%s - ", status_string[status]); 1544 min_hosts_alive);
1506 1545
1507 host = list; 1546 // Overwrite main state here
1508 while (host) { 1547 overall->evaluation_function = &mp_eval_ok;
1509 if (debug) { 1548 } else if ((targets_ok + targets_warn) >= min_hosts_alive) {
1510 puts(""); 1549 sc_min_targets_alive = mp_set_subcheck_state(sc_min_targets_alive, STATE_WARNING);
1511 } 1550 xasprintf(&sc_min_targets_alive.output, "%u targets OK or Warning of a minimum of %u",
1512 1551 targets_ok + targets_warn, min_hosts_alive);
1513 if (i) { 1552 overall->evaluation_function = &mp_eval_warning;
1514 if (i < targets) { 1553 } else {
1515 printf(" :: "); 1554 sc_min_targets_alive = mp_set_subcheck_state(sc_min_targets_alive, STATE_CRITICAL);
1516 } else { 1555 xasprintf(&sc_min_targets_alive.output, "%u targets OK or Warning of a minimum of %u",
1517 printf("\n"); 1556 targets_ok + targets_warn, min_hosts_alive);
1518 } 1557 overall->evaluation_function = &mp_eval_critical;
1519 }
1520
1521 i++;
1522
1523 if (!host->icmp_recv) {
1524 status = STATE_CRITICAL;
1525 host->rtmin = 0;
1526 host->jitter_min = 0;
1527
1528 if (host->flags & FLAG_LOST_CAUSE) {
1529 char address[INET6_ADDRSTRLEN];
1530 parse_address(&host->error_addr, address, sizeof(address));
1531 printf("%s: %s @ %s. rta nan, lost %d%%", host->name, get_icmp_error_msg(host->icmp_type, host->icmp_code), address, 100);
1532 } else { /* not marked as lost cause, so we have no flags for it */
1533 printf("%s: rta nan, lost 100%%", host->name);
1534 }
1535 } else { /* !icmp_recv */
1536 printf("%s", host->name);
1537 /* rta text output */
1538 if (rta_mode) {
1539 if (status == STATE_OK) {
1540 printf(" rta %0.3fms", host->rta / 1000);
1541 } else if (status == STATE_WARNING && host->rta_status == status) {
1542 printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)warn.rta / 1000);
1543 } else if (status == STATE_CRITICAL && host->rta_status == status) {
1544 printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)crit.rta / 1000);
1545 }
1546 }
1547
1548 /* pl text output */
1549 if (pl_mode) {
1550 if (status == STATE_OK) {
1551 printf(" lost %u%%", host->pl);
1552 } else if (status == STATE_WARNING && host->pl_status == status) {
1553 printf(" lost %u%% > %u%%", host->pl, warn.pl);
1554 } else if (status == STATE_CRITICAL && host->pl_status == status) {
1555 printf(" lost %u%% > %u%%", host->pl, crit.pl);
1556 }
1557 }
1558
1559 /* jitter text output */
1560 if (jitter_mode) {
1561 if (status == STATE_OK) {
1562 printf(" jitter %0.3fms", (float)host->jitter);
1563 } else if (status == STATE_WARNING && host->jitter_status == status) {
1564 printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, warn.jitter);
1565 } else if (status == STATE_CRITICAL && host->jitter_status == status) {
1566 printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, crit.jitter);
1567 }
1568 }
1569
1570 /* mos text output */
1571 if (mos_mode) {
1572 if (status == STATE_OK) {
1573 printf(" MOS %0.1f", (float)host->mos);
1574 } else if (status == STATE_WARNING && host->mos_status == status) {
1575 printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)warn.mos);
1576 } else if (status == STATE_CRITICAL && host->mos_status == status) {
1577 printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)crit.mos);
1578 }
1579 }
1580
1581 /* score text output */
1582 if (score_mode) {
1583 if (status == STATE_OK) {
1584 printf(" Score %u", (int)host->score);
1585 } else if (status == STATE_WARNING && host->score_status == status) {
1586 printf(" Score %u < %u", (int)host->score, (int)warn.score);
1587 } else if (status == STATE_CRITICAL && host->score_status == status) {
1588 printf(" Score %u < %u", (int)host->score, (int)crit.score);
1589 }
1590 }
1591
1592 /* order statis text output */
1593 if (order_mode) {
1594 if (status == STATE_OK) {
1595 printf(" Packets in order");
1596 } else if (status == STATE_CRITICAL && host->order_status == status) {
1597 printf(" Packets out of order");
1598 }
1599 }
1600 }
1601 host = host->next;
1602 }
1603
1604 /* iterate once more for pretty perfparse output */
1605 if (!(!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode && order_mode)) {
1606 printf("|");
1607 }
1608 i = 0;
1609 host = list;
1610 while (host) {
1611 if (debug) {
1612 puts("");
1613 }
1614
1615 if (rta_mode) {
1616 if (host->pl < 100) {
1617 printf("%srta=%0.3fms;%0.3f;%0.3f;0; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ", (targets > 1) ? host->name : "",
1618 host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000, (targets > 1) ? host->name : "",
1619 (float)host->rtmax / 1000, (targets > 1) ? host->name : "",
1620 (host->rtmin < INFINITY) ? (float)host->rtmin / 1000 : (float)0);
1621 } else {
1622 printf("%srta=U;;;; %srtmax=U;;;; %srtmin=U;;;; ", (targets > 1) ? host->name : "", (targets > 1) ? host->name : "",
1623 (targets > 1) ? host->name : "");
1624 }
1625 }
1626
1627 if (pl_mode) {
1628 printf("%spl=%u%%;%u;%u;0;100 ", (targets > 1) ? host->name : "", host->pl, warn.pl, crit.pl);
1629 }
1630
1631 if (jitter_mode) {
1632 if (host->pl < 100) {
1633 printf("%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %sjitter_max=%0.3fms;;;; %sjitter_min=%0.3fms;;;; ",
1634 (targets > 1) ? host->name : "", (float)host->jitter, (float)warn.jitter, (float)crit.jitter,
1635 (targets > 1) ? host->name : "", (float)host->jitter_max / 1000, (targets > 1) ? host->name : "",
1636 (float)host->jitter_min / 1000);
1637 } else {
1638 printf("%sjitter_avg=U;;;; %sjitter_max=U;;;; %sjitter_min=U;;;; ", (targets > 1) ? host->name : "",
1639 (targets > 1) ? host->name : "", (targets > 1) ? host->name : "");
1640 }
1641 }
1642
1643 if (mos_mode) {
1644 if (host->pl < 100) {
1645 printf("%smos=%0.1f;%0.1f;%0.1f;0;5 ", (targets > 1) ? host->name : "", (float)host->mos, (float)warn.mos, (float)crit.mos);
1646 } else {
1647 printf("%smos=U;;;; ", (targets > 1) ? host->name : "");
1648 }
1649 }
1650
1651 if (score_mode) {
1652 if (host->pl < 100) {
1653 printf("%sscore=%u;%u;%u;0;100 ", (targets > 1) ? host->name : "", (int)host->score, (int)warn.score, (int)crit.score);
1654 } else {
1655 printf("%sscore=U;;;; ", (targets > 1) ? host->name : "");
1656 }
1657 } 1558 }
1658 1559
1659 host = host->next; 1560 mp_add_subcheck_to_check(overall, sc_min_targets_alive);
1660 }
1661
1662 if (min_hosts_alive > -1) {
1663 if (hosts_ok >= min_hosts_alive) {
1664 status = STATE_OK;
1665 } else if ((hosts_ok + hosts_warn) >= min_hosts_alive) {
1666 status = STATE_WARNING;
1667 }
1668 } 1561 }
1669 1562
1670 /* finish with an empty line */ 1563 /* finish with an empty line */
1671 puts("");
1672 if (debug) { 1564 if (debug) {
1673 printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n", targets, targets_alive, hosts_ok, 1565 printf(
1674 hosts_warn, min_hosts_alive); 1566 "targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n",
1567 number_of_targets, targets_alive(number_of_targets, program_state->targets_down),
1568 targets_ok, targets_warn, min_hosts_alive);
1675 } 1569 }
1676
1677 exit(status);
1678} 1570}
1679 1571
1680static u_int get_timevaldiff(struct timeval *early, struct timeval *later) { 1572static time_t get_timevaldiff(const struct timeval earlier, const struct timeval later) {
1681 u_int ret;
1682 struct timeval now;
1683
1684 if (!later) {
1685 gettimeofday(&now, &tz);
1686 later = &now;
1687 }
1688 if (!early) {
1689 early = &prog_start;
1690 }
1691
1692 /* if early > later we return 0 so as to indicate a timeout */ 1573 /* if early > later we return 0 so as to indicate a timeout */
1693 if (early->tv_sec > later->tv_sec || (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec)) { 1574 if (earlier.tv_sec > later.tv_sec ||
1575 (earlier.tv_sec == later.tv_sec && earlier.tv_usec > later.tv_usec)) {
1694 return 0; 1576 return 0;
1695 } 1577 }
1696 ret = (later->tv_sec - early->tv_sec) * 1000000; 1578
1697 ret += later->tv_usec - early->tv_usec; 1579 time_t ret = (later.tv_sec - earlier.tv_sec) * 1000000;
1580 ret += later.tv_usec - earlier.tv_usec;
1698 1581
1699 return ret; 1582 return ret;
1700} 1583}
1701 1584
1702static int add_target_ip(char *arg, struct sockaddr_storage *in) { 1585static time_t get_timevaldiff_to_now(struct timeval earlier) {
1703 struct rta_host *host; 1586 struct timeval now;
1704 struct sockaddr_in *sin, *host_sin; 1587 gettimeofday(&now, NULL);
1705 struct sockaddr_in6 *sin6, *host_sin6; 1588
1589 return get_timevaldiff(earlier, now);
1590}
1591
1592static add_target_ip_wrapper add_target_ip(struct sockaddr_storage address) {
1593 assert((address.ss_family == AF_INET) || (address.ss_family == AF_INET6));
1706 1594
1707 if (address_family == AF_INET) { 1595 if (debug) {
1708 sin = (struct sockaddr_in *)in; 1596 char straddr[INET6_ADDRSTRLEN];
1597 parse_address((&address), straddr, sizeof(straddr));
1598 printf("add_target_ip called with: %s\n", straddr);
1599 }
1600 struct sockaddr_in *sin;
1601 struct sockaddr_in6 *sin6;
1602 if (address.ss_family == AF_INET) {
1603 sin = (struct sockaddr_in *)&address;
1604 } else if (address.ss_family == AF_INET6) {
1605 sin6 = (struct sockaddr_in6 *)&address;
1709 } else { 1606 } else {
1710 sin6 = (struct sockaddr_in6 *)in; 1607 assert(false);
1711 } 1608 }
1712 1609
1610 add_target_ip_wrapper result = {
1611 .error_code = OK,
1612 .target = NULL,
1613 };
1614
1713 /* disregard obviously stupid addresses 1615 /* disregard obviously stupid addresses
1714 * (I didn't find an ipv6 equivalent to INADDR_NONE) */ 1616 * (I didn't find an ipv6 equivalent to INADDR_NONE) */
1715 if (((address_family == AF_INET && (sin->sin_addr.s_addr == INADDR_NONE || sin->sin_addr.s_addr == INADDR_ANY))) || 1617 if (((address.ss_family == AF_INET &&
1716 (address_family == AF_INET6 && (sin6->sin6_addr.s6_addr == in6addr_any.s6_addr))) { 1618 (sin->sin_addr.s_addr == INADDR_NONE || sin->sin_addr.s_addr == INADDR_ANY))) ||
1717 return -1; 1619 (address.ss_family == AF_INET6 && (sin6->sin6_addr.s6_addr == in6addr_any.s6_addr))) {
1620 result.error_code = ERROR;
1621 return result;
1718 } 1622 }
1719 1623
1720 /* no point in adding two identical IP's, so don't. ;) */ 1624 // get string representation of address
1721 host = list; 1625 char straddr[INET6_ADDRSTRLEN];
1722 while (host) { 1626 parse_address((&address), straddr, sizeof(straddr));
1723 host_sin = (struct sockaddr_in *)&host->saddr_in;
1724 host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;
1725
1726 if ((address_family == AF_INET && host_sin->sin_addr.s_addr == sin->sin_addr.s_addr) ||
1727 (address_family == AF_INET6 && host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) {
1728 if (debug) {
1729 printf("Identical IP already exists. Not adding %s\n", arg);
1730 }
1731 return -1;
1732 }
1733 host = host->next;
1734 }
1735 1627
1736 /* add the fresh ip */ 1628 /* add the fresh ip */
1737 host = (struct rta_host *)malloc(sizeof(struct rta_host)); 1629 ping_target *target = (ping_target *)calloc(1, sizeof(ping_target));
1738 if (!host) { 1630 if (!target) {
1739 char straddr[INET6_ADDRSTRLEN]; 1631 crash("add_target_ip(%s): malloc(%lu) failed", straddr, sizeof(ping_target));
1740 parse_address((struct sockaddr_storage *)&in, straddr, sizeof(straddr));
1741 crash("add_target_ip(%s, %s): malloc(%lu) failed", arg, straddr, sizeof(struct rta_host));
1742 } 1632 }
1743 memset(host, 0, sizeof(struct rta_host));
1744 1633
1745 /* set the values. use calling name for output */ 1634 ping_target_create_wrapper target_wrapper = ping_target_create(address);
1746 host->name = strdup(arg);
1747 1635
1748 /* fill out the sockaddr_storage struct */ 1636 if (target_wrapper.errorcode == OK) {
1749 if (address_family == AF_INET) { 1637 *target = target_wrapper.host;
1750 host_sin = (struct sockaddr_in *)&host->saddr_in; 1638 result.target = target;
1751 host_sin->sin_family = AF_INET;
1752 host_sin->sin_addr.s_addr = sin->sin_addr.s_addr;
1753 } else { 1639 } else {
1754 host_sin6 = (struct sockaddr_in6 *)&host->saddr_in; 1640 result.error_code = target_wrapper.errorcode;
1755 host_sin6->sin6_family = AF_INET6;
1756 memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr, sizeof host_sin6->sin6_addr.s6_addr);
1757 }
1758
1759 /* fill out the sockaddr_in struct */
1760 host->rtmin = INFINITY;
1761 host->rtmax = 0;
1762 host->jitter = 0;
1763 host->jitter_max = 0;
1764 host->jitter_min = INFINITY;
1765 host->last_tdiff = 0;
1766 host->order_status = STATE_OK;
1767 host->last_icmp_seq = 0;
1768 host->rta_status = 0;
1769 host->pl_status = 0;
1770 host->jitter_status = 0;
1771 host->mos_status = 0;
1772 host->score_status = 0;
1773 host->pl_status = 0;
1774
1775 if (!list) {
1776 list = cursor = host;
1777 } else {
1778 cursor->next = host;
1779 } 1641 }
1780 1642
1781 cursor = host; 1643 return result;
1782 targets++;
1783
1784 return 0;
1785} 1644}
1786 1645
1787/* wrapper for add_target_ip */ 1646/* wrapper for add_target_ip */
1788static int add_target(char *arg) { 1647static add_target_wrapper add_target(char *arg, const check_icmp_execution_mode mode,
1789 int error, result = -1; 1648 sa_family_t enforced_proto) {
1790 struct sockaddr_storage ip; 1649 if (debug > 0) {
1791 struct addrinfo hints, *res, *p; 1650 printf("add_target called with argument %s\n", arg);
1792 struct sockaddr_in *sin; 1651 }
1793 struct sockaddr_in6 *sin6; 1652
1794 1653 struct sockaddr_storage address_storage = {};
1795 switch (address_family) { 1654 struct sockaddr_in *sin = NULL;
1796 case -1: 1655 struct sockaddr_in6 *sin6 = NULL;
1797 /* -4 and -6 are not specified on cmdline */ 1656 int error_code = -1;
1798 address_family = AF_INET; 1657
1799 sin = (struct sockaddr_in *)&ip; 1658 switch (enforced_proto) {
1800 result = inet_pton(address_family, arg, &sin->sin_addr); 1659 case AF_UNSPEC:
1801#ifdef USE_IPV6 1660 /*
1802 if (result != 1) { 1661 * no enforced protocol family
1803 address_family = AF_INET6; 1662 * try to parse the address with each one
1804 sin6 = (struct sockaddr_in6 *)&ip; 1663 */
1805 result = inet_pton(address_family, arg, &sin6->sin6_addr); 1664 sin = (struct sockaddr_in *)&address_storage;
1806 } 1665 error_code = inet_pton(AF_INET, arg, &sin->sin_addr);
1807#endif 1666 address_storage.ss_family = AF_INET;
1808 /* If we don't find any valid addresses, we still don't know the address_family */ 1667
1809 if (result != 1) { 1668 if (error_code != 1) {
1810 address_family = -1; 1669 sin6 = (struct sockaddr_in6 *)&address_storage;
1670 error_code = inet_pton(AF_INET6, arg, &sin6->sin6_addr);
1671 address_storage.ss_family = AF_INET6;
1811 } 1672 }
1812 break; 1673 break;
1813 case AF_INET: 1674 case AF_INET:
1814 sin = (struct sockaddr_in *)&ip; 1675 sin = (struct sockaddr_in *)&address_storage;
1815 result = inet_pton(address_family, arg, &sin->sin_addr); 1676 error_code = inet_pton(AF_INET, arg, &sin->sin_addr);
1677 address_storage.ss_family = AF_INET;
1816 break; 1678 break;
1817 case AF_INET6: 1679 case AF_INET6:
1818 sin6 = (struct sockaddr_in6 *)&ip; 1680 sin6 = (struct sockaddr_in6 *)&address_storage;
1819 result = inet_pton(address_family, arg, &sin6->sin6_addr); 1681 error_code = inet_pton(AF_INET, arg, &sin6->sin6_addr);
1682 address_storage.ss_family = AF_INET6;
1820 break; 1683 break;
1821 default: 1684 default:
1822 crash("Address family not supported"); 1685 crash("Address family not supported");
1823 } 1686 }
1824 1687
1825 /* don't resolve if we don't have to */ 1688 add_target_wrapper result = {
1826 if (result == 1) { 1689 .error_code = OK,
1690 .targets = NULL,
1691 .has_v4 = false,
1692 .has_v6 = false,
1693 };
1694
1695 // if error_code == 1 the address was a valid address parsed above
1696 if (error_code == 1) {
1827 /* don't add all ip's if we were given a specific one */ 1697 /* don't add all ip's if we were given a specific one */
1828 return add_target_ip(arg, &ip); 1698 add_target_ip_wrapper targeted = add_target_ip(address_storage);
1829 } else { 1699
1830 errno = 0; 1700 if (targeted.error_code != OK) {
1831 memset(&hints, 0, sizeof(hints)); 1701 result.error_code = ERROR;
1832 if (address_family == -1) { 1702 return result;
1833 hints.ai_family = AF_UNSPEC;
1834 } else {
1835 hints.ai_family = address_family == AF_INET ? PF_INET : PF_INET6;
1836 } 1703 }
1837 hints.ai_socktype = SOCK_RAW; 1704
1838 if ((error = getaddrinfo(arg, NULL, &hints, &res)) != 0) { 1705 if (targeted.target->address.ss_family == AF_INET) {
1839 errno = 0; 1706 result.has_v4 = true;
1840 crash("Failed to resolve %s: %s", arg, gai_strerror(error)); 1707 } else if (targeted.target->address.ss_family == AF_INET6) {
1841 return -1; 1708 result.has_v6 = true;
1709 } else {
1710 assert(false);
1842 } 1711 }
1843 address_family = res->ai_family; 1712 result.targets = targeted.target;
1713 result.number_of_targets = 1;
1714 return result;
1715 }
1716
1717 struct addrinfo hints = {};
1718 errno = 0;
1719 hints.ai_family = enforced_proto;
1720 hints.ai_socktype = SOCK_RAW;
1721
1722 int error;
1723 struct addrinfo *res;
1724 if ((error = getaddrinfo(arg, NULL, &hints, &res)) != 0) {
1725 errno = 0;
1726 crash("Failed to resolve %s: %s", arg, gai_strerror(error));
1727 result.error_code = ERROR;
1728 return result;
1844 } 1729 }
1845 1730
1846 /* possibly add all the IP's as targets */ 1731 /* possibly add all the IP's as targets */
1847 for (p = res; p != NULL; p = p->ai_next) { 1732 for (struct addrinfo *address = res; address != NULL; address = address->ai_next) {
1848 memcpy(&ip, p->ai_addr, p->ai_addrlen); 1733 struct sockaddr_storage temporary_ip_address;
1849 add_target_ip(arg, &ip); 1734 memcpy(&temporary_ip_address, address->ai_addr, address->ai_addrlen);
1735
1736 add_target_ip_wrapper tmp = add_target_ip(temporary_ip_address);
1737
1738 if (tmp.error_code != OK) {
1739 // No proper error handling
1740 // What to do?
1741 } else {
1742 if (result.targets == NULL) {
1743 result.targets = tmp.target;
1744 result.number_of_targets = 1;
1745 } else {
1746 result.number_of_targets += ping_target_list_append(result.targets, tmp.target);
1747 }
1748 if (address->ai_family == AF_INET) {
1749 result.has_v4 = true;
1750 } else if (address->ai_family == AF_INET6) {
1751 result.has_v6 = true;
1752 }
1753 }
1850 1754
1851 /* this is silly, but it works */ 1755 /* this is silly, but it works */
1852 if (mode == MODE_HOSTCHECK || mode == MODE_ALL) { 1756 if (mode == MODE_HOSTCHECK || mode == MODE_ALL) {
@@ -1855,20 +1759,22 @@ static int add_target(char *arg) {
1855 } 1759 }
1856 continue; 1760 continue;
1857 } 1761 }
1762
1763 // Abort after first hit if not in of the modes above
1858 break; 1764 break;
1859 } 1765 }
1860 freeaddrinfo(res); 1766 freeaddrinfo(res);
1861 1767
1862 return 0; 1768 return result;
1863} 1769}
1864 1770
1865static void set_source_ip(char *arg) { 1771static void set_source_ip(char *arg, const int icmp_sock, sa_family_t addr_family) {
1866 struct sockaddr_in src; 1772 struct sockaddr_in src;
1867 1773
1868 memset(&src, 0, sizeof(src)); 1774 memset(&src, 0, sizeof(src));
1869 src.sin_family = address_family; 1775 src.sin_family = addr_family;
1870 if ((src.sin_addr.s_addr = inet_addr(arg)) == INADDR_NONE) { 1776 if ((src.sin_addr.s_addr = inet_addr(arg)) == INADDR_NONE) {
1871 src.sin_addr.s_addr = get_ip_address(arg); 1777 src.sin_addr.s_addr = get_ip_address(arg, icmp_sock);
1872 } 1778 }
1873 if (bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1) { 1779 if (bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1) {
1874 crash("Cannot bind to IP address %s", arg); 1780 crash("Cannot bind to IP address %s", arg);
@@ -1876,10 +1782,11 @@ static void set_source_ip(char *arg) {
1876} 1782}
1877 1783
1878/* TODO: Move this to netutils.c and also change check_dhcp to use that. */ 1784/* TODO: Move this to netutils.c and also change check_dhcp to use that. */
1879static in_addr_t get_ip_address(const char *ifname) { 1785static in_addr_t get_ip_address(const char *ifname, const int icmp_sock) {
1880 // TODO: Rewrite this so the function return an error and we exit somewhere else 1786 // TODO: Rewrite this so the function return an error and we exit somewhere else
1881 struct sockaddr_in ip; 1787
1882 ip.sin_addr.s_addr = 0; // Fake initialization to make compiler happy 1788 struct sockaddr_in ip_address;
1789 ip_address.sin_addr.s_addr = 0; // Fake initialization to make compiler happy
1883#if defined(SIOCGIFADDR) 1790#if defined(SIOCGIFADDR)
1884 struct ifreq ifr; 1791 struct ifreq ifr;
1885 1792
@@ -1891,13 +1798,16 @@ static in_addr_t get_ip_address(const char *ifname) {
1891 crash("Cannot determine IP address of interface %s", ifname); 1798 crash("Cannot determine IP address of interface %s", ifname);
1892 } 1799 }
1893 1800
1894 memcpy(&ip, &ifr.ifr_addr, sizeof(ip)); 1801 memcpy(&ip_address, &ifr.ifr_addr, sizeof(ip_address));
1895#else 1802#else
1803 // fake operation to make the compiler happy
1804 (void)icmp_sock;
1805
1896 (void)ifname; 1806 (void)ifname;
1897 errno = 0; 1807 errno = 0;
1898 crash("Cannot get interface IP address on this platform."); 1808 crash("Cannot get interface IP address on this platform.");
1899#endif 1809#endif
1900 return ip.sin_addr.s_addr; 1810 return ip_address.sin_addr.s_addr;
1901} 1811}
1902 1812
1903/* 1813/*
@@ -1906,103 +1816,127 @@ static in_addr_t get_ip_address(const char *ifname) {
1906 * s = seconds 1816 * s = seconds
1907 * return value is in microseconds 1817 * return value is in microseconds
1908 */ 1818 */
1909static u_int get_timevar(const char *str) { 1819static get_timevar_wrapper get_timevar(const char *str) {
1910 char p, u, *ptr; 1820 get_timevar_wrapper result = {
1911 size_t len; 1821 .error_code = OK,
1912 u_int i, d; /* integer and decimal, respectively */ 1822 .time_range = 0,
1913 u_int factor = 1000; /* default to milliseconds */ 1823 };
1914 1824
1915 if (!str) { 1825 if (!str) {
1916 return 0; 1826 result.error_code = ERROR;
1827 return result;
1917 } 1828 }
1918 len = strlen(str); 1829
1830 size_t len = strlen(str);
1919 if (!len) { 1831 if (!len) {
1920 return 0; 1832 result.error_code = ERROR;
1833 return result;
1921 } 1834 }
1922 1835
1923 /* unit might be given as ms|m (millisec), 1836 /* unit might be given as ms|m (millisec),
1924 * us|u (microsec) or just plain s, for seconds */ 1837 * us|u (microsec) or just plain s, for seconds */
1925 p = '\0'; 1838 char tmp = '\0';
1926 u = str[len - 1]; 1839 char unit = str[len - 1];
1927 if (len >= 2 && !isdigit((int)str[len - 2])) { 1840 if (len >= 2 && !isdigit((int)str[len - 2])) {
1928 p = str[len - 2]; 1841 tmp = str[len - 2];
1929 } 1842 }
1930 if (p && u == 's') { 1843
1931 u = p; 1844 if (tmp && unit == 's') {
1932 } else if (!p) { 1845 unit = tmp;
1933 p = u; 1846 } else if (!tmp) {
1847 tmp = unit;
1934 } 1848 }
1849
1935 if (debug > 2) { 1850 if (debug > 2) {
1936 printf("evaluating %s, u: %c, p: %c\n", str, u, p); 1851 printf("evaluating %s, u: %c, p: %c\n", str, unit, tmp);
1937 } 1852 }
1938 1853
1939 if (u == 'u') { 1854 unsigned int factor = 1000; /* default to milliseconds */
1855 if (unit == 'u') {
1940 factor = 1; /* microseconds */ 1856 factor = 1; /* microseconds */
1941 } else if (u == 'm') { 1857 } else if (unit == 'm') {
1942 factor = 1000; /* milliseconds */ 1858 factor = 1000; /* milliseconds */
1943 } else if (u == 's') { 1859 } else if (unit == 's') {
1944 factor = 1000000; /* seconds */ 1860 factor = 1000000; /* seconds */
1945 } 1861 }
1862
1946 if (debug > 2) { 1863 if (debug > 2) {
1947 printf("factor is %u\n", factor); 1864 printf("factor is %u\n", factor);
1948 } 1865 }
1949 1866
1950 i = strtoul(str, &ptr, 0); 1867 char *ptr;
1868 unsigned long pre_radix;
1869 pre_radix = strtoul(str, &ptr, 0);
1951 if (!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1) { 1870 if (!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1) {
1952 return i * factor; 1871 result.time_range = (unsigned int)(pre_radix * factor);
1872 return result;
1953 } 1873 }
1954 1874
1955 /* time specified in usecs can't have decimal points, so ignore them */ 1875 /* time specified in usecs can't have decimal points, so ignore them */
1956 if (factor == 1) { 1876 if (factor == 1) {
1957 return i; 1877 result.time_range = (unsigned int)pre_radix;
1878 return result;
1958 } 1879 }
1959 1880
1960 d = strtoul(ptr + 1, NULL, 0); 1881 /* integer and decimal, respectively */
1882 unsigned int post_radix = (unsigned int)strtoul(ptr + 1, NULL, 0);
1961 1883
1962 /* d is decimal, so get rid of excess digits */ 1884 /* d is decimal, so get rid of excess digits */
1963 while (d >= factor) { 1885 while (post_radix >= factor) {
1964 d /= 10; 1886 post_radix /= 10;
1965 } 1887 }
1966 1888
1967 /* the last parenthesis avoids floating point exceptions. */ 1889 /* the last parenthesis avoids floating point exceptions. */
1968 return ((i * factor) + (d * (factor / 10))); 1890 result.time_range = (unsigned int)((pre_radix * factor) + (post_radix * (factor / 10)));
1891 return result;
1969} 1892}
1970 1893
1971/* not too good at checking errors, but it'll do (main() should barfe on -1) */ 1894static get_threshold_wrapper get_threshold(char *str, check_icmp_threshold threshold) {
1972static int get_threshold(char *str, threshold *th) { 1895 get_threshold_wrapper result = {
1973 char *p = NULL, i = 0; 1896 .errorcode = OK,
1897 .threshold = threshold,
1898 };
1974 1899
1975 if (!str || !strlen(str) || !th) { 1900 if (!str || !strlen(str)) {
1976 return -1; 1901 result.errorcode = ERROR;
1902 return result;
1977 } 1903 }
1978 1904
1979 /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */ 1905 /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
1980 p = &str[strlen(str) - 1]; 1906 bool is_at_last_char = false;
1981 while (p != &str[1]) { 1907 char *tmp = &str[strlen(str) - 1];
1982 if (*p == '%') { 1908 while (tmp != &str[1]) {
1983 *p = '\0'; 1909 if (*tmp == '%') {
1984 } else if (*p == ',' && i) { 1910 *tmp = '\0';
1985 *p = '\0'; /* reset it so get_timevar(str) works nicely later */ 1911 } else if (*tmp == ',' && is_at_last_char) {
1986 th->pl = (unsigned char)strtoul(p + 1, NULL, 0); 1912 *tmp = '\0'; /* reset it so get_timevar(str) works nicely later */
1913 result.threshold.pl = (unsigned char)strtoul(tmp + 1, NULL, 0);
1987 break; 1914 break;
1988 } 1915 }
1989 i = 1; 1916 is_at_last_char = true;
1990 p--; 1917 tmp--;
1991 } 1918 }
1992 th->rta = get_timevar(str);
1993 1919
1994 if (!th->rta) { 1920 get_timevar_wrapper parsed_time = get_timevar(str);
1995 return -1; 1921
1922 if (parsed_time.error_code == OK) {
1923 result.threshold.rta = parsed_time.time_range;
1924 } else {
1925 if (debug > 1) {
1926 printf("%s: failed to parse rta threshold\n", __FUNCTION__);
1927 }
1928 result.errorcode = ERROR;
1929 return result;
1996 } 1930 }
1997 1931
1998 if (th->rta > MAXTTL * 1000000) { 1932 if (result.threshold.rta > MAXTTL * 1000000) {
1999 th->rta = MAXTTL * 1000000; 1933 result.threshold.rta = MAXTTL * 1000000;
2000 } 1934 }
2001 if (th->pl > 100) { 1935 if (result.threshold.pl > 100) {
2002 th->pl = 100; 1936 result.threshold.pl = 100;
2003 } 1937 }
2004 1938
2005 return 0; 1939 return result;
2006} 1940}
2007 1941
2008/* 1942/*
@@ -2013,184 +1947,544 @@ static int get_threshold(char *str, threshold *th) {
2013 * @param[in] length strlen(str) 1947 * @param[in] length strlen(str)
2014 * @param[out] warn Pointer to the warn threshold struct to which the values should be assigned 1948 * @param[out] warn Pointer to the warn threshold struct to which the values should be assigned
2015 * @param[out] crit Pointer to the crit threshold struct to which the values should be assigned 1949 * @param[out] crit Pointer to the crit threshold struct to which the values should be assigned
2016 * @param[in] mode Determines whether this a threshold for rta, packet_loss, jitter, mos or score (exclusively) 1950 * @param[in] mode Determines whether this a threshold for rta, packet_loss, jitter, mos or score
1951 * (exclusively)
2017 */ 1952 */
2018static bool get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, threshold_mode mode) { 1953static get_threshold2_wrapper get_threshold2(char *str, size_t length, check_icmp_threshold warn,
2019 if (!str || !length || !warn || !crit) { 1954 check_icmp_threshold crit, threshold_mode mode) {
2020 return false; 1955 get_threshold2_wrapper result = {
1956 .errorcode = OK,
1957 .warn = warn,
1958 .crit = crit,
1959 };
1960
1961 if (!str || !length) {
1962 result.errorcode = ERROR;
1963 return result;
2021 } 1964 }
2022 1965
2023 // p points to the last char in str 1966 // p points to the last char in str
2024 char *p = &str[length - 1]; 1967 char *work_pointer = &str[length - 1];
2025 1968
2026 // first_iteration is bof-stop on stupid libc's 1969 // first_iteration is bof-stop on stupid libc's
2027 bool first_iteration = true; 1970 bool first_iteration = true;
2028 1971
2029 while (p != &str[0]) { 1972 while (work_pointer != &str[0]) {
2030 if ((*p == 'm') || (*p == '%')) { 1973 if ((*work_pointer == 'm') || (*work_pointer == '%')) {
2031 *p = '\0'; 1974 *work_pointer = '\0';
2032 } else if (*p == ',' && !first_iteration) { 1975 } else if (*work_pointer == ',' && !first_iteration) {
2033 *p = '\0'; /* reset it so get_timevar(str) works nicely later */ 1976 *work_pointer = '\0'; /* reset it so get_timevar(str) works nicely later */
2034 1977
2035 char *start_of_value = p + 1; 1978 char *start_of_value = work_pointer + 1;
2036 1979
2037 if (!parse_threshold2_helper(start_of_value, strlen(start_of_value), crit, mode)) { 1980 parse_threshold2_helper_wrapper tmp =
2038 return false; 1981 parse_threshold2_helper(start_of_value, strlen(start_of_value), result.crit, mode);
1982 if (tmp.errorcode != OK) {
1983 result.errorcode = ERROR;
1984 return result;
2039 } 1985 }
1986 result.crit = tmp.result;
2040 } 1987 }
2041 first_iteration = false; 1988 first_iteration = false;
2042 p--; 1989 work_pointer--;
2043 } 1990 }
2044 1991
2045 return parse_threshold2_helper(p, strlen(p), warn, mode); 1992 parse_threshold2_helper_wrapper tmp =
1993 parse_threshold2_helper(work_pointer, strlen(work_pointer), result.warn, mode);
1994 if (tmp.errorcode != OK) {
1995 result.errorcode = ERROR;
1996 } else {
1997 result.warn = tmp.result;
1998 }
1999 return result;
2046} 2000}
2047 2001
2048static bool parse_threshold2_helper(char *s, size_t length, threshold *thr, threshold_mode mode) { 2002static parse_threshold2_helper_wrapper parse_threshold2_helper(char *threshold_string,
2003 size_t length,
2004 check_icmp_threshold thr,
2005 threshold_mode mode) {
2049 char *resultChecker = {0}; 2006 char *resultChecker = {0};
2007 parse_threshold2_helper_wrapper result = {
2008 .result = thr,
2009 .errorcode = OK,
2010 };
2050 2011
2051 switch (mode) { 2012 switch (mode) {
2052 case const_rta_mode: 2013 case const_rta_mode:
2053 thr->rta = strtod(s, &resultChecker) * 1000; 2014 result.result.rta = (unsigned int)(strtod(threshold_string, &resultChecker) * 1000);
2054 break; 2015 break;
2055 case const_packet_loss_mode: 2016 case const_packet_loss_mode:
2056 thr->pl = (unsigned char)strtoul(s, &resultChecker, 0); 2017 result.result.pl = (unsigned char)strtoul(threshold_string, &resultChecker, 0);
2057 break; 2018 break;
2058 case const_jitter_mode: 2019 case const_jitter_mode:
2059 thr->jitter = strtod(s, &resultChecker); 2020 result.result.jitter = strtod(threshold_string, &resultChecker);
2060
2061 break; 2021 break;
2062 case const_mos_mode: 2022 case const_mos_mode:
2063 thr->mos = strtod(s, &resultChecker); 2023 result.result.mos = strtod(threshold_string, &resultChecker);
2064 break; 2024 break;
2065 case const_score_mode: 2025 case const_score_mode:
2066 thr->score = strtod(s, &resultChecker); 2026 result.result.score = strtod(threshold_string, &resultChecker);
2067 break; 2027 break;
2068 } 2028 }
2069 2029
2070 if (resultChecker == s) { 2030 if (resultChecker == threshold_string) {
2071 // Failed to parse 2031 // Failed to parse
2072 return false; 2032 result.errorcode = ERROR;
2033 return result;
2073 } 2034 }
2074 2035
2075 if (resultChecker != (s + length)) { 2036 if (resultChecker != (threshold_string + length)) {
2076 // Trailing symbols 2037 // Trailing symbols
2077 return false; 2038 result.errorcode = ERROR;
2078 } 2039 }
2079 2040
2080 return true; 2041 return result;
2081} 2042}
2082 2043
2083unsigned short icmp_checksum(uint16_t *p, size_t n) { 2044unsigned short icmp_checksum(uint16_t *packet, size_t packet_size) {
2084 unsigned short cksum;
2085 long sum = 0; 2045 long sum = 0;
2086 2046
2087 /* sizeof(uint16_t) == 2 */ 2047 /* sizeof(uint16_t) == 2 */
2088 while (n >= 2) { 2048 while (packet_size >= 2) {
2089 sum += *(p++); 2049 sum += *(packet++);
2090 n -= 2; 2050 packet_size -= 2;
2091 } 2051 }
2092 2052
2093 /* mop up the occasional odd byte */ 2053 /* mop up the occasional odd byte */
2094 if (n == 1) { 2054 if (packet_size == 1) {
2095 sum += *((uint8_t *)p - 1); 2055 sum += *((uint8_t *)packet);
2096 } 2056 }
2097 2057
2098 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 2058 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
2099 sum += (sum >> 16); /* add carry */ 2059 sum += (sum >> 16); /* add carry */
2100 cksum = ~sum; /* ones-complement, trunc to 16 bits */ 2060 unsigned short cksum;
2061 cksum = (unsigned short)~sum; /* ones-complement, trunc to 16 bits */
2101 2062
2102 return cksum; 2063 return cksum;
2103} 2064}
2104 2065
2105void print_help(void) { 2066void print_help(void) {
2106 /*print_revision (progname);*/ /* FIXME: Why? */ 2067 // print_revision (progname); /* FIXME: Why? */
2107 printf("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n"); 2068 printf("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n");
2108 2069
2109 printf(COPYRIGHT, copyright, email); 2070 printf(COPYRIGHT, copyright, email);
2110 2071
2111 printf("\n\n");
2112
2113 print_usage(); 2072 print_usage();
2114 2073
2115 printf(UT_HELP_VRSN); 2074 printf(UT_HELP_VRSN);
2116 printf(UT_EXTRA_OPTS); 2075 printf(UT_EXTRA_OPTS);
2117 2076
2118 printf(" %s\n", "-H"); 2077 printf(" -H, --Host=HOST\n");
2119 printf(" %s\n", _("specify a target")); 2078 printf(" %s\n",
2120 printf(" %s\n", "[-4|-6]"); 2079 _("specify a target, might be one of: resolveable name | IPv6 address | IPv4 address\n"
2121 printf(" %s\n", _("Use IPv4 (default) or IPv6 to communicate with the targets")); 2080 " (required, can be given multiple times)"));
2122 printf(" %s\n", "-w"); 2081 printf(" %s\n", "[-4|-6], [--ipv4-only|--ipv6-only]");
2123 printf(" %s", _("warning threshold (currently ")); 2082 printf(" %s\n", _("Use IPv4 or IPv6 only to communicate with the targets"));
2124 printf("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl); 2083 printf(" %s\n", "-w, --warning=WARN_VALUE");
2125 printf(" %s\n", "-c"); 2084 printf(" %s", _("warning threshold (default "));
2126 printf(" %s", _("critical threshold (currently ")); 2085 printf("%0.3fms,%u%%)\n", (float)DEFAULT_WARN_RTA / 1000, DEFAULT_WARN_PL);
2127 printf("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl); 2086 printf(" %s\n", "-c, --critical=CRIT_VALUE");
2128 2087 printf(" %s", _("critical threshold (default "));
2129 printf(" %s\n", "-R"); 2088 printf("%0.3fms,%u%%)\n", (float)DEFAULT_CRIT_RTA / 1000, DEFAULT_CRIT_PL);
2130 printf(" %s\n", _("RTA, round trip average, mode warning,critical, ex. 100ms,200ms unit in ms")); 2089
2131 printf(" %s\n", "-P"); 2090 printf(" %s\n", "-R, --rta-mode-thresholds=RTA_THRESHOLDS");
2091 printf(" %s\n",
2092 _("RTA (round trip average) mode warning,critical, ex. 100ms,200ms unit in ms"));
2093 printf(" %s\n", "-P, --packet-loss-mode-thresholds=PACKET_LOSS_THRESHOLD");
2132 printf(" %s\n", _("packet loss mode, ex. 40%,50% , unit in %")); 2094 printf(" %s\n", _("packet loss mode, ex. 40%,50% , unit in %"));
2133 printf(" %s\n", "-J"); 2095 printf(" %s\n", "-J, --jitter-mode-thresholds=JITTER_MODE_THRESHOLD");
2134 printf(" %s\n", _("jitter mode warning,critical, ex. 40.000ms,50.000ms , unit in ms ")); 2096 printf(" %s\n", _("jitter mode warning,critical, ex. 40.000ms,50.000ms , unit in ms "));
2135 printf(" %s\n", "-M"); 2097 printf(" %s\n", "-M, --mos-mode-thresholds=MOS_MODE_THRESHOLD");
2136 printf(" %s\n", _("MOS mode, between 0 and 4.4 warning,critical, ex. 3.5,3.0")); 2098 printf(" %s\n", _("MOS mode, between 0 and 4.4 warning,critical, ex. 3.5,3.0"));
2137 printf(" %s\n", "-S"); 2099 printf(" %s\n", "-S, --score-mode-thresholds=SCORE_MODE_THRESHOLD");
2138 printf(" %s\n", _("score mode, max value 100 warning,critical, ex. 80,70 ")); 2100 printf(" %s\n", _("score mode, max value 100 warning,critical, ex. 80,70 "));
2139 printf(" %s\n", "-O"); 2101 printf(" %s\n", "-O, --out-of-order-packets");
2140 printf(" %s\n", _("detect out of order ICMP packts ")); 2102 printf(
2141 printf(" %s\n", "-H"); 2103 " %s\n",
2142 printf(" %s\n", _("specify a target")); 2104 _("detect out of order ICMP packets, if such packets are found, the result is CRITICAL"));
2143 printf(" %s\n", "-s"); 2105 printf(" %s\n", "[-n|-p], --number-of-packets=NUMBER_OF_PACKETS");
2144 printf(" %s\n", _("specify a source IP address or device name")); 2106 printf(" %s", _("number of packets to send (default "));
2145 printf(" %s\n", "-n"); 2107 printf("%u)\n", DEFAULT_NUMBER_OF_PACKETS);
2146 printf(" %s", _("number of packets to send (currently ")); 2108
2147 printf("%u)\n", packets);
2148 printf(" %s\n", "-p");
2149 printf(" %s", _("number of packets to send (currently "));
2150 printf("%u)\n", packets);
2151 printf(" %s\n", "-i"); 2109 printf(" %s\n", "-i");
2152 printf(" %s", _("max packet interval (currently ")); 2110 printf(" %s", _("[DEPRECATED] packet interval (default "));
2153 printf("%0.3fms)\n", (float)pkt_interval / 1000); 2111 printf("%0.3fms)\n", (float)DEFAULT_PKT_INTERVAL / 1000);
2154 printf(" %s\n", "-I"); 2112 printf(" %s", _("This option was never actually used and is just mentioned here for "
2155 printf(" %s", _("max target interval (currently ")); 2113 "historical purposes\n"));
2156 printf("%0.3fms)\n", (float)target_interval / 1000); 2114
2157 printf(" %s\n", "-m"); 2115 printf(" %s\n", "-I, --target-interval=TARGET_INTERVAL");
2158 printf(" %s", _("number of alive hosts required for success")); 2116 printf(" %s%0.3fms)\n The time interval to wait in between one target and the next\n",
2117 _("max target interval (default "), (float)DEFAULT_TARGET_INTERVAL / 1000);
2118 printf(" %s\n", "-m, --minimal-host-alive=MIN_ALIVE");
2119 printf(" %s", _("number of alive hosts required for success. If less than MIN_ALIVE hosts "
2120 "are OK, but MIN_ALIVE hosts are WARNING or OK, WARNING, else CRITICAL"));
2159 printf("\n"); 2121 printf("\n");
2160 printf(" %s\n", "-l"); 2122 printf(" %s\n", "-l, --outgoing-ttl=OUTGOING_TTL");
2161 printf(" %s", _("TTL on outgoing packets (currently ")); 2123 printf(" %s", _("TTL on outgoing packets (default "));
2162 printf("%u)\n", ttl); 2124 printf("%u)\n", DEFAULT_TTL);
2163 printf(" %s\n", "-t"); 2125 printf(" %s\n", "-b, --size=SIZE");
2164 printf(" %s", _("timeout value (seconds, currently ")); 2126 printf(" %s\n", _("Number of icmp ping data bytes to send"));
2165 printf("%u)\n", timeout); 2127 printf(" %s %lu + %d)\n", _("Packet size will be SIZE + icmp header (default"),
2166 printf(" %s\n", "-b"); 2128 DEFAULT_PING_DATA_SIZE, ICMP_MINLEN);
2167 printf(" %s\n", _("Number of icmp data bytes to send")); 2129 printf(" %s\n", "-v, --verbose");
2168 printf(" %s %u + %d)\n", _("Packet size will be data bytes + icmp header (currently"), icmp_data_size, ICMP_MINLEN); 2130 printf(" %s\n", _("Verbosity, can be given multiple times (for debugging)"));
2169 printf(" %s\n", "-v"); 2131
2170 printf(" %s\n", _("verbose")); 2132 printf(UT_OUTPUT_FORMAT);
2133
2171 printf("\n"); 2134 printf("\n");
2172 printf("%s\n", _("Notes:")); 2135 printf("%s\n", _("Notes:"));
2173 printf(" %s\n", _("If none of R,P,J,M,S or O is specified, default behavior is -R -P")); 2136 printf(" %s\n", _("If none of R,P,J,M,S or O is specified, default behavior is -R -P"));
2174 printf(" %s\n", _("The -H switch is optional. Naming a host (or several) to check is not.")); 2137 printf(" %s\n", _("Naming a host (or several) to check is not."));
2175 printf("\n"); 2138 printf("\n");
2176 printf(" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%")); 2139 printf(" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%"));
2177 printf(" %s\n", _("packet loss. The default values should work well for most users.")); 2140 printf(" %s\n", _("packet loss. The default values should work well for most users."));
2178 printf(" %s\n", _("You can specify different RTA factors using the standardized abbreviations")); 2141 printf(" %s\n",
2179 printf(" %s\n", _("us (microseconds), ms (milliseconds, default) or just plain s for seconds.")); 2142 _("You can specify different RTA factors using the standardized abbreviations"));
2180 /* -d not yet implemented */ 2143 printf(" %s\n",
2181 /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means WARNING if >= 12 hops")); 2144 _("us (microseconds), ms (milliseconds, default) or just plain s for seconds."));
2182 printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent."));
2183 printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/
2184 printf("\n");
2185 printf(" %s\n", _("The -v switch can be specified several times for increased verbosity."));
2186 /* printf ("%s\n", _("Long options are currently unsupported."));
2187 printf ("%s\n", _("Options marked with * require an argument"));
2188 */
2189 2145
2190 printf(UT_SUPPORT); 2146 printf(UT_SUPPORT);
2191} 2147}
2192 2148
2193void print_usage(void) { 2149void print_usage(void) {
2194 printf("%s\n", _("Usage:")); 2150 printf("%s\n", _("Usage:"));
2195 printf(" %s [options] [-H] host1 host2 hostN\n", progname); 2151 printf(" %s [options] [-H host1 [-H host2 [-H hostN]]]\n", progname);
2152}
2153
2154static add_host_wrapper add_host(char *arg, check_icmp_execution_mode mode,
2155 sa_family_t enforced_proto) {
2156 if (debug) {
2157 printf("add_host called with argument %s\n", arg);
2158 }
2159
2160 add_host_wrapper result = {
2161 .error_code = OK,
2162 .host = check_icmp_target_container_init(),
2163 .has_v4 = false,
2164 .has_v6 = false,
2165 };
2166
2167 add_target_wrapper targets = add_target(arg, mode, enforced_proto);
2168
2169 if (targets.error_code != OK) {
2170 result.error_code = targets.error_code;
2171 return result;
2172 }
2173
2174 result.has_v4 = targets.has_v4;
2175 result.has_v6 = targets.has_v6;
2176
2177 result.host = check_icmp_target_container_init();
2178
2179 result.host.name = strdup(arg);
2180 result.host.target_list = targets.targets;
2181 result.host.number_of_targets = targets.number_of_targets;
2182
2183 return result;
2184}
2185
2186mp_subcheck evaluate_target(ping_target target, check_icmp_mode_switches modes,
2187 check_icmp_threshold warn, check_icmp_threshold crit) {
2188 /* if no new mode selected, use old schema */
2189 if (!modes.rta_mode && !modes.pl_mode && !modes.jitter_mode && !modes.score_mode &&
2190 !modes.mos_mode && !modes.order_mode) {
2191 modes.rta_mode = true;
2192 modes.pl_mode = true;
2193 }
2194
2195 mp_subcheck result = mp_subcheck_init();
2196 result = mp_set_subcheck_default_state(result, STATE_OK);
2197
2198 char address[INET6_ADDRSTRLEN];
2199 memset(address, 0, INET6_ADDRSTRLEN);
2200 parse_address(&target.address, address, sizeof(address));
2201
2202 xasprintf(&result.output, "%s", address);
2203
2204 double packet_loss;
2205 time_t rta;
2206 if (!target.icmp_recv) {
2207 /* rta 0 is of course not entirely correct, but will still show up
2208 * conspicuously as missing entries in perfparse and cacti */
2209 packet_loss = 100;
2210 rta = 0;
2211 result = mp_set_subcheck_state(result, STATE_CRITICAL);
2212 /* up the down counter if not already counted */
2213
2214 if (target.flags & FLAG_LOST_CAUSE) {
2215 xasprintf(&result.output, "%s: %s @ %s", result.output,
2216 get_icmp_error_msg(target.icmp_type, target.icmp_code), address);
2217 } else { /* not marked as lost cause, so we have no flags for it */
2218 xasprintf(&result.output, "%s", result.output);
2219 }
2220 } else {
2221 packet_loss =
2222 (unsigned char)((target.icmp_sent - target.icmp_recv) * 100) / target.icmp_sent;
2223 rta = target.time_waited / target.icmp_recv;
2224 }
2225
2226 double EffectiveLatency;
2227 double mos; /* Mean opinion score */
2228 double score; /* score */
2229
2230 if (target.icmp_recv > 1) {
2231 /*
2232 * This algorithm is probably pretty much blindly copied from
2233 * locations like this one:
2234 * https://www.slac.stanford.edu/comp/net/wan-mon/tutorial.html#mos It calculates a MOS
2235 * value (range of 1 to 5, where 1 is bad and 5 really good). According to some quick
2236 * research MOS originates from the Audio/Video transport network area. Whether it can
2237 * and should be computed from ICMP data, I can not say.
2238 *
2239 * Anyway the basic idea is to map a value "R" with a range of 0-100 to the MOS value
2240 *
2241 * MOS stands likely for Mean Opinion Score (
2242 * https://en.wikipedia.org/wiki/Mean_Opinion_Score )
2243 *
2244 * More links:
2245 * - https://confluence.slac.stanford.edu/display/IEPM/MOS
2246 */
2247 target.jitter = (target.jitter / (target.icmp_recv - 1) / 1000);
2248
2249 /*
2250 * Take the average round trip latency (in milliseconds), add
2251 * round trip jitter, but double the impact to latency
2252 * then add 10 for protocol latencies (in milliseconds).
2253 */
2254 EffectiveLatency = ((double)rta / 1000) + (target.jitter * 2) + 10;
2255
2256 double R;
2257 if (EffectiveLatency < 160) {
2258 R = 93.2 - (EffectiveLatency / 40);
2259 } else {
2260 R = 93.2 - ((EffectiveLatency - 120) / 10);
2261 }
2262
2263 // Now, let us deduct 2.5 R values per percentage of packet loss (i.e. a
2264 // loss of 5% will be entered as 5).
2265 R = R - (packet_loss * 2.5);
2266
2267 if (R < 0) {
2268 R = 0;
2269 }
2270
2271 score = R;
2272 mos = 1 + ((0.035) * R) + ((.000007) * R * (R - 60) * (100 - R));
2273 } else {
2274 target.jitter = 0;
2275 target.jitter_min = 0;
2276 target.jitter_max = 0;
2277 mos = 0;
2278 }
2279
2280 /* Check which mode is on and do the warn / Crit stuff */
2281 if (modes.rta_mode) {
2282 mp_subcheck sc_rta = mp_subcheck_init();
2283 sc_rta = mp_set_subcheck_default_state(sc_rta, STATE_OK);
2284 xasprintf(&sc_rta.output, "rta %0.3fms", (double)rta / 1000);
2285
2286 if (rta >= crit.rta) {
2287 sc_rta = mp_set_subcheck_state(sc_rta, STATE_CRITICAL);
2288 xasprintf(&sc_rta.output, "%s >= %0.3fms", sc_rta.output, (double)crit.rta / 1000);
2289 } else if (rta >= warn.rta) {
2290 sc_rta = mp_set_subcheck_state(sc_rta, STATE_WARNING);
2291 xasprintf(&sc_rta.output, "%s >= %0.3fms", sc_rta.output, (double)warn.rta / 1000);
2292 }
2293
2294 if (packet_loss < 100) {
2295 mp_perfdata pd_rta = perfdata_init();
2296 xasprintf(&pd_rta.label, "%srta", address);
2297 pd_rta.uom = strdup("ms");
2298 pd_rta.value = mp_create_pd_value(rta / 1000);
2299 pd_rta.min = mp_create_pd_value(0);
2300
2301 pd_rta.warn = mp_range_set_end(pd_rta.warn, mp_create_pd_value(warn.rta));
2302 pd_rta.crit = mp_range_set_end(pd_rta.crit, mp_create_pd_value(crit.rta));
2303 mp_add_perfdata_to_subcheck(&sc_rta, pd_rta);
2304
2305 mp_perfdata pd_rt_min = perfdata_init();
2306 xasprintf(&pd_rt_min.label, "%srtmin", address);
2307 pd_rt_min.value = mp_create_pd_value(target.rtmin / 1000);
2308 pd_rt_min.uom = strdup("ms");
2309 mp_add_perfdata_to_subcheck(&sc_rta, pd_rt_min);
2310
2311 mp_perfdata pd_rt_max = perfdata_init();
2312 xasprintf(&pd_rt_max.label, "%srtmax", address);
2313 pd_rt_max.value = mp_create_pd_value(target.rtmax / 1000);
2314 pd_rt_max.uom = strdup("ms");
2315 mp_add_perfdata_to_subcheck(&sc_rta, pd_rt_max);
2316 }
2317
2318 mp_add_subcheck_to_subcheck(&result, sc_rta);
2319 }
2320
2321 if (modes.pl_mode) {
2322 mp_subcheck sc_pl = mp_subcheck_init();
2323 sc_pl = mp_set_subcheck_default_state(sc_pl, STATE_OK);
2324 xasprintf(&sc_pl.output, "packet loss %.1f%%", packet_loss);
2325
2326 if (packet_loss >= crit.pl) {
2327 sc_pl = mp_set_subcheck_state(sc_pl, STATE_CRITICAL);
2328 xasprintf(&sc_pl.output, "%s >= %u%%", sc_pl.output, crit.pl);
2329 } else if (packet_loss >= warn.pl) {
2330 sc_pl = mp_set_subcheck_state(sc_pl, STATE_WARNING);
2331 xasprintf(&sc_pl.output, "%s >= %u%%", sc_pl.output, warn.pl);
2332 }
2333
2334 mp_perfdata pd_pl = perfdata_init();
2335 xasprintf(&pd_pl.label, "%spl", address);
2336 pd_pl.uom = strdup("%");
2337
2338 pd_pl.warn = mp_range_set_end(pd_pl.warn, mp_create_pd_value(warn.pl));
2339 pd_pl.crit = mp_range_set_end(pd_pl.crit, mp_create_pd_value(crit.pl));
2340 pd_pl.value = mp_create_pd_value(packet_loss);
2341
2342 mp_add_perfdata_to_subcheck(&sc_pl, pd_pl);
2343
2344 mp_add_subcheck_to_subcheck(&result, sc_pl);
2345 }
2346
2347 if (modes.jitter_mode) {
2348 mp_subcheck sc_jitter = mp_subcheck_init();
2349 sc_jitter = mp_set_subcheck_default_state(sc_jitter, STATE_OK);
2350 xasprintf(&sc_jitter.output, "jitter %0.3fms", target.jitter);
2351
2352 if (target.jitter >= crit.jitter) {
2353 sc_jitter = mp_set_subcheck_state(sc_jitter, STATE_CRITICAL);
2354 xasprintf(&sc_jitter.output, "%s >= %0.3fms", sc_jitter.output, crit.jitter);
2355 } else if (target.jitter >= warn.jitter) {
2356 sc_jitter = mp_set_subcheck_state(sc_jitter, STATE_WARNING);
2357 xasprintf(&sc_jitter.output, "%s >= %0.3fms", sc_jitter.output, warn.jitter);
2358 }
2359
2360 if (packet_loss < 100) {
2361 mp_perfdata pd_jitter = perfdata_init();
2362 pd_jitter.uom = strdup("ms");
2363 xasprintf(&pd_jitter.label, "%sjitter_avg", address);
2364 pd_jitter.value = mp_create_pd_value(target.jitter);
2365 pd_jitter.warn = mp_range_set_end(pd_jitter.warn, mp_create_pd_value(warn.jitter));
2366 pd_jitter.crit = mp_range_set_end(pd_jitter.crit, mp_create_pd_value(crit.jitter));
2367 mp_add_perfdata_to_subcheck(&sc_jitter, pd_jitter);
2368
2369 mp_perfdata pd_jitter_min = perfdata_init();
2370 pd_jitter_min.uom = strdup("ms");
2371 xasprintf(&pd_jitter_min.label, "%sjitter_min", address);
2372 pd_jitter_min.value = mp_create_pd_value(target.jitter_min);
2373 mp_add_perfdata_to_subcheck(&sc_jitter, pd_jitter_min);
2374
2375 mp_perfdata pd_jitter_max = perfdata_init();
2376 pd_jitter_max.uom = strdup("ms");
2377 xasprintf(&pd_jitter_max.label, "%sjitter_max", address);
2378 pd_jitter_max.value = mp_create_pd_value(target.jitter_max);
2379 mp_add_perfdata_to_subcheck(&sc_jitter, pd_jitter_max);
2380 }
2381 mp_add_subcheck_to_subcheck(&result, sc_jitter);
2382 }
2383
2384 if (modes.mos_mode) {
2385 mp_subcheck sc_mos = mp_subcheck_init();
2386 sc_mos = mp_set_subcheck_default_state(sc_mos, STATE_OK);
2387 xasprintf(&sc_mos.output, "MOS %0.1f", mos);
2388
2389 if (mos <= crit.mos) {
2390 sc_mos = mp_set_subcheck_state(sc_mos, STATE_CRITICAL);
2391 xasprintf(&sc_mos.output, "%s <= %0.1f", sc_mos.output, crit.mos);
2392 } else if (mos <= warn.mos) {
2393 sc_mos = mp_set_subcheck_state(sc_mos, STATE_WARNING);
2394 xasprintf(&sc_mos.output, "%s <= %0.1f", sc_mos.output, warn.mos);
2395 }
2396
2397 if (packet_loss < 100) {
2398 mp_perfdata pd_mos = perfdata_init();
2399 xasprintf(&pd_mos.label, "%smos", address);
2400 pd_mos.value = mp_create_pd_value(mos);
2401 pd_mos.warn = mp_range_set_end(pd_mos.warn, mp_create_pd_value(warn.mos));
2402 pd_mos.crit = mp_range_set_end(pd_mos.crit, mp_create_pd_value(crit.mos));
2403 pd_mos.min = mp_create_pd_value(0); // MOS starts at 0
2404 pd_mos.max = mp_create_pd_value(5); // MOS max is 5, by definition
2405 mp_add_perfdata_to_subcheck(&sc_mos, pd_mos);
2406 }
2407 mp_add_subcheck_to_subcheck(&result, sc_mos);
2408 }
2409
2410 if (modes.score_mode) {
2411 mp_subcheck sc_score = mp_subcheck_init();
2412 sc_score = mp_set_subcheck_default_state(sc_score, STATE_OK);
2413
2414 if (target.icmp_recv > 1) {
2415 xasprintf(&sc_score.output, "Score %f", score);
2416
2417 if (score <= crit.score) {
2418 sc_score = mp_set_subcheck_state(sc_score, STATE_CRITICAL);
2419 xasprintf(&sc_score.output, "%s <= %f", sc_score.output, crit.score);
2420 } else if (score <= warn.score) {
2421 sc_score = mp_set_subcheck_state(sc_score, STATE_WARNING);
2422 xasprintf(&sc_score.output, "%s <= %f", sc_score.output, warn.score);
2423 }
2424
2425 if (packet_loss < 100) {
2426 mp_perfdata pd_score = perfdata_init();
2427 xasprintf(&pd_score.label, "%sscore", address);
2428 pd_score.value = mp_create_pd_value(score);
2429 pd_score.warn = mp_range_set_end(pd_score.warn, mp_create_pd_value(warn.score));
2430 pd_score.crit = mp_range_set_end(pd_score.crit, mp_create_pd_value(crit.score));
2431 pd_score.min = mp_create_pd_value(0);
2432 pd_score.max = mp_create_pd_value(100);
2433 mp_add_perfdata_to_subcheck(&sc_score, pd_score);
2434 }
2435
2436 } else {
2437 // score mode disabled due to not enough received packages
2438 xasprintf(&sc_score.output, "Score mode disabled, not enough packets received");
2439 }
2440
2441 mp_add_subcheck_to_subcheck(&result, sc_score);
2442 }
2443
2444 if (modes.order_mode) {
2445 mp_subcheck sc_order = mp_subcheck_init();
2446 sc_order = mp_set_subcheck_default_state(sc_order, STATE_OK);
2447
2448 if (target.found_out_of_order_packets) {
2449 mp_set_subcheck_state(sc_order, STATE_CRITICAL);
2450 xasprintf(&sc_order.output, "Packets out of order");
2451 } else {
2452 xasprintf(&sc_order.output, "Packets in order");
2453 }
2454
2455 mp_add_subcheck_to_subcheck(&result, sc_order);
2456 }
2457
2458 return result;
2459}
2460
2461evaluate_host_wrapper evaluate_host(check_icmp_target_container host,
2462 check_icmp_mode_switches modes, check_icmp_threshold warn,
2463 check_icmp_threshold crit) {
2464 evaluate_host_wrapper result = {
2465 .targets_warn = 0,
2466 .targets_ok = 0,
2467 .sc_host = mp_subcheck_init(),
2468 };
2469 result.sc_host = mp_set_subcheck_default_state(result.sc_host, STATE_OK);
2470
2471 result.sc_host.output = strdup(host.name);
2472
2473 ping_target *target = host.target_list;
2474 for (unsigned int i = 0; i < host.number_of_targets; i++) {
2475 mp_subcheck sc_target = evaluate_target(*target, modes, warn, crit);
2476
2477 mp_state_enum target_state = mp_compute_subcheck_state(sc_target);
2478
2479 if (target_state == STATE_WARNING) {
2480 result.targets_warn++;
2481 } else if (target_state == STATE_OK) {
2482 result.targets_ok++;
2483 }
2484 mp_add_subcheck_to_subcheck(&result.sc_host, sc_target);
2485
2486 target = target->next;
2487 }
2488
2489 return result;
2196} 2490}