File Coverage

blib/lib/Win32/Tracert.pm
Criterion Covered Total %
statement 71 95 74.7
branch 19 32 59.3
condition 11 12 91.6
subroutine 14 17 82.3
pod 6 7 85.7
total 121 163 74.2


line stmt bran cond sub pod time code
1             package Win32::Tracert;
2             $Win32::Tracert::VERSION = '0.011';
3 4     4   3059 use strict;
  4         8  
  4         181  
4 4     4   35 use warnings;
  4         7  
  4         183  
5              
6 4     4   2497 use Net::hostent;
  4         21040  
  4         34  
7 4     4   3628 use Socket qw ();
  4         19356  
  4         180  
8 4     4   1578 use Object::Tiny qw (destination circuit);
  4         722  
  4         31  
9 4     4   3040 use Win32::Tracert::Parser;
  4         11  
  4         53  
10              
11             # ABSTRACT: Call Win32 tracert tool or parse Win32 tracert output
12              
13             my %tracert_result;
14              
15             #redefine constuctor
16             sub new {
17 9     9 0 61501 my $class = shift;
18 9         74 my $self = $class->SUPER::new( @_ );
19             # Extra checking and such
20 9 100 66     387 die "You must define [destination] attribute or [circuit] attribute" if ((! defined $self->destination) && (! defined $self->circuit));
21 8 100 100     375 die "constructor can't accept [circuit] and [destination] together" if ((defined $self->circuit) && (defined $self->destination));
22 7 100 100     291 die "Attribute [circuit] have to contain a Tracert result" if ((! defined $self->destination) && (scalar(@{$self->circuit}) == 0));
  5         105  
23 6 100 100     123 die "Attribute [destination] have to contain a hostname or IP address" if ((! defined $self->circuit) && ($self->destination eq ""));
24            
25 5         60 return $self;
26             }
27              
28              
29              
30             sub path {
31 9     9 1 16 my ($self,$value)=@_;
32 9 100       26 if (@_ == 2) {
33 4         9 $self->{path}=$value;
34             }
35            
36 9         24 return $self->{path};
37             }
38              
39             sub _destination_hostname{
40 1     1   1 my ($self,$value)=@_;
41 1 50       3 if (@_ == 2) {
42 1         5 $self->{_destination_hostname}=$value;
43             }
44            
45 1         2 return $self->{_destination_hostname};
46             }
47              
48             sub destination_hostname{
49 0     0 1 0 my $self=shift;
50 0         0 return $self->_destination_hostname;
51             }
52              
53             sub _destination_ip{
54 1     1   6 my ($self,$value)=@_;
55 1 50       4 if (@_ == 2) {
56 1         3 $self->{_destination_ip}=$value;
57             }
58            
59 1         1 return $self->{_destination_ip};
60             }
61              
62             sub destination_ip{
63 0     0 1 0 my $self=shift;
64 0         0 return $self->_destination_ip;
65             }
66              
67             sub _to_find{
68 0     0   0 my $self=shift;
69 0         0 my $hosttocheck=$self->destination;
70 0         0 my $iptocheck;
71            
72 0 0       0 if ($hosttocheck =~ /(?:\d{1,3}\.){3}\d{1,3}/) {
73 0         0 $iptocheck=$self->destination;
74             }
75             else{
76 0         0 my $h;
77             my $ipadress;
78            
79 0 0       0 if ($h = gethost($hosttocheck)){
80 0         0 $ipadress = inet_ntoa($h->addr);
81 0         0 $iptocheck=$ipadress;
82             }
83             else{
84 0         0 die "No such host: $hosttocheck\n";
85             }
86             }
87              
88             #Executing tracert call before to send its result to Parser
89 0         0 my @tracert_output=`tracert $hosttocheck`;
90              
91 0         0 return \@tracert_output;
92             }
93              
94             sub _get_target_ip{
95 3     3   6 my $self=shift;
96 3         5 my $hosttocheck=shift;
97 3         3 my $iptocheck;
98            
99 3 50       24 if ($hosttocheck =~ /(?:\d{1,3}\.){3}\d{1,3}/) {
100 3         4 $iptocheck=$hosttocheck;
101             }
102             else{
103 0         0 my $h;
104             my $ipadress;
105 0 0       0 if ($h = gethost($hosttocheck)){
106 0         0 $ipadress = inet_ntoa($h->addr);
107 0         0 $iptocheck=$ipadress;
108             }
109             else{
110 0         0 die "No such host: $hosttocheck\n";
111             }
112             }
113            
114 3         6 return $iptocheck;
115             }
116              
117             sub to_trace{
118 4     4 1 1524 my $self=shift;
119            
120 4 50       148 my $result = defined $self->destination ? $self->_to_find : $self->circuit ;
121 4         147 my $path=Win32::Tracert::Parser->new(input => $result);
122            
123             #put returned result from [to_parse] method in current [path] attribute
124 4         42 $self->path($path->to_parse);
125            
126 4         40 return $self;
127             }
128              
129             sub found{
130 2     2 1 5 my $self=shift;
131 2         4 my $tracert_result=$self->path();
132            
133 2         8 my $hosttocheck;
134             my $iptocheck;
135            
136 2 50       60 if (defined $self->destination) {
137 0         0 $hosttocheck=$self->destination;
138             }
139             else{
140 2         18 ($hosttocheck)=keys %{$tracert_result};
  2         9  
141             }
142            
143 2         8 $iptocheck=$self->_get_target_ip($hosttocheck);
144            
145 2 50       8 if (exists $tracert_result->{"$iptocheck"}) {
146 2 100       10 if ("$iptocheck" eq $tracert_result->{"$iptocheck"}->{'HOPS'}->[-1]->{'IPADRESS'}) {
147             #If we find path to destination we can initialise following private writable attributes
148 1         4 $self->_destination_ip($tracert_result->{"$iptocheck"}->{'HOPS'}->[-1]->{'IPADRESS'});
149 1         6 $self->_destination_hostname($tracert_result->{"$iptocheck"}->{'HOPS'}->[-1]->{'HOSTNAME'});
150            
151 1         7 return $self;
152             }
153             else{
154             #route to target undetermined
155 1         5 return;
156             }
157             }
158             else{
159 0         0 die "No traceroute result for $hosttocheck\n";
160             }
161             }
162              
163             sub hops{
164 1     1 1 3 my $self=shift;
165 1         4 my $tracert_result=$self->path();
166 1         2 my $hosttocheck;
167             my $iptocheck;
168            
169 1 50       36 if (defined $self->destination) {
170 0         0 $hosttocheck=$self->destination;
171             }
172             else{
173 1         8 ($hosttocheck)=keys %{$tracert_result};
  1         4  
174             }
175 1         5 $iptocheck=$self->_get_target_ip($hosttocheck);
176              
177 1         3 return scalar(@{$tracert_result->{"$iptocheck"}->{'HOPS'}});
  1         9  
178             }
179              
180              
181             1;
182              
183             __END__