| 1 | package NetworkInfo::Discovery::Traceroute; | ||
| 2 | |||
| 3 | use vars qw(@ISA); | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use NetworkInfo::Discovery::Detect; | ||
| 8 | use Net::Traceroute; | ||
| 9 | |||
| 10 | @ISA = ("NetworkInfo::Discovery::Detect"); | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $classname = shift; | ||
| 14 | my $self = $classname->SUPER::new(@_); | ||
| 15 | |||
| 16 | |||
| 17 | return $self; # And give it back | ||
| 18 | } | ||
| 19 | |||
| 20 | sub do_it { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | # temporary hack to avoid picking up broadcast addresses which | ||
| 24 | # traceroute dumps on. Thanks to John Towry for getting annoyed | ||
| 25 | # with that thus making me fix it :) | ||
| 26 | if ($self->host =~ /\d+\.\d+\.\d+\.255/) { | ||
| 27 | return undef; | ||
| 28 | } | ||
| 29 | |||
| 30 | $self->{'tr'} = Net::Traceroute->new( | ||
| 31 | host=> $self->host, queries => 3, query_timeout => 2, max_ttl => $self->max_ttl | ||
| 32 | ); | ||
| 33 | |||
| 34 | my $tr = $self->{'tr'}; | ||
| 35 | |||
| 36 | for (my $hop=1; $hop <= $tr->hops; $hop++) { | ||
| 37 | |||
| 38 | my $lastip = $tr->hop_query_host($hop - 1 ,0) if ($hop > 1); | ||
| 39 | my $ip = $tr->hop_query_host($hop,0); | ||
| 40 | |||
| 41 | # my $lasthost = new NetworkInfo::Discovery::Host (ipaddress => $lastip ) if ($lastip); | ||
| 42 | # my $host = new NetworkInfo::Discovery::Host (ipaddress => $ip ) if ($ip); | ||
| 43 | |||
| 44 | $self->add_interface({ ip=>$lastip }) if $lastip; | ||
| 45 | $self->add_interface({ ip=>$ip }) if $ip; | ||
| 46 | $self->add_gateway({ ip=>$lastip }) if $lastip; | ||
| 47 | $self->add_gateway({ ip=>$ip }) if $ip; | ||
| 48 | |||
| 49 | # my $avglatency; | ||
| 50 | # | ||
| 51 | # for (my $query=1; $query <= $tr->hop_queries($hop); $query++) { | ||
| 52 | # $avglatency += $tr->hop_query_time($hop, $query); | ||
| 53 | # } | ||
| 54 | # | ||
| 55 | # $avglatency = $avglatency / $tr->hop_queries($hop); | ||
| 56 | # | ||
| 57 | # if (defined $host && $host) { | ||
| 58 | # $self->add_host( $host ); | ||
| 59 | # | ||
| 60 | # if (defined $lasthost && $lasthost) { | ||
| 61 | # $self->add_hop( [$host,$lasthost,$avglatency] ); | ||
| 62 | # } | ||
| 63 | # } | ||
| 64 | } | ||
| 65 | |||
| 66 | return $self->get_interfaces; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub max_ttl { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | $self->{'max_ttl'} = shift if (@_); | ||
| 73 | return $self->{'max_ttl'}; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub host { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | $self->{'host'} = shift if (@_); | ||
| 80 | return $self->{'host'}; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | 1; |