blib/lib/OSPF/LSDB/gated.pm

filter: Statementstmt 81.1% 215 / 265 Branchbran 66.0% 111 / 168 Conditioncond 45.4% 5 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 95.0% 19 / 20 Podpod 9.0% 1 / 11 totaltotal 73.4% 354 / 482 CC 99 SCAR 29.1
CC
99
Cov
75%
get_dump
90.0
parse_area
22.2
parse_links
19.0
CRAP 18.4
« script/ospf2dot ↑ Summary script/ospfd2yaml »
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 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =pod
21  
22 =head1 NAME
23  
24 OSPF::LSDB::gated - parse B<gated> OSPF link state database
25  
26 =head1 SYNOPSIS
27  
28 use OSPF::LSDB::gated;
29  
30 my $gated = OSPF::LSDB::gated-E<gt>new();
31  
32 my $gated = OSPF::LSDB::gated-E<gt>new(ssh => "user@host");
33  
34 $gated-E<gt>parse(%todo);
35  
36 =head1 DESCRIPTION
37  
38 The OSPF::LSDB::gated module parses the OSPF part of a B<gated>
39 dump file and fills the L<OSPF::LSDB> base object.
40 An existing F<gated_dump> file can be given or it can be created
41 dynammically.
42 In the latter case B<sudo> is invoked if permissions are not
43 sufficient to run B<gdc dump>.
44 If the object has been created with the C<ssh> argument, the specified
45 user and host are used to login and run B<gdc dump> there.
46  
47 There is only one public method:
48  
49 =cut
50  
51 package OSPF::LSDB::gated;
52 4 ▶use base 'OSPF::LSDB';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 4 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 use fields qw(
56 dump
57 4 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # add a Regexp::Common regep that recognizes short IP addresses
60 4 my $IPunitdec = q{(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})};
61 4 my $IPdefsep = '[.]';
62 4 Regexp::Common::pattern
63 name => [qw (net sIPv4)],
64 create => "(?k:$IPunitdec(?:$IPdefsep$IPunitdec){0,3})",
65 ;
66  
67 # add a Regexp::Common regep that recognizes time in 0:00:00 format
68 4 my $time60 = q{(?:[0-6]?[0-9])};
69 4 Regexp::Common::pattern
70 name => [qw (time)],
71 create => "(?k:(?:[0-9]+:)?$time60:$time60|(?:$time60:)?$time60)",
72 ;
73  
74 # shortcut
75 4 my $IP = qr/$RE{net}{IPv4}{-keep}/;
76 4 my $SIP = qr/$RE{net}{sIPv4}{-keep}/;
77 4 my $TIME = qr/$RE{time}{-keep}/;
78 4 my $DEC = qr/([0-9]+)/;
79 4 my $NUM = qr/$RE{num}{dec}{-keep}/;
80 4 my $HEX = qr/$RE{num}{hex}{-keep}/;
81 4 my $OO = qr/(On|Off)/;
82 4 my $TAG = qr/(?:$DEC|Invalid tag: $HEX)/;
83  
84 # convert short IP to long IP
85 sub _s2lIP($) {
86 912 ▶ my $ip = $_[0].".0.0.0";
Subroutine _s2lIP: called CC=1 SCAR=0.0
87 912 $ip =~ /^$RE{net}{IPv4}{-keep}/;
88 912 return $1;
89 }
90  
91 # convert time to seconds
92 sub _time2sec($) {
93 192 ▶ my @a = split(/:/, "0:0:".$_[0]);
Subroutine _time2sec: called CC=1 SCAR=0.0
94 192 return 60*(60*$a[-3] + $a[-2]) + $a[-1];
95 }
96  
97 # convert On/Off to boolean 0/1
98 52 ▶sub _oo2bool($) { $_[0] eq "On" ? 1 : $_[0] eq "Off" ? 0 : undef }
Subroutine _oo2bool: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
$_[0] eq 'On' ? : 24 28 52
$_[0] eq 'Off' ? : 28 0 28
99  
100 sub get_dump {
101 0 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine get_dump: not called CC=9 SCAR=45.0
Pod undocumented
102 0 ▶ my $file = $_[0] || "/var/tmp/gated_dump";
Condition0%0/2
$_[0] || '/var/tmp/gated_dump'
l!l
00
MC/DC0%0/1
$_[0] || '/var/tmp/gated_dump'
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
103 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
104 0 my @cmd = ("mv", "-f", $file, "$file.old");
105 0 ▶ unshift @cmd, "sudo" if $> != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $> != 0 0 0 0
106 0 system(@cmd);
107 }
108 0 my @cmd = qw(gdc dump);
109 0 ▶ if ($self->{ssh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssh'}) { } 0 0 0
110 0 unshift @cmd, "ssh", $self->{ssh};
111 } else {
112 0 ▶ unshift @cmd, "sudo" if $> != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $> != 0 0 0 0
113 }
114 0 ▶ system(@cmd)
Branch: 1 branch
BranchTrueFalseTotal
system @cmd and die "Command '@{cmd}' failed: $?\n" 0 0 0
115 and die "Command '@cmd' failed: $?\n";
116 0 sleep(1); # XXX when is gated finished ?
117 0 ▶ if ($self->{ssh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssh'}) { } 0 0 0
118 0 @cmd = ("ssh", $self->{ssh}, "cat", $file);
119 0 @{$self->{dump}} = `@cmd`;
120 0 ▶ die "Command '@cmd' failed: $?\n" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
121 } else {
122 0 @{$self->{dump}} = read_file($file);
123 }
124 }
125  
126 sub parse_links {
127 26 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_links: called CC=11 SCAR=29.5
Pod undocumented
128 26 my($router, @lines) = @_;
129 26 my %typename = (
130 "Router" => "pointtopoint",
131 "Transit net" => "transit",
132 "Stub net" => "stub",
133 "Virtual" => "virtual",
134 );
135 26 my $type;
136 my $l;
137 26 foreach (@lines) {
138 164 ▶ if (/Type: ([\w ]+)\s+Cost: $DEC$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/Type: ([\w ]+)\s+Cost: $DEC$/) { } 82 82 164
elsif (/RouterID: $SIP\s+Address: $SIP$/) { } 0 82 82
elsif (/DR: $SIP\s+Address: $SIP$/) { } 64 18 82
elsif (/Network: $SIP\s+NetMask: $SIP$/) { } 18 0 18
139 82 ▶ defined($type = $typename{$1})
Branch: 1 branch
BranchTrueFalseTotal
defined($type = $typename{$1}) or die "Unknown link type: $1\n" 0 82 82
140 or die "Unknown link type: $1\n";
141 82 $l = { metric => $2 };
142 82 push @{$router->{$type.'s'}}, $l;
143 } elsif (/RouterID: $SIP\s+Address: $SIP$/) {
144 0 ▶ if ($type eq "pointtopoint" || $type eq "virtual") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'pointtopoint' or $type eq 'virtual') { } 0 0 0
Condition0%0/3
$type eq "pointtopoint" or $type eq "virtual"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "pointtopoint" or $type eq "virtual"
$type eq "pointtopoint" $type eq "virtual"
Truth table0%0/3
A or B
A= $type eq "pointtopoint"B= $type eq "virtual"
ABresult
1X1
011
000
145 0 $l->{routerid} = _s2lIP($1);
146 0 $l->{interface} = _s2lIP($2);
147 } else {
148 0 die "$_ Bad line for link type $type.\n";
149 }
150 } elsif (/DR: $SIP\s+Address: $SIP$/) {
151 64 ▶ if ($type eq "transit") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'transit') { } 64 0 64
152 64 $l->{address} = _s2lIP($1);
153 64 $l->{interface} = _s2lIP($2);
154 } else {
155 0 die "$_ Bad line for link type $type.\n";
156 }
157 } elsif (/Network: $SIP\s+NetMask: $SIP$/) {
158 18 ▶ if ($type eq "stub") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'stub') { } 18 0 18
159 18 $l->{network} = _s2lIP($1);
160 18 $l->{netmask} = _s2lIP($2);
161 } else {
162 0 die "$_ Bad line for link type $type.\n";
163 }
164 } else {
165 0 die "$_ Unknown link line.\n";
166 }
167 }
168 }
169  
170 sub parse_router {
171 26 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_router: called CC=9 SCAR=22.1
Pod undocumented
172 26 my @lines = @_;
173 26 my %router;
174 26 my($section, @link_lines);
175 26 foreach (@lines) {
176 260 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/) 74 186 260
177 74 undef $section;
178 }
179 260 ▶ if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) { } 26 234 260
elsif (/^RouterID: $SIP\s+Area Border: $OO\s+AS Border: $OO$/) { } 26 208 234
elsif (/^Nexthops\b/) { } 22 186 208
elsif (s/^\t//) { } 186 0 186
180 26 $router{routerid} = _s2lIP($1);
181 26 $router{age} = _time2sec($3);
182 26 $router{sequence} = "0x$4";
183 } elsif (/^RouterID: $SIP\s+Area Border: $OO\s+AS Border: $OO$/) {
184 26 $section = "link";
185 26 $router{router} = _s2lIP($1);
186 26 $router{bits}{B} = _oo2bool($2);
187 26 $router{bits}{E} = _oo2bool($3);
188 26 $router{bits}{V} = 0; # XXX need gated dump with virtual link
189 } elsif (/^Nexthops\b/) {
190 22 $section = "nexthop";
191 } elsif (s/^\t//) {
192 186 ▶ if ($section eq "link") {
Branch: 2 branches
BranchTrueFalseTotal
if ($section eq 'link') { } 164 22 186
elsif ($section eq 'nexthop') { } 22 0 22
193 164 push @link_lines, $_;
194 } elsif ($section eq "nexthop") {
195 # not part of LSDB, redundant in gated dump
196 } else {
197 0 die "$_ No router section.\n";
198 }
199 } else {
200 0 die "$_ Unknown router line.\n";
201 }
202 }
203 26 $self->parse_links(\%router, @link_lines);
204 26 return \%router;
205 }
206  
207 sub parse_network {
208 26 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_network: called CC=9 SCAR=22.1
Pod undocumented
209 26 my @lines = @_;
210 26 my %network;
211 my($section);
212 26 foreach (@lines) {
213 166 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/) 142 24 166
214 142 undef $section;
215 }
216 166 ▶ if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) {
Branch: 5 branches
BranchTrueFalseTotal
if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) { } 26 140 166
elsif (/^Router: $SIP\s+Netmask: $SIP\s+Network: $SIP$/) { } 26 114 140
elsif (/^Attached Router: $SIP$/) { } 66 48 114
elsif (/^Nexthops\b/) { } 24 24 48
elsif (s/^\t//) { } 24 0 24
217 26 $network{routerid} = _s2lIP($1);
218 26 $network{age} = _time2sec($3);
219 26 $network{sequence} = "0x$4";
220 } elsif (/^Router: $SIP\s+Netmask: $SIP\s+Network: $SIP$/) {
221 26 $network{address} = _s2lIP($1);
222 26 $network{netmask} = _s2lIP($2);
223 } elsif (/^Attached Router: $SIP$/) {
224 66 push @{$network{attachments}}, { routerid => _s2lIP($1) };
225 } elsif (/^Nexthops\b/) {
226 24 $section = "nexthop";
227 } elsif (s/^\t//) {
228 24 ▶ if ($section eq "nexthop") {
Branch: 1 branch
BranchTrueFalseTotal
if ($section eq 'nexthop') { } 24 0 24
229 # not part of LSDB, redundant in gated dump
230 } else {
231 0 die "$_ No network section.\n";
232 }
233 } else {
234 0 die "$_ Unknown network line.\n";
235 }
236 }
237 26 return \%network;
238 }
239  
240 sub parse_summary {
241 4 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_summary: called CC=8 SCAR=21.0
Pod undocumented
242 4 my @lines = @_;
243 4 my %summary;
244 my($section);
245 4 foreach (@lines) {
246 16 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/) 12 4 16
247 12 undef $section;
248 }
249 16 ▶ if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) { } 4 12 16
elsif (/^LSID: $SIP\s+Network: $SIP\s+Netmask: $SIP\s+Cost: $DEC$/) { } 4 8 12
elsif (/^Nexthops\b/) { } 4 4 8
elsif (s/^\t//) { } 4 0 4
250 4 $summary{routerid} = _s2lIP($1);
251 4 $summary{age} = _time2sec($3);
252 4 $summary{sequence} = "0x$4";
253 } elsif (/^LSID: $SIP\s+Network: $SIP\s+Netmask: $SIP\s+Cost: $DEC$/) {
254 4 $summary{address} = _s2lIP($1);
255 4 $summary{netmask} = _s2lIP($3);
256 4 $summary{metric} = $4;
257 } elsif (/^Nexthops\b/) {
258 4 $section = "nexthop";
259 } elsif (s/^\t//) {
260 4 ▶ if ($section eq "nexthop") {
Branch: 1 branch
BranchTrueFalseTotal
if ($section eq 'nexthop') { } 4 0 4
261 # not part of LSDB, redundant in gated dump
262 } else {
263 0 die "$_ No summary section.\n";
264 }
265 } else {
266 0 die "$_ Unknown summary line.\n";
267 }
268 }
269 4 return \%summary;
270 }
271  
272 sub parse_boundary {
273 4 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_boundary: called CC=8 SCAR=25.3
Pod undocumented
274 4 my @lines = @_;
275 4 my %boundary;
276 my($section);
277 4 foreach (@lines) {
278 8 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/) 8 0 8
279 8 undef $section;
280 }
281 8 ▶ if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) { } 4 4 8
elsif (/^RouterID: $SIP\s+Cost: $DEC$/) { } 4 0 4
elsif (/^Nexthops\b/) { } 0 0 0
elsif (s/^\t//) { } 0 0 0
282 4 $boundary{routerid} = _s2lIP($1);
283 4 $boundary{age} = _time2sec($3);
284 4 $boundary{sequence} = "0x$4";
285 } elsif (/^RouterID: $SIP\s+Cost: $DEC$/) {
286 4 $boundary{asbrouter} = _s2lIP($1);
287 4 $boundary{metric} = $2;
288 } elsif (/^Nexthops\b/) {
289 0 $section = "nexthop";
290 } elsif (s/^\t//) {
291 0 ▶ if ($section eq "nexthop") {
Branch: 1 branch
BranchTrueFalseTotal
if ($section eq 'nexthop') { } 0 0 0
292 # not part of LSDB, redundant in gated dump
293 } else {
294 0 die "$_ No boundary section.\n";
295 }
296 } else {
297 0 die "$_ Unknown boundary line.\n";
298 }
299 }
300 4 return \%boundary;
301 }
302  
303 sub parse_area {
304 2 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_area: called CC=18 SCAR=31.0
Pod undocumented
305 2 my($area, @lines) = @_;
306 2 my %typename = (
307 Stub => [ "stubs" ], # not an RFC LSA type, redundant in gated dump
308 Router => [ routers => \&parse_router ],
309 SumNet => [ summarys => \&parse_summary ],
310 SumASB => [ boundarys => \&parse_boundary ],
311 Net => [ networks => \&parse_network ],
312 );
313 2 my($lsdb, $type, @type_lines);
314 2 foreach (@lines) {
315 710 ▶ if (/^Link State Database:/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^Link State Database:/) { } 2 708 710
elsif ($lsdb) { } 600 108 708
316 2 ▶ die "$_ Duplicate LSDB.\n" if $lsdb;
Branch: 1 branch
BranchTrueFalseTotal
if $lsdb 0 2 2
317 2 $lsdb = 1;
318 } elsif ($lsdb) {
319 600 ▶ if (/^Retransmission List:$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Retransmission List:$/) 0 600 600
320 0 ▶ $type
Branch: 1 branch
BranchTrueFalseTotal
$type or die "Retransmission without LSA type\n" 0 0 0
321 or die "Retransmission without LSA type\n";
322 0 warn "Retransmission list for $type->[0]\n";
323 }
324 600 ▶ if (! /^\t/ && @type_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (not /^\t/ and @type_lines) 78 522 600
Condition100%3/3
not /^\t/ and @type_lines
!ll&&!rl&&r
4447878
MC/DC100%2/2
not /^\t/ and @type_lines
not /^\t/ @type_lines
Truth table100%3/3
not(A) and B
A= not /^\t/B= @type_lines
ABresult
0X0
100
111
325 78 ▶ if ($type && $type->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $type->[1]) 60 18 78
Condition66%2/3
$type and $type->[1]
!ll&&!rl&&r
01860
MC/DC50%1/2
$type and $type->[1]
$type $type->[1]
Truth table66%2/3
A and B
A= $typeB= $type->[1]
ABresult
0X0
100
111
326 60 my($name, $lsaparser) = @$type;
327 60 my $lsa = $lsaparser->($self, @type_lines);
328 60 $lsa->{area} = $area;
329 60 push @{$self->{ospf}{database}{$name}}, $lsa;
330 }
331 78 undef @type_lines;
332 78 undef $type;
333 }
334 600 ▶ if (s/^(\w+)\t//) {
Branch: 4 branches
BranchTrueFalseTotal
if (s/^(\w+)\t//) { } 78 522 600
elsif (s/\t//) { } 444 78 522
elsif (/^Retransmission List:$/) { } 0 78 78
elsif (/^$/) { } 78 0 78
335 78 ▶ $type = $typename{$1}
Branch: 1 branch
BranchTrueFalseTotal
$type = $typename{$1} or die "Unknown LSA type: $1\n" 0 78 78
336 or die "Unknown LSA type: $1\n";
337 78 push @type_lines, $_;
338 } elsif(s/\t//) {
339 444 ▶ $type
Branch: 1 branch
BranchTrueFalseTotal
$type or die "No LSA type\n" 0 444 444
340 or die "No LSA type\n";
341 444 push @type_lines, $_;
342 } elsif (/^Retransmission List:$/) {
343 0 $type = [ "retransmission" ];
344 } elsif(/^$/) {
345 78 undef $type;
346 } else {
347 0 die "$_ Unknown LSA line.\n";
348 }
349 }
350 }
351 2 ▶ if (@type_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@type_lines) 0 2 2
352 0 die "Unprocessed LSA lines:\n", @type_lines;
353 }
354 }
355  
356 sub parse_externals {
357 2 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_externals: called CC=12 SCAR=26.0
Pod undocumented
358 2 my @lines = @_;
359 2 my @externals;
360 my($section);
361 2 foreach (@lines) {
362 684 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/) 472 212 684
363 472 undef $section;
364 }
365 684 ▶ if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) {
Branch: 7 branches
BranchTrueFalseTotal
if (/^AdvRtr: $SIP\s+Len: $DEC\s+Age: $TIME\s+Seq: $HEX$/) { } 132 552 684
elsif (/^LSID: $SIP\s+Network: $SIP\s+Netmask: $SIP\s+Cost: $DEC$/) { } 132 420 552
elsif (/^Type: ([1-2])\s+Forward: $SIP\s+Tag: $TAG\b/) { } 132 288 420
elsif (/^Nexthops\b/) { } 76 212 288
elsif (/^Retransmission List:$/) { } 0 212 212
elsif (s/^\t//) { } 76 136 212
elsif (/^$/) { } 136 0 136
366 132 push @externals, {
367 routerid => _s2lIP($1),
368 age => _time2sec($3),
369 sequence => "0x$4",
370 };
371 } elsif (/^LSID: $SIP\s+Network: $SIP\s+Netmask: $SIP\s+Cost: $DEC$/) {
372 132 $externals[-1]{address} = _s2lIP($1);
373 132 $externals[-1]{netmask} = _s2lIP($3);
374 132 $externals[-1]{metric} = $4;
375 } elsif (/^Type: ([1-2])\s+Forward: $SIP\s+Tag: $TAG\b/) {
376 132 $externals[-1]{type} = $1;
377 132 $externals[-1]{forward} = _s2lIP($2);
378 } elsif (/^Nexthops\b/) {
379 76 $section = "nexthop";
380 } elsif (/^Retransmission List:$/) {
381 0 $section = "retransmission";
382 0 warn "Retransmission list for external\n";
383 } elsif (s/^\t//) {
384 76 ▶ if ($section eq "nexthop") {
Branch: 2 branches
BranchTrueFalseTotal
if ($section eq 'nexthop') { } 76 0 76
elsif ($section eq 'retransmission') { } 0 0 0
385 # not part of LSDB, redundant in gated dump
386 } elsif ($section eq "retransmission") {
387 # not part of LSDB, internal gated information
388 } else {
389 0 die "$_ No external section.\n";
390 }
391 } elsif (/^$/) {
392 136 undef $section;
393 } else {
394 0 die "$_ Unknown external line.\n";
395 }
396 }
397 2 $self->{ospf}{database}{externals} = \@externals;
398 }
399  
400 sub parse_lsdb {
401 2 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_lsdb: called CC=2 SCAR=6.9
Pod undocumented
402 2 my($area_lines, $external_lines) = @_;
403 2 foreach my $area (@{$self->{ospf}{self}{areas}}) {
404 2 $self->parse_area($area, @{$area_lines->{$area}});
405 }
406 2 $self->parse_externals(@$external_lines);
407 }
408  
409 sub parse_ospf {
410 2 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse_ospf: called CC=9 SCAR=22.0
Pod undocumented
411 2 my @lines = @_;
412 2 my(%section, %area_lines, @external_lines);
413 2 my($routerid, @areas);
414 2 foreach (@lines) {
415 1688 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/) { } 32 1656 1688
416 32 undef %section;
417 32 ▶ if (/^RouterID: $SIP\s+/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^RouterID: $SIP\s+/) { } 2 30 32
elsif (/^Area $SIP:/) { } 2 28 30
elsif (/^AS Externals\s+/) { } 2 26 28
418 2 $routerid = _s2lIP($1);
419 } elsif (/^Area $SIP:/) {
420 2 my $area = _s2lIP($1);
421 2 push @areas, $area;
422 2 $section{area} = $area;
423 } elsif (/^AS Externals\s+/) {
424 2 $section{external} = 1;
425 }
426 } else {
427 1656 s/^\t//;
428 1656 ▶ if ($section{area}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($section{'area'}) { } 710 946 1656
elsif ($section{'external'}) { } 684 262 946
429 710 push @{$area_lines{$section{area}}}, $_;
430 } elsif ($section{external}) {
431 684 push @external_lines, $_;
432 }
433 }
434 }
435 2 ▶ $self->{ospf}{self}{routerid} = $routerid
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ospf"}{"self"}{"routerid"} = $routerid or die "No router id.\n" 0 2 2
436 or die "No router id.\n";
437 2 $self->{ospf}{self}{areas} = \@areas;
438 2 $self->parse_lsdb(\%area_lines, \@external_lines);
439 }
440  
441 =pod
442  
443 =over 4
444  
445 =item $self-E<gt>parse(%todo)
446  
447 This function takes a hash describing how the OSPF LSDB can be
448 obtained.
449 The bool value of C<dump> specifies wether the dump file should be
450 created dynamically by calling B<gdc dump>.
451 The C<file> parameter contains the path to the F<gated_dump> file,
452 it defaults to F</var/tmp/gated_dump>.
453 The dump file may contain more than one instance of the gated memory
454 dump separated by form feeds.
455 If the numeric B<skip> paremeter is set, that many dumps from the
456 beginning of the file are skipped and the next one is used.
457  
458 The complete OSPF link state database is stored in the B<ospf> field
459 of the base class.
460  
461 =back
462  
463 =cut
464  
465 sub parse {
466 2 ▶ my OSPF::LSDB::gated $self = shift;
Subroutine parse: called CC=12 SCAR=26.1
Pod documented
467 2 my %todo = @_;
468 2 ▶ if ($todo{dump}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($todo{'dump'}) { } 0 2 2
469 0 $self->get_dump($todo{file});
470 } else {
471 2 @{$self->{dump}} = read_file($todo{file});
472 }
473 2 ▶ my $skip = $todo{skip} + 1 if $todo{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $todo{"skip"} 0 2 2
474 2 my($task, @ospf_lines);
475 2 my $n = 0;
476 2 foreach (@{$self->{dump}}) {
477 6904 $n++;
478 6904 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 0 6904 6904
479 0 ▶ if (/^\f$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\f$/) 0 0 0
480 0 $skip--;
481 }
482 0 next;
483 }
484 6904 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/) 30 6874 6904
485 30 undef $task;
486 }
487 6904 ▶ if (/^Task (\w+):/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^Task (\w+):/) { } 22 6882 6904
elsif (/^Done$/) { } 2 6880 6882
elsif (defined $task) { } 3210 3670 6880
488 22 $task = lc($1);
489 } elsif (/^Done$/) {
490 2 last;
491 } elsif (defined $task) {
492 3210 s/^\t//;
493 3210 ▶ if ($task eq "ospf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($task eq "ospf") 1688 1522 3210
494 1688 push @ospf_lines, $_;
495 }
496 }
497 }
498 2 ▶ if ($n < @{$self->{dump}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @{$self->{"dump"};}) 0 2 2
499 0 warn "More data in gated dump.\n";
500 }
501 2 $self->parse_ospf(@ospf_lines);
502 2 $self->{ospf}{ipv6} = 0;
503 }
504  
505 =pod
506  
507 This module has been tested with gated 3.6.
508 If it works with other versions is unknown.
509  
510 =head1 ERRORS
511  
512 The methods die if any error occurs.
513  
514 =head1 SEE ALSO
515  
516 L<OSPF::LSDB>
517  
518 L<gated2yaml>
519  
520 =head1 AUTHORS
521  
522 Alexander Bluhm
523  
524 =cut
525  
526 4 1;
« script/ospf2dot ↑ Summary script/ospfd2yaml »