DNS/Question.pm

filter: Statementstmt 100% 104 / 104 Branchbran 100% 40 / 40 Conditioncond 100% 17 / 17 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 23 / 23 Podpod 100% 14 / 14 totaltotal 100% 207 / 207 CC 33 SCAR 20.7
CC
33
Cov
100%
new
15.0
_dns_addr
13.0
decode
3.0
CRAP 7.9
« DNS/ZoneFile.pm ↑ Summary DNS/Packet.pm »
1 package Net::DNS::Question;
2  
3 95 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 95 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 95 our $VERSION = (qw$Id: Question.pm 2002 2025-01-07 09:57:46Z willem $)[2];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::Question - DNS question record
12  
13 =head1 SYNOPSIS
14  
15 use Net::DNS::Question;
16  
17 $question = Net::DNS::Question->new('example.com', 'AAAA', 'IN');
18  
19 =head1 DESCRIPTION
20  
21 A Net::DNS::Question object represents a record in the question
22 section of a DNS packet.
23  
24 =cut
25  
26  
27 95 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 95 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 95 ▶use Net::DNS::Parameters qw(%classbyname %typebyname :class :type);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 95 ▶use Net::DNS::Domain;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 95 ▶use Net::DNS::DomainName;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 =head1 METHODS
36  
37 =head2 new
38  
39 $question = Net::DNS::Question->new('example.com', 'AAAA', 'IN');
40 $question = Net::DNS::Question->new('example.com', 'A', 'IN');
41 $question = Net::DNS::Question->new('example.com');
42  
43 $question = Net::DNS::Question->new('2001::DB8::dead:beef', 'PTR', 'IN');
44 $question = Net::DNS::Question->new('2001::DB8::dead:beef');
45  
46 Creates a question object from the domain, type, and class passed as
47 arguments. One or both type and class arguments may be omitted and
48 will assume the default values shown above.
49  
50 RFC4291 and RFC4632 IP address/prefix notation is supported for
51 queries in both in-addr.arpa and ip6.arpa namespaces.
52  
53 =cut
54  
55 301 ▶sub new {
Subroutine new: called CC=15 SCAR=27.1
Pod documented
56 301 my $self = bless {}, shift;
57 301 my $qname = shift;
58 301 ▶ my $qtype = shift || '';
Condition100%2/2
shift() || ''
l!l
169132
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
59 301 ▶ my $qclass = shift || '';
Condition100%2/2
shift() || ''
l!l
72229
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
60  
61 # tolerate (possibly unknown) type and class in zone file order
62 301 ▶ unless ( exists $classbyname{$qclass} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Net::DNS::Question::classbyname{$qclass}) 238 63 301
63 238 ▶ ( $qtype, $qclass ) = ( $qclass, $qtype ) if exists $classbyname{$qtype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Net::DNS::Question::classbyname{$qtype} 7 231 238
64 238 ▶ ( $qtype, $qclass ) = ( $qclass, $qtype ) if $qtype =~ /CLASS/;
Branch: 1 branch
BranchTrueFalseTotal
if $qtype =~ /CLASS/ 3 235 238
65 }
66 301 ▶ unless ( exists $typebyname{$qtype} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Net::DNS::Question::typebyname{$qtype}) 142 159 301
67 142 ▶ ( $qtype, $qclass ) = ( $qclass, $qtype ) if exists $typebyname{$qclass};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Net::DNS::Question::typebyname{$qclass} 5 137 142
68 142 ▶ ( $qtype, $qclass ) = ( $qclass, $qtype ) if $qclass =~ /TYPE/;
Branch: 1 branch
BranchTrueFalseTotal
if $qclass =~ /TYPE/ 2 140 142
69 }
70  
71 # if argument is an IP address, do appropriate reverse lookup
72 301 ▶ if ( defined $qname and $qname =~ m/:|\d$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $qname and $qname =~ /:|\d$/) 84 217 301
Condition100%3/3
defined $qname and $qname =~ /:|\d$/
!ll&&!rl&&r
221584
MC/DC100%2/2
defined $qname and $qname =~ /:|\d$/
defined $qname $qname =~ /:|\d$/
Truth table100%3/3
A and B
A= defined $qnameB= $qname =~ /:|\d$/
ABresult
0X0
100
111
73 84 ▶ if ( my $reverse = _dns_addr($qname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reverse = _dns_addr($qname)) 82 2 84
74 82 $qname = $reverse;
75 82 ▶ $qtype ||= 'PTR';
Condition100%2/2
$qtype ||= "PTR"
l!l
478
MC/DC100%1/1
$qtype ||= "PTR"
$qtype
Truth table100%2/2
A
A= $qtype
Aresult
11
00
76 }
77 }
78  
79 301 $self->{qname} = Net::DNS::DomainName1035->new($qname);
80 299 ▶ $self->{qtype} = typebyname( $qtype || 'A' );
Condition100%2/2
$qtype || 'A'
l!l
24554
MC/DC100%1/1
$qtype || 'A'
$qtype
Truth table100%2/2
A
A= $qtype
Aresult
11
00
81 299 ▶ $self->{qclass} = classbyname( $qclass || 'IN' );
Condition100%2/2
$qclass || 'IN'
l!l
73226
MC/DC100%1/1
$qclass || 'IN'
$qclass
Truth table100%2/2
A
A= $qclass
Aresult
11
00
82  
83 299 return $self;
84 }
85  
86  
87 =head2 decode
88  
89 $question = Net::DNS::Question->decode(\$data, $offset);
90  
91 ($question, $offset) = Net::DNS::Question->decode(\$data, $offset);
92  
93 Decodes the question record at the specified location within a DNS
94 wire-format packet. The first argument is a reference to the buffer
95 containing the packet data. The second argument is the offset of
96 the start of the question record.
97  
98 Returns a Net::DNS::Question object and the offset of the next
99 location in the packet.
100  
101 An exception is raised if the object cannot be created
102 (e.g., corrupt or insufficient data).
103  
104 =cut
105  
106 95 ▶use constant QFIXEDSZ => length pack 'n2', (0) x 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 138 ▶sub decode {
Subroutine decode: called CC=3 SCAR=11.0
Pod documented
109 138 my ( $class, @argument ) = @_;
110 138 my ( $data, $offset ) = @argument;
111 138 my $self = bless {}, $class;
112  
113 138 ( $self->{qname}, $offset ) = Net::DNS::DomainName1035->decode(@argument);
114  
115 125 my $next = $offset + QFIXEDSZ;
116 125 ▶ die 'corrupt wire-format data' if length $$data < $next;
Branch: 1 branch
BranchTrueFalseTotal
if length $$data < $next 5 120 125
117 120 @{$self}{qw(qtype qclass)} = unpack "\@$offset n2", $$data;
118  
119 120 ▶ return wantarray ? ( $self, $next ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 102 18 120
120 }
121  
122  
123 =head2 encode
124  
125 $data = $question->encode( $offset, $hash );
126  
127 Returns the Net::DNS::Question in binary format suitable for
128 inclusion in a DNS packet buffer.
129  
130 The optional arguments are the offset within the packet data where
131 the Net::DNS::Question is to be stored and a reference to a hash
132 table used to index compressed names within the packet.
133  
134 =cut
135  
136 208 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
137 208 my ( $self, @opaque ) = @_;
138 208 return pack 'a* n2', $self->{qname}->encode(@opaque), @{$self}{qw(qtype qclass)};
139 }
140  
141  
142 =head2 string
143  
144 print "string = ", $question->string, "\n";
145  
146 Returns a string representation of the question record.
147  
148 =cut
149  
150 85 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
151 85 my $self = shift;
152 85 return join "\t", $self->{qname}->string, $self->qclass, $self->qtype;
153 }
154  
155  
156 =head2 print
157  
158 $object->print;
159  
160 Prints the record to the standard output. Calls the string() method
161 to get the string representation.
162  
163 =cut
164  
165 1 ▶sub print {
Subroutine print: called CC=1 SCAR=0.0
Pod documented
166 1 print &string, "\n";
167 1 return;
168 }
169  
170  
171 =head2 name
172  
173 $name = $question->name;
174  
175 Internationalised domain name corresponding to the qname attribute.
176  
177 Decoding non-ASCII domain names is computationally expensive and
178 undesirable for names which are likely to be used to construct
179 further queries.
180  
181 When required to communicate with humans, the 'proper' domain name
182 should be extracted from a query or reply packet.
183  
184 $query = Net::DNS::Packet->new( $example, 'SOA' );
185 $reply = $resolver->send($query) or die;
186 ($question) = $reply->question;
187 $name = $question->name;
188  
189 =cut
190  
191 2 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
192 2 my ( $self, @argument ) = @_;
193 2 for (@argument) { croak 'immutable object: argument invalid' }
194 1 return $self->{qname}->xname;
195 }
196  
197  
198 =head2 qname, zname
199  
200 $qname = $question->qname;
201 $zname = $question->zname;
202  
203 Fully qualified domain name in the form required for a query
204 transmitted to a nameserver. In dynamic update packets, this
205 attribute is known as zname() and refers to the zone name.
206  
207 =cut
208  
209 127 ▶sub qname {
Subroutine qname: called CC=2 SCAR=6.9
Pod documented
210 127 my ( $self, @argument ) = @_;
211 127 for (@argument) { croak 'immutable object: argument invalid' }
212 126 return $self->{qname}->name;
213 }
214  
215 3 ▶sub zname { return &qname; }
Subroutine zname: called CC=1 SCAR=0.0
Pod documented
216  
217  
218 =head2 qtype, ztype, type
219  
220 $qtype = $question->type;
221 $qtype = $question->qtype;
222 $ztype = $question->ztype;
223  
224 Returns the question type attribute. In dynamic update packets,
225 this attribute is known as ztype() and refers to the zone type.
226  
227 =cut
228  
229 95 ▶sub type {
Subroutine type: called CC=2 SCAR=6.9
Pod documented
230 95 my ( $self, @argument ) = @_;
231 95 for (@argument) { croak 'immutable object: argument invalid' }
232 94 return typebyval( $self->{qtype} );
233 }
234  
235 92 ▶sub qtype { return &type; }
Subroutine qtype: called CC=1 SCAR=0.0
Pod documented
236 2 ▶sub ztype { return &type; }
Subroutine ztype: called CC=1 SCAR=0.0
Pod documented
237  
238  
239 =head2 qclass, zclass, class
240  
241 $qclass = $question->class;
242 $qclass = $question->qclass;
243 $zclass = $question->zclass;
244  
245 Returns the question class attribute. In dynamic update packets,
246 this attribute is known as zclass() and refers to the zone class.
247  
248 =cut
249  
250 185 ▶sub class {
Subroutine class: called CC=2 SCAR=6.9
Pod documented
251 185 my ( $self, @argument ) = @_;
252 185 for (@argument) { croak 'immutable object: argument invalid' }
253 184 return classbyval( $self->{qclass} );
254 }
255  
256 87 ▶sub qclass { return &class; }
Subroutine qclass: called CC=1 SCAR=0.0
Pod documented
257 97 ▶sub zclass { return &class; }
Subroutine zclass: called CC=1 SCAR=0.0
Pod documented
258  
259  
260 ########################################
261  
262 85 ▶sub _dns_addr { ## Map IP address into reverse lookup namespace
Subroutine _dns_addr: called CC=13 SCAR=25.6
263 85 local $_ = shift;
264  
265 # IP address must contain address characters only
266 85 s/[%].+$//; # discard RFC4007 scopeid
267 85 ▶ return unless m#^[a-fA-F0-9:./]+$#;
Branch: 1 branch
BranchTrueFalseTotal
unless m[^[a-fA-F0-9:./]+$] 1 84 85
268  
269 84 my ( $address, $pfxlen ) = split m#/#;
270  
271 # map IPv4 address to in-addr.arpa space
272 84 ▶ if (m#^\d*[.\d]*\d(/\d+)?$#) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[^\d*[.\d]*\d(/\d+)?$]) 22 62 84
273 22 my @parse = split /\./, $address;
274 22 ▶ $pfxlen = scalar(@parse) << 3 unless $pfxlen;
Branch: 1 branch
BranchTrueFalseTotal
unless $pfxlen 13 9 22
275 22 ▶ my $last = $pfxlen > 24 ? 3 : ( $pfxlen - 1 ) >> 3;
Branch: 1 branch
BranchTrueFalseTotal
$pfxlen > 24 ? : 7 15 22
276 22 return join '.', reverse( ( @parse, (0) x 3 )[0 .. $last] ), 'in-addr.arpa.';
277 }
278  
279 # map IPv6 address to ip6.arpa space
280 62 ▶ return unless m#^[:\w]+:([.\w]*)(/\d+)?$#;
Branch: 1 branch
BranchTrueFalseTotal
unless m[^[:\w]+:([.\w]*)(/\d+)?$] 1 61 62
281 61 ▶ my $rhs = $1 || '0';
Condition100%2/2
$1 || '0'
l!l
547
MC/DC100%1/1
$1 || '0'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
282 61 ▶ return _dns_addr($rhs) if m#^[:0]*:0*:[fF]{4}:[^:]+$#; # IPv4
Branch: 1 branch
BranchTrueFalseTotal
if /^[:0]*:0*:[fF]{4}:[^:]+$/ 1 60 61
283 60 ▶ $rhs = sprintf '%x%0.2x:%x%0.2x', map { $_ || 0 } split( /\./, $rhs, 4 ) if /\./;
Branch: 1 branch
BranchTrueFalseTotal
if /\./ 1 59 60
Condition100%2/2
$_ || 0
l!l
31
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
284 60 $address =~ s/:[^:]*$/:0$rhs/;
285 60 my @parse = split /:/, ( reverse "0$address" ), 9;
286 60 ▶ my @xpand = map { /./ ? $_ : ('0') x ( 9 - @parse ) } @parse; # expand ::
Branch: 1 branch
BranchTrueFalseTotal
/./ ? : 340 28 368
287 60 ▶ $pfxlen = ( scalar(@xpand) << 4 ) unless $pfxlen; # implicit length if unspecified
Branch: 1 branch
BranchTrueFalseTotal
unless $pfxlen 45 15 60
288 60 ▶ my $len = $pfxlen > 124 ? 32 : ( $pfxlen + 3 ) >> 2;
Branch: 1 branch
BranchTrueFalseTotal
$pfxlen > 124 ? : 40 20 60
289 60 my $hex = pack 'A4' x 8, map { $_ . '000' } ('0') x ( 8 - @xpand ), @xpand;
290 60 return join '.', split( //, substr( $hex, -$len ) ), 'ip6.arpa.';
291 }
292  
293  
294 95 1;
295 __END__
« DNS/ZoneFile.pm ↑ Summary DNS/Packet.pm »