Make it work with badip system active
[spider.git] / perl / DXCIDR.pm
index 88a26d5c552631e4b66c173eb0c3262400e17d0d..a91d13967cf7c3db845e6e49df1c954c9fd1ba53 100644 (file)
@@ -82,20 +82,21 @@ sub _put
 
 sub append
 {
+       return 0 unless $active;
+       
        my $suffix = shift;
        my @in = @_;
        my @out;
        
        if ($suffix) {
                my $fn = _fn() . ".$suffix";
-               my $r = rand;
-               my $fh = IO::File->new (">>$fn.$r");
-               if ($fh) {
+               my $fh = IO::File->new;
+               if ($fh->open("$fn", "a+")) {
+                       $fh->seek(0, 2);        # belt and braces !!
                        print $fh "$_\n" for @in;
                        $fh->close;
-                       move "$fn.$r", $fn;
                } else {
-                       LogDbg('err', "DXCIDR::append error appending to $fn.$r $!");
+                       LogDbg('err', "DXCIDR::append error appending to $fn $!");
                }
        } else {
                LogDbg('err', "DXCIDR::append require badip suffix");
@@ -105,6 +106,7 @@ sub append
 
 sub add
 {
+       return 0 unless $active;
        my $count = 0;
        
        for my $ip (@_) {
@@ -128,6 +130,8 @@ sub add
 
 sub clean_prep
 {
+       return unless $active;
+
        if ($ipv4 && $count4) {
                $ipv4->clean;
                $ipv4->prep_find;
@@ -151,6 +155,7 @@ sub _sort
 
 sub list
 {
+       return () unless $active;
        my @out;
        push @out, $ipv4->list if $count4;
        push @out, $ipv6->list if $count6;
@@ -200,18 +205,35 @@ sub _touch
 
 sub reload
 {
-       new();
+       return 0 unless $active;
 
-       my $count = _load('base');
-       $count += _load('local');
+       new();
 
-       LogDbg('DXProt', "DXCIDR::reload $count ip addresses found (IPV4: $count4 IPV6: $count6)" );
+       my $count = 0;
+       my $files = 0;
+
+       LogDbg('DXProt', "DXCIDR::reload reload database" );
+
+       my $dir;
+       opendir($dir, $main::local_data);
+       while (my $fn = readdir $dir) {
+               next unless my ($suffix) = $fn =~ /^badip\.(\w+)$/;
+               my $c = _load($suffix);
+               LogDbg('DXProt', "DXCIDR::reload: $fn read containing $c ip addresses" );
+               $count += $c;
+               $files++;
+       }
+       closedir $dir;
+       
+       LogDbg('DXProt', "DXCIDR::reload $count ip addresses found (IPV4: $count4 IPV6: $count6) in $files badip files" );
 
        return $count;
 }
 
 sub new
 {
+       return 0 unless $active;
+
        $ipv4 = Net::CIDR::Lite->new;
        $ipv6 = Net::CIDR::Lite->new;
        $count4 = $count6 = 0;