2 # IP Address block list / checker
4 # This is a DXSpider compatible, optional skin over Net::CIDR::Lite
5 # If Net::CIDR::Lite is not present, then a find will always returns 0
20 use Socket qw(AF_INET AF_INET6 inet_pton inet_ntop);
23 our $badipfn = "badip";
35 LogDbg('DXProt', "DXCIDR: loaded $count4 IPV4 addresses and $count6 IPV6 addresses");
36 return $count4 + $count6;
41 return localdata($badipfn);
47 my $fh = IO::File->new($fn);
62 LogDbg('err', "DXCIDR: $fn not found ($!)");
74 my $fh = IO::File->new (">$fn.$r");
77 for ($ipv4->list, $ipv6->list) {
83 LogDbg('err', "DXCIDR: cannot write $fn.$r $!");
93 # protect against stupid or malicious
104 LogDbg('DXProt', "DXCIDR: Added IPV6 $ip address");
112 if ($ipv4 && $count4) {
116 if ($ipv6 && $count6) {
124 return 0 unless $active;
125 _put() if $count4 || $count6;
133 push @in, [inet_pton(m|:|?AF_INET6:AF_INET, $_), split m|/|];
135 @out = sort {$a->[0] <=> $b->[0]} @in;
136 return map { "$_->[1]/$_->[2]"} @out;
142 push @out, $ipv4->list if $count4;
143 push @out, $ipv6->list if $count6;
149 return 0 unless $active;
150 return 0 unless $_[0];
153 return $ipv4->find($_[0]) if $count4;
155 return $ipv6->find($_[0]) if $count6;
160 eval { require Net::CIDR::Lite };
162 LogDbg('DXProt', "DXCIDR: load (cpanm) the perl module Net::CIDR::Lite to check for bad IP addresses (or CIDR ranges)");
166 import Net::CIDR::Lite;
176 $ipv4 = Net::CIDR::Lite->new;
177 $ipv6 = Net::CIDR::Lite->new;
178 $count4 = $count6 = 0;