many changes (see Changes)
[spider.git] / perl / DXProtHandle.pm
index 49c54734401670f054851d18ba65cefe6d9617d5..600bfd8f61db19163be7fdae8cb3da3de426d5fc 100644 (file)
@@ -49,7 +49,7 @@ use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restim
                        $eph_pc15_restime $pc9x_past_age $pc9x_dupe_age
                        $pc10_dupe_age $pc92_slug_changes $last_pc92_slug
                        $pc92Ain $pc92Cin $pc92Din $pc92Kin $pc9x_time_tolerance
-                       $pc92filterdef $senderverify $pc11_dwell_time
+                       $pc92filterdef $senderverify $pc11_dwell_time $pc11_extract_route $pc92_ad_enabled $pc92c_ipaddr_enable
                   );
 
 $pc9x_dupe_age = 60;                   # catch loops of circular (usually) D records
@@ -62,7 +62,8 @@ $pc9x_time_tolerance;           # thing a node might send - once an hour and we
                                 # this is actually the partition between "yesterday" and "today" but old.
 $senderverify = 0;                             # 1 - check for forged PC11 or PC61.
                                 # 2 - if forged, dump them.
-$pc11_dwell_time = 1;                  # number of seconds to wait for a PC61 to come to substitute the PC11
+$pc11_dwell_time = 2;                  # number of seconds to wait for a PC61 to come to substitute the PC11
+$pc11_extract_route = 1;               # generate missing  user route entry and IP address from passing PC61s
 
 
 $pc92filterdef = bless ([
@@ -140,16 +141,16 @@ sub handle_10
        }
 
        # convert this to a PC93, coming from mycall with origin set and process it as such
-       $main::me->normal(pc93($to, $from, $via, $pc->[3], $pc->[6]));
+       my $ref = Route::get($pc->[6]);
+       my $ip = $ref->ip;
+       $main::me->normal(pc93($to, $from, $via, $pc->[3], $pc->[6]), $ip);
 }
 
-my $last;
-my $pc11_saved;
-my $pc11_saved_time;
-my $pc11_rx;
-my $rpc11_to_61;
-my $pc11_to_61;
-my $pc61_rx;
+my %pc11_saved;                                        # delayed PC11s
+my $pc11_rx;                                   # no of pc11 rxed
+my $pc61_rx;                                   # no of pc61 rxed
+my $pc11_to_61;                                        # no of 'better' pc61, that replaced stored waiting pc11
+my $rpc11_to_61;                               # no of pc11s 'promoted' to pc61 by IP captured in routing table
 
 # DX Spot handling
 sub handle_11
@@ -169,7 +170,8 @@ sub handle_11
                }
        }
 
-       dbg("INPUT PC$pcno $line origin $origin recurse: $recurse") if isdbg("pc11") || isdbg("pc61"); 
+       dbg("---") if isdbg("pc11") || isdbg("pc61"); 
+       dbg("INPUT $self->{call}: $line via: $origin recurse: $recurse") if isdbg("pc11") || isdbg("pc61"); 
 
 #      my ($hops) = $pc->[8] =~ /^H(\d+)/;
 
@@ -257,92 +259,122 @@ sub handle_11
                }
        }
 
+       # Populate the routing table
+       $self->populate_routing_table($pc->[7], $pc->[6], $pc->[8]);
+       my $r = Route::User::get($pc->[6]);
+       if ($pcno == 61) {
+               unless ($pc->[8] && is_ipaddr($pc->[8])) {
+                       dbg("PCPROT: ROUTE $self->{call} NO IP ADDRESS in '$line'!");   
+               }
+       }
 
        # this is where we decide to delay PC11s in the hope that a PC61 will be along soon.
-       
        my $key = join '|', @spot[0..2,4,7]; # not including text
 
        unless ($recurse) {
+
                if ($pcno == 61) {
-                       ++$pc61_rx;
-
-                       if ($pc11_saved) {
-                               if ($key eq $pc11_saved->[0]) {
-                                       ++$pc11_to_61;
-                                       my $percent = $pc11_rx ? $pc11_to_61 * 100 / $pc11_rx : 0;
-                                       dbg(sprintf("recurse: $recurse saved PC11 spot $key dumped, better pc61 received pc61: $pc61_rx pc11: $pc11_rx -> pc61: $pc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
-                                       undef $pc11_saved;
+
+                       if ($pc11_saved{$key}) {
+                               # before we promote  because it's a better pc61, check that it's not a dupe (but don't insert it).
+                               if (Spot::dup_find(@spot[0..4,7])) {
+                                       dbg("PCPROT: Duplicate Spot  $self->{call}: $pc->[0] $key ignored\n") if isdbg('chanerr') || isdbg('dupespot') || isdbg('pc11');
+                                       delete $pc11_saved{$key};
+                                       return;
                                }
+
+                               ++$pc11_to_61;
+
+                               my $percent = $pc11_rx ? $pc11_to_61 * 100 / $pc11_rx : 0;
+                               dbg(sprintf("PROMOTED $self->{call}: BETTER $pc->[0] $key, using pc61, WAITING pc11 DUMPED: $pc61_rx pc11: $pc11_rx better pc61: $pc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
+
+                               delete $pc11_saved{$key}; # because we have promoted it, no longer needed.
+
+                               # so unlike the promotion by route, there is a stored, but uncounted PC11
+                               # which have discarded. But we have chosen to use better PC61 that's just come in
+                               # so we allow the PC61 to be counted later, but count the now discarded PC11
+                               # because it *did* come in.
+                               ++$pc11_rx;
                        } 
                }
+
                if ($pcno == 11) {
 
-                       ++$pc11_rx;
-                       
-                       if ($pc11_saved) {
-                               if ($key eq $pc11_saved->[0] ) {
-                                       dbg("recurse: $recurse saved PC11 spot $key, dupe pc11 received and dumped") if isdbg("pc11");
-                                       return;         # because it's a dup
-                               }
+                       # if this fires then we have already had one or more PC11s but no PC61 for this spot
+                       if ($pc11_saved{$key}) {
+                               dbg("DUPE $self->{call} $pc->[0] key: $key is saved, ignored") if isdbg("pc11");
+                               return;         # because it's a dup
                        }
 
-                       # can we promote this to a PC61?
-                       my $r = Route::User::get($spot[4]); # find spotter
-                       if ($r && $r->ip) {                     # do we have an ip addres
-                               $pcno = 61;                                             # now turn this into a PC61
-                               $spot[14] = $r->ip;
+                       # before we promote by route, check that it's not been preceded by a previous PC61
+                       if (Spot::dup_find(@spot[0..4,7])) {
+                               my $s = exists $pc11_saved{$key} ? " stored $key removed" : "";
+                               dbg("PCPROT: Duplicate Spot $self->{call}: PC11$s, recurse: $recurse, ignored\n") if isdbg('chanerr') || isdbg('dupespot') || isdbg('pc11');
+                               delete $pc11_saved{$key};
+                               return;
+                       }
+
+
+                       # If we have an ip address we can promote by route
+                       if ($r && $r->ip) {
+                               $pcno = 61;
+                               $pc->[0] = 'PC61';
+                               my $hops = $pc->[8];
+                               $pc->[8] = $spot[14] = $r->ip;
                                ++$rpc11_to_61;
                                my $percent = $pc11_rx ? $rpc11_to_61 * 100 / $pc11_rx : 0;
-                               dbg(sprintf("recurse: $recurse PC11 spot $key promoted to pc61 ip $spot[14] pc61: $pc61_rx pc11: $pc11_rx -> pc61 $pc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
-                               undef $pc11_saved;
+                               dbg(sprintf("PROMOTED $self->{call}: ROUTE pc11 $key PROMOTED to pc61 with IP $spot[14] pc61: $pc61_rx pc11: $pc11_rx route->pc61 $rpc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
+                               $line = join '^', @$pc, $hops, '~';
+
+                               # update the stats (NOTE, thie record was a PC11, it has now become a PC61
+                               # this is NOT the same choosing a better PC61, that is a separate record. 
+                               ++$pc11_rx;             # 'cos we received as a PC11 it and it won't be a pc11 anymore 
+                               --$pc61_rx;             # 'cos we'll increment it later as it's now a pc61, no double counting
+                               
+#                              dbg("CHANGED saved key: $key PC11 line to $line") if isdbg('pc11');
+                               delete $pc11_saved{$key};
                        }
 
-                       # if it is STILL (despite all efforts to change it)  a PC11
+                       # if it is STILL (despite all efforts to change it) a PC11
+                       # save it and wait - it will be called from pc11_process
                        if ($pcno == 11) {
-                               if ($pc11_saved && $key ne $pc11_saved->[0]) {
-                                       dbg("recurse: $recurse NEW PC11 spot $key ne $pc11_saved->[0], recursing") if isdbg("pc11");
-                                       # shift @$pc11_saved;   # saved key
-                                       my $self = $pc11_saved->[1];
-                                       my @saved = @$pc11_saved[2..5];
-                                       $self->handle_11(@saved, 1);
-                               }
-                               
-                               $pc11_saved = [$key, $self, $pcno, $line, $origin, $pc];
-                               $pc11_saved_time = $main::systime;
-                               dbg("recurse: $recurse saved new PC11 spot $key for a better offer") if isdbg("pc11");
+                               $pc11_saved{$key} = [$main::systime, $self, $pcno, $line, $origin, $pc];
+                               dbg("WAITING $self->{call}: NEW $pc->[0] spot $key waiting for a better offer") if isdbg("pc11");
                                return;
                        }
-                       
-               } else {
-                       my $count =  $pc11_to_61+$rpc11_to_61;
-                       my $percent = $pc11_rx ? $count*100 / $pc11_rx : 0;
-                       dbg(sprintf("recurse: $recurse PC61 spot $key passed onward pc61: $pc61_rx pc11: $pc11_rx -> pc61: $count (%0.1f%%)", $percent)) if isdbg("pc11");
-                       $recurse = 0;
-                       undef $pc11_saved;
                }
        }
        
-       
        # this goes after the input filtering, but before the add
        # so that if it is input filtered, it isn't added to the dup
        # list. This allows it to come in from a "legitimate" source
+       #
+       ## NOTE: this is where we insert the spot into the DXDupe cache
+       #
        if (Spot::dup(@spot[0..4,7])) {
-               dbg("PCPROT: Duplicate Spot $pc->[0] $key ignored\n") if isdbg('chanerr') || isdbg('dupespot');
+               dbg("PCPROT: Duplicate Spot  $self->{call}: $pc->[0] $key ignored\n") if isdbg('chanerr') || isdbg('dupespot') || isdbg('pc11');
                return;
        }
 
-       # we check IP addresses for PC61 - this will also dedupe PC11 copies
+       dbg("PROCESSING $self->{call}: $pc->[0] key: $key") if isdbg('pc11');
+       
+       if ($pcno == 11) {
+               ++$pc11_rx;
+       } else {
+               ++$pc61_rx;
+       }
+
+       # we check IP addresses for PC61 - this will also dedupe PC11 promotions
        if (@$pc > 8 && is_ipaddr($pc->[8])) {
                my $ip = $pc->[8];
                $ip =~ s/,/:/g;
                $ip =~ s/^::ffff://;
                if (DXCIDR::find($ip)) {
                        dbg($line) if isdbg('nologchan');
-                       dbg("PCPROT: $ip in badip list, dropped");
+                       dbg("PCPROT: PC61 $ip in badip list, dropped");
                        return;
                }
        }
-
        
        # here we verify the spotter is currently connected to the node it says it is one. AKA email sender verify
        # but without the explicit probe to the node. We are relying on "historical" information, but it very likely
@@ -452,15 +484,6 @@ sub handle_11
                }
        }
 
-       # cancel any recursion as we have now processed it
-       if ($recurse) {
-               if ($pc11_saved && $key eq $pc11_saved->[0]) {
-                       dbg("recurse: $recurse key: $key saved_key: $pc11_saved->[0] removed") if isdbg('pc11');
-                       undef $pc11_saved;
-               }
-               $recurse = 0;
-       }
-
        # local processing
        if (defined &Local::spot) {
                my $r;
@@ -476,22 +499,57 @@ sub handle_11
        # send out the filtered spots
        send_dx_spot($self, $line, @spot) if @spot;
 
-       
+       # cancel any recursion as we have now processed it
+       my $count =  $pc11_to_61+$rpc11_to_61;
+       my $percent = ($pc11_rx + $pc61_rx) ? ($pc11_rx * 100) / ($pc11_rx + $pc61_rx) : 0;
+       my $pc11_61 = $pc11_rx ? ($count * 100) / $pc11_rx : 0;
+       my $data = "$pc->[0] key: $key";
+       my $stats = sprintf "pc11: $pc11_rx pc61: $pc61_rx pc11%%: %0.1f%% pc11->pc61: $count(%0.1f%%)", $percent, $pc11_61;
+       if ($recurse) {
+               if ($pc11_saved{$key}) {
+                       dbg("END $self->{call}: RECURSED $data removed and finished $stats") if isdbg('pc11');
+                       delete $pc11_saved{$key};
+               } else {
+                       dbg("END $self->{call}: RECURSED NO KEY finished $stats") if isdbg('pc11'); 
+               }
+               $recurse = 0;
+       } else {
+               dbg("END $self->{call}: NORMAL $data finished $stats") if isdbg('pc11');
+       }
 }
 
 # used to kick outstanding PC11 if required
 sub pc11_process
 {
-       if ($pc11_saved && $main::systime > $pc11_saved_time + $pc11_dwell_time) {
-               dbg("saved PC11 spot $pc11_saved->[0] timed out waiting, recursing") if isdbg("pc11");
-               shift @$pc11_saved;     # saved key
-               my $self = shift @$pc11_saved;
-               my @saved = @$pc11_saved;
-               undef $pc11_saved;
-               $self->handle_11(@saved, 1);
+       foreach my $key (keys %pc11_saved) {
+               my $r = $pc11_saved{$key};
+               if ($main::systime > $r->[0] + $pc11_dwell_time) {
+                       dbg("---") if isdbg("pc11");
+                       dbg("RECURSE $r->[1]->{call}: PC11 spot $key timed out waiting, resend") if isdbg("pc11");
+                       my $self = $r->[1];
+                       delete $pc11_saved{$key};
+                       $self->handle_11(@$r[2..5], 1);
+               }
        }
 }
 
+# return the pc11 / pc61 / promotions data
+sub get_pc11_61_stats
+{
+       my $promotions =  $pc11_to_61+$rpc11_to_61;
+       my $percent = ($pc11_rx + $pc61_rx) ? ($pc11_rx * 100) / ($pc11_rx + $pc61_rx) : 0;
+       my $pc11_61p = $pc11_rx ? ($promotions * 100) / $pc11_rx : 0;
+       return {
+                       pc11_rx => $pc11_rx+0,
+                       pc61_rx => $pc61_rx+0,
+                       pc11_to_61 => $pc11_to_61+0,
+                       rpc11_to_61 => $rpc11_to_61+0,
+                       promotions => $promotions+0,
+                       pc11_percent => $percent+0,
+                       promotions_percent => $pc11_61p+0,
+                  };
+}
+
 # announces
 sub handle_12
 {
@@ -805,7 +863,7 @@ sub handle_18
        $self->state('init');
 
        my $parent = Route::Node::get($self->{call});
-
+       
        # record the type and version offered
        if (my ($software, $version) = $pc->[1] =~ /(DXSpider|CC\s*Cluster)\s+Version: (\d+(?:\.\d+)?)/i) {
                $version += 0;
@@ -815,6 +873,7 @@ sub handle_18
                $parent->version($version);
                my ($build) = $pc->[1] =~ /Build: (\d+(?:\.\d+)?)/;
                $build += 0;
+               $build = 0 unless $build > 1;
                $self->{build} = $build;
                $self->user->build($build);
                $parent->build($build);
@@ -826,6 +885,20 @@ sub handle_18
                        $self->sort('S');
                }
 #              $self->{handle_xml}++ if DXXml::available() && $pc->[1] =~ /\bxml/;
+       } elsif (my ($software, $version, $build) = $pc->[1] =~ /(AR-Cluster)\s+Version:\s+(\d+\.\d+).?(\d+\.\d+)?/) {
+               dbg("$self->{call} = $software version $version build $build");
+               $self->{version} = $version;
+               $self->user->version($version);
+               $parent->version($version);
+               $self->{build} = $build;
+               $self->user->build($build);
+               $parent->build($build);
+               unless ($self->is_arcluster) {
+                       dbg("Change U " . $self->user->sort . " C $self->{sort} -> R");
+                       $self->user->sort('R');
+                       $self->user->put;
+                       $self->sort('R');
+               }
        } else {
                dbg("$self->{call} = Unknown software ($pc->[1] $pc->[2])");
                $self->version(50.0);
@@ -833,7 +906,7 @@ sub handle_18
                $self->user->version($self->version);
        }
 
-       if ($pc->[1] =~ /CC\*Cluster/i || $pc->[1] =~ /\bpc9x/i) {
+       if ($pc->[1] =~ /CC\s*Cluster/i || $pc->[1] =~ /\bpc9x/i) {
                if ($self->{isolate}) {
                        dbg("$self->{call} pc9x recognised, but node is isolated, using old protocol");
                } elsif (!$self->user->wantpc9x) {
@@ -2249,7 +2322,7 @@ sub handle_92
        }
 
        # broadcast it if we get here
-       $self->broadcast_route_pc9x($pcall, undef, $line, 0);
+       $self->broadcast_route_pc9x($pcall, undef, $line, 0) unless !$pc92_ad_enabled && ($sort eq 'A' || $sort eq 'D');
 }
 
 # get all the routes for a thing, bearing in mind that the thing (e.g. a user)
@@ -2316,7 +2389,9 @@ sub handle_93
        my $via = uc $pc->[5];
        my $text = $pc->[6];
        my $onode = uc $pc->[7];
-       $onode = $pcall if @$pc <= 8;
+       my $ipaddr = $pc->[8];
+       
+       $onode //= $pcall;
 
        # this is catch loops caused by bad software ...
        if (eph_dup("PC93|$from|$text|$onode", $pc10_dupe_age)) {
@@ -2355,6 +2430,8 @@ sub handle_93
                }
        }
 
+       $self->populate_routing_table($onode, $from, $ipaddr);
+
        # if it is routeable then then treat it like a talk
        my $ref = Route::get($to);
        if ($ref) {
@@ -2433,4 +2510,48 @@ sub handle_default
        }
 }
 
+sub populate_routing_table
+{
+       my ($self, $node, $user, $ip) = @_;
+
+       my $rn = Route::Node::get($node);
+       unless ($rn) {
+               $rn = Route::Node->new($node);
+               dbg("ROUTE $self->{call}: ADD NEW node: $node") if isdbg('pc11');
+       }
+
+       my $ru;
+       if ($user ne $node) {
+               $ru = Route::User::get($user);
+               unless ($ru) {
+                       $rn->add_user($user, 0, undef);
+                       dbg("ROUTE $self->{call}: ADD NEW user: $user -> $node") if isdbg('pc11');
+               }
+               $ru = Route::User::get($user);
+       }
+       
+       # Add/Change any IP address info
+
+       # as we have a route to a user, if it (or the node) does not exist then create them
+       # link the user to the node if not already done.
+       # then add or alter the IP address
+       if ($ip && is_ipaddr($ip)) {
+               my $new = $ip;
+               if ($ru) {
+                       if ($ru->ip ne $new) {
+                               if ($ru->ip) {
+                                       my $old = $ru->ip;
+                                       $ru->ip($new);
+                                       dbg("ROUTE $self->{call}: ALTER IP node: $node user: $user old IP: '$old'-> new IP: '$new'") if isdbg('pc11');
+                               } else{
+                                       $ru->ip($new);
+                                       dbg("ROUTE $self->{call}: NEW IP node: $node user: $user IP: '$new'") if isdbg('pc11');
+                               }
+                       }
+               } else {
+                       dbg("ROUTE $self->{call}: ADD Failed for node $node user $user") if isdbg('pc11');
+               }
+       }
+}
+
 1;