blib/lib/Geo/IPfree.pm

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