BGP/Transport.pm

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