add all the basic aranea routing + local configuration broadcasts
[spider.git] / perl / Thingy / Hello.pm
1 #
2 # Hello Thingy handling
3 #
4 # Note that this is a generator of pc19n and pc16n/pc16u
5 # and a consumer of fpc19n and fpc16n
6 #
7 # $Id$
8 #
9 # Copyright (c) 2005 Dirk Koopman G1TLH
10 #
11
12 use strict;
13
14 package Thingy::Hello;
15
16 use vars qw($VERSION $BRANCH);
17
18 main::mkver($VERSION = q$Revision$);
19
20 use DXChannel;
21 use DXDebug;
22 use Verify;
23 use Thingy;
24 use Thingy::RouteFilter;
25 use Thingy::Rt;
26
27 use vars qw(@ISA $verify_on_login);
28 @ISA = qw(Thingy Thingy::RouteFilter);
29
30 $verify_on_login = 1;                   # make sure that a HELLO coming from
31                                 # the dxchan call is authentic
32
33 sub gen_Aranea
34 {
35         my $thing = shift;
36         unless ($thing->{Aranea}) {
37                 $thing->add_auth;
38
39                 $thing->{sw} ||= 'DXSp';
40                 $thing->{v} ||= $main::version;
41                 $thing->{b} ||= $main::build;
42                 
43                 $thing->{Aranea} = Aranea::genmsg($thing, [qw(sw v b s auth)]);
44         }
45         return $thing->{Aranea};
46 }
47
48 sub handle
49 {
50         my $thing = shift;
51         my $dxchan = shift;
52         
53         my $nref;
54         $thing->{pc19n} ||= [];
55         
56         # verify authenticity
57         if ($dxchan->{call} eq $thing->{origin}) {
58
59                 # for directly connected calls
60                 if ($verify_on_login) {
61                         my $pp = $dxchan->user->passphrase;
62                         unless ($pp) {
63                                 dbglog('err', "Thingy::Hello::handle: verify on and $thing->{origin} has no passphrase");
64                                 $dxchan->disconnect;
65                                 return;
66                         }
67                         my $auth = Verify->new("DXSp,$thing->{origin},$thing->{s},$thing->{v},$thing->{b}");
68                         unless ($auth->verify($thing->{auth}, $dxchan->user->passphrase)) {
69                                 dbglog('err', "Thingy::Hello::handle: verify on and $thing->{origin} failed auth check");
70                                 $dxchan->disconnect;
71                                 return;
72                         }
73                 }
74                 if ($dxchan->{state} ne 'normal') {
75                         $dxchan->start($dxchan->{conn}->{csort}, $dxchan->{conn}->{outbound} ? 'O' : 'A');
76                         if ($dxchan->{outbound}) {
77                                 my $thing = Thingy::Hello->new();
78                                 $thing->send($dxchan);
79
80                                 # broadcast our configuration to the world
81                                 $thing = Thingy::Rt->new_lcf;
82                                 $thing->broadcast;
83                         }
84                 }
85                 my $origin = $thing->{origin};
86                 $nref = $main::routeroot->add($origin, $thing->{v}, 1);
87                 push @{$thing->{pc19n}}, $nref if $nref;
88         } else {
89                 
90                 # for otherwise connected calls, that come in relayed from other nodes
91                 # note that we cannot do any connections at this point
92                 $nref = Route::Node::get($thing->{origin});
93                 unless ($nref) {
94                         my $v = $thing->{user} ? undef : $thing->{v};
95                         $nref = Route::Node->new($thing->{origin}, $v, 1);
96                         push @{$thing->{pc19n}}, $nref;
97                 }
98         }
99
100         # handle "User"
101         if (my $user = $thing->{user}) {
102                 my $ur = Route::get($user);
103                 unless ($ur) {
104                         my $uref = DXUser->get_current($user);
105                         if ($uref->is_node || $uref->is_aranea) {
106                                 my $u = $nref->add($user, $thing->{v}, 1);
107                                 push @{$thing->{pc19n}}, $u if $u;
108                         } else {
109                                 $thing->{pc16n} = $nref;
110                                 $thing->{pc16u} = [$nref->add_user($user, 1)];
111                         }
112                 }
113         }
114         RouteDB::update($thing->{origin}, $dxchan->{call}, $thing->{hopsaway});
115         RouteDB::update($thing->{user}, $dxchan->{call}, $thing->{hopsaway}) if $thing->{user};
116         
117         delete $thing->{pc19n} unless @{$thing->{pc19n}};
118         
119         $thing->broadcast($dxchan);
120 }
121
122 1;