BGP/Transport.pm

filter: Statementstmt 73.6% 444 / 603 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 / 1054 CC 125 SCAR 22.2
CC
125
Cov
67%
_handle_socket_read_ready
48.2
_handle_bgp_open_received
22.2
_handle_bgp_start_event
21.2
CRAP 9.2
« BGP/ASPath/AS.pm ↑ Summary BGP/ASPath.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(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @BGP @GENERIC
10 @BGP_EVENT_MESSAGE_MAP @BGP_EVENTS @BGP_FSM @BGP_STATES
11 4 );
12  
13 ## Inheritance and Versioning ##
14  
15 4 @ISA = qw( Exporter );
16 4 $VERSION = '0.18';
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 0 ▶{
Subroutine dump_hex: not called CC=3 SCAR=24.8
Pod undocumented
231 0 my $data = shift();
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 6 ▶{
Subroutine new: called CC=7 SCAR=20.3
Pod documented
260 6 my $class = shift();
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 5 ▶{
Subroutine start: called CC=1 SCAR=0.0
Pod documented
322 5 my $this = shift();
323 5 $this->_enqueue_event(BGP_EVENT_START);
324 }
325  
326 sub stop
327 6 ▶{
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
328 6 my $this = shift();
329 6 $this->{_fsm_state} = $this->_cease();
330 }
331  
332 sub version
333 0 ▶{
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
334 0 return shift->{_bgp_version};
335 }
336  
337 sub is_established
338 0 ▶{
Subroutine is_established: not called CC=2 SCAR=17.9
Pod documented
339 0 ▶ return ( (shift->{_fsm_state} == BGP_STATE_ESTABLISHED) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'_fsm_state'} == BGP_STATE_ESTABLISHED() ? : 0 0 0
340 }
341  
342 sub can_refresh
343 1 ▶{
Subroutine can_refresh: called CC=1 SCAR=0.0
Pod undocumented
344 1 return shift->{_peer_refresh};
345 }
346  
347 sub can_as4
348 2 ▶{
Subroutine can_as4: called CC=1 SCAR=0.0
Pod undocumented
349 2 return shift->{_peer_as4};
350 }
351  
352 sub can_mbgp
353 3 ▶{
Subroutine can_mbgp: called CC=1 SCAR=0.0
Pod undocumented
354 3 return shift->{_peer_mbgp};
355 }
356  
357 sub update
358 4 ▶{
Subroutine update: called CC=2 SCAR=7.0
Pod documented
359 4 my ($this, $update) = @_;
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 2 ▶{
Subroutine refresh: called CC=4 SCAR=14.5
Pod documented
376 2 my $this = shift;
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 110 ▶{
Subroutine parent: called CC=1 SCAR=0.0
Pod undocumented
393 110 return shift->{_parent};
394 }
395  
396 sub sibling
397 238 ▶{
Subroutine sibling: called CC=3 SCAR=16.5
Pod undocumented
398 238 my $this = shift();
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 0 ▶{
Subroutine _clone: not called CC=4 SCAR=30.0
408 0 my $this = shift();
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 0 ▶{
Subroutine _error: not called CC=2 SCAR=17.9
451 0 my $this = shift();
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 66 ▶{
Subroutine _is_connected: called CC=1 SCAR=0.0
462 66 my $this = shift();
463 66 return ( $this->{_peer_socket_connected} );
464 }
465  
466 sub _get_socket
467 80 ▶{
Subroutine _get_socket: called CC=1 SCAR=0.0
468 80 my $this = shift();
469 80 return ( $this->{_peer_socket} );
470 }
471  
472 sub _set_socket
473 2 ▶{
Subroutine _set_socket: called CC=1 SCAR=0.0
474 2 my ($this, $socket) = @_;
475 2 $this->{_peer_socket} = $socket;
476 2 $this->{_peer_socket_connected} = TRUE;
477 }
478  
479 sub _enqueue_event
480 33 ▶{
Subroutine _enqueue_event: called CC=1 SCAR=0.0
481 33 my $this = shift();
482 33 push(@{ $this->{_event_queue} }, shift());
483 }
484  
485 sub _dequeue_event
486 108 ▶{
Subroutine _dequeue_event: called CC=1 SCAR=0.0
487 108 my $this = shift();
488 108 return ( shift(@{ $this->{_event_queue} }) );
489 }
490  
491 sub _enqueue_message
492 12 ▶{
Subroutine _enqueue_message: called CC=1 SCAR=0.0
493 12 my $this = shift();
494 12 push(@{ $this->{_message_queue} }, shift());
495 }
496  
497 sub _dequeue_message
498 12 ▶{
Subroutine _dequeue_message: called CC=1 SCAR=0.0
499 12 my $this = shift();
500 12 return ( shift(@{ $this->{_message_queue} }) );
501 }
502  
503 sub _handle_event
504 32 ▶{
Subroutine _handle_event: called CC=10 SCAR=26.4
505 32 my ($this, $event) = @_;
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 18 ▶{
Subroutine _trigger_post_transition_action: called CC=3 SCAR=11.0
556 18 my ($this, $pre_state, $pos_state) = @_;
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 76 ▶{
Subroutine _handle_pending_events: called CC=3 SCAR=11.2
574 76 my $this = shift();
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 76 ▶{
Subroutine _update_timers: called CC=8 SCAR=21.8
589 76 my ($this, $delta) = @_;
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) 89 29 118
613 89 $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 20 ▶{
Subroutine _send_msg: called CC=6 SCAR=28.9
630 20 my ($this, $msg, $oktofail) = @_;
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 else {
650 0 $this->{_out_msg_buffer} = $buffer;
651 }
652  
653 0 return;
654 }
655  
656 20 $this->{_out_msg_buffer} = substr($buffer, $sent);
657 }
658  
659 sub _handle_socket_read_ready
660 32 ▶{
Subroutine _handle_socket_read_ready: called CC=14 SCAR=38.8
661 32 my $this = shift();
662  
663 32 my $socket = $this->{_peer_socket};
664  
665 32 ▶ unless (defined $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 32 32
666 0 warn $this->parent->asstring . ": Connection lost - Connection is fully shutdown now\n";
667 0 $this->_close_session();
668 0 $this->parent->reset_callback();
669 0 return;
670 }
671  
672 32 my $conn_closed = FALSE;
673 32 my $buffer = $this->{_in_msg_buffer};
674  
675 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
676 20 my $num_read = $socket->sysread($buffer, BGP_MESSAGE_HEADER_LENGTH, length($buffer));
677  
678 20 ▶ if ($!) { # Something went wrong with none-blocking connect()
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 20 20
679 0 $this->{_peer_socket} = $socket = undef;
680 0 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED);
681 0 return;
682 }
683  
684 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
685 0 $conn_closed = TRUE;
686 }
687 elsif ( $num_read != BGP_MESSAGE_HEADER_LENGTH ) {
688 0 $this->{_in_msg_buf_state} = AWAITING_HEADER_FRAGMENT;
689 0 $this->{_in_msg_buf_bytes_exp} = (BGP_MESSAGE_HEADER_LENGTH) - ($num_read);
690 0 $this->{_in_msg_buffer} = $buffer;
691 }
692 else {
693 20 $this->_decode_bgp_message_header($buffer);
694 20 $this->{_in_msg_buffer} = '';
695 }
696 }
697 elsif ( $this->{_in_msg_buf_state} == AWAITING_HEADER_FRAGMENT ) {
698 0 my $num_read = $socket->sysread($buffer, $this->{_in_msg_buf_bytes_exp}, length($buffer));
699 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
700 0 $conn_closed = TRUE;
701 }
702 elsif ( $num_read == $this->{_in_msg_buf_bytes_exp} ) {
703 0 $this->_decode_bgp_message_header($buffer);
704 0 $this->{_in_msg_buffer} = '';
705 }
706 else {
707 0 $this->{_in_msg_buf_bytes_exp} -= $num_read;
708 0 $this->{_in_msg_buffer} = $buffer;
709 }
710 }
711 elsif ( $this->{_in_msg_buf_state} == AWAITING_MESSAGE_FRAGMENT ) {
712 12 my $num_read = $socket->sysread($buffer, $this->{_in_msg_buf_bytes_exp}, length($buffer));
713 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
714 0 $conn_closed = TRUE;
715 }
716 elsif ( $num_read == $this->{_in_msg_buf_bytes_exp} ) {
717 12 $this->_enqueue_message($buffer);
718 12 $this->_enqueue_event($BGP_EVENT_MESSAGE_MAP[$this->{_in_msg_buf_type}]);
719 12 $this->{_in_msg_buffer} = '';
720 12 $this->{_in_msg_buf_state} = AWAITING_HEADER_START;
721 }
722 else {
723 0 $this->{_in_msg_buf_bytes_exp} -= $num_read;
724 0 $this->{_in_msg_buffer} = $buffer;
725 }
726 }
727 else {
728 0 croak("unknown socket state!\n");
729 }
730  
731 32 ▶ if ( $conn_closed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn_closed) 0 32 32
732 0 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_CLOSED);
733 }
734 }
735  
736 sub _handle_socket_write_ready
737 4 ▶{
Subroutine _handle_socket_write_ready: called CC=2 SCAR=7.0
738 4 my $this = shift();
739 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
740 4 $this->{_peer_socket_connected} = TRUE;
741 4 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_OPEN);
742 }
743  
744 sub _handle_socket_error_condition
745 0 ▶{
Subroutine _handle_socket_error_condition: not called CC=1 SCAR=6.9
746 0 my $this = shift();
747 0 warn "_handle_socket_error_condition()\n" . $this->{_peer_socket}->error(), "\n";
748 }
749  
750 sub _close_session
751 8 ▶{
Subroutine _close_session: called CC=2 SCAR=6.9
752 8 my $this = shift();
753 8 my $socket = $this->{_peer_socket};
754  
755 8 ▶ if ( defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) 4 4 8
756 4 $socket->close();
757 }
758  
759 8 $this->{_peer_socket} = $socket = undef;
760 8 $this->{_peer_socket_connected} = FALSE;
761 8 $this->{_in_msg_buffer} = '';
762 8 $this->{_out_msg_buffer} = '';
763 8 $this->{_in_msg_buf_state} = AWAITING_HEADER_START;
764 8 $this->{_hold_timer} = undef;
765 8 $this->{_keep_alive_timer} = undef;
766 8 $this->{_connect_retry_timer} = undef;
767 8 $this->{_message_queue} = [];
768  
769 8 return ( BGP_STATE_IDLE );
770 }
771  
772 sub _kill_session
773 6 ▶{
Subroutine _kill_session: called CC=2 SCAR=6.9
774 6 my ($this, $error) = @_;
775 6 my $buffer;
776  
777 6 ▶ if (defined($this->{_peer_socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_peer_socket"}) 2 4 6
778 2 $buffer = $this->_encode_bgp_notification_message(
779 $error->error_code(),
780 $error->error_subcode(),
781 $error->error_data()
782 );
783  
784 2 $this->_send_msg($buffer,1);
785 };
786 6 $this->_close_session();
787  
788 # invoke user callback function
789 6 $this->parent->error_callback($error);
790 }
791  
792 sub _ignore_start_event
793 0 ▶{
Subroutine _ignore_start_event: not called CC=1 SCAR=6.9
794 0 my $this = shift();
795 0 return ( $this->{_fsm_state} );
796 }
797  
798 sub _handle_receive_keepalive_message
799 8 ▶{
Subroutine _handle_receive_keepalive_message: called CC=2 SCAR=7.0
800 8 my $this = shift();
801  
802 # restart Hold Timer
803 8 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 8 0 8
804 8 $this->{_hold_timer} = $this->{_hold_time};
805 }
806  
807 # invoke user callback function
808 8 $this->parent->keepalive_callback();
809  
810 8 return ( BGP_STATE_ESTABLISHED );
811 }
812  
813 sub _handle_receive_update_message
814 4 ▶{
Subroutine _handle_receive_update_message: called CC=2 SCAR=7.0
815 4 my $this = shift();
816 4 my ($buffer, $update);
817  
818 # restart Hold Timer
819 4 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 4 0 4
820 4 $this->{_hold_timer} = $this->{_hold_time};
821 }
822  
823 4 $buffer = $this->_dequeue_message();
824 $update = Net::BGP::Update->_new_from_msg(
825 $buffer,
826 { as4 => $this->{_peer_as4} }
827 4 );
828  
829 # invoke user callback function
830 4 $this->parent->update_callback($update);
831  
832 4 return ( BGP_STATE_ESTABLISHED );
833 }
834  
835 sub _handle_receive_refresh_message
836 2 ▶{
Subroutine _handle_receive_refresh_message: called CC=3 SCAR=11.3
837 2 my $this = shift();
838 2 my ($buffer, $refresh);
839  
840 # restart Hold Timer
841 2 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 2 0 2
842 2 $this->{_hold_timer} = $this->{_hold_time};
843 }
844  
845 2 $buffer = $this->_dequeue_message();
846 2 $refresh = Net::BGP::Refresh->_new_from_msg($buffer);
847  
848 2 ▶ unless ( $this->parent->this_can_refresh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->parent->this_can_refresh) 0 2 2
849 0 Net::BGP::Notification->throw(
850 ErrorCode => BGP_ERROR_CODE_FINITE_STATE_MACHINE
851 );
852 }
853  
854 # invoke user callback function
855 2 $this->parent->refresh_callback($refresh);
856  
857 2 return ( BGP_STATE_ESTABLISHED );
858 }
859  
860 sub _handle_receive_notification_message
861 2 ▶{
Subroutine _handle_receive_notification_message: called CC=1 SCAR=0.0
862 2 my $this = shift();
863 2 my $error;
864  
865 2 $error = $this->_decode_bgp_notification_message($this->_dequeue_message());
866 2 $this->_close_session();
867  
868 # invoke user callback function
869 2 $this->parent->notification_callback($error);
870  
871 2 return ( BGP_STATE_IDLE );
872 }
873  
874 sub _handle_keepalive_expired
875 4 ▶{
Subroutine _handle_keepalive_expired: called CC=1 SCAR=0.0
876 4 my $this = shift();
877 4 my $buffer;
878  
879 # send KEEPALIVE message to peer
880 4 $buffer = $this->_encode_bgp_keepalive_message();
881 4 $this->_send_msg($buffer);
882  
883 # restart KeepAlive timer
884 4 $this->{_keep_alive_timer} = $this->{_keep_alive_time};
885  
886 4 return ( $this->{_fsm_state} );
887 }
888  
889 sub _handle_hold_timer_expired
890 0 ▶{
Subroutine _handle_hold_timer_expired: not called CC=1 SCAR=6.9
891 0 my $this = shift();
892  
893 0 $this->_error(BGP_ERROR_CODE_HOLD_TIMER_EXPIRED);
894 }
895  
896 sub _handle_bgp_fsm_error
897 0 ▶{
Subroutine _handle_bgp_fsm_error: not called CC=1 SCAR=6.9
898 0 my $this = shift();
899  
900 0 $this->_error(BGP_ERROR_CODE_FINITE_STATE_MACHINE);
901 }
902  
903 sub _handle_bgp_conn_open
904 4 ▶{
Subroutine _handle_bgp_conn_open: called CC=1 SCAR=0.0
905 4 my $this = shift();
906 4 my $buffer;
907  
908 # clear ConnectRetry timer
909 4 $this->{_connect_retry_timer} = undef;
910  
911 # send OPEN message to peer
912 4 $buffer = $this->_encode_bgp_open_message();
913 4 $this->_send_msg($buffer);
914  
915 4 return ( BGP_STATE_OPEN_SENT );
916 }
917  
918 sub _handle_collision_selfdestuct
919 0 ▶{
Subroutine _handle_collision_selfdestuct: not called CC=1 SCAR=6.9
920 0 my $this = shift;
921 0 $this->stop();
922 0 $this->parent->transport($this->{_sibling});
923 0 $this->{_sibling}->{_sibling} = undef;
924 }
925  
926 sub _handle_bgp_open_received
927 4 ▶{
Subroutine _handle_bgp_open_received: called CC=8 SCAR=31.0
928 4 my $this = shift();
929 4 my ($buffer, $this_id, $peer_id);
930  
931 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
932 ; # do failure stuff
933 0 return ( BGP_STATE_IDLE );
934 }
935  
936 # check for connection collision
937 4 ▶ if ( defined($this->{_sibling}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_sibling"}) 0 4 4
938 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
939 ($this->{_sibling}->{_fsm_state} == BGP_STATE_OPEN_CONFIRM) ) {
940  
941 0 $this_id = unpack('N', inet_aton($this->parent->this_id));
942 0 $peer_id = unpack('N', inet_aton($this->parent->peer_id));
943  
944 0 ▶ if ( $this_id < $peer_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_id < $peer_id) { } 0 0 0
945 0 $this->_handle_collision_selfdestuct;
946 0 return ( BGP_STATE_IDLE );
947 }
948 else {
949 0 $this->{_sibling}->_handle_collision_selfdestuct;
950 }
951 }
952 elsif ( ($this->{_sibling}->{_fsm_state} == BGP_STATE_ESTABLISHED) ) {
953 0 $this->_handle_collision_selfdestuct;
954 0 return ( BGP_STATE_IDLE );
955 }
956 else { # Other in Idle, conect, active
957 0 $this->{_sibling}->_handle_collision_selfdestuct;
958 }
959 }
960  
961 # clear the message buffer after decoding and validation
962 4 $this->{_message} = undef;
963  
964 # send KEEPALIVE message to peer
965 4 $buffer = $this->_encode_bgp_keepalive_message();
966 4 $this->_send_msg($buffer);
967  
968 # set Hold Time and KeepAlive timers
969 4 ▶ if ( $this->{_hold_time} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_hold_time"} != 0) 4 0 4
970 4 $this->{_hold_timer} = $this->{_hold_time};
971 4 $this->{_keep_alive_timer} = $this->{_keep_alive_time};
972 }
973  
974 # invoke user callback function
975 4 $this->parent->open_callback();
976  
977 # transition to state OpenConfirm
978 4 return ( BGP_STATE_OPEN_CONFIRM );
979 }
980  
981 sub _handle_open_sent_disconnect
982 0 ▶{
Subroutine _handle_open_sent_disconnect: not called CC=1 SCAR=6.9
983 0 my $this = shift();
984  
985 0 $this->_close_session();
986 0 return ( $this->_handle_connect_retry_restart() );
987 }
988  
989 sub _handle_connect_retry_restart
990 0 ▶{
Subroutine _handle_connect_retry_restart: not called CC=1 SCAR=6.9
991 0 my $this = shift();
992  
993 # restart ConnectRetry timer
994 0 $this->{_connect_retry_timer} = $this->{_connect_retry_time};
995  
996 0 return ( BGP_STATE_ACTIVE );
997 }
998  
999 sub _handle_bgp_start_event
1000 4 ▶{
Subroutine _handle_bgp_start_event: called CC=12 SCAR=30.5
1001 4 my $this = shift();
1002 4 my ($socket, $proto, $remote_addr, $this_addr, $rv);
1003  
1004 # initialize ConnectRetry timer
1005 4 ▶ if ( ! $this->parent->is_passive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->parent->is_passive) 2 2 4
1006 2 $this->{_connect_retry_timer} = $this->{_connect_retry_time};
1007 }
1008  
1009 # initiate the TCP transport connection
1010 4 ▶ if ( ! $this->parent->is_passive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->parent->is_passive) 2 2 4
1011 2 eval {
1012 2 $socket = IO::Socket->new( Domain => AF_INET );
1013 2 ▶ if ( ! defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 2 2
1014 0 die("IO::Socket construction failed");
1015 }
1016  
1017 2 $proto = getprotobyname('tcp');
1018 2 $rv = $socket->socket(PF_INET, SOCK_STREAM, $proto);
1019 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
1020 0 die("socket() failed");
1021 }
1022  
1023 2 $this_addr = sockaddr_in(0, inet_aton($this->parent->this_id));
1024 2 $rv = $socket->bind($this_addr);
1025 2 ▶ if ( ! $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 2 2
1026 0 die("bind() failed");
1027 }
1028  
1029 2 $rv = $socket->blocking(FALSE);
1030 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
1031 0 die("set socket non-blocking failed");
1032 }
1033  
1034 2 $remote_addr = sockaddr_in($this->parent->peer_port, inet_aton($this->parent->peer_id));
1035 2 $rv = $socket->connect($remote_addr);
1036 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
1037 0 ▶ die "OK - but connect() failed: $!" unless ($! == EINPROGRESS);
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 115 0 0 0
1038 }
1039  
1040 # $rv = $socket->blocking(TRUE);
1041 # if ( ! defined($rv) ) {
1042 # die("set socket blocking failed");
1043 # }
1044 };
1045  
1046 # check for exception in transport initiation
1047 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
1048 0 ▶ carp $@ unless $@ =~ /^OK/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^OK/ 0 0 0
1049 0 ▶ if ( defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) 0 0 0
1050 0 $socket->close();
1051 }
1052 0 $this->{_peer_socket} = $socket = undef;
1053 0 $this->_enqueue_event(BGP_EVENT_TRANSPORT_CONN_OPEN_FAILED);
1054 }
1055  
1056 2 $this->{_peer_socket} = $socket;
1057 2 $this->{_peer_socket_connected} = FALSE;
1058 }
1059  
1060 4 return ( BGP_STATE_CONNECT );
1061 }
1062  
1063 sub _min
1064 5 ▶{
Subroutine _min: called CC=2 SCAR=7.2
1065 5 my ($a, $b) = @_;
1066 5 ▶ return ( ($a < $b) ? $a : $b );
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 0 5 5
1067 }
1068  
1069 sub _cease
1070 6 ▶{
Subroutine _cease: called CC=2 SCAR=7.0
1071 6 my $this = shift();
1072  
1073 6 ▶ if ( $this->{_fsm_state} == BGP_STATE_ESTABLISHED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_fsm_state"} == BGP_STATE_ESTABLISHED()) 6 0 6
1074 6 $this->parent->reset_callback();
1075 }
1076  
1077 6 my $error = Net::BGP::Notification->new( ErrorCode => BGP_ERROR_CODE_CEASE );
1078  
1079 6 $this->_kill_session($error);
1080  
1081 6 return ( BGP_STATE_IDLE );
1082 }
1083  
1084 sub _encode_bgp_message
1085 22 ▶{
Subroutine _encode_bgp_message: called CC=3 SCAR=11.0
1086 22 my ($this, $type, $payload) = @_;
1087 22 my ($buffer, $length);
1088  
1089 22 $buffer = '';
1090 22 $length = BGP_MESSAGE_HEADER_LENGTH;
1091  
1092 22 ▶ if ( defined($payload) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload) 14 8 22
1093 14 $length += length($payload);
1094 14 $buffer = $payload;
1095 }
1096  
1097 # encode the type field
1098 22 $buffer = pack('C', $type) . $buffer;
1099  
1100 # encode the length field
1101 22 $buffer = pack('n', $length) . $buffer;
1102  
1103 # encode the marker field
1104 22 ▶ if ( defined($this->{_auth_data}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{'_auth_data'}) { } 0 22 22
1105 0 $buffer = $this->{_auth_data} . $buffer;
1106 }
1107 else {
1108 22 $buffer = (pack('C', 0xFF) x 16) . $buffer;
1109 }
1110  
1111 22 return ( $buffer );
1112 }
1113  
1114 sub _decode_bgp_message_header
1115 20 ▶{
Subroutine _decode_bgp_message_header: called CC=8 SCAR=22.9
1116 20 my ($this, $header) = @_;
1117 20 my ($marker, $length, $type);
1118  
1119 # validate the BGP message header length
1120 20 ▶ if ( length($header) != BGP_MESSAGE_HEADER_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $header != BGP_MESSAGE_HEADER_LENGTH()) 0 20 20
1121 0 $this->_error(
1122 BGP_ERROR_CODE_MESSAGE_HEADER,
1123 BGP_ERROR_SUBCODE_BAD_MSG_LENGTH,
1124 pack('n', length($header))
1125 );
1126 }
1127  
1128 # decode and validate the message header Marker field
1129 20 $marker = substr($header, 0, 16);
1130 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
1131 0 $this->_error(BGP_ERROR_CODE_MESSAGE_HEADER,
1132 BGP_ERROR_SUBCODE_CONN_NOT_SYNC);
1133 }
1134  
1135 # decode and validate the message header Length field
1136 20 $length = unpack('n', substr($header, 16, 2));
1137 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
1138 0 $this->_error(
1139 BGP_ERROR_CODE_MESSAGE_HEADER,
1140 BGP_ERROR_SUBCODE_BAD_MSG_LENGTH,
1141 pack('n', $length)
1142 );
1143 }
1144  
1145 # decode and validate the message header Type field
1146 20 $type = unpack('C', substr($header, 18, 1));
1147 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
1148 0 $this->_error(
1149 BGP_ERROR_CODE_MESSAGE_HEADER,
1150 BGP_ERROR_SUBCODE_BAD_MSG_TYPE,
1151 pack('C', $type)
1152 );
1153 }
1154  
1155 20 ▶ if ( $type == BGP_MESSAGE_KEEPALIVE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_MESSAGE_KEEPALIVE()) { } 8 12 20
1156 8 $this->{_in_msg_buffer} = '';
1157 8 $this->{_in_msg_buf_state} = AWAITING_HEADER_START;
1158 8 $this->{_in_msg_buf_bytes_exp} = 0;
1159 8 $this->{_in_msg_buf_type} = 0;
1160 8 $this->_enqueue_event(BGP_EVENT_RECEIVE_KEEP_ALIVE_MESSAGE);
1161 }
1162 else {
1163 12 $this->{_in_msg_buf_state} = AWAITING_MESSAGE_FRAGMENT;
1164 12 $this->{_in_msg_buf_bytes_exp} = $length - BGP_MESSAGE_HEADER_LENGTH;
1165 12 $this->{_in_msg_buf_type} = $type;
1166 }
1167  
1168 # indicate decoding and validation success
1169 20 return ( TRUE );
1170 }
1171  
1172 sub _encode_bgp_open_message
1173 6 ▶{
Subroutine _encode_bgp_open_message: called CC=9 SCAR=22.1
1174 6 my $this = shift();
1175 6 my ($buffer, $length);
1176  
1177 # encode optional parameters and length
1178 6 my $opt = '';
1179  
1180 6 ▶ if ($this->parent->support_capabilities) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->support_capabilities) 6 0 6
1181  
1182 6 ▶ if ( defined($this->{_peer_announced_id}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{'_peer_announced_id'}) { } 2 4 6
1183 # We received an open from the other end
1184  
1185 2 ▶ if ($this->{_peer_mbgp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_peer_mbgp"}) 2 0 2
1186 2 $opt .= $this->_encode_capability_mbgp();
1187 }
1188  
1189 2 ▶ if ($this->{_peer_as4}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_peer_as4"}) 1 1 2
1190 1 $opt .= $this->_encode_capability_as4();
1191 }
1192  
1193 } else {
1194 # We are sending the open
1195  
1196 4 ▶ if ( $this->parent->support_mbgp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->support_mbgp) 4 0 4
1197 4 $opt .= $this->_encode_capability_mbgp();
1198 }
1199 4 ▶ if ( $this->parent->this_can_as4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_can_as4) 2 2 4
1200 2 $opt .= $this->_encode_capability_as4();
1201 }
1202  
1203 }
1204  
1205 # Both the standard (2) and Cisco (128) capabilities are sent
1206 6 ▶ if ($this->parent->this_can_refresh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_can_refresh) 6 0 6
1207 6 $opt .= $this->_encode_capability(BGP_CAPABILITY_REFRESH, '');
1208 6 $opt .= $this->_encode_capability(BGP_CAPABILITY_REFRESH_OLD, '');
1209 }
1210 }
1211  
1212 6 $buffer = pack('C', length($opt)) . $opt;
1213  
1214 # encode BGP Identifier field
1215 6 $buffer = inet_aton($this->parent->this_id) . $buffer;
1216  
1217 # encode Hold Time
1218 6 $buffer = pack('n', $this->{_hold_time}) . $buffer;
1219  
1220 # encode local Autonomous System number
1221 6 ▶ if ($this->parent->this_as > 65535) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_as > 65535) { } 3 3 6
1222 3 $buffer = pack('n', 23456) . $buffer;
1223 } else {
1224 3 $buffer = pack('n', $this->parent->this_as) . $buffer;
1225 }
1226  
1227 # encode BGP version
1228 6 $buffer = pack('C', $this->{_bgp_version}) . $buffer;
1229  
1230 6 return ( $this->_encode_bgp_message(BGP_MESSAGE_OPEN, $buffer) );
1231 }
1232  
1233 sub _encode_capability_mbgp
1234 6 ▶{
Subroutine _encode_capability_mbgp: called CC=1 SCAR=0.0
1235 6 my $this = shift;
1236  
1237 # Capability 1 with data of:
1238 # Address family 1 (IPv4), reserved bit 0, type 1 (unicast)
1239 6 my $cap = pack('ncc', 1, 0, 1);
1240 6 my $opt = $this->_encode_capability(BGP_CAPABILITY_MBGP, $cap);
1241  
1242 6 return $opt;
1243 }
1244  
1245 sub _encode_capability_as4
1246 3 ▶{
Subroutine _encode_capability_as4: called CC=1 SCAR=0.0
1247 3 my $this = shift;
1248  
1249 # Capability 65 with data of the ASN
1250 3 my $cap = pack('N', $this->parent->this_as());
1251 3 my $opt = $this->_encode_capability(BGP_CAPABILITY_AS4, $cap);
1252  
1253 3 return $opt;
1254 }
1255  
1256 # Encodes a capability (inside the capability option)
1257 # RFC5492
1258 # Format is <2> <capability_len> <cap_code> <data_len>
1259 sub _encode_capability
1260 21 ▶{
Subroutine _encode_capability: called CC=1 SCAR=0.0
1261 21 my ($this, $type, $data) = @_;
1262  
1263 21 my $opt = '';
1264 21 $opt .= pack('C', BGP_OPTION_CAPABILITIES); # Option Type
1265  
1266 21 my $cap = '';
1267 21 $cap .= pack('C', $type); # Capability Type
1268 21 $cap .= pack('C', length($data)); # Capability Data Len
1269 21 $cap .= $data; # Capability data
1270  
1271 21 $opt .= pack('C', length($cap)); # Option Data Len
1272 21 $opt .= $cap;
1273  
1274 21 return $opt;
1275 }
1276  
1277 sub _decode_bgp_open_message
1278 5 ▶{
Subroutine _decode_bgp_open_message: called CC=9 SCAR=23.5
1279 5 my ($this, $buffer) = @_;
1280 5 my ($version, $as, $hold_time, $bgp_id);
1281  
1282 # decode and validate BGP version
1283 5 $version = unpack('C', substr($buffer, 0, 1));
1284 5 ▶ if ( $version != BGP_VERSION_4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version != BGP_VERSION_4()) 0 5 5
1285 0 $this->_error(
1286 BGP_ERROR_CODE_OPEN_MESSAGE,
1287 BGP_ERROR_SUBCODE_BAD_VERSION_NUM,
1288 pack('n', BGP_VERSION_4)
1289 );
1290 }
1291  
1292 # decode and validate remote Autonomous System number
1293 5 $as = unpack('n', substr($buffer, 1, 2));
1294 5 ▶ if ( $as != $this->parent->peer_as ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as != $this->parent->peer_as) 3 2 5
1295 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
1296 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1297 BGP_ERROR_SUBCODE_BAD_PEER_AS);
1298 } elsif ($as != 23456) {
1299 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1300 BGP_ERROR_SUBCODE_BAD_PEER_AS);
1301 }
1302 }
1303  
1304 # decode and validate received Hold Time
1305 5 $hold_time = _min(unpack('n', substr($buffer, 3, 2)), $this->{_hold_time});
1306 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
1307 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1308 BGP_ERROR_SUBCODE_BAD_HOLD_TIME);
1309 }
1310  
1311 # decode received BGP Identifier
1312 # Spelling error is retained for compatibility.
1313 5 $this->{_peer_annonced_id} = inet_ntoa(substr($buffer, 5, 4));
1314 5 $this->{_peer_announced_id} = inet_ntoa(substr($buffer, 5, 4));
1315  
1316 # decode known Optional Parameters
1317 5 my $opt_length = unpack('c', substr($buffer, 9, 1));
1318 5 my $opt = substr($buffer, 10, $opt_length);
1319 5 ▶ while ($opt ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($opt ne "") 15 5 20
1320 {
1321 15 my ($type, $length) = unpack('cc', substr($opt, 0, 2));
1322 15 my $value = substr($opt, 2, $length);
1323 15 ▶ if ($type eq BGP_OPTION_CAPABILITIES)
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq BGP_OPTION_CAPABILITIES()) { } 15 0 15
1324 {
1325 15 $this->_decode_capabilities($value);
1326 }
1327 else
1328 { # Unknown optional parameter!
1329 # XXX We should send a notify here.
1330 }
1331 15 $opt = substr($opt, 2+$length);
1332 };
1333  
1334 # set Hold Time to negotiated value
1335 5 $this->{_hold_time} = $hold_time;
1336  
1337 # indicate decoding and validation success
1338 5 return ( TRUE );
1339 }
1340  
1341 # Capabilities we don't understand get ignored.
1342 sub _decode_capabilities
1343 15 ▶{
Subroutine _decode_capabilities: called CC=3 SCAR=11.3
1344 15 my ($this, $value) = @_;
1345  
1346 15 $this->{'_peer_refresh'} = TRUE;
1347  
1348 15 ▶ while (length($value) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > 0) 17 15 32
1349  
1350 17 ▶ if (length($value) < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value < 2) 0 17 17
1351 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1352 BGP_ERROR_SUBCODE_BAD_OPT_PARAMETER);
1353 0 return;
1354 }
1355  
1356 17 my ($type, $len) = unpack('cc', substr($value, 0, 2));
1357 17 my $data = substr($value, 2, $len);
1358  
1359 17 $this->_decode_one_capability($type, $len, $data);
1360  
1361 17 $value = substr($value, 2+$len);
1362 }
1363  
1364 }
1365  
1366 17 ▶sub _decode_one_capability {
Subroutine _decode_one_capability: called CC=9 SCAR=23.6
1367 17 my ($this, $type, $len, $data) = @_;
1368  
1369 17 ▶ if (length($data) != $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data != $len) 0 17 17
1370 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1371 BGP_ERROR_SUBCODE_BAD_OPT_PARAMETER);
1372 }
1373  
1374 17 ▶ if ($type == BGP_CAPABILITY_MBGP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_MBGP()) 5 12 17
1375 5 $this->{_peer_mbgp} = TRUE;
1376 }
1377  
1378 17 ▶ if ($type == BGP_CAPABILITY_REFRESH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_REFRESH()) 5 12 17
1379 5 $this->{_peer_refresh} = TRUE;
1380 }
1381 17 ▶ if ($type == BGP_CAPABILITY_REFRESH_OLD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_REFRESH_OLD()) 0 17 17
1382 0 $this->{_peer_refresh} = TRUE;
1383 }
1384  
1385 17 ▶ if ($type == BGP_CAPABILITY_AS4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == BGP_CAPABILITY_AS4()) 3 14 17
1386  
1387 3 ▶ if ($len != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len != 4) 0 3 3
1388 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1389 BGP_ERROR_SUBCODE_BAD_OPT_PARAMETER);
1390 }
1391
1392 3 my $as = unpack('N', $data);
1393 3 ▶ if ($as != $this->parent->peer_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as != $this->parent->peer_as) 0 3 3
1394 0 $this->_error(BGP_ERROR_CODE_OPEN_MESSAGE,
1395 BGP_ERROR_SUBCODE_BAD_PEER_AS);
1396 }
1397  
1398 # Both ends must support this.
1399 3 ▶ if ( $this->parent->this_can_as4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->parent->this_can_as4) 3 0 3
1400 3 $this->{_peer_as4} = TRUE;
1401 }
1402 }
1403  
1404 }
1405  
1406 sub _decode_bgp_notification_message
1407 2 ▶{
Subroutine _decode_bgp_notification_message: called CC=5 SCAR=18.9
1408 2 my ($this, $buffer) = @_;
1409 2 my ($error, $error_code, $error_subcode, $data);
1410  
1411 # decode and validate Error code
1412 2 $error_code = unpack('C', substr($buffer, 0, 1));
1413 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
1414 0 die("_decode_bgp_notification_message(): invalid error code = $error_code\n");
1415 }
1416  
1417 # decode and validate Error subcode
1418 2 $error_subcode = unpack('C', substr($buffer, 1, 1));
1419 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
1420 0 die("_decode_bgp_notification_message(): invalid error subcode = $error_subcode\n");
1421 }
1422  
1423 # decode Data field
1424 2 $data = substr($buffer, 2, length($buffer) - 2);
1425  
1426 2 return Net::BGP::Notification->new(
1427 ErrorCode => $error_code,
1428 ErrorSubcode => $error_subcode,
1429 ErrorData => $data);
1430 }
1431  
1432 sub _encode_bgp_keepalive_message
1433 8 ▶{
Subroutine _encode_bgp_keepalive_message: called CC=1 SCAR=0.0
1434 8 my $this = shift();
1435 8 return ( $this->_encode_bgp_message(BGP_MESSAGE_KEEPALIVE) );
1436 }
1437  
1438 sub _encode_bgp_update_message
1439 4 ▶{
Subroutine _encode_bgp_update_message: called CC=1 SCAR=0.0
1440 4 my ($this, $buffer) = @_;
1441 4 return ( $this->_encode_bgp_message(BGP_MESSAGE_UPDATE, $buffer) );
1442 }
1443  
1444 sub _encode_bgp_refresh_message
1445 2 ▶{
Subroutine _encode_bgp_refresh_message: called CC=1 SCAR=0.0
1446 2 my ($this, $buffer) = @_;
1447 2 return ( $this->_encode_bgp_message(BGP_MESSAGE_REFRESH, $buffer) );
1448 }
1449  
1450 sub _encode_bgp_notification_message
1451 2 ▶{
Subroutine _encode_bgp_notification_message: called CC=2 SCAR=7.0
1452 2 my ($this, $error_code, $error_subcode, $data) = @_;
1453 2 my $buffer;
1454  
1455 # encode the Data field
1456 2 ▶ $buffer = $data ? $data : '';
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 0 2 2
1457  
1458 # encode the Error Subcode field
1459 2 $buffer = pack('C', $error_subcode) . $buffer;
1460  
1461 # encode the Error Code field
1462 2 $buffer = pack('C', $error_code) . $buffer;
1463  
1464 2 return ( $this->_encode_bgp_message(BGP_MESSAGE_NOTIFICATION, $buffer) );
1465 }
1466  
1467 ## POD ##
1468  
1469 =pod
1470  
1471 =head1 NAME
1472  
1473 C<Net::BGP::Transport> - Class encapsulating BGP-4 transport session state and functionality
1474  
1475 =head1 SYNOPSIS
1476  
1477 use Net::BGP::Transport;
1478  
1479 $trans = Net::BGP::Transport->new(
1480 Start => 1,
1481 Parent => Net::BGP::Peer->new(),
1482 ConnectRetryTime => 300,
1483 HoldTime => 60,
1484 KeepAliveTime => 20
1485 );
1486  
1487 $version = $trans->version();
1488  
1489 $trans->start();
1490 $trans->stop();
1491  
1492 $trans->update($update);
1493 $trans->refresh($refresh);
1494  
1495  
1496 =head1 DESCRIPTION
1497  
1498 This module encapsulates the state and functionality associated with a BGP
1499 transport connection. Each instance of a C<Net::BGP::Transport> object
1500 corresponds to a TCP session with a distinct peer. It should not be used by
1501 itself, but encapsulated in a L<Net::BGP::Peer> object.
1502  
1503 =head1 CONSTRUCTOR
1504  
1505 =over 4
1506  
1507 =item new() - create a new C<Net::BGP::Transport> object
1508  
1509 This is the constructor for C<Net::BGP::Transport> objects. It returns a
1510 reference to the newly created object. The following named parameters may
1511 be passed to the constructor. Once the object is created, the information
1512 can not be changed.
1513  
1514 =over 4
1515  
1516 =item Start
1517  
1518 =item ConnectRetryTime
1519  
1520 =item HoldTime
1521  
1522 =item KeepAliveTime
1523  
1524 Has the same meaning as the equivalent named argument for L<Net::BGP::Peer>.
1525  
1526 =item Parent
1527  
1528 The parent L<Net::BGP::Peer> object.
1529  
1530 =back
1531  
1532 =item renew() - fetch the existing L<Net::BGP::Peer> object from the "object string".
1533  
1534 This "reconstructor" returns a previously constructed object from the
1535 perl generated string-context scalar of the object, e.g.
1536 I<Net::BGP::Transport=HASH(0x820952c)>.
1537  
1538 =back
1539  
1540 =head1 ACCESSOR METHODS
1541  
1542 =over 4
1543  
1544 =item version()
1545  
1546 =item start()
1547  
1548 =item stop()
1549  
1550 =item update()
1551  
1552 =item refresh()
1553  
1554 =item is_established()
1555  
1556 These methods do the actual work for the methods of the same name in
1557 L<Net::BGP::Peer>.
1558  
1559 =back
1560  
1561 =head1 SEE ALSO
1562  
1563 Net::BGP::Peer, Net::BGP, Net::BGP::Update, Net::BGP::Refresh
1564  
1565 =head1 AUTHOR
1566  
1567 Stephen J. Scheck <sscheck@cpan.org> in original Peer.pm form
1568 Martin Lorensen <lorensen@cpan.org> separated into Transport.pm
1569  
1570 =cut
1571  
1572 ## End Package Net::BGP::Transport ##
1573  
1574 4 1;
« BGP/ASPath/AS.pm ↑ Summary BGP/ASPath.pm »