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 # get rid of any PC16/17/19
157 # eph_del_regex("^PC1[679]*$call");
159 # do routing stuff, remove me from routing table
160 my $node = Route::Node::get($call);
163 @rout = $node->del($main::routeroot);
165 # and all my ephemera as well
168 # eph_del_regex("^PC1[679].*$c");
172 RouteDB::delete_interface($call);
174 # unbusy and stop and outgoing mail
175 my $mref = DXMsg::get_busy($call);
176 $mref->stop_msg($call) if $mref;
178 # broadcast to all other nodes that all the nodes connected to via me are gone
179 # $self->route_pc21($main::mycall, undef, @rout) if @rout;
181 # remove outstanding pings
182 # delete $pings{$call};
184 # I was the last node visited
185 $self->user->node($main::mycall);
187 # send info to all logged in thingies
188 $self->tell_login('logoutn');
190 Log('Aranea', $call . " Disconnected");
192 $self->SUPER::disconnect;
196 # generate new header (this is a general subroutine, not a method
197 # because it has to be used before a channel is fully initialised).
206 my $date = ((($dayno << 1) | $ntpflag) << 18) | ($main::systime % 86400);
207 my $r = "$mycall," . sprintf('%6X%04X,0', $date, $seqno);
209 $r .= "," if $from && !$to;
210 $r .= ",$from" if $from;
212 $seqno = 0 if $seqno > 0x0ffff;
217 # decode the date time sequence group
223 my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
224 my $secs = $dt & 0x3FFFF;
229 if ($dayno == $day) {
230 $t = $daystart + $secs;
231 } elsif ($dayno < $day) {
232 $t = $daystart + (($day-$dayno) * 86400) + $secs;
234 $t = $daystart + (($dayno-$day) * 86400) + $secs;
236 return ($t, $seqno, $ntp);
239 # subroutines to encode and decode values in lists
243 $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
244 $s = "'$s'" if $s =~ / /;
251 $s =~ s/^'(.*)'$/$1/;
252 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
260 my $head = genheader($thing->{origin},
261 ($thing->{group} || $thing->{touser} || $thing->{tonode}),
262 ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
267 my $v = tencode(shift);
271 return "$head|$data";
279 return ('I', $self->{call}, $line);
285 my ($head, $data) = split /\|/, $line, 2;
286 return unless $head && $data;
288 my ($origin, $dts, $hop, $group, $user) = split /,/, $head;
289 return if DXDupe::check("Ara,$origin,$dts", $dupeage);
291 $err .= "incomplete header," unless $origin && $dts && defined $hop;
292 my ($cmd, $rdata) = split /,/, $data, 2;
294 # validate it further
295 $err .= "missing cmd or data," unless $cmd && $data;
296 $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
297 my ($gp, $tus) = split /:/, $group, 2 if $group;
299 $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
300 $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
301 $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
303 my $class = 'Thingy::' . ucfirst(lc $cmd);
305 my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
306 $err .= "invalid date/seq," unless $t;
310 dbg("Aranea input: $err");
311 } elsif ($class->can('new')) {
312 # create the appropriate Thingy
313 $thing = $class->new();
315 # reconstitute the header but wth hop increased by one
316 $head = join(',', $origin, $dts, ++$hop);
317 $head .= ",$group" if $group;
318 $head .= ",$user" if $user;
319 $thing->{Aranea} = "$head|$data";
322 $thing->{origin} = $origin;
324 $thing->{group} = $gp if $gp;
325 $thing->{touser} = $tus if $tus;
326 $thing->{user} = $user if $user;
327 $thing->{hopsaway} = $hop;
329 for (split(/,/, $rdata)) {
331 my ($k,$v) = split /=/, $_, 2;
332 $thing->{$k} = tdecode($v);
338 # post process the thing, this generally adds on semantic meaning
339 # does parameter checking etc. It also adds / prepares the thingy so
340 # this is compatible with older protocol and arranges data so
341 # that the filtering can still work.
342 if ($thing->can('from_Aranea')) {
344 # if a thing is ok then return that thing, otherwise return
346 $thing = $thing->from_Aranea;
352 # this is the DXChannel send
353 # note that this does NOT send out stuff in same way as other DXChannels
354 # it is just as it comes, no extra bits added (here)
355 sub send # this is always later and always data
358 my $conn = $self->{conn};
360 my $call = $self->{call};
364 my @lines = split /\n/;
366 $conn->send_later($_);
367 dbg("-> D $call $_") if isdbg('chan');
370 $self->{t} = $main::systime;
374 # load of dummies for DXChannel broadcasts
375 # these will go away in time?
376 # These are all from PC protocol
386 if ($self->{spotsfilter}) {
387 ($filter, $hops) = $self->{spotsfilter}->it(@_);
388 return unless $filter;
390 # send_prot_line($self, $filter, $hops, $isolate, $line);
400 if ($self->{wwvfilter}) {
401 ($filter, $hops) = $self->{wwvfilter}->it(@_);
402 return unless $filter;
404 # send_prot_line($self, $filter, $hops, $isolate, $line)
414 if ($self->{wcyfilter}) {
415 ($filter, $hops) = $self->{wcyfilter}->it(@_);
416 return unless $filter;
418 # send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
431 if ($self->{annfilter}) {
432 ($filter, $hops) = $self->{annfilter}->it(@_);
433 return unless $filter;
435 # send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;