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