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);
32 main::mkver($VERSION = q$Revision$);
34 use vars qw(@ISA $ntpflag $dupeage);
38 $ntpflag = 0; # should be set in startup if NTP in use
39 $dupeage = 12*60*60; # duplicates stored half a day
52 my $self = DXChannel::alloc(@_);
54 # add this node to the table, the values get filled in later
57 $main::routeroot->add($call, '5251', Route::here(1)) if $call ne $main::mycall;
58 $self->{'sort'} = 'W';
64 my ($self, $line, $sort) = @_;
65 my $call = $self->{call};
66 my $user = $self->{user};
69 my $host = $self->{conn}->{peerhost} || "unknown";
70 Log('Aranea', "$call connected from $host");
72 # remember type of connection
73 $self->{consort} = $line;
74 $self->{outbound} = $sort eq 'O';
75 my $priv = $user->priv;
76 $priv = $user->priv(1) unless $priv;
77 $self->{priv} = $priv; # other clusters can always be 'normal' users
78 $self->{lang} = $user->lang || 'en';
79 $self->{consort} = $line; # save the connection type
83 # sort out registration
84 $self->{registered} = 1;
86 # get the output filters
87 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
88 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
89 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
90 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
91 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
94 # get the INPUT filters (these only pertain to Clusters)
95 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
96 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
97 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
98 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
99 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
101 $self->conn->echo(0) if $self->conn->can('echo');
103 # ping neighbour node stuff
104 my $ping = $user->pingint;
105 $ping = $DXProt::pingint unless defined $ping;
106 $self->{pingint} = $ping;
107 $self->{nopings} = $user->nopings || $DXProt::obscount;
108 $self->{pingtime} = [ ];
109 $self->{pingave} = 999;
110 $self->{metric} ||= 100;
111 $self->{lastping} = $main::systime;
113 $self->state('normal');
114 $self->{pc50_t} = $main::systime;
116 # send info to all logged in thingies
117 $self->tell_login('loginn');
119 # run a script send the output to the debug file
120 my $script = new Script(lc $call) || new Script('node_default');
121 $script->run($self) if $script;
125 # This is the normal despatcher
129 my ($self, $line) = @_;
130 my $thing = input($line);
131 $thing->queue($self) if $thing;
135 # periodic processing
142 my $d = (gmtime($main::systime))[3];
145 $daystart = $main::systime - ($main::systime % 86400);
152 my $call = $self->call;
154 return if $self->{disconnecting}++;
156 my $thing = Thingy::Bye->new(origin=>$main::mycall, user=>$call);
157 $thing->process($self);
159 # get rid of any PC16/17/19
160 eph_del_regex("^PC1[679]*$call");
162 # do routing stuff, remove me from routing table
163 my $node = Route::Node::get($call);
166 @rout = $node->del($main::routeroot);
168 # and all my ephemera as well
171 eph_del_regex("^PC1[679].*$c");
175 RouteDB::delete_interface($call);
177 # unbusy and stop and outgoing mail
178 my $mref = DXMsg::get_busy($call);
179 $mref->stop_msg($call) if $mref;
181 # broadcast to all other nodes that all the nodes connected to via me are gone
182 # $self->route_pc21($main::mycall, undef, @rout) if @rout;
184 # remove outstanding pings
185 # delete $pings{$call};
187 # I was the last node visited
188 $self->user->node($main::mycall);
190 # send info to all logged in thingies
191 $self->tell_login('logoutn');
193 Log('Aranea', $call . " Disconnected");
195 $self->SUPER::disconnect;
199 # generate new header (this is a general subroutine, not a method
200 # because it has to be used before a channel is fully initialised).
211 my $s = "$mycall,$dts,$hop";
212 $s .= ",$user" if $user;
214 $s .= "," unless $user;
215 $s .= ",$group" if $group;
226 my $date = ((($dayno << 1) | $ntpflag) << 18) | ($main::systime % 86400);
227 my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to);
229 $seqno = 0 if $seqno > 0x0ffff;
234 # decode the date time sequence group
240 my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
241 my $secs = $dt & 0x3FFFF;
246 if ($dayno == $day) {
247 $t = $daystart + $secs;
248 } elsif ($dayno < $day) {
249 $t = $daystart + (($day-$dayno) * 86400) + $secs;
251 $t = $daystart + (($dayno-$day) * 86400) + $secs;
253 return ($t, $seqno, $ntp);
256 # subroutines to encode and decode values in lists
260 $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
261 $s = "'$s'" if $s =~ / /;
268 $s =~ s/^'(.*)'$/$1/;
269 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
277 my $head = genheader($thing->{origin},
278 ($thing->{group} || $thing->{touser} || $thing->{tonode}),
279 ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
284 my $v = tencode(shift);
288 return "$head|$data";
296 return ('I', $self->{call}, $line);
302 my ($head, $data) = split /\|/, $line, 2;
303 return unless $head && $data;
305 my ($origin, $dts, $hop, $user, $group) = split /,/, $head;
306 return if DXDupe::check("Ara,$origin,$dts", $dupeage);
308 $err .= "incomplete header," unless $origin && $dts && defined $hop;
309 my ($cmd, $rdata) = split /,/, $data, 2;
311 # validate it further
312 $err .= "missing cmd or data," unless $cmd && $data;
313 $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
314 my ($gp, $tus) = split /:/, $group, 2 if $group;
316 $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
317 $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
318 $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
320 my $class = 'Thingy::' . ucfirst(lc $cmd);
322 my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
323 $err .= "invalid date/seq," unless $t;
327 dbg("Aranea input: $err");
328 } elsif ($class->can('new')) {
329 # create the appropriate Thingy
330 $thing = $class->new();
332 # reconstitute the header but wth hop increased by one
333 $head = formathead($origin, $dts, ++$hop, $user, $group);
334 $thing->{Aranea} = "$head|$data";
337 $thing->{origin} = $origin;
339 $thing->{group} = $gp if $gp;
340 $thing->{touser} = $tus if $tus;
341 $thing->{user} = $user if $user;
342 $thing->{hopsaway} = $hop;
345 for (split(/,/, $rdata)) {
347 my ($k,$v) = split /=/, $_, 2;
348 $thing->{$k} = tdecode($v);
355 # post process the thing, this generally adds on semantic meaning
356 # does parameter checking etc. It also adds / prepares the thingy so
357 # this is compatible with older protocol and arranges data so
358 # that the filtering can still work.
359 if ($thing->can('from_Aranea')) {
361 # if a thing is ok then return that thing, otherwise return
363 $thing = $thing->from_Aranea;
369 # this is the DXChannel send
370 # note that this does NOT send out stuff in same way as other DXChannels
371 # it is just as it comes, no extra bits added (here)
372 sub send # this is always later and always data
375 my $conn = $self->{conn};
377 my $call = $self->{call};
381 my @lines = split /\n/;
383 $conn->send_later($_);
384 dbg("-> D $call $_") if isdbg('chan');
387 $self->{t} = $main::systime;
391 # load of dummies for DXChannel broadcasts
392 # these will go away in time?
393 # These are all from PC protocol
403 if ($self->{spotsfilter}) {
404 ($filter, $hops) = $self->{spotsfilter}->it(@_);
405 return unless $filter;
407 # send_prot_line($self, $filter, $hops, $isolate, $line);
417 if ($self->{wwvfilter}) {
418 ($filter, $hops) = $self->{wwvfilter}->it(@_);
419 return unless $filter;
421 # send_prot_line($self, $filter, $hops, $isolate, $line)
431 if ($self->{wcyfilter}) {
432 ($filter, $hops) = $self->{wcyfilter}->it(@_);
433 return unless $filter;
435 # send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
448 if ($self->{annfilter}) {
449 ($filter, $hops) = $self->{annfilter}->it(@_);
450 return unless $filter;
452 # send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;