File Coverage

blib/lib/Net/DNS/RR/IPSECKEY.pm
Criterion Covered Total %
statement 109 109 100.0
branch 28 28 100.0
path n/a
condition 10 10 100.0
subroutine 20 20 100.0
pod 7 7 100.0
total 174 174 100.0


line stmt bran path cond sub pod time code
1               package Net::DNS::RR::IPSECKEY;
2                
3 2       2   14 use strict;
  2           5  
  2           91  
4 2       2   25 use warnings;
  2           2  
  2           191  
5               our $VERSION = (qw$Id: IPSECKEY.pm 2003 2025-01-21 12:06:06Z willem $)[2];
6                
7 2       2   11 use base qw(Net::DNS::RR);
  2           5  
  2           204  
8                
9                
10               =head1 NAME
11                
12               Net::DNS::RR::IPSECKEY - DNS IPSECKEY resource record
13                
14               =cut
15                
16 2       2   12 use integer;
  2           3  
  2           13  
17                
18 2       2   123 use Carp;
  2           4  
  2           163  
19                
20 2       2   12 use Net::DNS::DomainName;
  2           3  
  2           103  
21 2       2   704 use Net::DNS::RR::A;
  2           5  
  2           60  
22 2       2   739 use Net::DNS::RR::AAAA;
  2           6  
  2           92  
23                
24 2       2   13 use constant BASE64 => defined eval { require MIME::Base64 };
  2           4  
  2           4  
  2           2985  
25                
26               my %wireformat = (
27               0 => 'C3 a0 a*',
28               1 => 'C3 a4 a*',
29               2 => 'C3 a16 a*',
30               3 => 'C3 a* a*',
31               );
32                
33                
34               sub _decode_rdata { ## decode rdata from wire-format octet string
35 30       30   49 my ( $self, $data, $offset, @opaque ) = @_;
36                
37 30           42 my $limit = $offset + $self->{rdlength};
38                
39 30           68 @{$self}{qw(precedence gatetype algorithm)} = unpack "\@$offset C3", $$data;
  30           55  
40 30           39 $offset += 3;
41                
42 30           38 my $gatetype = $self->{gatetype};
43 30 100         77 if ( not $gatetype ) {
    100            
    100            
    100            
44 7           12 delete $self->{gateway}; # no gateway
45                
46               } elsif ( $gatetype == 1 ) {
47 7           19 $self->{gateway} = unpack "\@$offset a4", $$data;
48 7           10 $offset += 4;
49                
50               } elsif ( $gatetype == 2 ) {
51 7           20 $self->{gateway} = unpack "\@$offset a16", $$data;
52 7           13 $offset += 16;
53                
54               } elsif ( $gatetype == 3 ) {
55 8           10 my $name;
56 8           19 ( $name, $offset ) = Net::DNS::DomainName->decode( $data, $offset, @opaque );
57 8           25 $self->{gateway} = $name->encode;
58                
59               } else {
60 1           19 die "unknown gateway type ($gatetype)";
61               }
62                
63 29           88 $self->keybin( substr $$data, $offset, $limit - $offset );
64 29           55 return;
65               }
66                
67                
68               sub _encode_rdata { ## encode rdata as wire-format octet string
69 9       9   16 my $self = shift;
70                
71 9           21 my $gatetype = $self->gatetype;
72 9     100     32 my $gateway = $self->{gateway} || '';
73 9           21 my $precedence = $self->precedence;
74 9           23 my $algorithm = $self->algorithm;
75 9           22 my $keybin = $self->keybin;
76                
77 9           56 return pack $wireformat{$gatetype}, $precedence, $gatetype, $algorithm, $gateway, $keybin;
78               }
79                
80                
81               sub _format_rdata { ## format rdata portion of RR string.
82 23       23   41 my $self = shift;
83                
84 23           35 return $self->SUPER::_format_rdata() unless BASE64;
85 23           45 my @rdata = map { $self->$_ } qw(precedence gatetype algorithm);
  69           177  
86 23           55 my @base64 = split /\s+/, MIME::Base64::encode( $self->keybin );
87 23           87 push @rdata, ( $self->gateway, @base64 );
88 23           137 return @rdata;
89               }
90                
91                
92               sub _parse_rdata { ## populate RR from rdata in argument list
93 7       7   26 my ( $self, @argument ) = @_;
94                
95 7           15 foreach (qw(precedence gatetype algorithm gateway)) { $self->$_( shift @argument ) }
  28           94  
96 7           24 $self->key(@argument);
97 7           20 return;
98               }
99                
100                
101               sub precedence {
102 42       42 1 104 my ( $self, @value ) = @_;
103 42           86 for (@value) { $self->{precedence} = 0 + $_ }
  8           31  
104 42     100     186 return $self->{precedence} || 0;
105               }
106                
107                
108               sub gatetype {
109 71     100 71 1 1636 return shift->{gatetype} || 0;
110               }
111                
112                
113               sub algorithm {
114 42       42 1 1394 my ( $self, @value ) = @_;
115 42           75 for (@value) { $self->{algorithm} = 0 + $_ }
  8           50  
116 42     100     155 return $self->{algorithm} || 0;
117               }
118                
119                
120               sub gateway {
121 42       42 1 4400 my ( $self, @value ) = @_;
122                
123 42           85 for (@value) {
124 13 100         98 /^\.*$/ && do {
125 2           9 $self->{gatetype} = 0;
126 2           5 delete $self->{gateway}; # no gateway
127 2           5 last;
128               };
129 11 100         36 /:.*:/ && do {
130 2           6 $self->{gatetype} = 2;
131 2           11 $self->{gateway} = Net::DNS::RR::AAAA::address( {}, $_ );
132 2           7 last;
133               };
134 9 100         42 /\.\d+$/ && do {
135 2           7 $self->{gatetype} = 1;
136 2           12 $self->{gateway} = Net::DNS::RR::A::address( {}, $_ );
137 2           9 last;
138               };
139 7 100         37 /\..+/ && do {
140 6           15 $self->{gatetype} = 3;
141 6           34 $self->{gateway} = Net::DNS::DomainName->new($_)->encode;
142 6           17 last;
143               };
144 1           171 croak 'unrecognised gateway type';
145               }
146                
147 41 100         150 if ( defined wantarray ) {
148 29           58 my $gateway = $self->{gateway};
149 29           75 for ( $self->gatetype ) {
150 29 100         141 /^1$/ && return Net::DNS::RR::A::address( {address => $gateway} );
151 23 100         81 /^2$/ && return Net::DNS::RR::AAAA::address( {address => $gateway} );
152 17 100         89 /^3$/ && return Net::DNS::DomainName->decode( \$gateway )->name;
153               }
154 7 100         38 return wantarray ? '.' : undef;
155               }
156 12           36 return;
157               }
158                
159                
160               sub key {
161 12       12 1 1390 my ( $self, @value ) = @_;
162 12 100         54 return MIME::Base64::encode( $self->keybin(), "" ) unless scalar @value;
163 8           45 return $self->keybin( MIME::Base64::decode( join "", @value ) );
164               }
165                
166                
167               sub keybin {
168 75       75 1 1104 my ( $self, @value ) = @_;
169 75           124 for (@value) { $self->{keybin} = $_ }
  37           92  
170 75     100     318 return $self->{keybin} || "";
171               }
172                
173                
174 2       2 1 967 sub pubkey { return &key; }
175                
176                
177               my $function = sub { ## sort RRs in numerically ascending order.
178               return $Net::DNS::a->{'preference'} <=> $Net::DNS::b->{'preference'};
179               };
180                
181               __PACKAGE__->set_rrsort_func( 'preference', $function );
182                
183               __PACKAGE__->set_rrsort_func( 'default_sort', $function );
184                
185                
186               1;
187               __END__