blib/lib/OSPF/LSDB/ospfd.pm

filter: Statementstmt 94.2% 229 / 243 Branchbran 91.7% 211 / 230 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 94.1% 16 / 17 Podpod 18.1% 2 / 11 totaltotal 91.5% 468 / 511 CC 126 SCAR 32.9
CC
126
Cov
93%
ospfctl_show
72.0
parse_router
42.2
parse_network
23.0
CRAP 26.9
« blib/lib/OSPF/LSDB/Cisco.pm ↑ Summary blib/lib/OSPF/LSDB/ospf6d.pm »
1 ##########################################################################
2 # Copyright (c) 2010-2022 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=8 SCAR=42.8
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 ▶ open(my $fh, '-|', @cmd)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @cmd or die "Open pipe from '@{cmd}' failed: $!" 0 0 0
100 or die "Open pipe from '@cmd' failed: $!";
101 0 my @lines = $fh->getlines();
102 0 ▶ close($fh) or die $! ?
Branch: 2 branches
BranchTrueFalseTotal
close $fh or die $! ? "Close pipe from '@{cmd}' failed: $!" : "Command '@{cmd}' failed: $?\n" 0 0 0
$! ? : 0 0 0
103 "Close pipe from '@cmd' failed: $!" :
104 "Command '@cmd' failed: $?\n";
105 0 ▶ return wantarray ? @lines : join("", @lines);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
106 }
107  
108 sub read_files {
109 4 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine read_files: called CC=4 SCAR=14.0
Pod undocumented
110 4 my %files = @_;
111 4 my $file = $files{selfid};
112 4 ▶ my @lines = $file ? read_file($file) : $self->ospfctl_show("summary");
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 4 0 4
113 4 $self->{selfid} = \@lines;
114 4 foreach (sort keys %{$self->{showdb}}) {
115 24 my $file = $files{$_};
116 my @lines = $file ? read_file($file) :
117 24 ▶ $self->ospfctl_show("database", $self->{showdb}{$_});
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 24 0 24
118 24 $self->{$_} = \@lines;
119 }
120 }
121  
122 sub parse_self {
123 6 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_self: called CC=4 SCAR=13.9
Pod undocumented
124 6 my($routerid, @areas);
125 6 foreach (@{$self->{selfid}}) {
126 99 ▶ if (/^Router ID: $IP$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^Router ID: $IP$/) { } 6 93 99
elsif (/^Area ID: $IP$/) { } 10 83 93
127 6 $routerid = $1;
128 } elsif (/^Area ID: $IP$/) {
129 10 push @areas, $1;
130 }
131 }
132 6 $self->{ospf}{self} = { routerid => $routerid, areas => \@areas };
133 }
134  
135 sub parse_router {
136 16 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_router: called CC=42 SCAR=37.4
Pod undocumented
137 16 my($area, $router, $link) = ("", "", "");
138 16 my(@routers, $lnum, $type, $r, $l);
139 16 foreach (@{$self->{router}}) {
140 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
141 22 ▶ die "$_ Link $link of router $router in area $area not finished.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $l 1 21 22
142 if $l;
143 21 ▶ die "$_ Too few links at router $router in area $area.\n" if $lnum;
Branch: 1 branch
BranchTrueFalseTotal
if $lnum 1 20 21
144 20 ▶ die "$_ Router $router in area $area not finished.\n" if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 1 19 20
145 19 $area = $1;
146 19 next;
147 } elsif (/^$/) {
148 193 ▶ if ($l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l) 105 88 193
149 105 ▶ die "Link $link of router $router in area $area without type.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 104 105
150 if ! $type;
151 104 push @{$r->{$type.'s'}}, $l;
152 104 undef $type;
153 104 undef $l;
154 }
155 192 ▶ if (! $lnum) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lnum) 81 111 192
156 81 undef $r;
157 }
158 192 next;
159 ▶ } 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
160 53 ▶ die "$_ No area for router defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 52 53
161 52 $router = "";
162 52 $r = { area => $area };
163 52 push @routers, $r;
164 ▶ } 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
165 110 ▶ die "$_ Too many links at router $router in area $area.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $lnum 1 109 110
166 if ! $lnum;
167 109 $lnum--;
168 109 $link = "";
169 109 $l = {};
170 }
171 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
172 52 $r->{age} = $1;
173 } elsif (/^LS Type: ([\w ()]+)$/) {
174 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
175 if $1 ne "Router";
176 } elsif (/^Link State ID: $IP$/) {
177 51 $r->{router} = $1;
178 51 $router = $1;
179 } elsif (/^Advertising Router: $IP$/) {
180 51 $r->{routerid} = $1;
181 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
182 51 $r->{sequence} = $1;
183 } elsif (/^Flags: ([-*|\w]*)$/) {
184 48 my $flags = $1;
185 48 foreach (qw(V E B)) {
186 144 ▶ $r->{bits}{$_} = $flags =~ /\b$_\b/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags =~ /\b$_\b/ ? : 42 102 144
187 }
188 } elsif (/^Number of Links: $RE{num}{int}{-keep}$/) {
189 48 $lnum = $1;
190 } elsif (/^ Link connected to: ([\w -]+)$/) {
191 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
192 0 $type = "pointtopoint";
193 } elsif ($1 eq "Transit Network") {
194 79 $type = "transit";
195 } elsif ($1 eq "Stub Network") {
196 28 $type = "stub";
197 } elsif ($1 eq "Virtual Link") {
198 0 $type = "virtual";
199 } else {
200 1 die "$_ Unknown link type $1 at router $router ".
201 "in area $area.\n";
202 }
203 } elsif (/^ Link ID \(Neighbors Router ID\): $IP$/) {
204 0 $l->{routerid} = $1;
205 0 $link = $1;
206 } elsif (/^ Link ID \(Designated Router address\): $IP$/) {
207 79 $l->{address} = $1;
208 79 $link = $1;
209 } elsif (/^ Link ID \(Network ID\): $IP$/) {
210 29 $l->{network} = $1;
211 29 $link = $1;
212 } elsif (/^ Link Data \(Router Interface address\): $IP$/) {
213 79 $l->{interface} = $1;
214 } elsif (/^ Link Data \(Network Mask\): $IP$/) {
215 29 $l->{netmask} = $1;
216 } elsif (/^ Metric: $RE{num}{int}{-keep}$/) {
217 105 $l->{metric} = $1;
218 } elsif (/^ {4}\w/) {
219 1 die "$_ Unknown line at link $link of router $router ".
220 "in area $area.\n";
221 } elsif (! /^(Options|Checksum|Length):/) {
222 1 die "$_ Unknown line at router $router in area $area.\n";
223 }
224 }
225 6 ▶ die "Link $link of router $router in area $area not finished.\n" if $l;
Branch: 1 branch
BranchTrueFalseTotal
if $l 1 5 6
226 5 ▶ die "Too few links at router $router in area $area.\n" if $lnum;
Branch: 1 branch
BranchTrueFalseTotal
if $lnum 1 4 5
227 4 ▶ die "Router $router in area $area not finished.\n" if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 1 3 4
228 3 $self->{ospf}{database}{routers} = \@routers;
229 }
230  
231 sub parse_network {
232 11 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_network: called CC=23 SCAR=31.4
Pod undocumented
233 11 my($area, $network) = ("", "");
234 11 my(@networks, $attachments, $rnum, $n);
235 11 foreach (@{$self->{network}}) {
236 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
237 16 ▶ die "$_ Attached routers of network $network in area $area ".
Branch: 1 branch
BranchTrueFalseTotal
if $attachments 1 15 16
238 "not finished.\n" if $attachments;
239 15 ▶ die "$_ Network $network in area $area not finished.\n" if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 1 14 15
240 14 $area = $1;
241 14 next;
242 } elsif (/^$/) {
243 64 ▶ die "$_ Too few attached routers at network $network ".
Branch: 1 branch
BranchTrueFalseTotal
if $rnum 1 63 64
244 "in area $area.\n" if $rnum;
245 63 undef $attachments;
246 63 undef $n;
247 63 next;
248 } elsif (! $n) {
249 41 ▶ die "$_ No area for network defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 40 41
250 40 $network = "";
251 40 $n = { area => $area };
252 40 push @networks, $n;
253 }
254 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
255 40 $n->{age} = $1;
256 } elsif (/^LS Type: ([\w ()]+)$/) {
257 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
258 if $1 ne "Network";
259 } elsif (/^Link State ID: $IP \(address of Designated Router\)$/) {
260 39 $n->{address} = $1;
261 39 $network = $1;
262 } elsif (/^Advertising Router: $IP$/) {
263 39 $n->{routerid} = $1;
264 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
265 39 $n->{sequence} = $1;
266 } elsif (/^Network Mask: $IP$/) {
267 36 $n->{netmask} = $1;
268 } elsif (/^Number of Routers: $RE{num}{int}{-keep}$/) {
269 4 $rnum = $1;
270 } elsif (/^ Attached Router: $IP$/) {
271 86 ▶ if (! $attachments) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attachments) 36 50 86
272 36 $attachments = [];
273 36 $n->{attachments} = $attachments;
274 }
275 86 ▶ $rnum-- if defined $rnum ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rnum 6 80 86
276 86 push @$attachments, { routerid => $1 };
277 } elsif (! /^(Options|Checksum|Length):/) {
278 1 die "$_ Unknown line at network $network in area $area.\n";
279 }
280 }
281 5 ▶ die "Attached routers of network $network in area $area not finished.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $attachments 1 4 5
282 if $attachments;
283 4 ▶ die "Network $network in area $area not finished.\n" if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 1 3 4
284 3 $self->{ospf}{database}{networks} = \@networks;
285 }
286  
287 sub parse_summary {
288 8 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_summary: called CC=17 SCAR=28.3
Pod undocumented
289 8 my($area, $summary) = ("", "");
290 8 my(@summarys, $s);
291 8 foreach (@{$self->{summary}}) {
292 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
293 12 ▶ die "$_ Summary $summary in area $area not finished.\n" if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 1 11 12
294 11 $area = $1;
295 11 next;
296 } elsif (/^$/) {
297 145 undef $s;
298 145 next;
299 } elsif (! $s) {
300 126 ▶ die "$_ No area for summary defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 125 126
301 125 $summary = "";
302 125 $s = { area => $area };
303 125 push @summarys, $s;
304 }
305 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
306 125 $s->{age} = $1;
307 } elsif (/^LS Type: ([\w ()]+)$/) {
308 125 ▶ die "$_ Type of summary-LSA is $1 and not Summary (Network) ".
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "Summary (Network)" 1 124 125
309 "in area $area.\n" if $1 ne "Summary (Network)";
310 } elsif (/^Link State ID: $IP \(Network ID\)$/) {
311 124 $s->{address} = $1;
312 124 $summary = $1;
313 } elsif (/^Advertising Router: $IP$/) {
314 124 $s->{routerid} = $1;
315 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
316 124 $s->{sequence} = $1;
317 } elsif (/^Network Mask: $IP$/) {
318 121 $s->{netmask} = $1;
319 } elsif (/^Metric: $RE{num}{int}{-keep}$/) {
320 121 $s->{metric} = $1;
321 } elsif (! /^(Options|Checksum|Length):/) {
322 1 die "$_ Unknown line at summary $summary in area $area.\n";
323 }
324 }
325 4 ▶ die "Summary $summary in area $area not finished.\n" if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 1 3 4
326 3 $self->{ospf}{database}{summarys} = \@summarys;
327 }
328  
329 sub parse_boundary {
330 8 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_boundary: called CC=16 SCAR=27.7
Pod undocumented
331 8 my($area, $boundary) = ("", "");
332 8 my(@boundarys, $b);
333 8 foreach (@{$self->{boundary}}) {
334 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
335 12 ▶ die "$_ Boundary $boundary in area $area not finished.\n" if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 1 11 12
336 11 $area = $1;
337 11 next;
338 } elsif (/^$/) {
339 85 undef $b;
340 85 next;
341 } elsif (! $b) {
342 66 ▶ die "$_ No area for boundary defined.\n" if ! $area;
Branch: 1 branch
BranchTrueFalseTotal
unless $area 1 65 66
343 65 $boundary = "";
344 65 $b = { area => $area };
345 65 push @boundarys, $b;
346 }
347 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
348 65 $b->{age} = $1;
349 } elsif (/^LS Type: ([\w ()]+)$/) {
350 65 ▶ die "$_ Type of boundary-LSA is $1 and not Summary (Router) ".
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "Summary (Router)" 1 64 65
351 "in area $area.\n" if $1 ne "Summary (Router)";
352 } elsif (/^Link State ID: $IP \(ASBR Router ID\)$/) {
353 64 $b->{asbrouter} = $1;
354 64 $boundary = $1;
355 } elsif (/^Advertising Router: $IP$/) {
356 64 $b->{routerid} = $1;
357 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
358 64 $b->{sequence} = $1;
359 } elsif (/^Metric: $RE{num}{int}{-keep}$/) {
360 61 $b->{metric} = $1;
361 } elsif (! /^(Options|Checksum|Length|Network Mask):/) {
362 1 die "$_ Unknown line at boundary $boundary in area $area.\n";
363 }
364 }
365 4 ▶ die "Boundary $boundary in area $area not finished.\n" if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 1 3 4
366 3 $self->{ospf}{database}{boundarys} = \@boundarys;
367 }
368  
369 sub parse_external {
370 8 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_external: called CC=19 SCAR=29.4
Pod undocumented
371 8 my $external = "";
372 8 my(@externals, $e);
373 8 foreach (@{$self->{external}}) {
374 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
375 10 ▶ die "$_ External $external not finished.\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 1 9 10
376 9 ▶ die "$_ Too many external sections.\n", if @externals;
Branch: 1 branch
BranchTrueFalseTotal
if @externals 1 8 9
377 8 next;
378 } elsif (/^$/) {
379 154 undef $e;
380 154 next;
381 } elsif (! $e) {
382 142 $external = "";
383 142 $e = {};
384 142 push @externals, $e;
385 }
386 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
387 142 $e->{age} = $1;
388 } elsif (/^LS Type: ([\w ()]+)$/) {
389 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
390 if $1 ne "AS External";
391 } elsif (/^Link State ID: $IP \(External Network Number\)$/) {
392 141 $e->{address} = $1;
393 141 $external = $1;
394 } elsif (/^Advertising Router: $IP$/) {
395 141 $e->{routerid} = $1;
396 } elsif (/^LS Seq Number: (0x$RE{num}{hex})$/) {
397 141 $e->{sequence} = $1;
398 } elsif (/^Network Mask: $IP$/) {
399 138 $e->{netmask} = $1;
400 } elsif (/^ Metric type: ([1-2])$/) {
401 138 $e->{type} = $1;
402 } elsif (/^ Metric: $RE{num}{int}{-keep}$/) {
403 138 $e->{metric} = $1;
404 } elsif (/^ Forwarding Address: $IP$/) {
405 138 $e->{forward} = $1;
406 } elsif (! /^(Options|Checksum|Length| External Route Tag):/) {
407 1 die "$_ Unknown line at external $external.\n";
408 }
409 }
410 4 ▶ die "External $external not finished.\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 1 3 4
411 3 $self->{ospf}{database}{externals} = \@externals;
412 }
413  
414 sub parse_lsdb {
415 4 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse_lsdb: called CC=1 SCAR=0.0
Pod undocumented
416 4 $self->parse_router();
417 4 $self->parse_network();
418 4 $self->parse_summary();
419 4 $self->parse_boundary();
420 4 $self->parse_external();
421 }
422  
423 =pod
424  
425 =over 4
426  
427 =item $self-E<gt>parse(%files)
428  
429 This function takes a hash with file names as value containing the
430 B<ospfctl> output data.
431 The hash keys are named C<selfid>, C<router>, C<network>, C<summary>,
432 C<boundary>, C<external>.
433 If a hash entry is missing, B<ospfctl> is run instead to obtain the
434 information dynamically.
435  
436 The complete OSPF link state database is stored in the B<ospf> field
437 of the base class.
438  
439 =back
440  
441 =cut
442  
443 sub parse {
444 4 ▶ my OSPF::LSDB::ospfd $self = shift;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
445 4 my %files = @_;
446 4 $self->read_files(%files);
447 4 $self->parse_self();
448 4 $self->parse_lsdb();
449 4 $self->{ospf}{ipv6} = 0;
450 }
451  
452 =pod
453  
454 This module has been tested with OpenBSD 4.8 and 5.1.
455 If it works with other versions is unknown.
456  
457 =head1 ERRORS
458  
459 The methods die if any error occurs.
460  
461 =head1 SEE ALSO
462  
463 L<OSPF::LSDB>,
464 L<OSPF::LSDB::ospf6d>
465  
466 L<ospfd2yaml>
467  
468 =head1 AUTHORS
469  
470 Alexander Bluhm
471  
472 =cut
473  
474 18 1;
« blib/lib/OSPF/LSDB/Cisco.pm ↑ Summary blib/lib/OSPF/LSDB/ospf6d.pm »