-
Notifications
You must be signed in to change notification settings - Fork 251
/
fping.c
3194 lines (2676 loc) · 96 KB
/
fping.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* fping: fast-ping, file-ping, favorite-ping, funky-ping
*
* Ping a list of target hosts in a round robin fashion.
* A better ping overall.
*
* fping website: http://www.fping.org
*
* Current maintainer of fping: David Schweikert
* Please send suggestions and patches to: david@schweikert.ch
*
*
* Original author: Roland Schemers <schemers@stanford.edu>
* IPv6 Support: Jeroen Massar <jeroen@unfix.org / jeroen@ipng.nl>
* Improved main loop: David Schweikert <david@schweikert.ch>
* Debian Merge, TOS settings: Tobi Oetiker <tobi@oetiker.ch>
* Bugfixes, byte order & senseful seq.-numbers: Stephan Fuhrmann (stephan.fuhrmann AT 1und1.de)
*
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by Stanford University. The name of the University may not be used
* to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include "fping.h"
#include "config.h"
#include "options.h"
#include "optparse.h"
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include "seqmap.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#include <stddef.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#if HAVE_SYS_FILE_H
#include <sys/file.h>
#endif /* HAVE_SYS_FILE_H */
#ifdef IPV6
#include <netinet/icmp6.h>
#endif
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <netdb.h>
#include <sys/select.h>
/*** compatibility ***/
/* Mac OS X's getaddrinfo() does not fail if we use an invalid combination,
* e.g. AF_INET6 with "127.0.0.1". If we pass AI_UNUSABLE to flags, it behaves
* like other platforms. But AI_UNUSABLE isn't available on other platforms,
* and we can safely use 0 for flags instead.
*/
#ifndef AI_UNUSABLE
#define AI_UNUSABLE 0
#endif
/* MSG_TRUNC available on Linux kernel 2.2+, makes recvmsg return the full
* length of the raw packet received, even if the buffer is smaller */
#ifndef MSG_TRUNC
#define MSG_TRUNC 0
#define RECV_BUFSIZE 4096
#else
#define RECV_BUFSIZE 128
#endif
/*** externals ***/
extern char *optarg;
extern int optind, opterr;
#ifndef h_errno
extern int h_errno;
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
/*** Constants ***/
/* CLOCK_MONTONIC starts under macOS, OpenBSD and FreeBSD with undefined positive point and can not be use
* see github PR #217
* The configure script detect the predefined operating systems an set CLOCK_REALTIME using over ONLY_CLOCK_REALTIME variable
*/
#if HAVE_SO_TIMESTAMPNS || ONLY_CLOCK_REALTIME
#define CLOCKID CLOCK_REALTIME
#endif
#if !defined(CLOCKID)
#if defined(CLOCK_MONOTONIC)
#define CLOCKID CLOCK_MONOTONIC
#else
#define CLOCKID CLOCK_REALTIME
#endif
#endif
/*** Ping packet defines ***/
#define MAX_IP_PACKET 65535 /* (theoretical) max IPv4 packet size */
#define SIZE_IP_HDR 20 /* min IPv4 header size */
#define SIZE_ICMP_HDR 8 /* from ip_icmp.h */
#define MAX_PING_DATA (MAX_IP_PACKET - SIZE_IP_HDR - SIZE_ICMP_HDR)
#define MAX_GENERATE 131070 /* maximum number of hosts that -g can generate */
/* sized so as to be like traditional ping */
#define DEFAULT_PING_DATA_SIZE 56
/* ICMP Timestamp has a fixed payload size of 12 bytes */
#define ICMP_TIMESTAMP_DATA_SIZE 12
/* maxima and minima */
#ifdef FPING_SAFE_LIMITS
#define MIN_INTERVAL 1 /* in millisec */
#define MIN_PERHOST_INTERVAL 10 /* in millisec */
#else
#define MIN_INTERVAL 0
#define MIN_PERHOST_INTERVAL 0
#endif
/* response time array flags */
#define RESP_WAITING -1
#define RESP_UNUSED -2
#define RESP_ERROR -3
#define RESP_TIMEOUT -4
/* debugging flags */
#if defined(DEBUG) || defined(_DEBUG)
#define DBG_TRACE 1
#define DBG_SENT_TIMES 2
#define DBG_RANDOM_LOSE_FEW 4
#define DBG_RANDOM_LOSE_MANY 8
#define DBG_PRINT_PER_SYSTEM 16
#define DBG_REPORT_ALL_RTTS 32
#endif /* DEBUG || _DEBUG */
/* Long names for ICMP packet types */
#define ICMP_TYPE_STR_MAX 18
char *icmp_type_str[19] = {
"ICMP Echo Reply", /* 0 */
"",
"",
"ICMP Unreachable", /* 3 */
"ICMP Source Quench", /* 4 */
"ICMP Redirect", /* 5 */
"",
"",
"ICMP Echo", /* 8 */
"",
"",
"ICMP Time Exceeded", /* 11 */
"ICMP Parameter Problem", /* 12 */
"ICMP Timestamp Request", /* 13 */
"ICMP Timestamp Reply", /* 14 */
"ICMP Information Request", /* 15 */
"ICMP Information Reply", /* 16 */
"ICMP Mask Request", /* 17 */
"ICMP Mask Reply" /* 18 */
};
char *icmp_unreach_str[16] = {
"ICMP Network Unreachable", /* 0 */
"ICMP Host Unreachable", /* 1 */
"ICMP Protocol Unreachable", /* 2 */
"ICMP Port Unreachable", /* 3 */
"ICMP Unreachable (Fragmentation Needed)", /* 4 */
"ICMP Unreachable (Source Route Failed)", /* 5 */
"ICMP Unreachable (Destination Network Unknown)", /* 6 */
"ICMP Unreachable (Destination Host Unknown)", /* 7 */
"ICMP Unreachable (Source Host Isolated)", /* 8 */
"ICMP Unreachable (Communication with Network Prohibited)", /* 9 */
"ICMP Unreachable (Communication with Host Prohibited)", /* 10 */
"ICMP Unreachable (Network Unreachable For Type Of Service)", /* 11 */
"ICMP Unreachable (Host Unreachable For Type Of Service)", /* 12 */
"ICMP Unreachable (Communication Administratively Prohibited)", /* 13 */
"ICMP Unreachable (Host Precedence Violation)", /* 14 */
"ICMP Unreachable (Precedence cutoff in effect)" /* 15 */
};
#define ICMP_UNREACH_MAXTYPE 15
struct event;
typedef struct host_entry {
int i; /* index into array */
char *name; /* name as given by user */
char *host; /* text description of host */
struct sockaddr_storage saddr; /* internet address */
socklen_t saddr_len;
int64_t timeout; /* time to wait for response */
int64_t last_send_time; /* time of last packet sent */
int num_sent; /* number of ping packets sent (for statistics) */
int num_recv; /* number of pings received (duplicates ignored) */
int num_recv_total; /* number of pings received, including duplicates */
int64_t max_reply; /* longest response time */
int64_t min_reply; /* shortest response time */
int64_t total_time; /* sum of response times */
/* _i -> splits (reset on every report interval) */
int num_sent_i; /* number of ping packets sent */
int num_recv_i; /* number of pings received */
int64_t max_reply_i; /* longest response time */
int64_t min_reply_i; /* shortest response time */
int64_t total_time_i; /* sum of response times */
int64_t *resp_times; /* individual response times */
/* to avoid allocating two struct events each time that we send a ping, we
* preallocate here two struct events for each ping that we might send for
* this host. */
struct event *event_storage_ping;
struct event *event_storage_timeout;
} HOST_ENTRY;
int event_storage_count; /* how many events can be stored in host_entry->event_storage_xxx */
/* basic algorithm to ensure that we have correct data at all times:
*
* 1. when a ping is sent:
* - two events get added into event_queue:
* - t+PERIOD: ping event
* - t+TIMEOUT: timeout event
*
* 2. when a ping is received:
* - record statistics (increase num_sent and num_received)
* - remove timeout event (we store the event in seqmap, so that we can retrieve it when the response is received)
*
* 3. when a timeout happens:
* - record statistics (increase num_sent only)
*/
#define EV_TYPE_PING 1
#define EV_TYPE_TIMEOUT 2
struct event {
struct event *ev_prev;
struct event *ev_next;
int64_t ev_time;
struct host_entry *host;
int ping_index;
};
struct event_queue {
struct event *first;
struct event *last;
};
/*** globals ***/
HOST_ENTRY **table = NULL; /* array of pointers to items in the list */
/* we keep two separate queues: a ping queue, for when the next ping should be
* sent, and a timeout queue. the reason for having two separate queues is that
* the ping period and the timeout value are different, so if we put them in
* the same event queue, we would need to scan many more entries when inserting
* into the sorted list.
*/
struct event_queue event_queue_ping;
struct event_queue event_queue_timeout;
char *prog;
int ident4 = 0; /* our icmp identity field */
int ident6 = 0;
int socket4 = -1;
int socktype4 = -1;
int using_sock_dgram4 = 0;
#ifndef IPV6
int hints_ai_family = AF_INET;
#else
int socket6 = -1;
int socktype6 = -1;
int hints_ai_family = AF_UNSPEC;
#endif
volatile sig_atomic_t status_snapshot = 0;
volatile sig_atomic_t finish_requested = 0;
unsigned int debugging = 0;
/* all time-related values are int64_t nanoseconds */
unsigned int retry = DEFAULT_RETRY;
int64_t timeout = (int64_t)DEFAULT_TIMEOUT * 1000000;
int64_t interval = (int64_t)DEFAULT_INTERVAL * 1000000;
int64_t perhost_interval = (int64_t)DEFAULT_PERHOST_INTERVAL * 1000000;
float backoff = DEFAULT_BACKOFF_FACTOR;
unsigned int ping_data_size = DEFAULT_PING_DATA_SIZE;
unsigned int count = 1, min_reachable = 0;
unsigned int trials;
int64_t report_interval = 0;
unsigned int ttl = 0;
int src_addr_set = 0;
struct in_addr src_addr;
#ifdef IPV6
int src_addr6_set = 0;
struct in6_addr src_addr6;
#endif
/* global stats */
int64_t max_reply = 0;
int64_t min_reply = 0;
int64_t total_replies = 0;
int64_t sum_replies = 0;
int max_hostname_len = 0;
int num_hosts = 0; /* total number of hosts */
int num_alive = 0, /* total number alive */
num_unreachable = 0, /* total number unreachable */
num_noaddress = 0; /* total number of addresses not found */
int num_timeout = 0, /* number of times select timed out */
num_pingsent = 0, /* total pings sent */
num_pingreceived = 0, /* total pings received */
num_othericmprcvd = 0; /* total non-echo-reply ICMP received */
struct timespec current_time; /* current time (pseudo) */
int64_t current_time_ns;
int64_t start_time;
int64_t end_time;
int64_t last_send_time; /* time last ping was sent */
int64_t next_report_time; /* time next -Q report is expected */
/* switches */
int generate_flag = 0; /* flag for IP list generation */
int verbose_flag, quiet_flag, stats_flag, unreachable_flag, alive_flag;
int elapsed_flag, version_flag, count_flag, loop_flag, netdata_flag;
int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag, rdns_flag;
int multif_flag, timeout_flag, fast_reachable;
int outage_flag = 0;
int timestamp_flag = 0;
int timestamp_format_flag = 0;
int random_data_flag = 0;
int cumulative_stats_flag = 0;
int check_source_flag = 0;
int icmp_request_typ = 0;
int print_tos_flag = 0;
int print_ttl_flag = 0;
int size_flag = 0;
#if defined(DEBUG) || defined(_DEBUG)
int randomly_lose_flag, trace_flag, print_per_system_flag;
int lose_factor;
#endif /* DEBUG || _DEBUG */
unsigned int fwmark = 0;
char *filename = NULL; /* file containing hosts to ping */
/*** forward declarations ***/
void add_name(char *name);
void add_addr(char *name, char *host, struct sockaddr *ipaddr, socklen_t ipaddr_len);
char *na_cat(char *name, struct in_addr ipaddr);
void crash_and_burn(char *message);
void errno_crash_and_burn(char *message);
char *get_host_by_address(struct in_addr in);
int send_ping(HOST_ENTRY *h, int index);
void usage(int);
int wait_for_reply(int64_t);
void print_per_system_stats(void);
void print_per_system_splits(void);
void stats_reset_interval(HOST_ENTRY *h);
void print_netdata(void);
void print_global_stats(void);
void main_loop();
void signal_handler(int);
void finish();
const char *sprint_tm(int64_t t);
void ev_enqueue(struct event_queue *queue, struct event *event);
struct event *ev_dequeue(struct event_queue *queue);
void ev_remove(struct event_queue *queue, struct event *event);
void add_cidr(char *);
void add_range(char *, char *);
void add_addr_range_ipv4(unsigned long, unsigned long);
void print_warning(char *fmt, ...);
int addr_cmp(struct sockaddr *a, struct sockaddr *b);
void host_add_ping_event(HOST_ENTRY *h, int index, int64_t ev_time);
void host_add_timeout_event(HOST_ENTRY *h, int index, int64_t ev_time);
struct event *host_get_timeout_event(HOST_ENTRY *h, int index);
void stats_add(HOST_ENTRY *h, int index, int success, int64_t latency);
void update_current_time();
void print_timestamp_format(int64_t current_time_ns, int timestamp_format);
static uint32_t ms_since_midnight_utc(int64_t time_val);
/************************************************************
Function: p_setsockopt
*************************************************************
Inputs: p_uid: privileged uid. Others as per setsockopt(2)
Description:
Elevates privileges to p_uid when required, calls
setsockopt, and drops privileges back.
************************************************************/
int p_setsockopt(uid_t p_uid, int sockfd, int level, int optname,
const void *optval, socklen_t optlen)
{
const uid_t saved_uid = geteuid();
int res;
if (p_uid != saved_uid && seteuid(p_uid)) {
perror("cannot elevate privileges for setsockopt");
}
res = setsockopt(sockfd, level, optname, optval, optlen);
if (p_uid != saved_uid && seteuid(saved_uid)) {
perror("fatal error: could not drop privileges after setsockopt");
/* continuing would be a security hole */
exit(4);
}
return res;
}
/************************************************************
Function: main
*************************************************************
Inputs: int argc, char** argv
Description:
Main program entry point
************************************************************/
int main(int argc, char **argv)
{
/* Debug: CPU Performance */
#if defined(DEBUG) || defined(_DEBUG)
clock_t perf_cpu_start, perf_cpu_end;
double perf_cpu_time_used;
perf_cpu_start = clock();
#endif /* DEBUG || _DEBUG */
int c;
const uid_t suid = geteuid();
int tos = 0;
struct optparse optparse_state;
#ifdef USE_SIGACTION
struct sigaction act;
#endif
/* pre-parse -h/--help, so that we also can output help information
* without trying to open the socket, which might fail */
prog = argv[0];
if (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
usage(0);
}
socket4 = open_ping_socket_ipv4(&socktype4);
#ifdef __linux__
/* We only treat SOCK_DGRAM differently on Linux, where the IPv4 header
* structure is missing in the message.
*/
using_sock_dgram4 = (socktype4 == SOCK_DGRAM);
#endif
#ifdef IPV6
socket6 = open_ping_socket_ipv6(&socktype6);
/* if called (sym-linked) via 'fping6', imply '-6'
* for backward compatibility */
if (strstr(prog, "fping6")) {
hints_ai_family = AF_INET6;
}
#endif
memset(&src_addr, 0, sizeof(src_addr));
#ifdef IPV6
memset(&src_addr6, 0, sizeof(src_addr6));
#endif
if (!suid && suid != getuid()) {
/* *temporarily* drop privileges */
if (seteuid(getuid()) == -1)
perror("cannot setuid");
}
optparse_init(&optparse_state, argv);
ident4 = ident6 = htons(getpid() & 0xFFFF);
verbose_flag = 1;
backoff_flag = 1;
opterr = 1;
/* get command line options */
struct optparse_long longopts[] = {
{ "ipv4", '4', OPTPARSE_NONE },
{ "ipv6", '6', OPTPARSE_NONE },
{ "alive", 'a', OPTPARSE_NONE },
{ "addr", 'A', OPTPARSE_NONE },
{ "size", 'b', OPTPARSE_REQUIRED },
{ "backoff", 'B', OPTPARSE_REQUIRED },
{ "count", 'c', OPTPARSE_REQUIRED },
{ "vcount", 'C', OPTPARSE_REQUIRED },
{ "rdns", 'd', OPTPARSE_NONE },
{ "timestamp", 'D', OPTPARSE_NONE },
{ "timestamp-format", '0', OPTPARSE_REQUIRED },
{ "elapsed", 'e', OPTPARSE_NONE },
{ "file", 'f', OPTPARSE_REQUIRED },
{ "generate", 'g', OPTPARSE_NONE },
{ "help", 'h', OPTPARSE_NONE },
{ "ttl", 'H', OPTPARSE_REQUIRED },
{ "interval", 'i', OPTPARSE_REQUIRED },
{ "iface", 'I', OPTPARSE_REQUIRED },
{ "icmp-timestamp", '0', OPTPARSE_NONE },
#ifdef SO_MARK
{ "fwmark", 'k', OPTPARSE_REQUIRED },
#endif
{ "loop", 'l', OPTPARSE_NONE },
{ "all", 'm', OPTPARSE_NONE },
{ "dontfrag", 'M', OPTPARSE_NONE },
{ "name", 'n', OPTPARSE_NONE },
{ "netdata", 'N', OPTPARSE_NONE },
{ "outage", 'o', OPTPARSE_NONE },
{ "tos", 'O', OPTPARSE_REQUIRED },
{ "period", 'p', OPTPARSE_REQUIRED },
{ "quiet", 'q', OPTPARSE_NONE },
{ "squiet", 'Q', OPTPARSE_REQUIRED },
{ "retry", 'r', OPTPARSE_REQUIRED },
{ "random", 'R', OPTPARSE_NONE },
{ "stats", 's', OPTPARSE_NONE },
{ "src", 'S', OPTPARSE_REQUIRED },
{ "timeout", 't', OPTPARSE_REQUIRED },
{ NULL, 'T', OPTPARSE_REQUIRED },
{ "unreach", 'u', OPTPARSE_NONE },
{ "version", 'v', OPTPARSE_NONE },
{ "reachable", 'x', OPTPARSE_REQUIRED },
{ "fast-reachable", 'X', OPTPARSE_REQUIRED },
{ "check-source", '0', OPTPARSE_NONE },
{ "print-tos", '0', OPTPARSE_NONE },
{ "print-ttl", '0', OPTPARSE_NONE },
#if defined(DEBUG) || defined(_DEBUG)
{ NULL, 'z', OPTPARSE_REQUIRED },
#endif
{ 0, 0, 0 }
};
float opt_value_float;
while ((c = optparse_long(&optparse_state, longopts, NULL)) != EOF) {
switch (c) {
case '0':
if(strstr(optparse_state.optlongname, "timestamp-format") != NULL) {
if(strcmp(optparse_state.optarg, "ctime") == 0) {
timestamp_format_flag = 1;
}else if(strcmp(optparse_state.optarg, "iso") == 0) {
timestamp_format_flag = 2;
}else if(strcmp(optparse_state.optarg, "rfc3339") == 0) {
timestamp_format_flag = 3;
}else{
usage(1);
}
} else if (strstr(optparse_state.optlongname, "check-source") != NULL) {
check_source_flag = 1;
} else if (strstr(optparse_state.optlongname, "icmp-timestamp") != NULL) {
#ifdef IPV6
if (hints_ai_family != AF_UNSPEC && hints_ai_family != AF_INET) {
fprintf(stderr, "%s: ICMP Timestamp is IPv4 only\n", prog);
exit(1);
}
hints_ai_family = AF_INET;
#endif
icmp_request_typ = 13;
ping_data_size = ICMP_TIMESTAMP_DATA_SIZE;
} else if (strstr(optparse_state.optlongname, "print-tos") != NULL) {
print_tos_flag = 1;
} else if (strstr(optparse_state.optlongname, "print-ttl") != NULL) {
print_ttl_flag = 1;
} else {
usage(1);
}
break;
case '4':
#ifdef IPV6
if (hints_ai_family != AF_UNSPEC && hints_ai_family != AF_INET) {
fprintf(stderr, "%s: can't specify both -4 and -6\n", prog);
exit(1);
}
hints_ai_family = AF_INET;
#endif
break;
case '6':
#ifdef IPV6
if (hints_ai_family != AF_UNSPEC && hints_ai_family != AF_INET6) {
fprintf(stderr, "%s: can't specify both -4 and -6\n", prog);
exit(1);
}
hints_ai_family = AF_INET6;
#else
fprintf(stderr, "%s: IPv6 not supported by this binary\n", prog);
exit(1);
#endif
break;
case 'M':
#ifdef IP_MTU_DISCOVER
if (socket4 >= 0) {
int val = IP_PMTUDISC_DO;
if (setsockopt(socket4, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val))) {
perror("setsockopt IP_MTU_DISCOVER");
}
}
#ifdef IPV6
if (socket6 >= 0) {
int val = IPV6_PMTUDISC_DO;
if (setsockopt(socket6, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, sizeof(val))) {
perror("setsockopt IPV6_MTU_DISCOVER");
}
}
#endif
#else
fprintf(stderr, "%s, -M option not supported on this platform\n", prog);
exit(1);
#endif
break;
case 't':
if (sscanf(optparse_state.optarg, "%f", &opt_value_float) != 1)
usage(1);
if (opt_value_float < 0) {
usage(1);
}
timeout = opt_value_float * 1000000;
timeout_flag = 1;
break;
case 'r':
if (sscanf(optparse_state.optarg, "%u", &retry) != 1)
usage(1);
break;
case 'i':
if (sscanf(optparse_state.optarg, "%f", &opt_value_float) != 1)
usage(1);
if (opt_value_float < 0) {
usage(1);
}
interval = opt_value_float * 1000000;
break;
case 'p':
if (sscanf(optparse_state.optarg, "%f", &opt_value_float) != 1)
usage(1);
if (opt_value_float < 0) {
usage(1);
}
perhost_interval = opt_value_float * 1000000;
break;
case 'c':
if (!(count = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
count_flag = 1;
break;
case 'C':
if (!(count = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
count_flag = 1;
report_all_rtts_flag = 1;
break;
case 'b':
if (sscanf(optparse_state.optarg, "%u", &ping_data_size) != 1)
usage(1);
size_flag = 1;
break;
case 'h':
usage(0);
break;
case 'q':
verbose_flag = 0;
quiet_flag = 1;
break;
case 'Q':
verbose_flag = 0;
quiet_flag = 1;
if (sscanf(optparse_state.optarg, "%f", &opt_value_float) != 1)
usage(1);
if (opt_value_float < 0) {
usage(1);
}
report_interval = opt_value_float * 1e9;
/* recognize keyword(s) after number, ignore everything else */
{
char *comma = strchr(optparse_state.optarg, ',');
if ((comma != NULL) && (strcmp(++comma, "cumulative") == 0)) {
cumulative_stats_flag = 1;
}
}
break;
case 'e':
elapsed_flag = 1;
break;
case 'm':
multif_flag = 1;
break;
case 'N':
netdata_flag = 1;
break;
case 'n':
name_flag = 1;
if (rdns_flag) {
fprintf(stderr, "%s: use either one of -d or -n\n", prog);
exit(1);
}
break;
case 'd':
rdns_flag = 1;
if (name_flag) {
fprintf(stderr, "%s: use either one of -d or -n\n", prog);
exit(1);
}
break;
case 'A':
addr_flag = 1;
break;
case 'B':
if (!(backoff = atof(optparse_state.optarg)))
usage(1);
break;
case 's':
stats_flag = 1;
break;
case 'D':
timestamp_flag = 1;
break;
case 'R':
random_data_flag = 1;
break;
case 'l':
loop_flag = 1;
backoff_flag = 0;
break;
case 'u':
unreachable_flag = 1;
break;
case 'a':
alive_flag = 1;
break;
case 'H':
if (!(ttl = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
break;
#if defined(DEBUG) || defined(_DEBUG)
case 'z':
if (sscanf(optparse_state.optarg, "0x%x", &debugging) != 1)
if (sscanf(optparse_state.optarg, "%u", &debugging) != 1)
usage(1);
break;
#endif /* DEBUG || _DEBUG */
case 'v':
printf("%s: Version %s\n", prog, VERSION);
exit(0);
case 'x':
if (!(min_reachable = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
break;
case 'X':
if (!(min_reachable = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
fast_reachable = 1;
break;
case 'f':
filename = optparse_state.optarg;
break;
#ifdef SO_MARK
case 'k':
if (!(fwmark = (unsigned int)atol(optparse_state.optarg)))
usage(1);
if (socket4 >= 0)
if(-1 == p_setsockopt(suid, socket4, SOL_SOCKET, SO_MARK, &fwmark, sizeof fwmark))
perror("fwmark ipv4");
#ifdef IPV6
if (socket6 >= 0)
if(-1 == p_setsockopt(suid, socket6, SOL_SOCKET, SO_MARK, &fwmark, sizeof fwmark))
perror("fwmark ipv6");
#endif
break;
#endif
case 'g':
/* use IP list generation */
/* mutually exclusive with using file input or command line targets */
generate_flag = 1;
break;
case 'S':
if (inet_pton(AF_INET, optparse_state.optarg, &src_addr)) {
src_addr_set = 1;
break;
}
#ifdef IPV6
if (inet_pton(AF_INET6, optparse_state.optarg, &src_addr6)) {
src_addr6_set = 1;
break;
}
#endif
fprintf(stderr, "%s: can't parse source address: %s\n", prog, optparse_state.optarg);
exit(1);
case 'I':
#ifdef SO_BINDTODEVICE
if (socket4 >= 0) {
if (p_setsockopt(suid, socket4, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) {
perror("binding to specific interface (SO_BINTODEVICE)");
exit(1);
}
}
#ifdef IPV6
if (socket6 >= 0) {
if (p_setsockopt(suid, socket6, SOL_SOCKET, SO_BINDTODEVICE, optparse_state.optarg, strlen(optparse_state.optarg))) {
perror("binding to specific interface (SO_BINTODEVICE), IPV6");
exit(1);
}
}
#endif
#else
printf("%s: cant bind to a particular net interface since SO_BINDTODEVICE is not supported on your os.\n", prog);
exit(3);
;
#endif
break;
case 'T':
/* This option is ignored for compatibility reasons ("select timeout" is not meaningful anymore) */
break;
case 'O':
if (sscanf(optparse_state.optarg, "%i", &tos) == 1) {
if (socket4 >= 0) {
if (setsockopt(socket4, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) {
perror("setting type of service octet IP_TOS");
}
}
#if defined(IPV6) && defined(IPV6_TCLASS)
if (socket6 >= 0) {
if (setsockopt(socket6, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos))) {
perror("setting type of service octet IPV6_TCLASS");
}
}
#endif
}
else {
usage(1);
}
break;
case 'o':
outage_flag = 1;
break;
case '?':
fprintf(stderr, "%s: %s\n", argv[0], optparse_state.errmsg);
fprintf(stderr, "see 'fping -h' for usage information\n");
exit(1);
break;
}
}
/* permanently drop privileges */
if (suid != getuid() && setuid(getuid())) {
perror("fatal: failed to permanently drop privileges");
/* continuing would be a security hole */
exit(4);
}
/* validate various option settings */
#ifndef IPV6
if (socket4 < 0) {
crash_and_burn("can't create socket (must run as root?)");
}
#else
if ((socket4 < 0 && socket6 < 0) || (hints_ai_family == AF_INET6 && socket6 < 0)) {
crash_and_burn("can't create socket (must run as root?)");
}
#endif
if (ttl > 255) {
fprintf(stderr, "%s: ttl %u out of range\n", prog, ttl);
exit(1);
}
if (unreachable_flag && alive_flag) {
fprintf(stderr, "%s: specify only one of a, u\n", prog);
exit(1);
}
if (count_flag && loop_flag) {
fprintf(stderr, "%s: specify only one of c, l\n", prog);
exit(1);
}
#ifdef FPING_SAFE_LIMITS
if ((interval < (int64_t)MIN_INTERVAL * 1000000 || perhost_interval < (int64_t)MIN_PERHOST_INTERVAL * 1000000)
&& getuid()) {
fprintf(stderr, "%s: these options are too risky for mere mortals.\n", prog);
fprintf(stderr, "%s: You need -i >= %u and -p >= %u\n",
prog, MIN_INTERVAL, MIN_PERHOST_INTERVAL);
exit(1);
}
#endif
if (ping_data_size > MAX_PING_DATA) {
fprintf(stderr, "%s: data size %u not valid, must not be larger than %u\n",
prog, ping_data_size, (unsigned int)MAX_PING_DATA);
exit(1);
}
if ((backoff > MAX_BACKOFF_FACTOR) || (backoff < MIN_BACKOFF_FACTOR)) {
fprintf(stderr, "%s: backoff factor %.1f not valid, must be between %.1f and %.1f\n",
prog, backoff, MIN_BACKOFF_FACTOR, MAX_BACKOFF_FACTOR);
exit(1);
}
if (icmp_request_typ == 13 && size_flag != 0) {
fprintf(stderr, "%s: cannot change ICMP Timestamp size\n", prog);
exit(1);
}
if (count_flag) {
if (verbose_flag)
per_recv_flag = 1;
alive_flag = unreachable_flag = verbose_flag = 0;
}
if (loop_flag) {
if (!report_interval)