blib/lib/Net/Whois/IP.pm

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