Router.pm

filter: Statementstmt 23.2% 20 / 86 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 35.2% 6 / 17 Podpod 100% 4 / 4 totaltotal 18.1% 30 / 165 CC 26 SCAR 36.0
CC
26
Cov
15%
set_policy
90.0
new
42.0
add_peer
30.0
CRAP 36.4
↑ Summary RIBEntry.pm »
1 #!/usr/bin/perl -wT
2  
3 # $Id: Router.pm,v 1.15 2003/06/02 15:01:17 unimlo Exp $
4  
5 package Net::BGP::Router;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use vars qw( $VERSION @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ## Inheritance and Versioning ##
12  
13 1 $VERSION = '0.04';
14  
15 ## Import modules ##
16  
17 1 ▶use Net::BGP::RIB;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 ## Public Class Methods ##
22  
23 sub new
24 0 ▶{
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
25 0 my $proto = shift;
26 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
27  
28 0 my $this = {
29 _name => undef,
30 _RIB => new Net::BGP::RIB,
31 _inpeers => {},
32 _outpeers => {},
33 _policy => undef
34 };
35  
36 0 ▶ while ( defined(my $arg = shift) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arg = shift())) 0 0 0
37 0 my $value = shift;
38  
39 0 ▶ if ( $arg =~ /name/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /name/i) { } 0 0 0
elsif ($arg =~ /policy/i) { } 0 0 0
40 0 $this->{_name} = $value;
41 }
42 elsif ( $arg =~ /policy/i ) {
43 0 ▶ croak "Policy should be a Net::BGP::Policy or sub-class"
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("Net::BGP::Policy") 0 0 0
44 unless $value->isa('Net::BGP::Policy');
45 0 $this->{_policy} = $value;
46 }
47 else {
48 0 croak "unrecognized argument $arg\n";
49 }
50 }
51  
52 0 bless($this, $class);
53 }
54  
55 ## Public Object Methods ##
56  
57 sub add_peer
58 0 ▶{
Subroutine add_peer: not called CC=5 SCAR=34.0
Pod documented
59 0 my ($this,$peer,$dir,$acl) = @_;
60  
61 0 ▶ if ($dir =~ /(out|both)/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /(out|both)/i) 0 0 0
62 {
63 # Policy
64 0 ▶ $this->{_policy}->set($peer,'out',$acl) if defined $this->{_policy};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_policy"} 0 0 0
65  
66 # RIB
67 0 $this->{_RIB}->add_peer($peer,'out',$acl);
68  
69 # Refresh handler
70 0 ▶ my $callbackrefresh = sub { $this->_handle_refresh(@_); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 $peer->set_refresh_callback($callbackrefresh);
72  
73 # Remember for destruction
74 0 weaken($this->{_outpeers}->{$peer} = $peer);
75 };
76  
77 0 ▶ if ($dir =~ /(in|both)/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /(in|both)/i) 0 0 0
78 {
79 # Policy
80 0 ▶ $this->{_policy}->set($peer,'in',$acl) if defined $this->{_policy};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_policy"} 0 0 0
81  
82 # RIB
83 0 $this->{_RIB}->add_peer($peer,'in',$acl);
84  
85 # Update handler
86 0 ▶ my $callbackupdate = sub { $this->_handle_update(@_); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 $peer->set_update_callback($callbackupdate);
88  
89 # Reset handler
90 0 ▶ my $callbackreset = sub { $this->_handle_reset(@_); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 $peer->set_reset_callback($callbackreset);
92  
93 # Remember for destruction
94 0 weaken($this->{_inpeers}->{$peer} = $peer);
95 };
96 }
97  
98 sub remove_peer
99 0 ▶{
Subroutine remove_peer: not called CC=5 SCAR=34.0
Pod documented
100 0 my ($this,$peer,$dir) = @_;
101  
102 0 ▶ if ($dir =~ /(out|both)/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /(out|both)/i) 0 0 0
103 {
104 # Callbacks
105 0 $peer->set_refresh_callback(undef);
106  
107 # Policy
108 0 ▶ $this->{_policy}->delete($peer,'out') if defined $this->{_policy};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_policy"} 0 0 0
109  
110 # RIB
111 0 $this->{_RIB}->remove_peer($peer,'out',$this->{_policy});
112  
113 # Forget!
114 0 delete $this->{_outpeers}->{$peer};
115 };
116  
117 0 ▶ if ($dir =~ /(in|both)/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /(in|both)/i) 0 0 0
118 {
119 # Callbacks
120 0 $peer->set_reset_callback(undef);
121 0 $peer->set_update_callback(undef);
122  
123 # Policy
124 0 ▶ $this->{_policy}->delete($peer,'in') if defined $this->{_policy};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_policy"} 0 0 0
125  
126 # RIB
127 0 $this->{_RIB}->remove_peer($peer,'in',$this->{_policy});
128  
129 # Forget!
130 0 delete $this->{_inpeers}->{$peer};
131 };
132 }
133  
134 sub set_policy
135 0 ▶{
Subroutine set_policy: not called CC=9 SCAR=45.0
Pod documented
136 0 my ($this,$policy,$peer,$dir) = @_;
137 0 ▶ if (! defined $policy || $policy->isa('Net::ACL'))
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $policy or $policy->isa('Net::ACL')) { } 0 0 0
elsif ($policy->isa('Net::BGP::Policy')) { } 0 0 0
Condition0%0/3
not defined $policy or $policy->isa("Net::ACL")
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $policy or $policy->isa("Net::ACL")
not defined $policy $policy->isa("Net::ACL")
Truth table0%0/3
not(A) or B
A= not defined $policyB= $policy->isa("Net::ACL")
ABresult
1X1
011
000
138 {
139 0 ▶ croak "Need peer and direction when assigning or removing local policy"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer and defined $dir 0 0 0
Condition0%0/3
defined $peer and defined $dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $peer and defined $dir
defined $peer defined $dir
Truth table0%0/3
A and B
A= defined $peerB= defined $dir
ABresult
0X0
100
111
140 unless defined $peer && defined $dir;
141 0 ▶ croak "No global policy object to modify" unless defined $this->{_policy};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"_policy"} 0 0 0
142 0 $this->{_policy}->set($peer,$dir,$policy);
143 }
144 elsif ($policy->isa('Net::BGP::Policy'))
145 {
146 0 ▶ croak "No peer or direction allowed when asigning globel policy"
Branch: 1 branch
BranchTrueFalseTotal
if defined $peer or defined $dir 0 0 0
Condition0%0/3
defined $peer or defined $dir
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $peer or defined $dir
defined $peer defined $dir
Truth table0%0/3
A or B
A= defined $peerB= defined $dir
ABresult
1X1
011
000
147 if defined $peer || defined $dir;
148 0 $this->{_policy} = $policy;
149 }
150 else
151 {
152 0 croak "Invalid policy - Need a Net::ACL, a Net::BGP::Policy, or a sub-class of these\n";
153 };
154 }
155  
156 sub DESTROY
157 0 ▶{
Subroutine DESTROY: not called CC=5 SCAR=34.0
158 0 my $this = shift;
159 0 foreach my $peer (values %{$this->{_outpeers}})
160 {
161 0 ▶ next unless defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 0 0
162 0 $this->remove_peer($peer,'out');
163 };
164 0 foreach my $peer (values %{$this->{_inpeers}})
165 {
166 0 ▶ next unless defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 0 0
167 0 $this->remove_peer($peer,'in');
168 };
169 }
170  
171 ## Private Object Methods ##
172  
173 sub _handle_update
174 0 ▶{
Subroutine _handle_update: not called CC=1 SCAR=6.9
175 0 my ($this,$peer,$update) = @_;
176 0 $this->{_RIB}->handle_update($peer,$update,$this->{_policy});
177 }
178  
179 sub _handle_reset
180 0 ▶{
Subroutine _handle_reset: not called CC=1 SCAR=6.9
181 0 my ($this,$peer,$notif) = @_;
182 # The notification packet itself is ignored - But peer is down when we get here!
183 0 warn "GOT RESET: " . $peer->asstring . "\n";
184 0 $this->{_RIB}->reset_peer($peer,'in',$this->{_policy});
185 }
186  
187 sub _handle_refresh
188 0 ▶{
Subroutine _handle_refresh: not called CC=1 SCAR=6.9
189 0 my ($this,$peer,$refresh) = @_;
190 # The refresh packet itself is ignored - No understading of Address Families yet...
191 0 warn "GOT REFRESH: " . $peer->asstring . "\n";
192 0 $this->{_RIB}->reset_peer($peer,'out',$this->{_policy});
193 }
194  
195 =pod
196  
197 =head1 NAME
198  
199 Net::BGP::Router - A BGP Router based on Net::BGP
200  
201 =head1 SYNOPSIS
202  
203 use Net::BGP::Router;
204  
205 # Constructor
206 $router = new Net::BGP::Router(
207 Name => 'My very own router!',
208 Policy => new Net::BGP::Policy
209 );
210  
211 # Accessor Methods
212 $router->add_peer($peer,'both',$acl);
213 $router->remove_peer($peer,'both');
214 $router->set_policy($policy);
215 $router->set_policy($peer,'in',$acl);
216  
217  
218 =head1 DESCRIPTION
219  
220 This module implement a BGP router. It uses L<Net::BGP|Net::BGP> objects for
221 the BGP sessions and a L<Net::BGP::RIB|Net::BGP::RIB> object to store the
222 routes. Policy are handled using a L<Net::BGP::Policy|Net::BGP::Policy> object.
223  
224 =head1 CONSTRUCTOR
225  
226 =over 4
227  
228 =item new() - create a new Net::BGP::Router object
229  
230 $router = new Net::BGP::Router(
231 Name => 'My very own router!',
232 Policy => new Net::BGP::Policy
233 );
234  
235 This is the constructor for Net::BGP::Router object. It returns a
236 reference to the newly created object. The following named parameters may
237 be passed to the constructor:
238  
239 =over 4
240  
241 =item Name
242  
243 This is the name of the router or router-context. This is for informational
244 use only.
245  
246 =item Policy
247  
248 This is the Net::BGP::Policy object used as policy. If not specified, no
249 policy will be used. Note that the Policy method set()
250 will be issued on every add_peer() and remove_peer(). Therefor there is no
251 reason to do this manualy before adding the peers.
252  
253 =back
254  
255 =back
256  
257 =head1 ACCESSOR METHODS
258  
259 =over 4
260  
261 =item add_peer()
262  
263 This method adds a peer to the router. The first argument is the peer object.
264 The second argument is the direction of the peer. A peer can either only
265 contribute with updates C<in>, only recieve updates C<out>, or both C<both>.
266 The third argument is optitional and is a peer/direction-specific policy as
267 a Net::ACL object.
268  
269 =item remove_peer()
270  
271 This medhod removes a peer from the router. The first argument is the peer
272 object. The second argument is the direction in which the peer should be
273 removed.
274  
275 =item set_policy()
276  
277 This medhod can either change the global policy or the policy for a peer in
278 some direction. The first argument is the policy object. If the policy object
279 is a Net::BGP::Policy object, it will be used as a new global policy. If it
280 is a Net::ACL object, it will be used as a peer policy for the peer object and
281 diraction specified as second and third argument.
282  
283 =back
284  
285 =head1 SEE ALSO
286  
287 Net::BGP, Net::BGP::RIB, Net::BGP::Policy, Net::ACL
288  
289 =head1 AUTHOR
290  
291 Martin Lorensen <bgp@martin.lorensen.dk>
292  
293 =cut
294  
295 ## End of Net::BGP::Router ##
296  
297 1 1;
↑ Summary RIBEntry.pm »