a3526331503bd8552cbcbf2812453132d353bd89
3 * Report MTU on path to specified host
5 * (c) 2008 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_icmp.h>
50 #include <netinet/udp.h>
54 #include <sys/ioctl.h>
56 #include <mLib/alloc.h>
57 #include <mLib/bits.h>
58 #include <mLib/dstr.h>
60 #include <mLib/mdwopt.h>
61 #include <mLib/quis.h>
62 #include <mLib/report.h>
65 /*----- Static variables --------------------------------------------------*/
67 static unsigned char buf
[65536];
71 /*----- Utility functions -------------------------------------------------*/
73 /* Step a value according to a simple LFSR. */
75 do (q) = ((q) & 0x8000) ? ((q) << 1) ^ POLY : ((q) << 1); while (0)
77 /* Fill buffer with a constant but pseudorandom string. Uses a simple
80 static void fillbuffer(unsigned char *p
, size_t sz
)
82 unsigned int y
= 0xbc20;
83 const unsigned char *l
= p
+ sz
;
88 for (i
= 0; i
< 8; i
++) STEP(y
);
92 /* Convert a string to floating point. */
93 static double s2f(const char *s
, const char *what
)
100 if (errno
|| *q
) die(EXIT_FAILURE
, "bad %s", what
);
104 /* Convert a floating-point value into a struct timeval. */
105 static void f2tv(struct timeval
*tv
, double t
)
106 { tv
->tv_sec
= t
; tv
->tv_usec
= (t
- tv
->tv_sec
)*MILLION
; }
110 struct sockaddr_in sin
;
111 struct sockaddr_in6 sin6
;
114 /* Check whether an address family is even slightly supported. */
115 static int addrfamok(int af
)
118 case AF_INET
: case AF_INET6
: return (1);
123 /* Return the size of a socket address. */
124 static size_t addrsz(const union addr
*a
)
126 switch (a
->sa
.sa_family
) {
127 case AF_INET
: return (sizeof(a
->sin
));
128 case AF_INET6
: return (sizeof(a
->sin6
));
133 /* Compare two addresses. Maybe compare the port numbers too. */
135 static int addreq(const union addr
*a
, const union addr
*b
, unsigned f
)
137 switch (a
->sa
.sa_family
) {
139 return (a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
&&
140 (!(f
&AEF_PORT
) || a
->sin
.sin_port
== b
->sin
.sin_port
));
142 return (!memcmp(a
->sin6
.sin6_addr
.s6_addr
,
143 b
->sin6
.sin6_addr
.s6_addr
, 16) &&
144 (!(f
&AEF_PORT
) || a
->sin6
.sin6_port
== b
->sin6
.sin6_port
));
150 /*----- Main algorithm skeleton -------------------------------------------*/
153 unsigned f
; /* Various flags */
154 #define F_VERBOSE 1u /* Give a running commentary */
155 double retx
; /* Initial retransmit interval */
156 double regr
; /* Retransmit growth factor */
157 double timeout
; /* Retransmission timeout */
158 int seqoff
; /* Offset to write sequence number */
159 const struct probe_ops
*pops
; /* Probe algorithm description */
160 union addr a
; /* Destination address */
164 const struct param
*pp
;
170 const struct probe_ops
*next
;
172 int (*setup
)(void *, int, const struct param
*);
173 void (*finish
)(void *);
174 void (*selprep
)(void *, int *, fd_set
*);
175 int (*xmit
)(void *, int);
176 int (*selproc
)(void *, fd_set
*, struct probestate
*);
187 /* or a positive MTU upper-bound */
190 /* Add a file descriptor FD to the set `fd_in', updating `*maxfd'. */
192 do { FD_SET(fd, fd_in); if (*maxfd < fd) *maxfd = fd; } while (0)
194 /* Check whether a buffer contains a packet from our current probe. */
195 static int mypacketp(struct probestate
*ps
,
196 const unsigned char *p
, size_t sz
)
198 const struct param
*pp
= ps
->pp
;
200 return (sz
>= pp
->seqoff
+ 2 && LOAD16(p
+ pp
->seqoff
) == ps
->q
);
203 /* See whether MTU is an acceptable MTU value. Return an appropriate
204 * RC_... code or a new suggested MTU.
206 static int probe(struct probestate
*ps
, void *st
, int mtu
)
208 const struct param
*pp
= ps
->pp
;
210 struct timeval tv
, now
, when
, done
;
211 double timer
= pp
->retx
;
214 /* Set up the first retransmit and give-up timers. */
215 gettimeofday(&now
, 0);
216 f2tv(&tv
, pp
->timeout
); TV_ADD(&done
, &now
, &tv
);
217 f2tv(&tv
, timer
); TV_ADD(&when
, &now
, &tv
);
218 if (TV_CMP(&when
, >, &done
)) when
= done
;
220 /* Send the initial probe. */
221 if (pp
->f
& F_VERBOSE
)
222 moan("sending probe of size %d (seq = %04x)", mtu
, ps
->q
);
224 STORE16(buf
+ pp
->seqoff
, ps
->q
);
225 if ((rc
= pp
->pops
->xmit(st
, mtu
)) != RC_OK
) return (rc
);
229 /* Wait for something interesting to happen. */
230 maxfd
= 0; FD_ZERO(&fd_in
);
231 pp
->pops
->selprep(st
, &maxfd
, &fd_in
);
232 TV_SUB(&tv
, &when
, &now
);
233 if (select(maxfd
+ 1, &fd_in
, 0, 0, &tv
) < 0) return (RC_FAIL
);
234 gettimeofday(&now
, 0);
236 /* See whether the probe method has any answers for us. */
237 if ((rc
= pp
->pops
->selproc(st
, &fd_in
, ps
)) != RC_OK
) return (rc
);
239 /* If we've waited too long, give up. If we should retransmit, do
242 if (TV_CMP(&now
, >, &done
))
244 else if (TV_CMP(&now
, >, &when
)) {
245 if (pp
->f
& F_VERBOSE
) moan("re-sending probe of size %d", mtu
);
246 if ((rc
= pp
->pops
->xmit(st
, mtu
)) != RC_OK
) return (rc
);
248 timer
*= pp
->regr
; f2tv(&tv
, timer
); TV_ADD(&when
, &when
, &tv
);
249 } while (TV_CMP(&when
, <, &now
));
250 if (TV_CMP(&when
, >, &done
)) when
= done
;
255 /* Discover the path MTU to the destination address. */
256 static int pathmtu(const struct param
*pp
)
262 struct probestate ps
;
264 /* Build and connect a UDP socket. We'll need this to know the local port
265 * number to use if nothing else. Set other stuff up.
267 if ((sk
= socket(pp
->a
.sa
.sa_family
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
269 if (connect(sk
, &pp
->a
.sa
, addrsz(&pp
->a
))) goto fail_1
;
270 st
= xmalloc(pp
->pops
->statesz
);
271 if ((mtu
= pp
->pops
->setup(st
, sk
, pp
)) < 0) goto fail_2
;
272 ps
.pp
= pp
; ps
.q
= rand() & 0xffff;
273 switch (pp
->a
.sa
.sa_family
) {
274 case AF_INET
: lo
= 576; break;
275 case AF_INET6
: lo
= 1280; break;
279 if (hi
< lo
) { errno
= EMSGSIZE
; return (-1); }
281 /* And now we do a thing which is sort of like a binary search, except that
282 * we also take explicit clues as establishing a new upper bound, and we
283 * try to hug that initially.
286 assert(lo
<= mtu
&& mtu
<= hi
);
287 if (pp
->f
& F_VERBOSE
) moan("probe: %d <= %d <= %d", lo
, mtu
, hi
);
288 rc
= probe(&ps
, st
, mtu
);
292 if (pp
->f
& F_VERBOSE
) moan("probe failed");
296 /* If we've not seen a dropped packet before then we don't know what
297 * this means yet -- in particular, we don't know which bit of the
298 * network is swallowing packets. Send a minimum-size probe. If
299 * that doesn't come back then assume that the remote host is
300 * swallowing our packets. If it does, then we assume that dropped
301 * packets are a result of ICMP fragmentation-needed reports being
302 * lost or suppressed.
304 if (pp
->f
& F_VERBOSE
) moan("gave up: black hole detected");
306 if (pp
->f
& F_VERBOSE
) moan("sending minimum-size probe");
307 switch (probe(&ps
, st
, lo
)) {
311 if (pp
->f
& F_VERBOSE
) {
312 moan("no reply from min-size probe: "
313 "assume black hole at target");
318 if (pp
->f
& F_VERBOSE
) {
319 moan("reply from min-size probe OK: "
320 "assume black hole in network");
325 if (pp
->f
& F_VERBOSE
)
326 moan("unexpected return code from probe");
332 if (droppy
) goto higher
; else goto lower
;
337 if (pp
->f
& F_VERBOSE
)
338 moan("probe returned: remote host is not a black hole");
342 if (pp
->f
& F_VERBOSE
) moan("probe returned: found correct MTU");
347 /* Now we must make a new guess, between lo and hi. We know that lo
348 * is good; but we're not so sure about hi here. We know that hi >
349 * lo, so this will find an approximate midpoint, greater than lo and
352 if (pp
->f
& F_VERBOSE
) moan("probe returned: guessing higher");
353 mtu
+= (hi
- lo
+ 1)/2;
358 /* If this didn't work, and we're already at the bottom of our
359 * possible range, then something has gone horribly wrong.
364 if (pp
->f
& F_VERBOSE
) moan("error returned: found correct MTU");
369 /* We must make a new guess, between lo and hi. We're probably
370 * fairly sure that lo will succeed, since either it's the minimum
371 * MTU or we've tested it already; but we're not quite sure about hi,
372 * so we want to aim high.
374 if (pp
->f
& F_VERBOSE
) moan("error returned: guessing lower");
375 mtu
-= (hi
- lo
+ 1)/2;
379 if (pp
->f
& F_VERBOSE
) moan("error returned with new MTU estimate");
386 /* Clean up and return our result. */
387 pp
->pops
->finish(st
);
393 pp
->pops
->finish(st
);
402 /*----- Doing it the hard way ---------------------------------------------*/
404 #if defined(linux) || defined(__OpenBSD__)
409 # define sane_htons htons
410 # define sane_htonl htonl
416 static int rawicmp
= -1, rawudp
= -1, rawerr
= 0;
418 #define IPCK_INIT 0xffff
420 /* Compute an IP checksum over some data. This is a restartable interface:
421 * initialize A to `IPCK_INIT' for the first call.
423 static unsigned ipcksum(const void *buf
, size_t n
, unsigned a
)
425 unsigned long aa
= a
^ 0xffff;
426 const unsigned char *p
= buf
, *l
= p
+ n
;
428 while (p
< l
- 1) { aa
+= LOAD16_B(p
); p
+= 2; }
429 if (p
< l
) { aa
+= (unsigned)(*p
) << 8; }
430 do aa
= (aa
& 0xffff) + (aa
>> 16); while (aa
>= 0x10000);
431 return (aa
== 0xffff ? aa
: aa
^ 0xffff);
434 /* TCP/UDP pseudoheader structure. */
436 struct in_addr ph_src
, ph_dst
;
443 int sk
, rawicmp
, rawudp
;
447 static int raw_setup(void *stv
, int sk
, const struct param
*pp
)
449 struct raw_state
*st
= stv
;
452 struct ifaddrs
*ifa
, *ifaa
, *ifap
;
455 /* Check that the address is OK, and that we have the necessary raw
458 switch (pp
->a
.sa
.sa_family
) {
460 if (rawerr
) { errno
= rawerr
; goto fail_0
; }
461 st
->rawicmp
= rawicmp
; st
->rawudp
= rawudp
; st
->sk
= sk
;
464 errno
= EPFNOSUPPORT
; goto fail_0
;
467 /* Initialize the sequence number. */
468 st
->q
= rand() & 0xffff;
470 /* Snaffle the local and remote address and port number. */
473 if (getsockname(sk
, &st
->me
.sa
, &sz
))
476 /* There isn't a portable way to force the DF flag onto a packet through
477 * UDP, or even through raw IP, unless we write the entire IP header
478 * ourselves. This is somewhat annoying, especially since we have an
479 * uphill struggle keeping track of which systems randomly expect which
480 * header fields to be presented in host byte order. Oh, well.
483 if (setsockopt(rawudp
, IPPROTO_IP
, IP_HDRINCL
, &i
, sizeof(i
))) goto fail_0
;
485 /* Find an upper bound on the MTU. Do two passes over the interface
486 * list. If we can find matches for our local address then use the
487 * highest one of those; otherwise do a second pass and simply take the
488 * highest MTU of any network interface.
490 if (getifaddrs(&ifaa
)) goto fail_0
;
491 for (i
= 0; i
< 2; i
++) {
492 for (ifap
= 0, ifa
= ifaa
; ifa
; ifa
= ifa
->ifa_next
) {
493 if (!(ifa
->ifa_flags
& IFF_UP
) || !ifa
->ifa_addr
||
494 ifa
->ifa_addr
->sa_family
!= st
->me
.sa
.sa_family
||
496 !addreq((union addr
*)ifa
->ifa_addr
, &st
->me
, 0)) ||
497 (i
== 1 && ifap
&& strcmp(ifap
->ifa_name
, ifa
->ifa_name
) == 0) ||
498 strlen(ifa
->ifa_name
) >= sizeof(ifr
.ifr_name
))
501 strcpy(ifr
.ifr_name
, ifa
->ifa_name
);
502 if (ioctl(sk
, SIOCGIFMTU
, &ifr
)) goto fail_1
;
503 if (mtu
< ifr
.ifr_mtu
) mtu
= ifr
.ifr_mtu
;
507 if (mtu
< 0) { errno
= ENOTCONN
; goto fail_1
; }
519 static void raw_finish(void *stv
) { ; }
521 static void raw_selprep(void *stv
, int *maxfd
, fd_set
*fd_in
)
522 { struct raw_state
*st
= stv
; ADDFD(st
->sk
); ADDFD(st
->rawicmp
); }
524 static int raw_xmit(void *stv
, int mtu
)
526 struct raw_state
*st
= stv
;
527 unsigned char b
[65536], *p
;
533 /* Build the IP header. */
536 ip
->ip_hl
= sizeof(*ip
)/4;
537 ip
->ip_tos
= IPTOS_RELIABILITY
;
538 ip
->ip_len
= sane_htons(mtu
);
539 STEP(st
->q
); ip
->ip_id
= htons(st
->q
);
540 ip
->ip_off
= sane_htons(0 | IP_DF
);
542 ip
->ip_p
= IPPROTO_UDP
;
544 ip
->ip_src
= st
->me
.sin
.sin_addr
;
545 ip
->ip_dst
= st
->a
.sin
.sin_addr
;
547 /* Build a UDP packet in the output buffer. */
548 udp
= (struct udphdr
*)(ip
+ 1);
549 udp
->uh_sport
= st
->me
.sin
.sin_port
;
550 udp
->uh_dport
= st
->a
.sin
.sin_port
;
551 udp
->uh_ulen
= htons(mtu
- sizeof(*ip
));
554 /* Copy the payload. */
555 p
= (unsigned char *)(udp
+ 1);
556 memcpy(p
, buf
, mtu
- (p
- b
));
558 /* Calculate the UDP checksum. */
559 ph
.ph_src
= ip
->ip_src
;
560 ph
.ph_dst
= ip
->ip_dst
;
562 ph
.ph_p
= IPPROTO_UDP
;
563 ph
.ph_len
= udp
->uh_ulen
;
565 ck
= ipcksum(&ph
, sizeof(ph
), ck
);
566 ck
= ipcksum(udp
, mtu
- sizeof(*ip
), ck
);
567 udp
->uh_sum
= htons(ck
);
569 /* Send the whole thing off. If we're too big for the interface then we
570 * might need to trim immediately.
572 if (sendto(st
->rawudp
, b
, mtu
, 0, &st
->a
.sa
, addrsz(&st
->a
)) < 0) {
573 if (errno
== EMSGSIZE
) return (RC_LOWER
);
584 static int raw_selproc(void *stv
, fd_set
*fd_in
, struct probestate
*ps
)
586 struct raw_state
*st
= stv
;
587 unsigned char b
[65536];
591 const unsigned char *payload
;
594 /* An ICMP packet: see what's inside. */
595 if (FD_ISSET(st
->rawicmp
, fd_in
)) {
596 if ((n
= read(st
->rawicmp
, b
, sizeof(b
))) < 0) goto fail_0
;
599 if (n
< sizeof(*ip
) || n
< sizeof(4*ip
->ip_hl
) ||
600 ip
->ip_v
!= 4 || ip
->ip_p
!= IPPROTO_ICMP
)
602 n
-= sizeof(4*ip
->ip_hl
);
604 icmp
= (struct icmp
*)(b
+ 4*ip
->ip_hl
);
605 if (n
< sizeof(*icmp
) || icmp
->icmp_type
!= ICMP_UNREACH
)
607 n
-= offsetof(struct icmp
, icmp_ip
);
610 if (n
< sizeof(*ip
) ||
611 ip
->ip_p
!= IPPROTO_UDP
|| ip
->ip_hl
!= sizeof(*ip
)/4 ||
612 ip
->ip_id
!= htons(st
->q
) ||
613 ip
->ip_src
.s_addr
!= st
->me
.sin
.sin_addr
.s_addr
||
614 ip
->ip_dst
.s_addr
!= st
->a
.sin
.sin_addr
.s_addr
)
618 udp
= (struct udphdr
*)(ip
+ 1);
619 if (n
< sizeof(udp
) || udp
->uh_sport
!= st
->me
.sin
.sin_port
||
620 udp
->uh_dport
!= st
->a
.sin
.sin_port
)
624 payload
= (const unsigned char *)(udp
+ 1);
625 if (!mypacketp(ps
, payload
, n
)) goto skip_icmp
;
627 if (icmp
->icmp_code
== ICMP_UNREACH_PORT
) return (RC_HIGHER
);
628 else if (icmp
->icmp_code
!= ICMP_UNREACH_NEEDFRAG
) goto skip_icmp
;
629 else if (icmp
->icmp_nextmtu
) return (htons(icmp
->icmp_nextmtu
));
630 else return (RC_LOWER
);
634 /* If we got a reply to the current probe then we're good. If we got an
635 * error, or the packet's sequence number is wrong, then ignore it.
637 if (FD_ISSET(st
->sk
, fd_in
)) {
638 if ((n
= read(st
->sk
, b
, sizeof(b
))) < 0) return (RC_OK
);
639 else if (mypacketp(ps
, b
, n
)) return (RC_HIGHER
);
649 static const struct probe_ops raw_ops
= {
650 "raw", OPS_CHAIN
, sizeof(struct raw_state
),
651 raw_setup
, raw_finish
,
652 raw_selprep
, raw_xmit
, raw_selproc
656 #define OPS_CHAIN &raw_ops
658 /*----- Doing the job on Linux --------------------------------------------*/
663 # define IP_MTU 14 /* Blech! */
667 int sol
, so_mtu_discover
, so_mtu
;
672 static int linux_setup(void *stv
, int sk
, const struct param
*pp
)
674 struct linux_state
*st
= stv
;
678 /* Check that the address is OK. */
679 switch (pp
->a
.sa
.sa_family
) {
681 st
->sol
= IPPROTO_IP
;
682 st
->so_mtu_discover
= IP_MTU_DISCOVER
;
687 st
->sol
= IPPROTO_IPV6
;
688 st
->so_mtu_discover
= IPV6_MTU_DISCOVER
;
689 st
->so_mtu
= IPV6_MTU
;
693 errno
= EPFNOSUPPORT
;
697 /* Snaffle the UDP socket. */
700 /* Turn on kernel path-MTU discovery and force DF on. */
701 i
= IP_PMTUDISC_PROBE
;
702 if (setsockopt(st
->sk
, st
->sol
, st
->so_mtu_discover
, &i
, sizeof(i
)))
705 /* Read the initial MTU guess back and report it. */
707 if (getsockopt(st
->sk
, st
->sol
, st
->so_mtu
, &mtu
, &sz
))
714 static void linux_finish(void *stv
) { ; }
716 static void linux_selprep(void *stv
, int *maxfd
, fd_set
*fd_in
)
717 { struct linux_state
*st
= stv
; ADDFD(st
->sk
); }
719 static int linux_xmit(void *stv
, int mtu
)
721 struct linux_state
*st
= stv
;
723 /* Write the packet. */
724 if (write(st
->sk
, buf
, mtu
- st
->hdrlen
) >= 0) return (RC_OK
);
725 else if (errno
== EMSGSIZE
) return (RC_LOWER
);
726 else return (RC_FAIL
);
729 static int linux_selproc(void *stv
, fd_set
*fd_in
, struct probestate
*ps
)
731 struct linux_state
*st
= stv
;
735 unsigned char b
[65536];
737 /* Read an answer. If it looks like the right kind of error then report a
738 * success. This is potentially wrong, since we can't tell whether an
739 * error was delayed from an earlier probe. However, we never return
740 * RC_LOWER from this method, so the packet sizes ought to be monotonically
741 * decreasing and this won't cause trouble. Otherwise update from the
742 * kernel's idea of the right MTU.
744 if (FD_ISSET(st
->sk
, fd_in
)) {
745 n
= read(st
->sk
, &buf
, sizeof(buf
));
747 mypacketp(ps
, b
, n
) :
748 errno
== ECONNREFUSED
|| errno
== EHOSTUNREACH
)
751 if (getsockopt(st
->sk
, st
->sol
, st
->so_mtu
, &mtu
, &sz
))
758 static const struct probe_ops linux_ops
= {
759 "linux", OPS_CHAIN
, sizeof(struct linux_state
),
760 linux_setup
, linux_finish
,
761 linux_selprep
, linux_xmit
, linux_selproc
765 #define OPS_CHAIN &linux_ops
769 /*----- Help options ------------------------------------------------------*/
771 static const struct probe_ops
*probe_ops
= OPS_CHAIN
;
773 static void version(FILE *fp
)
774 { pquis(fp
, "$, TrIPE version " VERSION
"\n"); }
776 static void usage(FILE *fp
)
778 pquis(fp
, "Usage: $ [-46v] [-H HEADER] [-m METHOD]\n\
779 [-r SECS] [-g FACTOR] [-t SECS] HOST [PORT]\n");
782 static void help(FILE *fp
)
784 const struct probe_ops
*ops
;
793 -h, --help Show this help text.\n\
794 -V, --version Show version number.\n\
795 -u, --usage Show brief usage message.\n\
797 -4, --ipv4 Restrict to IPv4 only.\n\
798 -6, --ipv6 Restrict to IPv6 only.\n\
799 -g, --growth=FACTOR Growth factor for retransmit interval.\n\
800 -m, --method=METHOD Use METHOD to probe for MTU.\n\
801 -r, --retransmit=SECS Retransmit if no reply after SEC.\n\
802 -t, --timeout=SECS Give up expecting a reply after SECS.\n\
803 -v, --verbose Write a running commentary to stderr.\n\
804 -H, --header=HEX Packet header, in hexadecimal.\n\
808 for (ops
= probe_ops
; ops
; ops
= ops
->next
)
809 printf("\t%s\n", ops
->name
);
812 /*----- Main code ---------------------------------------------------------*/
814 int main(int argc
, char *argv
[])
816 struct param pp
= { 0, 0.333, 3.0, 8.0, 0, OPS_CHAIN
};
821 struct addrinfo aihint
= { 0 }, *ailist
, *ai
;
822 const char *host
, *svc
= "7";
827 if ((rawicmp
= socket(PF_INET
, SOCK_RAW
, IPPROTO_ICMP
)) < 0 ||
828 (rawudp
= socket(PF_INET
, SOCK_RAW
, IPPROTO_UDP
)) < 0)
830 if (setuid(getuid()))
834 fillbuffer(buf
, sizeof(buf
));
836 aihint
.ai_family
= AF_UNSPEC
;
837 aihint
.ai_protocol
= IPPROTO_UDP
;
838 aihint
.ai_socktype
= SOCK_DGRAM
;
839 aihint
.ai_flags
= AI_ADDRCONFIG
;
842 static const struct option opts
[] = {
843 { "help", 0, 0, 'h' },
844 { "version", 0, 0, 'V' },
845 { "usage", 0, 0, 'u' },
846 { "ipv4", 0, 0, '4' },
847 { "ipv6", 0, 0, '6' },
848 { "header", OPTF_ARGREQ
, 0, 'H' },
849 { "growth", OPTF_ARGREQ
, 0, 'g' },
850 { "method", OPTF_ARGREQ
, 0, 'm' },
851 { "retransmit", OPTF_ARGREQ
, 0, 'r' },
852 { "timeout", OPTF_ARGREQ
, 0, 't' },
853 { "verbose", 0, 0, 'v' },
857 i
= mdwopt(argc
, argv
, "hVu" "46H:g:m:r:t:v", opts
, 0, 0, 0);
860 case 'h': help(stdout
); exit(0);
861 case 'V': version(stdout
); exit(0);
862 case 'u': usage(stdout
); exit(0);
867 hex_decode(&hc
, optarg
, strlen(optarg
), &d
);
868 hex_decode(&hc
, 0, 0, &d
);
869 sz
= d
.len
< 532 ? d
.len
: 532;
870 memcpy(buf
, d
.buf
, sz
);
874 case '4': aihint
.ai_family
= AF_INET
; break;
875 case '6': aihint
.ai_family
= AF_INET6
; break;
876 case 'g': pp
.regr
= s2f(optarg
, "retransmit growth factor"); break;
877 case 'r': pp
.retx
= s2f(optarg
, "retransmit interval"); break;
878 case 't': pp
.timeout
= s2f(optarg
, "timeout"); break;
881 for (pp
.pops
= OPS_CHAIN
; pp
.pops
; pp
.pops
= pp
.pops
->next
)
882 if (strcmp(pp
.pops
->name
, optarg
) == 0) goto found_alg
;
883 die(EXIT_FAILURE
, "unknown probe algorithm `%s'", optarg
);
887 case 'v': pp
.f
|= F_VERBOSE
; break;
894 argv
+= optind
; argc
-= optind
;
895 if ((f
& f_bogus
) || 1 > argc
|| argc
> 2) {
901 if (argv
[1]) svc
= argv
[1];
902 if ((err
= getaddrinfo(host
, svc
, &aihint
, &ailist
)) != 0) {
903 die(EXIT_FAILURE
, "unknown host `%s' or service `%s': %s",
904 host
, svc
, gai_strerror(err
));
906 for (ai
= ailist
; ai
&& !addrfamok(ai
->ai_family
); ai
= ai
->ai_next
);
907 if (!ai
) die(EXIT_FAILURE
, "no supported address families for `%s'", host
);
908 assert(ai
->ai_addrlen
<= sizeof(pp
.a
));
909 memcpy(&pp
.a
, ai
->ai_addr
, ai
->ai_addrlen
);
913 die(EXIT_FAILURE
, "failed to discover MTU: %s", strerror(errno
));
915 if (ferror(stdout
) || fflush(stdout
) || fclose(stdout
))
916 die(EXIT_FAILURE
, "failed to write result: %s", strerror(errno
));
920 /*----- That's all, folks -------------------------------------------------*/