xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/ping/ping_aux.c (revision 0406ceaaee37395222cc0d87df03b940c8946a5c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*0406ceaaSmeem  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/time.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/socket.h>
487c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
497c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
527c478bd9Sstevel@tonic-gate #include <net/if.h>
537c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
547c478bd9Sstevel@tonic-gate #include <netinet/in.h>
557c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
567c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
577c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
587c478bd9Sstevel@tonic-gate #include <netdb.h>
597c478bd9Sstevel@tonic-gate #include <stdlib.h>
607c478bd9Sstevel@tonic-gate 
61*0406ceaaSmeem #include <libinetutil.h>
627c478bd9Sstevel@tonic-gate #include "ping.h"
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * IPv4 source routing option.
677c478bd9Sstevel@tonic-gate  * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
687c478bd9Sstevel@tonic-gate  * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate struct ip_sourceroute {
717c478bd9Sstevel@tonic-gate 	uint8_t ipsr_code;
727c478bd9Sstevel@tonic-gate 	uint8_t ipsr_len;
737c478bd9Sstevel@tonic-gate 	uint8_t ipsr_ptr;
747c478bd9Sstevel@tonic-gate 	/* up to 9 IPv4 addresses */
757c478bd9Sstevel@tonic-gate 	uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate void check_reply(struct addrinfo *, struct msghdr *, int, ushort_t);
797c478bd9Sstevel@tonic-gate extern void find_dstaddr(ushort_t, union any_in_addr *);
807c478bd9Sstevel@tonic-gate extern boolean_t is_a_target(struct addrinfo *, union any_in_addr *);
817c478bd9Sstevel@tonic-gate extern char *pr_name(char *, int);
827c478bd9Sstevel@tonic-gate static void pr_options(uchar_t *, int);
837c478bd9Sstevel@tonic-gate extern char *pr_protocol(int);
847c478bd9Sstevel@tonic-gate static void pr_rropt(uchar_t *, int, boolean_t);
857c478bd9Sstevel@tonic-gate static void pr_tsopt(uchar_t *, int);
867c478bd9Sstevel@tonic-gate static char *pr_type(int);
877c478bd9Sstevel@tonic-gate extern void schedule_sigalrm();
887c478bd9Sstevel@tonic-gate extern void send_scheduled_probe();
897c478bd9Sstevel@tonic-gate extern boolean_t seq_match(ushort_t, int, ushort_t);
907c478bd9Sstevel@tonic-gate extern void sigalrm_handler();
917c478bd9Sstevel@tonic-gate void set_IPv4_options(int, union any_in_addr *, int, struct in_addr *,
927c478bd9Sstevel@tonic-gate     struct in_addr *);
937c478bd9Sstevel@tonic-gate extern void tvsub(struct timeval *, struct timeval *);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Set IPv4 options
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate void
997c478bd9Sstevel@tonic-gate set_IPv4_options(int sock, union any_in_addr *gw_IP_list, int gw_count,
1007c478bd9Sstevel@tonic-gate     struct in_addr *src, struct in_addr *dst)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	int req_size;
1037c478bd9Sstevel@tonic-gate 	char srr[ROUTE_SIZE + 1];
1047c478bd9Sstevel@tonic-gate 	char *bufp;
1057c478bd9Sstevel@tonic-gate 	int optsize = ROUTE_SIZE;
1067c478bd9Sstevel@tonic-gate 	struct ip_sourceroute *srp;
1077c478bd9Sstevel@tonic-gate 	struct ip_timestamp *tsp;
1087c478bd9Sstevel@tonic-gate 	int i;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (rr_option || ts_option || gw_count > 0) {
1117c478bd9Sstevel@tonic-gate 		bzero(srr, sizeof (srr));
1127c478bd9Sstevel@tonic-gate 		bufp = srr;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		if (gw_count > 0) {
1157c478bd9Sstevel@tonic-gate 			/* 3 = 1 (code) + 1 (len) + 1 (ptr) of src route opt. */
1167c478bd9Sstevel@tonic-gate 			req_size = 3 + (sizeof (struct in_addr)) * gw_count;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 			if (optsize < req_size) {
1197c478bd9Sstevel@tonic-gate 				Fprintf(stderr, "%s: too many IPv4 gateways\n",
1207c478bd9Sstevel@tonic-gate 				    progname);
1217c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1227c478bd9Sstevel@tonic-gate 			}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 			srp = (struct ip_sourceroute *)bufp;
1257c478bd9Sstevel@tonic-gate 			srp->ipsr_code = strict ? IPOPT_SSRR : IPOPT_LSRR;
1267c478bd9Sstevel@tonic-gate 			srp->ipsr_len = req_size;
1277c478bd9Sstevel@tonic-gate 			srp->ipsr_ptr = IPOPT_MINOFF;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 			for (i = 0; i < gw_count; i++) {
1307c478bd9Sstevel@tonic-gate 				bcopy((char *)&gw_IP_list[i].addr,
1317c478bd9Sstevel@tonic-gate 				    &srp->ipsr_addrs[i],
1327c478bd9Sstevel@tonic-gate 				    sizeof (struct in_addr));
1337c478bd9Sstevel@tonic-gate 			}
1347c478bd9Sstevel@tonic-gate 			optsize -= srp->ipsr_len;
1357c478bd9Sstevel@tonic-gate 			bufp += srp->ipsr_len;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		/* do we send a timestamp option? */
1387c478bd9Sstevel@tonic-gate 		if (ts_option) {
1397c478bd9Sstevel@tonic-gate 			if (optsize < IPOPT_MINOFF) {
1407c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
1417c478bd9Sstevel@tonic-gate 				    "%s: no room for timestamp option\n",
1427c478bd9Sstevel@tonic-gate 				    progname);
1437c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 			/* LINTED */
1467c478bd9Sstevel@tonic-gate 			tsp = (struct ip_timestamp *)bufp;
1477c478bd9Sstevel@tonic-gate 			tsp->ipt_code = IPOPT_TS;
1487c478bd9Sstevel@tonic-gate 			tsp->ipt_len = optsize;
1497c478bd9Sstevel@tonic-gate 			tsp->ipt_ptr = IPOPT_MINOFF + 1;
1507c478bd9Sstevel@tonic-gate 			tsp->ipt_flg = ts_flag & 0x0f;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 			if (tsp->ipt_flg > IPOPT_TS_TSANDADDR) {
1537c478bd9Sstevel@tonic-gate 				req_size = IPOPT_MINOFF +
1547c478bd9Sstevel@tonic-gate 				    2 * sizeof (struct ipt_ta);
1557c478bd9Sstevel@tonic-gate 				/*
1567c478bd9Sstevel@tonic-gate 				 * Note: BSD/4.X is broken in their check so we
1577c478bd9Sstevel@tonic-gate 				 * have to  bump up this number by at least one.
1587c478bd9Sstevel@tonic-gate 				 */
1597c478bd9Sstevel@tonic-gate 				req_size++;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 				if (optsize < req_size) {
1627c478bd9Sstevel@tonic-gate 					Fprintf(stderr, "%s: no room for "
1637c478bd9Sstevel@tonic-gate 					    "timestamp option\n", progname);
1647c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
1657c478bd9Sstevel@tonic-gate 				}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 				bcopy((char *)dst,
1687c478bd9Sstevel@tonic-gate 				    &tsp->ipt_timestamp.ipt_ta[0].ipt_addr,
1697c478bd9Sstevel@tonic-gate 				    sizeof (struct in_addr));
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 				bcopy((char *)src,
1727c478bd9Sstevel@tonic-gate 				    &tsp->ipt_timestamp.ipt_ta[1].ipt_addr,
1737c478bd9Sstevel@tonic-gate 				    sizeof (struct in_addr));
1747c478bd9Sstevel@tonic-gate 				tsp->ipt_len = req_size;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 			optsize -= tsp->ipt_len;
1787c478bd9Sstevel@tonic-gate 			bufp += tsp->ipt_len;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 		/* do we send a record route option? */
1817c478bd9Sstevel@tonic-gate 		if (rr_option) {
1827c478bd9Sstevel@tonic-gate 			if (optsize < IPOPT_MINOFF) {
1837c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
1847c478bd9Sstevel@tonic-gate 				    "%s: no room for record route option\n",
1857c478bd9Sstevel@tonic-gate 				    progname);
1867c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 			}
1897c478bd9Sstevel@tonic-gate 			/*
1907c478bd9Sstevel@tonic-gate 			 * Format of record route option is same as source
1917c478bd9Sstevel@tonic-gate 			 * route option.
1927c478bd9Sstevel@tonic-gate 			 */
1937c478bd9Sstevel@tonic-gate 			srp = (struct ip_sourceroute *)bufp;
1947c478bd9Sstevel@tonic-gate 			srp->ipsr_code = IPOPT_RR;
1957c478bd9Sstevel@tonic-gate 			srp->ipsr_len = optsize;
1967c478bd9Sstevel@tonic-gate 			srp->ipsr_ptr = IPOPT_MINOFF;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 			optsize -= srp->ipsr_len;
1997c478bd9Sstevel@tonic-gate 			bufp += srp->ipsr_len;
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 		optsize = bufp - srr;
2027c478bd9Sstevel@tonic-gate 		/* Round up to 4 byte boundary */
2037c478bd9Sstevel@tonic-gate 		if (optsize & 0x3)
2047c478bd9Sstevel@tonic-gate 			optsize = (optsize & ~0x3) + 4;
2057c478bd9Sstevel@tonic-gate 		if (setsockopt(sock, IPPROTO_IP, IP_OPTIONS, srr, optsize) <
2067c478bd9Sstevel@tonic-gate 		    0) {
2077c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: setsockopt IP_OPTIONS %s\n",
2087c478bd9Sstevel@tonic-gate 			    progname, strerror(errno));
2097c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * Check out the packet to see if it came from us.  This logic is necessary
2167c478bd9Sstevel@tonic-gate  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
2177c478bd9Sstevel@tonic-gate  * which arrive ('tis only fair).  This permits multiple copies of this
2187c478bd9Sstevel@tonic-gate  * program to be run without having intermingled output (or statistics!).
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate void
2217c478bd9Sstevel@tonic-gate check_reply(struct addrinfo *ai_dst, struct msghdr *msg, int cc,
2227c478bd9Sstevel@tonic-gate     ushort_t udp_src_port)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	struct ip *ip;
2257c478bd9Sstevel@tonic-gate 	struct icmp *icp;
2267c478bd9Sstevel@tonic-gate 	struct udphdr *up;
2277c478bd9Sstevel@tonic-gate 	union any_in_addr dst_addr;
2287c478bd9Sstevel@tonic-gate 	uchar_t *buf;
2297c478bd9Sstevel@tonic-gate 	int32_t *intp;
2307c478bd9Sstevel@tonic-gate 	struct sockaddr_in *from;
2317c478bd9Sstevel@tonic-gate 	struct timeval *tp;
2327c478bd9Sstevel@tonic-gate 	struct timeval tv;
2337c478bd9Sstevel@tonic-gate 	int hlen, hlen1;
2347c478bd9Sstevel@tonic-gate 	int64_t triptime;
2357c478bd9Sstevel@tonic-gate 	boolean_t valid_reply = _B_FALSE;
2367c478bd9Sstevel@tonic-gate 	boolean_t reply_matched_current_target;	/* Is the source address of */
2377c478bd9Sstevel@tonic-gate 						/* this reply same as where */
2387c478bd9Sstevel@tonic-gate 						/* we're sending currently? */
2397c478bd9Sstevel@tonic-gate 	boolean_t last_reply_from_targetaddr = _B_FALSE; /* Is this stats, */
2407c478bd9Sstevel@tonic-gate 						/* probe all with npackets>0 */
2417c478bd9Sstevel@tonic-gate 						/* and we received reply for */
2427c478bd9Sstevel@tonic-gate 						/* the last probe sent to */
2437c478bd9Sstevel@tonic-gate 						/* targetaddr */
2447c478bd9Sstevel@tonic-gate 	int cc_left;
2457c478bd9Sstevel@tonic-gate 	char tmp_buf[INET6_ADDRSTRLEN];
2467c478bd9Sstevel@tonic-gate 	static char *unreach[] = {
2477c478bd9Sstevel@tonic-gate 	    "Net Unreachable",
2487c478bd9Sstevel@tonic-gate 	    "Host Unreachable",
2497c478bd9Sstevel@tonic-gate 	    "Protocol Unreachable",
2507c478bd9Sstevel@tonic-gate 	    "Port Unreachable",
2517c478bd9Sstevel@tonic-gate 	    "Fragmentation needed and DF set",
2527c478bd9Sstevel@tonic-gate 	    "Source Route Failed",
2537c478bd9Sstevel@tonic-gate 	    /* The following are from RFC1700 */
2547c478bd9Sstevel@tonic-gate 	    "Net Unknown",
2557c478bd9Sstevel@tonic-gate 	    "Host Unknown",
2567c478bd9Sstevel@tonic-gate 	    "Source Host Isolated",
2577c478bd9Sstevel@tonic-gate 	    "Dest Net Prohibited",
2587c478bd9Sstevel@tonic-gate 	    "Dest Host Prohibited",
2597c478bd9Sstevel@tonic-gate 	    "Net Unreachable for TOS",
2607c478bd9Sstevel@tonic-gate 	    "Host Unreachable for TOS",
2617c478bd9Sstevel@tonic-gate 	    "Communication Administratively Prohibited",
2627c478bd9Sstevel@tonic-gate 	    "Host Precedence Violation",
2637c478bd9Sstevel@tonic-gate 	    "Precedence Cutoff in Effect"
2647c478bd9Sstevel@tonic-gate 	};
2657c478bd9Sstevel@tonic-gate 	static char *redirect[] = {
2667c478bd9Sstevel@tonic-gate 	    "Net",
2677c478bd9Sstevel@tonic-gate 	    "Host",
2687c478bd9Sstevel@tonic-gate 	    "TOS Net",
2697c478bd9Sstevel@tonic-gate 	    "TOS Host"
2707c478bd9Sstevel@tonic-gate 	};
2717c478bd9Sstevel@tonic-gate 	static char *timexceed[] = {
2727c478bd9Sstevel@tonic-gate 	    "Time exceeded in transit",
2737c478bd9Sstevel@tonic-gate 	    "Time exceeded during reassembly"
2747c478bd9Sstevel@tonic-gate 	};
2757c478bd9Sstevel@tonic-gate 	boolean_t print_newline = _B_FALSE;
2767c478bd9Sstevel@tonic-gate 	int i;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* decompose msghdr into useful pieces */
2797c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)msg->msg_iov->iov_base;
2807c478bd9Sstevel@tonic-gate 	from = (struct sockaddr_in *)msg->msg_name;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* LINTED */
2837c478bd9Sstevel@tonic-gate 	intp = (int32_t *)buf;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&tv, (struct timezone *)NULL);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/* LINTED */
2887c478bd9Sstevel@tonic-gate 	ip = (struct ip *)buf;
2897c478bd9Sstevel@tonic-gate 	hlen = ip->ip_hl << 2;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if ((cc < sizeof (struct ip)) || (cc < hlen + ICMP_MINLEN)) {
2927c478bd9Sstevel@tonic-gate 		if (verbose) {
2937c478bd9Sstevel@tonic-gate 			Printf("packet too short (%d bytes) from %s\n", cc,
2947c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 		return;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	cc -= hlen;
3007c478bd9Sstevel@tonic-gate 	/* LINTED */
3017c478bd9Sstevel@tonic-gate 	icp = (struct icmp *)(buf + hlen);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (ip->ip_p == 0) {
3047c478bd9Sstevel@tonic-gate 		/*
3057c478bd9Sstevel@tonic-gate 		 * Assume that we are running on a pre-4.3BSD system
3067c478bd9Sstevel@tonic-gate 		 * such as SunOS before 4.0
3077c478bd9Sstevel@tonic-gate 		 */
3087c478bd9Sstevel@tonic-gate 		/* LINTED */
3097c478bd9Sstevel@tonic-gate 		icp = (struct icmp *)buf;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	cc_left = cc - ICMP_MINLEN;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	switch (icp->icmp_type) {
3147c478bd9Sstevel@tonic-gate 	case ICMP_UNREACH:
3157c478bd9Sstevel@tonic-gate 		ip = &icp->icmp_ip;
3167c478bd9Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		/* check if we have enough of the packet to work on */
3197c478bd9Sstevel@tonic-gate 		if ((cc_left < sizeof (struct ip)) ||
3207c478bd9Sstevel@tonic-gate 		    (cc_left < hlen1 + sizeof (struct udphdr))) {
3217c478bd9Sstevel@tonic-gate 			if (verbose) {
3227c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
3237c478bd9Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
3247c478bd9Sstevel@tonic-gate 				    AF_INET));
3257c478bd9Sstevel@tonic-gate 			}
3267c478bd9Sstevel@tonic-gate 			return;
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		/* get the UDP packet */
3307c478bd9Sstevel@tonic-gate 		cc_left -= hlen1 + sizeof (struct udphdr);
3317c478bd9Sstevel@tonic-gate 		/* LINTED */
3327c478bd9Sstevel@tonic-gate 		up = (struct udphdr *)((uchar_t *)ip + hlen1);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		/* check to see if this is what we sent */
3357c478bd9Sstevel@tonic-gate 		if (icp->icmp_code == ICMP_UNREACH_PORT &&
3367c478bd9Sstevel@tonic-gate 		    ip->ip_p == IPPROTO_UDP &&
3377c478bd9Sstevel@tonic-gate 		    udp_src_port == up->uh_sport &&
3387c478bd9Sstevel@tonic-gate 		    use_udp) {
3397c478bd9Sstevel@tonic-gate 			valid_reply = _B_TRUE;
3407c478bd9Sstevel@tonic-gate 		} else {
3417c478bd9Sstevel@tonic-gate 			valid_reply = _B_FALSE;
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		if (valid_reply) {
3457c478bd9Sstevel@tonic-gate 			/*
3467c478bd9Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
3477c478bd9Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
3487c478bd9Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
3497c478bd9Sstevel@tonic-gate 			 */
3507c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
3517c478bd9Sstevel@tonic-gate 			is_alive = _B_TRUE;
3527c478bd9Sstevel@tonic-gate 			nreceived++;
3537c478bd9Sstevel@tonic-gate 			reply_matched_current_target =
3547c478bd9Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
3557c478bd9Sstevel@tonic-gate 				current_targetaddr->num_sent,
3567c478bd9Sstevel@tonic-gate 				ntohs(up->uh_dport));
3577c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
3587c478bd9Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
3597c478bd9Sstevel@tonic-gate 				nreceived_last_target++;
3607c478bd9Sstevel@tonic-gate 				/*
3617c478bd9Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
3627c478bd9Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
3637c478bd9Sstevel@tonic-gate 				 * the last probe we sent to current target
3647c478bd9Sstevel@tonic-gate 				 * address.
3657c478bd9Sstevel@tonic-gate 				 */
3667c478bd9Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
3677c478bd9Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
3687c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
3697c478bd9Sstevel@tonic-gate 				    (MAX_PORT + 1) == ntohs(up->uh_dport)) &&
3707c478bd9Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
3717c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_sent))
3727c478bd9Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
3737c478bd9Sstevel@tonic-gate 			} else {
3747c478bd9Sstevel@tonic-gate 				/*
3757c478bd9Sstevel@tonic-gate 				 * If it's just probe_all and we just received
3767c478bd9Sstevel@tonic-gate 				 * a reply from a target address we were
3777c478bd9Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
3787c478bd9Sstevel@tonic-gate 				 * some other target address), we ignore
3797c478bd9Sstevel@tonic-gate 				 * this reply.
3807c478bd9Sstevel@tonic-gate 				 */
3817c478bd9Sstevel@tonic-gate 				if (probe_all && !stats) {
3827c478bd9Sstevel@tonic-gate 					valid_reply = _B_FALSE;
3837c478bd9Sstevel@tonic-gate 					/*
3847c478bd9Sstevel@tonic-gate 					 * Only if it's verbose, we get a
3857c478bd9Sstevel@tonic-gate 					 * message regarding this reply,
3867c478bd9Sstevel@tonic-gate 					 * otherwise we are done here.
3877c478bd9Sstevel@tonic-gate 					 */
3887c478bd9Sstevel@tonic-gate 					if (!verbose) {
3897c478bd9Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
3907c478bd9Sstevel@tonic-gate 						return;
3917c478bd9Sstevel@tonic-gate 					}
3927c478bd9Sstevel@tonic-gate 				}
3937c478bd9Sstevel@tonic-gate 			}
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		/* stats mode doesn't print 'alive' messages */
3977c478bd9Sstevel@tonic-gate 		if (valid_reply && !stats) {
3987c478bd9Sstevel@tonic-gate 			/*
3997c478bd9Sstevel@tonic-gate 			 * if we are still sending to the same target address,
4007c478bd9Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
4017c478bd9Sstevel@tonic-gate 			 */
4027c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
4037c478bd9Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
4047c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
4057c478bd9Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
4067c478bd9Sstevel@tonic-gate 			}
4077c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 			if (!probe_all) {
4107c478bd9Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
4117c478bd9Sstevel@tonic-gate 			} else {
4127c478bd9Sstevel@tonic-gate 				(void) inet_ntop(AF_INET, (void *)&ip->ip_dst,
4137c478bd9Sstevel@tonic-gate 				    tmp_buf, sizeof (tmp_buf));
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 				if (nflag) {
4167c478bd9Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
4177c478bd9Sstevel@tonic-gate 				} else {
4187c478bd9Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
4197c478bd9Sstevel@tonic-gate 					    targethost, tmp_buf);
4207c478bd9Sstevel@tonic-gate 				}
4217c478bd9Sstevel@tonic-gate 			}
4227c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
4237c478bd9Sstevel@tonic-gate 				/*
4247c478bd9Sstevel@tonic-gate 				 * Let's get things going again, but now
4257c478bd9Sstevel@tonic-gate 				 * ping will start sending to next target IP
4267c478bd9Sstevel@tonic-gate 				 * address.
4277c478bd9Sstevel@tonic-gate 				 */
4287c478bd9Sstevel@tonic-gate 				send_scheduled_probe();
4297c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
4307c478bd9Sstevel@tonic-gate 				schedule_sigalrm();
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			return;
4337c478bd9Sstevel@tonic-gate 		} else {
4347c478bd9Sstevel@tonic-gate 			/*
4357c478bd9Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
4367c478bd9Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
4377c478bd9Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
4387c478bd9Sstevel@tonic-gate 			 * call (see below) takes longer.
4397c478bd9Sstevel@tonic-gate 			 */
4407c478bd9Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
4417c478bd9Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
4427c478bd9Sstevel@tonic-gate 			/* else, we'll release it later */
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
4467c478bd9Sstevel@tonic-gate 		if (valid_reply) {
4477c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
4487c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
4497c478bd9Sstevel@tonic-gate 			Printf("udp_port=%d. ", ntohs(up->uh_dport));
4507c478bd9Sstevel@tonic-gate 			print_newline = _B_TRUE;
4517c478bd9Sstevel@tonic-gate 		} else if (is_a_target(ai_dst, &dst_addr) || verbose) {
4527c478bd9Sstevel@tonic-gate 			if (icp->icmp_code >= A_CNT(unreach)) {
4537c478bd9Sstevel@tonic-gate 				Printf("ICMP %d Unreachable from gateway %s\n",
4547c478bd9Sstevel@tonic-gate 				    icp->icmp_code,
4557c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
4567c478bd9Sstevel@tonic-gate 			} else {
4577c478bd9Sstevel@tonic-gate 				Printf("ICMP %s from gateway %s\n",
4587c478bd9Sstevel@tonic-gate 				    unreach[icp->icmp_code],
4597c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
4607c478bd9Sstevel@tonic-gate 			}
4617c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
4627c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
4637c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
4647c478bd9Sstevel@tonic-gate 			if (ip->ip_p == IPPROTO_TCP ||
4657c478bd9Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) {
4667c478bd9Sstevel@tonic-gate 				Printf(" port %d ", ntohs(up->uh_dport));
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 			print_newline = _B_TRUE;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		/* if we are timing and the reply has a timeval */
4727c478bd9Sstevel@tonic-gate 		if (valid_reply && datalen >= sizeof (struct timeval) &&
4737c478bd9Sstevel@tonic-gate 		    cc_left >= sizeof (struct timeval)) {
4747c478bd9Sstevel@tonic-gate 			/* LINTED */
4757c478bd9Sstevel@tonic-gate 			tp = (struct timeval *)((char *)up +
4767c478bd9Sstevel@tonic-gate 			    sizeof (struct udphdr));
4777c478bd9Sstevel@tonic-gate 			(void) tvsub(&tv, tp);
4787c478bd9Sstevel@tonic-gate 			triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
4797c478bd9Sstevel@tonic-gate 			Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
4807c478bd9Sstevel@tonic-gate 			tsum += triptime;
4817c478bd9Sstevel@tonic-gate 			tsum2 += triptime*triptime;
4827c478bd9Sstevel@tonic-gate 			if (triptime < tmin)
4837c478bd9Sstevel@tonic-gate 				tmin = triptime;
4847c478bd9Sstevel@tonic-gate 			if (triptime > tmax)
4857c478bd9Sstevel@tonic-gate 				tmax = triptime;
4867c478bd9Sstevel@tonic-gate 			print_newline = _B_TRUE;
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 		if (print_newline)
4897c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
4907c478bd9Sstevel@tonic-gate 		/*
4917c478bd9Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
4927c478bd9Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
4937c478bd9Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
4947c478bd9Sstevel@tonic-gate 		 * address, now!
4957c478bd9Sstevel@tonic-gate 		 */
4967c478bd9Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
4977c478bd9Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
4987c478bd9Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
4997c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
5007c478bd9Sstevel@tonic-gate 			send_scheduled_probe();
5017c478bd9Sstevel@tonic-gate 			schedule_sigalrm();
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 		break;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	case ICMP_REDIRECT:
5067c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
5077c478bd9Sstevel@tonic-gate 			if (verbose) {
5087c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
5097c478bd9Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
5107c478bd9Sstevel@tonic-gate 				    AF_INET));
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 			return;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		ip = &icp->icmp_ip;
5167c478bd9Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
5177c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
5187c478bd9Sstevel@tonic-gate 			if (icp->icmp_code >= A_CNT(redirect)) {
5197c478bd9Sstevel@tonic-gate 				Printf("ICMP %d redirect from gateway %s\n",
5207c478bd9Sstevel@tonic-gate 				    icp->icmp_code,
5217c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
5227c478bd9Sstevel@tonic-gate 			} else {
5237c478bd9Sstevel@tonic-gate 				Printf("ICMP %s redirect from gateway %s\n",
5247c478bd9Sstevel@tonic-gate 				    redirect[icp->icmp_code],
5257c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 			Printf(" to %s",
5287c478bd9Sstevel@tonic-gate 			    pr_name((char *)&icp->icmp_gwaddr, AF_INET));
5297c478bd9Sstevel@tonic-gate 			Printf(" for %s\n",
5307c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_dst, AF_INET));
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 		break;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	case ICMP_ECHOREPLY:
5357c478bd9Sstevel@tonic-gate 		if (ntohs(icp->icmp_id) == ident) {
5367c478bd9Sstevel@tonic-gate 			if (!use_udp && !use_icmp_ts)
5377c478bd9Sstevel@tonic-gate 				valid_reply = _B_TRUE;
5387c478bd9Sstevel@tonic-gate 			else
5397c478bd9Sstevel@tonic-gate 				valid_reply = _B_FALSE;
5407c478bd9Sstevel@tonic-gate 		} else {
5417c478bd9Sstevel@tonic-gate 			return;
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		if (valid_reply) {
5457c478bd9Sstevel@tonic-gate 			/*
5467c478bd9Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
5477c478bd9Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
5487c478bd9Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
5497c478bd9Sstevel@tonic-gate 			 */
5507c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
5517c478bd9Sstevel@tonic-gate 			is_alive = _B_TRUE;
5527c478bd9Sstevel@tonic-gate 			nreceived++;
5537c478bd9Sstevel@tonic-gate 			reply_matched_current_target =
5547c478bd9Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
5557c478bd9Sstevel@tonic-gate 				current_targetaddr->num_sent,
5567c478bd9Sstevel@tonic-gate 				ntohs(icp->icmp_seq));
5577c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
5587c478bd9Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
5597c478bd9Sstevel@tonic-gate 				nreceived_last_target++;
5607c478bd9Sstevel@tonic-gate 				/*
5617c478bd9Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
5627c478bd9Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
5637c478bd9Sstevel@tonic-gate 				 * the last probe we sent to current target
5647c478bd9Sstevel@tonic-gate 				 * address.
5657c478bd9Sstevel@tonic-gate 				 */
5667c478bd9Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
5677c478bd9Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
5687c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
5697c478bd9Sstevel@tonic-gate 				    (MAX_ICMP_SEQ + 1) ==
5707c478bd9Sstevel@tonic-gate 				    ntohs(icp->icmp_seq)) &&
5717c478bd9Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
5727c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_sent))
5737c478bd9Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
5747c478bd9Sstevel@tonic-gate 			} else {
5757c478bd9Sstevel@tonic-gate 				/*
5767c478bd9Sstevel@tonic-gate 				 * If it's just probe_all and we just received
5777c478bd9Sstevel@tonic-gate 				 * a reply from a target address we were
5787c478bd9Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
5797c478bd9Sstevel@tonic-gate 				 * some other target address), we ignore
5807c478bd9Sstevel@tonic-gate 				 * this reply.
5817c478bd9Sstevel@tonic-gate 				 */
5827c478bd9Sstevel@tonic-gate 				if (probe_all && !stats) {
5837c478bd9Sstevel@tonic-gate 					valid_reply = _B_FALSE;
5847c478bd9Sstevel@tonic-gate 					/*
5857c478bd9Sstevel@tonic-gate 					 * Only if it's verbose, we get a
5867c478bd9Sstevel@tonic-gate 					 * message regarding this reply,
5877c478bd9Sstevel@tonic-gate 					 * otherwise we are done here.
5887c478bd9Sstevel@tonic-gate 					 */
5897c478bd9Sstevel@tonic-gate 					if (!verbose) {
5907c478bd9Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
5917c478bd9Sstevel@tonic-gate 						return;
5927c478bd9Sstevel@tonic-gate 					}
5937c478bd9Sstevel@tonic-gate 				}
5947c478bd9Sstevel@tonic-gate 			}
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		if (!stats && valid_reply) {
5987c478bd9Sstevel@tonic-gate 			/*
5997c478bd9Sstevel@tonic-gate 			 * if we are still sending to the same target address,
6007c478bd9Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
6017c478bd9Sstevel@tonic-gate 			 */
6027c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
6037c478bd9Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
6047c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
6057c478bd9Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
6067c478bd9Sstevel@tonic-gate 			}
6077c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 			if (!probe_all) {
6107c478bd9Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
6117c478bd9Sstevel@tonic-gate 			} else {
6127c478bd9Sstevel@tonic-gate 				/*
6137c478bd9Sstevel@tonic-gate 				 * If we are using send_reply, the real
6147c478bd9Sstevel@tonic-gate 				 * target address is not the src address of the
6157c478bd9Sstevel@tonic-gate 				 * replies. Use icmp_seq to find out where this
6167c478bd9Sstevel@tonic-gate 				 * probe was sent to.
6177c478bd9Sstevel@tonic-gate 				 */
6187c478bd9Sstevel@tonic-gate 				if (send_reply) {
6197c478bd9Sstevel@tonic-gate 					(void) find_dstaddr(
6207c478bd9Sstevel@tonic-gate 					    ntohs(icp->icmp_seq), &dst_addr);
6217c478bd9Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
6227c478bd9Sstevel@tonic-gate 					    (void *)&dst_addr.addr,
6237c478bd9Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
6247c478bd9Sstevel@tonic-gate 				} else {
6257c478bd9Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
6267c478bd9Sstevel@tonic-gate 					    (void *)&from->sin_addr,
6277c478bd9Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
6287c478bd9Sstevel@tonic-gate 				}
6297c478bd9Sstevel@tonic-gate 				if (nflag) {
6307c478bd9Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
6317c478bd9Sstevel@tonic-gate 				} else {
6327c478bd9Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
6337c478bd9Sstevel@tonic-gate 					    targethost, tmp_buf);
6347c478bd9Sstevel@tonic-gate 				}
6357c478bd9Sstevel@tonic-gate 			}
6367c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
6377c478bd9Sstevel@tonic-gate 				/*
6387c478bd9Sstevel@tonic-gate 				 * Let's get things going again, but now
6397c478bd9Sstevel@tonic-gate 				 * ping will start sending to next target IP
6407c478bd9Sstevel@tonic-gate 				 * address.
6417c478bd9Sstevel@tonic-gate 				 */
6427c478bd9Sstevel@tonic-gate 				send_scheduled_probe();
6437c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
6447c478bd9Sstevel@tonic-gate 				schedule_sigalrm();
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 			return;
6477c478bd9Sstevel@tonic-gate 		} else {
6487c478bd9Sstevel@tonic-gate 			/*
6497c478bd9Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
6507c478bd9Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
6517c478bd9Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
6527c478bd9Sstevel@tonic-gate 			 * call (see below) takes longer.
6537c478bd9Sstevel@tonic-gate 			 */
6547c478bd9Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
6557c478bd9Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
6567c478bd9Sstevel@tonic-gate 			/* else, we'll release it later */
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 		/*
6597c478bd9Sstevel@tonic-gate 		 * If we are using send_reply, the real target address is
6607c478bd9Sstevel@tonic-gate 		 * not the src address of the replies. Use icmp_seq to find out
6617c478bd9Sstevel@tonic-gate 		 * where this probe was sent to.
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if (send_reply) {
6647c478bd9Sstevel@tonic-gate 			(void) find_dstaddr(ntohs(icp->icmp_seq), &dst_addr);
6657c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
6667c478bd9Sstevel@tonic-gate 			    pr_name((char *)&dst_addr.addr,  AF_INET));
6677c478bd9Sstevel@tonic-gate 		} else {
6687c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
6697c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 		Printf("icmp_seq=%d. ", ntohs(icp->icmp_seq));
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		if (valid_reply && datalen >= sizeof (struct timeval) &&
6747c478bd9Sstevel@tonic-gate 		    cc_left >= sizeof (struct timeval)) {
6757c478bd9Sstevel@tonic-gate 			/* LINTED */
6767c478bd9Sstevel@tonic-gate 			tp = (struct timeval *)&icp->icmp_data[0];
6777c478bd9Sstevel@tonic-gate 			(void) tvsub(&tv, tp);
6787c478bd9Sstevel@tonic-gate 			triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
6797c478bd9Sstevel@tonic-gate 			Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
6807c478bd9Sstevel@tonic-gate 			tsum += triptime;
6817c478bd9Sstevel@tonic-gate 			tsum2 += triptime*triptime;
6827c478bd9Sstevel@tonic-gate 			if (triptime < tmin)
6837c478bd9Sstevel@tonic-gate 				tmin = triptime;
6847c478bd9Sstevel@tonic-gate 			if (triptime > tmax)
6857c478bd9Sstevel@tonic-gate 				tmax = triptime;
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		/*
6907c478bd9Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
6917c478bd9Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
6927c478bd9Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
6937c478bd9Sstevel@tonic-gate 		 * address, now!
6947c478bd9Sstevel@tonic-gate 		 */
6957c478bd9Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
6967c478bd9Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
6977c478bd9Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
6987c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
6997c478bd9Sstevel@tonic-gate 			send_scheduled_probe();
7007c478bd9Sstevel@tonic-gate 			schedule_sigalrm();
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		break;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	case ICMP_SOURCEQUENCH:
7057c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
7067c478bd9Sstevel@tonic-gate 			if (verbose) {
7077c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
7087c478bd9Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
7097c478bd9Sstevel@tonic-gate 				    AF_INET));
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 			return;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 		ip = &icp->icmp_ip;
7147c478bd9Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
7157c478bd9Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
7167c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
7177c478bd9Sstevel@tonic-gate 			Printf("ICMP Source Quench from %s\n",
7187c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
7197c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
7207c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
7217c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 			/*
7247c478bd9Sstevel@tonic-gate 			 * if it's a UDP or TCP packet, we need at least first
7257c478bd9Sstevel@tonic-gate 			 * 4 bytes of it to see the src/dst ports
7267c478bd9Sstevel@tonic-gate 			 */
7277c478bd9Sstevel@tonic-gate 			if ((ip->ip_p == IPPROTO_TCP ||
7287c478bd9Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) &&
7297c478bd9Sstevel@tonic-gate 			    (cc_left >= hlen1 + 4)) {
7307c478bd9Sstevel@tonic-gate 				/* LINTED */
7317c478bd9Sstevel@tonic-gate 				up = (struct udphdr *)((uchar_t *)ip + hlen1);
7327c478bd9Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
7337c478bd9Sstevel@tonic-gate 			}
7347c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
7357c478bd9Sstevel@tonic-gate 		}
7367c478bd9Sstevel@tonic-gate 		break;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	case ICMP_PARAMPROB:
7397c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
7407c478bd9Sstevel@tonic-gate 			if (verbose) {
7417c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
7427c478bd9Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
7437c478bd9Sstevel@tonic-gate 				    AF_INET));
7447c478bd9Sstevel@tonic-gate 			}
7457c478bd9Sstevel@tonic-gate 			return;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 		ip = &icp->icmp_ip;
7487c478bd9Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
7497c478bd9Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
7507c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
7517c478bd9Sstevel@tonic-gate 			switch (icp->icmp_code) {
7527c478bd9Sstevel@tonic-gate 			case ICMP_PARAMPROB_OPTABSENT:
7537c478bd9Sstevel@tonic-gate 				Printf("ICMP Missing a Required Option "
7547c478bd9Sstevel@tonic-gate 				    "parameter problem from %s\n",
7557c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
7567c478bd9Sstevel@tonic-gate 				Printf(" option type = %d", icp->icmp_pptr);
7577c478bd9Sstevel@tonic-gate 				break;
7587c478bd9Sstevel@tonic-gate 			case ICMP_PARAMPROB_BADLENGTH:
7597c478bd9Sstevel@tonic-gate 				Printf("ICMP Bad Length parameter problem "
7607c478bd9Sstevel@tonic-gate 				    "from %s\n",
7617c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
7627c478bd9Sstevel@tonic-gate 				Printf(" in byte %d", icp->icmp_pptr);
7637c478bd9Sstevel@tonic-gate 				if (icp->icmp_pptr <= hlen1) {
7647c478bd9Sstevel@tonic-gate 					Printf(" (value 0x%x)",
7657c478bd9Sstevel@tonic-gate 					    *((char *)ip + icp->icmp_pptr));
7667c478bd9Sstevel@tonic-gate 				}
7677c478bd9Sstevel@tonic-gate 				break;
7687c478bd9Sstevel@tonic-gate 			case 0:
7697c478bd9Sstevel@tonic-gate 			default:
7707c478bd9Sstevel@tonic-gate 				Printf("ICMP Parameter Problem from %s\n",
7717c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
7727c478bd9Sstevel@tonic-gate 				Printf(" in byte %d", icp->icmp_pptr);
7737c478bd9Sstevel@tonic-gate 				if (icp->icmp_pptr <= hlen1) {
7747c478bd9Sstevel@tonic-gate 					Printf(" (value 0x%x)",
7757c478bd9Sstevel@tonic-gate 					    *((char *)ip + icp->icmp_pptr));
7767c478bd9Sstevel@tonic-gate 				}
7777c478bd9Sstevel@tonic-gate 				break;
7787c478bd9Sstevel@tonic-gate 			}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
7817c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
7827c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 			/*
7857c478bd9Sstevel@tonic-gate 			 * if it's a UDP or TCP packet, we need at least first
7867c478bd9Sstevel@tonic-gate 			 * 4 bytes of it to see the src/dst ports
7877c478bd9Sstevel@tonic-gate 			 */
7887c478bd9Sstevel@tonic-gate 			if ((ip->ip_p == IPPROTO_TCP ||
7897c478bd9Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) &&
7907c478bd9Sstevel@tonic-gate 			    (cc_left >= hlen1 + 4)) {
7917c478bd9Sstevel@tonic-gate 				/* LINTED */
7927c478bd9Sstevel@tonic-gate 				up = (struct udphdr *)((uchar_t *)ip + hlen1);
7937c478bd9Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
7947c478bd9Sstevel@tonic-gate 			}
7957c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 		break;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	case ICMP_TIMXCEED:
8007c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
8017c478bd9Sstevel@tonic-gate 			if (verbose) {
8027c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
8037c478bd9Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
8047c478bd9Sstevel@tonic-gate 				    AF_INET));
8057c478bd9Sstevel@tonic-gate 			}
8067c478bd9Sstevel@tonic-gate 			return;
8077c478bd9Sstevel@tonic-gate 		}
8087c478bd9Sstevel@tonic-gate 		ip = &icp->icmp_ip;
8097c478bd9Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
8107c478bd9Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
8117c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
8127c478bd9Sstevel@tonic-gate 			if (icp->icmp_code >= A_CNT(timexceed)) {
8137c478bd9Sstevel@tonic-gate 				Printf("ICMP %d time exceeded from %s\n",
8147c478bd9Sstevel@tonic-gate 				    icp->icmp_code,
8157c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
8167c478bd9Sstevel@tonic-gate 			} else {
8177c478bd9Sstevel@tonic-gate 				Printf("ICMP %s from %s\n",
8187c478bd9Sstevel@tonic-gate 				    timexceed[icp->icmp_code],
8197c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
8207c478bd9Sstevel@tonic-gate 			}
8217c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
8227c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
8237c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
8247c478bd9Sstevel@tonic-gate 			if ((ip->ip_p == IPPROTO_TCP ||
8257c478bd9Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) &&
8267c478bd9Sstevel@tonic-gate 			    (cc_left >= hlen1 + 4)) {
8277c478bd9Sstevel@tonic-gate 				/* LINTED */
8287c478bd9Sstevel@tonic-gate 				up = (struct udphdr *)((uchar_t *)ip + hlen1);
8297c478bd9Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
8307c478bd9Sstevel@tonic-gate 			}
8317c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate 		break;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	case ICMP_TSTAMPREPLY:
8367c478bd9Sstevel@tonic-gate 		/* the packet should have enough space to store timestamps */
8377c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (struct id_ts)) {
8387c478bd9Sstevel@tonic-gate 			if (verbose) {
8397c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
8407c478bd9Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
8417c478bd9Sstevel@tonic-gate 				    AF_INET));
8427c478bd9Sstevel@tonic-gate 			}
8437c478bd9Sstevel@tonic-gate 			return;
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 		if (ntohs(icp->icmp_id) == ident) {
8477c478bd9Sstevel@tonic-gate 			if (use_icmp_ts)
8487c478bd9Sstevel@tonic-gate 				valid_reply = _B_TRUE;
8497c478bd9Sstevel@tonic-gate 			else
8507c478bd9Sstevel@tonic-gate 				valid_reply = _B_FALSE;
8517c478bd9Sstevel@tonic-gate 		} else {
8527c478bd9Sstevel@tonic-gate 			return;
8537c478bd9Sstevel@tonic-gate 		}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		if (valid_reply) {
8567c478bd9Sstevel@tonic-gate 			/*
8577c478bd9Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
8587c478bd9Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
8597c478bd9Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
8607c478bd9Sstevel@tonic-gate 			 */
8617c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
8627c478bd9Sstevel@tonic-gate 			is_alive = _B_TRUE;
8637c478bd9Sstevel@tonic-gate 			nreceived++;
8647c478bd9Sstevel@tonic-gate 			reply_matched_current_target =
8657c478bd9Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
8667c478bd9Sstevel@tonic-gate 				current_targetaddr->num_sent,
8677c478bd9Sstevel@tonic-gate 				ntohs(icp->icmp_seq));
8687c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
8697c478bd9Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
8707c478bd9Sstevel@tonic-gate 				nreceived_last_target++;
8717c478bd9Sstevel@tonic-gate 				/*
8727c478bd9Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
8737c478bd9Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
8747c478bd9Sstevel@tonic-gate 				 * the last probe we sent to current target
8757c478bd9Sstevel@tonic-gate 				 * address.
8767c478bd9Sstevel@tonic-gate 				 */
8777c478bd9Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
8787c478bd9Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
8797c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
8807c478bd9Sstevel@tonic-gate 				    (MAX_ICMP_SEQ + 1) ==
8817c478bd9Sstevel@tonic-gate 				    ntohs(icp->icmp_seq)) &&
8827c478bd9Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
8837c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_sent))
8847c478bd9Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
8857c478bd9Sstevel@tonic-gate 			} else {
8867c478bd9Sstevel@tonic-gate 				/*
8877c478bd9Sstevel@tonic-gate 				 * If it's just probe_all and we just received
8887c478bd9Sstevel@tonic-gate 				 * a reply from a target address we were
8897c478bd9Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
8907c478bd9Sstevel@tonic-gate 				 * some other target address), we ignore
8917c478bd9Sstevel@tonic-gate 				 * this reply.
8927c478bd9Sstevel@tonic-gate 				 */
8937c478bd9Sstevel@tonic-gate 				if (probe_all && !stats) {
8947c478bd9Sstevel@tonic-gate 					valid_reply = _B_FALSE;
8957c478bd9Sstevel@tonic-gate 					/*
8967c478bd9Sstevel@tonic-gate 					 * Only if it's verbose, we get a
8977c478bd9Sstevel@tonic-gate 					 * message regarding this reply,
8987c478bd9Sstevel@tonic-gate 					 * otherwise we are done here.
8997c478bd9Sstevel@tonic-gate 					 */
9007c478bd9Sstevel@tonic-gate 					if (!verbose) {
9017c478bd9Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
9027c478bd9Sstevel@tonic-gate 						return;
9037c478bd9Sstevel@tonic-gate 					}
9047c478bd9Sstevel@tonic-gate 				}
9057c478bd9Sstevel@tonic-gate 			}
9067c478bd9Sstevel@tonic-gate 		}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		if (!stats && valid_reply) {
9097c478bd9Sstevel@tonic-gate 			/*
9107c478bd9Sstevel@tonic-gate 			 * if we are still sending to the same target address,
9117c478bd9Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
9127c478bd9Sstevel@tonic-gate 			 */
9137c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
9147c478bd9Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
9157c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
9167c478bd9Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
9177c478bd9Sstevel@tonic-gate 			}
9187c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 			if (!probe_all) {
9217c478bd9Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
9227c478bd9Sstevel@tonic-gate 			} else {
9237c478bd9Sstevel@tonic-gate 				/*
9247c478bd9Sstevel@tonic-gate 				 * If we are using send_reply, the real
9257c478bd9Sstevel@tonic-gate 				 * target address is not the src address of the
9267c478bd9Sstevel@tonic-gate 				 * replies. Use icmp_seq to find out where this
9277c478bd9Sstevel@tonic-gate 				 * probe was sent to.
9287c478bd9Sstevel@tonic-gate 				 */
9297c478bd9Sstevel@tonic-gate 				if (send_reply) {
9307c478bd9Sstevel@tonic-gate 					(void) find_dstaddr(
9317c478bd9Sstevel@tonic-gate 					    ntohs(icp->icmp_seq), &dst_addr);
9327c478bd9Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
9337c478bd9Sstevel@tonic-gate 					    (void *)&dst_addr.addr,
9347c478bd9Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
9357c478bd9Sstevel@tonic-gate 				} else {
9367c478bd9Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
9377c478bd9Sstevel@tonic-gate 					    (void *)&from->sin_addr,
9387c478bd9Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
9397c478bd9Sstevel@tonic-gate 				}
9407c478bd9Sstevel@tonic-gate 				if (nflag) {
9417c478bd9Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
9427c478bd9Sstevel@tonic-gate 				} else {
9437c478bd9Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
9447c478bd9Sstevel@tonic-gate 					    targethost, tmp_buf);
9457c478bd9Sstevel@tonic-gate 				}
9467c478bd9Sstevel@tonic-gate 			}
9477c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
9487c478bd9Sstevel@tonic-gate 				/*
9497c478bd9Sstevel@tonic-gate 				 * Let's get things going again, but now
9507c478bd9Sstevel@tonic-gate 				 * ping will start sending to next target IP
9517c478bd9Sstevel@tonic-gate 				 * address.
9527c478bd9Sstevel@tonic-gate 				 */
9537c478bd9Sstevel@tonic-gate 				send_scheduled_probe();
9547c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
9557c478bd9Sstevel@tonic-gate 				schedule_sigalrm();
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 			return;
9587c478bd9Sstevel@tonic-gate 		} else {
9597c478bd9Sstevel@tonic-gate 			/*
9607c478bd9Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
9617c478bd9Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
9627c478bd9Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
9637c478bd9Sstevel@tonic-gate 			 * call (see below) takes longer.
9647c478bd9Sstevel@tonic-gate 			 */
9657c478bd9Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
9667c478bd9Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
9677c478bd9Sstevel@tonic-gate 			/* else, we'll release it later */
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		/*
9717c478bd9Sstevel@tonic-gate 		 * If we are using send_reply, the real target address is
9727c478bd9Sstevel@tonic-gate 		 * not the src address of the replies. Use icmp_seq to find out
9737c478bd9Sstevel@tonic-gate 		 * where this probe was sent to.
9747c478bd9Sstevel@tonic-gate 		 */
9757c478bd9Sstevel@tonic-gate 		if (send_reply) {
9767c478bd9Sstevel@tonic-gate 			(void) find_dstaddr(ntohs(icp->icmp_seq), &dst_addr);
9777c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
9787c478bd9Sstevel@tonic-gate 			    pr_name((char *)&dst_addr.addr,  AF_INET));
9797c478bd9Sstevel@tonic-gate 		} else {
9807c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
9817c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 		Printf("icmp_seq=%d. ", ntohs(icp->icmp_seq));
9847c478bd9Sstevel@tonic-gate 		Printf("orig = %lu, recv = %lu, xmit = %lu ",
9857c478bd9Sstevel@tonic-gate 		    (ulong_t)ntohl(icp->icmp_otime),
9867c478bd9Sstevel@tonic-gate 		    (ulong_t)ntohl(icp->icmp_rtime),
9877c478bd9Sstevel@tonic-gate 		    (ulong_t)ntohl(icp->icmp_ttime));
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		if (valid_reply) {
9907c478bd9Sstevel@tonic-gate 			/*
9917c478bd9Sstevel@tonic-gate 			 * icp->icmp_otime is the time passed since midnight.
9927c478bd9Sstevel@tonic-gate 			 * Therefore we need to adjust tv value, which is
9937c478bd9Sstevel@tonic-gate 			 * the time passed since Jan 1, 1970.
9947c478bd9Sstevel@tonic-gate 			 */
9957c478bd9Sstevel@tonic-gate 			triptime = (tv.tv_sec % (24LL * 60 * 60)) * MILLISEC +
9967c478bd9Sstevel@tonic-gate 			    (tv.tv_usec / (MICROSEC/MILLISEC));
9977c478bd9Sstevel@tonic-gate 			triptime -= ntohl(icp->icmp_otime);
9987c478bd9Sstevel@tonic-gate 			if (triptime < 0)
9997c478bd9Sstevel@tonic-gate 				triptime += 24LL * 60 * 60 * MILLISEC;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 			Printf("time=%d. ms", (int)triptime);
10027c478bd9Sstevel@tonic-gate 			triptime *= (MICROSEC/MILLISEC);
10037c478bd9Sstevel@tonic-gate 			tsum += triptime;
10047c478bd9Sstevel@tonic-gate 			tsum2 += triptime*triptime;
10057c478bd9Sstevel@tonic-gate 			if (triptime < tmin)
10067c478bd9Sstevel@tonic-gate 				tmin = triptime;
10077c478bd9Sstevel@tonic-gate 			if (triptime > tmax)
10087c478bd9Sstevel@tonic-gate 				tmax = triptime;
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
10117c478bd9Sstevel@tonic-gate 		/*
10127c478bd9Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
10137c478bd9Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
10147c478bd9Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
10157c478bd9Sstevel@tonic-gate 		 * address, now!
10167c478bd9Sstevel@tonic-gate 		 */
10177c478bd9Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
10187c478bd9Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
10197c478bd9Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
10207c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
10217c478bd9Sstevel@tonic-gate 			send_scheduled_probe();
10227c478bd9Sstevel@tonic-gate 			schedule_sigalrm();
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 		break;
10257c478bd9Sstevel@tonic-gate 	case ICMP_ROUTERADVERT:
10267c478bd9Sstevel@tonic-gate 	case ICMP_ROUTERSOLICIT:
10277c478bd9Sstevel@tonic-gate 		/* Router discovery messages */
10287c478bd9Sstevel@tonic-gate 		return;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	case ICMP_ECHO:
10317c478bd9Sstevel@tonic-gate 	case ICMP_TSTAMP:
10327c478bd9Sstevel@tonic-gate 	case ICMP_IREQ:
10337c478bd9Sstevel@tonic-gate 	case ICMP_MASKREQ:
10347c478bd9Sstevel@tonic-gate 		/* These were never passed out from the SunOS 4.X kernel. */
10357c478bd9Sstevel@tonic-gate 		return;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	case ICMP_IREQREPLY:
10387c478bd9Sstevel@tonic-gate 	case ICMP_MASKREPLY:
10397c478bd9Sstevel@tonic-gate 		/* Replies for information and address mask requests */
10407c478bd9Sstevel@tonic-gate 		return;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	default:
10437c478bd9Sstevel@tonic-gate 		if (verbose) {
10447c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s:\n", cc,
10457c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
10467c478bd9Sstevel@tonic-gate 			Printf("icmp_type=%d (%s) ",
10477c478bd9Sstevel@tonic-gate 			    icp->icmp_type, pr_type(icp->icmp_type));
10487c478bd9Sstevel@tonic-gate 			Printf("icmp_code=%d\n", icp->icmp_code);
10497c478bd9Sstevel@tonic-gate 			for (i = 0; i < 12; i++) {
10507c478bd9Sstevel@tonic-gate 				Printf("x%2.2x: x%8.8x\n",
10517c478bd9Sstevel@tonic-gate 				    i * sizeof (int32_t), *intp++);
10527c478bd9Sstevel@tonic-gate 			}
10537c478bd9Sstevel@tonic-gate 		}
10547c478bd9Sstevel@tonic-gate 		break;
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	buf += sizeof (struct ip);
10587c478bd9Sstevel@tonic-gate 	hlen -= sizeof (struct ip);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* if verbose and there exists IP options */
10617c478bd9Sstevel@tonic-gate 	if (verbose && hlen > 0)
10627c478bd9Sstevel@tonic-gate 		pr_options((uchar_t *)buf, hlen);
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate  * Print out the ip options.
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate static void
10697c478bd9Sstevel@tonic-gate pr_options(uchar_t *opt, int optlength)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	int curlength;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	Printf("  IP options: ");
10747c478bd9Sstevel@tonic-gate 	while (optlength > 0) {
10757c478bd9Sstevel@tonic-gate 		curlength = opt[1];
10767c478bd9Sstevel@tonic-gate 		switch (*opt) {
10777c478bd9Sstevel@tonic-gate 		case IPOPT_EOL:
10787c478bd9Sstevel@tonic-gate 			optlength = 0;
10797c478bd9Sstevel@tonic-gate 			break;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 		case IPOPT_NOP:
10827c478bd9Sstevel@tonic-gate 			opt++;
10837c478bd9Sstevel@tonic-gate 			optlength--;
10847c478bd9Sstevel@tonic-gate 			continue;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 		case IPOPT_RR:
10877c478bd9Sstevel@tonic-gate 			Printf(" <record route> ");
10887c478bd9Sstevel@tonic-gate 			pr_rropt(opt, curlength, _B_TRUE);
10897c478bd9Sstevel@tonic-gate 			break;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		case IPOPT_TS:
10927c478bd9Sstevel@tonic-gate 			Printf(" <time stamp> ");
10937c478bd9Sstevel@tonic-gate 			pr_tsopt(opt, curlength);
10947c478bd9Sstevel@tonic-gate 			break;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		case IPOPT_SECURITY:
10977c478bd9Sstevel@tonic-gate 			Printf(" <security>");
10987c478bd9Sstevel@tonic-gate 			break;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		case IPOPT_LSRR:
11017c478bd9Sstevel@tonic-gate 			Printf(" <loose source route> ");
11027c478bd9Sstevel@tonic-gate 			pr_rropt(opt, curlength, _B_FALSE);
11037c478bd9Sstevel@tonic-gate 			break;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		case IPOPT_SATID:
11067c478bd9Sstevel@tonic-gate 			Printf(" <stream id>");
11077c478bd9Sstevel@tonic-gate 			break;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 		case IPOPT_SSRR:
11107c478bd9Sstevel@tonic-gate 			Printf(" <strict source route> ");
11117c478bd9Sstevel@tonic-gate 			pr_rropt(opt, curlength, _B_FALSE);
11127c478bd9Sstevel@tonic-gate 			break;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 		default:
11157c478bd9Sstevel@tonic-gate 			Printf(" <option %d, len %d>", *opt, curlength);
11167c478bd9Sstevel@tonic-gate 			break;
11177c478bd9Sstevel@tonic-gate 		}
11187c478bd9Sstevel@tonic-gate 		/*
11197c478bd9Sstevel@tonic-gate 		 * Following most options comes a length field
11207c478bd9Sstevel@tonic-gate 		 */
11217c478bd9Sstevel@tonic-gate 		opt += curlength;
11227c478bd9Sstevel@tonic-gate 		optlength -= curlength;
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * Print out a recorded route option. If rrflag is _B_TRUE, it prints record
11297c478bd9Sstevel@tonic-gate  * route option, otherwise LSRR/SSRR.
11307c478bd9Sstevel@tonic-gate  */
11317c478bd9Sstevel@tonic-gate static void
11327c478bd9Sstevel@tonic-gate pr_rropt(uchar_t *opt, int length, boolean_t rrflag)
11337c478bd9Sstevel@tonic-gate {
11347c478bd9Sstevel@tonic-gate 	struct ip_sourceroute *rrp;
11357c478bd9Sstevel@tonic-gate 	int sr_index = 0;
11367c478bd9Sstevel@tonic-gate 	struct in_addr addr;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	rrp = (struct ip_sourceroute *)opt;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	/* data starts at offset 3 */
11417c478bd9Sstevel@tonic-gate 	length -= 3;
11427c478bd9Sstevel@tonic-gate 	while (length > 0) {
11437c478bd9Sstevel@tonic-gate 		/*
11447c478bd9Sstevel@tonic-gate 		 * Let's see if we are examining the addr pointed by ipsr_ptr
11457c478bd9Sstevel@tonic-gate 		 */
11467c478bd9Sstevel@tonic-gate 		if ((rrp->ipsr_ptr == (sr_index + 1) * sizeof (addr)) &&
11477c478bd9Sstevel@tonic-gate 		    rrflag) {
11487c478bd9Sstevel@tonic-gate 			Printf(" (End of record)");
11497c478bd9Sstevel@tonic-gate 			break;
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 		bcopy(&rrp->ipsr_addrs[sr_index], &addr, sizeof (addr));
11537c478bd9Sstevel@tonic-gate 		Printf("%s", pr_name((char *)&addr, AF_INET));
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		if (rrp->ipsr_ptr == (sr_index + 1) * sizeof (addr)) {
11567c478bd9Sstevel@tonic-gate 			Printf("(Current)");
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 		sr_index++;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		length -= sizeof (addr);
11627c478bd9Sstevel@tonic-gate 		if (length > 0)
11637c478bd9Sstevel@tonic-gate 			Printf(", ");
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate /*
11687c478bd9Sstevel@tonic-gate  * Print out a timestamp option.
11697c478bd9Sstevel@tonic-gate  */
11707c478bd9Sstevel@tonic-gate static void
11717c478bd9Sstevel@tonic-gate pr_tsopt(uchar_t *opt, int length)
11727c478bd9Sstevel@tonic-gate {
11737c478bd9Sstevel@tonic-gate 	boolean_t address_present;
11747c478bd9Sstevel@tonic-gate 	boolean_t rrflag;		/* End at current entry? */
11757c478bd9Sstevel@tonic-gate 	struct ip_timestamp *tsp;
11767c478bd9Sstevel@tonic-gate 	int ts_index = 0;
11777c478bd9Sstevel@tonic-gate 	struct in_addr addr;
11787c478bd9Sstevel@tonic-gate 	size_t data_len;
11797c478bd9Sstevel@tonic-gate 	int32_t time;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/* LINTED */
11827c478bd9Sstevel@tonic-gate 	tsp = (struct ip_timestamp *)opt;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	switch (tsp->ipt_flg) {
11857c478bd9Sstevel@tonic-gate 	case IPOPT_TS_TSONLY:
11867c478bd9Sstevel@tonic-gate 		address_present = _B_FALSE;
11877c478bd9Sstevel@tonic-gate 		data_len = sizeof (tsp->ipt_timestamp.ipt_time[0]);
11887c478bd9Sstevel@tonic-gate 		rrflag = _B_TRUE;
11897c478bd9Sstevel@tonic-gate 		break;
11907c478bd9Sstevel@tonic-gate 	case IPOPT_TS_TSANDADDR:
11917c478bd9Sstevel@tonic-gate 		address_present = _B_TRUE;
11927c478bd9Sstevel@tonic-gate 		data_len = sizeof (tsp->ipt_timestamp.ipt_ta[0]);
11937c478bd9Sstevel@tonic-gate 		rrflag = _B_TRUE;
11947c478bd9Sstevel@tonic-gate 		break;
11957c478bd9Sstevel@tonic-gate 	case IPOPT_TS_PRESPEC:
11967c478bd9Sstevel@tonic-gate 	case 3:
11977c478bd9Sstevel@tonic-gate 		address_present = _B_TRUE;
11987c478bd9Sstevel@tonic-gate 		data_len = sizeof (tsp->ipt_timestamp.ipt_ta[0]);
11997c478bd9Sstevel@tonic-gate 		rrflag = _B_FALSE;
12007c478bd9Sstevel@tonic-gate 		break;
12017c478bd9Sstevel@tonic-gate 	default:
12027c478bd9Sstevel@tonic-gate 		Printf("(Bad flag value: 0x%x)", tsp->ipt_flg);
12037c478bd9Sstevel@tonic-gate 		return;
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate 	if (tsp->ipt_oflw > 0)
12067c478bd9Sstevel@tonic-gate 		Printf("(Overflow: %d) ", tsp->ipt_oflw);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/* data starts at offset 4 */
12097c478bd9Sstevel@tonic-gate 	length -= 4;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	while (length > 0) {
12127c478bd9Sstevel@tonic-gate 		if (length < data_len)
12137c478bd9Sstevel@tonic-gate 			break;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		/* the minimum value of ipt_ptr is 5 */
12167c478bd9Sstevel@tonic-gate 		if ((tsp->ipt_ptr == ts_index * data_len + 5) && rrflag) {
12177c478bd9Sstevel@tonic-gate 			Printf(" (End of record)");
12187c478bd9Sstevel@tonic-gate 			break;
12197c478bd9Sstevel@tonic-gate 		}
12207c478bd9Sstevel@tonic-gate 		if (address_present) {
12217c478bd9Sstevel@tonic-gate 			bcopy(&tsp->ipt_timestamp.ipt_ta[ts_index].ipt_addr,
12227c478bd9Sstevel@tonic-gate 			    &addr, sizeof (addr));
12237c478bd9Sstevel@tonic-gate 			Printf("%s: ", pr_name((char *)&addr, AF_INET));
12247c478bd9Sstevel@tonic-gate 			bcopy(&tsp->ipt_timestamp.ipt_ta[ts_index].ipt_time,
12257c478bd9Sstevel@tonic-gate 			    &time, sizeof (time));
12267c478bd9Sstevel@tonic-gate 		} else {
12277c478bd9Sstevel@tonic-gate 			bcopy(&tsp->ipt_timestamp.ipt_time[ts_index],
12287c478bd9Sstevel@tonic-gate 			    &time, sizeof (time));
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 		Printf("%d", ntohl(time));
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		if (tsp->ipt_ptr == ts_index * data_len + 5)
12337c478bd9Sstevel@tonic-gate 			Printf("(Current)");
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		ts_index++;
12367c478bd9Sstevel@tonic-gate 		length -= data_len;
12377c478bd9Sstevel@tonic-gate 		if (length > 0)
12387c478bd9Sstevel@tonic-gate 			Printf(", ");
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate /*
12437c478bd9Sstevel@tonic-gate  * Convert an ICMP "type" field to a printable string.
12447c478bd9Sstevel@tonic-gate  */
12457c478bd9Sstevel@tonic-gate static char *
12467c478bd9Sstevel@tonic-gate pr_type(int icmp_type)
12477c478bd9Sstevel@tonic-gate {
12487c478bd9Sstevel@tonic-gate 	static struct icmptype_table ttab[] = {
12497c478bd9Sstevel@tonic-gate 		{ICMP_ECHOREPLY,	"Echo Reply"},
12507c478bd9Sstevel@tonic-gate 		{1,			"ICMP 1"},
12517c478bd9Sstevel@tonic-gate 		{2,			"ICMP 2"},
12527c478bd9Sstevel@tonic-gate 		{ICMP_UNREACH,		"Dest Unreachable"},
12537c478bd9Sstevel@tonic-gate 		{ICMP_SOURCEQUENCH,	"Source Quench"},
12547c478bd9Sstevel@tonic-gate 		{ICMP_REDIRECT,		"Redirect"},
12557c478bd9Sstevel@tonic-gate 		{6,			"ICMP 6"},
12567c478bd9Sstevel@tonic-gate 		{7,			"ICMP 7"},
12577c478bd9Sstevel@tonic-gate 		{ICMP_ECHO,		"Echo"},
12587c478bd9Sstevel@tonic-gate 		{ICMP_ROUTERADVERT,	"Router Advertisement"},
12597c478bd9Sstevel@tonic-gate 		{ICMP_ROUTERSOLICIT,	"Router Solicitation"},
12607c478bd9Sstevel@tonic-gate 		{ICMP_TIMXCEED,		"Time Exceeded"},
12617c478bd9Sstevel@tonic-gate 		{ICMP_PARAMPROB,	"Parameter Problem"},
12627c478bd9Sstevel@tonic-gate 		{ICMP_TSTAMP,		"Timestamp"},
12637c478bd9Sstevel@tonic-gate 		{ICMP_TSTAMPREPLY,	"Timestamp Reply"},
12647c478bd9Sstevel@tonic-gate 		{ICMP_IREQ,		"Info Request"},
12657c478bd9Sstevel@tonic-gate 		{ICMP_IREQREPLY,	"Info Reply"},
12667c478bd9Sstevel@tonic-gate 		{ICMP_MASKREQ,		"Netmask Request"},
12677c478bd9Sstevel@tonic-gate 		{ICMP_MASKREPLY,	"Netmask Reply"}
12687c478bd9Sstevel@tonic-gate 	};
12697c478bd9Sstevel@tonic-gate 	int i;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	for (i = 0; i < A_CNT(ttab); i++) {
12727c478bd9Sstevel@tonic-gate 		if (ttab[i].type == icmp_type)
12737c478bd9Sstevel@tonic-gate 			return (ttab[i].message);
12747c478bd9Sstevel@tonic-gate 	}
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	return ("OUT-OF-RANGE");
12777c478bd9Sstevel@tonic-gate }
1278