Routing/Linux.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/FreeBSD.pm ↑ Summary Routing/NetBSD.pm »
1 #
2 # $Id: Linux.pm,v 717225574cff 2015/11/12 08:46:57 gomor $
3 #
4 package Net::Routing::Linux;
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.44';
9  
10 6 ▶use base qw(Net::Routing);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use IPC::Run3;
13 use Net::IPv4Addr;
14 use Net::IPv6Addr;
15 use Net::Routing qw($Error :constants);
16  
17 sub new {
18 my $self = shift->SUPER::new(
19 @_,
20 ) or return;
21  
22 if (! defined($self->path)) {
23 $Error = "you must give a `path' attribute";
24 return;
25 }
26  
27 my $family = $self->family;
28 if (! defined($family)) {
29 $Error = "you must give a `family' attribute";
30 return;
31 }
32 else {
33 if ($family ne NR_FAMILY_INET4() && $family ne NR_FAMILY_INET6()) {
34 $Error = "family not supported [$family]: use either NR_FAMILY_INET4() or NR_FAMILY_INET6()";
35 return;
36 }
37 }
38  
39 return $self;
40 }
41  
42 sub get {
43 my $self = shift;
44 my ($cmd4, $cmd6) = @_;
45  
46 my $path = $self->path;
47 my $family = $self->family;
48  
49 my $bin = '';
50 {
51 local $ENV{LC_ALL} = $self->lc_all;
52  
53 for my $path (@{$self->path}) {
54 if (-f "$path/netstat") {
55 $bin = "$path/netstat";
56 last;
57 }
58 }
59 if (! length($bin)) {
60 $Error = "unable to find netstat command from current PATH";
61 return;
62 }
63 };
64  
65 $cmd4 ||= [ $bin, '-rnA', 'inet' ];
66 $cmd6 ||= [ $bin, '-rnA', 'inet6' ];
67  
68 my $cmd = [];
69 if ($family eq NR_FAMILY_INET4()) {
70 $cmd = $cmd4;
71 }
72 # If not NR_FAMILY_INET4(), it must be NR_FAMILY_INET6() because we validated family at new()
73 else {
74 $cmd = $cmd6;
75 }
76  
77 my $out;
78 my $err;
79 eval {
80 run3($cmd, undef, \$out, \$err);
81 };
82 # Error in executing run3()
83 if ($@) {
84 chomp($@);
85 $Error = "unable to execute command [".join(' ', @$cmd)."]: $@";
86 return;
87 }
88 # Error in command execution
89 elsif ($?) {
90 chomp($err);
91 $Error = "command execution failed [".join(' ', @$cmd)."]: $err";
92 return;
93 }
94  
95 my $routes = [];
96  
97 my @lines = split(/\n/, $out);
98 if ($family eq NR_FAMILY_INET4()) {
99 $routes = $self->_get_inet4(\@lines);
100 }
101 # If not NR_FAMILY_INET4(), it must be NR_FAMILY_INET6() because we validated family at new()
102 else {
103 $routes = $self->_get_inet6(\@lines);
104 }
105  
106 return $routes;
107 }
108  
109 sub _get_inet4 {
110 my $self = shift;
111 my ($lines) = @_;
112  
113 my @routes = ();
114 my %cache = ();
115  
116 for my $line (@$lines) {
117 my @toks = split(/\s+/, $line);
118 my $route = $toks[0];
119 my $gateway = $toks[1];
120 my $netmask = $toks[2];
121 my $flags = $toks[3];
122 my $mss = $toks[4];
123 my $window = $toks[5];
124 my $irtt = $toks[6];
125 my $interface = $toks[7];
126  
127 if (defined($route) && defined($gateway) && defined($interface)
128 && defined($netmask)) {
129 # A first sanity check to help Net::IPv4Addr
130 if ($route !~ /^[0-9\.]+$/ || $gateway !~ /^[0-9\.]+$/
131 || $netmask !~ /^[0-9\.]+$/) {
132 next;
133 }
134  
135 eval {
136 my ($ip1, $cidr1) = Net::IPv4Addr::ipv4_parse($route);
137 my ($ip2, $cidr2) = Net::IPv4Addr::ipv4_parse($gateway);
138 my ($ip3, $cidr3) = Net::IPv4Addr::ipv4_parse($netmask);
139 };
140 if ($@) {
141 #chomp($@);
142 #print "*** DEBUG[$@]\n";
143 next; # Not a valid line for us.
144 }
145  
146 # Ok, proceed.
147 my %route = (
148 route => $route,
149 gateway => $gateway,
150 interface => $interface,
151 );
152  
153 # Default route
154 if ($route eq '0.0.0.0' && $netmask eq '0.0.0.0') {
155 $route{default} = 1;
156 $route{route} = NR_DEFAULT_ROUTE4();
157 }
158 else {
159 my ($ip, $cidr) = Net::IPv4Addr::ipv4_parse("$route / $netmask");
160 $route{route} = "$ip/$cidr";
161 }
162  
163 # Local subnet
164 if ($gateway eq '0.0.0.0') {
165 $route{local} = 1;
166 $route{gateway} = NR_LOCAL_ROUTE4();
167 }
168  
169 my $id = $self->_to_psv(\%route);
170 if (! exists($cache{$id})) {
171 push @routes, \%route;
172 $cache{$id}++;
173 }
174 }
175 }
176  
177 return \@routes;
178 }
179  
180 sub _get_inet6 {
181 my $self = shift;
182 my ($lines) = @_;
183  
184 my @routes = ();
185 my %cache = ();
186  
187 for my $line (@$lines) {
188 my @toks = split(/\s+/, $line);
189 my $route = $toks[0];
190 my $gateway = $toks[1];
191 my $flag = $toks[2];
192 my $met = $toks[3];
193 my $ref = $toks[4];
194 my $use = $toks[5];
195 my $interface = $toks[6];
196  
197 if (defined($route) && defined($gateway) && defined($interface)) {
198 # A first sanity check to help Net::IPv6Addr
199 if ($route !~ /^[0-9a-f:\/]+$/i || $gateway !~ /^[0-9a-f:\/]+$/i) {
200 next;
201 }
202  
203 eval {
204 #print "*** DEBUG $route $gateway\n";
205 my $ip1 = Net::IPv6Addr::ipv6_parse($route);
206 my $ip2 = Net::IPv6Addr::ipv6_parse($gateway);
207 };
208 if ($@) {
209 #chomp($@);
210 #print "*** DEBUG[$@]\n";
211 next; # Not a valid line for us.
212 }
213  
214 # Ok, proceed.
215 my %route = (
216 route => $route,
217 gateway => $gateway,
218 interface => $interface,
219 );
220  
221 # Default route
222 if ($route eq '::/0' && $interface ne 'lo') {
223 $route{default} = 1;
224 $route{route} = NR_DEFAULT_ROUTE6();
225 }
226  
227 # Local subnet
228 if ($gateway eq '::') {
229 $route{local} = 1;
230 $route{gateway} = NR_LOCAL_ROUTE6();
231 }
232  
233 my $id = $self->_to_psv(\%route);
234 if (! exists($cache{$id})) {
235 push @routes, \%route;
236 $cache{$id}++;
237 }
238 }
239 }
240  
241 return \@routes;
242 }
243  
244 1;
245  
246 __END__
« Routing/FreeBSD.pm ↑ Summary Routing/NetBSD.pm »