blib/lib/Net/Whois/IANA.pm

filter: Statementstmt 85.3% 268 / 314 Branchbran 64.8% 109 / 168 Conditioncond 41.2% 85 / 206 MC/DCmcdc 16.9% 18 / 106 Subroutinesub 94.4% 34 / 36 Podpod 7.6% 2 / 26 totaltotal 60.2% 516 / 856 CC 159 SCAR 36.5
CC
159
Cov
67%
is_valid_ipv6
272.0
set_source
66.8
ripe_process_query
39.9
CRAP 38.6
↑ Summary
1 package Net::Whois::IANA;
2 10 $Net::Whois::IANA::VERSION = '0.48';
3 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use IO::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Net::CIDR ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Net::Whois::IANA - A universal WHOIS data extractor.
15  
16 10 our $WHOIS_PORT = 43;
17 10 our $WHOIS_TIMEOUT = 30;
18 10 our @DEFAULT_SOURCE_ORDER = qw(arin ripe apnic lacnic afrinic);
19  
20 10 our %IANA;
21 10 our @IANA;
22  
23 BEGIN {
24 # populate the hash at compile time
25  
26 10 ▶ %IANA = (
Subroutine BEGIN: called CC=2 SCAR=7.0
27 apnic => [
28 [ 'whois.apnic.net', $WHOIS_PORT, $WHOIS_TIMEOUT, \&apnic_query ],
29 ],
30 ripe => [ [ 'whois.ripe.net', $WHOIS_PORT, $WHOIS_TIMEOUT, \&ripe_query ], ],
31 arin => [ [ 'whois.arin.net', $WHOIS_PORT, $WHOIS_TIMEOUT, \&arin_query ], ],
32 lacnic => [
33 [ 'whois.lacnic.net', $WHOIS_PORT, $WHOIS_TIMEOUT, \&lacnic_query ],
34 ],
35 afrinic => [
36 [
37 'whois.afrinic.net', $WHOIS_PORT,
38 $WHOIS_TIMEOUT, \&afrinic_query
39 ],
40 ],
41 );
42  
43 10 @IANA = sort keys %IANA;
44  
45 # accessors
46 # do not use AUTOLOAD - only accept lowercase function name
47 # define accessors at compile time
48 10 my @accessors = qw{country netname descr status source server inetnum inet6num cidr};
49  
50 10 foreach my $accessor (@accessors) {
51 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 *$accessor = sub {
53 26 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.4
54 26 ▶ die qq[$accessor is a method call] unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 26 26
55 26 ▶ return unless $self->{QUERY};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"QUERY"} 0 26 26
56 26 return $self->{QUERY}->{$accessor};
57 90 };
58 }
59  
60 10 *desc = \&descr; # backward compatibility
61 }
62  
63 10 our @EXPORT = qw( @IANA %IANA );
64  
65 sub new ($) {
66  
67 9 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
68 9 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
69 9 my $self = {};
70  
71 9 bless $self, $class;
72  
73 9 return $self;
74 }
75  
76 sub whois_connect ($;$$) {
77 19 ▶ my ( $host, $port, $timeout ) = @_;
Subroutine whois_connect: called CC=7 SCAR=21.5
Pod undocumented
78  
79 19 ▶ ( $host, $port, $timeout ) = @$host if ref $host;
Branch: 1 branch
BranchTrueFalseTotal
if ref $host 17 2 19
80  
81 19 ▶ $port ||= $WHOIS_PORT;
Condition66%2/3
$port ||= $Net::Whois::IANA::WHOIS_PORT
l!l&&r!l&&!r
2170
MC/DC0%0/2
$port ||= $Net::Whois::IANA::WHOIS_PORT
$port $Net::Whois::IANA::WHOIS_PORT
Truth table66%2/3
A ||= B
A= $portB= $Net::Whois::IANA::WHOIS_PORT
ABresult
1X1
011
000
82 19 ▶ $timeout ||= $WHOIS_TIMEOUT;
Condition66%2/3
$timeout ||= $Net::Whois::IANA::WHOIS_TIMEOUT
l!l&&r!l&&!r
2170
MC/DC0%0/2
$timeout ||= $Net::Whois::IANA::WHOIS_TIMEOUT
$timeout $Net::Whois::IANA::WHOIS_TIMEOUT
Truth table66%2/3
A ||= B
A= $timeoutB= $Net::Whois::IANA::WHOIS_TIMEOUT
ABresult
1X1
011
000
83  
84 #my $port = $host_ref->[1] || $WHOIS_PORT;
85 #my $timeout = $host_ref->[2] || $WHOIS_TIMEOUT;
86 #my $host = $host_ref->[0];
87 19 my $retries = 2;
88 19 my $sleep = 2;
89  
90 19 my $sock;
91  
92 19 foreach my $iter ( 0 .. $retries ) {
93 19 local $@;
94  
95 # catch errors
96 19 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sock = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port, "Timeout", $timeout); 1 } } and return $sock 19 0 19
97 19 $sock = IO::Socket::INET->new(
98 PeerAddr => $host,
99 PeerPort => $port,
100 Timeout => $timeout,
101 );
102 19 1;
103 } and return $sock;
104  
105 0 Carp::carp "Cannot connect to $host at port $port";
106 0 Carp::carp $@;
107 0 ▶ sleep $sleep unless $iter == $retries; # avoid the last sleep
Branch: 1 branch
BranchTrueFalseTotal
unless $iter == $retries 0 0 0
108 }
109 0 return 0;
110 }
111  
112 sub is_valid_ipv4 ($) {
113  
114 22 ▶ my $ip = shift;
Subroutine is_valid_ipv4: called CC=3 SCAR=11.0
Pod undocumented
115  
116 22 ▶ return $ip
Condition83%5/6
$ip && $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ && ($1 + 0 | $2 + 0 | $3 + 0 | $4 + 0) < 256
!ll&&!rl&&r
1120
$ip and $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/
!ll&&!rl&&r
0121
MC/DC66%2/3
$ip && $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ && ($1 + 0 | $2 + 0 | $3 + 0 | $4 + 0) < 256
$ip $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ($1 + 0 | $2 + 0 | $3 + 0 | $4 + 0) < 256
Truth table75%3/4
A and B && C
A= $ipB= $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/C= ($1 + 0 | $2 + 0 | $3 + 0 | $4 + 0) < 256
ABCresult
0XX0
10X0
1100
1111
117 && $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/
118  
119 # not absolutely correct
120 && ( ( $1 + 0 ) | ( $2 + 0 ) | ( $3 + 0 ) | ( $4 + 0 ) ) < 0x100;
121 }
122  
123 sub is_valid_ipv6 {
124 0 ▶ my ($ip) = @_;
Subroutine is_valid_ipv6: not called CC=16 SCAR=56.1
Pod undocumented
125  
126 return
127 0 ▶ if $ip =~ /^:[^:]/
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /^:[^:]/ or $ip =~ /[^:]:$/ 0 0 0
Condition0%0/3
$ip =~ /^:[^:]/ or $ip =~ /[^:]:$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip =~ /^:[^:]/ or $ip =~ /[^:]:$/
$ip =~ /^:[^:]/ $ip =~ /[^:]:$/
Truth table0%0/3
A or B
A= $ip =~ /^:[^:]/B= $ip =~ /[^:]:$/
ABresult
1X1
011
000
128 || $ip =~ /[^:]:$/; # Can't have single : on front or back
129  
130 0 my @seg = split /:/, $ip, -1; # -1 to keep trailing empty fields
131 # Clean up leading/trailing double colon effects.
132 0 ▶ shift @seg if $seg[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $seg[0] eq "" 0 0 0
133 0 ▶ pop @seg if $seg[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $seg[-1] eq "" 0 0 0
134  
135 0 my $max = 8;
136 0 ▶ if ( $seg[-1] =~ tr/.// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg[-1] =~ tr/.//) 0 0 0
137 0 ▶ return unless is_valid_ipv4( pop @seg );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_valid_ipv4(pop @seg) 0 0 0
138 0 $max -= 2;
139 }
140  
141 0 my $cmp;
142 0 for my $seg (@seg) {
143 0 ▶ if ( $seg eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg eq "") 0 0 0
144  
145 # Only one compression segment allowed.
146 0 ▶ return if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 0 0 0
147 0 ++$cmp;
148 0 next;
149 }
150 0 ▶ return if $seg =~ /[^0-9a-fA-F]/;
Branch: 1 branch
BranchTrueFalseTotal
if $seg =~ /[^0-9a-fA-F]/ 0 0 0
151 0 ▶ return if length $seg == 0 || length $seg > 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $seg == 0 or length $seg > 4 0 0 0
Condition0%0/3
length $seg == 0 or length $seg > 4
l!l&&r!l&&!r
000
MC/DC0%0/2
length $seg == 0 or length $seg > 4
length $seg == 0 length $seg > 4
Truth table0%0/3
A or B
A= length $seg == 0B= length $seg > 4
ABresult
1X1
011
000
152 }
153 0 ▶ if ($cmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp) 0 0 0
154  
155 # If compressed, we need fewer than $max segments, but at least 1
156 0 ▶ return ( @seg && @seg < $max ) && 1; # true returned as 1
Condition0%0/5
@seg && @seg < $max && 1
!ll
00
@seg and @seg < $max
!ll&&!rl&&r
000
MC/DC0%0/2
@seg && @seg < $max && 1
@seg @seg < $max
Truth table0%0/3
A and B
A= @segB= @seg < $max
ABresult
0X0
100
111
157 }
158  
159 # Not compressed, all segments need to be there.
160 0 return $max == @seg;
161 }
162  
163 # Is valid IP v4 or IP v6 address.
164 sub is_valid_ip ($) {
165 23 ▶ my ($ip) = @_;
Subroutine is_valid_ip: called CC=3 SCAR=11.1
Pod undocumented
166  
167 23 ▶ return unless defined $ip; # shortcut earlier
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 22 23
168 22 ▶ return index( $ip, ':' ) >= 0 ? is_valid_ipv6($ip) : is_valid_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
index($ip, ':') != -1 ? : 0 22 22
169 }
170  
171 sub set_source ($$) {
172  
173 16 ▶ my $self = shift;
Subroutine set_source: called CC=14 SCAR=42.0
Pod undocumented
174 16 my $source = shift;
175  
176 16 ▶ $self->{source} = {%IANA} || return 0 unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 2 14 16
Condition50%1/2
{%Net::Whois::IANA::IANA} || (return 0)
l!l
20
MC/DC0%0/1
{%Net::Whois::IANA::IANA} || (return 0)
{%Net::Whois::IANA::IANA}
Truth table50%1/2
A
A= {%Net::Whois::IANA::IANA}
Aresult
11
00
177 16 ▶ return 0 unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 2 14 16
178 14 ▶ unless ( ref $source ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $source) 14 0 14
179 14 ▶ if ( $IANA{$source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Whois::IANA::IANA{$source}) 13 1 14
180 13 $self->{source} = { $source => $IANA{$source} };
181 13 return 0;
182 }
183 1 return 1;
184 }
185 return 2
186 unless ref $source eq 'HASH'
187 0 ▶ && scalar grep { ref $_ && ref $_ eq 'ARRAY' && @{$_} && ref $_->[0] && ref $_->[0] eq 'ARRAY' && @{ $_->[0] } && $_->[0][0] } values %{$source} == scalar keys %{$source};
Branch: 2 branches
BranchTrueFalseTotal
unless ref $source eq "HASH" and scalar grep({ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0] and ref $_->[0] eq "ARRAY" and @{$_->[0];} and $_->[0][0];} values %{$source;} == scalar keys %{$source;}) 0 0 0
ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0] and ref $_->[0] eq "ARRAY" and @{$_->[0];} and $_->[0][0] 0 0 0
Condition0%0/17
ref $source eq "HASH" and scalar grep({ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0] and ref $_->[0] eq "ARRAY" and @{$_->[0];} and $_->[0][0];} values %{$source;} == scalar keys %{$source;})
!ll
00
ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0] and ref $_->[0] eq "ARRAY" and @{$_->[0];}
!ll&&!rl&&r
000
ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0] and ref $_->[0] eq "ARRAY"
!ll&&!rl&&r
000
ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0]
!ll&&!rl&&r
000
ref $_ and ref $_ eq "ARRAY" and @{$_;}
!ll&&!rl&&r
000
ref $_ and ref $_ eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/1
ref $source eq "HASH" and scalar grep({ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0] and ref $_->[0] eq "ARRAY" and @{$_->[0];} and $_->[0][0];} values %{$source;} == scalar keys %{$source;})
ref $source eq "HASH"
MC/DC0%0/6
ref $_ and ref $_ eq "ARRAY" and @{$_;} and ref $_->[0] and ref $_->[0] eq "ARRAY" and @{$_->[0];}
ref $_ ref $_ eq "ARRAY" @{$_;} ref $_->[0] ref $_->[0] eq "ARRAY" @{$_->[0];}
Truth table0%0/2
A
A= ref $source eq "HASH"
Aresult
00
11
Truth table0%0/7
A and B and C and D and E and F
A= ref $_B= ref $_ eq "ARRAY"C= @{$_;}D= ref $_->[0]E= ref $_->[0] eq "ARRAY"F= @{$_->[0];}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
188 0 $self->{source} = $source;
189 0 return 0;
190 }
191  
192 sub init_query ($%) {
193  
194 19 ▶ my $self = shift;
Subroutine init_query: called CC=5 SCAR=16.3
Pod undocumented
195 19 my %param = @_;
196  
197 19 ▶ if ( !is_valid_ip( $param{-ip} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is_valid_ip($param{"-ip"})) 3 16 19
198 3 warn q{
199 Method usage:
200 $iana->whois_query(
201 -ip=>$ip,
202 -debug=>$debug, # optional
203 -whois=>$whois | -mywhois=>\%mywhois, # optional
204 };
205 3 return {};
206 }
207  
208 16 ▶ my $set_source = $self->set_source( $param{-whois} || $param{-mywhois} );
Condition66%2/3
$param{'-whois'} || $param{'-mywhois'}
l!l&&r!l&&!r
1402
MC/DC50%1/2
$param{'-whois'} || $param{'-mywhois'}
$param{'-whois'} $param{'-mywhois'}
Truth table66%2/3
A || B
A= $param{'-whois'}B= $param{'-mywhois'}
ABresult
1X1
011
000
209 16 ▶ if ( $set_source == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($set_source == 1) { } 1 15 16
elsif ($set_source == 2) { } 0 15 15
210 1 warn "Unknown whois server requested. Known servers are:\n";
211 1 warn join( ", ", @IANA ) . "\n";
212 1 return {};
213 }
214 elsif ( $set_source == 2 ) {
215 0 warn q{
216 Custom sources must be of form:
217 %source = (
218 source_name1 => [
219 [ source_host, source_port || undef, source_timeout || undef, \&source_query || undef ],
220 ],
221 source_name1 => [
222 [ source_host, source_port || undef, source_timeout || undef, \&source_query || undef ],
223 ],
224 ...,
225 );
226 };
227 }
228 }
229  
230 sub source_connect ($$) {
231 64 ▶ my ( $self, $source_name ) = @_;
Subroutine source_connect: called CC=5 SCAR=16.7
Pod undocumented
232  
233 64 foreach my $server_ref ( @{ $self->{source}{$source_name} } ) {
234 17 ▶ if ( my $sock = whois_connect($server_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sock = &whois_connect($server_ref)) 17 0 17
235 17 my ( $whois_host, $whois_port, $whois_timeout, $query_code ) = @{$server_ref};
236 17 ▶ $self->{query_sub} = $query_code
Branch: 1 branch
BranchTrueFalseTotal
$query_code && ref $query_code eq 'CODE' ? : 17 0 17
Condition33%1/3
$query_code and ref $query_code eq "CODE"
!ll&&!rl&&r
0017
MC/DC0%0/2
$query_code and ref $query_code eq "CODE"
$query_code ref $query_code eq "CODE"
Truth table33%1/3
A and B
A= $query_codeB= ref $query_code eq "CODE"
ABresult
0X0
100
111
237 && ref $query_code eq 'CODE' ? $query_code : \&default_query;
238 17 $self->{whois_host} = $whois_host;
239 17 return $sock;
240 }
241 }
242 47 return undef;
243 }
244  
245 sub post_process_query (%) {
246  
247 14 ▶ my %query = @_;
Subroutine post_process_query: called CC=17 SCAR=30.6
Pod undocumented
248 14 for my $qkey ( keys %query ) {
249 294 ▶ chomp $query{$qkey} if defined $query{$qkey};
Branch: 1 branch
BranchTrueFalseTotal
if defined $query{$qkey} 293 1 294
250 $query{abuse} = $query{$qkey} and last
251 294 ▶ if $qkey =~ /abuse/i && $query{$qkey} =~ /\@/;
Branch: 1 branch
BranchTrueFalseTotal
if $qkey =~ /abuse/i and $query{$qkey} =~ /\@/ 2 292 294
Condition75%6/8
$qkey =~ /abuse/i && $query{$qkey} =~ /\@/ && (($query{'abuse'} = $query{$qkey}) && (last))
!ll&&!rl&&r
29220
$qkey =~ /abuse/i and $query{$qkey} =~ /\@/
!ll&&!rl&&r
281112
$query{"abuse"} = $query{$qkey} and last
!ll
02
MC/DC0%0/3
$qkey =~ /abuse/i && $query{$qkey} =~ /\@/ && (($query{'abuse'} = $query{$qkey}) && (last))
$qkey =~ /abuse/i $query{$qkey} =~ /\@/ $query{"abuse"} = $query{$qkey}
Truth table50%2/4
A and B && C
A= $qkey =~ /abuse/iB= $query{$qkey} =~ /\@/C= $query{"abuse"} = $query{$qkey}
ABCresult
0XX0
10X0
1100
1111
252 }
253 14 ▶ unless ( $query{abuse} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query{"abuse"}) 12 2 14
254 12 ▶ if ( $query{fullinfo} && $query{fullinfo} =~ /(\S*abuse\S*\@\S+)/m ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($query{'fullinfo'} and $query{'fullinfo'} =~ /(\S*abuse\S*\@\S+)/m) { } 10 2 12
elsif ($query{'email'} or $query{'e-mail'} or $query{'orgtechemail'}) { } 1 1 2
Condition66%2/3
$query{"fullinfo"} and $query{"fullinfo"} =~ /(\S*abuse\S*\@\S+)/m
!ll&&!rl&&r
0210
MC/DC50%1/2
$query{"fullinfo"} and $query{"fullinfo"} =~ /(\S*abuse\S*\@\S+)/m
$query{"fullinfo"} $query{"fullinfo"} =~ /(\S*abuse\S*\@\S+)/m
Truth table66%2/3
A and B
A= $query{"fullinfo"}B= $query{"fullinfo"} =~ /(\S*abuse\S*\@\S+)/m
ABresult
0X0
100
111
255 10 $query{abuse} = $1;
256 }
257 ▶ elsif ( $query{email} || $query{'e-mail'} || $query{orgtechemail} ) {
Condition66%4/6
$query{"email"} or $query{"e-mail"} or $query{"orgtechemail"}
l!l&&r!l&&!r
101
$query{"email"} or $query{"e-mail"}
l!l&&r!l&&!r
011
MC/DC33%1/3
$query{"email"} or $query{"e-mail"} or $query{"orgtechemail"}
$query{"email"} $query{"e-mail"} $query{"orgtechemail"}
Truth table50%2/4
A or B or C
A= $query{"email"}B= $query{"e-mail"}C= $query{"orgtechemail"}
ABCresult
1XX1
01X1
0011
0000
258 $query{abuse} =
259 1 ▶ $query{email} || $query{'e-mail'} || $query{orgtechemail};
Condition33%2/6
$query{'email'} || $query{'e-mail'} || $query{'orgtechemail'}
l!l&&r!l&&!r
100
$query{"email"} or $query{"e-mail"}
l!l&&r!l&&!r
010
MC/DC0%0/3
$query{'email'} || $query{'e-mail'} || $query{'orgtechemail'}
$query{"email"} $query{"e-mail"} $query{'orgtechemail'}
Truth table25%1/4
A or B || C
A= $query{"email"}B= $query{"e-mail"}C= $query{'orgtechemail'}
ABCresult
1XX1
01X1
0011
0000
260 }
261 }
262 14 ▶ if ( !ref $query{cidr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $query{"cidr"}) 2 12 14
263 2 ▶ if ( defined $query{cidr} && $query{cidr} =~ /\,/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query{'cidr'} and $query{'cidr'} =~ /\,/) { } 0 2 2
Condition33%1/3
defined $query{"cidr"} and $query{"cidr"} =~ /\,/
!ll&&!rl&&r
020
MC/DC0%0/2
defined $query{"cidr"} and $query{"cidr"} =~ /\,/
defined $query{"cidr"} $query{"cidr"} =~ /\,/
Truth table33%1/3
A and B
A= defined $query{"cidr"}B= $query{"cidr"} =~ /\,/
ABresult
0X0
100
111
264 0 $query{cidr} = [ split( /\s*\,\s*/, $query{cidr} ) ];
265 }
266 else {
267 2 $query{cidr} = [ $query{cidr} ];
268 }
269 }
270  
271 14 return %query;
272 }
273  
274 sub whois_query ($%) {
275 19 ▶ my ( $self, %params ) = @_;
Subroutine whois_query: called CC=8 SCAR=21.9
Pod documented
276  
277 19 $self->init_query(%params);
278 19 $self->{QUERY} = {};
279  
280 19 for my $source_name (@DEFAULT_SOURCE_ORDER) {
281 64 ▶ print STDERR "Querying $source_name ...\n" if $params{-debug};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"-debug"} 0 64 64
282 64 ▶ my $sock = $self->source_connect($source_name)
Condition80%4/5
$self->source_connect($source_name) || Carp::carp("Connection failed to $source_name." && (next))
l!l&&r!l&&!r
172720
"Connection failed to $source_name." && (next)
!ll
047
MC/DC0%0/2
$self->source_connect($source_name) || Carp::carp("Connection failed to $source_name." && (next))
$self->source_connect($source_name) Carp::carp("Connection failed to $source_name." && (next))
MC/DC0%0/1
"Connection failed to $source_name." && (next)
"Connection failed to $source_name."
Truth table33%1/3
A || B
A= $self->source_connect($source_name)B= Carp::carp("Connection failed to $source_name." && (next))
ABresult
1X1
011
000
Truth table50%1/2
A
A= "Connection failed to $source_name."
Aresult
00
11
283 || Carp::carp "Connection failed to $source_name." && next;
284 17 my %query = $self->{query_sub}( $sock, $params{-ip} );
285  
286 17 ▶ next unless keys %query;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %query 3 14 17
287 0 do { Carp::carp "Warning: permission denied at $source_name server $self->{whois_host}\n"; next }
288 14 ▶ if $query{permission} && $query{permission} eq 'denied';
Branch: 1 branch
BranchTrueFalseTotal
if ($query{"permission"} and $query{"permission"} eq "denied") 0 14 14
Condition33%1/3
$query{"permission"} and $query{"permission"} eq "denied"
!ll&&!rl&&r
0140
MC/DC0%0/2
$query{"permission"} and $query{"permission"} eq "denied"
$query{"permission"} $query{"permission"} eq "denied"
Truth table33%1/3
A and B
A= $query{"permission"}B= $query{"permission"} eq "denied"
ABresult
0X0
100
111
289 14 $query{server} = uc $source_name;
290 14 $self->{QUERY} = { post_process_query(%query) };
291  
292 14 return $self->{QUERY};
293 }
294  
295 5 return {};
296 }
297  
298 sub default_query ($$) {
299  
300 0 ▶ return arin_query(@_);
Subroutine default_query: not called CC=1 SCAR=6.9
Pod undocumented
301 }
302  
303 sub ripe_read_query ($$) {
304  
305 4 ▶ my ( $sock, $ip ) = @_;
Subroutine ripe_read_query: called CC=7 SCAR=19.8
Pod undocumented
306  
307 4 my %query = ( fullinfo => '' );
308 4 print $sock "-r $ip\n";
309 4 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 171 4 175
310 171 $query{fullinfo} .= $_;
311 171 ▶ close $sock and return ( permission => 'denied' ) if /ERROR:201/;
Branch: 1 branch
BranchTrueFalseTotal
if /ERROR:201/ 0 171 171
Condition20%1/5
/ERROR:201/ && (close $sock && (return 'permission', 'denied'))
!ll&&!rl&&r
17100
close $sock and return "permission", "denied"
!ll
00
MC/DC0%0/2
/ERROR:201/ && (close $sock && (return 'permission', 'denied'))
/ERROR:201/ close $sock
Truth table33%1/3
A && B
A= /ERROR:201/B= close $sock
ABresult
0X0
100
111
312 171 ▶ next if ( /^(\%|\#)/ || !/\:/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^(\%|\#)/ or not /\:/ 83 88 171
Condition100%3/3
/^(\%|\#)/ or not /\:/
l!l&&r!l&&!r
453888
MC/DC100%2/2
/^(\%|\#)/ or not /\:/
/^(\%|\#)/ not /\:/
Truth table100%3/3
A or not(B)
A= /^(\%|\#)/B= not /\:/
ABresult
1X1
011
000
313 88 s/\s+$//;
314 88 my ( $field, $value ) = split( /:/, $_, 2 );
315 88 $value =~ s/^\s+//;
316 88 ▶ $query{ lc($field) } .= ( $query{ lc($field) } ? ' ' : '' ) . $value;
Branch: 1 branch
BranchTrueFalseTotal
$query{lc $field} ? : 28 60 88
317 }
318 4 close $sock;
319 4 return %query;
320 }
321  
322 sub ripe_process_query (%) {
323  
324 4 ▶ my %query = @_;
Subroutine ripe_process_query: called CC=12 SCAR=36.9
Pod undocumented
325  
326 4 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (defined $query{'remarks'} and $query{'remarks'} =~ /The country is really world wide/ or defined $query{'netname'} and $query{'netname'} =~ /IANA-BLK/ or defined $query{'netname'} and $query{'netname'} =~ /AFRINIC-NET-TRANSFERRED/ or defined $query{'country'} and $query{'country'} =~ /world wide/) { } 0 4 4
elsif (not $query{'inet6num'} || $query{'inetnum'}) { } 0 4 4
Condition33%7/21
defined $query{"remarks"} and $query{"remarks"} =~ /The country is really world wide/ or defined $query{"netname"} and $query{"netname"} =~ /IANA-BLK/ or defined $query{"netname"} and $query{"netname"} =~ /AFRINIC-NET-TRANSFERRED/ or defined $query{"country"} and $query{"country"} =~ /world wide/
l!l&&r!l&&!r
004
defined $query{"remarks"} and $query{"remarks"} =~ /The country is really world wide/ or defined $query{"netname"} and $query{"netname"} =~ /IANA-BLK/ or defined $query{"netname"} and $query{"netname"} =~ /AFRINIC-NET-TRANSFERRED/
l!l&&r!l&&!r
004
defined $query{"remarks"} and $query{"remarks"} =~ /The country is really world wide/ or defined $query{"netname"} and $query{"netname"} =~ /IANA-BLK/
l!l&&r!l&&!r
004
defined $query{"remarks"} and $query{"remarks"} =~ /The country is really world wide/
!ll&&!rl&&r
040
defined $query{"netname"} and $query{"netname"} =~ /IANA-BLK/
!ll&&!rl&&r
040
defined $query{"netname"} and $query{"netname"} =~ /AFRINIC-NET-TRANSFERRED/
!ll&&!rl&&r
040
defined $query{"country"} and $query{"country"} =~ /world wide/
!ll&&!rl&&r
040
MC/DC0%0/8
defined $query{"remarks"} and $query{"remarks"} =~ /The country is really world wide/ or defined $query{"netname"} and $query{"netname"} =~ /IANA-BLK/ or defined $query{"netname"} and $query{"netname"} =~ /AFRINIC-NET-TRANSFERRED/ or defined $query{"country"} and $query{"country"} =~ /world wide/
defined $query{"remarks"} $query{"remarks"} =~ /The country is really world wide/ defined $query{"netname"} $query{"netname"} =~ /IANA-BLK/ defined $query{"netname"} $query{"netname"} =~ /AFRINIC-NET-TRANSFERRED/ defined $query{"country"} $query{"country"} =~ /world wide/
Truth table3%1/31
A and B or C and D or E and F or G and H
A= defined $query{"remarks"}B= $query{"remarks"} =~ /The country is really world wide/C= defined $query{"netname"}D= $query{"netname"} =~ /IANA-BLK/E= defined $query{"netname"}F= $query{"netname"} =~ /AFRINIC-NET-TRANSFERRED/G= defined $query{"country"}H= $query{"country"} =~ /world wide/
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X1011XX1
100X11XX1
101011XX1
0X0X0X111
0X0X10111
0X100X111
0X1010111
100X0X111
100X10111
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
327 ( defined $query{remarks} && $query{remarks} =~ /The country is really world wide/ )
328 || ( defined $query{netname}
329 && $query{netname} =~ /IANA-BLK/ )
330 || ( defined $query{netname}
331 && $query{netname} =~ /AFRINIC-NET-TRANSFERRED/ )
332 || ( defined $query{country}
333 && $query{country} =~ /world wide/ )
334 ) {
335 0 return ();
336 }
337 ▶ elsif ( !$query{inet6num} && !$query{inetnum} ) {
Condition33%1/3
$query{'inet6num'} || $query{'inetnum'}
l!l&&r!l&&!r
040
MC/DC0%0/2
$query{'inet6num'} || $query{'inetnum'}
$query{'inet6num'} $query{'inetnum'}
Truth table33%1/3
A || B
A= $query{'inet6num'}B= $query{'inetnum'}
ABresult
1X1
011
000
338 0 return ();
339 }
340 else {
341 4 $query{permission} = 'allowed';
342 4 ▶ $query{cidr} = [ Net::CIDR::range2cidr( uc( $query{inet6num} || $query{inetnum} ) ) ];
Condition33%1/3
$query{'inet6num'} || $query{'inetnum'}
l!l&&r!l&&!r
040
MC/DC0%0/2
$query{'inet6num'} || $query{'inetnum'}
$query{'inet6num'} $query{'inetnum'}
Truth table33%1/3
A || B
A= $query{'inet6num'}B= $query{'inetnum'}
ABresult
1X1
011
000
343 }
344 4 return %query;
345 }
346  
347 sub ripe_query ($$) {
348 4 ▶ my ( $sock, $ip ) = @_;
Subroutine ripe_query: called CC=2 SCAR=7.0
Pod undocumented
349  
350 4 my %query = ripe_read_query( $sock, $ip );
351 4 ▶ return () unless defined $query{country};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query{"country"} 0 4 4
352 4 return ripe_process_query(%query);
353 }
354  
355 sub apnic_read_query ($$) {
356 5 ▶ my ( $sock, $ip ) = @_;
Subroutine apnic_read_query: called CC=15 SCAR=27.7
Pod undocumented
357  
358 5 my %query = ( fullinfo => '' );
359 5 my %tmp;
360 5 print $sock "-r $ip\n";
361 5 my $skip_block = 0;
362 5 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 200 5 205
363 200 $query{fullinfo} .= $_;
364 200 ▶ close $sock and return ( permission => 'denied' ) if /^\%201/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\%201/ 0 200 200
Condition20%1/5
/^\%201/ && (close $sock && (return 'permission', 'denied'))
!ll&&!rl&&r
20000
close $sock and return "permission", "denied"
!ll
00
MC/DC0%0/2
/^\%201/ && (close $sock && (return 'permission', 'denied'))
/^\%201/ close $sock
Truth table33%1/3
A && B
A= /^\%201/B= close $sock
ABresult
0X0
100
111
365 200 ▶ if (m{^\%}) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\%/) 33 167 200
366  
367 # Always skip 0.0.0.0 data
368 # It looks like:
369 # % Information related to '0.0.0.0 - 255.255.255.255'
370 33 ▶ if (m{^\%.*0\.0\.0\.0\s+}) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\%.*0\.0\.0\.0\s+/) 0 33 33
371 0 $skip_block = 1;
372 0 next;
373 }
374 33 $skip_block = 0;
375 33 next;
376 }
377 167 ▶ next if $skip_block;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_block 0 167 167
378 167 ▶ next if ( !/\:/ );
Branch: 1 branch
BranchTrueFalseTotal
unless /\:/ 44 123 167
379 123 s/\s+$//;
380 123 my ( $field, $value ) = split( /:/, $_, 2 );
381 123 $value =~ s/^\s+//;
382 123 ▶ if ( $field =~ /^inet6?num$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /^inet6?num$/) 6 117 123
383 6 ▶ next if $value =~ m{0\.0\.0\.0\s+};
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /0\.0\.0\.0\s+/ 0 6 6
384 6 %tmp = %query;
385 6 %query = ();
386 6 $query{fullinfo} = $tmp{fullinfo};
387 }
388 123 my $lc_field = lc($field);
389 123 ▶ next if $lc_field eq 'country' && defined $query{$lc_field};
Branch: 1 branch
BranchTrueFalseTotal
if $lc_field eq "country" and defined $query{$lc_field} 1 122 123
Condition100%3/3
$lc_field eq "country" and defined $query{$lc_field}
!ll&&!rl&&r
11661
MC/DC100%2/2
$lc_field eq "country" and defined $query{$lc_field}
$lc_field eq "country" defined $query{$lc_field}
Truth table100%3/3
A and B
A= $lc_field eq "country"B= defined $query{$lc_field}
ABresult
0X0
100
111
390 122 ▶ $query{$lc_field} .= ( $query{$lc_field} ? ' ' : '' ) . $value;
Branch: 1 branch
BranchTrueFalseTotal
$query{$lc_field} ? : 35 87 122
391 }
392 5 close $sock;
393 5 for ( keys %tmp ) {
394 17 ▶ $query{$_} = $tmp{$_} if !defined $query{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query{$_} 3 14 17
395 }
396 5 return %query;
397 }
398  
399 sub apnic_process_query (%) {
400 3 ▶ my %query = @_;
Subroutine apnic_process_query: called CC=8 SCAR=27.7
Pod undocumented
401  
402 3 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (defined $query{'remarks'} and $query{'remarks'} =~ /address range is not administered by APNIC|This network in not allocated/ or defined $query{'descr'} and $query{'descr'} =~ /not allocated to|by APNIC|placeholder reference/i) { } 0 3 3
elsif (not $query{'inet6num'} || $query{'inetnum'}) { } 0 3 3
Condition44%4/9
defined $query{"remarks"} and $query{"remarks"} =~ /address range is not administered by APNIC|This network in not allocated/ or defined $query{"descr"} and $query{"descr"} =~ /not allocated to|by APNIC|placeholder reference/i
l!l&&r!l&&!r
003
defined $query{"remarks"} and $query{"remarks"} =~ /address range is not administered by APNIC|This network in not allocated/
!ll&&!rl&&r
120
defined $query{"descr"} and $query{"descr"} =~ /not allocated to|by APNIC|placeholder reference/i
!ll&&!rl&&r
030
MC/DC0%0/4
defined $query{"remarks"} and $query{"remarks"} =~ /address range is not administered by APNIC|This network in not allocated/ or defined $query{"descr"} and $query{"descr"} =~ /not allocated to|by APNIC|placeholder reference/i
defined $query{"remarks"} $query{"remarks"} =~ /address range is not administered by APNIC|This network in not allocated/ defined $query{"descr"} $query{"descr"} =~ /not allocated to|by APNIC|placeholder reference/i
Truth table28%2/7
A and B or C and D
A= defined $query{"remarks"}B= $query{"remarks"} =~ /address range is not administered by APNIC|This network in not allocated/C= defined $query{"descr"}D= $query{"descr"} =~ /not allocated to|by APNIC|placeholder reference/i
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
403 ( defined $query{remarks} && $query{remarks} =~ /address range is not administered by APNIC|This network in not allocated/ )
404 || ( defined $query{descr}
405 && $query{descr} =~ /not allocated to|by APNIC|placeholder reference/i )
406 ) {
407 0 return ();
408 }
409 ▶ elsif ( !$query{inet6num} && !$query{inetnum} ) {
Condition33%1/3
$query{'inet6num'} || $query{'inetnum'}
l!l&&r!l&&!r
030
MC/DC0%0/2
$query{'inet6num'} || $query{'inetnum'}
$query{'inet6num'} $query{'inetnum'}
Truth table33%1/3
A || B
A= $query{'inet6num'}B= $query{'inetnum'}
ABresult
1X1
011
000
410 0 return ();
411 }
412 else {
413 3 $query{permission} = 'allowed';
414 3 ▶ $query{cidr} = [ Net::CIDR::range2cidr( uc( $query{inet6num} || $query{inetnum} ) ) ];
Condition33%1/3
$query{'inet6num'} || $query{'inetnum'}
l!l&&r!l&&!r
030
MC/DC0%0/2
$query{'inet6num'} || $query{'inetnum'}
$query{'inet6num'} $query{'inetnum'}
Truth table33%1/3
A || B
A= $query{'inet6num'}B= $query{'inetnum'}
ABresult
1X1
011
000
415 }
416  
417 3 return %query;
418 }
419  
420 sub apnic_query ($$) {
421 3 ▶ my ( $sock, $ip ) = @_;
Subroutine apnic_query: called CC=1 SCAR=0.0
Pod undocumented
422  
423 3 my %query = apnic_read_query( $sock, $ip );
424 3 return apnic_process_query(%query);
425 }
426  
427 sub arin_read_query ($$) {
428 3 ▶ my ( $sock, $ip ) = @_;
Subroutine arin_read_query: called CC=13 SCAR=26.2
Pod undocumented
429  
430 3 my %query = ( fullinfo => '' );
431 3 my %tmp = ();
432  
433 3 print $sock "+ $ip\n";
434 3 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 265 3 268
435 265 $query{fullinfo} .= $_;
436 265 ▶ close $sock and return ( permission => 'denied' ) if /^\#201/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\#201/ 0 265 265
Condition20%1/5
/^\#201/ && (close $sock && (return 'permission', 'denied'))
!ll&&!rl&&r
26500
close $sock and return "permission", "denied"
!ll
00
MC/DC0%0/2
/^\#201/ && (close $sock && (return 'permission', 'denied'))
/^\#201/ close $sock
Truth table33%1/3
A && B
A= /^\#201/B= close $sock
ABresult
0X0
100
111
437 265 ▶ return () if /no match found for/i;
Branch: 1 branch
BranchTrueFalseTotal
if /no match found for/i 0 265 265
438 265 ▶ next if ( /^\#/ || !/\:/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\#/ or not /\:/ 117 148 265
Condition100%3/3
/^\#/ or not /\:/
l!l&&r!l&&!r
7245148
MC/DC100%2/2
/^\#/ or not /\:/
/^\#/ not /\:/
Truth table100%3/3
A or not(B)
A= /^\#/B= not /\:/
ABresult
1X1
011
000
439 148 s/\s+$//;
440 148 my ( $field, $value ) = split( /:/, $_, 2 );
441 148 $value =~ s/^\s+//;
442 148 ▶ if ( $field eq 'OrgName'
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "OrgName" or $field eq "CustName") 3 145 148
Condition66%2/3
$field eq "OrgName" or $field eq "CustName"
l!l&&r!l&&!r
30145
MC/DC50%1/2
$field eq "OrgName" or $field eq "CustName"
$field eq "OrgName" $field eq "CustName"
Truth table66%2/3
A or B
A= $field eq "OrgName"B= $field eq "CustName"
ABresult
1X1
011
000
443 || $field eq 'CustName' ) {
444 3 %tmp = %query;
445 3 %query = ();
446 3 $query{fullinfo} = $tmp{fullinfo};
447 }
448 148 ▶ $query{ lc($field) } .= ( $query{ lc($field) } ? ' ' : '' ) . $value;
Branch: 1 branch
BranchTrueFalseTotal
$query{lc $field} ? : 34 114 148
449 }
450 3 close $sock;
451  
452 3 ▶ $query{orgname} = $query{custname} if defined $query{custname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $query{"custname"} 0 3 3
453  
454 3 for ( keys %tmp ) {
455 39 ▶ $query{$_} = $tmp{$_} unless defined $query{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query{$_} 25 14 39
456 }
457  
458 3 return %query;
459 }
460  
461 sub arin_process_query (%) {
462 3 ▶ my %query = @_;
Subroutine arin_process_query: called CC=5 SCAR=16.1
Pod undocumented
463  
464 return ()
465 3 ▶ if $query{orgid} && $query{orgid} =~ /^\s*RIPE|LACNIC|APNIC|AFRINIC\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $query{"orgid"} and $query{"orgid"} =~ /^\s*RIPE|LACNIC|APNIC|AFRINIC\s*$/ 1 2 3
Condition66%2/3
$query{"orgid"} and $query{"orgid"} =~ /^\s*RIPE|LACNIC|APNIC|AFRINIC\s*$/
!ll&&!rl&&r
021
MC/DC50%1/2
$query{"orgid"} and $query{"orgid"} =~ /^\s*RIPE|LACNIC|APNIC|AFRINIC\s*$/
$query{"orgid"} $query{"orgid"} =~ /^\s*RIPE|LACNIC|APNIC|AFRINIC\s*$/
Truth table66%2/3
A and B
A= $query{"orgid"}B= $query{"orgid"} =~ /^\s*RIPE|LACNIC|APNIC|AFRINIC\s*$/
ABresult
0X0
100
111
466  
467 2 $query{permission} = 'allowed';
468 2 $query{descr} = $query{orgname};
469 2 $query{remarks} = $query{comment};
470 2 $query{status} = $query{nettype};
471 2 $query{inetnum} = $query{netrange};
472 2 $query{source} = 'ARIN';
473 2 ▶ if ( defined $query{cidr} && $query{cidr} =~ /\,/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query{'cidr'} and $query{'cidr'} =~ /\,/) { } 1 1 2
Condition66%2/3
defined $query{"cidr"} and $query{"cidr"} =~ /\,/
!ll&&!rl&&r
011
MC/DC50%1/2
defined $query{"cidr"} and $query{"cidr"} =~ /\,/
defined $query{"cidr"} $query{"cidr"} =~ /\,/
Truth table66%2/3
A and B
A= defined $query{"cidr"}B= $query{"cidr"} =~ /\,/
ABresult
0X0
100
111
474 1 $query{cidr} = [ split( /\s*\,\s*/, $query{cidr} ) ];
475 }
476 else {
477 1 $query{cidr} = [ $query{cidr} ];
478 }
479  
480 2 return %query;
481 }
482  
483 sub arin_query ($$) {
484 3 ▶ my ( $sock, $ip ) = @_;
Subroutine arin_query: called CC=1 SCAR=0.0
Pod undocumented
485  
486 3 my %query = arin_read_query( $sock, $ip );
487  
488 3 return arin_process_query(%query);
489 }
490  
491 sub lacnic_read_query ($$) {
492 3 ▶ my ( $sock, $ip ) = @_;
Subroutine lacnic_read_query: called CC=15 SCAR=30.1
Pod undocumented
493  
494 3 my %query = ( fullinfo => '' );
495  
496 3 print $sock "$ip\n";
497  
498 3 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 171 3 174
499 171 $query{fullinfo} .= $_;
500 171 ▶ close $sock
Branch: 1 branch
BranchTrueFalseTotal
if /^\%201/ or /^\% Query rate limit exceeded/ or /^\% Not assigned to LACNIC/ or /\% Permission denied/ 0 171 171
Condition28%4/14
(/^\%201/ || /^\% Query rate limit exceeded/ || /^\% Not assigned to LACNIC/ || /\% Permission denied/) && (close $sock && (return 'permission', 'denied'))
!ll&&!rl&&r
17100
/^\%201/ or /^\% Query rate limit exceeded/ or /^\% Not assigned to LACNIC/ or /\% Permission denied/
l!l&&r!l&&!r
00171
/^\%201/ or /^\% Query rate limit exceeded/ or /^\% Not assigned to LACNIC/
l!l&&r!l&&!r
00171
/^\%201/ or /^\% Query rate limit exceeded/
l!l&&r!l&&!r
00171
close $sock and return "permission", "denied"
!ll
00
MC/DC0%0/5
(/^\%201/ || /^\% Query rate limit exceeded/ || /^\% Not assigned to LACNIC/ || /\% Permission denied/) && (close $sock && (return 'permission', 'denied'))
/^\%201/ /^\% Query rate limit exceeded/ /^\% Not assigned to LACNIC/ /\% Permission denied/ close $sock
Truth table11%1/9
A or B or C or D && E
A= /^\%201/B= /^\% Query rate limit exceeded/C= /^\% Not assigned to LACNIC/D= /\% Permission denied/E= close $sock
ABCDEresult
0000X0
1XXX00
01XX00
001X00
000100
1XXX11
01XX11
001X11
000111
501 and return ( permission => 'denied' )
502 if /^\%201/ || /^\% Query rate limit exceeded/ || /^\% Not assigned to LACNIC/ || /\% Permission denied/;
503 171 ▶ if (/^\% (\S+) resource:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\% (\S+) resource:/) 2 169 171
504 2 my $srv = $1;
505 2 ▶ close $sock and return () if $srv !~ /lacnic|brazil/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $srv =~ /lacnic|brazil/i 0 2 2
Condition20%1/5
$srv =~ /lacnic|brazil/i || close $sock && (return ())
l!l&&r!l&&!r
200
close $sock and return ()
!ll
00
MC/DC0%0/2
$srv =~ /lacnic|brazil/i || close $sock && (return ())
$srv =~ /lacnic|brazil/i close $sock
Truth table33%1/3
A || B
A= $srv =~ /lacnic|brazil/iB= close $sock
ABresult
1X1
011
000
506 }
507 171 ▶ next if ( /^\%/ || !/\:/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\%/ or not /\:/ 61 110 171
Condition100%3/3
/^\%/ or not /\:/
l!l&&r!l&&!r
3823110
MC/DC100%2/2
/^\%/ or not /\:/
/^\%/ not /\:/
Truth table100%3/3
A or not(B)
A= /^\%/B= not /\:/
ABresult
1X1
011
000
508 110 s/\s+$//;
509 110 my ( $field, $value ) = split( /:/, $_, 2 );
510 110 $value =~ s/^\s+//;
511 110 ▶ next if $field eq 'country' && $query{country};
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "country" and $query{"country"} 4 106 110
Condition100%3/3
$field eq "country" and $query{"country"}
!ll&&!rl&&r
10424
MC/DC100%2/2
$field eq "country" and $query{"country"}
$field eq "country" $query{"country"}
Truth table100%3/3
A and B
A= $field eq "country"B= $query{"country"}
ABresult
0X0
100
111
512 106 ▶ $query{ lc($field) } .= ( $query{ lc($field) } ? ' ' : '' ) . $value;
Branch: 1 branch
BranchTrueFalseTotal
$query{lc $field} ? : 47 59 106
513 }
514 3 close $sock;
515 3 return %query;
516 }
517  
518 sub lacnic_process_query (%) {
519 3 ▶ my %query = @_;
Subroutine lacnic_process_query: called CC=7 SCAR=21.5
Pod undocumented
520  
521 3 $query{permission} = 'allowed';
522 3 $query{descr} = $query{owner};
523 3 $query{netname} = $query{ownerid};
524 3 $query{source} = 'LACNIC';
525 3 ▶ if ( $query{inetnum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query{"inetnum"}) 3 0 3
526 3 $query{cidr} = $query{inetnum};
527 3 $query{inetnum} = ( Net::CIDR::cidr2range( $query{cidr} ) )[0];
528 }
529 3 ▶ unless ( $query{country} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query{"country"}) 1 2 3
530 1 ▶ if ( $query{nserver} && $query{nserver} =~ /\.(\w\w)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($query{'nserver'} and $query{'nserver'} =~ /\.(\w\w)$/) { } 0 1 1
elsif ($query{'descr'} and $query{'descr'} =~ /\s(\w\w)$/) { } 0 1 1
Condition33%1/3
$query{"nserver"} and $query{"nserver"} =~ /\.(\w\w)$/
!ll&&!rl&&r
010
MC/DC0%0/2
$query{"nserver"} and $query{"nserver"} =~ /\.(\w\w)$/
$query{"nserver"} $query{"nserver"} =~ /\.(\w\w)$/
Truth table33%1/3
A and B
A= $query{"nserver"}B= $query{"nserver"} =~ /\.(\w\w)$/
ABresult
0X0
100
111
531 0 $query{country} = uc $1;
532 }
533 ▶ elsif ( $query{descr} && $query{descr} =~ /\s(\w\w)$/ ) {
Condition33%1/3
$query{"descr"} and $query{"descr"} =~ /\s(\w\w)$/
!ll&&!rl&&r
010
MC/DC0%0/2
$query{"descr"} and $query{"descr"} =~ /\s(\w\w)$/
$query{"descr"} $query{"descr"} =~ /\s(\w\w)$/
Truth table33%1/3
A and B
A= $query{"descr"}B= $query{"descr"} =~ /\s(\w\w)$/
ABresult
0X0
100
111
534 0 $query{country} = uc $1;
535 }
536 else {
537 1 return ();
538 }
539 }
540 2 return %query;
541 }
542  
543 sub lacnic_query ($$) {
544 3 ▶ my ( $sock, $ip ) = @_;
Subroutine lacnic_query: called CC=1 SCAR=0.0
Pod undocumented
545  
546 3 my %query = lacnic_read_query( $sock, $ip );
547  
548 3 return lacnic_process_query(%query);
549 }
550  
551 10 *afrinic_read_query = *apnic_read_query;
552  
553 sub afrinic_process_query (%) {
554 2 ▶ my %query = @_;
Subroutine afrinic_process_query: called CC=8 SCAR=27.7
Pod undocumented
555  
556 return ()
557 if defined $query{remarks} && $query{remarks} =~ /country is really worldwide/
558 2 ▶ or defined $query{descr} && $query{descr} =~ /Here for in-addr\.arpa authentication/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $query{"remarks"} and $query{"remarks"} =~ /country is really worldwide/ or defined $query{"descr"} and $query{"descr"} =~ /Here for in-addr\.arpa authentication/ 0 2 2
Condition33%3/9
defined $query{"remarks"} and $query{"remarks"} =~ /country is really worldwide/ or defined $query{"descr"} and $query{"descr"} =~ /Here for in-addr\.arpa authentication/
l!l&&r!l&&!r
002
defined $query{"remarks"} and $query{"remarks"} =~ /country is really worldwide/
!ll&&!rl&&r
200
defined $query{"descr"} and $query{"descr"} =~ /Here for in-addr\.arpa authentication/
!ll&&!rl&&r
020
MC/DC0%0/4
defined $query{"remarks"} and $query{"remarks"} =~ /country is really worldwide/ or defined $query{"descr"} and $query{"descr"} =~ /Here for in-addr\.arpa authentication/
defined $query{"remarks"} $query{"remarks"} =~ /country is really worldwide/ defined $query{"descr"} $query{"descr"} =~ /Here for in-addr\.arpa authentication/
Truth table14%1/7
A and B or C and D
A= defined $query{"remarks"}B= $query{"remarks"} =~ /country is really worldwide/C= defined $query{"descr"}D= $query{"descr"} =~ /Here for in-addr\.arpa authentication/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
559  
560 2 ▶ if ( !$query{inet6num} && !$query{inetnum} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query{"inet6num"} or $query{"inetnum"}) 0 2 2
Condition33%1/3
$query{"inet6num"} or $query{"inetnum"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$query{"inet6num"} or $query{"inetnum"}
$query{"inet6num"} $query{"inetnum"}
Truth table33%1/3
A or B
A= $query{"inet6num"}B= $query{"inetnum"}
ABresult
1X1
011
000
561 0 return ();
562 }
563  
564 2 $query{permission} = 'allowed';
565 $query{cidr} =
566 2 ▶ [ Net::CIDR::range2cidr( uc( $query{inet6num} || $query{inetnum} ) ) ];
Condition33%1/3
$query{'inet6num'} || $query{'inetnum'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$query{'inet6num'} || $query{'inetnum'}
$query{'inet6num'} $query{'inetnum'}
Truth table33%1/3
A || B
A= $query{'inet6num'}B= $query{'inetnum'}
ABresult
1X1
011
000
567 2 return %query;
568 }
569  
570 sub afrinic_query ($$) {
571 2 ▶ my ( $sock, $ip ) = @_;
Subroutine afrinic_query: called CC=1 SCAR=0.0
Pod undocumented
572  
573 2 my %query = afrinic_read_query( $sock, $ip );
574  
575 2 return afrinic_process_query(%query);
576 }
577  
578 sub is_mine ($$;@) {
579 4 ▶ my ( $self, $ip, @cidr ) = @_;
Subroutine is_mine: called CC=4 SCAR=13.9
Pod documented
580  
581 4 ▶ return 0 unless is_valid_ip($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_valid_ip($ip) 0 4 4
582 4 ▶ if ( !scalar @cidr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @cidr) 2 2 4
583 2 my $out = $self->cidr();
584 2 ▶ @cidr = @$out if ref $out;
Branch: 1 branch
BranchTrueFalseTotal
if ref $out 2 0 2
585 }
586  
587 @cidr = map {
588 4 my @dots = ( split /\./ );
589 4 my $pad = '.0' x ( 4 - @dots );
590 4 s|(/.*)|$pad$1|;
591 4 $_;
592 }
593 4 map { split(/\s+/) }
594 4 grep { defined $_ } @cidr;
595  
596 4 return Net::CIDR::cidrlookup( $ip, @cidr );
597 }
598  
599 10 1;
600  
601 __END__
602  
603 =pod
604  
605 =encoding UTF-8
606  
607 =head1 NAME
608  
609 Net::Whois::IANA - Net::Whois::IANA - A universal WHOIS data extractor.
610  
611 =head1 VERSION
612  
613 version 0.48
614  
615 =head1 SYNOPSIS
616  
617 use Net::Whois::IANA;
618 my $ip = '132.66.16.2';
619 my $iana = Net::Whois::IANA->new;
620 $iana->whois_query(-ip=>$ip);
621 print "Country: " , $iana->country() , "\n";
622 print "Netname: " , $iana->netname() , "\n";
623 print "Descr: " , $iana->descr() , "\n";
624 print "Status: " , $iana->status() , "\n";
625 print "Source: " , $iana->source() , "\n";
626 print "Server: " , $iana->server() , "\n";
627 print "Inetnum: " , $iana->inetnum() , "\n";
628 print "CIDR: " , join(",", $iana->cidr()) , "\n";
629  
630 =head1 DESCRIPTION
631  
632 Various Net::Whois and IP:: modules have been created.
633 This is just something I had to write because none of them s
634 uited my purpose. It is conceptually based on Net::Whois::IP
635 by Ben Schmitz <bschmitz@orbitz.com>, but differs from it by
636 a few points:
637  
638 =over
639  
640 =item It is object-oriented.
641  
642 =item It has a few immediate methods for representing some whois fields.
643  
644 =item It allows the user to specify explicitly which whois servers
645 to query, and those servers might even not be of the four main
646 registries mentioned above.
647  
648 =item It has more robust error handling.
649  
650 =back
651  
652 Net::Whois::IANA was designed to provide a mechanism to lookup
653 whois information and store most descriptive part of it (descr,
654 netname and country fields) in the object. This mechanism is
655 supposed to be attached to a log parser (for example an Apache
656 web server log) to provide various accounting and statistics
657 information.
658  
659 The query is performed in a roundrobin system over all four
660 registries until a valid entry is found. The valid entry stops
661 the main query loop and the object with information is returned.
662 Unfortunately, the output formats of each one of the registries
663 is not completely the same and sometimes even unsimilar but
664 some common ground was always found and the assignment of the
665 information into the query object is based upon this common
666 ground, whatever misleading it might be.
667  
668 The query to the RIPE and APNIC registries are always performed
669 with a '-r' flag to avoid blocking of the querying IP. Thus, the
670 contact info for the given entry is not obtainable with this
671 module. The query to the ARIN registry is performed with a '+'
672 flag to force the colon-separated output of the information.
673  
674 =head1 NAME
675  
676 Net::Whois::IANA - A universal WHOIS data extractor.
677  
678 =head1 ABSTRACT
679  
680 This is a simple module to extract the descriptive whois
681 information about various IPs as they are stored in the four
682 regional whois registries of IANA - RIPE (Europe, Middle East)
683 APNIC (Asia/Pacific), ARIN (North America), AFRINIC (Africa)
684 and LACNIC (Latin American & Caribbean).
685  
686 It is designed to serve statistical harvesters of various
687 access logs and likewise, therefore it only collects partial
688 and [rarely] unprecise information.
689  
690 =head1 METHODS
691  
692 For the convenience of the user, basic list of IANA servers
693 (@IANA) and their mapping to host names and ports (%IANA) are
694 being exported.
695  
696 Also the following methods are being exported:
697  
698 =head2 $iana->whois_query
699  
700 Perform the query on the ip specified by $ip. You can limit
701 the lookup to a single server (of the IANA list) by specifying
702 '-whois=>$whois' pair or you can provide a set of your own
703 servers by specifying the '-mywhois=>\%mywhois' pair. The latter
704 one overrides all of the IANA list for lookup. You can also set
705 -debug option in order to trigger some verbosity in the output.
706  
707 $iana->whois_query(-ip=>$ip,-whois=>$whois|-mywhois=>\%mywhois)
708  
709 =head2 $iana->descr()
710  
711 Returns some of the "descr:" field contents of the queried IP.
712  
713 =head2 $iana->netname()
714  
715 Returns the "netname:" field contents of the queried IP.
716  
717 =head2 $iana->country()
718  
719 Returns "country:" field contents of the queried IP. Useful
720 to combine with the Geography::Countries module.
721  
722 =head2 $iana->inetnum()
723  
724 Returns the IP range of the queried IP. Often it is contained
725 within the inetnum field, but it is calculated for LACNIC.
726  
727 =head2 $iana->status()
728  
729 Returns the "status:" field contents of the queried IP.
730  
731 =head2 $iana->source()
732  
733 Returns the "source:" field contents of the queried IP.
734  
735 =head2 $iana->server()
736  
737 Returns the server that returned most valuable ntents of
738 the queried IP.
739  
740 =head2 $iana->cidr()
741  
742 Returns an array in CIDR notation (1.2.3.4/5) of the IP's registered range.
743  
744 =head2 $iana->fullinfo()
745  
746 Returns the complete output of the query.
747  
748 =head2 $iana->is_mine($ip,@cidrrange)
749  
750 Checks if the ip is within one of the CIDR ranges given by
751 @cidrrange. Returns 0 if none, 1 if a range matches.
752  
753 =head2 $iana->abuse()
754  
755 Yields the best guess for the potential abuse report email address
756 candidate. This is not a very reliable thing, but sometimes it proves
757 useful.
758  
759 =head1 BUGS
760  
761 As stated many times before, this module is not completely
762 homogeneous and precise because of the differences between
763 outputs of the IANA servers and because of some inconsistencies
764 within each one of them. Its primary target is to collect info
765 for general, shallow statistical purposes. The is_mine() method
766 might be optimized.
767  
768 =head1 CAVEATS
769  
770 The introduction of AFRINIC server may create some confusion
771 among servers. It might be that some entries are existant either in
772 both ARIN and AFRINIC or in both RIPE and AFRINIC, and some do not
773 exist at all. Moreover, there is a border confusion between Middle
774 East and Africa, thus, some Egypt sites appear under RIPE and some
775 under AFRINIC. LACNIC server arbitrarily imposes query rate temporary
776 block. ARIN "subconciously" redirects the client to appropriate
777 server sometimes. This redirection is not reflected yet by the package.
778  
779 =head1 SEE ALSO
780  
781 =over
782  
783 =item Net::Whois::IP
784  
785 =item Net::Whois::RIPE
786  
787 =item IP::Country
788  
789 =item Geography::Countries
790  
791 =item Net::CIDR
792  
793 =item NetAddr::IP
794  
795 =back
796  
797 =head1 AUTHOR
798  
799 Roman M. Parparov <roman@parparov.com>, Nicolas R <atoomic@cpan.org>
800  
801 =head1 COPYRIGHT AND LICENSE
802  
803 This software is copyright (c) 2003-2013, 2018 by Bolet Consulting <bolet@parparov.com>.
804  
805 This is free software; you can redistribute it and/or modify it under
806 the same terms as the Perl 5 programming language system itself.
807  
808 =cut
↑ Summary