2 # The new protocol for real at last
6 # Copyright (c) 2005 Dirk Koopman G1TLH
20 use Time::HiRes qw(gettimeofday tv_interval);
30 use vars qw($VERSION $BRANCH);
31 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
32 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /^\d+\.\d+(?:\.(\d+)\.(\d+))?$/ || (0,0));
33 $main::build += $VERSION;
34 $main::branch += $BRANCH;
36 use vars qw(@ISA $ntpflag $dupeage);
40 $ntpflag = 0; # should be set in startup if NTP in use
41 $dupeage = 12*60*60; # duplicates stored half a day
54 my $self = DXChannel::alloc(@_);
56 # add this node to the table, the values get filled in later
59 $main::routeroot->add($call, '5251', Route::here(1)) if $call ne $main::mycall;
60 $self->{'sort'} = 'W';
66 my ($self, $line, $sort) = @_;
67 my $call = $self->{call};
68 my $user = $self->{user};
71 my $host = $self->{conn}->{peerhost} || "unknown";
72 Log('Aranea', "$call connected from $host");
74 # remember type of connection
75 $self->{consort} = $line;
76 $self->{outbound} = $sort eq 'O';
77 my $priv = $user->priv;
78 $priv = $user->priv(1) unless $priv;
79 $self->{priv} = $priv; # other clusters can always be 'normal' users
80 $self->{lang} = $user->lang || 'en';
81 $self->{consort} = $line; # save the connection type
85 # sort out registration
86 $self->{registered} = 1;
88 # get the output filters
89 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
90 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
91 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
92 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
93 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
96 # get the INPUT filters (these only pertain to Clusters)
97 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
98 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
99 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
100 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
101 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
103 $self->conn->echo(0) if $self->conn->can('echo');
105 # ping neighbour node stuff
106 my $ping = $user->pingint;
107 $ping = $DXProt::pingint unless defined $ping;
108 $self->{pingint} = $ping;
109 $self->{nopings} = $user->nopings || $DXProt::obscount;
110 $self->{pingtime} = [ ];
111 $self->{pingave} = 999;
112 $self->{metric} ||= 100;
113 $self->{lastping} = $main::systime;
115 $self->state('normal');
116 $self->{pc50_t} = $main::systime;
118 # send info to all logged in thingies
119 $self->tell_login('loginn');
121 # run a script send the output to the debug file
122 my $script = new Script(lc $call) || new Script('node_default');
123 $script->run($self) if $script;
127 # This is the normal despatcher
131 my ($self, $line) = @_;
132 my $thing = input($line);
133 $thing->queue($self) if $thing;
137 # periodic processing
144 my $d = (gmtime($main::systime))[3];
147 $daystart = $main::systime - ($main::systime % 86400);
154 my $call = $self->call;
156 return if $self->{disconnecting}++;
158 # get rid of any PC16/17/19
159 # eph_del_regex("^PC1[679]*$call");
161 # do routing stuff, remove me from routing table
162 my $node = Route::Node::get($call);
165 @rout = $node->del($main::routeroot);
167 # and all my ephemera as well
170 # eph_del_regex("^PC1[679].*$c");
174 RouteDB::delete_interface($call);
176 # unbusy and stop and outgoing mail
177 my $mref = DXMsg::get_busy($call);
178 $mref->stop_msg($call) if $mref;
180 # broadcast to all other nodes that all the nodes connected to via me are gone
181 # $self->route_pc21($main::mycall, undef, @rout) if @rout;
183 # remove outstanding pings
184 # delete $pings{$call};
186 # I was the last node visited
187 $self->user->node($main::mycall);
189 # send info to all logged in thingies
190 $self->tell_login('logoutn');
192 Log('Aranea', $call . " Disconnected");
194 $self->SUPER::disconnect;
198 # generate new header (this is a general subroutine, not a method
199 # because it has to be used before a channel is fully initialised).
208 my $date = ((($dayno << 1) | $ntpflag) << 18) | ($main::systime % 86400);
209 my $r = "$mycall," . sprintf('%6X%04X,0', $date, $seqno);
211 $r .= ",$from" if $from;
213 $seqno = 0 if $seqno > 0x0ffff;
218 # decode the date time sequence group
224 my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
225 my $secs = $dt & 0x3FFFF;
230 if ($dayno == $day) {
231 $t = $daystart + $secs;
232 } elsif ($dayno < $day) {
233 $t = $daystart + (($day-$dayno) * 86400) + $secs;
235 $t = $daystart + (($dayno-$day) * 86400) + $secs;
237 return ($t, $seqno, $ntp);
240 # subroutines to encode and decode values in lists
244 $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
245 $s = "'$s'" if $s =~ / /;
252 $s =~ s/^'(.*)'$/$1/;
253 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
261 my $head = genheader($thing->{origin},
262 ($thing->{group} || $thing->{touser} || $thing->{tonode}),
263 ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
268 my $v = tencode(shift);
272 return "$head|$data";
280 return ('I', $self->{call}, $line);
286 my ($head, $data) = split /\|/, $line, 2;
287 return unless $head && $data;
289 my ($origin, $dts, $hop, $group, $user) = split /,/, $head;
290 return if DXDupe::check("Ara,$origin,$dts", $dupeage);
292 $err .= "incomplete header," unless $origin && $dts && defined $hop;
293 my ($cmd, $rdata) = split /,/, $data, 2;
295 # validate it further
296 $err .= "missing cmd or data," unless $cmd && $data;
297 $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
298 my ($gp, $tus) = split /:/, $group, 2 if $group;
300 $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
301 $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
302 $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
304 my $class = 'Thingy::' . ucfirst(lc $cmd);
306 my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
307 $err .= "invalid date/seq," unless $t;
311 dbg("Aranea input: $err");
312 } elsif ($class->can('new')) {
313 # create the appropriate Thingy
314 $thing = $class->new();
316 # reconstitute the header but wth hop increased by one
317 $head = join(',', $origin, $dts, ++$hop);
318 $head .= ",$group" if $group;
319 $head .= ",$user" if $user;
320 $thing->{Aranea} = "$head|$data";
323 $thing->{origin} = $origin;
325 $thing->{group} = $gp if $gp;
326 $thing->{touser} = $tus if $tus;
327 $thing->{user} = $user if $user;
328 $thing->{hopsaway} = $hop;
330 for (split(/,/, $rdata)) {
332 my ($k,$v) = split /=/, $_, 2;
333 $thing->{$k} = tdecode($v);
339 # post process the thing, this generally adds on semantic meaning
340 # does parameter checking etc. It also adds / prepares the thingy so
341 # this is compatible with older protocol and arranges data so
342 # that the filtering can still work.
343 if ($thing->can('from_Aranea')) {
345 # if a thing is ok then return that thing, otherwise return
347 $thing = $thing->from_Aranea;
353 # this is the DXChannel send
354 # note that this does NOT send out stuff in same way as other DXChannels
355 # it is just as it comes, no extra bits added (here)
356 sub send # this is always later and always data
359 my $conn = $self->{conn};
361 my $call = $self->{call};
365 my @lines = split /\n/;
367 $conn->send_later($_);
368 dbg("-> D $call $_") if isdbg('chan');
371 $self->{t} = $main::systime;
375 # load of dummies for DXChannel broadcasts
376 # these will go away in time?
377 # These are all from PC protocol
387 if ($self->{spotsfilter}) {
388 ($filter, $hops) = $self->{spotsfilter}->it(@_);
389 return unless $filter;
391 # send_prot_line($self, $filter, $hops, $isolate, $line);
401 if ($self->{wwvfilter}) {
402 ($filter, $hops) = $self->{wwvfilter}->it(@_);
403 return unless $filter;
405 # send_prot_line($self, $filter, $hops, $isolate, $line)
415 if ($self->{wcyfilter}) {
416 ($filter, $hops) = $self->{wcyfilter}->it(@_);
417 return unless $filter;
419 # send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
432 if ($self->{annfilter}) {
433 ($filter, $hops) = $self->{annfilter}->it(@_);
434 return unless $filter;
436 # send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;