Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added minimal IPv6 support to Geo::IP

This only provides country_code_by_addr_v6() and ip_by_addr_v6()
support. Boris Zentner, Geo::IP maintainer has been working on IPv6
support. His code is more complete but not released to CPAN yet.
  • Loading branch information...
commit a3f322252134f20edaa46279921bee403e2e8445 1 parent b91ea1b
@cosimo authored
Showing with 103 additions and 24 deletions.
  1. +3 −0  Changes
  2. +18 −0 IP.xs
  3. +1 −0  MANIFEST
  4. +46 −24 lib/Geo/IP.pm
  5. +35 −0 t/4_lookup_ipv6.t
View
3  Changes
@@ -1,4 +1,7 @@
Revision history for Perl extension Geo::IP.
+1.38_01 Feb 24th 2011 [UNOFFICIAL]
+ - Added minimal IPv6 support waiting for 1.39 release on CPAN ( Cosimo Streppone )
+
1.38 May 16th 2009
- PurePerl region_by_addr and region_by_name return '00' for unknown regions, like the CAPI does ( Boris Zentner )
- Update to latest FIPS codes from 20090401 ( Boris Zentner )
View
18 IP.xs
@@ -56,6 +56,15 @@ id_by_addr(gi, addr)
RETVAL
int
+id_by_addr_v6(gi, addr)
+ GeoIP *gi
+ char * addr
+ CODE:
+ RETVAL = GeoIP_id_by_addr_v6(gi,addr);
+ OUTPUT:
+ RETVAL
+
+int
id_by_name(gi, name)
GeoIP *gi
char * name
@@ -82,6 +91,15 @@ country_code_by_addr(gi, addr)
RETVAL
const char *
+country_code_by_addr_v6(gi, addr)
+ GeoIP *gi
+ char * addr
+ CODE:
+ RETVAL = GeoIP_country_code_by_addr_v6(gi,addr);
+ OUTPUT:
+ RETVAL
+
+const char *
country_code_by_name(gi, name)
GeoIP *gi
char * name
View
1  MANIFEST
@@ -17,5 +17,6 @@ t/0_base.t
t/1_lookup.t
t/2_namelookup.t
t/3_mirror.t
+t/4_lookup_ipv6.t
t/cpan_mirror.txt
META.yml Module meta-data (added by MakeMaker)
View
70 lib/Geo/IP.pm
@@ -7,7 +7,7 @@ use vars qw($VERSION @EXPORT $GEOIP_PP_ONLY @ISA $XS_VERSION);
BEGIN { $GEOIP_PP_ONLY = 0 unless defined($GEOIP_PP_ONLY); }
BEGIN {
- $VERSION = '1.38';
+ $VERSION = '1.38_01';
eval {
# PERL_DL_NONLAZY must be false, or any errors in loading will just
@@ -38,17 +38,24 @@ BEGIN {
#my $pp = !( defined &_XScompiled && &_XScompiled && !$TESTING_PERL_ONLY );
my $pp = !defined &open;
- sub GEOIP_COUNTRY_EDITION() { 1; }
- sub GEOIP_CITY_EDITION_REV1() { 2; }
- sub GEOIP_REGION_EDITION_REV1() { 3; }
- sub GEOIP_ISP_EDITION() { 4; }
- sub GEOIP_ORG_EDITION() { 5; }
- sub GEOIP_CITY_EDITION_REV0() { 6; }
- sub GEOIP_REGION_EDITION_REV0() { 7; }
- sub GEOIP_PROXY_EDITION() { 8; }
- sub GEOIP_ASNUM_EDITION() { 9; }
- sub GEOIP_NETSPEED_EDITION() { 10; }
- sub GEOIP_DOMAIN_EDITION() { 11; }
+ sub GEOIP_COUNTRY_EDITION() { 1; }
+ sub GEOIP_CITY_EDITION_REV1() { 2; }
+ sub GEOIP_REGION_EDITION_REV1() { 3; }
+ sub GEOIP_ISP_EDITION() { 4; }
+ sub GEOIP_ORG_EDITION() { 5; }
+ sub GEOIP_CITY_EDITION_REV0() { 6; }
+ sub GEOIP_REGION_EDITION_REV0() { 7; }
+ sub GEOIP_PROXY_EDITION() { 8; }
+ sub GEOIP_ASNUM_EDITION() { 9; }
+ sub GEOIP_NETSPEED_EDITION() { 10; }
+ sub GEOIP_DOMAIN_EDITION() { 11; }
+ sub GEOIP_COUNTRY_EDITION_V6() { 12; }
+ sub GEOIP_LOCATIONA_EDITION() { 13; }
+ sub GEOIP_ACCURACYRADIUS_EDITION() { 14; }
+ sub GEOIP_CITYCONFIDENCE_EDITION() { 15; }
+ sub GEOIP_CITYCONFIDENCEDIST_EDITION() { 16; }
+ sub GEOIP_LARGE_COUNTRY_EDITION() { 17; }
+ sub GEOIP_LARGE_COUNTRY_EDITION_V6() { 18; }
sub GEOIP_CHARSET_ISO_8859_1() { 0; }
sub GEOIP_CHARSET_UTF8() { 1; }
@@ -5131,6 +5138,7 @@ sub open_type {
GEOIP_ASNUM_EDITION() => 'GeoIPASNum',
GEOIP_NETSPEED_EDITION() => 'GeoIPNetSpeed',
GEOIP_DOMAIN_EDITION() => 'GeoIPDomain',
+ GEOIP_COUNTRY_V6_EDITION() => 'GeoIPv6',
);
# backward compatibility for 2003 databases.
@@ -5284,6 +5292,7 @@ sub _setup_segments {
#if database Type is GEOIP_COUNTY_EDITION then use database segment GEOIP_COUNTRY_BEGIN
if ( $gi->{"databaseType"} == GEOIP_COUNTRY_EDITION
+ || $gi->{"databaseType"} == GEOIP_COUNTRY_EDITION_V6
|| $gi->{"databaseType"} == GEOIP_NETSPEED_EDITION ) {
$gi->{"databaseSegments"} = GEOIP_COUNTRY_BEGIN;
}
@@ -5353,6 +5362,14 @@ sub country_code_by_addr {
return $countries[ $gi->id_by_addr($ip_address) ];
}
+#this function returns the country code of ipv6 address
+sub country_code_by_addr_v6 {
+ my ( $gi, $ip_address ) = @_;
+ return unless $ip_address =~ m!^(?:[a-fA-f\d]{0,4}\:){1,8}$!;
+ return $countries[ $gi->country_code_by_addr_v6($ip_address) ];
+ #return $countries[ $gi->id_by_addr_v6($ip_address) ];
+}
+
#this function returns the country code3 of ip address
sub country_code3_by_addr {
my ( $gi, $ip_address ) = @_;
@@ -5767,17 +5784,20 @@ __PP_CODE__
print STDERR $@ if $@;
@EXPORT = qw(
- GEOIP_STANDARD GEOIP_MEMORY_CACHE
- GEOIP_CHECK_CACHE GEOIP_INDEX_CACHE
- GEOIP_UNKNOWN_SPEED GEOIP_DIALUP_SPEED
- GEOIP_CABLEDSL_SPEED GEOIP_CORPORATE_SPEED
- GEOIP_COUNTRY_EDITION GEOIP_REGION_EDITION_REV0
- GEOIP_CITY_EDITION_REV0 GEOIP_ORG_EDITION
- GEOIP_ISP_EDITION GEOIP_CITY_EDITION_REV1
- GEOIP_REGION_EDITION_REV1 GEOIP_PROXY_EDITION
- GEOIP_ASNUM_EDITION GEOIP_NETSPEED_EDITION
- GEOIP_CHARSET_ISO_8859_1 GEOIP_CHARSET_UTF8
- GEOIP_MMAP_CACHE
+ GEOIP_STANDARD GEOIP_MEMORY_CACHE
+ GEOIP_CHECK_CACHE GEOIP_INDEX_CACHE
+ GEOIP_UNKNOWN_SPEED GEOIP_DIALUP_SPEED
+ GEOIP_CABLEDSL_SPEED GEOIP_CORPORATE_SPEED
+ GEOIP_COUNTRY_EDITION GEOIP_REGION_EDITION_REV0
+ GEOIP_CITY_EDITION_REV0 GEOIP_ORG_EDITION
+ GEOIP_ISP_EDITION GEOIP_CITY_EDITION_REV1
+ GEOIP_REGION_EDITION_REV1 GEOIP_PROXY_EDITION
+ GEOIP_ASNUM_EDITION GEOIP_NETSPEED_EDITION
+ GEOIP_COUNTRY_EDITION_V6 GEOIP_LOCATIONA_EDITION
+ GEOIP_ACCURACYRADIUS_EDITION GEOIP_CITYCONFIDENCE_EDITION
+ GEOIP_CITYCONFIDENCEDIST_EDITION GEOIP_LARGE_COUNTRY_EDITION
+ GEOIP_LARGE_COUNTRY_EDITION_V6 GEOIP_CHARSET_ISO_8859_1
+ GEOIP_CHARSET_UTF8 GEOIP_MMAP_CACHE
);
1;
@@ -5959,7 +5979,9 @@ http://lists.sourceforge.net/lists/listinfo/geoip-perl
=head1 VERSION
-1.38
+1.38_01 w/ minimal IPv6 support
+
+https://github.com/cosimo/Geo-IP/
=head1 SEE ALSO
View
35 t/4_lookup_ipv6.t
@@ -0,0 +1,35 @@
+# -*- Mode: Perl; -*-
+
+use strict;
+use Test::More;
+
+$^W = 1;
+
+BEGIN { plan tests => 1 }
+
+use Geo::IP;
+
+my $gi;
+
+eval {
+ $gi = Geo::IP->open_type(GEOIP_COUNTRY_EDITION_V6);
+};
+
+# If no GeoIPv6.dat database is installed,
+# we have to skip all the tests
+SKIP: {
+
+ skip("No GeoIPv6.dat database installed?", 1)
+ unless defined $gi;
+
+ while (<DATA>) {
+ chomp;
+ my ($ipaddr, $exp_country) = split("\t");
+ my $country = $gi->country_code_by_addr_v6($ipaddr);
+ is(uc($country), $exp_country, "$ipaddr should resolve to '$exp_country'");
+ }
+
+}
+
+__DATA__
+2a01:e35:8bd9:8bb0:92b:8628:5ca5:5f2b FR
Please sign in to comment.
Something went wrong with that request. Please try again.