BGP/Transport.pm

filter: Statementstmt 73.8% 444 / 601 Branchbran 53.6% 118 / 220 Conditioncond 31.4% 11 / 35 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 83.9% 99 / 118 Podpod 12.5% 7 / 56 totaltotal 64.6% 680 / 1052 SCAR 65.4
CC
125
Cov
67%
_handle_socket_read_ready
47.0
_handle_bgp_open_received
22.2
_handle_bgp_start_event
21.2
CRAP 689.6
↑ Summary BGP/Update.pm »
1 #!/usr/bin/perl
2  
3 package Net::BGP::Transport;
4 4 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Errno qw(EAGAIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 use vars qw(
9 $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @BGP @GENERIC
10 @BGP_EVENT_MESSAGE_MAP @BGP_EVENTS @BGP_FSM @BGP_STATES
11 4 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ## Inheritance and Versioning ##
14  
15 4 @ISA = qw( Exporter );
16 4 $VERSION = '0.16';
17  
18 ## General Definitions ##
19  
20 65 ▶sub TRUE { 1 }
Subroutine TRUE: called CC=1 SCAR=0.0
Pod undocumented
21 70 ▶sub FALSE { 0 }
Subroutine FALSE: called CC=1 SCAR=0.0
Pod undocumented
22  
23 ## BGP Network Constants ##
24  
25 0 ▶sub BGP_PORT { 179 }
Subroutine BGP_PORT: not called CC=1 SCAR=6.9
Pod undocumented
26 11 ▶sub BGP_VERSION_4 { 4 }
Subroutine BGP_VERSION_4: called CC=1 SCAR=0.0
Pod undocumented
27  
28 ## BGP General Constant Definitions ##
29  
30 114 ▶sub BGP_MESSAGE_HEADER_LENGTH { 19 }
Subroutine BGP_MESSAGE_HEADER_LENGTH: called CC=1 SCAR=0.0
Pod undocumented
31 20 ▶sub BGP_MAX_MESSAGE_LENGTH { 4096 }
Subroutine BGP_MAX_MESSAGE_LENGTH: called CC=1 SCAR=0.0
Pod undocumented
32 6 ▶sub BGP_CONNECT_RETRY_TIME { 120 }
Subroutine BGP_CONNECT_RETRY_TIME: called CC=1 SCAR=0.0
Pod undocumented
33 6 ▶sub BGP_HOLD_TIME { 90 }
Subroutine BGP_HOLD_TIME: called CC=1 SCAR=0.0
Pod undocumented
34 6 ▶sub BGP_KEEPALIVE_TIME { 30 }
Subroutine BGP_KEEPALIVE_TIME: called CC=1 SCAR=0.0
Pod undocumented
35  
36 ## BGP Finite State Machine State Enumerations ##
37  
38 18 ▶sub BGP_STATE_IDLE { 1 }
Subroutine BGP_STATE_IDLE: called CC=1 SCAR=0.0
Pod undocumented
39 4 ▶sub BGP_STATE_CONNECT { 2 }
Subroutine BGP_STATE_CONNECT: called CC=1 SCAR=0.0
Pod undocumented
40 0 ▶sub BGP_STATE_ACTIVE { 3 }
Subroutine BGP_STATE_ACTIVE: not called CC=1 SCAR=6.9
Pod undocumented
41 4 ▶sub BGP_STATE_OPEN_SENT { 4 }
Subroutine BGP_STATE_OPEN_SENT: called CC=1 SCAR=0.0
Pod undocumented
42 22 ▶sub BGP_STATE_OPEN_CONFIRM { 5 }
Subroutine BGP_STATE_OPEN_CONFIRM: called CC=1 SCAR=0.0
Pod undocumented
43 64 ▶sub BGP_STATE_ESTABLISHED { 6 }
Subroutine BGP_STATE_ESTABLISHED: called CC=1 SCAR=0.0
Pod undocumented
44  
45 ## BGP State Names ##
46  
47 4 @BGP_STATES = qw( Null Idle Connect Active OpenSent OpenConfirm Established );
48  
49 ## BGP Event Enumerations ##
50  
51 5 ▶sub BGP_EVENT_START { 1 }
Subroutine BGP_EVENT_START: called CC=1 SCAR=0.0
Pod undocumented
52 0 ▶sub BGP_EVENT_STOP { 2 }
Subroutine BGP_EVENT_STOP: not called CC=1 SCAR=6.9
Pod undocumented
53 4 ▶sub BGP_EVENT_TRANSPORT_CONN_OPEN { 3 }
Subroutine BGP_EVENT_TRANSPORT_CONN_OPEN: called CC=1 SCAR=0.0
Pod undocumented
54 0 ▶sub BGP_EVENT_TRANSPORT_CONN_CLOSED { 4 }
Subroutine BGP_EVENT_TRANSPORT_CONN_CLOSED: not called CC=1 SCAR=6.9
Pod undocumented
55 0 ▶sub BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED { 5 }
Subroutine BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED: not called CC=1 SCAR=6.9
Pod undocumented
56 0 ▶sub BGP_EVENT_TRANSPORT_FATAL_ERROR { 6 }
Subroutine BGP_EVENT_TRANSPORT_FATAL_ERROR: not called CC=1 SCAR=6.9
Pod undocumented
57 76 ▶sub BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED { 7 }
Subroutine BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED: called CC=1 SCAR=0.0
Pod undocumented
58 76 ▶sub BGP_EVENT_HOLD_TIMER_EXPIRED { 8 }
Subroutine BGP_EVENT_HOLD_TIMER_EXPIRED: called CC=1 SCAR=0.0
Pod undocumented
59 76 ▶sub BGP_EVENT_KEEPALIVE_TIMER_EXPIRED { 9 }
Subroutine BGP_EVENT_KEEPALIVE_TIMER_EXPIRED: called CC=1 SCAR=0.0
Pod undocumented
60 4 ▶sub BGP_EVENT_RECEIVE_OPEN_MESSAGE { 10 }
Subroutine BGP_EVENT_RECEIVE_OPEN_MESSAGE: called CC=1 SCAR=0.0
Pod undocumented
61 12 ▶sub BGP_EVENT_RECEIVE_KEEP_ALIVE_MESSAGE { 11 }
Subroutine BGP_EVENT_RECEIVE_KEEP_ALIVE_MESSAGE: called CC=1 SCAR=0.0
Pod undocumented
62 4 ▶sub BGP_EVENT_RECEIVE_UPDATE_MESSAGE { 12 }
Subroutine BGP_EVENT_RECEIVE_UPDATE_MESSAGE: called CC=1 SCAR=0.0
Pod undocumented
63 4 ▶sub BGP_EVENT_RECEIVE_NOTIFICATION_MESSAGE { 13 }
Subroutine BGP_EVENT_RECEIVE_NOTIFICATION_MESSAGE: called CC=1 SCAR=0.0
Pod undocumented
64 4 ▶sub BGP_EVENT_RECEIVE_REFRESH_MESSAGE { 14 }
Subroutine BGP_EVENT_RECEIVE_REFRESH_MESSAGE: called CC=1 SCAR=0.0
Pod undocumented
65  
66 ## BGP Event Names ##
67  
68 4 @BGP_EVENTS = (
69 'Null',
70 'BGP Start',
71 'BGP Stop',
72 'BGP Transport connection open',
73 'BGP Transport connection closed',
74 'BGP Transport connection open failed',
75 'BGP Transport fatal error',
76 'ConnectRetry timer expired',
77 'Hold Timer expired',
78 'KeepAlive timer expired',
79 'Receive OPEN message',
80 'Receive KEEPALIVE message',
81 'Receive UPDATE message',
82 'Receive NOTIFICATION message',
83 'Receive REFRESH message'
84 );
85  
86 ## BGP Protocol Message Type Enumerations ##
87  
88 26 ▶sub BGP_MESSAGE_OPEN { 1 }
Subroutine BGP_MESSAGE_OPEN: called CC=1 SCAR=0.0
Pod undocumented
89 4 ▶sub BGP_MESSAGE_UPDATE { 2 }
Subroutine BGP_MESSAGE_UPDATE: called CC=1 SCAR=0.0
Pod undocumented
90 2 ▶sub BGP_MESSAGE_NOTIFICATION { 3 }
Subroutine BGP_MESSAGE_NOTIFICATION: called CC=1 SCAR=0.0
Pod undocumented
91 28 ▶sub BGP_MESSAGE_KEEPALIVE { 4 }
Subroutine BGP_MESSAGE_KEEPALIVE: called CC=1 SCAR=0.0
Pod undocumented
92 22 ▶sub BGP_MESSAGE_REFRESH { 5 }
Subroutine BGP_MESSAGE_REFRESH: called CC=1 SCAR=0.0
Pod undocumented
93  
94 ## BGP Open Optional Parameter Types ##
95  
96 0 ▶sub BGP_OPTION_AUTH { 1 }
Subroutine BGP_OPTION_AUTH: not called CC=1 SCAR=6.9
Pod undocumented
97 36 ▶sub BGP_OPTION_CAPABILITIES { 2 }
Subroutine BGP_OPTION_CAPABILITIES: called CC=1 SCAR=0.0
Pod undocumented
98  
99 ## BGP Open Capabilities Parameter Types
100 23 ▶sub BGP_CAPABILITY_MBGP { 1 }
Subroutine BGP_CAPABILITY_MBGP: called CC=1 SCAR=0.0
Pod undocumented
101 23 ▶sub BGP_CAPABILITY_REFRESH { 2 }
Subroutine BGP_CAPABILITY_REFRESH: called CC=1 SCAR=0.0
Pod undocumented
102 20 ▶sub BGP_CAPABILITY_AS4 { 65 }
Subroutine BGP_CAPABILITY_AS4: called CC=1 SCAR=0.0
Pod undocumented
103 23 ▶sub BGP_CAPABILITY_REFRESH_OLD { 128 }
Subroutine BGP_CAPABILITY_REFRESH_OLD: called CC=1 SCAR=0.0
Pod undocumented
104  
105 ## Event-Message Type Correlation ##
106  
107 4 @BGP_EVENT_MESSAGE_MAP = (
108 undef,
109 BGP_EVENT_RECEIVE_OPEN_MESSAGE,
110 BGP_EVENT_RECEIVE_UPDATE_MESSAGE,
111 BGP_EVENT_RECEIVE_NOTIFICATION_MESSAGE,
112 BGP_EVENT_RECEIVE_KEEP_ALIVE_MESSAGE,
113 BGP_EVENT_RECEIVE_REFRESH_MESSAGE
114 );
115  
116 ## BGP FSM State Transition Table ##
117  
118 4 @BGP_FSM = (
119 undef, # Null (zero placeholder)
120  
121 [ # Idle
122 \&_close_session, # Default transition
123 \&_handle_bgp_start_event # BGP_EVENT_START
124 ],
125 [ # Connect
126 \&_close_session, # Default transition
127 \&_ignore_start_event, # BGP_EVENT_START
128 undef, # BGP_EVENT_STOP
129 \&_handle_bgp_conn_open, # BGP_EVENT_TRANSPORT_CONN_OPEN
130 undef, # BGP_EVENT_TRANSPORT_CONN_CLOSED
131 \&_handle_connect_retry_restart, # BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED
132 undef, # BGP_EVENT_TRANSPORT_FATAL_ERROR
133 \&_handle_bgp_start_event # BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED
134 ],
135 [ # Active
136 \&_close_session, # Default transition
137 \&_ignore_start_event, # BGP_EVENT_START
138 undef, # BGP_EVENT_STOP
139 \&_handle_bgp_conn_open, # BGP_EVENT_TRANSPORT_CONN_OPEN
140 undef, # BGP_EVENT_TRANSPORT_CONN_CLOSED
141 \&_handle_connect_retry_restart, # BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED
142 undef, # BGP_EVENT_TRANSPORT_FATAL_ERROR
143 \&_handle_bgp_start_event # BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED
144 ],
145 [ # OpenSent
146 \&_handle_bgp_fsm_error, # Default transition
147 \&_ignore_start_event, # BGP_EVENT_START
148 \&_cease, # BGP_EVENT_STOP
149 undef, # BGP_EVENT_TRANSPORT_CONN_OPEN
150 \&_handle_open_sent_disconnect, # BGP_EVENT_TRANSPORT_CONN_CLOSED
151 undef, # BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED
152 \&_close_session, # BGP_EVENT_TRANSPORT_FATAL_ERROR
153 undef, # BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED
154 \&_handle_hold_timer_expired, # BGP_EVENT_HOLD_TIMER_EXPIRED
155 undef, # BGP_EVENT_KEEPALIVE_TIMER_EXPIRED
156 \&_handle_bgp_open_received # BGP_EVENT_RECEIVE_OPEN_MESSAGE
157 ],
158 [ # OpenConfirm
159 \&_handle_bgp_fsm_error, # Default transition
160 \&_ignore_start_event, # BGP_EVENT_START
161 \&_cease, # BGP_EVENT_STOP
162 undef, # BGP_EVENT_TRANSPORT_CONN_OPEN
163 \&_close_session, # BGP_EVENT_TRANSPORT_CONN_CLOSED
164 undef, # BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED
165 \&_close_session, # BGP_EVENT_TRANSPORT_FATAL_ERROR
166 undef, # BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED
167 \&_handle_hold_timer_expired, # BGP_EVENT_HOLD_TIMER_EXPIRED
168 \&_handle_keepalive_expired, # BGP_EVENT_KEEPALIVE_TIMER_EXPIRED
169 undef, # BGP_EVENT_RECEIVE_OPEN_MESSAGE
170 \&_handle_receive_keepalive_message, # BGP_EVENT_RECEIVE_KEEP_ALIVE_MESSAGE
171 undef, # BGP_EVENT_RECEIVE_UPDATE_MESSAGE
172 \&_handle_receive_notification_message,# BGP_EVENT_RECEIVE_NOTIFICATION_MESSAGE
173 \&_handle_receive_refresh_message # BGP_EVENT_RECEIVE_REFRESH_MESSAGE
174 ],
175 [ # Established
176 \&_handle_bgp_fsm_error, # Default transition
177 \&_ignore_start_event, # BGP_EVENT_START
178 \&_cease, # BGP_EVENT_STOP
179 undef, # BGP_EVENT_TRANSPORT_CONN_OPEN
180 \&_close_session, # BGP_EVENT_TRANSPORT_CONN_CLOSED
181 undef, # BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED
182 \&_close_session, # BGP_EVENT_TRANSPORT_FATAL_ERROR
183 undef, # BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED
184 \&_handle_hold_timer_expired, # BGP_EVENT_HOLD_TIMER_EXPIRED
185 \&_handle_keepalive_expired, # BGP_EVENT_KEEPALIVE_TIMER_EXPIRED
186 undef, # BGP_EVENT_RECEIVE_OPEN_MESSAGE
187 \&_handle_receive_keepalive_message, # BGP_EVENT_RECEIVE_KEEP_ALIVE_MESSAGE
188 \&_handle_receive_update_message, # BGP_EVENT_RECEIVE_UPDATE_MESSAGE
189 \&_handle_receive_notification_message,# BGP_EVENT_RECEIVE_NOTIFICATION_MESSAGE
190 \&_handle_receive_refresh_message # BGP_EVENT_RECEIVE_REFRESH_MESSAGE
191 ]
192 );
193  
194 ## Socket States ##
195  
196 62 ▶sub AWAITING_HEADER_START { 1 }
Subroutine AWAITING_HEADER_START: called CC=1 SCAR=0.0
Pod undocumented
197 12 ▶sub AWAITING_HEADER_FRAGMENT { 2 }
Subroutine AWAITING_HEADER_FRAGMENT: called CC=1 SCAR=0.0
Pod undocumented
198 24 ▶sub AWAITING_MESSAGE_FRAGMENT { 3 }
Subroutine AWAITING_MESSAGE_FRAGMENT: called CC=1 SCAR=0.0
Pod undocumented
199  
200 ## Export Tag Definitions ##
201  
202 4 @EXPORT = ();
203 4 @EXPORT_OK = ();
204 4 %EXPORT_TAGS = (
205 ALL => [ @EXPORT, @EXPORT_OK ]
206 );
207  
208 ## Module Imports ##
209  
210 4 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
211 4 ▶use Errno qw(EINPROGRESS ENOTCONN);
Subroutine BEGIN: called CC=1 SCAR=0.0
212 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
213 4 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
214 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 4 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
216 4 ▶use Net::BGP::Notification qw( :errors );
Subroutine BEGIN: called CC=1 SCAR=0.0
217 4 ▶use Net::BGP::Refresh;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 4 ▶use Net::BGP::Update;
Subroutine BEGIN: called CC=1 SCAR=0.0
219  
220 ## Generic Subroutines ##
221  
222 # This subroutine was snicked from David Town's excellent Net::SNMP
223 # module and renamed as dump_hex(). Removed class dependence and made
224 # into standalone subroutine.
225  
226 sub dump_hex
227 {
228 0 ▶ my $data = shift();
Subroutine dump_hex: not called CC=3 SCAR=24.8
Pod undocumented
229 0 my ($length, $offset, $line, $hex) = (0, 0, '', '');
230 0 my $string;
231  
232 0 $string = '';
233 0 $length = length($data);
234  
235 0 ▶ while ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
236 0 ▶ if ($length >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length >= 16) { } 0 0 0
237 0 $line = substr($data, $offset, 16);
238 } else {
239 0 $line = substr($data, $offset, $length);
240 }
241 0 $hex = unpack('H*', $line);
242 0 $hex .= ' ' x (32 - length($hex));
243 0 $hex = sprintf("%s %s %s %s " x 4, unpack('a2' x 16, $hex));
244 0 $line =~ s/[\x00-\x1f\x7f-\xff]/./g;
245 0 $string .= sprintf("[%03d] %s %s\n", $offset, uc($hex), $line);
246 0 $offset += 16;
247 0 $length -= 16;
248 }
249  
250 0 return ( $string );
251 }
252  
253 ## Public Class Methods ##
254  
255 sub new
256 {
257 6 ▶ my $class = shift();
Subroutine new: called CC=7 SCAR=20.3
Pod documented
258 6 my ($arg, $value);
259  
260 6 my $this = {
261 _parent => undef,
262 _sibling => undef,
263 _bgp_version => BGP_VERSION_4,
264 _fsm_state => BGP_STATE_IDLE,
265 _peer_refresh => FALSE,
266 _peer_as4 => FALSE,
267 _peer_mbgp => FALSE,
268 _peer_announced_id => undef,
269 _event_queue => [],
270 _message_queue => [],
271 _hold_time => BGP_HOLD_TIME,
272 _hold_timer => undef,
273 _keep_alive_time => BGP_KEEPALIVE_TIME,
274 _keep_alive_timer => undef,
275 _connect_retry_time => BGP_CONNECT_RETRY_TIME,
276 _connect_retry_timer => undef,
277 _peer_socket => undef,
278 _peer_socket_connected => FALSE, # is AWARE - Not established, not socket->connected!
279 _last_timer_update => undef,
280 _in_msg_buffer => '',
281 _in_msg_buf_state => AWAITING_HEADER_START,
282 _in_msg_buf_bytes_exp => 0,
283 _in_msg_buf_type => 0,
284 _out_msg_buffer => ''
285 };
286  
287 6 bless($this, $class);
288  
289 6 ▶ while ( defined($arg = shift()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($arg = shift())) 15 6 21
290 15 $value = shift();
291  
292 15 ▶ if ( $arg =~ /start/i ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($arg =~ /start/i) { } 5 10 15
elsif ($arg =~ /parent/i) { } 6 4 10
elsif ($arg =~ /holdtime/i) { } 0 4 4
elsif ($arg =~ /connectretrytime/i) { } 0 4 4
elsif ($arg =~ /keepalivetime/i) { } 4 0 4
293 5 $this->start();
294 }
295 elsif ( $arg =~ /parent/i ) {
296 6 $this->{_parent} = $value;
297 }
298 elsif ( $arg =~ /holdtime/i ) {
299 0 $this->{_hold_time} = $value;
300 }
301 elsif ( $arg =~ /connectretrytime/i ) {
302 0 $this->{_connect_retry_time} = $value;
303 }
304 elsif ( $arg =~ /keepalivetime/i ) {
305 4 $this->{_keep_alive_time} = $value;
306 }
307 else {
308 0 croak "unrecognized argument $arg\n";
309 }
310 }
311  
312 6 return ( $this );
313 }
314  
315 ## Public Object Methods ##
316  
317 sub start
318 {
319 5 ▶ my $this = shift();
Subroutine start: called CC=1 SCAR=0.0
Pod documented
320 5 $this->_enqueue_event(BGP_EVENT_START);
321 }
322  
323 sub stop
324 {
325 6 ▶ my $this = shift();
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
326 6 $this->{_fsm_state} = $this->_cease();
327 }
328  
329 sub version
330 {
331 0 ▶ return shift->{_bgp_version};
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
332 }
333  
334 sub is_established
335 {
336 0 ▶ return ( (shift->{_fsm_state} == BGP_STATE_ESTABLISHED) ? 1 : 0 );
Subroutine is_established: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'_fsm_state'} == BGP_STATE_ESTABLISHED() ? : 0 0 0
337 }
338  
339 sub can_refresh
340 {
341 1 ▶ return shift->{_peer_refresh};
Subroutine can_refresh: called CC=1 SCAR=0.0
Pod undocumented
342 }
343  
344 sub can_as4
345 {
346 2 ▶ return shift->{_peer_as4};
Subroutine can_as4: called CC=1 SCAR=0.0
Pod undocumented
347 }
348  
349 sub can_mbgp
350 {
351 3 ▶ return shift->{_peer_mbgp};
Subroutine can_mbgp: called CC=1 SCAR=0.0
Pod undocumented
352 }
353  
354 sub update
355 {
356 4 ▶ my ($this, $update) = @_;
Subroutine update: called CC=2 SCAR=7.0
Pod documented
357  
358 4 my $result = FALSE;
359 4 ▶ if ( $this->{_fsm_state} == BGP_STATE_ESTABLISHED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_fsm_state"} == BGP_STATE_ESTABLISHED()) 4 0 4
360  
361 4 my $encoded = $update->_encode_message( { as4 => $this->{_peer_as4} } );
362  
363 4 my $buffer = $this->_encode_bgp_update_message($encoded);
364 4 $this->_send_msg($buffer);
365 4 $result = TRUE;
366 }
367  
368 4 return $result;
369 }
370  
371 sub refresh
372 {
373 2 ▶ my $this = shift;
Subroutine refresh: called CC=4 SCAR=14.5
Pod documented
374  
375 2 my ($refresh) = @_;
376 2 ▶ $refresh = Net::BGP::Refresh->new(@_) unless ref $refresh eq 'Net::BGP::Refresh';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $refresh eq "Net::BGP::Refresh" 2 0 2
377  
378 2 my $result = FALSE;
379 2 ▶ if (( $this->{_fsm_state} == BGP_STATE_ESTABLISHED ) && $this->{_peer_refresh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_fsm_state"} == BGP_STATE_ESTABLISHED() and $this->{"_peer_refresh"}) 2 0 2
Condition33%1/3
$this->{"_fsm_state"} == BGP_STATE_ESTABLISHED() and $this->{"_peer_refresh"}
!ll&&!rl&&r
002
MC/DC0%0/2
$this->{"_fsm_state"} == BGP_STATE_ESTABLISHED() and $this->{"_peer_refresh"}
$this->{"_fsm_state"} == BGP_STATE_ESTABLISHED() $this->{"_peer_refresh"}
Truth table33%1/3
A and B
A= $this->{"_fsm_state"} == BGP_STATE_ESTABLISHED()B= $this->{"_peer_refresh"}
ABresult
0X0
100
111
380 2 my $buffer = $this->_encode_bgp_refresh_message($refresh->_encode_message());
381 2 $this->_send_msg($buffer);
382 2 $result = TRUE;
383 }
384  
385 2 return $result;
386 }
387  
388 sub parent
389 {
390 110 ▶ return shift->{_parent};
Subroutine parent: called CC=1 SCAR=0.0
Pod undocumented
391 }
392  
393 sub sibling
394 {
395 238 ▶ my $this = shift();
Subroutine sibling: called CC=3 SCAR=16.5
Pod undocumented
396 238 ▶ return undef unless defined $this->{_sibling};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"_sibling"} 238 0 238
397 0 ▶ return undef unless $this->parent->transport eq $this;
Branch: 1 branch
BranchTrueFalseTotal
unless $this->parent->transport eq $this 0 0 0
398 0 return $this->{_sibling};
399 }
400  
401 ## Private Class Methods ##
402  
403 sub _clone
404 {
405 0 ▶ my $this = shift();
Subroutine _clone: not called CC=4 SCAR=30.0
406  
407 0 ▶ croak 'Cannot have more than one clone at a time!' if defined $this->{_sibling};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_sibling"} 0 0 0
408  
409 0 my $clone = {};
410 0 foreach my $key ( keys(%{ $this }) ) {
411 0 $clone->{$key} = $this->{$key};
412 }
413  
414 0 bless($clone, ref($this));
415  
416 # override some of the inherited properties
417 0 $clone->{_peer_refresh} = FALSE;
418 0 $clone->{_peer_announced_id} = undef;
419 0 $clone->{_hold_timer} = undef;
420 0 $clone->{_keep_alive_timer} = undef;
421 0 $clone->{_fsm_state} = BGP_STATE_CONNECT;
422 0 $clone->{_event_queue} = [];
423 0 $clone->{_message_queue} = [];
424 0 $clone->{_peer_socket} = undef;
425 0 $clone->{_peer_socket_connected}= FALSE;
426 0 $clone->{_connect_retry_timer} = undef;
427 0 $clone->{_last_timer_update} = undef;
428 0 $clone->{_in_msg_buffer} = '';
429 0 $clone->{_in_msg_buf_state} = AWAITING_HEADER_START;
430 0 $clone->{_in_msg_buf_bytes_exp} = 0;
431 0 $clone->{_in_msg_buf_type} = 0;
432 0 $clone->{_out_msg_buffer} = '';
433 0 $clone->{_sibling} = $this;
434 0 $this->{_sibling} = $clone;
435  
436 0 ▶ if ( $this->{_fsm_state} != BGP_STATE_IDLE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_fsm_state"} != BGP_STATE_IDLE()) 0 0 0
437 0 $clone->start();
438 }
439  
440 0 return $clone;
441 }
442  
443 ## Private Object Methods ##
444  
445 ## This creates AND throws a ::Notification object.
446 sub _error
447 {
448 0 ▶ my $this = shift();
Subroutine _error: not called CC=2 SCAR=17.9
449  
450 0 ▶ Net::BGP::Notification->throw(
Condition0%0/3
shift() || BGP_ERROR_SUBCODE_NULL()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || BGP_ERROR_SUBCODE_NULL()
shift() BGP_ERROR_SUBCODE_NULL()
Truth table0%0/3
A || B
A= shift()B= BGP_ERROR_SUBCODE_NULL()
ABresult
1X1
011
000
451 ErrorCode => shift(),
452 ErrorSubCode => shift() || BGP_ERROR_SUBCODE_NULL,
453 ErrorData => shift()
454 );
455 }
456  
457 sub _is_connected
458 {
459 66 ▶ my $this = shift();
Subroutine _is_connected: called CC=1 SCAR=0.0
460 66 return ( $this->{_peer_socket_connected} );
461 }
462  
463 sub _get_socket
464 {
465 80 ▶ my $this = shift();
Subroutine _get_socket: called CC=1 SCAR=0.0
466 80 return ( $this->{_peer_socket} );
467 }
468  
469 sub _set_socket
470 {
471 2 ▶ my ($this, $socket) = @_;
Subroutine _set_socket: called CC=1 SCAR=0.0
472 2 $this->{_peer_socket} = $socket;
473 2 $this->{_peer_socket_connected} = TRUE;
474 }
475  
476 sub _enqueue_event
477 {
478 33 ▶ my $this = shift();
Subroutine _enqueue_event: called CC=1 SCAR=0.0
479 33 push(@{ $this->{_event_queue} }, shift());
480 }
481  
482 sub _dequeue_event
483 {
484 108 ▶ my $this = shift();
Subroutine _dequeue_event: called CC=1 SCAR=0.0
485 108 return ( shift(@{ $this->{_event_queue} }) );
486 }
487  
488 sub _enqueue_message
489 {
490 12 ▶ my $this = shift();
Subroutine _enqueue_message: called CC=1 SCAR=0.0
491 12 push(@{ $this->{_message_queue} }, shift());
492 }
493  
494 sub _dequeue_message
495 {
496 12 ▶ my $this = shift();
Subroutine _dequeue_message: called CC=1 SCAR=0.0
497 12 return ( shift(@{ $this->{_message_queue} }) );
498 }
499  
500 sub _handle_event
501 {
502 32 ▶ my ($this, $event) = @_;
Subroutine _handle_event: called CC=10 SCAR=26.4
503  
504 32 my $state = my $next_state = $this->{_fsm_state};
505  
506 32 ▶ my $action =
Condition40%2/5
$Net::BGP::Transport::BGP_FSM[$state][$event] || $Net::BGP::Transport::BGP_FSM[$state][0] || undef
l!l
320
$Net::BGP::Transport::BGP_FSM[$state][$event] or $Net::BGP::Transport::BGP_FSM[$state][0]
l!l&&r!l&&!r
3200
MC/DC0%0/2
$Net::BGP::Transport::BGP_FSM[$state][$event] || $Net::BGP::Transport::BGP_FSM[$state][0] || undef
$Net::BGP::Transport::BGP_FSM[$state][$event] $Net::BGP::Transport::BGP_FSM[$state][0]
Truth table33%1/3
A or B
A= $Net::BGP::Transport::BGP_FSM[$state][$event]B= $Net::BGP::Transport::BGP_FSM[$state][0]
ABresult
1X1
011
000
507 $BGP_FSM[$state]->[$event]
508 || $BGP_FSM[$state]->[0] ## default action
509 || undef ;
510  
511 32 eval {
512 32 ▶ $next_state = $action->($this) if defined $action;
Branch: 1 branch
BranchTrueFalseTotal
if defined $action 32 0 32
513 };
514 32 ▶ if (my $oops = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $oops = $@) 0 32 32
515 {
516 0 ▶ if (UNIVERSAL::isa($oops, 'Net::BGP::Notification'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($oops, 'Net::BGP::Notification')) { } 0 0 0
517 {
518 0 $this->_kill_session($oops);
519 0 $next_state = BGP_STATE_IDLE;
520 }
521 else
522 {
523 0 die $oops;
524 }
525 }
526  
527 # transition to next state
528 32 ▶ $this->{_fsm_state} = $next_state if defined $next_state;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_state 32 0 32
529  
530 ## trigger callbacks if we changed states
531 32 ▶ if ($next_state != $state)
Branch: 1 branch
BranchTrueFalseTotal
if ($next_state != $state) 18 14 32
532 {
533 18 ▶ if ( $state == BGP_STATE_ESTABLISHED )
Branch: 2 branches
BranchTrueFalseTotal
if ($state == BGP_STATE_ESTABLISHED()) { } 2 16 18
elsif ($next_state == BGP_STATE_ESTABLISHED()) { } 4 12 16
534 {
535 ## session has terminated
536 ##
537 2 $this->parent->reset_callback(undef)
538 }
539 elsif ( $next_state == BGP_STATE_ESTABLISHED )
540 {
541 ## newly established session
542 ##
543 4 $this->parent->refresh_callback(undef);
544 }
545  
546 # trigger post-transition actions
547 18 $this->_trigger_post_transition_action($state, $next_state);
548 }
549 }
550  
551 sub _trigger_post_transition_action
552 {
553 18 ▶ my ($this, $pre_state, $pos_state) = @_;
Subroutine _trigger_post_transition_action: called CC=3 SCAR=11.0
554  
555 # TODO:
556 #
557 # This needs to be broken out into a separate table similar to $BGP_FSM
558 # which triggers actions prior to state transition. Or, alternately,
559 # $BGP_FSM could be augmented to have an array of subrefs, one each for the
560 # pre- and post- transition action, rather than the current scalar subref.
561 # But I'm too lazy to refactor the entire table right now, so just handle
562 # the single current use case of firing the ESTABLISHED callback...
563  
564 18 ▶ if (($pre_state == BGP_STATE_OPEN_CONFIRM) && ($pos_state == BGP_STATE_ESTABLISHED)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_state == BGP_STATE_OPEN_CONFIRM() and $pos_state == BGP_STATE_ESTABLISHED()) 4 14 18
Condition66%2/3
$pre_state == BGP_STATE_OPEN_CONFIRM() and $pos_state == BGP_STATE_ESTABLISHED()
!ll&&!rl&&r
1404
MC/DC50%1/2
$pre_state == BGP_STATE_OPEN_CONFIRM() and $pos_state == BGP_STATE_ESTABLISHED()
$pre_state == BGP_STATE_OPEN_CONFIRM() $pos_state == BGP_STATE_ESTABLISHED()
Truth table66%2/3
A and B
A= $pre_state == BGP_STATE_OPEN_CONFIRM()B= $pos_state == BGP_STATE_ESTABLISHED()
ABresult
0X0
100
111
565 4 $this->parent->established_callback();
566 }
567 }
568  
569 sub _handle_pending_events
570 {
571 76 ▶ my $this = shift();
Subroutine _handle_pending_events: called CC=3 SCAR=11.2
572 76 my $event;
573  
574 # flush the outbound message buffer
575 76 ▶ if ( length($this->{_out_msg_buffer}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $this->{"_out_msg_buffer"}) 0 76 76
576 0 $this->_send_msg();
577 }
578  
579 76 ▶ while ( defined($event = $this->_dequeue_event()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($event = $this->_dequeue_event)) 32 76 108
580 32 $this->_handle_event($event);
581 }
582 }
583  
584 sub _update_timers
585 {
586 76 ▶ my ($this, $delta) = @_;
Subroutine _update_timers: called CC=8 SCAR=21.8
587 76 my ($timer, $key, $min, $min_time);
588 76 my %timers = (
589 _connect_retry_timer => BGP_EVENT_CONNECT_RETRY_TIMER_EXPIRED,
590 _hold_timer => BGP_EVENT_HOLD_TIMER_EXPIRED,
591 _keep_alive_timer => BGP_EVENT_KEEPALIVE_TIMER_EXPIRED
592 );
593  
594 76 $min_time = 3600;
595 76 ▶ if ( length($this->{_out_msg_buffer}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $this->{"_out_msg_buffer"}) 0 76 76
596 0 $min_time = 0;
597 }
598  
599 # Update BGP timers
600 76 foreach $timer ( keys(%timers) ) {
601 228 ▶ if ( defined($this->{$timer}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{$timer}) 118 110 228
602 118 $this->{$timer} -= $delta;
603  
604 118 ▶ if ( $this->{$timer} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{$timer} <= 0) 4 114 118
605 4 $this->{$timer} = 0;
606 4 $this->_enqueue_event($timers{$timer});
607 }
608  
609 118 ▶ if ( $this->{$timer} < $min_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{$timer} < $min_time) 91 27 118
610 91 $min_time = $this->{$timer};
611 }
612 }
613 }
614  
615 # Got a sibling-child?
616 76 ▶ if (defined $this->sibling)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->sibling) 0 76 76
617 {
618 0 my $sibmin = $this->sibling->_update_timers($delta);
619 0 ▶ $min_time = $sibmin if $sibmin < $min_time;
Branch: 1 branch
BranchTrueFalseTotal
if $sibmin < $min_time 0 0 0
620 }
621  
622 76 return $min_time;
623 }
624  
625 sub _send_msg
626 {
627 20 ▶ my ($this, $msg, $oktofail) = @_;
Subroutine _send_msg: called CC=6 SCAR=28.5
628  
629  
630 20 ▶ unless (defined $this->{_peer_socket}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_peer_socket"}) 0 20 20
631 0 ▶ return if $oktofail;
Branch: 1 branch
BranchTrueFalseTotal
if $oktofail 0 0 0
632 0 cluck $this->parent->asstring . ": Internal error - no _peer_socket - Connection is shutdown\n";
633 0 $this->_cease;
634 0 return;
635 }
636  
637 20 my $buffer = $this->{_out_msg_buffer} . $msg;
638 20 my $sent = $this->{_peer_socket}->syswrite($buffer);
639  
640 20 ▶ if ( ! defined($sent) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sent) 0 20 20
641 0 ▶ return if $oktofail; # In a _cease process - Don't complain...
Branch: 1 branch
BranchTrueFalseTotal
if $oktofail 0 0 0
642 0 ▶ if ($!{EAGAIN} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EAGAIN"} == 0) 0 0 0
643 0 warn $this->parent->asstring . ": Error on socket write: $! - Connection is shutdown\n";
644 0 $this->_cease;
645 }
646 0 return;
647 }
648  
649 20 $this->{_out_msg_buffer} = substr($buffer, $sent);
650 }
651  
652 sub _handle_socket_read_ready
653 {
654 32 ▶ my $this = shift();
Subroutine _handle_socket_read_ready: called CC=14 SCAR=38.5
655  
656 32 my $socket = $this->{_peer_socket};
657  
658 32 ▶ unless (defined $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 32 32
659 0 warn $this->parent->asstring . ": Connection lost - Connection is formaly shutdown now\n";
660 0 $this->_cease;
661 0 return;
662 }
663  
664 32 my $conn_closed = FALSE;
665 32 my $buffer = $this->{_in_msg_buffer};
666  
667 32 ▶ if ( $this->{_in_msg_buf_state} == AWAITING_HEADER_START ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($this->{'_in_msg_buf_state'} == AWAITING_HEADER_START()) { } 20 12 32
elsif ($this->{'_in_msg_buf_state'} == AWAITING_HEADER_FRAGMENT()) { } 0 12 12
elsif ($this->{'_in_msg_buf_state'} == AWAITING_MESSAGE_FRAGMENT()) { } 12 0 12
668 20 my $num_read = $socket->sysread($buffer, BGP_MESSAGE_HEADER_LENGTH, length($buffer));
669  
670 20 ▶ if ($!) { # Something went wrong with none-blocking connect()
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 20 20
671 0 $this->{_peer_socket} = $socket = undef;
672 0 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED);
673 0 return;
674 }
675  
676 20 ▶ if ( $num_read == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num_read == 0) { } 0 20 20
elsif ($num_read != BGP_MESSAGE_HEADER_LENGTH()) { } 0 20 20
677 0 $conn_closed = TRUE;
678 }
679 elsif ( $num_read != BGP_MESSAGE_HEADER_LENGTH ) {
680 0 $this->{_in_msg_buf_state} = AWAITING_HEADER_FRAGMENT;
681 0 $this->{_in_msg_buf_bytes_exp} = (BGP_MESSAGE_HEADER_LENGTH) - ($num_read);
682 0 $this->{_in_msg_buffer} = $buffer;
683 }
684 else {
685 20 $this->_decode_bgp_message_header($buffer);
686 20 $this->{_in_msg_buffer} = '';
687 }
688 }
689 elsif ( $this->{_in_msg_buf_state} == AWAITING_HEADER_FRAGMENT ) {
690 0 my $num_read = $socket->sysread($buffer, $this->{_in_msg_buf_bytes_exp}, length($buffer));
691 0 ▶ if ( $num_read == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num_read == 0) { } 0 0 0
elsif ($num_read == $this->{'_in_msg_buf_bytes_exp'}) { } 0 0 0
692 0 $conn_closed = TRUE;
693 }
694 elsif ( $num_read == $this->{_in_msg_buf_bytes_exp} ) {
695 0 $this->_decode_bgp_message_header($buffer);
696 0 $this->{_in_msg_buffer} = '';
697 }
698 else {
699 0 $this->{_in_msg_buf_bytes_exp} -= $num_read;
700 0 $this->{_in_msg_buffer} = $buffer;
701 }
702 }
703 elsif ( $this->{_in_msg_buf_state} == AWAITING_MESSAGE_FRAGMENT ) {
704 12 my $num_read = $socket->sysread($buffer, $this->{_in_msg_buf_bytes_exp}, length($buffer));
705 12 ▶ if ( ($num_read == 0) && ($this->{_in_msg_buf_bytes_exp} != 0) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num_read == 0 and $this->{'_in_msg_buf_bytes_exp'} != 0) { } 0 12 12
elsif ($num_read == $this->{'_in_msg_buf_bytes_exp'}) { } 12 0 12
Condition33%1/3
$num_read == 0 and $this->{"_in_msg_buf_bytes_exp"} != 0
!ll&&!rl&&r
1200
MC/DC0%0/2
$num_read == 0 and $this->{"_in_msg_buf_bytes_exp"} != 0
$num_read == 0 $this->{"_in_msg_buf_bytes_exp"} != 0
Truth table33%1/3
A and B
A= $num_read == 0B= $this->{"_in_msg_buf_bytes_exp"} != 0
ABresult
0X0
100
111
706 0 $conn_closed = TRUE;
707 }
708 elsif ( $num_read == $this->{_in_msg_buf_bytes_exp} ) {
709 12 $this->_enqueue_message($buffer);
710 12 $this->_enqueue_event($BGP_EVENT_MESSAGE_MAP[$this->{_in_msg_buf_type}]);
711 12 $this->{_in_msg_buffer} = '';
712 12 $this->{_in_msg_buf_state} = AWAITING_HEADER_START;
713 }
714 else {
715 0 $this->{_in_msg_buf_bytes_exp} -= $num_read;
716 0 $this->{_in_msg_buffer} = $buffer;
717 }
718 }
719 else {
720 0 croak("unknown socket state!\n");
721 }
722  
723 32 ▶ if ( $conn_closed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn_closed) 0 32 32
724 0 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_CLOSED);
725 }
726 }
727  
728 sub _handle_socket_write_ready
729 {
730 4 ▶ my $this = shift();
Subroutine _handle_socket_write_ready: called CC=2 SCAR=7.0
731 4 ▶ return unless defined($this->{_peer_socket}); # Might have been closed by _handle_socket_read_ready!
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"_peer_socket"} 0 4 4
732 4 $this->{_peer_socket_connected} = TRUE;
733 4 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_OPEN);
734 }
735  
736 sub _handle_socket_error_condition
737 {
738 0 ▶ my $this = shift();
Subroutine _handle_socket_error_condition: not called CC=1 SCAR=6.9
739 0 warn "_handle_socket_error_condition()\n" . $this->{_peer_socket}->error(), "\n";
740 }
741  
742 sub _close_session
743 {
744 4 ▶ my $this = shift();
Subroutine _close_session: called CC=2 SCAR=6.9
745 4 my $socket = $this->{_peer_socket};
746  
747 4 ▶ if ( defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) 4 0 4
748 4 $socket->close();
749 }
750  
751 4 $this->{_peer_socket} = $socket = undef;
752 4 $this->{_peer_socket_connected} = FALSE;
753 4 $this->{_in_msg_buffer} = '';
754 4 $this->{_out_msg_buffer} = '';
755 4 $this->{_in_msg_buf_state} = AWAITING_HEADER_START;
756 4 $this->{_hold_timer} = undef;
757 4 $this->{_keep_alive_timer} = undef;
758 4 $this->{_connect_retry_timer} = undef;
759 4 $this->{_message_queue} = [];
760  
761 4 return ( BGP_STATE_IDLE );
762 }
763  
764 sub _kill_session
765 {
766 6 ▶ my ($this, $error) = @_;
Subroutine _kill_session: called CC=2 SCAR=6.9
767 6 my $buffer;
768  
769 6 ▶ if (defined($this->{_peer_socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_peer_socket"}) 2 4 6
770 2 $buffer = $this->_encode_bgp_notification_message(
771 $error->error_code(),
772 $error->error_subcode(),
773 $error->error_data()
774 );
775  
776 2 $this->_send_msg($buffer,1);
777 2 $this->_close_session();
778 };
779  
780 # invoke user callback function
781 6 $this->parent->error_callback($error);
782 }
783  
784 sub _ignore_start_event
785 {
786 0 ▶ my $this = shift();
Subroutine _ignore_start_event: not called CC=1 SCAR=6.9
787 0 return ( $this->{_fsm_state} );
788 }
789  
790 sub _handle_receive_keepalive_message
791 {
792 8 ▶ my $this = shift();
Subroutine _handle_receive_keepalive_message: called CC=2 SCAR=7.0
793  
794 # restart Hold Timer
795 8 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 8 0 8
796 8 $this->{_hold_timer} = $this->{_hold_time};
797 }
798  
799 # invoke user callback function
800 8 $this->parent->keepalive_callback();
801  
802 8 return ( BGP_STATE_ESTABLISHED );
803 }
804  
805 sub _handle_receive_update_message
806 {
807 4 ▶ my $this = shift();
Subroutine _handle_receive_update_message: called CC=2 SCAR=7.0
808 4 my ($buffer, $update);
809  
810 # restart Hold Timer
811 4 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 4 0 4
812 4 $this->{_hold_timer} = $this->{_hold_time};
813 }
814  
815 4 $buffer = $this->_dequeue_message();
816 $update = Net::BGP::Update->_new_from_msg(
817 $buffer,
818 { as4 => $this->{_peer_as4} }
819 4 );
820  
821 # invoke user callback function
822 4 $this->parent->update_callback($update);
823  
824 4 return ( BGP_STATE_ESTABLISHED );
825 }
826  
827 sub _handle_receive_refresh_message
828 {
829 2 ▶ my $this = shift();
Subroutine _handle_receive_refresh_message: called CC=3 SCAR=11.3
830 2 my ($buffer, $refresh);
831  
832 # restart Hold Timer
833 2 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 2 0 2
834 2 $this->{_hold_timer} = $this->{_hold_time};
835 }
836  
837 2 $buffer = $this->_dequeue_message();
838 2 $refresh = Net::BGP::Refresh->_new_from_msg($buffer);
839  
840 2 ▶ unless ( $this->parent->this_can_refresh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->parent->this_can_refresh) 0 2 2
841 0 Net::BGP::Notification->throw(
842 ErrorCode => BGP_ERROR_CODE_FINITE_STATE_MACHINE
843 );
844 }
845  
846 # invoke user callback function
847 2 $this->parent->refresh_callback($refresh);
848  
849 2 return ( BGP_STATE_ESTABLISHED );
850 }
851  
852 sub _handle_receive_notification_message
853 {
854 2 ▶ my $this = shift();
Subroutine _handle_receive_notification_message: called CC=1 SCAR=0.0
855 2 my $error;
856  
857 2 $error = $this->_decode_bgp_notification_message($this->_dequeue_message());
858 2 $this->_close_session();
859  
860 # invoke user callback function
861 2 $this->parent->notification_callback($error);
862  
863 2 return ( BGP_STATE_IDLE );
864 }
865  
866 sub _handle_keepalive_expired
867 {
868 4 ▶ my $this = shift();
Subroutine _handle_keepalive_expired: called CC=1 SCAR=0.0
869 4 my $buffer;
870  
871 # send KEEPALIVE message to peer
872 4 $buffer = $this->_encode_bgp_keepalive_message();
873 4 $this->_send_msg($buffer);
874  
875 # restart KeepAlive timer
876 4 $this->{_keep_alive_timer} = $this->{_keep_alive_time};
877  
878 4 return ( $this->{_fsm_state} );
879 }
880  
881 sub _handle_hold_timer_expired
882 {
883 0 ▶ my $this = shift();
Subroutine _handle_hold_timer_expired: not called CC=1 SCAR=6.9
884  
885 0 $this->_error(BGP_ERROR_CODE_HOLD_TIMER_EXPIRED);
886 }
887  
888 sub _handle_bgp_fsm_error
889 {
890 0 ▶ my $this = shift();
Subroutine _handle_bgp_fsm_error: not called CC=1 SCAR=6.9
891  
892 0 $this->_error(BGP_ERROR_CODE_FINITE_STATE_MACHINE);
893 }
894  
895 sub _handle_bgp_conn_open
896 {
897 4 ▶ my $this = shift();
Subroutine _handle_bgp_conn_open: called CC=1 SCAR=0.0
898 4 my $buffer;
899  
900 # clear ConnectRetry timer
901 4 $this->{_connect_retry_timer} = undef;
902  
903 # send OPEN message to peer
904 4 $buffer = $this->_encode_bgp_open_message();
905 4 $this->_send_msg($buffer);
906  
907 4 return ( BGP_STATE_OPEN_SENT );
908 }
909  
910 sub _handle_collision_selfdestuct
911 {
912 0 ▶ my $this = shift;
Subroutine _handle_collision_selfdestuct: not called CC=1 SCAR=6.9
913 0 $this->stop();
914 0 $this->parent->transport($this->{_sibling});
915 0 $this->{_sibling}->{_sibling} = undef;
916 }
917  
918 sub _handle_bgp_open_received
919 {
920 4 ▶ my $this = shift();
Subroutine _handle_bgp_open_received: called CC=8 SCAR=31.0
921 4 my ($buffer, $this_id, $peer_id);
922  
923 4 ▶ if ( ! $this->_decode_bgp_open_message($this->_dequeue_message()) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_decode_bgp_open_message($this->_dequeue_message)) 0 4 4
924 ; # do failure stuff
925 0 return ( BGP_STATE_IDLE );
926 }
927  
928 # check for connection collision
929 4 ▶ if ( defined($this->{_sibling}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_sibling"}) 0 4 4
930 0 ▶ if ( ($this->{_sibling}->{_fsm_state} == BGP_STATE_OPEN_SENT) ||
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'_sibling'}{'_fsm_state'} == BGP_STATE_OPEN_SENT() or $this->{'_sibling'}{'_fsm_state'} == BGP_STATE_OPEN_CONFIRM()) { } 0 0 0
elsif ($this->{'_sibling'}{'_fsm_state'} == BGP_STATE_ESTABLISHED()) { } 0 0 0
Condition0%0/3
$this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_SENT() or $this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_CONFIRM()
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_SENT() or $this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_CONFIRM()
$this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_SENT() $this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_CONFIRM()
Truth table0%0/3
A or B
A= $this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_SENT()B= $this->{"_sibling"}{"_fsm_state"} == BGP_STATE_OPEN_CONFIRM()
ABresult
1X1
011
000
931 ($this->{_sibling}->{_fsm_state} == BGP_STATE_OPEN_CONFIRM) ) {
932  
933 0 $this_id = unpack('N', inet_aton($this->parent->this_id));
934 0 $peer_id = unpack('N', inet_aton($this->parent->peer_id));
935  
936 0 ▶ if ( $this_id < $peer_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_id < $peer_id) { } 0 0 0
937 0 $this->_handle_collision_selfdestuct;
938 0 return ( BGP_STATE_IDLE );
939 }
940 else {
941 0 $this->{_sibling}->_handle_collision_selfdestuct;
942 }
943 }
944 elsif ( ($this->{_sibling}->{_fsm_state} == BGP_STATE_ESTABLISHED) ) {
945 0 $this->_handle_collision_selfdestuct;
946 0 return ( BGP_STATE_IDLE );
947 }
948 else { # Other in Idle, conect, active
949 0 $this->{_sibling}->_handle_collision_selfdestuct;
950 }
951 }
952  
953 # clear the message buffer after decoding and validation
954 4 $this->{_message} = undef;
955  
956 # send KEEPALIVE message to peer
957 4 $buffer = $this->_encode_bgp_keepalive_message();
958 4 $this->_send_msg($buffer);
959  
960 # set Hold Time and KeepAlive timers
961 4 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 4 0 4
962 4 $this->{_hold_timer} = $this->{_hold_time};
963 4 $this->{_keep_alive_timer} = $this->{_keep_alive_time};
964 }
965  
966 # invoke user callback function
967 4 $this->parent->open_callback();
968  
969 # transition to state OpenConfirm
970 4 return ( BGP_STATE_OPEN_CONFIRM );
971 }
972  
973 sub _handle_open_sent_disconnect
974 {
975 0 ▶ my $this = shift();
Subroutine _handle_open_sent_disconnect: not called CC=1 SCAR=6.9
976  
977 0 $this->_close_session();
978 0 return ( $this->_handle_connect_retry_restart() );
979 }
980  
981 sub _handle_connect_retry_restart
982 {
983 0 ▶ my $this = shift();
Subroutine _handle_connect_retry_restart: not called CC=1 SCAR=6.9
984  
985 # restart ConnectRetry timer
986 0 $this->{_connect_retry_timer} = $this->{_connect_retry_time};
987  
988 0 return ( BGP_STATE_ACTIVE );
989 }
990  
991 sub _handle_bgp_start_event
992 {
993 4 ▶ my $this = shift();
Subroutine _handle_bgp_start_event: called CC=12 SCAR=30.5
994 4 my ($socket, $proto, $remote_addr, $this_addr, $rv);
995  
996 # initialize ConnectRetry timer
997 4 ▶ if ( ! $this->parent->is_passive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->parent->is_passive) 2 2 4
998 2 $this->{_connect_retry_timer} = $this->{_connect_retry_time};
999 }
1000  
1001 # initiate the TCP transport connection
1002 4 ▶ if ( ! $this->parent->is_passive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->parent->is_passive) 2 2 4
1003 2 eval {
1004 2 $socket = IO::Socket->new( Domain => AF_INET );
1005 2 ▶ if ( ! defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 2 2
1006 0 die("IO::Socket construction failed");
1007 }
1008  
1009 2 $proto = getprotobyname('tcp');
1010 2 $rv = $socket->socket(PF_INET, SOCK_STREAM, $proto);
1011 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
1012 0 die("socket() failed");
1013 }
1014  
1015 2 $this_addr = sockaddr_in(0, inet_aton($this->parent->this_id));
1016 2 $rv = $socket->bind($this_addr);
1017 2 ▶ if ( ! $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 2 2
1018 0 die("bind() failed");
1019 }
1020  
1021 2 $rv = $socket->blocking(FALSE);
1022 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
1023 0 die("set socket non-blocking failed");
1024 }
1025  
1026 2 $remote_addr = sockaddr_in($this->parent->peer_port, inet_aton($this->parent->peer_id));
1027 2 $rv = $socket->connect($remote_addr);
1028 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
1029 0 ▶ die "OK - but connect() failed: $!" unless ($! == EINPROGRESS);
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 115 0 0 0
1030 }
1031  
1032 # $rv = $socket->blocking(TRUE);
1033 # if ( ! defined($rv) ) {
1034 # die("set socket blocking failed");
1035 # }
1036 };
1037  
1038 # check for exception in transport initiation
1039 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
1040 0 ▶ carp $@ unless $@ =~ /^OK/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^OK/ 0 0 0
1041 0 ▶ if ( defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) 0 0 0
1042 0 $socket->close();
1043 }
1044 0 $this->{_peer_socket} = $socket = undef;
1045 0 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED);
1046 }
1047  
1048 2 $this->{_peer_socket} = $socket;
1049 2 $this->{_peer_socket_connected} = FALSE;
1050 }
1051  
1052 4 return ( BGP_STATE_CONNECT );
1053 }
1054  
1055 sub _min
1056 {
1057 5 ▶ my ($a, $b) = @_;
Subroutine _min: called CC=2 SCAR=7.2
1058 5 ▶ return ( ($a < $b) ? $a : $b );
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 0 5 5
1059 }
1060  
1061 sub _cease
1062 {
1063 6 ▶ my $this = shift();
Subroutine _cease: called CC=2 SCAR=7.0
1064  
1065 6 ▶ if ( $this->{_fsm_state} == BGP_STATE_ESTABLISHED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_fsm_state"} == BGP_STATE_ESTABLISHED()) 6 0 6
1066 6 $this->parent->reset_callback();
1067 }
1068  
1069 6 my $error = Net::BGP::Notification->new( ErrorCode => BGP_ERROR_CODE_CEASE );
1070  
1071 6 $this->_kill_session($error);
1072  
1073 6 return ( BGP_STATE_IDLE );
1074 }
1075  
1076 sub _encode_bgp_message
1077 {
1078 22 ▶ my ($this, $type, $payload) = @_;
Subroutine _encode_bgp_message: called CC=3 SCAR=11.0
1079 22 my ($buffer, $length);
1080  
1081 22 $buffer = '';
1082 22 $length = BGP_MESSAGE_HEADER_LENGTH;
1083  
1084 22 ▶ if ( defined($payload) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload) 14 8 22
1085 14 $length += length($payload);
1086 14 $buffer = $payload;
1087 }
1088  
1089 # encode the type field
1090 22 $buffer = pack('C', $type) . $buffer;
1091  
1092 # encode the length field
1093 22 $buffer = pack('n', $length) . $buffer;
1094  
1095 # encode the marker field
1096 22 ▶ if ( defined($this->{_auth_data}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{'_auth_data'}) { } 0 22 22
1097 0 $buffer = $this->{_auth_data} . $buffer;
1098 }
1099 else {
1100 22 $buffer = (pack('C', 0xFF) x 16) . $buffer;
1101 }
1102  
1103 22 return ( $buffer );
1104 }
1105  
1106 sub _decode_bgp_message_header
1107 {
1108 20 ▶ my ($this, $header) = @_;
Subroutine _decode_bgp_message_header: called CC=8 SCAR=22.9
1109 20 my ($marker, $length, $type);
1110  
1111 # validate the BGP message header length
1112 20 ▶ if ( length($header) != BGP_MESSAGE_HEADER_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $header != BGP_MESSAGE_HEADER_LENGTH()) 0 20 20
1113 0 $this->_error(
1114 BGP_ERROR_CODE_MESSAGE_HEADER,
1115 BGP_ERROR_SUBCODE_BAD_MSG_LENGTH,
1116 pack('n', length($header))
1117 );
1118 }
1119  
1120 # decode and validate the message header Marker field
1121 20 $marker = substr($header, 0, 16);
1122 20 ▶ if ( $marker ne (pack('C', 0xFF) x 16) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marker ne "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377") 0 20 20
1123 0 $this->_error(BGP_ERROR_CODE_MESSAGE_HEADER,
1124 BGP_ERROR_SUBCODE_CONN_NOT_SYNC);
1125 }
1126  
1127 # decode and validate the message header Length field
1128 20 $length = unpack('n', substr($header, 16, 2));
1129 20 ▶ if ( ($length < BGP_MESSAGE_HEADER_LENGTH) || ($length > BGP_MAX_MESSAGE_LENGTH) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < BGP_MESSAGE_HEADER_LENGTH() or $length > BGP_MAX_MESSAGE_LENGTH()) 0 20 20
Condition33%1/3
$length < BGP_MESSAGE_HEADER_LENGTH() or $length > BGP_MAX_MESSAGE_LENGTH()
l!l&&r!l&&!r
0020
MC/DC0%0/2
$length < BGP_MESSAGE_HEADER_LENGTH() or $length > BGP_MAX_MESSAGE_LENGTH()
$length < BGP_MESSAGE_HEADER_LENGTH() $length > BGP_MAX_MESSAGE_LENGTH()
Truth table33%1/3
A or B
A= $length < BGP_MESSAGE_HEADER_LENGTH()B= $length > BGP_MAX_MESSAGE_LENGTH()
ABresult
1X1
011
000
1130 0 $this->_error(
1131 BGP_ERROR_CODE_MESSAGE_HEADER,
1132 BGP_ERROR_SUBCODE_BAD_MSG_LENGTH,
1133 pack('n', $length)
1134 );
1135 }
1136  
1137 # decode and validate the message header Type field
1138 20 $type = unpack('C', substr($header, 18, 1));
1139 20 ▶ if ( ($type < BGP_MESSAGE_OPEN) || ($type > BGP_MESSAGE_REFRESH) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type < BGP_MESSAGE_OPEN() or $type > BGP_MESSAGE_REFRESH()) 0 20 20
Condition33%1/3
$type < BGP_MESSAGE_OPEN() or $type > BGP_MESSAGE_REFRESH()
l!l&&r!l&&!r
0020
MC/DC0%0/2
$type < BGP_MESSAGE_OPEN() or $type > BGP_MESSAGE_REFRESH()
$type < BGP_MESSAGE_OPEN() $type > BGP_MESSAGE_REFRESH()
Truth table33%1/3
A or B
A= $type < BGP_MESSAGE_OPEN()B= $type > BGP_MESSAGE_REFRESH()
ABresult
1X1
011
000
1140 0 $this->_error(
1141 BGP_ERROR_CODE_MESSAGE_HEADER,
1142 BGP_ERROR_SUBCODE_BAD_MSG_TYPE,
1143 pack('C', $type)
1144 );
1145 }
1146  
1147 20 ▶ if ( $type == BGP_MESSAGE_KEEPALIVE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_MESSAGE_KEEPALIVE()) { } 8 12 20
1148 8 $this->{_in_msg_buffer} = '';
1149 8 $this->{_in_msg_buf_state} = AWAITING_HEADER_START;
1150 8 $this->{_in_msg_buf_bytes_exp} = 0;
1151 8 $this->{_in_msg_buf_type} = 0;
1152 8 $this->_enqueue_event(BGP_EVENT_RECEIVE_KEEP_ALIVE_MESSAGE);
1153 }
1154 else {
1155 12 $this->{_in_msg_buf_state} = AWAITING_MESSAGE_FRAGMENT;
1156 12 $this->{_in_msg_buf_bytes_exp} = $length - BGP_MESSAGE_HEADER_LENGTH;
1157 12 $this->{_in_msg_buf_type} = $type;
1158 }
1159  
1160 # indicate decoding and validation success
1161 20 return ( TRUE );
1162 }
1163  
1164 sub _encode_bgp_open_message
1165 {
1166 6 ▶ my $this = shift();
Subroutine _encode_bgp_open_message: called CC=9 SCAR=22.1
1167 6 my ($buffer, $length);
1168  
1169 # encode optional parameters and length
1170 6 my $opt = '';
1171  
1172 6 ▶ if ($this->parent->support_capabilities) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->support_capabilities) 6 0 6
1173  
1174 6 ▶ if ( defined($this->{_peer_announced_id}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{'_peer_announced_id'}) { } 2 4 6
1175 # We received an open from the other end
1176  
1177 2 ▶ if ($this->{_peer_mbgp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_peer_mbgp"}) 2 0 2
1178 2 $opt .= $this->_encode_capability_mbgp();
1179 }
1180  
1181 2 ▶ if ($this->{_peer_as4}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_peer_as4"}) 1 1 2
1182 1 $opt .= $this->_encode_capability_as4();
1183 }
1184  
1185 } else {
1186 # We are sending the open
1187  
1188 4 ▶ if ( $this->parent->support_mbgp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->support_mbgp) 4 0 4
1189 4 $opt .= $this->_encode_capability_mbgp();
1190 }
1191 4 ▶ if ( $this->parent->this_can_as4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_can_as4) 2 2 4
1192 2 $opt .= $this->_encode_capability_as4();
1193 }
1194  
1195 }
1196  
1197 # Both the standard (2) and Cisco (128) capabilities are sent
1198 6 ▶ if ($this->parent->this_can_refresh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_can_refresh) 6 0 6
1199 6 $opt .= $this->_encode_capability(BGP_CAPABILITY_REFRESH, '');
1200 6 $opt .= $this->_encode_capability(BGP_CAPABILITY_REFRESH_OLD, '');
1201 }
1202 }
1203  
1204 6 $buffer = pack('C', length($opt)) . $opt;
1205  
1206 # encode BGP Identifier field
1207 6 $buffer = inet_aton($this->parent->this_id) . $buffer;
1208  
1209 # encode Hold Time
1210 6 $buffer = pack('n', $this->{_hold_time}) . $buffer;
1211  
1212 # encode local Autonomous System number
1213 6 ▶ if ($this->parent->this_as > 65535) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_as > 65535) { } 3 3 6
1214 3 $buffer = pack('n', 23456) . $buffer;
1215 } else {
1216 3 $buffer = pack('n', $this->parent->this_as) . $buffer;
1217 }
1218  
1219 # encode BGP version
1220 6 $buffer = pack('C', $this->{_bgp_version}) . $buffer;
1221  
1222 6 return ( $this->_encode_bgp_message(BGP_MESSAGE_OPEN, $buffer) );
1223 }
1224  
1225 sub _encode_capability_mbgp
1226 {
1227 6 ▶ my $this = shift;
Subroutine _encode_capability_mbgp: called CC=1 SCAR=0.0
1228  
1229 # Capability 1 with data of:
1230 # Address family 1 (IPv4), reserved bit 0, type 1 (unicast)
1231 6 my $cap = pack('ncc', 1, 0, 1);
1232 6 my $opt = $this->_encode_capability(BGP_CAPABILITY_MBGP, $cap);
1233  
1234 6 return $opt;
1235 }
1236  
1237 sub _encode_capability_as4
1238 {
1239 3 ▶ my $this = shift;
Subroutine _encode_capability_as4: called CC=1 SCAR=0.0
1240  
1241 # Capability 65 with data of the ASN
1242 3 my $cap = pack('N', $this->parent->this_as());
1243 3 my $opt = $this->_encode_capability(BGP_CAPABILITY_AS4, $cap);
1244  
1245 3 return $opt;
1246 }
1247  
1248 # Encodes a capability (inside the capability option)
1249 # RFC5492
1250 # Format is <2> <capability_len> <cap_code> <data_len>
1251 sub _encode_capability
1252 {
1253 21 ▶ my ($this, $type, $data) = @_;
Subroutine _encode_capability: called CC=1 SCAR=0.0
1254  
1255 21 my $opt = '';
1256 21 $opt .= pack('C', BGP_OPTION_CAPABILITIES); # Option Type
1257  
1258 21 my $cap = '';
1259 21 $cap .= pack('C', $type); # Capability Type
1260 21 $cap .= pack('C', length($data)); # Capability Data Len
1261 21 $cap .= $data; # Capability data
1262  
1263 21 $opt .= pack('C', length($cap)); # Option Data Len
1264 21 $opt .= $cap;
1265  
1266 21 return $opt;
1267 }
1268  
1269 sub _decode_bgp_open_message
1270 {
1271 5 ▶ my ($this, $buffer) = @_;
Subroutine _decode_bgp_open_message: called CC=9 SCAR=23.5
1272 5 my ($version, $as, $hold_time, $bgp_id);
1273  
1274 # decode and validate BGP version
1275 5 $version = unpack('C', substr($buffer, 0, 1));
1276 5 ▶ if ( $version != BGP_VERSION_4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version != BGP_VERSION_4()) 0 5 5
1277 0 $this->_error(
1278 BGP_ERROR_CODE_OPEN_MESSAGE,
1279 BGP_ERROR_SUBCODE_BAD_VERSION_NUM,
1280 pack('n', BGP_VERSION_4)
1281 );
1282 }
1283  
1284 # decode and validate remote Autonomous System number
1285 5 $as = unpack('n', substr($buffer, 1, 2));
1286 5 ▶ if ( $as != $this->parent->peer_as ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as != $this->parent->peer_as) 3 2 5
1287 3 ▶ if ($this->parent->peer_as < 65536) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->parent->peer_as < 65536) { } 0 3 3
elsif ($as != 23456) { } 0 3 3
1288 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1289 BGP_ERROR_SUBCODE_BAD_PEER_AS);
1290 } elsif ($as != 23456) {
1291 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1292 BGP_ERROR_SUBCODE_BAD_PEER_AS);
1293 }
1294 }
1295  
1296 # decode and validate received Hold Time
1297 5 $hold_time = _min(unpack('n', substr($buffer, 3, 2)), $this->{_hold_time});
1298 5 ▶ if ( ($hold_time < 3) && ($hold_time != 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hold_time < 3 and $hold_time != 0) 0 5 5
Condition33%1/3
$hold_time < 3 and $hold_time != 0
!ll&&!rl&&r
500
MC/DC0%0/2
$hold_time < 3 and $hold_time != 0
$hold_time < 3 $hold_time != 0
Truth table33%1/3
A and B
A= $hold_time < 3B= $hold_time != 0
ABresult
0X0
100
111
1299 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1300 BGP_ERROR_SUBCODE_BAD_HOLD_TIME);
1301 }
1302  
1303 # decode received BGP Identifier
1304 # Spelling error is retained for compatibility.
1305 5 $this->{_peer_annonced_id} = inet_ntoa(substr($buffer, 5, 4));
1306 5 $this->{_peer_announced_id} = inet_ntoa(substr($buffer, 5, 4));
1307  
1308 # decode known Optional Parameters
1309 5 my $opt_length = unpack('c', substr($buffer, 9, 1));
1310 5 my $opt = substr($buffer, 10, $opt_length);
1311 5 ▶ while ($opt ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($opt ne "") 15 5 20
1312 {
1313 15 my ($type, $length) = unpack('cc', substr($opt, 0, 2));
1314 15 my $value = substr($opt, 2, $length);
1315 15 ▶ if ($type eq BGP_OPTION_CAPABILITIES)
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq BGP_OPTION_CAPABILITIES()) { } 15 0 15
1316 {
1317 15 $this->_decode_capabilities($value);
1318 }
1319 else
1320 { # Unknown optional parameter!
1321 # XXX We should send a notify here.
1322 }
1323 15 $opt = substr($opt, 2+$length);
1324 };
1325  
1326 # set Hold Time to negotiated value
1327 5 $this->{_hold_time} = $hold_time;
1328  
1329 # indicate decoding and validation success
1330 5 return ( TRUE );
1331 }
1332  
1333 # Capabilities we don't understand get ignored.
1334 sub _decode_capabilities
1335 {
1336 15 ▶ my ($this, $value) = @_;
Subroutine _decode_capabilities: called CC=3 SCAR=11.3
1337  
1338 15 $this->{'_peer_refresh'} = TRUE;
1339  
1340 15 ▶ while (length($value) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > 0) 17 15 32
1341  
1342 17 ▶ if (length($value) < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value < 2) 0 17 17
1343 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1344 BGP_ERROR_SUBCODE_BAD_OPT_PARAMETER);
1345 0 return;
1346 }
1347  
1348 17 my ($type, $len) = unpack('cc', substr($value, 0, 2));
1349 17 my $data = substr($value, 2, $len);
1350  
1351 17 $this->_decode_one_capability($type, $len, $data);
1352  
1353 17 $value = substr($value, 2+$len);
1354 }
1355  
1356 }
1357  
1358 sub _decode_one_capability {
1359 17 ▶ my ($this, $type, $len, $data) = @_;
Subroutine _decode_one_capability: called CC=9 SCAR=23.6
1360  
1361 17 ▶ if (length($data) != $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data != $len) 0 17 17
1362 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1363 BGP_ERROR_SUBCODE_BAD_OPT_PARAMETER);
1364 }
1365  
1366 17 ▶ if ($type == BGP_CAPABILITY_MBGP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_MBGP()) 5 12 17
1367 5 $this->{_peer_mbgp} = TRUE;
1368 }
1369  
1370 17 ▶ if ($type == BGP_CAPABILITY_REFRESH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_REFRESH()) 5 12 17
1371 5 $this->{_peer_refresh} = TRUE;
1372 }
1373 17 ▶ if ($type == BGP_CAPABILITY_REFRESH_OLD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_REFRESH_OLD()) 0 17 17
1374 0 $this->{_peer_refresh} = TRUE;
1375 }
1376  
1377 17 ▶ if ($type == BGP_CAPABILITY_AS4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_AS4()) 3 14 17
1378  
1379 3 ▶ if ($len != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len != 4) 0 3 3
1380 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1381 BGP_ERROR_SUBCODE_BAD_OPT_PARAMETER);
1382 }
1383
1384 3 my $as = unpack('N', $data);
1385 3 ▶ if ($as != $this->parent->peer_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as != $this->parent->peer_as) 0 3 3
1386 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1387 BGP_ERROR_SUBCODE_BAD_PEER_AS);
1388 }
1389  
1390 # Both ends must support this.
1391 3 ▶ if ( $this->parent->this_can_as4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_can_as4) 3 0 3
1392 3 $this->{_peer_as4} = TRUE;
1393 }
1394 }
1395  
1396 }
1397  
1398 sub _decode_bgp_notification_message
1399 {
1400 2 ▶ my ($this, $buffer) = @_;
Subroutine _decode_bgp_notification_message: called CC=5 SCAR=18.9
1401 2 my ($error, $error_code, $error_subcode, $data);
1402  
1403 # decode and validate Error code
1404 2 $error_code = unpack('C', substr($buffer, 0, 1));
1405 2 ▶ if ( ($error_code < 1) || ($error_code > 6) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_code < 1 or $error_code > 6) 0 2 2
Condition33%1/3
$error_code < 1 or $error_code > 6
l!l&&r!l&&!r
002
MC/DC0%0/2
$error_code < 1 or $error_code > 6
$error_code < 1 $error_code > 6
Truth table33%1/3
A or B
A= $error_code < 1B= $error_code > 6
ABresult
1X1
011
000
1406 0 die("_decode_bgp_notification_message(): invalid error code = $error_code\n");
1407 }
1408  
1409 # decode and validate Error subcode
1410 2 $error_subcode = unpack('C', substr($buffer, 1, 1));
1411 2 ▶ if ( ($error_subcode < 0) || ($error_subcode > 11) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_subcode < 0 or $error_subcode > 11) 0 2 2
Condition33%1/3
$error_subcode < 0 or $error_subcode > 11
l!l&&r!l&&!r
002
MC/DC0%0/2
$error_subcode < 0 or $error_subcode > 11
$error_subcode < 0 $error_subcode > 11
Truth table33%1/3
A or B
A= $error_subcode < 0B= $error_subcode > 11
ABresult
1X1
011
000
1412 0 die("_decode_bgp_notification_message(): invalid error subcode = $error_subcode\n");
1413 }
1414  
1415 # decode Data field
1416 2 $data = substr($buffer, 2, length($buffer) - 2);
1417  
1418 2 return Net::BGP::Notification->new(
1419 ErrorCode => $error_code,
1420 ErrorSubcode => $error_subcode,
1421 ErrorData => $data);
1422 }
1423  
1424 sub _encode_bgp_keepalive_message
1425 {
1426 8 ▶ my $this = shift();
Subroutine _encode_bgp_keepalive_message: called CC=1 SCAR=0.0
1427 8 return ( $this->_encode_bgp_message(BGP_MESSAGE_KEEPALIVE) );
1428 }
1429  
1430 sub _encode_bgp_update_message
1431 {
1432 4 ▶ my ($this, $buffer) = @_;
Subroutine _encode_bgp_update_message: called CC=1 SCAR=0.0
1433 4 return ( $this->_encode_bgp_message(BGP_MESSAGE_UPDATE, $buffer) );
1434 }
1435  
1436 sub _encode_bgp_refresh_message
1437 {
1438 2 ▶ my ($this, $buffer) = @_;
Subroutine _encode_bgp_refresh_message: called CC=1 SCAR=0.0
1439 2 return ( $this->_encode_bgp_message(BGP_MESSAGE_REFRESH, $buffer) );
1440 }
1441  
1442 sub _encode_bgp_notification_message
1443 {
1444 2 ▶ my ($this, $error_code, $error_subcode, $data) = @_;
Subroutine _encode_bgp_notification_message: called CC=2 SCAR=7.0
1445 2 my $buffer;
1446  
1447 # encode the Data field
1448 2 ▶ $buffer = $data ? $data : '';
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 0 2 2
1449  
1450 # encode the Error Subcode field
1451 2 $buffer = pack('C', $error_subcode) . $buffer;
1452  
1453 # encode the Error Code field
1454 2 $buffer = pack('C', $error_code) . $buffer;
1455  
1456 2 return ( $this->_encode_bgp_message(BGP_MESSAGE_NOTIFICATION, $buffer) );
1457 }
1458  
1459 ## POD ##
1460  
1461 =pod
1462  
1463 =head1 NAME
1464  
1465 Net::BGP::Transport - Class encapsulating BGP-4 transport session state and functionality
1466  
1467 =head1 SYNOPSIS
1468  
1469 use Net::BGP::Transport;
1470  
1471 $trans = Net::BGP::Transport->new(
1472 Start => 1,
1473 Parent => Net::BGP::Peer->new(),
1474 ConnectRetryTime => 300,
1475 HoldTime => 60,
1476 KeepAliveTime => 20
1477 );
1478  
1479 $version = $trans->version();
1480  
1481 $trans->start();
1482 $trans->stop();
1483  
1484 $trans->update($update);
1485 $trans->refresh($refresh);
1486  
1487  
1488 =head1 DESCRIPTION
1489  
1490 This module encapsulates the state and functionality associated with a BGP
1491 transport connection. Each instance of a Net::BGP::Transport object
1492 corresponds to a TCP session with a distinct peer. It should not be used by
1493 it self, but encapsulated in a Net::BGP::Peer object.
1494  
1495 =head1 CONSTRUCTOR
1496  
1497 =over 4
1498  
1499 =item new() - create a new Net::BGP::Transport object
1500  
1501 This is the constructor for Net::BGP::Transport objects. It returns a
1502 reference to the newly created object. The following named parameters may
1503 be passed to the constructor. Once the object is created, the information
1504 can not be changed.
1505  
1506 =over 4
1507  
1508 =item Start
1509  
1510 =item ConnectRetryTime
1511  
1512 =item HoldTime
1513  
1514 =item KeepAliveTime
1515  
1516 Has the same meaning as their equivalente named argument for Net::BGP::Peer.
1517  
1518 =item Parent
1519  
1520 The parent Net::BGP::Peer object.
1521  
1522 =back
1523  
1524 =item renew() - fetch the existing Net::BGP::Peer object from the "object string".
1525  
1526 This "reconstructor" returns a previeus constructed object from the
1527 perl genereted string-context scalar of the object, eg.
1528 I<Net::BGP::Peer=HASH(0x820952c)>.
1529  
1530 =back
1531  
1532 =head1 ACCESSOR METHODS
1533  
1534 =over 4
1535  
1536 =item version()
1537  
1538 =item start()
1539  
1540 =item stop()
1541  
1542 =item update()
1543  
1544 =item refresh()
1545  
1546 =item is_established()
1547  
1548 This methods does the actuall I<work> for the methods of the same name in
1549 Net::BGP::Peer.
1550  
1551 =back
1552  
1553 =head1 SEE ALSO
1554  
1555 Net::BGP::Peer, Net::BGP, Net::BGP::Update, Net::BGP::Refresh
1556  
1557 =head1 AUTHOR
1558  
1559 Stephen J. Scheck <code@neurosphere.com> in original Peer.pm form
1560 Martin Lorensen <lorensen@cpan.org> seperated into Transort.pm
1561  
1562 =cut
1563  
1564 ## End Package Net::BGP::Transport ##
1565  
1566 4 1;
↑ Summary BGP/Update.pm »