| 1 | # | ||
| 2 | # $Id: Routing.pm,v 717225574cff 2015/11/12 08:46:57 gomor $ | ||
| 3 | # | ||
| 4 | package Net::Routing; | ||
| 5 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.44'; | ||
| 9 | |||
| 10 | 7 | ▶ | use base qw(Class::Gomor::Hash); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | our @AS = qw( | ||
| 13 | path | ||
| 14 | lc_all | ||
| 15 | target | ||
| 16 | family | ||
| 17 | _target_type | ||
| 18 | _routing_module | ||
| 19 | _routes | ||
| 20 | ); | ||
| 21 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 22 | |||
| 23 | use Net::CIDR; | ||
| 24 | use Net::IPv4Addr; | ||
| 25 | use Net::IPv6Addr; | ||
| 26 | |||
| 27 | our $_routing_module; | ||
| 28 | our $Error; | ||
| 29 | |||
| 30 | use constant NR_TARGET_ALL => 'all'; | ||
| 31 | use constant NR_TARGET_DEFAULT => 'default'; | ||
| 32 | use constant NR_FAMILY_INET4 => 'inet4'; | ||
| 33 | use constant NR_FAMILY_INET6 => 'inet6'; | ||
| 34 | use constant NR_DEFAULT_ROUTE4 => '0.0.0.0/0'; | ||
| 35 | use constant NR_DEFAULT_ROUTE6 => '::/0'; | ||
| 36 | use constant NR_LOCAL_ROUTE4 => '0.0.0.0'; | ||
| 37 | use constant NR_LOCAL_ROUTE6 => '::'; | ||
| 38 | |||
| 39 | use constant _TARGET_TYPE_ALL => 'all'; | ||
| 40 | use constant _TARGET_TYPE_DEFAULT => 'default'; | ||
| 41 | use constant _TARGET_TYPE_IPv4 => 'ipv4'; | ||
| 42 | use constant _TARGET_TYPE_IPv6 => 'ipv6'; | ||
| 43 | use constant _TARGET_TYPE_INTERFACE => 'interface'; | ||
| 44 | |||
| 45 | our %EXPORT_TAGS = ( | ||
| 46 | constants => [qw( | ||
| 47 | NR_TARGET_ALL | ||
| 48 | NR_TARGET_DEFAULT | ||
| 49 | NR_FAMILY_INET4 | ||
| 50 | NR_FAMILY_INET6 | ||
| 51 | NR_DEFAULT_ROUTE4 | ||
| 52 | NR_DEFAULT_ROUTE6 | ||
| 53 | NR_LOCAL_ROUTE4 | ||
| 54 | NR_LOCAL_ROUTE6 | ||
| 55 | )], | ||
| 56 | ); | ||
| 57 | |||
| 58 | our @EXPORT_OK = ( | ||
| 59 | '$Error', | ||
| 60 | @{$EXPORT_TAGS{constants}}, | ||
| 61 | ); | ||
| 62 | |||
| 63 | BEGIN { | ||
| 64 | if ($^O eq 'linux') { | ||
| 65 | return $_routing_module = "Net::Routing::Linux"; | ||
| 66 | } | ||
| 67 | elsif ($^O eq 'freebsd') { | ||
| 68 | return $_routing_module = "Net::Routing::FreeBSD"; | ||
| 69 | } | ||
| 70 | elsif ($^O eq 'netbsd') { | ||
| 71 | return $_routing_module = "Net::Routing::NetBSD"; | ||
| 72 | } | ||
| 73 | elsif ($^O eq 'darwin') { | ||
| 74 | return $_routing_module = "Net::Routing::Darwin"; | ||
| 75 | } | ||
| 76 | #elsif ($^O eq 'MSWin32') { | ||
| 77 | # return $_routing_module = "Net::Routing::MSWin32"; | ||
| 78 | #} | ||
| 79 | #elsif ($^O eq 'openbsd') { | ||
| 80 | # return $_routing_module = "Net::Routing::OpenBSD"; | ||
| 81 | #} | ||
| 82 | |||
| 83 | die("[-] Net::Routing: Operating System not supported: $^O\n"); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub new { | ||
| 87 | my $self = shift->SUPER::new( | ||
| 88 | path => [ qw(/bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin) ], | ||
| 89 | lc_all => 'en_GB.UTF-8', | ||
| 90 | target => NR_TARGET_ALL(), | ||
| 91 | family => NR_FAMILY_INET4(), | ||
| 92 | @_, | ||
| 93 | ); | ||
| 94 | |||
| 95 | $self->path([ @{$self->path}, split(':', $ENV{PATH}) ]); | ||
| 96 | |||
| 97 | eval("use $_routing_module;"); | ||
| 98 | if ($@) { | ||
| 99 | chomp($@); | ||
| 100 | $Error = "unable to load routing module [$_routing_module]: $@"; | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | $self->_routing_module($_routing_module); | ||
| 105 | |||
| 106 | my $routes = $self->get or return; | ||
| 107 | |||
| 108 | $self->_routes($routes); | ||
| 109 | |||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _get_target_type { | ||
| 114 | my $self = shift; | ||
| 115 | my ($target) = @_; | ||
| 116 | |||
| 117 | my $target_type = ''; | ||
| 118 | |||
| 119 | if ($target eq NR_TARGET_ALL()) { | ||
| 120 | $target_type = _TARGET_TYPE_ALL(); | ||
| 121 | } | ||
| 122 | elsif ($target eq NR_TARGET_DEFAULT()) { | ||
| 123 | $target_type = _TARGET_TYPE_DEFAULT(); | ||
| 124 | } | ||
| 125 | elsif ($target =~ /^[0-9\.]+$/) { | ||
| 126 | eval { | ||
| 127 | my ($ip, $cidr) = Net::IPv4Addr::ipv4_parse($target); | ||
| 128 | }; | ||
| 129 | if (! $@) { | ||
| 130 | $target_type = _TARGET_TYPE_IPv4(); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | elsif ($target =~ /^[0-9a-f:\/]+$/i) { | ||
| 134 | eval { | ||
| 135 | my $x = Net::IPv6Addr::ipv6_parse($target); | ||
| 136 | }; | ||
| 137 | if (! $@) { | ||
| 138 | $target_type = _TARGET_TYPE_IPv6(); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | # If it is not an IPv4 nor IPv6 address or default nor all routes, | ||
| 142 | # we consider it is an interface. | ||
| 143 | else { | ||
| 144 | $target_type = _TARGET_TYPE_INTERFACE(); | ||
| 145 | } | ||
| 146 | |||
| 147 | return $target_type; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub get { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | my $target = $self->target; | ||
| 154 | my $family = $self->family; | ||
| 155 | my $target_type = $self->_get_target_type($target); | ||
| 156 | |||
| 157 | if ($target_type eq _TARGET_TYPE_IPv4()) { | ||
| 158 | $family = NR_FAMILY_INET4(); | ||
| 159 | } | ||
| 160 | elsif ($target_type eq _TARGET_TYPE_IPv6()) { | ||
| 161 | $family = NR_FAMILY_INET6(); | ||
| 162 | } | ||
| 163 | |||
| 164 | my $routes = $self->_routing_module_get or return; | ||
| 165 | if ($target_type eq _TARGET_TYPE_ALL()) { | ||
| 166 | return $routes; | ||
| 167 | } | ||
| 168 | |||
| 169 | # Return only wanted routes | ||
| 170 | my @routes = (); | ||
| 171 | for my $route (@$routes) { | ||
| 172 | # Will return default route only. | ||
| 173 | if ($target_type eq _TARGET_TYPE_DEFAULT()) { | ||
| 174 | if ($route->{default}) { | ||
| 175 | push @routes, $route; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | # Will return routes on interface only. | ||
| 179 | elsif ($target_type eq _TARGET_TYPE_INTERFACE()) { | ||
| 180 | if ($route->{interface} eq $target) { | ||
| 181 | push @routes, $route; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | # Will return local route only. | ||
| 185 | elsif ($target_type eq _TARGET_TYPE_IPv4() || $target_type eq _TARGET_TYPE_IPv6()) { | ||
| 186 | if ($route->{route} | ||
| 187 | && $route->{route} ne NR_DEFAULT_ROUTE4() | ||
| 188 | && $route->{route} ne NR_DEFAULT_ROUTE6()) { | ||
| 189 | my $r; | ||
| 190 | eval { | ||
| 191 | $r = Net::CIDR::cidrlookup($target, $route->{route}); | ||
| 192 | }; | ||
| 193 | if (! $@ && $r) { | ||
| 194 | push @routes, $route; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | # If no route matches, we will return the default route for types 'ipv4' and 'ipv6' | ||
| 201 | if (@routes == 0 | ||
| 202 | && ($target_type eq _TARGET_TYPE_IPv4() || $target_type eq _TARGET_TYPE_IPv6()) | ||
| 203 | ) { | ||
| 204 | for my $route (@$routes) { | ||
| 205 | if ($route->{default}) { | ||
| 206 | push @routes, $route; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | return \@routes; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub _routing_module_get { | ||
| 215 | my $self = shift; | ||
| 216 | |||
| 217 | my $routing_module = $self->_routing_module; | ||
| 218 | |||
| 219 | my $routing; | ||
| 220 | eval { | ||
| 221 | $routing = $routing_module->new( | ||
| 222 | path => $self->path, | ||
| 223 | family => $self->family, | ||
| 224 | ); | ||
| 225 | }; | ||
| 226 | if ($@) { | ||
| 227 | chomp($@); | ||
| 228 | $Error = "unable to load module [$routing_module]: $@"; | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | if (! defined($routing)) { | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | |||
| 235 | my $routes = $routing->get; | ||
| 236 | if (! defined($routes)) { | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | |||
| 240 | return $routes; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub list { | ||
| 244 | my $self = shift; | ||
| 245 | |||
| 246 | printf("%-33s %-33s %-10s\n", "Route", "Gateway", "Interface"); | ||
| 247 | |||
| 248 | my $routes = $self->_routes; | ||
| 249 | for my $route (@$routes) { | ||
| 250 | my $route2 = $route->{route}; | ||
| 251 | my $gateway = $route->{gateway}; | ||
| 252 | my $interface = $route->{interface}; | ||
| 253 | |||
| 254 | printf("%-33s %-33s %-10s", $route2, $gateway, $interface); | ||
| 255 | if ($route->{local}) { | ||
| 256 | print "[local]"; | ||
| 257 | } | ||
| 258 | elsif ($route->{default}) { | ||
| 259 | print "[default]"; | ||
| 260 | } | ||
| 261 | |||
| 262 | print "\n"; | ||
| 263 | } | ||
| 264 | |||
| 265 | return 1; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _to_psv { | ||
| 269 | my $self = shift; | ||
| 270 | my ($route) = @_; | ||
| 271 | |||
| 272 | my $psv = $route->{route}.'|'.$route->{gateway}.'|'.$route->{interface}.'|'. | ||
| 273 | (exists($route->{default})?'1':'0').'|'.(exists($route->{local})?'1':'0'); | ||
| 274 | |||
| 275 | return $psv; | ||
| 276 | } | ||
| 277 | |||
| 278 | 1; | ||
| 279 | |||
| 280 | __END__ | ||