| 1 | package Net::Traceroute::PurePerl; | ||
| 2 | |||
| 3 | use vars qw(@ISA $VERSION $AUTOLOAD %net_traceroute_native_var %protocols); | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Net::Traceroute; | ||
| 7 | use Socket; | ||
| 8 | use FileHandle; | ||
| 9 | use Carp qw(carp croak); | ||
| 10 | use Time::HiRes qw(time); | ||
| 11 | |||
| 12 | @ISA = qw(Net::Traceroute); | ||
| 13 | $VERSION = '0.10'; | ||
| 14 | |||
| 15 | # Constants from header files or RFCs | ||
| 16 | |||
| 17 | use constant SO_BINDTODEVICE => 25; # from asm/socket.h | ||
| 18 | use constant IPPROTO_IP => 0; # from netinet/in.h | ||
| 19 | |||
| 20 | # Windows winsock2 uses 4 for IP_TTL instead of 2 | ||
| 21 | use constant IP_TTL => ($^O eq "MSWin32") ? 4 : 2; | ||
| 22 | |||
| 23 | use constant IP_HEADERS => 20; # Length of IP headers | ||
| 24 | use constant ICMP_HEADERS => 8; # Length of ICMP headers | ||
| 25 | use constant UDP_HEADERS => 8; # Length of UDP headers | ||
| 26 | |||
| 27 | use constant IP_PROTOCOL => 9; # Position of protocol number | ||
| 28 | |||
| 29 | use constant UDP_DATA => IP_HEADERS + UDP_HEADERS; | ||
| 30 | use constant ICMP_DATA => IP_HEADERS + ICMP_HEADERS; | ||
| 31 | |||
| 32 | use constant UDP_SPORT => IP_HEADERS + 0; # Position of sport | ||
| 33 | use constant UDP_DPORT => IP_HEADERS + 2; # Position of dport | ||
| 34 | |||
| 35 | use constant ICMP_TYPE => IP_HEADERS + 0; # Position of type | ||
| 36 | use constant ICMP_CODE => IP_HEADERS + 2; # Position of code | ||
| 37 | use constant ICMP_ID => IP_HEADERS + 4; # Position of ID | ||
| 38 | use constant ICMP_SEQ => IP_HEADERS + 6; # Position of seq | ||
| 39 | |||
| 40 | use constant ICMP_PORT => 0; # ICMP has no port | ||
| 41 | |||
| 42 | use constant ICMP_TYPE_TIMEEXCEED => 11; # ICMP Type | ||
| 43 | use constant ICMP_TYPE_ECHO => 8; # ICMP Type | ||
| 44 | use constant ICMP_TYPE_UNREACHABLE => 3; # ICMP Type | ||
| 45 | use constant ICMP_TYPE_ECHOREPLY => 0; # ICMP Type | ||
| 46 | |||
| 47 | use constant ICMP_CODE_ECHO => 0; # ICMP Echo has no code | ||
| 48 | |||
| 49 | # Perl 5.8.6 under Windows has a bug in the socket code, this env variable | ||
| 50 | # works around the bug. It may effect other versions as well, and they should | ||
| 51 | # be added here | ||
| 52 | BEGIN | ||
| 53 | { | ||
| 54 | if ($^O eq "MSWin32" and $^V eq v5.8.6) | ||
| 55 | { | ||
| 56 | $ENV{PERL_ALLOW_NON_IFS_LSP} = 1; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | # The list of currently accepted protocols | ||
| 61 | %protocols = | ||
| 62 | ( | ||
| 63 | 'icmp' => 1, | ||
| 64 | 'udp' => 1, | ||
| 65 | ); | ||
| 66 | |||
| 67 | my @icmp_unreach_code = | ||
| 68 | ( | ||
| 69 | TRACEROUTE_UNREACH_NET, | ||
| 70 | TRACEROUTE_UNREACH_HOST, | ||
| 71 | TRACEROUTE_UNREACH_PROTO, | ||
| 72 | 0, | ||
| 73 | TRACEROUTE_UNREACH_NEEDFRAG, | ||
| 74 | TRACEROUTE_UNREACH_SRCFAIL, | ||
| 75 | ); | ||
| 76 | |||
| 77 | # set up allowed autoload attributes we need | ||
| 78 | my @net_traceroute_native_vars = qw(use_alarm concurrent_hops protocol | ||
| 79 | first_hop device); | ||
| 80 | |||
| 81 | @net_traceroute_native_var{@net_traceroute_native_vars} = (); | ||
| 82 | |||
| 83 | # Methods | ||
| 84 | |||
| 85 | # AUTOLOAD (perl internal) | ||
| 86 | # Used to create the methods for the object dynamically from | ||
| 87 | # net_traceroute_naive_vars. | ||
| 88 | sub AUTOLOAD | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | my $attr = $AUTOLOAD; | ||
| 92 | $attr =~ s/.*:://; | ||
| 93 | return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods | ||
| 94 | carp "invalid attribute method: ->$attr()" | ||
| 95 | unless exists $net_traceroute_native_var{$attr}; | ||
| 96 | $self->{$attr} = shift if @_; | ||
| 97 | return $self->{$attr}; | ||
| 98 | } | ||
| 99 | |||
| 100 | # new (public method) | ||
| 101 | # Creates a new blessed object of type Net::Traceroute::PurePerl. | ||
| 102 | # Accepts many options as arguments, and initilizes the new object with | ||
| 103 | # their values. | ||
| 104 | # Croaks on bad arguments. | ||
| 105 | sub new | ||
| 106 | { | ||
| 107 | my $self = shift; | ||
| 108 | my $type = ref($self) || $self; | ||
| 109 | my %arg = @_; | ||
| 110 | |||
| 111 | $self = bless {}, $type; | ||
| 112 | |||
| 113 | # keep a loop from happening when calling super::new | ||
| 114 | my $backend = delete $arg{'backend'}; | ||
| 115 | |||
| 116 | # used to get around the real traceroute running the trace | ||
| 117 | my $host = delete $arg{'host'}; | ||
| 118 | |||
| 119 | # Old method to use ICMP for traceroutes, using `protocol' is preferred | ||
| 120 | my $useicmp = delete $arg{'useicmp'}; | ||
| 121 | |||
| 122 | $self->debug_print(1, | ||
| 123 | "The useicmp parameter is depreciated, use `protocol'\n") if ($useicmp); | ||
| 124 | |||
| 125 | # Initialize blessed hash with passed arguments | ||
| 126 | $self->_init(%arg); | ||
| 127 | |||
| 128 | # Set protocol to ICMP if useicmp was set; | ||
| 129 | if ($useicmp) | ||
| 130 | { | ||
| 131 | carp ("Protocol already set, useicmp is overriding") | ||
| 132 | if (defined $self->protocol and $self->protocol ne "icmp"); | ||
| 133 | $self->protocol('icmp') if ($useicmp); | ||
| 134 | } | ||
| 135 | |||
| 136 | # put our host back in and set defaults for undefined options | ||
| 137 | $self->host($host) if (defined $host); | ||
| 138 | $self->max_ttl(30) unless (defined $self->max_ttl); | ||
| 139 | $self->queries(3) unless (defined $self->queries); | ||
| 140 | $self->base_port(33434) unless (defined $self->base_port); | ||
| 141 | $self->query_timeout(5) unless (defined $self->query_timeout); | ||
| 142 | $self->packetlen(40) unless (defined $self->packetlen); | ||
| 143 | $self->first_hop(1) unless (defined $self->first_hop); | ||
| 144 | $self->concurrent_hops(6) unless (defined $self->concurrent_hops); | ||
| 145 | |||
| 146 | # UDP is the UNIX default for traceroute | ||
| 147 | $self->protocol('udp') unless (defined $self->protocol); | ||
| 148 | |||
| 149 | # Depreciated: we no longer use libpcap, so the alarm is no longer | ||
| 150 | # required. Kept for backwards compatibility but not used. | ||
| 151 | $self->use_alarm(0) unless (defined $self->use_alarm); | ||
| 152 | |||
| 153 | # Validates all of the parameters. | ||
| 154 | $self->_validate(); | ||
| 155 | |||
| 156 | return $self; | ||
| 157 | } | ||
| 158 | |||
| 159 | # _init (private initialization method) | ||
| 160 | # Overrides Net::Traceroutes init to set PurePerl specific parameters. | ||
| 161 | sub _init | ||
| 162 | { | ||
| 163 | my $self = shift; | ||
| 164 | my %arg = @_; | ||
| 165 | |||
| 166 | foreach my $var (@net_traceroute_native_vars) | ||
| 167 | { | ||
| 168 | if(defined($arg{$var})) { | ||
| 169 | $self->$var($arg{$var}); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | $self->SUPER::init(@_); | ||
| 174 | } | ||
| 175 | |||
| 176 | # pretty_print (public method) | ||
| 177 | # The output of pretty_print tries to match the output of traceroute(1) as | ||
| 178 | # close as possible, with two excpetions. First, I cleaned up the columns to | ||
| 179 | # make it easier to read, and second, I start a new line if the host IP changes | ||
| 180 | # instead of printing the new IP inline. The first column stays the same hop | ||
| 181 | # number, only the host changes. | ||
| 182 | sub pretty_print | ||
| 183 | { | ||
| 184 | my $self = shift; | ||
| 185 | my $resolve = shift; | ||
| 186 | |||
| 187 | print "traceroute to " . $self->host; | ||
| 188 | print " (" . inet_ntoa($self->{'_destination'}) . "), "; | ||
| 189 | print $self->max_ttl . " hops max, " . $self->packetlen ." byte packets\n"; | ||
| 190 | |||
| 191 | my $lasthop = $self->first_hop + $self->hops - 1; | ||
| 192 | |||
| 193 | for (my $hop=$self->first_hop; $hop <= $lasthop; $hop++) | ||
| 194 | { | ||
| 195 | my $lasthost = ''; | ||
| 196 | |||
| 197 | printf '%2s ', $hop; | ||
| 198 | |||
| 199 | if (not $self->hop_queries($hop)) | ||
| 200 | { | ||
| 201 | print "error: no responses\n"; | ||
| 202 | next; | ||
| 203 | } | ||
| 204 | |||
| 205 | for (my $query=1; $query <= $self->hop_queries($hop); $query++) { | ||
| 206 | my $host = $self->hop_query_host($hop,$query); | ||
| 207 | if ($host and $resolve) | ||
| 208 | { | ||
| 209 | my $ip = $host; | ||
| 210 | $host = (gethostbyaddr(inet_aton($ip),AF_INET))[0] || $ip; | ||
| 211 | } | ||
| 212 | if ($host and ( not $lasthost or $host ne $lasthost )) | ||
| 213 | { | ||
| 214 | printf "\n%2s ", $hop if ($lasthost and $host ne $lasthost); | ||
| 215 | printf '%-15s ', $host; | ||
| 216 | $lasthost = $host; | ||
| 217 | } | ||
| 218 | my $time = $self->hop_query_time($hop, $query); | ||
| 219 | if (defined $time and $time > 0) | ||
| 220 | { | ||
| 221 | printf '%7s ms ', $time; | ||
| 222 | } | ||
| 223 | else | ||
| 224 | { | ||
| 225 | print "* "; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | print "\n"; | ||
| 230 | } | ||
| 231 | |||
| 232 | return; | ||
| 233 | } | ||
| 234 | |||
| 235 | # traceroute (public method) | ||
| 236 | # Starts a new traceroute. This is a blocking call and it will either croak on | ||
| 237 | # error, or return 0 if the host wasn't reached, or 1 if it was. | ||
| 238 | sub traceroute | ||
| 239 | { | ||
| 240 | my $self = shift; | ||
| 241 | |||
| 242 | # Revalidate parameters incase they were changed by calling $t->parameter() | ||
| 243 | # since the object was created. | ||
| 244 | $self->_validate(); | ||
| 245 | |||
| 246 | carp "No host provided!" && return undef unless (defined $self->host); | ||
| 247 | |||
| 248 | $self->debug_print(1, "Performing traceroute\n"); | ||
| 249 | |||
| 250 | # Lookup the destination IP inside of a local scope | ||
| 251 | { | ||
| 252 | my $destination = inet_aton($self->host); | ||
| 253 | |||
| 254 | croak "Could not resolve host " . $self->host | ||
| 255 | unless (defined $destination); | ||
| 256 | |||
| 257 | $self->{_destination} = $destination; | ||
| 258 | } | ||
| 259 | |||
| 260 | # release any old hop structure | ||
| 261 | $self->_zero_hops(); | ||
| 262 | |||
| 263 | # Create the ICMP socket, used to send ICMP messages and receive ICMP errors | ||
| 264 | # Under windows, the ICMP socket doesn't get the ICMP errors unless the | ||
| 265 | # sending socket was ICMP, or the interface is in promiscuous mode, which | ||
| 266 | # is why ICMP is the only supported protocol under windows. | ||
| 267 | my $icmpsocket = FileHandle->new(); | ||
| 268 | |||
| 269 | socket($icmpsocket, PF_INET, SOCK_RAW, getprotobyname('icmp')) || | ||
| 270 | croak("ICMP Socket error - $!"); | ||
| 271 | |||
| 272 | $self->debug_print(2, "Created ICMP socket to receive errors\n"); | ||
| 273 | |||
| 274 | $self->{'_icmp_socket'} = $icmpsocket; | ||
| 275 | $self->{'_trace_socket'} = $self->_create_tracert_socket(); | ||
| 276 | |||
| 277 | # _run_traceroute is the event loop that actually does the work. | ||
| 278 | my $success = $self->_run_traceroute(); | ||
| 279 | |||
| 280 | return $success; | ||
| 281 | } | ||
| 282 | |||
| 283 | # Private methods | ||
| 284 | |||
| 285 | # _validate (private method) | ||
| 286 | # Normalizes and validates all parameters, croaks on error | ||
| 287 | sub _validate | ||
| 288 | { | ||
| 289 | my $self = shift; | ||
| 290 | |||
| 291 | # Normalize values; | ||
| 292 | |||
| 293 | $self->protocol( lc $self->protocol); | ||
| 294 | |||
| 295 | $self->max_ttl( sprintf('%i',$self->max_ttl)); | ||
| 296 | $self->queries( sprintf('%i',$self->queries)); | ||
| 297 | $self->base_port( sprintf('%i',$self->base_port)); | ||
| 298 | $self->query_timeout( sprintf('%i',$self->query_timeout)); | ||
| 299 | $self->packetlen( sprintf('%i',$self->packetlen)); | ||
| 300 | $self->first_hop( sprintf('%i',$self->first_hop)); | ||
| 301 | $self->concurrent_hops( sprintf('%i',$self->concurrent_hops)); | ||
| 302 | |||
| 303 | # Check to see if values are sane | ||
| 304 | |||
| 305 | croak "Parameter `protocol' value is not supported : " . $self->protocol | ||
| 306 | if (not exists $protocols{$self->protocol}); | ||
| 307 | |||
| 308 | croak "Parameter `first_hop' must be an integer between 1 and 255" | ||
| 309 | if ($self->first_hop < 1 or $self->first_hop > 255); | ||
| 310 | |||
| 311 | croak "Parameter `max_ttl' must be an integer between 1 and 255" | ||
| 312 | if ($self->max_ttl < 1 or $self->max_ttl > 255); | ||
| 313 | |||
| 314 | croak "Parameter `base_port' must be an integer between 1 and 65280" | ||
| 315 | if ($self->base_port < 1 or $self->base_port > 65280); | ||
| 316 | |||
| 317 | croak "Parameter `packetlen' must be an integer between 40 and 1492" | ||
| 318 | if ($self->packetlen < 40 or $self->packetlen > 1492); | ||
| 319 | |||
| 320 | croak "Parameter `first_hop' must be less than or equal to `max_ttl'" | ||
| 321 | if ($self->first_hop > $self->max_ttl); | ||
| 322 | |||
| 323 | croak "parameter `queries' must be an interger between 1 and 255" | ||
| 324 | if ($self->queries < 1 or $self->queries > 255); | ||
| 325 | |||
| 326 | croak "parameter `concurrent_hops' must be an interger between 1 and 255" | ||
| 327 | if ($self->concurrent_hops < 1 or $self->concurrent_hops > 255); | ||
| 328 | |||
| 329 | croak "protocol " . $self->protocol . " not supported under Windows" | ||
| 330 | if ($self->protocol ne 'icmp' and $^O eq 'MSWin32'); | ||
| 331 | |||
| 332 | return; | ||
| 333 | } | ||
| 334 | |||
| 335 | # _run_traceroute (private method) | ||
| 336 | # The heart of the traceroute method. Sends out packets with incrementing | ||
| 337 | # ttls per hop. Recieves responses, validates them, and updates the hops | ||
| 338 | # hash with the time and host. Processes timeouts and returns when the host | ||
| 339 | # is reached, or the last packet on the last hop sent has been received | ||
| 340 | # or has timed out. Returns 1 if the host was reached, or 0. | ||
| 341 | sub _run_traceroute | ||
| 342 | { | ||
| 343 | my $self = shift; | ||
| 344 | |||
| 345 | my ( $end, # Counter for endhop to wait until all queries return | ||
| 346 | $endhop, # The hop that the host was reached on | ||
| 347 | $stop, # Tells the main loop to exit | ||
| 348 | $sentpackets, # Number of packets sent | ||
| 349 | $currenthop, # Current hop | ||
| 350 | $currentquery, # Current query within the hop | ||
| 351 | $nexttimeout, # Next time a packet will timeout | ||
| 352 | $rbits, # select() bits | ||
| 353 | $nfound, # Number of ready sockets from select() | ||
| 354 | %packets, # Hash of packets sent but without a response | ||
| 355 | %pktids, # Hash of packet port or seq numbers to packet ids | ||
| 356 | ); | ||
| 357 | |||
| 358 | $stop = $end = $endhop = $sentpackets = 0; | ||
| 359 | |||
| 360 | %packets = (); | ||
| 361 | %pktids = (); | ||
| 362 | |||
| 363 | $currenthop = $self->first_hop; | ||
| 364 | $currentquery = 0; | ||
| 365 | |||
| 366 | $rbits = ""; | ||
| 367 | vec($rbits,$self->{'_icmp_socket'}->fileno(), 1) = 1; | ||
| 368 | |||
| 369 | while (not $stop) | ||
| 370 | { | ||
| 371 | # Reset the variable | ||
| 372 | $nfound = 0; | ||
| 373 | |||
| 374 | # Send packets so long as there are packets to send, there is less than | ||
| 375 | # conncurrent_hops packets currently outstanding, there is no packets | ||
| 376 | # waiting to be read on the socket and we haven't reached the host yet. | ||
| 377 | while (scalar keys %packets < $self->concurrent_hops and | ||
| 378 | $currenthop <= $self->max_ttl and | ||
| 379 | (not $endhop or $currenthop <= $endhop) and | ||
| 380 | not $nfound = select((my $rout = $rbits),undef,undef,0)) | ||
| 381 | { | ||
| 382 | # sentpackets is used as an uid in the %packets hash. | ||
| 383 | $sentpackets++; | ||
| 384 | |||
| 385 | $self->debug_print(1,"Sending packet $currenthop $currentquery\n"); | ||
| 386 | my $start_time = $self->_send_packet($currenthop,$currentquery); | ||
| 387 | my $id = $self->{'_last_id'}; | ||
| 388 | my $localport = $self->{'_local_port'}; | ||
| 389 | |||
| 390 | $packets{$sentpackets} = | ||
| 391 | { | ||
| 392 | 'id' => $id, | ||
| 393 | 'hop' => $currenthop, | ||
| 394 | 'query' => $currentquery, | ||
| 395 | 'localport' => $localport, | ||
| 396 | 'starttime' => $start_time, | ||
| 397 | 'timeout' => $start_time+$self->query_timeout, | ||
| 398 | }; | ||
| 399 | |||
| 400 | $pktids{$id} = $sentpackets; | ||
| 401 | |||
| 402 | $nexttimeout = $packets{$sentpackets}{'timeout'} | ||
| 403 | unless ($nexttimeout); | ||
| 404 | |||
| 405 | # Current query and current hop increments | ||
| 406 | $currentquery = ($currentquery + 1) % $self->queries; | ||
| 407 | if ($currentquery == 0) | ||
| 408 | { | ||
| 409 | $currenthop++; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | # If $nfound is nonzero than data is waiting to be read, no need to | ||
| 414 | # call select again. | ||
| 415 | if (not $nfound) # No data waiting to be read yet | ||
| 416 | { | ||
| 417 | # This sets the timeout for select to no more than .1 seconds | ||
| 418 | my $timeout = $nexttimeout - time; | ||
| 419 | $timeout = .1 if ($timeout > .1); | ||
| 420 | $nfound = select((my $rout = $rbits),undef,undef,$timeout); | ||
| 421 | } | ||
| 422 | |||
| 423 | # While data is waiting to be read, read it. | ||
| 424 | while ($nfound and keys %packets) | ||
| 425 | { | ||
| 426 | my ( $recv_msg, # The packet read by recv() | ||
| 427 | $from_saddr, # The saddr returned by recv() | ||
| 428 | $from_port, # The port the packet came from | ||
| 429 | $from_ip, # The IP the packet came from | ||
| 430 | $from_id, # The dport / seq of the received packet | ||
| 431 | $from_proto, # The protocol of the packet | ||
| 432 | $from_type, # The ICMP type of the packet | ||
| 433 | $from_code, # The ICMP code of the packet | ||
| 434 | $icmp_data, # The data portion of the ICMP packet | ||
| 435 | $local_port, # The local port the packet is a reply to | ||
| 436 | $end_time, # The time the packet arrived | ||
| 437 | $last_hop, # Set to 1 if this packet came from the host | ||
| 438 | ); | ||
| 439 | |||
| 440 | $end_time = time; | ||
| 441 | |||
| 442 | $from_saddr = recv($self->{'_icmp_socket'},$recv_msg,1500,0); | ||
| 443 | if (defined $from_saddr) | ||
| 444 | { | ||
| 445 | ($from_port,$from_ip) = sockaddr_in($from_saddr); | ||
| 446 | $from_ip = inet_ntoa($from_ip); | ||
| 447 | $self->debug_print(1,"Received packet from $from_ip\n"); | ||
| 448 | } | ||
| 449 | else | ||
| 450 | { | ||
| 451 | $self->debug_print(1,"No packet?\n"); | ||
| 452 | $nfound = 0; | ||
| 453 | last; | ||
| 454 | } | ||
| 455 | |||
| 456 | $from_proto = unpack('C',substr($recv_msg,IP_PROTOCOL,1)); | ||
| 457 | |||
| 458 | if ($from_proto != getprotobyname('icmp')) | ||
| 459 | { | ||
| 460 | my $protoname = getprotobynumber($from_proto); | ||
| 461 | $self->debug_print(1,"Packet not ICMP $from_proto($protoname)\n"); | ||
| 462 | last; | ||
| 463 | } | ||
| 464 | |||
| 465 | ($from_type,$from_code) = unpack('CC',substr($recv_msg,ICMP_TYPE,2)); | ||
| 466 | $icmp_data = substr($recv_msg,ICMP_DATA); | ||
| 467 | |||
| 468 | if (not $icmp_data) | ||
| 469 | { | ||
| 470 | $self->debug_print(1, | ||
| 471 | "No data in packet ($from_type,$from_code)\n"); | ||
| 472 | last; | ||
| 473 | } | ||
| 474 | |||
| 475 | # TODO This code does not decode ICMP codes, only ICMP types, which can lead | ||
| 476 | # to false results if a router sends, for instance, a Network Unreachable | ||
| 477 | # or Fragmentation Needed packet. | ||
| 478 | if ( $from_type == ICMP_TYPE_TIMEEXCEED or | ||
| 479 | $from_type == ICMP_TYPE_UNREACHABLE or | ||
| 480 | ($self->protocol eq "icmp" and | ||
| 481 | $from_type == ICMP_TYPE_ECHOREPLY) ) | ||
| 482 | { | ||
| 483 | |||
| 484 | if ($self->protocol eq 'udp') | ||
| 485 | { | ||
| 486 | # The local port is used to verify the packet was sent from | ||
| 487 | # This process. | ||
| 488 | $local_port = unpack('n',substr($icmp_data,UDP_SPORT,2)); | ||
| 489 | |||
| 490 | # The ID for UDP is the destination port number of the packet | ||
| 491 | $from_id = unpack('n',substr($icmp_data,UDP_DPORT,2)); | ||
| 492 | |||
| 493 | # The target system will send ICMP port unreachable, routers | ||
| 494 | # along the path will send ICMP Time Exceeded messages. | ||
| 495 | $last_hop = ($from_type == ICMP_TYPE_UNREACHABLE) ? 1 : 0; | ||
| 496 | } | ||
| 497 | elsif ($self->protocol eq 'icmp') | ||
| 498 | { | ||
| 499 | if ($from_type == ICMP_TYPE_ECHOREPLY) | ||
| 500 | { | ||
| 501 | # The ICMP ID is used to verify the packet was sent from | ||
| 502 | # this process. | ||
| 503 | my $icmp_id = unpack('n',substr($recv_msg,ICMP_ID,2)); | ||
| 504 | last unless ($icmp_id == $$); | ||
| 505 | |||
| 506 | my $seq = unpack('n',substr($recv_msg,ICMP_SEQ,2)); | ||
| 507 | $from_id = $seq; # The ID for ICMP is the seq number | ||
| 508 | $last_hop = 1;; | ||
| 509 | } | ||
| 510 | else | ||
| 511 | { | ||
| 512 | # The ICMP ID is used to verify the packet was sent from | ||
| 513 | # this process. | ||
| 514 | my $icmp_id = unpack('n',substr($icmp_data,ICMP_ID,2)); | ||
| 515 | last unless ($icmp_id == $$); | ||
| 516 | |||
| 517 | my $ptype = unpack('C',substr($icmp_data,ICMP_TYPE,1)); | ||
| 518 | my $pseq = unpack('n',substr($icmp_data,ICMP_SEQ,2)); | ||
| 519 | if ($ptype eq ICMP_TYPE_ECHO) | ||
| 520 | { | ||
| 521 | $from_id = $pseq; # The ID for ICMP is the seq number | ||
| 522 | } | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | # If we got and decoded the packet to get an ID, process it. | ||
| 528 | if ($from_ip and $from_id) | ||
| 529 | { | ||
| 530 | my $id = $pktids{$from_id}; | ||
| 531 | if (not $id) | ||
| 532 | { | ||
| 533 | $self->debug_print(1,"No packet sent matches the reply\n"); | ||
| 534 | last; | ||
| 535 | } | ||
| 536 | if (not exists $packets{$id}) | ||
| 537 | { | ||
| 538 | $self->debug_print(1,"Packet $id received after ID deleted"); | ||
| 539 | last; | ||
| 540 | } | ||
| 541 | if ($packets{$id}{'id'} == $from_id) | ||
| 542 | { | ||
| 543 | last if ($self->protocol eq 'udp' and | ||
| 544 | $packets{$id}{'localport'} != $local_port); | ||
| 545 | |||
| 546 | my $total_time = $end_time - $packets{$id}{'starttime'}; | ||
| 547 | my $hop = $packets{$id}{'hop'}; | ||
| 548 | my $query = $packets{$id}{'query'}; | ||
| 549 | |||
| 550 | if (not $endhop or $hop <= $endhop) | ||
| 551 | { | ||
| 552 | $self->debug_print(1,"Recieved response for $hop $query\n"); | ||
| 553 | $self->_add_hop_query($hop, $query+1, TRACEROUTE_OK, | ||
| 554 | $from_ip, sprintf("%.2f", 1000 * $total_time) ); | ||
| 555 | |||
| 556 | # Sometimes a route will change and last_hop won't be set | ||
| 557 | # causing the traceroute to hang. Therefore if hop = endhop | ||
| 558 | # we set $end to the number of query responses for the | ||
| 559 | # hop recieved so far. | ||
| 560 | |||
| 561 | if ($last_hop or ($endhop and $hop == $endhop)) | ||
| 562 | { | ||
| 563 | $end = $self->hop_queries($hop); | ||
| 564 | $endhop = $hop; | ||
| 565 | } | ||
| 566 | } | ||
| 567 | |||
| 568 | # No longer waiting for this packet | ||
| 569 | delete $packets{$id}; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | # Check if more data is waiting to be read, if so keep reading | ||
| 573 | $nfound = select((my $rout = $rbits),undef,undef,0); | ||
| 574 | } | ||
| 575 | |||
| 576 | # Process timed out packets | ||
| 577 | if (keys %packets and $nexttimeout < time) | ||
| 578 | { | ||
| 579 | undef $nexttimeout; | ||
| 580 | |||
| 581 | foreach my $id (sort keys %packets) | ||
| 582 | { | ||
| 583 | my $hop = $packets{$id}{'hop'}; | ||
| 584 | |||
| 585 | if ($packets{$id}{'timeout'} < time) | ||
| 586 | { | ||
| 587 | my $query = $packets{$id}{'query'}; | ||
| 588 | |||
| 589 | $self->debug_print(1,"Timeout for $hop $query\n"); | ||
| 590 | $self->_add_hop_query($hop, $query+1, TRACEROUTE_TIMEOUT, | ||
| 591 | "", 0 ); | ||
| 592 | |||
| 593 | if ($endhop and $hop == $endhop) | ||
| 594 | { | ||
| 595 | # Sometimes a route will change and last_hop won't be set | ||
| 596 | # causing the traceroute to hang. Therefore if hop = endhop | ||
| 597 | # we set $end to the number of query responses for the | ||
| 598 | # hop recieved so far. | ||
| 599 | |||
| 600 | $end = $self->hop_queries($hop); | ||
| 601 | } | ||
| 602 | |||
| 603 | # No longer waiting for this packet | ||
| 604 | delete $packets{$id}; | ||
| 605 | } | ||
| 606 | elsif (not defined $nexttimeout) | ||
| 607 | { | ||
| 608 | # Reset next timeout to the next packet | ||
| 609 | $nexttimeout = $packets{$id}{'timeout'}; | ||
| 610 | last; | ||
| 611 | } | ||
| 612 | } | ||
| 613 | } | ||
| 614 | |||
| 615 | # Check if it is time to stop the looping | ||
| 616 | if ($currenthop > $self->max_ttl and not keys %packets) | ||
| 617 | { | ||
| 618 | $self->debug_print(1,"No more packets, reached max_ttl\n"); | ||
| 619 | $stop = 1; | ||
| 620 | } | ||
| 621 | elsif ($end >= $self->queries) | ||
| 622 | { | ||
| 623 | # Delete packets for hops after $endhop | ||
| 624 | foreach my $id (sort keys %packets) | ||
| 625 | { | ||
| 626 | my $hop = $packets{$id}{'hop'}; | ||
| 627 | if (not $hop or ( $endhop and $hop > $endhop) ) | ||
| 628 | { | ||
| 629 | # No longer care about this packet | ||
| 630 | delete $packets{$id}; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | if (not keys %packets) | ||
| 634 | { | ||
| 635 | $self->debug_print(1,"Reached host on $endhop hop\n"); | ||
| 636 | $end = 1; | ||
| 637 | $stop = 1; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | # Looping | ||
| 642 | } | ||
| 643 | |||
| 644 | return $end; | ||
| 645 | } | ||
| 646 | |||
| 647 | # _create_tracert_socket (private method) | ||
| 648 | # Reuses the ICMP socket already created for icmp traceroutes, or creates a | ||
| 649 | # new socket. It then binds the socket to the user defined device and/or | ||
| 650 | # source address if provided and returns the created socket. | ||
| 651 | sub _create_tracert_socket | ||
| 652 | { | ||
| 653 | my $self = shift; | ||
| 654 | my $socket; | ||
| 655 | |||
| 656 | if ($self->protocol eq "icmp") | ||
| 657 | { | ||
| 658 | $socket = $self->{'_icmp_socket'}; | ||
| 659 | } | ||
| 660 | elsif ($self->protocol eq "udp") | ||
| 661 | { | ||
| 662 | $socket = FileHandle->new(); | ||
| 663 | |||
| 664 | socket($socket, PF_INET, SOCK_DGRAM, getprotobyname('udp')) or | ||
| 665 | croak "UDP Socket creation error - $!"; | ||
| 666 | |||
| 667 | $self->debug_print(2,"Created UDP socket"); | ||
| 668 | } | ||
| 669 | |||
| 670 | if ($self->device) | ||
| 671 | { | ||
| 672 | setsockopt($socket, SOL_SOCKET, SO_BINDTODEVICE, | ||
| 673 | pack('Z*', $self->device)) or | ||
| 674 | croak "error binding to ". $self->device ." - $!"; | ||
| 675 | |||
| 676 | $self->debug_print(2,"Bound socket to ". $self->device ."\n"); | ||
| 677 | } | ||
| 678 | |||
| 679 | if ($self->source_address and $self->source_address ne '0.0.0.0') | ||
| 680 | { | ||
| 681 | $self->_bind($socket); | ||
| 682 | } | ||
| 683 | |||
| 684 | return $socket; | ||
| 685 | } | ||
| 686 | |||
| 687 | # _bind (private method) | ||
| 688 | # binds a sockets to a local address so all packets originate from that IP. | ||
| 689 | sub _bind | ||
| 690 | { | ||
| 691 | my $self = shift; | ||
| 692 | my $socket = shift; | ||
| 693 | |||
| 694 | my $ip = inet_aton($self->source_address); | ||
| 695 | |||
| 696 | croak "Nonexistant local address ". $self->source_address | ||
| 697 | unless (defined $ip); | ||
| 698 | |||
| 699 | CORE::bind($socket, sockaddr_in(0,$ip)) or | ||
| 700 | croak "Error binding to ".$self->source_address.", $!"; | ||
| 701 | |||
| 702 | $self->debug_print(2,"Bound socket to " . $self->source_address . "\n"); | ||
| 703 | |||
| 704 | return; | ||
| 705 | } | ||
| 706 | |||
| 707 | # _send_packet (private method) | ||
| 708 | # Sends the packet for $hop, $query to the destination. Actually calls | ||
| 709 | # submethods for the different protocols which create and send the packet. | ||
| 710 | sub _send_packet | ||
| 711 | { | ||
| 712 | my $self = shift; | ||
| 713 | my ($hop,$query) = @_; | ||
| 714 | |||
| 715 | if ($self->protocol eq "icmp") | ||
| 716 | { | ||
| 717 | # Sequence ID for the ICMP echo request | ||
| 718 | my $seq = ($hop-1) * $self->queries + $query + 1; | ||
| 719 | $self->_send_icmp_packet($seq,$hop); | ||
| 720 | $self->{'_last_id'} = $seq; | ||
| 721 | } | ||
| 722 | elsif ($self->protocol eq "udp") | ||
| 723 | { | ||
| 724 | # Destination port for the UDP packet | ||
| 725 | my $dport = $self->base_port + ($hop-1) * $self->queries + $query; | ||
| 726 | $self->_send_udp_packet($dport,$hop); | ||
| 727 | $self->{'_last_id'} = $dport; | ||
| 728 | } | ||
| 729 | |||
| 730 | return time; | ||
| 731 | } | ||
| 732 | |||
| 733 | # _send_icmp_packet (private method) | ||
| 734 | # Sends an ICMP packet with the given sequence number. The PID is used as | ||
| 735 | # the packet ID and $seq is the sequence number. | ||
| 736 | sub _send_icmp_packet | ||
| 737 | { | ||
| 738 | my $self = shift; | ||
| 739 | my ($seq,$hop) = @_; | ||
| 740 | |||
| 741 | # Set TTL of socket to $hop. | ||
| 742 | my $saddr = $self->_connect(ICMP_PORT,$hop); | ||
| 743 | my $data = 'a' x ($self->packetlen - ICMP_DATA); | ||
| 744 | |||
| 745 | my ($pkt, $chksum) = (0,0); | ||
| 746 | |||
| 747 | # Create packet twice, once without checksum, once with it | ||
| 748 | foreach (1 .. 2) | ||
| 749 | { | ||
| 750 | $pkt = pack('CC n3 A*', | ||
| 751 | ICMP_TYPE_ECHO, # Type | ||
| 752 | ICMP_CODE_ECHO, # Code | ||
| 753 | $chksum, # Checksum | ||
| 754 | $$, # ID (pid) | ||
| 755 | $seq, # Sequence | ||
| 756 | $data, # Data | ||
| 757 | ); | ||
| 758 | |||
| 759 | $chksum = $self->_checksum($pkt) unless ($chksum); | ||
| 760 | } | ||
| 761 | |||
| 762 | send($self->{'_trace_socket'}, $pkt, 0, $saddr); | ||
| 763 | |||
| 764 | return; | ||
| 765 | } | ||
| 766 | |||
| 767 | # _send_udp_packet (private method) | ||
| 768 | # Sends a udp packet to the given destination port. | ||
| 769 | sub _send_udp_packet | ||
| 770 | { | ||
| 771 | my $self = shift; | ||
| 772 | my ($dport,$hop) = @_; | ||
| 773 | |||
| 774 | # Connect socket to destination port and set TTL | ||
| 775 | my $saddr = $self->_connect($dport,$hop); | ||
| 776 | my $data = 'a' x ($self->packetlen - UDP_DATA); | ||
| 777 | |||
| 778 | $self->_connect($dport,$hop); | ||
| 779 | |||
| 780 | send($self->{'_trace_socket'}, $data, 0); | ||
| 781 | |||
| 782 | return; | ||
| 783 | } | ||
| 784 | |||
| 785 | # _connect (private method) | ||
| 786 | # Connects the socket unless the protocol is ICMP and sets the TTL. | ||
| 787 | sub _connect | ||
| 788 | { | ||
| 789 | my $self = shift; | ||
| 790 | my ($port,$hop) = @_; | ||
| 791 | |||
| 792 | my $socket_addr = sockaddr_in($port,$self->{_destination}); | ||
| 793 | |||
| 794 | if ($self->protocol eq 'udp') | ||
| 795 | { | ||
| 796 | CORE::connect($self->{'_trace_socket'},$socket_addr); | ||
| 797 | $self->debug_print(2,"Connected to " . $self->host . "\n"); | ||
| 798 | } | ||
| 799 | |||
| 800 | setsockopt($self->{'_trace_socket'}, IPPROTO_IP, IP_TTL, pack('C',$hop)); | ||
| 801 | $self->debug_print(2,"Set TTL to $hop\n"); | ||
| 802 | |||
| 803 | if ($self->protocol eq 'udp') | ||
| 804 | { | ||
| 805 | my $localaddr = getsockname($self->{'_trace_socket'}); | ||
| 806 | my ($lport,undef) = sockaddr_in($localaddr); | ||
| 807 | $self->{'_local_port'} = $lport; | ||
| 808 | } | ||
| 809 | |||
| 810 | return ($self->protocol eq 'icmp') ? $socket_addr : undef; | ||
| 811 | } | ||
| 812 | |||
| 813 | # _checksum (private method) | ||
| 814 | # Lifted verbatum from Net::Ping 2.31 | ||
| 815 | # Description: Do a checksum on the message. Basically sum all of | ||
| 816 | # the short words and fold the high order bits into the low order bits. | ||
| 817 | sub _checksum | ||
| 818 | { | ||
| 819 | my $self = shift; | ||
| 820 | my $msg = shift; | ||
| 821 | |||
| 822 | my ( $len_msg, # Length of the message | ||
| 823 | $num_short, # The number of short words in the message | ||
| 824 | $short, # One short word | ||
| 825 | $chk # The checksum | ||
| 826 | ); | ||
| 827 | |||
| 828 | $len_msg = length($msg); | ||
| 829 | $num_short = int($len_msg / 2); | ||
| 830 | $chk = 0; | ||
| 831 | foreach $short (unpack("n$num_short", $msg)) | ||
| 832 | { | ||
| 833 | $chk += $short; | ||
| 834 | } # Add the odd byte in | ||
| 835 | $chk += (unpack("C", substr($msg, $len_msg - 1, 1)) << 8) if $len_msg % 2; | ||
| 836 | $chk = ($chk >> 16) + ($chk & 0xffff); # Fold high into low | ||
| 837 | return(~(($chk >> 16) + $chk) & 0xffff); # Again and complement | ||
| 838 | } | ||
| 839 | |||
| 840 | 1; | ||
| 841 | |||
| 842 | __END__ |