DNS/Packet.pm

filter: Statementstmt 100% 303 / 303 Branchbran 98.9% 93 / 94 Conditioncond 100% 21 / 21 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 100% 41 / 41 Podpod 100% 27 / 31 totaltotal 99.6% 495 / 501 CC 67 SCAR 19.6
CC
67
Cov
100%
truncate
17.0
decode
13.0
string
12.0
CRAP 7.1
« DNS/Question.pm ↑ Summary DNS/RR/TSIG.pm »
1 package Net::DNS::Packet;
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: Packet.pm 2003 2025-01-21 12:06:06Z willem $)[2];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::Packet - DNS protocol packet
12  
13 =head1 SYNOPSIS
14  
15 use Net::DNS::Packet;
16  
17 $query = Net::DNS::Packet->new( 'example.com', 'MX', 'IN' );
18  
19 $reply = $resolver->send( $query );
20  
21  
22 =head1 DESCRIPTION
23  
24 A Net::DNS::Packet object represents a DNS protocol packet.
25  
26 =cut
27  
28  
29 94 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 94 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 94 ▶use Net::DNS::Parameters qw(:dsotype);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 94 ▶use constant UDPSZ => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 BEGIN {
36 94 ▶ require Net::DNS::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 94 require Net::DNS::Question;
38 94 require Net::DNS::RR;
39 }
40  
41  
42 =head1 METHODS
43  
44 =head2 new
45  
46 $packet = Net::DNS::Packet->new( 'example.com' );
47 $packet = Net::DNS::Packet->new( 'example.com', 'MX', 'IN' );
48  
49 $packet = Net::DNS::Packet->new();
50  
51 If passed a domain, type, and class, new() creates a Net::DNS::Packet
52 object which is suitable for making a DNS query for the specified
53 information. The type and class may be omitted; they default to A
54 and IN.
55  
56 If called with an empty argument list, new() creates an empty packet.
57  
58 =cut
59  
60 sub new {
61 195 ▶ my ( $class, @arg ) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
62 195 ▶ return &decode if ref $arg[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg[0] 5 190 195
63  
64 190 my $self = bless {
65 status => 0,
66 question => [],
67 answer => [],
68 authority => [],
69 additional => [],
70 }, $class;
71  
72 190 ▶ $self->{question} = [Net::DNS::Question->new(@arg)] if scalar @arg;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @arg 167 23 190
73  
74 189 return $self;
75 }
76  
77  
78 =head2 decode
79  
80 $packet = Net::DNS::Packet->decode( \$data );
81 $packet = Net::DNS::Packet->decode( \$data, 1 ); # debug
82 $packet = Net::DNS::Packet->new( \$data ... );
83  
84 A new packet object is created by decoding the DNS packet data
85 contained in the scalar referenced by the first argument.
86 The optional second boolean argument enables debugging output.
87  
88 Returns undef if unable to create a packet object.
89  
90 Decoding errors, including data corruption and truncation, are
91 collected in the $@ ($EVAL_ERROR) variable.
92  
93  
94 ( $packet, $length ) = Net::DNS::Packet->decode( \$data );
95  
96 If called in array context, returns a packet object and the number
97 of octets successfully decoded.
98  
99 Note that the number of RRs in each section of the packet may differ
100 from the corresponding header value if the data has been truncated
101 or corrupted during transmission.
102  
103 =cut
104  
105 94 ▶use constant HEADER_LENGTH => length pack 'n6', (0) x 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 sub decode {
108 169 ▶ my $class = shift;
Subroutine decode: called CC=13 SCAR=25.6
Pod documented
109 169 my $data = shift;
110 169 ▶ my $debug = shift || 0;
Condition100%2/2
shift() || 0
l!l
2167
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
111  
112 169 my $offset = 0;
113 169 my $self;
114 169 eval {
115 169 local $SIG{__DIE__};
116 169 my $length = length $$data;
117 169 ▶ die 'corrupt wire-format data' if $length < HEADER_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 12 14 155 169
118  
119 # header section
120 155 my ( $id, $status, @count ) = unpack 'n6', $$data;
121 155 my ( $qd, $an, $ns, $ar ) = @count;
122  
123 155 $self = bless {
124 id => $id,
125 status => $status,
126 count => [@count],
127 question => [],
128 answer => [],
129 authority => [],
130 additional => [],
131 replysize => $length
132 }, $class;
133  
134 # question/zone section
135 155 my $hash = {};
136 155 my $record;
137 155 $offset = HEADER_LENGTH;
138 155 ▶ while ( $qd-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qd--) 118 138 256
139 118 ( $record, $offset ) = Net::DNS::Question->decode( $data, $offset, $hash );
140 101 CORE::push( @{$self->{question}}, $record );
141 }
142  
143 # RR sections
144 138 ▶ while ( $an-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($an--) 9205 138 9343
145 9205 ( $record, $offset ) = Net::DNS::RR->decode( $data, $offset, $hash );
146 9205 CORE::push( @{$self->{answer}}, $record );
147 }
148  
149 138 ▶ while ( $ns-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns--) 208 138 346
150 208 ( $record, $offset ) = Net::DNS::RR->decode( $data, $offset, $hash );
151 208 CORE::push( @{$self->{authority}}, $record );
152 }
153  
154 138 ▶ while ( $ar-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ar--) 462 138 600
155 462 ( $record, $offset ) = Net::DNS::RR->decode( $data, $offset, $hash );
156 462 CORE::push( @{$self->{additional}}, $record );
157 }
158  
159 138 ▶ return unless $offset == HEADER_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset == 12 133 5 138
160 5 ▶ return unless $self->header->opcode eq 'DSO';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->header->opcode eq "DSO" 4 1 5
161  
162 1 $self->{dso} = [];
163 1 my $limit = $length - 4;
164 1 ▶ while ( $offset < $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $limit) 1 1 2
165 1 my ( $t, $l, $v ) = unpack "\@$offset n2a*", $$data;
166 1 CORE::push( @{$self->{dso}}, [$t, substr( $v, 0, $l )] );
167 1 $offset += ( $l + 4 );
168 }
169 };
170  
171 169 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 2 167 169
172 2 local $@ = $@;
173 2 ▶ print $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
174 2 eval { $self->print };
175 }
176  
177 169 ▶ return wantarray ? ( $self, $offset ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 168 169
178 }
179  
180  
181 =head2 encode
182  
183 $data = $packet->encode;
184 $data = $packet->encode( $size );
185  
186 Returns the packet data in binary format, suitable for sending as a
187 query or update request to a nameserver.
188  
189 Truncation may be specified using a non-zero optional size argument.
190  
191 =cut
192  
193 sub data {
194 1 ▶ return &encode; # uncoverable pod
Subroutine data: called CC=1 SCAR=0.0
Pod undocumented
195 }
196  
197 sub encode {
198 199 ▶ my ( $self, $size ) = @_;
Subroutine encode: called CC=4 SCAR=13.9
Pod documented
199  
200 199 my $edns = $self->edns; # EDNS support
201 199 my @addl = grep { !$_->isa('Net::DNS::RR::OPT') } @{$self->{additional}};
202 199 ▶ $self->{additional} = [$edns, @addl] if $edns->_specified;
Branch: 1 branch
BranchTrueFalseTotal
if $edns->_specified 55 144 199
203  
204 199 ▶ return $self->truncate($size) if $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size 1 198 199
205  
206 198 my @part = qw(question answer authority additional);
207 198 my @size = map { scalar @{$self->{$_}} } @part;
208 198 my $data = pack 'n6', $self->_quid, $self->{status}, @size;
209 198 $self->{count} = [];
210  
211 198 my $hash = {}; # packet body
212 198 foreach my $component ( map { @{$self->{$_}} } @part ) {
213 916 $data .= $component->encode( length $data, $hash, $self );
214 }
215  
216 198 return $data;
217 }
218  
219  
220 =head2 header
221  
222 $header = $packet->header;
223  
224 Constructor method which returns a Net::DNS::Header object which
225 represents the header section of the packet.
226  
227 =cut
228  
229 sub header {
230 680 ▶ my $self = shift;
Subroutine header: called CC=1 SCAR=0.0
Pod documented
231 680 return bless \$self, q(Net::DNS::Header);
232 }
233  
234  
235 =head2 edns
236  
237 $version = $packet->edns->version;
238 $UDPsize = $packet->edns->size;
239  
240 Auxiliary function which provides access to the EDNS protocol
241 extension OPT RR.
242  
243 =cut
244  
245 sub edns {
246 701 ▶ my $self = shift;
Subroutine edns: called CC=3 SCAR=11.0
Pod documented
247 701 my $link = \$self->{xedns};
248 701 ▶ ($$link) = grep { $_->isa(qw(Net::DNS::RR::OPT)) } @{$self->{additional}} unless $$link;
Branch: 1 branch
BranchTrueFalseTotal
unless $$link 238 463 701
249 701 ▶ $$link = Net::DNS::RR->new( type => 'OPT' ) unless $$link;
Branch: 1 branch
BranchTrueFalseTotal
unless $$link 188 513 701
250 701 return $$link;
251 }
252  
253  
254 =head2 reply
255  
256 $reply = $query->reply( $UDPmax );
257  
258 Constructor method which returns a new reply packet.
259  
260 The optional UDPsize argument is the maximum UDP packet size which
261 can be reassembled by the local network stack, and is advertised in
262 response to an EDNS query.
263  
264 =cut
265  
266 sub reply {
267 7 ▶ my ( $query, @UDPmax ) = @_;
Subroutine reply: called CC=3 SCAR=11.0
Pod documented
268 7 my $qheadr = $query->header;
269 7 ▶ croak 'erroneous qr flag in query packet' if $qheadr->qr;
Branch: 1 branch
BranchTrueFalseTotal
if $qheadr->qr 1 6 7
270  
271 6 my $reply = Net::DNS::Packet->new();
272 6 my $header = $reply->header;
273 6 $header->qr(1); # reply with same id, opcode and question
274 6 $header->id( $qheadr->id );
275 6 $header->opcode( $qheadr->opcode );
276 6 my @question = $query->question;
277 6 $reply->{question} = [@question];
278  
279 6 $header->rcode('FORMERR'); # no RCODE considered sinful!
280  
281 6 $header->rd( $qheadr->rd ); # copy these flags into reply
282 6 $header->cd( $qheadr->cd );
283  
284 6 ▶ return $reply unless grep { $_->isa('Net::DNS::RR::OPT') } @{$query->{additional}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->isa("Net::DNS::RR::OPT");} @{$$query{"additional"};} 5 1 6
285  
286 1 my $edns = $reply->edns();
287 1 CORE::push( @{$reply->{additional}}, $edns );
288 1 $edns->udpsize(@UDPmax);
289 1 return $reply;
290 }
291  
292  
293 =head2 question, zone
294  
295 @question = $packet->question;
296  
297 Returns a list of Net::DNS::Question objects representing the
298 question section of the packet.
299  
300 In dynamic update packets, this section is known as zone() and
301 specifies the DNS zone to be updated.
302  
303 =cut
304  
305 sub question {
306 163 ▶ my @qr = @{shift->{question}};
Subroutine question: called CC=1 SCAR=0.0
Pod documented
307 163 return @qr;
308 }
309  
310 98 ▶sub zone { return &question }
Subroutine zone: called CC=1 SCAR=0.0
Pod documented
311  
312  
313 =head2 answer, pre, prerequisite
314  
315 @answer = $packet->answer;
316  
317 Returns a list of Net::DNS::RR objects representing the answer
318 section of the packet.
319  
320 In dynamic update packets, this section is known as pre() or
321 prerequisite() and specifies the RRs or RRsets which must or must
322 not preexist.
323  
324 =cut
325  
326 sub answer {
327 142 ▶ my @rr = @{shift->{answer}};
Subroutine answer: called CC=1 SCAR=0.0
Pod documented
328 142 return @rr;
329 }
330  
331 2 ▶sub pre { return &answer }
Subroutine pre: called CC=1 SCAR=0.0
Pod documented
332 1 ▶sub prerequisite { return &answer }
Subroutine prerequisite: called CC=1 SCAR=0.0
Pod documented
333  
334  
335 =head2 authority, update
336  
337 @authority = $packet->authority;
338  
339 Returns a list of Net::DNS::RR objects representing the authority
340 section of the packet.
341  
342 In dynamic update packets, this section is known as update() and
343 specifies the RRs or RRsets to be added or deleted.
344  
345 =cut
346  
347 sub authority {
348 77 ▶ my @rr = @{shift->{authority}};
Subroutine authority: called CC=1 SCAR=0.0
Pod documented
349 77 return @rr;
350 }
351  
352 1 ▶sub update { return &authority }
Subroutine update: called CC=1 SCAR=0.0
Pod documented
353  
354  
355 =head2 additional
356  
357 @additional = $packet->additional;
358  
359 Returns a list of Net::DNS::RR objects representing the additional
360 section of the packet.
361  
362 =cut
363  
364 sub additional {
365 180 ▶ my @rr = @{shift->{additional}};
Subroutine additional: called CC=1 SCAR=0.0
Pod documented
366 180 return @rr;
367 }
368  
369  
370 =head2 print
371  
372 $packet->print;
373  
374 Prints the entire packet to the currently selected output filehandle
375 using the master file format mandated by RFC1035.
376  
377 =cut
378  
379 sub print {
380 1 ▶ print &string;
Subroutine print: called CC=1 SCAR=0.0
Pod documented
381 1 return;
382 }
383  
384  
385 =head2 string
386  
387 print $packet->string;
388  
389 Returns a string representation of the packet.
390  
391 =cut
392  
393 sub string {
394 17 ▶ my $self = shift;
Subroutine string: called CC=12 SCAR=24.8
Pod documented
395  
396 17 my $header = $self->header;
397 17 my $opcode = $header->opcode;
398 17 ▶ my $packet = $header->qr ? 'Response' : 'Query';
Branch: 1 branch
BranchTrueFalseTotal
$header->qr ? : 1 16 17
399 17 my $server = $self->{replyfrom};
400 17 my $length = $self->{replysize};
401 17 ▶ my $origin = $server ? ";; $packet received from [$server] $length octets\n" : "";
Branch: 1 branch
BranchTrueFalseTotal
$server ? : 1 16 17
402 17 my @record = ( "$origin;; HEADER SECTION", $header->string );
403  
404 17 ▶ if ( $opcode eq 'DSO' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode eq "DSO") 1 16 17
405 1 CORE::push( @record, ";; DSO SECTION" );
406 1 foreach ( @{$self->{dso}} ) {
407 1 my ( $t, $v ) = @$_;
408 1 CORE::push( @record, sprintf( ";;\t%s\t%s", dsotypebyval($t), unpack( 'H*', $v ) ) );
409 }
410 1 return join "\n", @record, "\n";
411 }
412  
413 16 my $edns = $self->edns;
414 16 ▶ CORE::push( @record, $edns->string ) if $edns->_specified;
Branch: 1 branch
BranchTrueFalseTotal
if $edns->_specified 2 14 16
415  
416 16 ▶ my @section = $opcode eq 'UPDATE' ? qw(ZONE PREREQUISITE UPDATE) : qw(QUESTION ANSWER AUTHORITY);
Branch: 1 branch
BranchTrueFalseTotal
$opcode eq 'UPDATE' ? : 1 15 16
417 16 my @question = $self->question;
418 16 my $qdcount = scalar @question;
419 16 ▶ my $qds = $qdcount != 1 ? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
$qdcount != 1 ? : 5 11 16
420 16 CORE::push( @record, ";; $section[0] SECTION ($qdcount record$qds)", map { ';; ' . $_->string } @question );
421  
422 16 my @answer = $self->answer;
423 16 my $ancount = scalar @answer;
424 16 ▶ my $ans = $ancount != 1 ? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
$ancount != 1 ? : 15 1 16
425 16 CORE::push( @record, "\n;; $section[1] SECTION ($ancount record$ans)", map { $_->string } @answer );
426  
427 16 my @authority = $self->authority;
428 16 my $nscount = scalar @authority;
429 16 ▶ my $nss = $nscount != 1 ? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
$nscount != 1 ? : 15 1 16
430 16 CORE::push( @record, "\n;; $section[2] SECTION ($nscount record$nss)", map { $_->string } @authority );
431  
432 16 my @additional = $self->additional;
433 16 my $arcount = scalar @additional;
434 16 ▶ my $ars = $arcount != 1 ? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
$arcount != 1 ? : 13 3 16
435 16 my $EDNSmarker = join ' ', qq[;; {\t"EDNS-VERSION":], $edns->version, qq[}];
436 16 CORE::push( @record, "\n;; ADDITIONAL SECTION ($arcount record$ars)" );
437 16 ▶ CORE::push( @record, map { ( $_ eq $edns ) ? $EDNSmarker : $_->string } @additional );
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $edns ? : 2 5 7
438  
439 16 return join "\n", @record, "\n";
440 }
441  
442  
443 =head2 from
444  
445 print "packet received from ", $packet->from, "\n";
446  
447 Returns the IP address from which this packet was received.
448 This method will return undef for user-created packets.
449  
450 =cut
451  
452 sub from {
453 92 ▶ my ( $self, @argument ) = @_;
Subroutine from: called CC=2 SCAR=6.9
Pod documented
454 92 for (@argument) { $self->{replyfrom} = $_ }
455 92 return $self->{replyfrom};
456 }
457  
458 1 ▶sub answerfrom { return &from; } # uncoverable pod
Subroutine answerfrom: called CC=1 SCAR=0.0
Pod undocumented
459  
460  
461 =head2 size
462  
463 print "packet size: ", $packet->size, " octets\n";
464  
465 Returns the size of the packet in octets as it was received from a
466 nameserver. This method will return undef for user-created packets
467 (use length($packet->data) instead).
468  
469 =cut
470  
471 sub size {
472 2 ▶ return shift->{replysize};
Subroutine size: called CC=1 SCAR=0.0
Pod documented
473 }
474  
475 1 ▶sub answersize { return &size; } # uncoverable pod
Subroutine answersize: called CC=1 SCAR=0.0
Pod undocumented
476  
477  
478 =head2 push
479  
480 $ancount = $packet->push( prereq => $rr );
481 $nscount = $packet->push( update => $rr );
482 $arcount = $packet->push( additional => $rr );
483  
484 $nscount = $packet->push( update => $rr1, $rr2, $rr3 );
485 $nscount = $packet->push( update => @rr );
486  
487 Adds RRs to the specified section of the packet.
488  
489 Returns the number of resource records in the specified section.
490  
491 Section names may be abbreviated to the first three characters.
492  
493 =cut
494  
495 sub push {
496 325 ▶ my ( $self, $section, @rr ) = @_;
Subroutine push: called CC=1 SCAR=0.0
Pod documented
497 325 my $list = $self->_section($section);
498 325 return CORE::push( @$list, @rr );
499 }
500  
501  
502 =head2 unique_push
503  
504 $ancount = $packet->unique_push( prereq => $rr );
505 $nscount = $packet->unique_push( update => $rr );
506 $arcount = $packet->unique_push( additional => $rr );
507  
508 $nscount = $packet->unique_push( update => $rr1, $rr2, $rr3 );
509 $nscount = $packet->unique_push( update => @rr );
510  
511 Adds RRs to the specified section of the packet provided that the
512 RRs are not already present in the same section.
513  
514 Returns the number of resource records in the specified section.
515  
516 Section names may be abbreviated to the first three characters.
517  
518 =cut
519  
520 sub unique_push {
521 93 ▶ my ( $self, $section, @rr ) = @_;
Subroutine unique_push: called CC=1 SCAR=0.0
Pod documented
522 93 my $list = $self->_section($section);
523  
524 93 my %unique = map { ( bless( {%$_, ttl => 0}, ref $_ )->canonical => $_ ) } @rr, @$list;
525 93 return scalar( @$list = values %unique );
526 }
527  
528  
529 =head2 pop
530  
531 my $rr = $packet->pop( 'pre' );
532 my $rr = $packet->pop( 'update' );
533 my $rr = $packet->pop( 'additional' );
534  
535 Removes a single RR from the specified section of the packet.
536  
537 =cut
538  
539 sub pop {
540 5 ▶ my $self = shift;
Subroutine pop: called CC=1 SCAR=0.0
Pod documented
541 5 my $list = $self->_section(shift);
542 5 return CORE::pop(@$list);
543 }
544  
545  
546 94 my %_section = ( ## section name abbreviation table
547 'ans' => 'answer',
548 'pre' => 'answer',
549 'aut' => 'authority',
550 'upd' => 'authority',
551 'add' => 'additional'
552 );
553  
554 sub _section { ## returns array reference for section
555 423 ▶ my $self = shift;
Subroutine _section: called CC=3 SCAR=11.0
556 423 my $name = shift;
557 423 ▶ my $list = $_section{unpack 'a3', $name} || $name;
Condition100%3/3
$_section{unpack 'a3', $name} || $name
l!l&&r!l&&!r
42111
MC/DC100%2/2
$_section{unpack 'a3', $name} || $name
$_section{unpack 'a3', $name} $name
Truth table100%3/3
A || B
A= $_section{unpack 'a3', $name}B= $name
ABresult
1X1
011
000
558 423 ▶ return $self->{$list} ||= [];
Condition100%2/2
$self->{$list} ||= []
l!l
4212
MC/DC100%1/1
$self->{$list} ||= []
$self->{$list}
Truth table100%2/2
A
A= $self->{$list}
Aresult
11
00
559 }
560  
561  
562 =head2 sign_tsig
563  
564 $query = Net::DNS::Packet->new( 'www.example.com', 'A' );
565  
566 $query->sign_tsig(
567 $keyfile,
568 fudge => 60
569 );
570  
571 $reply = $res->send( $query );
572  
573 $reply->verify( $query ) || die $reply->verifyerr;
574  
575 Attaches a TSIG resource record object, which will be used to sign
576 the packet (see RFC 2845).
577  
578 The TSIG record can be customised by optional additional arguments to
579 sign_tsig() or by calling the appropriate Net::DNS::RR::TSIG methods.
580  
581 If you wish to create a TSIG record using a non-standard algorithm,
582 you will have to create it yourself. In all cases, the TSIG name
583 must uniquely identify the key shared between the parties, and the
584 algorithm name must identify the signing function to be used with the
585 specified key.
586  
587 $tsig = Net::DNS::RR->new(
588 name => 'tsig.example',
589 type => 'TSIG',
590 algorithm => 'custom-algorithm',
591 key => '<base64 key text>',
592 sig_function => sub {
593 my ($key, $data) = @_;
594 ...
595 }
596 );
597  
598 $query->sign_tsig( $tsig );
599  
600  
601 The response to an inbound request is signed by presenting the request
602 in place of the key parameter.
603  
604 $response = $request->reply;
605 $response->sign_tsig( $request, @options );
606  
607  
608 Multi-packet transactions are signed by chaining the sign_tsig()
609 calls together as follows:
610  
611 $opaque = $packet1->sign_tsig( 'Kexample.+165+13281.private' );
612 $opaque = $packet2->sign_tsig( $opaque );
613 $opaque = $packet3->sign_tsig( $opaque );
614  
615 The opaque intermediate object references returned during multi-packet
616 signing are not intended to be accessed by the end-user application.
617 Any such access is expressly forbidden.
618  
619 Note that a TSIG record is added to every packet; this implementation
620 does not support the suppressed signature scheme described in RFC2845.
621  
622 =cut
623  
624 sub sign_tsig {
625 32 ▶ my ( $self, @argument ) = @_;
Subroutine sign_tsig: called CC=2 SCAR=6.9
Pod documented
626 32 ▶ return eval {
Condition100%2/2
eval { do { local $SIG{'__DIE__'}; require Net::DNS::RR::TSIG; my $tsig = 'Net::DNS::RR::TSIG'->create(@argument); $self->push('additional', $tsig); return $tsig } } || (return croak("$@\nTSIG: unable to sign packet"))
l!l
311
MC/DC0%0/1
eval { do { local $SIG{'__DIE__'}; require Net::DNS::RR::TSIG; my $tsig = 'Net::DNS::RR::TSIG'->create(@argument); $self->push('additional', $tsig); return $tsig } } || (return croak("$@\nTSIG: unable to sign packet"))
eval { do { local $SIG{'__DIE__'}; require Net::DNS::RR::TSIG; my $tsig = 'Net::DNS::RR::TSIG'->create(@argument); $self->push('additional', $tsig); return $tsig } }
Truth table50%1/2
A
A= eval { do { local $SIG{'__DIE__'}; require Net::DNS::RR::TSIG; my $tsig = 'Net::DNS::RR::TSIG'->create(@argument); $self->push('additional', $tsig); return $tsig } }
Aresult
11
00
627 32 local $SIG{__DIE__};
628 32 require Net::DNS::RR::TSIG;
629 32 my $tsig = Net::DNS::RR::TSIG->create(@argument);
630 31 $self->push( 'additional' => $tsig );
631 31 return $tsig;
632 } || return croak "$@\nTSIG: unable to sign packet";
633 }
634  
635  
636 =head2 verify and verifyerr
637  
638 $reply->verify($query) || die $reply->verifyerr;
639  
640 Verify TSIG signature of a reply to the corresponding query.
641  
642  
643 $opaque = $packet1->verify( $query ) || die $packet1->verifyerr;
644 $opaque = $packet2->verify( $opaque );
645 $verifed = $packet3->verify( $opaque ) || die $packet3->verifyerr;
646  
647 Verify TSIG signature of a multi-packet reply to the corresponding query.
648  
649 The opaque intermediate object references returned by verify() at each
650 stage will be undefined (Boolean false) if verification fails.
651 Testing at every stage is not necessary, which produces a BADSIG error
652 on the final packet in the absence of more specific information.
653 Access to the objects themselves, if they exist, is expressly forbidden.
654  
655 =cut
656  
657 sub verify {
658 39 ▶ my ( $self, @argument ) = @_;
Subroutine verify: called CC=2 SCAR=6.9
Pod documented
659 39 my $sig = $self->sigrr;
660 39 ▶ return $sig ? $sig->verify( $self, @argument ) : shift @argument;
Branch: 1 branch
BranchTrueFalseTotal
$sig ? : 38 1 39
661 }
662  
663 sub verifyerr {
664 25 ▶ my $sig = shift->sigrr;
Subroutine verifyerr: called CC=2 SCAR=6.9
Pod documented
665 25 ▶ return $sig ? $sig->vrfyerrstr : 'not signed';
Branch: 1 branch
BranchTrueFalseTotal
$sig ? : 24 1 25
666 }
667  
668  
669 =head2 sign_sig0
670  
671 SIG0 support is provided through the Net::DNS::RR::SIG class.
672 The requisite cryptographic components are not integrated into
673 Net::DNS but reside in the Net::DNS::SEC distribution available
674 from CPAN.
675  
676 $update = Net::DNS::Update->new('example.com');
677 $update->push( update => rr_add('foo.example.com A 10.1.2.3'));
678 $update->sign_sig0('Kexample.com+003+25317.private');
679  
680 Execution will be terminated if Net::DNS::SEC is not available.
681  
682  
683 =head2 verify SIG0
684  
685 $packet->verify( $keyrr ) || die $packet->verifyerr;
686 $packet->verify( [$keyrr, ...] ) || die $packet->verifyerr;
687  
688 Verify SIG0 packet signature against one or more specified KEY RRs.
689  
690 =cut
691  
692 sub sign_sig0 {
693 2 ▶ my $self = shift;
Subroutine sign_sig0: called CC=3 SCAR=11.0
Pod documented
694 2 my $karg = shift;
695  
696 2 ▶ return eval {
Condition100%2/2
eval { do { local $SIG{'__DIE__'}; my $sig0; if (ref $karg eq 'Net::DNS::RR::SIG') { $sig0 = $karg; } else { require Net::DNS::RR::SIG; $sig0 = 'Net::DNS::RR::SIG'->create('', $karg); } ; $self->push('additional', $sig0); return $sig0 } } || (return croak("$@\nSIG0: unable to sign packet"))
l!l
11
MC/DC100%1/1
eval { do { local $SIG{'__DIE__'}; my $sig0; if (ref $karg eq 'Net::DNS::RR::SIG') { $sig0 = $karg; } else { require Net::DNS::RR::SIG; $sig0 = 'Net::DNS::RR::SIG'->create('', $karg); } ; $self->push('additional', $sig0); return $sig0 } } || (return croak("$@\nSIG0: unable to sign packet"))
eval { do { local $SIG{'__DIE__'}; my $sig0; if (ref $karg eq 'Net::DNS::RR::SIG') { $sig0 = $karg; } else { require Net::DNS::RR::SIG; $sig0 = 'Net::DNS::RR::SIG'->create('', $karg); } ; $self->push('additional', $sig0); return $sig0 } }
Truth table100%2/2
A
A= eval { do { local $SIG{'__DIE__'}; my $sig0; if (ref $karg eq 'Net::DNS::RR::SIG') { $sig0 = $karg; } else { require Net::DNS::RR::SIG; $sig0 = 'Net::DNS::RR::SIG'->create('', $karg); } ; $self->push('additional', $sig0); return $sig0 } }
Aresult
11
00
697 2 local $SIG{__DIE__};
698  
699 2 my $sig0;
700 2 ▶ if ( ref($karg) eq 'Net::DNS::RR::SIG' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $karg eq 'Net::DNS::RR::SIG') { } 1 1 2
701 1 $sig0 = $karg;
702  
703 } else {
704 1 require Net::DNS::RR::SIG;
705 1 $sig0 = Net::DNS::RR::SIG->create( '', $karg );
706 }
707  
708 1 $self->push( 'additional' => $sig0 );
709 1 return $sig0;
710 } || return croak "$@\nSIG0: unable to sign packet";
711 }
712  
713  
714 =head2 sigrr
715  
716 $sigrr = $packet->sigrr() || die 'unsigned packet';
717  
718 The sigrr method returns the signature RR from a signed packet
719 or undefined if the signature is absent.
720  
721 =cut
722  
723 sub sigrr {
724 122 ▶ my $self = shift;
Subroutine sigrr: called CC=4 SCAR=13.9
Pod documented
725  
726 122 my ($sig) = reverse $self->additional;
727 122 ▶ return unless $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig 16 106 122
728 106 for ( $sig->type ) {
729 106 ▶ return $sig if /TSIG|SIG/;
Branch: 1 branch
BranchTrueFalseTotal
if /TSIG|SIG/ 99 7 106
730 }
731 7 return;
732 }
733  
734  
735 ########################################
736  
737 =head2 truncate
738  
739 The truncate method takes a maximum length as argument and then tries
740 to truncate the packet and set the TC bit according to the rules of
741 RFC2181 Section 9.
742  
743 The smallest length limit that is honoured is 512 octets.
744  
745 =cut
746  
747 # From RFC2181:
748 #
749 # 9. The TC (truncated) header bit
750 #
751 # The TC bit should be set in responses only when an RRSet is required
752 # as a part of the response, but could not be included in its entirety.
753 # The TC bit should not be set merely because some extra information
754 # could have been included, for which there was insufficient room. This
755 # includes the results of additional section processing. In such cases
756 # the entire RRSet that will not fit in the response should be omitted,
757 # and the reply sent as is, with the TC bit clear. If the recipient of
758 # the reply needs the omitted data, it can construct a query for that
759 # data and send that separately.
760 #
761 # Where TC is set, the partial RRSet that would not completely fit may
762 # be left in the response. When a DNS client receives a reply with TC
763 # set, it should ignore that response, and query again, using a
764 # mechanism, such as a TCP connection, that will permit larger replies.
765  
766 # Code developed from a contribution by Aaron Crane via rt.cpan.org 33547
767  
768 sub truncate {
769 5 ▶ my $self = shift;
Subroutine truncate: called CC=17 SCAR=28.3
Pod documented
770 5 ▶ my $size = shift || UDPSZ;
Condition100%2/2
shift() || 512
l!l
32
MC/DC100%1/1
shift() || 512
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
771  
772 5 my $sigrr = $self->sigrr;
773 5 ▶ $size = UDPSZ unless $size > UDPSZ;
Branch: 1 branch
BranchTrueFalseTotal
unless $size > 512 3 2 5
774 5 ▶ $size -= $sigrr->_size if $sigrr;
Branch: 1 branch
BranchTrueFalseTotal
if $sigrr 1 4 5
775  
776 5 my $data = pack 'x' x HEADER_LENGTH; # header placeholder
777 5 $self->{count} = [];
778  
779 5 my $tc;
780 5 my $hash = {};
781 5 foreach my $section ( map { $self->{$_} } qw(question answer authority) ) {
782 15 my @list;
783 15 foreach my $item (@$section) {
784 108 my $component = $item->encode( length $data, $hash );
785 108 ▶ last if length($data) + length($component) > $size;
Branch: 1 branch
BranchTrueFalseTotal
if length($data) + length($component) > $size 3 105 108
786 105 ▶ last if $tc;
Branch: 1 branch
BranchTrueFalseTotal
if $tc 1 104 105
787 104 $data .= $component;
788 104 CORE::push @list, $item;
789 }
790 15 ▶ $tc++ if scalar(@list) < scalar(@$section);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list < scalar @$section 4 11 15
791 15 @$section = @list;
792 }
793 5 ▶ $self->header->tc(1) if $tc; # only set if truncated here
Branch: 1 branch
BranchTrueFalseTotal
if $tc 2 3 5
794  
795 5 my %rrset;
796 my @order;
797 5 foreach my $item ( grep { ref($_) ne ref($sigrr) } $self->additional ) {
798 145 my $name = $item->{owner}->canonical;
799 145 ▶ my $class = $item->{class} || 0;
Condition100%2/2
$item->{'class'} || 0
l!l
1441
MC/DC100%1/1
$item->{'class'} || 0
$item->{'class'}
Truth table100%2/2
A
A= $item->{'class'}
Aresult
11
00
800 145 my $key = pack 'nna*', $class, $item->{type}, $name;
801 145 ▶ CORE::push @order, $key unless $rrset{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $rrset{$key} 121 24 145
802 145 CORE::push @{$rrset{$key}}, $item;
803 }
804  
805 5 my @list;
806 5 foreach my $key (@order) {
807 54 my $component = '';
808 54 my @item = @{$rrset{$key}};
809 54 foreach my $item (@item) {
810 66 $component .= $item->encode( length $data, $hash );
811 }
812 54 ▶ last if length($data) + length($component) > $size;
Branch: 1 branch
BranchTrueFalseTotal
if length($data) + length($component) > $size 4 50 54
813 50 $data .= $component;
814 50 CORE::push @list, @item;
815 }
816  
817 5 ▶ if ($sigrr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigrr) 1 4 5
818 1 $data .= $sigrr->encode( length $data, $hash, $self );
819 1 CORE::push @list, $sigrr;
820 }
821 5 $self->{'additional'} = \@list;
822  
823 5 my @part = qw(question answer authority additional);
824 5 my @size = map { scalar @{$self->{$_}} } @part;
825 5 return pack 'n6 a*', $self->_quid, $self->{status}, @size, substr( $data, HEADER_LENGTH );
826 }
827  
828  
829 ########################################
830  
831 sub dump { ## print internal data structure
832 3 ▶ my @data = @_; # uncoverable pod
Subroutine dump: called CC=4 SCAR=13.9
Pod undocumented
833 3 require Data::Dumper;
834 3 ▶ local $Data::Dumper::Maxdepth = $Data::Dumper::Maxdepth || 3;
Condition100%2/2
$Data::Dumper::Maxdepth || 3
l!l
21
MC/DC100%1/1
$Data::Dumper::Maxdepth || 3
$Data::Dumper::Maxdepth
Truth table100%2/2
A
A= $Data::Dumper::Maxdepth
Aresult
11
00
835 3 ▶ local $Data::Dumper::Sortkeys = $Data::Dumper::Sortkeys || 1;
Condition100%2/2
$Data::Dumper::Sortkeys || 1
l!l
21
MC/DC100%1/1
$Data::Dumper::Sortkeys || 1
$Data::Dumper::Sortkeys
Truth table100%2/2
A
A= $Data::Dumper::Sortkeys
Aresult
11
00
836 3 ▶ local $Data::Dumper::Useqq = $Data::Dumper::Useqq || 1;
Condition100%2/2
$Data::Dumper::Useqq || 1
l!l
21
MC/DC100%1/1
$Data::Dumper::Useqq || 1
$Data::Dumper::Useqq
Truth table100%2/2
A
A= $Data::Dumper::Useqq
Aresult
11
00
837 3 print Data::Dumper::Dumper(@data);
838 3 return;
839 }
840  
841  
842 94 my ( $cache1, $cache2, $limit );
843  
844 sub _quid { ## generate (short-term) unique query ID
845 203 ▶ my $self = shift;
Subroutine _quid: called CC=5 SCAR=16.1
846 203 my $id = $self->{id};
847 203 ▶ $cache1->{$id}++ if $id; # cache non-zero ID
Branch: 1 branch
BranchTrueFalseTotal
if $id 55 148 203
848 203 ▶ return $id if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 55 148 203
849 148 ▶ ( $cache2, $cache1, $limit ) = ( $cache1, {0 => 1}, 50 ) unless $limit--;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit-- 11 137 148
850 148 $id = int rand(0xffff); # two layer ID cache
851 148 ▶ $id = int rand(0xffff) while $cache1->{$id}++ + exists( $cache2->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
$cache1->{$id}++ + exists $cache2->{$id} and do { $id = int rand 65535 } 0 148 148
852 148 return $self->{id} = $id;
853 }
854  
855  
856 94 1;
857 __END__
« DNS/Question.pm ↑ Summary DNS/RR/TSIG.pm »