traceroute-1.0.4/000075500000000000000000000000001051105034600132625ustar00gitgit00000000000000traceroute-1.0.4/COPYING.GPL000064400000000000000000000431221051105034600147400ustar00gitgit00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. traceroute-1.0.4/ChangeLog000064400000000000000000000027141051105034600150400ustar00gitgit00000000000000------------------------------------------------------------------- Mon Dec 19 15:43:30 CET 2005 - okir@suse.de - Fix the ICMP6_DST_UNREACH_NOTNEIGHBOR -> ICMP6_DST_UNREACH_BEYONDSCOPE properly ------------------------------------------------------------------- Tue Nov 29 08:47:31 CET 2005 - okir@suse.de - Two additional fixes by Robert Scheck ------------------------------------------------------------------- Wed Nov 2 10:06:05 CET 2005 - okir@suse.de - Added support for IDN, based on an initial patch by Robert Scheck ------------------------------------------------------------------- Mon Sep 5 10:49:19 CEST 2005 - okir@suse.de - Included some x86_64 fixes from Ivan Skytte Jørgensen ------------------------------------------------------------------- Wed Aug 24 08:40:41 CEST 2005 okir - Added GPL license statement. Thanks to Andreas Ericsson for pointing out the omission. ------------------------------------------------------------------- Tue Jul 8 17:40:53 CEST 2003 okir - Fixed traceroute -S ------------------------------------------------------------------- Fri Nov 15 12:27:30 CET 2002 okir - Tried to make reported RTT more accurate on fast links - compile fixes for glibc 2.1.3 ------------------------------------------------------------------- Fri Jul 26 10:31:50 CEST 2002 okir - misc compile changes and a -V flag from Mads Martin Joergensen . traceroute-1.0.4/Makefile000064400000000000000000000006071051105034600147250ustar00gitgit00000000000000CCOPTS = -g CFLAGS = -Wall -D_GNU_SOURCE $(CCOPTS) LDFLAGS = $(CCOPTS) OBJS = traceroute.o utils.o BINARY = traceroute BINARY6 = traceroute6 BINDIR = /usr/sbin all: traceroute clean distclean: rm -f $(BINARY) $(OBJS) install: traceroute install -s -m 555 $(BINARY) $(DESTDIR)$(BINDIR) ln -s $(BINARY) $(DESTDIR)$(BINDIR)/$(BINARY6) traceroute: $(OBJS) $(CC) $(LDFLAGS) -o $@ $(OBJS) traceroute-1.0.4/README000064400000000000000000000013251051105034600141430ustar00gitgit00000000000000 This is a small traceroute replacement that works without requiring a setuid bit. I wrote it quite some time ago, but never got around to releasing it. Which I'm doing now. This traceroute implementation relies on a number of features of the 2.4 Linux kernel, so chances to port it to other platforms are probably zero. It works pretty much like ANK's tracepath, but tries to be command line compatible with the original traceroute. It also has IPv6 support, and does parallel probes, which makes it a little faster. The latest version is available from ftp://ftp.lst.de/pub/people/okir/traceroute If you have suggestions, bug reports, enhancements, etc, please mail me at . Olaf Kirch, Apr 10 2003 traceroute-1.0.4/traceroute.1000064400000000000000000000116701051105034600155260ustar00gitgit00000000000000.TH TRACEROUTE 1 "6 December 2000" .UC 6 .SH NAME traceroute \- print the route packets take to network host .SH SYNOPSIS .na .BR traceroute " [" \-46FnrRV "] [" "\-f first_ttl" "] [" "-p port" ] .br .ti +8 .BR "" [ "-m max_hops" "] [" "-N concurrent_hops" ] .br .ti +8 .BR "" "[" "-t tos" "] [" "-w timeout" "] [" "-q nqueries" ] .br .ti +8 .BR "" "[" "-S source_addr" "] [" "-I interface" "] .br .ti +8 .BR "" [ "-g gateway" "] " host " [" "packetlen" "]" .br .BR traceroute6 .RI " [" options ] .ad .SH DESCRIPTION .I traceroute tracks the route packets take across a TCP/IP network on their way to a given host. It utilizes the IP protocol's time to live (TTL) field and attempts to elicit an ICMP TIME_EXCEEDED response from each gateway along the path to the host. .P .I traceroute6 is just another name for the same program, and is equivalent to invoking .I traceroute with the .B \-6 option. .SH OPTIONS The only required parameter is the name or IP address of the destination host. This paremeter can be followed by the size of the probing packet sent to that host (40 by default). Varying the size of the packet in conjunction with the .B \-F parameter can be used to obtain information about the MTU of individual network hops. .PP Additional options are: .TP .BR \-6 ", " \-4 Explicitly force IPv4 or IPv6 traceouting. By default, the program will try to resolve the name given, and choose the appropriate protocol automatically. If resolving a host name returns both IPv4 and IPv6 addresses, .I traceroute will use IPv4. Invoking the program as .I traceroute6 is the same as using the .B -6 option. .TP .B \-F Set the "Don't Fragment" bit. This tells intermediate routers not to fragment the packet when they find it's too big for a network hop's MTU. .TP .BI \-f " first_ttl Specifies with what TTL to start. Defaults to 1. .TP .BI \-g " gateway Tells traceroute to add an IP source routing option to the outgoing packet that tells the network to route the packet through the specified .IR gateway . Not very useful, because most routers have disabled source routing for security reasons. .TP .BI \-I " interface Specifies the interface through which to .I traceroute should send packets. By default, the interface is selected according to the routing table. .TP .BI \-m " max_hops Specifies the maximum number of hops .I traceroute will probe. The default value is 30. .TP .BI \-N " concurrent_hops Specifies the number of probe packets sent out simultaneously. Sending several probes concurrently can speed up .I traceroute considerably. However, when specifying a number that's too large, the destination host will start to throw away random ICMP responses (if it implements ICMP rate throttling), and .I traceroute will be unable to detect the final hope reliably. The default value is 6. .TP .BI \-n Do not try to map IP addresses to host names when displaying them. .TP .BI \-p " port Specifies the UDP destination port base .I traceroute will use. When sending its UDP probe packets, it will send them to .I port + hop - 1 for each hop. If there are ports in this range in use on the destination host, .I traceroute will not be able to identify reliably when it has reached the destination host (probes will appear to time out). The default port is 33434; you can use .B \-p to change this to a different value. .TP .BI \-t " tos Set the IP Type of Service (TOS) and Precedence value. Useful values are 16 (low delay) and 8 (high throughput). Note that in order to use some TOS precendence values, you have to be super user. .TP .BI \-w " sec Wait for .I sec seconds before sending the next probe packet. .IP Note that unlike older .I traceroute versions, this implementation will transmit several probe packets in parallel, for different hop values. However, it will never send more than 1 packet per hop value at the same time. .TP .BI \-q " numqueries Sets the number of probe packets per hop. The default value is 3. .TP .BI \-r Bypass the normal routing tables and send directly to a host on an attached network. If the host is not on a directly-attached network, an error is returned. This option can be used to ping a local host through an interface that has no route through it. .TP .BI \-R Set the loose source route option on outgoing packets, asking intermediate routers to record their address as the packet passes. This can be useful if you want to find the address of an intermediate router that has been configured to not respond to traceroute packets. .IP This feature hasn't been implemented yet. .TP .BI \-S " source_addr Chooses an alternative source address. Note that you must select the address of one of the interfaces. By default, the address of the outgoing interface is used. .TP .BI \-V Print the version and exit. .SH SEE ALSO .BR ping (8), .BR netstat (8). .SH AUTHOR Concept and command line options based on the original LBL implementation of traceroute, written by Van Jacobson. This implentation is a complete rewrite and redesign, written and copyright (C) 2000 Olaf Kirch . traceroute-1.0.4/traceroute.c000075500000000000000000000423621051105034600156150ustar00gitgit00000000000000/* * traceroute implementation that does not require root privilege. * * This requires a Linux 2.4 kernel. * * Copyright (C) 2000-2005, Olaf Kirch * * License: GPL 2.0 or later. See COPYING.GPL for details. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "utils.h" #include "version.h" /* The kernel wants these */ typedef u_int32_t __u32; typedef u_int8_t __u8; #include /* These are not available with glibc 2.1.3: */ #ifndef SO_TIMESTAMP #define SO_TIMESTAMP 29 #endif #ifndef IPV6_RECVERR #define IPV6_RECVERR 25 #endif /* older glibc compatibility * From rfc 3542: * - Changed the name of ICMPv6 unreachable code 2 to be "beyond scope * of source address." ICMP6_DST_UNREACH_NOTNEIGHBOR was removed * with this change. */ #if defined(ICMP6_DST_UNREACH_NOTNEIGHBOR) && !defined(ICMP6_DST_UNREACH_BEYONDSCOPE) # define ICMP6_DST_UNREACH_BEYONDSCOPE ICMP6_DST_UNREACH_NOTNEIGHBOR #endif #define MAXHOPS 255 #define MAXPROBES 6 #define MAXGATEWAYS 9 struct hop { unsigned int ttl; unsigned int sent; unsigned int recvd; unsigned int final; /* Previously printed address */ sockaddr_any prev_addr; struct probe { struct hop * hop; int fd; struct sock_extended_err ee; struct timeval sent_time; unsigned long timeout; struct timeval recvd_time; sockaddr_any responder; const char * err_ind; unsigned int done : 1, printed : 1; } probe[MAXPROBES]; }; static struct hop hops[MAXHOPS]; static unsigned int first_hop, last_hop; static unsigned int opt_timeout = 5, opt_retries = 3, concurrent_hops = 6, max_hops = 30; static unsigned int have_final_response; static unsigned long now; static int af = -1; //static size_t af_len = sizeof(struct sockaddr_in); static char * src_addr_name; static sockaddr_any src_addr; static const char * src_device; static u_int16_t src_port = 64000; static const char * dst_name; static sockaddr_any dst_addr; static u_int16_t dst_port = 33434; static u_int8_t dst_tos; static int opt_debug = 0; static int opt_noresolver = 0; static int opt_dontfrag = IP_PMTUDISC_DONT; static int opt_dontroute = 0; static int opt_rr = 0; static char * opt_gateway_name[MAXGATEWAYS]; static sockaddr_any opt_gateway[MAXGATEWAYS]; static unsigned int ngateways; static unsigned char packet[65536]; static unsigned int packetsize = 40; static unsigned char ipoptions[40]; static unsigned int ipoptions_len = 0; static void set_now(void); static void hop_next(void); static void hop_xmit(struct hop *); static int hop_sendmsg(int); static void hop_print(struct hop *); static void probe_recv(struct probe *); static void probe_timeout(struct probe *); static int probe_recverr(struct probe *); static int probe_init(struct hop *, struct probe *); static void ipopt_init(void); static void usage(int exval); static void show_version(void); int main(int argc, char **argv) { unsigned int n; int c; /* Set the locale - needed for international domain names */ setlocale (LC_ALL, ""); while ((c = getopt(argc, argv, "46DFVf:g:I:m:N:np:t:w:q:rRS:")) != -1) { switch (c) { case '4': af = AF_INET; break; case '6': af = AF_INET6; break; case 'D': opt_debug++; break; case 'F': opt_dontfrag = IP_PMTUDISC_DO; break; case 'f': first_hop = getnum("hop", optarg, 1, 255); break; case 'g': /* fatal("option -g not yet supported"); */ if (ngateways >= MAXGATEWAYS) fatal("too many gateways"); opt_gateway_name[ngateways++] = optarg; break; case 'I': src_device = optarg; break; case 'm': max_hops = getnum("hop", optarg, 1, 255); break; case 'N': concurrent_hops = getnum("concurrent hop", optarg, 1, 16); break; case 'n': opt_noresolver = 1; break; case 'p': dst_port = getnum("port", optarg, 1, 65535); break; case 't': dst_tos = getnum("TOS", optarg, 0, 255); break; case 'w': opt_timeout = getnum("timeout", optarg, 1, 256); break; case 'q': opt_retries = getnum("retry", optarg, 1, 1024); break; case 'r': opt_dontroute = 1; break; case 'R': opt_rr = 1; break; case 'S': src_addr_name = optarg; break; case 'V': show_version(); break; default: usage(1); } } if (optind == argc - 2) { packetsize = getnum("packet length", argv[optind+1], 1, 65536); } else if (optind != argc - 1) usage(1); /* Check the program name. If we've called as traceroute6, * default to IPv6 */ if (af == -1) { char *s; if ((s = strrchr(argv[0], '/')) != 0) s++; else s = argv[0]; if (!strcmp(s, "traceroute6")) af = AF_INET6; } dst_name = argv[optind]; if (!getaddr(dst_name, af, &dst_addr)) return 1; af = dst_addr.any.sa_family; /* Resolve gateways if given */ for (n = 0; n < ngateways; n++) { if (!getaddr(opt_gateway_name[n], af, opt_gateway + n)) return 1; } /* Resolve source address if given */ if (src_addr_name && !getaddr(src_addr_name, af, &src_addr)) { return 1; } if (first_hop >= max_hops) { fprintf(stderr, "first hop %u larger than max hops %u\n", first_hop, max_hops); return 1; } /* Initialize packet */ for (n = 0; n < packetsize; n++) packet[n] = 0x40 + (n & 0x3f); /* Initialize IP options */ ipopt_init(); /* Start by sending the first packet */ last_hop = first_hop; printf("traceroute to %s (%s), %u hops max, %u byte packets\n", dst_name, straddr(&dst_addr), max_hops, packetsize); while (1) { struct probe *probe[MAXHOPS*MAXPROBES], *pb; struct pollfd pfd[MAXHOPS*MAXPROBES]; unsigned long timeout; struct hop *hop; unsigned int m, n, wait_some = 0; while (last_hop < max_hops && last_hop < first_hop + concurrent_hops) hop_next(); set_now(); timeout = now + 5000; memset(pfd, 0, sizeof(pfd)); for (m = 0, hop = hops + first_hop; hop < hops + last_hop; hop++) { unsigned int num; for (num = hop->recvd; num < opt_retries; num++, m++) { pb = hop->probe + num; if (num >= hop->sent) { hop_xmit(hop); wait_some = 1; } pfd[m].events = POLLERR; pfd[m].fd = pb->fd; if (pb->timeout < timeout) timeout = pb->timeout; probe[m] = pb; /* If we've already transmitted some * packets, wait for 1ms to receive * something. This helps us give much more * accurate RTTs on fast links */ if (wait_some) { timeout = now + 1; goto wait_for_some; } } } if (m == 0) break; wait_for_some: if (opt_debug > 1) { fprintf(stderr, "poll %d fds, timeout %lu ms\n", m, timeout - now); } poll(pfd, m, (now < timeout)? (timeout - now) : 0); set_now(); /* time has passed - catch up */ /* Receive any pending ICMP errors */ for (n = 0; n < m; n++) { pb = probe[n]; if (pfd[n].revents & POLLERR) { probe_recv(pb); } else if (pb->timeout <= now) { probe_timeout(pb); } } /* FIXME: we really shouldn't start printing packets * and resoving addresses while we still have packets * in flight, especially on low RTT links. * We should either skip the output part while that's * the case, or fork and do the printing in a separate * process */ /* Now loop over all hop structures and see whether we can * add more probes, or wrap up the timed out ones */ while (first_hop < last_hop) { hop = hops + first_hop; hop_print(hop); if (hop->recvd < hop->sent) break; /* This one is complete */ printf("\n"); first_hop++; if (hop->final) goto done; } } done: return 0; } static void usage(int exval) { fprintf(stderr, "usage: traceroute [-nFV] [-f first_ttl] [-m max_hops] [-p port]\n" " [-S source_addr] [-I interface] [-g gateway]\n" " [-t tos] [-w timeout] [-q nqueries] host [packetlen]\n"); exit(1); } static void show_version(void) { printf("This is traceroute %s\n", TRACEROUTE_VERSION); printf("Copyright (c) 2002 Olaf Kirch\n"); exit(0); } /* Set our idea of the current time */ static void set_now(void) { struct timeval tv; gettimeofday(&tv, NULL); now = tv.tv_sec * 1000 + tv.tv_usec / 1000; } static void hop_next(void) { struct hop *hop = hops + last_hop++; hop->ttl = last_hop; } static void hop_xmit(struct hop *hop) { struct probe *pb; if (opt_debug) fprintf(stderr, "sending hop %ld packet %d ttl=%d\n", (long)(hop - hops + 1), hop->sent, hop->ttl); /* Init probe and send packet */ pb = hop->probe + hop->sent++; probe_init(hop, pb); if (hop_sendmsg(pb->fd) < 0) perror("send failed"); gettimeofday(&pb->sent_time, NULL); } static void hop_print(struct hop *hop) { struct probe *p = hop->probe; struct timeval delta; unsigned int nr; while (hop->recvd < hop->sent) { if (!hop->probe[hop->recvd].done) break; hop->recvd++; } for (nr = 0; nr < hop->sent; nr++, p++) { if (!p->done) return; if (p->printed) continue; if (nr == 0) printf("%2lu ", (hop - hops) + 1UL); if (p->responder.sin.sin_family == 0) { printf(" *"); } else { printf(" "); timersub(&p->recvd_time, &p->sent_time, &delta); if (delta.tv_sec < 0) timerclear(&delta); if (!sameaddr(&p->responder, &hop->prev_addr)) printaddr(&p->responder, !opt_noresolver); hop->prev_addr = p->responder; if (p->err_ind) printf("(%s)", p->err_ind); printf(" %lu.%03lu ms", 1000 * delta.tv_sec + delta.tv_usec / 1000, delta.tv_usec % 1000); } p->printed = 1; } fflush(stdout); } static int probe_init(struct hop *hop, struct probe *pb) { struct sockaddr_in sin; struct sockaddr_in6 six; struct sockaddr *ap; socklen_t alen; int fd, val, len; if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) fatal("unable to create UDP socket: %m"); len = sizeof(val); if (af == AF_INET) { val = 1; if (setsockopt(fd, SOL_IP, IP_RECVERR, &val, len) < 0) fatal("unable to set SO_RECVERR: %m"); val = hop->ttl; if (setsockopt(fd, SOL_IP, IP_TTL, &val, len) < 0) fatal("unable to set TTL: %m"); val = dst_tos; if (setsockopt(fd, SOL_IP, IP_TOS, &val, len) < 0) fatal("unable to set TOS: %m"); val = opt_dontfrag; if (setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &val, len) < 0) fatal("unable to set MTU_DISCOVER: %m"); } else { val = 1; if (setsockopt(fd, SOL_IPV6, IPV6_RECVERR, &val, len) < 0) fatal("unable to set SO_RECVERR: %m"); val = hop->ttl; if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &val, len) < 0) fatal("unable to set IPV6_UNICAST_HOPS: %m"); } val = opt_dontroute; if (setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &val, len) < 0) fatal("unable to set SO_DONTROUTE: %m"); val = 1; if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &val, len) < 0) fatal("unable to set SO_TIMESTAMP: %m"); if (src_device) { len = strlen(src_device)+1; if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src_device, len) < 0) fatal("Unable to bind to network interface %s: %m", src_device); } if (af == AF_INET) { ap = (struct sockaddr *) &sin; alen = sizeof(sin); } else { ap = (struct sockaddr *) &six; alen = sizeof(six); } if (src_addr.any.sa_family == af) { memcpy(ap, &src_addr, alen); } else { memset(ap, 0, alen); ap->sa_family = af; } while (1) { if (af == AF_INET) sin.sin_port = htons(src_port); else six.sin6_port = htons(src_port); src_port++; if (bind(fd, ap, alen) >= 0) break; if (errno != EADDRINUSE) fatal("unable to bind socket: %m"); } memcpy(ap, &dst_addr, alen); if (af == AF_INET) { sin.sin_port = htons(dst_port); } else { six.sin6_port = htons(dst_port); } dst_port++; if (connect(fd, ap, alen) < 0) fatal("Unable to connect to %s: %m", straddr(&dst_addr)); pb->timeout = now + opt_timeout * 1000; pb->hop = hop; pb->fd = fd; return 0; } static inline struct cmsghdr * cmsg_put(struct cmsghdr *cm, int type, void *data, size_t len) { cm->cmsg_level = SOL_IP; cm->cmsg_type = type; memcpy(CMSG_DATA(cm), data, len); cm->cmsg_len = CMSG_LEN(len); // msg.msg_controllen += cm->cmsg_len; return (struct cmsghdr *) ((caddr_t) cm + cm->cmsg_len); } static int hop_sendmsg(int fd) { struct msghdr msg; unsigned char control[1024]; struct cmsghdr *cm; struct iovec iov; memset(&msg, 0, sizeof(msg)); iov.iov_base = packet; iov.iov_len = packetsize; msg.msg_iov = &iov; msg.msg_iovlen = 1; /* Copy IP options, if specified. */ cm = (struct cmsghdr *) control; if (ipoptions_len) { cm = cmsg_put(cm, IP_RETOPTS, ipoptions, ipoptions_len); } /* Set source address, if specified */ if (src_addr.sin.sin_family) { if (src_addr.sin.sin_family == AF_INET) { struct in_pktinfo info; memset(&info, 0, sizeof(info)); info.ipi_spec_dst = ((struct sockaddr_in *) &src_addr)->sin_addr; cm = cmsg_put(cm, IP_PKTINFO, &info, sizeof(info)); } else { struct in6_pktinfo info; memset(&info, 0, sizeof(info)); info.ipi6_addr = ((struct sockaddr_in6 *) &src_addr)->sin6_addr; cm = cmsg_put(cm, IPV6_PKTINFO, &info, sizeof(info)); } } msg.msg_controllen = ((unsigned char *) cm) - control; if (msg.msg_controllen) msg.msg_control = control; return sendmsg(fd, &msg, 0); } static void probe_recv(struct probe *p) { static char errbuf[32]; struct hop *hop = p->hop; const char *errstring = 0; /* recv errmsg */ if (probe_recverr(p) < 0) return; if (opt_debug) fprintf(stderr, "received hop %u response\n", hop->ttl); /* Handle error codes */ errstring = errbuf; if (p->ee.ee_origin == SO_EE_ORIGIN_ICMP) { switch (p->ee.ee_type) { case ICMP_TIME_EXCEEDED: return; case ICMP_DEST_UNREACH: switch (p->ee.ee_code) { case ICMP_UNREACH_NET: case ICMP_UNREACH_NET_UNKNOWN: case ICMP_UNREACH_ISOLATED: case ICMP_UNREACH_NET_PROHIB: case ICMP_UNREACH_TOSNET: case ICMP_UNREACH_FILTER_PROHIB: errstring = "N!"; break; case ICMP_UNREACH_HOST: case ICMP_UNREACH_HOST_UNKNOWN: case ICMP_UNREACH_HOST_PROHIB: case ICMP_UNREACH_TOSHOST: case ICMP_UNREACH_HOST_PRECEDENCE: errstring = "H!"; break; case ICMP_UNREACH_PORT: /* we've reached the destintation host */ errstring = 0; break; case ICMP_UNREACH_PROTOCOL: errstring = "P!"; break; case ICMP_UNREACH_NEEDFRAG: errstring = "F!"; break; case ICMP_UNREACH_SRCFAIL: errstring = "S!"; break; default: errstring = "!!"; break; } break; } } else if (p->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { switch (p->ee.ee_type) { case ICMP6_TIME_EXCEEDED: if (p->ee.ee_code == ICMP6_TIME_EXCEED_TRANSIT) return; break; case ICMP6_DST_UNREACH: switch (p->ee.ee_code) { case ICMP6_DST_UNREACH_NOROUTE: case ICMP6_DST_UNREACH_ADMIN: errstring = "N!"; break; case ICMP6_DST_UNREACH_BEYONDSCOPE: case ICMP6_DST_UNREACH_ADDR: errstring = "H!"; break; case ICMP6_DST_UNREACH_NOPORT: /* we've reached the destintation host */ errstring = 0; break; default: errstring = "!!"; break; } break; } } if (errstring == errbuf) { snprintf(errbuf, sizeof(errbuf), "icmp-%d-%d", p->ee.ee_type, p->ee.ee_code); } p->err_ind = errstring; have_final_response = 1; hop->final = 1; } static int probe_recverr(struct probe *p) { struct sockaddr_in sin; struct msghdr msg; unsigned char control[1024]; struct cmsghdr *cm; memset(&msg, 0, sizeof(msg)); memset(&sin, 0, sizeof(sin)); memset(&p->ee, 0, sizeof(p->ee)); gettimeofday(&p->recvd_time, NULL); memset(&p->responder, 0, sizeof(p->responder)); msg.msg_name = &sin; msg.msg_namelen = sizeof(sin); msg.msg_control = control; msg.msg_controllen = sizeof(control); if (recvmsg(p->fd, &msg, MSG_ERRQUEUE) < 0) return -1; //printf("cmsgs:"); for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) { struct sock_extended_err *ep; //printf(" lvl=%d/typ=%d", cm->cmsg_level, cm->cmsg_type); if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) { memcpy(&p->recvd_time, CMSG_DATA(cm), sizeof(struct timeval)); continue; } if (!(cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_RECVERR) && !(cm->cmsg_level == SOL_IPV6 && cm->cmsg_type == IPV6_RECVERR)) continue; ep = (struct sock_extended_err *) CMSG_DATA(cm); memcpy(&p->ee, ep, sizeof(p->ee)); memcpy(&p->responder, SO_EE_OFFENDER(ep), sizeof(sockaddr_any)); //printf(";addr="); printaddr(&p->responder, 0); } //printf("\n"); p->done = 1; close(p->fd); p->fd = -1; return 0; } static void probe_timeout(struct probe *pb) { pb->done = 1; } static void ipopt_init(void) { unsigned int nr; if (af == AF_INET6) { if (ngateways) fprintf(stderr, "Sorry, this traceroute implementation does " "not support source routing for IPv6.\n"); return; } if (ngateways) { ipoptions[0] = IPOPT_NOP; /* pad */ ipoptions[1] = IPOPT_LSRR; ipoptions[2] = 39; ipoptions[3] = 4; for (nr = 0; nr < ngateways; nr++) { struct sockaddr_in *sin; sin = (struct sockaddr_in *) &opt_gateway[nr]; memcpy(ipoptions + ((nr + 1) << 2), &sin->sin_addr, 4); } ipoptions_len = 40; } else if (opt_rr) { ipoptions[0] = IPOPT_NOP; /* pad */ ipoptions[1] = IPOPT_RR; ipoptions[2] = 39; ipoptions[3] = 4; ipoptions_len = 40; } } traceroute-1.0.4/utils.c000064400000000000000000000071171051105034600145740ustar00gitgit00000000000000/* * Utility functions * * Copyright (C) 2000-2005, Olaf Kirch * * License: GPL 2.0 or later. See COPYING.GPL for details. */ #include #include #include #include #include #include #include #include "utils.h" /* Older glibc versions do not have IDN support */ #ifndef AI_IDN # define AI_IDN 0 #endif #define AF_LEN(af) (((af) == AF_INET)? 4 : 16) #define SIN_FMLY(ap) ((ap)->any.sa_family) #define SIN_LEN(ap) AF_LEN(SIN_FMLY(ap)) #define SIN_ADDR(ap) ((SIN_FMLY(ap) == AF_INET)? \ (caddr_t) &(ap)->sin.sin_addr : \ (caddr_t) &(ap)->sin6.sin6_addr ) unsigned int getnum(const char *what, const char *arg, unsigned int min, unsigned int max) { unsigned int value; char *end; value = strtoul(arg, &end, 0); if (*end) fatal("bad %s value \"%s\"", what, arg); if (value < min) fatal("bad %s value %u, must be less than %u", what, value, min); if (value > max) fatal("bad %s value %u, must be greater than %u", what, value, max); return value; } int getaddr(const char *arg, int af, sockaddr_any *ap) { struct addrinfo hints, *res = NULL, *ai; int rv; memset(ap, 0, sizeof(*ap)); if (af != -1) { SIN_FMLY(ap) = af; if (inet_pton(af, arg, SIN_ADDR(ap))) return 1; } else { if (inet_pton(AF_INET, arg, &ap->sin.sin_addr)) { ap->sin.sin_family = AF_INET; return 1; } if (inet_pton(AF_INET6, arg, &ap->sin6.sin6_addr)) { ap->sin6.sin6_family = AF_INET6; return 1; } } memset(&hints, 0, sizeof(hints)); hints.ai_family = (af == -1)? PF_UNSPEC : af; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_CANONNAME|AI_IDN; if ((rv = getaddrinfo(arg, NULL, &hints, &res)) != 0) { fprintf(stderr, "%s: %s\n", arg, gai_strerror(rv)); return 0; } /* If no address family specified, prefer v4 (for now) */ for (ai = res; af != -1 && ai && ai->ai_family != AF_INET; ai = ai->ai_next) ; if (ai == NULL) ai = res; if ((af != -1 && ai->ai_family != af) || ai->ai_family != ai->ai_addr->sa_family || ai->ai_addrlen > sizeof(*ap)) { fprintf(stderr, "%s: inconsistent data from getaddrinfo\n", arg); return 0; } memcpy(ap, ai->ai_addr, ai->ai_addrlen); freeaddrinfo(res); return 1; } const char * getfqdn(sockaddr_any *ap, int resolve) { static char namebuf[4096]; socklen_t salen; int rv, flags; switch (SIN_FMLY(ap)) { case AF_INET: salen = sizeof(struct sockaddr_in); break; case AF_INET6: salen = sizeof(struct sockaddr_in6); break; default: return NULL; } flags = NI_IDN; if (!resolve) flags |= NI_NUMERICHOST; /* We do not set NI_NAMEREQD, so this should fall back to the * numeric address in the NXDOMAIN case */ rv = getnameinfo((struct sockaddr *) ap, salen, namebuf, sizeof(namebuf), NULL, 0, flags); if (rv != 0) { fprintf(stderr, "Unable to look up %s: %s\n", straddr(ap), gai_strerror(rv)); return NULL; } return namebuf; } const char * straddr(sockaddr_any *ap) { static char buffer[INET6_ADDRSTRLEN]; return inet_ntop(ap->any.sa_family, SIN_ADDR(ap), buffer, sizeof(buffer)); } int sameaddr(sockaddr_any *ap1, sockaddr_any *ap2) { return SIN_FMLY(ap1) == SIN_FMLY(ap2) && !memcmp(SIN_ADDR(ap1), SIN_ADDR(ap2), SIN_LEN(ap1)); } void printaddr(sockaddr_any *ap, int resolve) { const char *fqdn; if ((fqdn = getfqdn(ap, resolve)) != NULL) { printf("%s (%s)", fqdn, straddr(ap)); } else { printf("%s", straddr(ap)); } } void fatal(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); if (!strchr(fmt, '\n')) fputs("\n", stderr); va_end(ap); exit(1); } traceroute-1.0.4/utils.h000064400000000000000000000011771051105034600146010ustar00gitgit00000000000000/* * Utility functions * * Copyright (C) 2000-2005, Olaf Kirch * * License: GPL 2.0 or later. See COPYING.GPL for details. */ #ifndef UTILS_H #define UTILS_H typedef union { struct sockaddr any; struct sockaddr_in sin; struct sockaddr_in6 sin6; } sockaddr_any; extern unsigned int getnum(const char *, const char *, unsigned int, unsigned int); extern int getaddr(const char *, int, sockaddr_any *); extern const char * straddr(sockaddr_any *); extern int sameaddr(sockaddr_any *, sockaddr_any *); extern void printaddr(sockaddr_any *, int) ; extern void fatal(const char *, ...); #endif /* UTILS_H */ traceroute-1.0.4/version.h000064400000000000000000000000411051105034600151130ustar00gitgit00000000000000#define TRACEROUTE_VERSION "1.0"