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 $cf_interval $hello_interval);
43 $ntpflag = 0; # should be set in startup if NTP in use
44 $dupeage = 12*60*60; # duplicates stored half a day
45 $cf_interval = 30*60; # interval between config broadcasts
46 $hello_interval = 3*60*60; # interval between hello broadcasts for me and local users
59 my $self = DXChannel::alloc(@_);
61 # add this node to the table, the values get filled in later
64 $self->{'sort'} = 'W';
70 my ($self, $line, $sort) = @_;
71 my $call = $self->{call};
72 my $user = $self->{user};
75 my $host = $self->{conn}->{peerhost} || "unknown";
76 Log('Aranea', "$call connected from $host");
78 # remember type of connection
79 $self->{consort} = $line;
80 $self->{outbound} = $sort eq 'O';
81 my $priv = $user->priv;
82 $priv = $user->priv(1) unless $priv;
83 $self->{priv} = $priv; # other clusters can always be 'normal' users
84 $self->{lang} = $user->lang || 'en';
85 $self->{consort} = $line; # save the connection type
89 # sort out registration
90 $self->{registered} = 1;
92 # get the output filters
93 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
94 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
95 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
96 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
97 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
100 # get the INPUT filters (these only pertain to Clusters)
101 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
102 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
103 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
104 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
105 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
107 $self->conn->echo(0) if $self->conn->can('echo');
109 # ping neighbour node stuff
110 my $ping = $user->pingint;
111 $ping = $DXProt::pingint unless defined $ping;
112 $self->{pingint} = $ping;
113 $self->{nopings} = $user->nopings || $DXProt::obscount;
114 $self->{pingtime} = [ ];
115 $self->{pingave} = 999;
116 $self->{metric} ||= 100;
117 $self->{lastping} = $main::systime;
119 $self->state('normal');
120 $self->{pc50_t} = $main::systime;
122 # send info to all logged in thingies
123 $self->tell_login('loginn');
125 # broadcast our configuration to the world
126 unless ($self->{outbound}) {
127 my $thing = Thingy::Rt->new_cf;
129 $self->lastcf($main::systime);
132 # run a script send the output to the debug file
133 my $script = new Script(lc $call) || new Script('node_default');
134 $script->run($self) if $script;
138 # This is the normal despatcher
142 my ($self, $line) = @_;
143 my $thing = input($line);
144 $thing->queue($self) if $thing;
148 # periodic processing (every second)
157 my $d = (gmtime($main::systime))[3];
160 $daystart = $main::systime - ($main::systime % 86400);
162 if ($main::systime >= $lastmin + 60) {
165 $lastmin = $main::systime;
172 # send hello and cf packages periodically
173 foreach my $dxchan (DXChannel::get_all()) {
174 next if $dxchan == $main::me;
175 next if $dxchan->is_aranea;
176 if ($main::systime >= $dxchan->lasthello + $hello_interval) {
177 my $thing = Thingy::Hello->new(user => $dxchan->call, h => $dxchan->here);
178 $thing->broadcast($dxchan);
179 $dxchan->lasthello($main::systime);
181 if ($dxchan->is_node) {
182 if ($main::systime >= $dxchan->lastcf + $cf_interval) {
183 my $call = $dxchan->call;
184 my $thing = Thingy::Rt->new(user => $call);
185 if (my $nref = Route::Node::get($call)) {
186 $thing->copy_pc16_data($nref);
187 $thing->broadcast($dxchan);
188 $dxchan->lastcf($main::systime);
190 dbg("Aranea::per_minute: Route::Node for $call disappeared");
201 my $call = $self->call;
203 return if $self->{disconnecting}++;
205 my $thing = Thingy::Bye->new(origin=>$main::mycall, user=>$call);
206 $thing->broadcast($self);
208 # get rid of any PC16/17/19
209 DXProt::eph_del_regex("^PC1[679]*$call");
211 # do routing stuff, remove me from routing table
212 my $node = Route::Node::get($call);
215 @rout = $node->del($main::routeroot);
217 # and all my ephemera as well
220 DXProt::eph_del_regex("^PC1[679].*$c");
224 RouteDB::delete_interface($call);
226 # unbusy and stop and outgoing mail
227 my $mref = DXMsg::get_busy($call);
228 $mref->stop_msg($call) if $mref;
230 # broadcast to all other nodes that all the nodes connected to via me are gone
231 DXProt::route_pc21($self, $main::mycall, undef, @rout) if @rout;
233 # remove outstanding pings
234 # delete $pings{$call};
236 # I was the last node visited
237 $self->user->node($main::mycall);
239 # send info to all logged in thingies
240 $self->tell_login('logoutn');
242 Log('Aranea', $call . " Disconnected");
244 $self->SUPER::disconnect;
248 # generate new header (this is a general subroutine, not a method
249 # because it has to be used before a channel is fully initialised).
260 my $s = "$mycall,$dts,$hop";
261 $s .= ",$user" if $user;
263 $s .= "," unless $user;
264 $s .= ",$group" if $group;
275 my $date = ((($dayno << 1) | $ntpflag) << 18) | ($main::systime % 86400);
276 my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to);
278 $seqno = 0 if $seqno > 0x0ffff;
283 # decode the date time sequence group
289 my ($dt, $seqno) = map {hex} unpack "A6 A4", $dts;
290 my $secs = $dt & 0x3FFFF;
295 if ($dayno == $day) {
296 $t = $daystart + $secs;
297 } elsif ($dayno < $day) {
298 $t = $daystart + (($day-$dayno) * 86400) + $secs;
300 $t = $daystart + (($dayno-$day) * 86400) + $secs;
302 return ($t, $seqno, $ntp);
305 # subroutines to encode and decode values in lists
309 $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
310 # $s = "'$s'" if $s =~ / /;
317 $s =~ s/^'(.*)'$/$1/;
318 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
319 return length $s ? $s : '';
325 my $list = ref $_[0] ? shift : \@_;
326 my ($name) = uc ref $thing;
329 my $head = genheader($thing->{origin},
330 ($thing->{group} || $thing->{touser} || $thing->{tonode}),
331 ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
334 my $data = uc $name . ',';
336 my $k = lc shift @$list;
337 my $v = $thing->{$k};
338 $data .= "$k=" . tencode($v) . ',' if defined $v;
341 return "$head|$data";
349 return ('I', $self->{call}, $line);
355 my ($head, $data) = split /\|/, $line, 2;
356 return unless $head && $data;
358 my ($origin, $dts, $hop, $user, $group) = split /,/, $head;
359 return if DXDupe::check("Ara,$origin,$dts", $dupeage);
361 $err .= "incomplete header," unless $origin && $dts && defined $hop;
362 my ($cmd, $rdata) = split /,/, $data, 2;
364 # validate it further
365 $err .= "missing cmd or data," unless $cmd && $data;
366 $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
367 my ($gp, $tus) = split /:/, $group, 2 if $group;
369 $err .= "from me," if $origin eq $main::mycall;
370 $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
371 $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
372 $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
374 my $class = 'Thingy::' . ucfirst(lc $cmd);
376 my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
377 dbg("dts: $dts = $ntp $t($main::systime) $seqno") if isdbg('dts');
378 $err .= "invalid date/seq," unless $t;
382 dbg("Aranea input: $err");
383 } elsif ($class->can('new')) {
384 # create the appropriate Thingy
385 $thing = $class->new();
387 # reconstitute the header but wth hop increased by one
388 $head = formathead($origin, $dts, ++$hop, $user, $group);
389 $thing->{Aranea} = "$head|$data";
392 $thing->{origin} = $origin;
394 $thing->{group} = $gp if $gp;
395 $thing->{touser} = $tus if $tus;
396 $thing->{user} = $user if $user;
397 $thing->{hopsaway} = $hop;
400 for (split(/,/, $rdata)) {
402 my ($k,$v) = split /=/, $_, 2;
403 $thing->{$k} = tdecode($v);
410 # post process the thing, this generally adds on semantic meaning
411 # does parameter checking etc. It also adds / prepares the thingy so
412 # this is compatible with older protocol and arranges data so
413 # that the filtering can still work.
414 if ($thing->can('from_Aranea')) {
416 # if a thing is ok then return that thing, otherwise return
418 $thing = $thing->from_Aranea;
424 # this is the DXChannel send
425 # note that this does NOT send out stuff in same way as other DXChannels
426 # it is just as it comes, no extra bits added (here)
427 sub send # this is always later and always data
430 my $conn = $self->{conn};
432 my $call = $self->{call};
436 my @lines = split /\n/;
438 $conn->send_later($_);
439 dbg("-> D $call $_") if isdbg('chan');
442 $self->{t} = $main::systime;
446 # load of dummies for DXChannel broadcasts
447 # these will go away in time?
448 # These are all from PC protocol
458 if ($self->{spotsfilter}) {
459 ($filter, $hops) = $self->{spotsfilter}->it(@_);
460 return unless $filter;
462 # send_prot_line($self, $filter, $hops, $isolate, $line);
472 if ($self->{wwvfilter}) {
473 ($filter, $hops) = $self->{wwvfilter}->it(@_);
474 return unless $filter;
476 # send_prot_line($self, $filter, $hops, $isolate, $line)
486 if ($self->{wcyfilter}) {
487 ($filter, $hops) = $self->{wcyfilter}->it(@_);
488 return unless $filter;
490 # send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
503 if ($self->{annfilter}) {
504 ($filter, $hops) = $self->{annfilter}->it(@_);
505 return unless $filter;
507 # send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;