summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins-root/check_icmp.c1525
1 files changed, 742 insertions, 783 deletions
diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c
index f788d42..5ad1d84 100644
--- a/plugins-root/check_icmp.c
+++ b/plugins-root/check_icmp.c
@@ -1,40 +1,40 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_icmp plugin 3 * Monitoring check_icmp plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2005-2008 Monitoring Plugins Development Team 6 * Copyright (c) 2005-2008 Monitoring Plugins Development Team
7* Original Author : Andreas Ericsson <ae@op5.se> 7 * Original Author : Andreas Ericsson <ae@op5.se>
8* 8 *
9* Description: 9 * Description:
10* 10 *
11* This file contains the check_icmp plugin 11 * This file contains the check_icmp plugin
12* 12 *
13* Relevant RFC's: 792 (ICMP), 791 (IP) 13 * Relevant RFC's: 792 (ICMP), 791 (IP)
14* 14 *
15* This program was modeled somewhat after the check_icmp program, 15 * This program was modeled somewhat after the check_icmp program,
16* which was in turn a hack of fping (www.fping.org) but has been 16 * which was in turn a hack of fping (www.fping.org) but has been
17* completely rewritten since to generate higher precision rta values, 17 * completely rewritten since to generate higher precision rta values,
18* and support several different modes as well as setting ttl to control. 18 * and support several different modes as well as setting ttl to control.
19* redundant routes. The only remainders of fping is currently a few 19 * redundant routes. The only remainders of fping is currently a few
20* function names. 20 * function names.
21* 21 *
22* 22 *
23* This program is free software: you can redistribute it and/or modify 23 * This program is free software: you can redistribute it and/or modify
24* it under the terms of the GNU General Public License as published by 24 * it under the terms of the GNU General Public License as published by
25* the Free Software Foundation, either version 3 of the License, or 25 * the Free Software Foundation, either version 3 of the License, or
26* (at your option) any later version. 26 * (at your option) any later version.
27* 27 *
28* This program is distributed in the hope that it will be useful, 28 * This program is distributed in the hope that it will be useful,
29* but WITHOUT ANY WARRANTY; without even the implied warranty of 29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31* GNU General Public License for more details. 31 * GNU General Public License for more details.
32* 32 *
33* You should have received a copy of the GNU General Public License 33 * You should have received a copy of the GNU General Public License
34* along with this program. If not, see <http://www.gnu.org/licenses/>. 34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35* 35 *
36* 36 *
37*****************************************************************************/ 37 *****************************************************************************/
38 38
39/* progname may change */ 39/* progname may change */
40/* char *progname = "check_icmp"; */ 40/* char *progname = "check_icmp"; */
@@ -48,7 +48,7 @@ const char *email = "devel@monitoring-plugins.org";
48#include "utils.h" 48#include "utils.h"
49 49
50#if HAVE_SYS_SOCKIO_H 50#if HAVE_SYS_SOCKIO_H
51#include <sys/sockio.h> 51# include <sys/sockio.h>
52#endif 52#endif
53 53
54#include <sys/time.h> 54#include <sys/time.h>
@@ -65,84 +65,83 @@ const char *email = "devel@monitoring-plugins.org";
65#include <netinet/icmp6.h> 65#include <netinet/icmp6.h>
66#include <arpa/inet.h> 66#include <arpa/inet.h>
67 67
68
69/** sometimes undefined system macros (quite a few, actually) **/ 68/** sometimes undefined system macros (quite a few, actually) **/
70#ifndef MAXTTL 69#ifndef MAXTTL
71# define MAXTTL 255 70# define MAXTTL 255
72#endif 71#endif
73#ifndef INADDR_NONE 72#ifndef INADDR_NONE
74# define INADDR_NONE (in_addr_t)(-1) 73# define INADDR_NONE (in_addr_t)(-1)
75#endif 74#endif
76 75
77#ifndef SOL_IP 76#ifndef SOL_IP
78#define SOL_IP 0 77# define SOL_IP 0
79#endif 78#endif
80 79
81/* we bundle these in one #ifndef, since they're all from BSD 80/* we bundle these in one #ifndef, since they're all from BSD
82 * Put individual #ifndef's around those that bother you */ 81 * Put individual #ifndef's around those that bother you */
83#ifndef ICMP_UNREACH_NET_UNKNOWN 82#ifndef ICMP_UNREACH_NET_UNKNOWN
84# define ICMP_UNREACH_NET_UNKNOWN 6 83# define ICMP_UNREACH_NET_UNKNOWN 6
85# define ICMP_UNREACH_HOST_UNKNOWN 7 84# define ICMP_UNREACH_HOST_UNKNOWN 7
86# define ICMP_UNREACH_ISOLATED 8 85# define ICMP_UNREACH_ISOLATED 8
87# define ICMP_UNREACH_NET_PROHIB 9 86# define ICMP_UNREACH_NET_PROHIB 9
88# define ICMP_UNREACH_HOST_PROHIB 10 87# define ICMP_UNREACH_HOST_PROHIB 10
89# define ICMP_UNREACH_TOSNET 11 88# define ICMP_UNREACH_TOSNET 11
90# define ICMP_UNREACH_TOSHOST 12 89# define ICMP_UNREACH_TOSHOST 12
91#endif 90#endif
92/* tru64 has the ones above, but not these */ 91/* tru64 has the ones above, but not these */
93#ifndef ICMP_UNREACH_FILTER_PROHIB 92#ifndef ICMP_UNREACH_FILTER_PROHIB
94# define ICMP_UNREACH_FILTER_PROHIB 13 93# define ICMP_UNREACH_FILTER_PROHIB 13
95# define ICMP_UNREACH_HOST_PRECEDENCE 14 94# define ICMP_UNREACH_HOST_PRECEDENCE 14
96# define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 95# define ICMP_UNREACH_PRECEDENCE_CUTOFF 15
97#endif 96#endif
98 97
99typedef unsigned short range_t; /* type for get_range() -- unimplemented */ 98typedef unsigned short range_t; /* type for get_range() -- unimplemented */
100 99
101typedef struct rta_host { 100typedef struct rta_host {
102 unsigned short id; /* id in **table, and icmp pkts */ 101 unsigned short id; /* id in **table, and icmp pkts */
103 char *name; /* arg used for adding this host */ 102 char *name; /* arg used for adding this host */
104 char *msg; /* icmp error message, if any */ 103 char *msg; /* icmp error message, if any */
105 struct sockaddr_storage saddr_in; /* the address of this host */ 104 struct sockaddr_storage saddr_in; /* the address of this host */
106 struct sockaddr_storage error_addr; /* stores address of error replies */ 105 struct sockaddr_storage error_addr; /* stores address of error replies */
107 unsigned long long time_waited; /* total time waited, in usecs */ 106 unsigned long long time_waited; /* total time waited, in usecs */
108 unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */ 107 unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */
109 unsigned char icmp_type, icmp_code; /* type and code from errors */ 108 unsigned char icmp_type, icmp_code; /* type and code from errors */
110 unsigned short flags; /* control/status flags */ 109 unsigned short flags; /* control/status flags */
111 double rta; /* measured RTA */ 110 double rta; /* measured RTA */
112 int rta_status; // check result for RTA checks 111 int rta_status; // check result for RTA checks
113 double rtmax; /* max rtt */ 112 double rtmax; /* max rtt */
114 double rtmin; /* min rtt */ 113 double rtmin; /* min rtt */
115 double jitter; /* measured jitter */ 114 double jitter; /* measured jitter */
116 int jitter_status; // check result for Jitter checks 115 int jitter_status; // check result for Jitter checks
117 double jitter_max; /* jitter rtt maximum */ 116 double jitter_max; /* jitter rtt maximum */
118 double jitter_min; /* jitter rtt minimum */ 117 double jitter_min; /* jitter rtt minimum */
119 double EffectiveLatency; 118 double EffectiveLatency;
120 double mos; /* Mean opnion score */ 119 double mos; /* Mean opnion score */
121 int mos_status; // check result for MOS checks 120 int mos_status; // check result for MOS checks
122 double score; /* score */ 121 double score; /* score */
123 int score_status; // check result for score checks 122 int score_status; // check result for score checks
124 u_int last_tdiff; 123 u_int last_tdiff;
125 u_int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */ 124 u_int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */
126 unsigned char pl; /* measured packet loss */ 125 unsigned char pl; /* measured packet loss */
127 int pl_status; // check result for packet loss checks 126 int pl_status; // check result for packet loss checks
128 struct rta_host *next; /* linked list */ 127 struct rta_host *next; /* linked list */
129 int order_status; // check result for packet order checks 128 int order_status; // check result for packet order checks
130} rta_host; 129} rta_host;
131 130
132#define FLAG_LOST_CAUSE 0x01 /* decidedly dead target. */ 131#define FLAG_LOST_CAUSE 0x01 /* decidedly dead target. */
133 132
134/* threshold structure. all values are maximum allowed, exclusive */ 133/* threshold structure. all values are maximum allowed, exclusive */
135typedef struct threshold { 134typedef struct threshold {
136 unsigned char pl; /* max allowed packet loss in percent */ 135 unsigned char pl; /* max allowed packet loss in percent */
137 unsigned int rta; /* roundtrip time average, microseconds */ 136 unsigned int rta; /* roundtrip time average, microseconds */
138 double jitter; /* jitter time average, microseconds */ 137 double jitter; /* jitter time average, microseconds */
139 double mos; /* MOS */ 138 double mos; /* MOS */
140 double score; /* Score */ 139 double score; /* Score */
141} threshold; 140} threshold;
142 141
143/* the data structure */ 142/* the data structure */
144typedef struct icmp_ping_data { 143typedef struct icmp_ping_data {
145 struct timeval stime; /* timestamp (saved in protocol struct as well) */ 144 struct timeval stime; /* timestamp (saved in protocol struct as well) */
146 unsigned short ping_id; 145 unsigned short ping_id;
147} icmp_ping_data; 146} icmp_ping_data;
148 147
@@ -171,48 +170,42 @@ typedef union icmp_packet {
171 * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without 170 * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without
172 * tcp and udp args does this) 171 * tcp and udp args does this)
173 */ 172 */
174#define MODE_RTA 0 173#define MODE_RTA 0
175#define MODE_HOSTCHECK 1 174#define MODE_HOSTCHECK 1
176#define MODE_ALL 2 175#define MODE_ALL 2
177#define MODE_ICMP 3 176#define MODE_ICMP 3
178 177
179enum enum_threshold_mode { 178enum enum_threshold_mode { const_rta_mode, const_packet_loss_mode, const_jitter_mode, const_mos_mode, const_score_mode };
180 const_rta_mode,
181 const_packet_loss_mode,
182 const_jitter_mode,
183 const_mos_mode,
184 const_score_mode
185};
186 179
187typedef enum enum_threshold_mode threshold_mode; 180typedef enum enum_threshold_mode threshold_mode;
188 181
189/* the different ping types we can do 182/* the different ping types we can do
190 * TODO: investigate ARP ping as well */ 183 * TODO: investigate ARP ping as well */
191#define HAVE_ICMP 1 184#define HAVE_ICMP 1
192#define HAVE_UDP 2 185#define HAVE_UDP 2
193#define HAVE_TCP 4 186#define HAVE_TCP 4
194#define HAVE_ARP 8 187#define HAVE_ARP 8
195 188
196#define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data) 189#define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data)
197#define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */ 190#define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */
198#define IP_HDR_SIZE 20 191#define IP_HDR_SIZE 20
199#define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN) 192#define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN)
200#define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44) 193#define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44)
201 194
202/* various target states */ 195/* various target states */
203#define TSTATE_INACTIVE 0x01 /* don't ping this host anymore */ 196#define TSTATE_INACTIVE 0x01 /* don't ping this host anymore */
204#define TSTATE_WAITING 0x02 /* unanswered packets on the wire */ 197#define TSTATE_WAITING 0x02 /* unanswered packets on the wire */
205#define TSTATE_ALIVE 0x04 /* target is alive (has answered something) */ 198#define TSTATE_ALIVE 0x04 /* target is alive (has answered something) */
206#define TSTATE_UNREACH 0x08 199#define TSTATE_UNREACH 0x08
207 200
208/** prototypes **/ 201/** prototypes **/
209void print_help (void); 202void print_help(void);
210void print_usage (void); 203void print_usage(void);
211static u_int get_timevar(const char *); 204static u_int get_timevar(const char *);
212static u_int get_timevaldiff(struct timeval *, struct timeval *); 205static u_int get_timevaldiff(struct timeval *, struct timeval *);
213static in_addr_t get_ip_address(const char *); 206static in_addr_t get_ip_address(const char *);
214static int wait_for_reply(int, u_int); 207static int wait_for_reply(int, u_int);
215static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *, struct timeval*); 208static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *, struct timeval *);
216static int send_icmp_ping(int, struct rta_host *); 209static int send_icmp_ping(int, struct rta_host *);
217static int get_threshold(char *str, threshold *th); 210static int get_threshold(char *str, threshold *th);
218static bool get_threshold2(char *str, size_t length, threshold *, threshold *, threshold_mode mode); 211static bool get_threshold2(char *str, size_t length, threshold *, threshold *, threshold_mode mode);
@@ -235,20 +228,8 @@ extern char **environ;
235/** global variables **/ 228/** global variables **/
236static struct rta_host **table, *cursor, *list; 229static struct rta_host **table, *cursor, *list;
237 230
238static threshold crit = { 231static threshold crit = {.pl = 80, .rta = 500000, .jitter = 0.0, .mos = 0.0, .score = 0.0};
239 .pl = 80, 232static threshold warn = {.pl = 40, .rta = 200000, .jitter = 0.0, .mos = 0.0, .score = 0.0};
240 .rta = 500000,
241 .jitter = 0.0,
242 .mos = 0.0,
243 .score = 0.0
244};
245static threshold warn = {
246 .pl = 40,
247 .rta = 200000,
248 .jitter = 0.0,
249 .mos = 0.0,
250 .score = 0.0
251};
252 233
253static int mode, protocols, sockets, debug = 0, timeout = 10; 234static int mode, protocols, sockets, debug = 0, timeout = 10;
254static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE; 235static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE;
@@ -268,17 +249,15 @@ static unsigned int warn_down = 1, crit_down = 1; /* host down threshold values
268static int min_hosts_alive = -1; 249static int min_hosts_alive = -1;
269float pkt_backoff_factor = 1.5; 250float pkt_backoff_factor = 1.5;
270float target_backoff_factor = 1.5; 251float target_backoff_factor = 1.5;
271bool rta_mode=false; 252bool rta_mode = false;
272bool pl_mode=false; 253bool pl_mode = false;
273bool jitter_mode=false; 254bool jitter_mode = false;
274bool score_mode=false; 255bool score_mode = false;
275bool mos_mode=false; 256bool mos_mode = false;
276bool order_mode=false; 257bool order_mode = false;
277 258
278/** code start **/ 259/** code start **/
279static void 260static void crash(const char *fmt, ...) {
280crash(const char *fmt, ...)
281{
282 va_list ap; 261 va_list ap;
283 262
284 printf("%s: ", progname); 263 printf("%s: ", progname);
@@ -287,55 +266,100 @@ crash(const char *fmt, ...)
287 vprintf(fmt, ap); 266 vprintf(fmt, ap);
288 va_end(ap); 267 va_end(ap);
289 268
290 if(errno) printf(": %s", strerror(errno)); 269 if (errno)
270 printf(": %s", strerror(errno));
291 puts(""); 271 puts("");
292 272
293 exit(3); 273 exit(3);
294} 274}
295 275
296 276static const char *get_icmp_error_msg(unsigned char icmp_type, unsigned char icmp_code) {
297static const char *
298get_icmp_error_msg(unsigned char icmp_type, unsigned char icmp_code)
299{
300 const char *msg = "unreachable"; 277 const char *msg = "unreachable";
301 278
302 if(debug > 1) printf("get_icmp_error_msg(%u, %u)\n", icmp_type, icmp_code); 279 if (debug > 1)
303 switch(icmp_type) { 280 printf("get_icmp_error_msg(%u, %u)\n", icmp_type, icmp_code);
281 switch (icmp_type) {
304 case ICMP_UNREACH: 282 case ICMP_UNREACH:
305 switch(icmp_code) { 283 switch (icmp_code) {
306 case ICMP_UNREACH_NET: msg = "Net unreachable"; break; 284 case ICMP_UNREACH_NET:
307 case ICMP_UNREACH_HOST: msg = "Host unreachable"; break; 285 msg = "Net unreachable";
308 case ICMP_UNREACH_PROTOCOL: msg = "Protocol unreachable (firewall?)"; break; 286 break;
309 case ICMP_UNREACH_PORT: msg = "Port unreachable (firewall?)"; break; 287 case ICMP_UNREACH_HOST:
310 case ICMP_UNREACH_NEEDFRAG: msg = "Fragmentation needed"; break; 288 msg = "Host unreachable";
311 case ICMP_UNREACH_SRCFAIL: msg = "Source route failed"; break; 289 break;
312 case ICMP_UNREACH_ISOLATED: msg = "Source host isolated"; break; 290 case ICMP_UNREACH_PROTOCOL:
313 case ICMP_UNREACH_NET_UNKNOWN: msg = "Unknown network"; break; 291 msg = "Protocol unreachable (firewall?)";
314 case ICMP_UNREACH_HOST_UNKNOWN: msg = "Unknown host"; break; 292 break;
315 case ICMP_UNREACH_NET_PROHIB: msg = "Network denied (firewall?)"; break; 293 case ICMP_UNREACH_PORT:
316 case ICMP_UNREACH_HOST_PROHIB: msg = "Host denied (firewall?)"; break; 294 msg = "Port unreachable (firewall?)";
317 case ICMP_UNREACH_TOSNET: msg = "Bad TOS for network (firewall?)"; break; 295 break;
318 case ICMP_UNREACH_TOSHOST: msg = "Bad TOS for host (firewall?)"; break; 296 case ICMP_UNREACH_NEEDFRAG:
319 case ICMP_UNREACH_FILTER_PROHIB: msg = "Prohibited by filter (firewall)"; break; 297 msg = "Fragmentation needed";
320 case ICMP_UNREACH_HOST_PRECEDENCE: msg = "Host precedence violation"; break; 298 break;
321 case ICMP_UNREACH_PRECEDENCE_CUTOFF: msg = "Precedence cutoff"; break; 299 case ICMP_UNREACH_SRCFAIL:
322 default: msg = "Invalid code"; break; 300 msg = "Source route failed";
301 break;
302 case ICMP_UNREACH_ISOLATED:
303 msg = "Source host isolated";
304 break;
305 case ICMP_UNREACH_NET_UNKNOWN:
306 msg = "Unknown network";
307 break;
308 case ICMP_UNREACH_HOST_UNKNOWN:
309 msg = "Unknown host";
310 break;
311 case ICMP_UNREACH_NET_PROHIB:
312 msg = "Network denied (firewall?)";
313 break;
314 case ICMP_UNREACH_HOST_PROHIB:
315 msg = "Host denied (firewall?)";
316 break;
317 case ICMP_UNREACH_TOSNET:
318 msg = "Bad TOS for network (firewall?)";
319 break;
320 case ICMP_UNREACH_TOSHOST:
321 msg = "Bad TOS for host (firewall?)";
322 break;
323 case ICMP_UNREACH_FILTER_PROHIB:
324 msg = "Prohibited by filter (firewall)";
325 break;
326 case ICMP_UNREACH_HOST_PRECEDENCE:
327 msg = "Host precedence violation";
328 break;
329 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
330 msg = "Precedence cutoff";
331 break;
332 default:
333 msg = "Invalid code";
334 break;
323 } 335 }
324 break; 336 break;
325 337
326 case ICMP_TIMXCEED: 338 case ICMP_TIMXCEED:
327 /* really 'out of reach', or non-existent host behind a router serving 339 /* really 'out of reach', or non-existent host behind a router serving
328 * two different subnets */ 340 * two different subnets */
329 switch(icmp_code) { 341 switch (icmp_code) {
330 case ICMP_TIMXCEED_INTRANS: msg = "Time to live exceeded in transit"; break; 342 case ICMP_TIMXCEED_INTRANS:
331 case ICMP_TIMXCEED_REASS: msg = "Fragment reassembly time exceeded"; break; 343 msg = "Time to live exceeded in transit";
332 default: msg = "Invalid code"; break; 344 break;
345 case ICMP_TIMXCEED_REASS:
346 msg = "Fragment reassembly time exceeded";
347 break;
348 default:
349 msg = "Invalid code";
350 break;
333 } 351 }
334 break; 352 break;
335 353
336 case ICMP_SOURCEQUENCH: msg = "Transmitting too fast"; break; 354 case ICMP_SOURCEQUENCH:
337 case ICMP_REDIRECT: msg = "Redirect (change route)"; break; 355 msg = "Transmitting too fast";
338 case ICMP_PARAMPROB: msg = "Bad IP header (required option absent)"; break; 356 break;
357 case ICMP_REDIRECT:
358 msg = "Redirect (change route)";
359 break;
360 case ICMP_PARAMPROB:
361 msg = "Bad IP header (required option absent)";
362 break;
339 363
340 /* the following aren't error messages, so ignore */ 364 /* the following aren't error messages, so ignore */
341 case ICMP_TSTAMP: 365 case ICMP_TSTAMP:
@@ -344,25 +368,26 @@ get_icmp_error_msg(unsigned char icmp_type, unsigned char icmp_code)
344 case ICMP_IREQREPLY: 368 case ICMP_IREQREPLY:
345 case ICMP_MASKREQ: 369 case ICMP_MASKREQ:
346 case ICMP_MASKREPLY: 370 case ICMP_MASKREPLY:
347 default: msg = ""; break; 371 default:
372 msg = "";
373 break;
348 } 374 }
349 375
350 return msg; 376 return msg;
351} 377}
352 378
353static int 379static int handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr) {
354handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr)
355{
356 struct icmp p, sent_icmp; 380 struct icmp p, sent_icmp;
357 struct rta_host *host = NULL; 381 struct rta_host *host = NULL;
358 382
359 memcpy(&p, packet, sizeof(p)); 383 memcpy(&p, packet, sizeof(p));
360 if(p.icmp_type == ICMP_ECHO && ntohs(p.icmp_id) == pid) { 384 if (p.icmp_type == ICMP_ECHO && ntohs(p.icmp_id) == pid) {
361 /* echo request from us to us (pinging localhost) */ 385 /* echo request from us to us (pinging localhost) */
362 return 0; 386 return 0;
363 } 387 }
364 388
365 if(debug) printf("handle_random_icmp(%p, %p)\n", (void *)&p, (void *)addr); 389 if (debug)
390 printf("handle_random_icmp(%p, %p)\n", (void *)&p, (void *)addr);
366 391
367 /* only handle a few types, since others can't possibly be replies to 392 /* only handle a few types, since others can't possibly be replies to
368 * us in a sane network (if it is anyway, it will be counted as lost 393 * us in a sane network (if it is anyway, it will be counted as lost
@@ -374,44 +399,39 @@ handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr)
374 * TIMXCEED actually sends a proper icmp response we will have passed 399 * TIMXCEED actually sends a proper icmp response we will have passed
375 * too many hops to have a hope of reaching it later, in which case it 400 * too many hops to have a hope of reaching it later, in which case it
376 * indicates overconfidence in the network, poor routing or both. */ 401 * indicates overconfidence in the network, poor routing or both. */
377 if(p.icmp_type != ICMP_UNREACH && p.icmp_type != ICMP_TIMXCEED && 402 if (p.icmp_type != ICMP_UNREACH && p.icmp_type != ICMP_TIMXCEED && p.icmp_type != ICMP_SOURCEQUENCH && p.icmp_type != ICMP_PARAMPROB) {
378 p.icmp_type != ICMP_SOURCEQUENCH && p.icmp_type != ICMP_PARAMPROB)
379 {
380 return 0; 403 return 0;
381 } 404 }
382 405
383 /* might be for us. At least it holds the original package (according 406 /* might be for us. At least it holds the original package (according
384 * to RFC 792). If it isn't, just ignore it */ 407 * to RFC 792). If it isn't, just ignore it */
385 memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp)); 408 memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
386 if(sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != pid || 409 if (sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != pid || ntohs(sent_icmp.icmp_seq) >= targets * packets) {
387 ntohs(sent_icmp.icmp_seq) >= targets*packets) 410 if (debug)
388 { 411 printf("Packet is no response to a packet we sent\n");
389 if(debug) printf("Packet is no response to a packet we sent\n");
390 return 0; 412 return 0;
391 } 413 }
392 414
393 /* it is indeed a response for us */ 415 /* it is indeed a response for us */
394 host = table[ntohs(sent_icmp.icmp_seq)/packets]; 416 host = table[ntohs(sent_icmp.icmp_seq) / packets];
395 if(debug) { 417 if (debug) {
396 char address[INET6_ADDRSTRLEN]; 418 char address[INET6_ADDRSTRLEN];
397 parse_address(addr, address, sizeof(address)); 419 parse_address(addr, address, sizeof(address));
398 printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n", 420 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);
399 get_icmp_error_msg(p.icmp_type, p.icmp_code),
400 address, host->name);
401 } 421 }
402 422
403 icmp_lost++; 423 icmp_lost++;
404 host->icmp_lost++; 424 host->icmp_lost++;
405 /* don't spend time on lost hosts any more */ 425 /* don't spend time on lost hosts any more */
406 if(host->flags & FLAG_LOST_CAUSE) return 0; 426 if (host->flags & FLAG_LOST_CAUSE)
427 return 0;
407 428
408 /* source quench means we're sending too fast, so increase the 429 /* source quench means we're sending too fast, so increase the
409 * interval and mark this packet lost */ 430 * interval and mark this packet lost */
410 if(p.icmp_type == ICMP_SOURCEQUENCH) { 431 if (p.icmp_type == ICMP_SOURCEQUENCH) {
411 pkt_interval *= pkt_backoff_factor; 432 pkt_interval *= pkt_backoff_factor;
412 target_interval *= target_backoff_factor; 433 target_interval *= target_backoff_factor;
413 } 434 } else {
414 else {
415 targets_down++; 435 targets_down++;
416 host->flags |= FLAG_LOST_CAUSE; 436 host->flags |= FLAG_LOST_CAUSE;
417 } 437 }
@@ -422,21 +442,18 @@ handle_random_icmp(unsigned char *packet, struct sockaddr_storage *addr)
422 return 0; 442 return 0;
423} 443}
424 444
425void parse_address(struct sockaddr_storage *addr, char *address, int size) 445void parse_address(struct sockaddr_storage *addr, char *address, int size) {
426{
427 switch (address_family) { 446 switch (address_family) {
428 case AF_INET: 447 case AF_INET:
429 inet_ntop(address_family, &((struct sockaddr_in *)addr)->sin_addr, address, size); 448 inet_ntop(address_family, &((struct sockaddr_in *)addr)->sin_addr, address, size);
430 break; 449 break;
431 case AF_INET6: 450 case AF_INET6:
432 inet_ntop(address_family, &((struct sockaddr_in6 *)addr)->sin6_addr, address, size); 451 inet_ntop(address_family, &((struct sockaddr_in6 *)addr)->sin6_addr, address, size);
433 break; 452 break;
434 } 453 }
435} 454}
436 455
437int 456int main(int argc, char **argv) {
438main(int argc, char **argv)
439{
440 int i; 457 int i;
441 char *ptr; 458 char *ptr;
442 long int arg; 459 long int arg;
@@ -450,10 +467,10 @@ main(int argc, char **argv)
450 int on = 1; 467 int on = 1;
451#endif 468#endif
452 char *source_ip = NULL; 469 char *source_ip = NULL;
453 char * opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64"; 470 char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64";
454 setlocale (LC_ALL, ""); 471 setlocale(LC_ALL, "");
455 bindtextdomain (PACKAGE, LOCALEDIR); 472 bindtextdomain(PACKAGE, LOCALEDIR);
456 textdomain (PACKAGE); 473 textdomain(PACKAGE);
457 474
458 /* we only need to be setsuid when we get the sockets, so do 475 /* we only need to be setsuid when we get the sockets, so do
459 * that before pointer magic (esp. on network data) */ 476 * that before pointer magic (esp. on network data) */
@@ -465,8 +482,10 @@ main(int argc, char **argv)
465 /* get calling name the old-fashioned way for portability instead 482 /* get calling name the old-fashioned way for portability instead
466 * of relying on the glibc-ism __progname */ 483 * of relying on the glibc-ism __progname */
467 ptr = strrchr(argv[0], '/'); 484 ptr = strrchr(argv[0], '/');
468 if(ptr) progname = &ptr[1]; 485 if (ptr)
469 else progname = argv[0]; 486 progname = &ptr[1];
487 else
488 progname = argv[0];
470 489
471 /* now set defaults. Use progname to set them initially (allows for 490 /* now set defaults. Use progname to set them initially (allows for
472 * superfast check_host program when target host is up */ 491 * superfast check_host program when target host is up */
@@ -478,31 +497,29 @@ main(int argc, char **argv)
478 crit.rta = 500000; 497 crit.rta = 500000;
479 crit.pl = 80; 498 crit.pl = 80;
480 crit.jitter = 50; 499 crit.jitter = 50;
481 crit.mos= 3; 500 crit.mos = 3;
482 crit.score=70; 501 crit.score = 70;
483 /* Default warning thresholds */ 502 /* Default warning thresholds */
484 warn.rta = 200000; 503 warn.rta = 200000;
485 warn.pl = 40; 504 warn.pl = 40;
486 warn.jitter = 40; 505 warn.jitter = 40;
487 warn.mos= 3.5; 506 warn.mos = 3.5;
488 warn.score=80; 507 warn.score = 80;
489 508
490 protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP; 509 protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP;
491 pkt_interval = 80000; /* 80 msec packet interval by default */ 510 pkt_interval = 80000; /* 80 msec packet interval by default */
492 packets = 5; 511 packets = 5;
493 512
494 if(!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) { 513 if (!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) {
495 mode = MODE_ICMP; 514 mode = MODE_ICMP;
496 protocols = HAVE_ICMP; 515 protocols = HAVE_ICMP;
497 } 516 } else if (!strcmp(progname, "check_host")) {
498 else if(!strcmp(progname, "check_host")) {
499 mode = MODE_HOSTCHECK; 517 mode = MODE_HOSTCHECK;
500 pkt_interval = 1000000; 518 pkt_interval = 1000000;
501 packets = 5; 519 packets = 5;
502 crit.rta = warn.rta = 1000000; 520 crit.rta = warn.rta = 1000000;
503 crit.pl = warn.pl = 100; 521 crit.pl = warn.pl = 100;
504 } 522 } else if (!strcmp(progname, "check_rta_multi")) {
505 else if(!strcmp(progname, "check_rta_multi")) {
506 mode = MODE_ALL; 523 mode = MODE_ALL;
507 target_interval = 0; 524 target_interval = 0;
508 pkt_interval = 50000; 525 pkt_interval = 50000;
@@ -510,17 +527,17 @@ main(int argc, char **argv)
510 } 527 }
511 528
512 /* support "--help" and "--version" */ 529 /* support "--help" and "--version" */
513 if(argc == 2) { 530 if (argc == 2) {
514 if(!strcmp(argv[1], "--help")) 531 if (!strcmp(argv[1], "--help"))
515 strcpy(argv[1], "-h"); 532 strcpy(argv[1], "-h");
516 if(!strcmp(argv[1], "--version")) 533 if (!strcmp(argv[1], "--version"))
517 strcpy(argv[1], "-V"); 534 strcpy(argv[1], "-V");
518 } 535 }
519 536
520 /* Parse protocol arguments first */ 537 /* Parse protocol arguments first */
521 for(i = 1; i < argc; i++) { 538 for (i = 1; i < argc; i++) {
522 while((arg = getopt(argc, argv, opts_str)) != EOF) { 539 while ((arg = getopt(argc, argv, opts_str)) != EOF) {
523 switch(arg) { 540 switch (arg) {
524 case '4': 541 case '4':
525 if (address_family != -1) 542 if (address_family != -1)
526 crash("Multiple protocol versions not supported"); 543 crash("Multiple protocol versions not supported");
@@ -532,7 +549,7 @@ main(int argc, char **argv)
532 crash("Multiple protocol versions not supported"); 549 crash("Multiple protocol versions not supported");
533 address_family = AF_INET6; 550 address_family = AF_INET6;
534#else 551#else
535 usage (_("IPv6 support not available\n")); 552 usage(_("IPv6 support not available\n"));
536#endif 553#endif
537 break; 554 break;
538 } 555 }
@@ -545,22 +562,19 @@ main(int argc, char **argv)
545 unsigned long size; 562 unsigned long size;
546 bool err; 563 bool err;
547 /* parse the arguments */ 564 /* parse the arguments */
548 for(i = 1; i < argc; i++) { 565 for (i = 1; i < argc; i++) {
549 while((arg = getopt(argc, argv, opts_str)) != EOF) { 566 while ((arg = getopt(argc, argv, opts_str)) != EOF) {
550 switch(arg) { 567 switch (arg) {
551 case 'v': 568 case 'v':
552 debug++; 569 debug++;
553 break; 570 break;
554 case 'b': 571 case 'b':
555 size = strtol(optarg,NULL,0); 572 size = strtol(optarg, NULL, 0);
556 if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) && 573 if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) && size < MAX_PING_DATA) {
557 size < MAX_PING_DATA) {
558 icmp_data_size = size; 574 icmp_data_size = size;
559 icmp_pkt_size = size + ICMP_MINLEN; 575 icmp_pkt_size = size + ICMP_MINLEN;
560 } else 576 } else
561 usage_va("ICMP data length must be between: %lu and %lu", 577 usage_va("ICMP data length must be between: %lu and %lu", sizeof(struct icmp) + sizeof(struct icmp_ping_data), MAX_PING_DATA - 1);
562 sizeof(struct icmp) + sizeof(struct icmp_ping_data),
563 MAX_PING_DATA - 1);
564 break; 578 break;
565 case 'i': 579 case 'i':
566 pkt_interval = get_timevar(optarg); 580 pkt_interval = get_timevar(optarg);
@@ -580,7 +594,8 @@ main(int argc, char **argv)
580 break; 594 break;
581 case 't': 595 case 't':
582 timeout = strtoul(optarg, NULL, 0); 596 timeout = strtoul(optarg, NULL, 0);
583 if(!timeout) timeout = 10; 597 if (!timeout)
598 timeout = 10;
584 break; 599 break;
585 case 'H': 600 case 'H':
586 add_target(optarg); 601 add_target(optarg);
@@ -593,7 +608,7 @@ main(int argc, char **argv)
593 break; 608 break;
594 case 'd': /* implement later, for cluster checks */ 609 case 'd': /* implement later, for cluster checks */
595 warn_down = (unsigned char)strtoul(optarg, &ptr, 0); 610 warn_down = (unsigned char)strtoul(optarg, &ptr, 0);
596 if(ptr) { 611 if (ptr) {
597 crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0); 612 crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0);
598 } 613 }
599 break; 614 break;
@@ -601,54 +616,54 @@ main(int argc, char **argv)
601 source_ip = optarg; 616 source_ip = optarg;
602 break; 617 break;
603 case 'V': /* version */ 618 case 'V': /* version */
604 print_revision (progname, NP_VERSION); 619 print_revision(progname, NP_VERSION);
605 exit (STATE_UNKNOWN); 620 exit(STATE_UNKNOWN);
606 case 'h': /* help */ 621 case 'h': /* help */
607 print_help (); 622 print_help();
608 exit (STATE_UNKNOWN); 623 exit(STATE_UNKNOWN);
609 break; 624 break;
610 case 'R': /* RTA mode */ 625 case 'R': /* RTA mode */
611 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_rta_mode); 626 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_rta_mode);
612 if (!err) { 627 if (!err) {
613 crash("Failed to parse RTA threshold"); 628 crash("Failed to parse RTA threshold");
614 } 629 }
615 630
616 rta_mode=true; 631 rta_mode = true;
617 break; 632 break;
618 case 'P': /* packet loss mode */ 633 case 'P': /* packet loss mode */
619 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_packet_loss_mode); 634 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_packet_loss_mode);
620 if (!err) { 635 if (!err) {
621 crash("Failed to parse packet loss threshold"); 636 crash("Failed to parse packet loss threshold");
622 } 637 }
623 638
624 pl_mode=true; 639 pl_mode = true;
625 break; 640 break;
626 case 'J': /* jitter mode */ 641 case 'J': /* jitter mode */
627 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_jitter_mode); 642 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_jitter_mode);
628 if (!err) { 643 if (!err) {
629 crash("Failed to parse jitter threshold"); 644 crash("Failed to parse jitter threshold");
630 } 645 }
631 646
632 jitter_mode=true; 647 jitter_mode = true;
633 break; 648 break;
634 case 'M': /* MOS mode */ 649 case 'M': /* MOS mode */
635 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_mos_mode); 650 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_mos_mode);
636 if (!err) { 651 if (!err) {
637 crash("Failed to parse MOS threshold"); 652 crash("Failed to parse MOS threshold");
638 } 653 }
639 654
640 mos_mode=true; 655 mos_mode = true;
641 break; 656 break;
642 case 'S': /* score mode */ 657 case 'S': /* score mode */
643 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_score_mode); 658 err = get_threshold2(optarg, strlen(optarg), &warn, &crit, const_score_mode);
644 if (!err) { 659 if (!err) {
645 crash("Failed to parse score threshold"); 660 crash("Failed to parse score threshold");
646 } 661 }
647 662
648 score_mode=true; 663 score_mode = true;
649 break; 664 break;
650 case 'O': /* out of order mode */ 665 case 'O': /* out of order mode */
651 order_mode=true; 666 order_mode = true;
652 break; 667 break;
653 } 668 }
654 } 669 }
@@ -663,37 +678,42 @@ main(int argc, char **argv)
663 /* printf("pid = %u\n", pid); */ 678 /* printf("pid = %u\n", pid); */
664 679
665 /* Parse extra opts if any */ 680 /* Parse extra opts if any */
666 argv=np_extra_opts(&argc, argv, progname); 681 argv = np_extra_opts(&argc, argv, progname);
667 682
668 argv = &argv[optind]; 683 argv = &argv[optind];
669 while(*argv) { 684 while (*argv) {
670 add_target(*argv); 685 add_target(*argv);
671 argv++; 686 argv++;
672 } 687 }
673 688
674 if(!targets) { 689 if (!targets) {
675 errno = 0; 690 errno = 0;
676 crash("No hosts to check"); 691 crash("No hosts to check");
677 } 692 }
678 693
679 // add_target might change address_family 694 // add_target might change address_family
680 switch ( address_family ){ 695 switch (address_family) {
681 case AF_INET: icmp_proto = IPPROTO_ICMP; 696 case AF_INET:
682 break; 697 icmp_proto = IPPROTO_ICMP;
683 case AF_INET6: icmp_proto = IPPROTO_ICMPV6; 698 break;
684 break; 699 case AF_INET6:
685 default: crash("Address family not supported"); 700 icmp_proto = IPPROTO_ICMPV6;
701 break;
702 default:
703 crash("Address family not supported");
686 } 704 }
687 if((icmp_sock = socket(address_family, SOCK_RAW, icmp_proto)) != -1) 705 if ((icmp_sock = socket(address_family, SOCK_RAW, icmp_proto)) != -1)
688 sockets |= HAVE_ICMP; 706 sockets |= HAVE_ICMP;
689 else icmp_sockerrno = errno; 707 else
708 icmp_sockerrno = errno;
690 709
691 if( source_ip ) 710 if (source_ip)
692 set_source_ip(source_ip); 711 set_source_ip(source_ip);
693 712
694#ifdef SO_TIMESTAMP 713#ifdef SO_TIMESTAMP
695 if(setsockopt(icmp_sock, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on))) 714 if (setsockopt(icmp_sock, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)))
696 if(debug) printf("Warning: no SO_TIMESTAMP support\n"); 715 if (debug)
716 printf("Warning: no SO_TIMESTAMP support\n");
697#endif // SO_TIMESTAMP 717#endif // SO_TIMESTAMP
698 718
699 /* now drop privileges (no effect if not setsuid or geteuid() == 0) */ 719 /* now drop privileges (no effect if not setsuid or geteuid() == 0) */
@@ -702,8 +722,8 @@ main(int argc, char **argv)
702 return 1; 722 return 1;
703 } 723 }
704 724
705 if(!sockets) { 725 if (!sockets) {
706 if(icmp_sock == -1) { 726 if (icmp_sock == -1) {
707 errno = icmp_sockerrno; 727 errno = icmp_sockerrno;
708 crash("Failed to obtain ICMP socket"); 728 crash("Failed to obtain ICMP socket");
709 return -1; 729 return -1;
@@ -719,13 +739,16 @@ main(int argc, char **argv)
719 /* return -1; */ 739 /* return -1; */
720 /* } */ 740 /* } */
721 } 741 }
722 if(!ttl) ttl = 64; 742 if (!ttl)
743 ttl = 64;
723 744
724 if(icmp_sock) { 745 if (icmp_sock) {
725 result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl)); 746 result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
726 if(debug) { 747 if (debug) {
727 if(result == -1) printf("setsockopt failed\n"); 748 if (result == -1)
728 else printf("ttl set to %u\n", ttl); 749 printf("setsockopt failed\n");
750 else
751 printf("ttl set to %u\n", ttl);
729 } 752 }
730 } 753 }
731 754
@@ -733,85 +756,82 @@ main(int argc, char **argv)
733 * (nothing will break if they do), but some anal plugin maintainer 756 * (nothing will break if they do), but some anal plugin maintainer
734 * will probably add some printf() thing here later, so it might be 757 * will probably add some printf() thing here later, so it might be
735 * best to at least show them where to do it. ;) */ 758 * best to at least show them where to do it. ;) */
736 if(warn.pl > crit.pl) warn.pl = crit.pl; 759 if (warn.pl > crit.pl)
737 if(warn.rta > crit.rta) warn.rta = crit.rta; 760 warn.pl = crit.pl;
738 if(warn_down > crit_down) crit_down = warn_down; 761 if (warn.rta > crit.rta)
739 if(warn.jitter > crit.jitter) crit.jitter = warn.jitter; 762 warn.rta = crit.rta;
740 if(warn.mos < crit.mos) warn.mos = crit.mos; 763 if (warn_down > crit_down)
741 if(warn.score < crit.score) warn.score = crit.score; 764 crit_down = warn_down;
765 if (warn.jitter > crit.jitter)
766 crit.jitter = warn.jitter;
767 if (warn.mos < crit.mos)
768 warn.mos = crit.mos;
769 if (warn.score < crit.score)
770 warn.score = crit.score;
742 771
743#ifdef HAVE_SIGACTION 772#ifdef HAVE_SIGACTION
744 sig_action.sa_sigaction = NULL; 773 sig_action.sa_sigaction = NULL;
745 sig_action.sa_handler = finish; 774 sig_action.sa_handler = finish;
746 sigfillset(&sig_action.sa_mask); 775 sigfillset(&sig_action.sa_mask);
747 sig_action.sa_flags = SA_NODEFER|SA_RESTART; 776 sig_action.sa_flags = SA_NODEFER | SA_RESTART;
748 sigaction(SIGINT, &sig_action, NULL); 777 sigaction(SIGINT, &sig_action, NULL);
749 sigaction(SIGHUP, &sig_action, NULL); 778 sigaction(SIGHUP, &sig_action, NULL);
750 sigaction(SIGTERM, &sig_action, NULL); 779 sigaction(SIGTERM, &sig_action, NULL);
751 sigaction(SIGALRM, &sig_action, NULL); 780 sigaction(SIGALRM, &sig_action, NULL);
752#else /* HAVE_SIGACTION */ 781#else /* HAVE_SIGACTION */
753 signal(SIGINT, finish); 782 signal(SIGINT, finish);
754 signal(SIGHUP, finish); 783 signal(SIGHUP, finish);
755 signal(SIGTERM, finish); 784 signal(SIGTERM, finish);
756 signal(SIGALRM, finish); 785 signal(SIGALRM, finish);
757#endif /* HAVE_SIGACTION */ 786#endif /* HAVE_SIGACTION */
758 if(debug) printf("Setting alarm timeout to %u seconds\n", timeout); 787 if (debug)
788 printf("Setting alarm timeout to %u seconds\n", timeout);
759 alarm(timeout); 789 alarm(timeout);
760 790
761 /* make sure we don't wait any longer than necessary */ 791 /* make sure we don't wait any longer than necessary */
762 gettimeofday(&prog_start, &tz); 792 gettimeofday(&prog_start, &tz);
763 max_completion_time = 793 max_completion_time = ((targets * packets * pkt_interval) + (targets * target_interval)) + (targets * packets * crit.rta) + crit.rta;
764 ((targets * packets * pkt_interval) + (targets * target_interval)) +
765 (targets * packets * crit.rta) + crit.rta;
766 794
767 if(debug) { 795 if (debug) {
768 printf("packets: %u, targets: %u\n" 796 printf("packets: %u, targets: %u\n"
769 "target_interval: %0.3f, pkt_interval %0.3f\n" 797 "target_interval: %0.3f, pkt_interval %0.3f\n"
770 "crit.rta: %0.3f\n" 798 "crit.rta: %0.3f\n"
771 "max_completion_time: %0.3f\n", 799 "max_completion_time: %0.3f\n",
772 packets, targets, 800 packets, targets, (float)target_interval / 1000, (float)pkt_interval / 1000, (float)crit.rta / 1000, (float)max_completion_time / 1000);
773 (float)target_interval / 1000, (float)pkt_interval / 1000,
774 (float)crit.rta / 1000,
775 (float)max_completion_time / 1000);
776 } 801 }
777 802
778 if(debug) { 803 if (debug) {
779 if(max_completion_time > (u_int)timeout * 1000000) { 804 if (max_completion_time > (u_int)timeout * 1000000) {
780 printf("max_completion_time: %llu timeout: %u\n", 805 printf("max_completion_time: %llu timeout: %u\n", max_completion_time, timeout);
781 max_completion_time, timeout); 806 printf("Timeout must be at least %llu\n", max_completion_time / 1000000 + 1);
782 printf("Timeout must be at least %llu\n",
783 max_completion_time / 1000000 + 1);
784 } 807 }
785 } 808 }
786 809
787 if(debug) { 810 if (debug) {
788 printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n", 811 printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n", crit.rta, crit.pl, warn.rta, warn.pl);
789 crit.rta, crit.pl, warn.rta, warn.pl); 812 printf("pkt_interval: %u target_interval: %u retry_interval: %u\n", pkt_interval, target_interval, retry_interval);
790 printf("pkt_interval: %u target_interval: %u retry_interval: %u\n", 813 printf("icmp_pkt_size: %u timeout: %u\n", icmp_pkt_size, timeout);
791 pkt_interval, target_interval, retry_interval);
792 printf("icmp_pkt_size: %u timeout: %u\n",
793 icmp_pkt_size, timeout);
794 } 814 }
795 815
796 if(packets > 20) { 816 if (packets > 20) {
797 errno = 0; 817 errno = 0;
798 crash("packets is > 20 (%d)", packets); 818 crash("packets is > 20 (%d)", packets);
799 } 819 }
800 820
801 if(min_hosts_alive < -1) { 821 if (min_hosts_alive < -1) {
802 errno = 0; 822 errno = 0;
803 crash("minimum alive hosts is negative (%i)", min_hosts_alive); 823 crash("minimum alive hosts is negative (%i)", min_hosts_alive);
804 } 824 }
805 825
806 host = list; 826 host = list;
807 table = malloc(sizeof(struct rta_host *) * targets); 827 table = malloc(sizeof(struct rta_host *) * targets);
808 if(!table) { 828 if (!table) {
809 crash("main(): malloc failed for host table"); 829 crash("main(): malloc failed for host table");
810 } 830 }
811 831
812 i = 0; 832 i = 0;
813 while(host) { 833 while (host) {
814 host->id = i*packets; 834 host->id = i * packets;
815 table[i] = host; 835 table[i] = host;
816 host = host->next; 836 host = host->next;
817 i++; 837 i++;
@@ -822,25 +842,24 @@ main(int argc, char **argv)
822 errno = 0; 842 errno = 0;
823 finish(0); 843 finish(0);
824 844
825 return(0); 845 return (0);
826} 846}
827 847
828static void 848static void run_checks() {
829run_checks()
830{
831 u_int i, t; 849 u_int i, t;
832 u_int final_wait, time_passed; 850 u_int final_wait, time_passed;
833 851
834 /* this loop might actually violate the pkt_interval or target_interval 852 /* this loop might actually violate the pkt_interval or target_interval
835 * settings, but only if there aren't any packets on the wire which 853 * settings, but only if there aren't any packets on the wire which
836 * indicates that the target can handle an increased packet rate */ 854 * indicates that the target can handle an increased packet rate */
837 for(i = 0; i < packets; i++) { 855 for (i = 0; i < packets; i++) {
838 for(t = 0; t < targets; t++) { 856 for (t = 0; t < targets; t++) {
839 /* don't send useless packets */ 857 /* don't send useless packets */
840 if(!targets_alive) finish(0); 858 if (!targets_alive)
841 if(table[t]->flags & FLAG_LOST_CAUSE) { 859 finish(0);
842 if(debug) printf("%s is a lost cause. not sending any more\n", 860 if (table[t]->flags & FLAG_LOST_CAUSE) {
843 table[t]->name); 861 if (debug)
862 printf("%s is a lost cause. not sending any more\n", table[t]->name);
844 continue; 863 continue;
845 } 864 }
846 865
@@ -851,28 +870,27 @@ run_checks()
851 wait_for_reply(icmp_sock, pkt_interval * targets); 870 wait_for_reply(icmp_sock, pkt_interval * targets);
852 } 871 }
853 872
854 if(icmp_pkts_en_route && targets_alive) { 873 if (icmp_pkts_en_route && targets_alive) {
855 time_passed = get_timevaldiff(NULL, NULL); 874 time_passed = get_timevaldiff(NULL, NULL);
856 final_wait = max_completion_time - time_passed; 875 final_wait = max_completion_time - time_passed;
857 876
858 if(debug) { 877 if (debug) {
859 printf("time_passed: %u final_wait: %u max_completion_time: %llu\n", 878 printf("time_passed: %u final_wait: %u max_completion_time: %llu\n", time_passed, final_wait, max_completion_time);
860 time_passed, final_wait, max_completion_time);
861 } 879 }
862 if(time_passed > max_completion_time) { 880 if (time_passed > max_completion_time) {
863 if(debug) printf("Time passed. Finishing up\n"); 881 if (debug)
882 printf("Time passed. Finishing up\n");
864 finish(0); 883 finish(0);
865 } 884 }
866 885
867 /* catch the packets that might come in within the timeframe, but 886 /* catch the packets that might come in within the timeframe, but
868 * haven't yet */ 887 * haven't yet */
869 if(debug) printf("Waiting for %u micro-seconds (%0.3f msecs)\n", 888 if (debug)
870 final_wait, (float)final_wait / 1000); 889 printf("Waiting for %u micro-seconds (%0.3f msecs)\n", final_wait, (float)final_wait / 1000);
871 wait_for_reply(icmp_sock, final_wait); 890 wait_for_reply(icmp_sock, final_wait);
872 } 891 }
873} 892}
874 893
875
876/* response structure: 894/* response structure:
877 * IPv4: 895 * IPv4:
878 * ip header : 20 bytes 896 * ip header : 20 bytes
@@ -883,9 +901,7 @@ run_checks()
883 * both: 901 * both:
884 * icmp echo reply : the rest 902 * icmp echo reply : the rest
885 */ 903 */
886static int 904static int wait_for_reply(int sock, u_int t) {
887wait_for_reply(int sock, u_int t)
888{
889 int n, hlen; 905 int n, hlen;
890 static unsigned char buf[65536]; 906 static unsigned char buf[65536];
891 struct sockaddr_storage resp_addr; 907 struct sockaddr_storage resp_addr;
@@ -898,15 +914,14 @@ wait_for_reply(int sock, u_int t)
898 double jitter_tmp; 914 double jitter_tmp;
899 915
900 if (!(packet.buf = malloc(icmp_pkt_size))) { 916 if (!(packet.buf = malloc(icmp_pkt_size))) {
901 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", 917 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", icmp_pkt_size);
902 icmp_pkt_size); 918 return -1; /* might be reached if we're in debug mode */
903 return -1; /* might be reached if we're in debug mode */
904 } 919 }
905 920
906 memset(packet.buf, 0, icmp_pkt_size); 921 memset(packet.buf, 0, icmp_pkt_size);
907 922
908 /* if we can't listen or don't have anything to listen to, just return */ 923 /* if we can't listen or don't have anything to listen to, just return */
909 if(!t || !icmp_pkts_en_route) { 924 if (!t || !icmp_pkts_en_route) {
910 free(packet.buf); 925 free(packet.buf);
911 return 0; 926 return 0;
912 } 927 }
@@ -915,29 +930,25 @@ wait_for_reply(int sock, u_int t)
915 930
916 i = t; 931 i = t;
917 per_pkt_wait = t / icmp_pkts_en_route; 932 per_pkt_wait = t / icmp_pkts_en_route;
918 while(icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) { 933 while (icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) {
919 t = per_pkt_wait; 934 t = per_pkt_wait;
920 935
921 /* wrap up if all targets are declared dead */ 936 /* wrap up if all targets are declared dead */
922 if(!targets_alive || 937 if (!targets_alive || get_timevaldiff(&prog_start, NULL) >= max_completion_time || (mode == MODE_HOSTCHECK && targets_down)) {
923 get_timevaldiff(&prog_start, NULL) >= max_completion_time ||
924 (mode == MODE_HOSTCHECK && targets_down))
925 {
926 finish(0); 938 finish(0);
927 } 939 }
928 940
929 /* reap responses until we hit a timeout */ 941 /* reap responses until we hit a timeout */
930 n = recvfrom_wto(sock, buf, sizeof(buf), 942 n = recvfrom_wto(sock, buf, sizeof(buf), (struct sockaddr *)&resp_addr, &t, &now);
931 (struct sockaddr *)&resp_addr, &t, &now); 943 if (!n) {
932 if(!n) { 944 if (debug > 1) {
933 if(debug > 1) { 945 printf("recvfrom_wto() timed out during a %u usecs wait\n", per_pkt_wait);
934 printf("recvfrom_wto() timed out during a %u usecs wait\n",
935 per_pkt_wait);
936 } 946 }
937 continue; /* timeout for this one, so keep trying */ 947 continue; /* timeout for this one, so keep trying */
938 } 948 }
939 if(n < 0) { 949 if (n < 0) {
940 if(debug) printf("recvfrom_wto() returned errors\n"); 950 if (debug)
951 printf("recvfrom_wto() returned errors\n");
941 free(packet.buf); 952 free(packet.buf);
942 return n; 953 return n;
943 } 954 }
@@ -946,31 +957,27 @@ wait_for_reply(int sock, u_int t)
946 if (address_family != AF_INET6) { 957 if (address_family != AF_INET6) {
947 ip = (union ip_hdr *)buf; 958 ip = (union ip_hdr *)buf;
948 959
949 if(debug > 1) { 960 if (debug > 1) {
950 char address[INET6_ADDRSTRLEN]; 961 char address[INET6_ADDRSTRLEN];
951 parse_address(&resp_addr, address, sizeof(address)); 962 parse_address(&resp_addr, address, sizeof(address));
952 printf("received %u bytes from %s\n", 963 printf("received %u bytes from %s\n", address_family == AF_INET6 ? ntohs(ip->ip6.ip6_plen) : ntohs(ip->ip.ip_len), address);
953 address_family == AF_INET6 ? ntohs(ip->ip6.ip6_plen)
954 : ntohs(ip->ip.ip_len),
955 address);
956 } 964 }
957 } 965 }
958 966
959/* obsolete. alpha on tru64 provides the necessary defines, but isn't broken */ 967 /* obsolete. alpha on tru64 provides the necessary defines, but isn't broken */
960/* #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) */ 968 /* #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) */
961 /* alpha headers are decidedly broken. Using an ansi compiler, 969 /* alpha headers are decidedly broken. Using an ansi compiler,
962 * they provide ip_vhl instead of ip_hl and ip_v, so we mask 970 * they provide ip_vhl instead of ip_hl and ip_v, so we mask
963 * off the bottom 4 bits */ 971 * off the bottom 4 bits */
964/* hlen = (ip->ip_vhl & 0x0f) << 2; */ 972 /* hlen = (ip->ip_vhl & 0x0f) << 2; */
965/* #else */ 973 /* #else */
966 hlen = (address_family == AF_INET6) ? 0 : ip->ip.ip_hl << 2; 974 hlen = (address_family == AF_INET6) ? 0 : ip->ip.ip_hl << 2;
967/* #endif */ 975 /* #endif */
968 976
969 if(n < (hlen + ICMP_MINLEN)) { 977 if (n < (hlen + ICMP_MINLEN)) {
970 char address[INET6_ADDRSTRLEN]; 978 char address[INET6_ADDRSTRLEN];
971 parse_address(&resp_addr, address, sizeof(address)); 979 parse_address(&resp_addr, address, sizeof(address));
972 crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n", 980 crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n", n, hlen + icmp_pkt_size, address);
973 n, hlen + icmp_pkt_size, address);
974 } 981 }
975 /* else if(debug) { */ 982 /* else if(debug) { */
976 /* printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */ 983 /* printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */
@@ -981,16 +988,14 @@ wait_for_reply(int sock, u_int t)
981 /* check the response */ 988 /* check the response */
982 989
983 memcpy(packet.buf, buf + hlen, icmp_pkt_size); 990 memcpy(packet.buf, buf + hlen, icmp_pkt_size);
984/* address_family == AF_INET6 ? sizeof(struct icmp6_hdr) 991 /* address_family == AF_INET6 ? sizeof(struct icmp6_hdr)
985 : sizeof(struct icmp));*/ 992 : sizeof(struct icmp));*/
986 993
987 if( (address_family == PF_INET && 994 if ((address_family == PF_INET && (ntohs(packet.icp->icmp_id) != pid || packet.icp->icmp_type != ICMP_ECHOREPLY || ntohs(packet.icp->icmp_seq) >= targets * packets)) ||
988 (ntohs(packet.icp->icmp_id) != pid || packet.icp->icmp_type != ICMP_ECHOREPLY 995 (address_family == PF_INET6 &&
989 || ntohs(packet.icp->icmp_seq) >= targets * packets)) 996 (ntohs(packet.icp6->icmp6_id) != pid || packet.icp6->icmp6_type != ICMP6_ECHO_REPLY || ntohs(packet.icp6->icmp6_seq) >= targets * packets))) {
990 || (address_family == PF_INET6 && 997 if (debug > 2)
991 (ntohs(packet.icp6->icmp6_id) != pid || packet.icp6->icmp6_type != ICMP6_ECHO_REPLY 998 printf("not a proper ICMP_ECHOREPLY\n");
992 || ntohs(packet.icp6->icmp6_seq) >= targets * packets))) {
993 if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
994 handle_random_icmp(buf + hlen, &resp_addr); 999 handle_random_icmp(buf + hlen, &resp_addr);
995 continue; 1000 continue;
996 } 1001 }
@@ -999,22 +1004,20 @@ wait_for_reply(int sock, u_int t)
999 if (address_family == PF_INET) { 1004 if (address_family == PF_INET) {
1000 memcpy(&data, packet.icp->icmp_data, sizeof(data)); 1005 memcpy(&data, packet.icp->icmp_data, sizeof(data));
1001 if (debug > 2) 1006 if (debug > 2)
1002 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", 1007 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", (unsigned long)sizeof(data), ntohs(packet.icp->icmp_id), ntohs(packet.icp->icmp_seq),
1003 (unsigned long)sizeof(data), ntohs(packet.icp->icmp_id), 1008 packet.icp->icmp_cksum);
1004 ntohs(packet.icp->icmp_seq), packet.icp->icmp_cksum); 1009 host = table[ntohs(packet.icp->icmp_seq) / packets];
1005 host = table[ntohs(packet.icp->icmp_seq)/packets];
1006 } else { 1010 } else {
1007 memcpy(&data, &packet.icp6->icmp6_dataun.icmp6_un_data8[4], sizeof(data)); 1011 memcpy(&data, &packet.icp6->icmp6_dataun.icmp6_un_data8[4], sizeof(data));
1008 if (debug > 2) 1012 if (debug > 2)
1009 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", 1013 printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n", (unsigned long)sizeof(data), ntohs(packet.icp6->icmp6_id), ntohs(packet.icp6->icmp6_seq),
1010 (unsigned long)sizeof(data), ntohs(packet.icp6->icmp6_id), 1014 packet.icp6->icmp6_cksum);
1011 ntohs(packet.icp6->icmp6_seq), packet.icp6->icmp6_cksum); 1015 host = table[ntohs(packet.icp6->icmp6_seq) / packets];
1012 host = table[ntohs(packet.icp6->icmp6_seq)/packets];
1013 } 1016 }
1014 1017
1015 tdiff = get_timevaldiff(&data.stime, &now); 1018 tdiff = get_timevaldiff(&data.stime, &now);
1016 1019
1017 if (host->last_tdiff>0) { 1020 if (host->last_tdiff > 0) {
1018 /* Calculate jitter */ 1021 /* Calculate jitter */
1019 if (host->last_tdiff > tdiff) { 1022 if (host->last_tdiff > tdiff) {
1020 jitter_tmp = host->last_tdiff - tdiff; 1023 jitter_tmp = host->last_tdiff - tdiff;
@@ -1022,29 +1025,29 @@ wait_for_reply(int sock, u_int t)
1022 jitter_tmp = tdiff - host->last_tdiff; 1025 jitter_tmp = tdiff - host->last_tdiff;
1023 } 1026 }
1024 1027
1025 if (host->jitter==0) { 1028 if (host->jitter == 0) {
1026 host->jitter=jitter_tmp; 1029 host->jitter = jitter_tmp;
1027 host->jitter_max=jitter_tmp; 1030 host->jitter_max = jitter_tmp;
1028 host->jitter_min=jitter_tmp; 1031 host->jitter_min = jitter_tmp;
1029 } else { 1032 } else {
1030 host->jitter+=jitter_tmp; 1033 host->jitter += jitter_tmp;
1031 1034
1032 if (jitter_tmp < host->jitter_min) { 1035 if (jitter_tmp < host->jitter_min) {
1033 host->jitter_min=jitter_tmp; 1036 host->jitter_min = jitter_tmp;
1034 } 1037 }
1035 1038
1036 if (jitter_tmp > host->jitter_max) { 1039 if (jitter_tmp > host->jitter_max) {
1037 host->jitter_max=jitter_tmp; 1040 host->jitter_max = jitter_tmp;
1038 } 1041 }
1039 } 1042 }
1040 1043
1041 /* Check if packets in order */ 1044 /* Check if packets in order */
1042 if (host->last_icmp_seq >= packet.icp->icmp_seq) 1045 if (host->last_icmp_seq >= packet.icp->icmp_seq)
1043 host->order_status=STATE_CRITICAL; 1046 host->order_status = STATE_CRITICAL;
1044 } 1047 }
1045 host->last_tdiff=tdiff; 1048 host->last_tdiff = tdiff;
1046 1049
1047 host->last_icmp_seq=packet.icp->icmp_seq; 1050 host->last_icmp_seq = packet.icp->icmp_seq;
1048 1051
1049 host->time_waited += tdiff; 1052 host->time_waited += tdiff;
1050 host->icmp_recv++; 1053 host->icmp_recv++;
@@ -1054,39 +1057,27 @@ wait_for_reply(int sock, u_int t)
1054 if (tdiff < (unsigned int)host->rtmin) 1057 if (tdiff < (unsigned int)host->rtmin)
1055 host->rtmin = tdiff; 1058 host->rtmin = tdiff;
1056 1059
1057 if(debug) { 1060 if (debug) {
1058 char address[INET6_ADDRSTRLEN]; 1061 char address[INET6_ADDRSTRLEN];
1059 parse_address(&resp_addr, address, sizeof(address)); 1062 parse_address(&resp_addr, address, sizeof(address));
1060 1063
1061 switch(address_family) { 1064 switch (address_family) {
1062 case AF_INET: { 1065 case AF_INET: {
1063 printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: %0.3f, min: %0.3f\n", 1066 printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000, address, ttl, ip->ip.ip_ttl,
1064 (float)tdiff / 1000, 1067 (float)host->rtmax / 1000, (float)host->rtmin / 1000);
1065 address, 1068 break;
1066 ttl, 1069 };
1067 ip->ip.ip_ttl, 1070 case AF_INET6: {
1068 (float)host->rtmax / 1000, 1071 printf("%0.3f ms rtt from %s, outgoing ttl: %u, max: %0.3f, min: %0.3f\n", (float)tdiff / 1000, address, ttl, (float)host->rtmax / 1000, (float)host->rtmin / 1000);
1069 (float)host->rtmin / 1000); 1072 };
1070 break; 1073 }
1071 };
1072 case AF_INET6: {
1073 printf("%0.3f ms rtt from %s, outgoing ttl: %u, max: %0.3f, min: %0.3f\n",
1074 (float)tdiff / 1000,
1075 address,
1076 ttl,
1077 (float)host->rtmax / 1000,
1078 (float)host->rtmin / 1000);
1079 };
1080 }
1081 } 1074 }
1082 1075
1083 /* if we're in hostcheck mode, exit with limited printouts */ 1076 /* if we're in hostcheck mode, exit with limited printouts */
1084 if(mode == MODE_HOSTCHECK) { 1077 if (mode == MODE_HOSTCHECK) {
1085 printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|" 1078 printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|"
1086 "pkt=%u;;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n", 1079 "pkt=%u;;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n",
1087 host->name, icmp_recv, (float)tdiff / 1000, 1080 host->name, icmp_recv, (float)tdiff / 1000, icmp_recv, packets, (float)tdiff / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000);
1088 icmp_recv, packets, (float)tdiff / 1000,
1089 (float)warn.rta / 1000, (float)crit.rta / 1000);
1090 exit(STATE_OK); 1081 exit(STATE_OK);
1091 } 1082 }
1092 } 1083 }
@@ -1096,9 +1087,7 @@ wait_for_reply(int sock, u_int t)
1096} 1087}
1097 1088
1098/* the ping functions */ 1089/* the ping functions */
1099static int 1090static int send_icmp_ping(int sock, struct rta_host *host) {
1100send_icmp_ping(int sock, struct rta_host *host)
1101{
1102 long int len; 1091 long int len;
1103 size_t addrlen; 1092 size_t addrlen;
1104 struct icmp_ping_data data; 1093 struct icmp_ping_data data;
@@ -1107,22 +1096,21 @@ send_icmp_ping(int sock, struct rta_host *host)
1107 struct timeval tv; 1096 struct timeval tv;
1108 void *buf = NULL; 1097 void *buf = NULL;
1109 1098
1110 if(sock == -1) { 1099 if (sock == -1) {
1111 errno = 0; 1100 errno = 0;
1112 crash("Attempt to send on bogus socket"); 1101 crash("Attempt to send on bogus socket");
1113 return -1; 1102 return -1;
1114 } 1103 }
1115 1104
1116 if(!buf) { 1105 if (!buf) {
1117 if (!(buf = malloc(icmp_pkt_size))) { 1106 if (!(buf = malloc(icmp_pkt_size))) {
1118 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", 1107 crash("send_icmp_ping(): failed to malloc %d bytes for send buffer", icmp_pkt_size);
1119 icmp_pkt_size); 1108 return -1; /* might be reached if we're in debug mode */
1120 return -1; /* might be reached if we're in debug mode */
1121 } 1109 }
1122 } 1110 }
1123 memset(buf, 0, icmp_pkt_size); 1111 memset(buf, 0, icmp_pkt_size);
1124 1112
1125 if((gettimeofday(&tv, &tz)) == -1) { 1113 if ((gettimeofday(&tv, &tz)) == -1) {
1126 free(buf); 1114 free(buf);
1127 return -1; 1115 return -1;
1128 } 1116 }
@@ -1131,7 +1119,7 @@ send_icmp_ping(int sock, struct rta_host *host)
1131 memcpy(&data.stime, &tv, sizeof(tv)); 1119 memcpy(&data.stime, &tv, sizeof(tv));
1132 1120
1133 if (address_family == AF_INET) { 1121 if (address_family == AF_INET) {
1134 struct icmp *icp = (struct icmp*)buf; 1122 struct icmp *icp = (struct icmp *)buf;
1135 addrlen = sizeof(struct sockaddr_in); 1123 addrlen = sizeof(struct sockaddr_in);
1136 1124
1137 memcpy(&icp->icmp_data, &data, sizeof(data)); 1125 memcpy(&icp->icmp_data, &data, sizeof(data));
@@ -1141,14 +1129,13 @@ send_icmp_ping(int sock, struct rta_host *host)
1141 icp->icmp_cksum = 0; 1129 icp->icmp_cksum = 0;
1142 icp->icmp_id = htons(pid); 1130 icp->icmp_id = htons(pid);
1143 icp->icmp_seq = htons(host->id++); 1131 icp->icmp_seq = htons(host->id++);
1144 icp->icmp_cksum = icmp_checksum((uint16_t*)buf, (size_t)icmp_pkt_size); 1132 icp->icmp_cksum = icmp_checksum((uint16_t *)buf, (size_t)icmp_pkt_size);
1145 1133
1146 if (debug > 2) 1134 if (debug > 2)
1147 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", 1135 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", (unsigned long)sizeof(data), ntohs(icp->icmp_id), ntohs(icp->icmp_seq),
1148 (unsigned long)sizeof(data), ntohs(icp->icmp_id), ntohs(icp->icmp_seq), icp->icmp_cksum, host->name); 1136 icp->icmp_cksum, host->name);
1149 } 1137 } else {
1150 else { 1138 struct icmp6_hdr *icp6 = (struct icmp6_hdr *)buf;
1151 struct icmp6_hdr *icp6 = (struct icmp6_hdr*)buf;
1152 addrlen = sizeof(struct sockaddr_in6); 1139 addrlen = sizeof(struct sockaddr_in6);
1153 1140
1154 memcpy(&icp6->icmp6_dataun.icmp6_un_data8[4], &data, sizeof(data)); 1141 memcpy(&icp6->icmp6_dataun.icmp6_un_data8[4], &data, sizeof(data));
@@ -1161,9 +1148,8 @@ send_icmp_ping(int sock, struct rta_host *host)
1161 // let checksum be calculated automatically 1148 // let checksum be calculated automatically
1162 1149
1163 if (debug > 2) { 1150 if (debug > 2) {
1164 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", 1151 printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n", (unsigned long)sizeof(data), ntohs(icp6->icmp6_id), ntohs(icp6->icmp6_seq),
1165 (unsigned long)sizeof(data), ntohs(icp6->icmp6_id), 1152 icp6->icmp6_cksum, host->name);
1166 ntohs(icp6->icmp6_seq), icp6->icmp6_cksum, host->name);
1167 } 1153 }
1168 } 1154 }
1169 1155
@@ -1188,8 +1174,8 @@ send_icmp_ping(int sock, struct rta_host *host)
1188 1174
1189 free(buf); 1175 free(buf);
1190 1176
1191 if(len < 0 || (unsigned int)len != icmp_pkt_size) { 1177 if (len < 0 || (unsigned int)len != icmp_pkt_size) {
1192 if(debug) { 1178 if (debug) {
1193 char address[INET6_ADDRSTRLEN]; 1179 char address[INET6_ADDRSTRLEN];
1194 parse_address((struct sockaddr_storage *)&host->saddr_in, address, sizeof(address)); 1180 parse_address((struct sockaddr_storage *)&host->saddr_in, address, sizeof(address));
1195 printf("Failed to send ping to %s: %s\n", address, strerror(errno)); 1181 printf("Failed to send ping to %s: %s\n", address, strerror(errno));
@@ -1204,10 +1190,7 @@ send_icmp_ping(int sock, struct rta_host *host)
1204 return 0; 1190 return 0;
1205} 1191}
1206 1192
1207static int 1193static int recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr, u_int *timo, struct timeval *tv) {
1208recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
1209 u_int *timo, struct timeval* tv)
1210{
1211 u_int slen; 1194 u_int slen;
1212 int n, ret; 1195 int n, ret;
1213 struct timeval to, then, now; 1196 struct timeval to, then, now;
@@ -1218,11 +1201,12 @@ recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
1218 struct msghdr hdr; 1201 struct msghdr hdr;
1219 struct iovec iov; 1202 struct iovec iov;
1220#ifdef SO_TIMESTAMP 1203#ifdef SO_TIMESTAMP
1221 struct cmsghdr* chdr; 1204 struct cmsghdr *chdr;
1222#endif 1205#endif
1223 1206
1224 if(!*timo) { 1207 if (!*timo) {
1225 if(debug) printf("*timo is not\n"); 1208 if (debug)
1209 printf("*timo is not\n");
1226 return 0; 1210 return 0;
1227 } 1211 }
1228 1212
@@ -1235,11 +1219,13 @@ recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
1235 errno = 0; 1219 errno = 0;
1236 gettimeofday(&then, &tz); 1220 gettimeofday(&then, &tz);
1237 n = select(sock + 1, &rd, &wr, NULL, &to); 1221 n = select(sock + 1, &rd, &wr, NULL, &to);
1238 if(n < 0) crash("select() in recvfrom_wto"); 1222 if (n < 0)
1223 crash("select() in recvfrom_wto");
1239 gettimeofday(&now, &tz); 1224 gettimeofday(&now, &tz);
1240 *timo = get_timevaldiff(&then, &now); 1225 *timo = get_timevaldiff(&then, &now);
1241 1226
1242 if(!n) return 0; /* timeout */ 1227 if (!n)
1228 return 0; /* timeout */
1243 1229
1244 slen = sizeof(struct sockaddr_storage); 1230 slen = sizeof(struct sockaddr_storage);
1245 1231
@@ -1259,12 +1245,10 @@ recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
1259 1245
1260 ret = recvmsg(sock, &hdr, 0); 1246 ret = recvmsg(sock, &hdr, 0);
1261#ifdef SO_TIMESTAMP 1247#ifdef SO_TIMESTAMP
1262 for(chdr = CMSG_FIRSTHDR(&hdr); chdr; chdr = CMSG_NXTHDR(&hdr, chdr)) { 1248 for (chdr = CMSG_FIRSTHDR(&hdr); chdr; chdr = CMSG_NXTHDR(&hdr, chdr)) {
1263 if(chdr->cmsg_level == SOL_SOCKET 1249 if (chdr->cmsg_level == SOL_SOCKET && chdr->cmsg_type == SO_TIMESTAMP && chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) {
1264 && chdr->cmsg_type == SO_TIMESTAMP
1265 && chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) {
1266 memcpy(tv, CMSG_DATA(chdr), sizeof(*tv)); 1250 memcpy(tv, CMSG_DATA(chdr), sizeof(*tv));
1267 break ; 1251 break;
1268 } 1252 }
1269 } 1253 }
1270 1254
@@ -1274,54 +1258,55 @@ recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
1274 return (ret); 1258 return (ret);
1275} 1259}
1276 1260
1277static void 1261static void finish(int sig) {
1278finish(int sig)
1279{
1280 u_int i = 0; 1262 u_int i = 0;
1281 unsigned char pl; 1263 unsigned char pl;
1282 double rta; 1264 double rta;
1283 struct rta_host *host; 1265 struct rta_host *host;
1284 const char *status_string[] = 1266 const char *status_string[] = {"OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPENDENT"};
1285 {"OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPENDENT"};
1286 int hosts_ok = 0; 1267 int hosts_ok = 0;
1287 int hosts_warn = 0; 1268 int hosts_warn = 0;
1288 int this_status; 1269 int this_status;
1289 double R; 1270 double R;
1290 1271
1291 alarm(0); 1272 alarm(0);
1292 if(debug > 1) printf("finish(%d) called\n", sig); 1273 if (debug > 1)
1293 1274 printf("finish(%d) called\n", sig);
1294 if(icmp_sock != -1) close(icmp_sock); 1275
1295 if(udp_sock != -1) close(udp_sock); 1276 if (icmp_sock != -1)
1296 if(tcp_sock != -1) close(tcp_sock); 1277 close(icmp_sock);
1297 1278 if (udp_sock != -1)
1298 if(debug) { 1279 close(udp_sock);
1299 printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n", 1280 if (tcp_sock != -1)
1300 icmp_sent, icmp_recv, icmp_lost); 1281 close(tcp_sock);
1282
1283 if (debug) {
1284 printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n", icmp_sent, icmp_recv, icmp_lost);
1301 printf("targets: %u targets_alive: %u\n", targets, targets_alive); 1285 printf("targets: %u targets_alive: %u\n", targets, targets_alive);
1302 } 1286 }
1303 1287
1304 /* iterate thrice to calculate values, give output, and print perfparse */ 1288 /* iterate thrice to calculate values, give output, and print perfparse */
1305 status=STATE_OK; 1289 status = STATE_OK;
1306 host = list; 1290 host = list;
1307 1291
1308 while(host) { 1292 while (host) {
1309 this_status = STATE_OK; 1293 this_status = STATE_OK;
1310 1294
1311 if(!host->icmp_recv) { 1295 if (!host->icmp_recv) {
1312 /* rta 0 is ofcourse not entirely correct, but will still show up 1296 /* rta 0 is ofcourse not entirely correct, but will still show up
1313 * conspicuously as missing entries in perfparse and cacti */ 1297 * conspicuously as missing entries in perfparse and cacti */
1314 pl = 100; 1298 pl = 100;
1315 rta = 0; 1299 rta = 0;
1316 status = STATE_CRITICAL; 1300 status = STATE_CRITICAL;
1317 /* up the down counter if not already counted */ 1301 /* up the down counter if not already counted */
1318 if(!(host->flags & FLAG_LOST_CAUSE) && targets_alive) targets_down++; 1302 if (!(host->flags & FLAG_LOST_CAUSE) && targets_alive)
1303 targets_down++;
1319 } else { 1304 } else {
1320 pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent; 1305 pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent;
1321 rta = (double)host->time_waited / host->icmp_recv; 1306 rta = (double)host->time_waited / host->icmp_recv;
1322 } 1307 }
1323 1308
1324 if (host->icmp_recv>1) { 1309 if (host->icmp_recv > 1) {
1325 /* 1310 /*
1326 * This algorithm is probably pretty much blindly copied from 1311 * This algorithm is probably pretty much blindly copied from
1327 * locations like this one: https://www.slac.stanford.edu/comp/net/wan-mon/tutorial.html#mos 1312 * locations like this one: https://www.slac.stanford.edu/comp/net/wan-mon/tutorial.html#mos
@@ -1336,19 +1321,19 @@ finish(int sig)
1336 * More links: 1321 * More links:
1337 * - https://confluence.slac.stanford.edu/display/IEPM/MOS 1322 * - https://confluence.slac.stanford.edu/display/IEPM/MOS
1338 */ 1323 */
1339 host->jitter=(host->jitter / (host->icmp_recv - 1)/1000); 1324 host->jitter = (host->jitter / (host->icmp_recv - 1) / 1000);
1340 1325
1341 /* 1326 /*
1342 * Take the average round trip latency (in milliseconds), add 1327 * Take the average round trip latency (in milliseconds), add
1343 * round trip jitter, but double the impact to latency 1328 * round trip jitter, but double the impact to latency
1344 * then add 10 for protocol latencies (in milliseconds). 1329 * then add 10 for protocol latencies (in milliseconds).
1345 */ 1330 */
1346 host->EffectiveLatency = (rta/1000) + host->jitter * 2 + 10; 1331 host->EffectiveLatency = (rta / 1000) + host->jitter * 2 + 10;
1347 1332
1348 if (host->EffectiveLatency < 160) { 1333 if (host->EffectiveLatency < 160) {
1349 R = 93.2 - (host->EffectiveLatency / 40); 1334 R = 93.2 - (host->EffectiveLatency / 40);
1350 } else { 1335 } else {
1351 R = 93.2 - ((host->EffectiveLatency - 120) / 10); 1336 R = 93.2 - ((host->EffectiveLatency - 120) / 10);
1352 } 1337 }
1353 1338
1354 // Now, let us deduct 2.5 R values per percentage of packet loss (i.e. a 1339 // Now, let us deduct 2.5 R values per percentage of packet loss (i.e. a
@@ -1360,12 +1345,12 @@ finish(int sig)
1360 } 1345 }
1361 1346
1362 host->score = R; 1347 host->score = R;
1363 host->mos= 1 + ((0.035) * R) + ((.000007) * R * (R-60) * (100-R)); 1348 host->mos = 1 + ((0.035) * R) + ((.000007) * R * (R - 60) * (100 - R));
1364 } else { 1349 } else {
1365 host->jitter=0; 1350 host->jitter = 0;
1366 host->jitter_min=0; 1351 host->jitter_min = 0;
1367 host->jitter_max=0; 1352 host->jitter_max = 0;
1368 host->mos=0; 1353 host->mos = 0;
1369 } 1354 }
1370 1355
1371 host->pl = pl; 1356 host->pl = pl;
@@ -1379,62 +1364,62 @@ finish(int sig)
1379 1364
1380 /* Check which mode is on and do the warn / Crit stuff */ 1365 /* Check which mode is on and do the warn / Crit stuff */
1381 if (rta_mode) { 1366 if (rta_mode) {
1382 if(rta >= crit.rta) { 1367 if (rta >= crit.rta) {
1383 this_status = STATE_CRITICAL; 1368 this_status = STATE_CRITICAL;
1384 status = STATE_CRITICAL; 1369 status = STATE_CRITICAL;
1385 host->rta_status=STATE_CRITICAL; 1370 host->rta_status = STATE_CRITICAL;
1386 } else if(status!=STATE_CRITICAL && (rta >= warn.rta)) { 1371 } else if (status != STATE_CRITICAL && (rta >= warn.rta)) {
1387 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status); 1372 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1388 status = STATE_WARNING; 1373 status = STATE_WARNING;
1389 host->rta_status=STATE_WARNING; 1374 host->rta_status = STATE_WARNING;
1390 } 1375 }
1391 } 1376 }
1392 1377
1393 if (pl_mode) { 1378 if (pl_mode) {
1394 if(pl >= crit.pl) { 1379 if (pl >= crit.pl) {
1395 this_status = STATE_CRITICAL; 1380 this_status = STATE_CRITICAL;
1396 status = STATE_CRITICAL; 1381 status = STATE_CRITICAL;
1397 host->pl_status=STATE_CRITICAL; 1382 host->pl_status = STATE_CRITICAL;
1398 } else if(status!=STATE_CRITICAL && (pl >= warn.pl)) { 1383 } else if (status != STATE_CRITICAL && (pl >= warn.pl)) {
1399 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status); 1384 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1400 status = STATE_WARNING; 1385 status = STATE_WARNING;
1401 host->pl_status=STATE_WARNING; 1386 host->pl_status = STATE_WARNING;
1402 } 1387 }
1403 } 1388 }
1404 1389
1405 if (jitter_mode) { 1390 if (jitter_mode) {
1406 if(host->jitter >= crit.jitter) { 1391 if (host->jitter >= crit.jitter) {
1407 this_status = STATE_CRITICAL; 1392 this_status = STATE_CRITICAL;
1408 status = STATE_CRITICAL; 1393 status = STATE_CRITICAL;
1409 host->jitter_status=STATE_CRITICAL; 1394 host->jitter_status = STATE_CRITICAL;
1410 } else if(status!=STATE_CRITICAL && (host->jitter >= warn.jitter)) { 1395 } else if (status != STATE_CRITICAL && (host->jitter >= warn.jitter)) {
1411 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status); 1396 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1412 status = STATE_WARNING; 1397 status = STATE_WARNING;
1413 host->jitter_status=STATE_WARNING; 1398 host->jitter_status = STATE_WARNING;
1414 } 1399 }
1415 } 1400 }
1416 1401
1417 if (mos_mode) { 1402 if (mos_mode) {
1418 if(host->mos <= crit.mos) { 1403 if (host->mos <= crit.mos) {
1419 this_status = STATE_CRITICAL; 1404 this_status = STATE_CRITICAL;
1420 status = STATE_CRITICAL; 1405 status = STATE_CRITICAL;
1421 host->mos_status=STATE_CRITICAL; 1406 host->mos_status = STATE_CRITICAL;
1422 } else if(status!=STATE_CRITICAL && (host->mos <= warn.mos)) { 1407 } else if (status != STATE_CRITICAL && (host->mos <= warn.mos)) {
1423 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status); 1408 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1424 status = STATE_WARNING; 1409 status = STATE_WARNING;
1425 host->mos_status=STATE_WARNING; 1410 host->mos_status = STATE_WARNING;
1426 } 1411 }
1427 } 1412 }
1428 1413
1429 if (score_mode) { 1414 if (score_mode) {
1430 if(host->score <= crit.score) { 1415 if (host->score <= crit.score) {
1431 this_status = STATE_CRITICAL; 1416 this_status = STATE_CRITICAL;
1432 status = STATE_CRITICAL; 1417 status = STATE_CRITICAL;
1433 host->score_status=STATE_CRITICAL; 1418 host->score_status = STATE_CRITICAL;
1434 } else if(status!=STATE_CRITICAL && (host->score <= warn.score)) { 1419 } else if (status != STATE_CRITICAL && (host->score <= warn.score)) {
1435 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status); 1420 this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
1436 status = STATE_WARNING; 1421 status = STATE_WARNING;
1437 host->score_status=STATE_WARNING; 1422 host->score_status = STATE_WARNING;
1438 } 1423 }
1439 } 1424 }
1440 1425
@@ -1447,91 +1432,92 @@ finish(int sig)
1447 host = host->next; 1432 host = host->next;
1448 } 1433 }
1449 1434
1450
1451 /* this is inevitable */ 1435 /* this is inevitable */
1452 if(!targets_alive) status = STATE_CRITICAL; 1436 if (!targets_alive)
1453 if(min_hosts_alive > -1) { 1437 status = STATE_CRITICAL;
1454 if(hosts_ok >= min_hosts_alive) status = STATE_OK; 1438 if (min_hosts_alive > -1) {
1455 else if((hosts_ok + hosts_warn) >= min_hosts_alive) status = STATE_WARNING; 1439 if (hosts_ok >= min_hosts_alive)
1440 status = STATE_OK;
1441 else if ((hosts_ok + hosts_warn) >= min_hosts_alive)
1442 status = STATE_WARNING;
1456 } 1443 }
1457 printf("%s - ", status_string[status]); 1444 printf("%s - ", status_string[status]);
1458 1445
1459 host = list; 1446 host = list;
1460 while(host) { 1447 while (host) {
1461 1448
1462 if(debug) puts(""); 1449 if (debug)
1463 if(i) { 1450 puts("");
1464 if(i < targets) printf(" :: "); 1451 if (i) {
1465 else printf("\n"); 1452 if (i < targets)
1453 printf(" :: ");
1454 else
1455 printf("\n");
1466 } 1456 }
1467 i++; 1457 i++;
1468 if(!host->icmp_recv) { 1458 if (!host->icmp_recv) {
1469 status = STATE_CRITICAL; 1459 status = STATE_CRITICAL;
1470 host->rtmin=0; 1460 host->rtmin = 0;
1471 host->jitter_min=0; 1461 host->jitter_min = 0;
1472 if(host->flags & FLAG_LOST_CAUSE) { 1462 if (host->flags & FLAG_LOST_CAUSE) {
1473 char address[INET6_ADDRSTRLEN]; 1463 char address[INET6_ADDRSTRLEN];
1474 parse_address(&host->error_addr, address, sizeof(address)); 1464 parse_address(&host->error_addr, address, sizeof(address));
1475 printf("%s: %s @ %s. rta nan, lost %d%%", 1465 printf("%s: %s @ %s. rta nan, lost %d%%", host->name, get_icmp_error_msg(host->icmp_type, host->icmp_code), address, 100);
1476 host->name,
1477 get_icmp_error_msg(host->icmp_type, host->icmp_code),
1478 address,
1479 100);
1480 } else { /* not marked as lost cause, so we have no flags for it */ 1466 } else { /* not marked as lost cause, so we have no flags for it */
1481 printf("%s: rta nan, lost 100%%", host->name); 1467 printf("%s: rta nan, lost 100%%", host->name);
1482 } 1468 }
1483 } else { /* !icmp_recv */ 1469 } else { /* !icmp_recv */
1484 printf("%s", host->name); 1470 printf("%s", host->name);
1485 /* rta text output */ 1471 /* rta text output */
1486 if (rta_mode) { 1472 if (rta_mode) {
1487 if (status == STATE_OK) 1473 if (status == STATE_OK)
1488 printf(" rta %0.3fms", host->rta / 1000); 1474 printf(" rta %0.3fms", host->rta / 1000);
1489 else if (status==STATE_WARNING && host->rta_status==status) 1475 else if (status == STATE_WARNING && host->rta_status == status)
1490 printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)warn.rta/1000); 1476 printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)warn.rta / 1000);
1491 else if (status==STATE_CRITICAL && host->rta_status==status) 1477 else if (status == STATE_CRITICAL && host->rta_status == status)
1492 printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)crit.rta/1000); 1478 printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)crit.rta / 1000);
1493 } 1479 }
1494 /* pl text output */ 1480 /* pl text output */
1495 if (pl_mode) { 1481 if (pl_mode) {
1496 if (status == STATE_OK) 1482 if (status == STATE_OK)
1497 printf(" lost %u%%", host->pl); 1483 printf(" lost %u%%", host->pl);
1498 else if (status==STATE_WARNING && host->pl_status==status) 1484 else if (status == STATE_WARNING && host->pl_status == status)
1499 printf(" lost %u%% > %u%%", host->pl, warn.pl); 1485 printf(" lost %u%% > %u%%", host->pl, warn.pl);
1500 else if (status==STATE_CRITICAL && host->pl_status==status) 1486 else if (status == STATE_CRITICAL && host->pl_status == status)
1501 printf(" lost %u%% > %u%%", host->pl, crit.pl); 1487 printf(" lost %u%% > %u%%", host->pl, crit.pl);
1502 } 1488 }
1503 /* jitter text output */ 1489 /* jitter text output */
1504 if (jitter_mode) { 1490 if (jitter_mode) {
1505 if (status == STATE_OK) 1491 if (status == STATE_OK)
1506 printf(" jitter %0.3fms", (float)host->jitter); 1492 printf(" jitter %0.3fms", (float)host->jitter);
1507 else if (status==STATE_WARNING && host->jitter_status==status) 1493 else if (status == STATE_WARNING && host->jitter_status == status)
1508 printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, warn.jitter); 1494 printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, warn.jitter);
1509 else if (status==STATE_CRITICAL && host->jitter_status==status) 1495 else if (status == STATE_CRITICAL && host->jitter_status == status)
1510 printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, crit.jitter); 1496 printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, crit.jitter);
1511 } 1497 }
1512 /* mos text output */ 1498 /* mos text output */
1513 if (mos_mode) { 1499 if (mos_mode) {
1514 if (status == STATE_OK) 1500 if (status == STATE_OK)
1515 printf(" MOS %0.1f", (float)host->mos); 1501 printf(" MOS %0.1f", (float)host->mos);
1516 else if (status==STATE_WARNING && host->mos_status==status) 1502 else if (status == STATE_WARNING && host->mos_status == status)
1517 printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)warn.mos); 1503 printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)warn.mos);
1518 else if (status==STATE_CRITICAL && host->mos_status==status) 1504 else if (status == STATE_CRITICAL && host->mos_status == status)
1519 printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)crit.mos); 1505 printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)crit.mos);
1520 } 1506 }
1521 /* score text output */ 1507 /* score text output */
1522 if (score_mode) { 1508 if (score_mode) {
1523 if (status == STATE_OK) 1509 if (status == STATE_OK)
1524 printf(" Score %u", (int)host->score); 1510 printf(" Score %u", (int)host->score);
1525 else if (status==STATE_WARNING && host->score_status==status ) 1511 else if (status == STATE_WARNING && host->score_status == status)
1526 printf(" Score %u < %u", (int)host->score, (int)warn.score); 1512 printf(" Score %u < %u", (int)host->score, (int)warn.score);
1527 else if (status==STATE_CRITICAL && host->score_status==status ) 1513 else if (status == STATE_CRITICAL && host->score_status == status)
1528 printf(" Score %u < %u", (int)host->score, (int)crit.score); 1514 printf(" Score %u < %u", (int)host->score, (int)crit.score);
1529 } 1515 }
1530 /* order statis text output */ 1516 /* order statis text output */
1531 if (order_mode) { 1517 if (order_mode) {
1532 if (status == STATE_OK) 1518 if (status == STATE_OK)
1533 printf(" Packets in order"); 1519 printf(" Packets in order");
1534 else if (status==STATE_CRITICAL && host->order_status==status) 1520 else if (status == STATE_CRITICAL && host->order_status == status)
1535 printf(" Packets out of order"); 1521 printf(" Packets out of order");
1536 } 1522 }
1537 } 1523 }
@@ -1540,25 +1526,21 @@ finish(int sig)
1540 1526
1541 /* iterate once more for pretty perfparse output */ 1527 /* iterate once more for pretty perfparse output */
1542 if (!(!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode && order_mode)) { 1528 if (!(!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode && order_mode)) {
1543 printf("|"); 1529 printf("|");
1544 } 1530 }
1545 i = 0; 1531 i = 0;
1546 host = list; 1532 host = list;
1547 while(host) { 1533 while (host) {
1548 if(debug) puts(""); 1534 if (debug)
1535 puts("");
1549 1536
1550 if (rta_mode) { 1537 if (rta_mode) {
1551 if (host->pl<100) { 1538 if (host->pl < 100) {
1552 printf("%srta=%0.3fms;%0.3f;%0.3f;0; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ", 1539 printf("%srta=%0.3fms;%0.3f;%0.3f;0; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ", (targets > 1) ? host->name : "", host->rta / 1000, (float)warn.rta / 1000,
1553 (targets > 1) ? host->name : "", 1540 (float)crit.rta / 1000, (targets > 1) ? host->name : "", (float)host->rtmax / 1000, (targets > 1) ? host->name : "",
1554 host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000, 1541 (host->rtmin < INFINITY) ? (float)host->rtmin / 1000 : (float)0);
1555 (targets > 1) ? host->name : "", (float)host->rtmax / 1000,
1556 (targets > 1) ? host->name : "", (host->rtmin < INFINITY) ? (float)host->rtmin / 1000 : (float)0);
1557 } else { 1542 } else {
1558 printf("%srta=U;;;; %srtmax=U;;;; %srtmin=U;;;; ", 1543 printf("%srta=U;;;; %srtmax=U;;;; %srtmin=U;;;; ", (targets > 1) ? host->name : "", (targets > 1) ? host->name : "", (targets > 1) ? host->name : "");
1559 (targets > 1) ? host->name : "",
1560 (targets > 1) ? host->name : "",
1561 (targets > 1) ? host->name : "");
1562 } 1544 }
1563 } 1545 }
1564 1546
@@ -1567,43 +1549,27 @@ finish(int sig)
1567 } 1549 }
1568 1550
1569 if (jitter_mode) { 1551 if (jitter_mode) {
1570 if (host->pl<100) { 1552 if (host->pl < 100) {
1571 printf("%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %sjitter_max=%0.3fms;;;; %sjitter_min=%0.3fms;;;; ", 1553 printf("%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %sjitter_max=%0.3fms;;;; %sjitter_min=%0.3fms;;;; ", (targets > 1) ? host->name : "", (float)host->jitter,
1572 (targets > 1) ? host->name : "", 1554 (float)warn.jitter, (float)crit.jitter, (targets > 1) ? host->name : "", (float)host->jitter_max / 1000, (targets > 1) ? host->name : "",
1573 (float)host->jitter, 1555 (float)host->jitter_min / 1000);
1574 (float)warn.jitter,
1575 (float)crit.jitter,
1576 (targets > 1) ? host->name : "",
1577 (float)host->jitter_max / 1000, (targets > 1) ? host->name : "",
1578 (float)host->jitter_min / 1000
1579 );
1580 } else { 1556 } else {
1581 printf("%sjitter_avg=U;;;; %sjitter_max=U;;;; %sjitter_min=U;;;; ", 1557 printf("%sjitter_avg=U;;;; %sjitter_max=U;;;; %sjitter_min=U;;;; ", (targets > 1) ? host->name : "", (targets > 1) ? host->name : "",
1582 (targets > 1) ? host->name : "", 1558 (targets > 1) ? host->name : "");
1583 (targets > 1) ? host->name : "",
1584 (targets > 1) ? host->name : "");
1585 } 1559 }
1586 } 1560 }
1587 1561
1588 if (mos_mode) { 1562 if (mos_mode) {
1589 if (host->pl<100) { 1563 if (host->pl < 100) {
1590 printf("%smos=%0.1f;%0.1f;%0.1f;0;5 ", 1564 printf("%smos=%0.1f;%0.1f;%0.1f;0;5 ", (targets > 1) ? host->name : "", (float)host->mos, (float)warn.mos, (float)crit.mos);
1591 (targets > 1) ? host->name : "",
1592 (float)host->mos,
1593 (float)warn.mos,
1594 (float)crit.mos);
1595 } else { 1565 } else {
1596 printf("%smos=U;;;; ", (targets > 1) ? host->name : ""); 1566 printf("%smos=U;;;; ", (targets > 1) ? host->name : "");
1597 } 1567 }
1598 } 1568 }
1599 1569
1600 if (score_mode) { 1570 if (score_mode) {
1601 if (host->pl<100) { 1571 if (host->pl < 100) {
1602 printf("%sscore=%u;%u;%u;0;100 ", 1572 printf("%sscore=%u;%u;%u;0;100 ", (targets > 1) ? host->name : "", (int)host->score, (int)warn.score, (int)crit.score);
1603 (targets > 1) ? host->name : "",
1604 (int)host->score,
1605 (int)warn.score,
1606 (int)crit.score);
1607 } else { 1573 } else {
1608 printf("%sscore=U;;;; ", (targets > 1) ? host->name : ""); 1574 printf("%sscore=U;;;; ", (targets > 1) ? host->name : "");
1609 } 1575 }
@@ -1612,35 +1578,34 @@ finish(int sig)
1612 host = host->next; 1578 host = host->next;
1613 } 1579 }
1614 1580
1615 if(min_hosts_alive > -1) { 1581 if (min_hosts_alive > -1) {
1616 if(hosts_ok >= min_hosts_alive) status = STATE_OK; 1582 if (hosts_ok >= min_hosts_alive)
1617 else if((hosts_ok + hosts_warn) >= min_hosts_alive) status = STATE_WARNING; 1583 status = STATE_OK;
1584 else if ((hosts_ok + hosts_warn) >= min_hosts_alive)
1585 status = STATE_WARNING;
1618 } 1586 }
1619 1587
1620 /* finish with an empty line */ 1588 /* finish with an empty line */
1621 puts(""); 1589 puts("");
1622 if(debug) printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n", 1590 if (debug)
1623 targets, targets_alive, hosts_ok, hosts_warn, min_hosts_alive); 1591 printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n", targets, targets_alive, hosts_ok, hosts_warn, min_hosts_alive);
1624 1592
1625 exit(status); 1593 exit(status);
1626} 1594}
1627 1595
1628static u_int 1596static u_int get_timevaldiff(struct timeval *early, struct timeval *later) {
1629get_timevaldiff(struct timeval *early, struct timeval *later)
1630{
1631 u_int ret; 1597 u_int ret;
1632 struct timeval now; 1598 struct timeval now;
1633 1599
1634 if(!later) { 1600 if (!later) {
1635 gettimeofday(&now, &tz); 1601 gettimeofday(&now, &tz);
1636 later = &now; 1602 later = &now;
1637 } 1603 }
1638 if(!early) early = &prog_start; 1604 if (!early)
1605 early = &prog_start;
1639 1606
1640 /* if early > later we return 0 so as to indicate a timeout */ 1607 /* if early > later we return 0 so as to indicate a timeout */
1641 if(early->tv_sec > later->tv_sec || 1608 if (early->tv_sec > later->tv_sec || (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec)) {
1642 (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
1643 {
1644 return 0; 1609 return 0;
1645 } 1610 }
1646 ret = (later->tv_sec - early->tv_sec) * 1000000; 1611 ret = (later->tv_sec - early->tv_sec) * 1000000;
@@ -1649,9 +1614,7 @@ get_timevaldiff(struct timeval *early, struct timeval *later)
1649 return ret; 1614 return ret;
1650} 1615}
1651 1616
1652static int 1617static int add_target_ip(char *arg, struct sockaddr_storage *in) {
1653add_target_ip(char *arg, struct sockaddr_storage *in)
1654{
1655 struct rta_host *host; 1618 struct rta_host *host;
1656 struct sockaddr_in *sin, *host_sin; 1619 struct sockaddr_in *sin, *host_sin;
1657 struct sockaddr_in6 *sin6, *host_sin6; 1620 struct sockaddr_in6 *sin6, *host_sin6;
@@ -1661,51 +1624,46 @@ add_target_ip(char *arg, struct sockaddr_storage *in)
1661 else 1624 else
1662 sin6 = (struct sockaddr_in6 *)in; 1625 sin6 = (struct sockaddr_in6 *)in;
1663 1626
1664
1665
1666 /* disregard obviously stupid addresses 1627 /* disregard obviously stupid addresses
1667 * (I didn't find an ipv6 equivalent to INADDR_NONE) */ 1628 * (I didn't find an ipv6 equivalent to INADDR_NONE) */
1668 if (((address_family == AF_INET && (sin->sin_addr.s_addr == INADDR_NONE 1629 if (((address_family == AF_INET && (sin->sin_addr.s_addr == INADDR_NONE || sin->sin_addr.s_addr == INADDR_ANY))) ||
1669 || sin->sin_addr.s_addr == INADDR_ANY))) 1630 (address_family == AF_INET6 && (sin6->sin6_addr.s6_addr == in6addr_any.s6_addr))) {
1670 || (address_family == AF_INET6 && (sin6->sin6_addr.s6_addr == in6addr_any.s6_addr))) {
1671 return -1; 1631 return -1;
1672 } 1632 }
1673 1633
1674 /* no point in adding two identical IP's, so don't. ;) */ 1634 /* no point in adding two identical IP's, so don't. ;) */
1675 host = list; 1635 host = list;
1676 while(host) { 1636 while (host) {
1677 host_sin = (struct sockaddr_in *)&host->saddr_in; 1637 host_sin = (struct sockaddr_in *)&host->saddr_in;
1678 host_sin6 = (struct sockaddr_in6 *)&host->saddr_in; 1638 host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;
1679 1639
1680 if( (address_family == AF_INET && host_sin->sin_addr.s_addr == sin->sin_addr.s_addr) 1640 if ((address_family == AF_INET && host_sin->sin_addr.s_addr == sin->sin_addr.s_addr) ||
1681 || (address_family == AF_INET6 && host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) { 1641 (address_family == AF_INET6 && host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) {
1682 if(debug) printf("Identical IP already exists. Not adding %s\n", arg); 1642 if (debug)
1643 printf("Identical IP already exists. Not adding %s\n", arg);
1683 return -1; 1644 return -1;
1684 } 1645 }
1685 host = host->next; 1646 host = host->next;
1686 } 1647 }
1687 1648
1688 /* add the fresh ip */ 1649 /* add the fresh ip */
1689 host = (struct rta_host*)malloc(sizeof(struct rta_host)); 1650 host = (struct rta_host *)malloc(sizeof(struct rta_host));
1690 if(!host) { 1651 if (!host) {
1691 char straddr[INET6_ADDRSTRLEN]; 1652 char straddr[INET6_ADDRSTRLEN];
1692 parse_address((struct sockaddr_storage*)&in, straddr, sizeof(straddr)); 1653 parse_address((struct sockaddr_storage *)&in, straddr, sizeof(straddr));
1693 crash("add_target_ip(%s, %s): malloc(%lu) failed", 1654 crash("add_target_ip(%s, %s): malloc(%lu) failed", arg, straddr, sizeof(struct rta_host));
1694 arg, straddr, sizeof(struct rta_host));
1695 } 1655 }
1696 memset(host, 0, sizeof(struct rta_host)); 1656 memset(host, 0, sizeof(struct rta_host));
1697 1657
1698 /* set the values. use calling name for output */ 1658 /* set the values. use calling name for output */
1699 host->name = strdup(arg); 1659 host->name = strdup(arg);
1700 1660
1701
1702 /* fill out the sockaddr_storage struct */ 1661 /* fill out the sockaddr_storage struct */
1703 if(address_family == AF_INET) { 1662 if (address_family == AF_INET) {
1704 host_sin = (struct sockaddr_in *)&host->saddr_in; 1663 host_sin = (struct sockaddr_in *)&host->saddr_in;
1705 host_sin->sin_family = AF_INET; 1664 host_sin->sin_family = AF_INET;
1706 host_sin->sin_addr.s_addr = sin->sin_addr.s_addr; 1665 host_sin->sin_addr.s_addr = sin->sin_addr.s_addr;
1707 } 1666 } else {
1708 else {
1709 host_sin6 = (struct sockaddr_in6 *)&host->saddr_in; 1667 host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;
1710 host_sin6->sin6_family = AF_INET6; 1668 host_sin6->sin6_family = AF_INET6;
1711 memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr, sizeof host_sin6->sin6_addr.s6_addr); 1669 memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr, sizeof host_sin6->sin6_addr.s6_addr);
@@ -1714,22 +1672,23 @@ add_target_ip(char *arg, struct sockaddr_storage *in)
1714 /* fill out the sockaddr_in struct */ 1672 /* fill out the sockaddr_in struct */
1715 host->rtmin = INFINITY; 1673 host->rtmin = INFINITY;
1716 host->rtmax = 0; 1674 host->rtmax = 0;
1717 host->jitter=0; 1675 host->jitter = 0;
1718 host->jitter_max=0; 1676 host->jitter_max = 0;
1719 host->jitter_min=INFINITY; 1677 host->jitter_min = INFINITY;
1720 host->last_tdiff=0; 1678 host->last_tdiff = 0;
1721 host->order_status=STATE_OK; 1679 host->order_status = STATE_OK;
1722 host->last_icmp_seq=0; 1680 host->last_icmp_seq = 0;
1723 host->rta_status=0; 1681 host->rta_status = 0;
1724 host->pl_status=0; 1682 host->pl_status = 0;
1725 host->jitter_status=0; 1683 host->jitter_status = 0;
1726 host->mos_status=0; 1684 host->mos_status = 0;
1727 host->score_status=0; 1685 host->score_status = 0;
1728 host->pl_status=0; 1686 host->pl_status = 0;
1729 1687
1730 1688 if (!list)
1731 if(!list) list = cursor = host; 1689 list = cursor = host;
1732 else cursor->next = host; 1690 else
1691 cursor->next = host;
1733 1692
1734 cursor = host; 1693 cursor = host;
1735 targets++; 1694 targets++;
@@ -1738,9 +1697,7 @@ add_target_ip(char *arg, struct sockaddr_storage *in)
1738} 1697}
1739 1698
1740/* wrapper for add_target_ip */ 1699/* wrapper for add_target_ip */
1741static int 1700static int add_target(char *arg) {
1742add_target(char *arg)
1743{
1744 int error, result = -1; 1701 int error, result = -1;
1745 struct sockaddr_storage ip; 1702 struct sockaddr_storage ip;
1746 struct addrinfo hints, *res, *p; 1703 struct addrinfo hints, *res, *p;
@@ -1754,14 +1711,14 @@ add_target(char *arg)
1754 sin = (struct sockaddr_in *)&ip; 1711 sin = (struct sockaddr_in *)&ip;
1755 result = inet_pton(address_family, arg, &sin->sin_addr); 1712 result = inet_pton(address_family, arg, &sin->sin_addr);
1756#ifdef USE_IPV6 1713#ifdef USE_IPV6
1757 if( result != 1 ){ 1714 if (result != 1) {
1758 address_family = AF_INET6; 1715 address_family = AF_INET6;
1759 sin6 = (struct sockaddr_in6 *)&ip; 1716 sin6 = (struct sockaddr_in6 *)&ip;
1760 result = inet_pton(address_family, arg, &sin6->sin6_addr); 1717 result = inet_pton(address_family, arg, &sin6->sin6_addr);
1761 } 1718 }
1762#endif 1719#endif
1763 /* If we don't find any valid addresses, we still don't know the address_family */ 1720 /* If we don't find any valid addresses, we still don't know the address_family */
1764 if ( result != 1) { 1721 if (result != 1) {
1765 address_family = -1; 1722 address_family = -1;
1766 } 1723 }
1767 break; 1724 break;
@@ -1773,15 +1730,15 @@ add_target(char *arg)
1773 sin6 = (struct sockaddr_in6 *)&ip; 1730 sin6 = (struct sockaddr_in6 *)&ip;
1774 result = inet_pton(address_family, arg, &sin6->sin6_addr); 1731 result = inet_pton(address_family, arg, &sin6->sin6_addr);
1775 break; 1732 break;
1776 default: crash("Address family not supported"); 1733 default:
1734 crash("Address family not supported");
1777 } 1735 }
1778 1736
1779 /* don't resolve if we don't have to */ 1737 /* don't resolve if we don't have to */
1780 if(result == 1) { 1738 if (result == 1) {
1781 /* don't add all ip's if we were given a specific one */ 1739 /* don't add all ip's if we were given a specific one */
1782 return add_target_ip(arg, &ip); 1740 return add_target_ip(arg, &ip);
1783 } 1741 } else {
1784 else {
1785 errno = 0; 1742 errno = 0;
1786 memset(&hints, 0, sizeof(hints)); 1743 memset(&hints, 0, sizeof(hints));
1787 if (address_family == -1) { 1744 if (address_family == -1) {
@@ -1790,7 +1747,7 @@ add_target(char *arg)
1790 hints.ai_family = address_family == AF_INET ? PF_INET : PF_INET6; 1747 hints.ai_family = address_family == AF_INET ? PF_INET : PF_INET6;
1791 } 1748 }
1792 hints.ai_socktype = SOCK_RAW; 1749 hints.ai_socktype = SOCK_RAW;
1793 if((error = getaddrinfo(arg, NULL, &hints, &res)) != 0) { 1750 if ((error = getaddrinfo(arg, NULL, &hints, &res)) != 0) {
1794 errno = 0; 1751 errno = 0;
1795 crash("Failed to resolve %s: %s", arg, gai_strerror(error)); 1752 crash("Failed to resolve %s: %s", arg, gai_strerror(error));
1796 return -1; 1753 return -1;
@@ -1799,13 +1756,14 @@ add_target(char *arg)
1799 } 1756 }
1800 1757
1801 /* possibly add all the IP's as targets */ 1758 /* possibly add all the IP's as targets */
1802 for(p = res; p != NULL; p = p->ai_next) { 1759 for (p = res; p != NULL; p = p->ai_next) {
1803 memcpy(&ip, p->ai_addr, p->ai_addrlen); 1760 memcpy(&ip, p->ai_addr, p->ai_addrlen);
1804 add_target_ip(arg, &ip); 1761 add_target_ip(arg, &ip);
1805 1762
1806 /* this is silly, but it works */ 1763 /* this is silly, but it works */
1807 if(mode == MODE_HOSTCHECK || mode == MODE_ALL) { 1764 if (mode == MODE_HOSTCHECK || mode == MODE_ALL) {
1808 if(debug > 2) printf("mode: %d\n", mode); 1765 if (debug > 2)
1766 printf("mode: %d\n", mode);
1809 continue; 1767 continue;
1810 } 1768 }
1811 break; 1769 break;
@@ -1815,24 +1773,20 @@ add_target(char *arg)
1815 return 0; 1773 return 0;
1816} 1774}
1817 1775
1818static void 1776static void set_source_ip(char *arg) {
1819set_source_ip(char *arg)
1820{
1821 struct sockaddr_in src; 1777 struct sockaddr_in src;
1822 1778
1823 memset(&src, 0, sizeof(src)); 1779 memset(&src, 0, sizeof(src));
1824 src.sin_family = address_family; 1780 src.sin_family = address_family;
1825 if((src.sin_addr.s_addr = inet_addr(arg)) == INADDR_NONE) 1781 if ((src.sin_addr.s_addr = inet_addr(arg)) == INADDR_NONE)
1826 src.sin_addr.s_addr = get_ip_address(arg); 1782 src.sin_addr.s_addr = get_ip_address(arg);
1827 if(bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1) 1783 if (bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1)
1828 crash("Cannot bind to IP address %s", arg); 1784 crash("Cannot bind to IP address %s", arg);
1829} 1785}
1830 1786
1831/* TODO: Move this to netutils.c and also change check_dhcp to use that. */ 1787/* TODO: Move this to netutils.c and also change check_dhcp to use that. */
1832static in_addr_t 1788static in_addr_t get_ip_address(const char *ifname) {
1833get_ip_address(const char *ifname) 1789 // TODO: Rewrite this so the function return an error and we exit somewhere else
1834{
1835 // TODO: Rewrite this so the function return an error and we exit somewhere else
1836 struct sockaddr_in ip; 1790 struct sockaddr_in ip;
1837 ip.sin_addr.s_addr = 0; // Fake initialization to make compiler happy 1791 ip.sin_addr.s_addr = 0; // Fake initialization to make compiler happy
1838#if defined(SIOCGIFADDR) 1792#if defined(SIOCGIFADDR)
@@ -1842,12 +1796,12 @@ get_ip_address(const char *ifname)
1842 1796
1843 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; 1797 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
1844 1798
1845 if(ioctl(icmp_sock, SIOCGIFADDR, &ifr) == -1) 1799 if (ioctl(icmp_sock, SIOCGIFADDR, &ifr) == -1)
1846 crash("Cannot determine IP address of interface %s", ifname); 1800 crash("Cannot determine IP address of interface %s", ifname);
1847 1801
1848 memcpy(&ip, &ifr.ifr_addr, sizeof(ip)); 1802 memcpy(&ip, &ifr.ifr_addr, sizeof(ip));
1849#else 1803#else
1850 (void) ifname; 1804 (void)ifname;
1851 errno = 0; 1805 errno = 0;
1852 crash("Cannot get interface IP address on this platform."); 1806 crash("Cannot get interface IP address on this platform.");
1853#endif 1807#endif
@@ -1860,63 +1814,73 @@ get_ip_address(const char *ifname)
1860 * s = seconds 1814 * s = seconds
1861 * return value is in microseconds 1815 * return value is in microseconds
1862 */ 1816 */
1863static u_int 1817static u_int get_timevar(const char *str) {
1864get_timevar(const char *str)
1865{
1866 char p, u, *ptr; 1818 char p, u, *ptr;
1867 size_t len; 1819 size_t len;
1868 u_int i, d; /* integer and decimal, respectively */ 1820 u_int i, d; /* integer and decimal, respectively */
1869 u_int factor = 1000; /* default to milliseconds */ 1821 u_int factor = 1000; /* default to milliseconds */
1870 1822
1871 if(!str) return 0; 1823 if (!str)
1824 return 0;
1872 len = strlen(str); 1825 len = strlen(str);
1873 if(!len) return 0; 1826 if (!len)
1827 return 0;
1874 1828
1875 /* unit might be given as ms|m (millisec), 1829 /* unit might be given as ms|m (millisec),
1876 * us|u (microsec) or just plain s, for seconds */ 1830 * us|u (microsec) or just plain s, for seconds */
1877 p = '\0'; 1831 p = '\0';
1878 u = str[len - 1]; 1832 u = str[len - 1];
1879 if(len >= 2 && !isdigit((int)str[len - 2])) p = str[len - 2]; 1833 if (len >= 2 && !isdigit((int)str[len - 2]))
1880 if(p && u == 's') u = p; 1834 p = str[len - 2];
1881 else if(!p) p = u; 1835 if (p && u == 's')
1882 if(debug > 2) printf("evaluating %s, u: %c, p: %c\n", str, u, p); 1836 u = p;
1883 1837 else if (!p)
1884 if(u == 'u') factor = 1; /* microseconds */ 1838 p = u;
1885 else if(u == 'm') factor = 1000; /* milliseconds */ 1839 if (debug > 2)
1886 else if(u == 's') factor = 1000000; /* seconds */ 1840 printf("evaluating %s, u: %c, p: %c\n", str, u, p);
1887 if(debug > 2) printf("factor is %u\n", factor); 1841
1842 if (u == 'u')
1843 factor = 1; /* microseconds */
1844 else if (u == 'm')
1845 factor = 1000; /* milliseconds */
1846 else if (u == 's')
1847 factor = 1000000; /* seconds */
1848 if (debug > 2)
1849 printf("factor is %u\n", factor);
1888 1850
1889 i = strtoul(str, &ptr, 0); 1851 i = strtoul(str, &ptr, 0);
1890 if(!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1) 1852 if (!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1)
1891 return i * factor; 1853 return i * factor;
1892 1854
1893 /* time specified in usecs can't have decimal points, so ignore them */ 1855 /* time specified in usecs can't have decimal points, so ignore them */
1894 if(factor == 1) return i; 1856 if (factor == 1)
1857 return i;
1895 1858
1896 d = strtoul(ptr + 1, NULL, 0); 1859 d = strtoul(ptr + 1, NULL, 0);
1897 1860
1898 /* d is decimal, so get rid of excess digits */ 1861 /* d is decimal, so get rid of excess digits */
1899 while(d >= factor) d /= 10; 1862 while (d >= factor)
1863 d /= 10;
1900 1864
1901 /* the last parenthesis avoids floating point exceptions. */ 1865 /* the last parenthesis avoids floating point exceptions. */
1902 return ((i * factor) + (d * (factor / 10))); 1866 return ((i * factor) + (d * (factor / 10)));
1903} 1867}
1904 1868
1905/* not too good at checking errors, but it'll do (main() should barfe on -1) */ 1869/* not too good at checking errors, but it'll do (main() should barfe on -1) */
1906static int 1870static int get_threshold(char *str, threshold *th) {
1907get_threshold(char *str, threshold *th)
1908{
1909 char *p = NULL, i = 0; 1871 char *p = NULL, i = 0;
1910 1872
1911 if(!str || !strlen(str) || !th) return -1; 1873 if (!str || !strlen(str) || !th)
1874 return -1;
1912 1875
1913 /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */ 1876 /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
1914 p = &str[strlen(str) - 1]; 1877 p = &str[strlen(str) - 1];
1915 while(p != &str[1]) { 1878 while (p != &str[1]) {
1916 if(*p == '%') *p = '\0'; 1879 if (*p == '%')
1917 else if(*p == ',' && i) { 1880 *p = '\0';
1918 *p = '\0'; /* reset it so get_timevar(str) works nicely later */ 1881 else if (*p == ',' && i) {
1919 th->pl = (unsigned char)strtoul(p+1, NULL, 0); 1882 *p = '\0'; /* reset it so get_timevar(str) works nicely later */
1883 th->pl = (unsigned char)strtoul(p + 1, NULL, 0);
1920 break; 1884 break;
1921 } 1885 }
1922 i = 1; 1886 i = 1;
@@ -1924,10 +1888,13 @@ get_threshold(char *str, threshold *th)
1924 } 1888 }
1925 th->rta = get_timevar(str); 1889 th->rta = get_timevar(str);
1926 1890
1927 if(!th->rta) return -1; 1891 if (!th->rta)
1892 return -1;
1928 1893
1929 if(th->rta > MAXTTL * 1000000) th->rta = MAXTTL * 1000000; 1894 if (th->rta > MAXTTL * 1000000)
1930 if(th->pl > 100) th->pl = 100; 1895 th->rta = MAXTTL * 1000000;
1896 if (th->pl > 100)
1897 th->pl = 100;
1931 1898
1932 return 0; 1899 return 0;
1933} 1900}
@@ -1943,8 +1910,8 @@ get_threshold(char *str, threshold *th)
1943 * @param[in] mode Determines whether this a threshold for rta, packet_loss, jitter, mos or score (exclusively) 1910 * @param[in] mode Determines whether this a threshold for rta, packet_loss, jitter, mos or score (exclusively)
1944 */ 1911 */
1945static bool get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, threshold_mode mode) { 1912static bool get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, threshold_mode mode) {
1946 if (!str || !length || !warn || !crit) return false; 1913 if (!str || !length || !warn || !crit)
1947 1914 return false;
1948 1915
1949 // p points to the last char in str 1916 // p points to the last char in str
1950 char *p = &str[length - 1]; 1917 char *p = &str[length - 1];
@@ -1952,18 +1919,17 @@ static bool get_threshold2(char *str, size_t length, threshold *warn, threshold
1952 // first_iteration is bof-stop on stupid libc's 1919 // first_iteration is bof-stop on stupid libc's
1953 bool first_iteration = true; 1920 bool first_iteration = true;
1954 1921
1955 while(p != &str[0]) { 1922 while (p != &str[0]) {
1956 if( (*p == 'm') || (*p == '%') ) { 1923 if ((*p == 'm') || (*p == '%')) {
1957 *p = '\0'; 1924 *p = '\0';
1958 } else if(*p == ',' && !first_iteration) { 1925 } else if (*p == ',' && !first_iteration) {
1959 *p = '\0'; /* reset it so get_timevar(str) works nicely later */ 1926 *p = '\0'; /* reset it so get_timevar(str) works nicely later */
1960 1927
1961 char *start_of_value = p + 1; 1928 char *start_of_value = p + 1;
1962 1929
1963 if (!parse_threshold2_helper(start_of_value, strlen(start_of_value), crit, mode)){ 1930 if (!parse_threshold2_helper(start_of_value, strlen(start_of_value), crit, mode)) {
1964 return false; 1931 return false;
1965 } 1932 }
1966
1967 } 1933 }
1968 first_iteration = false; 1934 first_iteration = false;
1969 p--; 1935 p--;
@@ -1976,22 +1942,22 @@ static bool parse_threshold2_helper(char *s, size_t length, threshold *thr, thre
1976 char *resultChecker = {0}; 1942 char *resultChecker = {0};
1977 1943
1978 switch (mode) { 1944 switch (mode) {
1979 case const_rta_mode: 1945 case const_rta_mode:
1980 thr->rta = strtod(s, &resultChecker) * 1000; 1946 thr->rta = strtod(s, &resultChecker) * 1000;
1981 break; 1947 break;
1982 case const_packet_loss_mode: 1948 case const_packet_loss_mode:
1983 thr->pl = (unsigned char)strtoul(s, &resultChecker, 0); 1949 thr->pl = (unsigned char)strtoul(s, &resultChecker, 0);
1984 break; 1950 break;
1985 case const_jitter_mode: 1951 case const_jitter_mode:
1986 thr->jitter = strtod(s, &resultChecker); 1952 thr->jitter = strtod(s, &resultChecker);
1987 1953
1988 break; 1954 break;
1989 case const_mos_mode: 1955 case const_mos_mode:
1990 thr->mos = strtod(s, &resultChecker); 1956 thr->mos = strtod(s, &resultChecker);
1991 break; 1957 break;
1992 case const_score_mode: 1958 case const_score_mode:
1993 thr->score = strtod(s, &resultChecker); 1959 thr->score = strtod(s, &resultChecker);
1994 break; 1960 break;
1995 } 1961 }
1996 1962
1997 if (resultChecker == s) { 1963 if (resultChecker == s) {
@@ -2007,123 +1973,116 @@ static bool parse_threshold2_helper(char *s, size_t length, threshold *thr, thre
2007 return true; 1973 return true;
2008} 1974}
2009 1975
2010unsigned short 1976unsigned short icmp_checksum(uint16_t *p, size_t n) {
2011icmp_checksum(uint16_t *p, size_t n)
2012{
2013 unsigned short cksum; 1977 unsigned short cksum;
2014 long sum = 0; 1978 long sum = 0;
2015 1979
2016 /* sizeof(uint16_t) == 2 */ 1980 /* sizeof(uint16_t) == 2 */
2017 while(n >= 2) { 1981 while (n >= 2) {
2018 sum += *(p++); 1982 sum += *(p++);
2019 n -= 2; 1983 n -= 2;
2020 } 1984 }
2021 1985
2022 /* mop up the occasional odd byte */ 1986 /* mop up the occasional odd byte */
2023 if(n == 1) sum += *((uint8_t *)p -1); 1987 if (n == 1)
1988 sum += *((uint8_t *)p - 1);
2024 1989
2025 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 1990 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
2026 sum += (sum >> 16); /* add carry */ 1991 sum += (sum >> 16); /* add carry */
2027 cksum = ~sum; /* ones-complement, trunc to 16 bits */ 1992 cksum = ~sum; /* ones-complement, trunc to 16 bits */
2028 1993
2029 return cksum; 1994 return cksum;
2030} 1995}
2031 1996
2032void 1997void print_help(void) {
2033print_help(void)
2034{
2035 /*print_revision (progname);*/ /* FIXME: Why? */ 1998 /*print_revision (progname);*/ /* FIXME: Why? */
2036 printf ("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n"); 1999 printf("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n");
2037 2000
2038 printf (COPYRIGHT, copyright, email); 2001 printf(COPYRIGHT, copyright, email);
2039 2002
2040 printf ("\n\n"); 2003 printf("\n\n");
2041 2004
2042 print_usage (); 2005 print_usage();
2043 2006
2044 printf (UT_HELP_VRSN); 2007 printf(UT_HELP_VRSN);
2045 printf (UT_EXTRA_OPTS); 2008 printf(UT_EXTRA_OPTS);
2046 2009
2047 printf (" %s\n", "-H"); 2010 printf(" %s\n", "-H");
2048 printf (" %s\n", _("specify a target")); 2011 printf(" %s\n", _("specify a target"));
2049 printf (" %s\n", "[-4|-6]"); 2012 printf(" %s\n", "[-4|-6]");
2050 printf (" %s\n", _("Use IPv4 (default) or IPv6 to communicate with the targets")); 2013 printf(" %s\n", _("Use IPv4 (default) or IPv6 to communicate with the targets"));
2051 printf (" %s\n", "-w"); 2014 printf(" %s\n", "-w");
2052 printf (" %s", _("warning threshold (currently ")); 2015 printf(" %s", _("warning threshold (currently "));
2053 printf ("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl); 2016 printf("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl);
2054 printf (" %s\n", "-c"); 2017 printf(" %s\n", "-c");
2055 printf (" %s", _("critical threshold (currently ")); 2018 printf(" %s", _("critical threshold (currently "));
2056 printf ("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl); 2019 printf("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl);
2057 2020
2058 printf (" %s\n", "-R"); 2021 printf(" %s\n", "-R");
2059 printf (" %s\n", _("RTA, round trip average, mode warning,critical, ex. 100ms,200ms unit in ms")); 2022 printf(" %s\n", _("RTA, round trip average, mode warning,critical, ex. 100ms,200ms unit in ms"));
2060 printf (" %s\n", "-P"); 2023 printf(" %s\n", "-P");
2061 printf (" %s\n", _("packet loss mode, ex. 40%,50% , unit in %")); 2024 printf(" %s\n", _("packet loss mode, ex. 40%,50% , unit in %"));
2062 printf (" %s\n", "-J"); 2025 printf(" %s\n", "-J");
2063 printf (" %s\n", _("jitter mode warning,critical, ex. 40.000ms,50.000ms , unit in ms ")); 2026 printf(" %s\n", _("jitter mode warning,critical, ex. 40.000ms,50.000ms , unit in ms "));
2064 printf (" %s\n", "-M"); 2027 printf(" %s\n", "-M");
2065 printf (" %s\n", _("MOS mode, between 0 and 4.4 warning,critical, ex. 3.5,3.0")); 2028 printf(" %s\n", _("MOS mode, between 0 and 4.4 warning,critical, ex. 3.5,3.0"));
2066 printf (" %s\n", "-S"); 2029 printf(" %s\n", "-S");
2067 printf (" %s\n", _("score mode, max value 100 warning,critical, ex. 80,70 ")); 2030 printf(" %s\n", _("score mode, max value 100 warning,critical, ex. 80,70 "));
2068 printf (" %s\n", "-O"); 2031 printf(" %s\n", "-O");
2069 printf (" %s\n", _("detect out of order ICMP packts ")); 2032 printf(" %s\n", _("detect out of order ICMP packts "));
2070 printf (" %s\n", "-H"); 2033 printf(" %s\n", "-H");
2071 printf (" %s\n", _("specify a target")); 2034 printf(" %s\n", _("specify a target"));
2072 printf (" %s\n", "-s"); 2035 printf(" %s\n", "-s");
2073 printf (" %s\n", _("specify a source IP address or device name")); 2036 printf(" %s\n", _("specify a source IP address or device name"));
2074 printf (" %s\n", "-n"); 2037 printf(" %s\n", "-n");
2075 printf (" %s", _("number of packets to send (currently ")); 2038 printf(" %s", _("number of packets to send (currently "));
2076 printf ("%u)\n",packets); 2039 printf("%u)\n", packets);
2077 printf (" %s\n", "-p"); 2040 printf(" %s\n", "-p");
2078 printf (" %s", _("number of packets to send (currently ")); 2041 printf(" %s", _("number of packets to send (currently "));
2079 printf ("%u)\n",packets); 2042 printf("%u)\n", packets);
2080 printf (" %s\n", "-i"); 2043 printf(" %s\n", "-i");
2081 printf (" %s", _("max packet interval (currently ")); 2044 printf(" %s", _("max packet interval (currently "));
2082 printf ("%0.3fms)\n",(float)pkt_interval / 1000); 2045 printf("%0.3fms)\n", (float)pkt_interval / 1000);
2083 printf (" %s\n", "-I"); 2046 printf(" %s\n", "-I");
2084 printf (" %s", _("max target interval (currently ")); 2047 printf(" %s", _("max target interval (currently "));
2085 printf ("%0.3fms)\n", (float)target_interval / 1000); 2048 printf("%0.3fms)\n", (float)target_interval / 1000);
2086 printf (" %s\n", "-m"); 2049 printf(" %s\n", "-m");
2087 printf (" %s",_("number of alive hosts required for success")); 2050 printf(" %s", _("number of alive hosts required for success"));
2088 printf ("\n"); 2051 printf("\n");
2089 printf (" %s\n", "-l"); 2052 printf(" %s\n", "-l");
2090 printf (" %s", _("TTL on outgoing packets (currently ")); 2053 printf(" %s", _("TTL on outgoing packets (currently "));
2091 printf ("%u)\n", ttl); 2054 printf("%u)\n", ttl);
2092 printf (" %s\n", "-t"); 2055 printf(" %s\n", "-t");
2093 printf (" %s",_("timeout value (seconds, currently ")); 2056 printf(" %s", _("timeout value (seconds, currently "));
2094 printf ("%u)\n", timeout); 2057 printf("%u)\n", timeout);
2095 printf (" %s\n", "-b"); 2058 printf(" %s\n", "-b");
2096 printf (" %s\n", _("Number of icmp data bytes to send")); 2059 printf(" %s\n", _("Number of icmp data bytes to send"));
2097 printf (" %s %u + %d)\n", _("Packet size will be data bytes + icmp header (currently"),icmp_data_size, ICMP_MINLEN); 2060 printf(" %s %u + %d)\n", _("Packet size will be data bytes + icmp header (currently"), icmp_data_size, ICMP_MINLEN);
2098 printf (" %s\n", "-v"); 2061 printf(" %s\n", "-v");
2099 printf (" %s\n", _("verbose")); 2062 printf(" %s\n", _("verbose"));
2100 printf ("\n"); 2063 printf("\n");
2101 printf ("%s\n", _("Notes:")); 2064 printf("%s\n", _("Notes:"));
2102 printf (" %s\n", _("If none of R,P,J,M,S or O is specified, default behavior is -R -P")); 2065 printf(" %s\n", _("If none of R,P,J,M,S or O is specified, default behavior is -R -P"));
2103 printf (" %s\n", _("The -H switch is optional. Naming a host (or several) to check is not.")); 2066 printf(" %s\n", _("The -H switch is optional. Naming a host (or several) to check is not."));
2104 printf ("\n"); 2067 printf("\n");
2105 printf (" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%")); 2068 printf(" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%"));
2106 printf (" %s\n", _("packet loss. The default values should work well for most users.")); 2069 printf(" %s\n", _("packet loss. The default values should work well for most users."));
2107 printf (" %s\n", _("You can specify different RTA factors using the standardized abbreviations")); 2070 printf(" %s\n", _("You can specify different RTA factors using the standardized abbreviations"));
2108 printf (" %s\n", _("us (microseconds), ms (milliseconds, default) or just plain s for seconds.")); 2071 printf(" %s\n", _("us (microseconds), ms (milliseconds, default) or just plain s for seconds."));
2109 /* -d not yet implemented */ 2072 /* -d not yet implemented */
2110 /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means WARNING if >= 12 hops")); 2073 /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means WARNING if >= 12 hops"));
2111 printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent.")); 2074 printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent."));
2112 printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/ 2075 printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/
2113 printf ("\n"); 2076 printf("\n");
2114 printf (" %s\n", _("The -v switch can be specified several times for increased verbosity.")); 2077 printf(" %s\n", _("The -v switch can be specified several times for increased verbosity."));
2115 /* printf ("%s\n", _("Long options are currently unsupported.")); 2078 /* printf ("%s\n", _("Long options are currently unsupported."));
2116 printf ("%s\n", _("Options marked with * require an argument")); 2079 printf ("%s\n", _("Options marked with * require an argument"));
2117 */ 2080 */
2118 2081
2119 printf (UT_SUPPORT); 2082 printf(UT_SUPPORT);
2120} 2083}
2121 2084
2122 2085void print_usage(void) {
2123 2086 printf("%s\n", _("Usage:"));
2124void
2125print_usage (void)
2126{
2127 printf ("%s\n", _("Usage:"));
2128 printf(" %s [options] [-H] host1 host2 hostN\n", progname); 2087 printf(" %s [options] [-H] host1 host2 hostN\n", progname);
2129} 2088}