2 # The new protocol for real at last
6 # Copyright (c) 2005 Dirk Koopman G1TLH
20 use Time::HiRes qw(gettimeofday tv_interval);
29 use vars qw($VERSION $BRANCH);
30 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
31 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
32 $main::build += $VERSION;
33 $main::branch += $BRANCH;
35 use vars qw(@ISA $ntpflag $dupeage);
39 $ntpflag = 0; # should be set in startup if NTP in use
40 $dupeage = 12*60*60; # duplicates stored half a day
53 my $self = DXChannel::alloc(@_);
55 # add this node to the table, the values get filled in later
58 $main::routeroot->add($call, '5251', Route::here(1)) if $call ne $main::mycall;
59 $self->{'sort'} = 'W';
65 my ($self, $line, $sort) = @_;
66 my $call = $self->{call};
67 my $user = $self->{user};
70 my $host = $self->{conn}->{peerhost} || "unknown";
71 Log('Aranea', "$call connected from $host");
73 # remember type of connection
74 $self->{consort} = $line;
75 $self->{outbound} = $sort eq 'O';
76 my $priv = $user->priv;
77 $priv = $user->priv(1) unless $priv;
78 $self->{priv} = $priv; # other clusters can always be 'normal' users
79 $self->{lang} = $user->lang || 'en';
80 $self->{consort} = $line; # save the connection type
84 # sort out registration
85 $self->{registered} = 1;
87 # get the output filters
88 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
89 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
90 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
91 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
92 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
95 # get the INPUT filters (these only pertain to Clusters)
96 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
97 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
98 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
99 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
100 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
102 $self->conn->echo(0) if $self->conn->can('echo');
104 # ping neighbour node stuff
105 my $ping = $user->pingint;
106 $ping = $DXProt::pingint unless defined $ping;
107 $self->{pingint} = $ping;
108 $self->{nopings} = $user->nopings || $DXProt::obscount;
109 $self->{pingtime} = [ ];
110 $self->{pingave} = 999;
111 $self->{metric} ||= 100;
112 $self->{lastping} = $main::systime;
114 $self->state('normal');
115 $self->{pc50_t} = $main::systime;
117 # send info to all logged in thingies
118 $self->tell_login('loginn');
120 # run a script send the output to the debug file
121 my $script = new Script(lc $call) || new Script('node_default');
122 $script->run($self) if $script;
126 # This is the normal despatcher
130 my ($self, $line) = @_;
131 my $thing = input($line);
132 $thing->queue($self) if $thing;
136 # periodic processing
143 my $d = (gmtime($main::systime))[3];
146 $daystart = $main::systime - ($main::systime % 86400);
153 my $call = $self->call;
155 return if $self->{disconnecting}++;
157 # get rid of any PC16/17/19
158 # eph_del_regex("^PC1[679]*$call");
160 # do routing stuff, remove me from routing table
161 my $node = Route::Node::get($call);
164 @rout = $node->del($main::routeroot);
166 # and all my ephemera as well
169 # eph_del_regex("^PC1[679].*$c");
173 RouteDB::delete_interface($call);
175 # unbusy and stop and outgoing mail
176 my $mref = DXMsg::get_busy($call);
177 $mref->stop_msg($call) if $mref;
179 # broadcast to all other nodes that all the nodes connected to via me are gone
180 # $self->route_pc21($main::mycall, undef, @rout) if @rout;
182 # remove outstanding pings
183 # delete $pings{$call};
185 # I was the last node visited
186 $self->user->node($main::mycall);
188 # send info to all logged in thingies
189 $self->tell_login('logoutn');
191 Log('Aranea', $call . " Disconnected");
193 $self->SUPER::disconnect;
197 # generate new header (this is a general subroutine, not a method
198 # because it has to be used before a channel is fully initialised).
207 my $date = ((($dayno << 1) | $ntpflag) << 18) | ($main::systime % 86400);
208 my $r = "$mycall,$to," . sprintf('%6X%04X,0', $date, $seqno);
209 $r .= ",$from" if $from;
211 $seqno = 0 if $seqno > 0x0ffff;
216 # decode the date time sequence group
222 my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
223 my $secs = $dt & 0x3FFFF;
228 if ($dayno == $day) {
229 $t = $daystart + $secs;
230 } elsif ($dayno < $day) {
231 $t = $daystart + (($day-$dayno) * 86400) + $secs;
233 $t = $daystart + (($dayno-$day) * 86400) + $secs;
235 return ($t, $seqno, $ntp);
238 # subroutines to encode and decode values in lists
242 $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
243 $s = "'$s'" if $s =~ / /;
250 $s =~ s/^'(.*)'$/$1/;
251 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
259 my $head = genheader($thing->{origin},
260 ($thing->{group} || $thing->{touser} || $thing->{tonode}),
261 ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
266 my $v = tencode(shift);
270 return "$head|$data";
278 return ('I', $self->{call}, $line);
284 my ($head, $data) = split /\|/, $line, 2;
285 return unless $head && $data;
287 my ($origin, $group, $dts, $hop, $user) = split /,/, $head;
288 return if DXDupe::check("Ara,$origin,$dts", $dupeage);
290 $err .= "incomplete header," unless $origin && defined $group && $dts && defined $hop;
291 my ($cmd, $rdata) = split /,/, $data, 2;
293 # validate it further
294 $err .= "missing cmd or data," unless $cmd && $data;
295 $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
296 $err .= "invalid group ($group)," unless $group =~ /^[-A-Z0-9\/:]{2,}$/;
298 my $class = 'Thingy::' . ucfirst(lc $cmd);
300 my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
301 $err .= "invalid date/seq," unless $t;
305 dbg("Aranea input: $err");
306 } elsif ($class->can('new')) {
307 # create the appropriate Thingy
308 $thing = $class->new();
310 # reconstitute the header but wth hop increased by one
311 $head = join(',', $origin, $group, $dts, ++$hop);
312 $head .= ",$user" if $user;
313 $thing->{Aranea} = "$head|$data";
316 $thing->{origin} = $origin;
317 ($thing->{group}, $thing->{touser}) = split /:/, $group, 2;
319 $thing->{user} = $user if $user;
320 $thing->{hopsaway} = $hop;
322 for (split(/,/, $rdata)) {
324 my ($k,$v) = split /=/, $_, 2;
325 $thing->{$k} = tdecode($v);
331 # post process the thing, this generally adds on semantic meaning
332 # does parameter checking etc. It also adds / prepares the thingy so
333 # this is compatible with older protocol and arranges data so
334 # that the filtering can still work.
335 if ($thing->can('from_Aranea')) {
337 # if a thing is ok then return that thing, otherwise return
339 $thing = $thing->from_Aranea;
345 # this is the DXChannel send
346 # note that this does NOT send out stuff in same way as other DXChannels
347 # it is just as it comes, no extra bits added (here)
348 sub send # this is always later and always data
351 my $conn = $self->{conn};
353 my $call = $self->{call};
357 my @lines = split /\n/;
359 $conn->send_later($_);
360 dbg("-> D $call $_") if isdbg('chan');
363 $self->{t} = $main::systime;
367 # load of dummies for DXChannel broadcasts
368 # these will go away in time?
369 # These are all from PC protocol
379 if ($self->{spotsfilter}) {
380 ($filter, $hops) = $self->{spotsfilter}->it(@_);
381 return unless $filter;
383 # send_prot_line($self, $filter, $hops, $isolate, $line);
393 if ($self->{wwvfilter}) {
394 ($filter, $hops) = $self->{wwvfilter}->it(@_);
395 return unless $filter;
397 # send_prot_line($self, $filter, $hops, $isolate, $line)
407 if ($self->{wcyfilter}) {
408 ($filter, $hops) = $self->{wcyfilter}->it(@_);
409 return unless $filter;
411 # send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
424 if ($self->{annfilter}) {
425 ($filter, $hops) = $self->{annfilter}->it(@_);
426 return unless $filter;
428 # send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;