chase down version errors
[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                 $thing->{pcv} = $v;
71         }
72         
73         # verify authenticity
74         if ($node eq $origin) {
75
76                 # for directly connected calls
77                 if ($verify_on_login && !$thing->{user}) {
78                         my $pp = $dxchan->user->passphrase;
79                         unless ($pp) {
80                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin has no passphrase");
81                                 $dxchan->disconnect;
82                                 return;
83                         }
84                         my $auth = Verify->new("DXSp,$origin,$thing->{s},$thing->{v},$thing->{b}");
85                         unless ($auth->verify($thing->{auth}, $dxchan->user->passphrase)) {
86                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin failed auth check");
87                                 $dxchan->disconnect;
88                                 return;
89                         }
90                 }
91                 if ($dxchan->{state} ne 'normal') {
92                         $nref = $main::routeroot->add($origin, $thing->{pcv}, $thing->{h});
93                         push @{$thing->{pc19n}}, $nref if $nref;
94                         $dxchan->start($dxchan->{conn}->{csort}, $dxchan->{conn}->{outbound} ? 'O' : 'A');
95                         if ($dxchan->{outbound}) {
96                                 my $thing = Thingy::Hello->new();
97                                 $thing->send($dxchan);
98
99                                 # broadcast our configuration to the world
100                                 $thing = Thingy::Rt->new_cf;
101                                 $thing->broadcast;
102                         }
103                 }
104                 $nref = Route::Node::get($origin);
105                 $nref->np(1);
106         } else {
107                 
108                 # for otherwise connected calls, that come in relayed from other nodes
109                 # note that we cannot do any connections at this point
110                 $nref = Route::Node::get($origin);
111                 unless ($nref) {
112                         my $v = $thing->{user} ? undef : $thing->{pcv};
113                         $nref = Route::Node->new($origin, $v, 1);
114                         push @{$thing->{pc19n}}, $nref;
115                         $nref->np(1);
116                 }
117         }
118
119         # handle "User"
120         if (my $user = $thing->{user}) {
121                 my $ur = Route::get($user);
122                 unless ($ur) {
123                         my @ref;
124                         my $uref = DXUser->get_current($user) || Thingy::Rt::_upd_user_rec($user, $origin)->put;
125                         if ($uref->is_node || $uref->is_aranea) {
126                             push @ref, $nref->add($user, $thing->{pcv} || 0, $thing->{h} || 0);
127                                 push @{$thing->{pc19n}}, @ref if @ref;
128                                 do $_->np(1) for @ref;
129                         } else {
130                                 $thing->{pc16n} = $nref;
131                                 push @ref, $nref->add_user($user, $thing->{h} || 0);
132                                 $thing->{pc16u} = \@ref if @ref;
133                         }
134                 }
135         } else {
136                 $nref->version($v) unless $nref->version;
137                 $nref->build($thing->{b}) unless $nref->build;
138                 $nref->sw($thing->{sw}) unless $nref->sw;
139                 $nref->here($thing->{h}) if exists $thing->{h};
140         }
141
142         RouteDB::update($origin, $node, $thing->{hopsaway});
143         RouteDB::update($thing->{user}, $node, $thing->{hopsaway}) if $thing->{user};
144         
145         delete $thing->{pc19n} unless @{$thing->{pc19n}};
146         
147         $thing->broadcast($dxchan);
148 }
149
150 1;