rearrange badip files and code
[spider.git] / perl / DXCIDR.pm
index 495768edd5217bcc452c3e3a5030ecd6dbf6b28a..7cb13df0bf4ef7e7f870e99e1a785aa6dc0e0dc7 100644 (file)
@@ -17,6 +17,8 @@ use DXLog;
 use IO::File;
 use File::Copy;
 
+use Socket qw(AF_INET AF_INET6 inet_pton inet_ntop);
+
 our $active = 0;
 our $badipfn = "badip";
 my $ipv4;
@@ -28,8 +30,7 @@ my $count6 = 0;
 sub load
 {
        if ($active) {
-               $count4 = _get($ipv4, 4);
-               $count6 = _get($ipv6, 6);
+               _load();
        }
        LogDbg('DXProt', "DXCIDR: loaded $count4 IPV4 addresses and $count6 IPV6 addresses");
        return $count4 + $count6;
@@ -37,97 +38,117 @@ sub load
 
 sub _fn
 {
-       return localdata($badipfn) . "$_[0]";
+       return localdata($badipfn);
 }
 
-sub _get
+sub _load
 {
-       my $list = shift;
-       my $sort = shift;
-       my $fn = _fn($sort);
+       my $fn = _fn();
        my $fh = IO::File->new($fn);
        my $count = 0;
+
+       new();
        
        if ($fh) {
                while (<$fh>) {
                        chomp;
                        next if /^\s*\#/;
-                       $list->add($_);
+                       next unless /[\.:]/;
+                       add($_);
                        ++$count;
                }
                $fh->close;
-               $list->clean if $count;
        } elsif (-r $fn) {
                LogDbg('err', "DXCIDR: $fn not found ($!)");
        }
+
+       clean_prep();
+       
        return $count;
 }
 
 sub _put
 {
-       my $list = shift;
-       my $sort = shift;
-       my $fn = _fn($sort);
+       my $fn = _fn();
        my $r = rand;
        my $fh = IO::File->new (">$fn.$r");
+       my $count = 0;
        if ($fh) {
-               for ($list->list) {
+               for ($ipv4->list, $ipv6->list) {
                        $fh->print("$_\n");
+                       ++$count;
                }
                move "$fn.$r", $fn;
        } else {
                LogDbg('err', "DXCIDR: cannot write $fn.$r $!");
        }
+       return $count;
 }
 
 sub add
 {
-       for (@_) {
+       my $count = 0;
+       
+       for my $ip (@_) {
                # protect against stupid or malicious
                next if /^127\./;
                next if /^::1$/;
                if (/\./) {
-                       $ipv4->add($_);
+                       $ipv4->add_any($ip);
+                       ++$count;
                        ++$count4;
-                       LogDbg('DXProt', "DXCIDR: Added IPV4 $_ address");
-               } else {
-                       $ipv6->add($_);
+               } elsif (/:/) {
+                       $ipv6->add_any($ip);
+                       ++$count;
                        ++$count6;
-                       LogDbg('DXProt', "DXCIDR: Added IPV6 $_ address");
+                       LogDbg('DXProt', "DXCIDR: Added IPV6 $ip address");
                }
        }
+       return $count;
+}
+
+sub clean_prep
+{
        if ($ipv4 && $count4) {
+               $ipv4->clean;
                $ipv4->prep_find;
-               _put($ipv4, 4);
        }
        if ($ipv6 && $count6) {
+               $ipv6->clean;
                $ipv6->prep_find;
-               _put($ipv6, 6);
        }
 }
 
 sub save
 {
        return 0 unless $active;
-       my $list = $ipv4->list;
-       _put($list, 4) if $list;
-       $list = $ipv6->list;
-       _put($list, 6) if $list;
+       _put() if $count4 || $count6;
+}
+
+sub _sort
+{
+       my @in;
+       my @out;
+       for (@_) {
+               push @in, [inet_pton(m|:|?AF_INET6:AF_INET, $_), split m|/|];
+       }
+       @out = sort {$a->[0] <=> $b->[0]} @in;
+       return map { "$_->[1]/$_->[2]"} @out;
 }
 
 sub list
 {
        my @out;
-       push @out, $ipv4->list;
-       push @out, $ipv6->list;
-       return (1, sort @out);
+       push @out, $ipv4->list if $count4;
+       push @out, $ipv6->list if $count6;
+       return _sort(@out);
 }
 
 sub find
 {
        return 0 unless $active;
        return 0 unless $_[0];
-       
+
        if ($_[0] =~ /\./) {
                return $ipv4->find($_[0]) if $count4;
        }
@@ -143,14 +164,18 @@ sub init
        }
 
        import Net::CIDR::Lite;
+       $active = 1;
 
-       $ipv4 = Net::CIDR::Lite->new;
-       $ipv6 = Net::CIDR::Lite->new;
+       new();
 
        load();
-       $active = 1;
 }
 
-
+sub new
+{
+       $ipv4 = Net::CIDR::Lite->new;
+       $ipv6 = Net::CIDR::Lite->new;
+       $count4 = $count6 = 0; 
+}
 
 1;