| 1 | package Geo::IP6; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Geo::IP6 - get country code for an ipv6 or ipv4 address | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | Version 1.0 | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Geo::IP6; | ||
| 18 | |||
| 19 | my $geo = Geo::IP6->new(); # default currently uses maxmind GeoLite2 | ||
| 20 | my $geo = Geo::IP->new(db => 'software77'); # use software77 db | ||
| 21 | |||
| 22 | say $geo->cc("217.31.205.50"); # returns country code CZ | ||
| 23 | say $geo->cc("2001:1488:0:3::2"); # returns country code CZ | ||
| 24 | |||
| 25 | Hint: ipv6 network prefixes are sorted by network count internally for performance reasons, which may result in incorrect lookups. | ||
| 26 | Currently (2019-07-01) there is only one minor overlap (2001:1c00::/22,ZZ vs 2001:1c00::/23,NL) in software77 and none in GeoLite2 | ||
| 27 | ipv6db. If you want to avoid this condition use C<< $geo->cc_exact() >> for ipv6 addresses. There is no cc_exact for ipv4 which is | ||
| 28 | not affected. | ||
| 29 | |||
| 30 | say $geo->cc("2001:1c00::1"); # returns country code ZZ | ||
| 31 | say $geo->cc_exact("2001:1c00::1"); # returns country code NL | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This module provides functions to get the country code for ipv6 and ipv4 addresses | ||
| 36 | using IPV6 CIDR and IPV4 RANGE files in csv format provided by http://software77.net/faq.html#automated | ||
| 37 | and https://dev.maxmind.com/geoip/geoip2/geolite2/ | ||
| 38 | |||
| 39 | It depends on LMDB_FILE for internal storage and should run on 32bit and 64bit sytems as the databases | ||
| 40 | are quite small (around 6MB each). LMDB is not platform independent and may not be portable. You can | ||
| 41 | use the geoip6 cmdline tool to recreate all databases. | ||
| 42 | |||
| 43 | Initial setup: | ||
| 44 | If there are no geoip4-maxmind.lmdb/geoip4-software77.lmdb and geoip6-*.lmdb files in /usr/share/geoip6/ | ||
| 45 | create them with the geoip6 cmdline tool: | ||
| 46 | |||
| 47 | geoip6 update_maxmind | ||
| 48 | |||
| 49 | geoip6 update_db4_software77 | ||
| 50 | geoip6 update_db6_software77 | ||
| 51 | |||
| 52 | =head1 EXPORT | ||
| 53 | |||
| 54 | This module does not export any functions | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | our $VERSION = '1.0'; | ||
| 59 | |||
| 60 | require Exporter; | ||
| 61 | our @ISA = qw(Exporter); | ||
| 62 | our @EXPORT = qw(); | ||
| 63 | # our @EXPORT_OK = qw(); | ||
| 64 | |||
| 65 | 1 | ▶ | use Socket qw(inet_pton inet_ntop AF_INET6 AF_INET); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 66 | 1 | ▶ | use LMDB_File qw(:flags :cursor_op); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 67 | |||
| 68 | our @cidr6; | ||
| 69 | our @cidr6_exact; | ||
| 70 | our $ip4db_file_software77 = '/usr/share/geoip6/geoip4-software77.lmdb'; | ||
| 71 | our $ip4db_file_maxmind = '/usr/share/geoip6/geoip4-maxmind.lmdb'; | ||
| 72 | our $ip4db_url_software77 = 'http://software77.net/geo-ip/?DL=1'; | ||
| 73 | our $ip4db_url_maxmind = 'https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip'; | ||
| 74 | our $ip4db_size = 32 * 1024 * 1024; | ||
| 75 | our $ip6db_file_software77 = '/usr/share/geoip6/geoip6-software77.lmdb'; | ||
| 76 | our $ip6db_file_maxmind = '/usr/share/geoip6/geoip6-maxmind.lmdb'; | ||
| 77 | our $ip6db_url_software77 = 'http://software77.net/geo-ip/?DL=9'; | ||
| 78 | our $ip6db_url_maxmind = 'https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip'; | ||
| 79 | our $ip6db_size = 32 * 1024 * 1024; | ||
| 80 | our $ip4db_file; | ||
| 81 | our $ip6db_file; | ||
| 82 | our %opt; | ||
| 83 | |||
| 84 | our %ip6db; | ||
| 85 | our $ip4env; | ||
| 86 | |||
| 87 | |||
| 88 | =head1 SUBROUTINES/METHODS | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | |||
| 93 | sub int_ip4 ($) { | ||
| 94 | return join '.', unpack 'C4', pack 'N', $_[0]; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub ip4_int ($) { | ||
| 98 | return unpack('N', inet_pton(AF_INET, $_[0])); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub v4country_init { | ||
| 102 | |||
| 103 | $ip4env = LMDB::Env->new($ip4db_file, { mapsize => 32 * 1024 * 1024, maxdbs => 1, maxreaders => 128, mode => 0644, flags => MDB_NOSUBDIR|MDB_RDONLY }); | ||
| 104 | if($opt{'debug'}){ | ||
| 105 | if($ip4env){ print "Loaded database $ip4db_file\n"; } | ||
| 106 | else { print "Failed to load database $ip4db_file\n"; } | ||
| 107 | } | ||
| 108 | return undef if !$ip4env; | ||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub v4country ($) { | ||
| 113 | |||
| 114 | my $ip4 = shift || return undef; | ||
| 115 | my $ipnum = ip4_int($ip4); | ||
| 116 | |||
| 117 | my $txn = $ip4env->BeginTxn(); | ||
| 118 | my $db = $txn->OpenDB({ dbname => undef, flags => MDB_INTEGERKEY}); | ||
| 119 | my $cursor = $db->Cursor; | ||
| 120 | my ($dbkey, $dbkey_ip4, $dbdata, $dbkey_last, $dbkey_last_ip4, $cc); | ||
| 121 | |||
| 122 | $dbkey = $ipnum; | ||
| 123 | $cursor->get($dbkey, $dbdata, MDB_SET_RANGE); | ||
| 124 | return undef if !$dbkey; | ||
| 125 | |||
| 126 | ($dbkey_last, $cc) = split(/;/, $dbdata); | ||
| 127 | $dbkey_ip4 = int_ip4($dbkey); | ||
| 128 | $dbkey_last_ip4 = int_ip4($dbkey_last); | ||
| 129 | |||
| 130 | print "ip4:$ip4 = $ipnum, dbkey:$dbkey = $dbkey_ip4, dbkey_last:$dbkey_last = $dbkey_last_ip4, cc:$cc\n" if $opt{'debug'}; | ||
| 131 | |||
| 132 | if($ipnum eq $dbkey){ | ||
| 133 | my($net_last,$cc) = split(/;/, $dbdata); | ||
| 134 | return $cc; | ||
| 135 | } | ||
| 136 | |||
| 137 | ($dbkey, $dbkey_ip4, $dbdata, $dbkey_last, $dbkey_last_ip4, $cc) = undef; | ||
| 138 | $cursor->get($dbkey, $dbdata, MDB_PREV); | ||
| 139 | return undef if !$dbkey; | ||
| 140 | |||
| 141 | ($dbkey_last, $cc) = split(/;/, $dbdata); | ||
| 142 | $dbkey_ip4 = int_ip4($dbkey); | ||
| 143 | $dbkey_last_ip4 = int_ip4($dbkey_last); | ||
| 144 | |||
| 145 | print "ip4:$ip4 = $ipnum, dbkey:$dbkey = $dbkey_ip4, dbkey_last:$dbkey_last = $dbkey_last_ip4, cc:$cc\n" if $opt{'debug'}; | ||
| 146 | |||
| 147 | if($dbkey <= $ipnum && $dbkey_last >= $ipnum){ return $cc; } | ||
| 148 | |||
| 149 | return undef; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub v6country_init { | ||
| 154 | |||
| 155 | # load cidr order optimized by network count | ||
| 156 | foreach (split(/,/, $ip6db{'CIDRS'})){ | ||
| 157 | push(@cidr6, [$_, 1 x $_ . 0 x (128 - $_)]); | ||
| 158 | } | ||
| 159 | |||
| 160 | # load exact cidr order (small net to big net) | ||
| 161 | foreach ( sort { $b <=> $a } split(/,/, $ip6db{'CIDRS'})){ | ||
| 162 | push(@cidr6_exact, [$_, 1 x $_ . 0 x (128 - $_)]); | ||
| 163 | } | ||
| 164 | |||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | sub v6country ($;$) { | ||
| 169 | |||
| 170 | my $ip6 = shift || return undef; | ||
| 171 | my $exact = shift; | ||
| 172 | |||
| 173 | if(!$exact){ | ||
| 174 | foreach(@cidr6){ | ||
| 175 | my $net = $_->[0] . '/' . inet_ntop(AF_INET6, pack('B128', (unpack('B128', inet_pton(AF_INET6, $ip6)) & $_->[1]))); | ||
| 176 | print "search $net\n" if $opt{'debug'}; | ||
| 177 | my $p = $ip6db{ $net }; | ||
| 178 | return $p if $p; | ||
| 179 | } | ||
| 180 | } else { | ||
| 181 | |||
| 182 | foreach(@cidr6_exact){ | ||
| 183 | my $net = $_->[0] . '/' . inet_ntop(AF_INET6, pack('B128', (unpack('B128', inet_pton(AF_INET6, $ip6)) & $_->[1]))); | ||
| 184 | print "search $net\n" if $opt{'debug'}; | ||
| 185 | my $p = $ip6db{ $net }; | ||
| 186 | return $p if $p; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub net2cidr ($$) { | ||
| 192 | |||
| 193 | my $self = shift; | ||
| 194 | my $net = shift; | ||
| 195 | my $res = []; | ||
| 196 | |||
| 197 | foreach(@cidr6_exact){ | ||
| 198 | my $cc = $ip6db{ $_->[0] . '/' . $net}; | ||
| 199 | push(@$res, "$net/$_->[0]=$cc") if $cc; | ||
| 200 | } | ||
| 201 | |||
| 202 | return $res; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | =head2 cc | ||
| 208 | |||
| 209 | Lookup country code for ipv6 or ipv4 address | ||
| 210 | |||
| 211 | my $cc = $geo->cc("2001:1488:0:3::2"); # result: CZ | ||
| 212 | my $cc = $geo->cc("217.31.205.50"); # result: CZ | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | sub cc ($$) { | ||
| 217 | my ($self, $ip) = @_; | ||
| 218 | if($ip =~ /^[a-f0-9:]{3,39}$/){ return v6country($ip) || 'ZZ' } | ||
| 219 | elsif($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/o){ return v4country($ip) || 'ZZ' } | ||
| 220 | else { Carp::carp "invalid ip: <$ip>\n" } | ||
| 221 | } | ||
| 222 | |||
| 223 | =head2 cc_exact | ||
| 224 | |||
| 225 | Lookup exact country code for ipv6. See SYNOPSIS for overlap information. | ||
| 226 | |||
| 227 | my $cc = $geo->cc_exact("2001:1c00::1"); # result: NL | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | sub cc_exact ($$) { | ||
| 232 | my ($self, $ip) = @_; | ||
| 233 | if($ip =~ /^[a-f0-9:]{3,39}$/){ return v6country($ip, 1) || 'ZZ' } | ||
| 234 | elsif($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/o){ return v4country($ip) || 'ZZ' } | ||
| 235 | else { Carp::carp "invalid ip: <$ip>\n" } | ||
| 236 | } | ||
| 237 | |||
| 238 | =head2 new | ||
| 239 | |||
| 240 | my $geo = Geo::IP6->new(); | ||
| 241 | |||
| 242 | my $geo = Geo::IP6->new(db => 'maxmind', memcache => 0, debug => 0, ip4db_file => undef, ip6db_file => undef); | ||
| 243 | |||
| 244 | |||
| 245 | =over 4 | ||
| 246 | |||
| 247 | =item db | ||
| 248 | |||
| 249 | If not set, C<< maxmind >> is currently used. You can choose C<< maxmind >> (GeoIP2Lite) or C<< software77 >> ipv4/6 databases. | ||
| 250 | |||
| 251 | =item memcache | ||
| 252 | |||
| 253 | It is possible to load the ipv6 data into memory (probably not needed as LMDB is very fast). | ||
| 254 | Ipv4 dataset is using ip-ranges (integers) instead of netmasks (CIDR). There is no memcache option. | ||
| 255 | |||
| 256 | =item debug | ||
| 257 | |||
| 258 | Print debug output for ipv4 addresses (mainly convert ipv4 to integers back and forth to show network ranges) | ||
| 259 | |||
| 260 | [user@host ~]$ geoip6 217.31.205.50 | ||
| 261 | ip4:217.31.205.50 = 3642740018, dbkey:3642740736 = 217.31.208.0, dbkey_last:3642744831 = 217.31.223.255, cc:DE | ||
| 262 | ip4:217.31.205.50 = 3642740018, dbkey:3642736640 = 217.31.192.0, dbkey_last:3642740735 = 217.31.207.255, cc:CZ | ||
| 263 | CZ | ||
| 264 | |||
| 265 | [user@host ~]# geoip6 -debug 2001:1488:0:3::2 | ||
| 266 | Loaded database /usr/share/geoip6/geoip4-maxmind.lmdb | ||
| 267 | Loaded database /usr/share/geoip6/geoip6-maxmind.lmdb | ||
| 268 | search 32/2001:1488:: | ||
| 269 | search 48/2001:1488:: | ||
| 270 | search 29/2001:1488:: | ||
| 271 | CZ | ||
| 272 | |||
| 273 | =item ip4db_file | ||
| 274 | |||
| 275 | Manually set path to ip4 database file, e.g. /usr/share/geoip6/geoip4-maxmind.lmdb | ||
| 276 | |||
| 277 | =item ip6db_file | ||
| 278 | |||
| 279 | Manually set path to ip6 database file, e.g. /usr/share/geoip6/geoip6-software77.lmdb | ||
| 280 | |||
| 281 | =back | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub new { | ||
| 286 | my ($class, %copt) = @_; | ||
| 287 | %opt = %copt; | ||
| 288 | my $self = {}; | ||
| 289 | bless $self, $class; | ||
| 290 | |||
| 291 | $opt{'db'} = 'maxmind' if !$opt{'db'}; | ||
| 292 | $opt{'debug'} = 0 if !$opt{'db'}; | ||
| 293 | $opt{'memcache'} = 0 if !$opt{'memcache'}; | ||
| 294 | |||
| 295 | # ipv4 | ||
| 296 | if($opt{'db'} eq 'software77'){ $ip4db_file = $ip4db_file_software77 } else { $ip4db_file = $ip4db_file_maxmind } | ||
| 297 | $ip4db_file = $opt{'ip4db_file'} if $opt{'ipdb4_file'}; | ||
| 298 | Carp::croak "ip4db_file $ip4db_file not found\n" if !-e $ip4db_file; | ||
| 299 | |||
| 300 | my $v4status = v4country_init(); | ||
| 301 | return undef if !$v4status; | ||
| 302 | |||
| 303 | # ipv6 | ||
| 304 | if($opt{'db'} eq 'software77'){ $ip6db_file = $ip6db_file_software77 } else { $ip6db_file = $ip6db_file_maxmind } | ||
| 305 | $ip6db_file = $opt{'ip6db_file'} if $opt{'ip6db_file'}; | ||
| 306 | |||
| 307 | Carp::croak "ip6db_file $ip6db_file not found\n" if !-e $ip6db_file; | ||
| 308 | |||
| 309 | if($opt{'memcache'}){ | ||
| 310 | my $start = time; | ||
| 311 | my %db; | ||
| 312 | tie %db, 'LMDB_File', $ip6db_file, { mapsize => $ip6db_size, flags => MDB_NOSUBDIR|MDB_RDONLY }; | ||
| 313 | return undef if !%db; | ||
| 314 | while( my($key, $val) = each(%db) ){ | ||
| 315 | $ip6db{$key} = $val; | ||
| 316 | } | ||
| 317 | untie %db; | ||
| 318 | my $dur = time - $start; | ||
| 319 | if($opt{'debug'}){ print "memcache is read, duration $dur secs\n"; } | ||
| 320 | } else { | ||
| 321 | tie %ip6db, 'LMDB_File', $ip6db_file, { mapsize => $ip6db_size, flags => MDB_NOSUBDIR|MDB_RDONLY }; | ||
| 322 | return undef if !%ip6db; | ||
| 323 | } | ||
| 324 | |||
| 325 | if($opt{'debug'}){ | ||
| 326 | if(%ip6db){ print "Loaded database $ip6db_file\n"; } | ||
| 327 | else { print "Failed to load database $ip6db_file\n"; } | ||
| 328 | } | ||
| 329 | |||
| 330 | v6country_init(); | ||
| 331 | |||
| 332 | return $self; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub DESTROY { | ||
| 336 | untie %ip6db if !$opt{'memcache'}; | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | |||
| 341 | =head1 geoip6 cmdline tool | ||
| 342 | |||
| 343 | This module includes the geoip6 program that can be used to get the country code of ipv6 and ipv4 addresses on the command line. | ||
| 344 | |||
| 345 | [user@host ~] geoip6 | ||
| 346 | |||
| 347 | geoip6 [options] 2001:1488:0:3::2 # returns country code CZ (2001:1488:0:3::2 = nic.cz ipv6) | ||
| 348 | geoip6 217.31.205.50 # returns country code CZ (217.31.205.50 = nic.cz ipv4) | ||
| 349 | # options: -exact (slow but exact lookup for ipv4) | ||
| 350 | # options: -debug (enable debug) | ||
| 351 | # options: -memcache (load ipv6 data into ram) | ||
| 352 | # options: -software77 (use software77 ip database) | ||
| 353 | # options: -maxmind (use maxmind ip database; default) | ||
| 354 | |||
| 355 | * Hint: ipv6 network prefixes are sorted by network count for performance reasons, which may result in incorrect lookups. | ||
| 356 | * Currently (2019-07-01) there is only one minor overlap (2001:1c00::/22,ZZ vs 2001:1c00::/23,NL) | ||
| 357 | * If you want to avoid this condition use "-exact" for ipv6 addresses. There is no "-exact" for ipv4 which is not affected. | ||
| 358 | |||
| 359 | geoip6 update_maxmind # update using maxmind GeoLite2 Country (ipv4 and ipv6 databases) | ||
| 360 | |||
| 361 | geoip6 update_db6_software77 # update ipv6 networks from http://software77.net/faq.html#automated | ||
| 362 | # needs /usr/bin/curl | /usr/bin/wget, /usr/bin/gunzip, /usr/bin/unzip | ||
| 363 | |||
| 364 | geoip6 update_db4_software77 # update ipv4 networks (same as above) | ||
| 365 | |||
| 366 | geoip6 verify_db6 # show software77 ipv6 overlaps (ipv4 is not affected); see "geoip6 [-exact]" | ||
| 367 | |||
| 368 | geoip6 version # show version number | ||
| 369 | |||
| 370 | * geoip6 uses the IpToCountry database from http://software77.net/geo-ip/ which is donationware | ||
| 371 | * See license http://software77.net/geo-ip/?license and FAQ http://software77.net/faq.html | ||
| 372 | |||
| 373 | * geoip6 uses GeoLite2 Country data created by MaxMind, available from https://www.maxmind.com/ | ||
| 374 | * See https://dev.maxmind.com/geoip/geoip2/geolite2/ and https://creativecommons.org/licenses/by-sa/4.0/ | ||
| 375 | |||
| 376 | |||
| 377 | |||
| 378 | =head1 AUTHOR | ||
| 379 | |||
| 380 | GCORE, C<< <cpan at gcore.de> >> | ||
| 381 | |||
| 382 | =head1 BUGS | ||
| 383 | |||
| 384 | Please report any bugs or feature requests to C<bug-geo-ip6 at rt.cpan.org>, or through | ||
| 385 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geo-IP6>. I will be notified, and then you'll | ||
| 386 | automatically be notified of progress on your bug as I make changes. | ||
| 387 | |||
| 388 | |||
| 389 | =head1 SUPPORT | ||
| 390 | |||
| 391 | You can find documentation for this module with the perldoc command. | ||
| 392 | |||
| 393 | perldoc Geo::IP6 | ||
| 394 | |||
| 395 | |||
| 396 | You can also look for information at: | ||
| 397 | |||
| 398 | =over 4 | ||
| 399 | |||
| 400 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 401 | |||
| 402 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-IP6> | ||
| 403 | |||
| 404 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 405 | |||
| 406 | L<http://annocpan.org/dist/Geo-IP6> | ||
| 407 | |||
| 408 | =item * CPAN Ratings | ||
| 409 | |||
| 410 | L<https://cpanratings.perl.org/d/Geo-IP6> | ||
| 411 | |||
| 412 | =item * Search CPAN | ||
| 413 | |||
| 414 | L<https://metacpan.org/release/Geo-IP6> | ||
| 415 | |||
| 416 | =back | ||
| 417 | |||
| 418 | |||
| 419 | =head1 ACKNOWLEDGEMENTS | ||
| 420 | |||
| 421 | The IpToCountry databases are provided by http://software77.net/geo-ip/ and are donationware. | ||
| 422 | Software77.net is asking for donations to cope with hosting costs, see | ||
| 423 | |||
| 424 | L<< http://software77.net/geo-ip/?license >> and L<< http://software77.net/faq.html >> | ||
| 425 | |||
| 426 | GeoLite2 data is created by MaxMind, see L<< https://www.maxmind.com >> and L<< https://dev.maxmind.com/geoip/geoip2/geolite2/ >> | ||
| 427 | |||
| 428 | |||
| 429 | |||
| 430 | =head1 LICENSE AND COPYRIGHT | ||
| 431 | |||
| 432 | This software is Copyright (c) 2019 by GCore GmbH. | ||
| 433 | L<https://gcore.de/> | ||
| 434 | |||
| 435 | This is free software, licensed under: | ||
| 436 | |||
| 437 | The Artistic License 2.0 (GPL Compatible) | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | 1; # End of Geo::IP6 | ||