Routing/FreeBSD.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Routing/Darwin.pm ↑ Summary Routing/Linux.pm »
1 #
2 # $Id: FreeBSD.pm,v 717225574cff 2015/11/12 08:46:57 gomor $
3 #
4 package Net::Routing::FreeBSD;
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.44';
9  
10 5 ▶use base qw(Net::Routing::Linux);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use Net::CIDR;
13 use Net::IPv4Addr;
14 use Net::IPv6Addr;
15 use Net::Routing qw($Error :constants);
16  
17 sub get {
18 my $self = shift;
19  
20 my $bin = '';
21 {
22 local $ENV{LC_ALL} = $self->lc_all;
23  
24 for my $path (@{$self->path}) {
25 if (-f "$path/netstat") {
26 $bin = "$path/netstat";
27 last;
28 }
29 }
30 if (! length($bin)) {
31 $Error = "unable to find netstat command from current PATH";
32 return;
33 }
34 }
35  
36 my $cmd4 = [ $bin, '-rnf', 'inet' ];
37 my $cmd6 = [ $bin, '-rnf', 'inet6' ];
38  
39 return $self->SUPER::get($cmd4, $cmd6);
40 }
41  
42 sub _get_inet4 {
43 my $self = shift;
44 my ($lines) = @_;
45  
46 my @routes = ();
47 my %cache = ();
48  
49 # FreeBSD 9.x
50 # Destination Gateway Flags Refs Use Netif Expire
51 # default 8.8.210.254 UGS 0 14188719 em0
52 #
53 # FreeBSD 10.x
54 # Destination Gateway Flags Netif Expire
55 # default 8.8.25.254 UGS re0
56  
57 my $freebsd_version = '10.x';
58  
59 for my $line (@$lines) {
60 # FreeBSD 10.1-RELEASE
61 if ($line =~ /^\s*destination\s+gateway\s+flags\s+netif\s+expire\s*$/i) {
62 #print STDERR "*** DEBUG FreeBSD 10.x\n";
63 $freebsd_version = '10.x';
64 next;
65 }
66 # FreeBSD 9.3-RELEASE
67 elsif ($line =~ /^\s*destination\s+gateway\s+flags\s+refs\s+use\s+netif\s+expire\s*$/i) {
68 #print STDERR "*** DEBUG FreeBSD 9.x\n";
69 $freebsd_version = '9.x';
70 next;
71 }
72  
73 my @toks = split(/\s+/, $line);
74  
75 my ($route, $gateway, $flags, $refs, $use, $interface, $expire);
76  
77 if ($freebsd_version eq '9.x') {
78 $route = $toks[0];
79 $gateway = $toks[1];
80 $flags = $toks[2];
81 $refs = $toks[3];
82 $use = $toks[4];
83 $interface = $toks[5];
84 $expire = $toks[6];
85 }
86 else { # Default to FreeBSD 10.x
87 $route = $toks[0];
88 $gateway = $toks[1];
89 $flags = $toks[2];
90 $interface = $toks[3];
91 $expire = $toks[4];
92 }
93  
94 if (defined($route) && defined($gateway) && defined($interface)) {
95 #print STDERR "*** DEBUG $route $gateway $interface\n";
96  
97 # Convert FreeBSD strings to universal IP addresses
98 if ($route eq 'default') {
99 $route = '0.0.0.0/0';
100 }
101 if ($gateway =~ /^link/) {
102 $gateway = '0.0.0.0';
103 }
104  
105 # Special case: an entry with a MAC address means a direct route
106 #if ($gateway =~ /^[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}$/i) {
107 #my %route = (
108 #route => "$route/32",
109 #gateway => '0.0.0.0',
110 #interface => $interface,
111 #);
112 #my $id = $self->_to_psv(\%route);
113 #if (! exists($cache{$id})) {
114 #push @routes, \%route;
115 #$cache{$id}++;
116 #}
117 #}
118  
119 # A first sanity check to help Net::IPv4Addr
120 if ($gateway !~ m{^[0-9\.]+$} || $route !~ m{^[0-9\.]+(?:/\d+)?$}) {
121 next;
122 }
123  
124 # Normalize IP addresses
125 $route = Net::CIDR::range2cidr($route); # 127.16 => 172.16/16
126 $route = Net::CIDR::cidrvalidate($route); # 172.16/16 => 172.16.0.0/16
127  
128 eval {
129 my ($ip1, $cidr1) = Net::IPv4Addr::ipv4_parse($route);
130 my ($ip2, $cidr2) = Net::IPv4Addr::ipv4_parse($gateway);
131 };
132 if ($@) {
133 #chomp($@);
134 #print "*** DEBUG[$@]\n";
135 next; # Not a valid line for us.
136 }
137  
138 # Ok, proceed.
139 my %route = (
140 route => $route,
141 gateway => $gateway,
142 interface => $interface,
143 );
144  
145 # Default route
146 if ($route eq '0.0.0.0/0') {
147 $route{default} = 1;
148 $route{route} = NR_DEFAULT_ROUTE4();
149 }
150  
151 # Local subnet
152 if ($gateway eq '0.0.0.0') {
153 $route{local} = 1;
154 $route{gateway} = NR_LOCAL_ROUTE4();
155 }
156  
157 if ($route{route} !~ /\/\d+$/) {
158 $route{route} .= '/32';
159 }
160  
161 my $id = $self->_to_psv(\%route);
162 if (! exists($cache{$id})) {
163 #print STDERR "*** DEBUG new $id\n";
164 push @routes, \%route;
165 $cache{$id}++;
166 }
167 }
168 }
169  
170 return \@routes;
171 }
172  
173 sub _get_inet6 {
174 my $self = shift;
175 my ($lines) = @_;
176  
177 my @routes = ();
178 my %cache = ();
179  
180 # FreeBSD 9.3-RELEASE
181 # Internet6:
182 # Destination Gateway Flags Netif Expire
183 # ::/96 ::1 UGRS lo0 =>
184 # default 2003:1122:1:ffff:ff:ff:ff:ff UGS em0
185 # ::1 link#5 UH lo0
186  
187 # FreeBSD 10.1-RELEASE
188 # Internet6:
189 # Destination Gateway Flags Netif Expire
190 # ::/96 ::1 UGRS lo0
191 # ::1 link#2 UH lo0
192 # ::ffff:0.0.0.0/96 ::1 UGRS lo0
193 # 2003:1122:2:1a00::/56 link#1 U re0
194  
195 for my $line (@$lines) {
196 my @toks = split(/\s+/, $line);
197  
198 my $route = $toks[0];
199 my $gateway = $toks[1];
200 my $flag = $toks[2];
201 my $interface = $toks[3];
202 my $expire = $toks[4];
203  
204 if (defined($route) && defined($gateway) && defined($interface)) {
205 # Convert FreeBSD strings to universal IP addresses
206 if ($gateway =~ /^link/ || $gateway eq '::1') {
207 $gateway = '::';
208 }
209 if ($route eq 'default') {
210 $route = '::/0';
211 }
212 # Strip interface name from route
213 $route =~ s/%[a-z]+\d+//g;
214  
215 # Special case: an entry with a MAC address means a default gateway
216 if ($gateway =~ /^[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}$/i) {
217 my %route = (
218 route => NR_DEFAULT_ROUTE6(),
219 gateway => $route,
220 interface => $interface,
221 );
222 my $id = $self->_to_psv(\%route);
223 if (! exists($cache{$id})) {
224 push @routes, \%route;
225 $cache{$id}++;
226 }
227 }
228  
229 # A first sanity check to help Net::IPv6Addr
230 if ($route !~ m{^[0-9a-f:/]+$}i || $gateway !~ m{^[0-9a-f:/]+$}i) {
231 next;
232 }
233  
234 #print STDERR "*** DEBUG $route $gateway $interface\n";
235  
236 eval {
237 #print "*** DEBUG $route $gateway\n";
238 my $ip1 = Net::IPv6Addr::ipv6_parse($route);
239 my $ip2 = Net::IPv6Addr::ipv6_parse($gateway);
240 };
241 if ($@) {
242 #chomp($@);
243 #print "*** DEBUG[$@]\n";
244 next; # Not a valid line for us.
245 }
246  
247 # Ok, proceed.
248 my %route = (
249 route => $route,
250 gateway => $gateway,
251 interface => $interface,
252 );
253  
254 # Default route
255 if ($route eq '::/0') {
256 $route{default} = 1;
257 $route{route} = NR_DEFAULT_ROUTE6();
258 }
259  
260 # Local subnet
261 if ($gateway eq '::') {
262 $route{local} = 1;
263 $route{gateway} = NR_LOCAL_ROUTE6();
264 }
265  
266 if ($route{route} !~ /\/\d+$/) {
267 $route{route} .= '/128';
268 }
269  
270 my $id = $self->_to_psv(\%route);
271 if (! exists($cache{$id})) {
272 push @routes, \%route;
273 $cache{$id}++;
274 }
275 }
276 }
277  
278 return \@routes;
279 }
280  
281 1;
282  
283 __END__
« Routing/Darwin.pm ↑ Summary Routing/Linux.pm »