| 1 | # | ||
| 2 | # $Id: NetBSD.pm,v 717225574cff 2015/11/12 08:46:57 gomor $ | ||
| 3 | # | ||
| 4 | package Net::Routing::NetBSD; | ||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.44'; | ||
| 9 | |||
| 10 | 2 | ▶ | use base qw(Net::Routing::FreeBSD); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | use Net::IPv4Addr; | ||
| 13 | use Net::IPv6Addr; | ||
| 14 | use Net::CIDR; | ||
| 15 | use Net::Routing qw($Error :constants); | ||
| 16 | |||
| 17 | sub _get_inet4 { | ||
| 18 | my $self = shift; | ||
| 19 | my ($lines) = @_; | ||
| 20 | |||
| 21 | my @routes = (); | ||
| 22 | my %cache = (); | ||
| 23 | |||
| 24 | # NetBSD | ||
| 25 | # Destination Gateway Flags Refs Use Mtu Interface | ||
| 26 | # default 208.44.95.1 UGS 0 330309 1500 ex0 | ||
| 27 | |||
| 28 | for my $line (@$lines) { | ||
| 29 | my @toks = split(/\s+/, $line); | ||
| 30 | |||
| 31 | my ($route, $gateway, $flags, $refs, $use, $mtu, $interface); | ||
| 32 | |||
| 33 | $route = $toks[0]; | ||
| 34 | $gateway = $toks[1]; | ||
| 35 | $flags = $toks[2]; | ||
| 36 | $refs = $toks[3]; | ||
| 37 | $use = $toks[4]; | ||
| 38 | $mtu = $toks[5]; | ||
| 39 | $interface = $toks[6]; | ||
| 40 | |||
| 41 | if (defined($route) && defined($gateway) && defined($interface)) { | ||
| 42 | #print STDERR "*** DEBUG $route $gateway $interface\n"; | ||
| 43 | |||
| 44 | # Convert NetBSD strings to "universal" IP addresses | ||
| 45 | if ($route eq 'default') { | ||
| 46 | $route = '0.0.0.0/0'; | ||
| 47 | } | ||
| 48 | if ($gateway =~ /^link/) { | ||
| 49 | $gateway = '0.0.0.0'; | ||
| 50 | } | ||
| 51 | |||
| 52 | # A first sanity check to help Net::IPv4Addr | ||
| 53 | if ($gateway !~ m{^[0-9\.]+$} || $route !~ m{^[0-9\.]+(?:/\d+)?$}) { | ||
| 54 | #print STDERR "*** SKIP [$gateway] [$route]\n"; | ||
| 55 | next; | ||
| 56 | } | ||
| 57 | |||
| 58 | # Normalize IP addresses | ||
| 59 | $route = Net::CIDR::range2cidr($route); # 127.16 => 172.16/16 | ||
| 60 | $route = Net::CIDR::cidrvalidate($route); # 172.16/16 => 172.16.0.0/16 | ||
| 61 | |||
| 62 | eval { | ||
| 63 | my ($ip1, $cidr1) = Net::IPv4Addr::ipv4_parse($route); | ||
| 64 | my ($ip2, $cidr2) = Net::IPv4Addr::ipv4_parse($gateway); | ||
| 65 | }; | ||
| 66 | if ($@) { | ||
| 67 | #chomp($@); | ||
| 68 | #print "*** DEBUG[$@]\n"; | ||
| 69 | next; # Not a valid line for us. | ||
| 70 | } | ||
| 71 | |||
| 72 | # Ok, proceed. | ||
| 73 | my %route = ( | ||
| 74 | route => $route, | ||
| 75 | gateway => $gateway, | ||
| 76 | interface => $interface, | ||
| 77 | ); | ||
| 78 | |||
| 79 | # Default route | ||
| 80 | if ($route eq '0.0.0.0/0') { | ||
| 81 | $route{default} = 1; | ||
| 82 | $route{route} = NR_DEFAULT_ROUTE4(); | ||
| 83 | } | ||
| 84 | |||
| 85 | # Local subnet | ||
| 86 | if ($gateway eq '0.0.0.0') { | ||
| 87 | $route{local} = 1; | ||
| 88 | $route{gateway} = NR_LOCAL_ROUTE4(); | ||
| 89 | } | ||
| 90 | |||
| 91 | if ($route{route} !~ /\/\d+$/) { | ||
| 92 | $route{route} .= '/32'; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $id = $self->_to_psv(\%route); | ||
| 96 | if (! exists($cache{$id})) { | ||
| 97 | #print STDERR "*** DEBUG new $id\n"; | ||
| 98 | push @routes, \%route; | ||
| 99 | $cache{$id}++; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | return \@routes; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _get_inet6 { | ||
| 108 | my $self = shift; | ||
| 109 | my ($lines) = @_; | ||
| 110 | |||
| 111 | my @routes = (); | ||
| 112 | my %cache = (); | ||
| 113 | |||
| 114 | # NetBSD | ||
| 115 | # Internet6: | ||
| 116 | # Destination Gateway Flags Refs Use Mtu Interface | ||
| 117 | # ::/104 ::1 UGRS 0 0 33228 lo0 => | ||
| 118 | # ::/96 ::1 UGRS 0 0 | ||
| 119 | |||
| 120 | for my $line (@$lines) { | ||
| 121 | my @toks = split(/\s+/, $line); | ||
| 122 | |||
| 123 | my $route = $toks[0]; | ||
| 124 | my $gateway = $toks[1]; | ||
| 125 | my $flags = $toks[2]; | ||
| 126 | my $refs = $toks[3]; | ||
| 127 | my $use = $toks[4]; | ||
| 128 | my $mtu = $toks[5]; | ||
| 129 | my $interface = $toks[6]; | ||
| 130 | |||
| 131 | if (defined($route) && defined($gateway) && defined($interface)) { | ||
| 132 | # Convert NetBSD strings to "universal" IP addresses | ||
| 133 | if ($gateway =~ /^link/ || $gateway eq '::1') { | ||
| 134 | $gateway = '::'; | ||
| 135 | } | ||
| 136 | if ($route eq 'default') { | ||
| 137 | $route = '::/0'; | ||
| 138 | } | ||
| 139 | |||
| 140 | # Special case: an entry with a MAC address means a default gateway | ||
| 141 | if ($gateway =~ /^[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}$/) { | ||
| 142 | my %route = ( | ||
| 143 | route => NR_DEFAULT_ROUTE6(), | ||
| 144 | gateway => $route, | ||
| 145 | interface => $interface, | ||
| 146 | ); | ||
| 147 | my $id = $self->_to_psv(\%route); | ||
| 148 | if (! exists($cache{$id})) { | ||
| 149 | push @routes, \%route; | ||
| 150 | $cache{$id}++; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | # A first sanity check to help Net::IPv6Addr | ||
| 155 | if ($route !~ m{^[0-9a-f:/]+$}i || $gateway !~ m{^[0-9a-f:/]+$}i) { | ||
| 156 | next; | ||
| 157 | } | ||
| 158 | |||
| 159 | #print STDERR "*** DEBUG $route $gateway $interface\n"; | ||
| 160 | |||
| 161 | eval { | ||
| 162 | #print "*** DEBUG $route $gateway\n"; | ||
| 163 | my $ip1 = Net::IPv6Addr::ipv6_parse($route); | ||
| 164 | my $ip2 = Net::IPv6Addr::ipv6_parse($gateway); | ||
| 165 | }; | ||
| 166 | if ($@) { | ||
| 167 | #chomp($@); | ||
| 168 | #print "*** DEBUG[$@]\n"; | ||
| 169 | next; # Not a valid line for us. | ||
| 170 | } | ||
| 171 | |||
| 172 | # Ok, proceed. | ||
| 173 | my %route = ( | ||
| 174 | route => $route, | ||
| 175 | gateway => $gateway, | ||
| 176 | interface => $interface, | ||
| 177 | ); | ||
| 178 | |||
| 179 | # Default route | ||
| 180 | if ($route eq '::/0') { | ||
| 181 | $route{default} = 1; | ||
| 182 | $route{route} = NR_DEFAULT_ROUTE6(); | ||
| 183 | } | ||
| 184 | |||
| 185 | # Local subnet | ||
| 186 | if ($gateway eq '::') { | ||
| 187 | $route{local} = 1; | ||
| 188 | $route{gateway} = NR_LOCAL_ROUTE6(); | ||
| 189 | } | ||
| 190 | |||
| 191 | if ($route{route} !~ /\/\d+$/) { | ||
| 192 | $route{route} .= '/128'; | ||
| 193 | } | ||
| 194 | |||
| 195 | my $id = $self->_to_psv(\%route); | ||
| 196 | if (! exists($cache{$id})) { | ||
| 197 | push @routes, \%route; | ||
| 198 | $cache{$id}++; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | return \@routes; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; | ||
| 207 | |||
| 208 | __END__ | ||