if (datalen >= sizeof(struct timeval)) {
if (datalen >= sizeof(struct ping6_timeval)) {
char *dnsdecode __P((const u_char **, const u_char *, const u_char *,	u_char *, size_t));void	 pr_pack __P((u_char *, int, struct msghdr *));void	 pr_exthdrs __P((struct msghdr *));void	 pr_ip6opt __P((void *));void	 pr_rthdr __P((void *));int	 pr_bitrange __P((u_int32_t, int, int));void	 pr_retip __P((struct ip6_hdr *, u_char *));void	 summary __P((void));void	 tvsub __P((struct timeval *, struct timeval *));int	 setpolicy __P((int, char *));char	*nigroup __P((char *));void	 usage __P((void));intmain(argc, argv)	int argc;	char *argv[];{	struct itimerval itimer;	struct sockaddr_in6 from;	struct timeval timeout, *tv;	struct addrinfo hints;	fd_set *fdmaskp;	int fdmasks;	int cc, i;	int ch, fromlen, hold, packlen, preload, optval, ret_ga;	u_char *datap, *packet;	char *e, *target, *ifname = NULL;	int ip6optlen = 0;	struct cmsghdr *scmsgp = NULL;	int sockbufsize = 0;	int usepktinfo = 0;	struct in6_pktinfo *pktinfo = NULL;#ifdef USE_RFC2292BIS	struct ip6_rthdr *rthdr = NULL;#endif#ifdef IPSEC_POLICY_IPSEC	char *policy_in = NULL;	char *policy_out = NULL;#endif	double intval;	size_t rthlen;	/* just to be sure */	memset(&smsghdr, 0, sizeof(&smsghdr));	memset(&smsgiov, 0, sizeof(&smsgiov));	preload = 0;	datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN];#ifndef IPSEC#define ADDOPTS#else#ifdef IPSEC_POLICY_IPSEC#define ADDOPTS	"P:"#else#define ADDOPTS	"AE"#endif /*IPSEC_POLICY_IPSEC*/#endif	while ((ch = getopt(argc, argv,	    "a:b:c:dfHh:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS)) != -1) {#undef ADDOPTS		switch (ch) {		case 'a':		{			char *cp;			options &= ~F_NOUSERDATA;			options |= F_NODEADDR;			for (cp = optarg; *cp != '\0'; cp++) {				switch (*cp) {				case 'a':					naflags |= NI_NODEADDR_FLAG_ALL;					break;				case 'c':				case 'C':					naflags |= NI_NODEADDR_FLAG_COMPAT;					break;				case 'l':				case 'L':					naflags |= NI_NODEADDR_FLAG_LINKLOCAL;					break;				case 's':				case 'S':					naflags |= NI_NODEADDR_FLAG_SITELOCAL;					break;				case 'g':				case 'G':					naflags |= NI_NODEADDR_FLAG_GLOBAL;					break;				case 'A': /* experimental. not in the spec */#ifdef NI_NODEADDR_FLAG_ANYCAST					naflags |= NI_NODEADDR_FLAG_ANYCAST;					break;#else					errx(1,"-a A is not supported on the platform");					/*NOTREACHED*/#endif				default:					usage();					/*NOTREACHED*/				}			}			break;		}		case 'b':#if defined(SO_SNDBUF) && defined(SO_RCVBUF)			sockbufsize = atoi(optarg);#else			err(1,"-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported");#endif			break;		case 'c':			npackets = strtol(optarg, &e, 10);			if (npackets <= 0 || *optarg == '\0' || *e != '\0')				errx(1,				    "illegal number of packets -- %s", optarg);			break;		case 'd':			options |= F_SO_DEBUG;			break;		case 'f':			if (getuid()) {				errno = EPERM;				errx(1, "Must be superuser to flood ping");			}			options |= F_FLOOD;			setbuf(stdout, (char *)NULL);			break;		case 'H':			options |= F_HOSTNAME;			break;		case 'h':		/* hoplimit */			hoplimit = strtol(optarg, &e, 10);			if (255 < hoplimit || hoplimit < -1)				errx(1,				    "illegal hoplimit -- %s", optarg);			break;		case 'I':			ifname = optarg;			options |= F_INTERFACE;#ifndef USE_SIN6_SCOPE_ID			usepktinfo++;#endif			break;		case 'i':		/* wait between sending packets */			intval = strtod(optarg, &e);			if (*optarg == '\0' || *e != '\0')				errx(1, "illegal timing interval %s", optarg);			if (intval < 1 && getuid()) {				errx(1, "%s: only root may use interval < 1s",				    strerror(EPERM));			}			interval.tv_sec = (long)intval;			interval.tv_usec =			    (long)((intval - interval.tv_sec) * 1000000);			if (interval.tv_sec < 0)				errx(1, "illegal timing interval %s", optarg);			/* less than 1/hz does not make sense */			if (interval.tv_sec == 0 && interval.tv_usec < 10000) {				warnx("too small interval, raised to 0.01");				interval.tv_usec = 10000;			}			options |= F_INTERVAL;			break;		case 'l':			if (getuid()) {				errno = EPERM;				errx(1, "Must be superuser to preload");			}			preload = strtol(optarg, &e, 10);			if (preload < 0 || *optarg == '\0' || *e != '\0')				errx(1, "illegal preload value -- %s", optarg);			break;		case 'm':#ifdef IPV6_USE_MIN_MTU			options |= F_NOMINMTU;			break;#else			errx(1, "-%c is not supported on this platform", ch);			/*NOTREACHED*/#endif		case 'n':			options &= ~F_HOSTNAME;			break;		case 'N':			options |= F_NIGROUP;			break;		case 'p':		/* fill buffer with user pattern */			options |= F_PINGFILLED;			fill((char *)datap, optarg);				break;		case 'q':			options |= F_QUIET;			break;		case 'R':#ifdef IPV6_REACHCONF			options |= F_REACHCONF;			break;#else			errx(1, "-R is not supported in this configuration");#endif		case 'S':			/* XXX: use getaddrinfo? */			if (inet_pton(AF_INET6, optarg, (void *)&srcaddr) != 1)				errx(1, "invalid IPv6 address: %s", optarg);			options |= F_SRCADDR;			usepktinfo++;			break;		case 's':		/* size of packet to send */			datalen = strtol(optarg, &e, 10);			if (datalen <= 0 || *optarg == '\0' || *e != '\0')				errx(1, "illegal datalen value -- %s", optarg);			if (datalen > MAXDATALEN) {				errx(1,				    "datalen value too large, maximum is %d",				    MAXDATALEN);			}			break;		case 't':			options &= ~F_NOUSERDATA;			options |= F_SUPTYPES;			break;		case 'v':			options |= F_VERBOSE;			break;		case 'w':			options &= ~F_NOUSERDATA;			options |= F_FQDN;			break;		case 'W':			options &= ~F_NOUSERDATA;			options |= F_FQDNOLD;			break;#ifdef IPSEC#ifdef IPSEC_POLICY_IPSEC		case 'P':			options |= F_POLICY;			if (!strncmp("in", optarg, 2)) {				if ((policy_in = strdup(optarg)) == NULL)					errx(1, "strdup");			} else if (!strncmp("out", optarg, 3)) {				if ((policy_out = strdup(optarg)) == NULL)					errx(1, "strdup");			} else				errx(1, "invalid security policy");			break;#else		case 'A':			options |= F_AUTHHDR;			break;		case 'E':			options |= F_ENCRYPT;			break;#endif /*IPSEC_POLICY_IPSEC*/#endif /*IPSEC*/		default:			usage();			/*NOTREACHED*/		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		usage();		/*NOTREACHED*/	}	if (argc > 1) {#ifdef IPV6_RECVRTHDR	/* 2292bis */		rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0,		    argc - 1));#else  /* RFC2292 */		rthlen = inet6_rthdr_space(IPV6_RTHDR_TYPE_0, argc - 1);#endif		if (rthlen == 0) {			errx(1, "too many intermediate hops");			/*NOTREACHED*/		}		ip6optlen += rthlen;	}	if (options & F_NIGROUP) {		target = nigroup(argv[argc - 1]);		if (target == NULL) {			usage();			/*NOTREACHED*/		}	} else		target = argv[argc - 1];	/* getaddrinfo */	bzero(&hints, sizeof(struct addrinfo));	hints.ai_flags = AI_CANONNAME;	hints.ai_family = AF_INET6;	hints.ai_socktype = SOCK_RAW;	hints.ai_protocol = IPPROTO_ICMPV6;	ret_ga = getaddrinfo(target, NULL, &hints, &res);	if (ret_ga) {		fprintf(stderr, "ping6: %s\n", gai_strerror(ret_ga));		exit(1);	}	if (res->ai_canonname)		hostname = res->ai_canonname;	else		hostname = target;	if (!res->ai_addr)		errx(1, "getaddrinfo failed");	(void)memcpy(&dst, res->ai_addr, res->ai_addrlen);	if ((s = socket(res->ai_family, res->ai_socktype,	    res->ai_protocol)) < 0)		err(1, "socket");	/*	 * let the kerel pass extension headers of incoming packets,	 * for privileged socket options	 */	if ((options & F_VERBOSE) != 0) {		int opton = 1;#ifdef IPV6_RECVHOPOPTS		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton,		    sizeof(opton)))			err(1, "setsockopt(IPV6_RECVHOPOPTS)");#else  /* old adv. API */		if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton,		    sizeof(opton)))			err(1, "setsockopt(IPV6_HOPOPTS)");#endif#ifdef IPV6_RECVDSTOPTS		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton,		    sizeof(opton)))			err(1, "setsockopt(IPV6_RECVDSTOPTS)");#else  /* old adv. API */		if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton,		    sizeof(opton)))			err(1, "setsockopt(IPV6_DSTOPTS)");#endif#ifdef IPV6_RECVRTHDRDSTOPTS		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton,		    sizeof(opton)))			err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)");#endif	}	/* revoke root privilege */	seteuid(getuid());	setuid(getuid());	if (options & F_FLOOD && options & F_INTERVAL)		errx(1, "-f and -i incompatible options");	if ((options & F_NOUSERDATA) == 0) {		if (datalen >= sizeof(struct timeval)) {			/* we can time transfer */			timing = 1;		} else			timing = 0;		/* in F_VERBOSE case, we may get non-echoreply packets*/		if (options & F_VERBOSE)			packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;		else			packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA;	} else {		/* suppress timing for node information query */		timing = 0;		datalen = 2048;		packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA;	}	if (!(packet = (u_char *)malloc((u_int)packlen)))		err(1, "Unable to allocate packet");	if (!(options & F_PINGFILLED))		for (i = ICMP6ECHOLEN; i < packlen; ++i)			*datap++ = i;	ident = getpid() & 0xFFFF;#ifndef __OpenBSD__	gettimeofday(&timeout, NULL);	srand((unsigned int)(timeout.tv_sec ^ timeout.tv_usec ^ (long)ident));	memset(nonce, 0, sizeof(nonce));	for (i = 0; i < sizeof(nonce); i += sizeof(int))		*((int *)&nonce[i]) = rand();#else	memset(nonce, 0, sizeof(nonce));	for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t))		*((u_int32_t *)&nonce[i]) = arc4random();#endif	hold = 1;	if (options & F_SO_DEBUG)		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,		    sizeof(hold));	optval = IPV6_DEFHLIM;	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))		if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,		    &optval, sizeof(optval)) == -1)			err(1, "IPV6_MULTICAST_HOPS");#ifdef IPV6_USE_MIN_MTU	if ((options & F_NOMINMTU) == 0) {		optval = 1;		if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,		    &optval, sizeof(optval)) == -1)			err(1, "setsockopt(IPV6_USE_MIN_MTU)");	}#ifdef IPV6_RECVPATHMTU	else {		optval = 1;		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU,		    &optval, sizeof(optval)) == -1)			err(1, "setsockopt(IPV6_RECVPATHMTU)");	}#endif /* IPV6_RECVPATHMTU */#endif /* IPV6_USE_MIN_MTU */#ifdef IPSEC#ifdef IPSEC_POLICY_IPSEC	if (options & F_POLICY) {		if (setpolicy(s, policy_in) < 0)			errx(1, "%s", ipsec_strerror());		if (setpolicy(s, policy_out) < 0)			errx(1, "%s", ipsec_strerror());	}#else	if (options & F_AUTHHDR) {		optval = IPSEC_LEVEL_REQUIRE;#ifdef IPV6_AUTH_TRANS_LEVEL		if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,		    &optval, sizeof(optval)) == -1)			err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)");#else /* old def */		if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL,		    &optval, sizeof(optval)) == -1)			err(1, "setsockopt(IPV6_AUTH_LEVEL)");#endif	}	if (options & F_ENCRYPT) {		optval = IPSEC_LEVEL_REQUIRE;		if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,		    &optval, sizeof(optval)) == -1)			err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)");	}#endif /*IPSEC_POLICY_IPSEC*/#endif#ifdef ICMP6_FILTER    {	struct icmp6_filter filt;	if (!(options & F_VERBOSE)) {		ICMP6_FILTER_SETBLOCKALL(&filt);		if ((options & F_FQDN) || (options & F_FQDNOLD) ||		    (options & F_NODEADDR) || (options & F_SUPTYPES))			ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);		else			ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);	} else {		ICMP6_FILTER_SETPASSALL(&filt);	}	if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,	    sizeof(filt)) < 0)		err(1, "setsockopt(ICMP6_FILTER)");    }#endif /*ICMP6_FILTER*/	/* let the kerel pass extension headers of incoming packets */	if ((options & F_VERBOSE) != 0) {		int opton = 1;#ifdef IPV6_RECVRTHDR		if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton,		    sizeof(opton)))			err(1, "setsockopt(IPV6_RECVRTHDR)");#else  /* old adv. API */		if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton,		    sizeof(opton)))			err(1, "setsockopt(IPV6_RTHDR)");#endif	}/*	optval = 1;	if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr))		if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,		    &optval, sizeof(optval)) == -1)			err(1, "IPV6_MULTICAST_LOOP");*/	/* Specify the outgoing interface and/or the source address */	if (usepktinfo)		ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo));	if (hoplimit != -1)		ip6optlen += CMSG_SPACE(sizeof(int));#ifdef IPV6_REACHCONF	if (options & F_REACHCONF)		ip6optlen += CMSG_SPACE(0);#endif	/* set IP6 packet options */	if (ip6optlen) {		if ((scmsg = (char *)malloc(ip6optlen)) == 0)			errx(1, "can't allocate enough memory");		smsghdr.msg_control = (caddr_t)scmsg;		smsghdr.msg_controllen = ip6optlen;		scmsgp = (struct cmsghdr *)scmsg;	}	if (usepktinfo) {		pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));		memset(pktinfo, 0, sizeof(*pktinfo));		scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));		scmsgp->cmsg_level = IPPROTO_IPV6;		scmsgp->cmsg_type = IPV6_PKTINFO;		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);	}	/* set the outgoing interface */	if (ifname) {#ifndef USE_SIN6_SCOPE_ID		/* pktinfo must have already been allocated */		if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0)			errx(1, "%s: invalid interface name", ifname);#else		if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0)			errx(1, "%s: invalid interface name", ifname);#endif	}	/* set the source address */	if (options & F_SRCADDR)/* pktinfo must be valid */		pktinfo->ipi6_addr = srcaddr;	if (hoplimit != -1) {		scmsgp->cmsg_len = CMSG_LEN(sizeof(int));		scmsgp->cmsg_level = IPPROTO_IPV6;		scmsgp->cmsg_type = IPV6_HOPLIMIT;		*(int *)(CMSG_DATA(scmsgp)) = hoplimit;		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);	}#ifdef IPV6_REACHCONF	if (options & F_REACHCONF) {		scmsgp->cmsg_len = CMSG_LEN(0);		scmsgp->cmsg_level = IPPROTO_IPV6;		scmsgp->cmsg_type = IPV6_REACHCONF;		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);	}#endif	if (argc > 1) {	/* some intermediate addrs are specified */		int hops, error;#ifdef USE_RFC2292BIS		int rthdrlen;#endif#ifdef USE_RFC2292BIS		rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);		scmsgp->cmsg_len = CMSG_LEN(rthdrlen);		scmsgp->cmsg_level = IPPROTO_IPV6;		scmsgp->cmsg_type = IPV6_RTHDR;		rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp);		rthdr = inet6_rth_init((void *)rthdr, rthdrlen,		    IPV6_RTHDR_TYPE_0, argc - 1);		if (rthdr == NULL)			errx(1, "can't initialize rthdr");#else  /* old advanced API */		if ((scmsgp = (struct cmsghdr *)inet6_rthdr_init(scmsgp,		    IPV6_RTHDR_TYPE_0)) == 0)			errx(1, "can't initialize rthdr");#endif /* USE_RFC2292BIS */		for (hops = 0; hops < argc - 1; hops++) {			struct addrinfo *iaip;			if ((error = getaddrinfo(argv[hops], NULL, &hints,			    &iaip)))				errx(1, "%s", gai_strerror(error));			if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6)				errx(1,				    "bad addr family of an intermediate addr");#ifdef USE_RFC2292BIS			if (inet6_rth_add(rthdr,			    &(SIN6(iaip->ai_addr))->sin6_addr))				errx(1, "can't add an intermediate node");#else  /* old advanced API */			if (inet6_rthdr_add(scmsgp,			    &(SIN6(iaip->ai_addr))->sin6_addr,			    IPV6_RTHDR_LOOSE))				errx(1, "can't add an intermediate node");#endif /* USE_RFC2292BIS */			freeaddrinfo(iaip);		}#ifndef USE_RFC2292BIS		if (inet6_rthdr_lasthop(scmsgp, IPV6_RTHDR_LOOSE))			errx(1, "can't set the last flag");#endif		scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp);	}	{		/*		 * source selection		 */		int dummy, len = sizeof(src);		if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)			err(1, "UDP socket");		src.sin6_family = AF_INET6;		src.sin6_addr = dst.sin6_addr;		src.sin6_port = ntohs(DUMMY_PORT);		src.sin6_scope_id = dst.sin6_scope_id;#ifdef USE_RFC2292BIS		if (pktinfo &&		    setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO,		    (void *)pktinfo, sizeof(*pktinfo)))			err(1, "UDP setsockopt(IPV6_PKTINFO)");		if (hoplimit != -1 &&		    setsockopt(dummy, IPPROTO_IPV6, IPV6_HOPLIMIT,		    (void *)&hoplimit, sizeof(hoplimit)))			err(1, "UDP setsockopt(IPV6_HOPLIMIT)");		if (rthdr &&		    setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR,		    (void *)rthdr, (rthdr->ip6r_len + 1) << 3))			err(1, "UDP setsockopt(IPV6_RTHDR)");#else  /* old advanced API */		if (smsghdr.msg_control &&		    setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTOPTIONS,		    (void *)smsghdr.msg_control, smsghdr.msg_controllen))			err(1, "UDP setsockopt(IPV6_PKTOPTIONS)");#endif		if (connect(dummy, (struct sockaddr *)&src, len) < 0)			err(1, "UDP connect");		if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0)			err(1, "getsockname");		close(dummy);	}#if defined(SO_SNDBUF) && defined(SO_RCVBUF)	if (sockbufsize) {		if (datalen > sockbufsize)			warnx("you need -b to increase socket buffer size");		if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize,		    sizeof(sockbufsize)) < 0)			err(1, "setsockopt(SO_SNDBUF)");		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize,		    sizeof(sockbufsize)) < 0)			err(1, "setsockopt(SO_RCVBUF)");	}	else {		if (datalen > 8 * 1024)	/*XXX*/			warnx("you need -b to increase socket buffer size");		/*		 * When pinging the broadcast address, you can get a lot of		 * answers. Doing something so evil is useful if you are trying		 * to stress the ethernet, or just want to fill the arp cache		 * to get some stuff for /etc/ethers.		 */		hold = 48 * 1024;		setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,		    sizeof(hold));	}#endif	optval = 1;#ifndef USE_SIN6_SCOPE_ID#ifdef IPV6_RECVPKTINFO	if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval,	    sizeof(optval)) < 0)		warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */#else  /* old adv. API */	if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval,	    sizeof(optval)) < 0)		warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */#endif#endif /* USE_SIN6_SCOPE_ID */#ifdef IPV6_RECVHOPLIMIT	if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,	    sizeof(optval)) < 0)		warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */#else  /* old adv. API */	if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,	    sizeof(optval)) < 0)		warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */#endif	printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),	    (unsigned long)(pingerlen() - 8));	printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));	printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));	while (preload--)		/* Fire off them quickies. */		(void)pinger();	(void)signal(SIGINT, onsignal);#ifdef SIGINFO	(void)signal(SIGINFO, onsignal);#endif	if ((options & F_FLOOD) == 0) {		(void)signal(SIGALRM, onsignal);		itimer.it_interval = interval;		itimer.it_value = interval;		(void)setitimer(ITIMER_REAL, &itimer, NULL);		retransmit();	}	fdmasks = howmany(s + 1, NFDBITS) * sizeof(fd_mask);	if ((fdmaskp = malloc(fdmasks)) == NULL)		err(1, "malloc");	signo = seenalrm = seenint = 0;#ifdef SIGINFO	seeninfo = 0;#endif	for (;;) {		struct msghdr m;		struct cmsghdr *cm;		u_char buf[1024];		struct iovec iov[2];		/* signal handling */		if (seenalrm) {			retransmit();			seenalrm = 0;			continue;		}		if (seenint) {			onint(SIGINT);			seenint = 0;			continue;		}#ifdef SIGINFO		if (seeninfo) {			summary();			seeninfo = 0;			continue;		}#endif		if (options & F_FLOOD) {			(void)pinger();			timeout.tv_sec = 0;			timeout.tv_usec = 10000;			tv = &timeout;		} else			tv = NULL;		memset(fdmaskp, 0, fdmasks);		FD_SET(s, fdmaskp);		cc = select(s + 1, fdmaskp, NULL, NULL, tv);		if (cc < 0) {			if (errno != EINTR) {				warn("select");				sleep(1);			}			continue;		} else if (cc == 0)			continue;		fromlen = sizeof(from);		m.msg_name = (caddr_t)&from;		m.msg_namelen = sizeof(from);		memset(&iov, 0, sizeof(iov));		iov[0].iov_base = (caddr_t)packet;		iov[0].iov_len = packlen;		m.msg_iov = iov;		m.msg_iovlen = 1;		cm = (struct cmsghdr *)buf;		m.msg_control = (caddr_t)buf;		m.msg_controllen = sizeof(buf);		cc = recvmsg(s, &m, 0);		if (cc < 0) {			if (errno != EINTR) {				warn("recvmsg");				sleep(1);			}			continue;		} else if (cc == 0) {			int mtu;			/*			 * receive control messages only. Process the			 * exceptions (currently the only possiblity is			 * a path MTU notification.)			 */			if ((mtu = get_pathmtu(&m)) > 0) {				if ((options & F_VERBOSE) != 0) {					printf("new path MTU (%d) is "					    "notified\n", mtu);				}			}			continue;		} else {			/*			 * an ICMPv6 message (probably an echoreply) arrived.			 */			pr_pack(packet, cc, &m);		}		if (npackets && nreceived >= npackets)			break;	}	summary();	exit(nreceived == 0);}voidonsignal(sig)	int sig;{	signo = sig;	switch (sig) {	case SIGALRM:		seenalrm++;		break;	case SIGINT:		seenint++;		break;#ifdef SIGINFO	case SIGINFO:		seeninfo++;		break;#endif	}}/* * retransmit -- *	This routine transmits another ping6. */voidretransmit(){	struct itimerval itimer;	if (pinger() == 0)		return;	/*	 * If we're not transmitting any more packets, change the timer	 * to wait two round-trip times if we've received any packets or	 * ten seconds if we haven't.	 */#define	MAXWAIT		10	if (nreceived) {		itimer.it_value.tv_sec =  2 * tmax / 1000;		if (itimer.it_value.tv_sec == 0)			itimer.it_value.tv_sec = 1;	} else		itimer.it_value.tv_sec = MAXWAIT;	itimer.it_interval.tv_sec = 0;	itimer.it_interval.tv_usec = 0;	itimer.it_value.tv_usec = 0;	(void)signal(SIGALRM, onint);	(void)setitimer(ITIMER_REAL, &itimer, NULL);}/* * pinger -- *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet * will be added on by the kernel.  The ID field is our UNIX process ID, * and the sequence number is an ascending integer.  The first 8 bytes * of the data portion are used to hold a UNIX "timeval" struct in VAX * byte-order, to compute the round-trip time. */size_tpingerlen(){	size_t l;	if (options & F_FQDN)		l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);	else if (options & F_FQDNOLD)		l = ICMP6_NIQLEN;	else if (options & F_NODEADDR)		l = ICMP6_NIQLEN + sizeof(dst.sin6_addr);	else if (options & F_SUPTYPES)		l = ICMP6_NIQLEN;	else		l = ICMP6ECHOLEN + datalen;	return l;}intpinger(){	struct icmp6_hdr *icp;	struct iovec iov[2];	int i, cc;	struct icmp6_nodeinfo *nip;	int seq;	if (npackets && ntransmitted >= npackets)		return(-1);	/* no more transmission */	icp = (struct icmp6_hdr *)outpack;	nip = (struct icmp6_nodeinfo *)outpack;	memset(icp, 0, sizeof(*icp));	icp->icmp6_cksum = 0;	seq = ntransmitted++;	CLR(seq % mx_dup_ck);	if (options & F_FQDN) {		icp->icmp6_type = ICMP6_NI_QUERY;		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;		nip->ni_qtype = htons(NI_QTYPE_FQDN);		nip->ni_flags = htons(0);		memcpy(nip->icmp6_ni_nonce, nonce,		    sizeof(nip->icmp6_ni_nonce));		*(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,		    sizeof(dst.sin6_addr));		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);		datalen = 0;	} else if (options & F_FQDNOLD) {		/* packet format in 03 draft - no Subject data on queries */		icp->icmp6_type = ICMP6_NI_QUERY;		icp->icmp6_code = 0;	/* code field is always 0 */		nip->ni_qtype = htons(NI_QTYPE_FQDN);		nip->ni_flags = htons(0);		memcpy(nip->icmp6_ni_nonce, nonce,		    sizeof(nip->icmp6_ni_nonce));		*(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);		cc = ICMP6_NIQLEN;		datalen = 0;	} else if (options & F_NODEADDR) {		icp->icmp6_type = ICMP6_NI_QUERY;		icp->icmp6_code = ICMP6_NI_SUBJ_IPV6;		nip->ni_qtype = htons(NI_QTYPE_NODEADDR);		nip->ni_flags = naflags;		memcpy(nip->icmp6_ni_nonce, nonce,		    sizeof(nip->icmp6_ni_nonce));		*(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);		memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr,		    sizeof(dst.sin6_addr));		cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr);		datalen = 0;	} else if (options & F_SUPTYPES) {		icp->icmp6_type = ICMP6_NI_QUERY;		icp->icmp6_code = ICMP6_NI_SUBJ_FQDN;	/*empty*/		nip->ni_qtype = htons(NI_QTYPE_SUPTYPES);		/* we support compressed bitmap */		nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS;		memcpy(nip->icmp6_ni_nonce, nonce,		    sizeof(nip->icmp6_ni_nonce));		*(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq);		cc = ICMP6_NIQLEN;		datalen = 0;	} else {		icp->icmp6_type = ICMP6_ECHO_REQUEST;		icp->icmp6_code = 0;		icp->icmp6_id = htons(ident);		icp->icmp6_seq = ntohs(seq);		if (timing)			(void)gettimeofday((struct timeval *)					   &outpack[ICMP6ECHOLEN], NULL);		cc = ICMP6ECHOLEN + datalen;	}#ifdef DIAGNOSTIC	if (pingerlen() != cc)		errx(1, "internal error; length mismatch");#endif	smsghdr.msg_name = (caddr_t)&dst;	smsghdr.msg_namelen = sizeof(dst);	memset(&iov, 0, sizeof(iov));	iov[0].iov_base = (caddr_t)outpack;	iov[0].iov_len = cc;	smsghdr.msg_iov = iov;	smsghdr.msg_iovlen = 1;	i = sendmsg(s, &smsghdr, 0);	if (i < 0 || i != cc)  {		if (i < 0)			warn("sendmsg");		(void)printf("ping6: wrote %s %d chars, ret=%d\n",		    hostname, cc, i);	}	if (!(options & F_QUIET) && options & F_FLOOD)		(void)write(STDOUT_FILENO, &DOT, 1);	return(0);}intmyechoreply(icp)	const struct icmp6_hdr *icp;{	if (ntohs(icp->icmp6_id) == ident)		return 1;	else		return 0;}intmynireply(nip)	const struct icmp6_nodeinfo *nip;{	if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t),	    nonce + sizeof(u_int16_t),	    sizeof(nonce) - sizeof(u_int16_t)) == 0)		return 1;	else		return 0;}char *dnsdecode(sp, ep, base, buf, bufsiz)	const u_char **sp;	const u_char *ep;	const u_char *base;	/*base for compressed name*/	u_char *buf;	size_t bufsiz;{	int i = 0;	const u_char *cp;	char cresult[MAXDNAME + 1];	const u_char *comp;	int l;	cp = *sp;	*buf = '\0';	if (cp >= ep)		return NULL;	while (cp < ep) {		i = *cp;		if (i == 0 || cp != *sp) {			if (strlcat(buf, ".", bufsiz) >= bufsiz)				return NULL;	/*result overrun*/		}		if (i == 0)			break;		cp++;		if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) {			/* DNS compression */			if (!base)				return NULL;			comp = base + (i & 0x3f);			if (dnsdecode(&comp, cp, base, cresult,			    sizeof(cresult)) == NULL)				return NULL;			if (strlcat(buf, cresult, bufsiz) >= bufsiz)				return NULL;	/*result overrun*/			break;		} else if ((i & 0x3f) == i) {			if (i > ep - cp)				return NULL;	/*source overrun*/			while (i-- > 0 && cp < ep) {				l = snprintf(cresult, sizeof(cresult),				    isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff);				if (l >= sizeof(cresult) || l < 0)					return NULL;				if (strlcat(buf, cresult, bufsiz) >= bufsiz)					return NULL;	/*result overrun*/				cp++;			}		} else			return NULL;	/*invalid label*/	}	if (i != 0)		return NULL;	/*not terminated*/	cp++;	*sp = cp;	return buf;}