7221d7898722fc58dd15a451b388211d5ada13fd
[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         my $dxchan = shift;
37         
38         unless ($thing->{Aranea}) {
39                 if ($thing->{user}) {
40                         $thing->{h} ||= $dxchan->here;
41                 } else {
42                         $thing->{sw} ||= 'DXSp';
43                         $thing->{v} ||= $main::me->version;
44                         $thing->{b} ||= $main::me->build;
45                         $thing->{h} ||= $main::me->here;
46                         $thing->add_auth;
47                 }
48                 
49                 $thing->{Aranea} = Aranea::genmsg($thing, [qw(sw h v b s auth)]);
50         }
51         return $thing->{Aranea};
52 }
53
54 sub handle
55 {
56         my $thing = shift;
57         my $dxchan = shift;
58         
59         my $origin = $thing->{origin};
60         my $node = $dxchan->{call};
61         
62         my $nref;
63
64         $thing->{pc19n} ||= [];
65
66         my $v = $thing->{v};
67         if ($v) {
68                 $v = $DXProt::myprot_version + int ($v*100) if $v > 2 && $v < 3;
69                 $v = $DXProt::myprot_version + 150 unless $v >= 5400;
70                 $v =~ s/\.\d+$//;
71                 $thing->{pcv} = $v;
72         }
73         
74         # verify authenticity
75         if ($node eq $origin) {
76
77                 # for directly connected calls
78                 if ($verify_on_login && !$thing->{user}) {
79                         my $pp = $dxchan->user->passphrase;
80                         unless ($pp) {
81                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin has no passphrase");
82                                 $dxchan->disconnect;
83                                 return;
84                         }
85                         my $auth = Verify->new("DXSp,$origin,$thing->{s},$thing->{v},$thing->{b}");
86                         unless ($auth->verify($thing->{auth}, $dxchan->user->passphrase)) {
87                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin failed auth check");
88                                 $dxchan->disconnect;
89                                 return;
90                         }
91                 }
92                 if ($dxchan->{state} ne 'normal') {
93                         $nref = $main::routeroot->add($origin, $thing->{pcv}, $thing->{h});
94                         push @{$thing->{pc19n}}, $nref if $nref;
95                         $dxchan->start($dxchan->{conn}->{csort}, $dxchan->{conn}->{outbound} ? 'O' : 'A');
96                         if ($dxchan->{outbound}) {
97                                 my $thing = Thingy::Hello->new();
98                                 $thing->send($dxchan);
99
100                                 # broadcast our configuration to the world
101                                 $thing = Thingy::Rt->new_cf;
102                                 $thing->broadcast;
103                         }
104                 }
105                 $nref = Route::Node::get($origin);
106                 $nref->np(1);
107         } else {
108                 
109                 # for otherwise connected calls, that come in relayed from other nodes
110                 # note that we cannot do any connections at this point
111                 $nref = Route::Node::get($origin);
112                 unless ($nref) {
113                         my $v = $thing->{user} ? undef : $thing->{pcv};
114                         $nref = Route::Node->new($origin, $v, 1);
115                         push @{$thing->{pc19n}}, $nref;
116                         $nref->np(1);
117                 }
118         }
119
120         # handle "User"
121         if (my $user = $thing->{user}) {
122                 my $ur = Route::get($user);
123                 unless ($ur) {
124                         my @ref;
125                         my $uref = DXUser->get_current($user) || Thingy::Rt::_upd_user_rec($user, $origin)->put;
126                         if ($uref->is_node || $uref->is_aranea) {
127                             push @ref, $nref->add($user, $thing->{pcv} || 0, $thing->{h} || 0);
128                                 push @{$thing->{pc19n}}, @ref if @ref;
129                                 do $_->np(1) for @ref;
130                         } else {
131                                 $thing->{pc16n} = $nref;
132                                 push @ref, $nref->add_user($user, $thing->{h} || 0);
133                                 $thing->{pc16u} = \@ref if @ref;
134                         }
135                 }
136         } else {
137                 $nref->version($v) unless $nref->version;
138                 $nref->build($thing->{b}) unless $nref->build;
139                 $nref->sw($thing->{sw}) unless $nref->sw;
140                 $nref->here($thing->{h}) if exists $thing->{h};
141         }
142
143         RouteDB::update($origin, $node, $thing->{hopsaway});
144         RouteDB::update($thing->{user}, $node, $thing->{hopsaway}) if $thing->{user};
145         
146         delete $thing->{pc19n} unless @{$thing->{pc19n}};
147         
148         $thing->broadcast($dxchan);
149 }
150
151 1;