BGP/Process.pm

filter: Statementstmt 87.2% 151 / 173 Branchbran 62.5% 40 / 64 Conditioncond 83.3% 10 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 80.2% 224 / 279 CC 46 SCAR 24.7
CC
46
Cov
81%
event_loop
24.4
_update_select
9.0
_init_listen_socket
8.2
CRAP 11.8
« BGP/Update.pm ↑ Summary BGP/ASPath/AS.pm »
1 #!/usr/bin/perl
2  
3 package Net::BGP::Process;
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 ## Inheritance and Versioning ##
9  
10 3 $VERSION = '0.18';
11  
12 ## Module Imports ##
13  
14 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Net::BGP::Peer qw( BGP_PORT TRUE FALSE );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 ## Socket Constants ##
20  
21 2 ▶sub LISTEN_QUEUE_SIZE { 5 }
Subroutine LISTEN_QUEUE_SIZE: called CC=1 SCAR=0.0
Pod undocumented
22  
23 ## Public Methods ##
24  
25 sub new
26 3 ▶{
Subroutine new: called CC=4 SCAR=14.8
Pod undocumented
27 3 my $class = shift();
28 3 my ($arg, $value);
29  
30 3 my $this = {
31 _read_fh => IO::Select->new(),
32 _write_fh => IO::Select->new(),
33 _error_fh => IO::Select->new(),
34 _peer_list => {},
35 _peer_addr => {},
36 _trans_sock => {},
37 _trans_sock_fh => {},
38 _trans_sock_map=> {},
39 _listen_socket => undef,
40 _listen_port => BGP_PORT,
41 _listen_addr => INADDR_ANY,
42 };
43  
44 3 ▶ while ( defined($arg = shift()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($arg = shift())) 2 3 5
45 2 $value = shift();
46 2 ▶ if ( $arg =~ /port/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /port/i) { } 2 0 2
elsif ($arg =~ /listenaddr/i) { } 0 0 0
47 2 $this->{_listen_port} = $value;
48 }
49 elsif ( $arg =~ /listenaddr/i ) {
50 0 $this->{_listen_addr} = inet_aton($value);
51 }
52 else {
53 0 croak "Unknown argument '$arg'";
54 }
55 }
56  
57 3 bless($this, $class);
58  
59 3 return ( $this );
60 }
61  
62 sub add_peer
63 4 ▶{
Subroutine add_peer: called CC=2 SCAR=6.9
Pod undocumented
64 4 my ($this, $peer) = @_;
65  
66 4 ▶ $this->{_peer_addr}->{$peer->this_id}->{$peer->peer_id} = $peer if $peer->is_listener;;
Branch: 1 branch
BranchTrueFalseTotal
if $peer->is_listener 2 2 4
67 4 $this->{_peer_list}->{$peer} = $peer;
68 }
69  
70 sub remove_peer
71 4 ▶{
Subroutine remove_peer: called CC=3 SCAR=11.0
Pod undocumented
72 4 my ($this, $peer) = @_;
73 4 ▶ if ( defined($this->{_peer_list}->{$peer}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_peer_list"}{$peer}) 4 0 4
74 4 $peer->stop();
75 4 foreach my $trans ($peer->transports)
76 {
77 4 $this->_update_select($trans);
78 };
79 4 delete $this->{_peer_addr}->{$peer->this_id}->{$peer->peer_id};
80 4 delete $this->{_peer_list}->{$peer};
81 }
82 }
83  
84 sub event_loop
85 2 ▶{
Subroutine event_loop: called CC=21 SCAR=31.9
Pod undocumented
86 2 my $this = shift();
87 2 my ($time, $last_time, $delta, $min, $min_timer);
88 2 my ($timer);
89  
90 2 my $sigorig = $SIG{'PIPE'};
91 2 ▶ unless (defined $SIG{'PIPE'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $SIG{"PIPE"}) 2 0 2
92 2 $SIG{'PIPE'} = 'IGNORE';
93 }
94  
95 # Poll each peer and create listen socket if any is a listener
96 2 foreach my $peer ( values(%{$this->{_peer_list}}) ) {
97 3 ▶ if ( $peer->is_listener() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer->is_listener) 2 1 3
98 2 $this->_init_listen_socket();
99 2 last;
100 }
101 }
102  
103 2 ▶ while ( scalar(keys(%{$this->{_peer_list}})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$this{"_peer_list"};}) 40 0 40
104  
105 # Process timeouts, events, etc.
106 40 $min_timer = 2147483647;
107 40 $time = time();
108  
109 40 ▶ if ( ! defined($last_time) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $last_time) 2 38 40
110 2 $last_time = $time;
111 }
112  
113 40 $delta = $time - $last_time;
114 40 $last_time = $time;
115  
116 40 foreach my $peer ( values(%{$this->{_peer_list}}) ) {
117  
118 76 foreach my $trans ($peer->transports) {
119 76 $trans->_handle_pending_events();
120 }
121  
122 76 $min = $peer->_update_timers($delta);
123 76 ▶ if ( $min < $min_timer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $min_timer) 51 25 76
124 51 $min_timer = $min;
125 }
126  
127 76 foreach my $trans ($peer->transports)
128 {
129 76 $this->_update_select($trans);
130 };
131 }
132  
133 40 ▶ last if scalar(keys(%{$this->{_peer_list}})) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$this{"_peer_list"};} == 0 2 38 40
134  
135 38 $! = 0;
136  
137 38 my @ready = IO::Select->select($this->{_read_fh}, $this->{_write_fh}, $this->{_error_fh}, $min_timer);
138  
139 38 ▶ if ( @ready ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ready) { } 28 10 38
140  
141 # dispatch ready to reads
142 28 foreach my $ready ( @{$ready[0]} ) {
143 34 ▶ if ( $ready == $this->{_listen_socket} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ready == $this->{'_listen_socket'}) { } 2 32 34
144 2 $this->_handle_accept();
145 }
146 else {
147 32 my $trans = $this->{_trans_sock_map}->{$ready};
148 32 $trans->_handle_socket_read_ready();
149 }
150 }
151  
152 # dispatch ready to writes
153 28 foreach my $ready ( @{$ready[1]} ) {
154 4 my $trans = $this->{_trans_sock_map}->{$ready};
155 4 $trans->_handle_socket_write_ready();
156 }
157  
158 # dispatch exception conditions
159 28 foreach my $ready ( @{$ready[2]} ) {
160 0 my $trans = $this->{_trans_sock_map}->{$ready};
161 0 $trans->_handle_socket_error_condition();
162 }
163 } else {
164 10 ▶ if ($!{EBADF}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EBADF"}) 0 10 10
165 # One of the sockets is bad
166 0 foreach my $fh ( $this->{_error_fh}->handles ) {
167 0 ▶ if (!$fh->opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->opened) 0 0 0
168 0 my $trans = $this->{_trans_sock_map}->{$fh};
169 # We seem to have a transport with a dud socket
170 # Update the select statement - not sure if this
171 # is right though - Damian Ivereigh 29/09/2016
172 0 ▶ if ($trans) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trans) { } 0 0 0
173 0 $this->_update_select($trans);
174 } else {
175 0 warn "Cannot find trans object\n";
176 }
177 }
178 }
179 }
180 }
181 }
182  
183 2 $this->_cleanup();
184  
185 2 delete $SIG{'PIPE'};
186 2 ▶ $SIG{'PIPE'} = $sigorig if defined $sigorig;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sigorig 0 2 2
187 }
188  
189 ## Private Methods ##
190  
191 sub _add_trans_sock
192 4 ▶{
Subroutine _add_trans_sock: called CC=1 SCAR=0.0
193 4 my ($this, $trans, $sock) = @_;
194  
195 4 $this->{_trans_sock}->{$trans} = $sock;
196 4 $this->{_trans_sock_fh}->{$trans} = $sock->fileno();
197 4 $this->{_trans_sock_map}->{$sock} = $trans;
198 }
199  
200 sub _remove_trans_sock
201 4 ▶{
Subroutine _remove_trans_sock: called CC=1 SCAR=0.0
202 4 my ($this, $trans) = @_;
203  
204 4 delete $this->{_trans_sock_map}->{$this->{_trans_sock}->{$trans}};
205 4 delete $this->{_trans_sock}->{$trans};
206 4 delete $this->{_trans_sock_fh}->{$trans};
207 }
208  
209 sub _init_listen_socket
210 2 ▶{
Subroutine _init_listen_socket: called CC=7 SCAR=21.0
211 2 my $this = shift();
212 2 my ($socket, $proto, $rv, $sock_addr);
213  
214 2 eval {
215 2 $socket = IO::Socket->new( Domain => AF_INET );
216 2 ▶ if ( ! defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 2 2
217 0 die("IO::Socket construction failed");
218 }
219  
220 2 $rv = $socket->blocking(FALSE);
221 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
222 0 die("set socket non-blocking failed");
223 }
224  
225 2 $proto = getprotobyname('tcp');
226 2 $rv = $socket->socket(PF_INET, SOCK_STREAM, $proto);
227 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
228 0 die("socket() failed");
229 }
230  
231 2 $socket->sockopt(SO_REUSEADDR, TRUE);
232  
233 $sock_addr = sockaddr_in($this->{_listen_port},
234 2 $this->{_listen_addr});
235 2 $rv = $socket->bind($sock_addr);
236 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
237 0 die("bind() failed");
238 }
239  
240 2 $rv = $socket->listen(LISTEN_QUEUE_SIZE);
241 2 ▶ if ( ! defined($rv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 2 2
242 0 die("listen() failed");
243 }
244  
245 2 $this->{_read_fh}->add($socket);
246 2 $this->{_write_fh}->add($socket);
247 2 $this->{_error_fh}->add($socket);
248 2 $this->{_listen_socket} = $socket;
249 };
250 2 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
251 }
252  
253 sub _cleanup
254 2 ▶{
Subroutine _cleanup: called CC=2 SCAR=6.9
255 2 my $this = shift();
256 2 my $socket;
257  
258 2 ▶ if ( defined($this->{_listen_socket}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_listen_socket"}) 2 0 2
259 2 $socket = $this->{_listen_socket};
260 2 $this->{_read_fh}->remove($socket);
261 2 $this->{_write_fh}->remove($socket);
262 2 $this->{_error_fh}->remove($socket);
263  
264 2 $socket->close();
265 2 $this->{_listen_socket} = undef;
266 }
267 }
268  
269 sub _handle_accept
270 2 ▶{
Subroutine _handle_accept: called CC=5 SCAR=20.5
271 2 my $this = shift;
272  
273 2 my ($socket, $peer_addr) = $this->{_listen_socket}->accept();
274 2 my ($port, $addr) = sockaddr_in($peer_addr);
275
276 2 my $ip_addr = inet_ntoa($addr);
277 2 my $ip_local = inet_ntoa($socket->sockaddr);
278  
279 2 my $peer = $this->{_peer_addr}->{$ip_local}->{$ip_addr};
280 2 ▶ if ( ! defined($peer)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $peer) { } 0 2 2
elsif (not $peer->is_listener) { } 0 2 2
281 0 warn "Ignored incoming connection from unknown peer ($ip_addr => $ip_local)\n";
282 0 $socket->close();
283 }
284 elsif ( ! $peer->is_listener() ) {
285 0 warn "Ignored incoming connection for non-listning peer\n";
286 0 $socket->close();
287 }
288 else {
289 2 my $trans = $peer->transport;
290  
291 # Can't reuse the existing Net::BGP::Peer object unless it is a passive session
292 2 ▶ if (! $peer->is_passive() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($peer->is_passive) 0 2 2
293  
294 # If there is a sibling, we need to kill it, assuming that there
295 # is a collision here.
296 #
297 # This can happen in a BGP misconfiguration where no OPEN gets
298 # sent by the other end (for instance, the other end is expecting
299 # a smaller bigger TTL, but still listens for BGP and sends SYNACK
300 # packets back - like Mikrotik)
301 0 ▶ if (defined $trans->{_sibling}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trans->{"_sibling"}) 0 0 0
302 0 $trans->{_sibling}->_handle_collision_selfdestuct;
303 }
304  
305 # Now we can clone
306 0 $trans = $trans->_clone;
307 }
308  
309 2 $trans->_set_socket($socket);
310 }
311 }
312  
313 sub _update_select
314 80 ▶{
Subroutine _update_select: called CC=9 SCAR=22.0
315 80 my ($this, $trans) = @_;
316  
317 80 my $trans_socket = $trans->_get_socket();
318 80 my $this_socket = $this->{_trans_sock}->{$trans};
319  
320 80 ▶ if ( defined($trans_socket) && ! defined($this_socket) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $trans_socket and not defined $this_socket) { } 4 76 80
elsif (defined $this_socket and not defined $trans_socket) { } 4 72 76
elsif (defined $this_socket and defined $trans_socket) { } 66 12 78
Condition100%3/3
defined $trans_socket and not defined $this_socket
!ll&&!rl&&r
10664
MC/DC100%2/2
defined $trans_socket and not defined $this_socket
defined $trans_socket not defined $this_socket
Truth table100%3/3
A and not(B)
A= defined $trans_socketB= not defined $this_socket
ABresult
0X0
100
111
321 4 $this->_add_trans_sock($trans, $trans_socket);
322 4 $this->{_read_fh}->add($trans_socket);
323 4 $this->{_write_fh}->add($trans_socket);
324 4 $this->{_error_fh}->add($trans_socket);
325 }
326 ▶ elsif ( defined($this_socket) && ! defined($trans_socket) ) {
Condition100%3/3
defined $this_socket and not defined $trans_socket
!ll&&!rl&&r
6664
MC/DC100%2/2
defined $this_socket and not defined $trans_socket
defined $this_socket not defined $trans_socket
Truth table100%3/3
A and not(B)
A= defined $this_socketB= not defined $trans_socket
ABresult
0X0
100
111
327 4 $this->{_read_fh}->remove($this->{_trans_sock_fh}->{$trans});
328 4 $this->{_write_fh}->remove($this->{_trans_sock_fh}->{$trans});
329 4 $this->{_error_fh}->remove($this->{_trans_sock_fh}->{$trans});
330 4 $this->_remove_trans_sock($trans);
331 }
332 ▶ elsif ( defined($this_socket) && defined($trans_socket) ) {
Condition66%2/3
defined $this_socket and defined $trans_socket
!ll&&!rl&&r
6066
MC/DC50%1/2
defined $this_socket and defined $trans_socket
defined $this_socket defined $trans_socket
Truth table66%2/3
A and B
A= defined $this_socketB= defined $trans_socket
ABresult
0X0
100
111
333 66 ▶ if ( $trans->_is_connected() && $this->{_write_fh}->exists($this_socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trans->_is_connected and $this->{"_write_fh"}->exists($this_socket)) 4 62 66
Condition66%2/3
$trans->_is_connected and $this->{"_write_fh"}->exists($this_socket)
!ll&&!rl&&r
0624
MC/DC50%1/2
$trans->_is_connected and $this->{"_write_fh"}->exists($this_socket)
$trans->_is_connected $this->{"_write_fh"}->exists($this_socket)
Truth table66%2/3
A and B
A= $trans->_is_connectedB= $this->{"_write_fh"}->exists($this_socket)
ABresult
0X0
100
111
334 4 $this->{_write_fh}->remove($this_socket);
335 }
336 }
337 }
338  
339 ## POD ##
340  
341 =pod
342  
343 =head1 NAME
344  
345 C<Net::BGP::Process> - Class encapsulating BGP session multiplexing functionality
346  
347 =head1 SYNOPSIS
348  
349 use Net::BGP::Process;
350  
351 $bgp = Net::BGP::Process->new( Port => $port );
352  
353 $bgp->add_peer($peer);
354 $bgp->remove_peer($peer);
355 $bgp->event_loop();
356  
357 =head1 DESCRIPTION
358  
359 This module encapsulates the functionality necessary to multiplex multiple
360 BGP peering sessions. While individual L<Net::BGP::Peer> objects contain
361 the state of each peering session, it is the C<Net::BGP::Process> object
362 which monitors each peer's transport-layer connection and timers and signals
363 the peer whenever messages are available for processing or timers expire.
364 A C<Net::BGP::Process> object must be instantiated, even if a program only
365 intends to establish a session with a single peer.
366  
367 =head1 METHODS
368  
369 I<new()> - create a new C<Net::BGP::Process> object
370  
371 $bgp = Net::BGP::Process->new( Port => $port, ListenAddr => '1.2.3.4' );
372  
373 This is the constructor for C<Net::BGP::Process> objects. It returns a
374 reference to the newly created object. The following named parameters may
375 be passed to the constructor.
376  
377 =head2 Port
378  
379 This parameter sets the TCP port the BGP process listens on. It may be
380 omitted, in which case it defaults to the well-known BGP port TCP/179.
381 If the program cannot run with root priviliges, it is necessary to set
382 this parameter to a value greater than or equal to 1024. Note that some
383 BGP implementations may not allow the specification of an alternate port
384 and may be unable to establish a connection to the C<Net::BGP::Process>.
385  
386 =head2 ListenAddr
387  
388 This parameter sets the IP address the BGP process listens on. Defaults
389 to INADDR_ANY.
390  
391 I<add_peer()> - add a new peer to the BGP process
392  
393 $bgp->add_peer($peer);
394  
395 Each L<Net::BGP::Peer> object, which corresponds to a distinct peering
396 session, must be registered with the C<Net::BGP::Process> object via this
397 method. It is typically called immediately after a new peer object is created
398 to add the peer to the BGP process. The method accepts a single parameter,
399 which is a reference to a L<Net::BGP::Peer> object.
400  
401 I<remove_peer()> - remove a peer from the BGP process
402  
403 $bgp->remove_peer($peer);
404  
405 This method should be called if a peer should no longer be managed by the
406 BGP process, for example, if the session is broken or closed and will not
407 be re-established. The method accepts a single parameter, which is a
408 reference to a L<Net::BGP::Peer> object which has previously been registered
409 with the process object with the add_peer() method.
410  
411 I<event_loop()> - start the process event loop
412  
413 $bgp->event_loop();
414  
415 This method must called after all peers are instantiated and added to the
416 BGP process and any other necessary initialization has occured. Once it
417 is called, it takes over program control flow, and control will
418 only return to user code when one of the event callback functions is
419 invoked upon receipt of a BGP protocol message or a user
420 established timer expires (see L<Net::BGP::Peer> for details
421 on how to establish timers and callback functions). The method takes
422 no parameters. It will only return when there are no L<Net::BGP::Peer>
423 objects remaining under its management, which can only occur if they
424 are explicitly removed with the remove_peer() method (perhaps called
425 in one of the callback or timer functions).
426  
427 =head1 SEE ALSO
428  
429 =over
430  
431 =item L<Net::BGP>
432  
433 =item L<Net::BGP::Peer>
434  
435 =item L<Net::BGP::Update>
436  
437 =item L<Net::BGP::Refresh>
438  
439 =item L<Net::BGP::ASPath>
440  
441 =item L<Net::BGP::NLRI>
442  
443 =item L<Net::BGP::Notification>
444  
445 =back
446  
447 =head1 AUTHOR
448  
449 Stephen J. Scheck <sscheck@cpan.org>
450  
451 =cut
452  
453 ## End Package Net::BGP::Process ##
454  
455 3 1;
« BGP/Update.pm ↑ Summary BGP/ASPath/AS.pm »