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