BGP/Peer.pm

filter: Statementstmt 78.3% 181 / 231 Branchbran 57.1% 48 / 84 Conditioncond 58.3% 7 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 87.1% 54 / 62 Podpod 0.0% 0 / 49 totaltotal 65.7% 292 / 444 CC 51 SCAR 28.7
CC
51
Cov
72%
new
65.0
dump_hex
12.0
_update_timers
6.1
CRAP 17.6
↑ Summary BGP/Update.pm »
1 #!/usr/bin/perl
2  
3 package Net::BGP::Peer;
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 vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @BGP @GENERIC
9 @BGP_EVENT_MESSAGE_MAP @BGP_EVENTS @BGP_FSM @BGP_STATES
10 4 );
11  
12 ## Inheritance and Versioning ##
13  
14 4 @ISA = qw( Exporter );
15 4 $VERSION = '0.18';
16  
17 ## General Definitions ##
18  
19 17 ▶sub TRUE { 1 }
Subroutine TRUE: called CC=1 SCAR=0.0
Pod undocumented
20 17 ▶sub FALSE { 0 }
Subroutine FALSE: called CC=1 SCAR=0.0
Pod undocumented
21  
22 ## BGP Network Constants ##
23  
24 8 ▶sub BGP_PORT { 179 }
Subroutine BGP_PORT: called CC=1 SCAR=0.0
Pod undocumented
25 5 ▶sub BGP_VERSION_4 { 4 }
Subroutine BGP_VERSION_4: called CC=1 SCAR=0.0
Pod undocumented
26  
27 ## Export Tag Definitions ##
28  
29 4 @BGP = qw( BGP_PORT BGP_VERSION_4 );
30 4 @GENERIC = qw( TRUE FALSE dump_hex );
31 4 @EXPORT = ();
32 4 @EXPORT_OK = ( @BGP, @GENERIC );
33 4 %EXPORT_TAGS = (
34 bgp => [ @BGP ],
35 generic => [ @GENERIC ],
36 ALL => [ @EXPORT, @EXPORT_OK ]
37 );
38  
39 ## Module Imports ##
40  
41 4 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use Net::BGP::Notification qw( :errors );
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use Net::BGP::Refresh;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use Net::BGP::Update;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶use Net::BGP::Transport;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 ## Global private variables ##
52  
53 4 my %knownpeers;
54  
55 ## Generic Subroutines ##
56  
57 # This subroutine was snicked from David Town's excellent Net::SNMP
58 # module and renamed as dump_hex(). Removed class dependence and made
59 # into standalone subroutine.
60  
61 sub dump_hex
62 0 ▶{
Subroutine dump_hex: not called CC=3 SCAR=24.8
Pod undocumented
63 0 my $data = shift();
64 0 my ($length, $offset, $line, $hex) = (0, 0, '', '');
65 0 my $string;
66  
67 0 $string = '';
68 0 $length = length($data);
69  
70 0 ▶ while ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
71 0 ▶ if ($length >= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length >= 16) { } 0 0 0
72 0 $line = substr($data, $offset, 16);
73 } else {
74 0 $line = substr($data, $offset, $length);
75 }
76 0 $hex = unpack('H*', $line);
77 0 $hex .= ' ' x (32 - length($hex));
78 0 $hex = sprintf("%s %s %s %s " x 4, unpack('a2' x 16, $hex));
79 0 $line =~ s/[\x00-\x1f\x7f-\xff]/./g;
80 0 $string .= sprintf("[%03d] %s %s\n", $offset, uc($hex), $line);
81 0 $offset += 16;
82 0 $length -= 16;
83 }
84  
85 0 return ( $string );
86 }
87  
88 ## Public Class Methods ##
89  
90 sub new
91 5 ▶{
Subroutine new: called CC=27 SCAR=41.7
Pod undocumented
92 5 my $class = shift();
93 5 my ($arg, $value);
94  
95 5 my $this = {
96 _bgp_version => BGP_VERSION_4,
97 _local_id => undef,
98 _peer_id => undef,
99 _peer_port => BGP_PORT,
100 _local_as => 0,
101 _peer_as => 0,
102 _user_timers => {},
103 _transport => undef,
104 _listen => TRUE,
105 _passive => FALSE,
106 _announce_refresh => FALSE,
107 _support_capabilities => TRUE,
108 _support_mbgp => TRUE,
109 _support_as4 => FALSE,
110 _opaque_data => undef,
111 _open_callback => undef,
112 _established_callback => undef,
113 _keepalive_callback => undef,
114 _update_callback => undef,
115 _refresh_callback => undef,
116 _reset_callback => undef,
117 _notification_callback => undef,
118 _error_callback => undef
119 };
120  
121 5 bless($this, $class);
122  
123 5 my %transarg = ();
124 5 ▶ while ( defined($arg = shift()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($arg = shift())) 54 5 59
125 54 $value = shift();
126  
127 54 ▶ if (( $arg =~ /^start$/i )
Branch: 22 branches
BranchTrueFalseTotal
if ($arg =~ /^start$/i or $arg =~ /^connectretrytime$/i or $arg =~ /^keepalivetime$/i or $arg =~ /^holdtime$/i) { } 9 45 54
elsif ($arg =~ /^thisid$/i) { } 5 40 45
elsif ($arg =~ /^thisas$/i) { } 5 35 40
elsif ($arg =~ /^peerid$/i) { } 5 30 35
elsif ($arg =~ /^peeras$/i) { } 5 25 30
elsif ($arg =~ /^peerport$/i) { } 4 21 25
elsif ($arg =~ /^listen$/i) { } 5 16 21
elsif ($arg =~ /^passive$/i) { } 5 11 16
elsif ($arg =~ /^refreshcallback$/i) { } 0 11 11
elsif ($arg =~ /^opaquedata$/i) { } 0 11 11
elsif ($arg =~ /^refresh$/i) { } 0 11 11
elsif ($arg =~ /^announcerefresh$/i) { } 4 7 11
elsif ($arg =~ /^supportcapabilities$/i) { } 0 7 7
elsif ($arg =~ /^supportmbgp$/i) { } 4 3 7
elsif ($arg =~ /^supportas4$/i) { } 3 0 3
elsif ($arg =~ /^opencallback$/i) { } 0 0 0
elsif ($arg =~ /^establishedcallback$/i) { } 0 0 0
elsif ($arg =~ /^keepalivecallback$/i) { } 0 0 0
elsif ($arg =~ /^updatecallback$/i) { } 0 0 0
elsif ($arg =~ /^resetcallback$/i) { } 0 0 0
elsif ($arg =~ /^notificationcallback$/i) { } 0 0 0
elsif ($arg =~ /^errorcallback$/i) { } 0 0 0
Condition77%7/9
$arg =~ /^start$/i or $arg =~ /^connectretrytime$/i or $arg =~ /^keepalivetime$/i or $arg =~ /^holdtime$/i
l!l&&r!l&&!r
9045
$arg =~ /^start$/i or $arg =~ /^connectretrytime$/i or $arg =~ /^keepalivetime$/i
l!l&&r!l&&!r
5445
$arg =~ /^start$/i or $arg =~ /^connectretrytime$/i
l!l&&r!l&&!r
5049
MC/DC50%2/4
$arg =~ /^start$/i or $arg =~ /^connectretrytime$/i or $arg =~ /^keepalivetime$/i or $arg =~ /^holdtime$/i
$arg =~ /^start$/i $arg =~ /^connectretrytime$/i $arg =~ /^keepalivetime$/i $arg =~ /^holdtime$/i
Truth table60%3/5
A or B or C or D
A= $arg =~ /^start$/iB= $arg =~ /^connectretrytime$/iC= $arg =~ /^keepalivetime$/iD= $arg =~ /^holdtime$/i
ABCDresult
1XXX1
01XX1
001X1
00011
00000
128 || ( $arg =~ /^connectretrytime$/i )
129 || ( $arg =~ /^keepalivetime$/i )
130 || ( $arg =~ /^holdtime$/i )) {
131 9 $transarg{$arg} = $value;
132 }
133 elsif ( $arg =~ /^thisid$/i ) {
134 5 $this->{_local_id} = $value;
135 }
136 elsif ( $arg =~ /^thisas$/i ) {
137 5 $this->{_local_as} = $value;
138 }
139 elsif ( $arg =~ /^peerid$/i ) {
140 5 $this->{_peer_id} = $value;
141 }
142 elsif ( $arg =~ /^peeras$/i ) {
143 5 $this->{_peer_as} = $value;
144 }
145 elsif ( $arg =~ /^peerport$/i ) {
146 4 $this->{_peer_port} = $value;
147 }
148 elsif ( $arg =~ /^listen$/i ) {
149 5 $this->{_listen} = $value;
150 }
151 elsif ( $arg =~ /^passive$/i ) {
152 5 $this->{_passive} = $value;
153 }
154 elsif ( $arg =~ /^refreshcallback$/i ) {
155 0 $this->{_refresh_callback} = $value;
156 }
157 elsif ( $arg =~ /^opaquedata$/i ) {
158 0 $this->{_opaque_data} = $value;
159 }
160 elsif ( $arg =~ /^refresh$/i ) {
161 0 warnings::warnif(
162 'deprecated',
163 'Refresh in is deprecated, use AnnounceRefresh instead'
164 );
165 0 $this->{_announce_refresh} = $value;
166 }
167 elsif ( $arg =~ /^announcerefresh$/i ) {
168 4 $this->{_announce_refresh} = $value;
169 }
170 elsif ( $arg =~ /^supportcapabilities$/i ) {
171 0 $this->{_support_capabilities} = $value;
172 }
173 elsif ( $arg =~ /^supportmbgp$/i ) {
174 4 $this->{_support_mbgp} = $value;
175 }
176 elsif ( $arg =~ /^supportas4$/i ) {
177 3 $this->{_support_as4} = $value;
178 }
179 elsif ( $arg =~ /^opencallback$/i ) {
180 0 $this->{_open_callback} = $value;
181 }
182 elsif ( $arg =~ /^establishedcallback$/i ) {
183 0 $this->{_established_callback} = $value;
184 }
185 elsif ( $arg =~ /^keepalivecallback$/i ) {
186 0 $this->{_keepalive_callback} = $value;
187 }
188 elsif ( $arg =~ /^updatecallback$/i ) {
189 0 $this->{_update_callback} = $value;
190 }
191 elsif ( $arg =~ /^resetcallback$/i ) {
192 0 $this->{_reset_callback} = $value;
193 }
194 elsif ( $arg =~ /^notificationcallback$/i ) {
195 0 $this->{_notification_callback} = $value;
196 }
197 elsif ( $arg =~ /^errorcallback$/i ) {
198 0 $this->{_error_callback} = $value;
199 }
200 else {
201 0 croak "unrecognized argument $arg\n";
202 }
203 }
204  
205 5 $transarg{parent} = $this;
206 5 $this->{_transport} = Net::BGP::Transport->new(%transarg);
207  
208 5 weaken($knownpeers{$this} = $this);
209 5 return ( $this );
210 }
211  
212 sub renew
213 0 ▶{
Subroutine renew: not called CC=2 SCAR=17.9
Pod undocumented
214 0 my $proto = shift;
215 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
216 0 my $name = shift;
217 0 return $knownpeers{$name};
218 }
219  
220 ## Public Object Methods - Proxies for backward compatibility ##
221  
222 sub start
223 0 ▶{
Subroutine start: not called CC=2 SCAR=17.9
Pod undocumented
224 0 foreach my $trans (shift->transports)
225 {
226 0 $trans->start();
227 };
228 }
229  
230 sub stop
231 6 ▶{
Subroutine stop: called CC=2 SCAR=6.9
Pod undocumented
232 6 foreach my $trans (shift->transports)
233 {
234 6 $trans->stop();
235 };
236 }
237  
238 sub version
239 0 ▶{
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
240 0 return shift->transport->version;
241 }
242  
243 sub update
244 4 ▶{
Subroutine update: called CC=1 SCAR=0.0
Pod undocumented
245 4 return shift->transport->update(@_);
246 }
247  
248 sub refresh
249 2 ▶{
Subroutine refresh: called CC=1 SCAR=0.0
Pod undocumented
250 2 return shift->transport->refresh(@_);
251 }
252  
253 ## Public Object Methods ##
254  
255 sub transport
256 413 ▶{
Subroutine transport: called CC=2 SCAR=7.1
Pod undocumented
257 413 my $this = shift();
258 413 ▶ $this->{_transport} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 413 413
259 413 return $this->{_transport};
260 }
261  
262 sub transports
263 162 ▶{
Subroutine transports: called CC=2 SCAR=8.1
Pod undocumented
264 162 my $this = shift();
265 162 ▶ return ($this->transport) unless defined $this->transport->sibling;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->transport->sibling 162 0 162
266 0 return ($this->transport,$this->transport->sibling);
267 }
268  
269 sub this_id
270 14 ▶{
Subroutine this_id: called CC=1 SCAR=0.0
Pod undocumented
271 14 my $this = shift();
272 14 return ( $this->{_local_id} );
273 }
274  
275 sub this_as
276 12 ▶{
Subroutine this_as: called CC=1 SCAR=0.0
Pod undocumented
277 12 my $this = shift();
278 12 return ( $this->{_local_as} );
279 }
280  
281 sub this_can_refresh
282 8 ▶{
Subroutine this_can_refresh: called CC=1 SCAR=0.0
Pod undocumented
283 8 my $this = shift();
284 8 return ( $this->{_announce_refresh} );
285 }
286  
287 sub this_can_as4
288 8 ▶{
Subroutine this_can_as4: called CC=1 SCAR=0.0
Pod undocumented
289 8 my $this = shift();
290 8 return ( $this->{_support_as4} );
291 }
292  
293 sub support_capabilities
294 6 ▶{
Subroutine support_capabilities: called CC=1 SCAR=0.0
Pod undocumented
295 6 my $this = shift();
296 6 return ( $this->{_support_capabilities} );
297 }
298  
299 sub support_mbgp
300 6 ▶{
Subroutine support_mbgp: called CC=1 SCAR=0.0
Pod undocumented
301 6 my $this = shift();
302 6 return ( $this->{_support_mbgp} );
303 }
304  
305 sub peer_id
306 8 ▶{
Subroutine peer_id: called CC=1 SCAR=0.0
Pod undocumented
307 8 my $this = shift();
308 8 return ( $this->{_peer_id} );
309 }
310  
311 sub peer_as
312 11 ▶{
Subroutine peer_as: called CC=1 SCAR=0.0
Pod undocumented
313 11 my $this = shift();
314 11 return ( $this->{_peer_as} );
315 }
316  
317 sub peer_port
318 2 ▶{
Subroutine peer_port: called CC=1 SCAR=0.0
Pod undocumented
319 2 my $this = shift();
320 2 return ( $this->{_peer_port} );
321 }
322  
323 sub peer_can_as4
324 1 ▶{
Subroutine peer_can_as4: called CC=1 SCAR=0.0
Pod undocumented
325 1 my $this = shift();
326 1 return $this->transport->can_as4;
327 }
328  
329 sub peer_can_mbgp
330 2 ▶{
Subroutine peer_can_mbgp: called CC=1 SCAR=0.0
Pod undocumented
331 2 my $this = shift();
332 2 return $this->transport->can_mbgp;
333 }
334  
335 sub peer_can_refresh
336 0 ▶{
Subroutine peer_can_refresh: not called CC=1 SCAR=6.9
Pod undocumented
337 0 my $this = shift();
338 0 return $this->transport->can_refresh;
339 }
340  
341 sub is_listener
342 9 ▶{
Subroutine is_listener: called CC=1 SCAR=0.0
Pod undocumented
343 9 my $this = shift();
344 9 return ( $this->{_listen} );
345 }
346  
347 sub is_passive
348 10 ▶{
Subroutine is_passive: called CC=1 SCAR=0.0
Pod undocumented
349 10 my $this = shift();
350 10 return ( $this->{_passive} );
351 }
352  
353 sub is_established
354 0 ▶{
Subroutine is_established: not called CC=1 SCAR=6.9
Pod undocumented
355 0 my $this = shift();
356 0 return ( $this->transport->is_established );
357 }
358  
359 sub opaque_data
360 0 ▶{
Subroutine opaque_data: not called CC=2 SCAR=17.9
Pod undocumented
361 0 my ($this, $opaque_data) = @_;
362 0 my $old_value = $this->{_opaque_data};
363 0 ▶ $this->{_opaque_data} = $opaque_data if defined($opaque_data);
Branch: 1 branch
BranchTrueFalseTotal
if defined $opaque_data 0 0 0
364 0 return ( $old_value );
365 }
366  
367 sub set_open_callback
368 2 ▶{
Subroutine set_open_callback: called CC=1 SCAR=0.0
Pod undocumented
369 2 my ($this, $callback) = @_;
370 2 $this->{_open_callback} = $callback;
371 }
372  
373 sub set_established_callback
374 2 ▶{
Subroutine set_established_callback: called CC=1 SCAR=0.0
Pod undocumented
375 2 my ($this, $callback) = @_;
376 2 $this->{_established_callback} = $callback;
377 }
378  
379 sub set_keepalive_callback
380 4 ▶{
Subroutine set_keepalive_callback: called CC=1 SCAR=0.0
Pod undocumented
381 4 my ($this, $callback) = @_;
382 4 $this->{_keepalive_callback} = $callback;
383 }
384  
385 sub set_update_callback
386 2 ▶{
Subroutine set_update_callback: called CC=1 SCAR=0.0
Pod undocumented
387 2 my ($this, $callback) = @_;
388 2 $this->{_update_callback} = $callback;
389 }
390  
391 sub set_refresh_callback
392 2 ▶{
Subroutine set_refresh_callback: called CC=1 SCAR=0.0
Pod undocumented
393 2 my ($this, $callback) = @_;
394 2 $this->{_refresh_callback} = $callback;
395 }
396  
397 sub set_reset_callback
398 4 ▶{
Subroutine set_reset_callback: called CC=1 SCAR=0.0
Pod undocumented
399 4 my ($this, $callback) = @_;
400 4 $this->{_reset_callback} = $callback;
401 }
402  
403 sub set_notification_callback
404 4 ▶{
Subroutine set_notification_callback: called CC=1 SCAR=0.0
Pod undocumented
405 4 my ($this, $callback) = @_;
406 4 $this->{_notification_callback} = $callback;
407 }
408  
409 sub set_error_callback
410 6 ▶{
Subroutine set_error_callback: called CC=1 SCAR=0.0
Pod undocumented
411 6 my ($this, $callback) = @_;
412 6 $this->{_error_callback} = $callback;
413 }
414  
415 sub add_timer
416 2 ▶{
Subroutine add_timer: called CC=1 SCAR=0.0
Pod undocumented
417 2 my ($this, $callback, $timeout) = @_;
418 2 my $timer;
419  
420 2 $timer = {
421 _timer => $timeout,
422 _timeout => $timeout,
423 _callback => $callback,
424 };
425  
426 2 $this->{_user_timers}->{$timer} = $timer;
427 }
428  
429 sub remove_timer
430 2 ▶{
Subroutine remove_timer: called CC=3 SCAR=11.0
Pod undocumented
431 2 my ($this, $callback) = @_;
432 2 my ($key, $timer);
433  
434 2 foreach $key ( keys(%{$this->{_user_timers}}) ) {
435 2 $timer = $this->{_user_timers}->{$key};
436 2 ▶ if ( $timer->{_callback} == $callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer->{"_callback"} == $callback) 2 0 2
437 2 delete $this->{_user_timers}->{$key};
438 }
439 }
440 }
441  
442 sub asstring
443 0 ▶{
Subroutine asstring: not called CC=2 SCAR=17.9
Pod undocumented
444 0 my $this = shift;
445  
446 0 ▶ my @l = map { defined $_ ? $_ : 'n/a'; } (
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
447 $this->this_id, $this->this_as,
448 $this->peer_id, $this->peer_as);
449  
450 0 return $l[0] . ':' . $l[1] . '<=>' . $l[2] . ':' . $l[3];
451 }
452  
453 ## Overridable Methods ##
454  
455 sub open_callback
456 4 ▶{
Subroutine open_callback: called CC=2 SCAR=6.9
Pod undocumented
457 4 my $this = shift();
458  
459 4 ▶ if ( defined($this->{_open_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_open_callback"}) 2 2 4
460 2 &{ $this->{_open_callback} }($this);
461 }
462 }
463  
464 sub established_callback
465 4 ▶{
Subroutine established_callback: called CC=2 SCAR=6.9
Pod undocumented
466 4 my $this = shift();
467  
468 4 ▶ if ( defined($this->{_established_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_established_callback"}) 2 2 4
469 2 &{ $this->{_established_callback} }($this);
470 }
471 }
472  
473 sub keepalive_callback
474 8 ▶{
Subroutine keepalive_callback: called CC=2 SCAR=6.9
Pod undocumented
475 8 my $this = shift();
476  
477 8 ▶ if ( defined($this->{_keepalive_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_keepalive_callback"}) 4 4 8
478 4 &{ $this->{_keepalive_callback} }($this);
479 }
480 }
481  
482 sub update_callback
483 4 ▶{
Subroutine update_callback: called CC=2 SCAR=7.0
Pod undocumented
484 4 my ($this, $update) = @_;
485  
486 4 ▶ if ( defined($this->{_update_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_update_callback"}) 4 0 4
487 4 &{ $this->{_update_callback} }($this, $update);
488 }
489 }
490  
491 sub refresh_callback
492 6 ▶{
Subroutine refresh_callback: called CC=2 SCAR=6.9
Pod undocumented
493 6 my ($this,$refresh) = @_;
494  
495 6 ▶ if ( defined($this->{_refresh_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_refresh_callback"}) 4 2 6
496 4 &{ $this->{_refresh_callback} }($this,$refresh);
497 }
498 }
499  
500 sub reset_callback
501 8 ▶{
Subroutine reset_callback: called CC=2 SCAR=7.0
Pod undocumented
502 8 my ($this,$refresh) = @_;
503  
504 8 ▶ if ( defined($this->{_reset_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_reset_callback"}) 8 0 8
505 8 &{ $this->{_reset_callback} }($this);
506 }
507 }
508  
509 sub notification_callback
510 2 ▶{
Subroutine notification_callback: called CC=2 SCAR=7.0
Pod undocumented
511 2 my ($this, $error) = @_;
512  
513 2 ▶ if ( defined($this->{_notification_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_notification_callback"}) 2 0 2
514 2 &{ $this->{_notification_callback} }($this, $error);
515 }
516 }
517  
518 sub error_callback
519 6 ▶{
Subroutine error_callback: called CC=2 SCAR=6.9
Pod undocumented
520 6 my ($this, $error) = @_;
521  
522 6 ▶ if ( defined($this->{_error_callback}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_error_callback"}) 4 2 6
523 4 &{ $this->{_error_callback} }($this, $error);
524 }
525 }
526  
527 ## Private Object Methods ##
528  
529 sub _update_timers
530 76 ▶{
Subroutine _update_timers: called CC=6 SCAR=18.0
531 76 my ($this, $delta) = @_;
532  
533 # Proxy for transport timers
534 76 my $min_time = $this->transport->_update_timers($delta);
535
536 # Update user defined timers
537 76 foreach my $key ( keys(%{$this->{_user_timers}}) ) {
538 20 my $timer = $this->{_user_timers}->{$key};
539 20 ▶ if ( defined($timer->{_timer}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timer->{"_timer"}) 20 0 20
540 20 $timer->{_timer} -= $delta;
541  
542 20 ▶ if ( $timer->{_timer} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer->{"_timer"} <= 0) 4 16 20
543 4 $timer->{_timer} = $timer->{_timeout};
544 4 &{ $timer->{_callback} }($this);
545 }
546  
547 20 ▶ my $min = ($timer->{_timer} < 0) ? 0 : $timer->{_timer};
Branch: 1 branch
BranchTrueFalseTotal
$timer->{'_timer'} < 0 ? : 0 20 20
548 20 ▶ if ( $min < $min_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $min_time) 20 0 20
549 20 $min_time = $min;
550 }
551 }
552 }
553  
554 76 return ( $min_time );
555 }
556  
557 ## POD ##
558  
559 =pod
560  
561 =head1 NAME
562  
563 C<Net::BGP::Peer> - Class encapsulating BGP-4 peering session state and functionality
564  
565 =head1 SYNOPSIS
566  
567 use Net::BGP::Peer;
568  
569 $peer = Net::BGP::Peer->new(
570 Start => 1,
571 ThisID => '10.0.0.1',
572 ThisAS => 64512,
573 PeerID => '10.0.0.2',
574 PeerAS => 64513,
575 PeerPort => 1179,
576 ConnectRetryTime => 300,
577 HoldTime => 60,
578 KeepAliveTime => 20,
579 Listen => 0,
580 Passive => 0,
581 AnnounceRefresh => 1,
582 SupportCapabilities => 1,
583 SupportMBGP => 1,
584 SupportAS4 => 1,
585 OpaqueData => $my_ref,
586 OpenCallback => \&my_open_callback,
587 KeepaliveCallback => \&my_keepalive_callback,
588 UpdateCallback => \&my_update_callback,
589 NotificationCallback => \&my_notification_callback,
590 ErrorCallback => \&my_error_callback
591 );
592  
593 $peer = renew Net::BGP::Peer("$peer");
594  
595 $peer->start();
596 $peer->stop();
597  
598 $peer->update($update);
599 $peer->refresh($refresh);
600  
601 $version = $peer->version();
602  
603 $this_id = $peer->this_id();
604 $this_as = $peer->this_as();
605 $peer_id = $peer->peer_id();
606 $peer_as = $peer->peer_as();
607 $my_ref = $peer->opaque_data();
608 $my_ref = $peer->opaque_data($new_ref);
609  
610 $i_will = $peer->support_capabilities();
611  
612 $i_mbgp = $peer->support_mbgp();
613  
614 $i_can = $peer->this_can_refresh();
615 $peer_can= $peer->peer_can_refresh();
616  
617 $peer_as4= $peer->peer_can_as4();
618  
619 $listen = $peer->is_listener();
620 $passive = $peer->is_passive();
621 $estab = $peer->is_established();
622  
623 $trans = $peer->transport($trans);
624 @trans = $peer->transports;
625  
626 $string = $peer->asstring();
627  
628 $peer->set_open_callback(\&my_open_callback);
629 $peer->set_established_callback(\&my_established_callback);
630 $peer->set_keepalive_callback(\&my_keepalive_callback);
631 $peer->set_update_callback(\&my_update_callback);
632 $peer->set_notification_callback(\&my_notification_callback);
633 $peer->set_error_callback(\&my_error_callback);
634  
635 $peer->add_timer(\&my_minute_timer, 60);
636 $peer->remove_timer(\&my_minute_timer);
637  
638 =head1 DESCRIPTION
639  
640 This module encapsulates the state and functionality associated with a BGP
641 peering session. Each instance of a C<Net::BGP::Peer> object corresponds
642 to a peering session with a distinct peer and presents a programming
643 interface to manipulate the peering session state and exchange of routing
644 information. Through the methods provided by the C<Net::BGP::Peer> module,
645 a program can start or stop peering sessions, send BGP routing UPDATE
646 messages, and register callback functions which are invoked whenever the
647 peer receives BGP messages from its peer.
648  
649 =head1 CONSTRUCTOR
650  
651 I<new()> - create a new C<Net::BGP::Peer> object
652  
653 This is the constructor for C<Net::BGP::Peer> objects. It returns a
654 reference to the newly created object. The following named parameters may
655 be passed to the constructor. Once the object is created, only the
656 callback function references can later be changed.
657  
658 =head2 Start
659  
660 Setting this parameter to a true value causes the peer to initiate a
661 session with its peer immediately after it is registered with the
662 L<Net::BGP::Process> object's I<add_peer()> method. If omitted or
663 set to a false value, the peer will remain in the Idle state until
664 the I<start()> method is called explicitly by the program. When in
665 the Idle state the peer will refuse connections and will not initiate
666 connection attempts.
667  
668 =head2 ThisID
669  
670 This parameter sets the BGP ID (IP address) of the C<Net::BGP::Peer>
671 object. It takes a string in IP dotted decimal notation.
672  
673 =head2 ThisAS
674  
675 This parameter sets the BGP Autonomous System number of the C<Net::BGP::Peer>
676 object. It takes an integer value in the range of a 16-bit unsigned integer.
677  
678 =head2 PeerID
679  
680 This parameter sets the BGP ID (IP address) of the object's peer. It takes
681 a string in IP dotted decimal notation.
682  
683 =head2 PeerAS
684  
685 This parameter sets the BGP Autonomous System number of the object's peer.
686 It takes an integer value in the range of a 16-bit unsigned integer.
687  
688 =head2 PeerPort
689  
690 This parameter sets the TCP port number on the peer to which to connect. It
691 must be in the range of a valid TCP port number.
692  
693 =head2 ConnectRetryTime
694  
695 This parameter sets the BGP ConnectRetry timer duration, the value of which
696 is given in seconds.
697  
698 =head2 HoldTime
699  
700 This parameter sets the BGP Hold Time duration, the value of which
701 is given in seconds.
702  
703 =head2 KeepAliveTime
704  
705 This parameter sets the BGP KeepAlive timer duration, the value of which
706 is given in seconds.
707  
708 =head2 Listen
709  
710 This parameter specifies whether the C<Net::BGP::Peer> will listen for
711 and accept connections from its peer. If set to a false value, the peer
712 will only initiate connections and will not accept connection attempts
713 from the peer (unless the B<Passive> parameter is set to a true value).
714 Note that this behavior is not specified by RFC 1771 and should be
715 considered non-standard. However, it is useful under certain circumstances
716 and should not present problems as long as one side of the connection is
717 configured to listen.
718  
719 =head2 Passive
720  
721 This parameter specifies whether the C<Net::BGP::Peer> will attempt to
722 initiate connections to its peer. If set to a true value, the peer will
723 only listen for connections and will not initate connections to its peer
724 (unless the B<Listen> parameter is set to false value). Note that this
725 behavior is not specified by RFC 1771 and should be considered non-standard.
726 However, it is useful under certain circumstances and should not present
727 problems as long as one side of the connection is configured to initiate
728 connections.
729  
730 =head2 AnnounceRefresh
731  
732 This parameter specifies whether the C<Net::BGP::Peer> will announce support
733 for route refresh ('soft re-configure' as specified by RFC 2918). No support
734 for route refresh is implemented - only the B<RefreshCallback> function. This
735 has no effect if SupportCapabilities is FALSE.
736  
737 =head2 SupportCapabilities
738  
739 This parameter specifies whether the C<Net::BGP::Peer> will attempt to
740 negotiate capabilities. You can set this to FALSE if talking to an old BGP
741 speaker that doesn't support it (you'll get a notification message for an
742 unsupported capability if this is the case). This defaults to TRUE.
743  
744 =head2 SupportMBGP
745  
746 This parameter specifies whether the C<Net::BGP::Peer> will attempt to
747 negotiate MBGP. Quagga (and probably others) need this if you want to send
748 the REFRESH capability. Today this just indicates support for IPv4 unicast.
749 This defaults to TRUE. This has no effect if SupportCapabilities is FALSE.
750  
751 =head2 SupportAS4
752  
753 This parameter specifies whether outgoing connections from the C<Net::BGP::Peer>
754 will attempt to negotiate AS4 (32 bit ASNs). For received connections, this
755 parameter has no effect - it only determines whether or not AS4 is negotiated
756 during outgoing connection. For received connections, this will be changed
757 to TRUE (on the listening connection) whenever the appropriate OPEN capability
758 is received. Note that the B<SupportCapabilities> must be true for this to
759 be sent. This defaults to FALSE.
760  
761 =head2 OpaqueData
762  
763 This parameter is an optional scalar that will be kept as part of the
764 C<Net::BGP::Peer> and can be queried by the callback routines when they
765 receive a peer hashref - see B<opaque_data>. This allows extra data to be
766 stored with the peer. The contents of this are completely ignored by
767 C<Net::BGP::Peer>. This defaults to I<undef>.
768  
769 =head2 OpenCallback
770  
771 This parameter sets the callback function which is invoked when the
772 peer receives an OPEN message. It takes a subroutine reference. See
773 L<"CALLBACK FUNCTIONS"> later in this manual for further details of
774 the conventions of callback invocation.
775  
776 =head2 KeepaliveCallback
777  
778 This parameter sets the callback function which is invoked when the
779 peer receives a KEEPALIVE message. It takes a subroutine reference.
780 See L<"CALLBACK FUNCTIONS"> later in this manual for further details
781 of the conventions of callback invocation.
782  
783 =head2 UpdateCallback
784  
785 This parameter sets the callback function which is invoked when the
786 peer receives an UPDATE message. It takes a subroutine reference. See
787 L<"CALLBACK FUNCTIONS"> later in this manual for further details of
788 the conventions of callback invocation.
789  
790 =head2 RefreshCallback
791  
792 This parameter sets the callback function which is invoked when the
793 peer receives a REFRESH message. It takes a subroutine reference. See
794 L<"CALLBACK FUNCTIONS"> later in this manual for further details of
795 the conventions of callback invocation.
796  
797 =head2 NotificationCallback
798  
799 This parameter sets the callback function which is invoked when the
800 peer receives a NOTIFICATION message. It takes a subroutine reference.
801 See L<"CALLBACK FUNCTIONS"> later in this manual for further details
802 of the conventions of callback invocation.
803  
804 =head2 ErrorCallback
805  
806 This parameter sets the callback function which is invoked when the
807 peer encounters an error and must send a NOTIFICATION message to its
808 peer. It takes a subroutine reference. See L<"CALLBACK FUNCTIONS">
809 later in this manual for further details of the conventions of callback
810 invocation.
811  
812 I<renew> - fetch the existing C<Net::BGP::Peer> object from the "object string".
813  
814 This "reconstructor" returns a previously constructed object from the
815 perl generated string-context scalar of the object, e.g.
816 I<Net::BGP::Peer=HASH(0x820952c)>.
817  
818 =head1 ACCESSOR METHODS
819  
820 I<start()> - start the BGP peering session with the peer
821  
822 $peer->start();
823  
824 This method initiates the BGP peering session with the peer by
825 internally emitting the BGP Start event, which causes the peer
826 to initiate a transport-layer connection to its peer (unless
827 the B<Passive> parameter was set to a true value in the
828 constructor) and listen for a connection from the peer (unless
829 the B<Listen> parameter is set to a false value).
830  
831 I<stop()> - cease the BGP peering session with the peer
832  
833 $peer->stop();
834  
835 This method immediately ceases the peering session with the
836 peer by sending it a NOTIFICATION message with Error Code
837 Cease, closing the transport-layer connection, and entering
838 the Idle state.
839  
840 I<update()> - send a BGP UPDATE message to the peer
841  
842 $peer->update($update);
843  
844 This method sends the peer an UPDATE message. It takes a reference
845 to a L<Net::BGP::Update> object. See the L<Net::BGP::Update>
846 manual page for details on setting UPDATE attributes.
847  
848 I<refresh()> - send a BGP REFRESH message to the peer
849  
850 $peer->refresh($refresh);
851  
852 This method sends the peer a REFRESH message. It takes a reference
853 to a L<Net::BGP::Refresh> object. If no argument is provided, a default
854 L<Net::BGP::Refresh> object is constructed. See the L<Net::BGP::Refresh>
855 manual page for details on setting REFRESH attributes.
856  
857 I<this_id()>
858  
859 I<this_as()>
860  
861 I<peer_id()>
862  
863 I<peer_as()>
864  
865 I<this_can_refresh()>
866  
867 I<support_capabilities()>
868  
869 I<support_mbgp()>
870  
871 I<is_listener()>
872  
873 I<is_passive()>
874  
875 I<version()>
876  
877 These are accessor methods for the corresponding constructor named parameters.
878 They retrieve the values set when the object was created, but the values cannot
879 be changed after object construction. Hence, they take no arguments.
880  
881 I<opaque_data()>
882  
883 $peer->opaque_data();
884 $peer->opaque_data($new_ref);
885  
886 This method can be used to both query (no argument) or set (with an argument)
887 the opaque data held with the peer object. The method returns the old opaque data
888 scalar (which is the current value if not provided).
889  
890 I<is_established()>
891  
892 This accessor method returns true if the peer has a established transport
893 connection - e.g. the peering is up.
894  
895 I<peer_can_refresh()>
896  
897 This accessor method returns a true value if connected to a peer that supports
898 refresh messages - otherwise a false value.
899  
900 I<asstring()>
901  
902 This accessor method returns a print friendly string with the local and remote
903 IP and AS numbers.
904  
905 I<set_open_callback()>
906  
907 I<set_established_callback()>
908  
909 I<set_keepalive_callback()>
910  
911 I<set_update_callback()>
912  
913 I<set_refresh_callback()>
914  
915 I<set_reset_callback()>
916  
917 I<set_notification_callback()>
918  
919 I<set_error_callback()>
920  
921 These methods set the callback functions which are invoked whenever the
922 peer receives the corresponding BGP message type from its peer, or, in the
923 case of I<set_established_callback>, transitions to the relevant state. They
924 can be set in the constructor as well as with these methods. These methods
925 each take one argument, which is the subroutine reference to be invoked.
926 A callback function can be removed by calling the corresponding one of these
927 methods and passing it the perl I<undef> value. For callback definition and
928 invocation conventions see L<"CALLBACK FUNCTIONS"> later in this manual.
929  
930 I<add_timer()> - add a program defined timer callback function
931  
932 $peer->add_timer(\&my_minute_timer, 60);
933  
934 This method sets a program defined timer which invokes the specified callback
935 function when the timer expires. It takes two arguments: the first is a code
936 reference to the subroutine to be invoked when the timer expires, and the
937 second is the timer interval, in seconds. The program may set as many timers
938 as needed, and multiple timer callbacks may share the same interval. Program
939 timers add an asynchronous means for user code to gain control of the program
940 control flow - without them user code would only be invoked whenever BGP
941 events exposed by the module occur. They may be used to perform any necessary
942 action - for example, sending UPDATEs, starting or stopping the peering
943 session, house-keeping, etc.
944  
945 I<remove_timer()> - remove a program defined timer callback function
946  
947 $peer->remove_timer(\&my_minute_timer);
948  
949 This method removes a program defined timer callback which has been previously
950 set with the I<add_timer()> method. It takes a single argument: a reference
951 to the subroutine previously added.
952  
953 =head1 CALLBACK FUNCTIONS
954  
955 Whenever a C<Net::BGP::Peer> object receives one of the BGP protocol messages -
956 OPEN, KEEPALIVE, UPDATE, REFRESH, or NOTIFICATION - from its peer, or whenever it
957 encounters an error condition and must send a NOTIFICATION message to its peer,
958 the peer object will invoke a program defined callback function corresponding
959 to the event type, if one has been provided, to inform the application about
960 the event. These callback functions are installed as described in the preceding
961 section of the manual. Whenever any callback function is invoked, it is passed
962 one or more arguments, depending on the BGP message type associated with the
963 callback. The first argument passed to all of the callbacks is a reference
964 to the C<Net::BGP::Peer> object which the application may use to identify
965 which peer has signalled the event and to take appropriate action. For OPEN
966 and KEEPALIVE callbacks, this is the only argument passed. It is very unlikely
967 that applications will be interested in OPEN and KEEPALIVE events, since the
968 C<Net::BGP> module handles all details of OPEN and KEEPALIVE message processing
969 in order to establish and maintain BGP sessions. Callback handling for these
970 messages is mainly included for the sake of completeness. For UPDATE and
971 NOTIFICATION messages, however, most applications will install callback handlers.
972 Whenever an UPDATE, REFRESH, NOTIFICATION, or error handler is called, the object
973 will pass a second argument. In the first two cases, this is a L<Net::BGP::Update>
974 or L<Net::BGP::Refresh> object respectivly encapsulating the information contained
975 in the UPDATE or REFRESH message, while in the latter two cases it is a
976 L<Net::BGP::Notification> object encapsulating the information in the
977 NOTIFICATION message sent or received.
978  
979 The RESET and ESTABLISHED callbacks are special, since they are used whenever an
980 established BGP session is reset, even though no message has been recieved or sent.
981 The REFRESH callback is also special, since it is also called without a REFRESH
982 object whenever a BGP session is established. The two callbacks can be used to
983 clear and retransmit a RIB from/to the peer in question.
984  
985 Whenever a callback function is to be invoked, the action occuring internally is
986 the invocation of one of the following methods, corresponding to the event which
987 has occured:
988  
989 I<open_callback()>
990  
991 I<established_callback()>
992  
993 I<keepalive_callback()>
994  
995 I<update_callback()>
996  
997 I<refresh_callback()>
998  
999 I<reset_callback()>
1000  
1001 I<notification_callback()>
1002  
1003 I<error_callback()>
1004  
1005 Internally, each of these methods just checks to see whether a program defined
1006 callback function has been set and calls it if so, passing it arguments as
1007 described above. As an alternative to providing subroutine references to the
1008 constructor or through the I<set_open_callback()>, I<set_established_callback()>,
1009 I<set_keepalive_callback()>, I<set_update_callback()>, I<set_refresh_callback()>,
1010 I<set_reset_callback()>, I<set_notification_callback()>, and I<set_error_callback()>
1011 methods, an application may effect a similar result by sub-classing the
1012 C<Net::BGP::Peer> module and overridding the defintions of the above methods
1013 to perform whatever actions would have been executed by ordinary callback functions.
1014 The overridden methods are passed the same arguments as the callback functions.
1015 This method might offer an advantage in organizing code according to different
1016 derived classes which apply specifc routing policies.
1017  
1018 =head1 ERROR HANDLING
1019  
1020 There are two possibilities for error handling callbacks to be invoked. The first
1021 case occurs when the peer receives a NOTIFICATION messages from its peer. The
1022 second case occurs when the peer detects an error condition while processing an
1023 incoming BGP message or when some other protocol covenant is violated - for
1024 example if a KEEPALIVE or UPDATE message is not received before the peer's
1025 Keepalive timer expires. In this case, the peer responds by sending a NOTIFICATION
1026 message to its peer. In the former case the I<notification_callback()> method
1027 is invoked as described above to handle the error, while in the latter the
1028 I<error_callback()> method is invoked to inform the application that it has
1029 encountered an error. Both methods are passed a L<Net::BGP::Notification>
1030 object encapsulating the details of the error. In both cases, the transport-layer
1031 connection and BGP session are closed and the peer transitions to the Idle state.
1032 The error handler callbacks can examine the cause of the error and take appropriate
1033 action. This could be to attempt to re-establish the session (perhaps after
1034 sleeping for some amount of time), or to unregister the peer object from the
1035 L<Net::BGP::Process> object and permanently end the session (for the duration
1036 of the application's running time), or to log the event to a file on the host
1037 system, or some combination of these or none.
1038  
1039 =head1 SEE ALSO
1040  
1041 =over
1042  
1043 =item L<Net::BGP>
1044  
1045 =item L<Net::BGP::Process>
1046  
1047 =item L<Net::BGP::Update>
1048  
1049 =item L<Net::BGP::Refresh>
1050  
1051 =item L<Net::BGP::ASPath>
1052  
1053 =item L<Net::BGP::NLRI>
1054  
1055 =item L<Net::BGP::Notification>
1056  
1057 =back
1058  
1059 =head1 AUTHOR
1060  
1061 Stephen J. Scheck <sscheck@cpan.org>
1062  
1063 =cut
1064  
1065 ## End Package Net::BGP::Peer ##
1066  
1067 4 1;
↑ Summary BGP/Update.pm »