blib/lib/Geo/IPfree.pm

filter: Statementstmt 94.2% 165 / 175 Branchbran 77.5% 62 / 80 Conditioncond 57.8% 11 / 19 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 93.7% 15 / 16 Podpod 100% 11 / 11 totaltotal 85.3% 267 / 313 CC 49 SCAR 25.8
CC
49
Cov
87%
LookUp
27.0
LoadDB
9.5
baseX2dec
6.0
CRAP 13.1
↑ Summary
1 package Geo::IPfree;
2 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 require Exporter;
9 8 our @ISA = qw(Exporter);
10  
11 8 our $VERSION = '1.160001'; # VERSION
12  
13 # ABSTRACT: Geo::IPfree - Look up the country of an IPv4 address
14  
15 8 our @EXPORT = qw(LookUp LoadDB);
16 8 our @EXPORT_OK = @EXPORT;
17  
18 8 my $DEFAULT_DB = 'ipscountry.dat';
19 8 my $cache_expire = 5000;
20 8 my @baseX = (
21 0 .. 9,
22 'A' .. 'Z',
23 'a' .. 'z',
24 split( m{}, q(.,;'"`<>{}[]=+-~*@#%$&!?) )
25 );
26  
27 8 my ( %baseX, $base, $THIS, %countrys, $base0, $base1, $base2, $base3, $base4 );
28  
29 {
30 8 my $c = 0;
31 8 %baseX = map { $_ => ( $c++ ) } @baseX;
32 8 $base = @baseX;
33 8 $base0 = $base**0;
34 8 $base1 = $base**1;
35 8 $base2 = $base**2;
36 8 $base3 = $base**3;
37 8 $base4 = $base**4;
38  
39 8 my @data;
40 8 ▶ while (<DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 2160 0 2160
41 2160 ▶ last if m{^__END__};
Branch: 1 branch
BranchTrueFalseTotal
if /^__END__/ 8 2152 2160
42 2152 chomp;
43 2152 push @data, split m{ }, $_, 2;
44 }
45 8 %countrys = @data;
46 }
47  
48 5 ▶sub new {
Subroutine new: called CC=4 SCAR=15.2
Pod documented
49 5 my ( $class, $db_file ) = @_;
50  
51 5 ▶ if ( !defined $_[0] || $_[0] !~ /^[\w:]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0] and $_[0] =~ /^[\w:]+$/) 0 5 5
Condition33%1/3
defined $_[0] and $_[0] =~ /^[\w:]+$/
!ll&&!rl&&r
005
MC/DC0%0/2
defined $_[0] and $_[0] =~ /^[\w:]+$/
defined $_[0] $_[0] =~ /^[\w:]+$/
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] =~ /^[\w:]+$/
ABresult
0X0
100
111
52 0 $class = 'Geo::IPfree';
53 0 $db_file = $_[0];
54 }
55  
56 5 my $this = bless( {}, $class );
57  
58 5 ▶ if ( !defined $db_file ) { $db_file = _find_db_file(); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $db_file) 5 0 5
59  
60 5 $this->LoadDB($db_file);
61  
62 5 $this->Clean_Cache();
63 5 $this->{cache} = 1;
64  
65 5 return $this;
66 }
67  
68 0 ▶sub get_all_countries {
Subroutine get_all_countries: not called CC=1 SCAR=6.9
Pod documented
69 0 return {%countrys}; # copy
70 }
71  
72 5 ▶sub _find_db_file {
Subroutine _find_db_file: called CC=3 SCAR=11.0
73 my @locations = (
74 qw(/usr/local/share /usr/local/share/GeoIPfree),
75 5 map { $_, "$_/Geo" } @INC
76 );
77  
78 # lastly, find where this module was loaded, and try that dir
79 5 my ($lib) = ( $INC{'Geo/IPfree.pm'} =~ /^(.*?)[\\\/]+[^\\\/]+$/gs );
80 5 push @locations, $lib;
81  
82 5 for my $file ( map { "$_/$DEFAULT_DB" } @locations ) {
83 30 ▶ return $file if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 5 25 30
84 }
85 }
86  
87 5 ▶sub LoadDB {
Subroutine LoadDB: called CC=9 SCAR=22.5
Pod documented
88 5 my $this = shift;
89 5 my ($db_file) = @_;
90  
91 0 ▶ if ( -d $db_file ) { $db_file .= "/$DEFAULT_DB"; }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $db_file) 0 5 5
92  
93 5 ▶ if ( !-s $db_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $db_file) 0 5 5
94 0 Carp::croak("Can't load database, blank or not there: $db_file");
95 }
96  
97 5 my $buffer = '';
98 5 ▶ open( my $handler, '<', $db_file )
Branch: 1 branch
BranchTrueFalseTotal
open my $handler, "<", $db_file or Carp::croak("Failed to open database file $db_file for read!") 5 0 5
99 || Carp::croak("Failed to open database file $db_file for read!");
100 5 binmode($handler);
101 5 $this->{dbfile} = $db_file;
102  
103 5 ▶ delete $this->{pos} if $this->{pos};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"pos"} 0 5 5
104  
105 5 ▶ while ( read( $handler, $buffer, 1, length($buffer) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $handler, $buffer, 1, length $buffer) 85100 0 85100
106 85100 ▶ if ( $buffer =~ /##headers##(\d+)##$/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buffer =~ /##headers##(\d+)##$/s) { } 5 85095 85100
elsif ($buffer =~ /##start##$/s) { } 5 85090 85095
107 5 my $headers;
108 5 read( $handler, $headers, $1 );
109 5 my (%head) = ( $headers =~ /(\d+)=(\d+)/gs );
110 5 $this->{pos}{$_} = $head{$_} for keys %head;
111 5 $buffer = '';
112 }
113 elsif ( $buffer =~ /##start##$/s ) {
114 5 $this->{start} = tell($handler);
115 5 last;
116 }
117 }
118  
119 5 $this->{searchorder} = [ sort { $a <=> $b } keys %{ $this->{pos} } ];
120 5 $this->{handler} = $handler;
121 }
122  
123 28 ▶sub LookUp {
Subroutine LookUp: called CC=24 SCAR=33.0
Pod documented
124 28 my $this;
125  
126 28 ▶ if ( $#_ == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 0) { } 6 22 28
127 6 ▶ if ( !$THIS ) { $THIS = Geo::IPfree->new(); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($THIS) 1 5 6
128 6 $this = $THIS;
129 }
130 22 else { $this = shift; }
131  
132 28 my ($ip) = @_;
133  
134 28 ▶ $ip =~ s/\.+/\./gs if index( $ip, '..' ) > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($ip, "..") != -1 0 28 28
135 28 ▶ substr( $ip, 0, 1, '' ) if substr( $ip, 0, 1 ) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if substr($ip, 0, 1) eq "." 0 28 28
136 28 ▶ chop $ip if substr( $ip, -1 ) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if substr($ip, -1) eq "." 0 28 28
137  
138 28 ▶ if ( $ip !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 3 25 28
139 3 $ip = nslookup($ip);
140 }
141  
142 28 ▶ return unless length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ip 3 25 28
143  
144 ## Cache key uses /25 granularity: .0 for last octet 0-127, .128 for 128-255.
145 ## This fixes lookups in split /24 blocks while keeping cache compact.
146 25 my $ip_cache = $ip;
147 25 ▶ $ip_cache =~ s/\.(\d+)$/ '.' . ($1 < 128 ? '0' : '128') /e;
Branch: 1 branch
BranchTrueFalseTotal
$1 < 128 ? : 20 5 25
148  
149 25 ▶ if ( $this->{cache} && $this->{CACHE}{$ip_cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"cache"} and $this->{"CACHE"}{$ip_cache}) 2 23 25
Condition66%2/3
$this->{"cache"} and $this->{"CACHE"}{$ip_cache}
!ll&&!rl&&r
0232
MC/DC50%1/2
$this->{"cache"} and $this->{"CACHE"}{$ip_cache}
$this->{"cache"} $this->{"CACHE"}{$ip_cache}
Truth table66%2/3
A and B
A= $this->{"cache"}B= $this->{"CACHE"}{$ip_cache}
ABresult
0X0
100
111
150 2 return ( @{ $this->{CACHE}{$ip_cache} }, $ip );
151 }
152  
153 23 my $ipnb = ip2nb($ip);
154  
155 23 my $buf_pos = 0;
156  
157 23 foreach my $Key ( @{ $this->{searchorder} } ) {
158 3399 ▶ if ( $ipnb <= $Key ) { $buf_pos = $this->{pos}{$Key}; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ipnb <= $Key) 23 3376 3399
159 }
160  
161 23 my ( $buffer, $country, $iprange, $basex2 );
162  
163 ## Will use the DB in the memory:
164 23 ▶ if ( $this->{FASTER} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'FASTER'}) { } 7 16 23
165 7 ▶ my $base_cache = $this->{'baseX2dec'} ||= {};
Condition100%2/2
$this->{"baseX2dec"} ||= {}
l!l
52
MC/DC100%1/1
$this->{"baseX2dec"} ||= {}
$this->{"baseX2dec"}
Truth table100%2/2
A
A= $this->{"baseX2dec"}
Aresult
11
00
166 7 ▶ while ( $buf_pos < $this->{DB_SIZE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf_pos < $this->{"DB_SIZE"}) 1798 0 1798
167 1798 ▶ if ( $ipnb >= ( $base_cache->{ ( $basex2 = substr( $this->{DB}, $buf_pos + 2, 5 ) ) } ||= baseX2dec($basex2) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipnb >= ($$base_cache{$basex2 = substr($this->{"DB"}, $buf_pos + 2, 5)} ||= baseX2dec($basex2))) 7 1791 1798
Condition66%2/3
$$base_cache{$basex2 = substr($this->{"DB"}, $buf_pos + 2, 5)} ||= baseX2dec($basex2)
l!l&&r!l&&!r
7317250
MC/DC0%0/2
$$base_cache{$basex2 = substr($this->{"DB"}, $buf_pos + 2, 5)} ||= baseX2dec($basex2)
$$base_cache{$basex2 = substr($this->{"DB"}, $buf_pos + 2, 5)} baseX2dec($basex2)
Truth table66%2/3
A ||= B
A= $$base_cache{$basex2 = substr($this->{"DB"}, $buf_pos + 2, 5)}B= baseX2dec($basex2)
ABresult
1X1
011
000
168 7 $country = substr( $this->{DB}, $buf_pos, 2 );
169 7 last;
170 }
171 1791 $buf_pos += 7;
172 }
173 7 ▶ $country ||= substr( $this->{DB}, $buf_pos - 7, 2 );
Condition33%1/3
$country ||= substr($this->{"DB"}, $buf_pos - 7, 2)
l!l&&r!l&&!r
700
MC/DC0%0/2
$country ||= substr($this->{"DB"}, $buf_pos - 7, 2)
$country substr($this->{"DB"}, $buf_pos - 7, 2)
Truth table33%1/3
A ||= B
A= $countryB= substr($this->{"DB"}, $buf_pos - 7, 2)
ABresult
1X1
011
000
174 }
175 ## Will read the DB in the disk:
176 else {
177 16 ▶ seek( $this->{handler}, 0, 0 )
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.006001" 0 16 16
178 if $] < 5.006001; ## Fix bug on Perl 5.6.0
179 16 seek( $this->{handler}, $buf_pos + $this->{start}, 0 );
180 16 ▶ while ( read( $this->{handler}, $buffer, 7 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $this->{"handler"}, $buffer, 7) 3743 0 3743
181 3743 ▶ if ( $ipnb >= baseX2dec( substr( $buffer, 2 ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipnb >= baseX2dec(substr($buffer, 2))) 16 3727 3743
182 16 $country = substr( $buffer, 0, 2 );
183 16 last;
184 }
185 }
186 }
187  
188 23 ▶ if ( $this->{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"cache"}) 23 0 23
189 23 ▶ if ( $this->{CACHE_COUNT} > $cache_expire ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'CACHE_COUNT'} > $cache_expire) { } 0 23 23
190 0 keys %{ $this->{CACHE} };
191 0 my ($d_key) = each( %{ $this->{CACHE} } );
192 0 delete $this->{CACHE}{$d_key};
193 }
194 else {
195 23 $this->{CACHE_COUNT}++;
196 }
197 23 $this->{CACHE}{$ip_cache} = [ $country, $countrys{$country} ];
198 }
199  
200 23 return ( $country, $countrys{$country}, $ip );
201 }
202  
203 2 ▶sub Faster {
Subroutine Faster: called CC=1 SCAR=0.0
Pod documented
204 2 my $this = shift;
205 2 my $handler = $this->{handler};
206  
207 2 seek( $handler, 0, 0 ); ## Fix bug on Perl 5.6.0
208 2 seek( $handler, $this->{start}, 0 );
209  
210 2 $this->{DB} = do { local $/; <$handler>; };
211 2 $this->{DB_SIZE} = length( $this->{DB} );
212 2 $this->{FASTER} = 1;
213 }
214  
215 8 ▶sub Clean_Cache {
Subroutine Clean_Cache: called CC=1 SCAR=0.0
Pod documented
216 8 my $this = shift;
217 8 $this->{CACHE_COUNT} = 0;
218 8 delete $this->{CACHE};
219 8 delete $this->{'baseX2dec'};
220 8 return 1;
221 }
222  
223 6 ▶sub nslookup {
Subroutine nslookup: called CC=4 SCAR=14.0
Pod documented
224 6 my ( $host, $last_lookup ) = @_;
225 6 require Socket;
226 6 ▶ my $iaddr = Socket::inet_aton($host) || '';
Condition50%1/2
Socket::inet_aton($host) || ''
l!l
06
MC/DC0%0/1
Socket::inet_aton($host) || ''
Socket::inet_aton($host)
Truth table50%1/2
A
A= Socket::inet_aton($host)
Aresult
11
00
227 6 my @ip = unpack( 'C4', $iaddr );
228  
229 6 ▶ return nslookup( "www.${host}", 1 ) if !@ip && !$last_lookup;
Branch: 1 branch
BranchTrueFalseTotal
unless @ip or $last_lookup 3 3 6
Condition66%2/3
@ip or $last_lookup
l!l&&r!l&&!r
033
MC/DC50%1/2
@ip or $last_lookup
@ip $last_lookup
Truth table66%2/3
A or B
A= @ipB= $last_lookup
ABresult
1X1
011
000
230 3 return join( '.', @ip );
231 }
232  
233 24 ▶sub ip2nb {
Subroutine ip2nb: called CC=1 SCAR=0.0
Pod documented
234 24 my @ip = split( /\./, $_[0] );
235 24 return ( $ip[0] << 24 ) + ( $ip[1] << 16 ) + ( $ip[2] << 8 ) + $ip[3];
236 }
237  
238 1 ▶sub nb2ip {
Subroutine nb2ip: called CC=3 SCAR=11.0
Pod documented
239 1 my ($input) = @_;
240 1 my @ip;
241  
242 1 ▶ while ( $input > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input > 1) 4 1 5
243 4 my $int = int( $input / 256 );
244 4 push @ip, $input - ( $int << 8 );
245 4 $input = $int;
246 }
247  
248 1 ▶ push @ip, $input if $input > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $input > 0 0 1 1
249 1 push @ip, (0) x ( 4 - @ip );
250  
251 1 return join( '.', reverse @ip );
252 }
253  
254 86 ▶sub dec2baseX {
Subroutine dec2baseX: called CC=3 SCAR=11.0
Pod documented
255 86 my ($dec) = @_;
256 86 my @base;
257  
258 86 ▶ while ( $dec > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dec > 1) 84 86 170
259 84 my $int = int( $dec / $base );
260 84 push @base, $dec - $int * $base;
261 84 $dec = $int;
262 }
263  
264 86 ▶ push @base, $dec if $dec > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dec > 0 1 85 86
265 86 push @base, (0) x ( 5 - @base );
266  
267 86 return join( '', map { $baseX[$_] } reverse @base );
268 }
269  
270 5558 ▶sub baseX2dec {
Subroutine baseX2dec: called CC=6 SCAR=17.9
Pod documented
271 5558 my $string = reverse $_[0];
272 5558 my $length = length $string;
273 return #
274 (
275 0 + ( $length > 4 ? ( $baseX{ substr( $string, 4, 1 ) } * $base4 ) : 0 ) + #
276 ( $length > 3 ? ( $baseX{ substr( $string, 3, 1 ) } * $base3 ) : 0 ) + #
277 ( $length > 2 ? ( $baseX{ substr( $string, 2, 1 ) } * $base2 ) : 0 ) + #
278 ( $length > 1 ? ( $baseX{ substr( $string, 1, 1 ) } * $base1 ) : 0 ) + #
279 5558 ▶ ( $length ? ( $baseX{ substr( $string, 0, 1 ) } * $base0 ) : 0 ) #
Branch: 5 branches
BranchTrueFalseTotal
$length > 4 ? : 5472 86 5558
$length > 3 ? : 5472 86 5558
$length > 2 ? : 5472 86 5558
$length > 1 ? : 5472 86 5558
$length ? : 5558 0 5558
280 ); #
281 }
282  
283 8 1;
284  
285 =pod
286  
287 =encoding UTF-8
288  
289 =head1 NAME
290  
291 Geo::IPfree - Geo::IPfree - Look up the country of an IPv4 address
292  
293 =head1 VERSION
294  
295 version 1.160001
296  
297 =head1 AUTHOR
298  
299 Graciliano M. P. <gm@virtuasites.com.br>
300  
301 =head1 COPYRIGHT AND LICENSE
302  
303 This software is copyright (c) 2022 by Graciliano M. P.
304  
305 This is free software; you can redistribute it and/or modify it under
306 the same terms as the Perl 5 programming language system itself.
307  
308 =cut
309  
310 __DATA__
↑ Summary