-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathtracepath.c
More file actions
844 lines (745 loc) · 18.9 KB
/
Copy pathtracepath.c
File metadata and controls
844 lines (745 loc) · 18.9 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
/*
* tracepath.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*/
#define _GNU_SOURCE
#include <assert.h>
#include <stddef.h>
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
/*
* Keep linux/ includes after standard headers.
* https://github.com/iputils/iputils/issues/168
*/
#include <linux/errqueue.h>
#include <linux/icmp.h>
#include <linux/icmpv6.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/types.h>
#include "iputils_common.h"
#ifdef USE_IDN
# define getnameinfo_flags NI_IDN
#else
# define getnameinfo_flags 0
#endif
enum {
MAX_PROBES = 10,
MAX_HOPS_DEFAULT = 30,
MAX_HOPS_LIMIT = 255,
HOST_COLUMN_SIZE = 52,
HIS_ARRAY_SIZE = 64,
DEFAULT_OVERHEAD_IPV4 = 28,
DEFAULT_OVERHEAD_IPV6 = 48,
MAX_ICMP_MSG_SIZE = 1280,
DEFAULT_MTU_IPV4 = 65535,
DEFAULT_MTU_IPV6 = 128000,
DEFAULT_BASEPORT = 44444,
ANCILLARY_DATA_LEN = 512,
};
struct hhistory {
int hops;
struct timespec sendtime;
};
struct probehdr {
union {
struct {
uint32_t ttl;
struct timespec ts;
};
char buf[MAX_ICMP_MSG_SIZE];
};
};
struct run_state {
struct hhistory his[HIS_ARRAY_SIZE];
int hisptr;
struct sockaddr_storage target;
struct addrinfo *ai;
int socket_fd;
socklen_t targetlen;
uint16_t base_port;
uint8_t ttl;
int max_hops;
int overhead;
int mtu;
void *pktbuf;
int hops_to;
int hops_from;
unsigned int
no_resolve:1,
show_both:1,
mapped:1,
show_extensions:1;
};
#ifdef SO_EE_RFC4884_FLAG_INVALID
enum {
ICMP_EXT_OBJ_CLASS_IIO = 2,
};
/*
* RFC 5837: Interface Information Object
*/
#define ICMP_EXT_CTYPE_IIO_ROLE(ROLE) (((ROLE) >> 6) & 0x03)
#define ICMP_EXT_CTYPE_IIO_MTU (1U << (0))
#define ICMP_EXT_CTYPE_IIO_NAME (1U << (1))
#define ICMP_EXT_CTYPE_IIO_IPADDR (1U << (2))
#define ICMP_EXT_CTYPE_IIO_IFINDEX (1U << (3))
#ifndef ICMP_AFI_IP
#define ICMP_AFI_IP 1
#endif
#ifndef ICMP_AFI_IP6
#define ICMP_AFI_IP6 2
#endif
struct icmp_ext_iio_name_subobj {
uint8_t len;
char name[];
};
#endif
/*
* All includes, definitions, struct declarations, and global variables are
* above. After this comment all you can find is functions.
*/
static void data_wait(struct run_state const *const ctl)
{
fd_set fds;
struct timeval tv = {
.tv_sec = 1,
.tv_usec = 0
};
FD_ZERO(&fds);
FD_SET(ctl->socket_fd, &fds);
select(ctl->socket_fd + 1, &fds, NULL, NULL, &tv);
}
static void print_host(struct run_state const *const ctl, char const *const a,
char const *const b)
{
int plen;
plen = printf("%s", a);
if (ctl->show_both)
plen += printf(" (%s)", b);
if (plen >= HOST_COLUMN_SIZE)
plen = HOST_COLUMN_SIZE - 1;
printf("%*s", HOST_COLUMN_SIZE - plen, "");
}
#ifdef SO_EE_RFC4884_FLAG_INVALID
static inline int get_u16(uint16_t *res, uint8_t *addr, unsigned int *off,
unsigned int len)
{
if (*off + sizeof(*res) > len)
return 1;
*res = ntohs(*((uint16_t *)(addr + *off)));
*off += sizeof(*res);
return 0;
}
static inline int get_u32(uint32_t *res, uint8_t *addr, unsigned int *off,
unsigned int len)
{
if (*off + sizeof(*res) > len)
return 1;
*res = ntohl(*((uint32_t *)(addr + *off)));
*off += sizeof(*res);
return 0;
}
static void print_iio(const struct icmp_extobj_hdr *objh)
{
const char *roles[] = {"inc", "sub", "out", "nxt"};
uint16_t obj_len = ntohs(objh->length);
uint8_t *pext = (uint8_t *)objh;
unsigned int offset = 0;
printf(" <type=%u: ", objh->class_num);
printf("role=%s", roles[ICMP_EXT_CTYPE_IIO_ROLE(objh->class_type)]);
offset += sizeof(*objh);
if (objh->class_type & ICMP_EXT_CTYPE_IIO_IFINDEX) {
uint32_t ifindex;
if (get_u32(&ifindex, pext, &offset, obj_len))
goto err;
printf(",ifindex=%u", ifindex);
}
if (objh->class_type & ICMP_EXT_CTYPE_IIO_IPADDR) {
char ipaddr[INET6_ADDRSTRLEN] = "";
uint16_t afi, reserved;
size_t addr_size;
int af;
if (get_u16(&afi, pext, &offset, obj_len) ||
get_u16(&reserved, pext, &offset, obj_len))
goto err;
switch (afi) {
case ICMP_AFI_IP:
af = AF_INET;
addr_size = sizeof(struct in_addr);
break;
case ICMP_AFI_IP6:
af = AF_INET6;
addr_size = sizeof(struct in6_addr);
break;
default:
goto err;
}
if (offset + addr_size > obj_len)
goto err;
inet_ntop(af, pext + offset, ipaddr, sizeof(ipaddr));
printf(",ip=%s", ipaddr);
offset += addr_size;
}
if (objh->class_type & ICMP_EXT_CTYPE_IIO_NAME) {
struct icmp_ext_iio_name_subobj *name_subobj;
if (offset + sizeof(uint8_t) > obj_len)
goto err;
name_subobj =
(struct icmp_ext_iio_name_subobj *)(pext + offset);
if (offset + name_subobj->len > obj_len)
goto err;
printf(",name=%.*s", name_subobj->len, name_subobj->name);
offset += name_subobj->len;
}
if (objh->class_type & ICMP_EXT_CTYPE_IIO_MTU) {
uint32_t mtu;
if (get_u32(&mtu, pext, &offset, obj_len))
goto err;
printf(",mtu=%u", mtu);
}
printf(">\n");
return;
err:
printf(_("<Malformed interface information extension>\n"));
}
static void print_extension(const char *buf, unsigned int recv_size,
unsigned int offset)
{
const struct icmp_ext_hdr *hdr;
if (offset + sizeof(*hdr) > recv_size) {
printf(_("Unable to read the ICMP extension header\n"));
return;
}
hdr = (const struct icmp_ext_hdr *) &buf[offset];
if (hdr->version != 2) {
printf(_("Invalid extension version\n"));
return;
}
offset += sizeof(*hdr);
while (offset + sizeof(struct icmp_extobj_hdr) <= recv_size) {
const struct icmp_extobj_hdr *objh;
uint16_t obj_len;
objh = (const struct icmp_extobj_hdr *) &buf[offset];
obj_len = ntohs(objh->length);
if (obj_len + offset > recv_size || obj_len < sizeof(*objh)) {
printf(_("Invalid object length\n"));
return;
}
switch (objh->class_num) {
case ICMP_EXT_OBJ_CLASS_IIO:
print_iio(objh);
break;
default:
printf(_("<unknown object class number (%u)>\n"),
objh->class_num);
break;
}
offset += obj_len;
}
}
#endif
static int recverr(struct run_state *const ctl)
{
ssize_t recv_size;
struct probehdr rcvbuf;
char cbuf[ANCILLARY_DATA_LEN];
struct cmsghdr *cmsg;
struct sock_extended_err *e;
struct sockaddr_storage addr;
struct timespec ts;
struct timespec *retts;
int slot = 0;
int rethops;
int sndhops;
int progress = -1;
int broken_router;
char hnamebuf[NI_MAXHOST] = "";
struct iovec iov = {
.iov_base = &rcvbuf,
.iov_len = sizeof(rcvbuf)
};
struct msghdr msg;
const struct msghdr reset = {
.msg_name = (uint8_t *)&addr,
.msg_namelen = sizeof(addr),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cbuf,
.msg_controllen = sizeof(cbuf),
0
};
bool restart;
restart:
memset(&rcvbuf, -1, sizeof(rcvbuf));
msg = reset;
clock_gettime(CLOCK_MONOTONIC, &ts);
recv_size = recvmsg(ctl->socket_fd, &msg, MSG_ERRQUEUE);
if (recv_size < 0) {
if (errno == EAGAIN)
return progress;
goto restart;
}
progress = ctl->mtu;
rethops = -1;
sndhops = -1;
e = NULL;
retts = NULL;
broken_router = 0;
slot = -ctl->base_port;
switch (ctl->ai->ai_family) {
case AF_INET6:
slot += ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
break;
case AF_INET:
slot += ntohs(((struct sockaddr_in *)&addr)->sin_port);
break;
}
if (slot >= 0 && slot < (HIS_ARRAY_SIZE - 1) && ctl->his[slot].hops) {
sndhops = ctl->his[slot].hops;
retts = &ctl->his[slot].sendtime;
ctl->his[slot].hops = 0;
}
if (recv_size >= (ssize_t)(offsetof(struct probehdr, ts) + sizeof(rcvbuf.ts))) {
if (rcvbuf.ttl == 0 || (rcvbuf.ts.tv_sec == 0 && rcvbuf.ts.tv_nsec == 0))
broken_router = 1;
else {
sndhops = rcvbuf.ttl;
retts = &rcvbuf.ts;
}
}
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
switch (cmsg->cmsg_level) {
case SOL_IPV6:
switch (cmsg->cmsg_type) {
case IPV6_RECVERR:
e = (struct sock_extended_err *)CMSG_DATA(cmsg);
break;
case IPV6_HOPLIMIT:
#ifdef IPV6_2292HOPLIMIT
case IPV6_2292HOPLIMIT:
#endif
memcpy(&rethops, CMSG_DATA(cmsg), sizeof(rethops));
break;
default:
printf(_("cmsg6:%d\n "), cmsg->cmsg_type);
}
break;
case SOL_IP:
switch (cmsg->cmsg_type) {
case IP_RECVERR:
e = (struct sock_extended_err *)CMSG_DATA(cmsg);
break;
case IP_TTL:
rethops = *(uint8_t *)CMSG_DATA(cmsg);
break;
default:
printf(_("cmsg4:%d\n "), cmsg->cmsg_type);
}
}
}
if (e == NULL) {
printf(_("no info\n"));
return 0;
}
if (e->ee_origin == SO_EE_ORIGIN_LOCAL)
printf("%2d?: %-32s ", ctl->ttl, _("[LOCALHOST]"));
else if (e->ee_origin == SO_EE_ORIGIN_ICMP6 ||
e->ee_origin == SO_EE_ORIGIN_ICMP) {
char abuf[NI_MAXHOST];
struct sockaddr *sa = (struct sockaddr *)(e + 1);
socklen_t salen;
if (sndhops > 0)
printf("%2d: ", sndhops);
else
printf("%2d?: ", ctl->ttl);
switch (sa->sa_family) {
case AF_INET6:
salen = sizeof(struct sockaddr_in6);
break;
case AF_INET:
salen = sizeof(struct sockaddr_in);
break;
default:
salen = 0;
}
if (ctl->no_resolve || ctl->show_both) {
if (getnameinfo(sa, salen, abuf, sizeof(abuf), NULL, 0,
NI_NUMERICHOST))
strcpy(abuf, "???");
} else
abuf[0] = 0;
if (!ctl->no_resolve || ctl->show_both) {
fflush(stdout);
if (getnameinfo(sa, salen, hnamebuf, sizeof hnamebuf, NULL, 0,
getnameinfo_flags))
strcpy(hnamebuf, "???");
} else
hnamebuf[0] = 0;
if (ctl->no_resolve)
print_host(ctl, abuf, hnamebuf);
else
print_host(ctl, hnamebuf, abuf);
}
if (retts) {
struct timespec res;
timespecsub(&ts, retts, &res);
printf(_("%3lld.%03ldms "), (long long int)res.tv_sec * 1000
+ res.tv_nsec / 1000000, (res.tv_nsec % 1000000) / 1000);
if (broken_router)
printf(_("(This broken router returned corrupted payload) "));
}
if (rethops <= 64)
rethops = 65 - rethops;
else if (rethops <= 128)
rethops = 129 - rethops;
else
rethops = 256 - rethops;
restart = false;
switch (e->ee_errno) {
case ETIMEDOUT:
printf("\n");
restart = true;
break;
case EMSGSIZE:
printf(_("pmtu %d\n"), e->ee_info);
ctl->mtu = e->ee_info;
progress = ctl->mtu;
restart = true;
break;
case ECONNREFUSED:
printf(_("reached\n"));
ctl->hops_to = sndhops < 0 ? ctl->ttl : sndhops;
ctl->hops_from = rethops;
break;
case EPROTO:
printf("!P\n");
break;
case EHOSTUNREACH:
if ((e->ee_origin == SO_EE_ORIGIN_ICMP &&
e->ee_type == ICMP_TIME_EXCEEDED &&
e->ee_code == ICMP_EXC_TTL) ||
(e->ee_origin == SO_EE_ORIGIN_ICMP6 &&
e->ee_type == ICMPV6_TIME_EXCEED &&
e->ee_code == ICMPV6_EXC_HOPLIMIT)) {
if (rethops >= 0) {
if ((sndhops >= 0 && rethops != sndhops) ||
(sndhops < 0 && rethops != ctl->ttl))
printf(_("asymm %2d "), rethops);
}
printf("\n");
restart = true;
break;
}
printf("!H\n");
break;
case ENETUNREACH:
printf("!N\n");
break;
case EACCES:
printf("!A\n");
break;
default:
printf("\n");
error(0, e->ee_errno, _("NET ERROR"));
break;
}
if (ctl->show_extensions) {
#ifdef SO_EE_RFC4884_FLAG_INVALID
if (!(e->ee_rfc4884.flags & SO_EE_RFC4884_FLAG_INVALID)) {
if (e->ee_rfc4884.len)
print_extension(rcvbuf.buf, recv_size,
e->ee_rfc4884.len);
} else {
printf(_("ICMP extension is invalid\n"));
}
#endif
}
if (restart)
goto restart;
return 0;
}
static int probe_ttl(struct run_state *const ctl)
{
int i;
struct probehdr *hdr = ctl->pktbuf;
memset(ctl->pktbuf, 0, ctl->mtu);
restart:
for (i = 0; i < MAX_PROBES; i++) {
int res;
hdr->ttl = ctl->ttl;
switch (ctl->ai->ai_family) {
case AF_INET6:
((struct sockaddr_in6 *)&ctl->target)->sin6_port =
htons(ctl->base_port + ctl->hisptr);
break;
case AF_INET:
((struct sockaddr_in *)&ctl->target)->sin_port =
htons(ctl->base_port + ctl->hisptr);
break;
}
clock_gettime(CLOCK_MONOTONIC, &hdr->ts);
ctl->his[ctl->hisptr].hops = ctl->ttl;
ctl->his[ctl->hisptr].sendtime = hdr->ts;
if (sendto(ctl->socket_fd, ctl->pktbuf, ctl->mtu - ctl->overhead, 0,
(struct sockaddr *)&ctl->target, ctl->targetlen) > 0)
break;
res = recverr(ctl);
ctl->his[ctl->hisptr].hops = 0;
if (res == 0)
return 0;
if (res > 0)
goto restart;
}
ctl->hisptr = (ctl->hisptr + 1) & (HIS_ARRAY_SIZE - 1);
if (i < MAX_PROBES) {
data_wait(ctl);
if (recv(ctl->socket_fd, ctl->pktbuf, ctl->mtu, MSG_DONTWAIT) > 0) {
printf(_("%2d?: reply received 8)\n"), ctl->ttl);
return 0;
}
return recverr(ctl);
}
printf(_("%2d: send failed\n"), ctl->ttl);
return 0;
}
static void usage(void)
{
fprintf(stderr, _(
"\nUsage\n"
" tracepath [options] <destination>\n"
"\nOptions:\n"
" -4 use IPv4\n"
" -6 use IPv6\n"
" -b print both name and IP\n"
" -l <length> use packet <length>\n"
" -m <hops> use maximum <hops>\n"
" -n no reverse DNS name resolution\n"
" -p <port> use destination <port>\n"
" -e show ICMP extensions (if present)\n"
" -V print version and exit\n"
" <destination> DNS name or IP address\n"
"\nFor more details see tracepath(8).\n"));
exit(-1);
}
int main(int argc, char **argv)
{
struct run_state ctl = {
.max_hops = MAX_HOPS_DEFAULT,
.hops_to = -1,
.hops_from = -1,
0
};
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = IPPROTO_UDP,
#ifdef USE_IDN
.ai_flags = AI_IDN | AI_CANONNAME,
#endif
};
struct addrinfo *result;
int ch;
int status;
int on;
char *p;
char pbuf[NI_MAXSERV];
atexit(close_stdout);
#if defined(USE_IDN) || defined(ENABLE_NLS)
setlocale(LC_ALL, "");
#ifdef ENABLE_NLS
bindtextdomain (PACKAGE_NAME, LOCALEDIR);
textdomain (PACKAGE_NAME);
#endif
#endif
/* Support being called using `tracepath4` or `tracepath6` symlinks */
if (argv[0][strlen(argv[0]) - 1] == '4')
hints.ai_family = AF_INET;
else if (argv[0][strlen(argv[0]) - 1] == '6')
hints.ai_family = AF_INET6;
while ((ch = getopt(argc, argv, "46nbh?l:m:p:eV")) != EOF) {
switch (ch) {
case '4':
if (hints.ai_family == AF_INET6)
error(2, 0, _("Only one -4 or -6 option may be specified"));
hints.ai_family = AF_INET;
break;
case '6':
if (hints.ai_family == AF_INET)
error(2, 0, _("Only one -4 or -6 option may be specified"));
hints.ai_family = AF_INET6;
break;
case 'n':
ctl.no_resolve = 1;
break;
case 'b':
ctl.show_both = 1;
break;
case 'l':
ctl.mtu = strtol_or_err(optarg, _("invalid argument"), 0, INT_MAX);
break;
case 'm':
ctl.max_hops = strtol_or_err(optarg, _("invalid argument"), 0, MAX_HOPS_LIMIT);
break;
case 'p':
ctl.base_port = strtol_or_err(optarg, _("invalid argument"), 0, UINT16_MAX);
break;
case 'e':
ctl.show_extensions = 1;
break;
case 'V':
printf(IPUTILS_VERSION("tracepath"));
print_config();
return 0;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
/* Backward compatibility */
if (!ctl.base_port) {
p = strchr(argv[0], '/');
if (p) {
*p = 0;
ctl.base_port = strtol_or_err(p + 1, _("invalid argument"), 0, UINT16_MAX);
} else
ctl.base_port = DEFAULT_BASEPORT;
}
sprintf(pbuf, "%u", ctl.base_port);
status = getaddrinfo(argv[0], pbuf, &hints, &result);
if (status || !result) {
error(1, 0, "%s: %s", argv[0], gai_strerror(status));
abort();
}
for (ctl.ai = result; ctl.ai; ctl.ai = ctl.ai->ai_next) {
assert(ctl.ai != NULL);
if (ctl.ai->ai_family != AF_INET6 && ctl.ai->ai_family != AF_INET)
continue;
ctl.socket_fd = socket(ctl.ai->ai_family, ctl.ai->ai_socktype, ctl.ai->ai_protocol);
if (ctl.socket_fd < 0)
continue;
memcpy(&ctl.target, ctl.ai->ai_addr, ctl.ai->ai_addrlen);
ctl.targetlen = ctl.ai->ai_addrlen;
break;
}
if (ctl.socket_fd < 0)
error(1, errno, "socket/connect");
switch (ctl.ai->ai_family) {
case AF_INET6:
ctl.overhead = DEFAULT_OVERHEAD_IPV6;
if (!ctl.mtu)
ctl.mtu = DEFAULT_MTU_IPV6;
if (ctl.mtu <= ctl.overhead)
goto pktlen_error;
on = IPV6_PMTUDISC_PROBE;
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_MTU_DISCOVER, &on, sizeof(on)) &&
(on = IPV6_PMTUDISC_DO, setsockopt(ctl.socket_fd, SOL_IPV6,
IPV6_MTU_DISCOVER, &on, sizeof(on))))
error(1, errno, "IPV6_MTU_DISCOVER");
on = 1;
#ifdef IPV6_RECVERR_RFC4884
if (ctl.show_extensions &&
setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_RECVERR_RFC4884,
&on, sizeof(on)))
error(1, errno, "IPV6_RECVERR_RFC4884");
#endif
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_RECVERR, &on, sizeof(on)))
error(1, errno, "IPV6_RECVERR");
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_HOPLIMIT, &on, sizeof(on))
#ifdef IPV6_RECVHOPLIMIT
&& setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_2292HOPLIMIT, &on, sizeof(on))
#endif
)
error(1, errno, "IPV6_HOPLIMIT");
if (!IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)&ctl.target)->sin6_addr)))
break;
ctl.mapped = 1;
/*FALLTHROUGH*/
case AF_INET:
ctl.overhead = DEFAULT_OVERHEAD_IPV4;
if (!ctl.mtu)
ctl.mtu = DEFAULT_MTU_IPV4;
if (ctl.mtu <= ctl.overhead)
goto pktlen_error;
on = IP_PMTUDISC_PROBE;
if (setsockopt(ctl.socket_fd, SOL_IP, IP_MTU_DISCOVER, &on, sizeof(on)))
error(1, errno, "IP_MTU_DISCOVER");
on = 1;
#ifdef IP_RECVERR_RFC4884
if (ctl.show_extensions &&
setsockopt(ctl.socket_fd, SOL_IP, IP_RECVERR_RFC4884, &on,
sizeof(on)))
error(1, errno, "IP_RECVERR_RFC4884");
#endif
if (setsockopt(ctl.socket_fd, SOL_IP, IP_RECVERR, &on, sizeof(on)))
error(1, errno, "IP_RECVERR");
if (setsockopt(ctl.socket_fd, SOL_IP, IP_RECVTTL, &on, sizeof(on)))
error(1, errno, "IP_RECVTTL");
}
ctl.pktbuf = malloc(ctl.mtu);
if (!ctl.pktbuf)
error(1, errno, "malloc");
for (ctl.ttl = 1; ctl.ttl <= ctl.max_hops; ctl.ttl++) {
int res = -1;
int i;
on = ctl.ttl;
switch (ctl.ai->ai_family) {
case AF_INET6:
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_UNICAST_HOPS, &on, sizeof(on)))
error(1, errno, "IPV6_UNICAST_HOPS");
if (!ctl.mapped)
break;
/*FALLTHROUGH*/
case AF_INET:
if (setsockopt(ctl.socket_fd, SOL_IP, IP_TTL, &on, sizeof(on)))
error(1, errno, "IP_TTL");
}
restart:
for (i = 0; i < 3; i++) {
int old_mtu;
old_mtu = ctl.mtu;
res = probe_ttl(&ctl);
if (ctl.mtu != old_mtu)
goto restart;
if (res == 0)
goto done;
if (res > 0)
break;
}
if (res < 0)
printf(_("%2d: no reply\n"), ctl.ttl);
}
printf(_(" Too many hops: pmtu %d\n"), ctl.mtu);
done:
freeaddrinfo(result);
printf(_(" Resume: pmtu %d "), ctl.mtu);
if (ctl.hops_to >= 0)
printf(_("hops %d "), ctl.hops_to);
if (ctl.hops_from >= 0)
printf(_("back %d "), ctl.hops_from);
printf("\n");
exit(0);
pktlen_error:
error(1, 0, _("pktlen must be within: %d < value <= %d"), ctl.overhead, INT_MAX);
}