- # check for sane parameters
- next if $ver < 5000; # only works with version 5 software
- next if length $call < 3; # min 3 letter callsigns
- DXNode->new($self, $call, $confmode, $here, $ver);
- }
- last SWITCH;
- }
-
- if ($pcno == 20) { # send local configuration
- $self->send_local_config();
- $self->send(pc22());
- return;
- }
-
- if ($pcno == 21) { # delete a cluster from the list
- my $call = uc $field[1];
- my $ref = DXCluster->get($call);
- $ref->del() if $ref;
- last SWITCH;
- }
-
- if ($pcno == 22) {last SWITCH;}
- if ($pcno == 23) {last SWITCH;}
-
- if ($pcno == 24) { # set here status
- my $call = uc $field[1];
- $call =~ s/-\d+//o;
- my $ref = DXCluster->get($call);
- $ref->here($field[2]) if $ref;
- last SWITCH;
- }
-
- if ($pcno == 25) {last SWITCH;}
- if ($pcno == 26) {last SWITCH;}
- if ($pcno == 27) {last SWITCH;}
- if ($pcno == 28) {last SWITCH;}
- if ($pcno == 29) {last SWITCH;}
- if ($pcno == 30) {last SWITCH;}
- if ($pcno == 31) {last SWITCH;}
- if ($pcno == 32) {last SWITCH;}
- if ($pcno == 33) {last SWITCH;}
- if ($pcno == 34) {last SWITCH;}
- if ($pcno == 35) {last SWITCH;}
- if ($pcno == 36) {last SWITCH;}
- if ($pcno == 37) {last SWITCH;}
- if ($pcno == 38) {last SWITCH;}
-
- if ($pcno == 39) { # incoming disconnect
- $self->disconnect();
- return;
- }
-
- if ($pcno == 40) {last SWITCH;}
- if ($pcno == 41) { # user info
- # add this station to the user database, if required
- my $user = DXUser->get_current($field[1]);
- $user = DXUser->new($field[1]) if !$user;
-
- if ($field[2] == 1) {
- $user->name($field[3]);
- } elsif ($field[2] == 2) {
- $user->qth($field[3]);
- } elsif ($field[2] == 3) {
- my ($latd, $latm, $latl, $longd, $longm, $longl) = split /\s+/, $field[3];
- $longd += ($longm/60);
- $longd = 0-$longd if (uc $longl) eq 'W';
- $user->long($longd);
- $latd += ($latm/60);
- $latd = 0-$latd if (uc $latl) eq 'S';
- $user->lat($latd);
- } elsif ($field[2] == 4) {
- $user->node($field[3]);
- }
- $user->put;
- last SWITCH;
- }
- if ($pcno == 42) {last SWITCH;}
- if ($pcno == 43) {last SWITCH;}
- if ($pcno == 44) {last SWITCH;}
- if ($pcno == 45) {last SWITCH;}
- if ($pcno == 46) {last SWITCH;}
- if ($pcno == 47) {last SWITCH;}
- if ($pcno == 48) {last SWITCH;}
- if ($pcno == 49) {last SWITCH;}
-
- if ($pcno == 50) { # keep alive/user list
- my $ref = DXCluster->get($field[1]);
- $ref->update_users($field[2]) if $ref;
- last SWITCH;
- }
-
- if ($pcno == 51) { # incoming ping requests/answers
-
- # is it for us?
- if ($field[1] eq $main::mycall) {
- my $flag = $field[3];
- $flag ^= 1;
- $self->send($self->pc51($field[2], $field[1], $flag));
- } else {
- # route down an appropriate thingy
- route($field[1], $line);
- }
- return;
- }
- }
-
- # if get here then rebroadcast the thing with its Hop count decremented (if
- # there is one). If it has a hop count and it decrements to zero then don't
- # rebroadcast it.
- #
- # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
- # REBROADCAST!!!!
- #
-
- my $hops;
- if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
- my $newhops = $hops - 1;
- if ($newhops > 0) {
- $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
- broadcast_ak1a($line, $self); # send it to everyone but me
- }
- }
+ if ($pcno == 20) { # send local configuration
+ $self->send_local_config();
+ $self->send(pc22());
+ $self->state('normal');
+ $self->{lastping} = 0;
+ return;
+ }
+
+ if ($pcno == 21) { # delete a cluster from the list
+ my $call = uc $field[1];
+
+ eph_del_regex("^PC1[79].*$call");
+
+ # if I get a PC21 from the same callsign as self then treat it
+ # as a PC39: I have gone away
+ if ($call eq $self->call) {
+ $self->disconnect(1);
+ return;
+ }
+
+ my @rout;
+ my $parent = Route::Node::get($self->{call});
+ unless ($parent) {
+ dbg("DXPROT: my parent $self->{call} has disappeared");
+ $self->disconnect;
+ return;
+ }
+ if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
+ my $node = Route::Node::get($call);
+ if ($node) {
+
+ my $dxchan = DXChannel->get($call);
+ if ($dxchan && $dxchan != $self) {
+ dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
+ return;
+ }
+
+ # input filter it
+ return unless $self->in_filter_route($node);
+
+ # routing objects
+ push @rout, $node->del($parent);
+ }
+ } else {
+ dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
+ return;
+ }
+
+# if (eph_dup($line)) {
+# dbg("PCPROT: dup PC21 detected") if isdbg('chanerr');
+# return;
+# }
+
+ $self->route_pc21(@rout) if @rout;
+ return;
+ }
+
+ if ($pcno == 22) {
+ $self->state('normal');
+ $self->{lastping} = 0;
+ return;
+ }
+
+ if ($pcno == 23 || $pcno == 27) { # WWV info
+
+ # route 'foreign' pc27s
+ if ($pcno == 27) {
+ if ($field[8] ne $main::mycall) {
+ $self->route($field[8], $line);
+ return;
+ }
+ }
+
+ return if $rspfcheck and !$self->rspfcheck(1, $field[8], $field[7]);
+
+ # do some de-duping
+ my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
+ my $sfi = unpad($field[3]);
+ my $k = unpad($field[4]);
+ my $i = unpad($field[5]);
+ my ($r) = $field[6] =~ /R=(\d+)/;
+ $r = 0 unless $r;
+ if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
+ dbg("PCPROT: WWV Date ($field[1] $field[2]) out of range") if isdbg('chanerr');
+ return;
+ }
+ if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
+ dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
+ return;
+ }
+ $field[7] =~ s/-\d+$//o; # remove spotter's ssid
+
+ my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
+
+ my $rep;
+ eval {
+ $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
+ };
+# dbg("Local::wwv2 error $@") if isdbg('local') if $@;
+ return if $rep;
+
+ # DON'T be silly and send on PC27s!
+ return if $pcno == 27;
+
+ # broadcast to the eager world
+ send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
+ return;
+ }
+
+ if ($pcno == 24) { # set here status
+ my $call = uc $field[1];
+ my ($nref, $uref);
+ $nref = Route::Node::get($call);
+ $uref = Route::User::get($call);
+ return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
+
+ unless (eph_dup($line)) {
+ $nref->here($field[2]) if $nref;
+ $uref->here($field[2]) if $uref;
+ my $ref = $nref || $uref;
+ return unless $self->in_filter_route($ref);
+ $self->route_pc24($ref, $field[3]);
+ }
+ return;
+ }
+
+ if ($pcno == 25) { # merge request
+ if ($field[1] ne $main::mycall) {
+ $self->route($field[1], $line);
+ return;
+ }
+ if ($field[2] eq $main::mycall) {
+ dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
+ return;
+ }
+
+ Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[2]");
+
+ # spots
+ if ($field[3] > 0) {
+ my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
+ my $in;
+ foreach $in (@in) {
+ $self->send(pc26(@{$in}[0..4], $field[2]));
+ }
+ }
+
+ # wwv
+ if ($field[4] > 0) {
+ my @in = reverse Geomag::search(0, $field[4], time, 1);
+ my $in;
+ foreach $in (@in) {
+ $self->send(pc27(@{$in}[0..5], $field[2]));
+ }
+ }
+ return;
+ }
+
+ if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
+ return if $pcno == 49 && eph_dup($line);
+ if ($pcno == 49 || $field[1] eq $main::mycall) {
+ DXMsg::process($self, $line);
+ } else {
+ $self->route($field[1], $line) unless $self->is_clx;
+ }
+ return;
+ }
+
+ if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
+ if (eph_dup($line, $eph_pc34_restime)) {
+ dbg("PCPROT: dupe") if isdbg('chanerr');
+ } else {
+ $self->process_rcmd($field[1], $field[2], $field[2], $field[3]);
+ }
+ return;
+ }
+
+ if ($pcno == 35) { # remote command replies
+ eph_del_regex("^PC35\\^$field[2]\\^$field[1]\\^");
+ $self->process_rcmd_reply($field[1], $field[2], $field[1], $field[3]);
+ return;
+ }
+
+ # for pc 37 see 44 onwards
+
+ if ($pcno == 38) { # node connected list from neighbour
+ return;
+ }
+
+ if ($pcno == 39) { # incoming disconnect
+ if ($field[1] eq $self->{call}) {
+ $self->disconnect(1);
+ } else {
+ dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
+ }
+ return;
+ }
+
+ if ($pcno == 41) { # user info
+ my $call = $field[1];
+
+ if (eph_dup($line, $eph_info_restime)) {
+ dbg("PCPROT: dupe") if isdbg('chanerr');
+ return;
+ }
+
+ # input filter if required
+# my $ref = Route::get($call) || Route->new($call);
+# return unless $self->in_filter_route($ref);
+
+ if ($field[3] eq $field[2] || $field[3] =~ /^\s*$/) {
+ dbg('PCPROT: invalid value') if isdbg('chanerr');
+ return;
+ }
+
+ # add this station to the user database, if required
+ my $user = DXUser->get_current($call);
+ $user = DXUser->new($call) if !$user;
+
+ if ($field[2] == 1) {
+ $user->name($field[3]);
+ } elsif ($field[2] == 2) {
+ $user->qth($field[3]);
+ } elsif ($field[2] == 3) {
+ if (is_latlong($field[3])) {
+ my ($lat, $long) = DXBearing::stoll($field[3]);
+ $user->lat($lat);
+ $user->long($long);
+ $user->qra(DXBearing::lltoqra($lat, $long));
+ } else {
+ dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
+ return;
+ }
+ } elsif ($field[2] == 4) {
+ $user->homenode($field[3]);
+ } elsif ($field[2] == 5) {
+ if (is_qra(uc $field[3])) {
+ my ($lat, $long) = DXBearing::qratoll(uc $field[3]);
+ $user->lat($lat);
+ $user->long($long);
+ $user->qra(uc $field[3]);
+ } else {
+ dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
+ return;
+ }
+ }
+ $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
+ $user->put;
+
+ unless ($self->{isolate}) {
+ DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
+ }
+
+# perhaps this IS what we want after all
+# $self->route_pc41($ref, $call, $field[2], $field[3], $field[4]);
+ return;
+ }
+
+ if ($pcno == 43) {
+ last SWITCH;
+ }
+
+ if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
+ DXDb::process($self, $line);
+ return;
+ }
+
+ if ($pcno == 50) { # keep alive/user list
+ my $call = $field[1];
+ my $node = Route::Node::get($call);
+ if ($node) {
+ return unless $node->call eq $self->{call};
+ $node->usercount($field[2]);
+
+ # input filter if required
+ return unless $self->in_filter_route($node);
+
+ $self->route_pc50($node, $field[2], $field[3]) unless eph_dup($line);
+ }
+ return;
+ }
+
+ if ($pcno == 51) { # incoming ping requests/answers
+ my $to = $field[1];
+ my $from = $field[2];
+ my $flag = $field[3];
+
+
+ # is it for us?
+ if ($to eq $main::mycall) {
+ if ($flag == 1) {
+ $self->send(pc51($from, $to, '0'));
+ } else {
+ # it's a reply, look in the ping list for this one
+ my $ref = $pings{$from};
+ if ($ref) {
+ my $tochan = DXChannel->get($from);
+ while (@$ref) {
+ my $r = shift @$ref;
+ my $dxchan = DXChannel->get($r->{call});
+ next unless $dxchan;
+ my $t = tv_interval($r->{t}, [ gettimeofday ]);
+ if ($dxchan->is_user) {
+ my $s = sprintf "%.2f", $t;
+ my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
+ $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
+ } elsif ($dxchan->is_node) {
+ if ($tochan) {
+ my $nopings = $tochan->user->nopings || 2;
+ push @{$tochan->{pingtime}}, $t;
+ shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
+
+ # cope with a missed ping, this means you must set the pingint large enough
+ if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
+ $t -= $tochan->{pingint};
+ }
+
+ # calc smoothed RTT a la TCP
+ if (@{$tochan->{pingtime}} == 1) {
+ $tochan->{pingave} = $t;
+ } else {
+ $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
+ }
+# my $st;
+# for (@{$tochan->{pingtime}}) {
+# $st += $_;
+# }
+# $tochan->{pingave} = $st / @{$tochan->{pingtime}};
+ $tochan->{nopings} = $nopings; # pump up the timer
+ }
+ }
+ }
+ }
+ }
+ } else {
+ if (eph_dup($line)) {
+ dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
+ return;
+ }
+ # route down an appropriate thingy
+ $self->route($to, $line);
+ }
+ return;
+ }
+
+ if ($pcno == 75) { # dunno but route it
+ my $call = $field[1];
+ if ($call ne $main::mycall) {
+ $self->route($call, $line);
+ }
+ return;
+ }
+
+ if ($pcno == 73) { # WCY broadcasts
+ my $call = $field[1];
+
+ # do some de-duping
+ my $d = cltounix($call, sprintf("%02d18Z", $field[2]));
+ if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
+ dbg("PCPROT: WCY Date ($call $field[2]) out of range") if isdbg('chanerr');
+ return;
+ }
+ @field = map { unpad($_) } @field;
+ if (WCY::dup($d)) {
+ dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
+ return;
+ }
+
+ my $wcy = WCY::update($d, @field[2..12]);
+
+ my $rep;
+ eval {
+ $rep = Local::wcy($self, @field[1..12]);
+ };
+ # dbg("Local::wcy error $@") if isdbg('local') if $@;
+ return if $rep;
+
+ # broadcast to the eager world
+ send_wcy_spot($self, $line, $d, @field[2..12]);
+ return;
+ }
+
+ if ($pcno == 84) { # remote commands (incoming)
+ $self->process_rcmd($field[1], $field[2], $field[3], $field[4]);
+ return;
+ }
+
+ if ($pcno == 85) { # remote command replies
+ $self->process_rcmd_reply($field[1], $field[2], $field[3], $field[4]);
+
+ return;
+ }
+ if ($pcno == 90) { # new style PC16,17,19,21
+ return;
+ }
+ }
+
+ # if get here then rebroadcast the thing with its Hop count decremented (if
+ # there is one). If it has a hop count and it decrements to zero then don't
+ # rebroadcast it.
+ #
+ # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
+ # REBROADCAST!!!!
+ #
+
+ if (eph_dup($line)) {
+ dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
+ } else {
+ unless ($self->{isolate}) {
+ DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
+ }
+ }