BGP/Peer.pm

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