traceroute.c revision 1.8 1 1.8 glass /* $NetBSD: traceroute.c,v 1.8 1995/03/26 21:45:23 glass Exp $ */
2 1.8 glass
3 1.1 cgd /*-
4 1.3 mycroft * Copyright (c) 1990, 1993
5 1.3 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Van Jacobson.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd #ifndef lint
40 1.3 mycroft static char copyright[] =
41 1.3 mycroft "@(#) Copyright (c) 1990, 1993\n\
42 1.3 mycroft The Regents of the University of California. All rights reserved.\n";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #ifndef lint
46 1.8 glass #if 0
47 1.8 glass static char sccsid[] = "@(#)traceroute.c 8.1 (Berkeley) 6/6/93";*/
48 1.8 glass #else
49 1.8 glass static char rcsid[] = "$NetBSD: traceroute.c,v 1.8 1995/03/26 21:45:23 glass Exp $";
50 1.8 glass #endif
51 1.1 cgd #endif /* not lint */
52 1.1 cgd
53 1.1 cgd /*
54 1.1 cgd * traceroute host - trace the route ip packets follow going to "host".
55 1.1 cgd *
56 1.1 cgd * Attempt to trace the route an ip packet would follow to some
57 1.1 cgd * internet host. We find out intermediate hops by launching probe
58 1.1 cgd * packets with a small ttl (time to live) then listening for an
59 1.1 cgd * icmp "time exceeded" reply from a gateway. We start our probes
60 1.1 cgd * with a ttl of one and increase by one until we get an icmp "port
61 1.1 cgd * unreachable" (which means we got to "host") or hit a max (which
62 1.1 cgd * defaults to 30 hops & can be changed with the -m flag). Three
63 1.1 cgd * probes (change with -q flag) are sent at each ttl setting and a
64 1.1 cgd * line is printed showing the ttl, address of the gateway and
65 1.1 cgd * round trip time of each probe. If the probe answers come from
66 1.1 cgd * different gateways, the address of each responding system will
67 1.1 cgd * be printed. If there is no response within a 5 sec. timeout
68 1.1 cgd * interval (changed with the -w flag), a "*" is printed for that
69 1.1 cgd * probe.
70 1.1 cgd *
71 1.1 cgd * Probe packets are UDP format. We don't want the destination
72 1.1 cgd * host to process them so the destination port is set to an
73 1.1 cgd * unlikely value (if some clod on the destination is using that
74 1.1 cgd * value, it can be changed with the -p flag).
75 1.1 cgd *
76 1.1 cgd * A sample use might be:
77 1.1 cgd *
78 1.1 cgd * [yak 71]% traceroute nis.nsf.net.
79 1.1 cgd * traceroute to nis.nsf.net (35.1.1.48), 30 hops max, 56 byte packet
80 1.1 cgd * 1 helios.ee.lbl.gov (128.3.112.1) 19 ms 19 ms 0 ms
81 1.1 cgd * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
82 1.1 cgd * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 39 ms 19 ms
83 1.1 cgd * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 39 ms
84 1.1 cgd * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 39 ms 39 ms 39 ms
85 1.1 cgd * 6 128.32.197.4 (128.32.197.4) 40 ms 59 ms 59 ms
86 1.1 cgd * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 59 ms
87 1.1 cgd * 8 129.140.70.13 (129.140.70.13) 99 ms 99 ms 80 ms
88 1.1 cgd * 9 129.140.71.6 (129.140.71.6) 139 ms 239 ms 319 ms
89 1.1 cgd * 10 129.140.81.7 (129.140.81.7) 220 ms 199 ms 199 ms
90 1.1 cgd * 11 nic.merit.edu (35.1.1.48) 239 ms 239 ms 239 ms
91 1.1 cgd *
92 1.1 cgd * Note that lines 2 & 3 are the same. This is due to a buggy
93 1.1 cgd * kernel on the 2nd hop system -- lbl-csam.arpa -- that forwards
94 1.1 cgd * packets with a zero ttl.
95 1.1 cgd *
96 1.1 cgd * A more interesting example is:
97 1.1 cgd *
98 1.1 cgd * [yak 72]% traceroute allspice.lcs.mit.edu.
99 1.1 cgd * traceroute to allspice.lcs.mit.edu (18.26.0.115), 30 hops max
100 1.1 cgd * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
101 1.1 cgd * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 19 ms 19 ms
102 1.1 cgd * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 19 ms
103 1.1 cgd * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 19 ms 39 ms 39 ms
104 1.1 cgd * 5 ccn-nerif22.Berkeley.EDU (128.32.168.22) 20 ms 39 ms 39 ms
105 1.1 cgd * 6 128.32.197.4 (128.32.197.4) 59 ms 119 ms 39 ms
106 1.1 cgd * 7 131.119.2.5 (131.119.2.5) 59 ms 59 ms 39 ms
107 1.1 cgd * 8 129.140.70.13 (129.140.70.13) 80 ms 79 ms 99 ms
108 1.1 cgd * 9 129.140.71.6 (129.140.71.6) 139 ms 139 ms 159 ms
109 1.1 cgd * 10 129.140.81.7 (129.140.81.7) 199 ms 180 ms 300 ms
110 1.1 cgd * 11 129.140.72.17 (129.140.72.17) 300 ms 239 ms 239 ms
111 1.1 cgd * 12 * * *
112 1.1 cgd * 13 128.121.54.72 (128.121.54.72) 259 ms 499 ms 279 ms
113 1.1 cgd * 14 * * *
114 1.1 cgd * 15 * * *
115 1.1 cgd * 16 * * *
116 1.1 cgd * 17 * * *
117 1.1 cgd * 18 ALLSPICE.LCS.MIT.EDU (18.26.0.115) 339 ms 279 ms 279 ms
118 1.1 cgd *
119 1.1 cgd * (I start to see why I'm having so much trouble with mail to
120 1.1 cgd * MIT.) Note that the gateways 12, 14, 15, 16 & 17 hops away
121 1.1 cgd * either don't send ICMP "time exceeded" messages or send them
122 1.1 cgd * with a ttl too small to reach us. 14 - 17 are running the
123 1.1 cgd * MIT C Gateway code that doesn't send "time exceeded"s. God
124 1.1 cgd * only knows what's going on with 12.
125 1.1 cgd *
126 1.1 cgd * The silent gateway 12 in the above may be the result of a bug in
127 1.1 cgd * the 4.[23]BSD network code (and its derivatives): 4.x (x <= 3)
128 1.1 cgd * sends an unreachable message using whatever ttl remains in the
129 1.1 cgd * original datagram. Since, for gateways, the remaining ttl is
130 1.1 cgd * zero, the icmp "time exceeded" is guaranteed to not make it back
131 1.1 cgd * to us. The behavior of this bug is slightly more interesting
132 1.1 cgd * when it appears on the destination system:
133 1.1 cgd *
134 1.1 cgd * 1 helios.ee.lbl.gov (128.3.112.1) 0 ms 0 ms 0 ms
135 1.1 cgd * 2 lilac-dmc.Berkeley.EDU (128.32.216.1) 39 ms 19 ms 39 ms
136 1.1 cgd * 3 lilac-dmc.Berkeley.EDU (128.32.216.1) 19 ms 39 ms 19 ms
137 1.1 cgd * 4 ccngw-ner-cc.Berkeley.EDU (128.32.136.23) 39 ms 40 ms 19 ms
138 1.1 cgd * 5 ccn-nerif35.Berkeley.EDU (128.32.168.35) 39 ms 39 ms 39 ms
139 1.1 cgd * 6 csgw.Berkeley.EDU (128.32.133.254) 39 ms 59 ms 39 ms
140 1.1 cgd * 7 * * *
141 1.1 cgd * 8 * * *
142 1.1 cgd * 9 * * *
143 1.1 cgd * 10 * * *
144 1.1 cgd * 11 * * *
145 1.1 cgd * 12 * * *
146 1.1 cgd * 13 rip.Berkeley.EDU (128.32.131.22) 59 ms ! 39 ms ! 39 ms !
147 1.1 cgd *
148 1.1 cgd * Notice that there are 12 "gateways" (13 is the final
149 1.1 cgd * destination) and exactly the last half of them are "missing".
150 1.1 cgd * What's really happening is that rip (a Sun-3 running Sun OS3.5)
151 1.1 cgd * is using the ttl from our arriving datagram as the ttl in its
152 1.1 cgd * icmp reply. So, the reply will time out on the return path
153 1.1 cgd * (with no notice sent to anyone since icmp's aren't sent for
154 1.1 cgd * icmp's) until we probe with a ttl that's at least twice the path
155 1.1 cgd * length. I.e., rip is really only 7 hops away. A reply that
156 1.1 cgd * returns with a ttl of 1 is a clue this problem exists.
157 1.1 cgd * Traceroute prints a "!" after the time if the ttl is <= 1.
158 1.1 cgd * Since vendors ship a lot of obsolete (DEC's Ultrix, Sun 3.x) or
159 1.1 cgd * non-standard (HPUX) software, expect to see this problem
160 1.1 cgd * frequently and/or take care picking the target host of your
161 1.1 cgd * probes.
162 1.1 cgd *
163 1.1 cgd * Other possible annotations after the time are !H, !N, !P (got a host,
164 1.1 cgd * network or protocol unreachable, respectively), !S or !F (source
165 1.1 cgd * route failed or fragmentation needed -- neither of these should
166 1.1 cgd * ever occur and the associated gateway is busted if you see one). If
167 1.1 cgd * almost all the probes result in some kind of unreachable, traceroute
168 1.1 cgd * will give up and exit.
169 1.1 cgd *
170 1.1 cgd * Notes
171 1.1 cgd * -----
172 1.1 cgd * This program must be run by root or be setuid. (I suggest that
173 1.1 cgd * you *don't* make it setuid -- casual use could result in a lot
174 1.1 cgd * of unnecessary traffic on our poor, congested nets.)
175 1.1 cgd *
176 1.1 cgd * This program requires a kernel mod that does not appear in any
177 1.1 cgd * system available from Berkeley: A raw ip socket using proto
178 1.1 cgd * IPPROTO_RAW must interpret the data sent as an ip datagram (as
179 1.1 cgd * opposed to data to be wrapped in a ip datagram). See the README
180 1.1 cgd * file that came with the source to this program for a description
181 1.1 cgd * of the mods I made to /sys/netinet/raw_ip.c. Your mileage may
182 1.1 cgd * vary. But, again, ANY 4.x (x < 4) BSD KERNEL WILL HAVE TO BE
183 1.1 cgd * MODIFIED TO RUN THIS PROGRAM.
184 1.1 cgd *
185 1.1 cgd * The udp port usage may appear bizarre (well, ok, it is bizarre).
186 1.1 cgd * The problem is that an icmp message only contains 8 bytes of
187 1.1 cgd * data from the original datagram. 8 bytes is the size of a udp
188 1.1 cgd * header so, if we want to associate replies with the original
189 1.1 cgd * datagram, the necessary information must be encoded into the
190 1.1 cgd * udp header (the ip id could be used but there's no way to
191 1.1 cgd * interlock with the kernel's assignment of ip id's and, anyway,
192 1.1 cgd * it would have taken a lot more kernel hacking to allow this
193 1.1 cgd * code to set the ip id). So, to allow two or more users to
194 1.1 cgd * use traceroute simultaneously, we use this task's pid as the
195 1.1 cgd * source port (the high bit is set to move the port number out
196 1.1 cgd * of the "likely" range). To keep track of which probe is being
197 1.1 cgd * replied to (so times and/or hop counts don't get confused by a
198 1.1 cgd * reply that was delayed in transit), we increment the destination
199 1.1 cgd * port number before each probe.
200 1.1 cgd *
201 1.1 cgd * Don't use this as a coding example. I was trying to find a
202 1.1 cgd * routing problem and this code sort-of popped out after 48 hours
203 1.1 cgd * without sleep. I was amazed it ever compiled, much less ran.
204 1.1 cgd *
205 1.1 cgd * I stole the idea for this program from Steve Deering. Since
206 1.1 cgd * the first release, I've learned that had I attended the right
207 1.1 cgd * IETF working group meetings, I also could have stolen it from Guy
208 1.1 cgd * Almes or Matt Mathis. I don't know (or care) who came up with
209 1.1 cgd * the idea first. I envy the originators' perspicacity and I'm
210 1.1 cgd * glad they didn't keep the idea a secret.
211 1.1 cgd *
212 1.1 cgd * Tim Seaver, Ken Adelman and C. Philip Wood provided bug fixes and/or
213 1.1 cgd * enhancements to the original distribution.
214 1.1 cgd *
215 1.1 cgd * I've hacked up a round-trip-route version of this that works by
216 1.1 cgd * sending a loose-source-routed udp datagram through the destination
217 1.1 cgd * back to yourself. Unfortunately, SO many gateways botch source
218 1.1 cgd * routing, the thing is almost worthless. Maybe one day...
219 1.1 cgd *
220 1.1 cgd * -- Van Jacobson (van (at) helios.ee.lbl.gov)
221 1.1 cgd * Tue Dec 20 03:50:13 PST 1988
222 1.1 cgd */
223 1.1 cgd
224 1.1 cgd #include <sys/param.h>
225 1.1 cgd #include <sys/time.h>
226 1.1 cgd #include <sys/socket.h>
227 1.1 cgd #include <sys/file.h>
228 1.1 cgd #include <sys/ioctl.h>
229 1.1 cgd
230 1.1 cgd #include <netinet/in_systm.h>
231 1.1 cgd #include <netinet/in.h>
232 1.1 cgd #include <netinet/ip.h>
233 1.1 cgd #include <netinet/ip_icmp.h>
234 1.4 mycroft #include <netinet/ip_var.h>
235 1.1 cgd #include <netinet/udp.h>
236 1.3 mycroft
237 1.3 mycroft #include <arpa/inet.h>
238 1.3 mycroft
239 1.4 mycroft #include <ctype.h>
240 1.5 mycroft #include <err.h>
241 1.4 mycroft #include <errno.h>
242 1.1 cgd #include <netdb.h>
243 1.1 cgd #include <stdio.h>
244 1.3 mycroft #include <stdlib.h>
245 1.1 cgd #include <string.h>
246 1.3 mycroft #include <unistd.h>
247 1.1 cgd
248 1.1 cgd #define Fprintf (void)fprintf
249 1.1 cgd #define Sprintf (void)sprintf
250 1.1 cgd #define Printf (void)printf
251 1.1 cgd
252 1.4 mycroft #define HEADERSIZE (sizeof(struct ip) + lsrrlen + sizeof(struct udphdr) + sizeof(struct packetdata))
253 1.4 mycroft #define MAX_LSRR ((MAX_IPOPTLEN - 4) / 4)
254 1.4 mycroft
255 1.1 cgd /*
256 1.4 mycroft * Format of the data in a (udp) probe packet.
257 1.1 cgd */
258 1.4 mycroft struct packetdata {
259 1.1 cgd u_char seq; /* sequence number of this packet */
260 1.1 cgd u_char ttl; /* ttl packet left with */
261 1.1 cgd struct timeval tv; /* time packet left */
262 1.1 cgd };
263 1.1 cgd
264 1.4 mycroft struct in_addr gateway[MAX_LSRR + 1];
265 1.4 mycroft int lsrrlen = 0;
266 1.4 mycroft
267 1.4 mycroft u_char packet[512], *outpacket; /* last inbound (icmp) packet */
268 1.3 mycroft
269 1.3 mycroft int wait_for_reply __P((int, struct sockaddr_in *));
270 1.4 mycroft void send_probe __P((int, int, struct sockaddr_in *));
271 1.3 mycroft double deltaT __P((struct timeval *, struct timeval *));
272 1.3 mycroft int packet_ok __P((u_char *, int, struct sockaddr_in *, int));
273 1.3 mycroft void print __P((u_char *, int, struct sockaddr_in *));
274 1.3 mycroft char *inetname __P((struct in_addr));
275 1.4 mycroft void usage __P((void));
276 1.1 cgd
277 1.1 cgd int s; /* receive (icmp) socket file descriptor */
278 1.1 cgd int sndsock; /* send (udp) socket file descriptor */
279 1.1 cgd struct timezone tz; /* leftover */
280 1.1 cgd
281 1.1 cgd int datalen; /* How much data */
282 1.1 cgd
283 1.1 cgd char *source = 0;
284 1.1 cgd char *hostname;
285 1.1 cgd
286 1.1 cgd int nprobes = 3;
287 1.1 cgd int max_ttl = 30;
288 1.1 cgd u_short ident;
289 1.1 cgd u_short port = 32768+666; /* start udp dest port # for probe packets */
290 1.1 cgd int options; /* socket options */
291 1.1 cgd int verbose;
292 1.1 cgd int waittime = 5; /* time to wait for response (in seconds) */
293 1.1 cgd int nflag; /* print addresses numerically */
294 1.4 mycroft int dump;
295 1.1 cgd
296 1.3 mycroft int
297 1.1 cgd main(argc, argv)
298 1.3 mycroft int argc;
299 1.1 cgd char *argv[];
300 1.1 cgd {
301 1.1 cgd struct hostent *hp;
302 1.1 cgd struct protoent *pe;
303 1.4 mycroft struct sockaddr_in from, to;
304 1.4 mycroft int ch, i, lsrr, on, probe, seq, tos, ttl;
305 1.4 mycroft u_long gw;
306 1.4 mycroft struct ip *ip;
307 1.1 cgd
308 1.4 mycroft lsrr = 0;
309 1.1 cgd on = 1;
310 1.1 cgd seq = tos = 0;
311 1.4 mycroft while ((ch = getopt(argc, argv, "dDg:m:np:q:rs:t:w:v")) != -1)
312 1.4 mycroft switch (ch) {
313 1.1 cgd case 'd':
314 1.1 cgd options |= SO_DEBUG;
315 1.1 cgd break;
316 1.4 mycroft case 'D':
317 1.4 mycroft dump = 1;
318 1.4 mycroft break;
319 1.4 mycroft case 'g':
320 1.5 mycroft if (lsrr >= MAX_LSRR)
321 1.5 mycroft errx(1, "too many gateways; max %d", MAX_LSRR);
322 1.4 mycroft gw = inet_addr(optarg);
323 1.4 mycroft if (gw == -1) {
324 1.4 mycroft hp = gethostbyname(optarg);
325 1.5 mycroft if (hp == 0)
326 1.5 mycroft errx(1, "unknown host %s", optarg);
327 1.8 glass memcpy(&gw, hp->h_addr, 4);
328 1.4 mycroft }
329 1.4 mycroft gateway[lsrr++].s_addr = gw;
330 1.4 mycroft if (lsrr == 1)
331 1.4 mycroft lsrrlen = 4;
332 1.4 mycroft lsrrlen += 4;
333 1.4 mycroft break;
334 1.1 cgd case 'm':
335 1.1 cgd max_ttl = atoi(optarg);
336 1.5 mycroft if (max_ttl < 1 || max_ttl > MAXTTL)
337 1.5 mycroft errx(1, "max ttl must be 1 to %d.", MAXTTL);
338 1.1 cgd break;
339 1.1 cgd case 'n':
340 1.1 cgd nflag++;
341 1.1 cgd break;
342 1.1 cgd case 'p':
343 1.1 cgd port = atoi(optarg);
344 1.5 mycroft if (port < 1)
345 1.5 mycroft errx(1, "port must be >0.");
346 1.1 cgd break;
347 1.1 cgd case 'q':
348 1.1 cgd nprobes = atoi(optarg);
349 1.5 mycroft if (nprobes < 1)
350 1.5 mycroft errx(1, "nprobes must be >0.");
351 1.1 cgd break;
352 1.1 cgd case 'r':
353 1.1 cgd options |= SO_DONTROUTE;
354 1.1 cgd break;
355 1.1 cgd case 's':
356 1.1 cgd /*
357 1.1 cgd * set the ip source address of the outbound
358 1.1 cgd * probe (e.g., on a multi-homed host).
359 1.1 cgd */
360 1.1 cgd source = optarg;
361 1.1 cgd break;
362 1.1 cgd case 't':
363 1.1 cgd tos = atoi(optarg);
364 1.5 mycroft if (tos < 0 || tos > 255)
365 1.5 mycroft errx(1, "tos must be 0 to 255.");
366 1.1 cgd break;
367 1.1 cgd case 'v':
368 1.1 cgd verbose++;
369 1.1 cgd break;
370 1.1 cgd case 'w':
371 1.1 cgd waittime = atoi(optarg);
372 1.5 mycroft if (waittime <= 1)
373 1.5 mycroft errx(1, "wait must be >1 sec.");
374 1.1 cgd break;
375 1.1 cgd default:
376 1.1 cgd usage();
377 1.1 cgd }
378 1.1 cgd argc -= optind;
379 1.1 cgd argv += optind;
380 1.1 cgd
381 1.3 mycroft if (argc < 1)
382 1.1 cgd usage();
383 1.1 cgd
384 1.1 cgd setlinebuf (stdout);
385 1.1 cgd
386 1.8 glass (void) memset((char *)&to, 0, sizeof(struct sockaddr));
387 1.4 mycroft to.sin_family = AF_INET;
388 1.4 mycroft to.sin_addr.s_addr = inet_addr(*argv);
389 1.4 mycroft if (to.sin_addr.s_addr != -1)
390 1.1 cgd hostname = *argv;
391 1.1 cgd else {
392 1.1 cgd hp = gethostbyname(*argv);
393 1.5 mycroft if (hp == 0)
394 1.5 mycroft errx(1, "unknown host %s", *argv);
395 1.4 mycroft to.sin_family = hp->h_addrtype;
396 1.8 glass memcpy((caddr_t)&to.sin_addr, hp->h_addr, hp->h_length);
397 1.4 mycroft hostname = hp->h_name;
398 1.1 cgd }
399 1.3 mycroft if (*++argv)
400 1.1 cgd datalen = atoi(*argv);
401 1.5 mycroft if (datalen < 0 || datalen > IP_MAXPACKET - HEADERSIZE)
402 1.5 mycroft errx(1, "packet size must be 0 to %d.",
403 1.4 mycroft IP_MAXPACKET - HEADERSIZE);
404 1.4 mycroft datalen += HEADERSIZE;
405 1.6 mycroft
406 1.4 mycroft outpacket = (u_char *)malloc(datalen);
407 1.6 mycroft if (outpacket == 0)
408 1.6 mycroft err(1, "malloc");
409 1.8 glass (void) memset(outpacket, 0, datalen);
410 1.4 mycroft
411 1.4 mycroft ip = (struct ip *)outpacket;
412 1.4 mycroft if (lsrr != 0) {
413 1.4 mycroft u_char *p;
414 1.4 mycroft p = (u_char *)(ip + 1);
415 1.4 mycroft *p++ = IPOPT_NOP;
416 1.4 mycroft *p++ = IPOPT_LSRR;
417 1.4 mycroft *p++ = lsrrlen - 1;
418 1.4 mycroft *p++ = IPOPT_MINOFF;
419 1.4 mycroft gateway[lsrr] = to.sin_addr;
420 1.4 mycroft for (i = 1; i <= lsrr; i++)
421 1.4 mycroft *((struct in_addr *)p)++ = gateway[i];
422 1.4 mycroft ip->ip_dst = gateway[0];
423 1.4 mycroft } else
424 1.4 mycroft ip->ip_dst = to.sin_addr;
425 1.4 mycroft ip->ip_off = 0;
426 1.4 mycroft ip->ip_hl = (sizeof(struct ip) + lsrrlen) >> 2;
427 1.4 mycroft ip->ip_p = IPPROTO_UDP;
428 1.4 mycroft ip->ip_v = IPVERSION;
429 1.4 mycroft ip->ip_tos = tos;
430 1.1 cgd
431 1.1 cgd ident = (getpid() & 0xffff) | 0x8000;
432 1.1 cgd
433 1.1 cgd if ((pe = getprotobyname("icmp")) == NULL) {
434 1.1 cgd Fprintf(stderr, "icmp: unknown protocol\n");
435 1.1 cgd exit(10);
436 1.1 cgd }
437 1.6 mycroft if ((s = socket(AF_INET, SOCK_RAW, pe->p_proto)) < 0)
438 1.6 mycroft err(5, "icmp socket");
439 1.1 cgd if (options & SO_DEBUG)
440 1.1 cgd (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
441 1.1 cgd (char *)&on, sizeof(on));
442 1.1 cgd if (options & SO_DONTROUTE)
443 1.1 cgd (void) setsockopt(s, SOL_SOCKET, SO_DONTROUTE,
444 1.1 cgd (char *)&on, sizeof(on));
445 1.1 cgd
446 1.6 mycroft if ((sndsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
447 1.6 mycroft err(5, "raw socket");
448 1.4 mycroft
449 1.1 cgd #ifdef SO_SNDBUF
450 1.1 cgd if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen,
451 1.6 mycroft sizeof(datalen)) < 0)
452 1.6 mycroft err(6, "SO_SNDBUF");
453 1.1 cgd #endif SO_SNDBUF
454 1.1 cgd #ifdef IP_HDRINCL
455 1.1 cgd if (setsockopt(sndsock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
456 1.6 mycroft sizeof(on)) < 0)
457 1.6 mycroft err(6, "IP_HDRINCL");
458 1.1 cgd #endif IP_HDRINCL
459 1.1 cgd if (options & SO_DEBUG)
460 1.1 cgd (void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
461 1.1 cgd (char *)&on, sizeof(on));
462 1.1 cgd if (options & SO_DONTROUTE)
463 1.1 cgd (void) setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE,
464 1.1 cgd (char *)&on, sizeof(on));
465 1.1 cgd
466 1.1 cgd if (source) {
467 1.8 glass (void) memset((char *)&from, 0, sizeof(struct sockaddr));
468 1.1 cgd from.sin_family = AF_INET;
469 1.1 cgd from.sin_addr.s_addr = inet_addr(source);
470 1.6 mycroft if (from.sin_addr.s_addr == -1)
471 1.6 mycroft errx(1, "unknown host %s", source);
472 1.4 mycroft ip->ip_src = from.sin_addr;
473 1.1 cgd #ifndef IP_HDRINCL
474 1.6 mycroft if (bind(sndsock, (struct sockaddr *)&from, sizeof(from)) < 0)
475 1.6 mycroft err(1, "bind");
476 1.1 cgd #endif IP_HDRINCL
477 1.1 cgd }
478 1.1 cgd
479 1.1 cgd Fprintf(stderr, "traceroute to %s (%s)", hostname,
480 1.4 mycroft inet_ntoa(to.sin_addr));
481 1.1 cgd if (source)
482 1.1 cgd Fprintf(stderr, " from %s", source);
483 1.1 cgd Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl, datalen);
484 1.1 cgd (void) fflush(stderr);
485 1.1 cgd
486 1.1 cgd for (ttl = 1; ttl <= max_ttl; ++ttl) {
487 1.1 cgd u_long lastaddr = 0;
488 1.1 cgd int got_there = 0;
489 1.1 cgd int unreachable = 0;
490 1.1 cgd
491 1.1 cgd Printf("%2d ", ttl);
492 1.1 cgd for (probe = 0; probe < nprobes; ++probe) {
493 1.1 cgd int cc;
494 1.3 mycroft struct timeval t1, t2;
495 1.3 mycroft struct timezone tz;
496 1.1 cgd
497 1.3 mycroft (void) gettimeofday(&t1, &tz);
498 1.4 mycroft send_probe(++seq, ttl, &to);
499 1.1 cgd while (cc = wait_for_reply(s, &from)) {
500 1.3 mycroft (void) gettimeofday(&t2, &tz);
501 1.1 cgd if ((i = packet_ok(packet, cc, &from, seq))) {
502 1.1 cgd if (from.sin_addr.s_addr != lastaddr) {
503 1.1 cgd print(packet, cc, &from);
504 1.1 cgd lastaddr = from.sin_addr.s_addr;
505 1.1 cgd }
506 1.3 mycroft Printf(" %g ms", deltaT(&t1, &t2));
507 1.1 cgd switch(i - 1) {
508 1.1 cgd case ICMP_UNREACH_PORT:
509 1.1 cgd #ifndef ARCHAIC
510 1.1 cgd ip = (struct ip *)packet;
511 1.1 cgd if (ip->ip_ttl <= 1)
512 1.1 cgd Printf(" !");
513 1.1 cgd #endif ARCHAIC
514 1.1 cgd ++got_there;
515 1.1 cgd break;
516 1.1 cgd case ICMP_UNREACH_NET:
517 1.1 cgd ++unreachable;
518 1.1 cgd Printf(" !N");
519 1.1 cgd break;
520 1.1 cgd case ICMP_UNREACH_HOST:
521 1.1 cgd ++unreachable;
522 1.1 cgd Printf(" !H");
523 1.1 cgd break;
524 1.1 cgd case ICMP_UNREACH_PROTOCOL:
525 1.1 cgd ++got_there;
526 1.1 cgd Printf(" !P");
527 1.1 cgd break;
528 1.1 cgd case ICMP_UNREACH_NEEDFRAG:
529 1.1 cgd ++unreachable;
530 1.1 cgd Printf(" !F");
531 1.1 cgd break;
532 1.1 cgd case ICMP_UNREACH_SRCFAIL:
533 1.1 cgd ++unreachable;
534 1.1 cgd Printf(" !S");
535 1.1 cgd break;
536 1.1 cgd }
537 1.1 cgd break;
538 1.1 cgd }
539 1.1 cgd }
540 1.1 cgd if (cc == 0)
541 1.1 cgd Printf(" *");
542 1.1 cgd (void) fflush(stdout);
543 1.1 cgd }
544 1.1 cgd putchar('\n');
545 1.4 mycroft if (got_there || unreachable >= nprobes)
546 1.1 cgd exit(0);
547 1.1 cgd }
548 1.1 cgd }
549 1.1 cgd
550 1.3 mycroft int
551 1.1 cgd wait_for_reply(sock, from)
552 1.1 cgd int sock;
553 1.1 cgd struct sockaddr_in *from;
554 1.1 cgd {
555 1.1 cgd fd_set fds;
556 1.1 cgd struct timeval wait;
557 1.1 cgd int cc = 0;
558 1.1 cgd int fromlen = sizeof (*from);
559 1.1 cgd
560 1.1 cgd FD_ZERO(&fds);
561 1.1 cgd FD_SET(sock, &fds);
562 1.1 cgd wait.tv_sec = waittime; wait.tv_usec = 0;
563 1.1 cgd
564 1.1 cgd if (select(sock+1, &fds, (fd_set *)0, (fd_set *)0, &wait) > 0)
565 1.1 cgd cc=recvfrom(s, (char *)packet, sizeof(packet), 0,
566 1.1 cgd (struct sockaddr *)from, &fromlen);
567 1.1 cgd
568 1.1 cgd return(cc);
569 1.1 cgd }
570 1.1 cgd
571 1.4 mycroft void
572 1.4 mycroft dump_packet()
573 1.4 mycroft {
574 1.4 mycroft u_char *p;
575 1.4 mycroft int i;
576 1.4 mycroft
577 1.4 mycroft Fprintf(stderr, "packet data:");
578 1.4 mycroft for (p = outpacket, i = 0; i < datalen; i++) {
579 1.4 mycroft if ((i % 24) == 0)
580 1.4 mycroft Fprintf(stderr, "\n ");
581 1.4 mycroft Fprintf(stderr, " %02x", *p++);
582 1.4 mycroft }
583 1.4 mycroft Fprintf(stderr, "\n");
584 1.4 mycroft }
585 1.1 cgd
586 1.3 mycroft void
587 1.4 mycroft send_probe(seq, ttl, to)
588 1.3 mycroft int seq, ttl;
589 1.4 mycroft struct sockaddr_in *to;
590 1.1 cgd {
591 1.4 mycroft struct ip *ip = (struct ip *)outpacket;
592 1.4 mycroft u_char *p = (u_char *)(ip + 1);
593 1.4 mycroft struct udphdr *up = (struct udphdr *)(p + lsrrlen);
594 1.4 mycroft struct packetdata *op = (struct packetdata *)(up + 1);
595 1.1 cgd int i;
596 1.1 cgd
597 1.1 cgd ip->ip_len = datalen;
598 1.1 cgd ip->ip_ttl = ttl;
599 1.3 mycroft ip->ip_id = htons(ident+seq);
600 1.1 cgd
601 1.1 cgd up->uh_sport = htons(ident);
602 1.1 cgd up->uh_dport = htons(port+seq);
603 1.4 mycroft up->uh_ulen = htons((u_short)(datalen - sizeof(struct ip) - lsrrlen));
604 1.1 cgd up->uh_sum = 0;
605 1.1 cgd
606 1.1 cgd op->seq = seq;
607 1.1 cgd op->ttl = ttl;
608 1.1 cgd (void) gettimeofday(&op->tv, &tz);
609 1.1 cgd
610 1.4 mycroft if (dump)
611 1.4 mycroft dump_packet();
612 1.4 mycroft
613 1.4 mycroft i = sendto(sndsock, outpacket, datalen, 0, (struct sockaddr *)to,
614 1.4 mycroft sizeof(struct sockaddr_in));
615 1.1 cgd if (i < 0 || i != datalen) {
616 1.1 cgd if (i<0)
617 1.1 cgd perror("sendto");
618 1.1 cgd Printf("traceroute: wrote %s %d chars, ret=%d\n", hostname,
619 1.1 cgd datalen, i);
620 1.1 cgd (void) fflush(stdout);
621 1.1 cgd }
622 1.1 cgd }
623 1.1 cgd
624 1.1 cgd
625 1.3 mycroft double
626 1.3 mycroft deltaT(t1p, t2p)
627 1.3 mycroft struct timeval *t1p, *t2p;
628 1.3 mycroft {
629 1.3 mycroft register double dt;
630 1.3 mycroft
631 1.3 mycroft dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
632 1.3 mycroft (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
633 1.3 mycroft return (dt);
634 1.1 cgd }
635 1.1 cgd
636 1.1 cgd
637 1.1 cgd /*
638 1.1 cgd * Convert an ICMP "type" field to a printable string.
639 1.1 cgd */
640 1.1 cgd char *
641 1.1 cgd pr_type(t)
642 1.1 cgd u_char t;
643 1.1 cgd {
644 1.1 cgd static char *ttab[] = {
645 1.1 cgd "Echo Reply", "ICMP 1", "ICMP 2", "Dest Unreachable",
646 1.1 cgd "Source Quench", "Redirect", "ICMP 6", "ICMP 7",
647 1.1 cgd "Echo", "ICMP 9", "ICMP 10", "Time Exceeded",
648 1.1 cgd "Param Problem", "Timestamp", "Timestamp Reply", "Info Request",
649 1.1 cgd "Info Reply"
650 1.1 cgd };
651 1.1 cgd
652 1.1 cgd if(t > 16)
653 1.1 cgd return("OUT-OF-RANGE");
654 1.1 cgd
655 1.1 cgd return(ttab[t]);
656 1.1 cgd }
657 1.1 cgd
658 1.1 cgd
659 1.3 mycroft int
660 1.1 cgd packet_ok(buf, cc, from, seq)
661 1.1 cgd u_char *buf;
662 1.1 cgd int cc;
663 1.1 cgd struct sockaddr_in *from;
664 1.1 cgd int seq;
665 1.1 cgd {
666 1.1 cgd register struct icmp *icp;
667 1.1 cgd u_char type, code;
668 1.1 cgd int hlen;
669 1.1 cgd #ifndef ARCHAIC
670 1.1 cgd struct ip *ip;
671 1.1 cgd
672 1.1 cgd ip = (struct ip *) buf;
673 1.1 cgd hlen = ip->ip_hl << 2;
674 1.1 cgd if (cc < hlen + ICMP_MINLEN) {
675 1.1 cgd if (verbose)
676 1.1 cgd Printf("packet too short (%d bytes) from %s\n", cc,
677 1.1 cgd inet_ntoa(from->sin_addr));
678 1.1 cgd return (0);
679 1.1 cgd }
680 1.1 cgd cc -= hlen;
681 1.1 cgd icp = (struct icmp *)(buf + hlen);
682 1.1 cgd #else
683 1.1 cgd icp = (struct icmp *)buf;
684 1.1 cgd #endif ARCHAIC
685 1.1 cgd type = icp->icmp_type; code = icp->icmp_code;
686 1.1 cgd if ((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS) ||
687 1.1 cgd type == ICMP_UNREACH) {
688 1.1 cgd struct ip *hip;
689 1.1 cgd struct udphdr *up;
690 1.1 cgd
691 1.1 cgd hip = &icp->icmp_ip;
692 1.1 cgd hlen = hip->ip_hl << 2;
693 1.1 cgd up = (struct udphdr *)((u_char *)hip + hlen);
694 1.1 cgd if (hlen + 12 <= cc && hip->ip_p == IPPROTO_UDP &&
695 1.1 cgd up->uh_sport == htons(ident) &&
696 1.1 cgd up->uh_dport == htons(port+seq))
697 1.1 cgd return (type == ICMP_TIMXCEED? -1 : code+1);
698 1.1 cgd }
699 1.1 cgd #ifndef ARCHAIC
700 1.1 cgd if (verbose) {
701 1.1 cgd int i;
702 1.1 cgd u_long *lp = (u_long *)&icp->icmp_ip;
703 1.1 cgd
704 1.1 cgd Printf("\n%d bytes from %s to %s", cc,
705 1.1 cgd inet_ntoa(from->sin_addr), inet_ntoa(ip->ip_dst));
706 1.1 cgd Printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
707 1.1 cgd icp->icmp_code);
708 1.1 cgd for (i = 4; i < cc ; i += sizeof(long))
709 1.1 cgd Printf("%2d: x%8.8lx\n", i, *lp++);
710 1.1 cgd }
711 1.1 cgd #endif ARCHAIC
712 1.1 cgd return(0);
713 1.1 cgd }
714 1.1 cgd
715 1.1 cgd
716 1.3 mycroft void
717 1.1 cgd print(buf, cc, from)
718 1.1 cgd u_char *buf;
719 1.1 cgd int cc;
720 1.1 cgd struct sockaddr_in *from;
721 1.1 cgd {
722 1.1 cgd struct ip *ip;
723 1.1 cgd int hlen;
724 1.1 cgd
725 1.1 cgd ip = (struct ip *) buf;
726 1.1 cgd hlen = ip->ip_hl << 2;
727 1.1 cgd cc -= hlen;
728 1.1 cgd
729 1.1 cgd if (nflag)
730 1.1 cgd Printf(" %s", inet_ntoa(from->sin_addr));
731 1.1 cgd else
732 1.1 cgd Printf(" %s (%s)", inetname(from->sin_addr),
733 1.1 cgd inet_ntoa(from->sin_addr));
734 1.1 cgd
735 1.1 cgd if (verbose)
736 1.1 cgd Printf (" %d bytes to %s", cc, inet_ntoa (ip->ip_dst));
737 1.1 cgd }
738 1.1 cgd
739 1.1 cgd
740 1.1 cgd #ifdef notyet
741 1.1 cgd /*
742 1.1 cgd * Checksum routine for Internet Protocol family headers (C Version)
743 1.1 cgd */
744 1.3 mycroft u_short
745 1.1 cgd in_cksum(addr, len)
746 1.3 mycroft u_short *addr;
747 1.3 mycroft int len;
748 1.1 cgd {
749 1.1 cgd register int nleft = len;
750 1.1 cgd register u_short *w = addr;
751 1.1 cgd register u_short answer;
752 1.1 cgd register int sum = 0;
753 1.1 cgd
754 1.1 cgd /*
755 1.1 cgd * Our algorithm is simple, using a 32 bit accumulator (sum),
756 1.1 cgd * we add sequential 16 bit words to it, and at the end, fold
757 1.1 cgd * back all the carry bits from the top 16 bits into the lower
758 1.1 cgd * 16 bits.
759 1.1 cgd */
760 1.1 cgd while (nleft > 1) {
761 1.1 cgd sum += *w++;
762 1.1 cgd nleft -= 2;
763 1.1 cgd }
764 1.1 cgd
765 1.1 cgd /* mop up an odd byte, if necessary */
766 1.1 cgd if (nleft == 1)
767 1.1 cgd sum += *(u_char *)w;
768 1.1 cgd
769 1.1 cgd /*
770 1.1 cgd * add back carry outs from top 16 bits to low 16 bits
771 1.1 cgd */
772 1.1 cgd sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
773 1.1 cgd sum += (sum >> 16); /* add carry */
774 1.1 cgd answer = ~sum; /* truncate to 16 bits */
775 1.1 cgd return (answer);
776 1.1 cgd }
777 1.1 cgd #endif notyet
778 1.1 cgd
779 1.1 cgd /*
780 1.1 cgd * Construct an Internet address representation.
781 1.3 mycroft * If the nflag has been supplied, give
782 1.1 cgd * numeric value, otherwise try for symbolic name.
783 1.1 cgd */
784 1.1 cgd char *
785 1.1 cgd inetname(in)
786 1.1 cgd struct in_addr in;
787 1.1 cgd {
788 1.1 cgd register char *cp;
789 1.1 cgd static char line[50];
790 1.1 cgd struct hostent *hp;
791 1.1 cgd static char domain[MAXHOSTNAMELEN + 1];
792 1.1 cgd static int first = 1;
793 1.1 cgd
794 1.1 cgd if (first && !nflag) {
795 1.1 cgd first = 0;
796 1.1 cgd if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
797 1.1 cgd (cp = index(domain, '.')))
798 1.1 cgd (void) strcpy(domain, cp + 1);
799 1.1 cgd else
800 1.1 cgd domain[0] = 0;
801 1.1 cgd }
802 1.1 cgd cp = 0;
803 1.1 cgd if (!nflag && in.s_addr != INADDR_ANY) {
804 1.1 cgd hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
805 1.1 cgd if (hp) {
806 1.1 cgd if ((cp = index(hp->h_name, '.')) &&
807 1.1 cgd !strcmp(cp + 1, domain))
808 1.1 cgd *cp = 0;
809 1.1 cgd cp = hp->h_name;
810 1.1 cgd }
811 1.1 cgd }
812 1.1 cgd if (cp)
813 1.1 cgd (void) strcpy(line, cp);
814 1.1 cgd else {
815 1.1 cgd in.s_addr = ntohl(in.s_addr);
816 1.1 cgd #define C(x) ((x) & 0xff)
817 1.1 cgd Sprintf(line, "%lu.%lu.%lu.%lu", C(in.s_addr >> 24),
818 1.1 cgd C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
819 1.1 cgd }
820 1.1 cgd return (line);
821 1.1 cgd }
822 1.1 cgd
823 1.3 mycroft void
824 1.1 cgd usage()
825 1.1 cgd {
826 1.3 mycroft (void)fprintf(stderr,
827 1.4 mycroft "usage: traceroute [-dDnrv] [-g gateway_addr] ... [-m max_ttl] [-p port#]\n\t\
828 1.4 mycroft [-q nqueries] [-s src_addr] [-t tos] [-w wait] host [data size]\n");
829 1.5 mycroft exit(1);
830 1.5 mycroft }
831