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