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