-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathfping.c
More file actions
2420 lines (1908 loc) · 69.2 KB
/
Copy pathfping.c
File metadata and controls
2420 lines (1908 loc) · 69.2 KB
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 "options.h"
/* if compiling for Windows, use this separate set
(too difficult to ifdef all the autoconf defines) */
#ifdef WIN32
/*** Windows includes ***/
#else
/*** autoconf includes ***/
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <getopt.h>
#include <stdarg.h>
#include "config.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 <string.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.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 <netdb.h>
/* RS6000 has sys/select.h */
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif /* HAVE_SYS_SELECT_H */
#endif /* WIN32 */
/*** externals ***/
extern char *optarg;
extern int optind,opterr;
extern int h_errno;
#ifdef __cplusplus
}
#endif /* __cplusplus */
/*** Constants ***/
#define EMAIL "david@schweikert.ch"
#define ANSI_COLOUR_RED "\x1b[31m"
#define ANSI_COLOUR_GREEN "\x1b[32m"
#define ANSI_COLOUR_RESET "\x1b[0m"
/*** Ping packet defines ***/
#define MIN_PING_DATA 0
#define MAX_IP_PACKET 65536 /* (theoretical) max IP packet size */
#define SIZE_IP_HDR 20
#ifndef IPV6
#define SIZE_ICMP_HDR ICMP_MINLEN /* from ip_icmp.h */
#else
#define SIZE_ICMP_HDR sizeof(FPING_ICMPHDR)
#endif
#define MAX_PING_DATA ( MAX_IP_PACKET - SIZE_IP_HDR - SIZE_ICMP_HDR )
/* sized so as to be like traditional ping */
#define DEFAULT_PING_DATA_SIZE 56
/* maxima and minima */
#define MAX_COUNT 10000
#define MIN_INTERVAL 10 /* in millisec */
#define MIN_PERHOST_INTERVAL 20 /* in millisec */
#define MIN_TIMEOUT 50 /* in millisec */
#define MAX_RETRY 20
/* response time array flags */
#define RESP_WAITING -1
#define RESP_UNUSED -2
#define RESP_ERROR -3
/* debugging flags */
#if defined( DEBUG ) || defined( _DEBUG )
#define DBG_TRACE 1
#define DBG_SENT_TIMES 2
#define DBG_RANDOM_LOSE_FEW 4
#define DBG_RANDOM_LOSE_MANY 8
#define DBG_PRINT_PER_SYSTEM 16
#define DBG_REPORT_ALL_RTTS 32
#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
/* entry used to keep track of each host we are pinging */
#define EV_TYPE_PING 1
#define EV_TYPE_TIMEOUT 2
typedef struct host_entry
{
/* Each host can have an event attached: either the next time that a ping needs
* to be sent, or the timeout, if the last ping was sent */
struct host_entry *ev_prev; /* double linked list for the event-queue */
struct host_entry *ev_next; /* double linked list for the event-queue */
struct timeval ev_time; /* time, after which this event should happen */
int ev_type; /* event type */
int i; /* index into array */
char *name; /* name as given by user */
char *host; /* text description of host */
char *pad; /* pad to align print names */
struct sockaddr_storage saddr; /* internet address */
socklen_t saddr_len;
int timeout; /* time to wait for response */
unsigned char running; /* unset when through sending */
unsigned char waiting; /* waiting for response */
struct timeval last_send_time; /* time of last packet sent */
int num_sent; /* number of ping packets sent */
int num_recv; /* number of pings received (duplicates ignored) */
int num_recv_total; /* number of pings received, including duplicates */
int max_reply; /* longest response time */
int min_reply; /* shortest response time */
int total_time; /* sum of response times */
/* _i -> splits (reset on every report interval) */
int num_sent_i; /* number of ping packets sent */
int num_recv_i; /* number of pings received */
int max_reply_i; /* longest response time */
int min_reply_i; /* shortest response time */
int total_time_i; /* sum of response times */
int *resp_times; /* individual response times */
#if defined( DEBUG ) || defined( _DEBUG )
int *sent_times; /* per-sent-ping timestamp */
#endif /* DEBUG || _DEBUG */
} HOST_ENTRY;
/*** globals ***/
HOST_ENTRY **table = NULL; /* array of pointers to items in the list */
/* event queue (ev): This, together with the ev_next / ev_prev elements are used
* to track the next event happening for each host. This can be either a new ping
* that needs to be sent or a timeout */
HOST_ENTRY *ev_first;
HOST_ENTRY *ev_last;
char *prog;
int ident; /* our pid */
int s; /* socket */
unsigned int debugging = 0;
/* times get *100 because all times are calculated in 10 usec units, not ms */
unsigned int retry = DEFAULT_RETRY;
unsigned int timeout = DEFAULT_TIMEOUT * 100;
unsigned int interval = DEFAULT_INTERVAL * 100;
unsigned int perhost_interval = DEFAULT_PERHOST_INTERVAL * 100;
float backoff = DEFAULT_BACKOFF_FACTOR;
unsigned int ping_data_size = DEFAULT_PING_DATA_SIZE;
unsigned int count = 1;
unsigned int trials;
unsigned int report_interval = 0;
unsigned int ttl = 0;
int src_addr_present = 0;
#ifndef IPV6
struct in_addr src_addr;
#else
struct in6_addr src_addr;
#endif
/* global stats */
long max_reply = 0;
long min_reply = 0;
int total_replies = 0;
double sum_replies = 0;
int max_hostname_len = 0;
int num_jobs = 0; /* number of hosts still to do */
int num_hosts; /* total number of hosts */
int num_alive = 0, /* total number alive */
num_unreachable = 0, /* total number unreachable */
num_noaddress = 0; /* total number of addresses not found */
int num_timeout = 0, /* number of times select timed out */
num_pingsent = 0, /* total pings sent */
num_pingreceived = 0, /* total pings received */
num_othericmprcvd = 0; /* total non-echo-reply ICMP received */
struct timeval current_time; /* current time (pseudo) */
struct timeval start_time;
struct timeval end_time;
struct timeval last_send_time; /* time last ping was sent */
struct timeval last_report_time; /* time last report was printed */
struct timezone tz;
/* 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;
int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag;
int multif_flag;
int timestamp_flag = 0;
int random_data_flag = 0;
int colour_flag = 0;
#if defined( DEBUG ) || defined( _DEBUG )
int randomly_lose_flag, sent_times_flag, trace_flag, print_per_system_flag;
int lose_factor;
#endif /* DEBUG || _DEBUG */
char *filename = NULL; /* file containing hosts to ping */
/*** 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 recvfrom_wto( int s, char *buf, int len, struct sockaddr *saddr, socklen_t *saddr_len, long timo );
void remove_job( HOST_ENTRY *h );
int send_ping( int s, HOST_ENTRY *h );
long timeval_diff( struct timeval *a, struct timeval *b );
void timeval_add(struct timeval *a, long t_10u);
void usage( int );
int wait_for_reply( long );
void print_per_system_stats( void );
void print_per_system_splits( void );
void print_global_stats( void );
void main_loop();
void finish();
int handle_random_icmp( FPING_ICMPHDR *p, struct sockaddr *addr, socklen_t addr_len);
char *sprint_tm( int t );
void ev_enqueue(HOST_ENTRY *h);
HOST_ENTRY *ev_dequeue();
void ev_remove(HOST_ENTRY *h);
void add_cidr(char *);
void add_range(char *, char *);
void print_warning(char *fmt, ...);
int addr_cmp(struct sockaddr *a, struct sockaddr *b);
/*** function definitions ***/
/************************************************************
Function: main
*************************************************************
Inputs: int argc, char** argv
Description:
Main program entry point
************************************************************/
int main( int argc, char **argv )
{
int c, i, n;
char *buf;
uid_t uid;
int tos = 0;
HOST_ENTRY *cursor;
s = open_ping_socket(ping_data_size);
if((uid = getuid())) {
/* drop privileges */
setuid( getuid() );
}
prog = argv[0];
ident = getpid() & 0xFFFF;
verbose_flag = 1;
backoff_flag = 1;
opterr = 1;
/* get command line options */
while( ( c = getopt( argc, argv, "gedhklmnqusaAvDRz:t:H:i:p:f:r:c:b:C:Q:B:S:I:T:O:" ) ) != EOF )
{
switch( c )
{
case 't':
if( !( timeout = ( unsigned int )atoi( optarg ) * 100 ) )
usage(1);
break;
case 'r':
if (!sscanf(optarg,"%i",&retry))
usage(1);
break;
case 'i':
if( !( interval = ( unsigned int )atoi( optarg ) * 100 ) )
usage(1);
break;
case 'p':
if( !( perhost_interval = ( unsigned int )atoi( optarg ) * 100 ) )
usage(1);
break;
case 'c':
if( !( count = ( unsigned int )atoi( optarg ) ) )
usage(1);
count_flag = 1;
break;
case 'C':
if( !( count = ( unsigned int )atoi( optarg ) ) )
usage(1);
count_flag = 1;
report_all_rtts_flag = 1;
break;
case 'b':
if (!sscanf(optarg,"%i",&ping_data_size))
usage(1);
break;
case 'h':
usage(0);
break;
case 'k':
colour_flag = 1;
break;
case 'q':
verbose_flag = 0;
quiet_flag = 1;
break;
case 'Q':
verbose_flag = 0;
quiet_flag = 1;
if( !( report_interval = ( unsigned int )atoi( optarg ) * 100000 ) )
usage(1);
break;
case 'e':
elapsed_flag = 1;
break;
case 'm':
multif_flag = 1;
break;
case 'd':
case 'n':
name_flag = 1;
break;
case 'A':
addr_flag = 1;
break;
case 'B':
if( !( backoff = atof( 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 = ( u_int )atoi( optarg ) ))
usage(1);
break;
#if defined( DEBUG ) || defined( _DEBUG )
case 'z':
if( ! ( debugging = ( unsigned int )atoi( optarg ) ) )
usage(1);
break;
#endif /* DEBUG || _DEBUG */
case 'v':
printf( "%s: Version %s\n", argv[0], VERSION);
printf( "%s: comments to %s\n", argv[0], EMAIL );
exit( 0 );
case 'f':
filename = optarg;
break;
case 'g':
/* use IP list generation */
/* mutually exclusive with using file input or command line targets */
generate_flag = 1;
break;
case 'S':
#ifndef IPV6
if( ! inet_pton( AF_INET, optarg, &src_addr ) )
#else
if( ! inet_pton( AF_INET6, optarg, &src_addr ) )
#endif
usage(1);
src_addr_present = 1;
break;
case 'I':
#ifdef SO_BINDTODEVICE
if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, optarg, strlen(optarg))) {
perror("binding to specific interface (SO_BINTODEVICE)");
}
#else
printf( "%s: cant bind to a particular net interface since SO_BINDTODEVICE is not supported on your os.\n", argv[0] );
exit(3);;
#endif
break;
case 'T':
/* This option is ignored for compatibility reasons ("select timeout" is not meaningful anymore) */
break;
case 'O':
if (sscanf(optarg,"%i",&tos)){
if ( setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) {
perror("setting type of service octet IP_TOS");
}
}
else {
usage(1);
}
break;
default:
fprintf(stderr, "see 'fping -h' for usage information\n");
exit(1);
break;
}/* SWITCH */
}/* WHILE */
/* validate various option settings */
if (ttl > 255) {
fprintf(stderr, "ttl %u out of range\n", ttl);
exit(1);
}
if( unreachable_flag && alive_flag )
{
fprintf( stderr, "%s: specify only one of a, u\n", argv[0] );
exit(1);
}/* IF */
if( count_flag && loop_flag )
{
fprintf( stderr, "%s: specify only one of c, l\n", argv[0] );
exit(1);
}/* IF */
if( ( interval < MIN_INTERVAL * 100 ||
perhost_interval < MIN_PERHOST_INTERVAL * 100 ||
retry > MAX_RETRY ||
timeout < MIN_TIMEOUT * 100 )
&& getuid() )
{
fprintf( stderr, "%s: these options are too risky for mere mortals.\n", prog );
fprintf( stderr, "%s: You need i >= %u, p >= %u, r < %u, and t >= %u\n",
prog, MIN_INTERVAL, MIN_PERHOST_INTERVAL, MAX_RETRY, MIN_TIMEOUT );
exit(1);
}/* IF */
if( ( ping_data_size > MAX_PING_DATA ) || ( ping_data_size < MIN_PING_DATA ) )
{
fprintf( stderr, "%s: data size %u not valid, must be between %u and %u\n",
prog, ping_data_size, (unsigned int) MIN_PING_DATA, (unsigned int) MAX_PING_DATA );
exit(1);
}/* IF */
if( ( backoff > MAX_BACKOFF_FACTOR ) || ( backoff < MIN_BACKOFF_FACTOR ) )
{
fprintf( stderr, "%s: backoff factor %.1f not valid, must be between %.1f and %.1f\n",
prog, backoff, MIN_BACKOFF_FACTOR, MAX_BACKOFF_FACTOR );
exit(1);
}/* IF */
if( count > MAX_COUNT )
{
fprintf( stderr, "%s: count %u not valid, must be less than %u\n",
prog, count, MAX_COUNT );
exit(1);
}/* IF */
if( alive_flag || unreachable_flag )
verbose_flag = 0;
if( count_flag )
{
if( verbose_flag )
per_recv_flag = 1;
alive_flag = unreachable_flag = verbose_flag = 0;
}/* IF */
if( loop_flag )
{
if( !report_interval )
per_recv_flag = 1;
alive_flag = unreachable_flag = verbose_flag = 0;
}/* IF */
trials = ( count > retry + 1 ) ? count : retry + 1;
#if defined( DEBUG ) || defined( _DEBUG )
if( debugging & DBG_TRACE )
trace_flag = 1;
if( ( debugging & DBG_SENT_TIMES ) && !loop_flag )
sent_times_flag = 1;
if( debugging & DBG_RANDOM_LOSE_FEW )
{
randomly_lose_flag = 1;
lose_factor = 1; /* ie, 1/4 */
}/* IF */
if( debugging & DBG_RANDOM_LOSE_MANY )
{
randomly_lose_flag = 1;
lose_factor = 5; /* ie, 3/4 */
}/* IF */
if( debugging & DBG_PRINT_PER_SYSTEM )
print_per_system_flag = 1;
if( ( debugging & DBG_REPORT_ALL_RTTS ) && !loop_flag )
report_all_rtts_flag = 1;
if( trace_flag )
{
fprintf( stderr, "%s:\n count: %u, retry: %u, interval: %u\n",
prog, count, retry, interval / 10 );
fprintf( stderr, " perhost_interval: %u, timeout: %u\n",
perhost_interval / 10, timeout / 10 );
fprintf( stderr, " ping_data_size = %u, trials = %u\n",
ping_data_size, trials );
if( verbose_flag ) fprintf( stderr, " verbose_flag set\n" );
if( multif_flag ) fprintf( stderr, " multif_flag set\n" );
if( name_flag ) fprintf( stderr, " name_flag set\n" );
if( addr_flag ) fprintf( stderr, " addr_flag set\n" );
if( stats_flag ) fprintf( stderr, " stats_flag set\n" );
if( unreachable_flag ) fprintf( stderr, " unreachable_flag set\n" );
if( alive_flag ) fprintf( stderr, " alive_flag set\n" );
if( elapsed_flag ) fprintf( stderr, " elapsed_flag set\n" );
if( version_flag ) fprintf( stderr, " version_flag set\n" );
if( count_flag ) fprintf( stderr, " count_flag set\n" );
if( loop_flag ) fprintf( stderr, " loop_flag set\n" );
if( backoff_flag ) fprintf( stderr, " backoff_flag set\n" );
if( per_recv_flag ) fprintf( stderr, " per_recv_flag set\n" );
if( report_all_rtts_flag ) fprintf( stderr, " report_all_rtts_flag set\n" );
if( randomly_lose_flag ) fprintf( stderr, " randomly_lose_flag set\n" );
if( sent_times_flag ) fprintf( stderr, " sent_times_flag set\n" );
if( print_per_system_flag ) fprintf( stderr, " print_per_system_flag set\n" );
}/* IF */
#endif /* DEBUG || _DEBUG */
/* set the TTL, if the -H option was set (otherwise ttl will be = 0) */
if(ttl > 0) {
if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl))) {
perror("setting time to live");
}
}
/* handle host names supplied on command line or in a file */
/* if the generate_flag is on, then generate the IP list */
argv = &argv[optind];
argc -= optind;
/* cover allowable conditions */
/* file and generate are mutually exclusive */
/* file and command line are mutually exclusive */
/* generate requires command line parameters beyond the switches */
if( ( *argv && filename ) || ( filename && generate_flag ) || ( generate_flag && !*argv ) )
usage(1);
/* if no conditions are specified, then assume input from stdin */
if( !*argv && !filename && !generate_flag )
filename = "-";
if( *argv && !generate_flag )
{
while( *argv )
{
add_name( *argv );
++argv;
}/* WHILE */
}/* IF */
else if( filename )
{
FILE *ping_file;
char line[132];
char host[132];
if( strcmp( filename, "-" ) == 0 )
ping_file = fdopen( 0, "r" );
else
ping_file = fopen( filename, "r" );
if( !ping_file )
errno_crash_and_burn( "fopen" );
while( fgets( line, sizeof(line), ping_file ) )
{
if( sscanf( line, "%s", host ) != 1 )
continue;
if( ( !*host ) || ( host[0] == '#' ) ) /* magic to avoid comments */
continue;
add_name(host);
}/* WHILE */
fclose( ping_file );
}
else if( *argv && generate_flag ) {
if(argc == 1) {
/* one target: we expect a cidr range (n.n.n.n/m) */
add_cidr(argv[0]);
}
else if(argc == 2) {
add_range(argv[0], argv[1]);
}
else {
usage(1);
}
}
else {
usage(1);
}
if( !num_hosts )
exit(1);
if(src_addr_present) {
socket_set_src_addr(s, src_addr);
}
/* allocate array to hold outstanding ping requests */
table = ( HOST_ENTRY** )malloc( sizeof( HOST_ENTRY* ) * num_hosts );
if( !table )
crash_and_burn( "Can't malloc array of hosts" );
cursor = ev_first;
for( num_jobs = 0; num_jobs < num_hosts; num_jobs++ )
{
table[num_jobs] = cursor;
cursor->i = num_jobs;
/* as long as we're here, put this in so names print out nicely */
if( count_flag || loop_flag )
{
n = max_hostname_len - strlen( cursor->host );
buf = ( char* ) malloc( n + 1 );
if( !buf )
crash_and_burn( "can't malloc host pad" );
for ( i = 0; i < n; i++ )
buf[i] = ' ';
buf[n] = '\0';
cursor->pad = buf;
}/* IF */
cursor=cursor->ev_next;
}/* FOR */
init_ping_buffer(ping_data_size);
signal( SIGINT, finish );
gettimeofday( &start_time, &tz );
current_time = start_time;
if( report_interval )
last_report_time = start_time;
last_send_time.tv_sec = current_time.tv_sec - 10000;
#if defined( DEBUG ) || defined( _DEBUG )
if( randomly_lose_flag )
srandom( start_time.tv_usec );
#endif /* DEBUG || _DEBUG */
seqmap_init();
/* main loop */
main_loop();
finish();
return 0;
} /* main() */
void add_cidr(char *addr)
{
char *addr_end;
char *mask_str;
unsigned long mask;
unsigned long bitmask;
int ret;
struct addrinfo addr_hints;
struct addrinfo *addr_res;
unsigned long net_addr;
unsigned long net_last;
/* Split address from mask */
addr_end = strchr(addr, '/');
if(addr_end == NULL) {
usage(1);
}
*addr_end = '\0';
mask_str = addr_end + 1;
mask = atoi(mask_str);
/* parse address (IPv4 only) */
memset(&addr_hints, 0, sizeof(struct addrinfo));
addr_hints.ai_family = AF_UNSPEC;
addr_hints.ai_flags = AI_NUMERICHOST;
ret = getaddrinfo(addr, NULL, &addr_hints, &addr_res);
if(ret) {
fprintf(stderr, "Error: can't parse address %s: %s\n", addr, gai_strerror(ret));
exit(1);
}
if(addr_res->ai_family != AF_INET) {
fprintf(stderr, "Error: -g works only with IPv4 addresses\n");
exit(1);
}
net_addr = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr);
/* check mask */
if(mask < 1 || mask > 30) {
fprintf(stderr, "Error: netmask must be between 1 and 30 (is: %s)\n", mask_str);
exit(1);
}
/* convert mask integer from 1 to 32 to a bitmask */
bitmask = ((unsigned long) 0xFFFFFFFF) << (32-mask);
/* calculate network range */
net_addr &= bitmask;
net_last = net_addr + ((unsigned long) 0x1 << (32-mask)) - 1;
/* add all hosts in that network (excluding network and broadcast address) */
while(++net_addr < net_last) {
struct in_addr in_addr_tmp;
char buffer[20];
in_addr_tmp.s_addr = htonl(net_addr);
inet_ntop(AF_INET, &in_addr_tmp, buffer, sizeof(buffer));
add_name(buffer);
}
freeaddrinfo(addr_res);
}
void add_range(char *start, char *end)
{
struct addrinfo addr_hints;
struct addrinfo *addr_res;
unsigned long start_long;
unsigned long end_long;
int ret;
/* parse start address (IPv4 only) */
memset(&addr_hints, 0, sizeof(struct addrinfo));
addr_hints.ai_family = AF_UNSPEC;
addr_hints.ai_flags = AI_NUMERICHOST;
ret = getaddrinfo(start, NULL, &addr_hints, &addr_res);
if(ret) {
fprintf(stderr, "Error: can't parse address %s: %s\n", start, gai_strerror(ret));
exit(1);
}
if(addr_res->ai_family != AF_INET) {
fprintf(stderr, "Error: -g works only with IPv4 addresses\n");
exit(1);
}
start_long = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr);
/* parse end address (IPv4 only) */
memset(&addr_hints, 0, sizeof(struct addrinfo));
addr_hints.ai_family = AF_UNSPEC;
addr_hints.ai_flags = AI_NUMERICHOST;
ret = getaddrinfo(end, NULL, &addr_hints, &addr_res);
if(ret) {
fprintf(stderr, "Error: can't parse address %s: %s\n", end, gai_strerror(ret));
exit(1);
}
if(addr_res->ai_family != AF_INET) {
fprintf(stderr, "Error: -g works only with IPv4 addresses\n");
exit(1);
}
end_long = ntohl(((struct sockaddr_in *) addr_res->ai_addr)->sin_addr.s_addr);
/* generate */
while(start_long <= end_long) {
struct in_addr in_addr_tmp;
char buffer[20];
in_addr_tmp.s_addr = htonl(start_long);
inet_ntop(AF_INET, &in_addr_tmp, buffer, sizeof(buffer));
add_name(buffer);
start_long++;
}
}
void main_loop()
{
long lt;
long wait_time;
HOST_ENTRY *h;
while(ev_first) {
/* Any event that can be processed now ? */
if(ev_first->ev_time.tv_sec < current_time.tv_sec ||
(ev_first->ev_time.tv_sec == current_time.tv_sec &&
ev_first->ev_time.tv_usec < current_time.tv_usec))
{
/* Event type: ping */
if(ev_first->ev_type == EV_TYPE_PING) {
/* Make sure that we don't ping more than once every "interval" */
lt = timeval_diff( ¤t_time, &last_send_time );
if(lt < interval) goto wait_for_reply;
/* Dequeue the event */
h = ev_dequeue();
/* Send the ping */
/*printf("Sending ping after %d ms\n", lt/100); */
send_ping(s, h);
/* Check what needs to be done next */
if(!loop_flag && !count_flag) {
/* Normal mode: schedule retry */
if(h->waiting < retry + 1) {
h->ev_type = EV_TYPE_PING;
h->ev_time.tv_sec = last_send_time.tv_sec;
h->ev_time.tv_usec = last_send_time.tv_usec;
timeval_add(&h->ev_time, h->timeout);
ev_enqueue(h);