X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FDXCIDR.pm;h=0a829bf1f474035eb73e42c85b381569b479d236;hb=56b93915d65c299a5f2d2e555ee361c056773843;hp=5a6272aa4747e0c813b96431b5932b5df22f1516;hpb=cbbf83baa54e859c382de79954607923cbb1b653;p=spider.git diff --git a/perl/DXCIDR.pm b/perl/DXCIDR.pm index 5a6272aa..0a829bf1 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,6 +96,8 @@ sub _put sub append { + return 0 unless $active; + my $suffix = shift; my @in = @_; my @out; @@ -104,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 =~ /\./) { @@ -127,6 +145,8 @@ sub add sub clean_prep { + return unless $active; + if ($ipv4 && $count4) { $ipv4->clean; $ipv4->prep_find; @@ -142,14 +162,16 @@ 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; } sub list { + return () unless $active; my @out; push @out, $ipv4->list if $count4; push @out, $ipv6->list if $count6; @@ -199,6 +221,8 @@ sub _touch sub reload { + return 0 unless $active; + new(); my $count = 0; @@ -224,6 +248,8 @@ sub reload sub new { + return 0 unless $active; + $ipv4 = Net::CIDR::Lite->new; $ipv6 = Net::CIDR::Lite->new; $count4 = $count6 = 0;