BGP/ASPath/AS.pm

filter: Statementstmt 85.8% 109 / 127 Branchbran 71.4% 40 / 56 Conditioncond 86.3% 19 / 22 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 6 totaltotal 79.0% 189 / 239 CC 41 SCAR 24.6
CC
41
Cov
82%
new
23.9
_new_from_msg
7.1
_encode
6.0
CRAP 11.7
« BGP/Process.pm ↑ Summary BGP/Transport.pm »
1 #!/usr/bin/perl
2  
3 package Net::BGP::ASPath::AS;
4 7 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 $VERSION @ISA
11 7 );
12  
13 use overload
14 7 ▶ '<=>' => \&compare,
Subroutine BEGIN: called CC=1 SCAR=0.0
15 '""' => \&as_string,
16 7 'fallback' => 1;
17  
18 # DO NOT OVERLOAD @{} - it's an array - we need this!
19  
20 7 $VERSION = '0.18';
21  
22 7 ▶use Net::BGP::Notification qw( :errors );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 7 @Net::BGP::ASPath::AS_SEQUENCE::ISA = qw( Exporter );
25  
26 ## BGP AS_PATH Path Attribute Type Classes ##
27  
28 7 my @BGP_PATH_ATTR_CLASS = (
29 undef, # unused
30 'Net::BGP::ASPath::AS_SET', # BGP_PATH_ATTR_AS_SET
31 'Net::BGP::ASPath::AS_SEQUENCE', # BGP_PATH_ATTR_AS_SEQUENCE
32 'Net::BGP::ASPath::AS_CONFED_SEQUENCE', # BGP_PATH_ATTR_AS_CONFED_SEQUENCE
33 'Net::BGP::ASPath::AS_CONFED_SET' # BGP_PATH_ATTR_AS_CONFED_SET
34 );
35  
36 ## Public Class Methods ##
37  
38 664 ▶sub new {
Subroutine new: called CC=21 SCAR=31.7
Pod undocumented
39 664 my ($class, $value) = (shift, shift);
40  
41 664 ▶ return $value->clone if (ref $value) =~ /^Net::BGP::ASPath::AS_/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($value) =~ /^Net::BGP::ASPath::AS_/ 0 664 664
42  
43 664 my ($this, $realclass);
44  
45 664 ▶ $value = '' unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 8 656 664
46  
47 664 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 0 664 664
48  
49 # Construct SET from HASH
50 0 ▶ croak "Hash argument given for a non-set AS_PATH element"
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /_SET$/ 0 0 0
51 unless $class =~ /_SET$/;
52 0 $this->{ keys %{$value} } = values(%{$value});
53 0 bless($this, $class);
54 0 return $this;
55 }
56  
57 664 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 301 363 664
58  
59 # Construct SET from HASH
60 301 ▶ if ($class =~ /_SEQUENCE$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /_SEQUENCE$/) { } 208 93 301
61 208 push(@{$this}, @{$value});
62 } else {
63 93 $this = {};
64 93 foreach my $a (@{$value}) { $this->{$a} = 1; }
65 }
66 301 bless($this, $class);
67 301 return $this;
68 }
69  
70 363 ▶ croak "Unknown argument type ("
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 363 363
71 . (ref $value)
72 . ") parsed as argument to AS_PATH construtor."
73 if (ref $value);
74  
75 # Only a scalar left - Parse string!
76 363 my $confed = '';
77 363 ▶ if ( ($value =~ /^\((.*)\)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\((.*)\)$/ or $value eq "" and $class =~ /_CONFED_/) 134 229 363
Condition100%6/6
$value =~ /^\((.*)\)$/ or $value eq "" and $class =~ /_CONFED_/
l!l&&r!l&&!r
1304229
$value eq "" and $class =~ /_CONFED_/
!ll&&!rl&&r
22544
MC/DC100%3/3
$value =~ /^\((.*)\)$/ or $value eq "" and $class =~ /_CONFED_/
$value =~ /^\((.*)\)$/ $value eq "" $class =~ /_CONFED_/
Truth table100%4/4
A or B and C
A= $value =~ /^\((.*)\)$/B= $value eq ""C= $class =~ /_CONFED_/
ABCresult
1XX1
0111
00X0
0100
78 || ($value eq '' && $class =~ /_CONFED_/))
79 {
80 134 ▶ $value = $1 if defined($1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 130 4 134
81 134 $confed = '_CONFED';
82 }
83 363 ▶ if ( ($value =~ /^\{([0-9,]*)\}$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^\{([0-9,]*)\}$/ or $value eq '' and $class =~ /_SET$/) { } 102 261 363
elsif ($value =~ /^[0-9 ]*$/) { } 261 0 261
Condition100%6/6
$value =~ /^\{([0-9,]*)\}$/ or $value eq "" and $class =~ /_SET$/
l!l&&r!l&&!r
948261
$value eq "" and $class =~ /_SET$/
!ll&&!rl&&r
25838
MC/DC100%3/3
$value =~ /^\{([0-9,]*)\}$/ or $value eq "" and $class =~ /_SET$/
$value =~ /^\{([0-9,]*)\}$/ $value eq "" $class =~ /_SET$/
Truth table100%4/4
A or B and C
A= $value =~ /^\{([0-9,]*)\}$/B= $value eq ""C= $class =~ /_SET$/
ABCresult
1XX1
0111
00X0
0100
84 || ($value eq '' && $class =~ /_SET$/))
85 {
86 102 ▶ my $set = defined $1 ? $1 : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 94 8 102
87 102 $realclass = 'Net::BGP::ASPath::AS' . $confed . '_SET';
88 102 $this = {};
89 102 foreach my $a (split(/,/, $set)) { $this->{$a} = 1; }
90 } elsif ($value =~ /^[0-9 ]*$/) {
91 261 $realclass = 'Net::BGP::ASPath::AS' . $confed . '_SEQUENCE';
92 261 $this = [ split(' ', $value) ];
93 } else {
94 0 croak "$value is not a valid AS_PATH segment";
95 }
96  
97 363 ▶ croak "AS_PATH segment is a $realclass but was constructed as $class"
Branch: 1 branch
BranchTrueFalseTotal
if not $class =~ /::AS$/ and $class ne $realclass 0 363 363
Condition66%2/3
not $class =~ /::AS$/ and $class ne $realclass
!ll&&!rl&&r
35580
MC/DC0%0/2
not $class =~ /::AS$/ and $class ne $realclass
not $class =~ /::AS$/ $class ne $realclass
Truth table66%2/3
not(A) and B
A= not $class =~ /::AS$/B= $class ne $realclass
ABresult
0X0
100
111
98 if $class !~ /::AS$/ && $class ne $realclass;
99  
100 363 bless($this, $realclass);
101 363 return ($this);
102 }
103  
104 sub _new_from_msg
105  
106 # Constructor - returns object AND buffer with data removed
107 44 ▶{
Subroutine _new_from_msg: called CC=7 SCAR=19.6
108 44 my ($class, $buffer, $args) = @_;
109  
110 0 ▶ if (!defined($args)) { $args = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args) 0 44 44
111 44 ▶ $args->{as4} ||= 0;
Condition100%2/2
$args->{"as4"} ||= 0
l!l
935
MC/DC100%1/1
$args->{"as4"} ||= 0
$args->{"as4"}
Truth table100%2/2
A
A= $args->{"as4"}
Aresult
11
00
112  
113 44 ▶ my $size = $args->{as4} ? 4 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'as4'} ? : 9 35 44
114  
115 44 my ($type, $len) = unpack('CC', $buffer);
116  
117 44 ▶ if ( ($len * $size + 2) > length($buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len * $size + 2 > length $buffer) 0 44 44
118 0 Net::BGP::Notification->throw(
119 ErrorCode => BGP_ERROR_CODE_UPDATE_MESSAGE,
120 ErrorSubCode => BGP_ERROR_SUBCODE_BAD_AS_PATH
121 );
122 }
123  
124 44 my @list;
125 44 ▶ if ($args->{as4}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'as4'}) { } 9 35 44
126 9 @list = unpack('N*', substr($buffer,2,(4*$len)) );
127 } else {
128 35 @list = unpack('n*', substr($buffer,2,(2*$len)) );
129 }
130 44 $class = $BGP_PATH_ATTR_CLASS[$type];
131  
132 44 ▶ if (length($buffer) > 2+($size*$len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer > 2 + $size * $len) { } 10 34 44
133 10 $buffer = substr($buffer, 2+($size*$len));
134 } else {
135 34 $buffer = '';
136 }
137 44 return ($class->new(\@list), $buffer);
138 }
139  
140 # This encodes the standard AS Path
141 # TODO: Note that if AS4 != True, then there is an issue with this code.
142 # In particular, it will stick 23456 into the confederation types. In
143 # theory, no confederation using AS4 should be transmitting confed types
144 # to any node that is NOT using AS4, per RFC4893.
145 #
146 # But when this breaks the internet, it's not my fault.
147 35 ▶sub _encode {
Subroutine _encode: called CC=6 SCAR=17.9
148 35 my ($this, $args) = @_;
149 0 ▶ if (!defined($args)) { $args = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args) 0 35 35
150 35 ▶ $args->{as4} ||= 0;
Condition100%2/2
$args->{"as4"} ||= 0
l!l
629
MC/DC100%1/1
$args->{"as4"} ||= 0
$args->{"as4"}
Truth table100%2/2
A
A= $args->{"as4"}
Aresult
11
00
151  
152 35 my $list = $this->asarray;
153 35 my $len = scalar @{$list};
154 35 my $type = $this->type;
155  
156 35 my $msg;
157 35 ▶ if (!($args->{as4})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args->{'as4'}) { } 29 6 35
158 29 $msg = pack('CC', $type, $len);
159 29 foreach my $as ( @{$list} ) {
160 371 ▶ $msg .= ($as <= 65535) ? pack('n', $as) : pack('n', 23456);
Branch: 1 branch
BranchTrueFalseTotal
$as <= 65535 ? : 367 4 371
161 }
162 } else {
163 6 $msg = pack('CCN*', $type, $len, @{$list});
164 }
165  
166 35 return $msg;
167 }
168  
169 # Determines if the path element has any ASNs > 23456
170 33 ▶sub _has_as4 {
Subroutine _has_as4: called CC=4 SCAR=13.9
171 33 my ($this) = @_;
172
173 33 ▶ if ( ref($this) =~ /_CONFED_/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($this) =~ /_CONFED_/) 10 23 33
174 # No confeds in AS4_ paths
175 10 return 0;
176 }
177  
178 23 my $list = $this->asarray;
179 23 foreach my $as ( @{$list} ) {
180 342 ▶ if ($as > 65535) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($as > 65535) 6 336 342
181 }
182  
183 17 return 0;
184 }
185  
186 0 ▶sub compare {
Subroutine compare: not called CC=2 SCAR=17.9
Pod undocumented
187 0 my ($this, $other) = @_;
188 0 ▶ return undef unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 0 0
189 0 return $this->length <=> $other->length;
190 }
191  
192 253 ▶sub clone {
Subroutine clone: called CC=4 SCAR=14.1
Pod undocumented
193 253 my $proto = shift;
194 253 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
25300
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
195 253 ▶ $proto = shift unless ref $proto;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $proto 0 253 253
196  
197 253 my $clone;
198 253 ▶ if ($class =~ /_SET$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /_SET$/) { } 81 172 253
199 81 return $class->new([ keys %{$proto} ]);
200 } else {
201 172 return $class->new([ @{$proto} ]); # Unblessed!
202 }
203 }
204  
205 0 ▶sub asstring {
Subroutine asstring: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my $this = shift;
207 0 return $this->as_string(@_);
208 }
209  
210 8 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=7.1
Pod undocumented
211 8 my $this = shift;
212 8 ▶ croak 'Instance of ASPath::AS should not exist!'
Branch: 1 branch
BranchTrueFalseTotal
if ref $this eq "Net::BGP::ASPath::AS" 0 8 8
213 if (ref $this eq 'Net::BGP::ASPath::AS');
214 8 return $this->as_string;
215 }
216  
217 0 ▶sub asarray {
Subroutine asarray: not called CC=2 SCAR=17.9
Pod undocumented
218 0 my $this = shift;
219 0 ▶ croak 'Instance of ASPath::AS should not exist!'
Branch: 1 branch
BranchTrueFalseTotal
if ref $this eq "Net::BGP::ASPath::AS" 0 0 0
220 if (ref $this eq 'Net::BGP::ASPath::AS');
221 0 return $this->asarray;
222 }
223  
224 7 1;
« BGP/Process.pm ↑ Summary BGP/Transport.pm »