BGP/ASPath.pm

filter: Statementstmt 95.8% 253 / 264 Branchbran 75.9% 79 / 104 Conditioncond 79.3% 23 / 29 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 94.8% 37 / 39 Podpod 52.6% 10 / 19 totaltotal 86.4% 408 / 472 SCAR 43.7
CC
73
Cov
89%
_new_from_msg
18.1
len_notequal
12.0
_encode
9.0
CRAP 79.3
« BGP/Peer.pm ↑ Summary BGP/NLRI.pm »
1 #!/usr/bin/perl
2  
3 package Net::BGP::ASPath;
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 use vars qw(
7 $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @PATHTYPES
8 @BGP_PATH_ATTR_COUNTS
9 7 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ## Inheritance and Versioning ##
12  
13 7 @ISA = qw( Exporter );
14 7 $VERSION = '0.16';
15  
16 ## Module Imports ##
17  
18 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 use overload
21 '<=>' => \&len_compare,
22 '<' => \&len_lessthen,
23 '>' => \&len_greaterthen,
24 '==' => \&len_equal,
25 '!=' => \&len_notequal,
26 '""' => \&as_string,
27 0 ▶ '+' => sub { my $x = shift->clone; $x->prepend(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 7 '+=' => \&prepend,
29 'eq' => \&equal,
30 'ne' => \&notequal,
31 '@{}' => \&asarray,
32 7 ▶ 'fallback' => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 7 ▶use Net::BGP::ASPath::AS;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use Net::BGP::ASPath::AS_CONFED_SEQUENCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use Net::BGP::ASPath::AS_CONFED_SET;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 7 ▶use Net::BGP::ASPath::AS_SEQUENCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use Net::BGP::ASPath::AS_SET;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 ## Public Class Methods ##
41  
42 sub new {
43 111 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=19.7
Pod documented
44 111 my $value = shift;
45 111 my $options = shift;
46  
47 111 ▶ if (!defined($options)) { $options = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options) 111 0 111
48 111 ▶ $options->{as4} ||= 0;
Condition50%1/2
$options->{"as4"} ||= 0
l!l
0111
MC/DC0%0/1
$options->{"as4"} ||= 0
$options->{"as4"}
Truth table50%1/2
A
A= $options->{"as4"}
Aresult
11
00
49  
50 111 ▶ return clone Net::BGP::ASPath($value) if (ref $value eq 'Net::BGP::ASPath');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "Net::BGP::ASPath" 1 110 111
51  
52 my $this = {
53 _as_path => [],
54 _as4 => $options->{as4}
55 110 };
56  
57 110 bless($this, $class);
58  
59 110 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 57 53 110
60 57 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 1 56 57
61 1 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 1 0 1
62 1 $this->_setfromstring(join ' ', @$value);
63 } else {
64 0 croak "Unknown ASPath constructor argument type: " . ref $value
65 }
66 } else {
67 # Scalar/string
68 56 $this->_setfromstring($value);
69 }
70 }
71  
72 110 return ($this);
73 }
74  
75 sub _setfromstring {
76 203 ▶ my ($this, $value) = @_;
Subroutine _setfromstring: called CC=6 SCAR=17.9
77 203 $this->{_as_path} = [];
78  
79 # Normalize string
80 203 $value =~ s/\s+/ /g;
81 203 $value =~ s/^\s//;
82 203 $value =~ s/\s$//;
83 203 $value =~ s/\s?,\s?/,/g;
84  
85 203 ▶ while ($value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "") 355 203 558
86  
87 # Note that the AS_SEQUENCE can't be > 255 path elements. The entire len
88 # of the AS_PATH can be > 255 octets, true, but not an individual AS_SET
89 # segment.
90 # TODO: We should do the same for other path types and also take care to
91 # not allow ourselves to overflow the 65535 byte length limit if this is
92 # converted back to a usable path.
93 # TODO: It would be better to put the short AS PATH at end of the path,
94 # not the beginning of the path, so that it is easier for other routers
95 # to process.
96 355 ▶ confess 'Invalid path segments for path object: >>' . $value . '<<'
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^(\([^\)]*\))( (.*))?$/ or $value =~ /^(\{[^\}]*\})( (.*))?$/ or $value =~ /^(([0-9]+\s*){1,255})(.*)?$/ 0 355 355
Condition83%5/6
$value =~ /^(\([^\)]*\))( (.*))?$/ or $value =~ /^(\{[^\}]*\})( (.*))?$/ or $value =~ /^(([0-9]+\s*){1,255})(.*)?$/
l!l&&r!l&&!r
2031520
$value =~ /^(\([^\)]*\))( (.*))?$/ or $value =~ /^(\{[^\}]*\})( (.*))?$/
l!l&&r!l&&!r
13073152
MC/DC0%0/3
$value =~ /^(\([^\)]*\))( (.*))?$/ or $value =~ /^(\{[^\}]*\})( (.*))?$/ or $value =~ /^(([0-9]+\s*){1,255})(.*)?$/
$value =~ /^(\([^\)]*\))( (.*))?$/ $value =~ /^(\{[^\}]*\})( (.*))?$/ $value =~ /^(([0-9]+\s*){1,255})(.*)?$/
Truth table75%3/4
A or B or C
A= $value =~ /^(\([^\)]*\))( (.*))?$/B= $value =~ /^(\{[^\}]*\})( (.*))?$/C= $value =~ /^(([0-9]+\s*){1,255})(.*)?$/
ABCresult
1XX1
01X1
0011
0000
97 unless (
98 ($value =~ /^(\([^\)]*\))( (.*))?$/) || # AS_CONFED_* segment
99 ($value =~ /^(\{[^\}]*\})( (.*))?$/) || # AS_SET segment
100 ($value =~ /^(([0-9]+\s*){1,255})(.*)?$/)
101 ); # AS_SEQUENCE seqment
102  
103 355 ▶ $value = $3 || '';
Condition100%2/2
$3 || ''
l!l
165190
MC/DC100%1/1
$3 || ''
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
104 355 my $segment = Net::BGP::ASPath::AS->new($1);
105  
106 355 push(@{ $this->{_as_path} }, $segment);
107 }
108 203 return $this;
109 }
110  
111 sub clone {
112 162 ▶ my $proto = shift;
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
113 162 ▶ my $class = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
16110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
114 162 ▶ $proto = shift unless ref $proto;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $proto 1 161 162
115  
116 162 my $clone = { _as_path => [] };
117  
118 162 foreach my $p (@{ $proto->{_as_path} }) {
119 253 push(@{ $clone->{_as_path} }, $p->clone);
120 }
121  
122 162 return (bless($clone, $class));
123 }
124  
125 # This takes two buffers. The first buffer is the standard AS_PATH buffer and
126 # should always be defined.
127 #
128 # The second buffer is the AS4_PATH buffer.
129 #
130 # The third parameter is true if AS4 is natively supported, false if AS4 is not
131 sub _new_from_msg {
132 31 ▶ my ($class, $buffer, $buffer2, $options) = @_;
Subroutine _new_from_msg: called CC=17 SCAR=29.0
133 31 my $this = $class->new;
134  
135 31 ▶ if (!defined($options)) { $options = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options) 14 17 31
136 31 ▶ $options->{as4} ||= 0;
Condition100%2/2
$options->{"as4"} ||= 0
l!l
427
MC/DC100%1/1
$options->{"as4"} ||= 0
$options->{"as4"}
Truth table100%2/2
A
A= $options->{"as4"}
Aresult
11
00
137  
138 31 ▶ my $size = $options->{as4} ? 4 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'as4'} ? : 4 27 31
139  
140 31 ▶ if (!defined($buffer2)) { $buffer2 = ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $buffer2) 13 18 31
141  
142 31 my $segment;
143 31 ▶ while ($buffer ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer ne "") 39 31 70
144  
145 39 ($segment, $buffer)
146 = Net::BGP::ASPath::AS->_new_from_msg($buffer, $options);
147  
148 # Error handling
149 39 ▶ if ( !(defined $segment) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $segment) 0 39 39
150 0 return undef;
151 }
152 39 ▶ if ( length($buffer) && ( ( length($buffer) - 2 ) % $size) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $buffer and (CORE::length($buffer) - 2) % $size) 0 39 39
Condition66%2/3
CORE::length $buffer and (CORE::length($buffer) - 2) % $size
!ll&&!rl&&r
3090
MC/DC0%0/2
CORE::length $buffer and (CORE::length($buffer) - 2) % $size
CORE::length $buffer (CORE::length($buffer) - 2) % $size
Truth table66%2/3
A and B
A= CORE::length $bufferB= (CORE::length($buffer) - 2) % $size
ABresult
0X0
100
111
153 0 return undef;
154 }
155  
156 39 push(@{ $this->{_as_path} }, $segment);
157 }
158  
159 # We ignore AS4_PATHs on native AS4 speaker sessions
160 # So we stop here.
161 31 ▶ if ($options->{as4}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"as4"}) 4 27 31
162 4 return $this;
163 }
164  
165 27 my @as4_path;
166  
167 27 ▶ while ($buffer2 ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer2 ne "") 5 27 32
168  
169 5 ($segment, $buffer2)
170 = Net::BGP::ASPath::AS->_new_from_msg(
171 $buffer2,
172 { as4 => 1 }
173 );
174  
175 # TODO: Should make sure type is only AS_SEQUENCE or AS_SET!
176  
177 5 ▶ if ( !(defined $segment) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $segment) 0 5 5
178 0 return undef;
179 }
180 5 ▶ if ( length($buffer2) && ( ( length($buffer2) - 2 ) % 4) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $buffer2 and (CORE::length($buffer2) - 2) % 4) 0 5 5
Condition66%2/3
CORE::length $buffer2 and (CORE::length($buffer2) - 2) % 4
!ll&&!rl&&r
410
MC/DC0%0/2
CORE::length $buffer2 and (CORE::length($buffer2) - 2) % 4
CORE::length $buffer2 (CORE::length($buffer2) - 2) % 4
Truth table66%2/3
A and B
A= CORE::length $buffer2B= (CORE::length($buffer2) - 2) % 4
ABresult
0X0
100
111
181 0 return undef;
182 }
183  
184 5 push (@as4_path, $segment);
185 }
186  
187 27 my $as_count = $this->_length_helper( $this->{_as_path} );
188 27 my $as4_count = $this->_length_helper( \@as4_path );
189  
190 27 ▶ if ($as_count < $as4_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as_count < $as4_count) 0 27 27
191 # We ignroe the AS4 stuff per RFC4893 in this case
192 0 return $this;
193 }
194  
195 27 my $remove = $as4_count;
196  
197 27 ▶ while ($remove > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove > 0) 5 27 32
198 5 my $ele = pop @{ $this->{_as_path} };
199 5 ▶ if ($ele->length <= $remove) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ele->length <= $remove) { } 2 3 5
200 2 $remove -= $ele->length;
201 } else {
202 3 push @{ $this->{_as_path} }, $ele->remove_tail($remove);
203 3 $remove = 0;
204 }
205 }
206  
207 27 push @{ $this->{_as_path} }, @as4_path;
208  
209 27 return $this;
210 }
211  
212 ## Public Object Methods ##
213  
214 # This encodes the AS_PATH and AS4_PATH elements (both are returned)
215 #
216 # If the AS4_PATH element is undef, that indicates an AS4_PATH is not
217 # needed - either we're encoding in 32-bit clear format, or all
218 # elements have only 16 bit ASNs.
219 sub _encode {
220 25 ▶ my ($this, $args) = @_;
Subroutine _encode: called CC=9 SCAR=22.0
221  
222 25 ▶ if (!defined($args)) { $args = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args) 14 11 25
223 25 ▶ $args->{as4} ||= 0;
Condition100%2/2
$args->{"as4"} ||= 0
l!l
421
MC/DC100%1/1
$args->{"as4"} ||= 0
$args->{"as4"}
Truth table100%2/2
A
A= $args->{"as4"}
Aresult
11
00
224  
225 25 my $has_as4;
226 25 my $msg = '';
227 25 foreach my $segment (@{ $this->{_as_path} }) {
228 33 $msg .= $segment->_encode($args);
229  
230 33 ▶ if ($segment->_has_as4()) { $has_as4 = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->_has_as4) 6 27 33
231 }
232  
233 25 my $as4;
234 25 ▶ if ( ( !($args->{as4} ) ) && ($has_as4) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args->{"as4"} and $has_as4) 1 24 25
Condition100%3/3
not $args->{"as4"} and $has_as4
!ll&&!rl&&r
4201
MC/DC100%2/2
not $args->{"as4"} and $has_as4
not $args->{"as4"} $has_as4
Truth table100%3/3
not(A) and B
A= not $args->{"as4"}B= $has_as4
ABresult
0X0
100
111
235 1 $as4 = '';
236  
237 1 foreach my $segment (@{ $this->{_as_path} }) {
238 2 ▶ if ( !(ref($segment) =~ /_CONFED_/) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($segment) =~ /_CONFED_/) 2 0 2
239 2 $as4 .= $segment->_encode( { as4 => 1 } );
240 }
241 }
242 }
243  
244 25 return ($msg, $as4);
245 }
246  
247 sub prepend {
248 46 ▶ my $this = shift;
Subroutine prepend: called CC=4 SCAR=13.9
Pod documented
249 46 my $value = shift;
250 46 ▶ return $this->prepend_confed($value) if ($value =~ /^\(/);
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\(/ 15 31 46
251 31 $this->strip;
252  
253 31 my @list = ($value);
254 31 ▶ @list = @{$value} if (ref $value eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 1 30 31
255 31 ▶ @list = split(' ', $list[0]) if $list[0] =~ / /;
Branch: 1 branch
BranchTrueFalseTotal
if $list[0] =~ / / 1 30 31
256  
257 # Ugly - slow - but simple! Should be improved later!
258 31 return $this->_setfromstring(join(' ', @list) . ' ' . $this)->cleanup;
259 }
260  
261 sub prepend_confed {
262 33 ▶ my $this = shift;
Subroutine prepend_confed: called CC=4 SCAR=13.9
Pod documented
263  
264 33 my $value = shift;
265 33 ▶ $value =~ s/^\((.*)\)$/$1/ unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 32 1 33
266  
267 33 my @list = ($value);
268 33 ▶ @list = @{$value} if (ref $value eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 1 32 33
269 33 ▶ @list = split(' ', $list[0]) if $list[0] =~ / /;
Branch: 1 branch
BranchTrueFalseTotal
if $list[0] =~ / / 2 31 33
270  
271 # Ugly - slow - but simple! Should be improved later!
272 33 return $this->_setfromstring('(' . join(' ', @list) . ') ' . $this)
273 ->cleanup;
274 }
275  
276 sub cleanup {
277 82 ▶ my $this = shift;
Subroutine cleanup: called CC=1 SCAR=0.0
Pod documented
278  
279 # Ugly - slow - but simple! Should be improved later!
280 82 my $str = $this->as_string;
281 82 $str =~ s/\{\}//g;
282 82 $str =~ s/\(\)//g;
283 82 $str =~ s/(\d)\) +\((\d)/$1 $2/g;
284 82 return $this->_setfromstring($str);
285 }
286  
287 sub _confed {
288 12 ▶ my $this = shift->clone;
Subroutine _confed: called CC=1 SCAR=0.0
289 12 @{ $this->{_as_path} } =
290 12 grep { (ref $_) =~ /_CONFED_/ } @{ $this->{_as_path} };
291 12 return $this;
292 }
293  
294 sub strip {
295 71 ▶ my $this = shift;
Subroutine strip: called CC=1 SCAR=0.0
Pod documented
296 71 @{ $this->{_as_path} } =
297 71 grep { (ref $_) !~ /_CONFED_/ } @{ $this->{_as_path} };
298 71 return $this;
299 }
300  
301 sub striped {
302 26 ▶ return shift->clone->strip(@_);
Subroutine striped: called CC=1 SCAR=0.0
Pod documented
303 }
304  
305 sub aggregate {
306 4 ▶ my @olist = @_;
Subroutine aggregate: called CC=7 SCAR=19.5
Pod documented
307 4 ▶ shift(@olist) unless ref $olist[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $olist[0] 3 1 4
308  
309 # Sets
310 4 my $cset = Net::BGP::ASPath::AS_CONFED_SET->new;
311 4 my $nset = Net::BGP::ASPath::AS_SET->new;
312  
313 # Lists of confed / normal part of paths
314 4 my @clist = map { $_->_confed } @olist;
315 4 my @nlist = map { $_->striped } @olist;
316  
317 4 my $res = '';
318 4 foreach my $pair ([ \@clist, $cset ], [ \@nlist, $nset ]) {
319 8 my ($list, $set) = @{$pair};
320  
321 # Find common head
322 8 my $head = $list->[0]->_head;
323 8 foreach my $obj (@{$list}[ 1 .. @{$list} - 1 ]) {
324 16 my $s = $obj->_head;
325 16 $head = _longest_common_head($head, $s);
326 }
327  
328 # Find tail set
329 8 foreach my $obj (@{$list}) {
330 24 my $tail = $obj->_tail($head);
331 24 ▶ $tail = '(' . $tail if $tail =~ /^[^\(]*\).*$/; # Fix tail
Branch: 1 branch
BranchTrueFalseTotal
if $tail =~ /^[^\(]*\).*$/ 6 18 24
332 24 $obj = Net::BGP::ASPath->new($tail);
333 24 $set->merge($obj);
334 }
335 8 ▶ $head .= ')' if $head =~ /^\([^\)]+$/; # Fix head
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /^\([^\)]+$/ 2 6 8
336 8 $res .= "$head $set ";
337 }
338  
339 # Construct result
340 4 return Net::BGP::ASPath->new($res)->cleanup;
341 }
342  
343 ## Utility functions (not methods!) ##
344 sub _longest_common_head {
345 16 ▶ my ($s1, $s2) = @_;
Subroutine _longest_common_head: called CC=4 SCAR=13.9
346 16 my $pos = 0;
347 16 $s1 .= ' ';
348 16 $s2 .= ' ';
349 16 for my $i (0 .. length($s1) - 1) {
350 80 ▶ last unless substr($s1, $i, 1) eq substr($s2, $i, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless substr($s1, $i, 1) eq substr($s2, $i, 1) 4 76 80
351 76 ▶ $pos = $i if substr($s1, $i, 1) eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if substr($s1, $i, 1) eq " " 34 42 76
352 }
353 16 return substr($s1, 0, $pos);
354 }
355  
356 sub _head
357  
358 # Head means the leading non-set part of the path
359 {
360 24 ▶ my $this = shift->clone;
Subroutine _head: called CC=3 SCAR=11.0
361 24 my $ok = 1;
362 $this->{_as_path} =
363 25 ▶ [ grep { $ok &&= (ref $_) =~ /_SEQUENCE$/; $_ = undef unless $ok; }
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 4 21 25
Condition66%2/3
$ok &&= ref($_) =~ /_SEQUENCE$/
!ll&&!rl&&r
0421
MC/DC50%1/2
$ok &&= ref($_) =~ /_SEQUENCE$/
$ok ref($_) =~ /_SEQUENCE$/
Truth table66%2/3
A &&= B
A= $okB= ref($_) =~ /_SEQUENCE$/
ABresult
0X0
100
111
364 24 @{ $this->{_as_path} } ];
365 24 return $this;
366 }
367  
368 sub _tail
369  
370 # Tail means everything after the "head" given as argument.
371 # The tail is returned as a string. Returns undef if "head" is invalid.
372 {
373 24 ▶ my $thisstr = shift() . " ";
Subroutine _tail: called CC=2 SCAR=7.0
374 24 my $head = shift() . " ";
375 24 $head =~ s/\(/\\(/g;
376 24 $head =~ s/\)/\\)/g;
377 24 ▶ return undef unless $thisstr =~ s/^$head//;
Branch: 1 branch
BranchTrueFalseTotal
unless $thisstr =~ s/^$head// 0 24 24
378 24 $thisstr =~ s/ $//;
379 24 return $thisstr;
380 }
381  
382 # For compatability
383 sub asstring {
384 7 ▶ my $this = shift;
Subroutine asstring: called CC=1 SCAR=0.0
Pod undocumented
385 7 return $this->as_string(@_);
386 }
387  
388 sub as_string {
389 519 ▶ my $this = shift;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
390  
391 519 return $this->_as_string_helper($this->{_as_path});
392 }
393  
394 sub _as_string_helper {
395 519 ▶ my ($this, $path) = @_;
Subroutine _as_string_helper: called CC=1 SCAR=0.0
396  
397 519 return join(' ', map { $_->as_string; } @{ $path });
398 }
399  
400  
401 sub asarray {
402 26 ▶ my $this = shift;
Subroutine asarray: called CC=2 SCAR=6.9
Pod undocumented
403 26 my @res;
404 26 foreach my $s (@{ $this->{_as_path} }) {
405 26 push(@res, @{ $s->asarray });
406 }
407 26 return \@res;
408 }
409  
410 sub len_equal {
411 1 ▶ my ($this, $other) = @_;
Subroutine len_equal: called CC=3 SCAR=11.7
Pod undocumented
412 1 ▶ return 0 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 1 1
413 1 ▶ return ($this->length == $other->length) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->length == $other->length ? : 1 0 1
414 }
415  
416 sub len_notequal {
417 0 ▶ my ($this, $other) = @_;
Subroutine len_notequal: not called CC=3 SCAR=24.8
Pod undocumented
418 0 ▶ return 1 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 0 0
419 0 ▶ return ($this->length != $other->length) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->length != $other->length ? : 0 0 0
420 }
421  
422 sub len_lessthen {
423 1 ▶ my ($this, $other) = @_;
Subroutine len_lessthen: called CC=3 SCAR=11.7
Pod undocumented
424 1 ▶ return 0 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 1 1
425 1 ▶ return ($this->length < $other->length) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->length < $other->length ? : 1 0 1
426 }
427  
428 sub len_greaterthen {
429 1 ▶ my ($this, $other) = @_;
Subroutine len_greaterthen: called CC=3 SCAR=11.7
Pod undocumented
430 1 ▶ return 1 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 1 1
431 1 ▶ return ($this->length > $other->length) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->length > $other->length ? : 1 0 1
432 }
433  
434 sub len_compare {
435 12 ▶ my ($this, $other) = @_;
Subroutine len_compare: called CC=2 SCAR=7.1
Pod undocumented
436 12 ▶ return 1 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 12 12
437 12 return $this->length <=> $other->length;
438 }
439  
440 sub equal {
441 22 ▶ my ($this, $other) = @_;
Subroutine equal: called CC=4 SCAR=14.2
Pod undocumented
442 22 ▶ return 0 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 22 22
443 22 ▶ confess "Cannot compare " . (ref $this) . " with a " . (ref $other) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other eq ref $this 0 22 22
444 unless ref $other eq ref $this;
445 22 ▶ return $this->as_string eq $other->as_string ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->as_string eq $other->as_string ? : 21 1 22
446 }
447  
448 sub notequal {
449 1 ▶ my ($this, $other) = @_;
Subroutine notequal: called CC=3 SCAR=11.7
Pod undocumented
450 1 ▶ return 1 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 1 1
451 1 ▶ return $this->as_string ne $other->as_string ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->as_string ne $other->as_string ? : 1 0 1
452 }
453  
454 sub length {
455 51 ▶ my ($this) = @_;
Subroutine length: called CC=1 SCAR=0.0
Pod documented
456  
457 51 return $this->_length_helper($this->{_as_path});
458 }
459  
460 sub _length_helper {
461 105 ▶ my ($this, $path) = @_;
Subroutine _length_helper: called CC=2 SCAR=6.9
462  
463 105 my $res = 0;
464 105 foreach my $p (@{ $path }) {
465 151 $res += $p->length;
466 }
467 105 return $res;
468 }
469  
470 ## POD ##
471  
472 =pod
473  
474 =head1 NAME
475  
476 Net::BGP::ASPath - Class encapsulating BGP-4 AS Path information
477  
478 =head1 SYNOPSIS
479  
480 use Net::BGP::ASPath;
481  
482 # Constructor
483 $aspath = Net::BGP::ASPath->new(undef, { as4 => 1 });
484 $aspath2 = Net::BGP::ASPath->new([65001,65002]);
485 $aspath3 = Net::BGP::ASPath->new("(65001 65002) 65010");
486 $aspath4 = Net::BGP::ASPath->new("65001 {65011,65010}");
487  
488 # Object Copy
489 $clone = $aspath->clone();
490  
491 # Modifiers;
492 $aspath = $aspath->prepend(64999);
493 $aspath = $aspath->prepend("64999 65998");
494 $aspath = $aspath->prepend([64999,65998]);
495  
496 $aspath = $aspath->prepend("(64999 65998)");
497 $aspath = $aspath->prepend_confed("64999 65998");
498  
499 $aspath += "65001 65002"; # Same as $aspath->prepend("65001 65002")
500  
501 $aspath5 = $aspath->striped; # New object
502 $aspath = $aspath->strip; # Same modified
503  
504 $aspath = $aspath->cleanup # Same modified
505  
506 # Aggregation
507 $aspath = $aspath1->aggregate($aspath2,$aspath3);
508 $aspath = Net::BGP::ASPath->aggregate($aspath1,$aspath2,$aspath3);
509  
510  
511 # Accessor Methods
512 $length = $aspath->length;
513 $string = $aspath->as_string;
514 $array_ref = $aspath->asarray
515  
516 # In context
517 $string = "The AS path is: " . $aspath;
518 $firstas = $aspath[0];
519  
520 # Length comparisons
521 if ($aspath < $aspath2) { ... };
522 if ($aspath > $aspath2) { ... };
523 if ($aspath == $aspath2) { ... };
524 if ($aspath != $aspath2) { ... };
525 @sorted = sort { $a <=> $b } ($aspath, $aspath2, $aspath3, $aspath4);
526  
527 # Path comparisons
528 if ($aspath eq $aspath2) { ... };
529 if ($aspath ne $aspath2) { ... };
530  
531 =head1 DESCRIPTION
532  
533 This module encapsulates the data contained in a BGP-4 AS_PATH, inluding
534 confederation extentions.
535  
536 =head1 CONSTRUCTOR
537  
538 =over 4
539  
540 =item new() - create a new Net::BGP::ASPath object
541  
542 $aspath = Net::BGP::ASPath->new( PATHDATA, OPTIONS );
543  
544 This is the constructor for Net::BGP::ASPath objects. It returns a
545 reference to the newly created object. The first parameter may be either:
546  
547 =over 4
548  
549 =item ARRAY_REF
550  
551 An array ref containing AS numbers inteperted as an AS_PATH_SEQUENCE.
552  
553 =item SCALAR
554  
555 A string with AS numbers seperated by spaces (AS_PATH_SEQUANCE).
556 AS_PATH_SETs is written using "{}" with "," to seperate AS numbers.
557 AS_PATH_CONFED_* is writen equally, but encapsulated in "()".
558  
559 =item Net::BGP::ASPath
560  
561 Another ASPath object, in which case a clone is constructed.
562  
563 =item C<undef>
564  
565 This will create the ASPath object with empty contents
566  
567 =back
568  
569 Following the PATHDATA, the OPTIONS may be specified. Currently the
570 only valid option is c<as4>, which, if true, builds ASPath objects
571 usable for talking to an peer that supports 32 bit ASNs. False, or
572 the default value, assumes that the peer does not support 32 bit ASNs,
573 which affects the decode routines. Note that the encode routines
574 are not dependent upon this option.
575  
576 Basically, if as4 is true, AS_PATH is populated from messages assuming
577 4 byte ASNs and AS4_PATH is not used. Encoded AS_PATH attributes also
578 assume a 4 byte ASN.
579  
580 If as4 is false, AS_PATH is populated from messages assuming 2 byte ASNs,
581 and, if available, AS4_PATH is used to replace occurences of 23456
582 when possible when outputing to user-readable formats. Encoding routines
583 will also allow output of AS4_PATH objects when appropriate.
584  
585 =back
586  
587 =head1 OBJECT COPY
588  
589 =over 4
590  
591 =item clone() - clone a Net::BGP::ASPath object
592  
593 $clone = $aspath->clone();
594  
595 This method creates an exact copy of the Net::BGP::ASPath object.
596  
597 =back
598  
599 =head1 ACCESSOR METHODS
600  
601 =over 4
602  
603 =item length()
604  
605 Return the path-length used in BGP path selection. This is the sum
606 of the lengths of all AS_PATH elements. This does however not include
607 AS_PATH_CONFED_* elements and AS_SEGMENTS count as one BGP hop.
608  
609 =item as_string()
610  
611 Returns the path as a string in same notation as the constructor accept.
612  
613 =item cleanup()
614  
615 Reduce the path by removing meaningless AS_PATH elements (empty sets or
616 sequences) and joining neighbour elements of same _SET type.
617  
618 =item strip()
619  
620 Strips AS_CONFED_* segments from the path.
621  
622 =item striped()
623  
624 Returns a strip() 'ed clone() of the path.
625  
626 =item prepend(ARRAY)
627  
628 =item prepend(SCALAR)
629  
630 Strips AS_CONFED_* segments from the path and prepends one or more AS numbers
631 to the path as given as arguments, either as an array of AS numbers or as a
632 string with space seperated AS numbers. If string has "()" arround, prepend_confed
633 will be used instead.
634  
635 =item prepend_confed(ARRAY)
636  
637 =item prepend_confed(SCALAR)
638  
639 Prepends one or more confederation AS numbers to the path as given as
640 arguments, either as an array of AS numbers or as a string with space
641 seperated AS numbers. "()" arround the string is ignored.
642  
643 =item aggregate(ASPath)
644  
645 =item aggregate(ARRAY)
646  
647 Aggregates the current ASPath with the ASPath(s) given as argument.
648 If invoked as class method, aggregate all ASPaths given as argument.
649  
650 To aggregate means to find the longest common substring (of the paths of all
651 objects that should be aggregated) and keep them, but
652 replacing the non-common substrings with AS_SET segments. Currently only
653 the longest common normal and confederation head will be found and the remaing
654 will be left as an AS_SET and AS_CONFED_SET.
655  
656 Returns the aggregated object. The objects self are not modified.
657  
658 =back
659  
660 =head1 SEE ALSO
661  
662 B<RFC 1771>, B<RFC 1997>, Net::BGP, Net::BGP::Process, Net::BGP::Peer,
663 Net::BGP::Notification, Net::BGP::NLRI, Net::BGP::Update
664  
665 =head1 AUTHOR
666  
667 Martin Lorensen <bgp@martin.lorensen.dk>
668  
669 =cut
670  
671 ## End Package Net::BGP::ASPath ##
« BGP/Peer.pm ↑ Summary BGP/NLRI.pm »