]> git.ipfire.org Git - thirdparty/xtables-addons.git/commitdiff
geoip: simplify handling table column names
authorPhilip Prindeville <philipp@redfish-solutions.com>
Mon, 30 Apr 2018 00:06:05 +0000 (02:06 +0200)
committerJan Engelhardt <jengelh@inai.de>
Mon, 30 Apr 2018 07:41:29 +0000 (09:41 +0200)
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
geoip/xt_geoip_build

index f71d3f6b1455e930cb973ea25828a7f1badb3800..1955fbdb7583554a54b211e8b9cf72386ddabc2b 100755 (executable)
@@ -58,6 +58,8 @@ sub loadCountries
 {
        my $file = "$dir/GeoLite2-Country-Locations-en.csv";
 
+       sub id; sub cc; sub long; sub ct; sub cn;
+
        %countryId = ();
        %countryName = ();
 
@@ -79,19 +81,24 @@ sub loadCountries
        # verify that the columns we need are present
        map { die "Table has no $pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
 
-       my $id = $header{geoname_id};
-       my $cc = $header{country_iso_code};
-       my $long = $header{country_name};
-       my $ct = $header{continent_code};
-       my $cn = $header{continent_name};
+       my %remapping = (
+               id => 'geoname_id',
+               cc => 'country_iso_code',
+               long => 'country_name',
+               ct => 'continent_code',
+               cn => 'continent_name',
+       );
+
+       # now create a function which returns the value of that column #
+       map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
 
        while (my $row = $csv->getline($fh)) {
-               if ($row->[$cc] eq '' && $row->[$long] eq '') {
-                       $countryId{$row->[$id]} = $row->[$ct];
-                       $countryName{$row->[$ct]} = $row->[$cn];
+               if ($row->[cc] eq '' && $row->[long] eq '') {
+                       $countryId{$row->[id]} = $row->[ct];
+                       $countryName{$row->[ct]} = $row->[cn];
                } else {
-                       $countryId{$row->[$id]} = $row->[$cc];
-                       $countryName{$row->[$cc]} = $row->[$long];
+                       $countryId{$row->[id]} = $row->[cc];
+                       $countryName{$row->[cc]} = $row->[long];
                }
        }
 
@@ -100,6 +107,9 @@ sub loadCountries
        $countryName{O1} = 'Other Country';
 
        close($fh);
+
+       # clean up the namespace
+       undef &id; undef &cc; undef &long; undef &ct; undef &cn;
 }
 
 sub lookupCountry
@@ -121,9 +131,11 @@ sub lookupCountry
 
 sub collect
 {
-       my ($file, $fh, $net, $id, $rid, $proxy, $sat, $row);
+       my ($file, $fh, $row);
        my (%country, %header);
 
+       sub net; sub id; sub rid; sub proxy; sub sat;
+
        my %pairs = (
                network => 'Network',
                registered_country_geoname_id => 'Registered Country ID',
@@ -152,17 +164,22 @@ sub collect
        # verify that the columns we need are present
        map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
 
-       $net = $header{network};
-       $id = $header{geoname_id};
-       $rid = $header{registered_country_geoname_id};
-       $proxy = $header{is_anonymous_proxy};
-       $sat = $header{is_satellite_provider};
+       my %remapping = (
+               net => 'network',
+               id => 'geoname_id',
+               rid => 'registered_country_geoname_id',
+               proxy => 'is_anonymous_proxy',
+               sat => 'is_satellite_provider',
+       );
+
+       # now create a function which returns the value of that column #
+       map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
 
        while ($row = $csv->getline($fh)) {
                my ($cc, $cidr);
 
-               $cc = lookupCountry($row->[$id], $row->[$rid], $row->[$proxy], $row->[$sat]);
-               $cidr = $row->[$net];
+               $cc = lookupCountry($row->[id], $row->[rid], $row->[proxy], $row->[sat]);
+               $cidr = $row->[net];
                $country{$cc}->{pool_v4}->add($cidr);
 
                if ($. % 4096 == 0) {
@@ -174,6 +191,9 @@ sub collect
 
        close($fh);
 
+       # clean up the namespace
+       undef &net; undef &id; undef &rid; undef &proxy; undef &sat;
+
        $file = "$dir/GeoLite2-Country-Blocks-IPv6.csv";
 
        open($fh, '<', $file) || die "Can't open IPv6 database\n";
@@ -186,17 +206,15 @@ sub collect
        # verify that the columns we need are present
        map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
 
-       $net = $header{network};
-       $id = $header{geoname_id};
-       $rid = $header{registered_country_geoname_id};
-       $proxy = $header{is_anonymous_proxy};
-       $sat = $header{is_satellite_provider};
+       # unlikely the IPv6 table has different columns, but just to be sure
+       # create a function which returns the value of that column #
+       map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
 
        while ($row = $csv->getline($fh)) {
                my ($cc, $cidr);
 
-               $cc = lookupCountry($row->[$id], $row->[$rid], $row->[$proxy], $row->[$sat]);
-               $cidr = $row->[$net];
+               $cc = lookupCountry($row->[id], $row->[rid], $row->[proxy], $row->[sat]);
+               $cidr = $row->[net];
                $country{$cc}->{pool_v6}->add($cidr);
 
                if ($. % 4096 == 0) {
@@ -208,6 +226,9 @@ sub collect
 
        close($fh);
 
+       # clean up the namespace
+       undef &net; undef &id; undef &rid; undef &proxy; undef &sat;
+
        return \%country;
 }