1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
8*0Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
9*0Sstevel@tonic-gate  *
10*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
11*0Sstevel@tonic-gate  * modification, are permitted provided that: (1) source code distributions
12*0Sstevel@tonic-gate  * retain the above copyright notice and this paragraph in its entirety, (2)
13*0Sstevel@tonic-gate  * distributions including binary code include the above copyright notice and
14*0Sstevel@tonic-gate  * this paragraph in its entirety in the documentation or other materials
15*0Sstevel@tonic-gate  * provided with the distribution, and (3) all advertising materials mentioning
16*0Sstevel@tonic-gate  * features or use of this software display the following acknowledgement:
17*0Sstevel@tonic-gate  * ``This product includes software developed by the University of California,
18*0Sstevel@tonic-gate  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
19*0Sstevel@tonic-gate  * the University nor the names of its contributors may be used to endorse
20*0Sstevel@tonic-gate  * or promote products derived from this software without specific prior
21*0Sstevel@tonic-gate  * written permission.
22*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
23*0Sstevel@tonic-gate  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
24*0Sstevel@tonic-gate  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/param.h>
33*0Sstevel@tonic-gate #include <sys/file.h>
34*0Sstevel@tonic-gate #include <sys/ioctl.h>
35*0Sstevel@tonic-gate #include <sys/socket.h>
36*0Sstevel@tonic-gate #include <sys/time.h>
37*0Sstevel@tonic-gate #include <sys/sysmacros.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <netinet/in_systm.h>
40*0Sstevel@tonic-gate #include <netinet/in.h>
41*0Sstevel@tonic-gate #include <netinet/ip.h>
42*0Sstevel@tonic-gate #include <netinet/ip_var.h>
43*0Sstevel@tonic-gate #include <netinet/ip_icmp.h>
44*0Sstevel@tonic-gate #include <netinet/udp.h>
45*0Sstevel@tonic-gate #include <netinet/udp_var.h>
46*0Sstevel@tonic-gate #include <netinet/ip6.h>
47*0Sstevel@tonic-gate #include <netinet/icmp6.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include <arpa/inet.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <ctype.h>
52*0Sstevel@tonic-gate #include <errno.h>
53*0Sstevel@tonic-gate #include <malloc.h>
54*0Sstevel@tonic-gate #include <memory.h>
55*0Sstevel@tonic-gate #include <netdb.h>
56*0Sstevel@tonic-gate #include <stdio.h>
57*0Sstevel@tonic-gate #include <stdlib.h>
58*0Sstevel@tonic-gate #include <strings.h>
59*0Sstevel@tonic-gate #include <unistd.h>
60*0Sstevel@tonic-gate #include <libintl.h>
61*0Sstevel@tonic-gate #include <locale.h>
62*0Sstevel@tonic-gate #include <signal.h>
63*0Sstevel@tonic-gate #include <setjmp.h>
64*0Sstevel@tonic-gate #include <limits.h>
65*0Sstevel@tonic-gate #include <zone.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #include <priv_utils.h>
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #include <ifaddrlist.h>
71*0Sstevel@tonic-gate #include "traceroute.h"
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #define	MAX_SEQ			65535	/* max sequence value for ICMP */
74*0Sstevel@tonic-gate #define	MAX_TRAFFIC_CLASS	255	/* max traffic class for IPv6 */
75*0Sstevel@tonic-gate #define	MAX_FLOW_LABEL		0xFFFFF	/* max flow label for IPv6 */
76*0Sstevel@tonic-gate #define	MAX_TOS			255	/* max type-of-service for IPv4 */
77*0Sstevel@tonic-gate #define	STR_LEN			30
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /* store the information about a host */
80*0Sstevel@tonic-gate struct hostinfo {
81*0Sstevel@tonic-gate 	char *name;		/* hostname */
82*0Sstevel@tonic-gate 	int family;		/* address family of the IP addresses */
83*0Sstevel@tonic-gate 	int num_addr;			/* number of IP addresses */
84*0Sstevel@tonic-gate 	union any_in_addr *addrs;	/* list of IP addresses */
85*0Sstevel@tonic-gate };
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /* used to store a bunch of protocol specific values */
88*0Sstevel@tonic-gate struct pr_set {
89*0Sstevel@tonic-gate 	int family;		/* AF_INET or AF_INET6 */
90*0Sstevel@tonic-gate 	char name[STR_LEN];	/* "IPv4" or "IPv6" */
91*0Sstevel@tonic-gate 	char icmp[STR_LEN];	/* "icmp" or "ipv6-icmp" */
92*0Sstevel@tonic-gate 	int icmp_minlen;
93*0Sstevel@tonic-gate 	int addr_len;
94*0Sstevel@tonic-gate 	int ip_hdr_len;
95*0Sstevel@tonic-gate 	int packlen;
96*0Sstevel@tonic-gate 	int sock_size;		/* size of sockaddr_in or sockaddr_in6 */
97*0Sstevel@tonic-gate 	struct sockaddr *to;
98*0Sstevel@tonic-gate 	struct sockaddr *from;
99*0Sstevel@tonic-gate 	void *from_sin_addr;
100*0Sstevel@tonic-gate 	union any_in_addr *gwIPlist;
101*0Sstevel@tonic-gate 	/* pointers to v4/v6 functions */
102*0Sstevel@tonic-gate 	struct ip *(*set_buffers_fn) (int);
103*0Sstevel@tonic-gate 	int (*check_reply_fn)(struct msghdr *, int, int, uchar_t *, uchar_t *);
104*0Sstevel@tonic-gate 	boolean_t (*print_icmp_other_fn)(uchar_t, uchar_t);
105*0Sstevel@tonic-gate 	void (*print_addr_fn)(uchar_t *, int, struct sockaddr *);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate };
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * LBNL bug fixed: in LBNL traceroute 'uchar_t packet[512];'
111*0Sstevel@tonic-gate  * Not sufficient to hold the complete packet for ECHO REPLY of a big probe.
112*0Sstevel@tonic-gate  * Packet size is reported incorrectly in such a case.
113*0Sstevel@tonic-gate  * Also this buffer needs to be 32 bit aligned. In the future the alignment
114*0Sstevel@tonic-gate  * requirement will be increased to 64 bit. So, let's use 64 bit alignment now.
115*0Sstevel@tonic-gate  */
116*0Sstevel@tonic-gate static uint64_t packet[(IP_MAXPACKET + 1)/8];	/* received packet */
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate static struct ip *outip4;	/* output buffer to send as an IPv4 datagram */
119*0Sstevel@tonic-gate static struct ip *outip6;	/* output buffer to send as an IPv6 datagram */
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /* Used to store the ancillary data that comes with the received packets */
122*0Sstevel@tonic-gate static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate /* first get the gw names, later you'll resolve them based on the family */
125*0Sstevel@tonic-gate static char *gwlist[MAXMAX_GWS];		/* gateway names list */
126*0Sstevel@tonic-gate static union any_in_addr gwIPlist[MAX_GWS];	/* gateway IPv4 address list */
127*0Sstevel@tonic-gate static union any_in_addr gwIP6list[MAX_GWS6];	/* gateway IPv6 address list */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate static int family_input = AF_UNSPEC;	/* User supplied protocol family */
130*0Sstevel@tonic-gate static int rcvsock4;		/* receive (icmp) socket file descriptor */
131*0Sstevel@tonic-gate static int sndsock4;		/* send (udp/icmp) socket file descriptor */
132*0Sstevel@tonic-gate static int rcvsock6;		/* receive (icmp6) socket file descriptor */
133*0Sstevel@tonic-gate static int sndsock6;		/* send (udp6/icmp6) socket file descriptor */
134*0Sstevel@tonic-gate int gw_count = 0;		/* number of gateways */
135*0Sstevel@tonic-gate static struct sockaddr_in whereto;	/* Who to try to reach */
136*0Sstevel@tonic-gate static struct sockaddr_in6 whereto6;
137*0Sstevel@tonic-gate static struct sockaddr_in wherefrom;	/* Who we are */
138*0Sstevel@tonic-gate static struct sockaddr_in6 wherefrom6;
139*0Sstevel@tonic-gate static int packlen_input = 0;		/* user input for packlen */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate char *prog;
142*0Sstevel@tonic-gate static char *source_input = NULL; /* this is user arg. source, doesn't change */
143*0Sstevel@tonic-gate static char *source = NULL;	/* this gets modified after name lookup */
144*0Sstevel@tonic-gate char *hostname;
145*0Sstevel@tonic-gate static char *device = NULL;   	/* interface name */
146*0Sstevel@tonic-gate static struct pr_set *pr4;	/* protocol info for IPv4 */
147*0Sstevel@tonic-gate static struct pr_set *pr6;	/* protocol info for IPv6 */
148*0Sstevel@tonic-gate static struct ifaddrlist *al4;	/* list of interfaces */
149*0Sstevel@tonic-gate static struct ifaddrlist *al6;	/* list of interfaces */
150*0Sstevel@tonic-gate static uint_t if_index = 0;	/* interface index */
151*0Sstevel@tonic-gate static int num_v4 = 0;		/* count of IPv4 addresses */
152*0Sstevel@tonic-gate static int num_v6 = 0;		/* count of IPv6 addresses */
153*0Sstevel@tonic-gate static int num_ifs4 = 0;	/* count of local IPv4 interfaces */
154*0Sstevel@tonic-gate static int num_ifs6 = 0;	/* count of local IPv6 interfaces */
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate static int nprobes = 3;		/* number of probes */
157*0Sstevel@tonic-gate static int max_ttl = 30;	/* max number of hops */
158*0Sstevel@tonic-gate static int first_ttl = 1;	/* initial number of hops */
159*0Sstevel@tonic-gate ushort_t ident;			/* used to authenticate replies */
160*0Sstevel@tonic-gate ushort_t port = 32768 + 666;	/* start udp dest port # for probe packets */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate static int options = 0;		/* socket options */
163*0Sstevel@tonic-gate boolean_t verbose = _B_FALSE;	/* verbose output */
164*0Sstevel@tonic-gate static int waittime = 5;	/* time to wait for response (in seconds) */
165*0Sstevel@tonic-gate static struct timeval delay = {0, 0}; /* delay between consecutive probe */
166*0Sstevel@tonic-gate boolean_t nflag = _B_FALSE;	/* print addresses numerically */
167*0Sstevel@tonic-gate static boolean_t showttl = _B_FALSE; /* print the ttl(hop limit) of recvd pkt */
168*0Sstevel@tonic-gate boolean_t useicmp = _B_FALSE;  	/* use icmp echo instead of udp packets */
169*0Sstevel@tonic-gate boolean_t docksum = _B_TRUE;	/* calculate checksums */
170*0Sstevel@tonic-gate static boolean_t collect_stat = _B_FALSE;	/* print statistics */
171*0Sstevel@tonic-gate boolean_t settos = _B_FALSE;   	/* set type-of-service field */
172*0Sstevel@tonic-gate static int max_timeout = 5;	/* quit after this consecutive timeouts */
173*0Sstevel@tonic-gate static boolean_t probe_all = _B_FALSE;	/* probe all the IFs of the target */
174*0Sstevel@tonic-gate static boolean_t pick_src = _B_FALSE;	/* traceroute picks the src address */
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate  * flow and class are specific to IPv6, tos and off are specific to IPv4.
178*0Sstevel@tonic-gate  * Each protocol uses the ones that are specific to itself, and ignores
179*0Sstevel@tonic-gate  * others.
180*0Sstevel@tonic-gate  */
181*0Sstevel@tonic-gate static uint_t flow = 0;		/* IPv6 flow info */
182*0Sstevel@tonic-gate static uint_t class = 0;	/* IPv6 class */
183*0Sstevel@tonic-gate uchar_t tos = 0;		/* IPv4 type-of-service */
184*0Sstevel@tonic-gate ushort_t off = 0;		/* set DF bit */
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate static jmp_buf env;		/* stack environment for longjmp() */
187*0Sstevel@tonic-gate boolean_t raw_req;		/* if sndsock for IPv4 must be raw */
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate /* Forwards */
190*0Sstevel@tonic-gate static uint_t calc_packetlen(int, struct pr_set *);
191*0Sstevel@tonic-gate extern int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
192*0Sstevel@tonic-gate extern int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *);
193*0Sstevel@tonic-gate static double deltaT(struct timeval *, struct timeval *);
194*0Sstevel@tonic-gate static char *device_name(struct ifaddrlist *, int, union any_in_addr *,
195*0Sstevel@tonic-gate     struct pr_set *);
196*0Sstevel@tonic-gate extern void *find_ancillary_data(struct msghdr *, int, int);
197*0Sstevel@tonic-gate static boolean_t has_addr(struct addrinfo *, union any_in_addr *);
198*0Sstevel@tonic-gate static struct ifaddrlist *find_device(struct ifaddrlist *, int, char *);
199*0Sstevel@tonic-gate static struct ifaddrlist *find_ifaddr(struct ifaddrlist *, int,
200*0Sstevel@tonic-gate     union any_in_addr *, int);
201*0Sstevel@tonic-gate static void get_gwaddrs(char **, int, union any_in_addr *,
202*0Sstevel@tonic-gate     union any_in_addr *, int *, int *);
203*0Sstevel@tonic-gate static void get_hostinfo(char *, int, struct addrinfo **);
204*0Sstevel@tonic-gate char *inet_name(union any_in_addr *, int);
205*0Sstevel@tonic-gate ushort_t in_cksum(ushort_t *, int);
206*0Sstevel@tonic-gate extern int ip_hdr_length_v6(ip6_t *, int, uint8_t *);
207*0Sstevel@tonic-gate void main(int, char **);
208*0Sstevel@tonic-gate extern char *pr_type(uchar_t);
209*0Sstevel@tonic-gate extern char *pr_type6(uchar_t);
210*0Sstevel@tonic-gate extern void print_addr(uchar_t *, int, struct sockaddr *);
211*0Sstevel@tonic-gate extern void print_addr6(uchar_t *, int, struct sockaddr *);
212*0Sstevel@tonic-gate extern boolean_t print_icmp_other(uchar_t, uchar_t);
213*0Sstevel@tonic-gate extern boolean_t print_icmp_other6(uchar_t, uchar_t);
214*0Sstevel@tonic-gate static void print_stats(int, int, double, double, double, double);
215*0Sstevel@tonic-gate static void print_unknown_host_msg(const char *, const char *);
216*0Sstevel@tonic-gate static void record_stats(double, int *, double *, double *, double *, double *);
217*0Sstevel@tonic-gate static void resolve_nodes(int *, struct addrinfo **);
218*0Sstevel@tonic-gate static void select_src_addr(union any_in_addr *, union any_in_addr *, int);
219*0Sstevel@tonic-gate extern void send_probe(int, struct sockaddr *, struct ip *, int, int,
220*0Sstevel@tonic-gate     struct timeval *, int);
221*0Sstevel@tonic-gate extern void send_probe6(int, struct msghdr *, struct ip *, int, int,
222*0Sstevel@tonic-gate     struct timeval *, int);
223*0Sstevel@tonic-gate extern void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int,
224*0Sstevel@tonic-gate     uint_t);
225*0Sstevel@tonic-gate extern struct ip *set_buffers(int);
226*0Sstevel@tonic-gate extern struct ip *set_buffers6(int);
227*0Sstevel@tonic-gate extern void set_IPv4opt_sourcerouting(int, union any_in_addr *,
228*0Sstevel@tonic-gate     union any_in_addr *);
229*0Sstevel@tonic-gate static void set_sin(struct sockaddr *, union any_in_addr *, int);
230*0Sstevel@tonic-gate static int set_src_addr(struct pr_set *, struct ifaddrlist **);
231*0Sstevel@tonic-gate static void setup_protocol(struct pr_set *, int);
232*0Sstevel@tonic-gate static void setup_socket(struct pr_set *, int);
233*0Sstevel@tonic-gate static void sig_handler(int);
234*0Sstevel@tonic-gate static int str2int(const char *, const char *, int, int);
235*0Sstevel@tonic-gate static double str2dbl(const char *, const char *, double, double);
236*0Sstevel@tonic-gate static void trace_it(struct addrinfo *);
237*0Sstevel@tonic-gate static void traceroute(union any_in_addr *, struct msghdr *, struct pr_set *,
238*0Sstevel@tonic-gate     int, struct ifaddrlist *);
239*0Sstevel@tonic-gate static void tv_sub(struct timeval *, struct timeval *);
240*0Sstevel@tonic-gate static void usage(void);
241*0Sstevel@tonic-gate static int wait_for_reply(int, struct msghdr *, struct timeval *);
242*0Sstevel@tonic-gate static double xsqrt(double);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate  * main
246*0Sstevel@tonic-gate  */
247*0Sstevel@tonic-gate void
248*0Sstevel@tonic-gate main(int argc, char **argv)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	struct addrinfo *ai_dst = NULL;		/* destination host */
251*0Sstevel@tonic-gate 	/*
252*0Sstevel@tonic-gate 	 * "probing_successful" indicates if we could successfully send probes,
253*0Sstevel@tonic-gate 	 * not necessarily received reply from the target (this behavior is from
254*0Sstevel@tonic-gate 	 * the original traceroute). It's _B_FALSE if packlen is invalid, or no
255*0Sstevel@tonic-gate 	 * interfaces found.
256*0Sstevel@tonic-gate 	 */
257*0Sstevel@tonic-gate 	boolean_t probing_successful = _B_FALSE;
258*0Sstevel@tonic-gate 	int longjmp_return;			/* return value from longjump */
259*0Sstevel@tonic-gate 	int i = 0;
260*0Sstevel@tonic-gate 	char *cp;
261*0Sstevel@tonic-gate 	int op;
262*0Sstevel@tonic-gate 	char *ep;
263*0Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
264*0Sstevel@tonic-gate 	double pause;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/*
267*0Sstevel@tonic-gate 	 * A raw socket will be used for IPv4 if there is sufficient
268*0Sstevel@tonic-gate 	 * privilege.
269*0Sstevel@tonic-gate 	 */
270*0Sstevel@tonic-gate 	raw_req = priv_ineffect(PRIV_NET_RAWACCESS);
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	/*
273*0Sstevel@tonic-gate 	 * We'll need the privilege only when we open the sockets; that's
274*0Sstevel@tonic-gate 	 * when we'll fail if the program has insufficient privileges.
275*0Sstevel@tonic-gate 	 */
276*0Sstevel@tonic-gate 	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_NET_ICMPACCESS,
277*0Sstevel@tonic-gate 	    raw_req ? PRIV_NET_RAWACCESS : NULL, NULL);
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	(void) setlinebuf(stdout);
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	if ((cp = strrchr(argv[0], '/')) != NULL)
282*0Sstevel@tonic-gate 		prog = cp + 1;
283*0Sstevel@tonic-gate 	else
284*0Sstevel@tonic-gate 		prog = argv[0];
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	opterr = 0;
287*0Sstevel@tonic-gate 	while ((op = getopt(argc, argv, "adFIlnrSvxA:c:f:g:i:L:m:P:p:Q:q:s:"
288*0Sstevel@tonic-gate 	    "t:w:")) != EOF) {
289*0Sstevel@tonic-gate 		switch (op) {
290*0Sstevel@tonic-gate 		case 'A':
291*0Sstevel@tonic-gate 			if (strcmp(optarg, "inet") == 0) {
292*0Sstevel@tonic-gate 				family_input = AF_INET;
293*0Sstevel@tonic-gate 			} else if (strcmp(optarg, "inet6") == 0) {
294*0Sstevel@tonic-gate 				family_input = AF_INET6;
295*0Sstevel@tonic-gate 			} else {
296*0Sstevel@tonic-gate 				Fprintf(stderr,
297*0Sstevel@tonic-gate 				    "%s: unknown address family %s\n",
298*0Sstevel@tonic-gate 				    prog, optarg);
299*0Sstevel@tonic-gate 				exit(EXIT_FAILURE);
300*0Sstevel@tonic-gate 			}
301*0Sstevel@tonic-gate 			break;
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 		case 'a':
304*0Sstevel@tonic-gate 			probe_all = _B_TRUE;
305*0Sstevel@tonic-gate 			break;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 		case 'c':
308*0Sstevel@tonic-gate 			class = str2int(optarg, "traffic class", 0,
309*0Sstevel@tonic-gate 			    MAX_TRAFFIC_CLASS);
310*0Sstevel@tonic-gate 			break;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 		case 'd':
313*0Sstevel@tonic-gate 			options |= SO_DEBUG;
314*0Sstevel@tonic-gate 			break;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 		case 'f':
317*0Sstevel@tonic-gate 			first_ttl = str2int(optarg, "first ttl", 1, MAXTTL);
318*0Sstevel@tonic-gate 			break;
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 		case 'F':
321*0Sstevel@tonic-gate 			off = IP_DF;
322*0Sstevel@tonic-gate 			break;
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 		case 'g':
325*0Sstevel@tonic-gate 			if (!raw_req) {
326*0Sstevel@tonic-gate 				Fprintf(stderr,
327*0Sstevel@tonic-gate 				    "%s: privilege to specify a loose source "
328*0Sstevel@tonic-gate 				    "route gateway is unavailable\n",
329*0Sstevel@tonic-gate 				    prog);
330*0Sstevel@tonic-gate 				exit(EXIT_FAILURE);
331*0Sstevel@tonic-gate 			}
332*0Sstevel@tonic-gate 			if (gw_count > MAXMAX_GWS) {
333*0Sstevel@tonic-gate 				Fprintf(stderr,
334*0Sstevel@tonic-gate 				    "%s: Too many gateways\n", prog);
335*0Sstevel@tonic-gate 				exit(EXIT_FAILURE);
336*0Sstevel@tonic-gate 			}
337*0Sstevel@tonic-gate 			gwlist[gw_count] = strdup(optarg);
338*0Sstevel@tonic-gate 			if (gwlist[gw_count] == NULL) {
339*0Sstevel@tonic-gate 				Fprintf(stderr, "%s: strdup %s\n", prog,
340*0Sstevel@tonic-gate 				    strerror(errno));
341*0Sstevel@tonic-gate 				exit(EXIT_FAILURE);
342*0Sstevel@tonic-gate 			}
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 			++gw_count;
345*0Sstevel@tonic-gate 			break;
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 		case 'l':
348*0Sstevel@tonic-gate 			showttl = _B_TRUE;
349*0Sstevel@tonic-gate 			break;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 		case 'i':
352*0Sstevel@tonic-gate 			/* this can be IF name or IF index */
353*0Sstevel@tonic-gate 			if_index = (uint_t)strtol(optarg, &ep, 10);
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 			/* convert IF index <-->  IF name */
356*0Sstevel@tonic-gate 			if (errno != 0 || *ep != '\0') {
357*0Sstevel@tonic-gate 				device = optarg;
358*0Sstevel@tonic-gate 				if_index = if_nametoindex((const char *)device);
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 				/*
361*0Sstevel@tonic-gate 				 * In case it fails, check to see if the problem
362*0Sstevel@tonic-gate 				 * is other than "IF not found".
363*0Sstevel@tonic-gate 				 */
364*0Sstevel@tonic-gate 				if (if_index == 0 && errno != ENXIO) {
365*0Sstevel@tonic-gate 					Fprintf(stderr, "%s: if_nametoindex:"
366*0Sstevel@tonic-gate 					    "%s\n", prog, strerror(errno));
367*0Sstevel@tonic-gate 					exit(EXIT_FAILURE);
368*0Sstevel@tonic-gate 				}
369*0Sstevel@tonic-gate 			} else {
370*0Sstevel@tonic-gate 				device = (char *)malloc(LIFNAMSIZ + 1);
371*0Sstevel@tonic-gate 				if (device == NULL) {
372*0Sstevel@tonic-gate 					Fprintf(stderr, "%s: malloc: %s\n",
373*0Sstevel@tonic-gate 					    prog, strerror(errno));
374*0Sstevel@tonic-gate 					exit(EXIT_FAILURE);
375*0Sstevel@tonic-gate 				}
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 				device = if_indextoname(if_index, device);
378*0Sstevel@tonic-gate 				if (device != NULL) {
379*0Sstevel@tonic-gate 					device[LIFNAMSIZ] = '\0';
380*0Sstevel@tonic-gate 				} else if (errno != ENXIO) {
381*0Sstevel@tonic-gate 					/*
382*0Sstevel@tonic-gate 					 * The problem was other than "index
383*0Sstevel@tonic-gate 					 * not found".
384*0Sstevel@tonic-gate 					 */
385*0Sstevel@tonic-gate 					Fprintf(stderr, "%s: if_indextoname:"
386*0Sstevel@tonic-gate 					    "%s\n", prog, strerror(errno));
387*0Sstevel@tonic-gate 					exit(EXIT_FAILURE);
388*0Sstevel@tonic-gate 				}
389*0Sstevel@tonic-gate 			}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 			if (device == NULL || if_index == 0) {
392*0Sstevel@tonic-gate 				Fprintf(stderr, "%s: interface %s "
393*0Sstevel@tonic-gate 				    "doesn't match any actual interfaces\n",
394*0Sstevel@tonic-gate 				    prog, optarg);
395*0Sstevel@tonic-gate 				exit(EXIT_FAILURE);
396*0Sstevel@tonic-gate 			}
397*0Sstevel@tonic-gate 			break;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 		case 'I':
400*0Sstevel@tonic-gate 			useicmp = _B_TRUE;
401*0Sstevel@tonic-gate 			break;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 		case 'L':
404*0Sstevel@tonic-gate 			flow = str2int(optarg, "flow label", 0, MAX_FLOW_LABEL);
405*0Sstevel@tonic-gate 			break;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 		case 'm':
408*0Sstevel@tonic-gate 			max_ttl = str2int(optarg, "max ttl(hop limit)", 1,
409*0Sstevel@tonic-gate 			    MAXTTL);
410*0Sstevel@tonic-gate 			break;
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 		case 'n':
413*0Sstevel@tonic-gate 			nflag = _B_TRUE;
414*0Sstevel@tonic-gate 			break;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 		case 'P':
417*0Sstevel@tonic-gate 			pause = str2dbl(optarg, "pause", 0, INT_MAX);
418*0Sstevel@tonic-gate 			delay.tv_sec = (time_t)pause;
419*0Sstevel@tonic-gate 			delay.tv_usec = (suseconds_t)((pause - delay.tv_sec) *
420*0Sstevel@tonic-gate 			    1000000);
421*0Sstevel@tonic-gate 			break;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 		case 'p':
424*0Sstevel@tonic-gate 			port = str2int(optarg, "port", 1, MAX_PORT);
425*0Sstevel@tonic-gate 			break;
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 		case 'Q':
428*0Sstevel@tonic-gate 			max_timeout = str2int(optarg, "max timeout", 1, -1);
429*0Sstevel@tonic-gate 			break;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 		case 'q':
432*0Sstevel@tonic-gate 			nprobes = str2int(optarg, "nprobes", 1, -1);
433*0Sstevel@tonic-gate 			break;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 		case 'r':
436*0Sstevel@tonic-gate 			options |= SO_DONTROUTE;
437*0Sstevel@tonic-gate 			break;
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 		case 'S':
440*0Sstevel@tonic-gate 			collect_stat = _B_TRUE;
441*0Sstevel@tonic-gate 			break;
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 		case 's':
444*0Sstevel@tonic-gate 			/*
445*0Sstevel@tonic-gate 			 * set the ip source address of the outbound
446*0Sstevel@tonic-gate 			 * probe (e.g., on a multi-homed host).
447*0Sstevel@tonic-gate 			 */
448*0Sstevel@tonic-gate 			source_input = optarg;
449*0Sstevel@tonic-gate 			break;
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 		case 't':
452*0Sstevel@tonic-gate 			tos = (uchar_t)str2int(optarg, "tos", 0, MAX_TOS);
453*0Sstevel@tonic-gate 			settos = _B_TRUE;
454*0Sstevel@tonic-gate 			break;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 		case 'v':
457*0Sstevel@tonic-gate 			verbose = _B_TRUE;
458*0Sstevel@tonic-gate 			break;
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 		case 'x':
461*0Sstevel@tonic-gate 			docksum = _B_FALSE;
462*0Sstevel@tonic-gate 			break;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 		case 'w':
465*0Sstevel@tonic-gate 			waittime = str2int(optarg, "wait time", 2, -1);
466*0Sstevel@tonic-gate 			break;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 		default:
469*0Sstevel@tonic-gate 			usage();
470*0Sstevel@tonic-gate 			break;
471*0Sstevel@tonic-gate 		}
472*0Sstevel@tonic-gate 	}
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	/*
475*0Sstevel@tonic-gate 	 * If it's probe_all, SIGQUIT makes traceroute exit(). But we set the
476*0Sstevel@tonic-gate 	 * address to jump back to in traceroute(). Until then, we'll need to
477*0Sstevel@tonic-gate 	 * temporarily specify one.
478*0Sstevel@tonic-gate 	 */
479*0Sstevel@tonic-gate 	if (probe_all) {
480*0Sstevel@tonic-gate 		if ((longjmp_return = setjmp(env)) != 0) {
481*0Sstevel@tonic-gate 			if (longjmp_return == SIGQUIT) {
482*0Sstevel@tonic-gate 				Printf("(exiting)\n");
483*0Sstevel@tonic-gate 				exit(EXIT_SUCCESS);
484*0Sstevel@tonic-gate 			} else {		/* should never happen */
485*0Sstevel@tonic-gate 				exit(EXIT_FAILURE);
486*0Sstevel@tonic-gate 			}
487*0Sstevel@tonic-gate 		}
488*0Sstevel@tonic-gate 		(void) signal(SIGQUIT, sig_handler);
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	if ((gw_count > 0) && (options & SO_DONTROUTE)) {
492*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: loose source route gateways (-g)"
493*0Sstevel@tonic-gate 		    " cannot be specified when probe packets are sent"
494*0Sstevel@tonic-gate 		    " directly to a host on an attached network (-r)\n",
495*0Sstevel@tonic-gate 		    prog);
496*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
497*0Sstevel@tonic-gate 	}
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	i = argc - optind;
500*0Sstevel@tonic-gate 	if (i == 1 || i == 2) {
501*0Sstevel@tonic-gate 		hostname = argv[optind];
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 		if (i == 2) {
504*0Sstevel@tonic-gate 			/* accept any length now, we'll check it later */
505*0Sstevel@tonic-gate 			packlen_input = str2int(argv[optind + 1],
506*0Sstevel@tonic-gate 			    "packet length", 0, -1);
507*0Sstevel@tonic-gate 		}
508*0Sstevel@tonic-gate 	} else {
509*0Sstevel@tonic-gate 		usage();
510*0Sstevel@tonic-gate 	}
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	if (first_ttl > max_ttl) {
513*0Sstevel@tonic-gate 		Fprintf(stderr,
514*0Sstevel@tonic-gate 		    "%s: first ttl(hop limit) (%d) may not be greater"
515*0Sstevel@tonic-gate 		    " than max ttl(hop limit) (%d)\n",
516*0Sstevel@tonic-gate 		    prog, first_ttl, max_ttl);
517*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	/* resolve hostnames */
521*0Sstevel@tonic-gate 	resolve_nodes(&family_input, &ai_dst);
522*0Sstevel@tonic-gate 	if (ai_dst == NULL) {
523*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
524*0Sstevel@tonic-gate 	}
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	/*
527*0Sstevel@tonic-gate 	 * If it's probe_all, SIGINT makes traceroute skip to probing next IP
528*0Sstevel@tonic-gate 	 * address of the target. The new interrupt handler is assigned in
529*0Sstevel@tonic-gate 	 * traceroute() function. Until then let's ignore the signal.
530*0Sstevel@tonic-gate 	 */
531*0Sstevel@tonic-gate 	if (probe_all)
532*0Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	ident = (getpid() & 0xffff) | 0x8000;
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	/*
537*0Sstevel@tonic-gate 	 * We KNOW that probe_all == TRUE if family is AF_UNSPEC,
538*0Sstevel@tonic-gate 	 * since family is set to the specific AF found unless it's
539*0Sstevel@tonic-gate 	 * probe_all. So if family == AF_UNSPEC, we need to init pr4 and pr6.
540*0Sstevel@tonic-gate 	 */
541*0Sstevel@tonic-gate 	switch (family_input) {
542*0Sstevel@tonic-gate 	case AF_UNSPEC:
543*0Sstevel@tonic-gate 		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
544*0Sstevel@tonic-gate 		if (pr4 == NULL) {
545*0Sstevel@tonic-gate 			Fprintf(stderr,
546*0Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
547*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
548*0Sstevel@tonic-gate 		}
549*0Sstevel@tonic-gate 		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
550*0Sstevel@tonic-gate 		if (pr6 == NULL) {
551*0Sstevel@tonic-gate 			Fprintf(stderr,
552*0Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
553*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
554*0Sstevel@tonic-gate 		}
555*0Sstevel@tonic-gate 		setup_protocol(pr6, AF_INET6);
556*0Sstevel@tonic-gate 		setup_protocol(pr4, AF_INET);
557*0Sstevel@tonic-gate 		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
558*0Sstevel@tonic-gate 		setup_socket(pr6, pr6->packlen);
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
561*0Sstevel@tonic-gate 		setup_socket(pr4, pr4->packlen);
562*0Sstevel@tonic-gate 		num_ifs6 = set_src_addr(pr6, &al6);
563*0Sstevel@tonic-gate 		num_ifs4 = set_src_addr(pr4, &al4);
564*0Sstevel@tonic-gate 		break;
565*0Sstevel@tonic-gate 	case AF_INET6:
566*0Sstevel@tonic-gate 		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
567*0Sstevel@tonic-gate 		if (pr6 == NULL) {
568*0Sstevel@tonic-gate 			Fprintf(stderr,
569*0Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
570*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
571*0Sstevel@tonic-gate 		}
572*0Sstevel@tonic-gate 		setup_protocol(pr6, AF_INET6);
573*0Sstevel@tonic-gate 		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
574*0Sstevel@tonic-gate 		setup_socket(pr6, pr6->packlen);
575*0Sstevel@tonic-gate 		num_ifs6 = set_src_addr(pr6, &al6);
576*0Sstevel@tonic-gate 		break;
577*0Sstevel@tonic-gate 	case AF_INET:
578*0Sstevel@tonic-gate 		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
579*0Sstevel@tonic-gate 		if (pr4 == NULL) {
580*0Sstevel@tonic-gate 			Fprintf(stderr,
581*0Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
582*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
583*0Sstevel@tonic-gate 		}
584*0Sstevel@tonic-gate 		setup_protocol(pr4, AF_INET);
585*0Sstevel@tonic-gate 		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
586*0Sstevel@tonic-gate 		setup_socket(pr4, pr4->packlen);
587*0Sstevel@tonic-gate 		num_ifs4 = set_src_addr(pr4, &al4);
588*0Sstevel@tonic-gate 		break;
589*0Sstevel@tonic-gate 	default:
590*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: unknow address family.\n", prog);
591*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
592*0Sstevel@tonic-gate 	}
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if (num_v4 + num_v6 > 1 && !probe_all) {
595*0Sstevel@tonic-gate 		if (ai_dst->ai_family == AF_INET) {
596*0Sstevel@tonic-gate 			Fprintf(stderr,
597*0Sstevel@tonic-gate 			    "%s: Warning: %s has multiple addresses;"
598*0Sstevel@tonic-gate 			    " using %s\n", prog, hostname,
599*0Sstevel@tonic-gate 			    inet_ntop(AF_INET,
600*0Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
601*0Sstevel@tonic-gate 			    (void *)&((struct sockaddr_in *)
602*0Sstevel@tonic-gate 			    ai_dst->ai_addr)->sin_addr,
603*0Sstevel@tonic-gate 			    temp_buf, sizeof (temp_buf)));
604*0Sstevel@tonic-gate 		} else {
605*0Sstevel@tonic-gate 			Fprintf(stderr,
606*0Sstevel@tonic-gate 			    "%s: Warning: %s has multiple addresses;"
607*0Sstevel@tonic-gate 			    " using %s\n", prog, hostname,
608*0Sstevel@tonic-gate 			    inet_ntop(AF_INET6,
609*0Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
610*0Sstevel@tonic-gate 			    (void *)&((struct sockaddr_in6 *)
611*0Sstevel@tonic-gate 			    ai_dst->ai_addr)->sin6_addr,
612*0Sstevel@tonic-gate 			    temp_buf, sizeof (temp_buf)));
613*0Sstevel@tonic-gate 		}
614*0Sstevel@tonic-gate 	}
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 	if (num_ifs4 + num_ifs6 > 0) {
617*0Sstevel@tonic-gate 		trace_it(ai_dst);
618*0Sstevel@tonic-gate 		probing_successful = _B_TRUE;
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	(void) close(rcvsock4);
622*0Sstevel@tonic-gate 	(void) close(sndsock4);
623*0Sstevel@tonic-gate 	(void) close(rcvsock6);
624*0Sstevel@tonic-gate 	(void) close(sndsock6);
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	/*
627*0Sstevel@tonic-gate 	 * if we could probe any of the IP addresses of the target, that means
628*0Sstevel@tonic-gate 	 * this was a successful operation
629*0Sstevel@tonic-gate 	 */
630*0Sstevel@tonic-gate 	if (probing_successful)
631*0Sstevel@tonic-gate 		exit(EXIT_SUCCESS);
632*0Sstevel@tonic-gate 	else
633*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
634*0Sstevel@tonic-gate }
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate /*
637*0Sstevel@tonic-gate  * print "unknown host" message
638*0Sstevel@tonic-gate  */
639*0Sstevel@tonic-gate static void
640*0Sstevel@tonic-gate print_unknown_host_msg(const char *protocol, const char *host)
641*0Sstevel@tonic-gate {
642*0Sstevel@tonic-gate 	Fprintf(stderr, "%s: unknown%s host %s\n", prog, protocol, host);
643*0Sstevel@tonic-gate }
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate /*
646*0Sstevel@tonic-gate  * resolve destination host and gateways
647*0Sstevel@tonic-gate  */
648*0Sstevel@tonic-gate static void
649*0Sstevel@tonic-gate resolve_nodes(int *family, struct addrinfo **ai_dstp)
650*0Sstevel@tonic-gate {
651*0Sstevel@tonic-gate 	struct addrinfo *ai_dst = NULL;
652*0Sstevel@tonic-gate 	struct addrinfo *aip = NULL;
653*0Sstevel@tonic-gate 	int num_resolved_gw = 0;
654*0Sstevel@tonic-gate 	int num_resolved_gw6 = 0;
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 	get_hostinfo(hostname, *family, &ai_dst);
657*0Sstevel@tonic-gate 	if (ai_dst == NULL) {
658*0Sstevel@tonic-gate 		print_unknown_host_msg("", hostname);
659*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
660*0Sstevel@tonic-gate 	}
661*0Sstevel@tonic-gate 	/* Get a count of the v4 & v6 addresses */
662*0Sstevel@tonic-gate 	for (aip = ai_dst; aip != NULL; aip = aip->ai_next) {
663*0Sstevel@tonic-gate 		switch (aip->ai_family) {
664*0Sstevel@tonic-gate 		case AF_INET:
665*0Sstevel@tonic-gate 			num_v4++;
666*0Sstevel@tonic-gate 			break;
667*0Sstevel@tonic-gate 		case AF_INET6:
668*0Sstevel@tonic-gate 			num_v6++;
669*0Sstevel@tonic-gate 			break;
670*0Sstevel@tonic-gate 		}
671*0Sstevel@tonic-gate 	}
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	if (*family == AF_UNSPEC && !probe_all) {
674*0Sstevel@tonic-gate 		*family = ai_dst->ai_family;
675*0Sstevel@tonic-gate 	}
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	/* resolve gateways */
678*0Sstevel@tonic-gate 	if (gw_count > 0) {
679*0Sstevel@tonic-gate 		get_gwaddrs(gwlist, *family, gwIPlist, gwIP6list,
680*0Sstevel@tonic-gate 		    &num_resolved_gw, &num_resolved_gw6);
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 		/* we couldn't resolve a gateway as an IPv6 host */
683*0Sstevel@tonic-gate 		if (num_resolved_gw6 != gw_count && num_v6 != 0) {
684*0Sstevel@tonic-gate 			if (*family == AF_INET6 || *family == AF_UNSPEC)
685*0Sstevel@tonic-gate 				print_unknown_host_msg(" IPv6",
686*0Sstevel@tonic-gate 				    gwlist[num_resolved_gw6]);
687*0Sstevel@tonic-gate 			num_v6 = 0;
688*0Sstevel@tonic-gate 		}
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 		/* we couldn't resolve a gateway as an IPv4 host */
691*0Sstevel@tonic-gate 		if (num_resolved_gw != gw_count && num_v4 != 0) {
692*0Sstevel@tonic-gate 			if (*family == AF_INET || *family == AF_UNSPEC)
693*0Sstevel@tonic-gate 				print_unknown_host_msg(" IPv4",
694*0Sstevel@tonic-gate 				    gwlist[num_resolved_gw]);
695*0Sstevel@tonic-gate 			num_v4 = 0;
696*0Sstevel@tonic-gate 		}
697*0Sstevel@tonic-gate 	}
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	*ai_dstp = ai_dst;
700*0Sstevel@tonic-gate }
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate /*
703*0Sstevel@tonic-gate  * Given IP address or hostname, return v4 and v6 hostinfo lists.
704*0Sstevel@tonic-gate  * Assumes that hostinfo ** ptrs are non-null.
705*0Sstevel@tonic-gate  */
706*0Sstevel@tonic-gate static void
707*0Sstevel@tonic-gate get_hostinfo(char *host, int family, struct addrinfo **aipp)
708*0Sstevel@tonic-gate {
709*0Sstevel@tonic-gate 	struct addrinfo hints, *ai;
710*0Sstevel@tonic-gate 	struct in6_addr addr6;
711*0Sstevel@tonic-gate 	struct in_addr addr;
712*0Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
713*0Sstevel@tonic-gate 	int rc;
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 	/*
716*0Sstevel@tonic-gate 	 * Take care of v4-mapped addresses. It should run same as v4, after
717*0Sstevel@tonic-gate 	 * chopping off the prefix, leaving the IPv4 address
718*0Sstevel@tonic-gate 	 */
719*0Sstevel@tonic-gate 	if ((inet_pton(AF_INET6, host, &addr6) > 0) &&
720*0Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&addr6)) {
721*0Sstevel@tonic-gate 		/* peel off the "mapping" stuff, leaving 32 bit IPv4 address */
722*0Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&addr6, &addr);
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 		/* convert it back to a string */
725*0Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (void *)&addr, temp_buf,
726*0Sstevel@tonic-gate 		    sizeof (temp_buf));
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 		/* now the host is an IPv4 address */
729*0Sstevel@tonic-gate 		(void) strcpy(host, temp_buf);
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 		/*
732*0Sstevel@tonic-gate 		 * If it's a mapped address, we convert it into IPv4
733*0Sstevel@tonic-gate 		 * address because traceroute will send and receive IPv4
734*0Sstevel@tonic-gate 		 * packets for that address. Therefore, it's a failure case to
735*0Sstevel@tonic-gate 		 * ask get_hostinfo() to treat a mapped address as an IPv6
736*0Sstevel@tonic-gate 		 * address.
737*0Sstevel@tonic-gate 		 */
738*0Sstevel@tonic-gate 		if (family == AF_INET6) {
739*0Sstevel@tonic-gate 			return;
740*0Sstevel@tonic-gate 		}
741*0Sstevel@tonic-gate 	}
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	(void) memset(&hints, 0, sizeof (hints));
744*0Sstevel@tonic-gate 	hints.ai_family = family;
745*0Sstevel@tonic-gate 	hints.ai_flags = AI_ADDRCONFIG;
746*0Sstevel@tonic-gate 	rc = getaddrinfo(host, NULL, &hints, &ai);
747*0Sstevel@tonic-gate 	if (rc != 0) {
748*0Sstevel@tonic-gate 		if (rc != EAI_NONAME)
749*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: getaddrinfo: %s\n", prog,
750*0Sstevel@tonic-gate 			    gai_strerror(rc));
751*0Sstevel@tonic-gate 		return;
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 	*aipp = ai;
754*0Sstevel@tonic-gate }
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate /*
757*0Sstevel@tonic-gate  * Calculate the packet length to be used, and check against the valid range.
758*0Sstevel@tonic-gate  * Returns -1 if range check fails.
759*0Sstevel@tonic-gate  */
760*0Sstevel@tonic-gate static uint_t
761*0Sstevel@tonic-gate calc_packetlen(int plen_input, struct pr_set *pr)
762*0Sstevel@tonic-gate {
763*0Sstevel@tonic-gate 	int minpacket;			/* min ip packet size */
764*0Sstevel@tonic-gate 	int optlen;			/* length of ip options */
765*0Sstevel@tonic-gate 	int plen;
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	/*
768*0Sstevel@tonic-gate 	 * LBNL bug fixed: miscalculation of optlen
769*0Sstevel@tonic-gate 	 */
770*0Sstevel@tonic-gate 	if (gw_count > 0) {
771*0Sstevel@tonic-gate 		/*
772*0Sstevel@tonic-gate 		 * IPv4:
773*0Sstevel@tonic-gate 		 * ----
774*0Sstevel@tonic-gate 		 * 5 (NO OPs) + 3 (code, len, ptr) + gateways
775*0Sstevel@tonic-gate 		 * IP options field can hold up to 9 gateways. But the API
776*0Sstevel@tonic-gate 		 * allows you to specify only 8, because the last one is the
777*0Sstevel@tonic-gate 		 * destination host. When this packet is sent, on the wire
778*0Sstevel@tonic-gate 		 * you see one gateway replaced by 4 NO OPs. The other 1 NO
779*0Sstevel@tonic-gate 		 * OP is for alignment
780*0Sstevel@tonic-gate 		 *
781*0Sstevel@tonic-gate 		 * IPv6:
782*0Sstevel@tonic-gate 		 * ----
783*0Sstevel@tonic-gate 		 * Well, formula is different, but the result is same.
784*0Sstevel@tonic-gate 		 * 8 byte fixed part for Type 0 Routing header, followed by
785*0Sstevel@tonic-gate 		 * gateway addresses
786*0Sstevel@tonic-gate 		 */
787*0Sstevel@tonic-gate 		optlen = 8 + gw_count * pr->addr_len;
788*0Sstevel@tonic-gate 	} else {
789*0Sstevel@tonic-gate 		optlen = 0;
790*0Sstevel@tonic-gate 	}
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	/* take care of the packet length calculations and checks */
793*0Sstevel@tonic-gate 	minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen;
794*0Sstevel@tonic-gate 	if (useicmp)
795*0Sstevel@tonic-gate 		minpacket += pr->icmp_minlen;	/* minimum ICMP header size */
796*0Sstevel@tonic-gate 	else
797*0Sstevel@tonic-gate 		minpacket += sizeof (struct udphdr);
798*0Sstevel@tonic-gate 	plen = plen_input;
799*0Sstevel@tonic-gate 	if (plen == 0) {
800*0Sstevel@tonic-gate 		plen = minpacket;		/* minimum sized packet */
801*0Sstevel@tonic-gate 	} else if (minpacket > plen || plen > IP_MAXPACKET) {
802*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n",
803*0Sstevel@tonic-gate 		    prog, pr->name, minpacket, IP_MAXPACKET);
804*0Sstevel@tonic-gate 		return (0);
805*0Sstevel@tonic-gate 	}
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate 	return (plen);
808*0Sstevel@tonic-gate }
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate /*
811*0Sstevel@tonic-gate  * Sets the source address by resolving -i and -s arguments, or if -i and -s
812*0Sstevel@tonic-gate  * don't dictate any, it sets the pick_src to make sure traceroute uses the
813*0Sstevel@tonic-gate  * kernel's pick of the source address.
814*0Sstevel@tonic-gate  * Returns number of interfaces configured on the source host, 0 on error or
815*0Sstevel@tonic-gate  * there's no interface which is up amd not a loopback.
816*0Sstevel@tonic-gate  */
817*0Sstevel@tonic-gate static int
818*0Sstevel@tonic-gate set_src_addr(struct pr_set *pr, struct ifaddrlist **alp)
819*0Sstevel@tonic-gate {
820*0Sstevel@tonic-gate 	union any_in_addr *ap;
821*0Sstevel@tonic-gate 	struct ifaddrlist *al = NULL;
822*0Sstevel@tonic-gate 	struct ifaddrlist *tmp1_al = NULL;
823*0Sstevel@tonic-gate 	struct ifaddrlist *tmp2_al = NULL;
824*0Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
825*0Sstevel@tonic-gate 	struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from;
826*0Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
827*0Sstevel@tonic-gate 	struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from;
828*0Sstevel@tonic-gate 	struct addrinfo *aip;
829*0Sstevel@tonic-gate 	char errbuf[ERRBUFSIZE];
830*0Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
831*0Sstevel@tonic-gate 	int num_ifs;				/* all the interfaces  */
832*0Sstevel@tonic-gate 	int num_src_ifs;			/* exclude loopback and down */
833*0Sstevel@tonic-gate 	int i;
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	source = source_input;
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate 	/* get the interface address list */
838*0Sstevel@tonic-gate 	num_ifs = ifaddrlist(&al, pr->family, errbuf);
839*0Sstevel@tonic-gate 	if (num_ifs < 0) {
840*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf);
841*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
842*0Sstevel@tonic-gate 	}
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	num_src_ifs = 0;
845*0Sstevel@tonic-gate 	for (i = 0; i < num_ifs; i++) {
846*0Sstevel@tonic-gate 		if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP))
847*0Sstevel@tonic-gate 			num_src_ifs++;
848*0Sstevel@tonic-gate 	}
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 	if (num_src_ifs == 0) {
851*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: can't find any %s network interfaces\n",
852*0Sstevel@tonic-gate 		    prog, pr->name);
853*0Sstevel@tonic-gate 		return (0);
854*0Sstevel@tonic-gate 	}
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	/* verify the device */
857*0Sstevel@tonic-gate 	if (device != NULL) {
858*0Sstevel@tonic-gate 		tmp1_al = find_device(al, num_ifs, device);
859*0Sstevel@tonic-gate 
860*0Sstevel@tonic-gate 		if (tmp1_al == NULL) {
861*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: %s (index %d) is an invalid %s"
862*0Sstevel@tonic-gate 			    " interface\n", prog, device, if_index, pr->name);
863*0Sstevel@tonic-gate 			free(al);
864*0Sstevel@tonic-gate 			return (0);
865*0Sstevel@tonic-gate 		}
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	/* verify the source address */
869*0Sstevel@tonic-gate 	if (source != NULL) {
870*0Sstevel@tonic-gate 		get_hostinfo(source, pr->family, &aip);
871*0Sstevel@tonic-gate 		if (aip == NULL) {
872*0Sstevel@tonic-gate 			Fprintf(stderr,
873*0Sstevel@tonic-gate 			    "%s: %s is an invalid %s source address\n",
874*0Sstevel@tonic-gate 			    prog, source, pr->name);
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 			free(al);
877*0Sstevel@tonic-gate 			return (0);
878*0Sstevel@tonic-gate 		}
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 		source = aip->ai_canonname;
881*0Sstevel@tonic-gate 		ap = (union any_in_addr *)
882*0Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
883*0Sstevel@tonic-gate 		    &((struct sockaddr_in6 *)
884*0Sstevel@tonic-gate 		    aip->ai_addr)->sin6_addr;
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate 		/*
887*0Sstevel@tonic-gate 		 * LBNL bug fixed: used to accept any src address
888*0Sstevel@tonic-gate 		 */
889*0Sstevel@tonic-gate 		tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family);
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 		if (tmp2_al == NULL) {
892*0Sstevel@tonic-gate 			Fprintf(stderr,
893*0Sstevel@tonic-gate 			    "%s: %s is an invalid %s source address\n", prog,
894*0Sstevel@tonic-gate 			    inet_ntop(pr->family, (const void *)ap,
895*0Sstevel@tonic-gate 				temp_buf, sizeof (temp_buf)),
896*0Sstevel@tonic-gate 			    pr->name);
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 			free(al);
899*0Sstevel@tonic-gate 			freeaddrinfo(aip);
900*0Sstevel@tonic-gate 			return (0);
901*0Sstevel@tonic-gate 		}
902*0Sstevel@tonic-gate 	}
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	pick_src = _B_FALSE;
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 	if (source == NULL) {			/* no -s used */
907*0Sstevel@tonic-gate 		if (device == NULL) {		/* no -i used, no -s used */
908*0Sstevel@tonic-gate 			pick_src = _B_TRUE;
909*0Sstevel@tonic-gate 		} else {			/* -i used, no -s used */
910*0Sstevel@tonic-gate 			/*
911*0Sstevel@tonic-gate 			 * -i used, but not -s, and it's IPv4: set the source
912*0Sstevel@tonic-gate 			 * address to whatever the interface has configured on
913*0Sstevel@tonic-gate 			 * it.
914*0Sstevel@tonic-gate 			 */
915*0Sstevel@tonic-gate 			if (pr->family == AF_INET)
916*0Sstevel@tonic-gate 				set_sin(pr->from, &(tmp1_al->addr), pr->family);
917*0Sstevel@tonic-gate 			else
918*0Sstevel@tonic-gate 				pick_src = _B_TRUE;
919*0Sstevel@tonic-gate 		}
920*0Sstevel@tonic-gate 	} else {				/* -s used */
921*0Sstevel@tonic-gate 		if (device == NULL) {		/* no -i used, -s used */
922*0Sstevel@tonic-gate 			set_sin(pr->from, ap, pr->family);
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate 			if (aip->ai_next != NULL) {
925*0Sstevel@tonic-gate 				Fprintf(stderr,
926*0Sstevel@tonic-gate 				    "%s: Warning: %s has multiple "
927*0Sstevel@tonic-gate 				    "addresses; using %s\n",
928*0Sstevel@tonic-gate 				    prog, source,
929*0Sstevel@tonic-gate 				    inet_ntop(pr->family,
930*0Sstevel@tonic-gate 					(const void *)pr->from_sin_addr,
931*0Sstevel@tonic-gate 					temp_buf, sizeof (temp_buf)));
932*0Sstevel@tonic-gate 			}
933*0Sstevel@tonic-gate 		} else {			/* -i and -s used */
934*0Sstevel@tonic-gate 			/*
935*0Sstevel@tonic-gate 			 * Make sure the source specified matches the
936*0Sstevel@tonic-gate 			 * interface address. You only care about this for IPv4
937*0Sstevel@tonic-gate 			 * IPv6 can handle IF not matching src address
938*0Sstevel@tonic-gate 			 */
939*0Sstevel@tonic-gate 			if (pr->family == AF_INET) {
940*0Sstevel@tonic-gate 				if (!has_addr(aip, &tmp1_al->addr)) {
941*0Sstevel@tonic-gate 					Fprintf(stderr,
942*0Sstevel@tonic-gate 					    "%s: %s is not on interface %s\n",
943*0Sstevel@tonic-gate 					    prog, source, device);
944*0Sstevel@tonic-gate 					exit(EXIT_FAILURE);
945*0Sstevel@tonic-gate 				}
946*0Sstevel@tonic-gate 				/*
947*0Sstevel@tonic-gate 				 * make sure we use the one matching the
948*0Sstevel@tonic-gate 				 * interface's address
949*0Sstevel@tonic-gate 				 */
950*0Sstevel@tonic-gate 				*ap = tmp1_al->addr;
951*0Sstevel@tonic-gate 			}
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 			set_sin(pr->from, ap, pr->family);
954*0Sstevel@tonic-gate 		}
955*0Sstevel@tonic-gate 	}
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate 	/*
958*0Sstevel@tonic-gate 	 * Binding at this point will set the source address to be used
959*0Sstevel@tonic-gate 	 * for both IPv4 (when raw IP datagrams are not required) and
960*0Sstevel@tonic-gate 	 * IPv6.  If the address being bound to is zero, then the kernel
961*0Sstevel@tonic-gate 	 * will end up choosing the source address when the datagram is
962*0Sstevel@tonic-gate 	 * sent.
963*0Sstevel@tonic-gate 	 *
964*0Sstevel@tonic-gate 	 * For raw IPv4 datagrams, the source address is initialized
965*0Sstevel@tonic-gate 	 * within traceroute() along with the outbound destination
966*0Sstevel@tonic-gate 	 * address.
967*0Sstevel@tonic-gate 	 */
968*0Sstevel@tonic-gate 	if (pr->family == AF_INET && !raw_req) {
969*0Sstevel@tonic-gate 		sin_from->sin_family = AF_INET;
970*0Sstevel@tonic-gate 		sin_from->sin_port = htons(ident);
971*0Sstevel@tonic-gate 		if (bind(sndsock4, (struct sockaddr *)pr->from,
972*0Sstevel@tonic-gate 			sizeof (struct sockaddr_in)) < 0) {
973*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: bind: %s\n", prog,
974*0Sstevel@tonic-gate 			    strerror(errno));
975*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
976*0Sstevel@tonic-gate 		}
977*0Sstevel@tonic-gate 	} else if (pr->family == AF_INET6) {
978*0Sstevel@tonic-gate 		sin6_from->sin6_family = AF_INET6;
979*0Sstevel@tonic-gate 		sin6_from->sin6_port = htons(ident);
980*0Sstevel@tonic-gate 		if (bind(sndsock6, (struct sockaddr *)pr->from,
981*0Sstevel@tonic-gate 			sizeof (struct sockaddr_in6)) < 0) {
982*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: bind: %s\n", prog,
983*0Sstevel@tonic-gate 			    strerror(errno));
984*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
985*0Sstevel@tonic-gate 		}
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 		whereto6.sin6_flowinfo = htonl((class << 20) | flow);
988*0Sstevel@tonic-gate 	}
989*0Sstevel@tonic-gate 	*alp = al;
990*0Sstevel@tonic-gate 	return (num_ifs);
991*0Sstevel@tonic-gate }
992*0Sstevel@tonic-gate 
993*0Sstevel@tonic-gate /*
994*0Sstevel@tonic-gate  * Returns the complete ifaddrlist structure matching the desired interface
995*0Sstevel@tonic-gate  * address. Ignores interfaces which are either down or loopback.
996*0Sstevel@tonic-gate  */
997*0Sstevel@tonic-gate static struct ifaddrlist *
998*0Sstevel@tonic-gate find_ifaddr(struct ifaddrlist *al, int len, union any_in_addr *addr,
999*0Sstevel@tonic-gate     int family)
1000*0Sstevel@tonic-gate {
1001*0Sstevel@tonic-gate 	struct ifaddrlist *tmp_al = al;
1002*0Sstevel@tonic-gate 	int i;
1003*0Sstevel@tonic-gate 	size_t addr_len = (family == AF_INET) ? sizeof (struct in_addr) :
1004*0Sstevel@tonic-gate 	    sizeof (struct in6_addr);
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	for (i = 0; i < len; i++, tmp_al++) {
1007*0Sstevel@tonic-gate 		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
1008*0Sstevel@tonic-gate 		    (tmp_al->flags & IFF_UP)) &&
1009*0Sstevel@tonic-gate 		    (memcmp(&tmp_al->addr, addr, addr_len) == 0))
1010*0Sstevel@tonic-gate 			break;
1011*0Sstevel@tonic-gate 	}
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	if (i < len) {
1014*0Sstevel@tonic-gate 		return (tmp_al);
1015*0Sstevel@tonic-gate 	} else {
1016*0Sstevel@tonic-gate 		return (NULL);
1017*0Sstevel@tonic-gate 	}
1018*0Sstevel@tonic-gate }
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate /*
1021*0Sstevel@tonic-gate  * Returns the complete ifaddrlist structure matching the desired interface name
1022*0Sstevel@tonic-gate  * Ignores interfaces which are either down or loopback.
1023*0Sstevel@tonic-gate  */
1024*0Sstevel@tonic-gate static struct ifaddrlist *
1025*0Sstevel@tonic-gate find_device(struct ifaddrlist *al, int len, char *device)
1026*0Sstevel@tonic-gate {
1027*0Sstevel@tonic-gate 	struct ifaddrlist *tmp_al = al;
1028*0Sstevel@tonic-gate 	int i;
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate 	for (i = 0; i < len; i++, tmp_al++) {
1031*0Sstevel@tonic-gate 		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
1032*0Sstevel@tonic-gate 		    (tmp_al->flags & IFF_UP)) &&
1033*0Sstevel@tonic-gate 		    (strcmp(tmp_al->device, device) == 0))
1034*0Sstevel@tonic-gate 			break;
1035*0Sstevel@tonic-gate 	}
1036*0Sstevel@tonic-gate 
1037*0Sstevel@tonic-gate 	if (i < len) {
1038*0Sstevel@tonic-gate 		return (tmp_al);
1039*0Sstevel@tonic-gate 	} else {
1040*0Sstevel@tonic-gate 		return (NULL);
1041*0Sstevel@tonic-gate 	}
1042*0Sstevel@tonic-gate }
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate /*
1045*0Sstevel@tonic-gate  * returns _B_TRUE if given hostinfo contains the given address
1046*0Sstevel@tonic-gate  */
1047*0Sstevel@tonic-gate static boolean_t
1048*0Sstevel@tonic-gate has_addr(struct addrinfo *ai, union any_in_addr *addr)
1049*0Sstevel@tonic-gate {
1050*0Sstevel@tonic-gate 	struct addrinfo *ai_tmp = NULL;
1051*0Sstevel@tonic-gate 	union any_in_addr *ap;
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 	for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) {
1054*0Sstevel@tonic-gate 		if (ai_tmp->ai_family == AF_INET6)
1055*0Sstevel@tonic-gate 			continue;
1056*0Sstevel@tonic-gate 		ap = (union any_in_addr *)
1057*0Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
1058*0Sstevel@tonic-gate 		    &((struct sockaddr_in *)ai_tmp->ai_addr)->sin_addr;
1059*0Sstevel@tonic-gate 		if (memcmp(ap, addr, sizeof (struct in_addr)) == 0)
1060*0Sstevel@tonic-gate 			break;
1061*0Sstevel@tonic-gate 	}
1062*0Sstevel@tonic-gate 
1063*0Sstevel@tonic-gate 	if (ai_tmp != NULL) {
1064*0Sstevel@tonic-gate 		return (_B_TRUE);
1065*0Sstevel@tonic-gate 	} else {
1066*0Sstevel@tonic-gate 		return (_B_FALSE);
1067*0Sstevel@tonic-gate 	}
1068*0Sstevel@tonic-gate }
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate /*
1071*0Sstevel@tonic-gate  * Resolve the gateway names, splitting results into v4 and v6 lists.
1072*0Sstevel@tonic-gate  * Gateway addresses are added to the appropriate passed-in array; the
1073*0Sstevel@tonic-gate  * number of resolved gateways for each af is returned in resolved[6].
1074*0Sstevel@tonic-gate  * Assumes that passed-in arrays are large enough for MAX_GWS[6] addrs
1075*0Sstevel@tonic-gate  * and resolved[6] ptrs are non-null; ignores array and counter if the
1076*0Sstevel@tonic-gate  * address family param makes them irrelevant.
1077*0Sstevel@tonic-gate  */
1078*0Sstevel@tonic-gate static void
1079*0Sstevel@tonic-gate get_gwaddrs(char **gwlist, int family, union any_in_addr *gwIPlist,
1080*0Sstevel@tonic-gate     union any_in_addr *gwIPlist6, int *resolved, int *resolved6)
1081*0Sstevel@tonic-gate {
1082*0Sstevel@tonic-gate 	int i;
1083*0Sstevel@tonic-gate 	boolean_t check_v4 = _B_TRUE, check_v6 = _B_TRUE;
1084*0Sstevel@tonic-gate 	struct addrinfo *ai = NULL;
1085*0Sstevel@tonic-gate 	struct addrinfo *aip = NULL;
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate 	*resolved = *resolved6 = 0;
1088*0Sstevel@tonic-gate 	switch (family) {
1089*0Sstevel@tonic-gate 	case AF_UNSPEC:
1090*0Sstevel@tonic-gate 		break;
1091*0Sstevel@tonic-gate 	case AF_INET:
1092*0Sstevel@tonic-gate 		check_v6 = _B_FALSE;
1093*0Sstevel@tonic-gate 		break;
1094*0Sstevel@tonic-gate 	case AF_INET6:
1095*0Sstevel@tonic-gate 		check_v4 = _B_FALSE;
1096*0Sstevel@tonic-gate 		break;
1097*0Sstevel@tonic-gate 	default:
1098*0Sstevel@tonic-gate 		return;
1099*0Sstevel@tonic-gate 	}
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate 	if (check_v4 && gw_count >= MAX_GWS) {
1102*0Sstevel@tonic-gate 		check_v4 = _B_FALSE;
1103*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: too many IPv4 gateways\n", prog);
1104*0Sstevel@tonic-gate 	}
1105*0Sstevel@tonic-gate 	if (check_v6 && gw_count >= MAX_GWS6) {
1106*0Sstevel@tonic-gate 		check_v6 = _B_FALSE;
1107*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: too many IPv6 gateways\n", prog);
1108*0Sstevel@tonic-gate 	}
1109*0Sstevel@tonic-gate 
1110*0Sstevel@tonic-gate 	for (i = 0; i < gw_count; i++) {
1111*0Sstevel@tonic-gate 		if (!check_v4 && !check_v6)
1112*0Sstevel@tonic-gate 			return;
1113*0Sstevel@tonic-gate 		get_hostinfo(gwlist[i], family, &ai);
1114*0Sstevel@tonic-gate 		if (ai == NULL)
1115*0Sstevel@tonic-gate 			return;
1116*0Sstevel@tonic-gate 		if (check_v4 && num_v4 != 0) {
1117*0Sstevel@tonic-gate 			for (aip = ai; aip != NULL; aip = aip->ai_next) {
1118*0Sstevel@tonic-gate 				if (aip->ai_family == AF_INET) {
1119*0Sstevel@tonic-gate 					/* LINTED E_BAD_PTR_CAST_ALIGN */
1120*0Sstevel@tonic-gate 					bcopy(&((struct sockaddr_in *)
1121*0Sstevel@tonic-gate 					    aip->ai_addr)->sin_addr,
1122*0Sstevel@tonic-gate 					    &gwIPlist[i].addr,
1123*0Sstevel@tonic-gate 					    aip->ai_addrlen);
1124*0Sstevel@tonic-gate 					(*resolved)++;
1125*0Sstevel@tonic-gate 					break;
1126*0Sstevel@tonic-gate 				}
1127*0Sstevel@tonic-gate 			}
1128*0Sstevel@tonic-gate 		} else if (check_v4) {
1129*0Sstevel@tonic-gate 			check_v4 = _B_FALSE;
1130*0Sstevel@tonic-gate 		}
1131*0Sstevel@tonic-gate 		if (check_v6 && num_v6 != 0) {
1132*0Sstevel@tonic-gate 			for (aip = ai; aip != NULL; aip = aip->ai_next) {
1133*0Sstevel@tonic-gate 				if (aip->ai_family == AF_INET6) {
1134*0Sstevel@tonic-gate 					/* LINTED E_BAD_PTR_CAST_ALIGN */
1135*0Sstevel@tonic-gate 					bcopy(&((struct sockaddr_in6 *)
1136*0Sstevel@tonic-gate 					    aip->ai_addr)->sin6_addr,
1137*0Sstevel@tonic-gate 					    &gwIPlist6[i].addr6,
1138*0Sstevel@tonic-gate 					    aip->ai_addrlen);
1139*0Sstevel@tonic-gate 					(*resolved6)++;
1140*0Sstevel@tonic-gate 					break;
1141*0Sstevel@tonic-gate 				}
1142*0Sstevel@tonic-gate 			}
1143*0Sstevel@tonic-gate 		} else if (check_v6) {
1144*0Sstevel@tonic-gate 			check_v6 = _B_FALSE;
1145*0Sstevel@tonic-gate 		}
1146*0Sstevel@tonic-gate 	}
1147*0Sstevel@tonic-gate 	freeaddrinfo(ai);
1148*0Sstevel@tonic-gate }
1149*0Sstevel@tonic-gate 
1150*0Sstevel@tonic-gate /*
1151*0Sstevel@tonic-gate  * set protocol specific values here
1152*0Sstevel@tonic-gate  */
1153*0Sstevel@tonic-gate static void
1154*0Sstevel@tonic-gate setup_protocol(struct pr_set *pr, int family)
1155*0Sstevel@tonic-gate {
1156*0Sstevel@tonic-gate 	/*
1157*0Sstevel@tonic-gate 	 * Set the global variables for each AF. This is going to save us lots
1158*0Sstevel@tonic-gate 	 * of "if (family == AF_INET)... else .."
1159*0Sstevel@tonic-gate 	 */
1160*0Sstevel@tonic-gate 	pr->family = family;
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate 	if (family == AF_INET) {
1163*0Sstevel@tonic-gate 		if (!docksum) {
1164*0Sstevel@tonic-gate 			Fprintf(stderr,
1165*0Sstevel@tonic-gate 			    "%s: Warning: checksums disabled\n", prog);
1166*0Sstevel@tonic-gate 		}
1167*0Sstevel@tonic-gate 		(void) strcpy(pr->name, "IPv4");
1168*0Sstevel@tonic-gate 		(void) strcpy(pr->icmp, "icmp");
1169*0Sstevel@tonic-gate 		pr->icmp_minlen = ICMP_MINLEN;
1170*0Sstevel@tonic-gate 		pr->addr_len = sizeof (struct in_addr);
1171*0Sstevel@tonic-gate 		pr->ip_hdr_len = sizeof (struct ip);
1172*0Sstevel@tonic-gate 		pr->sock_size = sizeof (struct sockaddr_in);
1173*0Sstevel@tonic-gate 		pr->to = (struct sockaddr *)&whereto;
1174*0Sstevel@tonic-gate 		pr->from = (struct sockaddr *)&wherefrom;
1175*0Sstevel@tonic-gate 		pr->from_sin_addr = (void *)&wherefrom.sin_addr;
1176*0Sstevel@tonic-gate 		pr->gwIPlist = gwIPlist;
1177*0Sstevel@tonic-gate 		pr->set_buffers_fn = set_buffers;
1178*0Sstevel@tonic-gate 		pr->check_reply_fn = check_reply;
1179*0Sstevel@tonic-gate 		pr->print_icmp_other_fn = print_icmp_other;
1180*0Sstevel@tonic-gate 		pr->print_addr_fn = print_addr;
1181*0Sstevel@tonic-gate 		pr->packlen = calc_packetlen(packlen_input, pr);
1182*0Sstevel@tonic-gate 	} else {
1183*0Sstevel@tonic-gate 		(void) strcpy(pr->name, "IPv6");
1184*0Sstevel@tonic-gate 		(void) strcpy(pr->icmp, "ipv6-icmp");
1185*0Sstevel@tonic-gate 		pr->icmp_minlen = ICMP6_MINLEN;
1186*0Sstevel@tonic-gate 		pr->addr_len = sizeof (struct in6_addr);
1187*0Sstevel@tonic-gate 		pr->ip_hdr_len = sizeof (struct ip6_hdr);
1188*0Sstevel@tonic-gate 		pr->sock_size = sizeof (struct sockaddr_in6);
1189*0Sstevel@tonic-gate 		pr->to = (struct sockaddr *)&whereto6;
1190*0Sstevel@tonic-gate 		pr->from = (struct sockaddr *)&wherefrom6;
1191*0Sstevel@tonic-gate 		pr->from_sin_addr = (void *)&wherefrom6.sin6_addr;
1192*0Sstevel@tonic-gate 		pr->gwIPlist = gwIP6list;
1193*0Sstevel@tonic-gate 		pr->set_buffers_fn = set_buffers6;
1194*0Sstevel@tonic-gate 		pr->check_reply_fn = check_reply6;
1195*0Sstevel@tonic-gate 		pr->print_icmp_other_fn = print_icmp_other6;
1196*0Sstevel@tonic-gate 		pr->print_addr_fn = print_addr6;
1197*0Sstevel@tonic-gate 		pr->packlen = calc_packetlen(packlen_input, pr);
1198*0Sstevel@tonic-gate 	}
1199*0Sstevel@tonic-gate 	if (pr->packlen == 0)
1200*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1201*0Sstevel@tonic-gate }
1202*0Sstevel@tonic-gate 
1203*0Sstevel@tonic-gate /*
1204*0Sstevel@tonic-gate  * setup the sockets for the given protocol's address family
1205*0Sstevel@tonic-gate  */
1206*0Sstevel@tonic-gate static void
1207*0Sstevel@tonic-gate setup_socket(struct pr_set *pr, int packet_len)
1208*0Sstevel@tonic-gate {
1209*0Sstevel@tonic-gate 	int on = 1;
1210*0Sstevel@tonic-gate 	struct protoent *pe;
1211*0Sstevel@tonic-gate 	int type;
1212*0Sstevel@tonic-gate 	int proto;
1213*0Sstevel@tonic-gate 	int int_op;
1214*0Sstevel@tonic-gate 	int rsock;
1215*0Sstevel@tonic-gate 	int ssock;
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate 	if ((pe = getprotobyname(pr->icmp)) == NULL) {
1218*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: unknown protocol %s\n", prog, pr->icmp);
1219*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1220*0Sstevel@tonic-gate 	}
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 	/* privilege bracketing */
1223*0Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_ON);
1224*0Sstevel@tonic-gate 
1225*0Sstevel@tonic-gate 	if ((rsock = socket(pr->family, SOCK_RAW, pe->p_proto)) < 0) {
1226*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno));
1227*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1228*0Sstevel@tonic-gate 	}
1229*0Sstevel@tonic-gate 
1230*0Sstevel@tonic-gate 	if (options & SO_DEBUG) {
1231*0Sstevel@tonic-gate 		if (setsockopt(rsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
1232*0Sstevel@tonic-gate 		    sizeof (on)) < 0) {
1233*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
1234*0Sstevel@tonic-gate 			    strerror(errno));
1235*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1236*0Sstevel@tonic-gate 		}
1237*0Sstevel@tonic-gate 	}
1238*0Sstevel@tonic-gate 	if (options & SO_DONTROUTE) {
1239*0Sstevel@tonic-gate 		if (setsockopt(rsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
1240*0Sstevel@tonic-gate 		    sizeof (on)) < 0) {
1241*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
1242*0Sstevel@tonic-gate 			    strerror(errno));
1243*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1244*0Sstevel@tonic-gate 		}
1245*0Sstevel@tonic-gate 	}
1246*0Sstevel@tonic-gate 
1247*0Sstevel@tonic-gate 	if (pr->family == AF_INET6) {
1248*0Sstevel@tonic-gate 		/* Enable receipt of destination address info */
1249*0Sstevel@tonic-gate 		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1250*0Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
1251*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: IPV6_RECVPKTINFO: %s\n", prog,
1252*0Sstevel@tonic-gate 			    strerror(errno));
1253*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1254*0Sstevel@tonic-gate 		}
1255*0Sstevel@tonic-gate 		/* Enable receipt of hoplimit info */
1256*0Sstevel@tonic-gate 		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1257*0Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
1258*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: IPV6_RECVHOPLIMIT: %s\n", prog,
1259*0Sstevel@tonic-gate 			    strerror(errno));
1260*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1261*0Sstevel@tonic-gate 		}
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate 	}
1264*0Sstevel@tonic-gate 
1265*0Sstevel@tonic-gate 	/*
1266*0Sstevel@tonic-gate 	 * Initialize the socket type and protocol based on the address
1267*0Sstevel@tonic-gate 	 * family, whether or not a raw IP socket is required (for IPv4)
1268*0Sstevel@tonic-gate 	 * or whether ICMP will be used instead of UDP.
1269*0Sstevel@tonic-gate 	 *
1270*0Sstevel@tonic-gate 	 * For historical reasons, the datagrams sent out by
1271*0Sstevel@tonic-gate 	 * traceroute(1M) do not have the "don't fragment" flag set.  For
1272*0Sstevel@tonic-gate 	 * this reason as well as the ability to set the Loose Source and
1273*0Sstevel@tonic-gate 	 * Record Route (LSRR) option, a raw IP socket will be used for
1274*0Sstevel@tonic-gate 	 * IPv4 when run in the global zone.  Otherwise, the actual
1275*0Sstevel@tonic-gate 	 * datagram that will be sent will be a regular UDP or ICMP echo
1276*0Sstevel@tonic-gate 	 * request packet.  However for convenience and for future options
1277*0Sstevel@tonic-gate 	 * when other IP header information may be specified using
1278*0Sstevel@tonic-gate 	 * traceroute, the buffer including the raw IP and UDP or ICMP
1279*0Sstevel@tonic-gate 	 * header is always filled in.  When the probe is actually sent,
1280*0Sstevel@tonic-gate 	 * the size of the request and the start of the packet is set
1281*0Sstevel@tonic-gate 	 * according to the type of datagram to send.
1282*0Sstevel@tonic-gate 	 */
1283*0Sstevel@tonic-gate 	if (pr->family == AF_INET && raw_req) {
1284*0Sstevel@tonic-gate 		type = SOCK_RAW;
1285*0Sstevel@tonic-gate 		proto = IPPROTO_RAW;
1286*0Sstevel@tonic-gate 	} else if (useicmp) {
1287*0Sstevel@tonic-gate 		type = SOCK_RAW;
1288*0Sstevel@tonic-gate 		if (pr->family == AF_INET)
1289*0Sstevel@tonic-gate 			proto = IPPROTO_ICMP;
1290*0Sstevel@tonic-gate 		else
1291*0Sstevel@tonic-gate 			proto = IPPROTO_ICMPV6;
1292*0Sstevel@tonic-gate 	} else {
1293*0Sstevel@tonic-gate 		type = SOCK_DGRAM;
1294*0Sstevel@tonic-gate 		proto = IPPROTO_UDP;
1295*0Sstevel@tonic-gate 	}
1296*0Sstevel@tonic-gate 	ssock = socket(pr->family, type, proto);
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 	if (ssock < 0) {
1299*0Sstevel@tonic-gate 		if (proto == IPPROTO_RAW) {
1300*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: raw socket: %s\n", prog,
1301*0Sstevel@tonic-gate 			    strerror(errno));
1302*0Sstevel@tonic-gate 		} else if (proto == IPPROTO_UDP) {
1303*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: udp socket: %s\n", prog,
1304*0Sstevel@tonic-gate 			    strerror(errno));
1305*0Sstevel@tonic-gate 		} else {
1306*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: icmp socket: %s\n", prog,
1307*0Sstevel@tonic-gate 			    strerror(errno));
1308*0Sstevel@tonic-gate 		}
1309*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1310*0Sstevel@tonic-gate 	}
1311*0Sstevel@tonic-gate 
1312*0Sstevel@tonic-gate 	if (setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&packet_len,
1313*0Sstevel@tonic-gate 	    sizeof (packet_len)) < 0) {
1314*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno));
1315*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1316*0Sstevel@tonic-gate 	}
1317*0Sstevel@tonic-gate 
1318*0Sstevel@tonic-gate 	if (pr->family == AF_INET && raw_req) {
1319*0Sstevel@tonic-gate 		if (setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
1320*0Sstevel@tonic-gate 		    sizeof (on)) < 0) {
1321*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog,
1322*0Sstevel@tonic-gate 			    strerror(errno));
1323*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1324*0Sstevel@tonic-gate 		}
1325*0Sstevel@tonic-gate 	}
1326*0Sstevel@tonic-gate 
1327*0Sstevel@tonic-gate 	if (options & SO_DEBUG) {
1328*0Sstevel@tonic-gate 		if (setsockopt(ssock, SOL_SOCKET, SO_DEBUG, (char *)&on,
1329*0Sstevel@tonic-gate 		    sizeof (on)) < 0) {
1330*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
1331*0Sstevel@tonic-gate 			    strerror(errno));
1332*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1333*0Sstevel@tonic-gate 		}
1334*0Sstevel@tonic-gate 	}
1335*0Sstevel@tonic-gate 	if (options & SO_DONTROUTE) {
1336*0Sstevel@tonic-gate 		if (setsockopt(ssock, SOL_SOCKET, SO_DONTROUTE,
1337*0Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
1338*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
1339*0Sstevel@tonic-gate 			    strerror(errno));
1340*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1341*0Sstevel@tonic-gate 		}
1342*0Sstevel@tonic-gate 	}
1343*0Sstevel@tonic-gate 
1344*0Sstevel@tonic-gate 	/*
1345*0Sstevel@tonic-gate 	 * If a raw IPv4 packet is going to be sent, the Type of Service
1346*0Sstevel@tonic-gate 	 * field in the packet will be initialized in set_buffers().
1347*0Sstevel@tonic-gate 	 * Otherwise, it is initialized here using the IPPROTO_IP level
1348*0Sstevel@tonic-gate 	 * socket option.
1349*0Sstevel@tonic-gate 	 */
1350*0Sstevel@tonic-gate 	if (settos && !raw_req) {
1351*0Sstevel@tonic-gate 		int_op = tos;
1352*0Sstevel@tonic-gate 		if (setsockopt(ssock, IPPROTO_IP, IP_TOS, (char *)&int_op,
1353*0Sstevel@tonic-gate 		    sizeof (int_op)) < 0) {
1354*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: IP_TOS: %s\n", prog,
1355*0Sstevel@tonic-gate 			    strerror(errno));
1356*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1357*0Sstevel@tonic-gate 		}
1358*0Sstevel@tonic-gate 	}
1359*0Sstevel@tonic-gate 	if (pr->family == AF_INET) {
1360*0Sstevel@tonic-gate 		rcvsock4 = rsock;
1361*0Sstevel@tonic-gate 		sndsock4 = ssock;
1362*0Sstevel@tonic-gate 	} else {
1363*0Sstevel@tonic-gate 		rcvsock6 = rsock;
1364*0Sstevel@tonic-gate 		sndsock6 = ssock;
1365*0Sstevel@tonic-gate 	}
1366*0Sstevel@tonic-gate 	/* Revert to non-privileged user after configuring sockets */
1367*0Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_OFF);
1368*0Sstevel@tonic-gate }
1369*0Sstevel@tonic-gate 
1370*0Sstevel@tonic-gate /*
1371*0Sstevel@tonic-gate  * If we are "probing all", this function calls traceroute() for each IP address
1372*0Sstevel@tonic-gate  * of the target, otherwise calls only once. Returns _B_FALSE if traceroute()
1373*0Sstevel@tonic-gate  * fails.
1374*0Sstevel@tonic-gate  */
1375*0Sstevel@tonic-gate static void
1376*0Sstevel@tonic-gate trace_it(struct addrinfo *ai_dst)
1377*0Sstevel@tonic-gate {
1378*0Sstevel@tonic-gate 	struct msghdr msg6;
1379*0Sstevel@tonic-gate 	int num_dst_IPaddrs;
1380*0Sstevel@tonic-gate 	struct addrinfo *aip;
1381*0Sstevel@tonic-gate 	int i;
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 	if (!probe_all)
1384*0Sstevel@tonic-gate 		num_dst_IPaddrs = 1;
1385*0Sstevel@tonic-gate 	else
1386*0Sstevel@tonic-gate 		num_dst_IPaddrs = num_v4 + num_v6;
1387*0Sstevel@tonic-gate 
1388*0Sstevel@tonic-gate 	/*
1389*0Sstevel@tonic-gate 	 * Initialize the msg6 structure using the hoplimit for the first
1390*0Sstevel@tonic-gate 	 * probe packet, gateway addresses and the outgoing interface index.
1391*0Sstevel@tonic-gate 	 */
1392*0Sstevel@tonic-gate 	if (ai_dst->ai_family == AF_INET6 || (probe_all && num_v6)) {
1393*0Sstevel@tonic-gate 		msg6.msg_control = NULL;
1394*0Sstevel@tonic-gate 		msg6.msg_controllen = 0;
1395*0Sstevel@tonic-gate 		set_ancillary_data(&msg6, first_ttl, pr6->gwIPlist, gw_count,
1396*0Sstevel@tonic-gate 		    if_index);
1397*0Sstevel@tonic-gate 	}
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 	/* run traceroute for all the IP addresses of the multihomed dest */
1400*0Sstevel@tonic-gate 	for (aip = ai_dst, i = 0; i < num_dst_IPaddrs && aip != NULL; i++) {
1401*0Sstevel@tonic-gate 		union any_in_addr *addrp;
1402*0Sstevel@tonic-gate 		if (aip->ai_family == AF_INET) {
1403*0Sstevel@tonic-gate 			addrp = (union any_in_addr *)
1404*0Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
1405*0Sstevel@tonic-gate 			    &((struct sockaddr_in *)
1406*0Sstevel@tonic-gate 			    aip->ai_addr)->sin_addr;
1407*0Sstevel@tonic-gate 			set_sin((struct sockaddr *)pr4->to, addrp,
1408*0Sstevel@tonic-gate 			    aip->ai_family);
1409*0Sstevel@tonic-gate 			traceroute(addrp, &msg6, pr4, num_ifs4, al4);
1410*0Sstevel@tonic-gate 		} else {
1411*0Sstevel@tonic-gate 			addrp = (union any_in_addr *)
1412*0Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
1413*0Sstevel@tonic-gate 			    &((struct sockaddr_in6 *)
1414*0Sstevel@tonic-gate 			    aip->ai_addr)->sin6_addr;
1415*0Sstevel@tonic-gate 			set_sin((struct sockaddr *)pr6->to, addrp,
1416*0Sstevel@tonic-gate 			    aip->ai_family);
1417*0Sstevel@tonic-gate 			traceroute(addrp, &msg6, pr6, num_ifs6, al6);
1418*0Sstevel@tonic-gate 		}
1419*0Sstevel@tonic-gate 		aip = aip->ai_next;
1420*0Sstevel@tonic-gate 		if (i < (num_dst_IPaddrs - 1))
1421*0Sstevel@tonic-gate 			(void) putchar('\n');
1422*0Sstevel@tonic-gate 	}
1423*0Sstevel@tonic-gate }
1424*0Sstevel@tonic-gate 
1425*0Sstevel@tonic-gate /*
1426*0Sstevel@tonic-gate  * set the IP address in a sockaddr struct
1427*0Sstevel@tonic-gate  */
1428*0Sstevel@tonic-gate static void
1429*0Sstevel@tonic-gate set_sin(struct sockaddr *sock, union any_in_addr *addr, int family)
1430*0Sstevel@tonic-gate {
1431*0Sstevel@tonic-gate 	sock->sa_family = family;
1432*0Sstevel@tonic-gate 
1433*0Sstevel@tonic-gate 	if (family == AF_INET)
1434*0Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1435*0Sstevel@tonic-gate 		((struct sockaddr_in *)sock)->sin_addr = addr->addr;
1436*0Sstevel@tonic-gate 	else
1437*0Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1438*0Sstevel@tonic-gate 		((struct sockaddr_in6 *)sock)->sin6_addr = addr->addr6;
1439*0Sstevel@tonic-gate }
1440*0Sstevel@tonic-gate 
1441*0Sstevel@tonic-gate /*
1442*0Sstevel@tonic-gate  * returns the IF name on which the given IP address is configured
1443*0Sstevel@tonic-gate  */
1444*0Sstevel@tonic-gate static char *
1445*0Sstevel@tonic-gate device_name(struct ifaddrlist *al, int len, union any_in_addr *ip_addr,
1446*0Sstevel@tonic-gate     struct pr_set *pr)
1447*0Sstevel@tonic-gate {
1448*0Sstevel@tonic-gate 	int i;
1449*0Sstevel@tonic-gate 	struct ifaddrlist *tmp_al;
1450*0Sstevel@tonic-gate 
1451*0Sstevel@tonic-gate 	tmp_al = al;
1452*0Sstevel@tonic-gate 
1453*0Sstevel@tonic-gate 	for (i = 0; i < len; i++, tmp_al++) {
1454*0Sstevel@tonic-gate 		if (memcmp(&tmp_al->addr, ip_addr, pr->addr_len) == 0) {
1455*0Sstevel@tonic-gate 			return (tmp_al->device);
1456*0Sstevel@tonic-gate 		}
1457*0Sstevel@tonic-gate 	}
1458*0Sstevel@tonic-gate 
1459*0Sstevel@tonic-gate 	return (NULL);
1460*0Sstevel@tonic-gate }
1461*0Sstevel@tonic-gate 
1462*0Sstevel@tonic-gate /*
1463*0Sstevel@tonic-gate  * Trace the route to the host with given IP address.
1464*0Sstevel@tonic-gate  */
1465*0Sstevel@tonic-gate static void
1466*0Sstevel@tonic-gate traceroute(union any_in_addr *ip_addr, struct msghdr *msg6, struct pr_set *pr,
1467*0Sstevel@tonic-gate     int num_ifs, struct ifaddrlist *al)
1468*0Sstevel@tonic-gate {
1469*0Sstevel@tonic-gate 	int ttl;
1470*0Sstevel@tonic-gate 	int probe;
1471*0Sstevel@tonic-gate 	uchar_t type;				/* icmp type */
1472*0Sstevel@tonic-gate 	uchar_t code;				/* icmp code */
1473*0Sstevel@tonic-gate 	int reply;
1474*0Sstevel@tonic-gate 	int seq = 0;
1475*0Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
1476*0Sstevel@tonic-gate 	int longjmp_return;			/* return value from longjump */
1477*0Sstevel@tonic-gate 	struct ip *ip = (struct ip *)packet;
1478*0Sstevel@tonic-gate 	boolean_t got_there = _B_FALSE;		/* we hit the destination */
1479*0Sstevel@tonic-gate 	static boolean_t first_pkt = _B_TRUE;
1480*0Sstevel@tonic-gate 	int hoplimit;				/* hoplimit for IPv6 packets */
1481*0Sstevel@tonic-gate 	struct in6_addr addr6;
1482*0Sstevel@tonic-gate 	int num_src_ifs;			/* excludes down and loopback */
1483*0Sstevel@tonic-gate 	struct msghdr in_msg;
1484*0Sstevel@tonic-gate 	struct iovec iov;
1485*0Sstevel@tonic-gate 	int *intp;
1486*0Sstevel@tonic-gate 	int sndsock;
1487*0Sstevel@tonic-gate 	int rcvsock;
1488*0Sstevel@tonic-gate 
1489*0Sstevel@tonic-gate 	msg6->msg_name = pr->to;
1490*0Sstevel@tonic-gate 	msg6->msg_namelen = sizeof (struct sockaddr_in6);
1491*0Sstevel@tonic-gate 	sndsock =  (pr->family == AF_INET) ? sndsock4 : sndsock6;
1492*0Sstevel@tonic-gate 	rcvsock =  (pr->family == AF_INET) ? rcvsock4 : rcvsock6;
1493*0Sstevel@tonic-gate 
1494*0Sstevel@tonic-gate 	/* carry out the source address selection */
1495*0Sstevel@tonic-gate 	if (pick_src) {
1496*0Sstevel@tonic-gate 		union any_in_addr src_addr;
1497*0Sstevel@tonic-gate 		char *dev_name;
1498*0Sstevel@tonic-gate 		int i;
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 		/*
1501*0Sstevel@tonic-gate 		 * If there's a gateway, a routing header as a consequence, our
1502*0Sstevel@tonic-gate 		 * kernel picks the source address based on the first hop
1503*0Sstevel@tonic-gate 		 * address, rather than final destination address.
1504*0Sstevel@tonic-gate 		 */
1505*0Sstevel@tonic-gate 		if (gw_count > 0) {
1506*0Sstevel@tonic-gate 			(void) select_src_addr(pr->gwIPlist, &src_addr,
1507*0Sstevel@tonic-gate 			    pr->family);
1508*0Sstevel@tonic-gate 		} else {
1509*0Sstevel@tonic-gate 			(void) select_src_addr(ip_addr, &src_addr, pr->family);
1510*0Sstevel@tonic-gate 		}
1511*0Sstevel@tonic-gate 		set_sin(pr->from, &src_addr, pr->family);
1512*0Sstevel@tonic-gate 
1513*0Sstevel@tonic-gate 		/* filter out down and loopback interfaces */
1514*0Sstevel@tonic-gate 		num_src_ifs = 0;
1515*0Sstevel@tonic-gate 		for (i = 0; i < num_ifs; i++) {
1516*0Sstevel@tonic-gate 			if (!(al[i].flags & IFF_LOOPBACK) &&
1517*0Sstevel@tonic-gate 			    (al[i].flags & IFF_UP))
1518*0Sstevel@tonic-gate 				num_src_ifs++;
1519*0Sstevel@tonic-gate 		}
1520*0Sstevel@tonic-gate 
1521*0Sstevel@tonic-gate 		if (num_src_ifs > 1) {
1522*0Sstevel@tonic-gate 			dev_name = device_name(al, num_ifs, &src_addr, pr);
1523*0Sstevel@tonic-gate 			if (dev_name == NULL)
1524*0Sstevel@tonic-gate 				dev_name = "?";
1525*0Sstevel@tonic-gate 
1526*0Sstevel@tonic-gate 			Fprintf(stderr,
1527*0Sstevel@tonic-gate 			    "%s: Warning: Multiple interfaces found;"
1528*0Sstevel@tonic-gate 			    " using %s @ %s\n",
1529*0Sstevel@tonic-gate 			    prog, inet_ntop(pr->family,
1530*0Sstevel@tonic-gate 				(const void *)pr->from_sin_addr,
1531*0Sstevel@tonic-gate 				temp_buf, sizeof (temp_buf)),
1532*0Sstevel@tonic-gate 			    dev_name);
1533*0Sstevel@tonic-gate 		}
1534*0Sstevel@tonic-gate 	}
1535*0Sstevel@tonic-gate 
1536*0Sstevel@tonic-gate 	if (pr->family == AF_INET) {
1537*0Sstevel@tonic-gate 		outip4->ip_src = *(struct in_addr *)pr->from_sin_addr;
1538*0Sstevel@tonic-gate 		outip4->ip_dst = ip_addr->addr;
1539*0Sstevel@tonic-gate 	}
1540*0Sstevel@tonic-gate 
1541*0Sstevel@tonic-gate 	/*
1542*0Sstevel@tonic-gate 	 * If the hostname is an IPv6 literal address, let's not print it twice.
1543*0Sstevel@tonic-gate 	 */
1544*0Sstevel@tonic-gate 	if (pr->family == AF_INET6 &&
1545*0Sstevel@tonic-gate 	    inet_pton(AF_INET6, hostname, &addr6) > 0) {
1546*0Sstevel@tonic-gate 		Fprintf(stderr, "%s to %s", prog, hostname);
1547*0Sstevel@tonic-gate 	} else {
1548*0Sstevel@tonic-gate 		Fprintf(stderr, "%s to %s (%s)", prog, hostname,
1549*0Sstevel@tonic-gate 		    inet_ntop(pr->family, (const void *)ip_addr, temp_buf,
1550*0Sstevel@tonic-gate 			sizeof (temp_buf)));
1551*0Sstevel@tonic-gate 	}
1552*0Sstevel@tonic-gate 
1553*0Sstevel@tonic-gate 	if (source)
1554*0Sstevel@tonic-gate 		Fprintf(stderr, " from %s", source);
1555*0Sstevel@tonic-gate 	Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl,
1556*0Sstevel@tonic-gate 	    pr->packlen);
1557*0Sstevel@tonic-gate 	(void) fflush(stderr);
1558*0Sstevel@tonic-gate 
1559*0Sstevel@tonic-gate 	/*
1560*0Sstevel@tonic-gate 	 * Setup the source routing for IPv4. For IPv6, we did the required
1561*0Sstevel@tonic-gate 	 * setup in the caller function, trace_it(), because it's independent
1562*0Sstevel@tonic-gate 	 * from the IP address of target.
1563*0Sstevel@tonic-gate 	 */
1564*0Sstevel@tonic-gate 	if (pr->family == AF_INET && gw_count > 0)
1565*0Sstevel@tonic-gate 		set_IPv4opt_sourcerouting(sndsock, ip_addr, pr->gwIPlist);
1566*0Sstevel@tonic-gate 
1567*0Sstevel@tonic-gate 	if (probe_all) {
1568*0Sstevel@tonic-gate 		/* interrupt handler sig_handler() jumps back to here */
1569*0Sstevel@tonic-gate 		if ((longjmp_return = setjmp(env)) != 0) {
1570*0Sstevel@tonic-gate 			switch (longjmp_return) {
1571*0Sstevel@tonic-gate 			case SIGINT:
1572*0Sstevel@tonic-gate 				Printf("(skipping)\n");
1573*0Sstevel@tonic-gate 				return;
1574*0Sstevel@tonic-gate 			case SIGQUIT:
1575*0Sstevel@tonic-gate 				Printf("(exiting)\n");
1576*0Sstevel@tonic-gate 				exit(EXIT_SUCCESS);
1577*0Sstevel@tonic-gate 			default:	/* should never happen */
1578*0Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1579*0Sstevel@tonic-gate 			}
1580*0Sstevel@tonic-gate 		}
1581*0Sstevel@tonic-gate 		(void) signal(SIGINT, sig_handler);
1582*0Sstevel@tonic-gate 	}
1583*0Sstevel@tonic-gate 
1584*0Sstevel@tonic-gate 	for (ttl = first_ttl; ttl <= max_ttl; ++ttl) {
1585*0Sstevel@tonic-gate 		union any_in_addr lastaddr;
1586*0Sstevel@tonic-gate 		int timeouts = 0;
1587*0Sstevel@tonic-gate 		double rtt;		/* for statistics */
1588*0Sstevel@tonic-gate 		int nreceived = 0;
1589*0Sstevel@tonic-gate 		double rttmin, rttmax;
1590*0Sstevel@tonic-gate 		double rttsum, rttssq;
1591*0Sstevel@tonic-gate 		int unreachable;
1592*0Sstevel@tonic-gate 
1593*0Sstevel@tonic-gate 		got_there = _B_FALSE;
1594*0Sstevel@tonic-gate 		unreachable = 0;
1595*0Sstevel@tonic-gate 
1596*0Sstevel@tonic-gate 		/*
1597*0Sstevel@tonic-gate 		 * The following line clears both IPv4 and IPv6 address stored
1598*0Sstevel@tonic-gate 		 * in the union.
1599*0Sstevel@tonic-gate 		 */
1600*0Sstevel@tonic-gate 		lastaddr.addr6 = in6addr_any;
1601*0Sstevel@tonic-gate 
1602*0Sstevel@tonic-gate 		if ((ttl == (first_ttl + 1)) && (options & SO_DONTROUTE)) {
1603*0Sstevel@tonic-gate 			Fprintf(stderr,
1604*0Sstevel@tonic-gate 			    "%s: host %s is not on a directly-attached"
1605*0Sstevel@tonic-gate 			    " network\n", prog, hostname);
1606*0Sstevel@tonic-gate 			break;
1607*0Sstevel@tonic-gate 		}
1608*0Sstevel@tonic-gate 
1609*0Sstevel@tonic-gate 		Printf("%2d ", ttl);
1610*0Sstevel@tonic-gate 		(void) fflush(stdout);
1611*0Sstevel@tonic-gate 
1612*0Sstevel@tonic-gate 		for (probe = 0; (probe < nprobes) && (timeouts < max_timeout);
1613*0Sstevel@tonic-gate 		    ++probe) {
1614*0Sstevel@tonic-gate 			int cc;
1615*0Sstevel@tonic-gate 			struct timeval t1, t2;
1616*0Sstevel@tonic-gate 
1617*0Sstevel@tonic-gate 			/*
1618*0Sstevel@tonic-gate 			 * Put a delay before sending this probe packet. Don't
1619*0Sstevel@tonic-gate 			 * delay it if it's the very first packet.
1620*0Sstevel@tonic-gate 			 */
1621*0Sstevel@tonic-gate 			if (!first_pkt) {
1622*0Sstevel@tonic-gate 				if (delay.tv_sec > 0)
1623*0Sstevel@tonic-gate 					(void) sleep((uint_t)delay.tv_sec);
1624*0Sstevel@tonic-gate 				if (delay.tv_usec > 0)
1625*0Sstevel@tonic-gate 					(void) usleep(delay.tv_usec);
1626*0Sstevel@tonic-gate 			} else {
1627*0Sstevel@tonic-gate 				first_pkt = _B_FALSE;
1628*0Sstevel@tonic-gate 			}
1629*0Sstevel@tonic-gate 
1630*0Sstevel@tonic-gate 			(void) gettimeofday(&t1, NULL);
1631*0Sstevel@tonic-gate 
1632*0Sstevel@tonic-gate 			if (pr->family == AF_INET) {
1633*0Sstevel@tonic-gate 				send_probe(sndsock, pr->to, outip4, seq, ttl,
1634*0Sstevel@tonic-gate 				    &t1, pr->packlen);
1635*0Sstevel@tonic-gate 			} else {
1636*0Sstevel@tonic-gate 				send_probe6(sndsock, msg6, outip6, seq, ttl,
1637*0Sstevel@tonic-gate 				    &t1, pr->packlen);
1638*0Sstevel@tonic-gate 			}
1639*0Sstevel@tonic-gate 
1640*0Sstevel@tonic-gate 			/* prepare msghdr for recvmsg() */
1641*0Sstevel@tonic-gate 			in_msg.msg_name = pr->from;
1642*0Sstevel@tonic-gate 			in_msg.msg_namelen = pr->sock_size;
1643*0Sstevel@tonic-gate 
1644*0Sstevel@tonic-gate 			iov.iov_base = (char *)packet;
1645*0Sstevel@tonic-gate 			iov.iov_len = sizeof (packet);
1646*0Sstevel@tonic-gate 
1647*0Sstevel@tonic-gate 			in_msg.msg_iov = &iov;
1648*0Sstevel@tonic-gate 			in_msg.msg_iovlen = 1;
1649*0Sstevel@tonic-gate 
1650*0Sstevel@tonic-gate 			in_msg.msg_control = ancillary_data;
1651*0Sstevel@tonic-gate 			in_msg.msg_controllen = sizeof (ancillary_data);
1652*0Sstevel@tonic-gate 
1653*0Sstevel@tonic-gate 			while ((cc = wait_for_reply(rcvsock, &in_msg,
1654*0Sstevel@tonic-gate 			    &t1)) != 0) {
1655*0Sstevel@tonic-gate 				(void) gettimeofday(&t2, NULL);
1656*0Sstevel@tonic-gate 
1657*0Sstevel@tonic-gate 				reply = (*pr->check_reply_fn) (&in_msg, cc, seq,
1658*0Sstevel@tonic-gate 				    &type, &code);
1659*0Sstevel@tonic-gate 
1660*0Sstevel@tonic-gate 				in_msg.msg_controllen =
1661*0Sstevel@tonic-gate 				    sizeof (ancillary_data);
1662*0Sstevel@tonic-gate 				/* Skip short packet */
1663*0Sstevel@tonic-gate 				if (reply == REPLY_SHORT_PKT) {
1664*0Sstevel@tonic-gate 					continue;
1665*0Sstevel@tonic-gate 				}
1666*0Sstevel@tonic-gate 
1667*0Sstevel@tonic-gate 				timeouts = 0;
1668*0Sstevel@tonic-gate 
1669*0Sstevel@tonic-gate 				/*
1670*0Sstevel@tonic-gate 				 * if reply comes from a different host, print
1671*0Sstevel@tonic-gate 				 * the hostname
1672*0Sstevel@tonic-gate 				 */
1673*0Sstevel@tonic-gate 				if (memcmp(pr->from_sin_addr, &lastaddr,
1674*0Sstevel@tonic-gate 				    pr->addr_len) != 0) {
1675*0Sstevel@tonic-gate 					(*pr->print_addr_fn) ((uchar_t *)packet,
1676*0Sstevel@tonic-gate 					    cc, pr->from);
1677*0Sstevel@tonic-gate 					/* store the address response */
1678*0Sstevel@tonic-gate 					(void) memcpy(&lastaddr,
1679*0Sstevel@tonic-gate 					    pr->from_sin_addr, pr->addr_len);
1680*0Sstevel@tonic-gate 				}
1681*0Sstevel@tonic-gate 
1682*0Sstevel@tonic-gate 				rtt = deltaT(&t1, &t2);
1683*0Sstevel@tonic-gate 				if (collect_stat) {
1684*0Sstevel@tonic-gate 					record_stats(rtt, &nreceived, &rttmin,
1685*0Sstevel@tonic-gate 					    &rttmax, &rttsum, &rttssq);
1686*0Sstevel@tonic-gate 				} else {
1687*0Sstevel@tonic-gate 					Printf("  %.3f ms", rtt);
1688*0Sstevel@tonic-gate 				}
1689*0Sstevel@tonic-gate 
1690*0Sstevel@tonic-gate 				if (pr->family == AF_INET6) {
1691*0Sstevel@tonic-gate 					intp =
1692*0Sstevel@tonic-gate 					    (int *)find_ancillary_data(&in_msg,
1693*0Sstevel@tonic-gate 						IPPROTO_IPV6, IPV6_HOPLIMIT);
1694*0Sstevel@tonic-gate 					if (intp == NULL) {
1695*0Sstevel@tonic-gate 						Fprintf(stderr,
1696*0Sstevel@tonic-gate 						    "%s: can't find "
1697*0Sstevel@tonic-gate 						    "IPV6_HOPLIMIT ancillary "
1698*0Sstevel@tonic-gate 						    "data\n", prog);
1699*0Sstevel@tonic-gate 						exit(EXIT_FAILURE);
1700*0Sstevel@tonic-gate 					}
1701*0Sstevel@tonic-gate 					hoplimit = *intp;
1702*0Sstevel@tonic-gate 				}
1703*0Sstevel@tonic-gate 
1704*0Sstevel@tonic-gate 				if (reply == REPLY_GOT_TARGET) {
1705*0Sstevel@tonic-gate 					got_there = _B_TRUE;
1706*0Sstevel@tonic-gate 
1707*0Sstevel@tonic-gate 					if (((pr->family == AF_INET) &&
1708*0Sstevel@tonic-gate 					    (ip->ip_ttl <= 1)) ||
1709*0Sstevel@tonic-gate 					    ((pr->family == AF_INET6) &&
1710*0Sstevel@tonic-gate 					    (hoplimit <= 1)))
1711*0Sstevel@tonic-gate 						Printf(" !");
1712*0Sstevel@tonic-gate 				}
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 				if (!collect_stat && showttl) {
1715*0Sstevel@tonic-gate 					if (pr->family == AF_INET) {
1716*0Sstevel@tonic-gate 						Printf(" (ttl=%d)",
1717*0Sstevel@tonic-gate 						    (int)ip->ip_ttl);
1718*0Sstevel@tonic-gate 					} else if (hoplimit != -1) {
1719*0Sstevel@tonic-gate 						Printf(" (hop limit=%d)",
1720*0Sstevel@tonic-gate 						    hoplimit);
1721*0Sstevel@tonic-gate 					}
1722*0Sstevel@tonic-gate 				}
1723*0Sstevel@tonic-gate 
1724*0Sstevel@tonic-gate 				if (reply == REPLY_GOT_OTHER) {
1725*0Sstevel@tonic-gate 					if ((*pr->print_icmp_other_fn)
1726*0Sstevel@tonic-gate 					    (type, code)) {
1727*0Sstevel@tonic-gate 						unreachable++;
1728*0Sstevel@tonic-gate 					}
1729*0Sstevel@tonic-gate 				}
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate 				/* special case */
1732*0Sstevel@tonic-gate 				if (pr->family == AF_INET &&
1733*0Sstevel@tonic-gate 				    type == ICMP_UNREACH &&
1734*0Sstevel@tonic-gate 				    code == ICMP_UNREACH_PROTOCOL)
1735*0Sstevel@tonic-gate 					got_there = _B_TRUE;
1736*0Sstevel@tonic-gate 
1737*0Sstevel@tonic-gate 				break;
1738*0Sstevel@tonic-gate 			}
1739*0Sstevel@tonic-gate 
1740*0Sstevel@tonic-gate 			seq = (seq + 1) % (MAX_SEQ + 1);
1741*0Sstevel@tonic-gate 
1742*0Sstevel@tonic-gate 			if (cc == 0) {
1743*0Sstevel@tonic-gate 				Printf(" *");
1744*0Sstevel@tonic-gate 				timeouts++;
1745*0Sstevel@tonic-gate 			}
1746*0Sstevel@tonic-gate 
1747*0Sstevel@tonic-gate 			(void) fflush(stdout);
1748*0Sstevel@tonic-gate 		}
1749*0Sstevel@tonic-gate 
1750*0Sstevel@tonic-gate 		if (collect_stat) {
1751*0Sstevel@tonic-gate 			print_stats(probe, nreceived, rttmin, rttmax, rttsum,
1752*0Sstevel@tonic-gate 			    rttssq);
1753*0Sstevel@tonic-gate 		}
1754*0Sstevel@tonic-gate 
1755*0Sstevel@tonic-gate 		(void) putchar('\n');
1756*0Sstevel@tonic-gate 
1757*0Sstevel@tonic-gate 		/* either we hit the target or received too many unreachables */
1758*0Sstevel@tonic-gate 		if (got_there ||
1759*0Sstevel@tonic-gate 		    (unreachable > 0 && unreachable >= nprobes - 1))
1760*0Sstevel@tonic-gate 			break;
1761*0Sstevel@tonic-gate 	}
1762*0Sstevel@tonic-gate 
1763*0Sstevel@tonic-gate 	/* Ignore the SIGINT between traceroute() runs */
1764*0Sstevel@tonic-gate 	if (probe_all)
1765*0Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
1766*0Sstevel@tonic-gate }
1767*0Sstevel@tonic-gate 
1768*0Sstevel@tonic-gate /*
1769*0Sstevel@tonic-gate  * for a given destination address and address family, it finds out what
1770*0Sstevel@tonic-gate  * source address kernel is going to pick
1771*0Sstevel@tonic-gate  */
1772*0Sstevel@tonic-gate static void
1773*0Sstevel@tonic-gate select_src_addr(union any_in_addr *dst_addr, union any_in_addr *src_addr,
1774*0Sstevel@tonic-gate     int family)
1775*0Sstevel@tonic-gate {
1776*0Sstevel@tonic-gate 	int tmp_fd;
1777*0Sstevel@tonic-gate 	struct sockaddr *sock;
1778*0Sstevel@tonic-gate 	struct sockaddr_in *sin;
1779*0Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
1780*0Sstevel@tonic-gate 	size_t sock_len;
1781*0Sstevel@tonic-gate 
1782*0Sstevel@tonic-gate 	sock = (struct sockaddr *)malloc(sizeof (struct sockaddr_in6));
1783*0Sstevel@tonic-gate 	if (sock == NULL) {
1784*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: malloc %s\n", prog, strerror(errno));
1785*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1786*0Sstevel@tonic-gate 	}
1787*0Sstevel@tonic-gate 	(void) bzero(sock, sizeof (struct sockaddr_in6));
1788*0Sstevel@tonic-gate 
1789*0Sstevel@tonic-gate 	if (family == AF_INET) {
1790*0Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1791*0Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sock;
1792*0Sstevel@tonic-gate 		sin->sin_family = AF_INET;
1793*0Sstevel@tonic-gate 		sin->sin_addr = dst_addr->addr;
1794*0Sstevel@tonic-gate 		sin->sin_port = IPPORT_ECHO;	/* port shouldn't be 0 */
1795*0Sstevel@tonic-gate 		sock_len = sizeof (struct sockaddr_in);
1796*0Sstevel@tonic-gate 	} else {
1797*0Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1798*0Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sock;
1799*0Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
1800*0Sstevel@tonic-gate 		sin6->sin6_addr = dst_addr->addr6;
1801*0Sstevel@tonic-gate 		sin6->sin6_port = IPPORT_ECHO;	/* port shouldn't be 0 */
1802*0Sstevel@tonic-gate 		sock_len = sizeof (struct sockaddr_in6);
1803*0Sstevel@tonic-gate 	}
1804*0Sstevel@tonic-gate 
1805*0Sstevel@tonic-gate 	/* open a UDP socket */
1806*0Sstevel@tonic-gate 	if ((tmp_fd = socket(family, SOCK_DGRAM, 0)) < 0) {
1807*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: udp socket: %s\n", prog,
1808*0Sstevel@tonic-gate 		    strerror(errno));
1809*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1810*0Sstevel@tonic-gate 	}
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate 	/* connect it */
1813*0Sstevel@tonic-gate 	if (connect(tmp_fd, sock, sock_len) < 0) {
1814*0Sstevel@tonic-gate 		/*
1815*0Sstevel@tonic-gate 		 * If there's no route to the destination, this connect() call
1816*0Sstevel@tonic-gate 		 * fails. We just return all-zero (wildcard) as the source
1817*0Sstevel@tonic-gate 		 * address, so that user can get to see "no route to dest"
1818*0Sstevel@tonic-gate 		 * message, as it'll try to send the probe packet out and will
1819*0Sstevel@tonic-gate 		 * receive ICMP unreachable.
1820*0Sstevel@tonic-gate 		 */
1821*0Sstevel@tonic-gate 		if (family == AF_INET)
1822*0Sstevel@tonic-gate 			src_addr->addr.s_addr = INADDR_ANY;
1823*0Sstevel@tonic-gate 		else
1824*0Sstevel@tonic-gate 			src_addr->addr6 = in6addr_any;
1825*0Sstevel@tonic-gate 		free(sock);
1826*0Sstevel@tonic-gate 		return;
1827*0Sstevel@tonic-gate 	}
1828*0Sstevel@tonic-gate 
1829*0Sstevel@tonic-gate 	/* get the local sock info */
1830*0Sstevel@tonic-gate 	if (getsockname(tmp_fd, sock, &sock_len) < 0) {
1831*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: getsockname: %s\n", prog,
1832*0Sstevel@tonic-gate 		    strerror(errno));
1833*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1834*0Sstevel@tonic-gate 	}
1835*0Sstevel@tonic-gate 
1836*0Sstevel@tonic-gate 	if (family == AF_INET) {
1837*0Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1838*0Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sock;
1839*0Sstevel@tonic-gate 		src_addr->addr = sin->sin_addr;
1840*0Sstevel@tonic-gate 	} else {
1841*0Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1842*0Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sock;
1843*0Sstevel@tonic-gate 		src_addr->addr6 = sin6->sin6_addr;
1844*0Sstevel@tonic-gate 	}
1845*0Sstevel@tonic-gate 
1846*0Sstevel@tonic-gate 	free(sock);
1847*0Sstevel@tonic-gate 	(void) close(tmp_fd);
1848*0Sstevel@tonic-gate }
1849*0Sstevel@tonic-gate 
1850*0Sstevel@tonic-gate /*
1851*0Sstevel@tonic-gate  * Checksum routine for Internet Protocol family headers (C Version)
1852*0Sstevel@tonic-gate  */
1853*0Sstevel@tonic-gate ushort_t
1854*0Sstevel@tonic-gate in_cksum(ushort_t *addr, int len)
1855*0Sstevel@tonic-gate {
1856*0Sstevel@tonic-gate 	int nleft = len;
1857*0Sstevel@tonic-gate 	ushort_t *w = addr;
1858*0Sstevel@tonic-gate 	ushort_t answer;
1859*0Sstevel@tonic-gate 	int sum = 0;
1860*0Sstevel@tonic-gate 
1861*0Sstevel@tonic-gate 	/*
1862*0Sstevel@tonic-gate 	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
1863*0Sstevel@tonic-gate 	 *  we add sequential 16 bit words to it, and at the end, fold
1864*0Sstevel@tonic-gate 	 *  back all the carry bits from the top 16 bits into the lower
1865*0Sstevel@tonic-gate 	 *  16 bits.
1866*0Sstevel@tonic-gate 	 */
1867*0Sstevel@tonic-gate 	while (nleft > 1)  {
1868*0Sstevel@tonic-gate 		sum += *w++;
1869*0Sstevel@tonic-gate 		nleft -= 2;
1870*0Sstevel@tonic-gate 	}
1871*0Sstevel@tonic-gate 
1872*0Sstevel@tonic-gate 	/* mop up an odd byte, if necessary */
1873*0Sstevel@tonic-gate 	if (nleft == 1)
1874*0Sstevel@tonic-gate 		sum += *(uchar_t *)w;
1875*0Sstevel@tonic-gate 
1876*0Sstevel@tonic-gate 	/* add back carry outs from top 16 bits to low 16 bits */
1877*0Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
1878*0Sstevel@tonic-gate 	sum += (sum >> 16);			/* add carry */
1879*0Sstevel@tonic-gate 	answer = ~sum;				/* truncate to 16 bits */
1880*0Sstevel@tonic-gate 	return (answer);
1881*0Sstevel@tonic-gate }
1882*0Sstevel@tonic-gate 
1883*0Sstevel@tonic-gate /*
1884*0Sstevel@tonic-gate  * Wait until a reply arrives or timeout occurs. If packet arrived, read it
1885*0Sstevel@tonic-gate  * return the size of the packet read.
1886*0Sstevel@tonic-gate  */
1887*0Sstevel@tonic-gate static int
1888*0Sstevel@tonic-gate wait_for_reply(int sock, struct msghdr *msg, struct timeval *tp)
1889*0Sstevel@tonic-gate {
1890*0Sstevel@tonic-gate 	fd_set fds;
1891*0Sstevel@tonic-gate 	struct timeval now, wait;
1892*0Sstevel@tonic-gate 	int cc = 0;
1893*0Sstevel@tonic-gate 	int result;
1894*0Sstevel@tonic-gate 
1895*0Sstevel@tonic-gate 	(void) FD_ZERO(&fds);
1896*0Sstevel@tonic-gate 	FD_SET(sock, &fds);
1897*0Sstevel@tonic-gate 
1898*0Sstevel@tonic-gate 	wait.tv_sec = tp->tv_sec + waittime;
1899*0Sstevel@tonic-gate 	wait.tv_usec = tp->tv_usec;
1900*0Sstevel@tonic-gate 	(void) gettimeofday(&now, NULL);
1901*0Sstevel@tonic-gate 	tv_sub(&wait, &now);
1902*0Sstevel@tonic-gate 
1903*0Sstevel@tonic-gate 	if (wait.tv_sec < 0 || wait.tv_usec < 0)
1904*0Sstevel@tonic-gate 		return (0);
1905*0Sstevel@tonic-gate 
1906*0Sstevel@tonic-gate 	result = select(sock + 1, &fds, (fd_set *)NULL, (fd_set *)NULL, &wait);
1907*0Sstevel@tonic-gate 
1908*0Sstevel@tonic-gate 	if (result == -1) {
1909*0Sstevel@tonic-gate 		if (errno != EINTR) {
1910*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: select: %s\n", prog,
1911*0Sstevel@tonic-gate 			    strerror(errno));
1912*0Sstevel@tonic-gate 		}
1913*0Sstevel@tonic-gate 	} else if (result > 0)
1914*0Sstevel@tonic-gate 		cc = recvmsg(sock, msg, 0);
1915*0Sstevel@tonic-gate 
1916*0Sstevel@tonic-gate 	return (cc);
1917*0Sstevel@tonic-gate }
1918*0Sstevel@tonic-gate 
1919*0Sstevel@tonic-gate /*
1920*0Sstevel@tonic-gate  * Construct an Internet address representation. If the nflag has been supplied,
1921*0Sstevel@tonic-gate  * give numeric value, otherwise try for symbolic name.
1922*0Sstevel@tonic-gate  */
1923*0Sstevel@tonic-gate char *
1924*0Sstevel@tonic-gate inet_name(union any_in_addr *in, int family)
1925*0Sstevel@tonic-gate {
1926*0Sstevel@tonic-gate 	char *cp;
1927*0Sstevel@tonic-gate 	static boolean_t first = _B_TRUE;
1928*0Sstevel@tonic-gate 	static char domain[NI_MAXHOST + 1];
1929*0Sstevel@tonic-gate 	static char line[NI_MAXHOST + 1];	/* assuming		*/
1930*0Sstevel@tonic-gate 				/* (NI_MAXHOST + 1) >= INET6_ADDRSTRLEN */
1931*0Sstevel@tonic-gate 	char hbuf[NI_MAXHOST];
1932*0Sstevel@tonic-gate 	socklen_t slen;
1933*0Sstevel@tonic-gate 	struct sockaddr_in sin;
1934*0Sstevel@tonic-gate 	struct sockaddr_in6 sin6;
1935*0Sstevel@tonic-gate 	struct sockaddr *sa;
1936*0Sstevel@tonic-gate 	int flags;
1937*0Sstevel@tonic-gate 
1938*0Sstevel@tonic-gate 	switch (family) {
1939*0Sstevel@tonic-gate 	case AF_INET:
1940*0Sstevel@tonic-gate 		slen = sizeof (struct sockaddr_in);
1941*0Sstevel@tonic-gate 		sin.sin_addr = in->addr;
1942*0Sstevel@tonic-gate 		sin.sin_port = 0;
1943*0Sstevel@tonic-gate 		sa = (struct sockaddr *)&sin;
1944*0Sstevel@tonic-gate 		break;
1945*0Sstevel@tonic-gate 	case AF_INET6:
1946*0Sstevel@tonic-gate 		slen = sizeof (struct sockaddr_in6);
1947*0Sstevel@tonic-gate 		sin6.sin6_addr = in->addr6;
1948*0Sstevel@tonic-gate 		sin6.sin6_port = 0;
1949*0Sstevel@tonic-gate 		sa = (struct sockaddr *)&sin6;
1950*0Sstevel@tonic-gate 		break;
1951*0Sstevel@tonic-gate 	deafult:
1952*0Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
1953*0Sstevel@tonic-gate 		    "<invalid address family>");
1954*0Sstevel@tonic-gate 		return (line);
1955*0Sstevel@tonic-gate 	}
1956*0Sstevel@tonic-gate 	sa->sa_family = family;
1957*0Sstevel@tonic-gate 
1958*0Sstevel@tonic-gate 	if (first && !nflag) {
1959*0Sstevel@tonic-gate 		/* find out the domain name */
1960*0Sstevel@tonic-gate 		first = _B_FALSE;
1961*0Sstevel@tonic-gate 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1962*0Sstevel@tonic-gate 		    (cp = strchr(domain, '.')) != NULL) {
1963*0Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain) - 1);
1964*0Sstevel@tonic-gate 			domain[sizeof (domain) - 1] = '\0';
1965*0Sstevel@tonic-gate 		} else {
1966*0Sstevel@tonic-gate 			domain[0] = '\0';
1967*0Sstevel@tonic-gate 		}
1968*0Sstevel@tonic-gate 	}
1969*0Sstevel@tonic-gate 
1970*0Sstevel@tonic-gate 	flags = (nflag) ? NI_NUMERICHOST : NI_NAMEREQD;
1971*0Sstevel@tonic-gate 	if (getnameinfo(sa, slen, hbuf, sizeof (hbuf), NULL, 0, flags) != 0) {
1972*0Sstevel@tonic-gate 		if (inet_ntop(family, (const void *)&in->addr6,
1973*0Sstevel@tonic-gate 		    hbuf, sizeof (hbuf)) == NULL)
1974*0Sstevel@tonic-gate 			hbuf[0] = 0;
1975*0Sstevel@tonic-gate 	} else if (!nflag && (cp = strchr(hbuf, '.')) != NULL &&
1976*0Sstevel@tonic-gate 	    strcmp(cp + 1, domain) == 0) {
1977*0Sstevel@tonic-gate 		*cp = '\0';
1978*0Sstevel@tonic-gate 	}
1979*0Sstevel@tonic-gate 	(void) strlcpy(line, hbuf, sizeof (line));
1980*0Sstevel@tonic-gate 
1981*0Sstevel@tonic-gate 	return (line);
1982*0Sstevel@tonic-gate }
1983*0Sstevel@tonic-gate 
1984*0Sstevel@tonic-gate /*
1985*0Sstevel@tonic-gate  * return the difference (in msec) between two time values
1986*0Sstevel@tonic-gate  */
1987*0Sstevel@tonic-gate static double
1988*0Sstevel@tonic-gate deltaT(struct timeval *t1p, struct timeval *t2p)
1989*0Sstevel@tonic-gate {
1990*0Sstevel@tonic-gate 	double dt;
1991*0Sstevel@tonic-gate 
1992*0Sstevel@tonic-gate 	dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
1993*0Sstevel@tonic-gate 	    (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
1994*0Sstevel@tonic-gate 	return (dt);
1995*0Sstevel@tonic-gate }
1996*0Sstevel@tonic-gate 
1997*0Sstevel@tonic-gate /*
1998*0Sstevel@tonic-gate  * Subtract 2 timeval structs:  out = out - in.
1999*0Sstevel@tonic-gate  * Out is assumed to be >= in.
2000*0Sstevel@tonic-gate  */
2001*0Sstevel@tonic-gate static void
2002*0Sstevel@tonic-gate tv_sub(struct timeval *out, struct timeval *in)
2003*0Sstevel@tonic-gate {
2004*0Sstevel@tonic-gate 	if ((out->tv_usec -= in->tv_usec) < 0)   {
2005*0Sstevel@tonic-gate 		--out->tv_sec;
2006*0Sstevel@tonic-gate 		out->tv_usec += 1000000;
2007*0Sstevel@tonic-gate 	}
2008*0Sstevel@tonic-gate 	out->tv_sec -= in->tv_sec;
2009*0Sstevel@tonic-gate }
2010*0Sstevel@tonic-gate 
2011*0Sstevel@tonic-gate /*
2012*0Sstevel@tonic-gate  * record statistics
2013*0Sstevel@tonic-gate  */
2014*0Sstevel@tonic-gate static void
2015*0Sstevel@tonic-gate record_stats(double rtt, int *nreceived, double *rttmin, double *rttmax,
2016*0Sstevel@tonic-gate     double *rttsum, double *rttssq)
2017*0Sstevel@tonic-gate {
2018*0Sstevel@tonic-gate 	if (*nreceived == 0) {
2019*0Sstevel@tonic-gate 		*rttmin = rtt;
2020*0Sstevel@tonic-gate 		*rttmax = rtt;
2021*0Sstevel@tonic-gate 		*rttsum = rtt;
2022*0Sstevel@tonic-gate 		*rttssq = rtt * rtt;
2023*0Sstevel@tonic-gate 	} else {
2024*0Sstevel@tonic-gate 		if (rtt < *rttmin)
2025*0Sstevel@tonic-gate 			*rttmin = rtt;
2026*0Sstevel@tonic-gate 
2027*0Sstevel@tonic-gate 		if (rtt > *rttmax)
2028*0Sstevel@tonic-gate 			*rttmax = rtt;
2029*0Sstevel@tonic-gate 
2030*0Sstevel@tonic-gate 		*rttsum += rtt;
2031*0Sstevel@tonic-gate 		*rttssq += rtt * rtt;
2032*0Sstevel@tonic-gate 	}
2033*0Sstevel@tonic-gate 
2034*0Sstevel@tonic-gate 	(*nreceived)++;
2035*0Sstevel@tonic-gate }
2036*0Sstevel@tonic-gate 
2037*0Sstevel@tonic-gate /*
2038*0Sstevel@tonic-gate  * display statistics
2039*0Sstevel@tonic-gate  */
2040*0Sstevel@tonic-gate static void
2041*0Sstevel@tonic-gate print_stats(int ntransmitted, int nreceived, double rttmin, double rttmax,
2042*0Sstevel@tonic-gate     double rttsum, double rttssq)
2043*0Sstevel@tonic-gate {
2044*0Sstevel@tonic-gate 	double rttavg;			/* average round-trip time */
2045*0Sstevel@tonic-gate 	double rttstd;			/* rtt standard deviation */
2046*0Sstevel@tonic-gate 
2047*0Sstevel@tonic-gate 	if (ntransmitted > 0 && ntransmitted >= nreceived) {
2048*0Sstevel@tonic-gate 		int missed = ntransmitted - nreceived;
2049*0Sstevel@tonic-gate 		double loss = 100 * (double)missed / (double)ntransmitted;
2050*0Sstevel@tonic-gate 
2051*0Sstevel@tonic-gate 		if (nreceived > 0) {
2052*0Sstevel@tonic-gate 			rttavg = rttsum / nreceived;
2053*0Sstevel@tonic-gate 			rttstd = rttssq - (rttavg * rttsum);
2054*0Sstevel@tonic-gate 			rttstd = xsqrt(rttstd / nreceived);
2055*0Sstevel@tonic-gate 
2056*0Sstevel@tonic-gate 			Printf("  %.3f", rttmin);
2057*0Sstevel@tonic-gate 			Printf("/%.3f", rttavg);
2058*0Sstevel@tonic-gate 			Printf("/%.3f", rttmax);
2059*0Sstevel@tonic-gate 
2060*0Sstevel@tonic-gate 			Printf(" (%.3f) ms ", rttstd);
2061*0Sstevel@tonic-gate 		}
2062*0Sstevel@tonic-gate 
2063*0Sstevel@tonic-gate 		Printf(" %d/%d pkts", nreceived, ntransmitted);
2064*0Sstevel@tonic-gate 
2065*0Sstevel@tonic-gate 		if (nreceived == 0)
2066*0Sstevel@tonic-gate 			Printf(" (100%% loss)");
2067*0Sstevel@tonic-gate 		else
2068*0Sstevel@tonic-gate 			Printf(" (%.2g%% loss)", loss);
2069*0Sstevel@tonic-gate 	}
2070*0Sstevel@tonic-gate }
2071*0Sstevel@tonic-gate 
2072*0Sstevel@tonic-gate /*
2073*0Sstevel@tonic-gate  * square root function
2074*0Sstevel@tonic-gate  */
2075*0Sstevel@tonic-gate double
2076*0Sstevel@tonic-gate xsqrt(double y)
2077*0Sstevel@tonic-gate {
2078*0Sstevel@tonic-gate 	double t, x;
2079*0Sstevel@tonic-gate 
2080*0Sstevel@tonic-gate 	if (y <= 0) {
2081*0Sstevel@tonic-gate 		return (0.0);
2082*0Sstevel@tonic-gate 	}
2083*0Sstevel@tonic-gate 
2084*0Sstevel@tonic-gate 	x = (y < 1.0) ? 1.0 : y;
2085*0Sstevel@tonic-gate 	do {
2086*0Sstevel@tonic-gate 		t = x;
2087*0Sstevel@tonic-gate 		x = (t + (y/t))/2.0;
2088*0Sstevel@tonic-gate 	} while (0 < x && x < t);
2089*0Sstevel@tonic-gate 
2090*0Sstevel@tonic-gate 	return (x);
2091*0Sstevel@tonic-gate }
2092*0Sstevel@tonic-gate 
2093*0Sstevel@tonic-gate /*
2094*0Sstevel@tonic-gate  * String to double with optional min and max.
2095*0Sstevel@tonic-gate  */
2096*0Sstevel@tonic-gate static double
2097*0Sstevel@tonic-gate str2dbl(const char *str, const char *what, double mi, double ma)
2098*0Sstevel@tonic-gate {
2099*0Sstevel@tonic-gate 	double val;
2100*0Sstevel@tonic-gate 	char *ep;
2101*0Sstevel@tonic-gate 
2102*0Sstevel@tonic-gate 	errno = 0;
2103*0Sstevel@tonic-gate 
2104*0Sstevel@tonic-gate 	val = strtod(str, &ep);
2105*0Sstevel@tonic-gate 	if (errno != 0 || *ep != '\0') {
2106*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
2107*0Sstevel@tonic-gate 		    prog, str, what);
2108*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2109*0Sstevel@tonic-gate 	}
2110*0Sstevel@tonic-gate 	if (val < mi && mi >= 0) {
2111*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s must be >= %f\n", prog, what, mi);
2112*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2113*0Sstevel@tonic-gate 	}
2114*0Sstevel@tonic-gate 	if (val > ma && ma >= 0) {
2115*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s must be <= %f\n", prog, what, ma);
2116*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2117*0Sstevel@tonic-gate 	}
2118*0Sstevel@tonic-gate 	return (val);
2119*0Sstevel@tonic-gate }
2120*0Sstevel@tonic-gate 
2121*0Sstevel@tonic-gate /*
2122*0Sstevel@tonic-gate  * String to int with optional min and max. Handles decimal and hex.
2123*0Sstevel@tonic-gate  */
2124*0Sstevel@tonic-gate static int
2125*0Sstevel@tonic-gate str2int(const char *str, const char *what, int mi, int ma)
2126*0Sstevel@tonic-gate {
2127*0Sstevel@tonic-gate 	const char *cp;
2128*0Sstevel@tonic-gate 	int val;
2129*0Sstevel@tonic-gate 	char *ep;
2130*0Sstevel@tonic-gate 
2131*0Sstevel@tonic-gate 	errno = 0;
2132*0Sstevel@tonic-gate 
2133*0Sstevel@tonic-gate 	if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
2134*0Sstevel@tonic-gate 		cp = str + 2;
2135*0Sstevel@tonic-gate 		val = (int)strtol(cp, &ep, 16);
2136*0Sstevel@tonic-gate 	} else {
2137*0Sstevel@tonic-gate 		val = (int)strtol(str, &ep, 10);
2138*0Sstevel@tonic-gate 	}
2139*0Sstevel@tonic-gate 	if (errno != 0 || *ep != '\0') {
2140*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
2141*0Sstevel@tonic-gate 		    prog, str, what);
2142*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2143*0Sstevel@tonic-gate 	}
2144*0Sstevel@tonic-gate 	if (val < mi && mi >= 0) {
2145*0Sstevel@tonic-gate 		if (mi == 0) {
2146*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: %s must be >= %d\n",
2147*0Sstevel@tonic-gate 			    prog, what, mi);
2148*0Sstevel@tonic-gate 		} else {
2149*0Sstevel@tonic-gate 			Fprintf(stderr, "%s: %s must be > %d\n",
2150*0Sstevel@tonic-gate 			    prog, what, mi - 1);
2151*0Sstevel@tonic-gate 		}
2152*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2153*0Sstevel@tonic-gate 	}
2154*0Sstevel@tonic-gate 	if (val > ma && ma >= 0) {
2155*0Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s must be <= %d\n", prog, what, ma);
2156*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2157*0Sstevel@tonic-gate 	}
2158*0Sstevel@tonic-gate 	return (val);
2159*0Sstevel@tonic-gate }
2160*0Sstevel@tonic-gate 
2161*0Sstevel@tonic-gate /*
2162*0Sstevel@tonic-gate  * This is the interrupt handler for SIGINT and SIGQUIT. It's completely handled
2163*0Sstevel@tonic-gate  * where it jumps to.
2164*0Sstevel@tonic-gate  */
2165*0Sstevel@tonic-gate static void
2166*0Sstevel@tonic-gate sig_handler(int sig)
2167*0Sstevel@tonic-gate {
2168*0Sstevel@tonic-gate 	longjmp(env, sig);
2169*0Sstevel@tonic-gate }
2170*0Sstevel@tonic-gate 
2171*0Sstevel@tonic-gate /*
2172*0Sstevel@tonic-gate  * display the usage of traceroute
2173*0Sstevel@tonic-gate  */
2174*0Sstevel@tonic-gate static void
2175*0Sstevel@tonic-gate usage(void)
2176*0Sstevel@tonic-gate {
2177*0Sstevel@tonic-gate 	Fprintf(stderr, "Usage: %s [-adFIlnSvx] [-A address_family] "
2178*0Sstevel@tonic-gate "[-c traffic_class] \n"
2179*0Sstevel@tonic-gate "\t[-f first_hop] [-g gateway [-g gateway ...]| -r] [-i iface]\n"
2180*0Sstevel@tonic-gate "\t[-L flow_label] [-m max_hop] [-P pause_sec] [-p port] [-Q max_timeout]\n"
2181*0Sstevel@tonic-gate "\t[-q nqueries] [-s src_addr] [-t tos] [-w wait_time] host [packetlen]\n",
2182*0Sstevel@tonic-gate 		prog);
2183*0Sstevel@tonic-gate 	exit(EXIT_FAILURE);
2184*0Sstevel@tonic-gate }
2185