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_cf;
127 $self->lastcf($main::systime);
130 # run a script send the output to the debug file
131 my $script = new Script(lc $call) || new Script('node_default');
132 $script->run($self) if $script;
136 # This is the normal despatcher
140 my ($self, $line) = @_;
141 my $thing = input($line);
142 $thing->queue($self) if $thing;
146 # periodic processing
153 my $d = (gmtime($main::systime))[3];
156 $daystart = $main::systime - ($main::systime % 86400);
163 my $call = $self->call;
165 return if $self->{disconnecting}++;
167 my $thing = Thingy::Bye->new(origin=>$main::mycall, user=>$call);
168 $thing->broadcast($self);
170 # get rid of any PC16/17/19
171 DXProt::eph_del_regex("^PC1[679]*$call");
173 # do routing stuff, remove me from routing table
174 my $node = Route::Node::get($call);
177 @rout = $node->del($main::routeroot);
179 # and all my ephemera as well
182 DXProt::eph_del_regex("^PC1[679].*$c");
186 RouteDB::delete_interface($call);
188 # unbusy and stop and outgoing mail
189 my $mref = DXMsg::get_busy($call);
190 $mref->stop_msg($call) if $mref;
192 # broadcast to all other nodes that all the nodes connected to via me are gone
193 DXProt::route_pc21($self, $main::mycall, undef, @rout) if @rout;
195 # remove outstanding pings
196 # delete $pings{$call};
198 # I was the last node visited
199 $self->user->node($main::mycall);
201 # send info to all logged in thingies
202 $self->tell_login('logoutn');
204 Log('Aranea', $call . " Disconnected");
206 $self->SUPER::disconnect;
210 # generate new header (this is a general subroutine, not a method
211 # because it has to be used before a channel is fully initialised).
222 my $s = "$mycall,$dts,$hop";
223 $s .= ",$user" if $user;
225 $s .= "," unless $user;
226 $s .= ",$group" if $group;
237 my $date = ((($dayno << 1) | $ntpflag) << 18) | ($main::systime % 86400);
238 my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to);
240 $seqno = 0 if $seqno > 0x0ffff;
245 # decode the date time sequence group
251 my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
252 my $secs = $dt & 0x3FFFF;
257 if ($dayno == $day) {
258 $t = $daystart + $secs;
259 } elsif ($dayno < $day) {
260 $t = $daystart + (($day-$dayno) * 86400) + $secs;
262 $t = $daystart + (($dayno-$day) * 86400) + $secs;
264 return ($t, $seqno, $ntp);
267 # subroutines to encode and decode values in lists
271 $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
272 $s = "'$s'" if $s =~ / /;
279 $s =~ s/^'(.*)'$/$1/;
280 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
287 my $list = ref $_[0] ? shift : \@_;
288 my ($name) = uc ref $thing;
291 my $head = genheader($thing->{origin},
292 ($thing->{group} || $thing->{touser} || $thing->{tonode}),
293 ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
296 my $data = uc $name . ',';
298 my $k = lc shift @$list;
299 my $v = $thing->{$k};
300 $data .= "$k=" . tencode($v) . ',' if defined $v;
303 return "$head|$data";
311 return ('I', $self->{call}, $line);
317 my ($head, $data) = split /\|/, $line, 2;
318 return unless $head && $data;
320 my ($origin, $dts, $hop, $user, $group) = split /,/, $head;
321 return if DXDupe::check("Ara,$origin,$dts", $dupeage);
323 $err .= "incomplete header," unless $origin && $dts && defined $hop;
324 my ($cmd, $rdata) = split /,/, $data, 2;
326 # validate it further
327 $err .= "missing cmd or data," unless $cmd && $data;
328 $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
329 my ($gp, $tus) = split /:/, $group, 2 if $group;
331 $err .= "from me," if $origin eq $main::mycall;
332 $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
333 $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
334 $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
336 my $class = 'Thingy::' . ucfirst(lc $cmd);
338 my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
339 $err .= "invalid date/seq," unless $t;
343 dbg("Aranea input: $err");
344 } elsif ($class->can('new')) {
345 # create the appropriate Thingy
346 $thing = $class->new();
348 # reconstitute the header but wth hop increased by one
349 $head = formathead($origin, $dts, ++$hop, $user, $group);
350 $thing->{Aranea} = "$head|$data";
353 $thing->{origin} = $origin;
355 $thing->{group} = $gp if $gp;
356 $thing->{touser} = $tus if $tus;
357 $thing->{user} = $user if $user;
358 $thing->{hopsaway} = $hop;
361 for (split(/,/, $rdata)) {
363 my ($k,$v) = split /=/, $_, 2;
364 $thing->{$k} = tdecode($v);
371 # post process the thing, this generally adds on semantic meaning
372 # does parameter checking etc. It also adds / prepares the thingy so
373 # this is compatible with older protocol and arranges data so
374 # that the filtering can still work.
375 if ($thing->can('from_Aranea')) {
377 # if a thing is ok then return that thing, otherwise return
379 $thing = $thing->from_Aranea;
385 # this is the DXChannel send
386 # note that this does NOT send out stuff in same way as other DXChannels
387 # it is just as it comes, no extra bits added (here)
388 sub send # this is always later and always data
391 my $conn = $self->{conn};
393 my $call = $self->{call};
397 my @lines = split /\n/;
399 $conn->send_later($_);
400 dbg("-> D $call $_") if isdbg('chan');
403 $self->{t} = $main::systime;
407 # load of dummies for DXChannel broadcasts
408 # these will go away in time?
409 # These are all from PC protocol
419 if ($self->{spotsfilter}) {
420 ($filter, $hops) = $self->{spotsfilter}->it(@_);
421 return unless $filter;
423 # send_prot_line($self, $filter, $hops, $isolate, $line);
433 if ($self->{wwvfilter}) {
434 ($filter, $hops) = $self->{wwvfilter}->it(@_);
435 return unless $filter;
437 # send_prot_line($self, $filter, $hops, $isolate, $line)
447 if ($self->{wcyfilter}) {
448 ($filter, $hops) = $self->{wcyfilter}->it(@_);
449 return unless $filter;
451 # send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
464 if ($self->{annfilter}) {
465 ($filter, $hops) = $self->{annfilter}->it(@_);
466 return unless $filter;
468 # send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;