blib/lib/Geo/IP2Location/Lite.pm

filter: Statementstmt 96.2% 181 / 188 Branchbran 87.5% 49 / 56 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 97.3% 37 / 38 Podpod 0.0% 0 / 35 totaltotal 82.2% 277 / 337 CC 34 SCAR 22.0
CC
34
Cov
93%
get_record
24.1
readFloat
6.0
name2ip
4.0
CRAP 9.1
↑ Summary
1 package Geo::IP2Location::Lite;
2  
3 # Copyright (C) 2005-2014 IP2Location.com
4 # All Rights Reserved
5 #
6 # This library is free software: you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation, either
9 # version 3 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; If not, see <http://www.gnu.org/licenses/>.
18  
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 $Geo::IP2Location::Lite::VERSION = '0.13';
23  
24 2 my $UNKNOWN = "UNKNOWN IP ADDRESS";
25 2 my $NO_IP = "MISSING IP ADDRESS";
26 2 my $INVALID_IP_ADDRESS = "INVALID IP ADDRESS";
27 2 my $NOT_SUPPORTED = "This parameter is unavailable in selected .BIN data file. Please upgrade data file.";
28 2 my $MAX_IPV4_RANGE = 4294967295;
29  
30 2 my $COUNTRYSHORT = 1;
31 2 my $COUNTRYLONG = 2;
32 2 my $REGION = 3;
33 2 my $CITY = 4;
34 2 my $ISP = 5;
35 2 my $LATITUDE = 6;
36 2 my $LONGITUDE = 7;
37 2 my $DOMAIN = 8;
38 2 my $ZIPCODE = 9;
39 2 my $TIMEZONE = 10;
40 2 my $NETSPEED = 11;
41 2 my $IDDCODE = 12;
42 2 my $AREACODE = 13;
43 2 my $WEATHERSTATIONCODE = 14;
44 2 my $WEATHERSTATIONNAME = 15;
45 2 my $MCC = 16;
46 2 my $MNC = 17;
47 2 my $MOBILEBRAND = 18;
48 2 my $ELEVATION = 19;
49 2 my $USAGETYPE = 20;
50  
51 2 my $NUMBER_OF_FIELDS = 20;
52 2 my $ALL = 100;
53  
54 2 my $IS_LITTLE_ENDIAN = unpack("h*", pack("s", 1)) =~ m/^1/;
55  
56 2 my $POSITIONS = {
57 $COUNTRYSHORT => [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
58 $COUNTRYLONG => [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
59 $REGION => [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
60 $CITY => [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
61 $LATITUDE => [0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
62 $LONGITUDE => [0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
63 $ZIPCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7],
64 $TIMEZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8],
65 $ISP => [0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9],
66 $DOMAIN => [0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10],
67 $NETSPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11, 8, 11, 0, 11, 0, 11, 0, 11],
68 $IDDCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12],
69 $AREACODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 13, 0, 13, 0, 13, 10, 13, 0, 13],
70 $WEATHERSTATIONCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14],
71 $WEATHERSTATIONNAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15],
72 $MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16],
73 $MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 17, 0, 17, 10, 17],
74 $MOBILEBRAND => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 18, 0, 18, 11, 18],
75 $ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19],
76 $USAGETYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20],
77 };
78  
79 2 my $IPv4_re = qr/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
80  
81 5 ▶sub open {
Subroutine open: called CC=3 SCAR=11.0
Pod undocumented
82 5 my ($class, $db_file) = @_;
83 5 ▶ $db_file || die "Geo::IP2Location::Lite::open() requires a database path name";
Branch: 1 branch
BranchTrueFalseTotal
$db_file or die "Geo::IP2Location::Lite::open() requires a database path name" 3 2 5
84 3 ▶ CORE::open( my $handle,'<',"$db_file" ) or die "Geo::IP2Location::Lite::open() error opening $db_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $handle, "<", "$db_file" or die "Geo::IP2Location::Lite::open() error opening ${db_file}: $!" 2 1 3
85 2 binmode($handle);
86 2 my $obj = bless {filehandle => $handle}, $class;
87 2 return $obj->initialize;
88 }
89  
90 2 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
91 2 my ($obj) = @_;
92 2 $obj->{"databasetype"} = $obj->read8($obj->{filehandle}, 1);
93 2 $obj->{"databasecolumn"} = $obj->read8($obj->{filehandle}, 2);
94 2 $obj->{"databaseyear"} = $obj->read8($obj->{filehandle}, 3);
95 2 $obj->{"databasemonth"} = $obj->read8($obj->{filehandle}, 4);
96 2 $obj->{"databaseday"} = $obj->read8($obj->{filehandle}, 5);
97 2 $obj->{"ipv4databasecount"} = $obj->read32($obj->{filehandle}, 6);
98 2 $obj->{"ipv4databaseaddr"} = $obj->read32($obj->{filehandle}, 10);
99 2 $obj->{"ipv4indexbaseaddr"} = $obj->read32($obj->{filehandle}, 22);
100 2 return $obj;
101 }
102  
103 1 ▶sub get_module_version { return $Geo::IP2Location::Lite::VERSION; }
Subroutine get_module_version: called CC=1 SCAR=0.0
Pod undocumented
104  
105 1 ▶sub get_database_version {
Subroutine get_database_version: called CC=1 SCAR=0.0
Pod undocumented
106 1 my $obj = shift(@_);
107 1 return $obj->{"databaseyear"} . "." . $obj->{"databasemonth"} . "." . $obj->{"databaseday"};
108 }
109  
110 226 ▶sub _get_by_pos {
Subroutine _get_by_pos: called CC=3 SCAR=11.0
111 226 my ( $obj,$ipaddr,$pos ) = @_;
112  
113 226 ▶ return $INVALID_IP_ADDRESS
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 1 225 226
114 if ! $pos;
115  
116 225 my ( $ipv,$ipnum ) = $obj->validate_ip( $ipaddr );
117  
118 225 ▶ return $ipv == 4
Branch: 1 branch
BranchTrueFalseTotal
$ipv == 4 ? : 223 2 225
119 ? $obj->get_record( $ipnum,$pos )
120 : $INVALID_IP_ADDRESS;
121 }
122  
123 10 ▶sub get_country { return ( _get_by_pos( @_,$COUNTRYSHORT ),_get_by_pos( @_,$COUNTRYLONG ) ) }
Subroutine get_country: called CC=1 SCAR=0.0
Pod undocumented
124 14 ▶sub get_country_short { return _get_by_pos( @_,$COUNTRYSHORT ); }
Subroutine get_country_short: called CC=1 SCAR=0.0
Pod undocumented
125 10 ▶sub get_country_long { return _get_by_pos( @_,$COUNTRYLONG ); }
Subroutine get_country_long: called CC=1 SCAR=0.0
Pod undocumented
126 10 ▶sub get_region { return _get_by_pos( @_,$REGION ); }
Subroutine get_region: called CC=1 SCAR=0.0
Pod undocumented
127 10 ▶sub get_city { return _get_by_pos( @_,$CITY ); }
Subroutine get_city: called CC=1 SCAR=0.0
Pod undocumented
128 10 ▶sub get_isp { return _get_by_pos( @_,$ISP ); }
Subroutine get_isp: called CC=1 SCAR=0.0
Pod undocumented
129 11 ▶sub get_latitude { return _get_by_pos( @_,$LATITUDE ); }
Subroutine get_latitude: called CC=1 SCAR=0.0
Pod undocumented
130 10 ▶sub get_zipcode { return _get_by_pos( @_,$ZIPCODE ); }
Subroutine get_zipcode: called CC=1 SCAR=0.0
Pod undocumented
131 10 ▶sub get_longitude { return _get_by_pos( @_,$LONGITUDE ); }
Subroutine get_longitude: called CC=1 SCAR=0.0
Pod undocumented
132 10 ▶sub get_domain { return _get_by_pos( @_,$DOMAIN ); }
Subroutine get_domain: called CC=1 SCAR=0.0
Pod undocumented
133 10 ▶sub get_timezone { return _get_by_pos( @_,$TIMEZONE ); }
Subroutine get_timezone: called CC=1 SCAR=0.0
Pod undocumented
134 10 ▶sub get_netspeed { return _get_by_pos( @_,$NETSPEED ); }
Subroutine get_netspeed: called CC=1 SCAR=0.0
Pod undocumented
135 10 ▶sub get_iddcode { return _get_by_pos( @_,$IDDCODE ); }
Subroutine get_iddcode: called CC=1 SCAR=0.0
Pod undocumented
136 10 ▶sub get_areacode { return _get_by_pos( @_,$AREACODE ); }
Subroutine get_areacode: called CC=1 SCAR=0.0
Pod undocumented
137 10 ▶sub get_weatherstationcode { return _get_by_pos( @_,$WEATHERSTATIONCODE ); }
Subroutine get_weatherstationcode: called CC=1 SCAR=0.0
Pod undocumented
138 10 ▶sub get_weatherstationname { return _get_by_pos( @_,$WEATHERSTATIONNAME ); }
Subroutine get_weatherstationname: called CC=1 SCAR=0.0
Pod undocumented
139 10 ▶sub get_mcc { return _get_by_pos( @_,$MCC ); }
Subroutine get_mcc: called CC=1 SCAR=0.0
Pod undocumented
140 10 ▶sub get_mnc { return _get_by_pos( @_,$MNC ); }
Subroutine get_mnc: called CC=1 SCAR=0.0
Pod undocumented
141 10 ▶sub get_mobilebrand { return _get_by_pos( @_,$MOBILEBRAND ); }
Subroutine get_mobilebrand: called CC=1 SCAR=0.0
Pod undocumented
142 10 ▶sub get_elevation { return _get_by_pos( @_,$ELEVATION ); }
Subroutine get_elevation: called CC=1 SCAR=0.0
Pod undocumented
143 10 ▶sub get_usagetype { return _get_by_pos( @_,$USAGETYPE ); }
Subroutine get_usagetype: called CC=1 SCAR=0.0
Pod undocumented
144  
145 1 ▶sub get_all {
Subroutine get_all: called CC=2 SCAR=7.6
Pod undocumented
146 1 my @res = _get_by_pos( @_,$ALL );
147  
148 1 ▶ if ( $res[0] eq $INVALID_IP_ADDRESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res[0] eq $INVALID_IP_ADDRESS) 1 0 1
149 1 return ( $INVALID_IP_ADDRESS x $NUMBER_OF_FIELDS );
150 }
151  
152 0 return @res;
153 }
154  
155 227 ▶sub get_record {
Subroutine get_record: called CC=23 SCAR=31.8
Pod undocumented
156 227 my ( $obj,$ipnum,$mode ) = @_;
157 227 my $dbtype= $obj->{"databasetype"};
158  
159 227 ▶ $mode = 0 if ! defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 1 226 227
160  
161 227 ▶ if ($ipnum eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipnum eq "") 2 225 227
162 2 ▶ if ($mode == $ALL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $ALL) { } 1 1 2
163 1 return ( $NO_IP x $NUMBER_OF_FIELDS );
164 } else {
165 1 return $NO_IP;
166 }
167 }
168  
169 225 ▶ if ( $mode != $ALL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode != $ALL) 224 1 225
170 224 ▶ if ( $POSITIONS->{$mode}[$dbtype] == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($POSITIONS->{$mode}[$dbtype] == 0) 181 43 224
171 181 return $NOT_SUPPORTED;
172 }
173 }
174
175 44 my $realipno = $ipnum;
176 44 my $handle = $obj->{"filehandle"};
177 44 my $baseaddr = $obj->{"ipv4databaseaddr"};
178 44 my $dbcount = $obj->{"ipv4databasecount"};
179 44 my $dbcolumn = $obj->{"databasecolumn"};
180 44 my $indexbaseaddr = $obj->{"ipv4indexbaseaddr"};
181  
182 44 my $ipnum1_2 = int($ipnum >> 16);
183 44 my $indexaddr = $indexbaseaddr + ($ipnum1_2 << 3);
184  
185 44 my $low = 0;
186 44 my $high = $dbcount;
187 44 ▶ if ($indexbaseaddr > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexbaseaddr > 0) 0 44 44
188 0 $low = $obj->read32($handle, $indexaddr);
189 0 $high = $obj->read32($handle, $indexaddr + 4);
190 }
191 44 my $mid = 0;
192 44 my $ipfrom = 0;
193 44 my $ipto = 0;
194 44 my $ipno = 0;
195  
196 44 ▶ if ($realipno == $MAX_IPV4_RANGE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($realipno == $MAX_IPV4_RANGE) { } 1 43 44
197 1 $ipno = $realipno - 1;
198 } else {
199 43 $ipno = $realipno;
200 }
201  
202 44 ▶ while ($low <= $high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low <= $high) 570 0 570
203 570 $mid = int(($low + $high) >> 1);
204 570 $ipfrom = $obj->read32($handle, $baseaddr + $mid * $dbcolumn * 4);
205 570 $ipto = $obj->read32($handle, $baseaddr + ($mid + 1) * $dbcolumn * 4);
206  
207 570 ▶ return $UNKNOWN if ( ! defined( $ipfrom ) || ! defined( $ipto ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ipfrom and defined $ipto 0 570 570
Condition33%1/3
defined $ipfrom and defined $ipto
!ll&&!rl&&r
00570
MC/DC0%0/2
defined $ipfrom and defined $ipto
defined $ipfrom defined $ipto
Truth table33%1/3
A and B
A= defined $ipfromB= defined $ipto
ABresult
0X0
100
111
208
209 570 ▶ if (($ipno >= $ipfrom) && ($ipno < $ipto)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipno >= $ipfrom and $ipno < $ipto) { } 44 526 570
Condition100%3/3
$ipno >= $ipfrom and $ipno < $ipto
!ll&&!rl&&r
29023644
MC/DC100%2/2
$ipno >= $ipfrom and $ipno < $ipto
$ipno >= $ipfrom $ipno < $ipto
Truth table100%3/3
A and B
A= $ipno >= $ipfromB= $ipno < $ipto
ABresult
0X0
100
111
210 # read whole results string into temp string and parse results from memory
211 44 my $raw_positions_row;
212 44 seek($handle, ($baseaddr + $mid * $dbcolumn * 4) - 1, 0);
213 44 read($handle, $raw_positions_row, $dbcolumn * 4);
214  
215 44 my @return_vals;
216  
217 44 ▶ foreach my $pos (
Branch: 1 branch
BranchTrueFalseTotal
$mode == $ALL ? : 1 43 44
218 $mode == $ALL
219 ? ( $COUNTRYSHORT .. $NUMBER_OF_FIELDS )
220 : $mode
221 ) {
222  
223 63 ▶ if ( $POSITIONS->{$pos}[$dbtype] == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($POSITIONS->{$pos}[$dbtype] == 0) { } 18 45 63
224 18 push( @return_vals, $NOT_SUPPORTED );
225 } else {
226 45 ▶ if ( $pos == $LATITUDE or $pos == $LONGITUDE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos == $LATITUDE or $pos == $LONGITUDE) { } 0 45 45
elsif ($pos == $COUNTRYLONG) { } 21 24 45
Condition33%1/3
$pos == $LATITUDE or $pos == $LONGITUDE
l!l&&r!l&&!r
0045
MC/DC0%0/2
$pos == $LATITUDE or $pos == $LONGITUDE
$pos == $LATITUDE $pos == $LONGITUDE
Truth table33%1/3
A or B
A= $pos == $LATITUDEB= $pos == $LONGITUDE
ABresult
1X1
011
000
227  
228 push( @return_vals, sprintf( "%.6f",$obj->readFloat(
229 0 substr($raw_positions_row, 4 * ( $POSITIONS->{$pos}[$dbtype] -1 ), 4)
230 ) ) );
231  
232 } elsif ( $pos == $COUNTRYLONG ) {
233  
234 push( @return_vals, $obj->readStr(
235 $handle,
236 21 unpack("V", substr($raw_positions_row, 4 * ( $POSITIONS->{$pos}[$dbtype] -1 ), 4 ) ) + 3
237 ) );
238  
239 } else {
240  
241 my $return_val = $obj->readStr(
242 $handle,
243 24 unpack("V", substr($raw_positions_row, 4 * ( $POSITIONS->{$pos}[$dbtype]-1), 4) )
244 );
245  
246 24 ▶ if ( $pos == $COUNTRYSHORT && $return_val eq 'UK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == $COUNTRYSHORT and $return_val eq "UK") 6 18 24
Condition66%2/3
$pos == $COUNTRYSHORT and $return_val eq "UK"
!ll&&!rl&&r
0186
MC/DC50%1/2
$pos == $COUNTRYSHORT and $return_val eq "UK"
$pos == $COUNTRYSHORT $return_val eq "UK"
Truth table66%2/3
A and B
A= $pos == $COUNTRYSHORTB= $return_val eq "UK"
ABresult
0X0
100
111
247 6 $return_val = 'GB';
248 }
249  
250 24 push( @return_vals,$return_val );
251 }
252 }
253 }
254  
255 44 ▶ return ( $mode == $ALL ) ? @return_vals : $return_vals[0];
Branch: 1 branch
BranchTrueFalseTotal
$mode == $ALL ? : 1 43 44
256  
257 } else {
258 526 ▶ if ($ipno < $ipfrom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipno < $ipfrom) { } 290 236 526
259 290 $high = $mid - 1;
260 } else {
261 236 $low = $mid + 1;
262 }
263 }
264 }
265  
266 0 return $UNKNOWN;
267 }
268  
269 1146 ▶sub read32 {
Subroutine read32: called CC=1 SCAR=0.0
Pod undocumented
270 1146 my ($obj, $handle, $position) = @_;
271 1146 my $data = "";
272 1146 seek($handle, $position-1, 0);
273 1146 read($handle, $data, 4);
274 1146 return unpack("V", $data);
275 }
276  
277 10 ▶sub read8 {
Subroutine read8: called CC=1 SCAR=0.0
Pod undocumented
278 10 my ($obj, $handle, $position) = @_;
279 10 my $data = "";
280 10 seek($handle, $position-1, 0);
281 10 read($handle, $data, 1);
282 10 return unpack("C", $data);
283 }
284  
285 45 ▶sub readStr {
Subroutine readStr: called CC=1 SCAR=0.0
Pod undocumented
286 45 my ($obj, $handle, $position) = @_;
287 45 my $data = "";
288 45 my $string = "";
289 45 seek($handle, $position, 0);
290 45 read($handle, $data, 1);
291 45 read($handle, $string, unpack("C", $data));
292 45 return $string;
293 }
294  
295 0 ▶sub readFloat {
Subroutine readFloat: not called CC=2 SCAR=17.9
Pod undocumented
296 0 my ($obj, $data) = @_;
297 0 ▶ return $IS_LITTLE_ENDIAN
Branch: 1 branch
BranchTrueFalseTotal
$IS_LITTLE_ENDIAN ? : 0 0 0
298 ? unpack("f", $data) # "LITTLE ENDIAN - x86\n";
299 : unpack("f", reverse($data)); # "BIG ENDIAN - MAC\n";
300 }
301  
302 225 ▶sub validate_ip {
Subroutine validate_ip: called CC=2 SCAR=6.9
Pod undocumented
303 225 my ( $obj,$ip ) = @_;
304 225 my $ipv = -1;
305 225 my $ipnum = -1;
306 #name server lookup if domain name
307 225 $ip = $obj->name2ip($ip);
308
309 225 ▶ if ($obj->ip_is_ipv4($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->ip_is_ipv4($ip)) 223 2 225
310 #ipv4 address
311 223 $ipv = 4;
312 223 $ipnum = $obj->ip2no($ip);
313 }
314 225 return ($ipv, $ipnum);
315 }
316  
317 223 ▶sub ip2no {
Subroutine ip2no: called CC=1 SCAR=0.0
Pod undocumented
318 223 my ( $obj,$ip ) = @_;
319 223 my @block = split(/\./, $ip);
320 223 my $no = 0;
321 223 $no = $block[3];
322 223 $no = $no + $block[2] * 256;
323 223 $no = $no + $block[1] * 256 * 256;
324 223 $no = $no + $block[0] * 256 * 256 * 256;
325 223 return $no;
326 }
327  
328 227 ▶sub name2ip {
Subroutine name2ip: called CC=4 SCAR=13.9
Pod undocumented
329 227 my ( $obj,$host ) = @_;
330 227 ▶ return "" if ! defined($host);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 1 226 227
331 226 my $ip_address = "";
332 226 ▶ if ($host =~ $IPv4_re){
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /$IPv4_re/) { } 223 3 226
333 223 $ip_address = $host;
334 } else {
335 3 ▶ if ( my $ip = gethostbyname($host) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ip = gethostbyname $host) 1 2 3
336 1 $ip_address = join('.', unpack('C4',($ip)[4]));
337 }
338 }
339 226 return $ip_address;
340 }
341  
342 225 ▶sub ip_is_ipv4 {
Subroutine ip_is_ipv4: called CC=2 SCAR=6.9
Pod undocumented
343 225 my ( $obj,$ip ) = @_;
344 225 ▶ if ($ip =~ $IPv4_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /$IPv4_re/) { } 223 2 225
345 223 return 1;
346 } else {
347 2 return 0;
348 }
349 }
350  
351 2 1;
352  
353 __END__
↑ Summary