fix stupid unpad typo in dx command
[spider.git] / perl / DXCIDR.pm
index a91d13967cf7c3db845e6e49df1c954c9fd1ba53..0a829bf1f474035eb73e42c85b381569b479d236 100644 (file)
@@ -38,12 +38,25 @@ sub _read
        $fn .= ".$suffix" if $suffix;
        my $fh = IO::File->new($fn);
        my @out;
+       my $ecount;
+       my $line;
+       
 
        if ($fh) {
                while (<$fh>) {
                        chomp;
+                       ++$line;
                        next if /^\s*\#/;
                        next unless /[\.:]/;
+                       next unless $_;
+                       unless (is_ipaddr($_)) {
+                               ++$ecount;
+                               LogDbg('err', qq(DXCIDR: $fn line $line: '$_' not an ip address));
+                               if ($ecount > 10) {
+                                       LogDbg('err', qq(DXCIDR: More than 10 errors in $fn at/after line $line: '$_' - INVALID INPUT FILE));
+                                       return ();
+                               }
+                       }
                        push @out, $_;
                }
                $fh->close;
@@ -57,6 +70,7 @@ sub _load
 {
        my $suffix = shift;
        my @in = _read($suffix);
+       return 0 unless @in;
        return scalar add(@in);
 }
 
@@ -111,6 +125,7 @@ sub add
        
        for my $ip (@_) {
                # protect against stupid or malicious
+               next unless is_ipaddr($ip);
                next if $ip =~ /^127\./;
                next if $ip =~ /^::1$/;
                if ($ip =~ /\./) {
@@ -147,9 +162,10 @@ sub _sort
        my @in;
        my @out;
        for (@_) {
-               push @in, [inet_pton(m|:|?AF_INET6:AF_INET, $_), split m|/|];
+               my @ip = split m|/|;
+               push @in, [inet_pton(m|:|?AF_INET6:AF_INET, $ip[0]), @ip];
        }
-       @out = sort {$a->[0] <=> $b->[0]} @in;
+       @out = sort {$a->[1] cmp $b->[1]} @in;
        return map { "$_->[1]/$_->[2]"} @out;
 }