File: Packet.pm

package info (click to toggle)
libnet-dns-perl 0.19-0.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 544 kB
  • ctags: 323
  • sloc: perl: 5,191; makefile: 54
file content (871 lines) | stat: -rw-r--r-- 20,498 bytes parent folder | download
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
package Net::DNS::Packet;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(dn_expand);

use strict;
use vars qw($VERSION $AUTOLOAD);

use Carp;
use Net::DNS;
use Net::DNS::Question;
use Net::DNS::RR;

# $Id: Packet.pm,v 1.13 2001/02/07 05:13:48 mfuhr Exp mfuhr $
$VERSION = $Net::DNS::VERSION;

=head1 NAME

Net::DNS::Packet - DNS packet object class

=head1 SYNOPSIS

C<use Net::DNS::Packet;>

=head1 DESCRIPTION

A C<Net::DNS::Packet> object represents a DNS packet.

=head1 METHODS

=head2 new

    $packet = Net::DNS::Packet->new("example.com");
    $packet = Net::DNS::Packet->new("example.com", "MX", "IN");

    $packet = Net::DNS::Packet->new(\$data);
    $packet = Net::DNS::Packet->new(\$data, 1);  # set debugging

    ($packet, $err) = Net::DNS::Packet->new(\$data);

If passed a domain, type, and class, C<new> creates a packet
object appropriate for making a DNS query for the requested
information.  The type and class can be omitted; they default
to A and IN.

If passed a reference to a scalar containing DNS packet data,
C<new> creates a packet object from that data.  A second argument
can be passed to turn on debugging output for packet parsing.

If called in array context, returns a packet object and an
error string.  The error string will only be defined if the
packet object is undefined (i.e., couldn't be created).

Returns B<undef> if unable to create a packet object (e.g., if
the packet data is truncated).

=cut

sub new {
	my $class = shift;
	my %self;

	$self{"compnames"} = {};

	PARSE: {
	if (ref($_[0])) {
		my $data = shift;
		my $debug = @_ ? shift : 0;

		#--------------------------------------------------------------
		# Parse the header section.
		#--------------------------------------------------------------

		if ($debug) {
			print ";; HEADER SECTION\n";
		}

		$self{"header"} = Net::DNS::Header->new($data);

		unless (defined $self{"header"}) {
			return wantarray
			       ? (undef, "header section incomplete")
			       : undef;
		}

		$self{"header"}->print if $debug;

		my $offset = &Net::DNS::HFIXEDSZ;

		#--------------------------------------------------------------
		# Parse the question/zone section.
		#--------------------------------------------------------------

		if ($debug) {
			print "\n";
			my $section = ($self{"header"}->opcode eq "UPDATE")
			            ? "ZONE"
				    : "QUESTION";
			print ";; $section SECTION (",
			      $self{"header"}->qdcount, " record",
			      $self{"header"}->qdcount == 1 ? "" : "s",
			      ")\n";
		}

		$self{"question"} = [];
		foreach (1 .. $self{"header"}->qdcount) {
			my $qobj;
			($qobj, $offset) = parse_question($data, $offset);

			unless (defined $qobj) {
				last PARSE if $self{"header"}->tc;
				return wantarray
				       ? (undef, "question section incomplete")
				       : undef;
			}

			push(@{$self{"question"}}, $qobj);
			if ($debug) {
				print ";; ";
				$qobj->print;
			}
		}
			
		#--------------------------------------------------------------
		# Parse the answer/prerequisite section.
		#--------------------------------------------------------------

		if ($debug) {
			print "\n";
			my $section = ($self{"header"}->opcode eq "UPDATE")
				    ? "PREREQUISITE"
			            : "ANSWER";
			print ";; $section SECTION (",
			      $self{"header"}->ancount, " record",
			      $self{"header"}->ancount == 1 ? "" : "s",
			      ")\n";
		}

		$self{"answer"} = [];
		foreach (1 .. $self{"header"}->ancount) {
			my $rrobj;
			($rrobj, $offset) = parse_rr($data, $offset);

			unless (defined $rrobj) {
				last PARSE if $self{"header"}->tc;
				return wantarray
				       ? (undef, "answer section incomplete")
				       : undef;
			}

			push(@{$self{"answer"}}, $rrobj);
			$rrobj->print if $debug;
		}

		#--------------------------------------------------------------
		# Parse the authority/update section.
		#--------------------------------------------------------------

		if ($debug) {
			print "\n";
			my $section = ($self{"header"}->opcode eq "UPDATE")
			            ? "UPDATE"
				    : "AUTHORITY";
			print ";; $section SECTION (",
			      $self{"header"}->nscount, " record",
			      $self{"header"}->nscount == 1 ? "" : "s",
			      ")\n";
		}

		$self{"authority"} = [];
		foreach (1 .. $self{"header"}->nscount) {
			my $rrobj;
			($rrobj, $offset) = parse_rr($data, $offset);

			unless (defined $rrobj) {
				last PARSE if $self{"header"}->tc;
				return wantarray
				       ? (undef, "authority section incomplete")
				       : undef;
			}

			push(@{$self{"authority"}}, $rrobj);
			$rrobj->print if $debug;
		}

		#--------------------------------------------------------------
		# Parse the additional section.
		#--------------------------------------------------------------

		if ($debug) {
			print "\n";
			print ";; ADDITIONAL SECTION (",
			      $self{"header"}->adcount, " record",
			      $self{"header"}->adcount == 1 ? "" : "s",
			      ")\n";
		}

		$self{"additional"} = [];
		foreach (1 .. $self{"header"}->arcount) {
			my $rrobj;
			($rrobj, $offset) = parse_rr($data, $offset);

			unless (defined $rrobj) {
				last PARSE if $self{"header"}->tc;
				return wantarray
				       ? (undef, "additional section incomplete")
				       : undef;
			}

			push(@{$self{"additional"}}, $rrobj);
			$rrobj->print if $debug;
		}
	}
	else {
		my ($qname, $qtype, $qclass) = @_;

		$qtype  = "A"  unless defined $qtype;
		$qclass = "IN" unless defined $qclass;

		$self{"header"} = Net::DNS::Header->new;
		$self{"header"}->qdcount(1);
		$self{"question"} = [ Net::DNS::Question->new($qname,
							      $qtype,
							      $qclass) ];
		$self{"answer"}     = [];
		$self{"authority"}  = [];
		$self{"additional"} = [];
	}
	} # PARSE

	return wantarray
		? ((bless \%self, $class), undef)
		: bless \%self, $class;
}

=head2 data

    $data = $packet->data;

Returns the packet data in binary format, suitable for sending to
a nameserver.

=cut

sub data {
	my $self = shift;

	#----------------------------------------------------------------------
	# Flush the cache of already-compressed names.  This should fix the bug
	# that caused this method to work only the first time it was called.
	#----------------------------------------------------------------------

	$self->{"compnames"} = {};

	#----------------------------------------------------------------------
	# Get the data for each section in the packet.
	#----------------------------------------------------------------------

	my $data = $self->{"header"}->data;

	foreach my $question (@{$self->{"question"}}) {
		$data .= $question->data($self, length $data);
	}

	foreach my $rr (@{$self->{"answer"}}) {
		$data .= $rr->data($self, length $data);
	}

	foreach my $rr (@{$self->{"authority"}}) {
		$data .= $rr->data($self, length $data);
	}

	foreach my $rr (@{$self->{"additional"}}) {
		$data .= $rr->data($self, length $data);
	}

	return $data;
}

=head2 header

    $header = $packet->header;

Returns a C<Net::DNS::Header> object representing the header section
of the packet.

=cut

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

=head2 question, zone

    @question = $packet->question;

Returns a list of C<Net::DNS::Question> objects representing the
question section of the packet.

In dynamic update packets, this section is known as C<zone> and
specifies the zone to be updated.

=cut

sub question {
	my $self = shift;
	return @{$self->{"question"}};
}

sub zone {
	my $self = shift;
	$self->question(@_);
}

=head2 answer, pre, prerequisite

    @answer = $packet->answer;

Returns a list of C<Net::DNS::RR> objects representing the answer
section of the packet.

In dynamic update packets, this section is known as C<pre> or
C<prerequisite> and specifies the RRs or RRsets which must or
must not preexist.

=cut

sub answer {
	my $self = shift;
	return @{$self->{"answer"}};
}

sub pre {
	my $self = shift;
	$self->answer(@_);
}

sub prerequisite {
	my $self = shift;
	$self->answer(@_);
}

=head2 authority, update

    @authority = $packet->authority;

Returns a list of C<Net::DNS::RR> objects representing the authority
section of the packet.

In dynamic update packets, this section is known as C<update> and
specifies the RRs or RRsets to be added or delted.

=cut

sub authority {
	my $self = shift;
	return @{$self->{"authority"}};
}

sub update {
	my $self = shift;
	$self->authority(@_);
}

=head2 additional

    @additional = $packet->additional;

Returns a list of C<Net::DNS::RR> objects representing the additional
section of the packet.

=cut

sub additional {
	my $self = shift;
	return @{$self->{"additional"}};
}

=head2 print

    $packet->print;

Prints the packet data on the standard output in an ASCII format
similar to that used in DNS zone files.

=cut

sub print {
	my $self = shift;
	print $self->string;
}

=head2 string

    print $packet->string;

Returns a string representation of the packet.

=cut

sub string {
	my $self = shift;
	my ($qr, $rr, $section);
	my $retval = "";

	if (exists $self->{"answerfrom"}) {
		$retval .= ";; Answer received from $self->{answerfrom} " .
			   "($self->{answersize} bytes)\n;;\n";
	}

	$retval .= ";; HEADER SECTION\n";
	$retval .= $self->header->string;

	$retval .= "\n";
	$section = ($self->header->opcode eq "UPDATE") ? "ZONE" : "QUESTION";
	$retval .= ";; $section SECTION (" . $self->header->qdcount     . 
		   " record" . ($self->header->qdcount == 1 ? "" : "s") .
		   ")\n";
	foreach $qr ($self->question) {
		$retval .= ";; " . $qr->string . "\n";
	}

	$retval .= "\n";
	$section = ($self->header->opcode eq "UPDATE") ? "PREREQUISITE" : "ANSWER";
	$retval .= ";; $section SECTION (" . $self->header->ancount     .
		   " record" . ($self->header->ancount == 1 ? "" : "s") .
		   ")\n";
	foreach $rr ($self->answer) {
		$retval .= $rr->string . "\n";
	}

	$retval .= "\n";
	$section = ($self->header->opcode eq "UPDATE") ? "UPDATE" : "AUTHORITY";
	$retval .= ";; $section SECTION (" . $self->header->nscount     .
		  " record" . ($self->header->nscount == 1 ? "" : "s") .
		  ")\n";
	foreach $rr ($self->authority) {
		$retval .= $rr->string . "\n";
	}

	$retval .= "\n";
	$retval .= ";; ADDITIONAL SECTION (" . $self->header->arcount   .
		   " record" . ($self->header->arcount == 1 ? "" : "s") .
		   ")\n";
	foreach $rr ($self->additional) {
		$retval .= $rr->string . "\n";
	}

	return $retval;
}

=head2 answerfrom

    print "packet received from ", $packet->answerfrom, "\n";

Returns the IP address from which we received this packet.  User-created
packets will return undef for this method.

=cut

sub answerfrom {
	my $self = shift;

	$self->{"answerfrom"} = shift if @_;

	return exists $self->{"answerfrom"}
	       ? $self->{"answerfrom"}
	       : undef;
}

=head2 answersize

    print "packet size: ", $packet->answersize, " bytes\n";

Returns the size of the packet in bytes as it was received from a
nameserver.  User-created packets will return undef for this method
(use C<length $packet>->C<data> instead).

=cut

sub answersize {
	my $self = shift;

	$self->{"answersize"} = shift if @_;

	return exists $self->{"answersize"}
	       ? $self->{"answersize"}
	       : undef;
}

=head2 push

    $packet->push("pre", $rr);
    $packet->push("update", $rr);
    $packet->push("additional", $rr);

    $packet->push("update", $rr1, $rr2, $rr3);
    $packet->push("update", @rr);

Adds RRs to the specified section of the packet.

=cut

sub push {
	my ($self, $section, @rr) = @_;
	my $rr;

	return unless $section;

	$section = lc $section;
	if (($section eq "prerequisite") || ($section eq "prereq")) {
		$section = "pre";
	}

	if (($self->{"header"}->opcode eq "UPDATE")
	 && (($section eq "pre") || ($section eq "update")) ) {
		my $zone_class = ($self->zone)[0]->zclass;
		foreach $rr (@rr) {
			unless ($rr->class eq "NONE" || $rr->class eq "ANY") {
				$rr->class($zone_class);
			}
		}
	}

	if ($section eq "answer" || $section eq "pre") {
		push(@{$self->{"answer"}}, @rr);
		my $ancount = $self->{"header"}->ancount;
		$self->{"header"}->ancount($ancount + @rr);
	}
	elsif ($section eq "authority" || $section eq "update") {
		push(@{$self->{"authority"}}, @rr);
		my $nscount = $self->{"header"}->nscount;
		$self->{"header"}->nscount($nscount + @rr);
	}
	elsif ($section eq "additional") {
		push(@{$self->{"additional"}}, @rr);
		my $adcount = $self->{"header"}->adcount;
		$self->{"header"}->adcount($adcount + @rr);
	}
	else {
		Carp::cluck(qq(invalid section "$section"\n));
	}
}

=head2 pop

    my $rr = $packet->pop("pre");
    my $rr = $packet->pop("update");
    my $rr = $packet->pop("additional");

Removes RRs from the specified section of the packet.

=cut

sub pop {
	my ($self, $section) = @_;

	return unless $section;
	$section = lc $section;

	if (($section eq "prerequisite") || ($section eq "prereq")) {
		$section = "pre";
	}

	my $rr;

	if ($section eq "answer" || $section eq "pre") {
		my $ancount = $self->{"header"}->ancount;
		if ($ancount) {
			$rr = pop @{$self->{"answer"}};
			$self->{"header"}->ancount($ancount - 1);
		}
	}
	elsif ($section eq "authority" || $section eq "update") {
		my $nscount = $self->{"header"}->nscount;
		if ($nscount) {
			$rr = pop @{$self->{"authority"}};
			$self->{"header"}->nscount($nscount - 1);
		}
	}
	elsif ($section eq "additional") {
		my $adcount = $self->{"header"}->adcount;
		if ($adcount) {
			$rr = pop @{$self->{"additional"}};
			$self->{"header"}->adcount($adcount - 1);
		}
	}
	else {
		Carp::cluck(qq(invalid section "$section"\n));
	}

	return $rr;
}

=head2 dn_comp

    $compname = $packet->dn_comp("foo.example.com", $offset);

Returns a domain name compressed for a particular packet object, to
be stored beginning at the given offset within the packet data.  The
name will be added to a running list of compressed domain names for
future use.

=cut

sub dn_comp {
	my ($self, $name, $offset) = @_;

	$name = "" unless defined($name);

	my $compname = "";
	my @names = map { s/\\\././g; $_ } split(/(?<!\\)\./, $name);

	while (@names) {
		my $dname = join(".", @names);

		if (exists $self->{"compnames"}->{$dname}) {
			my $pointer = $self->{"compnames"}->{$dname};
			$compname .= pack("n", 0xc000 | $pointer);
			last;
		}

		$self->{"compnames"}->{$dname} = $offset;
		my $first  = shift @names;
		my $length = length $first;
		$compname .= pack("C a*", $length, $first);
		$offset   += $length + 1;
	}

	$compname .= pack("C", 0) unless @names;
	return $compname;
}

=head2 dn_expand

    use Net::DNS::Packet qw(dn_expand);
    ($name, $nextoffset) = dn_expand(\$data, $offset);

    ($name, $nextoffset) = Net::DNS::Packet::dn_expand(\$data, $offset);

Expands the domain name stored at a particular location in a DNS
packet.  The first argument is a reference to a scalar containing
the packet data.  The second argument is the offset within the
packet where the (possibly compressed) domain name is stored.

Returns the domain name and the offset of the next location in the
packet.

Returns B<(undef, undef)> if the domain name couldn't be expanded.

=cut

sub dn_expand {
	my ($packet, $offset) = @_;
	my %seen;
	dn_expand2($packet, $offset, \%seen);
}

sub dn_expand2 {
	my ($packet, $offset, $seen) = @_;
	my $name = "";
	my $len;
	my $packetlen = length $$packet;
	my $int16sz = &Net::DNS::INT16SZ;

	# Debugging
	#if ($seen->{$offset}) {
	#	die "dn_expand: loop: offset=$offset (seen = ",
	#	     join(",", keys %$seen), ")\n";
	#}
	#$seen->{$offset} = 1;

	while (1) {
		return (undef, undef) if $packetlen < ($offset + 1);

		$len = unpack("\@$offset C", $$packet);

		if ($len == 0) {
			$offset++;
			last;
		}
		elsif (($len & 0xc0) == 0xc0) {
			return (undef, undef)
				if $packetlen < ($offset + $int16sz);

			my $ptr = unpack("\@$offset n", $$packet);
			$ptr &= 0x3fff;
			my($name2) = dn_expand2($packet, $ptr, $seen);

			return (undef, undef) unless defined $name2;

			$name .= $name2;
			$offset += $int16sz;
			last;
		}
		else {
			$offset++;

			return (undef, undef)
				if $packetlen < ($offset + $len);

			my $elem = substr($$packet, $offset, $len);
			$elem =~ s/\./\\./g;
			$name .= "$elem.";
			$offset += $len;
		}
	}

	$name =~ s/\.$//;
	return ($name, $offset);
}

=head2 sign_tsig

    $key_name = "tsig-key";
    $key      = "awwLOtRfpGE+rRKF2+DEiw==";

    $update = Net::DNS::Update->new("example.com");
    $update->push("update", rr_add("foo.example.com A 10.1.2.3"));

    $update->sign_tsig($key_name, $key);

    $response = $res->send($update);

Signs a packet with a TSIG resource record (see RFC 2845).  Uses the
following defaults:

    algorithm   = HMAC-MD5.SIG-ALG.REG.INT
    time_signed = current time
    fudge       = 300 seconds

If you wish to customize the TSIG record, you'll have to create it
yourself and call the appropriate Net::DNS::RR::TSIG methods.  The
following example creates a TSIG record and sets the fudge to 60
seconds:

    $key_name = "tsig-key";
    $key      = "awwLOtRfpGE+rRKF2+DEiw==";

    $tsig = Net::DNS::RR->new("$key_name TSIG $key");
    $tsig->fudge(60);

    $query = Net::DNS::Packet->new("www.example.com");
    $query->sign_tsig($tsig);

    $response = $res->send($query);

You shouldn't modify a packet after signing it; otherwise authentication
will probably fail.

=cut

sub sign_tsig {
	my $self = shift;

	my $tsig;

	if (@_ == 1 && ref($_[0])) {
		$tsig = $_[0];
	}
	elsif (@_ == 2) {
		my ($key_name, $key) = @_;
		if (defined($key_name) && defined($key)) {
			$tsig = Net::DNS::RR->new("$key_name TSIG $key")
		}
	}

	$self->push("additional", $tsig) if $tsig;
	return $tsig;
}

#------------------------------------------------------------------------------
# parse_question
#
#     ($queryobj, $newoffset) = parse_question(\$data, $offset);
#
# Parses a question section record contained at a particular location within
# a DNS packet.  The first argument is a reference to the packet data.  The
# second argument is the offset within the packet where the question record
# begins.
#
# Returns a Net::DNS::Question object and the offset of the next location
# in the packet.
#
# Returns (undef, undef) if the question object couldn't be created (e.g.,
# if there isn't enough data).
#------------------------------------------------------------------------------

sub parse_question {
	my ($data, $offset) = @_;
	my $qname;

	($qname, $offset) = dn_expand($data, $offset);
	return (undef, undef) unless defined $qname;

	return (undef, undef)
		if length($$data) < ($offset + 2 * &Net::DNS::INT16SZ);

	my ($qtype, $qclass) = unpack("\@$offset n2", $$data);
	$offset += 2 * &Net::DNS::INT16SZ;

	$qtype  = $Net::DNS::typesbyval{$qtype};
	$qclass = $Net::DNS::classesbyval{$qclass};

	return (Net::DNS::Question->new($qname, $qtype, $qclass), $offset);
}

#------------------------------------------------------------------------------
# parse_rr
#
#    ($rrobj, $newoffset) = parse_rr(\$data, $offset);
#
# Parses a DNS resource record (RR) contained at a particular location
# within a DNS packet.  The first argument is a reference to a scalar
# containing the packet data.  The second argument is the offset within
# the data where the RR is located.
#
# Returns a Net::DNS::RR object and the offset of the next location
# in the packet.
#------------------------------------------------------------------------------

sub parse_rr {
	my ($data, $offset) = @_;
	my $name;

	($name, $offset) = dn_expand($data, $offset);
	return (undef, undef) unless defined $name;

	return (undef, undef)
		if length($$data) < ($offset + &Net::DNS::RRFIXEDSZ);

	my ($type, $class, $ttl, $rdlength) = unpack("\@$offset n2 N n", $$data);
	$type  = $Net::DNS::typesbyval{$type}    || $type;
	$class = $Net::DNS::classesbyval{$class} || $class;

	$offset += &Net::DNS::RRFIXEDSZ;

	return (undef, undef)
		if length($$data) < ($offset + $rdlength);

	my $rrobj = Net::DNS::RR->new($name,
				      $type,
				      $class,
				      $ttl,
				      $rdlength, 
				      $data,
				      $offset);

	return (undef, undef) unless defined $rrobj;

	$offset += $rdlength;
	return ($rrobj, $offset);
}

=head1 COPYRIGHT

Copyright (c) 1997-2000 Michael Fuhr.  All rights reserved.  This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself. 

=head1 SEE ALSO

L<perl(1)>, L<Net::DNS>, L<Net::DNS::Resolver>, L<Net::DNS::Update>,
L<Net::DNS::Header>, L<Net::DNS::Question>, L<Net::DNS::RR>,
RFC 1035 Section 4.1, RFC 2136 Section 2, RFC 2845

=cut

1;