-
Notifications
You must be signed in to change notification settings - Fork 932
Expand file tree
/
Copy pathping.c
More file actions
2277 lines (2073 loc) · 56.7 KB
/
ping.c
File metadata and controls
2277 lines (2073 loc) · 56.7 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
/* $OpenBSD: ping.c,v 1.251 2025/12/06 10:41:07 phessler Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Muuss.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
* measure round-trip-delays and packet loss across network paths.
*
* Author -
* Mike Muuss
* U. S. Army Ballistic Research Laboratory
* December, 1983
*
* Status -
* Public Domain. Distribution Unlimited.
* Bugs -
* More statistics could always be gathered.
* This program has to run SUID to ROOT to access the ICMP socket.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet/ip_ah.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <siphash.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
struct tv64 {
u_int64_t tv64_sec;
u_int64_t tv64_nsec;
};
struct payload {
struct tv64 tv64;
u_int8_t mac[SIPHASH_DIGEST_LENGTH];
};
#define ECHOLEN 8 /* icmp echo header len excluding time */
#define ECHOTMLEN sizeof(struct payload)
#define DEFDATALEN (64 - ECHOLEN) /* default data length */
#define MAXIPLEN 60
#define MAXICMPLEN 76
#define MAXPAYLOAD (IP_MAXPACKET - MAXIPLEN - ECHOLEN)
#define IP6LEN 40
#define EXTRA 256 /* for AH and various other headers. weird. */
#define MAXPAYLOAD6 IPV6_MAXPACKET - IP6LEN - ECHOLEN
#define MAXWAIT_DEFAULT 10 /* secs to wait for response */
#define NROUTES 9 /* number of record route slots */
#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
#define SET(bit) (A(bit) |= B(bit))
#define CLR(bit) (A(bit) &= (~B(bit)))
#define TST(bit) (A(bit) & B(bit))
/* various options */
int options;
#define F_FLOOD 0x0001
#define F_INTERVAL 0x0002
#define F_HOSTNAME 0x0004
#define F_PINGFILLED 0x0008
#define F_QUIET 0x0010
#define F_RROUTE 0x0020
#define F_SO_DEBUG 0x0040
#define F_SHOWCHAR 0x0080
#define F_VERBOSE 0x0100
/* 0x0200 */
#define F_HDRINCL 0x0400
#define F_TTL 0x0800
#define F_TOS 0x1000
#define F_AUD_RECV 0x2000
#define F_AUD_MISS 0x4000
/* multicast options */
int moptions;
#define MULTICAST_NOLOOP 0x001
#define MULTICAST_TTL 0x002
#define DUMMY_PORT 10101
#define PING_USER "_ping"
/*
* MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
* number of received sequence numbers we can keep track of. Change 128
* to 8192 for complete accuracy...
*/
#define MAX_DUP_CHK (8 * 8192)
int mx_dup_ck = MAX_DUP_CHK;
char rcvd_tbl[MAX_DUP_CHK / 8];
int datalen = DEFDATALEN;
int maxpayload = MAXPAYLOAD;
u_char outpackhdr[IP_MAXPACKET+sizeof(struct ip)];
u_char *outpack = outpackhdr+sizeof(struct ip);
char BSPACE = '\b'; /* characters written for flood */
char DOT = '.';
char *hostname;
int ident; /* random number to identify our packets */
int v6flag; /* are we ping6? */
/* counters */
int64_t npackets; /* max packets to transmit */
int64_t nreceived; /* # of packets we got back */
int64_t nrepeats; /* number of duplicates */
int64_t ntransmitted; /* sequence # for outbound packets = #sent */
int64_t nmissedmax = 1; /* max value of ntransmitted - nreceived - 1 */
struct timeval interval = {1, 0}; /* interval between packets */
/* timing */
int timing; /* flag to do timing */
int timinginfo;
unsigned int maxwait = MAXWAIT_DEFAULT; /* max seconds to wait for response */
double tmin = 999999999.0; /* minimum round trip time */
double tmax; /* maximum round trip time */
double tsum; /* sum of all times, for doing average */
double tsumsq; /* sum of all times squared, for std. dev. */
struct tv64 tv64_offset;
SIPHASH_KEY mac_key;
struct msghdr smsghdr;
struct iovec smsgiov;
volatile sig_atomic_t seenalrm;
volatile sig_atomic_t seenint;
volatile sig_atomic_t seeninfo;
void fill(char *, char *);
void summary(void);
void onsignal(int);
void retransmit(int);
int pinger(int);
const char *pr_addr(struct sockaddr *, socklen_t);
void pr_pack(u_char *, int, struct msghdr *);
__dead void usage(void);
/* IPv4 specific functions */
void pr_ipopt(int, u_char *);
int in_cksum(u_short *, int);
void pr_icmph(struct icmp *);
void pr_retip(struct ip *);
void pr_iph(struct ip *);
#ifndef SMALL
int map_tos(char *, int *);
#endif /* SMALL */
/* IPv6 specific functions */
int get_hoplim(struct msghdr *);
int get_pathmtu(struct msghdr *, struct sockaddr_in6 *);
void pr_icmph6(struct icmp6_hdr *, u_char *);
void pr_iph6(struct ip6_hdr *);
void pr_exthdrs(struct msghdr *);
void pr_ip6opt(void *);
void pr_rthdr(void *);
void pr_retip6(struct ip6_hdr *, u_char *);
int
main(int argc, char *argv[])
{
struct addrinfo hints, *res;
struct itimerval itimer;
struct sockaddr *from, *dst;
struct sockaddr_in from4, dst4;
struct sockaddr_in6 from6, dst6;
struct cmsghdr *scmsg = NULL;
struct in6_pktinfo *pktinfo = NULL;
struct icmp6_filter filt;
struct passwd *pw;
socklen_t maxsizelen;
int64_t preload;
int ch, i, optval = 1, packlen, maxsize, error, s, flooddone = 0;
int df = 0, tos = 0, bufspace = IP_MAXPACKET, hoplimit = -1, mflag = 0;
u_char *datap, *packet;
u_char ttl = MAXTTL;
char *e, *target, hbuf[NI_MAXHOST], *source = NULL;
char rspace[3 + 4 * NROUTES + 1]; /* record route space */
const char *errstr;
double fraction, integral, seconds;
uid_t ouid, uid;
gid_t gid;
u_int rtableid = 0;
extern char *__progname;
/* Cannot pledge due to special setsockopt()s below */
if (unveil("/", "r") == -1)
err(1, "unveil /");
if (unveil(NULL, NULL) == -1)
err(1, "unveil");
if (strcmp("ping6", __progname) == 0) {
v6flag = 1;
maxpayload = MAXPAYLOAD6;
if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1)
err(1, "socket");
} else {
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
err(1, "socket");
}
/* revoke privs */
ouid = getuid();
if (ouid == 0 && (pw = getpwnam(PING_USER)) != NULL) {
uid = pw->pw_uid;
gid = pw->pw_gid;
} else {
uid = getuid();
gid = getgid();
}
if (ouid && (setgroups(1, &gid) ||
setresgid(gid, gid, gid) ||
setresuid(uid, uid, uid)))
err(1, "unable to revoke privs");
preload = 0;
datap = &outpack[ECHOLEN + ECHOTMLEN];
while ((ch = getopt(argc, argv, v6flag ?
"c:DdEefgHh:I:i:Ll:mNnp:qS:s:T:V:vw:" :
"DEI:LRS:c:defgHi:l:np:qs:T:t:V:vw:")) != -1) {
switch(ch) {
case 'c':
npackets = strtonum(optarg, 0, INT64_MAX, &errstr);
if (errstr)
errx(1,
"number of packets to transmit is %s: %s",
errstr, optarg);
break;
case 'D':
options |= F_HDRINCL;
df = 1;
break;
case 'd':
options |= F_SO_DEBUG;
break;
case 'E':
options |= F_AUD_MISS;
break;
case 'e':
options |= F_AUD_RECV;
break;
case 'f':
if (ouid)
errc(1, EPERM, NULL);
options |= F_FLOOD;
setvbuf(stdout, NULL, _IONBF, 0);
break;
case 'g':
options |= F_SHOWCHAR;
break;
case 'H':
options |= F_HOSTNAME;
break;
case 'h': /* hoplimit */
hoplimit = strtonum(optarg, 0, IPV6_MAXHLIM, &errstr);
if (errstr)
errx(1, "hoplimit is %s: %s", errstr, optarg);
break;
case 'I':
case 'S': /* deprecated */
source = optarg;
break;
case 'i': /* interval between packets */
seconds = strtod(optarg, &e);
if (*optarg == '\0' || *e != '\0' || seconds < 0.0)
errx(1, "interval is invalid: %s", optarg);
fraction = modf(seconds, &integral);
if (integral > UINT_MAX)
errx(1, "interval is too large: %s", optarg);
interval.tv_sec = integral;
interval.tv_usec = fraction * 1000000.0;
if (!timerisset(&interval))
errx(1, "interval is too small: %s", optarg);
if (interval.tv_sec < 1 && ouid != 0) {
errx(1, "only root may use an interval smaller"
" than one second");
}
options |= F_INTERVAL;
break;
case 'L':
moptions |= MULTICAST_NOLOOP;
break;
case 'l':
if (ouid)
errc(1, EPERM, NULL);
preload = strtonum(optarg, 1, INT64_MAX, &errstr);
if (errstr)
errx(1, "preload value is %s: %s", errstr,
optarg);
break;
case 'm':
mflag++;
break;
case 'n':
options &= ~F_HOSTNAME;
break;
case 'p': /* fill buffer with user pattern */
options |= F_PINGFILLED;
fill((char *)datap, optarg);
break;
case 'q':
options |= F_QUIET;
break;
case 'R':
options |= F_RROUTE;
break;
case 's': /* size of packet to send */
datalen = strtonum(optarg, 0, maxpayload, &errstr);
if (errstr)
errx(1, "packet size is %s: %s", errstr,
optarg);
break;
#ifndef SMALL
case 'T':
options |= F_HDRINCL;
options |= F_TOS;
errno = 0;
errstr = NULL;
if (map_tos(optarg, &tos))
break;
if (strlen(optarg) > 1 && optarg[0] == '0' &&
optarg[1] == 'x')
tos = (int)strtol(optarg, NULL, 16);
else
tos = strtonum(optarg, 0, 255, &errstr);
if (tos < 0 || tos > 255 || errstr || errno)
errx(1, "illegal tos value %s", optarg);
break;
#endif /* SMALL */
case 't':
options |= F_TTL;
ttl = strtonum(optarg, 0, MAXTTL, &errstr);
if (errstr)
errx(1, "ttl value is %s: %s", errstr, optarg);
break;
case 'V':
rtableid = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr);
if (errstr)
errx(1, "rtable value is %s: %s", errstr,
optarg);
if (setsockopt(s, SOL_SOCKET, SO_RTABLE, &rtableid,
sizeof(rtableid)) == -1)
err(1, "setsockopt SO_RTABLE");
break;
case 'v':
options |= F_VERBOSE;
break;
case 'w':
maxwait = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr)
errx(1, "maxwait value is %s: %s",
errstr, optarg);
break;
default:
usage();
}
}
if (ouid == 0 && (setgroups(1, &gid) ||
setresgid(gid, gid, gid) ||
setresuid(uid, uid, uid)))
err(1, "unable to revoke privs");
argc -= optind;
argv += optind;
if (argc != 1)
usage();
memset(&dst4, 0, sizeof(dst4));
memset(&dst6, 0, sizeof(dst6));
target = *argv;
memset(&hints, 0, sizeof(hints));
hints.ai_family = v6flag ? AF_INET6 : AF_INET;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = 0;
hints.ai_flags = AI_CANONNAME;
if ((error = getaddrinfo(target, NULL, &hints, &res)))
errx(1, "%s", gai_strerror(error));
switch (res->ai_family) {
case AF_INET:
dst = (struct sockaddr *)&dst4;
from = (struct sockaddr *)&from4;
break;
case AF_INET6:
dst = (struct sockaddr *)&dst6;
from = (struct sockaddr *)&from6;
break;
default:
errx(1, "unsupported AF: %d", res->ai_family);
break;
}
memcpy(dst, res->ai_addr, res->ai_addrlen);
if (!hostname) {
hostname = res->ai_canonname ? strdup(res->ai_canonname) :
target;
if (!hostname)
err(1, "malloc");
}
if (res->ai_next) {
if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
strlcpy(hbuf, "?", sizeof(hbuf));
warnx("Warning: %s has multiple "
"addresses; using %s", hostname, hbuf);
}
freeaddrinfo(res);
if (source) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = dst->sa_family;
if ((error = getaddrinfo(source, NULL, &hints, &res)))
errx(1, "%s: %s", source, gai_strerror(error));
memcpy(from, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (!v6flag && IN_MULTICAST(ntohl(dst4.sin_addr.s_addr))) {
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
&from4.sin_addr, sizeof(from4.sin_addr)) == -1)
err(1, "setsockopt IP_MULTICAST_IF");
} else {
if (bind(s, from, from->sa_len) == -1)
err(1, "bind");
}
} else if (options & F_VERBOSE) {
/*
* get the source address. XXX since we revoked the root
* privilege, we cannot use a raw socket for this.
*/
int dummy;
socklen_t len = dst->sa_len;
if ((dummy = socket(dst->sa_family, SOCK_DGRAM, 0)) == -1)
err(1, "UDP socket");
memcpy(from, dst, dst->sa_len);
if (v6flag) {
from6.sin6_port = ntohs(DUMMY_PORT);
if (pktinfo &&
setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,
pktinfo, sizeof(*pktinfo)))
err(1, "UDP setsockopt(IPV6_PKTINFO)");
if (hoplimit != -1 &&
setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
&hoplimit, sizeof(hoplimit)))
err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
if (hoplimit != -1 &&
setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&hoplimit, sizeof(hoplimit)))
err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
} else {
u_char loop = 0;
from4.sin_port = ntohs(DUMMY_PORT);
if ((moptions & MULTICAST_NOLOOP) && setsockopt(dummy,
IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
sizeof(loop)) == -1)
err(1, "setsockopt IP_MULTICAST_LOOP");
if ((moptions & MULTICAST_TTL) && setsockopt(dummy,
IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
sizeof(ttl)) == -1)
err(1, "setsockopt IP_MULTICAST_TTL");
}
if (rtableid > 0 &&
setsockopt(dummy, SOL_SOCKET, SO_RTABLE, &rtableid,
sizeof(rtableid)) == -1)
err(1, "setsockopt(SO_RTABLE)");
if (connect(dummy, from, len) == -1)
err(1, "UDP connect");
if (getsockname(dummy, from, &len) == -1)
err(1, "getsockname");
close(dummy);
}
if (options & F_SO_DEBUG)
(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, &optval,
sizeof(optval));
if ((options & F_FLOOD) && (options & F_INTERVAL))
errx(1, "-f and -i options are incompatible");
if ((options & F_FLOOD) && (options & (F_AUD_RECV | F_AUD_MISS)))
warnx("No audible output for flood pings");
if (datalen >= sizeof(struct payload)) /* can we time transfer */
timing = 1;
if (v6flag) {
/* in F_VERBOSE case, we may get non-echoreply packets*/
if ((options & F_VERBOSE) && datalen < 2048) /* XXX 2048? */
packlen = 2048 + IP6LEN + ECHOLEN + EXTRA;
else
packlen = datalen + IP6LEN + ECHOLEN + EXTRA;
} else
packlen = datalen + MAXIPLEN + MAXICMPLEN;
if (!(packet = malloc(packlen)))
err(1, "malloc");
if (!(options & F_PINGFILLED))
for (i = ECHOTMLEN; i < datalen; ++i)
*datap++ = i;
ident = arc4random() & 0xFFFF;
/*
* When trying to send large packets, you must increase the
* size of both the send and receive buffers...
*/
maxsizelen = sizeof maxsize;
if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &maxsize, &maxsizelen) == -1)
err(1, "getsockopt");
if (maxsize < packlen &&
setsockopt(s, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(maxsize)) == -1)
err(1, "setsockopt");
/*
* When pinging the broadcast address, you can get a lot of answers.
* Doing something so evil is useful if you are trying to stress the
* ethernet, or just want to fill the arp cache to get some stuff for
* /etc/ethers.
*/
while (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
&bufspace, sizeof(bufspace)) == -1) {
if ((bufspace -= 1024) <= 0)
err(1, "Cannot set the receive buffer size");
}
if (bufspace < IP_MAXPACKET)
warnx("Could only allocate a receive buffer of %d bytes "
"(default %d)", bufspace, IP_MAXPACKET);
if (v6flag) {
unsigned int loop = 0;
/*
* let the kernel pass extension headers of incoming packets,
* for privileged socket options
*/
if (options & F_VERBOSE) {
int opton = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS,
&opton, sizeof(opton)))
err(1, "setsockopt(IPV6_RECVHOPOPTS)");
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS,
&opton, sizeof(opton)))
err(1, "setsockopt(IPV6_RECVDSTOPTS)");
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR,
&opton, sizeof(opton)))
err(1, "setsockopt(IPV6_RECVRTHDR)");
ICMP6_FILTER_SETPASSALL(&filt);
} else {
ICMP6_FILTER_SETBLOCKALL(&filt);
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
}
if ((moptions & MULTICAST_NOLOOP) &&
setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
&loop, sizeof(loop)) == -1)
err(1, "setsockopt IPV6_MULTICAST_LOOP");
optval = IPV6_DEFHLIM;
if (IN6_IS_ADDR_MULTICAST(&dst6.sin6_addr)) {
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&optval, sizeof(optval)) == -1)
err(1, "IPV6_MULTICAST_HOPS");
}
if (mflag != 1) {
optval = mflag > 1 ? 0 : 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_USE_MIN_MTU)");
} else {
optval = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_RECVPATHMTU)");
}
if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
&filt, sizeof(filt)) == -1)
err(1, "setsockopt(ICMP6_FILTER)");
if (hoplimit != -1) {
/* set IP6 packet options */
if ((scmsg = malloc( CMSG_SPACE(sizeof(int)))) == NULL)
err(1, "malloc");
smsghdr.msg_control = (caddr_t)scmsg;
smsghdr.msg_controllen = CMSG_SPACE(sizeof(int));
scmsg->cmsg_len = CMSG_LEN(sizeof(int));
scmsg->cmsg_level = IPPROTO_IPV6;
scmsg->cmsg_type = IPV6_HOPLIMIT;
*(int *)(CMSG_DATA(scmsg)) = hoplimit;
}
if (options & F_TOS) {
optval = tos;
if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_TCLASS)");
}
if (df) {
optval = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_DONTFRAG,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_DONTFRAG)");
}
optval = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_RECVPKTINFO)");
if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
} else {
u_char loop = 0;
if (options & F_TTL) {
if (IN_MULTICAST(ntohl(dst4.sin_addr.s_addr)))
moptions |= MULTICAST_TTL;
else
options |= F_HDRINCL;
}
if ((options & F_RROUTE) && (options & F_HDRINCL))
errx(1, "-R option and -D or -T, or -t to unicast"
" destinations are incompatible");
if (options & F_HDRINCL) {
struct ip *ip = (struct ip *)outpackhdr;
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL,
&optval, sizeof(optval)) == -1)
err(1, "setsockopt(IP_HDRINCL)");
ip->ip_v = IPVERSION;
ip->ip_hl = sizeof(struct ip) >> 2;
ip->ip_tos = tos;
ip->ip_id = 0;
ip->ip_off = htons(df ? IP_DF : 0);
ip->ip_ttl = ttl;
ip->ip_p = IPPROTO_ICMP;
if (source)
ip->ip_src = from4.sin_addr;
else
ip->ip_src.s_addr = INADDR_ANY;
ip->ip_dst = dst4.sin_addr;
}
/* record route option */
if (options & F_RROUTE) {
if (IN_MULTICAST(ntohl(dst4.sin_addr.s_addr)))
errx(1, "record route not valid to multicast"
" destinations");
memset(rspace, 0, sizeof(rspace));
rspace[IPOPT_OPTVAL] = IPOPT_RR;
rspace[IPOPT_OLEN] = sizeof(rspace)-1;
rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
if (setsockopt(s, IPPROTO_IP, IP_OPTIONS,
rspace, sizeof(rspace)) == -1)
err(1, "record route");
}
if ((moptions & MULTICAST_NOLOOP) &&
setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP,
&loop, sizeof(loop)) == -1)
err(1, "setsockopt IP_MULTICAST_LOOP");
if ((moptions & MULTICAST_TTL) &&
setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
&ttl, sizeof(ttl)) == -1)
err(1, "setsockopt IP_MULTICAST_TTL");
}
if (options & F_HOSTNAME) {
if (pledge("stdio inet dns", NULL) == -1)
err(1, "pledge");
} else {
if (pledge("stdio inet", NULL) == -1)
err(1, "pledge");
}
arc4random_buf(&tv64_offset, sizeof(tv64_offset));
arc4random_buf(&mac_key, sizeof(mac_key));
printf("PING %s (", hostname);
if (options & F_VERBOSE)
printf("%s --> ", pr_addr(from, from->sa_len));
printf("%s): %d data bytes\n", pr_addr(dst, dst->sa_len), datalen);
smsghdr.msg_name = dst;
smsghdr.msg_namelen = dst->sa_len;
smsgiov.iov_base = (caddr_t)outpack;
smsghdr.msg_iov = &smsgiov;
smsghdr.msg_iovlen = 1;
/* Drain our socket. */
(void)signal(SIGALRM, onsignal);
memset(&itimer, 0, sizeof(itimer));
itimer.it_value.tv_sec = 1; /* make sure we don't get stuck */
(void)setitimer(ITIMER_REAL, &itimer, NULL);
for (;;) {
struct msghdr m;
union {
struct cmsghdr hdr;
u_char buf[CMSG_SPACE(1024)];
} cmsgbuf;
struct iovec iov[1];
struct pollfd pfd;
struct sockaddr_in peer4;
struct sockaddr_in6 peer6;
ssize_t cc;
if (seenalrm)
break;
pfd.fd = s;
pfd.events = POLLIN;
if (poll(&pfd, 1, 0) <= 0)
break;
if (v6flag) {
m.msg_name = &peer6;
m.msg_namelen = sizeof(peer6);
} else {
m.msg_name = &peer4;
m.msg_namelen = sizeof(peer4);
}
memset(&iov, 0, sizeof(iov));
iov[0].iov_base = (caddr_t)packet;
iov[0].iov_len = packlen;
m.msg_iov = iov;
m.msg_iovlen = 1;
m.msg_control = (caddr_t)&cmsgbuf.buf;
m.msg_controllen = sizeof(cmsgbuf.buf);
cc = recvmsg(s, &m, 0);
if (cc == -1 && errno != EINTR)
break;
}
memset(&itimer, 0, sizeof(itimer));
(void)setitimer(ITIMER_REAL, &itimer, NULL);
while (preload--) /* Fire off them quickies. */
pinger(s);
(void)signal(SIGINT, onsignal);
(void)signal(SIGINFO, onsignal);
if (!(options & F_FLOOD)) {
(void)signal(SIGALRM, onsignal);
itimer.it_interval = interval;
itimer.it_value = interval;
(void)setitimer(ITIMER_REAL, &itimer, NULL);
if (ntransmitted == 0)
retransmit(s);
}
seenalrm = seenint = 0;
seeninfo = 0;
for (;;) {
struct msghdr m;
union {
struct cmsghdr hdr;
u_char buf[CMSG_SPACE(1024)];
} cmsgbuf;
struct iovec iov[1];
struct pollfd pfd;
struct sockaddr_in peer4;
struct sockaddr_in6 peer6;
ssize_t cc;
int timeout;
/* signal handling */
if (seenint)
break;
if (seenalrm) {
if (flooddone)
break;
retransmit(s);
seenalrm = 0;
if (ntransmitted - nreceived - 1 > nmissedmax) {
nmissedmax = ntransmitted - nreceived - 1;
if (!(options & F_FLOOD) &&
(options & F_AUD_MISS))
fputc('\a', stderr);
if ((options & F_SHOWCHAR) &&
!(options & F_FLOOD)) {
putchar('.');
fflush(stdout);
}
}
continue;
}
if (seeninfo) {
summary();
seeninfo = 0;
continue;
}
if ((options & F_FLOOD && !flooddone)) {
if (pinger(s) != 0) {
(void)signal(SIGALRM, onsignal);
timeout = INFTIM;
memset(&itimer, 0, sizeof(itimer));
if (nreceived) {
itimer.it_value.tv_sec = 2 * tmax /
1000;
if (itimer.it_value.tv_sec == 0)
itimer.it_value.tv_sec = 1;
} else
itimer.it_value.tv_sec = maxwait;
(void)setitimer(ITIMER_REAL, &itimer, NULL);
/* When the alarm goes off we are done. */
flooddone = 1;
} else
timeout = 10;
} else
timeout = INFTIM;
pfd.fd = s;
pfd.events = POLLIN;
if (poll(&pfd, 1, timeout) <= 0)
continue;
if (v6flag) {
m.msg_name = &peer6;
m.msg_namelen = sizeof(peer6);
} else {
m.msg_name = &peer4;
m.msg_namelen = sizeof(peer4);
}
memset(&iov, 0, sizeof(iov));
iov[0].iov_base = (caddr_t)packet;
iov[0].iov_len = packlen;
m.msg_iov = iov;
m.msg_iovlen = 1;
m.msg_control = (caddr_t)&cmsgbuf.buf;
m.msg_controllen = sizeof(cmsgbuf.buf);
cc = recvmsg(s, &m, 0);
if (cc == -1) {
if (errno != EINTR) {
warn("recvmsg");
sleep(1);
}
continue;
} else if (cc == 0) {
int mtu;
/*
* receive control messages only. Process the
* exceptions (currently the only possibility is
* a path MTU notification.)
*/
if ((mtu = get_pathmtu(&m, &dst6)) > 0) {
if (options & F_VERBOSE) {
printf("new path MTU (%d) is "
"notified\n", mtu);
}
}
continue;
} else
pr_pack(packet, cc, &m);
if (npackets && nreceived >= npackets)
break;
}
summary();
exit(nreceived == 0);
}
void
onsignal(int sig)
{
switch (sig) {
case SIGALRM:
seenalrm++;
break;
case SIGINT:
seenint++;
break;
case SIGINFO:
seeninfo++;
break;
}
}
void
fill(char *bp, char *patp)
{
int ii, jj, kk;
int pat[16];
char *cp;
for (cp = patp; *cp; cp++)
if (!isxdigit((unsigned char)*cp))
errx(1, "patterns must be specified as hex digits");
ii = sscanf(patp,
"%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",