Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 Streppone authored February 24, 2011
3  Changes
... ...
@@ -1,4 +1,7 @@
1 1
 Revision history for Perl extension Geo::IP.
  2
+1.38_01 Feb 24th 2011 [UNOFFICIAL]
  3
+        - Added minimal IPv6 support waiting for 1.39 release on CPAN ( Cosimo Streppone )
  4
+
2 5
 1.38 May 16th 2009
3 6
         - PurePerl region_by_addr and region_by_name return '00' for unknown regions, like the CAPI does ( Boris Zentner )
4 7
         - Update to latest FIPS codes from 20090401 ( Boris Zentner )
18  IP.xs
@@ -56,6 +56,15 @@ id_by_addr(gi, addr)
56 56
 	RETVAL
57 57
 
58 58
 int
  59
+id_by_addr_v6(gi, addr)
  60
+	GeoIP *gi
  61
+	char * addr
  62
+    CODE:
  63
+	RETVAL = GeoIP_id_by_addr_v6(gi,addr);
  64
+    OUTPUT:
  65
+	RETVAL
  66
+
  67
+int
59 68
 id_by_name(gi, name)
60 69
 	GeoIP *gi
61 70
 	char * name
@@ -82,6 +91,15 @@ country_code_by_addr(gi, addr)
82 91
 	RETVAL
83 92
 
84 93
 const char *
  94
+country_code_by_addr_v6(gi, addr)
  95
+	GeoIP *gi
  96
+	char * addr
  97
+    CODE:
  98
+	RETVAL = GeoIP_country_code_by_addr_v6(gi,addr);
  99
+    OUTPUT:
  100
+	RETVAL
  101
+
  102
+const char *
85 103
 country_code_by_name(gi, name)
86 104
 	GeoIP *gi
87 105
 	char * name
1  MANIFEST
@@ -17,5 +17,6 @@ t/0_base.t
17 17
 t/1_lookup.t
18 18
 t/2_namelookup.t
19 19
 t/3_mirror.t
  20
+t/4_lookup_ipv6.t
20 21
 t/cpan_mirror.txt
21 22
 META.yml                                 Module meta-data (added by MakeMaker)
70  lib/Geo/IP.pm
@@ -7,7 +7,7 @@ use vars qw($VERSION @EXPORT  $GEOIP_PP_ONLY @ISA $XS_VERSION);
7 7
 BEGIN { $GEOIP_PP_ONLY = 0 unless defined($GEOIP_PP_ONLY); }
8 8
 
9 9
 BEGIN {
10  
-  $VERSION = '1.38';
  10
+  $VERSION = '1.38_01';
11 11
   eval {
12 12
 
13 13
     # PERL_DL_NONLAZY must be false, or any errors in loading will just
@@ -38,17 +38,24 @@ BEGIN {
38 38
   #my $pp = !( defined &_XScompiled && &_XScompiled && !$TESTING_PERL_ONLY );
39 39
   my $pp = !defined &open;
40 40
 
41  
-  sub GEOIP_COUNTRY_EDITION()     { 1; }
42  
-  sub GEOIP_CITY_EDITION_REV1()   { 2; }
43  
-  sub GEOIP_REGION_EDITION_REV1() { 3; }
44  
-  sub GEOIP_ISP_EDITION()         { 4; }
45  
-  sub GEOIP_ORG_EDITION()         { 5; }
46  
-  sub GEOIP_CITY_EDITION_REV0()   { 6; }
47  
-  sub GEOIP_REGION_EDITION_REV0() { 7; }
48  
-  sub GEOIP_PROXY_EDITION()       { 8; }
49  
-  sub GEOIP_ASNUM_EDITION()       { 9; }
50  
-  sub GEOIP_NETSPEED_EDITION()    { 10; }
51  
-  sub GEOIP_DOMAIN_EDITION()      { 11; }
  41
+  sub GEOIP_COUNTRY_EDITION()            { 1; }
  42
+  sub GEOIP_CITY_EDITION_REV1()          { 2; }
  43
+  sub GEOIP_REGION_EDITION_REV1()        { 3; }
  44
+  sub GEOIP_ISP_EDITION()                { 4; }
  45
+  sub GEOIP_ORG_EDITION()                { 5; }
  46
+  sub GEOIP_CITY_EDITION_REV0()          { 6; }
  47
+  sub GEOIP_REGION_EDITION_REV0()        { 7; }
  48
+  sub GEOIP_PROXY_EDITION()              { 8; }
  49
+  sub GEOIP_ASNUM_EDITION()              { 9; }
  50
+  sub GEOIP_NETSPEED_EDITION()           { 10; }
  51
+  sub GEOIP_DOMAIN_EDITION()             { 11; }
  52
+  sub GEOIP_COUNTRY_EDITION_V6()         { 12; }
  53
+  sub GEOIP_LOCATIONA_EDITION()          { 13; }
  54
+  sub GEOIP_ACCURACYRADIUS_EDITION()     { 14; }
  55
+  sub GEOIP_CITYCONFIDENCE_EDITION()     { 15; }
  56
+  sub GEOIP_CITYCONFIDENCEDIST_EDITION() { 16; }
  57
+  sub GEOIP_LARGE_COUNTRY_EDITION()      { 17; }
  58
+  sub GEOIP_LARGE_COUNTRY_EDITION_V6()   { 18; }
52 59
 
53 60
   sub GEOIP_CHARSET_ISO_8859_1() { 0; }
54 61
   sub GEOIP_CHARSET_UTF8()       { 1; }
@@ -5131,6 +5138,7 @@ sub open_type {
5131 5138
     GEOIP_ASNUM_EDITION()       => 'GeoIPASNum',
5132 5139
     GEOIP_NETSPEED_EDITION()    => 'GeoIPNetSpeed',
5133 5140
     GEOIP_DOMAIN_EDITION()      => 'GeoIPDomain',
  5141
+    GEOIP_COUNTRY_V6_EDITION()  => 'GeoIPv6',
5134 5142
   );
5135 5143
 
5136 5144
   # backward compatibility for 2003 databases.
@@ -5284,6 +5292,7 @@ sub _setup_segments {
5284 5292
 
5285 5293
 #if database Type is GEOIP_COUNTY_EDITION then use database segment GEOIP_COUNTRY_BEGIN
5286 5294
   if (    $gi->{"databaseType"} == GEOIP_COUNTRY_EDITION
  5295
+       || $gi->{"databaseType"} == GEOIP_COUNTRY_EDITION_V6
5287 5296
        || $gi->{"databaseType"} == GEOIP_NETSPEED_EDITION ) {
5288 5297
     $gi->{"databaseSegments"} = GEOIP_COUNTRY_BEGIN;
5289 5298
   }
@@ -5353,6 +5362,14 @@ sub country_code_by_addr {
5353 5362
   return $countries[ $gi->id_by_addr($ip_address) ];
5354 5363
 }
5355 5364
 
  5365
+#this function returns the country code of ipv6 address
  5366
+sub country_code_by_addr_v6 {
  5367
+  my ( $gi, $ip_address ) = @_;
  5368
+  return unless $ip_address =~ m!^(?:[a-fA-f\d]{0,4}\:){1,8}$!;
  5369
+  return $countries[ $gi->country_code_by_addr_v6($ip_address) ];
  5370
+  #return $countries[ $gi->id_by_addr_v6($ip_address) ];
  5371
+}
  5372
+
5356 5373
 #this function returns the country code3 of ip address
5357 5374
 sub country_code3_by_addr {
5358 5375
   my ( $gi, $ip_address ) = @_;
@@ -5767,17 +5784,20 @@ __PP_CODE__
5767 5784
 print STDERR $@ if $@;
5768 5785
 
5769 5786
 @EXPORT = qw(
5770  
-  GEOIP_STANDARD              GEOIP_MEMORY_CACHE
5771  
-  GEOIP_CHECK_CACHE           GEOIP_INDEX_CACHE
5772  
-  GEOIP_UNKNOWN_SPEED         GEOIP_DIALUP_SPEED
5773  
-  GEOIP_CABLEDSL_SPEED        GEOIP_CORPORATE_SPEED
5774  
-  GEOIP_COUNTRY_EDITION       GEOIP_REGION_EDITION_REV0
5775  
-  GEOIP_CITY_EDITION_REV0     GEOIP_ORG_EDITION
5776  
-  GEOIP_ISP_EDITION           GEOIP_CITY_EDITION_REV1
5777  
-  GEOIP_REGION_EDITION_REV1   GEOIP_PROXY_EDITION
5778  
-  GEOIP_ASNUM_EDITION         GEOIP_NETSPEED_EDITION
5779  
-  GEOIP_CHARSET_ISO_8859_1    GEOIP_CHARSET_UTF8
5780  
-  GEOIP_MMAP_CACHE
  5787
+  GEOIP_STANDARD                   GEOIP_MEMORY_CACHE
  5788
+  GEOIP_CHECK_CACHE                GEOIP_INDEX_CACHE
  5789
+  GEOIP_UNKNOWN_SPEED              GEOIP_DIALUP_SPEED
  5790
+  GEOIP_CABLEDSL_SPEED             GEOIP_CORPORATE_SPEED
  5791
+  GEOIP_COUNTRY_EDITION            GEOIP_REGION_EDITION_REV0
  5792
+  GEOIP_CITY_EDITION_REV0          GEOIP_ORG_EDITION
  5793
+  GEOIP_ISP_EDITION                GEOIP_CITY_EDITION_REV1
  5794
+  GEOIP_REGION_EDITION_REV1        GEOIP_PROXY_EDITION
  5795
+  GEOIP_ASNUM_EDITION              GEOIP_NETSPEED_EDITION
  5796
+  GEOIP_COUNTRY_EDITION_V6         GEOIP_LOCATIONA_EDITION
  5797
+  GEOIP_ACCURACYRADIUS_EDITION     GEOIP_CITYCONFIDENCE_EDITION
  5798
+  GEOIP_CITYCONFIDENCEDIST_EDITION GEOIP_LARGE_COUNTRY_EDITION
  5799
+  GEOIP_LARGE_COUNTRY_EDITION_V6   GEOIP_CHARSET_ISO_8859_1
  5800
+  GEOIP_CHARSET_UTF8               GEOIP_MMAP_CACHE
5781 5801
 );
5782 5802
 
5783 5803
 1;
@@ -5959,7 +5979,9 @@ http://lists.sourceforge.net/lists/listinfo/geoip-perl
5959 5979
 
5960 5980
 =head1 VERSION
5961 5981
 
5962  
-1.38
  5982
+1.38_01 w/ minimal IPv6 support
  5983
+
  5984
+https://github.com/cosimo/Geo-IP/
5963 5985
 
5964 5986
 =head1 SEE ALSO
5965 5987
 
35  t/4_lookup_ipv6.t
... ...
@@ -0,0 +1,35 @@
  1
+# -*- Mode: Perl; -*-
  2
+
  3
+use strict;
  4
+use Test::More;
  5
+
  6
+$^W = 1;
  7
+
  8
+BEGIN { plan tests => 1 }
  9
+
  10
+use Geo::IP;
  11
+
  12
+my $gi;
  13
+
  14
+eval {
  15
+    $gi = Geo::IP->open_type(GEOIP_COUNTRY_EDITION_V6);
  16
+};
  17
+
  18
+# If no GeoIPv6.dat database is installed,
  19
+# we have to skip all the tests
  20
+SKIP: {
  21
+   
  22
+  skip("No GeoIPv6.dat database installed?", 1)
  23
+    unless defined $gi;
  24
+
  25
+  while (<DATA>) {
  26
+    chomp;
  27
+    my ($ipaddr, $exp_country) = split("\t");
  28
+    my $country = $gi->country_code_by_addr_v6($ipaddr);
  29
+    is(uc($country), $exp_country, "$ipaddr should resolve to '$exp_country'");
  30
+  }
  31
+
  32
+}
  33
+
  34
+__DATA__
  35
+2a01:e35:8bd9:8bb0:92b:8628:5ca5:5f2b	FR

0 notes on commit a3f3222

Please sign in to comment.
Something went wrong with that request. Please try again.