| 1 | #!/usr/bin/false | ||
| 2 | # ABSTRACT: DUID parse/emit and helper constructors | ||
| 3 | # PODNAME: Net::DHCPv6::DUID | ||
| 4 | package Net::DHCPv6::DUID; | ||
| 5 | $Net::DHCPv6::DUID::VERSION = '0.001'; | ||
| 6 | use strictures 2; | ||
| 7 | use Carp qw(croak); | ||
| 8 | use Net::DHCPv6::Constants; | ||
| 9 | use Net::DHCPv6::X::BadDUID; | ||
| 10 | use namespace::clean; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ( $class, %args ) = @_; | ||
| 14 | my $type = $args{duid_type} // croak 'Net::DHCPv6::DUID->new: duid_type is required'; | ||
| 15 | |||
| 16 | my $self = { duid_type => $type }; | ||
| 17 | |||
| 18 | if ( $type == $DUID_LLT ) { | ||
| 19 | croak 'DUID-LLT requires link_layer_type, time, and identifier' | ||
| 20 | unless defined $args{link_layer_type} | ||
| 21 | && defined $args{time} | ||
| 22 | && defined $args{identifier}; | ||
| 23 | $self->{link_layer_type} = $args{link_layer_type}; | ||
| 24 | $self->{time} = $args{time}; | ||
| 25 | $self->{identifier} = $args{identifier}; | ||
| 26 | } | ||
| 27 | elsif ( $type == $DUID_EN ) { | ||
| 28 | croak 'DUID-EN requires enterprise_number and identifier' | ||
| 29 | unless defined $args{enterprise_number} | ||
| 30 | && defined $args{identifier}; | ||
| 31 | $self->{enterprise_number} = $args{enterprise_number}; | ||
| 32 | $self->{identifier} = $args{identifier}; | ||
| 33 | } | ||
| 34 | elsif ( $type == $DUID_LL ) { | ||
| 35 | croak 'DUID-LL requires link_layer_type and identifier' | ||
| 36 | unless defined $args{link_layer_type} | ||
| 37 | && defined $args{identifier}; | ||
| 38 | $self->{link_layer_type} = $args{link_layer_type}; | ||
| 39 | $self->{identifier} = $args{identifier}; | ||
| 40 | } | ||
| 41 | elsif ( $type == $DUID_UUID ) { | ||
| 42 | croak 'DUID-UUID requires identifier (16 bytes)' | ||
| 43 | unless defined $args{identifier}; | ||
| 44 | croak 'DUID-UUID identifier must be 16 bytes' | ||
| 45 | unless CORE::length( $args{identifier} ) == 16; | ||
| 46 | $self->{identifier} = $args{identifier}; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | $self->{identifier} = $args{identifier} // ''; | ||
| 50 | } | ||
| 51 | |||
| 52 | bless $self, $class; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub duid_type { shift->{duid_type} } | ||
| 56 | sub link_layer_type { shift->{link_layer_type} } | ||
| 57 | sub time { shift->{time} } | ||
| 58 | sub enterprise_number { shift->{enterprise_number} } | ||
| 59 | sub identifier { shift->{identifier} } | ||
| 60 | |||
| 61 | sub length { | ||
| 62 | my $self = shift; | ||
| 63 | my $type = $self->{duid_type}; | ||
| 64 | my $id = $self->{identifier} // ''; | ||
| 65 | if ( $type == $DUID_LLT ) { return 2 + 2 + 4 + CORE::length( $id ); } | ||
| 66 | elsif ( $type == $DUID_EN ) { return 2 + 4 + CORE::length( $id ); } | ||
| 67 | elsif ( $type == $DUID_LL ) { return 2 + 2 + CORE::length( $id ); } | ||
| 68 | elsif ( $type == $DUID_UUID ) { return 2 + CORE::length( $id ); } | ||
| 69 | else { return 2 + CORE::length( $id ); } | ||
| 70 | } | ||
| 71 | |||
| 72 | sub as_bytes { | ||
| 73 | my $self = shift; | ||
| 74 | my $type = $self->{duid_type}; | ||
| 75 | my $id = $self->{identifier} // ''; | ||
| 76 | my $buf = pack( 'n', $type ); | ||
| 77 | |||
| 78 | if ( $type == $DUID_LLT ) { | ||
| 79 | $buf .= pack( 'n', $self->{link_layer_type} ); | ||
| 80 | $buf .= pack( 'N', $self->{time} ); | ||
| 81 | $buf .= $id; | ||
| 82 | } | ||
| 83 | elsif ( $type == $DUID_EN ) { | ||
| 84 | $buf .= pack( 'N', $self->{enterprise_number} ); | ||
| 85 | $buf .= $id; | ||
| 86 | } | ||
| 87 | elsif ( $type == $DUID_LL ) { | ||
| 88 | $buf .= pack( 'n', $self->{link_layer_type} ); | ||
| 89 | $buf .= $id; | ||
| 90 | } | ||
| 91 | elsif ( $type == $DUID_UUID ) { | ||
| 92 | $buf .= $id; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | $buf .= $id; | ||
| 96 | } | ||
| 97 | |||
| 98 | return $buf; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub as_string { | ||
| 102 | my $self = shift; | ||
| 103 | my $bytes = $self->as_bytes; | ||
| 104 | my $tname = $Net::DHCPv6::Constants::REV_DUID_TYPE{ $self->{duid_type} } | ||
| 105 | || sprintf( 'TYPE%d', $self->{duid_type} ); | ||
| 106 | return sprintf( '%s:%s', $tname, unpack( 'H*', $bytes ) ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub try_from_bytes { | ||
| 110 | my ( $class, $bytes ) = @_; | ||
| 111 | return ( undef, 'No data provided' ) unless defined $bytes; | ||
| 112 | |||
| 113 | my $len = CORE::length( $bytes ); | ||
| 114 | return ( undef, 'Need at least 2 bytes for DUID type' ) if $len < 2; | ||
| 115 | |||
| 116 | my $type = unpack( 'n', substr( $bytes, 0, 2 ) ); | ||
| 117 | my $rest = substr( $bytes, 2 ); | ||
| 118 | my $rest_len = CORE::length( $rest ); | ||
| 119 | |||
| 120 | if ( $type == $DUID_LLT ) { | ||
| 121 | my $partial = bless( { duid_type => $DUID_LLT }, $class ); | ||
| 122 | my $error; | ||
| 123 | if ( $rest_len >= 2 ) { | ||
| 124 | $partial->{link_layer_type} = unpack( 'n', $rest ); | ||
| 125 | if ( $rest_len >= 6 ) { | ||
| 126 | $partial->{time} = unpack( 'x2 N', $rest ); | ||
| 127 | $partial->{identifier} = substr( $rest, 6 ); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | $error = "Need 6 bytes for DUID-LLT hwtype+time, got $rest_len"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | $error = "Need at least 2 bytes for DUID-LLT hwtype, got $rest_len"; | ||
| 135 | } | ||
| 136 | return ( $partial, $error ); | ||
| 137 | } | ||
| 138 | elsif ( $type == $DUID_EN ) { | ||
| 139 | my $partial = bless( { duid_type => $DUID_EN }, $class ); | ||
| 140 | my $error; | ||
| 141 | if ( $rest_len >= 4 ) { | ||
| 142 | $partial->{enterprise_number} = unpack( 'N', $rest ); | ||
| 143 | $partial->{identifier} = substr( $rest, 4 ); | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | $error = "Need 4 bytes for DUID-EN enterprise_number, got $rest_len"; | ||
| 147 | } | ||
| 148 | return ( $partial, $error ); | ||
| 149 | } | ||
| 150 | elsif ( $type == $DUID_LL ) { | ||
| 151 | my $partial = bless( { duid_type => $DUID_LL }, $class ); | ||
| 152 | my $error; | ||
| 153 | if ( $rest_len >= 2 ) { | ||
| 154 | $partial->{link_layer_type} = unpack( 'n', $rest ); | ||
| 155 | $partial->{identifier} = substr( $rest, 2 ); | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | $error = "Need at least 2 bytes for DUID-LL hwtype, got $rest_len"; | ||
| 159 | } | ||
| 160 | return ( $partial, $error ); | ||
| 161 | } | ||
| 162 | elsif ( $type == $DUID_UUID ) { | ||
| 163 | my $partial = bless( { duid_type => $DUID_UUID }, $class ); | ||
| 164 | my $error; | ||
| 165 | if ( $rest_len >= 16 ) { | ||
| 166 | $partial->{identifier} = substr( $rest, 0, 16 ); | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | $error = "Need 16 bytes for DUID-UUID, got $rest_len"; | ||
| 170 | } | ||
| 171 | return ( $partial, $error ); | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | return ( | ||
| 175 | bless( | ||
| 176 | { | ||
| 177 | duid_type => $type, | ||
| 178 | identifier => $rest, | ||
| 179 | }, | ||
| 180 | $class | ||
| 181 | ), | ||
| 182 | undef | ||
| 183 | ); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | sub from_bytes { | ||
| 188 | my ( $class, $bytes ) = @_; | ||
| 189 | my ( $duid, $error ) = $class->try_from_bytes( $bytes ); | ||
| 190 | Net::DHCPv6::X::BadDUID->throw( message => $error ) if $error; | ||
| 191 | return $duid; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub new_llt { | ||
| 195 | my ( $class, $link_layer_type, $time, $mac_bytes ) = @_; | ||
| 196 | return $class->new( | ||
| 197 | duid_type => $DUID_LLT, | ||
| 198 | link_layer_type => $link_layer_type, | ||
| 199 | time => $time, | ||
| 200 | identifier => $mac_bytes, | ||
| 201 | ); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub new_en { | ||
| 205 | my ( $class, $enterprise_number, $identifier ) = @_; | ||
| 206 | return $class->new( | ||
| 207 | duid_type => $DUID_EN, | ||
| 208 | enterprise_number => $enterprise_number, | ||
| 209 | identifier => $identifier, | ||
| 210 | ); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub new_ll { | ||
| 214 | my ( $class, $link_layer_type, $mac_bytes ) = @_; | ||
| 215 | return $class->new( | ||
| 216 | duid_type => $DUID_LL, | ||
| 217 | link_layer_type => $link_layer_type, | ||
| 218 | identifier => $mac_bytes, | ||
| 219 | ); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub new_uuid { | ||
| 223 | my ( $class, $uuid_bytes ) = @_; | ||
| 224 | return $class->new( | ||
| 225 | duid_type => $DUID_UUID, | ||
| 226 | identifier => $uuid_bytes, | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | |||
| 230 | 1; | ||
| 231 | |||
| 232 | __END__ |