mirror of
git://git.code.sf.net/p/xtables-addons/xtables-addons
synced 2025-09-08 13:44:56 +02:00
geoipdb builder: get rid of some global variables
This commit is contained in:
@@ -10,7 +10,6 @@ use IO::Handle;
|
|||||||
use Text::CSV_XS; # or trade for Text::CSV
|
use Text::CSV_XS; # or trade for Text::CSV
|
||||||
use strict;
|
use strict;
|
||||||
|
|
||||||
my %country;
|
|
||||||
my $csv = Text::CSV_XS->new({
|
my $csv = Text::CSV_XS->new({
|
||||||
allow_whitespace => 1,
|
allow_whitespace => 1,
|
||||||
binary => 1,
|
binary => 1,
|
||||||
@@ -35,11 +34,12 @@ foreach (qw(LE BE)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
&collect();
|
&dump(&collect());
|
||||||
&dump();
|
|
||||||
|
|
||||||
sub collect
|
sub collect
|
||||||
{
|
{
|
||||||
|
my %country;
|
||||||
|
|
||||||
while (my $row = $csv->getline(*ARGV)) {
|
while (my $row = $csv->getline(*ARGV)) {
|
||||||
if (!defined($country{$row->[4]})) {
|
if (!defined($country{$row->[4]})) {
|
||||||
$country{$row->[4]} = {
|
$country{$row->[4]} = {
|
||||||
@@ -61,23 +61,26 @@ sub collect
|
|||||||
}
|
}
|
||||||
|
|
||||||
print STDERR "\r\e[2K$. entries total\n";
|
print STDERR "\r\e[2K$. entries total\n";
|
||||||
|
return \%country;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub dump
|
sub dump
|
||||||
{
|
{
|
||||||
foreach my $iso_code (sort keys %country) {
|
my $country = shift @_;
|
||||||
&dump_one($iso_code);
|
|
||||||
|
foreach my $iso_code (sort keys %$country) {
|
||||||
|
&dump_one($iso_code, $country->{$iso_code});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub dump_one
|
sub dump_one
|
||||||
{
|
{
|
||||||
my $iso_code = shift @_;
|
my($iso_code, $country) = @_;
|
||||||
my($file, $fh_le, $fh_be);
|
my($file, $fh_le, $fh_be);
|
||||||
|
|
||||||
printf "%5u IPv6 ranges for %s %s\n",
|
printf "%5u IPv6 ranges for %s %s\n",
|
||||||
scalar(@{$country{$iso_code}{pool_v6}}),
|
scalar(@{$country->{pool_v6}}),
|
||||||
$iso_code, $country{$iso_code}{name};
|
$iso_code, $country->{name};
|
||||||
|
|
||||||
$file = "$target_dir/LE/".uc($iso_code).".iv6";
|
$file = "$target_dir/LE/".uc($iso_code).".iv6";
|
||||||
if (!open($fh_le, "> $file")) {
|
if (!open($fh_le, "> $file")) {
|
||||||
@@ -89,7 +92,7 @@ sub dump_one
|
|||||||
print STDERR "Error opening $file: $!\n";
|
print STDERR "Error opening $file: $!\n";
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
foreach my $range (@{$country{$iso_code}{pool_v6}}) {
|
foreach my $range (@{$country->{pool_v6}}) {
|
||||||
print $fh_be $range->[0], $range->[1];
|
print $fh_be $range->[0], $range->[1];
|
||||||
print $fh_le &ip6_swap($range->[0]), &ip6_swap($range->[1]);
|
print $fh_le &ip6_swap($range->[0]), &ip6_swap($range->[1]);
|
||||||
}
|
}
|
||||||
@@ -97,8 +100,8 @@ sub dump_one
|
|||||||
close $fh_be;
|
close $fh_be;
|
||||||
|
|
||||||
printf "%5u IPv4 ranges for %s %s\n",
|
printf "%5u IPv4 ranges for %s %s\n",
|
||||||
scalar(@{$country{$iso_code}{pool_v4}}),
|
scalar(@{$country->{pool_v4}}),
|
||||||
$iso_code, $country{$iso_code}{name};
|
$iso_code, $country->{name};
|
||||||
|
|
||||||
$file = "$target_dir/LE/".uc($iso_code).".iv4";
|
$file = "$target_dir/LE/".uc($iso_code).".iv4";
|
||||||
if (!open($fh_le, "> $file")) {
|
if (!open($fh_le, "> $file")) {
|
||||||
@@ -110,7 +113,7 @@ sub dump_one
|
|||||||
print STDERR "Error opening $file: $!\n";
|
print STDERR "Error opening $file: $!\n";
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
foreach my $range (@{$country{$iso_code}{pool_v4}}) {
|
foreach my $range (@{$country->{pool_v4}}) {
|
||||||
print $fh_le pack("VV", $range->[0], $range->[1]);
|
print $fh_le pack("VV", $range->[0], $range->[1]);
|
||||||
print $fh_be pack("NN", $range->[0], $range->[1]);
|
print $fh_be pack("NN", $range->[0], $range->[1]);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user