blib/lib/Net/Whois/IP.pm

filter: Statementstmt 15.4% 44 / 284 Branchbran 5.0% 7 / 140 Conditioncond 2.8% 2 / 71 MC/DCmcdc 2.4% 1 / 41 Subroutinesub 43.4% 10 / 23 Podpod 0.0% 0 / 2 totaltotal 11.4% 64 / 561 CC 110 SCAR 52.2
CC
110
Cov
11%
_do_processing
506.0
_do_lookup
306.0
_inspect_whois_response_lines
240.0
CRAP 185.3
↑ Summary
1 package Net::Whois::IP;
2  
3 ########################################
4 #$Id: IP.pm,v 1.21 2007-03-07 16:49:36 ben Exp $
5 ########################################
6  
7 =head1 NAME
8  
9 Net::Whois::IP - Perl extension for looking up the whois information for
10 ip addresses
11  
12 =head1 SYNOPSIS
13  
14 use Net::Whois::IP qw(whoisip_query);
15  
16 my $ip = "192.168.1.1";
17 my ($response, $array_of_responses) =
18 whoisip_query($ip,
19 $optional_registry,
20 $optional_multiple_flag,
21 $optional_raw_flag,
22 $optional_array_of_search_options);
23  
24 In scalar context (single response hash returned):
25  
26 my $response = whoisip_query($ip);
27  
28 The response will be a reference to a hash containing all information
29 provided by the whois registrar.
30  
31 In list context (response hash and response chain returned):
32  
33 my ($response, $array_of_responses) = whoisip_query($ip,
34 undef,
35 "true");
36  
37 N.B.: See NOTES, below.
38  
39 The array_of_responses is a reference to an array containing references
40 to hashes for each level of query performed. For example, many records
41 must be searched several times to obtain the most detailed information;
42 this array contains the responses from each level.
43  
44 If $optional_multiple_flag is not undef, all duplicate values for a given
45 field will be returned.
46  
47 For example, normally only the last instance of TechPhone will be
48 returned if a record contains more than one. However, setting this flag
49 to a non-undef value will return all values as an array.
50  
51 As a consequence, all returned field values in the response hash become
52 references to arrays and must be dereferenced before use.
53  
54 If $optional_raw_flag is not undef, the response will be a reference to
55 an array containing the raw responses from the registrar instead of a
56 reference to a hash. In raw mode, no parsed response chain is returned.
57  
58 If $optional_array_of_search_options is not undef, the first two entries
59 will be used to replace TechPhone and OrgTechPhone in the search method.
60 This is fairly dangerous and can cause the module not to work at all if
61 set incorrectly.
62  
63 Normal unwrap of $response ($optional_multiple_flag not set):
64  
65 my $response = whoisip_query($ip);
66 foreach (sort keys(%{$response}) ) {
67 print "$_ $response->{$_} \n";
68 }
69  
70 $optional_multiple_flag set to a value:
71  
72 my $response = whoisip_query($ip, undef, "true");
73 foreach ( sort keys %$response ) {
74 print "$_ is\n";
75 foreach ( @{ $response->{ $_ } } ) { print " $_\n"; }
76 }
77  
78 $optional_raw_flag set to a value:
79  
80 my $response = whoisip_query( $ip, undef, undef, "true");
81 foreach (@{$response}) { print $_; }
82  
83 $optional_array_of_search_options set but not $optional_multiple_flag or
84 $optional_raw_flag:
85  
86 my $search_options = ["NetName","OrgName"];
87 my $response = whoisip_query($ip, undef, undef, undef, $search_options);
88 foreach (sort keys(%{$response}) ) { print "$_ $response->{$_} \n"; }
89  
90 =head1 NOTES
91  
92 For certain ARIN queries, additional synthesized parent/ancestor
93 records may be prepended to the returned WHOIS response array
94 ($array_of_responses). These records are synthesized from ARIN
95 summary/hierarchy output and are normalized into standard WHOIS
96 response hash format where possible.
97  
98 Synthesized parent/ancestor records are tagged with the key
99 "Synthetic", currently containing the value "ARIN-SUMMARY".
100  
101 Because ARIN summary records are abbreviated, synthesized records
102 may contain fewer fields than full WHOIS responses.
103  
104 =head1 DESCRIPTION
105  
106 Perl module to allow whois lookup of ip addresses. This module should
107 recursively query the various whois providers until it gets more
108 detailed information including either TechPhone or OrgTechPhone by
109 default; however, this is overrideable.
110  
111 =head1 AUTHOR
112  
113 Ben Schmitz -- ben@foink.com
114  
115 Thanks to Orbitz for allowing the community access to this work
116  
117 Please email me any suggestions, complaints, etc.
118  
119 =head1 SEE ALSO
120  
121 perl(1). Net::Whois
122  
123 =cut
124  
125 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 3 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
127 3 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 3 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
129 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 3 require Exporter;
131 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 3 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 3 @ISA = qw(Exporter);
135 3 @EXPORT = qw(
136 whoisip_query
137 set_debug
138 );
139 3 $VERSION = '1.20_01';
140  
141 3 my %whois_servers = (
142 'RIPE' => 'whois.ripe.net',
143 'APNIC' => 'whois.apnic.net',
144 'KRNIC' => 'whois.krnic.net',
145 'LACNIC' => 'whois.lacnic.net',
146 'ARIN' => 'whois.arin.net',
147 'AFRINIC' => 'whois.afrinic.net',
148 );
149  
150 # For queries:
151 # If ARIN add n param. If RIPE or Afrinic add -B param
152 my %query_prefix = (
153 $whois_servers{ARIN} => 'n ',
154 $whois_servers{RIPE} => '-B ',
155 3 $whois_servers{AFRINIC} => '-B ',
156 );
157  
158 # Are we debugging?
159 3 my $do_debugging = 0;
160  
161 3 ▶use constant ARIN_EXACT_MATCH_PREFIX => '! ';
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 3 my $whois_query_delay = 2; # Be conservative to avoid getting refused
164 3 my $first_arin_query_delay = 1;
165  
166  
167 ######################################
168 # Public Subs
169 ######################################
170  
171 sub whoisip_query {
172 4 ▶ my($ip,$reg,$multiple_flag,$raw_flag,$search_options) = @_;
Subroutine whoisip_query: called CC=5 SCAR=16.1
Pod undocumented
173  
174 # It allows to set the first registry to query
175 4 ▶ if(($ip !~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) && ($ip !~ /^$IPv6_re$/) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or $ip =~ /^$Net::Whois::IP::IPv6_re$/) 1 3 4
Condition66%2/3
$ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or $ip =~ /^$Net::Whois::IP::IPv6_re$/
l!l&&r!l&&!r
301
MC/DC50%1/2
$ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or $ip =~ /^$Net::Whois::IP::IPv6_re$/
$ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ $ip =~ /^$Net::Whois::IP::IPv6_re$/
Truth table66%2/3
A or B
A= $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/B= $ip =~ /^$Net::Whois::IP::IPv6_re$/
ABresult
1X1
011
000
176 1 croak("$ip is not a valid ip address");
177 }
178 3 ▶ if(!defined($reg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reg) 2 1 3
179 2 $reg = 'ARIN';
180 }
181 3 _do_debug("looking up $ip - at $reg");
182 3 my ($response, $array_of_responses) =
183 _do_lookup($ip, $reg, $multiple_flag, $raw_flag, $search_options);
184  
185 3 _do_debug("whois_ip_query sees: \$array_of_responses: " . scalar(@$array_of_responses));
186  
187 # Preserve historical scalar-context behavior while restoring
188 # documented list-context behavior.
189 3 ▶ return wantarray? ($response, $array_of_responses) : $response;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
190 }
191  
192 # Enabled/disable debugging
193 sub set_debug {
194 0 ▶ my ($state) = @_;
Subroutine set_debug: not called CC=2 SCAR=17.9
Pod undocumented
195  
196 0 ▶ $do_debugging = $state ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 0 0 0
197 }
198  
199  
200 ######################################
201 #Private Subs
202 ######################################
203 sub _do_lookup {
204 0 ▶ my($ip,$registrar,$multiple_flag,$raw_flag,$search_options) = @_;
Subroutine _do_lookup: not called CC=17 SCAR=57.2
205 0 _do_debug("do lookup $ip at $registrar");
206 # let's not beat up on them too much
207 0 my $extraflag = '1';
208 0 my $whois_response;
209 my $whois_raw_response;
210 0 my $whois_response_hash;
211 0 my @whois_response_array;
212 0 my @arin_summary_records;
213  
214 0 ▶ LOOP: while($extraflag ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($extraflag ne "") 0 0 0
215 0 _do_debug("Entering loop $extraflag");
216  
217 # Guard against unknown WHOIS registrars
218 croak("Unknown WHOIS registrar: $registrar")
219 0 ▶ unless exists $whois_servers{$registrar};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $whois_servers{$registrar} 0 0 0
220  
221 0 my $lookup_host = $whois_servers{$registrar};
222 0 ($whois_response,$whois_response_hash) = _do_query($lookup_host,$ip,$multiple_flag);
223 0 _inspect_whois_response_lines($whois_response);
224 0 push(@whois_response_array,$whois_response_hash);
225 0 push(@{$whois_raw_response}, @{$whois_response});
226 0 my($new_ip,$new_registrar) =
227 _do_processing($whois_response,
228 $registrar,
229 $ip,
230 $whois_response_hash,
231 $search_options,
232 \@arin_summary_records
233 );
234  
235 0 ▶ if(($new_ip ne $ip) || ($new_registrar ne $registrar) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_ip ne $ip or $new_registrar ne $registrar) { } 0 0 0
Condition0%0/3
$new_ip ne $ip or $new_registrar ne $registrar
l!l&&r!l&&!r
000
MC/DC0%0/2
$new_ip ne $ip or $new_registrar ne $registrar
$new_ip ne $ip $new_registrar ne $registrar
Truth table0%0/3
A or B
A= $new_ip ne $ipB= $new_registrar ne $registrar
ABresult
1X1
011
000
236 0 _do_debug("ip was $ip -- new ip is $new_ip");
237 0 _do_debug("registrar was $registrar -- new registrar is $new_registrar");
238 0 $ip = $new_ip;
239 0 $registrar = $new_registrar;
240 0 $extraflag++;
241 0 next LOOP;
242 }else{
243 0 $extraflag='';
244 0 last LOOP;
245 }
246 }
247
248 # Return raw response from registrar
249 0 ▶ if( ($raw_flag) && ($raw_flag ne '') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_flag and $raw_flag ne "") 0 0 0
Condition0%0/3
$raw_flag and $raw_flag ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$raw_flag and $raw_flag ne ""
$raw_flag $raw_flag ne ""
Truth table0%0/3
A and B
A= $raw_flagB= $raw_flag ne ""
ABresult
0X0
100
111
250 0 return ($whois_raw_response);
251 }
252  
253 0 ▶ if(%{$whois_response_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$whois_response_hash;}) { } 0 0 0
254 0 foreach my $key (sort keys %{$whois_response_hash}) {
255 0 my $value = $whois_response_hash->{$key};
256  
257 0 ▶ if (!defined $value) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
elsif (ref $value eq 'ARRAY') { } 0 0 0
elsif (not ref $value) { } 0 0 0
258 0 _do_debug("sub -- $key -- undef");
259 }
260 elsif (ref($value) eq 'ARRAY') {
261 0 foreach my $item (@{$value}) {
262 0 ▶ next unless defined($item) && $item =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item and $item =~ /\S/ 0 0 0
Condition0%0/3
defined $item and $item =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $item and $item =~ /\S/
defined $item $item =~ /\S/
Truth table0%0/3
A and B
A= defined $itemB= $item =~ /\S/
ABresult
0X0
100
111
263 0 ▶ _do_debug("sub -- $key -- " . (defined $item ? $item : 'undef'));
Branch: 1 branch
BranchTrueFalseTotal
defined $item ? : 0 0 0
264 }
265 }
266 elsif (!ref($value)) {
267 0 _do_debug("sub -- $key -- $value");
268 }
269 else {
270 0 _do_debug("sub -- $key -- " . ref($value));
271 }
272 }
273  
274 # If we've multiple records, normalize the add'l records into WHOIS-hash shape,
275 # putting the "oldest" ancestor (widest range) at $whois_response_array[0]
276 #
277 # N.B.: These records are often highly-abbreviated. Preserve what ARIN
278 # provides, but do not synthesize fields such as Country from child records.
279 0 ▶ if(@arin_summary_records) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arin_summary_records) 0 0 0
280 unshift(@whois_response_array,
281 0 map { _arin_summary_to_whois_response($_) } @arin_summary_records);
282 }
283 0 _inspect_whois_response_array(\@whois_response_array);
284  
285 0 return($whois_response_hash,\@whois_response_array);
286 }else{
287 0 return($whois_response,\@whois_response_array);
288 }
289 }
290  
291 # Convert ARIN summary data to "standard" WHOIS response format
292 sub _arin_summary_to_whois_response {
293 0 ▶ my ($rec) = @_;
Subroutine _arin_summary_to_whois_response: not called CC=6 SCAR=37.4
294  
295 0 my %out;
296  
297 0 my %map = (
298 netname => 'NetName',
299 nethandle => 'NetHandle',
300 description => 'OrgName',
301 orgname => 'OrgName',
302 custname => 'CustName',
303 customer => 'Customer',
304 country => 'Country',
305 source => 'Source',
306 );
307  
308 0 for my $src_key (keys %map) {
309 0 ▶ next if !defined $rec->{$src_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec->{$src_key} 0 0 0
310 0 $out{$map{$src_key}} = [ $rec->{$src_key} ];
311 }
312  
313 0 ▶ $out{Source} ||= [ 'ARIN' ];
Condition0%0/2
$out{"Source"} ||= ["ARIN"]
l!l
00
MC/DC0%0/1
$out{"Source"} ||= ["ARIN"]
$out{"Source"}
Truth table0%0/2
A
A= $out{"Source"}
Aresult
11
00
314 0 $out{Synthetic} = [ 'ARIN-SUMMARY' ]; # Tag it for what it is
315  
316 0 ▶ if (defined $rec->{start} && defined $rec->{end}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rec->{"start"} and defined $rec->{"end"}) 0 0 0
Condition0%0/3
defined $rec->{"start"} and defined $rec->{"end"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rec->{"start"} and defined $rec->{"end"}
defined $rec->{"start"} defined $rec->{"end"}
Truth table0%0/3
A and B
A= defined $rec->{"start"}B= defined $rec->{"end"}
ABresult
0X0
100
111
317 0 $out{NetRange} = [ "$rec->{start} - $rec->{end}" ];
318 $out{CIDR} = [
319 _range_to_cidr_strings(
320 _ipv4_to_int($rec->{start}),
321 _ipv4_to_int($rec->{end})
322 0 )
323 ];
324 }
325  
326 0 return \%out;
327 }
328  
329 sub _do_query {
330 0 ▶ my($registrar,$ip,$multiple_flag) = @_;
Subroutine _do_query: not called CC=10 SCAR=47.0
331 0 my @response;
332 0 my $i =0;
333  
334 # Prevent abusing the registrars --- they may disable an ip if too many queries per minute
335 0 _throttle_whois_query($registrar);
336  
337 0 LOOP: while(1) {
338 0 $i++;
339 0 my $sock = _get_connect($registrar);
340  
341 0 ▶ _do_debug("Querying $registrar with " . ($query_prefix{$registrar} // q{}) . "$ip");
Condition0%0/2
$query_prefix{$registrar} // ""
l!l
00
MC/DC0%0/1
$query_prefix{$registrar} // ""
$query_prefix{$registrar}
Truth table0%0/2
A
A= $query_prefix{$registrar}
Aresult
11
00
342 0 ▶ print $sock (($query_prefix{$registrar} // q{}) . "$ip\n");
Condition0%0/2
$query_prefix{$registrar} // ""
l!l
00
MC/DC0%0/1
$query_prefix{$registrar} // ""
$query_prefix{$registrar}
Truth table0%0/2
A
A= $query_prefix{$registrar}
Aresult
11
00
343  
344 0 @response = <$sock>;
345  
346 0 close($sock);
347  
348 0 ▶ if($#response < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#response < 0) { } 0 0 0
349 0 _do_debug("No valid response recieved from $registrar -- attempt $i ");
350 0 ▶ if($i <=3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 3) { } 0 0 0
351 0 next LOOP;
352 }else{
353 0 croak("No valid response for 4th time... dying....");
354 }
355 }else{
356 0 last LOOP;
357 }
358 }
359  
360 0 my %hash_response;
361 0 ▶ _do_debug("multiple flag = |" . ($multiple_flag // '') . "|");
Condition0%0/2
$multiple_flag // ""
l!l
00
MC/DC0%0/1
$multiple_flag // ""
$multiple_flag
Truth table0%0/2
A
A= $multiple_flag
Aresult
11
00
362  
363 0 foreach my $line (@response) {
364 0 ▶ if ( $line =~ /^([^:]+):\s*(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^:]+):\s*(.*)$/) 0 0 0
365 0 my ($key, $val) = ($1, $2);
366  
367 0 ▶ if ( $multiple_flag && $multiple_flag ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiple_flag and $multiple_flag ne '') { } 0 0 0
Condition0%0/3
$multiple_flag and $multiple_flag ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$multiple_flag and $multiple_flag ne ""
$multiple_flag $multiple_flag ne ""
Truth table0%0/3
A and B
A= $multiple_flagB= $multiple_flag ne ""
ABresult
0X0
100
111
368 # Multiple_flag is set, so get all responses for a given record item
369 0 push @{ $hash_response{$key} }, $val;
370 } else {
371 # Multiple_flag is not set, so only the last entry for any given record item
372 0 $hash_response{$key} = $val;
373 }
374 }
375 }
376  
377 0 return(\@response,\%hash_response);
378 }
379  
380 sub _do_processing {
381 0 ▶ my($response,$registrar,$ip,$hash_response,$search_options,$arin_summary_records) = @_;
Subroutine _do_processing: not called CC=22 SCAR=62.3
382  
383 # Response to comment.
384 # Bug report stating the search method will work better with different options. Easy way to do it now.
385 # this way a reference to an array can be passed in, the defaults will still
386 # be TechPhone and OrgTechPhone
387 0 my $pattern1 = 'TechPhone';
388 0 my $pattern2 = 'OrgTechPhone';
389  
390 0 ▶ if(ref($search_options) eq 'ARRAY' && defined $search_options->[0] && $search_options->[0] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $search_options eq "ARRAY" and defined $search_options->[0] and $search_options->[0] ne "") 0 0 0
Condition0%0/6
ref $search_options eq "ARRAY" and defined $search_options->[0] and $search_options->[0] ne ""
!ll&&!rl&&r
000
ref $search_options eq "ARRAY" and defined $search_options->[0]
!ll&&!rl&&r
000
MC/DC0%0/3
ref $search_options eq "ARRAY" and defined $search_options->[0] and $search_options->[0] ne ""
ref $search_options eq "ARRAY" defined $search_options->[0] $search_options->[0] ne ""
Truth table0%0/4
A and B and C
A= ref $search_options eq "ARRAY"B= defined $search_options->[0]C= $search_options->[0] ne ""
ABCresult
0XX0
10X0
1100
1111
391 0 $pattern1 = $search_options->[0];
392 0 $pattern2 = $search_options->[1];
393 }
394  
395 0 _do_debug("pattern1 = $pattern1 || pattern2 == $pattern2");
396  
397 0 LOOP:foreach (@{$response}) {
398 0 ▶ if (/Contact information can be found in the (\S+)\s+database/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/Contact information can be found in the (\S+)\s+database/) { } 0 0 0
elsif (/OrgID:\s+(\S+)/i || /source:\s+(\S+)/i and not defined $hash_response->{$pattern1}) { } 0 0 0
elsif (/Parent:\s+(\S+)/) { } 0 0 0
elsif ($registrar eq 'ARIN' and /.+\((.+)\).+$/ and not $_ =~ /.+\:.+/) { } 0 0 0
399 0 $registrar = $1;
400 0 _do_debug("Contact -- registrar = $registrar -- trying again");
401 0 last LOOP;
402 ▶ }elsif((/OrgID:\s+(\S+)/i || /source:\s+(\S+)/i) && !defined($hash_response->{$pattern1})) {
Condition0%0/6
/OrgID:\s+(\S+)/i || /source:\s+(\S+)/i and not defined $hash_response->{$pattern1}
!ll&&!rl&&r
000
/OrgID:\s+(\S+)/i or /source:\s+(\S+)/i
l!l&&r!l&&!r
000
MC/DC0%0/3
/OrgID:\s+(\S+)/i || /source:\s+(\S+)/i and not defined $hash_response->{$pattern1}
/OrgID:\s+(\S+)/i /source:\s+(\S+)/i not defined $hash_response->{$pattern1}
Truth table0%0/5
A or B and not(C)
A= /OrgID:\s+(\S+)/iB= /source:\s+(\S+)/iC= not defined $hash_response->{$pattern1}
ABCresult
00X0
1X00
0100
1X11
0111
403 0 my $val = $1;
404 0 _do_debug("Org/source match: value was $val--if not known registrar, will skip");
405 0 ▶ if(exists $whois_servers{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $whois_servers{$val}) 0 0 0
406 0 $registrar = $val;
407 0 _do_debug(" Known registrar match --> $registrar --> trying again ");
408 0 last LOOP;
409 }
410 }elsif(/Parent:\s+(\S+)/) {
411 # Use $pattern1 instead of default TechPhone
412 0 ▶ if(($1 ne '') && (!defined($hash_response->{$pattern1})) && (!defined($hash_response->{$pattern2})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne "" and not defined $hash_response->{$pattern1} and not defined $hash_response->{$pattern2}) 0 0 0
Condition0%0/6
$1 ne "" and not defined $hash_response->{$pattern1} and not defined $hash_response->{$pattern2}
!ll&&!rl&&r
000
$1 ne "" and not defined $hash_response->{$pattern1}
!ll&&!rl&&r
000
MC/DC0%0/3
$1 ne "" and not defined $hash_response->{$pattern1} and not defined $hash_response->{$pattern2}
$1 ne "" not defined $hash_response->{$pattern1} not defined $hash_response->{$pattern2}
Truth table0%0/4
A and not(B) and not(C)
A= $1 ne ""B= not defined $hash_response->{$pattern1}C= not defined $hash_response->{$pattern2}
ABCresult
0XX0
10X0
1100
1111
413 # End Modif
414 0 $ip = $1;
415 0 _do_debug(" Parent match ip will be $ip --> trying again");
416 0 last LOOP;
417 }
418 # Test Loop via Jason Kirk -- Thanks
419 ▶ }elsif($registrar eq 'ARIN' && (/.+\((.+)\).+$/) && ($_ !~ /.+\:.+/)) {
Condition0%0/6
$registrar eq "ARIN" and /.+\((.+)\).+$/ and not $_ =~ /.+\:.+/
!ll&&!rl&&r
000
$registrar eq "ARIN" and /.+\((.+)\).+$/
!ll&&!rl&&r
000
MC/DC0%0/3
$registrar eq "ARIN" and /.+\((.+)\).+$/ and not $_ =~ /.+\:.+/
$registrar eq "ARIN" /.+\((.+)\).+$/ not $_ =~ /.+\:.+/
Truth table0%0/4
A and B and not(C)
A= $registrar eq "ARIN"B= /.+\((.+)\).+$/C= not $_ =~ /.+\:.+/
ABCresult
0XX0
10X0
1100
1111
420 0 my $arin_handle = $1;
421  
422 0 ▶ if(/^(.+?)\s+(\S+)\s+\((NET-[^)]+)\)\s+
Branch: 1 branch
BranchTrueFalseTotal
if (/^(.+?)\s+(\S+)\s+\((NET-[^)]+)\)\s+ (\d+\.\d+\.\d+\.\d+)\s+-\s+ (\d+\.\d+\.\d+\.\d+)\s*$/x) 0 0 0
423 (\d+\.\d+\.\d+\.\d+)\s+-\s+
424 (\d+\.\d+\.\d+\.\d+)\s*$/x)
425 {
426 0 push @$arin_summary_records, {
427 description => $1,
428 netname => $2,
429 nethandle => $3,
430 start => $4,
431 end => $5,
432 };
433 }
434  
435 0 my $origIp = $ip;
436 0 $ip = ARIN_EXACT_MATCH_PREFIX . $arin_handle;
437  
438 # Modif: Keep the smallest block
439 0 ▶ if ($origIp =~ /! NET-(\d{1,3}\-\d{1,3}\-\d{1,3}\-\d{1,3})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origIp =~ /! NET-(\d{1,3}\-\d{1,3}\-\d{1,3}\-\d{1,3})/) 0 0 0
440 0 my $orIP = $1;
441 0 ▶ if ($ip =~ /! NET-(\d{1,3}\-\d{1,3}\-\d{1,3}\-\d{1,3})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /! NET-(\d{1,3}\-\d{1,3}\-\d{1,3}\-\d{1,3})/) 0 0 0
442 0 my $nwIP = $1;
443 0 ▶ if (pack('C4', split(/\-/,$orIP)) ge pack('C4', split(/\-/,$nwIP))) {
Branch: 1 branch
BranchTrueFalseTotal
if (pack("C4", split(/\-/, $orIP, 0)) ge pack("C4", split(/\-/, $nwIP, 0))) 0 0 0
444 0 $ip = $origIp;
445 }
446 }
447 }
448 0 ▶ 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}/) 0 0 0
449 0 $ip = $origIp;
450 }
451 0 _do_debug("parens match $ip $registrar --> trying again");
452 }else{
453 0 $ip = $ip;
454 0 $registrar = $registrar;
455 }
456 }
457 0 _do_debug("_do_processing returns arin_summary_records: ARIN summary records captured: " . scalar(@$arin_summary_records));
458 0 return($ip,$registrar);
459 }
460
461
462  
463 sub _get_connect {
464 0 ▶ my($whois_registrar) = @_;
Subroutine _get_connect: not called CC=3 SCAR=24.8
465 0 my $sock = IO::Socket::INET->new(
466 PeerAddr=>$whois_registrar,
467 PeerPort=>'43',
468 Timeout=>'60',
469 );
470 0 ▶ unless($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
471 0 carp("Failed to Connect to $whois_registrar at port 43: $!");
472 0 sleep(5);
473 0 $sock = IO::Socket::INET->new(
474 PeerAddr=>$whois_registrar,
475 PeerPort=>'43',
476 Timeout=>'60',
477 );
478 0 ▶ unless($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
479 0 croak("Failed to Connect to $whois_registrar at port 43 for the second time - $@");
480 }
481 }
482 0 return($sock);
483 }
484  
485 sub _ipv4_to_int {
486 0 ▶ my ($ip) = @_;
Subroutine _ipv4_to_int: not called CC=6 SCAR=37.4
487  
488 0 ▶ croak "Undefined IP address\n" if !defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 0 0 0
489 0 ▶ croak "Invalid IPv4 address: '$ip'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ip =~ /\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/ 0 0 0
490 if $ip !~ /\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/;
491  
492 0 my @octets = ($1, $2, $3, $4);
493  
494 0 for my $octet (@octets) {
495 0 ▶ die "Invalid IPv4 octet in '$ip'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $octet < 0 or $octet > 255 0 0 0
Condition0%0/3
$octet < 0 or $octet > 255
l!l&&r!l&&!r
000
MC/DC0%0/2
$octet < 0 or $octet > 255
$octet < 0 $octet > 255
Truth table0%0/3
A or B
A= $octet < 0B= $octet > 255
ABresult
1X1
011
000
496 if $octet < 0 || $octet > 255;
497 }
498  
499 0 return (($octets[0] << 24) |
500 ($octets[1] << 16) |
501 ($octets[2] << 8) |
502 $octets[3]);
503 }
504  
505 sub _range_to_cidr_strings {
506 0 ▶ my ($start, $end) = @_;
Subroutine _range_to_cidr_strings: not called CC=7 SCAR=40.3
507  
508 0 ▶ croak "Invalid range" if $start > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end 0 0 0
509  
510 0 my @cidrs;
511  
512 0 ▶ while ($start <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start <= $end) 0 0 0
513  
514 # Special case: the entire IPv4 space
515 0 ▶ if ($start == 0 && $end == 0xFFFFFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == 0 and $end == 4294967295) 0 0 0
Condition0%0/3
$start == 0 and $end == 4294967295
!ll&&!rl&&r
000
MC/DC0%0/2
$start == 0 and $end == 4294967295
$start == 0 $end == 4294967295
Truth table0%0/3
A and B
A= $start == 0B= $end == 4294967295
ABresult
0X0
100
111
516 0 push @cidrs, '0.0.0.0/0';
517 0 last;
518 }
519  
520 # Largest power-of-two block aligned at $start.
521 # Special case: start==0, because the low-set-bit trick yields 0 there.
522 0 ▶ my $max_size = $start ? ($start & -$start) : 0x8000_0000;
Branch: 1 branch
BranchTrueFalseTotal
$start ? : 0 0 0
523  
524 # Limit block size so it does not exceed remaining range
525 0 my $remaining = $end - $start + 1;
526  
527 0 ▶ while ($max_size > $remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size > $remaining) 0 0 0
528 0 $max_size >>= 1;
529 }
530  
531 # Convert block size to prefix length
532 0 my $prefix = 32 - _log2($max_size);
533  
534 0 push @cidrs, _int_to_ipv4($start) . "/$prefix";
535  
536 0 $start += $max_size;
537 }
538  
539 0 return @cidrs;
540 }
541  
542 sub _log2 {
543 0 ▶ my ($n) = @_;
Subroutine _log2: not called CC=3 SCAR=24.8
544  
545 0 ▶ croak "log2(): undefined for n <= 0\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or $n <= 0 0 0 0
Condition0%0/3
not defined $n or $n <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $n or $n <= 0
not defined $n $n <= 0
Truth table0%0/3
not(A) or B
A= not defined $nB= $n <= 0
ABresult
1X1
011
000
546 if !defined($n) || $n <= 0;
547  
548 0 return int(log($n) / log(2));
549 }
550  
551 sub _int_to_ipv4 {
552 0 ▶ my ($n) = @_;
Subroutine _int_to_ipv4: not called CC=4 SCAR=30.0
553  
554 0 ▶ croak "Undefined integer IP\n" if !defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
555 0 ▶ croak "Invalid IPv4 integer: '$n'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 or $n > 4294967295 0 0 0
Condition0%0/3
$n < 0 or $n > 4294967295
l!l&&r!l&&!r
000
MC/DC0%0/2
$n < 0 or $n > 4294967295
$n < 0 $n > 4294967295
Truth table0%0/3
A or B
A= $n < 0B= $n > 4294967295
ABresult
1X1
011
000
556 if $n < 0 || $n > 0xFFFFFFFF;
557  
558 0 return join '.',
559 (($n >> 24) & 0xFF),
560 (($n >> 16) & 0xFF),
561 (($n >> 8) & 0xFF),
562 ( $n & 0xFF);
563 }
564  
565 sub _throttle_whois_query {
566 0 ▶ my ($registrar) = @_;
Subroutine _throttle_whois_query: not called CC=5 SCAR=34.0
567  
568 0 state %last_query_time;
569  
570 0 my $now = time();
571 0 my $last = $last_query_time{$registrar};
572  
573 0 ▶ my $wait = defined($last)
Branch: 1 branch
BranchTrueFalseTotal
defined $last ? : 0 0 0
574 ? $whois_query_delay - ($now - $last)
575 : 0;
576  
577 0 ▶ if (!defined($last) && $registrar eq $whois_servers{ARIN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last and $registrar eq $whois_servers{"ARIN"}) 0 0 0
Condition0%0/3
not defined $last and $registrar eq $whois_servers{"ARIN"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $last and $registrar eq $whois_servers{"ARIN"}
not defined $last $registrar eq $whois_servers{"ARIN"}
Truth table0%0/3
not(A) and B
A= not defined $lastB= $registrar eq $whois_servers{"ARIN"}
ABresult
0X0
100
111
578 0 $wait = $first_arin_query_delay;
579 }
580  
581 0 ▶ if ($wait > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait > 0) 0 0 0
582 0 _do_debug("WHOIS throttle for $registrar: sleeping $wait second(s)");
583 0 sleep $wait;
584 }
585  
586 0 $last_query_time{$registrar} = time();
587 }
588  
589 sub _do_debug {
590 6 ▶ return unless $do_debugging;
Subroutine _do_debug: called CC=7 SCAR=38.1
Branch: 1 branch
BranchTrueFalseTotal
unless $do_debugging 6 0 6
591  
592 0 state $did_warn = 0;
593  
594 0 my (@stuff) = @_;
595 0 my $date = scalar localtime;
596 0 ▶ my $tmp_dir = File::Spec->tmpdir() || '/tmp';
Condition0%0/2
'File::Spec'->tmpdir || '/tmp'
l!l
00
MC/DC0%0/1
'File::Spec'->tmpdir || '/tmp'
'File::Spec'->tmpdir
Truth table0%0/2
A
A= 'File::Spec'->tmpdir
Aresult
11
00
597 0 my $outdebug = File::Spec->catfile($tmp_dir, 'Net.WhoisIP.log');
598  
599 0 ▶ unless($did_warn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($did_warn) 0 0 0
600 0 print STDERR "Net::Whois::IP: Debugging to \"$outdebug\" enabled!\n";
601 0 $did_warn = 1;
602 }
603  
604 0 ▶ open(my $debug_fh, '>>', $outdebug)
Branch: 1 branch
BranchTrueFalseTotal
open my $debug_fh, ">>", $outdebug or warn "Unable to open ${outdebug}: $!" 0 0 0
605 or warn "Unable to open $outdebug: $!";
606 0 ▶ return if !$debug_fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug_fh 0 0 0
607  
608 0 for my $item (@stuff) {
609 0 print {$debug_fh} "$date|$item|\n";
610 }
611  
612 0 close($debug_fh);
613 }
614  
615 # More debugging
616 sub _inspect_whois_response_lines {
617 0 ▶ my ($lines, $label) = @_;
Subroutine _inspect_whois_response_lines: not called CC=15 SCAR=54.8
618  
619 0 ▶ return unless $do_debugging;
Branch: 1 branch
BranchTrueFalseTotal
unless $do_debugging 0 0 0
620  
621 0 ▶ $label //= 'WHOIS response';
Condition0%0/2
$label //= "WHOIS response"
l!l
00
MC/DC0%0/1
$label //= "WHOIS response"
$label
Truth table0%0/2
A
A= $label
Aresult
11
00
622  
623 0 my @interesting;
624 my @unknown;
625  
626 LINE:
627 0 for my $line (@$lines) {
628 0 chomp $line;
629  
630 0 ▶ next LINE if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
631 0 ▶ next LINE if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
632 0 ▶ next LINE if $line =~ /^%/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^%/ 0 0 0
633  
634 0 ▶ if($line =~ /^(NetRange|CIDR|NetName|NetHandle|Parent|OrgName|Country):\s*(.+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(NetRange|CIDR|NetName|NetHandle|Parent|OrgName|Country):\s*(.+)$/i) 0 0 0
635 0 push @interesting, $line;
636 0 next LINE;
637 }
638  
639 0 ▶ if($line =~ /^(.+?)\s+(\S+)\s+\((NET-[^)]+)\)\s+(\d+\.\d+\.\d+\.\d+)\s+-\s+(\d+\.\d+\.\d+\.\d+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(.+?)\s+(\S+)\s+\((NET-[^)]+)\)\s+(\d+\.\d+\.\d+\.\d+)\s+-\s+(\d+\.\d+\.\d+\.\d+)\s*$/) 0 0 0
640 0 push @interesting, "ARIN-SUMMARY: $line";
641 0 next LINE;
642 }
643  
644 0 ▶ next LINE if $line =~ /^(Comment|Remarks|RegDate|Updated|Created|Last-Modified):/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^(Comment|Remarks|RegDate|Updated|Created|Last-Modified):/i 0 0 0
645 0 ▶ next LINE if $line =~ /^(OrgAbuse|OrgTech|OrgNOC|RTech|RNOC|RAbuse)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^(OrgAbuse|OrgTech|OrgNOC|RTech|RNOC|RAbuse)/i 0 0 0
646  
647 0 push @unknown, $line;
648 }
649  
650 0 _do_debug(sprintf(
651 "|%s: %d interesting, %d unknown raw WHOIS lines|",
652 $label, scalar @interesting, scalar @unknown
653 ));
654  
655 0 _do_debug("|\n=== $label: INTERESTING raw WHOIS lines ===|");
656 0 ▶ if(@interesting) {
Branch: 1 branch
BranchTrueFalseTotal
if (@interesting) { } 0 0 0
657 0 _do_debug("|$_|") for @interesting;
658 } else {
659 0 _do_debug("|none|");
660 }
661  
662 0 _do_debug("|\n=== $label: UNKNOWN raw WHOIS lines ===|");
663 0 ▶ if(@unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown) { } 0 0 0
664 0 _do_debug("|$_|") for @unknown;
665 } else {
666 0 _do_debug("|none|");
667 }
668  
669 0 return;
670 }
671  
672 sub _inspect_whois_response_array {
673 0 ▶ my ($responses, $label) = @_;
Subroutine _inspect_whois_response_array: not called CC=12 SCAR=50.5
674  
675 0 ▶ return unless $do_debugging;
Branch: 1 branch
BranchTrueFalseTotal
unless $do_debugging 0 0 0
676  
677 0 ▶ $label //= 'WHOIS response array';
Condition0%0/2
$label //= "WHOIS response array"
l!l
00
MC/DC0%0/1
$label //= "WHOIS response array"
$label
Truth table0%0/2
A
A= $label
Aresult
11
00
678  
679 0 my $out = "$label:\n";
680  
681 0 for my $i (0 .. $#$responses) {
682 0 my $response = $responses->[$i];
683  
684 0 $out .= " Response [$i]:\n";
685  
686 0 ▶ if (ref($response) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response ne "HASH") 0 0 0
687 0 ▶ $out .= " <not a HASH ref: " . (defined $response ? $response : 'undef') . ">\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $response ? : 0 0 0
688 0 next;
689 }
690  
691 0 for my $key (sort keys %$response) {
692 0 my $value = $response->{$key};
693  
694 0 ▶ if (ref($value) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value) { } 0 0 0
695 0 $out .= " $key:\n";
696 0 for my $item (@$value) {
697 0 ▶ $out .= " - " . (defined $item ? $item : '<undef>') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $item ? : 0 0 0
698 }
699 } elsif (ref($value)) {
700 0 $out .= " $key: <" . ref($value) . " ref>\n";
701 } else {
702 0 ▶ $out .= " $key: " . (defined $value ? $value : '<undef>') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
703 }
704 }
705 }
706  
707 0 _do_debug($out);
708 }
709  
710 3 1;
↑ Summary