0e5bf3e8ce3a0dec514a78f80bdd3edee398b2e3
[spider.git] / perl / Thingy / RouteFilter.pm
1 #
2 # Thingy Route Filter handling
3 #
4 # This to provide multiple inheritance to Routing entities
5 # that wish to do standard filtering
6 #
7 # $Id$
8 #
9 # Copyright (c) 2005 Dirk Koopman G1TLH
10 #
11
12 use strict;
13
14 package Thingy::RouteFilter;
15
16 use vars qw($VERSION $BRANCH);
17
18 main::mkver($VERSION = q$Revision$);
19
20 use DXChannel;
21 use DXDebug;
22 use DXProt;
23 use Thingy;
24
25 use vars qw(@ISA);
26 @ISA = qw(Thingy);
27
28 sub _filter
29 {
30         my $dxchan = shift;
31         my $r = shift;
32         
33         my ($filter, $hops) = $dxchan->{inroutefilter}->it($dxchan->{call}, $dxchan->{dxcc}, $dxchan->{itu}, $dxchan->{cq}, $r->{call}, $r->{dxcc}, $r->{itu}, $r->{cq}, $dxchan->{state}, $r->{state});
34         return $filter ? $r : undef;
35 }
36
37 sub gen_DXProt
38 {
39         my $thing = shift;
40         my @out;
41         push @out, DXProt::pc21(@{$thing->{fpc21n}}) if $thing->{fpc21n};
42         push @out, DXProt::pc17($thing->{fpc17n}, $thing->{pc17u})  if $thing->{fpc17n};
43         push @out, DXProt::pc19(@{$thing->{fpc19n}}) if $thing->{fpc19n};
44         push @out, DXProt::pc16($thing->{fpc16n}, @{$thing->{pc16u}}) if $thing->{fpc16n};
45         return \@out;
46 }
47
48 sub in_filter
49 {
50         my $thing = shift;
51         my $dxchan = shift;
52         
53         # global route filtering on INPUT
54         if ($dxchan->{inroutefilter}) {
55                 my $r = Route::Node::get($thing->{origin}) || Route->new($thing->{origin});
56                 my ($filter, $hops) = $dxchan->{inroutefilter}->it($dxchan->{call}, $dxchan->{dxcc}, $dxchan->{itu}, $dxchan->{cq}, $r->{call}, $r->{dxcc}, $r->{itu}, $r->{cq}, $dxchan->{state}, $r->{state});
57                 unless ($filter) {
58                         dbg("PCPROT: Rejected by input route filter") if isdbg('chanerr');
59                         return;
60                 }
61         } elsif ($dxchan->{isolate} && $thing->{origin} ne $main::mycall) {
62                 return;
63         }
64         return 1;
65 }
66
67 sub out_filter
68 {
69         my $thing = shift;
70         my $dxchan = shift;
71         
72         # global route filtering on OUTPUT
73         if ($dxchan->{routefilter}) {
74                 my $r = Route::Node::get($thing->{origin});
75                 my ($filter, $hops) = $dxchan->{routefilter}->it($dxchan->{call}, $dxchan->{dxcc}, $dxchan->{itu}, $dxchan->{cq}, $r->{call}, $r->{dxcc}, $r->{itu}, $r->{cq}, $dxchan->{state}, $r->{state});          
76                 unless ($filter) {
77                         dbg("PCPROT: Rejected by output route filter") if isdbg('chanerr');
78                         return;
79                 }
80                 
81                 if ($dxchan->isa('DXProt')) {
82                         $thing->{hops} = $hops if $hops;
83                         delete $thing->{fpc16n};
84                         delete $thing->{fpc17n};
85                         delete $thing->{fpc19n};
86                         delete $thing->{fpc21n};
87         
88                         $thing->{fpc16n} = _filter($dxchan, $thing->{pc16n}) if $thing->{pc16n};
89                         $thing->{fpc17n} = _filter($dxchan, $thing->{pc17n}) if $thing->{pc17n};
90                         $thing->{fpc19n} = [_filter($dxchan, @{$thing->{pc19n}})] if $thing->{pc19n};
91                         $thing->{fpc21n} = [_filter($dxchan, @{$thing->{pc21n}})] if $thing->{pc21n};
92                 }
93                 return 1;
94                 
95         } elsif ($dxchan->{isolate}) {
96                 return if $thing->{origin} ne $main::mycall;
97         }
98         if ($dxchan->isa('DXProt')) {
99                 $thing->{fpc16n} ||= $thing->{pc16n}; 
100                 $thing->{fpc17n} ||= $thing->{pc17n}; 
101                 $thing->{fpc19n} ||= $thing->{pc18n}; 
102                 $thing->{fpc21n} ||= $thing->{pc21n}; 
103         }
104         return 1;
105 }
106
107 1;