X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FDXCIDR.pm;h=b702d7b899c197f2647dd92da34d2efb3f502396;hb=b261f11169a5704a0992b4198f5291a3af43d780;hp=88a26d5c552631e4b66c173eb0c3262400e17d0d;hpb=996fed89967a1fd4d8665a10fd73b9612aeb1606;p=spider.git diff --git a/perl/DXCIDR.pm b/perl/DXCIDR.pm index 88a26d5c..b702d7b8 100644 --- a/perl/DXCIDR.pm +++ b/perl/DXCIDR.pm @@ -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); } @@ -82,20 +96,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,10 +120,12 @@ sub append sub add { + return 0 unless $active; my $count = 0; for my $ip (@_) { # protect against stupid or malicious + next unless is_ipaddr($ip); next if $ip =~ /^127\./; next if $ip =~ /^::1$/; if ($ip =~ /\./) { @@ -128,6 +145,8 @@ sub add sub clean_prep { + return unless $active; + if ($ipv4 && $count4) { $ipv4->clean; $ipv4->prep_find; @@ -151,6 +170,7 @@ sub _sort sub list { + return () unless $active; my @out; push @out, $ipv4->list if $count4; push @out, $ipv6->list if $count6; @@ -200,18 +220,35 @@ sub _touch sub reload { + return 0 unless $active; + new(); - my $count = _load('base'); - $count += _load('local'); + my $count = 0; + my $files = 0; + + LogDbg('DXProt', "DXCIDR::reload reload database" ); - LogDbg('DXProt', "DXCIDR::reload $count ip addresses found (IPV4: $count4 IPV6: $count6)" ); + 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;