File: Header.pm

package info (click to toggle)
libnet-dns-perl 1.50-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,644 kB
  • sloc: perl: 18,185; makefile: 9
file content (513 lines) | stat: -rw-r--r-- 10,525 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package Net::DNS::Header;

use strict;
use warnings;

our $VERSION = (qw$Id: Header.pm 2002 2025-01-07 09:57:46Z willem $)[2];


=head1 NAME

Net::DNS::Header - DNS packet header

=head1 SYNOPSIS

	use Net::DNS;

	$packet = Net::DNS::Packet->new();
	$header = $packet->header;


=head1 DESCRIPTION

C<Net::DNS::Header> represents the header portion of a DNS packet.

=cut


use integer;
use Carp;

use Net::DNS::Parameters qw(:opcode :rcode);


=head1 METHODS


=head2 $packet->header

	$packet = Net::DNS::Packet->new();
	$header = $packet->header;

Net::DNS::Header objects emanate from the Net::DNS::Packet header()
method, and contain an opaque reference to the parent Packet object.

Header objects may be assigned to suitably scoped lexical variables.
They should never be stored in global variables or persistent data
structures.


=head2 string

	print $packet->header->string;

Returns a string representation of the packet header.

=cut

sub string {
	my $self = shift;

	my $id	   = $self->id;
	my $qr	   = $self->qr;
	my $opcode = $self->opcode;
	my $rcode  = $self->rcode;
	my $qd	   = $self->qdcount;
	my $an	   = $self->ancount;
	my $ns	   = $self->nscount;
	my $ar	   = $self->arcount;
	my $dispid = defined $id ? $id : 'undef';
	return <<"QQ" if $opcode eq 'DSO';
;;	id = $dispid	qr = $qr
;;	opcode = $opcode	rcode = $rcode
QQ
	return <<"QQ" if $opcode eq 'UPDATE';
;;	id = $dispid	qr = $qr
;;	opcode = $opcode	rcode = $rcode
;;	zocount = $qd	prcount = $an
;;	upcount = $ns	adcount = $ar
QQ
	my $aa = $self->aa;
	my $tc = $self->tc;
	my $rd = $self->rd;
	my $ra = $self->ra;
	my $zz = $self->z;
	my $ad = $self->ad;
	my $cd = $self->cd;
	my $do = $self->do;
	my $co = $self->co;
	return <<"QQ";
;;	id = $dispid
;;	qr = $qr	aa = $aa	tc = $tc	rd = $rd	opcode = $opcode
;;	ra = $ra	z  = $zz	ad = $ad	cd = $cd	rcode  = $rcode
;;	do = $do	co = $co
;;	qdcount = $qd	ancount = $an
;;	nscount = $ns	arcount = $ar
QQ
}


=head2 print

	$packet->header->print;

Prints the string representation of the packet header.

=cut

sub print {
	print &string;
	return;
}


=head2 id

	print "query id = ", $packet->header->id, "\n";
	$packet->header->id(1234);

Gets or sets the query identification number.

=cut

sub id {
	my ( $self, @value ) = @_;
	for (@value) { $$self->{id} = $_ }
	return $$self->{id};
}


=head2 opcode

	print "query opcode = ", $packet->header->opcode, "\n";
	$packet->header->opcode("UPDATE");

Gets or sets the query opcode (the purpose of the query).

=cut

sub opcode {
	my ( $self, $arg ) = @_;
	my $opcode;
	for ( $$self->{status} ) {
		return opcodebyval( ( $_ >> 11 ) & 0x0f ) unless defined $arg;
		$opcode = opcodebyname($arg);
		$_	= ( $_ & 0x87ff ) | ( $opcode << 11 );
	}
	return $opcode;
}


=head2 rcode

	print "query response code = ", $packet->header->rcode, "\n";
	$packet->header->rcode("SERVFAIL");

Gets or sets the query response code (the status of the query).

=cut

sub rcode {
	my ( $self, $arg ) = @_;
	my $rcode;
	for ( $$self->{status} ) {
		my $opt = $$self->edns;
		unless ( defined $arg ) {
			$rcode = ( $opt->rcode & 0xff0 ) | ( $_ & 0x00f );
			$opt->rcode($rcode);			# write back full 12-bit rcode
			return $rcode == 16 ? 'BADVERS' : rcodebyval($rcode);
		}
		$rcode = rcodebyname($arg);
		$opt->rcode($rcode);				# full 12-bit rcode
		$_ &= 0xfff0;					# low 4-bit rcode
		$_ |= ( $rcode & 0x000f );
	}
	return $rcode;
}


=head2 qr

	print "query response flag = ", $packet->header->qr, "\n";
	$packet->header->qr(0);

Gets or sets the query response flag.

=cut

sub qr {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x8000, @value );
}


=head2 aa

	print "response is ", $packet->header->aa ? "" : "non-", "authoritative\n";
	$packet->header->aa(0);

Gets or sets the authoritative answer flag.

=cut

sub aa {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x0400, @value );
}


=head2 tc

	print "packet is ", $packet->header->tc ? "" : "not ", "truncated\n";
	$packet->header->tc(0);

Gets or sets the truncated packet flag.

=cut

sub tc {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x0200, @value );
}


=head2 rd

	print "recursion was ", $packet->header->rd ? "" : "not ", "desired\n";
	$packet->header->rd(0);

Gets or sets the recursion desired flag.

=cut

sub rd {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x0100, @value );
}


=head2 ra

	print "recursion is ", $packet->header->ra ? "" : "not ", "available\n";
	$packet->header->ra(0);

Gets or sets the recursion available flag.

=cut

sub ra {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x0080, @value );
}


=head2 z

Unassigned bit, should always be zero.

=cut

sub z {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x0040, @value );
}


=head2 ad

	print "The response has ", $packet->header->ad ? "" : "not", "been verified\n";

Relevant in DNSSEC context.

(The AD bit is only set on a response where signatures have been
cryptographically verified or the server is authoritative for the data
and is allowed to set the bit by policy.)

=cut

sub ad {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x0020, @value );
}


=head2 cd

	print "checking was ", $packet->header->cd ? "not" : "", "desired\n";
	$packet->header->cd(0);

Gets or sets the checking disabled flag.

=cut

sub cd {
	my ( $self, @value ) = @_;
	return $self->_dnsflag( 0x0010, @value );
}


=head2 qdcount, zocount

	print "# of question records: ", $packet->header->qdcount, "\n";

Returns the number of records in the question section of the packet.
In dynamic update packets, this field is known as C<zocount> and refers
to the number of RRs in the zone section.

=cut

sub qdcount {
	my ( $self, @value ) = @_;
	for (@value) { $self->_warn('packet->header->qdcount is read-only') }
	return $$self->{count}[0] || scalar @{$$self->{question}};
}


=head2 ancount, prcount

	print "# of answer records: ", $packet->header->ancount, "\n";

Returns the number of records in the answer section of the packet
which may, in the case of corrupt packets, differ from the actual
number of records.
In dynamic update packets, this field is known as C<prcount> and refers
to the number of RRs in the prerequisite section.

=cut

sub ancount {
	my ( $self, @value ) = @_;
	for (@value) { $self->_warn('packet->header->ancount is read-only') }
	return $$self->{count}[1] || scalar @{$$self->{answer}};
}


=head2 nscount, upcount

	print "# of authority records: ", $packet->header->nscount, "\n";

Returns the number of records in the authority section of the packet
which may, in the case of corrupt packets, differ from the actual
number of records.
In dynamic update packets, this field is known as C<upcount> and refers
to the number of RRs in the update section.

=cut

sub nscount {
	my ( $self, @value ) = @_;
	for (@value) { $self->_warn('packet->header->nscount is read-only') }
	return $$self->{count}[2] || scalar @{$$self->{authority}};
}


=head2 arcount, adcount

	print "# of additional records: ", $packet->header->arcount, "\n";

Returns the number of records in the additional section of the packet
which may, in the case of corrupt packets, differ from the actual
number of records.
In dynamic update packets, this field is known as C<adcount>.

=cut

sub arcount {
	my ( $self, @value ) = @_;
	for (@value) { $self->_warn('packet->header->arcount is read-only') }
	return $$self->{count}[3] || scalar @{$$self->{additional}};
}

sub zocount { return &qdcount; }
sub prcount { return &ancount; }
sub upcount { return &nscount; }
sub adcount { return &arcount; }


=head1 EDNS Protocol Extensions

=head2 do, co

	print "DNSSEC_OK flag was ", $packet->header->do ? "not" : "", "set\n";
	$packet->header->do(1);

Gets or sets the named EDNS flag.

=cut

sub do {
	my ( $self, @value ) = @_;
	return $self->_ednsflag( 0x8000, @value );
}

sub co {
	my ( $self, @value ) = @_;
	return $self->_ednsflag( 0x4000, @value );
}


=head2 Extended rcode

EDNS extended rcodes are handled transparently by $packet->header->rcode().


=head2 UDP packet size

	$udp_max = $packet->edns->UDPsize;

EDNS offers a mechanism to advertise the maximum UDP packet size
which can be assembled by the local network stack.

=cut

sub size {				## historical
	my ( $self, @value ) = @_;
	return $$self->edns->UDPsize(@value);
}


=head2 edns

	$header  = $packet->header;
	$version = $header->edns->version;
	@options = $header->edns->options;
	$option  = $header->edns->option(n);
	$udp_max = $packet->edns->UDPsize;

Auxiliary function which provides access to the EDNS protocol
extension OPT RR.

=cut

sub edns {
	my $self = shift;
	return $$self->edns;
}


########################################

sub _dnsflag {
	my ( $self, $flag, @value ) = @_;
	for ( $$self->{status} ) {
		my $set = $_ | $flag;
		$_ = ( shift @value ) ? $set : ( $set ^ $flag ) if @value;
		$flag &= $_;
	}
	return $flag ? 1 : 0;
}


sub _ednsflag {
	my ( $self, $flag, @value ) = @_;
	my $edns = $$self->edns;
	for ( $edns->flags ) {
		my $set = $_ | $flag;
		$edns->flags( $_ = ( shift @value ) ? $set : ( $set ^ $flag ) ) if @value;
		$flag &= $_;
	}
	return $flag ? 1 : 0;
}


my %warned;

sub _warn {
	my ( undef, @note ) = @_;
	return carp "usage; @note" unless $warned{"@note"}++;
}


1;
__END__


########################################

=head1 COPYRIGHT

Copyright (c)1997 Michael Fuhr.

Portions Copyright (c)2002,2003 Chris Reinhardt.

Portions Copyright (c)2012,2022 Dick Franks.

All rights reserved.


=head1 LICENSE

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the original copyright notices appear in all copies and that both
copyright notice and this permission notice appear in supporting
documentation, and that the name of the author not be used in advertising
or publicity pertaining to distribution of the software without specific
prior written permission.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.


=head1 SEE ALSO

L<perl> L<Net::DNS> L<Net::DNS::Packet> L<Net::DNS::RR::OPT>
L<RFC1035(4.1.1)|https://iana.org/go/rfc1035#section-4.1.1>

=cut