Net-Routing

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

   - bugfix: do not mess with PATH and LC_ALL environment variables

0.43 Fri Feb 20 06:58:25 CET 2015
   - update: add a check in Makefile.PL/Build.PL for supported OS
   - bugfix: VERSION was not consistent in all modules

0.42 Thu Feb 19 07:26:03 CET 2015
   - bugfix: on cidrlookup: returns only routes matching the target IP in get()

0.41 Mon Feb  9 21:44:37 CET 2015
   - bugfix: FreeBSD netstat to use -f inet/inet6 so it is compatible with Darwin
   - bugfix: FreeBSD to report all IPv6 routes (by striping interface name)
   - bugfix: Linux new() to return undef on error

0.40 Fri Feb  6 18:53:48 CET 2015
   - new: routing for NetBSD
   - new: routing for Darwin (same as FreeBSD 9.x)

0.30 Wed Feb  4 17:20:11 CET 2015
   - UPDATE: rewrote main methods in Net::Routing in a cleaner way
   - new: use constants, and makes them exportable

lib/Net/Routing.pm  view on Meta::CPAN

use Net::CIDR;
use Net::IPv4Addr;
use Net::IPv6Addr;

our $_routing_module;
our $Error;

use constant NR_TARGET_ALL => 'all';
use constant NR_TARGET_DEFAULT => 'default';
use constant NR_FAMILY_INET4 => 'inet4';
use constant NR_FAMILY_INET6 => 'inet6';
use constant NR_DEFAULT_ROUTE4 => '0.0.0.0/0';
use constant NR_DEFAULT_ROUTE6 => '::/0';
use constant NR_LOCAL_ROUTE4 => '0.0.0.0';
use constant NR_LOCAL_ROUTE6 => '::';

use constant _TARGET_TYPE_ALL => 'all';
use constant _TARGET_TYPE_DEFAULT => 'default';
use constant _TARGET_TYPE_IPv4 => 'ipv4';
use constant _TARGET_TYPE_IPv6 => 'ipv6';
use constant _TARGET_TYPE_INTERFACE => 'interface';

lib/Net/Routing/FreeBSD.pm  view on Meta::CPAN

            last;
         }
      }
      if (! length($bin)) {
         $Error = "unable to find netstat command from current PATH";
         return;
      }
   }

   my $cmd4 = [ $bin, '-rnf', 'inet' ];
   my $cmd6 = [ $bin, '-rnf', 'inet6' ];

   return $self->SUPER::get($cmd4, $cmd6);
}

sub _get_inet4 {
   my $self = shift;
   my ($lines) = @_;

   my @routes = ();
   my %cache = ();

lib/Net/Routing/FreeBSD.pm  view on Meta::CPAN

            #print STDERR "*** DEBUG new $id\n";
            push @routes, \%route;
            $cache{$id}++;
         }
      }
   }

   return \@routes;
}

sub _get_inet6 {
   my $self = shift;
   my ($lines) = @_;

   my @routes = ();
   my %cache = ();

   # FreeBSD 9.3-RELEASE
   # Internet6:
   # Destination                       Gateway                       Flags      Netif Expire
   # ::/96                             ::1                           UGRS        lo0 =>

lib/Net/Routing/Linux.pm  view on Meta::CPAN

            last;
         }
      }
      if (! length($bin)) {
         $Error = "unable to find netstat command from current PATH";
         return;
      }
   };

   $cmd4 ||= [ $bin, '-rnA', 'inet' ];
   $cmd6 ||= [ $bin, '-rnA', 'inet6' ];

   my $cmd = [];
   if ($family eq NR_FAMILY_INET4()) {
     $cmd = $cmd4;
   }
   # If not NR_FAMILY_INET4(), it must be NR_FAMILY_INET6() because we validated family at new()
   else {
     $cmd = $cmd6;
   }

lib/Net/Routing/Linux.pm  view on Meta::CPAN

   }

   my $routes = [];

   my @lines = split(/\n/, $out);
   if ($family eq NR_FAMILY_INET4()) {
      $routes = $self->_get_inet4(\@lines);
   }
   # If not NR_FAMILY_INET4(), it must be NR_FAMILY_INET6() because we validated family at new()
   else {
      $routes = $self->_get_inet6(\@lines);
   }

   return $routes;
}

sub _get_inet4 {
   my $self = shift;
   my ($lines) = @_;

   my @routes = ();

lib/Net/Routing/Linux.pm  view on Meta::CPAN

         if (! exists($cache{$id})) {
            push @routes, \%route;
            $cache{$id}++;
         }
      }
   }

   return \@routes;
}

sub _get_inet6 {
   my $self = shift;
   my ($lines) = @_;

   my @routes = ();
   my %cache = ();

   for my $line (@$lines) {
      my @toks = split(/\s+/, $line);
      my $route = $toks[0];
      my $gateway = $toks[1];

lib/Net/Routing/NetBSD.pm  view on Meta::CPAN

            #print STDERR "*** DEBUG new $id\n";
            push @routes, \%route;
            $cache{$id}++;
         }
      }
   }

   return \@routes;
}

sub _get_inet6 {
   my $self = shift;
   my ($lines) = @_;

   my @routes = ();
   my %cache = ();

   # NetBSD
   # Internet6:
   # Destination                   Gateway                   Flags     Refs     Use Mtu  Interface
   # ::/104                        ::1                       UGRS        0        0 33228  lo0 =>

t/04-linux-netstat.t  view on Meta::CPAN

      my $psv = _to_psv($route);
      if (! exists($route4->{$psv})) {
         die("Invalid IPv4 route: $psv\n");
      }
   }

   return 1;
}

sub default_route6 {
   my $routes = Net::Routing::Linux->_get_inet6($lines6);

   my $count = @$routes;
   if ($count != $route6_count) {
      die("Invalid number of IPv6 routes: $count instead of $route6_count\n");
   }

   for my $route (@$routes) {
      my $psv = _to_psv($route);
      if (! exists($route6->{$psv})) {
         die("Invalid IPv6 route: $psv\n");

t/05-freebsd-netstat-10-x.t  view on Meta::CPAN

      my $psv = _to_psv($route);
      if (! exists($route4->{$psv})) {
         die("Invalid IPv4 route: $psv\n");
      }
   }

   return 1;
}

sub default_route6 {
   my $routes = Net::Routing::FreeBSD->_get_inet6($lines6);

   my $count = @$routes;
   if ($count != $route6_count) {
      die("Invalid number of IPv6 routes: $count instead of $route6_count\n");
   }

   for my $route (@$routes) {
      my $psv = _to_psv($route);
      if (! exists($route6->{$psv})) {
         die("Invalid IPv6 route: $psv\n");

t/06-freebsd-netstat-9-x.t  view on Meta::CPAN

      my $psv = _to_psv($route);
      if (! exists($route4->{$psv})) {
         die("Invalid IPv4 route: $psv\n");
      }
   }

   return 1;
}

sub default_route6 {
   my $routes = Net::Routing::FreeBSD->_get_inet6($lines6);

   my $count = @$routes;
   if ($count != $route6_count) {
      die("Invalid number of IPv6 routes: $count instead of $route6_count\n");
   }

   for my $route (@$routes) {
      my $psv = _to_psv($route);
      if (! exists($route6->{$psv})) {
         die("Invalid IPv6 route: $psv\n");

t/07-netbsd-netstat.t  view on Meta::CPAN

      my $psv = _to_psv($route);
      if (! exists($route4->{$psv})) {
         die("Invalid IPv4 route: $psv\n");
      }
   }

   return 1;
}

sub default_route6 {
   my $routes = Net::Routing::NetBSD->_get_inet6($lines6);

   my $count = @$routes;
   if ($count != $route6_count) {
      die("Invalid number of IPv6 routes: $count instead of $route6_count\n");
   }

   for my $route (@$routes) {
      my $psv = _to_psv($route);
      if (! exists($route6->{$psv})) {
         die("Invalid IPv6 route: $psv\n");

t/08-darwin-netstat.t  view on Meta::CPAN

      my $psv = _to_psv($route);
      if (! exists($route4->{$psv})) {
         die("Invalid IPv4 route: $psv\n");
      }
   }

   return 1;
}

sub default_route6 {
   my $routes = Net::Routing::Darwin->_get_inet6($lines6);

   my $count = @$routes;
   if ($count != $route6_count) {
      die("Invalid number of IPv6 routes: $count instead of $route6_count\n");
   }

   for my $route (@$routes) {
      my $psv = _to_psv($route);
      if (! exists($route6->{$psv})) {
         die("Invalid IPv6 route: $psv\n");



( run in 0.541 second using v1.01-cache-2.11-cpan-5f2e87ce722 )