blib/lib/OSPF/LSDB/ospfd.pm

filter: Statementstmt 94.6% 229 / 242 Branchbran 92.5% 211 / 228 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 94.1% 16 / 17 Podpod 18.1% 2 / 11 totaltotal 92.1% 468 / 508 CC 125 SCAR 32.5
CC
125
Cov
94%
ospfctl_show
56.0
parse_router
42.2
parse_network
23.0
CRAP 25.8
« blib/lib/OSPF/LSDB/ospf6d.pm ↑ Summary blib/lib/OSPF/LSDB/View.pm »
1 ##########################################################################
2 # Copyright (c) 2010-2021 Alexander Bluhm <alexander.bluhm@gmx.net>
3 #
4 # Permission to use, copy, modify, and distribute this software for any
5 # purpose with or without fee is hereby granted, provided that the above
6 # copyright notice and this permission notice appear in all copies.
7 #
8 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ##########################################################################
16  
17 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =pod
21  
22 =head1 NAME
23  
24 OSPF::LSDB::ospfd - parse OpenBSD B<ospfd> link state database
25  
26 =head1 SYNOPSIS
27  
28 use OSPF::LSDB::ospfd;
29  
30 my $ospfd = OSPF::LSDB::ospfd-E<gt>new();
31  
32 my $ospfd = OSPF::LSDB::ospfd-E<gt>new(ssh => "user@host");
33  
34 $ospfd-E<gt>parse(%files);
35  
36 =head1 DESCRIPTION
37  
38 The OSPF::LSDB::ospfd module parses the output of OpenBSD B<ospfctl>
39 and fills the L<OSPF::LSDB> base object.
40 The output of
41 C<show summary>,
42 C<show database router>,
43 C<show database network>,
44 C<show database summary>,
45 C<show database asbr>,
46 C<show database external>
47 is needed.
48 It can be given as separate files or obtained dynamically.
49 In the latter case B<doas> is invoked if permissions are not
50 sufficient to run B<ospfctl>.
51 If the object has been created with the C<ssh> argument, the specified
52 user and host are used to login and run B<ospfctl> there.
53  
54 There is only one public method:
55  
56 =cut
57  
58 package OSPF::LSDB::ospfd;
59 18 ▶use base 'OSPF::LSDB';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 18 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 18 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 18 use fields qw(
63 ospfd ospfctl ospfsock showdb
64 selfid
65 router network summary boundary external
66 18 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 sub new {
69 16 ▶ my OSPF::LSDB::ospfd $self = OSPF::LSDB::new(@_);
Subroutine new: called CC=1 SCAR=0.0
Pod documented
70 16 $self->{ospfd} = "ospfd";
71 16 $self->{ospfctl} = "ospfctl";
72 # XXX hard coded routing domain 0
73 16 $self->{ospfsock} = "/var/run/ospfd.sock.0";
74 $self->{showdb} = {
75 16 router => "router",
76 network => "network",
77 summary => "summary",
78 boundary => "asbr",
79 external => "external",
80 };
81 16 return $self;
82 }
83  
84 # shortcut
85 18 my $IP = qr/$RE{net}{IPv4}{-keep}/;
86  
87 sub ospfctl_show {
88 0 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine ospfctl_show: not called CC=7 SCAR=40.3
Pod undocumented
89 0 my @cmd = ($self->{ospfctl}, "show", @_);
90 0 ▶ if ($self->{ssh}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'ssh'}) { } 0 0 0
elsif (-S $self->{'ospfsock'}) { } 0 0 0
91 0 unshift @cmd, "ssh", $self->{ssh};
92 } elsif (-S $self->{ospfsock}) {
93 # no doas if user is root or in wheel group
94 # srw-rw---- 1 root wheel 0 Jun 13 10:10 /var/run/ospfd.sock
95 0 ▶ unshift @cmd, "doas" unless -w $self->{ospfsock};
Branch: 1 branch
BranchTrueFalseTotal
unless -w $self->{"ospfsock"} 0 0 0
96 } else {
97 0 die "Control socket '$self->{ospfsock}' for $self->{ospfd} missing.\n";
98 }
99 0 ▶ my @lines = wantarray ? `@cmd` : scalar `@cmd`;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
100 0 ▶ die "Command '@cmd' failed: $?\n" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
101 0 ▶ return wantarray ? @lines : $lines[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
102 }
103  
104 sub read_files {
105 4 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine read_files: called CC=4 SCAR=14.0
Pod undocumented
106 4 my %files = @_;
107 4 my $file = $files{selfid};
108 4 ▶ my @lines = $file ? read_file($file) : $self->ospfctl_show("summary");
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 4 0 4
109 4 $self->{selfid} = \@lines;
110 4 foreach (sort keys %{$self->{showdb}}) {
111 24 my $file = $files{$_};
112 my @lines = $file ? read_file($file) :
113 24 ▶ $self->ospfctl_show("database", $self->{showdb}{$_});
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 24 0 24
114 24 $self->{$_} = \@lines;
115 }
116 }
117  
118 sub parse_self {
119 6 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_self: called CC=4 SCAR=13.9
Pod undocumented
120 6 my($routerid, @areas);
121 6 foreach (@{$self->{selfid}}) {
122 99 ▶ if (/^Router ID: $IP$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^Router ID: $IP$/) { } 6 93 99
elsif (/^Area ID: $IP$/) { } 10 83 93
123 6 $routerid = $1;
124 } elsif (/^Area ID: $IP$/) {
125 10 push @areas, $1;
126 }
127 }
128 6 $self->{ospf}{self} = { routerid => $routerid, areas => \@areas };
129 }
130  
131 sub parse_router {
132 16 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_router: called CC=42 SCAR=37.4
Pod undocumented
133 16 my($area, $router, $link) = ("", "", "");
134 16 my(@routers, $lnum, $type, $r, $l);
135 16 foreach (@{$self->{router}}) {
136 1150 ▶ if (/^ +Router Link States \(Area $IP\)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^ +Router Link States \(Area $IP\)$/) { } 22 1128 1150
elsif (/^$/) { } 193 935 1128
elsif (not $r and /^\w/) { } 53 882 935
elsif (not $l and /^ {4}\w/) { } 110 1093 1203
137 22 ▶ die "$_ Link $link of router $router in area $area not finished.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $l 1 21 22
138 if $l;
139 21 ▶ die "$_ Too few links at router $router in area $area.\n" if $lnum;
Branch: 1 branch
BranchTrueFalseTotal
if $lnum 1 20 21
140 20 ▶ die "$_ Router $router in area $area not finished.\n" if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 1 19 20
141 19 $area = $1;
142 19 next;
143 } elsif (/^$/) {
144 193 ▶ if ($l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l) 105 88 193
145 105 ▶ die "Link $link of router $router in area $area without type.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 104 105
146 if ! $type;
147 104 push @{$r->{$type.'s'}}, $l;
148 104 undef $type;
149 104 undef $l;
150 }
151 192 ▶ if (! $lnum) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lnum) 81 111 192
152 81 undef $r;
153 }
154 192 next;
155 ▶ } elsif (! $r && /^\w/) {
Condition100%3/3
not $r and /^\w/
!ll&&!rl&&r
881153
MC/DC100%2/2
not $r and /^\w/
not $r /^\w/
Truth table100%3/3
not(A) and B
A= not $rB= /^\w/
ABresult
0X0
100
111
156 53 ▶ die "$_ No area for router defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 52 53
157 52 $router = "";
158 52 $r = { area => $area };
159 52 push @routers, $r;
160 ▶ } elsif (! $l && /^ {4}\w/) {
Condition100%3/3
not $l and /^ {4}\w/
!ll&&!rl&&r
321451110
MC/DC100%2/2
not $l and /^ {4}\w/
not $l /^ {4}\w/
Truth table100%3/3
not(A) and B
A= not $lB= /^ {4}\w/
ABresult
0X0
100
111
161 110 ▶ die "$_ Too many links at router $router in area $area.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $lnum 1 109 110
162 if ! $lnum;
163 109 $lnum--;
164 109 $link = "";
165 109 $l = {};
166 }
167 933 ▶ if (/^LS age: $RE{num}{int}{-keep}$/) {
Branch: 16 branches
BranchTrueFalseTotal
if (/^LS age: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 52 881 933
elsif (/^LS Type: ([\w ()]+)$/) { } 52 829 881
elsif (/^Link State ID: $IP$/) { } 51 778 829
elsif (/^Advertising Router: $IP$/) { } 51 727 778
elsif (/^LS Seq Number: (0x$OSPF::LSDB::ospfd::RE{'num'}{'hex'})$/) { } 51 676 727
elsif (/^Flags: ([-*|\w]*)$/) { } 48 628 676
elsif (/^Number of Links: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 48 580 628
elsif (/^ Link connected to: ([\w -]+)$/) { } 108 472 580
elsif (/^ Link ID \(Neighbors Router ID\): $IP$/) { } 0 472 472
elsif (/^ Link ID \(Designated Router address\): $IP$/) { } 79 393 472
elsif (/^ Link ID \(Network ID\): $IP$/) { } 29 364 393
elsif (/^ Link Data \(Router Interface address\): $IP$/) { } 79 285 364
elsif (/^ Link Data \(Network Mask\): $IP$/) { } 29 256 285
elsif (/^ Metric: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 105 151 256
elsif (/^ {4}\w/) { } 1 150 151
elsif (not /^(Options|Checksum|Length):/) { } 1 149 150
168 52 $r->{age} = $1;
169 } elsif (/^LS Type: ([\w ()]+)$/) {
170 52 ▶ die "$_ Type of router-LSA is $1 and not Router in area $area.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "Router" 1 51 52
171 if $1 ne "Router";
172 } elsif (/^Link State ID: $IP$/) {
173 51 $r->{router} = $1;
174 51 $router = $1;
175 } elsif (/^Advertising Router: $IP$/) {
176 51 $r->{routerid} = $1;
177 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
178 51 $r->{sequence} = $1;
179 } elsif (/^Flags: ([-*|\w]*)$/) {
180 48 my $flags = $1;
181 48 foreach (qw(V E B)) {
182 144 ▶ $r->{bits}{$_} = $flags =~ /\b$_\b/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags =~ /\b$_\b/ ? : 42 102 144
183 }
184 } elsif (/^Number of Links: $RE{num}{int}{-keep}$/) {
185 48 $lnum = $1;
186 } elsif (/^ Link connected to: ([\w -]+)$/) {
187 108 ▶ if ($1 eq "Point-to-Point") {
Branch: 4 branches
BranchTrueFalseTotal
if ($1 eq 'Point-to-Point') { } 0 108 108
elsif ($1 eq 'Transit Network') { } 79 29 108
elsif ($1 eq 'Stub Network') { } 28 1 29
elsif ($1 eq 'Virtual Link') { } 0 1 1
188 0 $type = "pointtopoint";
189 } elsif ($1 eq "Transit Network") {
190 79 $type = "transit";
191 } elsif ($1 eq "Stub Network") {
192 28 $type = "stub";
193 } elsif ($1 eq "Virtual Link") {
194 0 $type = "virtual";
195 } else {
196 1 die "$_ Unknown link type $1 at router $router ".
197 "in area $area.\n";
198 }
199 } elsif (/^ Link ID \(Neighbors Router ID\): $IP$/) {
200 0 $l->{routerid} = $1;
201 0 $link = $1;
202 } elsif (/^ Link ID \(Designated Router address\): $IP$/) {
203 79 $l->{address} = $1;
204 79 $link = $1;
205 } elsif (/^ Link ID \(Network ID\): $IP$/) {
206 29 $l->{network} = $1;
207 29 $link = $1;
208 } elsif (/^ Link Data \(Router Interface address\): $IP$/) {
209 79 $l->{interface} = $1;
210 } elsif (/^ Link Data \(Network Mask\): $IP$/) {
211 29 $l->{netmask} = $1;
212 } elsif (/^ Metric: $RE{num}{int}{-keep}$/) {
213 105 $l->{metric} = $1;
214 } elsif (/^ {4}\w/) {
215 1 die "$_ Unknown line at link $link of router $router ".
216 "in area $area.\n";
217 } elsif (! /^(Options|Checksum|Length):/) {
218 1 die "$_ Unknown line at router $router in area $area.\n";
219 }
220 }
221 6 ▶ die "Link $link of router $router in area $area not finished.\n" if $l;
Branch: 1 branch
BranchTrueFalseTotal
if $l 1 5 6
222 5 ▶ die "Too few links at router $router in area $area.\n" if $lnum;
Branch: 1 branch
BranchTrueFalseTotal
if $lnum 1 4 5
223 4 ▶ die "Router $router in area $area not finished.\n" if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 1 3 4
224 3 $self->{ospf}{database}{routers} = \@routers;
225 }
226  
227 sub parse_network {
228 11 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_network: called CC=23 SCAR=31.4
Pod undocumented
229 11 my($area, $network) = ("", "");
230 11 my(@networks, $attachments, $rnum, $n);
231 11 foreach (@{$self->{network}}) {
232 518 ▶ if (/^ +Net Link States \(Area $IP\)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^ +Net Link States \(Area $IP\)$/) { } 16 502 518
elsif (/^$/) { } 64 438 502
elsif (not $n) { } 41 397 438
233 16 ▶ die "$_ Attached routers of network $network in area $area ".
Branch: 1 branch
BranchTrueFalseTotal
if $attachments 1 15 16
234 "not finished.\n" if $attachments;
235 15 ▶ die "$_ Network $network in area $area not finished.\n" if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 1 14 15
236 14 $area = $1;
237 14 next;
238 } elsif (/^$/) {
239 64 ▶ die "$_ Too few attached routers at network $network ".
Branch: 1 branch
BranchTrueFalseTotal
if $rnum 1 63 64
240 "in area $area.\n" if $rnum;
241 63 undef $attachments;
242 63 undef $n;
243 63 next;
244 } elsif (! $n) {
245 41 ▶ die "$_ No area for network defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 40 41
246 40 $network = "";
247 40 $n = { area => $area };
248 40 push @networks, $n;
249 }
250 437 ▶ if (/^LS age: $RE{num}{int}{-keep}$/) {
Branch: 9 branches
BranchTrueFalseTotal
if (/^LS age: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 40 397 437
elsif (/^LS Type: ([\w ()]+)$/) { } 40 357 397
elsif (/^Link State ID: $IP \(address of Designated Router\)$/) { } 39 318 357
elsif (/^Advertising Router: $IP$/) { } 39 279 318
elsif (/^LS Seq Number: (0x$OSPF::LSDB::ospfd::RE{'num'}{'hex'})$/) { } 39 240 279
elsif (/^Network Mask: $IP$/) { } 36 204 240
elsif (/^Number of Routers: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 4 200 204
elsif (/^ Attached Router: $IP$/) { } 86 114 200
elsif (not /^(Options|Checksum|Length):/) { } 1 113 114
251 40 $n->{age} = $1;
252 } elsif (/^LS Type: ([\w ()]+)$/) {
253 40 ▶ die "$_ Type of network-LSA is $1 and not Network in area $area.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "Network" 1 39 40
254 if $1 ne "Network";
255 } elsif (/^Link State ID: $IP \(address of Designated Router\)$/) {
256 39 $n->{address} = $1;
257 39 $network = $1;
258 } elsif (/^Advertising Router: $IP$/) {
259 39 $n->{routerid} = $1;
260 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
261 39 $n->{sequence} = $1;
262 } elsif (/^Network Mask: $IP$/) {
263 36 $n->{netmask} = $1;
264 } elsif (/^Number of Routers: $RE{num}{int}{-keep}$/) {
265 4 $rnum = $1;
266 } elsif (/^ Attached Router: $IP$/) {
267 86 ▶ if (! $attachments) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attachments) 36 50 86
268 36 $attachments = [];
269 36 $n->{attachments} = $attachments;
270 }
271 86 ▶ $rnum-- if defined $rnum ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rnum 6 80 86
272 86 push @$attachments, { routerid => $1 };
273 } elsif (! /^(Options|Checksum|Length):/) {
274 1 die "$_ Unknown line at network $network in area $area.\n";
275 }
276 }
277 5 ▶ die "Attached routers of network $network in area $area not finished.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $attachments 1 4 5
278 if $attachments;
279 4 ▶ die "Network $network in area $area not finished.\n" if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 1 3 4
280 3 $self->{ospf}{database}{networks} = \@networks;
281 }
282  
283 sub parse_summary {
284 8 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_summary: called CC=17 SCAR=28.3
Pod undocumented
285 8 my($area, $summary) = ("", "");
286 8 my(@summarys, $s);
287 8 foreach (@{$self->{summary}}) {
288 1391 ▶ if (/^ +Summary Net Link States \(Area $IP\)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^ +Summary Net Link States \(Area $IP\)$/) { } 12 1379 1391
elsif (/^$/) { } 145 1234 1379
elsif (not $s) { } 126 1108 1234
289 12 ▶ die "$_ Summary $summary in area $area not finished.\n" if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 1 11 12
290 11 $area = $1;
291 11 next;
292 } elsif (/^$/) {
293 145 undef $s;
294 145 next;
295 } elsif (! $s) {
296 126 ▶ die "$_ No area for summary defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 125 126
297 125 $summary = "";
298 125 $s = { area => $area };
299 125 push @summarys, $s;
300 }
301 1233 ▶ if (/^LS age: $RE{num}{int}{-keep}$/) {
Branch: 8 branches
BranchTrueFalseTotal
if (/^LS age: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 125 1108 1233
elsif (/^LS Type: ([\w ()]+)$/) { } 125 983 1108
elsif (/^Link State ID: $IP \(Network ID\)$/) { } 124 859 983
elsif (/^Advertising Router: $IP$/) { } 124 735 859
elsif (/^LS Seq Number: (0x$OSPF::LSDB::ospfd::RE{'num'}{'hex'})$/) { } 124 611 735
elsif (/^Network Mask: $IP$/) { } 121 490 611
elsif (/^Metric: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 121 369 490
elsif (not /^(Options|Checksum|Length):/) { } 1 368 369
302 125 $s->{age} = $1;
303 } elsif (/^LS Type: ([\w ()]+)$/) {
304 125 ▶ die "$_ Type of summary-LSA is $1 and not Summary (Network) ".
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "Summary (Network)" 1 124 125
305 "in area $area.\n" if $1 ne "Summary (Network)";
306 } elsif (/^Link State ID: $IP \(Network ID\)$/) {
307 124 $s->{address} = $1;
308 124 $summary = $1;
309 } elsif (/^Advertising Router: $IP$/) {
310 124 $s->{routerid} = $1;
311 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
312 124 $s->{sequence} = $1;
313 } elsif (/^Network Mask: $IP$/) {
314 121 $s->{netmask} = $1;
315 } elsif (/^Metric: $RE{num}{int}{-keep}$/) {
316 121 $s->{metric} = $1;
317 } elsif (! /^(Options|Checksum|Length):/) {
318 1 die "$_ Unknown line at summary $summary in area $area.\n";
319 }
320 }
321 4 ▶ die "Summary $summary in area $area not finished.\n" if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 1 3 4
322 3 $self->{ospf}{database}{summarys} = \@summarys;
323 }
324  
325 sub parse_boundary {
326 8 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_boundary: called CC=16 SCAR=27.7
Pod undocumented
327 8 my($area, $boundary) = ("", "");
328 8 my(@boundarys, $b);
329 8 foreach (@{$self->{boundary}}) {
330 731 ▶ if (/^ +Summary Router Link States \(Area $IP\)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^ +Summary Router Link States \(Area $IP\)$/) { } 12 719 731
elsif (/^$/) { } 85 634 719
elsif (not $b) { } 66 568 634
331 12 ▶ die "$_ Boundary $boundary in area $area not finished.\n" if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 1 11 12
332 11 $area = $1;
333 11 next;
334 } elsif (/^$/) {
335 85 undef $b;
336 85 next;
337 } elsif (! $b) {
338 66 ▶ die "$_ No area for boundary defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 65 66
339 65 $boundary = "";
340 65 $b = { area => $area };
341 65 push @boundarys, $b;
342 }
343 633 ▶ if (/^LS age: $RE{num}{int}{-keep}$/) {
Branch: 7 branches
BranchTrueFalseTotal
if (/^LS age: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 65 568 633
elsif (/^LS Type: ([\w ()]+)$/) { } 65 503 568
elsif (/^Link State ID: $IP \(ASBR Router ID\)$/) { } 64 439 503
elsif (/^Advertising Router: $IP$/) { } 64 375 439
elsif (/^LS Seq Number: (0x$OSPF::LSDB::ospfd::RE{'num'}{'hex'})$/) { } 64 311 375
elsif (/^Metric: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 61 250 311
elsif (not /^(Options|Checksum|Length|Network Mask):/) { } 1 249 250
344 65 $b->{age} = $1;
345 } elsif (/^LS Type: ([\w ()]+)$/) {
346 65 ▶ die "$_ Type of boundary-LSA is $1 and not Summary (Router) ".
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "Summary (Router)" 1 64 65
347 "in area $area.\n" if $1 ne "Summary (Router)";
348 } elsif (/^Link State ID: $IP \(ASBR Router ID\)$/) {
349 64 $b->{asbrouter} = $1;
350 64 $boundary = $1;
351 } elsif (/^Advertising Router: $IP$/) {
352 64 $b->{routerid} = $1;
353 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
354 64 $b->{sequence} = $1;
355 } elsif (/^Metric: $RE{num}{int}{-keep}$/) {
356 61 $b->{metric} = $1;
357 } elsif (! /^(Options|Checksum|Length|Network Mask):/) {
358 1 die "$_ Unknown line at boundary $boundary in area $area.\n";
359 }
360 }
361 4 ▶ die "Boundary $boundary in area $area not finished.\n" if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 1 3 4
362 3 $self->{ospf}{database}{boundarys} = \@boundarys;
363 }
364  
365 sub parse_external {
366 8 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_external: called CC=19 SCAR=29.4
Pod undocumented
367 8 my $external = "";
368 8 my(@externals, $e);
369 8 foreach (@{$self->{external}}) {
370 1981 ▶ if (/^ +Type-5 AS External Link States$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^ +Type-5 AS External Link States$/) { } 10 1971 1981
elsif (/^$/) { } 154 1817 1971
elsif (not $e) { } 142 1675 1817
371 10 ▶ die "$_ External $external not finished.\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 1 9 10
372 9 ▶ die "$_ Too many external sections.\n", if @externals;
Branch: 1 branch
BranchTrueFalseTotal
if @externals 1 8 9
373 8 next;
374 } elsif (/^$/) {
375 154 undef $e;
376 154 next;
377 } elsif (! $e) {
378 142 $external = "";
379 142 $e = {};
380 142 push @externals, $e;
381 }
382 1817 ▶ if (/^LS age: $RE{num}{int}{-keep}$/) {
Branch: 10 branches
BranchTrueFalseTotal
if (/^LS age: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 142 1675 1817
elsif (/^LS Type: ([\w ()]+)$/) { } 142 1533 1675
elsif (/^Link State ID: $IP \(External Network Number\)$/) { } 141 1392 1533
elsif (/^Advertising Router: $IP$/) { } 141 1251 1392
elsif (/^LS Seq Number: (0x$OSPF::LSDB::ospfd::RE{'num'}{'hex'})$/) { } 141 1110 1251
elsif (/^Network Mask: $IP$/) { } 138 972 1110
elsif (/^ Metric type: ([1-2])$/) { } 138 834 972
elsif (/^ Metric: $OSPF::LSDB::ospfd::RE{'num'}{'int'}{'-keep'}$/) { } 138 696 834
elsif (/^ Forwarding Address: $IP$/) { } 138 558 696
elsif (not /^(Options|Checksum|Length| External Route Tag):/) { } 1 557 558
383 142 $e->{age} = $1;
384 } elsif (/^LS Type: ([\w ()]+)$/) {
385 142 ▶ die "$_ Type of external-LSA is $1 and not AS External.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "AS External" 1 141 142
386 if $1 ne "AS External";
387 } elsif (/^Link State ID: $IP \(External Network Number\)$/) {
388 141 $e->{address} = $1;
389 141 $external = $1;
390 } elsif (/^Advertising Router: $IP$/) {
391 141 $e->{routerid} = $1;
392 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
393 141 $e->{sequence} = $1;
394 } elsif (/^Network Mask: $IP$/) {
395 138 $e->{netmask} = $1;
396 } elsif (/^ Metric type: ([1-2])$/) {
397 138 $e->{type} = $1;
398 } elsif (/^ Metric: $RE{num}{int}{-keep}$/) {
399 138 $e->{metric} = $1;
400 } elsif (/^ Forwarding Address: $IP$/) {
401 138 $e->{forward} = $1;
402 } elsif (! /^(Options|Checksum|Length| External Route Tag):/) {
403 1 die "$_ Unknown line at external $external.\n";
404 }
405 }
406 4 ▶ die "External $external not finished.\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 1 3 4
407 3 $self->{ospf}{database}{externals} = \@externals;
408 }
409  
410 sub parse_lsdb {
411 4 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_lsdb: called CC=1 SCAR=0.0
Pod undocumented
412 4 $self->parse_router();
413 4 $self->parse_network();
414 4 $self->parse_summary();
415 4 $self->parse_boundary();
416 4 $self->parse_external();
417 }
418  
419 =pod
420  
421 =over 4
422  
423 =item $self-E<gt>parse(%files)
424  
425 This function takes a hash with file names as value containing the
426 B<ospfctl> output data.
427 The hash keys are named C<selfid>, C<router>, C<network>, C<summary>,
428 C<boundary>, C<external>.
429 If a hash entry is missing, B<ospfctl> is run instead to obtain the
430 information dynamically.
431  
432 The complete OSPF link state database is stored in the B<ospf> field
433 of the base class.
434  
435 =back
436  
437 =cut
438  
439 sub parse {
440 4 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
441 4 my %files = @_;
442 4 $self->read_files(%files);
443 4 $self->parse_self();
444 4 $self->parse_lsdb();
445 4 $self->{ospf}{ipv6} = 0;
446 }
447  
448 =pod
449  
450 This module has been tested with OpenBSD 4.8 and 5.1.
451 If it works with other versions is unknown.
452  
453 =head1 ERRORS
454  
455 The methods die if any error occurs.
456  
457 =head1 SEE ALSO
458  
459 L<OSPF::LSDB>,
460 L<OSPF::LSDB::ospf6d>
461  
462 L<ospfd2yaml>
463  
464 =head1 AUTHORS
465  
466 Alexander Bluhm
467  
468 =cut
469  
470 18 1;
« blib/lib/OSPF/LSDB/ospf6d.pm ↑ Summary blib/lib/OSPF/LSDB/View.pm »