| 1 | #!/bin/false | ||
| 2 | # ABSTRACT: NTP Server option (code 56) -- sub-options for NTP configuration per RFC 5908 | ||
| 3 | # PODNAME: Net::DHCPv6::Option::NtpServer | ||
| 4 | use strictures 2; | ||
| 5 | |||
| 6 | package Net::DHCPv6::Option::NtpServer; | ||
| 7 | $Net::DHCPv6::Option::NtpServer::VERSION = '0.002'; | ||
| 8 | use Net::DHCPv6::OptionList; | ||
| 9 | use Carp qw( croak ); | ||
| 10 | use Net::DHCPv6::Constants; | ||
| 11 | use Net::DHCPv6::X::Truncated; | ||
| 12 | use parent 'Net::DHCPv6::Option'; | ||
| 13 | use namespace::clean; | ||
| 14 | my $EMPTY = q(); | ||
| 15 | |||
| 16 | my %SUBOPT_DECODE = ( | ||
| 17 | 1 => sub { my ( $v ) = @_; return { subopt => 1, type => 'address', value => $v } }, | ||
| 18 | 2 => sub { my ( $v ) = @_; return { subopt => 2, type => 'fqdn', value => $v } }, | ||
| 19 | 3 => sub { my ( $v ) = @_; return { subopt => 3, type => 'domain', value => $v } }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ( $class, %args ) = @_; | ||
| 24 | $args{code} = $OPTION_NTP_SERVER; | ||
| 25 | my $entries = $args{entries} // []; | ||
| 26 | $args{data} = | ||
| 27 | join( $EMPTY, map { pack( 'n n', $_->{subopt}, CORE::length( $_->{value} ) ) . $_->{value} } @{$entries} ); | ||
| 28 | my $self = $class->SUPER::new( %args ); | ||
| 29 | $self->{entries} = $entries; | ||
| 30 | return bless $self, $class; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub entries { return shift->{entries} } | ||
| 34 | |||
| 35 | sub from_bytes_inner { | ||
| 36 | my ( $class, $code, $payload ) = @_; | ||
| 37 | my @entries; | ||
| 38 | my $len = CORE::length( $payload ); | ||
| 39 | my $off = 0; | ||
| 40 | while ( $off + 4 <= $len ) { | ||
| 41 | my $sc = unpack( 'n', substr( $payload, $off, 2 ) ); | ||
| 42 | my $slen = unpack( 'n', substr( $payload, $off + 2, 2 ) ); | ||
| 43 | $off += 4; | ||
| 44 | Net::DHCPv6::X::Truncated->throw( message => 'Truncated NtpServer sub-option' ) | ||
| 45 | if $off + $slen > $len; | ||
| 46 | my $sv = substr( $payload, $off, $slen ); | ||
| 47 | $off += $slen; | ||
| 48 | my $decoder = $SUBOPT_DECODE{$sc}; | ||
| 49 | if ( $decoder ) { | ||
| 50 | push @entries, $decoder->( $sv ); | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | push @entries, { subopt => $sc, value => $sv }; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | Net::DHCPv6::X::Truncated->throw( message => 'Truncated NtpServer sub-option header' ) | ||
| 57 | if $off != $len; | ||
| 58 | return $class->new( entries => \@entries ); | ||
| 59 | } | ||
| 60 | |||
| 61 | $Net::DHCPv6::OptionList::OPTION_CLASS{$OPTION_NTP_SERVER} = __PACKAGE__; | ||
| 62 | 1; | ||
| 63 | |||
| 64 | __END__ |