blib/lib/Net/Nslookup.pm

filter: Statementstmt 84.6% 55 / 65 Branchbran 63.1% 24 / 38 Conditioncond 63.3% 19 / 30 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 3 totaltotal 68.7% 108 / 157 CC 30 SCAR 34.4
CC
30
Cov
74%
nslookup
42.9
ns
6.5
__ANON__
1.1
CRAP 31.2
↑ Summary
1 package Net::Nslookup;
2  
3 # -------------------------------------------------------------------
4 # Net::Nslookup - Provide nslookup(1)-like capabilities
5 # Copyright (C) 2002-2013 darren chamberlain <darren@cpan.org>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; version 2.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # 02111-1307 USA
20 # -------------------------------------------------------------------
21  
22 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use vars qw($VERSION $DEBUG @EXPORT $TIMEOUT $WIN32);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 $VERSION = "2.04";
27 6 @EXPORT = qw(nslookup);
28 6 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Net::Nslookup::DEBUG 6 0 6
29 6 ▶$TIMEOUT = 15 unless defined $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Net::Nslookup::TIMEOUT 6 0 6
30 6 $WIN32 = $^O =~ /win32/i;
31  
32 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 6 my %_methods = qw(
35 A address
36 CNAME cname
37 MX exchange
38 NS nsdname
39 PTR ptrdname
40 TXT rdatastr
41 SOA dummy
42 SRV target
43 );
44  
45 # ----------------------------------------------------------------------
46 # nslookup(%args)
47 #
48 # Does the actual lookup, deferring to helper functions as necessary.
49 # ----------------------------------------------------------------------
50 12 ▶sub nslookup {
Subroutine nslookup: called CC=26 SCAR=37.6
Pod undocumented
51 12 ▶ my $options = isa($_[0], 'HASH') ? shift : @_ % 2 ? { 'host', @_ } : { @_ };
Branch: 2 branches
BranchTrueFalseTotal
Net::Nslookup::isa($_[0], 'HASH') ? : 0 12 12
@_ % 2 ? : 1 11 12
52 12 my ($term, $type, @answers);
53  
54 # Some reasonable defaults.
55 $term = lc ($options->{'term'} ||
56 $options->{'host'} ||
57 12 ▶ $options->{'domain'} || return);
Condition62%5/8
$options->{'term'} || $options->{'host'} || $options->{'domain'} || (return)
l!l
120
$options->{"term"} or $options->{"host"} or $options->{"domain"}
l!l&&r!l&&!r
840
$options->{"term"} or $options->{"host"}
l!l&&r!l&&!r
084
MC/DC0%0/3
$options->{'term'} || $options->{'host'} || $options->{'domain'} || (return)
$options->{"term"} $options->{"host"} $options->{"domain"}
Truth table50%2/4
A or B or C
A= $options->{"term"}B= $options->{"host"}C= $options->{"domain"}
ABCresult
1XX1
01X1
0011
0000
58 $type = uc ($options->{'type'} ||
59 12 ▶ $options->{'qtype'} || "A");
Condition80%4/5
$options->{'type'} || $options->{'qtype'} || 'A'
l!l
111
$options->{"type"} or $options->{"qtype"}
l!l&&r!l&&!r
1101
MC/DC50%1/2
$options->{'type'} || $options->{'qtype'} || 'A'
$options->{"type"} $options->{"qtype"}
Truth table66%2/3
A or B
A= $options->{"type"}B= $options->{"qtype"}
ABresult
1X1
011
000
60 12 ▶ $options->{'server'} ||= '';
Condition50%1/2
$options->{"server"} ||= ""
l!l
012
MC/DC0%0/1
$options->{"server"} ||= ""
$options->{"server"}
Truth table50%1/2
A
A= $options->{"server"}
Aresult
11
00
61 12 ▶ $options->{'recurse'} ||= 0;
Condition100%2/2
$options->{"recurse"} ||= 0
l!l
210
MC/DC100%1/1
$options->{"recurse"} ||= 0
$options->{"recurse"}
Truth table100%2/2
A
A= $options->{"recurse"}
Aresult
11
00
62  
63 $options->{'timeout'} = $TIMEOUT
64 12 ▶ unless defined $options->{'timeout'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"timeout"} 12 0 12
65  
66 $options->{'debug'} = $DEBUG
67 12 ▶ unless defined $options->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"debug"} 9 3 12
68  
69 12 eval {
70 0 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
71 12 ▶ alarm $options->{'timeout'} unless $WIN32;
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::Nslookup::WIN32 12 0 12
72  
73 12 ▶ my $meth = $_methods{ $type } || die "Unknown type '$type'";
Condition50%1/2
$_methods{$type} || die("Unknown type '${type}'")
l!l
120
MC/DC0%0/1
$_methods{$type} || die("Unknown type '${type}'")
$_methods{$type}
Truth table50%1/2
A
A= $_methods{$type}
Aresult
11
00
74 12 my $res = ns($options->{'server'});
75  
76 12 ▶ if ($options->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"debug"}) 0 12 12
77 0 warn "Performing `$type' lookup on `$term'\n";
78 }
79  
80 12 ▶ if (my $q = $res->search($term, $type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $q = $res->search($term, $type)) 8 4 12
81 8 ▶ if ('SOA' eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ('SOA' eq $type) { } 0 8 8
82 0 my $a = ($q->answer)[0];
83 0 @answers = (join " ", map { $a->$_ }
84 qw(mname rname serial refresh retry expire minimum));
85 }
86 else {
87 8 @answers = map { $_->$meth() } grep { $_->type eq $type } $q->answer;
88 }
89  
90 # If recurse option is set, for NS, MX, and CNAME requests,
91 # do an A lookup on the result. False by default.
92 8 ▶ if ($options->{'recurse'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"recurse"} and "NS" eq $type || "MX" eq $type || "CNAME" eq $type) 2 6 8
Condition55%5/9
$options->{"recurse"} and "NS" eq $type || "MX" eq $type || "CNAME" eq $type
!ll&&!rl&&r
602
"NS" eq $type or "MX" eq $type or "CNAME" eq $type
l!l&&r!l&&!r
200
"NS" eq $type or "MX" eq $type
l!l&&r!l&&!r
110
MC/DC25%1/4
$options->{"recurse"} and "NS" eq $type || "MX" eq $type || "CNAME" eq $type
$options->{"recurse"} "NS" eq $type "MX" eq $type "CNAME" eq $type
Truth table60%3/5
A and B or C or D
A= $options->{"recurse"}B= "NS" eq $typeC= "MX" eq $typeD= "CNAME" eq $type
ABCDresult
0XXX0
10000
11XX1
101X1
10011
93 (('NS' eq $type) ||
94 ('MX' eq $type) ||
95 ('CNAME' eq $type)
96 )) {
97  
98 @answers = map {
99 2 nslookup(
100 host => $_,
101 type => "A",
102 server => $options->{'server'},
103 3 debug => $options->{'debug'}
104 );
105 } @answers;
106 }
107 }
108  
109 12 ▶ alarm 0 unless $WIN32;
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::Nslookup::WIN32 12 0 12
110 };
111  
112 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
113 0 ▶ die "nslookup error: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless $@ eq "alarm\n" 0 0 0
114 unless $@ eq "alarm\n";
115 0 warn qq{Timeout: nslookup("type" => "$type", "host" => "$term")};
116 }
117  
118 12 ▶ return $answers[0] if (@answers == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @answers == 1 2 10 12
119 10 ▶ return (wantarray) ? @answers : $answers[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 3 10
120 }
121  
122 {
123 6 my %res;
124 12 ▶ sub ns {
Subroutine ns: called CC=5 SCAR=18.7
Pod undocumented
125 12 ▶ my $server = shift || "";
Condition50%1/2
shift() || ''
l!l
012
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
126  
127 12 ▶ unless (defined $res{$server}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res{$server}) 6 6 12
128 6 require Net::DNS;
129 6 import Net::DNS;
130 6 $res{$server} = Net::DNS::Resolver->new;
131  
132 # $server might be empty
133 6 ▶ if ($server) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server) 0 6 6
134 0 ▶ if (ref($server) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $server eq 'ARRAY') { } 0 0 0
135 0 $res{$server}->nameservers(@$server);
136 }
137 else {
138 0 $res{$server}->nameservers($server);
139 }
140 }
141 }
142  
143 12 return $res{$server};
144 }
145 }
146  
147 12 ▶sub isa { &UNIVERSAL::isa }
Subroutine isa: called CC=1 SCAR=0.0
Pod undocumented
148  
149 6 1;
150 __END__
↑ Summary