]>
Commit | Line | Data |
---|---|---|
ca842e18 SS |
1 | #!/usr/bin/perl |
2 | # | |
3 | # Converter for MaxMind CSV database to binary, for xt_geoip | |
4 | # Copyright © Jan Engelhardt, 2008-2011 | |
5 | # | |
6 | use Getopt::Long; | |
7 | use IO::Handle; | |
8 | use Text::CSV_XS; # or trade for Text::CSV | |
9 | use strict; | |
10 | ||
11 | my $csv = Text::CSV_XS->new({ | |
12 | allow_whitespace => 1, | |
13 | binary => 1, | |
14 | eol => $/, | |
15 | }); # or Text::CSV | |
16 | my $target_dir = "."; | |
17 | ||
18 | &Getopt::Long::Configure(qw(bundling)); | |
19 | &GetOptions( | |
20 | "D=s" => \$target_dir, | |
21 | ); | |
22 | ||
23 | if (!-d $target_dir) { | |
24 | print STDERR "Target directory $target_dir does not exist.\n"; | |
25 | exit 1; | |
26 | } | |
27 | ||
28 | my $dir = "$target_dir/LE"; | |
29 | if (!-e $dir && !mkdir($dir)) { | |
30 | print STDERR "Could not mkdir $dir: $!\n"; | |
31 | exit 1; | |
32 | } | |
33 | ||
34 | &dump(&collect()); | |
35 | ||
36 | sub collect | |
37 | { | |
38 | my %country; | |
39 | ||
40 | while (my $row = $csv->getline(*ARGV)) { | |
41 | if (!defined($country{$row->[4]})) { | |
42 | $country{$row->[4]} = { | |
43 | name => $row->[5], | |
44 | pool_v4 => [], | |
45 | pool_v6 => [], | |
46 | }; | |
47 | } | |
48 | my $c = $country{$row->[4]}; | |
49 | ||
50 | push(@{$c->{pool_v4}}, [$row->[2], $row->[3]]); | |
51 | ||
52 | if ($. % 4096 == 0) { | |
53 | print STDERR "\r\e[2K$. entries"; | |
54 | } | |
55 | } | |
56 | ||
57 | print STDERR "\r\e[2K$. entries total\n"; | |
58 | return \%country; | |
59 | } | |
60 | ||
61 | sub dump | |
62 | { | |
63 | my $country = shift @_; | |
64 | ||
65 | foreach my $iso_code (sort keys %$country) { | |
66 | &dump_one($iso_code, $country->{$iso_code}); | |
67 | } | |
68 | } | |
69 | ||
70 | sub dump_one | |
71 | { | |
72 | my($iso_code, $country) = @_; | |
73 | my($file, $fh_le, $fh_be); | |
74 | ||
75 | printf "%5u IPv4 ranges for %s %s\n", | |
76 | scalar(@{$country->{pool_v4}}), | |
77 | $iso_code, $country->{name}; | |
78 | ||
79 | $file = "$target_dir/LE/".uc($iso_code).".iv4"; | |
80 | if (!open($fh_le, "> $file")) { | |
81 | print STDERR "Error opening $file: $!\n"; | |
82 | exit 1; | |
83 | } | |
84 | foreach my $range (@{$country->{pool_v4}}) { | |
85 | print $fh_le pack("VV", $range->[0], $range->[1]); | |
86 | #print $fh_be pack("NN", $range->[0], $range->[1]); | |
87 | } | |
88 | close $fh_le; | |
89 | } |