File: OPT.pm

package info (click to toggle)
libnet-dns-perl 0.59-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 828 kB
  • ctags: 400
  • sloc: perl: 6,650; sh: 220; ansic: 101; makefile: 60
file content (286 lines) | stat: -rw-r--r-- 6,618 bytes parent folder | download | duplicates (3)
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
package Net::DNS::RR::OPT;
#
# $Id: OPT.pm 393 2005-07-01 19:21:52Z olaf $
#

use strict;
BEGIN { 
    eval { require bytes; }
} 
use vars qw(@ISA $VERSION %extendedrcodesbyname %extendedrcodesbyval $EDNSVERSION);

use Carp;

@ISA     = qw(Net::DNS::RR);
$VERSION = (qw$LastChangedRevision: 393 $)[1];

$EDNSVERSION = 0;

%extendedrcodesbyname = (
	"ONLY_RDATA" => 0,		# No name specified see 4.6 of 2671 
	"UNDEF1"     => 1,
	"UNDEF2"     => 2,
	"UNDEF3"     => 3,
	"UNDEF4"     => 4,
	"UNDEF5"     => 5,
	"UNDEF6"     => 6,
	"UNDEF7"     => 7,
	"UNDEF8"     => 8,
	"UNDEF9"     => 9,
	"UNDEF10"    => 10,
	"UNDEF11"    => 11,
	"UNDEF12"    => 12,
	"UNDEF13"    => 13,
	"UNDEF14"    => 14,
	"UNDEF15"    => 15,
	"BADVERS"    => 16,		# RFC 2671
);
%extendedrcodesbyval = reverse %extendedrcodesbyname;



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

	$self->{"name"} = "" ;   # should allway be "root"

	if ($self->{"rdlength"} > 0) {
		$self->{"optioncode"}   = unpack("n", substr($$data, $offset, 2));
		$self->{"optionlength"} = unpack("n", substr($$data, $offset+2, 2));
		$self->{"optiondata"}   = unpack("n", substr($$data, $offset+4, $self->{"optionlength"}));
	}

	$self->{"_rcode_flags"}  = pack("N",$self->{"ttl"});
	
	$self->{"extendedrcode"} = unpack("C", substr($self->{"_rcode_flags"}, 0, 1));
	$self->{"ednsversion"}   = unpack("C", substr($self->{"_rcode_flags"}, 1, 1));
	$self->{"ednsflags"}     = unpack("n", substr($self->{"_rcode_flags"}, 2, 2));
	
	
	return bless $self, $class;
}






sub new_from_string {
    my ($class, $self ) = @_;
    
    # There is no such thing as an OPT RR in a ZONE file. 
    # Not implemented!
    croak "You should not try to create a OPT RR from a string\nNot implemented";
    return bless $self, $class;
}



sub new_from_hash {
	my ($class, $self ) = @_;
	
	$self->{"name"} = "" ;   # should allway be "root"
	# Setting the MTU smaller then 512 does not make sense 
	# should we test for a maximum here?
	if ($self->{"class"} eq "IN" || $self->{"class"} < 512) {
		$self->{"class"} = 512;    # Default value...
	}
	
	$self->{"extendedrcode"} = 0 unless exists $self->{"extendedrcode"};
	
	$self->{"ednsflags"}   = 0 unless exists $self->{"ednsflags"};
	$self->{"ednsversion"} = $EDNSVERSION unless exists $self->{"ednsversion"};
	$self->{"ttl"}= unpack ("N", 
		pack("C", $self->{"extendedrcode"}) .
		pack("C", $self->{"ednsversion"})  .
		pack("n", $self->{"ednsflags"})
	);
	
	if (exists  $self->{"optioncode"}) {
		$self->{"optiondata"}   = "" if ! exists  $self->{"optiondata"};
		$self->{"optionlength"} = length $self->{"optiondata"}
	}
	
	return bless $self, $class;

}





sub string {
   my $self = shift;
   return
	"; EDNS Version "     . $self->{"ednsversion"} . 
	"\t UDP Packetsize: " .  $self->{"class"} . 
	"\n; EDNS-RCODE:\t"   . $self->{"extendedrcode"} .
	" (" . $extendedrcodesbyval{ $self->{"extendedrcode"} }. ")" .
	"\n; EDNS-FLAGS:\t"   . sprintf("0x%04x", $self->{"ednsflags"}) .
	"\n";
}


sub rdatastr {
	return '; Parsing of OPT rdata is not yet implemented';
}


sub rr_rdata {
	my $self = shift;
	my $rdata;
	
	if (exists $self->{"optioncode"}) {
		$rdata  = pack("n", $self->{"optioncode"});
		$rdata .= pack("n", $self->{"optionlength"}); 
		$rdata .= $self->{"optiondata"}
	} else {
		$rdata = "";
	}
	
	return $rdata;
}







sub do{
    my $self=shift;
    return ( 0x8000 & $self->{"ednsflags"} );
}



sub set_do{
    my $self=shift;
    return $self->{"ednsflags"} = ( 0x8000 | $self->{"ednsflags"} );

}



sub clear_do{
    my $self=shift;
    return $self->{"ednsflags"} = ( ~0x8000 & $self->{"ednsflags"} );

}



sub size {
    my $self=shift;
    my $size=shift;
    $self->{"class"}=$size if defined($size);
    return $self->{"class"};
}




1;
__END__

=head1 NAME

Net::DNS::RR::OPT - DNS OPT

=head1 SYNOPSIS

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

=head1 DESCRIPTION

Class for EDNS pseudo resource record OPT.

=head1 METHODS

This object should only be used inside the Net::DNS classes itself.

=head2 new 

Since "OPT" is a pseudo record and should not be stored in
masterfiles; Therefore we have not implemented a method to create this
RR from string.

One may create the object from a hash. See RFC 2671 for details for
the meaning of the hash keys.

 $rr= new Net::DNS::RR {
    name => "",     # Ignored and set to ""
    type => "OPT",  
    class => 1024,    # sets UDP payload size
    extendedrcode =>  0x00,    # sets the extended RCODE 1 octets
    ednsflags     =>  0x0000,  # sets the ednsflags (2octets)  
    optioncode   =>   0x0      # 2 octets
    optiondata   =>   0x0      # optionlength octets
 }    

The ednsversion is set to 0 for now. The ttl value is determined from 
the extendedrcode, the ednsversion and the ednsflag.
The rdata is constructed from the optioncode and optiondata 
see section 4.4 of RFC 2671

If optioncode is left undefined then we do not expect any RDATA.

The defaults are no rdata.   


=head2 do, set_do, clear_do

    $opt->set_do;

Reads, sets and clears the do flag. (first bit in the ednssflags);


=head2 size

    $opt->size(1498);
    print "Packet size:". $opt->size() ;
 
Sets or gets the packet size.


=head1 TODO

- This class is tailored to use with dnssec. 

- Do some range checking on the input.

- This class probably needs subclasses once OPTION codes start to be defined.

- look at use of extended labels

=head1 COPYRIGHT

Copyright (c) 2001, 2002  RIPE NCC.  Author Olaf M. Kolkman

All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that 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, written prior permission.


THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Based on, and contains, code by Copyright (c) 1997-2002 Michael Fuhr.

=head1 SEE ALSO

L<perl(1)>, L<Net::DNS>, L<Net::DNS::Resolver>, L<Net::DNS::Packet>,
L<Net::DNS::Header>, L<Net::DNS::Question>, L<Net::DNS::RR>,
RFC 2435 Section 3

=cut