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