DNS/Header.pm

filter: Statementstmt 100% 132 / 132 Branchbran 100% 26 / 26 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 34 / 34 Podpod 100% 26 / 26 totaltotal 100% 230 / 230 CC 27 SCAR 9.7
CC
27
Cov
100%
_dnsflag
5.0
_ednsflag
5.0
string
4.0
CRAP 2.6
« DNS/RR/LOC.pm ↑ Summary DNS/Mailbox.pm »
1 package Net::DNS::Header;
2  
3 94 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 94 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 94 our $VERSION = (qw$Id: Header.pm 2042 2025-12-24 10:23:11Z willem $)[2];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::Header - DNS packet header
12  
13 =head1 SYNOPSIS
14  
15 use Net::DNS;
16  
17 $packet = Net::DNS::Packet->new();
18 $header = $packet->header;
19  
20  
21 =head1 DESCRIPTION
22  
23 C<Net::DNS::Header> represents the header portion of a DNS packet.
24  
25 =cut
26  
27  
28 94 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 94 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 94 ▶use Net::DNS::Parameters qw(:opcode :rcode);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 =head1 METHODS
35  
36  
37 =head2 $packet->header
38  
39 $packet = Net::DNS::Packet->new();
40 $header = $packet->header;
41  
42 Net::DNS::Header objects emanate from the Net::DNS::Packet header()
43 method, and contain an opaque reference to the parent Packet object.
44  
45 Header objects may be assigned to suitably scoped lexical variables.
46 They should never be stored in global variables or persistent data
47 structures.
48  
49  
50 =head2 string
51  
52 print $packet->header->string;
53  
54 Returns a string representation of the packet header.
55  
56 =cut
57  
58 sub string {
59 31 ▶ my $self = shift;
Subroutine string: called CC=4 SCAR=13.9
Pod documented
60  
61 31 my $id = $self->id;
62 31 my $qr = $self->qr;
63 31 my $opcode = $self->opcode;
64 31 my $rcode = $self->rcode;
65 31 my $qd = $self->qdcount;
66 31 my $an = $self->ancount;
67 31 my $ns = $self->nscount;
68 31 my $ar = $self->arcount;
69 31 ▶ my $dispid = defined $id ? $id : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 21 10 31
70 31 ▶ return <<"QQ" if $opcode eq 'DSO';
Branch: 1 branch
BranchTrueFalseTotal
if $opcode eq "DSO" 2 29 31
71 ;; id = $dispid qr = $qr
72 ;; opcode = $opcode rcode = $rcode
73 QQ
74 29 ▶ return <<"QQ" if $opcode eq 'UPDATE';
Branch: 1 branch
BranchTrueFalseTotal
if $opcode eq "UPDATE" 9 20 29
75 ;; id = $dispid qr = $qr
76 ;; opcode = $opcode rcode = $rcode
77 ;; zocount = $qd prcount = $an
78 ;; upcount = $ns adcount = $ar
79 QQ
80 20 my $aa = $self->aa;
81 20 my $tc = $self->tc;
82 20 my $rd = $self->rd;
83 20 my $ra = $self->ra;
84 20 my $zz = $self->z;
85 20 my $ad = $self->ad;
86 20 my $cd = $self->cd;
87 20 my $do = $self->do;
88 20 my $co = $self->co;
89 20 my $de = $self->de;
90 20 return <<"QQ";
91 ;; id = $dispid
92 ;; qr = $qr aa = $aa tc = $tc rd = $rd opcode = $opcode
93 ;; ra = $ra z = $zz ad = $ad cd = $cd rcode = $rcode
94 ;; do = $do co = $co de = $de
95 ;; qdcount = $qd ancount = $an
96 ;; nscount = $ns arcount = $ar
97 QQ
98 }
99  
100  
101 =head2 print
102  
103 $packet->header->print;
104  
105 Prints the string representation of the packet header.
106  
107 =cut
108  
109 sub print {
110 1 ▶ print &string;
Subroutine print: called CC=1 SCAR=0.0
Pod documented
111 1 return;
112 }
113  
114  
115 =head2 id
116  
117 print "query id = ", $packet->header->id, "\n";
118 $packet->header->id(1234);
119  
120 Gets or sets the query identification number.
121  
122 =cut
123  
124 sub id {
125 324 ▶ my ( $self, @value ) = @_;
Subroutine id: called CC=2 SCAR=6.9
Pod documented
126 324 for (@value) { $$self->{id} = $_ }
127 324 return $$self->{id};
128 }
129  
130  
131 =head2 opcode
132  
133 print "query opcode = ", $packet->header->opcode, "\n";
134 $packet->header->opcode("UPDATE");
135  
136 Gets or sets the query opcode (the purpose of the query).
137  
138 =cut
139  
140 sub opcode {
141 121 ▶ my ( $self, $arg ) = @_;
Subroutine opcode: called CC=3 SCAR=11.0
Pod documented
142 121 my $opcode;
143 121 for ( $$self->{status} ) {
144 121 ▶ return opcodebyval( ( $_ >> 11 ) & 0x0f ) unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 63 58 121
145 58 $opcode = opcodebyname($arg);
146 58 $_ = ( $_ & 0x87ff ) | ( $opcode << 11 );
147 }
148 58 return $opcode;
149 }
150  
151  
152 =head2 rcode
153  
154 print "query response code = ", $packet->header->rcode, "\n";
155 $packet->header->rcode("SERVFAIL");
156  
157 Gets or sets the query response code (the status of the query).
158  
159 =cut
160  
161 sub rcode {
162 224 ▶ my ( $self, $arg ) = @_;
Subroutine rcode: called CC=4 SCAR=13.9
Pod documented
163 224 my $rcode;
164 224 for ( $$self->{status} ) {
165 224 my $opt = $$self->edns;
166 224 ▶ unless ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 210 14 224
167 210 $rcode = ( $opt->rcode & 0xff0 ) | ( $_ & 0x00f );
168 210 $opt->rcode($rcode); # write back full 12-bit rcode
169 210 ▶ return $rcode == 16 ? 'BADVERS' : rcodebyval($rcode);
Branch: 1 branch
BranchTrueFalseTotal
$rcode == 16 ? : 1 209 210
170 }
171 14 $rcode = rcodebyname($arg);
172 14 $opt->rcode($rcode); # full 12-bit rcode
173 14 $_ &= 0xfff0; # low 4-bit rcode
174 14 $_ |= ( $rcode & 0x000f );
175 }
176 14 return $rcode;
177 }
178  
179  
180 =head2 qr
181  
182 print "query response flag = ", $packet->header->qr, "\n";
183 $packet->header->qr(0);
184  
185 Gets or sets the query response flag.
186  
187 =cut
188  
189 sub qr {
190 212 ▶ my ( $self, @value ) = @_;
Subroutine qr: called CC=1 SCAR=0.0
Pod documented
191 212 return $self->_dnsflag( 0x8000, @value );
192 }
193  
194  
195 =head2 aa
196  
197 print "response is ", $packet->header->aa ? "" : "non-", "authoritative\n";
198 $packet->header->aa(0);
199  
200 Gets or sets the authoritative answer flag.
201  
202 =cut
203  
204 sub aa {
205 31 ▶ my ( $self, @value ) = @_;
Subroutine aa: called CC=1 SCAR=0.0
Pod documented
206 31 return $self->_dnsflag( 0x0400, @value );
207 }
208  
209  
210 =head2 tc
211  
212 print "packet is ", $packet->header->tc ? "" : "not ", "truncated\n";
213 $packet->header->tc(0);
214  
215 Gets or sets the truncated packet flag.
216  
217 =cut
218  
219 sub tc {
220 104 ▶ my ( $self, @value ) = @_;
Subroutine tc: called CC=1 SCAR=0.0
Pod documented
221 104 return $self->_dnsflag( 0x0200, @value );
222 }
223  
224  
225 =head2 rd
226  
227 print "recursion was ", $packet->header->rd ? "" : "not ", "desired\n";
228 $packet->header->rd(0);
229  
230 Gets or sets the recursion desired flag.
231  
232 =cut
233  
234 sub rd {
235 173 ▶ my ( $self, @value ) = @_;
Subroutine rd: called CC=1 SCAR=0.0
Pod documented
236 173 return $self->_dnsflag( 0x0100, @value );
237 }
238  
239  
240 =head2 ra
241  
242 print "recursion is ", $packet->header->ra ? "" : "not ", "available\n";
243 $packet->header->ra(0);
244  
245 Gets or sets the recursion available flag.
246  
247 =cut
248  
249 sub ra {
250 28 ▶ my ( $self, @value ) = @_;
Subroutine ra: called CC=1 SCAR=0.0
Pod documented
251 28 return $self->_dnsflag( 0x0080, @value );
252 }
253  
254  
255 =head2 z
256  
257 Unassigned bit, should always be zero.
258  
259 =cut
260  
261 sub z {
262 20 ▶ my ( $self, @value ) = @_;
Subroutine z: called CC=1 SCAR=0.0
Pod documented
263 20 return $self->_dnsflag( 0x0040, @value );
264 }
265  
266  
267 =head2 ad
268  
269 print "The response has ", $packet->header->ad ? "" : "not", "been verified\n";
270  
271 Relevant in DNSSEC context.
272  
273 (The AD bit is only set on a response where signatures have been
274 cryptographically verified or the server is authoritative for the data
275 and is allowed to set the bit by policy.)
276  
277 =cut
278  
279 sub ad {
280 113 ▶ my ( $self, @value ) = @_;
Subroutine ad: called CC=1 SCAR=0.0
Pod documented
281 113 return $self->_dnsflag( 0x0020, @value );
282 }
283  
284  
285 =head2 cd
286  
287 print "checking was ", $packet->header->cd ? "not" : "", "desired\n";
288 $packet->header->cd(0);
289  
290 Gets or sets the checking disabled flag.
291  
292 =cut
293  
294 sub cd {
295 125 ▶ my ( $self, @value ) = @_;
Subroutine cd: called CC=1 SCAR=0.0
Pod documented
296 125 return $self->_dnsflag( 0x0010, @value );
297 }
298  
299  
300 =head2 qdcount, zocount
301  
302 print "# of question records: ", $packet->header->qdcount, "\n";
303  
304 Returns the number of records in the question section of the packet.
305 In dynamic update packets, this field is known as C<zocount> and refers
306 to the number of RRs in the zone section.
307  
308 =cut
309  
310 sub qdcount {
311 38 ▶ my ( $self, @value ) = @_;
Subroutine qdcount: called CC=3 SCAR=11.0
Pod documented
312 38 for (@value) { $self->_warn('packet->header->qdcount is read-only') }
313 38 ▶ return $$self->{count}[0] || scalar @{$$self->{question}};
Condition100%2/2
$$self->{'count'}[0] || scalar @{$$self->{'question'};}
l!l
533
MC/DC100%1/1
$$self->{'count'}[0] || scalar @{$$self->{'question'};}
$$self->{'count'}[0]
Truth table100%2/2
A
A= $$self->{'count'}[0]
Aresult
11
00
314 }
315  
316  
317 =head2 ancount, prcount
318  
319 print "# of answer records: ", $packet->header->ancount, "\n";
320  
321 Returns the number of records in the answer section of the packet
322 which may, in the case of corrupt packets, differ from the actual
323 number of records.
324 In dynamic update packets, this field is known as C<prcount> and refers
325 to the number of RRs in the prerequisite section.
326  
327 =cut
328  
329 sub ancount {
330 65 ▶ my ( $self, @value ) = @_;
Subroutine ancount: called CC=3 SCAR=11.0
Pod documented
331 65 for (@value) { $self->_warn('packet->header->ancount is read-only') }
332 65 ▶ return $$self->{count}[1] || scalar @{$$self->{answer}};
Condition100%2/2
$$self->{'count'}[1] || scalar @{$$self->{'answer'};}
l!l
1154
MC/DC100%1/1
$$self->{'count'}[1] || scalar @{$$self->{'answer'};}
$$self->{'count'}[1]
Truth table100%2/2
A
A= $$self->{'count'}[1]
Aresult
11
00
333 }
334  
335  
336 =head2 nscount, upcount
337  
338 print "# of authority records: ", $packet->header->nscount, "\n";
339  
340 Returns the number of records in the authority section of the packet
341 which may, in the case of corrupt packets, differ from the actual
342 number of records.
343 In dynamic update packets, this field is known as C<upcount> and refers
344 to the number of RRs in the update section.
345  
346 =cut
347  
348 sub nscount {
349 51 ▶ my ( $self, @value ) = @_;
Subroutine nscount: called CC=3 SCAR=11.0
Pod documented
350 51 for (@value) { $self->_warn('packet->header->nscount is read-only') }
351 51 ▶ return $$self->{count}[2] || scalar @{$$self->{authority}};
Condition100%2/2
$$self->{'count'}[2] || scalar @{$$self->{'authority'};}
l!l
447
MC/DC100%1/1
$$self->{'count'}[2] || scalar @{$$self->{'authority'};}
$$self->{'count'}[2]
Truth table100%2/2
A
A= $$self->{'count'}[2]
Aresult
11
00
352 }
353  
354  
355 =head2 arcount, adcount
356  
357 print "# of additional records: ", $packet->header->arcount, "\n";
358  
359 Returns the number of records in the additional section of the packet
360 which may, in the case of corrupt packets, differ from the actual
361 number of records.
362 In dynamic update packets, this field is known as C<adcount>.
363  
364 =cut
365  
366 sub arcount {
367 51 ▶ my ( $self, @value ) = @_;
Subroutine arcount: called CC=3 SCAR=11.0
Pod documented
368 51 for (@value) { $self->_warn('packet->header->arcount is read-only') }
369 51 ▶ return $$self->{count}[3] || scalar @{$$self->{additional}};
Condition100%2/2
$$self->{'count'}[3] || scalar @{$$self->{'additional'};}
l!l
447
MC/DC100%1/1
$$self->{'count'}[3] || scalar @{$$self->{'additional'};}
$$self->{'count'}[3]
Truth table100%2/2
A
A= $$self->{'count'}[3]
Aresult
11
00
370 }
371  
372 1 ▶sub zocount { return &qdcount; }
Subroutine zocount: called CC=1 SCAR=0.0
Pod documented
373 1 ▶sub prcount { return &ancount; }
Subroutine prcount: called CC=1 SCAR=0.0
Pod documented
374 1 ▶sub upcount { return &nscount; }
Subroutine upcount: called CC=1 SCAR=0.0
Pod documented
375 3 ▶sub adcount { return &arcount; }
Subroutine adcount: called CC=1 SCAR=0.0
Pod documented
376  
377  
378 =head1 EDNS Protocol Extensions
379  
380 =head2 do, co, de
381  
382 print "DNSSEC_OK flag was ", $packet->header->do ? "not" : "", "set\n";
383 $packet->header->do(1);
384  
385 Gets or sets the named EDNS flag.
386  
387 =cut
388  
389 sub do {
390 114 ▶ my ( $self, @value ) = @_;
Subroutine do: called CC=1 SCAR=0.0
Pod documented
391 114 return $self->_ednsflag( 0x8000, @value );
392 }
393  
394 sub co {
395 28 ▶ my ( $self, @value ) = @_;
Subroutine co: called CC=1 SCAR=0.0
Pod documented
396 28 return $self->_ednsflag( 0x4000, @value );
397 }
398  
399 sub de {
400 20 ▶ my ( $self, @value ) = @_;
Subroutine de: called CC=1 SCAR=0.0
Pod documented
401 20 return $self->_ednsflag( 0x2000, @value );
402 }
403  
404  
405 =head2 Extended rcode
406  
407 EDNS extended rcodes are handled transparently by $packet->header->rcode().
408  
409  
410 =head2 UDP packet size
411  
412 $udp_max = $packet->edns->UDPsize;
413  
414 EDNS offers a mechanism to advertise the maximum UDP packet size
415 which can be assembled by the local network stack.
416  
417 =cut
418  
419 sub size { ## historical
420 1 ▶ my ( $self, @value ) = @_;
Subroutine size: called CC=1 SCAR=0.0
Pod documented
421 1 return $$self->edns->UDPsize(@value);
422 }
423  
424  
425 =head2 edns
426  
427 $header = $packet->header;
428 $version = $header->edns->version;
429 @options = $header->edns->options;
430 $option = $header->edns->option(n);
431 $udp_max = $packet->edns->UDPsize;
432  
433 Auxiliary function which provides access to the EDNS protocol
434 extension OPT RR.
435  
436 =cut
437  
438 sub edns {
439 1 ▶ my $self = shift;
Subroutine edns: called CC=1 SCAR=0.0
Pod documented
440 1 return $$self->edns;
441 }
442  
443  
444 ########################################
445  
446 sub _dnsflag {
447 806 ▶ my ( $self, $flag, @value ) = @_;
Subroutine _dnsflag: called CC=5 SCAR=16.1
448 806 for ( $$self->{status} ) {
449 806 my $set = $_ | $flag;
450 806 ▶ $_ = ( shift @value ) ? $set : ( $set ^ $flag ) if @value;
Branch: 2 branches
BranchTrueFalseTotal
if @value 399 407 806
shift @value ? : 89 310 399
451 806 $flag &= $_;
452 }
453 806 ▶ return $flag ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 216 590 806
454 }
455  
456  
457 sub _ednsflag {
458 162 ▶ my ( $self, $flag, @value ) = @_;
Subroutine _ednsflag: called CC=5 SCAR=16.1
459 162 my $edns = $$self->edns;
460 162 for ( $edns->flags ) {
461 162 my $set = $_ | $flag;
462 162 ▶ $edns->flags( $_ = ( shift @value ) ? $set : ( $set ^ $flag ) ) if @value;
Branch: 2 branches
BranchTrueFalseTotal
if @value 94 68 162
shift @value ? : 5 89 94
463 162 $flag &= $_;
464 }
465 162 ▶ return $flag ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 10 152 162
466 }
467  
468  
469 94 my %warned;
470  
471 sub _warn {
472 5 ▶ my ( undef, @note ) = @_;
Subroutine _warn: called CC=2 SCAR=6.9
473 5 ▶ return carp "usage; @note" unless $warned{"@note"}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $warned{join $", @note}++ 4 1 5
474 }
475  
476  
477 94 1;
478 __END__
« DNS/RR/LOC.pm ↑ Summary DNS/Mailbox.pm »