| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/perl | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # $Id: Policy.pm,v 1.3 2003/06/02 11:50:12 unimlo Exp $ | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package Net::BGP::Policy; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 137701 | use strict; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 102 |  | 
| 8 | 2 |  |  | 2 |  | 12 | use vars qw( $VERSION @ISA ); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 154 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | ## Inheritance and Versioning ## | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | @ISA = qw ( ); | 
| 13 |  |  |  |  |  |  | $VERSION = '0.04'; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | ## Module Imports ## | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 2 |  |  | 2 |  | 12 | use Carp; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 131 |  | 
| 18 | 2 |  |  | 2 |  | 11 | use Net::BGP::Peer; | 
|  | 2 |  |  |  |  | 11 |  | 
|  | 2 |  |  |  |  | 77 |  | 
| 19 | 2 |  |  | 2 |  | 11 | use Scalar::Util qw(weaken); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 1209 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | ## Public Class Methods ## | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub new | 
| 24 |  |  |  |  |  |  | { | 
| 25 | 0 |  | 0 | 0 | 1 |  | my $proto = shift || __PACKAGE__; | 
| 26 | 0 |  | 0 |  |  |  | my $class = ref $proto || $proto; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 0 |  |  |  |  |  | my $this = { | 
| 29 |  |  |  |  |  |  | _in    => {}, | 
| 30 |  |  |  |  |  |  | _out   => {}, | 
| 31 |  |  |  |  |  |  | _peer  => {} | 
| 32 |  |  |  |  |  |  | }; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | bless($this, $class); | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 0 |  |  |  |  |  | return $this; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | ## Public Object Methods ## | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub set | 
| 42 |  |  |  |  |  |  | { | 
| 43 | 0 |  |  | 0 | 1 |  | my ($this,$peer,$dir,$policy) = @_; | 
| 44 | 0 | 0 |  |  |  |  | croak "Set policy need 3 arguments!" if (scalar @_ < 3); | 
| 45 | 0 | 0 |  |  |  |  | $dir = $dir =~ /in/i ? '_in' : '_out'; | 
| 46 | 0 | 0 |  |  |  |  | if (scalar @_ == 3) # Remove peer if no 3rd argument | 
| 47 |  |  |  |  |  |  | { | 
| 48 | 0 |  |  |  |  |  | delete $this->{$dir}->{$peer}; | 
| 49 | 0 |  |  |  |  |  | return; | 
| 50 |  |  |  |  |  |  | } | 
| 51 | 0 | 0 |  |  |  |  | croak "Unknown policy type - Should be a Net::ACL object" | 
| 52 |  |  |  |  |  |  | unless $policy->isa('Net::ACL'); | 
| 53 | 0 | 0 |  |  |  |  | $peer = renew Net::BGP::Peer($peer) unless ref $peer; | 
| 54 | 0 | 0 |  |  |  |  | croak "Peer unknown - Should be a Net::BGP::Peer object" | 
| 55 |  |  |  |  |  |  | unless $peer->isa('Net::BGP::Peer'); | 
| 56 | 0 |  |  |  |  |  | $this->{$dir}->{$peer} = $policy; | 
| 57 | 0 |  |  |  |  |  | weaken($this->{_peer}->{$peer} = $peer); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub delete | 
| 61 |  |  |  |  |  |  | { | 
| 62 | 0 | 0 |  | 0 | 1 |  | croak "delete method needs 2 arguments: The peer and the direction" | 
| 63 |  |  |  |  |  |  | unless scalar @_ == 3; | 
| 64 | 0 |  |  |  |  |  | shift->set(@_); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub out | 
| 68 |  |  |  |  |  |  | { | 
| 69 | 0 |  |  | 0 | 1 |  | my ($this,$prefix,$nlri) = @_; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 |  |  |  |  |  | my %newout; | 
| 72 | 0 | 0 |  |  |  |  | unless (defined $nlri) | 
| 73 |  |  |  |  |  |  | { | 
| 74 | 0 |  |  |  |  |  | foreach my $peer (keys %{$this->{_out}}) | 
|  | 0 |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | { | 
| 76 | 0 |  |  |  |  |  | $newout{$peer} = undef; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  | else | 
| 80 |  |  |  |  |  |  | { | 
| 81 | 0 |  |  |  |  |  | foreach my $peer (keys %{$this->{_out}}) | 
|  | 0 |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | { | 
| 83 | 0 |  |  |  |  |  | my $p = $this->{_out}->{$peer}; | 
| 84 |  |  |  |  |  |  | # query should NOT modify the $nlri object itself! | 
| 85 | 0 | 0 |  |  |  |  | $newout{$peer} = defined $p | 
| 86 |  |  |  |  |  |  | ? ($p->query($prefix,$nlri,$this->{_peer}->{$peer}))[2] | 
| 87 |  |  |  |  |  |  | : $nlri; | 
| 88 |  |  |  |  |  |  | }; | 
| 89 |  |  |  |  |  |  | }; | 
| 90 | 0 |  |  |  |  |  | return \%newout; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub in | 
| 94 |  |  |  |  |  |  | { | 
| 95 | 0 |  |  | 0 | 1 |  | my ($this,$prefix,$nlri) = @_; | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 |  |  |  |  |  | my @nlri; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 |  |  |  |  |  | foreach my $peer (keys %{$nlri}) | 
|  | 0 |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | { | 
| 101 | 0 |  |  |  |  |  | my $n = $nlri->{$peer}; | 
| 102 | 0 | 0 |  |  |  |  | next unless defined($n); | 
| 103 | 0 |  |  |  |  |  | my $p = $this->{_in}->{$peer}; | 
| 104 |  |  |  |  |  |  | # query should NOT modify the $n(lri) object itself! | 
| 105 | 0 | 0 |  |  |  |  | $n = ($p->query($prefix,$n,$this->{_peer}->{$peer}))[2] if defined($p); | 
| 106 | 0 | 0 |  |  |  |  | push(@nlri,$n) if defined $n; | 
| 107 |  |  |  |  |  |  | }; | 
| 108 | 0 |  |  |  |  |  | return \@nlri; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =pod | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head1 NAME | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Net::BGP::Policy - Class representing a Global BGP Routing Policy | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | use Net::BGP::Policy; | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | # Constructor | 
| 122 |  |  |  |  |  |  | $policy = new Net::BGP::Policy(); | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | # Accessor Methods | 
| 125 |  |  |  |  |  |  | $policy->set($peer, 'in', $acl); | 
| 126 |  |  |  |  |  |  | $policy->delete($peer,'out'); | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | $nlri_array_ref  = $policy->in($prefix, { $peer => $nlri, ... } ); | 
| 129 |  |  |  |  |  |  | $out_hash_ref    = $policy->out($prefix, $nlri ); | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | This module implement a class representing a global BGP Routing Policy. It | 
| 134 |  |  |  |  |  |  | does so using L. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =head1 CONSTRUCTOR | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =over 4 | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =item new() - create a new Net::BGP::Policy object | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | $policy = new Net::BGP::Policy(); | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | This is the constructor for Net::BGP::Policy object. It returns a | 
| 145 |  |  |  |  |  |  | reference to the newly created object. It ignores all arguments. | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =back | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =head1 ACCESSOR METHODS | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =over 4 | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =item set() | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | This method is used to configure a policy for a peer in a direction. It takes | 
| 156 |  |  |  |  |  |  | two or three arguments. The first is the peer, the second is the direction | 
| 157 |  |  |  |  |  |  | (C or C). The third is the policy which should be a Net::ACL | 
| 158 |  |  |  |  |  |  | route-map (or an object inherited from Net::ACL). The rules of the route-map | 
| 159 |  |  |  |  |  |  | should be like Net::ACL::RouteMapRule objects. If the third parameter is | 
| 160 |  |  |  |  |  |  | undefined, no policy will be used for the peer. If the third parameter is not | 
| 161 |  |  |  |  |  |  | pressent, the peer will not get updates. | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =item delete() | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | This method is used to remove a peer from the policy in a direction. It takes | 
| 166 |  |  |  |  |  |  | two arguments. The first is the peer, the second is the direction (C or | 
| 167 |  |  |  |  |  |  | C). | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =item in() | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | The in() method executes the policy for incomming updates. The first argument | 
| 172 |  |  |  |  |  |  | is the prefix, the second should be an hash reference. The hash reference | 
| 173 |  |  |  |  |  |  | is indexed on peers with values of NLRI objects avaible from that peer. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | The method returns a list of NLRIs. | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =item out() | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | The out() method executes the policy for outgoing updates. The first argument | 
| 180 |  |  |  |  |  |  | is the prefix, the second is the NLRI object. | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | The method returns a reference to a hash of NLRIs indexed on peers. | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =back | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | Net::BGP, Net::BGP::RIB, Net::BGP::NLRI, Net::BGP::Router, Net::ACL | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =head1 AUTHOR | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | Martin Lorensen | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =cut | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | ## End Package Net::BGP::Policy ## | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | 1; |