File Coverage

blib/lib/Net/BGP/Peer.pm
Criterion Covered Total %
statement 172 222 77.4
branch 46 80 57.5
condition 7 12 58.3
subroutine 54 62 87.1
pod 0 49 0.0
total 279 425 65.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Net::BGP::Peer;
4 4     4   67376 use bytes;
  4         97  
  4         27  
5              
6 4     4   126 use strict;
  4         7  
  4         113  
7 4         877 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     4   20 );
  4         20  
11              
12             ## Inheritance and Versioning ##
13              
14             @ISA = qw( Exporter );
15             $VERSION = '0.17';
16              
17             ## General Definitions ##
18              
19 17     17 0 44 sub TRUE { 1 }
20 17     17 0 81 sub FALSE { 0 }
21              
22             ## BGP Network Constants ##
23              
24 8     8 0 100 sub BGP_PORT { 179 }
25 5     5 0 16 sub BGP_VERSION_4 { 4 }
26              
27             ## Export Tag Definitions ##
28              
29             @BGP = qw( BGP_PORT BGP_VERSION_4 );
30             @GENERIC = qw( TRUE FALSE dump_hex );
31             @EXPORT = ();
32             @EXPORT_OK = ( @BGP, @GENERIC );
33             %EXPORT_TAGS = (
34             bgp => [ @BGP ],
35             generic => [ @GENERIC ],
36             ALL => [ @EXPORT, @EXPORT_OK ]
37             );
38              
39             ## Module Imports ##
40              
41 4     4   38 use Scalar::Util qw( weaken );
  4         7  
  4         220  
42 4     4   23 use Exporter;
  4         40  
  4         138  
43 4     4   497 use IO::Socket;
  4         20875  
  4         23  
44 4     4   1945 use Carp;
  4         8  
  4         289  
45 4     4   32 use Carp qw(cluck);
  4         8  
  4         200  
46 4     4   858 use Net::BGP::Notification qw( :errors );
  4         8  
  4         827  
47 4     4   839 use Net::BGP::Refresh;
  4         6  
  4         171  
48 4     4   992 use Net::BGP::Update;
  4         13  
  4         170  
49 4     4   2536 use Net::BGP::Transport;
  4         14  
  4         10795  
50              
51             ## Global private variables ##
52              
53             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     0 0 0 my $data = shift();
64 0         0 my ($length, $offset, $line, $hex) = (0, 0, '', '');
65 0         0 my $string;
66              
67 0         0 $string = '';
68 0         0 $length = length($data);
69              
70 0         0 while ($length > 0) {
71 0 0       0 if ($length >= 16) {
72 0         0 $line = substr($data, $offset, 16);
73             } else {
74 0         0 $line = substr($data, $offset, $length);
75             }
76 0         0 $hex = unpack('H*', $line);
77 0         0 $hex .= ' ' x (32 - length($hex));
78 0         0 $hex = sprintf("%s %s %s %s " x 4, unpack('a2' x 16, $hex));
79 0         0 $line =~ s/[\x00-\x1f\x7f-\xff]/./g;
80 0         0 $string .= sprintf("[%03d] %s %s\n", $offset, uc($hex), $line);
81 0         0 $offset += 16;
82 0         0 $length -= 16;
83             }
84              
85 0         0 return ( $string );
86             }
87              
88             ## Public Class Methods ##
89              
90             sub new
91             {
92 5     5 0 2133 my $class = shift();
93 5         12 my ($arg, $value);
94              
95 5         15 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         13 bless($this, $class);
122              
123 5         11 my %transarg = ();
124 5         18 while ( defined($arg = shift()) ) {
125 54         76 $value = shift();
126              
127 54 100 66     456 if (( $arg =~ /^start$/i )
    100 100        
    100 66        
    100          
    100          
    100          
    100          
    100          
    50          
    50          
    50          
    100          
    50          
    100          
    50          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
128             || ( $arg =~ /^connectretrytime$/i )
129             || ( $arg =~ /^keepalivetime$/i )
130             || ( $arg =~ /^holdtime$/i )) {
131 9         30 $transarg{$arg} = $value;
132             }
133             elsif ( $arg =~ /^thisid$/i ) {
134 5         19 $this->{_local_id} = $value;
135             }
136             elsif ( $arg =~ /^thisas$/i ) {
137 5         14 $this->{_local_as} = $value;
138             }
139             elsif ( $arg =~ /^peerid$/i ) {
140 5         14 $this->{_peer_id} = $value;
141             }
142             elsif ( $arg =~ /^peeras$/i ) {
143 5         13 $this->{_peer_as} = $value;
144             }
145             elsif ( $arg =~ /^peerport$/i ) {
146 4         10 $this->{_peer_port} = $value;
147             }
148             elsif ( $arg =~ /^listen$/i ) {
149 5         22 $this->{_listen} = $value;
150             }
151             elsif ( $arg =~ /^passive$/i ) {
152 5         13 $this->{_passive} = $value;
153             }
154             elsif ( $arg =~ /^refreshcallback$/i ) {
155 0         0 $this->{_refresh_callback} = $value;
156             }
157             elsif ( $arg =~ /^opaquedata$/i ) {
158 0         0 $this->{_opaque_data} = $value;
159             }
160             elsif ( $arg =~ /^refresh$/i ) {
161 0         0 warnings::warnif(
162             'deprecated',
163             'Refresh in is deprecated, use AnnounceRefresh instead'
164             );
165 0         0 $this->{_announce_refresh} = $value;
166             }
167             elsif ( $arg =~ /^announcerefresh$/i ) {
168 4         10 $this->{_announce_refresh} = $value;
169             }
170             elsif ( $arg =~ /^supportcapabilities$/i ) {
171 0         0 $this->{_support_capabilities} = $value;
172             }
173             elsif ( $arg =~ /^supportmbgp$/i ) {
174 4         10 $this->{_support_mbgp} = $value;
175             }
176             elsif ( $arg =~ /^supportas4$/i ) {
177 3         8 $this->{_support_as4} = $value;
178             }
179             elsif ( $arg =~ /^opencallback$/i ) {
180 0         0 $this->{_open_callback} = $value;
181             }
182             elsif ( $arg =~ /^establishedcallback$/i ) {
183 0         0 $this->{_established_callback} = $value;
184             }
185             elsif ( $arg =~ /^keepalivecallback$/i ) {
186 0         0 $this->{_keepalive_callback} = $value;
187             }
188             elsif ( $arg =~ /^updatecallback$/i ) {
189 0         0 $this->{_update_callback} = $value;
190             }
191             elsif ( $arg =~ /^resetcallback$/i ) {
192 0         0 $this->{_reset_callback} = $value;
193             }
194             elsif ( $arg =~ /^notificationcallback$/i ) {
195 0         0 $this->{_notification_callback} = $value;
196             }
197             elsif ( $arg =~ /^errorcallback$/i ) {
198 0         0 $this->{_error_callback} = $value;
199             }
200             else {
201 0         0 croak "unrecognized argument $arg\n";
202             }
203             }
204              
205 5         11 $transarg{parent} = $this;
206 5         34 $this->{_transport} = Net::BGP::Transport->new(%transarg);
207              
208 5         39 weaken($knownpeers{$this} = $this);
209 5         19 return ( $this );
210             }
211              
212             sub renew
213             {
214 0     0 0 0 my $proto = shift;
215 0   0     0 my $class = ref $proto || $proto;
216 0         0 my $name = shift;
217 0         0 return $knownpeers{$name};
218             }
219              
220             ## Public Object Methods - Proxies for backward compatibility ##
221              
222             sub start
223             {
224 0     0 0 0 foreach my $trans (shift->transports)
225             {
226 0         0 $trans->start();
227             };
228             }
229              
230             sub stop
231             {
232 6     6 0 41 foreach my $trans (shift->transports)
233             {
234 6         21 $trans->stop();
235             };
236             }
237              
238             sub version
239             {
240 0     0 0 0 return shift->transport->version;
241             }
242              
243             sub update
244             {
245 4     4 0 56 return shift->transport->update(@_);
246             }
247              
248             sub refresh
249             {
250 2     2 0 18 return shift->transport->refresh(@_);
251             }
252              
253             ## Public Object Methods ##
254              
255             sub transport
256             {
257 413     413 0 706 my $this = shift();
258 413 50       730 $this->{_transport} = shift if @_;
259 413         1087 return $this->{_transport};
260             }
261              
262             sub transports
263             {
264 162     162 0 251 my $this = shift();
265 162 50       289 return ($this->transport) unless defined $this->transport->sibling;
266 0         0 return ($this->transport,$this->transport->sibling);
267             }
268              
269             sub this_id
270             {
271 14     14 0 25 my $this = shift();
272 14         76 return ( $this->{_local_id} );
273             }
274              
275             sub this_as
276             {
277 12     12 0 18 my $this = shift();
278 12         35 return ( $this->{_local_as} );
279             }
280              
281             sub this_can_refresh
282             {
283 8     8 0 14 my $this = shift();
284 8         50 return ( $this->{_announce_refresh} );
285             }
286              
287             sub this_can_as4
288             {
289 8     8 0 16 my $this = shift();
290 8         22 return ( $this->{_support_as4} );
291             }
292              
293             sub support_capabilities
294             {
295 6     6 0 10 my $this = shift();
296 6         16 return ( $this->{_support_capabilities} );
297             }
298              
299             sub support_mbgp
300             {
301 6     6 0 18 my $this = shift();
302 6         15 return ( $this->{_support_mbgp} );
303             }
304              
305             sub peer_id
306             {
307 8     8 0 14 my $this = shift();
308 8         55 return ( $this->{_peer_id} );
309             }
310              
311             sub peer_as
312             {
313 11     11 0 17 my $this = shift();
314 11         142 return ( $this->{_peer_as} );
315             }
316              
317             sub peer_port
318             {
319 2     2 0 5 my $this = shift();
320 2         14 return ( $this->{_peer_port} );
321             }
322              
323             sub peer_can_as4
324             {
325 1     1 0 6 my $this = shift();
326 1         2 return $this->transport->can_as4;
327             }
328              
329             sub peer_can_mbgp
330             {
331 2     2 0 15 my $this = shift();
332 2         7 return $this->transport->can_mbgp;
333             }
334              
335             sub peer_can_refresh
336             {
337 0     0 0 0 my $this = shift();
338 0         0 return $this->transport->can_refresh;
339             }
340              
341             sub is_listener
342             {
343 9     9 0 17 my $this = shift();
344 9         45 return ( $this->{_listen} );
345             }
346              
347             sub is_passive
348             {
349 10     10 0 16 my $this = shift();
350 10         40 return ( $this->{_passive} );
351             }
352              
353             sub is_established
354             {
355 0     0 0 0 my $this = shift();
356 0         0 return ( $this->transport->is_established );
357             }
358              
359             sub opaque_data
360             {
361 0     0 0 0 my ($this, $opaque_data) = @_;
362 0         0 my $old_value = $this->{_opaque_data};
363 0 0       0 $this->{_opaque_data} = $opaque_data if defined($opaque_data);
364 0         0 return ( $old_value );
365             }
366              
367             sub set_open_callback
368             {
369 2     2 0 9 my ($this, $callback) = @_;
370 2         5 $this->{_open_callback} = $callback;
371             }
372              
373             sub set_established_callback
374             {
375 2     2 0 10 my ($this, $callback) = @_;
376 2         5 $this->{_established_callback} = $callback;
377             }
378              
379             sub set_keepalive_callback
380             {
381 4     4 0 28 my ($this, $callback) = @_;
382 4         10 $this->{_keepalive_callback} = $callback;
383             }
384              
385             sub set_update_callback
386             {
387 2     2 0 8 my ($this, $callback) = @_;
388 2         15 $this->{_update_callback} = $callback;
389             }
390              
391             sub set_refresh_callback
392             {
393 2     2 0 7 my ($this, $callback) = @_;
394 2         3 $this->{_refresh_callback} = $callback;
395             }
396              
397             sub set_reset_callback
398             {
399 4     4 0 15 my ($this, $callback) = @_;
400 4         7 $this->{_reset_callback} = $callback;
401             }
402              
403             sub set_notification_callback
404             {
405 4     4 0 16 my ($this, $callback) = @_;
406 4         7 $this->{_notification_callback} = $callback;
407             }
408              
409             sub set_error_callback
410             {
411 6     6 0 34 my ($this, $callback) = @_;
412 6         12 $this->{_error_callback} = $callback;
413             }
414              
415             sub add_timer
416             {
417 2     2 0 11 my ($this, $callback, $timeout) = @_;
418 2         4 my $timer;
419              
420 2         8 $timer = {
421             _timer => $timeout,
422             _timeout => $timeout,
423             _callback => $callback,
424             };
425              
426 2         8 $this->{_user_timers}->{$timer} = $timer;
427             }
428              
429             sub remove_timer
430             {
431 2     2 0 36 my ($this, $callback) = @_;
432 2         6 my ($key, $timer);
433              
434 2         5 foreach $key ( keys(%{$this->{_user_timers}}) ) {
  2         9  
435 2         31 $timer = $this->{_user_timers}->{$key};
436 2 50       15 if ( $timer->{_callback} == $callback ) {
437 2         13 delete $this->{_user_timers}->{$key};
438             }
439             }
440             }
441              
442             sub asstring
443             {
444 0     0 0 0 my $this = shift;
445              
446 0 0       0 my @l = map { defined $_ ? $_ : 'n/a'; } (
  0         0  
447             $this->this_id, $this->this_as,
448             $this->peer_id, $this->peer_as);
449              
450 0         0 return $l[0] . ':' . $l[1] . '<=>' . $l[2] . ':' . $l[3];
451             }
452              
453             ## Overridable Methods ##
454              
455             sub open_callback
456             {
457 4     4 0 9 my $this = shift();
458              
459 4 100       14 if ( defined($this->{_open_callback}) ) {
460 2         4 &{ $this->{_open_callback} }($this);
  2         8  
461             }
462             }
463              
464             sub established_callback
465             {
466 4     4 0 7 my $this = shift();
467              
468 4 100       14 if ( defined($this->{_established_callback}) ) {
469 2         5 &{ $this->{_established_callback} }($this);
  2         8  
470             }
471             }
472              
473             sub keepalive_callback
474             {
475 8     8 0 15 my $this = shift();
476              
477 8 100       28 if ( defined($this->{_keepalive_callback}) ) {
478 4         8 &{ $this->{_keepalive_callback} }($this);
  4         23  
479             }
480             }
481              
482             sub update_callback
483             {
484 4     4 0 12 my ($this, $update) = @_;
485              
486 4 50       14 if ( defined($this->{_update_callback}) ) {
487 4         8 &{ $this->{_update_callback} }($this, $update);
  4         16  
488             }
489             }
490              
491             sub refresh_callback
492             {
493 6     6 0 16 my ($this,$refresh) = @_;
494              
495 6 100       18 if ( defined($this->{_refresh_callback}) ) {
496 4         21 &{ $this->{_refresh_callback} }($this,$refresh);
  4         21  
497             }
498             }
499              
500             sub reset_callback
501             {
502 8     8 0 18 my ($this,$refresh) = @_;
503              
504 8 50       22 if ( defined($this->{_reset_callback}) ) {
505 8         13 &{ $this->{_reset_callback} }($this);
  8         22  
506             }
507             }
508              
509             sub notification_callback
510             {
511 2     2 0 7 my ($this, $error) = @_;
512              
513 2 50       19 if ( defined($this->{_notification_callback}) ) {
514 2         6 &{ $this->{_notification_callback} }($this, $error);
  2         9  
515             }
516             }
517              
518             sub error_callback
519             {
520 6     6 0 16 my ($this, $error) = @_;
521              
522 6 100       20 if ( defined($this->{_error_callback}) ) {
523 4         9 &{ $this->{_error_callback} }($this, $error);
  4         14  
524             }
525             }
526              
527             ## Private Object Methods ##
528              
529             sub _update_timers
530             {
531 76     76   147 my ($this, $delta) = @_;
532              
533             # Proxy for transport timers
534 76         160 my $min_time = $this->transport->_update_timers($delta);
535            
536             # Update user defined timers
537 76         121 foreach my $key ( keys(%{$this->{_user_timers}}) ) {
  76         193  
538 20         38 my $timer = $this->{_user_timers}->{$key};
539 20 50       55 if ( defined($timer->{_timer}) ) {
540 20         35 $timer->{_timer} -= $delta;
541              
542 20 100       51 if ( $timer->{_timer} <= 0 ) {
543 4         11 $timer->{_timer} = $timer->{_timeout};
544 4         10 &{ $timer->{_callback} }($this);
  4         26  
545             }
546              
547 20 50       71 my $min = ($timer->{_timer} < 0) ? 0 : $timer->{_timer};
548 20 50       56 if ( $min < $min_time ) {
549 20         47 $min_time = $min;
550             }
551             }
552             }
553              
554 76         165 return ( $min_time );
555             }
556              
557             ## POD ##
558              
559             =pod
560              
561             =head1 NAME
562              
563             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 B 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 B 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 - create a new Net::BGP::Peer object
652              
653             This is the constructor for 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             B object's I method. If omitted or
663             set to a false value, the peer will remain in the Idle state until
664             the I 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 B
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 B
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 B 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 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 B 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 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 Refresh
731              
732             This parameter specifies whether the B will annonce support
733             for route refresh ('soft re-configure' as specified by RFC 2918). No support
734             for route refresh is implemented - only the B function. This
735             has no effect if SupportCapabilities is FALSE.
736              
737             =head2 SupportCapabilities
738              
739             This parameter specifies whether the B 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 B 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 paramemter specifies whether outgoing connections from B
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 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             B and can be queried by the callback routines when they
765             receive a peer hashref - see B. This allows extra data to be
766             stored with the peer. The contents of this are completely ignored by
767             B. This defaults to I.
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 - fetch the existing Net::BGP::Peer object from the "object string".
813              
814             This "reconstructor" returns a previeus constructed object from the
815             perl genereted string-context scalar of the object, eg.
816             I.
817              
818             =head1 ACCESSOR METHODS
819              
820             I - 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 parameter was set to a true value in the
828             constructor) and listen for a connection from the peer (unless
829             the B parameter is set to a false value).
830              
831             I - 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 - 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 Net::BGP::Update object. See the Net::BGP::Update
846             manual page for details on setting UPDATE attributes.
847              
848             I - 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 Net::BGP::Refesh object. If no argument is provided, a default
854             Net::BGP::Refresh object is constructed. See the Net::BGP::Refresh
855             manual page for details on setting REFRESH attributes.
856              
857             I
858              
859             Returns the active transport object to the peer - See Net::BGP::Transport.
860              
861             I
862              
863             Return a list of transport objects. The list will contain one or two elements.
864             The first will always be the primary transport object. If there are two
865             sessions (e.g. collision detection hasn't removed one of them), the sibling
866             will be returned as the second element of the list.
867              
868             I
869              
870             I
871              
872             I
873              
874             I
875              
876             I
877              
878             I
879              
880             I
881              
882             I
883              
884             I
885              
886             I
887              
888             These are accessor methods for the corresponding constructor named parameters.
889             They retrieve the values set when the object was created, but the values cannot
890             be changed after object construction. Hence, they take no arguments.
891              
892             I
893              
894             $peer->opaque_data();
895             $peer->opaque_data($new_ref);
896              
897             This method can be used to both query (no argument) or set (with an argument)
898             the opaque data held with the peer object. The method returns the old opaque data
899             scalar (which is the current value if not provided).
900              
901             I
902              
903             This accessor method returns true if the peer has a established transport
904             connection - e.g. the peering is up.
905              
906             I
907              
908             This accessor method returns a true value if connected to a peer that supports
909             refresh messages - otherwise a false value.
910              
911             I
912              
913             This accessor method returns a print friendly string with the local and remote
914             IP and AS numbers.
915              
916             I
917              
918             I
919              
920             I
921              
922             I
923              
924             I
925              
926             I
927              
928             I
929              
930             I
931              
932             These methods set the callback functions which are invoked whenever the
933             peer receives the corresponding BGP message type from its peer, or, in the
934             case of I, transitions to the relevant state. They
935             can be set in the constructor as well as with these methods. These methods
936             each take one argument, which is the subroutine reference to be invoked.
937             A callback function can be removed by calling the corresponding one of these
938             methods and passing it the perl I value. For callback definition and
939             invocation conventions see L<"CALLBACK FUNCTIONS"> later in this manual.
940              
941             I - add a program defined timer callback function
942              
943             $peer->add_timer(\&my_minute_timer, 60);
944              
945             This method sets a program defined timer which invokes the specified callback
946             function when the timer expires. It takes two arguments: the first is a code
947             reference to the subroutine to be invoked when the timer expires, and the
948             second is the timer interval, in seconds. The program may set as many timers
949             as needed, and multiple timer callbacks may share the same interval. Program
950             timers add an asynchronous means for user code to gain control of the program
951             control flow - without them user code would only be invoked whenever BGP
952             events exposed by the module occur. They may be used to perform any necessary
953             action - for example, sending UPDATEs, starting or stopping the peering
954             session, house-keeping, etc.
955              
956             I - remove a program defined timer callback function
957              
958             $peer->remove_timer(\&my_minute_timer);
959              
960             This method removes a program defined timer callback which has been previously
961             set with the I method. It takes a single argument: a reference
962             to the subroutine previously added.
963              
964             =head1 CALLBACK FUNCTIONS
965              
966             Whenever a B object receives one of the BGP protocol messages -
967             OPEN, KEEPALIVE, UPDATE, REFRESH, or NOTIFICATION - from its peer, or whenever it
968             encounters an error condition and must send a NOTIFICATION message to its peer,
969             the peer object will invoke a program defined callback function corresponding
970             to the event type, if one has been provided, to inform the application about
971             the event. These callback functions are installed as described in the preceding
972             section of the manual. Whenever any callback function is invoked, it is passed
973             one or more arguments, depending on the BGP message type associated with the
974             callback. The first argument passed to all of the callbacks is a reference
975             to the B object which the application may use to identify
976             which peer has signalled the event and to take appropriate action. For OPEN
977             and KEEPALIVE callbacks, this is the only argument passed. It is very unlikely
978             that applications will be interested in OPEN and KEEPALIVE events, since the
979             B module handles all details of OPEN and KEEPALIVE message processing
980             in order to establish and maintain BGP sessions. Callback handling for these
981             messages is mainly included for the sake of completeness. For UPDATE and
982             NOTIFICATION messages, however, most applications will install callback handlers.
983             Whenever an UPDATE, REFRESH, NOTIFICATION, or error handler is called, the object
984             will pass a second argument. In the first two cases, this is a B
985             or B object respectivly encapsulating the information contained
986             in the UPDATE or REFRESH message, while in the latter two cases it is a
987             B object encapsulating the information in the
988             NOTIFICATION message sent or received.
989              
990             The RESET and ESTABLISHED callbacks are special, since they are used whenever an
991             established BGP session is reset, even though no message has been recieved or sent.
992             The REFRESH callback is also special, since it is also called without a REFRESH
993             object whenever a BGP session is established. The two callbacks can be used to
994             clear and retransmit a RIB from/to the peer in question.
995              
996             Whenever a callback function is to be invoked, the action occuring internally is
997             the invocation of one of the following methods, corresponding to the event which
998             has occured:
999              
1000             I
1001              
1002             I
1003              
1004             I
1005              
1006             I
1007              
1008             I
1009              
1010             I
1011              
1012             I
1013              
1014             I
1015              
1016             Internally, each of these methods just checks to see whether a program defined
1017             callback function has been set and calls it if so, passing it arguments as
1018             described above. As an alternative to providing subroutine references to the
1019             constructor or through the I, I,
1020             I, I, I,
1021             I, I, and I
1022             methods, an application may effect a similar result by sub-classing the
1023             B module and overridding the defintions of the above methods
1024             to perform whatever actions would have been executed by ordinary callback functions.
1025             The overridden methods are passed the same arguments as the callback functions.
1026             This method might offer an advantage in organizing code according to different
1027             derived classes which apply specifc routing policies.
1028              
1029             =head1 ERROR HANDLING
1030              
1031             There are two possibilities for error handling callbacks to be invoked. The first
1032             case occurs when the peer receives a NOTIFICATION messages from its peer. The
1033             second case occurs when the peer detects an error condition while processing an
1034             incoming BGP message or when some other protocol covenant is violated - for
1035             example if a KEEPALIVE or UPDATE message is not received before the peer's
1036             Keepalive timer expires. In this case, the peer responds by sending a NOTIFICATION
1037             message to its peer. In the former case the I method
1038             is invoked as described above to handle the error, while in the latter the
1039             I method is invoked to inform the application that it has
1040             encountered an error. Both methods are passed a B
1041             object encapsulating the details of the error. In both cases, the transport-layer
1042             connection and BGP session are closed and the peer transitions to the Idle state.
1043             The error handler callbacks can examine the cause of the error and take appropriate
1044             action. This could be to attempt to re-establish the session (perhaps after
1045             sleeping for some amount of time), or to unregister the peer object from the
1046             B object and permanently end the session (for the duration
1047             of the application's running time), or to log the event to a file on the host
1048             system, or some combination of these or none.
1049              
1050             =head1 SEE ALSO
1051              
1052             Net::BGP, Net::BGP::Process, Net::BGP::Update, Net::BGP::Transport,
1053             Net::BGP::Refresh, Net::BGP::Notification
1054              
1055             =head1 AUTHOR
1056              
1057             Stephen J. Scheck
1058              
1059             =cut
1060              
1061             ## End Package Net::BGP::Peer ##
1062              
1063             1;