DDNS/DHCID/rfc4701.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 72 CC 6 SCAR 21.1
CC
6
Cov
0%
identifier_type
20.0
init
6.0
digest
6.0
CRAP 8.3
1 package Net::DHCP::DDNS::DHCID::rfc4701;
2 use warnings;
3 use strict;
4  
5 our @ISA = qw( Net::DHCP::DDNS::DHCID );
6  
7 use Digest::SHA qw( sha256 );
8  
9  
10 ###############################################################################
11 # constructor helper
12  
13 sub init {
14 my $self = shift;
15  
16 $self->{rrtype} = 'DHCID';
17  
18 unless ( $self->{fqdn} ) {
19 warn "DDNS bug: DHCID requires client FQDN.\n";
20 return undef;
21 }
22  
23 return 1;
24 }
25  
26  
27 ###############################################################################
28 # detail accessors
29  
30 sub identifier_type { # rfc4701 3.3
31 my $self = shift;
32  
33 # 0x0000 - 1 octet hwtype followed by hlen octets of chaddr
34 if ( $self->id_type eq 'HWID' ) {
35 return chr( 0x00 ) . chr( 0x00 );
36 }
37  
38 # 0x0001 - data octets (type and id fields) from dhcp client identifier
39 if ( $self->id_type eq 'DCID' ) {
40 return chr( 0x00 ) . chr( 0x01 );
41 }
42  
43 # 0x0002 - duid or duid field from dhcp client identifier
44 if ( $self->id_type eq 'DUID' ) {
45 return chr( 0x00 ) . chr( 0x02 );
46 }
47  
48 return undef;
49 }
50  
51 sub digest_type { # rfc4701 3.4
52 my $self = shift;
53  
54 # 0x00 - reserved
55 # 0x01 - SHA-256
56  
57 return chr( 0x01 );
58 }
59  
60 sub digest {
61 my $self = shift;
62  
63 if ( $self->digest_type eq chr( 0x01 ) ) {
64 return sha256( $self->idvalue . $self->fqdn );
65 }
66  
67 else {
68 my $t = printf "0x%x", ord( $self->digest_type );
69 warn "DDNS error: unknown DHCID digest type '$t'\n";
70 return undef;
71 }
72  
73 }
74  
75 sub encode {
76 my $self = shift
77  
78 return encode_base64( shift );
79 }
80  
81  
82 ###############################################################################
83 # rfc4701 DHCID field, 35 bytes
84  
85 sub form { # rfc4701 3.5
86 my $self = shift;
87  
88 my $buf = $self->identifier_type
89 . $self->digest_type
90 . $self->digest;
91  
92 my $t = new IO::File;
93 $t->open( "> /tmp/dhcid.tmp" )
94 and print $t $buf and $t->close;
95  
96 return $buf;
97 }
98  
99  
100 ###############################################################################
101 1;