2 # The new protocol for real at last
6 # Copyright (c) 2005 Dirk Koopman G1TLH
20 use Time::HiRes qw(gettimeofday tv_interval);
35 use vars qw($VERSION $BRANCH);
37 main::mkver($VERSION = q$Revision$);
39 use vars qw(@ISA $ntpflag $dupeage);
43 $ntpflag = 0; # should be set in startup if NTP in use
44 $dupeage = 12*60*60; # duplicates stored half a day
57 my $self = DXChannel::alloc(@_);
59 # add this node to the table, the values get filled in later
62 $self->{'sort'} = 'W';
68 my ($self, $line, $sort) = @_;
69 my $call = $self->{call};
70 my $user = $self->{user};
73 my $host = $self->{conn}->{peerhost} || "unknown";
74 Log('Aranea', "$call connected from $host");
76 # remember type of connection
77 $self->{consort} = $line;
78 $self->{outbound} = $sort eq 'O';
79 my $priv = $user->priv;
80 $priv = $user->priv(1) unless $priv;
81 $self->{priv} = $priv; # other clusters can always be 'normal' users
82 $self->{lang} = $user->lang || 'en';
83 $self->{consort} = $line; # save the connection type
87 # sort out registration
88 $self->{registered} = 1;
90 # get the output filters
91 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
92 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
93 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
94 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
95 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
98 # get the INPUT filters (these only pertain to Clusters)
99 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
100 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
101 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
102 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
103 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
105 $self->conn->echo(0) if $self->conn->can('echo');
107 # ping neighbour node stuff
108 my $ping = $user->pingint;
109 $ping = $DXProt::pingint unless defined $ping;
110 $self->{pingint} = $ping;
111 $self->{nopings} = $user->nopings || $DXProt::obscount;
112 $self->{pingtime} = [ ];
113 $self->{pingave} = 999;
114 $self->{metric} ||= 100;
115 $self->{lastping} = $main::systime;
117 $self->state('normal');
118 $self->{pc50_t} = $main::systime;
120 # send info to all logged in thingies
121 $self->tell_login('loginn');
123 # broadcast our configuration to the world
124 unless ($self->{outbound}) {
125 my $thing = Thingy::Rt->new_lcf;
129 # run a script send the output to the debug file
130 my $script = new Script(lc $call) || new Script('node_default');
131 $script->run($self) if $script;
135 # This is the normal despatcher
139 my ($self, $line) = @_;
140 my $thing = input($line);
141 $thing->queue($self) if $thing;
145 # periodic processing
152 my $d = (gmtime($main::systime))[3];
155 $daystart = $main::systime - ($main::systime % 86400);
162 my $call = $self->call;
164 return if $self->{disconnecting}++;
166 my $thing = Thingy::Bye->new(origin=>$main::mycall, user=>$call);
167 $thing->broadcast($self);
169 # get rid of any PC16/17/19
170 DXProt::eph_del_regex("^PC1[679]*$call");
172 # do routing stuff, remove me from routing table
173 my $node = Route::Node::get($call);
176 @rout = $node->del($main::routeroot);
178 # and all my ephemera as well
181 DXProt::eph_del_regex("^PC1[679].*$c");
185 RouteDB::delete_interface($call);
187 # unbusy and stop and outgoing mail
188 my $mref = DXMsg::get_busy($call);
189 $mref->stop_msg($call) if $mref;
191 # broadcast to all other nodes that all the nodes connected to via me are gone
192 DXProt::route_pc21($self, $main::mycall, undef, @rout) if @rout;
194 # remove outstanding pings
195 # delete $pings{$call};
197 # I was the last node visited
198 $self->user->node($main::mycall);
200 # send info to all logged in thingies
201 $self->tell_login('logoutn');
203 Log('Aranea', $call . " Disconnected");
205 $self->SUPER::disconnect;
209 # generate new header (this is a general subroutine, not a method
210 # because it has to be used before a channel is fully initialised).
221 my $s = "$mycall,$dts,$hop";
222 $s .= ",$user" if $user;
224 $s .= "," unless $user;
225 $s .= ",$group" if $group;
236 my $date = ((($dayno << 1) | $ntpflag) << 18) | ($main::systime % 86400);
237 my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to);
239 $seqno = 0 if $seqno > 0x0ffff;
244 # decode the date time sequence group
250 my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
251 my $secs = $dt & 0x3FFFF;
256 if ($dayno == $day) {
257 $t = $daystart + $secs;
258 } elsif ($dayno < $day) {
259 $t = $daystart + (($day-$dayno) * 86400) + $secs;
261 $t = $daystart + (($dayno-$day) * 86400) + $secs;
263 return ($t, $seqno, $ntp);
266 # subroutines to encode and decode values in lists
270 $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
271 $s = "'$s'" if $s =~ / /;
278 $s =~ s/^'(.*)'$/$1/;
279 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
286 my $list = ref $_[0] ? shift : \@_;
287 my ($name) = uc ref $thing;
290 my $head = genheader($thing->{origin},
291 ($thing->{group} || $thing->{touser} || $thing->{tonode}),
292 ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
295 my $data = uc $name . ',';
297 my $k = lc shift @$list;
298 my $v = $thing->{$k};
299 $data .= "$k=" . tencode($v) . ',' if defined $v;
302 return "$head|$data";
310 return ('I', $self->{call}, $line);
316 my ($head, $data) = split /\|/, $line, 2;
317 return unless $head && $data;
319 my ($origin, $dts, $hop, $user, $group) = split /,/, $head;
320 return if DXDupe::check("Ara,$origin,$dts", $dupeage);
322 $err .= "incomplete header," unless $origin && $dts && defined $hop;
323 my ($cmd, $rdata) = split /,/, $data, 2;
325 # validate it further
326 $err .= "missing cmd or data," unless $cmd && $data;
327 $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
328 my ($gp, $tus) = split /:/, $group, 2 if $group;
330 $err .= "from me," if $origin eq $main::mycall;
331 $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
332 $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
333 $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
335 my $class = 'Thingy::' . ucfirst(lc $cmd);
337 my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
338 $err .= "invalid date/seq," unless $t;
342 dbg("Aranea input: $err");
343 } elsif ($class->can('new')) {
344 # create the appropriate Thingy
345 $thing = $class->new();
347 # reconstitute the header but wth hop increased by one
348 $head = formathead($origin, $dts, ++$hop, $user, $group);
349 $thing->{Aranea} = "$head|$data";
352 $thing->{origin} = $origin;
354 $thing->{group} = $gp if $gp;
355 $thing->{touser} = $tus if $tus;
356 $thing->{user} = $user if $user;
357 $thing->{hopsaway} = $hop;
360 for (split(/,/, $rdata)) {
362 my ($k,$v) = split /=/, $_, 2;
363 $thing->{$k} = tdecode($v);
370 # post process the thing, this generally adds on semantic meaning
371 # does parameter checking etc. It also adds / prepares the thingy so
372 # this is compatible with older protocol and arranges data so
373 # that the filtering can still work.
374 if ($thing->can('from_Aranea')) {
376 # if a thing is ok then return that thing, otherwise return
378 $thing = $thing->from_Aranea;
384 # this is the DXChannel send
385 # note that this does NOT send out stuff in same way as other DXChannels
386 # it is just as it comes, no extra bits added (here)
387 sub send # this is always later and always data
390 my $conn = $self->{conn};
392 my $call = $self->{call};
396 my @lines = split /\n/;
398 $conn->send_later($_);
399 dbg("-> D $call $_") if isdbg('chan');
402 $self->{t} = $main::systime;
406 # load of dummies for DXChannel broadcasts
407 # these will go away in time?
408 # These are all from PC protocol
418 if ($self->{spotsfilter}) {
419 ($filter, $hops) = $self->{spotsfilter}->it(@_);
420 return unless $filter;
422 # send_prot_line($self, $filter, $hops, $isolate, $line);
432 if ($self->{wwvfilter}) {
433 ($filter, $hops) = $self->{wwvfilter}->it(@_);
434 return unless $filter;
436 # send_prot_line($self, $filter, $hops, $isolate, $line)
446 if ($self->{wcyfilter}) {
447 ($filter, $hops) = $self->{wcyfilter}->it(@_);
448 return unless $filter;
450 # send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
463 if ($self->{annfilter}) {
464 ($filter, $hops) = $self->{annfilter}->it(@_);
465 return unless $filter;
467 # send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;