| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | $TIMEOUT = 15 unless defined $TIMEOUT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)
$options->{"term"} or $options->{"host"} or $options->{"domain"}
$options->{"term"} or $options->{"host"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | $type = uc ($options->{'type'} || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 12 | ▶ | $options->{'qtype'} || "A"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
$options->{'type'} || $options->{'qtype'} || 'A'
$options->{"type"} or $options->{"qtype"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 12 | ▶ | $options->{'server'} ||= ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{"server"} ||= ""
MC/DC0%0/1
$options->{"server"} ||= ""
$options->{"server"}
Truth table50%1/2
A
A= $options->{"server"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 12 | ▶ | $options->{'recurse'} ||= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"recurse"} ||= 0
MC/DC100%1/1
$options->{"recurse"} ||= 0
$options->{"recurse"}
Truth table100%2/2
A
A= $options->{"recurse"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | $options->{'timeout'} = $TIMEOUT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 12 | ▶ | unless defined $options->{'timeout'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | $options->{'debug'} = $DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 12 | ▶ | unless defined $options->{'debug'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 12 | ▶ | my $meth = $_methods{ $type } || die "Unknown type '$type'"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_methods{$type} || die("Unknown type '${type}'")
MC/DC0%0/1
$_methods{$type} || die("Unknown type '${type}'")
$_methods{$type}
Truth table50%1/2
A
A= $_methods{$type}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 12 | my $res = ns($options->{'server'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 12 | ▶ | if ($options->{'debug'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | warn "Performing `$type' lookup on `$term'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 12 | ▶ | if (my $q = $res->search($term, $type)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 8 | ▶ | if ('SOA' eq $type) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition55%5/9
$options->{"recurse"} and "NS" eq $type || "MX" eq $type || "CNAME" eq $type
"NS" eq $type or "MX" eq $type or "CNAME" eq $type
"NS" eq $type or "MX" eq $type
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 12 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | die "nslookup error: $@" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 10 | ▶ | return (wantarray) ? @answers : $answers[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 12 | ▶ | unless (defined $res{$server}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if (ref($server) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||