| 1 | #!/bin/false | ||
| 2 | # ABSTRACT: DHCPv6 packet decoder/encoder | ||
| 3 | # PODNAME: Net::DHCPv6 | ||
| 4 | use strictures 2; | ||
| 5 | |||
| 6 | package Net::DHCPv6; | ||
| 7 | $Net::DHCPv6::VERSION = '0.002'; | ||
| 8 | use Carp qw( croak ); | ||
| 9 | use Net::DHCPv6::DUID; | ||
| 10 | |||
| 11 | # Option classes - loaded so they register in the dispatch tables | ||
| 12 | use Net::DHCPv6::Option::AftrName; | ||
| 13 | use Net::DHCPv6::Option::Auth; | ||
| 14 | use Net::DHCPv6::Option::BootfileParam; | ||
| 15 | use Net::DHCPv6::Option::BootfileUrl; | ||
| 16 | use Net::DHCPv6::Option::CaptivePortal; | ||
| 17 | use Net::DHCPv6::Option::ClientArchType; | ||
| 18 | use Net::DHCPv6::Option::ClientFqdn; | ||
| 19 | use Net::DHCPv6::Option::ClientId; | ||
| 20 | use Net::DHCPv6::Option::ClientLinkLayerAddr; | ||
| 21 | use Net::DHCPv6::Option::DnsServers; | ||
| 22 | use Net::DHCPv6::Option::DomainList; | ||
| 23 | use Net::DHCPv6::Option::ElapsedTime; | ||
| 24 | use Net::DHCPv6::Option::Generic; | ||
| 25 | use Net::DHCPv6::Option::IAAddr; | ||
| 26 | use Net::DHCPv6::Option::IANA; | ||
| 27 | use Net::DHCPv6::Option::IAPD; | ||
| 28 | use Net::DHCPv6::Option::IAPrefix; | ||
| 29 | use Net::DHCPv6::Option::IATA; | ||
| 30 | use Net::DHCPv6::Option::InfMaxRt; | ||
| 31 | use Net::DHCPv6::Option::InfoRefreshTime; | ||
| 32 | use Net::DHCPv6::Option::InterfaceId; | ||
| 33 | use Net::DHCPv6::Option::NewPosixTimezone; | ||
| 34 | use Net::DHCPv6::Option::NewTzdbTimezone; | ||
| 35 | use Net::DHCPv6::Option::NisDomainName; | ||
| 36 | use Net::DHCPv6::Option::NisServers; | ||
| 37 | use Net::DHCPv6::Option::NispDomainName; | ||
| 38 | use Net::DHCPv6::Option::NispServers; | ||
| 39 | use Net::DHCPv6::Option::NtpServer; | ||
| 40 | use Net::DHCPv6::Option::ORO; | ||
| 41 | use Net::DHCPv6::Option::PdExclude; | ||
| 42 | use Net::DHCPv6::Option::SntpServers; | ||
| 43 | use Net::DHCPv6::Option::Preference; | ||
| 44 | use Net::DHCPv6::Option::RapidCommit; | ||
| 45 | use Net::DHCPv6::Option::ReconfAccept; | ||
| 46 | use Net::DHCPv6::Option::ReconfMsg; | ||
| 47 | use Net::DHCPv6::Option::RelayMsg; | ||
| 48 | use Net::DHCPv6::Option::RemoteId; | ||
| 49 | use Net::DHCPv6::Option::RSOO; | ||
| 50 | use Net::DHCPv6::Option::ServerId; | ||
| 51 | use Net::DHCPv6::Option::SipServerA; | ||
| 52 | use Net::DHCPv6::Option::SipServerD; | ||
| 53 | use Net::DHCPv6::Option::MudUrl; | ||
| 54 | use Net::DHCPv6::Option::SolMaxRt; | ||
| 55 | use Net::DHCPv6::Option::StatusCode; | ||
| 56 | use Net::DHCPv6::Option::SubscriberId; | ||
| 57 | use Net::DHCPv6::Option::Unicast; | ||
| 58 | use Net::DHCPv6::Option::UserClass; | ||
| 59 | use Net::DHCPv6::Option::VendorClass; | ||
| 60 | use Net::DHCPv6::Option::VendorOpts; | ||
| 61 | |||
| 62 | # Message classes | ||
| 63 | use Net::DHCPv6::Message::Solicit; | ||
| 64 | use Net::DHCPv6::Message::Advertise; | ||
| 65 | use Net::DHCPv6::Message::Request; | ||
| 66 | use Net::DHCPv6::Message::Confirm; | ||
| 67 | use Net::DHCPv6::Message::Renew; | ||
| 68 | use Net::DHCPv6::Message::Rebind; | ||
| 69 | use Net::DHCPv6::Message::Reply; | ||
| 70 | use Net::DHCPv6::Message::Release; | ||
| 71 | use Net::DHCPv6::Message::Decline; | ||
| 72 | use Net::DHCPv6::Message::Reconfigure; | ||
| 73 | use Net::DHCPv6::Message::InformationRequest; | ||
| 74 | use Net::DHCPv6::Message::RelayForw; | ||
| 75 | use Net::DHCPv6::Message::RelayReply; | ||
| 76 | |||
| 77 | use Net::DHCPv6::OptionList; | ||
| 78 | use Net::DHCPv6::Packet; | ||
| 79 | use namespace::clean; | ||
| 80 | |||
| 81 | my $MIN_LEN = 4; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers) | ||
| 82 | |||
| 83 | # Packet-level decoders | ||
| 84 | sub decode_or_croak { | ||
| 85 | my ( $class, $bytes ) = @_; | ||
| 86 | croak 'No data provided' if !defined $bytes || CORE::length( $bytes ) < $MIN_LEN; | ||
| 87 | return Net::DHCPv6::Packet->from_bytes( $bytes ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub decode_or_null { | ||
| 91 | my ( $class, $bytes ) = @_; | ||
| 92 | return if !defined $bytes || CORE::length( $bytes ) < $MIN_LEN; | ||
| 93 | my $packet; | ||
| 94 | eval { $packet = Net::DHCPv6::Packet->from_bytes( $bytes ); }; | ||
| 95 | return $packet; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub decode_with_error { | ||
| 99 | my ( $class, $bytes ) = @_; | ||
| 100 | my $packet; | ||
| 101 | my $error; | ||
| 102 | if ( !defined $bytes || CORE::length( $bytes ) < $MIN_LEN ) { | ||
| 103 | $error = 'No data provided'; | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | eval { $packet = Net::DHCPv6::Packet->from_bytes( $bytes ); 1 } | ||
| 107 | or $error = $@; | ||
| 108 | } | ||
| 109 | return ( $packet, $error ); | ||
| 110 | } | ||
| 111 | |||
| 112 | # DUID streaming helpers | ||
| 113 | sub decode_duid_with_error { | ||
| 114 | my ( $class, $bytes ) = @_; | ||
| 115 | return Net::DHCPv6::DUID->try_from_bytes( $bytes ); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub decode_duid_or_null { | ||
| 119 | my ( $class, $bytes ) = @_; | ||
| 120 | my ( $duid ) = Net::DHCPv6::DUID->try_from_bytes( $bytes ); | ||
| 121 | return $duid; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub decode_duid_or_croak { | ||
| 125 | my ( $class, $bytes ) = @_; | ||
| 126 | my ( $duid, $error ) = Net::DHCPv6::DUID->try_from_bytes( $bytes ); | ||
| 127 | croak $error if $error; | ||
| 128 | return $duid; | ||
| 129 | } | ||
| 130 | |||
| 131 | # Option-list streaming helpers | ||
| 132 | sub decode_options_with_error { | ||
| 133 | my ( $class, $bytes ) = @_; | ||
| 134 | return Net::DHCPv6::OptionList->try_from_bytes( $bytes ); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub decode_options_or_null { | ||
| 138 | my ( $class, $bytes ) = @_; | ||
| 139 | my ( $ol ) = Net::DHCPv6::OptionList->try_from_bytes( $bytes ); | ||
| 140 | return $ol; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub decode_options_or_croak { | ||
| 144 | my ( $class, $bytes ) = @_; | ||
| 145 | my ( $ol, $error ) = Net::DHCPv6::OptionList->try_from_bytes( $bytes ); | ||
| 146 | croak $error if $error; | ||
| 147 | return $ol; | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ |