DDNS/DHCID/iscdhcp.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 67 CC 4 SCAR 21.5
CC
4
Cov
0%
identifier_type
20.0
init
2.0
digest
2.0
CRAP 8.5
1 package Net::DHCP::DDNS::DHCID::iscdhcp;
2 use warnings;
3 use strict;
4  
5 our @ISA = qw( Net::DHCP::DDNS::DHCID );
6  
7 use Digest::MD5 qw( md5 );
8  
9  
10 ###############################################################################
11 # constructor helper
12  
13 sub init {
14 my $self = shift;
15  
16 $self->{rrtype} = 'TXT';
17  
18 return 1;
19 }
20  
21  
22 ###############################################################################
23 # detail accessors
24  
25 sub identifier_type {
26 my $self = shift;
27  
28 # although we specify four nibbles (0x0000) perl will ignore
29 # the upper insignificant byte, so it is re-prepended below.
30  
31 # 0x0000 - 1 octet hwtype followed by hlen octets of chaddr
32 if ( $self->id_type eq 'HWID' ) {
33 return chr( 0x00 );
34 }
35  
36 # 0x0001 - data octets (type and id fields) from dhcp client identifier
37 if ( $self->id_type eq 'DCID' ) {
38 return chr( 0x31 );
39 # isc erroneously uses 61 (decimal), should be 1
40 # then correctly despite what common/dns.c says, sets
41 # the first nibble to ( ( 61 >> 4 ) & 0xf ), then
42 # erroneously sets the second to 61 % 15, should be % 16
43 # giving a decimal result of 49, or 0x31 hex
44 }
45  
46 # 0x0002 - duid or duid field from dhcp client identifier
47 if ( $self->id_type eq 'DUID' ) {
48 return chr( 0x02 );
49 }
50  
51 return undef;
52 }
53  
54 sub digest {
55 my $self = shift;
56  
57 return md5( $self->idvalue );
58 }
59  
60 sub encode {
61 my ( $self, $raw ) = @_;
62 my $rv = '';
63  
64 for ( my $i = 0; $i < length( $raw ); $i++ ) {
65 my $byte = ord( substr( $raw, $i, 1 ) );
66 my $nib1 = ( $byte >> 4 ) & 0xf;
67 my $nib2 = $byte & 0xf;
68 $rv .= sprintf "%x%x", $nib1, $nib2;
69 }
70  
71 return $rv;
72 }
73  
74  
75 ###############################################################################
76 # ISC dhcpd interim TXT DHCID field, 17 bytes
77  
78 sub form {
79 my $self = shift;
80  
81 my $buf = $self->identifier_type
82 . $self->digest;
83  
84 my $t = new IO::File;
85 $t->open( "> /tmp/dhcid-txt.tmp" )
86 and print $t $buf and $t->close;
87  
88 return $buf;
89 }
90  
91  
92 ###############################################################################
93 1;