]> git.ipfire.org Git - ipfire-2.x.git/blame - src/scripts/xt_geoip_build
Update libvirt to 2.1
[ipfire-2.x.git] / src / scripts / xt_geoip_build
CommitLineData
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#
6use Getopt::Long;
7use IO::Handle;
8use Text::CSV_XS; # or trade for Text::CSV
9use strict;
10
11my $csv = Text::CSV_XS->new({
12 allow_whitespace => 1,
13 binary => 1,
14 eol => $/,
15}); # or Text::CSV
16my $target_dir = ".";
17
18&Getopt::Long::Configure(qw(bundling));
19&GetOptions(
20 "D=s" => \$target_dir,
21);
22
23if (!-d $target_dir) {
24 print STDERR "Target directory $target_dir does not exist.\n";
25 exit 1;
26}
27
28my $dir = "$target_dir/LE";
29if (!-e $dir && !mkdir($dir)) {
30 print STDERR "Could not mkdir $dir: $!\n";
31 exit 1;
32}
33
34&dump(&collect());
35
36sub 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
61sub 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
70sub 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}