traceroute6.c revision 1.12 1 /* $NetBSD: traceroute6.c,v 1.12 2000/06/30 18:58:42 itojun Exp $ */
2 /* $KAME: traceroute6.c,v 1.30 2000/06/30 18:56:01 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c) 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * Van Jacobson.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 */
68
69 #if 0
70 #ifndef lint
71 static char copyright[] =
72 "@(#) Copyright (c) 1990, 1993\n\
73 The Regents of the University of California. All rights reserved.\n";
74 #endif /* not lint */
75
76 #ifndef lint
77 static char sccsid[] = "@(#)traceroute.c 8.1 (Berkeley) 6/6/93";
78 #endif /* not lint */
79 #else
80 #include <sys/cdefs.h>
81 #ifndef lint
82 __RCSID("$NetBSD: traceroute6.c,v 1.12 2000/06/30 18:58:42 itojun Exp $");
83 #endif
84 #endif
85
86 /*
87 * traceroute host - trace the route ip packets follow going to "host".
88 *
89 * Attempt to trace the route an ip packet would follow to some
90 * internet host. We find out intermediate hops by launching probe
91 * packets with a small ttl (time to live) then listening for an
92 * icmp "time exceeded" reply from a gateway. We start our probes
93 * with a ttl of one and increase by one until we get an icmp "port
94 * unreachable" (which means we got to "host") or hit a max (which
95 * defaults to 30 hops & can be changed with the -m flag). Three
96 * probes (change with -q flag) are sent at each ttl setting and a
97 * line is printed showing the ttl, address of the gateway and
98 * round trip time of each probe. If the probe answers come from
99 * different gateways, the address of each responding system will
100 * be printed. If there is no response within a 5 sec. timeout
101 * interval (changed with the -w flag), a "*" is printed for that
102 * probe.
103 *
104 * Probe packets are UDP format. We don't want the destination
105 * host to process them so the destination port is set to an
106 * unlikely value (if some clod on the destination is using that
107 * value, it can be changed with the -p flag).
108 *
109 * A sample use might be:
110 *
111 * [yak 71]% traceroute nis.nsf.net.
112 * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
113 * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
114 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
115 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
116 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
117 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
118 * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
119 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
120 * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
121 * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
122 * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
123 * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
124 *
125 * Note that lines 2 & 3 are the same. This is due to a buggy
126 * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
127 * packets with a zero ttl.
128 *
129 * A more interesting example is:
130 *
131 * [yak 72]% traceroute allspice.lcs.mit.edu.
132 * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
133 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
134 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
135 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
136 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
137 * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
138 * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
139 * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
140 * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
141 * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
142 * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
143 * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
144 * 12 * * *
145 * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
146 * 14 * * *
147 * 15 * * *
148 * 16 * * *
149 * 17 * * *
150 * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
151 *
152 * (I start to see why I'm having so much trouble with mail to
153 * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
154 * either don't send ICMP "time exceeded" messages or send them
155 * with a ttl too small to reach us. 14 - 17 are running the
156 * MIT C Gateway code that doesn't send "time exceeded"s. God
157 * only knows what's going on with 12.
158 *
159 * The silent gateway 12 in the above may be the result of a bug in
160 * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
161 * sends an unreachable message using whatever ttl remains in the
162 * original datagram. Since, for gateways, the remaining ttl is
163 * zero, the icmp "time exceeded" is guaranteed to not make it back
164 * to us. The behavior of this bug is slightly more interesting
165 * when it appears on the destination system:
166 *
167 * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
168 * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
169 * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
170 * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
171 * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
172 * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
173 * 7 * * *
174 * 8 * * *
175 * 9 * * *
176 * 10 * * *
177 * 11 * * *
178 * 12 * * *
179 * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
180 *
181 * Notice that there are 12 "gateways" (13 is the final
182 * destination) and exactly the last half of them are "missing".
183 * What's really happening is that rip (a Sun-3 running Sun OS3.5)
184 * is using the ttl from our arriving datagram as the ttl in its
185 * icmp reply. So, the reply will time out on the return path
186 * (with no notice sent to anyone since icmp's aren't sent for
187 * icmp's) until we probe with a ttl that's at least twice the path
188 * length. I.e., rip is really only 7 hops away. A reply that
189 * returns with a ttl of 1 is a clue this problem exists.
190 * Traceroute prints a "!" after the time if the ttl is <= 1.
191 * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
192 * non-standard (HPUX) software, expect to see this problem
193 * frequently and/or take care picking the target host of your
194 * probes.
195 *
196 * Other possible annotations after the time are !H, !N, !P (got a host,
197 * network or protocol unreachable, respectively), !S or !F (source
198 * route failed or fragmentation needed -- neither of these should
199 * ever occur and the associated gateway is busted if you see one). If
200 * almost all the probes result in some kind of unreachable, traceroute
201 * will give up and exit.
202 *
203 * Notes
204 * -----
205 * This program must be run by root or be setuid. (I suggest that
206 * you *don't* make it setuid -- casual use could result in a lot
207 * of unnecessary traffic on our poor, congested nets.)
208 *
209 * This program requires a kernel mod that does not appear in any
210 * system available from Berkeley: A raw ip socket using proto
211 * IPPROTO_RAW must interpret the data sent as an ip datagram (as
212 * opposed to data to be wrapped in a ip datagram). See the README
213 * file that came with the source to this program for a description
214 * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
215 * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
216 * MODIFIED TO RUN THIS PROGRAM.
217 *
218 * The udp port usage may appear bizarre (well, ok, it is bizarre).
219 * The problem is that an icmp message only contains 8 bytes of
220 * data from the original datagram. 8 bytes is the size of a udp
221 * header so, if we want to associate replies with the original
222 * datagram, the necessary information must be encoded into the
223 * udp header (the ip id could be used but there's no way to
224 * interlock with the kernel's assignment of ip id's and, anyway,
225 * it would have taken a lot more kernel hacking to allow this
226 * code to set the ip id). So, to allow two or more users to
227 * use traceroute simultaneously, we use this task's pid as the
228 * source port (the high bit is set to move the port number out
229 * of the "likely" range). To keep track of which probe is being
230 * replied to (so times and/or hop counts don't get confused by a
231 * reply that was delayed in transit), we increment the destination
232 * port number before each probe.
233 *
234 * Don't use this as a coding example. I was trying to find a
235 * routing problem and this code sort-of popped out after 48 hours
236 * without sleep. I was amazed it ever compiled, much less ran.
237 *
238 * I stole the idea for this program from Steve Deering. Since
239 * the first release, I've learned that had I attended the right
240 * IETF working group meetings, I also could have stolen it from Guy
241 * Almes or Matt Mathis. I don't know (or care) who came up with
242 * the idea first. I envy the originators' perspicacity and I'm
243 * glad they didn't keep the idea a secret.
244 *
245 * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
246 * enhancements to the original distribution.
247 *
248 * I've hacked up a round-trip-route version of this that works by
249 * sending a loose-source-routed udp datagram through the destination
250 * back to yourself. Unfortunately, SO many gateways botch source
251 * routing, the thing is almost worthless. Maybe one day...
252 *
253 * -- Van Jacobson (van (at) helios.ee.lbl.gov)
254 * Tue Dec 20 03:50:13 PST 1988
255 */
256
257 #include <sys/param.h>
258 #include <sys/time.h>
259 #include <sys/socket.h>
260 #include <sys/uio.h>
261 #include <sys/file.h>
262 #include <sys/ioctl.h>
263
264 #include <netinet/in.h>
265
266 #include <arpa/inet.h>
267
268 #include <netdb.h>
269 #include <stdio.h>
270 #include <err.h>
271 #include <errno.h>
272 #include <stdlib.h>
273 #include <string.h>
274 #include <unistd.h>
275
276 #include <netinet/ip6.h>
277 #include <netinet/icmp6.h>
278 #include <netinet/udp.h>
279
280 #ifdef IPSEC
281 #include <net/route.h>
282 #include <netinet6/ipsec.h>
283 #endif
284
285 #define DUMMY_PORT 10010
286
287 #define MAXPACKET 65535 /* max ip packet size */
288 #ifndef MAXHOSTNAMELEN
289 #define MAXHOSTNAMELEN 64
290 #endif
291
292 #ifndef FD_SET
293 #define NFDBITS (8*sizeof(fd_set))
294 #define FD_SETSIZE NFDBITS
295 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
296 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
297 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
298 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
299 #endif
300
301 #define Fprintf (void)fprintf
302 #define Sprintf (void)sprintf
303 #define Printf (void)printf
304
305 #ifndef HAVE_GETIPNODEBYNAME
306 #define getipnodebyname(x, y, z, u) gethostbyname2((x), (y))
307 #define freehostent(x)
308 #endif
309
310 /*
311 * format of a (udp) probe packet.
312 */
313 struct opacket {
314 u_char seq; /* sequence number of this packet */
315 u_char hops; /* hop limit of the packet */
316 struct timeval tv; /* time packet left */
317 };
318
319 u_char packet[512]; /* last inbound (icmp) packet */
320 struct opacket *outpacket; /* last output (udp) packet */
321
322 int main __P((int, char *[]));
323 int wait_for_reply __P((int, struct msghdr *));
324 #ifdef IPSEC
325 #ifdef IPSEC_POLICY_IPSEC
326 int setpolicy __P((int so, char *policy));
327 #endif
328 #endif
329 void send_probe __P((int, int));
330 struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *));
331 int get_hoplim __P((struct msghdr *));
332 double deltaT __P((struct timeval *, struct timeval *));
333 char *pr_type __P((int));
334 int packet_ok __P((struct msghdr *, int, int));
335 void print __P((struct msghdr *, int));
336 void tvsub __P((struct timeval *, struct timeval *));
337 const char *inetname __P((struct sockaddr *));
338 void usage __P((void));
339
340 int rcvsock; /* receive (icmp) socket file descriptor */
341 int sndsock; /* send (udp) socket file descriptor */
342 struct timezone tz; /* leftover */
343
344 struct msghdr rcvmhdr;
345 struct iovec rcviov[2];
346 int rcvhlim;
347 struct in6_pktinfo *rcvpktinfo;
348
349 struct sockaddr_in6 Src, Dst, Rcv;
350 int datalen; /* How much data */
351 /* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
352 char rtbuf[2064];
353 #ifdef USE_RFC2292BIS
354 struct ip6_rthdr *rth;
355 #endif
356 struct cmsghdr *cmsg;
357
358 char *source = 0;
359 char *hostname;
360
361 int nprobes = 3;
362 int first_hop = 1;
363 int max_hops = 30;
364 u_short ident;
365 u_short port = 32768+666; /* start udp dest port # for probe packets */
366 int options; /* socket options */
367 int verbose;
368 int waittime = 5; /* time to wait for response (in seconds) */
369 int nflag; /* print addresses numerically */
370 int lflag; /* print both numerical address & hostname */
371
372 #ifdef KAME_SCOPEID
373 const int niflag = NI_WITHSCOPEID;
374 #else
375 const int niflag = 0;
376 #endif
377
378 int
379 main(argc, argv)
380 int argc;
381 char *argv[];
382 {
383 struct hostent *hp;
384 int error;
385 struct addrinfo hints, *res;
386 int ch, i, on, probe, seq, hops, rcvcmsglen;
387 static u_char *rcvcmsgbuf;
388 char hbuf[NI_MAXHOST], src0[NI_MAXHOST];
389
390 /*
391 * Receive ICMP
392 */
393 if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
394 perror("socket(ICMPv6)");
395 exit(5);
396 }
397
398 /* set a minimum set of socket options */
399 on = 1;
400 /* specify to tell receiving interface */
401 #ifdef IPV6_RECVPKTINFO
402 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
403 sizeof(on)) < 0)
404 err(1, "setsockopt(IPV6_RECVPKTINFO)");
405 #else /* old adv. API */
406 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
407 sizeof(on)) < 0)
408 err(1, "setsockopt(IPV6_PKTINFO)");
409 #endif
410
411 /* specify to tell value of hoplimit field of received IP6 hdr */
412 #ifdef IPV6_RECVHOPLIMIT
413 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
414 sizeof(on)) < 0)
415 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
416 #else /* old adv. API */
417 if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
418 sizeof(on)) < 0)
419 err(1, "setsockopt(IPV6_HOPLIMIT)");
420 #endif
421
422 /* revoke privs */
423 seteuid(getuid());
424 setuid(getuid());
425
426 seq = 0;
427
428 while ((ch = getopt(argc, argv, "df:g:lm:np:q:rs:w:v")) != EOF)
429 switch(ch) {
430 case 'd':
431 options |= SO_DEBUG;
432 break;
433 case 'f':
434 first_hop = atoi(optarg);
435 if (first_hop > max_hops) {
436 Fprintf(stderr,
437 "traceroute6: min hoplimit must be <= %d.\n", max_hops);
438 exit(1);
439 }
440 break;
441 case 'g':
442 hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno);
443 if (hp == NULL) {
444 Fprintf(stderr,
445 "traceroute6: unknown host %s\n", optarg);
446 exit(1);
447 }
448 #ifdef USE_RFC2292BIS
449 if (rth == NULL) {
450 /*
451 * XXX: We can't detect the number of
452 * intermediate nodes yet.
453 */
454 if ((rth = inet6_rth_init((void *)rtbuf,
455 sizeof(rtbuf),
456 IPV6_RTHDR_TYPE_0,
457 0)) == NULL) {
458 Fprintf(stderr,
459 "inet6_rth_init failed.\n");
460 exit(1);
461 }
462 }
463 if (inet6_rth_add((void *)rth,
464 (struct in6_addr *)hp->h_addr)) {
465 Fprintf(stderr,
466 "inet6_rth_add failed for %s\n",
467 optarg);
468 exit(1);
469 }
470 #else /* old advanced API */
471 if (cmsg == NULL)
472 cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0);
473 inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr, IPV6_RTHDR_LOOSE);
474 #endif
475 freehostent(hp);
476 break;
477 case 'l':
478 lflag++;
479 break;
480 case 'm':
481 max_hops = atoi(optarg);
482 if (max_hops < first_hop) {
483 Fprintf(stderr,
484 "traceroute6: max hoplimit must be >= %d.\n", first_hop);
485 exit(1);
486 }
487 break;
488 case 'n':
489 nflag++;
490 break;
491 case 'p':
492 port = atoi(optarg);
493 if (port < 1) {
494 Fprintf(stderr,
495 "traceroute6: port must be >0.\n");
496 exit(1);
497 }
498 break;
499 case 'q':
500 nprobes = atoi(optarg);
501 if (nprobes < 1) {
502 Fprintf(stderr,
503 "traceroute6: nprobes must be >0.\n");
504 exit(1);
505 }
506 break;
507 case 'r':
508 options |= SO_DONTROUTE;
509 break;
510 case 's':
511 /*
512 * set the ip source address of the outbound
513 * probe (e.g., on a multi-homed host).
514 */
515 source = optarg;
516 break;
517 case 'v':
518 verbose++;
519 break;
520 case 'w':
521 waittime = atoi(optarg);
522 if (waittime <= 1) {
523 Fprintf(stderr,
524 "traceroute6: wait must be >1 sec.\n");
525 exit(1);
526 }
527 break;
528 default:
529 usage();
530 }
531 argc -= optind;
532 argv += optind;
533
534 if (argc < 1)
535 usage();
536
537 #if 1
538 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
539 #else
540 setlinebuf (stdout);
541 #endif
542
543 memset(&hints, 0, sizeof(hints));
544 hints.ai_family = PF_INET6;
545 hints.ai_socktype = SOCK_RAW;
546 hints.ai_protocol = IPPROTO_ICMPV6;
547 hints.ai_flags = AI_CANONNAME;
548 error = getaddrinfo(*argv, NULL, &hints, &res);
549 if (error) {
550 (void)fprintf(stderr,
551 "traceroute6: %s\n", gai_strerror(error));
552 exit(1);
553 }
554 if (res->ai_addrlen != sizeof(Dst)) {
555 (void)fprintf(stderr,
556 "traceroute6: size of sockaddr mismatch\n");
557 exit(1);
558 }
559 memcpy(&Dst, res->ai_addr, res->ai_addrlen);
560 hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv;
561
562 if (*++argv)
563 datalen = atoi(*argv);
564 if (datalen < 0 || datalen >= MAXPACKET - sizeof(struct opacket)) {
565 Fprintf(stderr,
566 "traceroute6: packet size must be 0 <= s < %ld.\n",
567 (long)(MAXPACKET - sizeof(struct opacket)));
568 exit(1);
569 }
570 datalen += sizeof(struct opacket);
571 outpacket = (struct opacket *)malloc((unsigned)datalen);
572 if (! outpacket) {
573 perror("malloc");
574 exit(1);
575 }
576 (void) bzero((char *)outpacket, datalen);
577
578 /* initialize msghdr for receiving packets */
579 rcviov[0].iov_base = (caddr_t)packet;
580 rcviov[0].iov_len = sizeof(packet);
581 rcvmhdr.msg_name = (caddr_t)&Rcv;
582 rcvmhdr.msg_namelen = sizeof(Rcv);
583 rcvmhdr.msg_iov = rcviov;
584 rcvmhdr.msg_iovlen = 1;
585 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo))
586 + CMSG_SPACE(sizeof(int));
587 if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
588 Fprintf(stderr, "traceroute6: malloc failed\n");
589 exit(1);
590 }
591 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
592 rcvmhdr.msg_controllen = rcvcmsglen;
593
594 if (options & SO_DEBUG)
595 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
596 (char *)&on, sizeof(on));
597 if (options & SO_DONTROUTE)
598 (void) setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
599 (char *)&on, sizeof(on));
600 #ifdef IPSEC
601 #ifdef IPSEC_POLICY_IPSEC
602 /*
603 * do not raise error even if setsockopt fails, kernel may have ipsec
604 * turned off.
605 */
606 if (setpolicy(rcvsock, "in bypass") < 0)
607 errx(1, ipsec_strerror());
608 if (setpolicy(rcvsock, "out bypass") < 0)
609 errx(1, ipsec_strerror());
610 #else
611 {
612 int level = IPSEC_LEVEL_NONE;
613
614 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
615 sizeof(level));
616 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
617 sizeof(level));
618 #ifdef IP_AUTH_TRANS_LEVEL
619 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
620 sizeof(level));
621 #else
622 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
623 sizeof(level));
624 #endif
625 #ifdef IP_AUTH_NETWORK_LEVEL
626 (void)setsockopt(rcvsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
627 sizeof(level));
628 #endif
629 }
630 #endif /*IPSEC_POLICY_IPSEC*/
631 #endif /*IPSEC*/
632
633 /*
634 * Send UDP
635 */
636 if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
637 perror("socket(SOCK_DGRAM)");
638 exit(5);
639 }
640 #ifdef SO_SNDBUF
641 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen,
642 sizeof(datalen)) < 0) {
643 perror("setsockopt(SO_SNDBUF)");
644 exit(6);
645 }
646 #endif /* SO_SNDBUF */
647 if (options & SO_DEBUG)
648 (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
649 (char *)&on, sizeof(on));
650 if (options & SO_DONTROUTE)
651 (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
652 (char *)&on, sizeof(on));
653 #ifdef USE_RFC2292BIS
654 if (rth) {/* XXX: there is no library to finalize the header... */
655 rth->ip6r_len = rth->ip6r_segleft * 2;
656 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_RTHDR,
657 (void *)rth, (rth->ip6r_len + 1) << 3)) {
658 Fprintf(stderr, "setsockopt(IPV6_RTHDR): %s\n",
659 strerror(errno));
660 exit(1);
661 }
662 }
663 #else /* old advanced API */
664 if (cmsg != NULL) {
665 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
666 if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS,
667 rtbuf, cmsg->cmsg_len) < 0) {
668 Fprintf(stderr, "setsockopt(IPV6_PKTOPTIONS): %s\n",
669 strerror(errno));
670 exit(1);
671 }
672 }
673 #endif /* USE_RFC2292BIS */
674 #ifdef IPSEC
675 #ifdef IPSEC_POLICY_IPSEC
676 /*
677 * do not raise error even if setsockopt fails, kernel may have ipsec
678 * turned off.
679 */
680 if (setpolicy(sndsock, "in bypass") < 0)
681 errx(1, ipsec_strerror());
682 if (setpolicy(sndsock, "out bypass") < 0)
683 errx(1, ipsec_strerror());
684 #else
685 {
686 int level = IPSEC_LEVEL_BYPASS;
687
688 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, &level,
689 sizeof(level));
690 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_ESP_NETWORK_LEVEL, &level,
691 sizeof(level));
692 #ifdef IP_AUTH_TRANS_LEVEL
693 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, &level,
694 sizeof(level));
695 #else
696 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_LEVEL, &level,
697 sizeof(level));
698 #endif
699 #ifdef IP_AUTH_NETWORK_LEVEL
700 (void)setsockopt(sndsock, IPPROTO_IPV6, IPV6_AUTH_NETWORK_LEVEL, &level,
701 sizeof(level));
702 #endif
703 }
704 #endif /*IPSEC_POLICY_IPSEC*/
705 #endif /*IPSEC*/
706
707 /*
708 * Source selection
709 */
710 bzero(&Src, sizeof(Src));
711 if (source) {
712 struct addrinfo hints, *res;
713 int error;
714
715 memset(&hints, 0, sizeof(hints));
716 hints.ai_family = AF_INET6;
717 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
718 hints.ai_flags = AI_NUMERICHOST;
719 error = getaddrinfo(source, "0", &hints, &res);
720 if (error) {
721 Printf("traceroute6: %s: %s\n", source,
722 gai_strerror(error));
723 exit(1);
724 }
725 if (res->ai_addrlen > sizeof(Src)) {
726 Printf("traceroute6: %s: %s\n", source,
727 gai_strerror(error));
728 exit(1);
729 }
730 memcpy(&Src, res->ai_addr, res->ai_addrlen);
731 freeaddrinfo(res);
732 } else {
733 struct sockaddr_in6 Nxt;
734 int dummy, len;
735
736 Nxt = Dst;
737 Nxt.sin6_port = htons(DUMMY_PORT);
738 if (cmsg != NULL)
739 bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr,
740 sizeof(Nxt.sin6_addr));
741 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
742 perror("socket");
743 exit(1);
744 }
745 if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0) {
746 perror("connect");
747 exit(1);
748 }
749 len = sizeof(Src);
750 if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0) {
751 perror("getsockname");
752 exit(1);
753 }
754 if (getnameinfo((struct sockaddr *)&Src, Src.sin6_len,
755 src0, sizeof(src0), NULL, 0,
756 NI_NUMERICHOST | niflag)) {
757 Fprintf(stderr, "getnameinfo failed for source\n");
758 exit(1);
759 }
760 source = src0;
761 close(dummy);
762 }
763
764 #if 1
765 ident = (getpid() & 0xffff) | 0x8000;
766 #else
767 ident = 0; /*let the kernel pick one*/
768 #endif
769 Src.sin6_port = htons(ident);
770 if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0) {
771 perror("bind");
772 exit(1);
773 }
774
775 if (ident == 0) {
776 int len;
777
778 len = sizeof(Src);
779 if (getsockname(sndsock, (struct sockaddr *)&Src, &i) < 0) {
780 perror("getsockname");
781 exit(1);
782 }
783 ident = ntohs(Src.sin6_port);
784 }
785
786 /*
787 * Message to users
788 */
789 if (getnameinfo((struct sockaddr *)&Dst, Dst.sin6_len, hbuf,
790 sizeof(hbuf), NULL, 0, NI_NUMERICHOST | niflag))
791 strcpy(hbuf, "(invalid)");
792 Fprintf(stderr, "traceroute6");
793 Fprintf(stderr, " to %s (%s)", hostname, hbuf);
794 if (source)
795 Fprintf(stderr, " from %s", source);
796 Fprintf(stderr,
797 ", %d hops max, %d byte packets\n",
798 max_hops, datalen);
799 (void) fflush(stderr);
800
801 if (first_hop > 1)
802 Printf("Skipping %d intermediate hops\n", first_hop - 1);
803
804 /*
805 * Main loop
806 */
807 for (hops = first_hop; hops <= max_hops; ++hops) {
808 struct in6_addr lastaddr;
809 int got_there = 0;
810 int unreachable = 0;
811
812 Printf("%2d ", hops);
813 bzero(&lastaddr, sizeof(lastaddr));
814 for (probe = 0; probe < nprobes; ++probe) {
815 int cc;
816 struct timeval t1, t2;
817 struct timezone tz;
818
819 (void) gettimeofday(&t1, &tz);
820 send_probe(++seq, hops);
821 while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) {
822 (void) gettimeofday(&t2, &tz);
823 if ((i = packet_ok(&rcvmhdr, cc, seq))) {
824 if (! IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr,
825 &lastaddr)) {
826 print(&rcvmhdr, cc);
827 lastaddr = Rcv.sin6_addr;
828 }
829 Printf(" %g ms", deltaT(&t1, &t2));
830 switch(i - 1) {
831 case ICMP6_DST_UNREACH_NOROUTE:
832 ++unreachable;
833 Printf(" !N");
834 break;
835 case ICMP6_DST_UNREACH_ADMIN:
836 ++unreachable;
837 Printf(" !P");
838 break;
839 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
840 ++unreachable;
841 Printf(" !S");
842 break;
843 case ICMP6_DST_UNREACH_ADDR:
844 ++unreachable;
845 Printf(" !A");
846 break;
847 case ICMP6_DST_UNREACH_NOPORT:
848 if (rcvhlim >= 0 &&
849 rcvhlim <= 1)
850 Printf(" !");
851 ++got_there;
852 break;
853 }
854 break;
855 }
856 }
857 if (cc == 0)
858 Printf(" *");
859 (void) fflush(stdout);
860 }
861 putchar('\n');
862 if (got_there ||
863 (unreachable > 0 && unreachable >= ((nprobes + 1) / 2))) {
864 exit(0);
865 }
866 }
867
868 exit(0);
869 }
870
871 int
872 wait_for_reply(sock, mhdr)
873 int sock;
874 struct msghdr *mhdr;
875 {
876 fd_set fds;
877 struct timeval wait;
878 int cc = 0;
879
880 FD_ZERO(&fds);
881 FD_SET(sock, &fds);
882 wait.tv_sec = waittime; wait.tv_usec = 0;
883
884 if (select(sock+1, &fds, (fd_set *)0, (fd_set *)0, &wait) > 0)
885 cc = recvmsg(rcvsock, mhdr, 0);
886
887 return(cc);
888 }
889
890 #ifdef IPSEC
891 #ifdef IPSEC_POLICY_IPSEC
892 int
893 setpolicy(so, policy)
894 int so;
895 char *policy;
896 {
897 char *buf;
898
899 buf = ipsec_set_policy(policy, strlen(policy));
900 if (buf == NULL) {
901 warnx("%s", ipsec_strerror());
902 return -1;
903 }
904 (void)setsockopt(so, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
905 buf, ipsec_get_policylen(buf));
906
907 free(buf);
908
909 return 0;
910 }
911 #endif
912 #endif
913
914 void
915 send_probe(seq, hops)
916 int seq, hops;
917 {
918 struct opacket *op = outpacket;
919 int i;
920
921 if(setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
922 (char *)&hops, sizeof(hops)) < 0) {
923 perror("setsockopt IPV6_UNICAST_HOPS");
924 }
925
926 Dst.sin6_port = htons(port + seq);
927
928 op->seq = seq;
929 op->hops = hops;
930 (void) gettimeofday(&op->tv, &tz);
931
932 i = sendto(sndsock, (char *)outpacket, datalen , 0,
933 (struct sockaddr *)&Dst, Dst.sin6_len);
934 if (i < 0 || i != datalen) {
935 if (i<0)
936 perror("sendto");
937 Printf("traceroute6: wrote %s %d chars, ret=%d\n", hostname,
938 datalen, i);
939 (void) fflush(stdout);
940 }
941 }
942
943 int
944 get_hoplim(mhdr)
945 struct msghdr *mhdr;
946 {
947 struct cmsghdr *cm;
948
949 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
950 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
951 if (cm->cmsg_level == IPPROTO_IPV6 &&
952 cm->cmsg_type == IPV6_HOPLIMIT &&
953 cm->cmsg_len == CMSG_LEN(sizeof(int)))
954 return(*(int *)CMSG_DATA(cm));
955 }
956
957 return(-1);
958 }
959
960 double
961 deltaT(t1p, t2p)
962 struct timeval *t1p, *t2p;
963 {
964 register double dt;
965
966 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
967 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
968 return (dt);
969 }
970
971
972 /*
973 * Convert an ICMP "type" field to a printable string.
974 */
975 char *
976 pr_type(t0)
977 int t0;
978 {
979 u_char t = t0 & 0xff;
980 char *cp;
981
982 switch (t) {
983 case ICMP6_DST_UNREACH:
984 cp = "Destination Unreachable";
985 break;
986 case ICMP6_PACKET_TOO_BIG:
987 cp = "Pakcet Too Big";
988 break;
989 case ICMP6_TIME_EXCEEDED:
990 cp = "Time Exceeded";
991 break;
992 case ICMP6_PARAM_PROB:
993 cp = "Parameter Problem";
994 break;
995 case ICMP6_ECHO_REQUEST:
996 cp = "Echo Request";
997 break;
998 case ICMP6_ECHO_REPLY:
999 cp = "Echo Reply";
1000 break;
1001 case ICMP6_MEMBERSHIP_QUERY:
1002 cp = "Group Membership Query";
1003 break;
1004 case ICMP6_MEMBERSHIP_REPORT:
1005 cp = "Group Membership Report";
1006 break;
1007 case ICMP6_MEMBERSHIP_REDUCTION:
1008 cp = "Group Membership Reduction";
1009 break;
1010 case ND_ROUTER_SOLICIT:
1011 cp = "Router Solicitation";
1012 break;
1013 case ND_ROUTER_ADVERT:
1014 cp = "Router Advertisement";
1015 break;
1016 case ND_NEIGHBOR_SOLICIT:
1017 cp = "Neighbor Solicitation";
1018 break;
1019 case ND_NEIGHBOR_ADVERT:
1020 cp = "Neighbor Advertisement";
1021 break;
1022 case ND_REDIRECT:
1023 cp = "Redirect";
1024 break;
1025 default:
1026 cp = "Unknown";
1027 break;
1028 }
1029 return cp;
1030 }
1031
1032
1033 int
1034 packet_ok(mhdr, cc, seq)
1035 struct msghdr *mhdr;
1036 int cc;
1037 int seq;
1038 {
1039 register struct icmp6_hdr *icp;
1040 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1041 u_char type, code;
1042 char *buf = (char *)mhdr->msg_iov[0].iov_base;
1043 struct cmsghdr *cm;
1044 int *hlimp;
1045 char hbuf[NI_MAXHOST];
1046
1047 #ifdef OLDRAWSOCKET
1048 int hlen;
1049 struct ip6_hdr *ip;
1050 #endif
1051
1052 #ifdef OLDRAWSOCKET
1053 ip = (struct ip6_hdr *) buf;
1054 hlen = sizeof(struct ip6_hdr);
1055 if (cc < hlen + sizeof(struct icmp6_hdr)) {
1056 if (verbose) {
1057 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1058 hbuf, sizeof(hbuf), NULL, 0,
1059 NI_NUMERICHOST | niflag) != 0)
1060 strcpy(hbuf, "invalid");
1061 Printf("packet too short (%d bytes) from %s\n", cc,
1062 hbuf);
1063 }
1064 return (0);
1065 }
1066 cc -= hlen;
1067 icp = (struct icmp6_hdr *)(buf + hlen);
1068 #else
1069 if (cc < sizeof(struct icmp6_hdr)) {
1070 if (verbose) {
1071 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1072 hbuf, sizeof(hbuf), NULL, 0,
1073 NI_NUMERICHOST | niflag) != 0)
1074 strcpy(hbuf, "invalid");
1075 Printf("data too short (%d bytes) from %s\n", cc, hbuf);
1076 }
1077 return(0);
1078 }
1079 icp = (struct icmp6_hdr *)buf;
1080 #endif
1081 /* get optional information via advanced API */
1082 rcvpktinfo = NULL;
1083 hlimp = NULL;
1084 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm;
1085 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) {
1086 if (cm->cmsg_level == IPPROTO_IPV6 &&
1087 cm->cmsg_type == IPV6_PKTINFO &&
1088 cm->cmsg_len ==
1089 CMSG_LEN(sizeof(struct in6_pktinfo)))
1090 rcvpktinfo = (struct in6_pktinfo *)(CMSG_DATA(cm));
1091
1092 if (cm->cmsg_level == IPPROTO_IPV6 &&
1093 cm->cmsg_type == IPV6_HOPLIMIT &&
1094 cm->cmsg_len == CMSG_LEN(sizeof(int)))
1095 hlimp = (int *)CMSG_DATA(cm);
1096 }
1097 if (rcvpktinfo == NULL || hlimp == NULL) {
1098 warnx("failed to get received hop limit or packet info");
1099 #if 0
1100 return(0);
1101 #else
1102 rcvhlim = 0; /*XXX*/
1103 #endif
1104 }
1105 else
1106 rcvhlim = *hlimp;
1107
1108 type = icp->icmp6_type;
1109 code = icp->icmp6_code;
1110 if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
1111 || type == ICMP6_DST_UNREACH) {
1112 struct ip6_hdr *hip;
1113 struct udphdr *up;
1114
1115 hip = (struct ip6_hdr *)(icp + 1);
1116 if ((up = get_udphdr(hip, (u_char *)(buf + cc))) == NULL) {
1117 if (verbose)
1118 warnx("failed to get upper layer header");
1119 return(0);
1120 }
1121 if (up->uh_sport == htons(ident) &&
1122 up->uh_dport == htons(port+seq))
1123 return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
1124 }
1125 if (verbose) {
1126 int i;
1127 u_int8_t *p;
1128 char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN];
1129
1130 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1131 sbuf, sizeof(sbuf), NULL, 0, NI_NUMERICHOST | niflag) != 0)
1132 strcpy(sbuf, "invalid");
1133 Printf("\n%d bytes from %s to %s", cc, sbuf,
1134 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1135 dbuf, sizeof(dbuf))
1136 : "?");
1137 Printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
1138 icp->icmp6_code);
1139 p = (u_int8_t *)(icp + 1);
1140 #define WIDTH 16
1141 for (i = 0; i < cc; i++) {
1142 if (i % WIDTH == 0)
1143 Printf("%04x:", i);
1144 if (i % 4 == 0)
1145 Printf(" ");
1146 Printf("%02x", p[i]);
1147 if (i % WIDTH == WIDTH - 1)
1148 Printf("\n");
1149 }
1150 if (cc % WIDTH != 0)
1151 Printf("\n");
1152 }
1153 return(0);
1154 }
1155
1156 /*
1157 * Increment pointer until find the UDP header.
1158 */
1159 struct udphdr *
1160 get_udphdr(ip6, lim)
1161 struct ip6_hdr *ip6;
1162 u_char *lim;
1163 {
1164 u_char *cp = (u_char *)ip6, nh;
1165 int hlen;
1166
1167 if (cp + sizeof(*ip6) >= lim)
1168 return(NULL);
1169
1170 nh = ip6->ip6_nxt;
1171 cp += sizeof(struct ip6_hdr);
1172
1173 while(lim - cp >= 8) {
1174 switch(nh) {
1175 case IPPROTO_ESP:
1176 case IPPROTO_TCP:
1177 case IPPROTO_ICMPV6:
1178 return(NULL);
1179 case IPPROTO_UDP:
1180 return((struct udphdr *)cp);
1181 case IPPROTO_FRAGMENT:
1182 hlen = sizeof(struct ip6_frag);
1183 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
1184 break;
1185 case IPPROTO_AH:
1186 hlen = (((struct ip6_ext *)cp)->ip6e_len + 2) << 2;
1187 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1188 break;
1189 default:
1190 hlen = (((struct ip6_ext *)cp)->ip6e_len + 1) << 3;
1191 nh = ((struct ip6_ext *)cp)->ip6e_nxt;
1192 break;
1193 }
1194
1195 cp += hlen;
1196 }
1197
1198 return(NULL);
1199 }
1200
1201 void
1202 print(mhdr, cc)
1203 struct msghdr *mhdr;
1204 int cc;
1205 {
1206 struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
1207 char hbuf[NI_MAXHOST];
1208
1209 if (getnameinfo((struct sockaddr *)from, from->sin6_len,
1210 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST | niflag) != 0)
1211 strcpy(hbuf, "invalid");
1212 if (nflag)
1213 Printf(" %s", hbuf);
1214 else if (lflag)
1215 Printf(" %s (%s)", inetname((struct sockaddr *)from), hbuf);
1216 else
1217 Printf(" %s", inetname((struct sockaddr *)from));
1218
1219 if (verbose) {
1220 #ifdef OLDRAWSOCKET
1221 Printf(" %d bytes to %s", cc,
1222 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1223 hbuf, sizeof(hbuf))
1224 : "?");
1225 #else
1226 Printf(" %d bytes of data to %s", cc,
1227 rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
1228 hbuf, sizeof(hbuf))
1229 : "?");
1230 #endif
1231 }
1232 }
1233
1234 /*
1235 * Subtract 2 timeval structs: out = out - in.
1236 * Out is assumed to be >= in.
1237 */
1238 void
1239 tvsub(out, in)
1240 register struct timeval *out, *in;
1241 {
1242 if ((out->tv_usec -= in->tv_usec) < 0) {
1243 out->tv_sec--;
1244 out->tv_usec += 1000000;
1245 }
1246 out->tv_sec -= in->tv_sec;
1247 }
1248
1249
1250 /*
1251 * Construct an Internet address representation.
1252 * If the nflag has been supplied, give
1253 * numeric value, otherwise try for symbolic name.
1254 */
1255 const char *
1256 inetname(sa)
1257 struct sockaddr *sa;
1258 {
1259 register char *cp;
1260 static char line[NI_MAXHOST];
1261 static char domain[MAXHOSTNAMELEN + 1];
1262 static int first = 1;
1263
1264 if (first && !nflag) {
1265 first = 0;
1266 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1267 (cp = index(domain, '.')))
1268 (void) strcpy(domain, cp + 1);
1269 else
1270 domain[0] = 0;
1271 }
1272 cp = NULL;
1273 if (!nflag) {
1274 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1275 NI_NAMEREQD) == 0) {
1276 if ((cp = index(line, '.')) &&
1277 !strcmp(cp + 1, domain))
1278 *cp = 0;
1279 cp = line;
1280 }
1281 }
1282 if (cp)
1283 return cp;
1284
1285 if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
1286 NI_NUMERICHOST | niflag) != 0)
1287 strcpy(line, "invalid");
1288 return line;
1289 }
1290
1291 void
1292 usage()
1293 {
1294 (void)fprintf(stderr,
1295 "usage: traceroute6 [-dlnrv] [-f first_hop] [-m max_hops] [-p port#] \n"
1296 " [-q nqueries] [-s src_addr] [-g gateway] [-w wait] host [data size]\n");
1297 exit(1);
1298 }
1299