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