use DXLog;
use IO::File;
use File::Copy;
+
use Socket qw(AF_INET AF_INET6 inet_pton inet_ntop);
our $active = 0;
my $count4 = 0;
my $count6 = 0;
-# load the badip file
-sub load
-{
- if ($active) {
- $count4 = _load($ipv4, 4);
- $count6 = _load($ipv6, 6);
- }
- LogDbg('DXProt', "DXCIDR: loaded $count4 IPV4 addresses and $count6 IPV6 addresses");
- return $count4 + $count6;
-}
-
sub _fn
{
- return localdata($badipfn) . ".$_[0]";
+ return localdata($badipfn);
}
-sub _load
+sub _read
{
- my $list = shift;
- my $sort = shift;
- my $fn = _fn($sort);
+ my $suffix = shift;
+ my $fn = _fn();
+ $fn .= ".$suffix" if $suffix;
my $fh = IO::File->new($fn);
- my $count = 0;
-
+ my @out;
+
if ($fh) {
while (<$fh>) {
chomp;
next if /^\s*\#/;
next unless /[\.:]/;
- $list->add_any($_);
- ++$count;
+ push @out, $_;
}
$fh->close;
- $list->clean if $count;
- $list->prep_find;
- } elsif (-r $fn) {
- LogDbg('err', "DXCIDR: $fn not found ($!)");
+ } else {
+ LogDbg('err', "DXCIDR: $fn read error ($!)");
}
- return $count;
+ return @out;
+}
+
+sub _load
+{
+ my $suffix = shift;
+ my @in = _read($suffix);
+ return scalar add(@in);
}
sub _put
{
- my $list = shift;
- my $sort = shift;
- my $fn = _fn($sort);
+ my $suffix = shift;
+ my $fn = _fn() . ".$suffix";
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;
+ LogDbg('cmd', "DXCIDR: put (re-)written $fn");
} else {
LogDbg('err', "DXCIDR: cannot write $fn.$r $!");
}
+ return $count;
+}
+
+sub append
+{
+ 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) {
+ print $fh "$_\n" for @in;
+ $fh->close;
+ move "$fn.$r", $fn;
+ } else {
+ LogDbg('err', "DXCIDR::append error appending to $fn.$r $!");
+ }
+ } else {
+ LogDbg('err', "DXCIDR::append require badip suffix");
+ }
+ return scalar @in;
}
sub add
{
+ my $count = 0;
+
for my $ip (@_) {
# protect against stupid or malicious
- next if /^127\./;
- next if /^::1$/;
- if (/\./) {
- if ($ipv4->find($ip)) {
- LogDbg('DXProt', "DXCIDR: Ignoring existing IPV4 $ip");
- next;
- }
+ next if $ip =~ /^127\./;
+ next if $ip =~ /^::1$/;
+ if ($ip =~ /\./) {
$ipv4->add_any($ip);
+ ++$count;
++$count4;
- } elsif (/:/) {
- if ($ipv6->find($ip)) {
- LogDbg('DXProt', "DXCIDR: Ignoring existing IPV6 $ip");
- next;
- }
+ } elsif ($ip =~ /:/) {
$ipv6->add_any($ip);
+ ++$count;
++$count6;
- LogDbg('DXProt', "DXCIDR: Added IPV6 $ip address");
+ } else {
+ LogDbg('err', "DXCIDR::add non-ip address '$ip' read");
}
}
+ 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;
- _put($ipv4, 4) if $count4;
- _put($ipv6, 6) if $count6;
-}
-
sub _sort
{
my @in;
}
import Net::CIDR::Lite;
+ $active = 1;
- $ipv4 = Net::CIDR::Lite->new;
- $ipv6 = Net::CIDR::Lite->new;
+ my $fn = _fn();
+ if (-e $fn) {
+ move $fn, "$fn.base";
+ }
- $active = 1;
- load();
+ _touch("$fn.local");
+
+ reload();
+
+}
+
+sub _touch
+{
+ my $fn = shift;
+ my $now = time;
+ local (*TMP);
+ utime ($now, $now, $fn) || open (TMP, ">>$fn") || LogDbg('err', "DXCIDR::touch: Couldn't touch $fn: $!");
}
+sub reload
+{
+ new();
+
+ my $count = _load('base');
+ $count += _load('local');
+
+ LogDbg('DXProt', "DXCIDR::reload $count ip addresses found (IPV4: $count4 IPV6: $count6)" );
+ return $count;
+}
+
+sub new
+{
+ $ipv4 = Net::CIDR::Lite->new;
+ $ipv6 = Net::CIDR::Lite->new;
+ $count4 = $count6 = 0;
+}
1;