Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / traceroute / traceroute.c
1 /*
2  * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21
22 #ifndef lint
23 static const char copyright[] =
24     "@(#) Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996\n\
25 The Regents of the University of California.  All rights reserved.\n";
26 #if 0
27 static const char rcsid[] =
28     "@(#)$Header: traceroute.c,v 1.43 96/09/27 20:08:10 leres Exp $ (LBL)";
29 #endif
30 static const char rcsid[] =
31     "$FreeBSD: src/contrib/traceroute/traceroute.c,v 1.11.2.6 2002/08/05 23:37:16 fenner Exp $";
32 #endif
33
34 /*
35  * traceroute host  - trace the route ip packets follow going to "host".
36  *
37  * Attempt to trace the route an ip packet would follow to some
38  * internet host.  We find out intermediate hops by launching probe
39  * packets with a small ttl (time to live) then listening for an
40  * icmp "time exceeded" reply from a gateway.  We start our probes
41  * with a ttl of one and increase by one until we get an icmp "port
42  * unreachable" (which means we got to "host") or hit a max (which
43  * defaults to net.inet.ip.ttl hops & can be changed with the -m flag).
44  * Three probes (change with -q flag) are sent at each ttl setting and
45  * a line is printed showing the ttl, address of the gateway and
46  * round trip time of each probe.  If the probe answers come from
47  * different gateways, the address of each responding system will
48  * be printed.  If there is no response within a 5 sec. timeout
49  * interval (changed with the -w flag), a "*" is printed for that
50  * probe.
51  *
52  * Probe packets are UDP format.  We don't want the destination
53  * host to process them so the destination port is set to an
54  * unlikely value (if some clod on the destination is using that
55  * value, it can be changed with the -p flag).
56  *
57  * A sample use might be:
58  *
59  *     [yak 71]% traceroute nis.nsf.net.
60  *     traceroute to nis.nsf.net (35.1.1.48), 64 hops max, 56 byte packet
61  *      1  helios.ee.lbl.gov (128.3.112.1)  19 ms  19 ms  0 ms
62  *      2  lilac-dmc.Berkeley.EDU (128.32.216.1)  39 ms  39 ms  19 ms
63  *      3  lilac-dmc.Berkeley.EDU (128.32.216.1)  39 ms  39 ms  19 ms
64  *      4  ccngw-ner-cc.Berkeley.EDU (128.32.136.23)  39 ms  40 ms  39 ms
65  *      5  ccn-nerif22.Berkeley.EDU (128.32.168.22)  39 ms  39 ms  39 ms
66  *      6  128.32.197.4 (128.32.197.4)  40 ms  59 ms  59 ms
67  *      7  131.119.2.5 (131.119.2.5)  59 ms  59 ms  59 ms
68  *      8  129.140.70.13 (129.140.70.13)  99 ms  99 ms  80 ms
69  *      9  129.140.71.6 (129.140.71.6)  139 ms  239 ms  319 ms
70  *     10  129.140.81.7 (129.140.81.7)  220 ms  199 ms  199 ms
71  *     11  nic.merit.edu (35.1.1.48)  239 ms  239 ms  239 ms
72  *
73  * Note that lines 2 & 3 are the same.  This is due to a buggy
74  * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
75  * packets with a zero ttl.
76  *
77  * A more interesting example is:
78  *
79  *     [yak 72]% traceroute allspice.lcs.mit.edu.
80  *     traceroute to allspice.lcs.mit.edu (18.26.0.115), 64 hops max
81  *      1  helios.ee.lbl.gov (128.3.112.1)  0 ms  0 ms  0 ms
82  *      2  lilac-dmc.Berkeley.EDU (128.32.216.1)  19 ms  19 ms  19 ms
83  *      3  lilac-dmc.Berkeley.EDU (128.32.216.1)  39 ms  19 ms  19 ms
84  *      4  ccngw-ner-cc.Berkeley.EDU (128.32.136.23)  19 ms  39 ms  39 ms
85  *      5  ccn-nerif22.Berkeley.EDU (128.32.168.22)  20 ms  39 ms  39 ms
86  *      6  128.32.197.4 (128.32.197.4)  59 ms  119 ms  39 ms
87  *      7  131.119.2.5 (131.119.2.5)  59 ms  59 ms  39 ms
88  *      8  129.140.70.13 (129.140.70.13)  80 ms  79 ms  99 ms
89  *      9  129.140.71.6 (129.140.71.6)  139 ms  139 ms  159 ms
90  *     10  129.140.81.7 (129.140.81.7)  199 ms  180 ms  300 ms
91  *     11  129.140.72.17 (129.140.72.17)  300 ms  239 ms  239 ms
92  *     12  * * *
93  *     13  128.121.54.72 (128.121.54.72)  259 ms  499 ms  279 ms
94  *     14  * * *
95  *     15  * * *
96  *     16  * * *
97  *     17  * * *
98  *     18  ALLSPICE.LCS.MIT.EDU (18.26.0.115)  339 ms  279 ms  279 ms
99  *
100  * (I start to see why I'm having so much trouble with mail to
101  * MIT.)  Note that the gateways 12, 14, 15, 16 & 17 hops away
102  * either don't send ICMP "time exceeded" messages or send them
103  * with a ttl too small to reach us.  14 - 17 are running the
104  * MIT C Gateway code that doesn't send "time exceeded"s.  God
105  * only knows what's going on with 12.
106  *
107  * The silent gateway 12 in the above may be the result of a bug in
108  * the 4.[23]BSD network code (and its derivatives):  4.x (x <= 3)
109  * sends an unreachable message using whatever ttl remains in the
110  * original datagram.  Since, for gateways, the remaining ttl is
111  * zero, the icmp "time exceeded" is guaranteed to not make it back
112  * to us.  The behavior of this bug is slightly more interesting
113  * when it appears on the destination system:
114  *
115  *      1  helios.ee.lbl.gov (128.3.112.1)  0 ms  0 ms  0 ms
116  *      2  lilac-dmc.Berkeley.EDU (128.32.216.1)  39 ms  19 ms  39 ms
117  *      3  lilac-dmc.Berkeley.EDU (128.32.216.1)  19 ms  39 ms  19 ms
118  *      4  ccngw-ner-cc.Berkeley.EDU (128.32.136.23)  39 ms  40 ms  19 ms
119  *      5  ccn-nerif35.Berkeley.EDU (128.32.168.35)  39 ms  39 ms  39 ms
120  *      6  csgw.Berkeley.EDU (128.32.133.254)  39 ms  59 ms  39 ms
121  *      7  * * *
122  *      8  * * *
123  *      9  * * *
124  *     10  * * *
125  *     11  * * *
126  *     12  * * *
127  *     13  rip.Berkeley.EDU (128.32.131.22)  59 ms !  39 ms !  39 ms !
128  *
129  * Notice that there are 12 "gateways" (13 is the final
130  * destination) and exactly the last half of them are "missing".
131  * What's really happening is that rip (a Sun-3 running Sun OS3.5)
132  * is using the ttl from our arriving datagram as the ttl in its
133  * icmp reply.  So, the reply will time out on the return path
134  * (with no notice sent to anyone since icmp's aren't sent for
135  * icmp's) until we probe with a ttl that's at least twice the path
136  * length.  I.e., rip is really only 7 hops away.  A reply that
137  * returns with a ttl of 1 is a clue this problem exists.
138  * Traceroute prints a "!" after the time if the ttl is <= 1.
139  * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
140  * non-standard (HPUX) software, expect to see this problem
141  * frequently and/or take care picking the target host of your
142  * probes.
143  *
144  * Other possible annotations after the time are !H, !N, !P (got a host,
145  * network or protocol unreachable, respectively), !S or !F (source
146  * route failed or fragmentation needed -- neither of these should
147  * ever occur and the associated gateway is busted if you see one).  If
148  * almost all the probes result in some kind of unreachable, traceroute
149  * will give up and exit.
150  *
151  * Notes
152  * -----
153  * This program must be run by root or be setuid.  (I suggest that
154  * you *don't* make it setuid -- casual use could result in a lot
155  * of unnecessary traffic on our poor, congested nets.)
156  *
157  * This program requires a kernel mod that does not appear in any
158  * system available from Berkeley:  A raw ip socket using proto
159  * IPPROTO_RAW must interpret the data sent as an ip datagram (as
160  * opposed to data to be wrapped in a ip datagram).  See the README
161  * file that came with the source to this program for a description
162  * of the mods I made to /sys/netinet/raw_ip.c.  Your mileage may
163  * vary.  But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
164  * MODIFIED TO RUN THIS PROGRAM.
165  *
166  * The udp port usage may appear bizarre (well, ok, it is bizarre).
167  * The problem is that an icmp message only contains 8 bytes of
168  * data from the original datagram.  8 bytes is the size of a udp
169  * header so, if we want to associate replies with the original
170  * datagram, the necessary information must be encoded into the
171  * udp header (the ip id could be used but there's no way to
172  * interlock with the kernel's assignment of ip id's and, anyway,
173  * it would have taken a lot more kernel hacking to allow this
174  * code to set the ip id).  So, to allow two or more users to
175  * use traceroute simultaneously, we use this task's pid as the
176  * source port (the high bit is set to move the port number out
177  * of the "likely" range).  To keep track of which probe is being
178  * replied to (so times and/or hop counts don't get confused by a
179  * reply that was delayed in transit), we increment the destination
180  * port number before each probe.
181  *
182  * Don't use this as a coding example.  I was trying to find a
183  * routing problem and this code sort-of popped out after 48 hours
184  * without sleep.  I was amazed it ever compiled, much less ran.
185  *
186  * I stole the idea for this program from Steve Deering.  Since
187  * the first release, I've learned that had I attended the right
188  * IETF working group meetings, I also could have stolen it from Guy
189  * Almes or Matt Mathis.  I don't know (or care) who came up with
190  * the idea first.  I envy the originators' perspicacity and I'm
191  * glad they didn't keep the idea a secret.
192  *
193  * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
194  * enhancements to the original distribution.
195  *
196  * I've hacked up a round-trip-route version of this that works by
197  * sending a loose-source-routed udp datagram through the destination
198  * back to yourself.  Unfortunately, SO many gateways botch source
199  * routing, the thing is almost worthless.  Maybe one day...
200  *
201  *  -- Van Jacobson (van@ee.lbl.gov)
202  *     Tue Dec 20 03:50:13 PST 1988
203  */
204
205 #include <sys/param.h>
206 #include <sys/file.h>
207 #include <sys/ioctl.h>
208 #ifdef HAVE_SYS_SELECT_H
209 #include <sys/select.h>
210 #endif
211 #include <sys/socket.h>
212 #ifdef HAVE_SYS_SYSCTL_H
213 #include <sys/sysctl.h>
214 #endif
215 #include <sys/time.h>
216
217 #include <netinet/in_systm.h>
218 #include <netinet/in.h>
219 #include <netinet/ip.h>
220 #include <netinet/ip_var.h>
221 #include <netinet/ip_icmp.h>
222 #include <netinet/udp.h>
223 #include <netinet/tcp.h>
224
225 #include <arpa/inet.h>
226
227 #ifdef  IPSEC
228 #include <net/route.h>
229 #include <netinet6/ipsec.h>     /* XXX */
230 #endif  /* IPSEC */
231
232 #include <ctype.h>
233 #include <errno.h>
234 #ifdef HAVE_MALLOC_H
235 #include <malloc.h>
236 #endif
237 #include <memory.h>
238 #include <netdb.h>
239 #include <stdio.h>
240 #include <stdlib.h>
241 #include <string.h>
242 #include <unistd.h>
243
244 #include "gnuc.h"
245 #ifdef HAVE_OS_PROTO_H
246 #include "os-proto.h"
247 #endif
248
249 /* Maximum number of gateways (include room for one noop) */
250 #define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(u_int32_t)))
251
252 #ifndef MAXHOSTNAMELEN
253 #define MAXHOSTNAMELEN  64
254 #endif
255
256 #define Fprintf (void)fprintf
257 #define Printf (void)printf
258
259 /* What a GRE packet header looks like */
260 struct grehdr {
261         u_int16_t   flags;
262         u_int16_t   proto;
263         u_int16_t   length;     /* PPTP version of these fields */
264         u_int16_t   callId;
265 };
266 #ifndef IPPROTO_GRE
267 #define IPPROTO_GRE     47
268 #endif
269
270 /* For GRE, we prepare what looks like a PPTP packet */
271 #define GRE_PPTP_PROTO  0x880b
272
273 /* Data section of the probe packet */
274 struct outdata {
275         u_char seq;             /* sequence number of this packet */
276         u_char ttl;             /* ttl packet left with */
277         struct timeval tv;      /* time packet left */
278         int    optlen;          /* length of ip options */
279 };
280
281 /* Descriptor structure for each outgoing protocol we support */
282 struct outproto {
283         char    *name;          /* name of protocol */
284         u_char  num;            /* IP protocol number */
285         u_short hdrlen;         /* max size of protocol header */
286         u_short port;           /* default base protocol-specific "port" */
287         void    (*prepare)(struct outdata *);
288                                 /* finish preparing an outgoing packet */
289         int     (*check)(const u_char *, int);
290                                 /* check an incoming packet */
291 };
292
293 u_char  packet[512];            /* last inbound (icmp) packet */
294
295 struct ip *outip;               /* last output ip packet */
296 u_char *outprot;                /* last output inner protocol packet */
297
298 /* loose source route gateway list (including room for final destination) */
299 u_int32_t gwlist[NGATEWAYS + 1];
300
301 int s;                          /* receive (icmp) socket file descriptor */
302 int sndsock;                    /* send (udp) socket file descriptor */
303
304 struct sockaddr whereto;        /* Who to try to reach */
305 int packlen;                    /* total length of packet */
306 int protlen;                    /* length of protocol part of packet */
307 int maxpacket = 32 * 1024;      /* max ip packet size */
308
309 char *prog;
310 char *source;
311 char *hostname;
312
313 int nprobes = 3;
314 int min_ttl = 1;
315 int max_ttl;
316 u_short ident;
317 u_short port;                   /* protocol specific base "port" */
318
319 int options;                    /* socket options */
320 int verbose;
321 int waittime = 5;               /* time to wait for response (in seconds) */
322 int nflag;                      /* print addresses numerically */
323
324 extern int optind;
325 extern int opterr;
326 extern char *optarg;
327
328 /* Forwards */
329 double  deltaT(struct timeval *, struct timeval *);
330 u_short in_cksum(u_short *, int);
331 char    *inetname(struct in_addr);
332 int     main(int, char **);
333 int     packet_ok(u_char *, int, struct sockaddr_in *, int);
334 char    *pr_type(u_char);
335 void    print(u_char *, int, struct sockaddr_in *);
336 char    *getaddr(u_int32_t *, char *);
337 char    *getsin(struct sockaddr_in *, char *);
338 char    *savestr(const char *);
339 #ifdef  IPSEC
340 int     setpolicy __P((int so, char *policy));
341 #endif
342 void    send_probe(int, int);
343 void    tvsub(struct timeval *, struct timeval *);
344 __dead  void usage(void);
345 int     wait_for_reply(int, struct sockaddr_in *, struct timeval *);
346
347 void    udp_prep(struct outdata *);
348 int     udp_check(const u_char *, int);
349 void    tcp_prep(struct outdata *);
350 int     tcp_check(const u_char *, int);
351 void    gre_prep(struct outdata *);
352 int     gre_check(const u_char *, int);
353 void    gen_prep(struct outdata *);
354 int     gen_check(const u_char *, int);
355 void    icmp_prep(struct outdata *);
356 int     icmp_check(const u_char *, int);
357
358 /* List of supported protocols. The first one is the default. The last
359    one is the handler for generic protocols not explicitly listed. */
360 struct  outproto protos[] = {
361         {
362                 "udp",
363                 IPPROTO_UDP,
364                 sizeof(struct udphdr),
365                 32768 + 666,
366                 udp_prep,
367                 udp_check
368         },
369         {
370                 "tcp",
371                 IPPROTO_TCP,
372                 sizeof(struct tcphdr),
373                 32768 + 666,
374                 tcp_prep,
375                 tcp_check
376         },
377         {
378                 "gre",
379                 IPPROTO_GRE,
380                 sizeof(struct grehdr),
381                 GRE_PPTP_PROTO,
382                 gre_prep,
383                 gre_check
384         },
385         {
386                 "icmp",
387                 IPPROTO_ICMP,
388                 sizeof(struct icmp),
389                 0,
390                 icmp_prep,
391                 icmp_check
392         },
393         {
394                 NULL,
395                 0,
396                 2 * sizeof(u_short),
397                 0,
398                 gen_prep,
399                 gen_check
400         },
401 };
402 struct  outproto *proto = &protos[0];
403
404 int
405 main(int argc, char **argv)
406 {
407         register int op, code;
408         register char *cp;
409         struct sockaddr_in from;
410         register struct sockaddr_in *to = (struct sockaddr_in *)&whereto;
411         int on = 1;
412         register struct protoent *pe;
413         register int ttl, probe, i;
414         register int seq = 0;
415         register int tos = 0;
416         register int lsrr = 0;
417         register int optlen = 0;
418         int requestPort = -1;
419         int sump = 0;
420         int sockerrno;
421
422         /*
423          * Do the setuid-required stuff first, then lose priveleges ASAP.
424          * Do error checking for these two calls where they appeared in
425          * the original code.
426          */
427         cp = "icmp";
428         pe = getprotobyname(cp);
429         if (pe) {
430                 if ((s = socket(AF_INET, SOCK_RAW, pe->p_proto)) < 0)
431                         sockerrno = errno;
432                 else if ((sndsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
433                         sockerrno = errno;
434         }
435
436         setuid(getuid());
437
438 #ifdef IPCTL_DEFTTL
439         {
440                 int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
441                 size_t sz = sizeof(max_ttl);
442
443                 if (sysctl(mib, 4, &max_ttl, &sz, NULL, 0) == -1)
444                         err(1, "sysctl(net.inet.ip.ttl)");
445         }
446 #else
447         max_ttl = 30;
448 #endif
449
450         if ((cp = strrchr(argv[0], '/')) != NULL)
451                 prog = cp + 1;
452         else
453                 prog = argv[0];
454
455         opterr = 0;
456         while ((op = getopt(argc, argv, "Sdnrvg:M:m:P:p:q:s:t:w:")) != EOF)
457                 switch (op) {
458
459                 case 'S':
460                         sump = 1;
461                         break;
462                 case 'd':
463                         options |= SO_DEBUG;
464                         break;
465
466                 case 'g':
467                         if (lsrr >= NGATEWAYS) {
468                                 Fprintf(stderr,
469                                     "%s: No more than %d gateways\n",
470                                     prog, NGATEWAYS);
471                                 exit(1);
472                         }
473                         (void)getaddr(gwlist + lsrr, optarg);
474                         ++lsrr;
475                         break;
476
477                 case 'M':
478                         min_ttl = atoi(optarg);
479                         if (min_ttl < 1 || min_ttl > 0xff) {
480                                 Fprintf(stderr, "%s: invalid ttl value %s\n",
481                                     prog, optarg);
482                                 exit(1);
483                         }
484                         break;
485
486                 case 'm':
487                         max_ttl = atoi(optarg);
488                         if (max_ttl < 1 || max_ttl > 0xff) {
489                                 Fprintf(stderr, "%s: invalid ttl value %s\n",
490                                     prog, optarg);
491                                 exit(1);
492                         }
493                         break;
494
495                 case 'n':
496                         ++nflag;
497                         break;
498
499                 case 'P':
500                         for (i = 0; protos[i].name != NULL; i++) {
501                                 if (strcasecmp(protos[i].name, optarg) == 0) {
502                                         break;
503                                 }
504                         }
505                         proto = &protos[i];
506                         if (proto->name == NULL) {      /* generic handler */
507                                 struct protoent *pe;
508                                 u_long pnum;
509                                 char *eptr;
510
511                                 /* Determine the IP protocol number */
512                                 if ((pe = getprotobyname(optarg)) != NULL)
513                                         pnum = pe->p_proto;
514                                 else {
515                                         pnum = strtoul(optarg, &eptr, 10);
516                                         if (pnum > 0xff
517                                             || *optarg == '\0'
518                                             || *eptr != '\0') {
519                                                 Fprintf(stderr, "%s: unknown "
520                                                     "protocol \"%s\"\n",
521                                                     prog, optarg);
522                                                 exit(1);
523                                         }
524                                 }
525                                 proto->num = pnum;
526                         }
527                         break;
528
529                 case 'p':
530                         requestPort = atoi(optarg);
531                         if (requestPort <= 0) {
532                                 Fprintf(stderr, "%s: port must be > 0\n", prog);
533                                 exit(1);
534                         }
535                         break;
536
537                 case 'q':
538                         nprobes = atoi(optarg);
539                         if (nprobes <= 0) {
540                                 Fprintf(stderr, "%s: nprobes must be > 0\n",
541                                     prog);
542                                 exit(1);
543                         }
544                         break;
545
546                 case 'r':
547                         options |= SO_DONTROUTE;
548                         break;
549
550                 case 's':
551                         /*
552                          * set the ip source address of the outbound
553                          * probe (e.g., on a multi-homed host).
554                          */
555                         source = optarg;
556                         break;
557
558                 case 't':
559                         tos = atoi(optarg);
560                         if (tos < 0 || tos > 255) {
561                                 Fprintf(stderr, "%s: tos must be 0 to 255\n",
562                                     prog);
563                                 exit(1);
564                         }
565                         break;
566
567                 case 'v':
568                         ++verbose;
569                         break;
570
571                 case 'w':
572                         waittime = atoi(optarg);
573                         if (waittime <= 1 || waittime >= 24L * 60 * 60) {
574                                 Fprintf(stderr,
575                                     "%s: wait must be > 1 sec and < 1 day\n",
576                                     prog);
577                                 exit(1);
578                         }
579                         break;
580
581                 default:
582                         usage();
583                 }
584
585         /* Set requested port, if any, else default for this protocol */
586         port = (requestPort != -1) ? requestPort : proto->port;
587
588         /* Check min vs. max TTL */
589         if (min_ttl > max_ttl) {
590                 Fprintf(stderr, "%s: min ttl must be <= max ttl\n", prog);
591                 exit(1);
592         }
593
594         /* Process destination and optional packet size */
595         switch (argc - optind) {
596
597         case 2:
598                 packlen = atoi(argv[optind + 1]);
599                 /* Fall thorugh */
600
601         case 1:
602                 hostname = savestr(getsin(to, argv[optind]));
603                 break;
604
605         default:
606                 usage();
607         }
608
609 #ifdef HAVE_SETLINEBUF
610         setlinebuf (stdout);
611 #else
612         setvbuf(stdout, NULL, _IOLBF, 0);
613 #endif
614
615         if (lsrr > 0)
616                 optlen = (lsrr + 1) * sizeof(gwlist[0]);
617         i = sizeof(*outip) + proto->hdrlen + sizeof(struct outdata) + optlen;
618         if (packlen == 0)
619                 packlen = i;                    /* minimum sized packet */
620         else if (i > packlen || packlen > maxpacket) {
621                 Fprintf(stderr, "%s: packet size must be %d <= s <= %d\n",
622                     prog, i, maxpacket);
623                 exit(1);
624         }
625         protlen = packlen - sizeof(*outip) - optlen;
626
627         outip = (struct ip *)malloc((unsigned)packlen);
628         if (outip == NULL) {
629                 Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno));
630                 exit(1);
631         }
632         memset((char *)outip, 0, packlen);
633
634         outip->ip_v = IPVERSION;
635         outip->ip_tos = tos;
636 #ifdef BYTESWAP_IP_LEN
637         outip->ip_len = htons(packlen);
638 #else
639         outip->ip_len = packlen;
640 #endif
641         outip->ip_p = proto->num;
642         outprot = (u_char *)(outip + 1);
643 #ifdef HAVE_RAW_OPTIONS
644         if (lsrr > 0) {
645                 register u_char *optlist;
646
647                 optlist = (u_char *)outprot;
648                 (u_char *)outprot += optlen;
649
650                 /* final hop */
651                 gwlist[lsrr] = to->sin_addr.s_addr;
652
653                 outip->ip_dst.s_addr = gwlist[0];
654
655                 /* force 4 byte alignment */
656                 optlist[0] = IPOPT_NOP;
657                 /* loose source route option */
658                 optlist[1] = IPOPT_LSRR;
659                 i = lsrr * sizeof(gwlist[0]);
660                 optlist[2] = i + 3;
661                 /* Pointer to LSRR addresses */
662                 optlist[3] = IPOPT_MINOFF;
663                 memcpy(optlist + 4, gwlist + 1, i);
664         } else
665 #endif
666                 outip->ip_dst = to->sin_addr;
667
668         outip->ip_hl = ((u_char *)outprot - (u_char *)outip) >> 2;
669
670         ident = (getpid() & 0xffff) | 0x8000;
671
672         if (pe == NULL) {
673                 Fprintf(stderr, "%s: unknown protocol %s\n", prog, cp);
674                 exit(1);
675         }
676         if (s < 0) {
677                 errno = sockerrno;
678                 Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno));
679                 exit(1);
680         }
681         if (options & SO_DEBUG)
682                 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&on,
683                     sizeof(on));
684         if (options & SO_DONTROUTE)
685                 (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
686                     sizeof(on));
687
688 #if     defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
689         if (setpolicy(s, "in bypass") < 0)
690                 errx(1, "%s", ipsec_strerror());
691
692         if (setpolicy(s, "out bypass") < 0)
693                 errx(1, "%s", ipsec_strerror());
694 #endif  /* defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) */
695
696         if (sndsock < 0) {
697                 errno = sockerrno;
698                 Fprintf(stderr, "%s: raw socket: %s\n", prog, strerror(errno));
699                 exit(1);
700         }
701
702 #if defined(IP_OPTIONS) && !defined(HAVE_RAW_OPTIONS)
703         if (lsrr > 0) {
704                 u_char optlist[MAX_IPOPTLEN];
705
706                 cp = "ip";
707                 if ((pe = getprotobyname(cp)) == NULL) {
708                         Fprintf(stderr, "%s: unknown protocol %s\n", prog, cp);
709                         exit(1);
710                 }
711
712                 /* final hop */
713                 gwlist[lsrr] = to->sin_addr.s_addr;
714                 ++lsrr;
715
716                 /* force 4 byte alignment */
717                 optlist[0] = IPOPT_NOP;
718                 /* loose source route option */
719                 optlist[1] = IPOPT_LSRR;
720                 i = lsrr * sizeof(gwlist[0]);
721                 optlist[2] = i + 3;
722                 /* Pointer to LSRR addresses */
723                 optlist[3] = IPOPT_MINOFF;
724                 memcpy(optlist + 4, gwlist, i);
725
726                 if ((setsockopt(sndsock, pe->p_proto, IP_OPTIONS, optlist,
727                     i + sizeof(gwlist[0]))) < 0) {
728                         Fprintf(stderr, "%s: IP_OPTIONS: %s\n",
729                             prog, strerror(errno));
730                         exit(1);
731                     }
732         }
733 #endif
734
735 #ifdef SO_SNDBUF
736         if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&packlen,
737             sizeof(packlen)) < 0) {
738                 Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno));
739                 exit(1);
740         }
741 #endif
742 #ifdef IP_HDRINCL
743         if (setsockopt(sndsock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
744             sizeof(on)) < 0) {
745                 Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog, strerror(errno));
746                 exit(1);
747         }
748 #endif
749         if (options & SO_DEBUG)
750                 (void)setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
751                     sizeof(on));
752         if (options & SO_DONTROUTE)
753                 (void)setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
754                     sizeof(on));
755
756         if (source != NULL) {
757                 source = savestr(getsin(&from, source));
758                 outip->ip_src = from.sin_addr;
759 #ifndef IP_HDRINCL
760                 if (bind(sndsock, (struct sockaddr *)&from, sizeof(from)) < 0) {
761                         Fprintf(stderr, "%s: bind: %s\n",
762                             prog, strerror(errno));
763                         exit (1);
764                 }
765 #endif
766         }
767
768 #if     defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
769         if (setpolicy(sndsock, "in bypass") < 0)
770                 errx(1, "%s", ipsec_strerror());
771
772         if (setpolicy(sndsock, "out bypass") < 0)
773                 errx(1, "%s", ipsec_strerror());
774 #endif  /* defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) */
775
776         Fprintf(stderr, "%s to %s (%s)",
777             prog, hostname, inet_ntoa(to->sin_addr));
778         if (source)
779                 Fprintf(stderr, " from %s", source);
780         Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl, packlen);
781         (void)fflush(stderr);
782
783         for (ttl = min_ttl; ttl <= max_ttl; ++ttl) {
784                 u_int32_t lastaddr = 0;
785                 int got_there = 0;
786                 int unreachable = 0;
787                 int loss;
788
789                 Printf("%2d ", ttl);
790                 for (probe = 0, loss = 0; probe < nprobes; ++probe) {
791                         register int cc;
792                         struct timeval t1, t2;
793                         struct timezone tz;
794                         register struct ip *ip;
795                         struct outdata outdata;
796
797                         /* Prepare outgoing data */
798                         outdata.seq = ++seq;
799                         outdata.ttl = ttl;
800                         outdata.optlen = optlen;
801
802                         /* Avoid alignment problems by copying bytewise: */
803                         (void)gettimeofday(&t1, &tz);
804                         memcpy(&outdata.tv, &t1, sizeof(outdata.tv));
805
806                         /* Finalize and send packet */
807                         (*proto->prepare)(&outdata);
808                         send_probe(seq, ttl);
809
810                         /* Wait for a reply */
811                         while ((cc = wait_for_reply(s, &from, &t1)) != 0) {
812                                 double T;
813                                 int precis;
814
815                                 (void)gettimeofday(&t2, &tz);
816                                 i = packet_ok(packet, cc, &from, seq);
817                                 /* Skip short packet */
818                                 if (i == 0)
819                                         continue;
820                                 if (from.sin_addr.s_addr != lastaddr) {
821                                         print(packet, cc, &from);
822                                         lastaddr = from.sin_addr.s_addr;
823                                 }
824                                 T = deltaT(&t1, &t2);
825 #ifdef SANE_PRECISION
826                                 if (T >= 1000.0)
827                                         precis = 0;
828                                 else if (T >= 100.0)
829                                         precis = 1;
830                                 else if (T >= 10.0)
831                                         precis = 2;
832                                 else
833 #endif
834                                         precis = 3;
835                                 Printf("  %.*f ms", precis, T);
836                                 if (i == -2) {
837 #ifndef ARCHAIC
838                                         ip = (struct ip *)packet;
839                                         if (ip->ip_ttl <= 1)
840                                                 Printf(" !");
841 #endif
842
843                                         ++got_there;
844                                         break;
845                                 }
846
847                                 /* time exceeded in transit */
848                                 if (i == -1)
849                                         break;
850                                         
851                                 code = i - 1;
852                                 switch (code) {
853
854                                 case ICMP_UNREACH_PORT:
855 #ifndef ARCHAIC
856                                         ip = (struct ip *)packet;
857                                         if (ip->ip_ttl <= 1)
858                                                 Printf(" !");
859 #endif
860                                         ++got_there;
861                                         break;
862
863                                 case ICMP_UNREACH_NET:
864                                         ++unreachable;
865                                         Printf(" !N");
866                                         break;
867
868                                 case ICMP_UNREACH_HOST:
869                                         ++unreachable;
870                                         Printf(" !H");
871                                         break;
872
873                                 case ICMP_UNREACH_PROTOCOL:
874                                         ++got_there;
875                                         Printf(" !P");
876                                         break;
877
878                                 case ICMP_UNREACH_NEEDFRAG:
879                                         ++unreachable;
880                                         Printf(" !F");
881                                         break;
882
883                                 case ICMP_UNREACH_SRCFAIL:
884                                         ++unreachable;
885                                         Printf(" !S");
886                                         break;
887
888 /* rfc1716 */
889 #ifndef ICMP_UNREACH_FILTER_PROHIB
890 #define ICMP_UNREACH_FILTER_PROHIB      13      /* admin prohibited filter */
891 #endif
892                                 case ICMP_UNREACH_FILTER_PROHIB:
893                                         ++unreachable;
894                                         Printf(" !X");
895                                         break;
896
897                                 default:
898                                         ++unreachable;
899                                         Printf(" !<%d>", code);
900                                         break;
901                                 }
902                                 break;
903                         }
904                         if (cc == 0) {
905                                 loss++;
906                                 Printf(" *");
907                         }
908                         (void)fflush(stdout);
909                 }
910                 if (sump) {
911                         Printf(" (%d%% loss)", (loss * 100) / nprobes);
912                 }
913                 putchar('\n');
914                 if (got_there ||
915                     (unreachable > 0 && unreachable >= nprobes - 1))
916                         break;
917         }
918         exit(0);
919 }
920
921 int
922 wait_for_reply(register int sock, register struct sockaddr_in *fromp,
923     register struct timeval *tp)
924 {
925         fd_set *fdsp;
926         size_t nfds;
927         struct timeval now, wait;
928         struct timezone tz;
929         register int cc = 0;
930         register int error;
931         int fromlen = sizeof(*fromp);
932
933         nfds = howmany(sock + 1, NFDBITS);
934         if ((fdsp = malloc(nfds * sizeof(fd_mask))) == NULL)
935                 err(1, "malloc");
936         memset(fdsp, 0, nfds * sizeof(fd_mask));
937         FD_SET(sock, fdsp);
938
939         wait.tv_sec = tp->tv_sec + waittime;
940         wait.tv_usec = tp->tv_usec;
941         (void)gettimeofday(&now, &tz);
942         tvsub(&wait, &now);
943         if (wait.tv_sec < 0) {
944                 wait.tv_sec = 0;
945                 wait.tv_usec = 1;
946         }
947
948         error = select(sock + 1, fdsp, NULL, NULL, &wait);
949         if (error == -1 && errno == EINVAL) {
950                 Fprintf(stderr, "%s: botched select() args\n", prog);
951                 exit(1);
952         }
953         if (error > 0)
954                 cc = recvfrom(s, (char *)packet, sizeof(packet), 0,
955                             (struct sockaddr *)fromp, &fromlen);
956
957         free(fdsp);
958         return(cc);
959 }
960
961 #if     defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
962 int
963 setpolicy(so, policy)
964         int so;
965         char *policy;
966 {
967         char *buf;
968
969         buf = ipsec_set_policy(policy, strlen(policy));
970         if (buf == NULL) {
971                 warnx("%s", ipsec_strerror());
972                 return -1;
973         }
974         (void)setsockopt(so, IPPROTO_IP, IP_IPSEC_POLICY,
975                 buf, ipsec_get_policylen(buf));
976
977         free(buf);
978
979         return 0;
980 }
981 #endif
982
983 void
984 send_probe(int seq, int ttl)
985 {
986         register int i;
987
988         outip->ip_ttl = ttl;
989         outip->ip_id = htons(ident + seq);
990
991         i = sendto(sndsock, (char *)outip, packlen, 0, &whereto,
992                    sizeof(whereto));
993         if (i < 0 || i != packlen)  {
994                 if (i < 0)
995                         Fprintf(stderr, "%s: sendto: %s\n",
996                             prog, strerror(errno));
997                 Printf("%s: wrote %s %d chars, ret=%d\n",
998                     prog, hostname, packlen, i);
999                 (void)fflush(stdout);
1000         }
1001 }
1002
1003 double
1004 deltaT(struct timeval *t1p, struct timeval *t2p)
1005 {
1006         register double dt;
1007
1008         dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
1009              (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
1010         return (dt);
1011 }
1012
1013 /*
1014  * Convert an ICMP "type" field to a printable string.
1015  */
1016 char *
1017 pr_type(register u_char t)
1018 {
1019         static char *ttab[] = {
1020         "Echo Reply",   "ICMP 1",       "ICMP 2",       "Dest Unreachable",
1021         "Source Quench", "Redirect",    "ICMP 6",       "ICMP 7",
1022         "Echo",         "ICMP 9",       "ICMP 10",      "Time Exceeded",
1023         "Param Problem", "Timestamp",   "Timestamp Reply", "Info Request",
1024         "Info Reply"
1025         };
1026
1027         if (t > 16)
1028                 return("OUT-OF-RANGE");
1029
1030         return(ttab[t]);
1031 }
1032
1033 int
1034 packet_ok(register u_char *buf, int cc, register struct sockaddr_in *from,
1035     register int seq)
1036 {
1037         register struct icmp *icp;
1038         register u_char type, code;
1039         register int hlen;
1040 #ifndef ARCHAIC
1041         register struct ip *ip;
1042
1043         ip = (struct ip *) buf;
1044         hlen = ip->ip_hl << 2;
1045         if (cc < hlen + ICMP_MINLEN) {
1046                 if (verbose)
1047                         Printf("packet too short (%d bytes) from %s\n", cc,
1048                                 inet_ntoa(from->sin_addr));
1049                 return (0);
1050         }
1051         cc -= hlen;
1052         icp = (struct icmp *)(buf + hlen);
1053 #else
1054         icp = (struct icmp *)buf;
1055 #endif
1056         type = icp->icmp_type;
1057         code = icp->icmp_code;
1058         if (type == ICMP_ECHOREPLY
1059             && proto->num == IPPROTO_ICMP
1060             && (*proto->check)((u_char *)icp,seq))
1061                 return -2;
1062         if ((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS) ||
1063             type == ICMP_UNREACH) {
1064                 struct ip *hip;
1065                 u_char *inner;
1066
1067                 hip = &icp->icmp_ip;
1068                 hlen = hip->ip_hl << 2;
1069                 inner = (u_char *)((u_char *)hip + hlen);
1070                 if (hlen + 12 <= cc
1071                     && hip->ip_p == proto->num
1072                     && (*proto->check)(inner, seq))
1073                         return (type == ICMP_TIMXCEED ? -1 : code + 1);
1074         }
1075 #ifndef ARCHAIC
1076         if (verbose) {
1077                 register int i;
1078                 u_int32_t *lp = (u_int32_t *)&icp->icmp_ip;
1079
1080                 Printf("\n%d bytes from %s to ", cc, inet_ntoa(from->sin_addr));
1081                 Printf("%s: icmp type %d (%s) code %d\n",
1082                     inet_ntoa(ip->ip_dst), type, pr_type(type), icp->icmp_code);
1083                 for (i = 4; i < cc ; i += sizeof(*lp))
1084                         Printf("%2d: x%8.8x\n", i, *lp++);
1085         }
1086 #endif
1087         return(0);
1088 }
1089
1090 void
1091 icmp_prep(struct outdata *outdata)
1092 {
1093         struct icmp *const icmpheader = (struct icmp *) outprot;
1094
1095         icmpheader->icmp_type = ICMP_ECHO;
1096         icmpheader->icmp_id = htons(ident);
1097         icmpheader->icmp_seq = htons(outdata->seq);
1098
1099         icmpheader->icmp_cksum = 0;
1100         icmpheader->icmp_cksum = in_cksum((u_short *)icmpheader,
1101             packlen - (sizeof(*outip) + outdata->optlen));
1102         if (icmpheader->icmp_cksum == 0)
1103                 icmpheader->icmp_cksum = 0xffff;
1104
1105 }
1106
1107 int
1108 icmp_check(const u_char *data, int seq)
1109 {
1110         struct icmp *const icmpheader = (struct icmp *) data;
1111
1112         return (icmpheader->icmp_id == htons(ident)
1113             && icmpheader->icmp_seq == htons(seq));
1114 }
1115
1116 void
1117 udp_prep(struct outdata *outdata)
1118 {
1119         struct udphdr *const udp = (struct udphdr *) outprot;
1120
1121         udp->uh_sport = htons(ident);
1122         udp->uh_dport = htons(port + outdata->seq);
1123         udp->uh_ulen = htons((u_short)protlen);
1124 }
1125
1126 int
1127 udp_check(const u_char *data, int seq)
1128 {
1129         struct udphdr *const udp = (struct udphdr *) data;
1130
1131         return (ntohs(udp->uh_sport) == ident
1132             && ntohs(udp->uh_dport) == port + seq);
1133 }
1134
1135 void
1136 tcp_prep(struct outdata *outdata)
1137 {
1138         struct tcphdr *const tcp = (struct tcphdr *) outprot;
1139
1140         tcp->th_sport = htons(ident);
1141         tcp->th_dport = htons(port + outdata->seq);
1142         tcp->th_seq = (tcp->th_sport << 16) | tcp->th_dport;
1143         tcp->th_ack = 0;
1144         tcp->th_off = 5;
1145         tcp->th_flags = TH_SYN;
1146 }
1147
1148 int
1149 tcp_check(const u_char *data, int seq)
1150 {
1151         struct tcphdr *const tcp = (struct tcphdr *) data;
1152
1153         return (ntohs(tcp->th_sport) == ident
1154             && ntohs(tcp->th_dport) == port + seq);
1155 }
1156
1157 void
1158 gre_prep(struct outdata *outdata)
1159 {
1160         struct grehdr *const gre = (struct grehdr *) outprot;
1161
1162         gre->flags = htons(0x2001);
1163         gre->proto = htons(port);
1164         gre->length = 0;
1165         gre->callId = htons(ident + outdata->seq);
1166 }
1167
1168 int
1169 gre_check(const u_char *data, int seq)
1170 {
1171         struct grehdr *const gre = (struct grehdr *) data;
1172
1173         return(ntohs(gre->proto) == port
1174             && ntohs(gre->callId) == ident + seq);
1175 }
1176
1177 void
1178 gen_prep(struct outdata *outdata)
1179 {
1180         u_int16_t *const ptr = (u_int16_t *) outprot;
1181
1182         ptr[0] = htons(ident);
1183         ptr[1] = htons(port + outdata->seq);
1184 }
1185
1186 int
1187 gen_check(const u_char *data, int seq)
1188 {
1189         u_int16_t *const ptr = (u_int16_t *) data;
1190
1191         return(ntohs(ptr[0]) == ident
1192             && ntohs(ptr[1]) == port + seq);
1193 }
1194
1195 void
1196 print(register u_char *buf, register int cc, register struct sockaddr_in *from)
1197 {
1198         register struct ip *ip;
1199         register int hlen;
1200
1201         ip = (struct ip *) buf;
1202         hlen = ip->ip_hl << 2;
1203         cc -= hlen;
1204
1205         if (nflag)
1206                 Printf(" %s", inet_ntoa(from->sin_addr));
1207         else
1208                 Printf(" %s (%s)", inetname(from->sin_addr),
1209                     inet_ntoa(from->sin_addr));
1210
1211         if (verbose)
1212                 Printf(" %d bytes to %s", cc, inet_ntoa (ip->ip_dst));
1213 }
1214
1215 /*
1216  * Checksum routine for Internet Protocol family headers (C Version)
1217  */
1218 u_short
1219 in_cksum(register u_short *addr, register int len)
1220 {
1221         register int nleft = len;
1222         register u_short *w = addr;
1223         register u_short answer;
1224         register int sum = 0;
1225
1226         /*
1227          *  Our algorithm is simple, using a 32 bit accumulator (sum),
1228          *  we add sequential 16 bit words to it, and at the end, fold
1229          *  back all the carry bits from the top 16 bits into the lower
1230          *  16 bits.
1231          */
1232         while (nleft > 1)  {
1233                 sum += *w++;
1234                 nleft -= 2;
1235         }
1236
1237         /* mop up an odd byte, if necessary */
1238         if (nleft == 1)
1239                 sum += *(u_char *)w;
1240
1241         /*
1242          * add back carry outs from top 16 bits to low 16 bits
1243          */
1244         sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
1245         sum += (sum >> 16);                     /* add carry */
1246         answer = ~sum;                          /* truncate to 16 bits */
1247         return (answer);
1248 }
1249
1250 /*
1251  * Subtract 2 timeval structs:  out = out - in.
1252  * Out is assumed to be within about LONG_MAX seconds of in.
1253  */
1254 void
1255 tvsub(register struct timeval *out, register struct timeval *in)
1256 {
1257
1258         if ((out->tv_usec -= in->tv_usec) < 0)   {
1259                 --out->tv_sec;
1260                 out->tv_usec += 1000000;
1261         }
1262         out->tv_sec -= in->tv_sec;
1263 }
1264
1265 /*
1266  * Construct an Internet address representation.
1267  * If the nflag has been supplied, give
1268  * numeric value, otherwise try for symbolic name.
1269  */
1270 char *
1271 inetname(struct in_addr in)
1272 {
1273         register char *cp;
1274         register struct hostent *hp;
1275         static int first = 1;
1276         static char domain[MAXHOSTNAMELEN + 1], line[MAXHOSTNAMELEN + 1];
1277
1278         if (first && !nflag) {
1279                 first = 0;
1280                 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1281                     (cp = strchr(domain, '.')) != NULL) {
1282                         (void)strncpy(domain, cp + 1, sizeof(domain) - 1);
1283                         domain[sizeof(domain) - 1] = '\0';
1284                 } else
1285                         domain[0] = '\0';
1286         }
1287         if (!nflag && in.s_addr != INADDR_ANY) {
1288                 hp = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
1289                 if (hp != NULL) {
1290                         if ((cp = strchr(hp->h_name, '.')) != NULL &&
1291                             strcmp(cp + 1, domain) == 0)
1292                                 *cp = '\0';
1293                         (void)strncpy(line, hp->h_name, sizeof(line) - 1);
1294                         line[sizeof(line) - 1] = '\0';
1295                         return (line);
1296                 }
1297         }
1298         return (inet_ntoa(in));
1299 }
1300
1301 char *
1302 getaddr(register u_int32_t *ap, register char *hostname)
1303 {
1304         register struct hostent *hp;
1305
1306         *ap = inet_addr(hostname);
1307         if ((int32_t)*ap != -1)
1308                 return (hostname);
1309
1310         hp = gethostbyname(hostname);
1311         if (hp == NULL) {
1312                 Fprintf(stderr, "%s: unknown host %s\n", prog, hostname);
1313                 exit(1);
1314         }
1315         if (hp->h_addrtype != AF_INET || hp->h_length != 4) {
1316                 Fprintf(stderr, "%s: bad host %s\n", prog, hostname);
1317                 exit(1);
1318         }
1319         memcpy((caddr_t)ap, hp->h_addr, hp->h_length);
1320         return (hp->h_name);
1321 }
1322
1323 char *
1324 getsin(register struct sockaddr_in *sin, register char *hostname)
1325 {
1326
1327         memset(sin, 0, sizeof(*sin));
1328         sin->sin_family = AF_INET;
1329         return (getaddr((u_int32_t *)&sin->sin_addr.s_addr, hostname));
1330 }
1331
1332 char *
1333 savestr(register const char *str)
1334 {
1335         register char *cp;
1336
1337         cp = strdup(str);
1338         if (cp == NULL) {
1339                 Fprintf(stderr, "%s: strdup: %s\n", prog, strerror(errno));
1340                 exit(1);
1341         }
1342         return (cp);
1343 }
1344
1345 __dead void
1346 usage(void)
1347 {
1348         extern char version[];
1349
1350         Fprintf(stderr, "Version %s\n", version);
1351         Fprintf(stderr, "Usage: %s [-Sdnrv] [-w wait] [-m max_ttl] [-M min_ttl] \
1352 [-P proto]\n\t [-p port#] [-q nqueries] [-t tos] [-s src_addr] [-g gateway] \
1353 \n\t host [data_size]\n", prog);
1354         exit(1);
1355 }