blib/lib/Geo/IP/PurePerl.pm

filter: Statementstmt 29.1% 125 / 429 Branchbran 1.3% 2 / 146 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 55.2% 37 / 67 Podpod 70.8% 17 / 24 totaltotal 24.7% 181 / 730 CC 89 SCAR 44.6
CC
89
Cov
21%
_setup_segments
342.0
get_city_record
272.0
open
90.0
CRAP 86.2
↑ Summary
1 package Geo::IP::PurePerl;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.3
8 1 ▶ if ( $] >= 5.008 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.008) { } 1 0 1
9 1 require Encode;
10 1 Encode->import(qw/ decode /);
11 }
12 else {
13 0 ▶ *decode = sub {
Subroutine __ANON__: not called CC=2 SCAR=12.6
14 0 local $_ = $_[1];
15 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 s/([\x80-\xff])/my $c = ord($1);
17 0 ▶ my $p = $c >= 192 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c >= 192 ? : 0 0 0
18 0 pack ( 'CC' => 0xc2 + $p , $c & ~0x40 ); /ge;
19 0 return $_;
20 0 };
21 }
22 };
23  
24  
25 1 ▶use vars qw( @ISA $VERSION @EXPORT $OPEN_TYPE_PATH );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use constant GEOIP_CHARSET_ISO_8859_1 => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant GEOIP_CHARSET_UTF8 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use constant FULL_RECORD_LENGTH => 50;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant GEOIP_COUNTRY_BEGIN => 16776960;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant RECORD_LENGTH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant GEOIP_STATE_BEGIN_REV0 => 16700000;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant GEOIP_STATE_BEGIN_REV1 => 16000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant STRUCTURE_INFO_MAX_SIZE => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant DATABASE_INFO_MAX_SIZE => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant GEOIP_COUNTRY_EDITION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant GEOIP_REGION_EDITION_REV0 => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant GEOIP_REGION_EDITION_REV1 => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant GEOIP_CITY_EDITION_REV0 => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant GEOIP_CITY_EDITION_REV1 => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant GEOIP_ORG_EDITION => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use constant GEOIP_ISP_EDITION => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant GEOIP_PROXY_EDITION => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant GEOIP_ASNUM_EDITION => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant GEOIP_NETSPEED_EDITION => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant GEOIP_DOMAIN_EDITION => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant SEGMENT_RECORD_LENGTH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant STANDARD_RECORD_LENGTH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant ORG_RECORD_LENGTH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant MAX_RECORD_LENGTH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use constant MAX_ORG_RECORD_LENGTH => 300;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use constant US_OFFSET => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant CANADA_OFFSET => 677;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant WORLD_OFFSET => 1353;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant FIPS_RANGE => 360;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 $VERSION = '1.26';
59  
60 1 require Exporter;
61 1 @ISA = qw(Exporter);
62  
63 # cheat --- try to load Sys::Mmap
64 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.1
65 1 eval {
66 # wrap into eval again, as workaround for centos / mod_perl issue
67 # seems they use $@ without eval somewhere
68
69 eval "require Sys::Mmap"
70 ? Sys::Mmap->import
71 1 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
eval 'require Sys::Mmap' ? : 0 1 1
72 1 for (qw/ PROT_READ MAP_PRIVATE MAP_SHARED /) {
73 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 3 my $unused_stub = $_; # we must use a copy
75 0 ▶ *$unused_stub = sub { die 'Sys::Mmap required for mmap support' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
76 } # for
77 }; # do
78 1 1;
79 }; # eval
80 } # begin
81  
82  
83 sub GEOIP_STANDARD(){0;}
84 sub GEOIP_MEMORY_CACHE(){1;}
85  
86 #sub GEOIP_CHECK_CACHE(){2;}
87 #sub GEOIP_INDEX_CACHE(){4;}
88 sub GEOIP_MMAP_CACHE(){8;}
89  
90 sub GEOIP_UNKNOWN_SPEED(){0;}
91 sub GEOIP_DIALUP_SPEED(){1;}
92 sub GEOIP_CABLEDSL_SPEED(){2;}
93 sub GEOIP_CORPORATE_SPEED(){3;}
94  
95 1 @EXPORT = qw( GEOIP_STANDARD GEOIP_MEMORY_CACHE GEOIP_MMAP_CACHE
96 GEOIP_UNKNOWN_SPEED GEOIP_DIALUP_SPEED GEOIP_CABLEDSL_SPEED GEOIP_CORPORATE_SPEED );
97  
98 1 my @countries = (
99 undef,"AP","EU","AD","AE","AF","AG","AI","AL","AM","CW","AO","AQ","AR",
100 "AS","AT","AU","AW","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ",
101 "BM","BN","BO","BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD","CF",
102 "CG","CH","CI","CK","CL","CM","CN","CO","CR","CU","CV","CX","CY","CZ",
103 "DE","DJ","DK","DM","DO","DZ","EC","EE","EG","EH","ER","ES","ET","FI",
104 "FJ","FK","FM","FO","FR","SX","GA","GB","GD","GE","GF","GH","GI","GL",
105 "GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HM","HN","HR",
106 "HT","HU","ID","IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO","JP",
107 "KE","KG","KH","KI","KM","KN","KP","KR","KW","KY","KZ","LA","LB","LC",
108 "LI","LK","LR","LS","LT","LU","LV","LY","MA","MC","MD","MG","MH","MK",
109 "ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY",
110 "MZ","NA","NC","NE","NF","NG","NI","NL","NO","NP","NR","NU","NZ","OM",
111 "PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PW","PY",
112 "QA","RE","RO","RU","RW","SA","SB","SC","SD","SE","SG","SH","SI","SJ",
113 "SK","SL","SM","SN","SO","SR","ST","SV","SY","SZ","TC","TD","TF","TG",
114 "TH","TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW","TZ","UA","UG",
115 "UM","US","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE",
116 "YT","RS","ZA","ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE","BL",
117 "MF","BQ");
118 1 my @code3s = (
119 undef,"AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","CUW","AGO","ATA","ARG",
120 "ASM","AUT","AUS","ABW","AZE","BIH","BRB","BGD","BEL","BFA","BGR","BHR","BDI","BEN",
121 "BMU","BRN","BOL","BRA","BHS","BTN","BVT","BWA","BLR","BLZ","CAN","CCK","COD","CAF",
122 "COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI","CUB","CPV","CXR","CYP","CZE",
123 "DEU","DJI","DNK","DMA","DOM","DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN",
124 "FJI","FLK","FSM","FRO","FRA","SXM","GAB","GBR","GRD","GEO","GUF","GHA","GIB","GRL",
125 "GMB","GIN","GLP","GNQ","GRC","SGS","GTM","GUM","GNB","GUY","HKG","HMD","HND","HRV",
126 "HTI","HUN","IDN","IRL","ISR","IND","IOT","IRQ","IRN","ISL","ITA","JAM","JOR","JPN",
127 "KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT","CYM","KAZ","LAO","LBN","LCA",
128 "LIE","LKA","LBR","LSO","LTU","LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD",
129 "MLI","MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV","MWI","MEX","MYS",
130 "MOZ","NAM","NCL","NER","NFK","NGA","NIC","NLD","NOR","NPL","NRU","NIU","NZL","OMN",
131 "PAN","PER","PYF","PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW","PRY",
132 "QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN","SWE","SGP","SHN","SVN","SJM",
133 "SVK","SLE","SMR","SEN","SOM","SUR","STP","SLV","SYR","SWZ","TCA","TCD","ATF","TGO",
134 "THA","TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN","TZA","UKR","UGA",
135 "UMI","USA","URY","UZB","VAT","VCT","VEN","VGB","VIR","VNM","VUT","WLF","WSM","YEM",
136 "MYT","SRB","ZAF","ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY","BLM",
137 "MAF","BES");
138 1 my @names = (
139 undef,"Asia/Pacific Region","Europe","Andorra","United Arab Emirates",
140 "Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia",
141 "Curacao","Angola","Antarctica","Argentina","American Samoa",
142 "Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina",
143 "Barbados","Bangladesh","Belgium","Burkina Faso","Bulgaria",
144 "Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam",
145 "Bolivia","Brazil","Bahamas","Bhutan","Bouvet Island",
146 "Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands",
147 "Congo, The Democratic Republic of the","Central African Republic","Congo","Switzerland","Cote D'Ivoire",
148 "Cook Islands","Chile","Cameroon","China","Colombia",
149 "Costa Rica","Cuba","Cape Verde","Christmas Island","Cyprus",
150 "Czech Republic","Germany","Djibouti","Denmark","Dominica",
151 "Dominican Republic","Algeria","Ecuador","Estonia","Egypt",
152 "Western Sahara","Eritrea","Spain","Ethiopia","Finland",
153 "Fiji","Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France",
154 "Sint Maarten (Dutch part)","Gabon","United Kingdom","Grenada","Georgia",
155 "French Guiana","Ghana","Gibraltar","Greenland","Gambia",
156 "Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands",
157 "Guatemala","Guam","Guinea-Bissau","Guyana","Hong Kong",
158 "Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary",
159 "Indonesia","Ireland","Israel","India","British Indian Ocean Territory",
160 "Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica",
161 "Jordan","Japan","Kenya","Kyrgyzstan","Cambodia",
162 "Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of",
163 "Kuwait","Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon",
164 "Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho",
165 "Lithuania","Luxembourg","Latvia","Libya","Morocco",
166 "Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia",
167 "Mali","Myanmar","Mongolia","Macau","Northern Mariana Islands",
168 "Martinique","Mauritania","Montserrat","Malta","Mauritius",
169 "Maldives","Malawi","Mexico","Malaysia","Mozambique",
170 "Namibia","New Caledonia","Niger","Norfolk Island","Nigeria",
171 "Nicaragua","Netherlands","Norway","Nepal","Nauru",
172 "Niue","New Zealand","Oman","Panama","Peru",
173 "French Polynesia","Papua New Guinea","Philippines","Pakistan","Poland",
174 "Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal",
175 "Palau","Paraguay","Qatar","Reunion","Romania",
176 "Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles",
177 "Sudan","Sweden","Singapore","Saint Helena","Slovenia",
178 "Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal",
179 "Somalia","Suriname","Sao Tome and Principe","El Salvador","Syrian Arab Republic",
180 "Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo",
181 "Thailand","Tajikistan","Tokelau","Turkmenistan","Tunisia",
182 "Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu",
183 "Taiwan","Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands",
184 "United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines",
185 "Venezuela","Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu",
186 "Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia",
187 "South Africa","Zambia","Montenegro","Zimbabwe","Anonymous Proxy",
188 "Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man",
189 "Jersey","Saint Barthelemy","Saint Martin","Bonaire, Saint Eustatius and Saba");
190  
191 # --- unfortunately we do not know the path so we assume the
192 # default path /usr/local/share/GeoIP
193 # if thats not true, you can set $Geo::IP::PurePerl::OPEN_TYPE_PATH
194 #
195 0 ▶sub open_type {
Subroutine open_type: not called CC=6 SCAR=37.4
Pod undocumented
196 0 my ( $class, $type, $flags ) = @_;
197 0 my %type_dat_name_mapper = (
198 GEOIP_COUNTRY_EDITION() => 'GeoIP',
199 GEOIP_REGION_EDITION_REV0() => 'GeoIPRegion',
200 GEOIP_REGION_EDITION_REV1() => 'GeoIPRegion',
201 GEOIP_CITY_EDITION_REV0() => 'GeoIPCity',
202 GEOIP_CITY_EDITION_REV1() => 'GeoIPCity',
203 GEOIP_ISP_EDITION() => 'GeoIPISP',
204 GEOIP_ORG_EDITION() => 'GeoIPOrg',
205 GEOIP_PROXY_EDITION() => 'GeoIPProxy',
206 GEOIP_ASNUM_EDITION() => 'GeoIPASNum',
207 GEOIP_NETSPEED_EDITION() => 'GeoIPNetSpeed',
208 GEOIP_DOMAIN_EDITION() => 'GeoIPDomain',
209 );
210  
211 # backward compatibility for 2003 databases.
212 0 ▶ $type -= 105 if $type >= 106;
Branch: 1 branch
BranchTrueFalseTotal
if $type >= 106 0 0 0
213  
214 0 my $name = $type_dat_name_mapper{$type};
215 0 ▶ die("Invalid database type $type\n") unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
216  
217 0 ▶ my $mkpath = sub { File::Spec->catfile( File::Spec->rootdir, @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
218  
219 my $path =
220 defined $Geo::IP::PurePerl::OPEN_TYPE_PATH
221 ? $Geo::IP::PurePerl::OPEN_TYPE_PATH
222 0 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $Geo::IP::PurePerl::OPEN_TYPE_PATH ? : 0 0 0
223 $^O eq 'NetWare'
224 ? $mkpath->(qw/ etc GeoIP /)
225 0 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'NetWare' ? : 0 0 0
226 0 ▶ $^O eq 'MSWin32'
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
227 ? $mkpath->(qw/ GeoIP /)
228 : $mkpath->(qw/ usr local share GeoIP /);
229 }
230 };
231  
232 0 my $filename = File::Spec->catfile( $path, $name . '.dat' );
233 0 return $class->open( $filename, $flags );
234 }
235  
236  
237 0 ▶sub open {
Subroutine open: not called CC=9 SCAR=45.0
Pod documented
238 0 ▶ die "Geo::IP::PurePerl::open() requires a path name"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 and $_[1] 0 0 0
Condition0%0/3
@_ > 1 and $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and $_[1]
@_ > 1 $_[1]
Truth table0%0/3
A and B
A= @_ > 1B= $_[1]
ABresult
0X0
100
111
239 unless( @_ > 1 and $_[1] );
240 0 my ($class, $db_file, $flags) = @_;
241 0 my $fh = FileHandle->new;
242 0 my $gi;
243 0 ▶ CORE::open $fh, $db_file or die "Error opening $db_file";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, $db_file or die "Error opening $db_file" 0 0 0
244 0 binmode($fh);
245 0 ▶ if ( $flags && ( $flags & ( GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags and $flags & 9) { } 0 0 0
Condition0%0/3
$flags and $flags & 9
!ll&&!rl&&r
000
MC/DC0%0/2
$flags and $flags & 9
$flags $flags & 9
Truth table0%0/3
A and B
A= $flagsB= $flags & 9
ABresult
0X0
100
111
246 0 my %self;
247  
248 0 ▶ if ( $flags & GEOIP_MMAP_CACHE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 8) { } 0 0 0
249 0 ▶ die "Sys::Mmap required for MMAP support"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Sys::Mmap::VERSION 0 0 0
250 unless defined $Sys::Mmap::VERSION;
251 0 ▶ mmap( $self{buf} = undef, 0, PROT_READ, MAP_PRIVATE, $fh )
Branch: 1 branch
BranchTrueFalseTotal
mmap($self{"buf"} = undef, 0, PROT_READ(), MAP_PRIVATE(), $fh) or die "mmap: $!" 0 0 0
252 or die "mmap: $!";
253 }
254 else {
255 0 local $/ = undef;
256 0 $self{buf} = <$fh>;
257 }
258 0 $self{fh} = $fh;
259 0 $gi = bless \%self, $class;
260 }
261 else {
262 0 $gi = bless { fh => $fh }, $class;
263 }
264 0 $gi->_setup_segments();
265 0 return $gi;
266 }
267  
268 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
269 0 my ($class, $db_file, $flags) = @_;
270 # this will be less messy once deprecated new( $path, [$flags] )
271 # is no longer supported (that's what open() is for)
272  
273 0 my $def_db_file = '/usr/local/share/GeoIP/GeoIP.dat';
274 0 ▶ if ($^O eq 'NetWare') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'NetWare') { } 0 0 0
elsif ($^O eq 'MSWin32') { } 0 0 0
275 0 $def_db_file = 'sys:/etc/GeoIP/GeoIP.dat';
276 } elsif ($^O eq 'MSWin32') {
277 0 $def_db_file = 'c:/GeoIP/GeoIP.dat';
278 }
279 0 ▶ if ( !defined $db_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $db_file) { } 0 0 0
elsif ($db_file =~ /^\d+$/) { } 0 0 0
280 # called as new()
281 0 $db_file = $def_db_file;
282 } elsif ( $db_file =~ /^\d+$/ ) {
283 # db_file is GEOIP_MEMORY_CACHE or GEOIP_STANDARD
284 # called as new( $flags )
285 0 $flags = $db_file;
286 0 $db_file = $def_db_file;
287 } # else called as new( $database_filename, [$flags] );
288  
289 0 $class->open( $db_file, $flags );
290 }
291  
292 #this function setups the database segments
293 0 ▶sub _setup_segments {
Subroutine _setup_segments: not called CC=18 SCAR=58.3
294 0 my ($gi) = @_;
295 0 my $a = 0;
296 0 my $i = 0;
297 0 my $j = 0;
298 0 my $delim;
299 my $buf;
300 0 $gi->{_charset} = GEOIP_CHARSET_ISO_8859_1;
301 0 $gi->{"databaseType"} = GEOIP_COUNTRY_EDITION;
302 0 $gi->{"record_length"} = STANDARD_RECORD_LENGTH;
303  
304 0 my $filepos = tell($gi->{fh});
305 0 seek($gi->{fh}, -3, 2);
306 0 ▶ for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 20) 0 0 0
307 0 read($gi->{fh},$delim,3);
308
309 #find the delim
310 0 ▶ if ($delim eq (chr(255).chr(255).chr(255))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delim eq "\377\377\377") { } 0 0 0
311 0 read($gi->{fh},$a,1);
312
313 #read the databasetype
314 0 $gi->{"databaseType"} = ord($a);
315  
316 # backward compatibility for 2003 databases.
317 0 ▶ $gi->{databaseType} -= 105 if $gi->{databaseType} >= 106;
Branch: 1 branch
BranchTrueFalseTotal
if $gi->{"databaseType"} >= 106 0 0 0
318  
319 #chose the database segment for the database type
320 #if database Type is GEOIP_REGION_EDITION then use database segment GEOIP_STATE_BEGIN
321 0 ▶ if ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($gi->{'databaseType'} == 7) { } 0 0 0
elsif ($gi->{'databaseType'} == 3) { } 0 0 0
elsif ($gi->{'databaseType'} == 6 or $gi->{'databaseType'} == 2 or $gi->{'databaseType'} == 5 or $gi->{'databaseType'} == 9 or $gi->{'databaseType'} == 11 or $gi->{'databaseType'} == 4) { } 0 0 0
322 0 $gi->{"databaseSegments"} = GEOIP_STATE_BEGIN_REV0;
323 } elsif ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV1) {
324 0 $gi->{"databaseSegments"} = GEOIP_STATE_BEGIN_REV1;
325 }
326  
327 #if database Type is GEOIP_CITY_EDITION, GEOIP_ISP_EDITION or GEOIP_ORG_EDITION then
328 #read in the database segment
329 ▶ elsif (($gi->{"databaseType"} == GEOIP_CITY_EDITION_REV0) ||
Condition0%0/15
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9 or $gi->{"databaseType"} == 11 or $gi->{"databaseType"} == 4
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9 or $gi->{"databaseType"} == 11
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2
l!l&&r!l&&!r
000
MC/DC0%0/6
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9 or $gi->{"databaseType"} == 11 or $gi->{"databaseType"} == 4
$gi->{"databaseType"} == 6 $gi->{"databaseType"} == 2 $gi->{"databaseType"} == 5 $gi->{"databaseType"} == 9 $gi->{"databaseType"} == 11 $gi->{"databaseType"} == 4
Truth table0%0/7
A or B or C or D or E or F
A= $gi->{"databaseType"} == 6B= $gi->{"databaseType"} == 2C= $gi->{"databaseType"} == 5D= $gi->{"databaseType"} == 9E= $gi->{"databaseType"} == 11F= $gi->{"databaseType"} == 4
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
330 ($gi->{"databaseType"} == GEOIP_CITY_EDITION_REV1) ||
331 ($gi->{"databaseType"} == GEOIP_ORG_EDITION) ||
332 ($gi->{"databaseType"} == GEOIP_ASNUM_EDITION) ||
333 ($gi->{"databaseType"} == GEOIP_DOMAIN_EDITION) ||
334 ($gi->{"databaseType"} == GEOIP_ISP_EDITION)) {
335 0 $gi->{"databaseSegments"} = 0;
336  
337 #read in the database segment for the database type
338 0 read($gi->{fh},$buf,SEGMENT_RECORD_LENGTH);
339 0 ▶ for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
340 0 $gi->{"databaseSegments"} += (ord(substr($buf,$j,1)) << ($j * 8));
341 }
342  
343 #record length is four for ISP databases and ORG databases
344 #record length is three for country databases, region database and city databases
345 0 ▶ if ($gi->{"databaseType"} == GEOIP_ORG_EDITION ||
Branch: 1 branch
BranchTrueFalseTotal
if ($gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4 or $gi->{"databaseType"} == 11) 0 0 0
Condition0%0/6
$gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4 or $gi->{"databaseType"} == 11
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4
l!l&&r!l&&!r
000
MC/DC0%0/3
$gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4 or $gi->{"databaseType"} == 11
$gi->{"databaseType"} == 5 $gi->{"databaseType"} == 4 $gi->{"databaseType"} == 11
Truth table0%0/4
A or B or C
A= $gi->{"databaseType"} == 5B= $gi->{"databaseType"} == 4C= $gi->{"databaseType"} == 11
ABCresult
1XX1
01X1
0011
0000
346 $gi->{"databaseType"} == GEOIP_ISP_EDITION ||
347 $gi->{"databaseType"} == GEOIP_DOMAIN_EDITION) {
348 0 $gi->{"record_length"} = ORG_RECORD_LENGTH;
349 }
350 }
351 0 last;
352 } else {
353 0 seek($gi->{fh}, -4 , 1);
354 }
355 }
356 #if database Type is GEOIP_COUNTY_EDITION then use database segment GEOIP_COUNTRY_BEGIN
357 0 ▶ if ($gi->{"databaseType"} == GEOIP_COUNTRY_EDITION ||
Branch: 1 branch
BranchTrueFalseTotal
if ($gi->{"databaseType"} == 1 or $gi->{"databaseType"} == 10) 0 0 0
Condition0%0/3
$gi->{"databaseType"} == 1 or $gi->{"databaseType"} == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
$gi->{"databaseType"} == 1 or $gi->{"databaseType"} == 10
$gi->{"databaseType"} == 1 $gi->{"databaseType"} == 10
Truth table0%0/3
A or B
A= $gi->{"databaseType"} == 1B= $gi->{"databaseType"} == 10
ABresult
1X1
011
000
358 $gi->{"databaseType"} == GEOIP_NETSPEED_EDITION) {
359 0 $gi->{"databaseSegments"} = GEOIP_COUNTRY_BEGIN;
360 }
361 0 seek($gi->{fh},$filepos,0);
362 0 return $gi;
363 }
364  
365 0 ▶sub _seek_country {
Subroutine _seek_country: not called CC=6 SCAR=37.4
366 0 my ($gi, $ipnum) = @_;
367  
368 0 my $fh = $gi->{fh};
369 0 my $offset = 0;
370  
371 0 my ($x0, $x1);
372  
373 0 my $reclen = $gi->{"record_length"};
374  
375 0 ▶ for (my $depth = 31; $depth >= 0; $depth--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= 0) 0 0 0
376 0 ▶ unless ( exists $gi->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{'buf'}) { } 0 0 0
377 0 seek $fh, $offset * 2 * $reclen, 0;
378 0 read $fh, $x0, $reclen;
379 0 read $fh, $x1, $reclen;
380 } else {
381
382 0 $x0 = substr($gi->{buf}, $offset * 2 * $reclen, $reclen);
383 0 $x1 = substr($gi->{buf}, $offset * 2 * $reclen + $reclen, $reclen);
384 }
385  
386 0 $x0 = unpack("V1", $x0."\0");
387 0 $x1 = unpack("V1", $x1."\0");
388  
389 0 ▶ if ($ipnum & (1 << $depth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipnum & 1 << $depth) { } 0 0 0
390 0 ▶ if ($x1 >= $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 >= $gi->{"databaseSegments"}) 0 0 0
391 0 $gi->{last_netmask} = 32 - $depth;
392 0 return $x1;
393 }
394 0 $offset = $x1;
395 } else {
396 0 ▶ if ($x0 >= $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x0 >= $gi->{"databaseSegments"}) 0 0 0
397 0 $gi->{last_netmask} = 32 - $depth;
398 0 return $x0;
399 }
400 0 $offset = $x0;
401 }
402 }
403  
404 0 print STDERR "Error Traversing Database for ipnum = $ipnum - Perhaps database is corrupt?";
405 }
406 0 ▶sub charset {
Subroutine charset: not called CC=1 SCAR=6.9
Pod documented
407 0 return $_[0]->{_charset};
408 }
409  
410 0 ▶sub set_charset{
Subroutine set_charset: not called CC=1 SCAR=6.9
Pod documented
411 0 my ( $gi, $charset ) = @_;
412 0 my $old_charset = $gi->{_charset};
413 0 $gi->{_charset} = $charset;
414  
415 0 return $old_charset;
416 }
417  
418 #this function returns the country code of ip address
419 0 ▶sub country_code_by_addr {
Subroutine country_code_by_addr: not called CC=2 SCAR=17.9
Pod documented
420 0 my ($gi, $ip_address) = @_;
421 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
422 0 return $countries[$gi->id_by_addr($ip_address)];
423 }
424  
425 #this function returns the country code3 of ip address
426 0 ▶sub country_code3_by_addr {
Subroutine country_code3_by_addr: not called CC=2 SCAR=17.9
Pod documented
427 0 my ($gi, $ip_address) = @_;
428 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
429 0 return $code3s[$gi->id_by_addr($ip_address)];
430 }
431  
432 #this function returns the name of ip address
433 0 ▶sub country_name_by_addr {
Subroutine country_name_by_addr: not called CC=2 SCAR=17.9
Pod documented
434 0 my ($gi, $ip_address) = @_;
435 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
436 0 return $names[$gi->id_by_addr($ip_address)];
437 }
438  
439 0 ▶sub id_by_addr {
Subroutine id_by_addr: not called CC=2 SCAR=17.9
Pod undocumented
440 0 my ($gi, $ip_address) = @_;
441 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
442 0 return $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_COUNTRY_BEGIN;
443 }
444  
445 #this function returns the country code of domain name
446 0 ▶sub country_code_by_name {
Subroutine country_code_by_name: not called CC=1 SCAR=6.9
Pod documented
447 0 my ($gi, $host) = @_;
448 0 my $country_id = $gi->id_by_name($host);
449 0 return $countries[$country_id];
450 }
451  
452 #this function returns the country code3 of domain name
453 0 ▶sub country_code3_by_name {
Subroutine country_code3_by_name: not called CC=1 SCAR=6.9
Pod documented
454 0 my ($gi, $host) = @_;
455 0 my $country_id = $gi->id_by_name($host);
456 0 return $code3s[$country_id];
457 }
458  
459 #this function returns the country name of domain name
460 0 ▶sub country_name_by_name {
Subroutine country_name_by_name: not called CC=1 SCAR=6.9
Pod documented
461 0 my ($gi, $host) = @_;
462 0 my $country_id = $gi->id_by_name($host);
463 0 return $names[$country_id];
464 }
465  
466 0 ▶sub id_by_name {
Subroutine id_by_name: not called CC=3 SCAR=24.8
Pod undocumented
467 0 my ($gi, $host) = @_;
468 0 my $ip_address;
469 0 ▶ if ($host =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 0 0
470 0 $ip_address = $host;
471 } else {
472 0 $ip_address = join('.',unpack('C4',(gethostbyname($host))[4]));
473 }
474 0 ▶ return unless $ip_address;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address 0 0 0
475 0 return $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_COUNTRY_BEGIN;
476 }
477  
478 #this function returns the city record as a array
479 0 ▶sub get_city_record {
Subroutine get_city_record: not called CC=16 SCAR=56.1
Pod documented
480 0 my ($gi, $host) = @_;
481 0 my $ip_address = $gi->get_ip_address($host);
482 0 ▶ return unless $ip_address;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address 0 0 0
483 0 my $record_buf;
484 my $record_buf_pos;
485 0 my $char;
486 0 my $metroarea_combo;
487 0 my $record_country_code = "";
488 0 my $record_country_code3 = "";
489 0 my $record_country_name = "";
490 0 my $record_region = "";
491 0 my $record_city = "";
492 0 my $record_postal_code = "";
493 0 my $record_latitude = "";
494 0 my $record_longitude = "";
495 0 my $record_metro_code = "";
496 0 my $record_area_code = "";
497 0 my $str_length = 0;
498 0 my $i;
499 my $j;
500  
501 #lookup the city
502 0 my $seek_country = $gi->_seek_country(addr_to_num($ip_address));
503 0 ▶ if ($seek_country == $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek_country == $gi->{"databaseSegments"}) 0 0 0
504 0 return;
505 }
506 #set the record pointer to location of the city record
507 0 my $record_pointer = $seek_country + (2 * $gi->{"record_length"} - 1) * $gi->{"databaseSegments"};
508  
509 0 ▶ unless ( exists $gi->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{'buf'}) { } 0 0 0
510 0 seek( $gi->{"fh"}, $record_pointer, 0 );
511 0 read( $gi->{"fh"}, $record_buf, FULL_RECORD_LENGTH );
512 0 $record_buf_pos = 0;
513 }
514 else {
515 0 $record_buf = substr($gi->{buf}, $record_pointer, FULL_RECORD_LENGTH);
516 0 $record_buf_pos = 0;
517 }
518  
519 #get the country
520 0 $char = ord(substr($record_buf,$record_buf_pos,1));
521 0 $record_country_code = $countries[$char];#get the country code
522 0 $record_country_code3 = $code3s[$char];#get the country code with 3 letters
523 0 $record_country_name = $names[$char];#get the country name
524 0 $record_buf_pos++;
525  
526 #get the region
527 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
528 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
529 0 $str_length++;#get the length of string
530 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
531 }
532 0 ▶ if ($str_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_length > 0) 0 0 0
533 0 $record_region = substr($record_buf,$record_buf_pos,$str_length);
534 }
535 0 $record_buf_pos += $str_length + 1;
536 0 $str_length = 0;
537  
538 #get the city
539 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
540 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
541 0 $str_length++;#get the length of string
542 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
543 }
544 0 ▶ if ($str_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_length > 0) 0 0 0
545 0 $record_city = substr($record_buf,$record_buf_pos,$str_length);
546 }
547 0 $record_buf_pos += $str_length + 1;
548 0 $str_length = 0;
549  
550 #get the postal code
551 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
552 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
553 0 $str_length++;#get the length of string
554 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
555 }
556 0 ▶ if ($str_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_length > 0) 0 0 0
557 0 $record_postal_code = substr($record_buf,$record_buf_pos,$str_length);
558 }
559 0 $record_buf_pos += $str_length + 1;
560 0 $str_length = 0;
561 0 my $latitude = 0;
562 0 my $longitude = 0;
563  
564 #get the latitude
565 0 ▶ for ($j = 0;$j < 3; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
566 0 $char = ord(substr($record_buf,$record_buf_pos++,1));
567 0 $latitude += ($char << ($j * 8));
568 }
569 0 $record_latitude = ($latitude/10000) - 180;
570  
571 #get the longitude
572 0 ▶ for ($j = 0;$j < 3; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
573 0 $char = ord(substr($record_buf,$record_buf_pos++,1));
574 0 $longitude += ($char << ($j * 8));
575 }
576 0 $record_longitude = ($longitude/10000) - 180;
577  
578 #get the metro code and the area code
579 0 ▶ if (GEOIP_CITY_EDITION_REV1 == $gi->{"databaseType"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 == $gi->{"databaseType"}) 0 0 0
580 0 $metroarea_combo = 0;
581 0 ▶ if ($record_country_code eq "US") {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_country_code eq "US") 0 0 0
582 #if the country is US then read the metro area combo
583 0 ▶ for ($j = 0;$j < 3;++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
584 0 $char = ord(substr($record_buf,$record_buf_pos++,1));
585 0 $metroarea_combo += ($char << ($j * 8));
586 }
587 #split the metro area combo into the metro code and the area code
588 0 $record_metro_code = int($metroarea_combo/1000);
589 0 $record_area_code = $metroarea_combo%1000;
590 }
591 }
592
593 # the pureperl API must convert the string by themself to UTF8
594 # using Encode for perl >= 5.008 otherwise use it's own iso-8859-1 to utf8 converter
595 0 ▶ $record_city = decode( 'iso-8859-1' => $record_city )
Branch: 1 branch
BranchTrueFalseTotal
if $gi->charset == 1 0 0 0
596 if $gi->charset == GEOIP_CHARSET_UTF8;
597  
598 0 return ($record_country_code,$record_country_code3,$record_country_name,$record_region,$record_city,$record_postal_code,$record_latitude,$record_longitude,$record_metro_code,$record_area_code);
599 }
600  
601 #this function returns the city record as a hash ref
602 0 ▶sub get_city_record_as_hash {
Subroutine get_city_record_as_hash: not called CC=1 SCAR=6.9
Pod documented
603 0 my ($gi, $host) = @_;
604 0 my %h;
605 0 @h{qw/ country_code country_code3 country_name
606 region city postal_code
607 latitude longitude metro_code
608 area_code /}
609 = $gi->get_city_record($host);
610 0 $h{dma_code} = $h{metro_code}; # alias for depreciated dma_code
611 0 return \%h;
612 }
613  
614 #this function returns isp or org of the domain name
615 0 ▶sub org_by_name {
Subroutine org_by_name: not called CC=4 SCAR=30.0
Pod documented
616 0 my ($gi, $host) = @_;
617 0 my $ip_address = $gi->get_ip_address($host);
618 0 my $seek_org = $gi->_seek_country(addr_to_num($ip_address));
619 0 my $char;
620 my $org_buf;
621 0 my $org_buf_length = 0;
622 0 my $record_pointer;
623  
624 0 ▶ if ($seek_org == $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek_org == $gi->{"databaseSegments"}) 0 0 0
625 0 return undef;
626 }
627  
628 0 $record_pointer = $seek_org + (2 * $gi->{"record_length"} - 1) * $gi->{"databaseSegments"};
629  
630 0 ▶ unless ( exists $gi->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{'buf'}) { } 0 0 0
631 0 seek( $gi->{"fh"}, $record_pointer, 0 );
632 0 read( $gi->{"fh"}, $org_buf, MAX_ORG_RECORD_LENGTH );
633 }
634 else {
635 0 $org_buf = substr($gi->{buf}, $record_pointer, MAX_ORG_RECORD_LENGTH );
636 }
637  
638 0 $char = ord(substr($org_buf,0,1));
639 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
640 0 $org_buf_length++;
641 0 $char = ord(substr($org_buf,$org_buf_length,1));
642 }
643  
644 0 $org_buf = substr($org_buf, 0, $org_buf_length);
645 0 return $org_buf;
646 }
647  
648 #this function returns isp or org of the domain name
649 1 *isp_by_name = \*org_by_name;
650  
651 1 *org_by_addr = \*org_by_name;
652 1 *isp_by_addr = \*org_by_name;
653  
654 #this function returns the region
655 0 ▶sub region_by_name {
Subroutine region_by_name: not called CC=8 SCAR=42.8
Pod undocumented
656 0 my ($gi, $host) = @_;
657 0 my $ip_address = $gi->get_ip_address($host);
658 0 ▶ return unless $ip_address;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address 0 0 0
659 0 ▶ if ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gi->{'databaseType'} == 7) { } 0 0 0
elsif ($gi->{'databaseType'} == 3) { } 0 0 0
660 0 my $seek_region = $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_STATE_BEGIN_REV0;
661 0 ▶ if ($seek_region >= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek_region >= 1000) { } 0 0 0
662 0 return ("US",chr(($seek_region - 1000)/26 + 65) . chr(($seek_region - 1000)%26 + 65));
663 } else {
664 0 return ($countries[$seek_region],"");
665 }
666 } elsif ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV1) {
667 0 my $seek_region = $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_STATE_BEGIN_REV1;
668 0 ▶ if ($seek_region < US_OFFSET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($seek_region < 1) { } 0 0 0
elsif ($seek_region < 677) { } 0 0 0
elsif ($seek_region < 1353) { } 0 0 0
669 0 return ("","");
670 } elsif ($seek_region < CANADA_OFFSET) {
671 # return a us state
672 0 return ("US",chr(($seek_region - US_OFFSET)/26 + 65) . chr(($seek_region - US_OFFSET)%26 + 65));
673 } elsif ($seek_region < WORLD_OFFSET) {
674 # return a canada province
675 0 return ("CA",chr(($seek_region - CANADA_OFFSET)/26 + 65) . chr(($seek_region - CANADA_OFFSET)%26 + 65));
676 } else {
677 # return a country of the world
678 0 my $c = $countries[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
679 0 my $a2 = ($seek_region - WORLD_OFFSET) % FIPS_RANGE;
680 0 my $r = chr(($a2 / 100)+48) . chr((($a2 / 10) % 10)+48) . chr(($a2 % 10)+48);
681 0 return ($c,$r);
682 }
683 }
684 }
685  
686 0 ▶sub get_ip_address {
Subroutine get_ip_address: not called CC=2 SCAR=17.9
Pod undocumented
687 0 my ($gi, $host) = @_;
688 0 my $ip_address;
689 #check if host is ip address
690 0 ▶ if ($host =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 0 0
691 #host is ip address
692 0 $ip_address = $host;
693 } else {
694 #host is domain name do a dns lookup
695 0 $ip_address = join('.',unpack('C4',(gethostbyname($host))[4]));
696 }
697 0 return $ip_address;
698 }
699  
700 0 ▶sub addr_to_num { unpack( N => pack( C4 => split( /\./, $_[0] ) ) ) }
Subroutine addr_to_num: not called CC=1 SCAR=6.9
Pod undocumented
701 0 ▶sub num_to_addr { join q{.}, unpack( C4 => pack( N => $_[0] ) ) }
Subroutine num_to_addr: not called CC=1 SCAR=6.9
Pod undocumented
702  
703 0 ▶sub database_info {
Subroutine database_info: not called CC=6 SCAR=37.4
Pod documented
704 0 my $gi = shift;
705 0 my $i = 0;
706 0 my $buf;
707 my $retval;
708 0 my $hasStructureInfo;
709 0 seek($gi->{fh},-3,2);
710 0 ▶ for (my $i = 0;$i < STRUCTURE_INFO_MAX_SIZE;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 20) 0 0 0
711 0 read($gi->{fh},$buf,3);
712 0 ▶ if ($buf eq (chr(255) . chr(255) . chr(255))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "\377\377\377") 0 0 0
713 0 $hasStructureInfo = 1;
714 0 last;
715 }
716 0 seek($gi->{fh},-4,1);
717 }
718 0 ▶ if ($hasStructureInfo == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hasStructureInfo == 1) { } 0 0 0
719 0 seek($gi->{fh},-6,1);
720 } else {
721 # no structure info, must be pre Sep 2002 database, go back to
722 0 seek($gi->{fh},-3,2);
723 }
724 0 ▶ for (my $i = 0;$i < DATABASE_INFO_MAX_SIZE;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 100) 0 0 0
725 0 read($gi->{fh},$buf,3);
726 0 ▶ if ($buf eq (chr(0). chr(0). chr(0))){
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "\0\0\0") 0 0 0
727 0 read($gi->{fh},$retval,$i);
728 0 return $retval;
729 }
730 0 seek($gi->{fh},-4,1);
731 }
732 0 return "";
733 }
734  
735 0 ▶sub range_by_ip {
Subroutine range_by_ip: not called CC=5 SCAR=34.0
Pod documented
736 0 my $gi = shift;
737 0 my $ipnum = addr_to_num( shift );
738 0 my $c = $gi->_seek_country( $ipnum );
739 0 my $nm = $gi->last_netmask;
740 0 my $m = 0xffffffff << 32 - $nm;
741 0 my $left_seek_num = $ipnum & $m;
742 0 my $right_seek_num = $left_seek_num + ( 0xffffffff & ~$m );
743  
744 0 ▶ while ( $left_seek_num != 0
Branch: 1 branch
BranchTrueFalseTotal
if ($left_seek_num != 0 and $c == $gi->_seek_country($left_seek_num - 1)) 0 0 0
Condition0%0/3
$left_seek_num != 0 and $c == $gi->_seek_country($left_seek_num - 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$left_seek_num != 0 and $c == $gi->_seek_country($left_seek_num - 1)
$left_seek_num != 0 $c == $gi->_seek_country($left_seek_num - 1)
Truth table0%0/3
A and B
A= $left_seek_num != 0B= $c == $gi->_seek_country($left_seek_num - 1)
ABresult
0X0
100
111
745 and $c == $gi->_seek_country( $left_seek_num - 1) ) {
746 0 my $lm = 0xffffffff << 32 - $gi->last_netmask;
747 0 $left_seek_num = ( $left_seek_num - 1 ) & $lm;
748 }
749 0 ▶ while ( $right_seek_num != 0xffffffff
Branch: 1 branch
BranchTrueFalseTotal
if ($right_seek_num != 4294967295 and $c == $gi->_seek_country($right_seek_num + 1)) 0 0 0
Condition0%0/3
$right_seek_num != 4294967295 and $c == $gi->_seek_country($right_seek_num + 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$right_seek_num != 4294967295 and $c == $gi->_seek_country($right_seek_num + 1)
$right_seek_num != 4294967295 $c == $gi->_seek_country($right_seek_num + 1)
Truth table0%0/3
A and B
A= $right_seek_num != 4294967295B= $c == $gi->_seek_country($right_seek_num + 1)
ABresult
0X0
100
111
750 and $c == $gi->_seek_country( $right_seek_num + 1 ) ) {
751 0 my $rm = 0xffffffff << 32 - $gi->last_netmask;
752 0 $right_seek_num = ( $right_seek_num + 1 ) & $rm;
753 0 $right_seek_num += ( 0xffffffff & ~$rm );
754 }
755 0 return ( num_to_addr($left_seek_num), num_to_addr($right_seek_num) );
756 }
757  
758 0 ▶sub netmask { $_[0]->{last_netmask} = $_[1] }
Subroutine netmask: not called CC=1 SCAR=6.9
Pod documented
759  
760 0 ▶sub last_netmask {
Subroutine last_netmask: not called CC=1 SCAR=6.9
Pod documented
761 0 return $_[0]->{last_netmask};
762 }
763  
764 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=5 SCAR=34.0
765 0 my $gi = shift;
766  
767 0 ▶ if ( exists $gi->{buf} && $gi->{flags} && ( $gi->{flags} & GEOIP_MMAP_CACHE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{"buf"} and $gi->{"flags"} and $gi->{"flags"} & 8) 0 0 0
Condition0%0/6
exists $gi->{"buf"} and $gi->{"flags"} and $gi->{"flags"} & 8
!ll&&!rl&&r
000
exists $gi->{"buf"} and $gi->{"flags"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $gi->{"buf"} and $gi->{"flags"} and $gi->{"flags"} & 8
exists $gi->{"buf"} $gi->{"flags"} $gi->{"flags"} & 8
Truth table0%0/4
A and B and C
A= exists $gi->{"buf"}B= $gi->{"flags"}C= $gi->{"flags"} & 8
ABCresult
0XX0
10X0
1100
1111
768 0 ▶ munmap( $gi->{buf} ) or die "munmap: $!";
Branch: 1 branch
BranchTrueFalseTotal
munmap($gi->{"buf"}) or die "munmap: $!" 0 0 0
769 0 delete $gi->{buf};
770 }
771 }
772 1 1;
773 __END__
↑ Summary