mirror of
git://git.code.sf.net/p/xtables-addons/xtables-addons
synced 2026-01-04 14:23:52 +01:00
74 lines
1.7 KiB
Perl
Executable File
74 lines
1.7 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
#
|
|
# Converter for MaxMind CSV database to binary, for xt_geoip
|
|
# Copyright © Jan Engelhardt <jengelh@medozas.de>, 2008
|
|
#
|
|
# Use -b argument to create big-endian tables.
|
|
#
|
|
use Getopt::Long;
|
|
use IO::Handle;
|
|
use Text::CSV_XS; # or trade for Text::CSV
|
|
use strict;
|
|
|
|
my %country;
|
|
my $csv = Text::CSV_XS->new({binary => 0, eol => $/}); # or Text::CSV
|
|
my $target_dir = ".";
|
|
|
|
&Getopt::Long::Configure(qw(bundling));
|
|
&GetOptions(
|
|
"D=s" => \$target_dir,
|
|
);
|
|
|
|
if (!-d $target_dir) {
|
|
print STDERR "Target directory $target_dir does not exist.\n";
|
|
exit 1;
|
|
}
|
|
foreach (qw(LE BE)) {
|
|
my $dir = "$target_dir/$_";
|
|
if (!-e $dir && !mkdir($dir)) {
|
|
print STDERR "Could not mkdir $dir: $!\n";
|
|
exit 1;
|
|
}
|
|
}
|
|
|
|
while (my $row = $csv->getline(*ARGV)) {
|
|
if (!defined($country{$row->[4]})) {
|
|
$country{$row->[4]} = {
|
|
name => $row->[5],
|
|
pool_v4 => [],
|
|
};
|
|
}
|
|
my $c = $country{$row->[4]}{pool_v4};
|
|
push(@$c, [$row->[2], $row->[3]]);
|
|
if ($. % 4096 == 0) {
|
|
print STDERR "\r\e[2K$. entries";
|
|
}
|
|
}
|
|
|
|
print STDERR "\r\e[2K$. entries total\n";
|
|
|
|
foreach my $iso_code (sort keys %country) {
|
|
my($file, $fh_le, $fh_be);
|
|
|
|
printf "%5u IPv4 ranges for %s %s\n",
|
|
scalar(@{$country{$iso_code}{pool_v4}}),
|
|
$iso_code, $country{$iso_code}{name};
|
|
|
|
$file = "$target_dir/LE/".uc($iso_code).".iv4";
|
|
if (!open($fh_le, "> $file")) {
|
|
print STDERR "Error opening $file: $!\n";
|
|
exit 1;
|
|
}
|
|
$file = "$target_dir/BE/".uc($iso_code).".iv4";
|
|
if (!open($fh_be, "> $file")) {
|
|
print STDERR "Error opening $file: $!\n";
|
|
exit 1;
|
|
}
|
|
foreach my $range (@{$country{$iso_code}{pool_v4}}) {
|
|
print $fh_le pack("VV", $range->[0], $range->[1]);
|
|
print $fh_be pack("NN", $range->[0], $range->[1]);
|
|
}
|
|
close $fh_le;
|
|
close $fh_be;
|
|
}
|