blib/lib/Traceroute/Similar.pm

filter: Statementstmt 52.8% 66 / 125 Branchbran 21.2% 14 / 66 Conditioncond 20.8% 5 / 24 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 76.9% 10 / 13 Podpod 100% 4 / 4 totaltotal 40.8% 100 / 245 CC 44 SCAR 49.0
CC
44
Cov
40%
_get_route_for_host
380.0
_detect_backend
20.5
get_last_common_hop
12.0
CRAP 135.0
↑ Summary
1 #!/usr/bin/env perl
2 #
3 # vim:ts=4:sw=4:expandtab
4  
5 package Traceroute::Similar;
6  
7 3 ▶use 5.000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.20';
13  
14 =head1 NAME
15  
16 Traceroute::Similar - calculate common route for a bunch of hosts
17  
18 =head1 SYNOPSIS
19  
20 use Traceroute::Similar;
21 my $ts = Traceroute::Similar->new();
22 print $ts->get_last_common_hop('host1.com', 'host2.org');
23  
24 =head1 DESCRIPTION
25  
26 This module calculates the furthest common hop from a list of host. The backend
27 will be Net::Traceroute:PurePerl or Net::Traceroute or system
28 tracerroute (which may require root or sudo permissions).
29  
30 =head1 CONSTRUCTOR
31  
32 =over 4
33  
34 =item new ( [ARGS] )
35  
36 Creates an C<Traceroute::Similar> object. All arguments are optional.
37  
38 backend 'Net::Traceroute' or 'Net::Traceroute::PurePerl'
39 verbose verbose mode
40  
41 =back
42  
43 =cut
44  
45 ########################################
46 3 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
47 3 my($class,%options) = @_;
48 3 my $self = {
49 "verbose" => 0,
50 "backend" => undef,
51 };
52 3 bless $self, $class;
53  
54 3 ▶ $self->{'verbose'} = $options{'verbose'} if defined $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"verbose"} 3 0 3
55  
56 # which backend do we use?
57 3 ▶ $self->{'backend'} = $options{'backend'} if defined $options{'backend'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"backend"} 0 3 3
58 3 ▶ $self->{'backend'} = $self->_detect_backend() unless defined $self->{'backend'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"backend"} 3 0 3
59  
60 3 ▶ if(!defined $self->{'backend'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend"}) 3 0 3
61 3 carp("No backend found, please install one of Net::Traceroute or Net::Traceroute::PurePerl. Or make sure your traceroute binary is in your path.");
62 }
63  
64 3 return $self;
65 }
66  
67  
68 ########################################
69  
70 =head1 METHODS
71  
72 =over 4
73  
74 =item get_backend ( )
75  
76 returns the used backend or undef if none found
77  
78 =cut
79  
80 2 ▶sub get_backend {
Subroutine get_backend: called CC=1 SCAR=0.0
Pod documented
81 2 my $self = shift;
82 2 return($self->{'backend'});
83 }
84  
85 =item get_last_common_hop ( host 1, host 2, [ host x...] )
86  
87 return the last hop which is part of all given hosts
88  
89 =cut
90  
91 0 ▶sub get_last_common_hop {
Subroutine get_last_common_hop: not called CC=3 SCAR=24.8
Pod documented
92 0 my $self = shift;
93 0 ▶ return if !defined $self->{'backend'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"backend"} 0 0 0
94 0 my $routes;
95 0 ▶ while(my $host = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = shift()) 0 0 0
96 0 $routes->{$host} = $self->_get_route_for_host($host);
97 }
98  
99 0 return($self->_calculate_last_common_hop($routes))
100 }
101  
102  
103 ########################################
104  
105 =item get_common_hops ( host 1, host 2, [ host x...] )
106  
107 return an array ref of the common hops from this list of hosts
108  
109 =cut
110  
111 0 ▶sub get_common_hops {
Subroutine get_common_hops: not called CC=3 SCAR=24.8
Pod documented
112 0 my $self = shift;
113 0 ▶ return if !defined $self->{'backend'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"backend"} 0 0 0
114 0 my $routes;
115 0 ▶ while(my $host = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = shift()) 0 0 0
116 0 $routes->{$host} = $self->_get_route_for_host($host);
117 }
118  
119 0 return($self->_calculate_common_hops($routes))
120 }
121  
122  
123 ########################################
124 # internal subs
125 ########################################
126 1 ▶sub _calculate_last_common_hop {
Subroutine _calculate_last_common_hop: called CC=3 SCAR=11.3
127 1 my $self = shift;
128 1 my $routes = shift;
129 1 my $last_common_addr;
130 1 my $common = $self->_calculate_common_hops($routes);
131  
132 1 ▶ if(defined $common and scalar @{$common} >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $common and scalar @{$common;} >= 1) 1 0 1
Condition33%1/3
defined $common and scalar @{$common;} >= 1
!ll&&!rl&&r
001
MC/DC0%0/2
defined $common and scalar @{$common;} >= 1
defined $common scalar @{$common;} >= 1
Truth table33%1/3
A and B
A= defined $commonB= scalar @{$common;} >= 1
ABresult
0X0
100
111
133 1 $last_common_addr = pop @{$common};
134 }
135  
136 1 return($last_common_addr);
137 }
138  
139 ########################################
140 2 ▶sub _calculate_common_hops {
Subroutine _calculate_common_hops: called CC=7 SCAR=20.0
141 2 my $self = shift;
142 2 my $routes = shift;
143 2 my $common;
144  
145 2 ▶ return if !defined $routes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $routes 0 2 2
146  
147 2 my @hostnames = keys %{$routes};
148 0 ▶ if(scalar @hostnames <= 1) { croak("need at least 2 hosts to calculate similiar routes"); }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @hostnames <= 1) 0 2 2
149  
150 2 my $last_common_addr = undef;
151 2 ▶ for(my $x = 0; $x <= scalar(@{$routes->{$hostnames[0]}}); $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= scalar @{$$routes{$hostnames[0]};}) 12 0 12
152 12 my $current_hop = $routes->{$hostnames[0]}->[$x]->{'addr'};
153 12 for my $host (@hostnames) {
154 24 ▶ if(!defined $routes->{$host}->[$x]->{'addr'} or $current_hop ne $routes->{$host}->[$x]->{'addr'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $routes->{$host}[$x]{"addr"} or $current_hop ne $routes->{$host}[$x]{"addr"}) 2 22 24
Condition66%2/3
not defined $routes->{$host}[$x]{"addr"} or $current_hop ne $routes->{$host}[$x]{"addr"}
l!l&&r!l&&!r
0222
MC/DC50%1/2
not defined $routes->{$host}[$x]{"addr"} or $current_hop ne $routes->{$host}[$x]{"addr"}
not defined $routes->{$host}[$x]{"addr"} $current_hop ne $routes->{$host}[$x]{"addr"}
Truth table66%2/3
not(A) or B
A= not defined $routes->{$host}[$x]{"addr"}B= $current_hop ne $routes->{$host}[$x]{"addr"}
ABresult
1X1
011
000
155 2 return $common;
156 }
157 }
158 10 $last_common_addr = $current_hop;
159 10 push @{$common}, $last_common_addr;
160 }
161  
162 0 return($common);
163 }
164  
165 ########################################
166 0 ▶sub _get_route_for_host {
Subroutine _get_route_for_host: not called CC=19 SCAR=59.4
167 0 my $self = shift;
168 0 my $host = shift;
169 0 my $routes = [];
170  
171 0 ▶ print "DEBUG: _get_route_for_host('".$host."')\n" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
172  
173 0 ▶ if($self->{'backend'} eq 'traceroute') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'backend'} eq 'traceroute') { } 0 0 0
elsif ($self->{'backend'} eq 'Net::Traceroute') { } 0 0 0
elsif ($self->{'backend'} eq 'Net::Traceroute::PurePerl') { } 0 0 0
174 0 my $cmd = "traceroute $host";
175 0 ▶ print "DEBUG: cmd: $cmd\n" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
176 0 ▶ open(my $ph, "-|", "$cmd 2>&1") or confess("cmd failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $ph, "-|", "$cmd 2>&1" or confess("cmd failed: $!") 0 0 0
177 0 my $output;
178 0 ▶ while(<$ph>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ph)) 0 0 0
179 0 my $line = $_;
180 0 $output .= $line;
181 0 ▶ print "DEBUG: traceroute: $line" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
182 }
183 0 close($ph);
184 0 my $rt = $?>>8;
185 0 ▶ print "DEBUG: return code from traceroute: $rt\n" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
186  
187 0 ▶ if($rt == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt == 0) 0 0 0
188 0 $routes = $self->_extract_routes_from_traceroute($output);
189 }
190 }
191 elsif($self->{'backend'} eq 'Net::Traceroute') {
192 0 my $tr = Net::Traceroute->new(host=> $host);
193 0 my $hops = $tr->hops;
194 0 my $last_hop;
195 0 ▶ for(my $x = 0; $x <= $hops; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $hops) 0 0 0
196 0 my $cur_hop = $tr->hop_query_host($x, 0);
197 0 ▶ if(defined $cur_hop and (!defined $last_hop or $last_hop ne $cur_hop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur_hop and !defined($last_hop) || $last_hop ne $cur_hop) 0 0 0
Condition0%0/6
defined $cur_hop and !defined($last_hop) || $last_hop ne $cur_hop
!ll&&!rl&&r
000
not defined $last_hop or $last_hop ne $cur_hop
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $cur_hop and !defined($last_hop) || $last_hop ne $cur_hop
defined $cur_hop not defined $last_hop $last_hop ne $cur_hop
Truth table0%0/4
A and not(B) or C
A= defined $cur_hopB= not defined $last_hopC= $last_hop ne $cur_hop
ABCresult
0XX0
1000
11X1
1011
198 0 push @{$routes}, { 'addr' => $cur_hop, 'name' => '' };
199 0 $last_hop = $cur_hop;
200 }
201 }
202 }
203 elsif($self->{'backend'} eq 'Net::Traceroute::PurePerl') {
204 0 my $tr = new Net::Traceroute::PurePerl( host => $host );
205 0 $tr->traceroute;
206 0 my $hops = $tr->hops;
207 0 my $last_hop;
208 0 ▶ for(my $x = 0; $x <= $hops; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $hops) 0 0 0
209 0 my $cur_hop = $tr->hop_query_host($x, 0);
210 0 ▶ if(defined $cur_hop and (!defined $last_hop or $last_hop ne $cur_hop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur_hop and !defined($last_hop) || $last_hop ne $cur_hop) 0 0 0
Condition0%0/6
defined $cur_hop and !defined($last_hop) || $last_hop ne $cur_hop
!ll&&!rl&&r
000
not defined $last_hop or $last_hop ne $cur_hop
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $cur_hop and !defined($last_hop) || $last_hop ne $cur_hop
defined $cur_hop not defined $last_hop $last_hop ne $cur_hop
Truth table0%0/4
A and not(B) or C
A= defined $cur_hopB= not defined $last_hopC= $last_hop ne $cur_hop
ABCresult
0XX0
1000
11X1
1011
211 0 push @{$routes}, { 'addr' => $cur_hop, 'name' => '' };
212 0 $last_hop = $cur_hop;
213 }
214 }
215 }
216 else {
217 0 carp("unknown backend: ".$self->{'backend'});
218 }
219  
220 0 return $routes;
221 }
222  
223 ########################################
224 2 ▶sub _extract_routes_from_traceroute {
Subroutine _extract_routes_from_traceroute: called CC=3 SCAR=11.0
225 2 my $self = shift;
226 2 my $output = shift;
227 2 my @routes;
228  
229 2 for my $line (split /\n/xm, $output) {
230 31 ▶ if($line =~ m/(\d+)\s+(.*?)\s+\((\d+\.\d+\.\d+\.\d+)\)/xm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /(\d+)\s+(.*?)\s+\((\d+\.\d+\.\d+\.\d+)\)/mx) 19 12 31
231 19 push @routes, { 'addr' => $3, 'name' => $2 };
232 }
233 }
234  
235 2 return(\@routes);
236 }
237  
238 ########################################
239 3 ▶sub _detect_backend {
Subroutine _detect_backend: called CC=8 SCAR=30.2
240 3 my $self = shift;
241  
242 3 ▶ print "DEBUG: detecting backend\n" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 3 3
243  
244 # try to load Net::Traceroute:PurePerl
245 3 eval {
246 3 require Net::Traceroute::PurePerl;
247 0 ▶ print "DEBUG: using Net::Traceroute::PurePerl as backend\n" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
248 0 return("Net::Traceroute::PurePerl");
249 };
250  
251 # try to load Net::Traceroute
252 3 eval {
253 3 require Net::Traceroute;
254 0 ▶ print "DEBUG: using Net::Traceroute as backend\n" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
255 0 return("Net::Traceroute");
256 };
257  
258 # try to use traceroute
259 3 chomp(my $traceroute_bin = qx{which traceroute});
260 3 ▶ if(defined $traceroute_bin and $traceroute_bin ne '' and -x $traceroute_bin) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $traceroute_bin and $traceroute_bin ne "" and -x $traceroute_bin) 0 3 3
Condition33%2/6
defined $traceroute_bin and $traceroute_bin ne "" and -x $traceroute_bin
!ll&&!rl&&r
300
defined $traceroute_bin and $traceroute_bin ne ""
!ll&&!rl&&r
030
MC/DC0%0/3
defined $traceroute_bin and $traceroute_bin ne "" and -x $traceroute_bin
defined $traceroute_bin $traceroute_bin ne "" -x $traceroute_bin
Truth table25%1/4
A and B and C
A= defined $traceroute_binB= $traceroute_bin ne ""C= -x $traceroute_bin
ABCresult
0XX0
10X0
1100
1111
261 0 ▶ print "DEBUG: found traceroute in path: $traceroute_bin\n" if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
262 0 return('traceroute');
263 }
264  
265 3 return;
266 }
267  
268 ########################################
269  
270 3 1;
271  
272 =head1 AUTHOR
273  
274 Sven Nierlein, E<lt>nierlein@cpan.orgE<gt>
275  
276 =head1 COPYRIGHT AND LICENSE
277  
278 Copyright (C) 2009 by Sven Nierlein
279  
280 This library is free software; you can redistribute it and/or modify
281 it under the same terms as Perl itself.
282  
283 =cut
284  
285 __END__
↑ Summary