summaryrefslogtreecommitdiff
path: root/chromium/third_party/cygwin/lib/perl5/vendor_perl/5.10/i686-cygwin/Net/DNS/RR.pm
blob: c3e2b829f8d7125aa67236fc4f99bd253579bd07 (plain)
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
package Net::DNS::RR;
#
# $Id: RR.pm 705 2008-02-06 21:59:18Z olaf $
#
use strict;

BEGIN { 
    eval { require bytes; }
} 


use vars qw($VERSION $AUTOLOAD     %rrsortfunct );
use Carp;
use Net::DNS;
use Net::DNS::RR::Unknown;



$VERSION = (qw$LastChangedRevision: 705 $)[1];

=head1 NAME

Net::DNS::RR - DNS Resource Record class

=head1 SYNOPSIS

C<use Net::DNS::RR>

=head1 DESCRIPTION

C<Net::DNS::RR> is the base class for DNS Resource Record (RR) objects.
See also the manual pages for each RR type.

=head1 METHODS

B<WARNING!!!>  Don't assume the RR objects you receive from a query
are of a particular type -- always check an object's type before calling
any of its methods.  If you call an unknown method, you'll get a nasty
warning message and C<Net::DNS::RR> will return C<undef> to the caller.

=cut
#' Stupid Emacs (I Don't even USE emacs!) '

# %RR needs to be available within the scope of the BEGIN block.
# $RR_REGEX is a global just to be on the safe side.  
# %_LOADED is used internally for autoloading the RR subclasses.
use vars qw(%RR %_LOADED $RR_REGEX);

BEGIN {

	%RR = map { $_ => 1 } qw(
		A
		AAAA
		AFSDB
		CNAME
		CERT
		DNAME
		EID
		HINFO
		ISDN
		LOC
		MB
		MG
		MINFO
		MR
		MX
		NAPTR
		NIMLOC
		NS
		NSAP
		NULL
		PTR
		PX
		RP
		RT
		SOA
		SRV
		TKEY
		TSIG
		TXT
		X25
		OPT
		SSHFP
		SPF
		IPSECKEY
	);

	#  Only load DNSSEC if available

	eval { 	    
	    local $SIG{'__DIE__'} = 'DEFAULT';
	    require Net::DNS::RR::SIG; 
	};

	unless ($@) {
		$RR{'SIG'} = 1;
		eval { 	    
		    local $SIG{'__DIE__'} = 'DEFAULT';
		    require Net::DNS::RR::NXT; 
		};
		
		unless ($@) {
		    $RR{'NXT'}	= 1;
		} else {
		    die $@;
		}
		
		eval { 
		    local $SIG{'__DIE__'} = 'DEFAULT';
		    require Net::DNS::RR::KEY; 
		};
		
		unless ($@) {
		    $RR{'KEY'} = 1;
		} else {
		    die $@;
		}

	 	eval { 
		    local $SIG{'__DIE__'} = 'DEFAULT';
		    require Net::DNS::RR::DS; 
		};

	 	unless ($@) {
		    $RR{'DS'} = 1;

		} else {
		    die $@;
		}

	 	eval { 
		    local $SIG{'__DIE__'} = 'DEFAULT';
		    require Net::DNS::RR::RRSIG; 
		};

	 	unless ($@) {
		    $RR{'RRSIG'} = 1;
		    # If RRSIG is available so should the other DNSSEC types
		    eval {		    
			local $SIG{'__DIE__'} = 'DEFAULT';
			require Net::DNS::RR::NSEC; 
		    };
		    unless ($@) {
		      $RR{'NSEC'} = 1;
		    } else {
		    die $@;
		  }
		    eval { 
			local $SIG{'__DIE__'} = 'DEFAULT';
			require Net::DNS::RR::DNSKEY; 
		    };

		    unless ($@) {
		      $RR{'DNSKEY'} = 1;
		    } else {
		      die $@;
		    }
		} 

	 	eval { 
		  local $SIG{'__DIE__'} = 'DEFAULT';
		  require Net::DNS::RR::DLV; 
		};

		unless ($@) {
		  $RR{'DLV'} =1;
		} else {
		  # Die only if we are dealing with a version for which DLV is 
		  # available 
		  die $@ if defined ($Net::DNS::SEC::HAS_DLV) ;

		}

	 	eval { 
		  local $SIG{'__DIE__'} = 'DEFAULT';
		  require Net::DNS::RR::NSEC3; 
		};

		unless ($@) {
		  $RR{'NSEC3'} =1;
		} else {
		  # Die only if we are dealing with a version for which NSEC3 is		  # available 
		  die $@ if defined ($Net::DNS::SEC::HAS_NSEC3);
		}
		
		
	 	eval { 
		  local $SIG{'__DIE__'} = 'DEFAULT';
		  require Net::DNS::RR::NSEC3PARAM; 
		};

		unless ($@) {
		  $RR{'NSEC3PARAM'} =1;
		} else {
		  # Die only if we are dealing with a version for which NSEC3 is 
		  # available 

		  die $@ if defined($Net::DNS::SEC::SVNVERSION) &&  $Net::DNS::SEC::SVNVERSION > 619;   # In the code since. (for users of the SVN trunk)
		}



    }
}

sub build_regex {
	my $classes = join('|', keys %Net::DNS::classesbyname, 'CLASS\\d+');

	# Longest ones go first, so the regex engine will match AAAA before A.
	my $types   = join('|', sort { length $b <=> length $a } keys %Net::DNS::typesbyname);

	$types .= '|TYPE\\d+';
				
	$RR_REGEX   = " ^ 
					\\s*
    	            (\\S+) # name anything non-space will do 
    	            \\s*                
    	            (\\d+)?           
    	            \\s*
    	            ($classes)?
    	            \\s*
    	            ($types)?
    	            \\s*
    	            (.*)
    	            \$";

#	print STDERR "Regex: $RR_REGEX\n";
}


=head2 new (from string)

 $a     = Net::DNS::RR->new("foo.example.com. 86400 A 10.1.2.3");
 $mx    = Net::DNS::RR->new("example.com. 7200 MX 10 mailhost.example.com.");
 $cname = Net::DNS::RR->new("www.example.com 300 IN CNAME www1.example.com");
 $txt   = Net::DNS::RR->new('baz.example.com 3600 HS TXT "text record"');

Returns a C<Net::DNS::RR> object of the appropriate type and
initialized from the string passed by the user.  The format of the
string is that used in zone files, and is compatible with the string
returned by C<< Net::DNS::RR->string >>.

The name and RR type are required; all other information is optional.
If omitted, the TTL defaults to 0 and the RR class defaults to IN.
Omitting the optional fields is useful for creating the empty RDATA
sections required for certain dynamic update operations.  See the
C<Net::DNS::Update> manual page for additional examples.

All names must be fully qualified.  The trailing dot (.) is optional.

=head2 new (from hash)

 $rr = Net::DNS::RR->new(
	 name    => "foo.example.com",
	 ttl     => 86400,
	 class   => "IN",
	 type    => "A",
	 address => "10.1.2.3",
 );
 
 $rr = Net::DNS::RR->new(
	 name => "foo.example.com",
	 type => "A",
 );

Returns an RR object of the appropriate type, or a C<Net::DNS::RR>
object if the type isn't implemented.  See the manual pages for
each RR type to see what fields the type requires.

The C<Name> and C<Type> fields are required; all others are optional.
If omitted, C<TTL> defaults to 0 and C<Class> defaults to IN.  Omitting
the optional fields is useful for creating the empty RDATA sections
required for certain dynamic update operations.

The fields are case-insensitive, but starting each with uppercase
is recommended.

=cut

#' Stupid Emacs


sub new {
	return new_from_string(@_) if @_ == 2;
	return new_from_string(@_) if @_ == 3;
	
	return new_from_hash(@_);
}


sub new_from_data {
	my $class = shift;
	my ($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset) = @_;

	my $self = {	name		=> $name,
			type		=> $rrtype,
			class		=> $rrclass,
			ttl		=> $ttl,
			rdlength	=> $rdlength,
			rdata		=> substr($$data, $offset, $rdlength)
			};

	if ($RR{$rrtype}) {
		my $subclass = $class->_get_subclass($rrtype);
		return $subclass->new($self, $data, $offset);
	} else {
		return Net::DNS::RR::Unknown->new($self, $data, $offset);
	}

}

sub new_from_string {
	my ($class, $rrstring, $update_type) = @_;

	build_regex() unless $RR_REGEX;

	# strip out comments
	# Test for non escaped ";" by means of the look-behind assertion
	# (the backslash is escaped)
	$rrstring   =~ s/(?<!\\);.*//og;
	
	($rrstring =~ m/$RR_REGEX/xso) || 
		confess qq|qInternal Error: "$rrstring" did not match RR pat.\nPlease report this to the author!\n|;

	my $name    = $1;
	my $ttl     = $2 || 0;
	my $rrclass = $3 || '';


	my $rrtype  = $4 || '';
	my $rdata   = $5 || '';

	$rdata =~ s/\s+$//o if $rdata;
	$name  =~ s/\.$//o  if $name;



	# RFC3597 tweaks
	# This converts to known class and type if specified as TYPE###
	$rrtype  = Net::DNS::typesbyval(Net::DNS::typesbyname($rrtype))      if $rrtype  =~ m/^TYPE\d+/o;
	$rrclass = Net::DNS::classesbyval(Net::DNS::classesbyname($rrclass)) if $rrclass =~ m/^CLASS\d+/o;


	if (!$rrtype && $rrclass && $rrclass eq 'ANY') {
		$rrtype  = 'ANY';
		$rrclass = 'IN';
	} elsif (!$rrclass) {
		$rrclass = 'IN';
	}

	$rrtype ||= 'ANY';
	

	if ($update_type) {
		$update_type = lc $update_type;
		
		if ($update_type eq 'yxrrset') {
			$ttl     = 0;
			$rrclass = 'ANY' unless $rdata;
		} elsif ($update_type eq 'nxrrset') {
			$ttl     = 0;
			$rrclass = 'NONE';
			$rdata   = '';
		} elsif ($update_type eq 'yxdomain') {
			$ttl     = 0;
			$rrclass = 'ANY';
			$rrtype  = 'ANY';
			$rdata   = '';
		} elsif ($update_type eq 'nxdomain') {
			$ttl     = 0;
			$rrclass = 'NONE';
			$rrtype  = 'ANY';
			$rdata   = '';
		} elsif ($update_type =~ /^(rr_)?add$/o) {
			$ttl = 86400 unless $ttl;
		} elsif ($update_type =~ /^(rr_)?del(ete)?$/o) {
			$ttl     = 0;
			$rrclass = $rdata ? 'NONE' : 'ANY';
		}
	}

	# We used to check if $rrtype was defined at this point.  However,
	# we just defaulted it to ANY earlier....

	my $self = {
		'name'     => $name,
		'type'     => $rrtype,
		'class'    => $rrclass,
		'ttl'      => $ttl,
		'rdlength' => 0,
		'rdata'    => '',
	};

	if ($RR{$rrtype} && $rdata !~ m/^\s*\\#/o ) {
		my $subclass = $class->_get_subclass($rrtype);
		return $subclass->new_from_string($self, $rdata);
	} elsif ($RR{$rrtype}) {   # A RR type known to Net::DNS starting with \#
		$rdata =~ m/\\\#\s+(\d+)\s+(.*)$/o;

		my $rdlength = $1;
		my $hexdump  = $2;		
		$hexdump =~ s/\s*//og;

		die "$rdata is inconsistent; length does not match content" 
			if length($hexdump) != $rdlength*2;

		$rdata = pack('H*', $hexdump);
	
		return Net::DNS::RR->new_from_data(
			$name, 
			$rrtype, 
			$rrclass, 
			$ttl, 
			$rdlength, 
			\$rdata, 
			length($rdata) - $rdlength
		);
	} elsif ($rdata=~/\s*\\\#\s+\d+\s+/o) {   
		#We are now dealing with the truly unknown.
		die 'Expected RFC3597 representation of RDATA' 
			unless $rdata =~ m/\\\#\s+(\d+)\s+(.*)$/o;

		my $rdlength = $1;
		my $hexdump  = $2;		
		$hexdump =~ s/\s*//og;

		die "$rdata is inconsistent; length does not match content" 
			if length($hexdump) != $rdlength*2;

		$rdata = pack('H*', $hexdump);

		return Net::DNS::RR->new_from_data(
			$name,
			$rrtype,
			$rrclass,
			$ttl,
			$rdlength,
			\$rdata,
			length($rdata) - $rdlength
		);
	} else {  
		#God knows how to handle these... bless them in the RR class.
		bless $self, $class;
		return $self
	}
	
}

sub new_from_hash {
	my $class    = shift;
	my %keyval   = @_;
	my $self     = {};

	while ( my ($key, $val) = each %keyval ) {
		( $self->{lc $key} = $val ) =~ s/\.+$// if defined $val;
	}

	croak('RR name not specified') unless defined $self->{name};
	croak('RR type not specified') unless defined $self->{type};

	$self->{'ttl'}   ||= 0;
	$self->{'class'} ||= 'IN';

	$self->{'rdlength'} = length $self->{'rdata'}
		if $self->{'rdata'};

	if ($RR{$self->{'type'}}) {
		my $subclass = $class->_get_subclass($self->{'type'});
	   
	    if (uc $self->{'type'} ne 'OPT') {
			bless $self, $subclass;
			
			return $self;
	    } else {  
			# Special processing of OPT. Since TTL and CLASS are
			# set by other variables. See Net::DNS::RR::OPT 
			# documentation
			return $subclass->new_from_hash($self);
	    }
	} elsif ($self->{'type'} =~ /TYPE\d+/o) {
		bless $self, 'Net::DNS::RR::Unknown';
		return $self;
	} else {
	 	bless $self, $class;
	 	return $self;
	}
}


=head2 parse

    ($rrobj, $offset) = Net::DNS::RR->parse(\$data, $offset);

Parses a DNS resource record at the specified 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 resource record begins.

Returns a Net::DNS::RR object and the offset of the next location in the packet.

Parsing is aborted if the object could not be created (e.g., corrupt or insufficient data).

=cut

use constant PACKED_LENGTH => length pack 'n2 N n', (0)x4;

sub parse {
	my ($objclass, $data, $offset) = @_;

	my ($name, $index) = Net::DNS::Packet::dn_expand($data, $offset);
	die 'Exception: corrupt or incomplete data' unless $index;

	my $rdindex = $index + PACKED_LENGTH;
	die 'Exception: incomplete data' if length $$data < $rdindex;
	my ($type, $class, $ttl, $rdlength) = unpack("\@$index n2 N n", $$data);

	my $next = $rdindex + $rdlength;
	die 'Exception: incomplete data' if length $$data < $next;

	$type = Net::DNS::typesbyval($type) || $type;

	# Special case for OPT RR where CLASS should be
	# interpreted as 16 bit unsigned (RFC2671, 4.3)
	if ($type ne 'OPT') {
		$class = Net::DNS::classesbyval($class) || $class;
	}
	# else just retain numerical value

	my $self = $objclass->new_from_data($name, $type, $class, $ttl, $rdlength, $data, $rdindex);
	die 'Exception: corrupt or incomplete RR subtype data' unless defined $self;

	return wantarray ? ($self, $next) : $self;
}


#
# Some people have reported that Net::DNS dies because AUTOLOAD picks up
# calls to DESTROY.
#
sub DESTROY {}

=head2 print

    $rr->print;

Prints the record to the standard output.  Calls the
B<string> method to get the RR's string representation.

=cut
#' someone said that emacs gets screwy here.  Who am I to claim otherwise...

sub print {	print &string, "\n"; }

=head2 string

    print $rr->string, "\n";

Returns a string representation of the RR.  Calls the
B<rdatastr> method to get the RR-specific data.

=cut

sub string {
	my $self = shift;
	my $data = $self->rdatastr || '; no data';
  
	join "\t", "$self->{name}.", $self->{ttl}, $self->{class}, $self->{type}, $data;
}

=head2 rdatastr

    $s = $rr->rdatastr;

Returns a string containing RR-specific data.  Subclasses will need
to implement this method.

=cut

sub rdatastr {
	my $self = shift;
	return exists $self->{'rdlength'}
	       ? "; rdlength = $self->{'rdlength'}"
	       : '';
}

=head2 name

    $name = $rr->name;

Returns the record's domain name.

=head2 type

    $type = $rr->type;

Returns the record's type.

=head2 class

    $class = $rr->class;

Returns the record's class.

=cut

# Used to AUTOLOAD this, but apparently some versions of Perl (specifically
# 5.003_07, included with some Linux distributions) would return the
# class the object was blessed into, instead of the RR's class.

sub class {
	my $self = shift;

	if (@_) {
		$self->{'class'} = shift;
	} elsif (!exists $self->{'class'}) {
		Carp::carp('class: no such method');
		return undef;
	}
	return $self->{'class'};
}
	

=head2 ttl

    $ttl = $rr->ttl;

Returns the record's time-to-live (TTL).

=head2 rdlength

    $rdlength = $rr->rdlength;

Returns the length of the record's data section.

=head2 rdata

    $rdata = $rr->rdata

Returns the record's data section as binary data.

=cut
#'
sub rdata {
	my $self = shift;
	my $retval = undef;

	if (@_ == 2) {
		my ($packet, $offset) = @_;
		$retval = $self->rr_rdata($packet, $offset);
	}
	elsif (exists $self->{'rdata'}) {
		$retval = $self->{'rdata'};
	}

	return $retval;
}

sub rr_rdata {
	my $self = shift;
	return exists $self->{'rdata'} ? $self->{'rdata'} : '';
}

#------------------------------------------------------------------------------
# sub data
#
# This method is called by Net::DNS::Packet->data to get the binary
# representation of an RR.
#------------------------------------------------------------------------------

sub data {
	my ($self, $packet, $offset) = @_;
	my $data;


	# Don't compress TSIG or TKEY names and don't mess with EDNS0 packets
	if (uc($self->{'type'}) eq 'TSIG' || uc($self->{'type'}) eq 'TKEY') {
		my $tmp_packet = Net::DNS::Packet->new();
		$data = $tmp_packet->dn_comp($self->{'name'}, 0);
		return undef unless defined $data;
	} elsif (uc($self->{'type'}) eq 'OPT') {
		my $tmp_packet = Net::DNS::Packet->new();
		$data = $tmp_packet->dn_comp('', 0);
	} else {
		$data  = $packet->dn_comp($self->{'name'}, $offset);
		return undef unless defined $data;	
	}

	my $qtype     = uc($self->{'type'});
	my $qtype_val = ($qtype =~ m/^\d+$/o) ? $qtype : Net::DNS::typesbyname($qtype);
	$qtype_val    = 0 if !defined($qtype_val);

	my $qclass     = uc($self->{'class'});
	my $qclass_val = ($qclass =~ m/^\d+$/o) ? $qclass : Net::DNS::classesbyname($qclass);
	$qclass_val    = 0 if !defined($qclass_val);
	$data .= pack('n', $qtype_val);
	
	# If the type is OPT then class will need to contain a decimal number
	# containing the UDP payload size. (RFC2671 section 4.3)
	if (uc($self->{'type'}) ne 'OPT') {
	    $data .= pack('n', $qclass_val);
	} else {
	    $data .= pack('n', $self->{'class'});
	}
	
	$data .= pack('N', $self->{'ttl'});

	$offset += length($data) + &Net::DNS::INT16SZ;	# allow for rdlength

	my $rdata = $self->rdata($packet, $offset);

	$data .= pack('n', length $rdata);
	$data.=$rdata;

	return $data;
}





#------------------------------------------------------------------------------
#  This method is called by SIG objects verify method. 
#  It is almost the same as data but needed to get an representation of the
#  packets in wire format withoud domain name compression.
#  It is essential to DNSSEC RFC 2535 section 8
#------------------------------------------------------------------------------

sub _canonicaldata {
	my $self = shift;
	my $data='';
	{   
	    my $name=$self->{'name'};
	    my @dname=Net::DNS::name2labels($name);
	    for (my $i=0;$i<@dname;$i++){
		$data .= pack ('C',length $dname[$i] );
		$data .= lc($dname[$i] );
	    }
	    $data .= pack ('C','0');
	}
	$data .= pack('n', Net::DNS::typesbyname(uc($self->{'type'})));
	$data .= pack('n', Net::DNS::classesbyname(uc($self->{'class'})));
	$data .= pack('N', $self->{'ttl'});
	
	
	my $rdata = $self->_canonicalRdata;
	
	$data .= pack('n', length $rdata);
	$data .= $rdata;
	return $data;


}

# These are methods that are used in the DNSSEC context...  Some RR
# have domain names in them. Verification works only on RRs with
# uncompressed domain names. (Canonical format as in sect 8 of
# RFC2535) _canonicalRdata is overwritten in those RR objects that
# have domain names in the RDATA and _name2wire is used to convert a
# domain name to "wire format"


sub _canonicalRdata {
    my $self=shift;
    my $packet=Net::DNS::Packet->new();
    my $rdata = $self->rr_rdata($packet,0);
    return $rdata;
}





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

    my $rdata="";
    my $compname = "";
    my @dname = Net::DNS::name2labels($name);


    for (@dname) {
		$rdata .= pack('C', length $_);
		$rdata .= $_ ;
    }
    
    $rdata .= pack('C', '0');
    return $rdata;
}





sub AUTOLOAD {
	my ($self) = @_;  # If we do shift here, it will mess up the goto below.
	my ($name) = $AUTOLOAD =~ m/^.*::(.*)$/o;
	if ($name =~ /set_rrsort_func/){
	    return Net::DNS::RR::set_rrsort_func(@_);
	}
	if ($name =~ /get_rrsort_func/){
	    return Net::DNS::RR::get_rrsort_func(@_);
	}
	# XXX -- We should test that we do in fact carp on unknown methods.	
	unless (exists $self->{$name}) {
	    my $rr_string = $self->string;
	    Carp::carp(<<"AMEN");
	    
***
***  WARNING!!!  The program has attempted to call the method
***  "$name" for the following RR object:
***
***  $rr_string
***
***  This object does not have a method "$name".  THIS IS A BUG
***  IN THE CALLING SOFTWARE, which has incorrectly assumed that
***  the object would be of a particular type.  The calling
***  software should check the type of each RR object before
***  calling any of its methods.
***
***  Net::DNS has returned undef to the caller.
*** 

AMEN
return;
	}
	
	no strict q/refs/;
	
	# Build a method in the class.
	*{$AUTOLOAD} = sub {
	    my ($self, $new_val) = @_;
	    
	    if (defined $new_val) {
		$self->{$name} = $new_val;
	    }
	    
	    return $self->{$name};
	};
	
	# And jump over to it.
	goto &{$AUTOLOAD};
}



#
#  Net::DNS::RR->_get_subclass($type)
#
# Return a subclass, after loading a subclass (if needed)
#
sub _get_subclass {
	my ($class, $type) = @_;
	
	return unless $type and $RR{$type};
	
	my $subclass = join('::', $class, $type);
	
	unless ($_LOADED{$subclass}) {
		eval "require $subclass";
		die $@ if $@;
		$_LOADED{$subclass}++;
	}
	
	return $subclass;
}	




=head1 Sorting of RR arrays

As of version 0.55 there is functionality to help you sort RR
arrays. The sorting is done by Net::DNS::rrsort(), see the
L<Net::DNS> documentation. This package provides class methods to set
the sorting functions used for a particular RR based on a particular
attribute.


=head2 set_rrsort_func

Net::DNS::RR::SRV->set_rrsort_func("priority",
			       sub {
				   my ($a,$b)=($Net::DNS::a,$Net::DNS::b);
				   $a->priority <=> $b->priority
				   ||
				   $b->weight <=> $a->weight
                     }

Net::DNS::RR::SRV->set_rrsort_func("default_sort",
			       sub {
				   my ($a,$b)=($Net::DNS::a,$Net::DNS::b);
				   $a->priority <=> $b->priority
				   ||
				   $b->weight <=> $a->weight
                     }

set_rrsort_func needs to be called as a class method. The first
argument is the attribute name on which the sorting will need to take
place. If you specify "default_sort" than that is the sort algorithm
that will be used in the case that rrsort() is called without an RR
attribute as argument.

The second argument is a reference to a function that uses the
variables $a and $b global to the C<from Net::DNS>(!!)package for the
sorting. During the sorting $a and $b will contain references to
objects from the class you called the set_prop_sort from. In other
words, you can rest assured that the above sorting function will only
get Net::DNS::RR::SRV objects.

The above example is the sorting function that actually is implemented in 
SRV.

=cut




sub set_rrsort_func{
    my $class=shift;
    my $attribute=shift;
    my $funct=shift;
#    print "Using ".__PACKAGE__."set_rrsort: $class\n";
    my ($type) = $class =~ m/^.*::(.*)$/o;
    $Net::DNS::RR::rrsortfunct{$type}{$attribute}=$funct;
}
		
sub get_rrsort_func {
    my $class=shift;    
    my $attribute=shift;  #can be undefined.
    my $sortsub;
    my ($type) = $class =~ m/^.*::(.*)$/o;


#    print "Using ".__PACKAGE__." get_rrsort: $class ($type,$attribute)\n";
#    use Data::Dumper;
#    print Dumper %Net::DNS::rrsortfunct;

    if (defined($attribute) &&
	exists($Net::DNS::RR::rrsortfunct{$type}) &&
	exists($Net::DNS::RR::rrsortfunct{$type}{$attribute})
	){
	#  The default overwritten by the class variable in Net::DNS
	return $Net::DNS::RR::rrsortfunct{$type}{$attribute};
    }elsif(
	! defined($attribute) &&
	exists($Net::DNS::RR::rrsortfunct{$type}) &&
	exists($Net::DNS::RR::rrsortfunct{$type}{'default_sort'})
	){
	#  The default overwritten by the class variable in Net::DNS
	return $Net::DNS::RR::rrsortfunct{$type}{'default_sort'};
    }
    elsif( defined($attribute) ){
	
	return sub{
	    my ($a,$b)=($Net::DNS::a,$Net::DNS::b);
	    ( exists($a->{$attribute}) &&   
	      $a->{$attribute} <=> $b->{$attribute})
		||
		$a->_canonicaldata() cmp $b->_canonicaldata()
	};
    }else{
	return sub{
	    my ($a,$b)=($Net::DNS::a,$Net::DNS::b);
	    $a->_canonicaldata() cmp $b->_canonicaldata()
	};
    }    

    return $sortsub;
}






	
sub STORABLE_freeze {
	my ($self, $cloning) = @_;

	return if $cloning;
	
	return ('', {%$self});
}

sub STORABLE_thaw {
	my ($self, $cloning, undef, $data) = @_;

	%{$self}  = %{$data};
	
	__PACKAGE__->_get_subclass($self->{'type'});
	
	return $self;
}

=head1 BUGS

This version of C<Net::DNS::RR> does little sanity checking on user-created
RR objects.

=head1 COPYRIGHT

Copyright (c) 1997-2002 Michael Fuhr. 

Portions Copyright (c) 2002-2004 Chris Reinhardt.

Portions Copyright (c) 2005-2007 Olaf Kolkman 

Portions Copyright (c) 2007 Dick Franks 

All rights reserved.  This program is free software; you may redistribute
it and/or modify it under the same terms as Perl itself.

EDNS0 extensions by Olaf Kolkman.

=head1 SEE ALSO

L<perl(1)>, L<Net::DNS>, L<Net::DNS::Resolver>, L<Net::DNS::Packet>,
L<Net::DNS::Update>, L<Net::DNS::Header>, L<Net::DNS::Question>,
RFC 1035 Section 4.1.3

=cut

1;