Net-DHCPv6

 view release on metacpan or  search on metacpan

lib/Net/DHCPv6.pm  view on Meta::CPAN

use Net::DHCPv6::Option::SipServerD          ();
use Net::DHCPv6::Option::MudUrl              ();
use Net::DHCPv6::Option::SolMaxRt            ();
use Net::DHCPv6::Option::StatusCode          ();
use Net::DHCPv6::Option::SubscriberId        ();
use Net::DHCPv6::Option::Unicast             ();
use Net::DHCPv6::Option::UserClass           ();
use Net::DHCPv6::Option::VendorClass         ();
use Net::DHCPv6::Option::VendorOpts          ();

# Message classes
use Net::DHCPv6::Message::Solicit            ();
use Net::DHCPv6::Message::Advertise          ();
use Net::DHCPv6::Message::Request            ();
use Net::DHCPv6::Message::Confirm            ();
use Net::DHCPv6::Message::Renew              ();
use Net::DHCPv6::Message::Rebind             ();
use Net::DHCPv6::Message::Reply              ();
use Net::DHCPv6::Message::Release            ();
use Net::DHCPv6::Message::Decline            ();
use Net::DHCPv6::Message::Reconfigure        ();
use Net::DHCPv6::Message::InformationRequest ();
use Net::DHCPv6::Message::RelayForw          ();
use Net::DHCPv6::Message::RelayReply         ();

use Net::DHCPv6::OptionList ();
use Net::DHCPv6::Packet     ();
use namespace::clean;

my $MIN_LEN = 4;    ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)

# Packet-level decoders
sub decode_or_croak {
    my ( $class, $bytes ) = @_;
    croak 'No data provided' if !defined $bytes || CORE::length( $bytes ) < $MIN_LEN;
    return Net::DHCPv6::Packet->from_bytes( $bytes );
}

sub decode_or_null {
    my ( $class, $bytes ) = @_;
    return if !defined $bytes || CORE::length( $bytes ) < $MIN_LEN;
    my $packet;
    eval { $packet = Net::DHCPv6::Packet->from_bytes( $bytes ); 1 } or return;
    return $packet;
}

sub decode_with_error {
    my ( $class, $bytes ) = @_;
    my $packet;
    my $error;
    if ( !defined $bytes || CORE::length( $bytes ) < $MIN_LEN ) {
        $error = 'No data provided';
    }
    else {
        eval { $packet = Net::DHCPv6::Packet->from_bytes( $bytes ); 1 }
            or $error = $@;
    }
    return ( $packet, $error );
}

# DUID streaming helpers
sub decode_duid_with_error {
    my ( $class, $bytes ) = @_;
    return Net::DHCPv6::DUID->try_from_bytes( $bytes );
}

sub decode_duid_or_null {
    my ( $class, $bytes ) = @_;
    my ( $duid ) = Net::DHCPv6::DUID->try_from_bytes( $bytes );
    return $duid;
}

sub decode_duid_or_croak {
    my ( $class, $bytes ) = @_;
    my ( $duid,  $error ) = Net::DHCPv6::DUID->try_from_bytes( $bytes );
    croak $error if $error;
    return $duid;
}

# Option-list streaming helpers
sub decode_options_with_error {
    my ( $class, $bytes ) = @_;
    return Net::DHCPv6::OptionList->try_from_bytes( $bytes );
}

sub decode_options_or_null {
    my ( $class, $bytes ) = @_;
    my ( $ol ) = Net::DHCPv6::OptionList->try_from_bytes( $bytes );
    return $ol;
}

sub decode_options_or_croak {
    my ( $class, $bytes ) = @_;
    my ( $ol,    $error ) = Net::DHCPv6::OptionList->try_from_bytes( $bytes );
    croak $error if $error;
    return $ol;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Net::DHCPv6 - DHCPv6 packet decoder/encoder

=head1 VERSION

version 0.003

=head1 SYNOPSIS

  use Net::DHCPv6;

  my $bytes  = ...;  # wire bytes from socket
  my $packet = Net::DHCPv6->decode_or_croak($bytes);
  print $packet->type;         # SOLICIT
  print $packet->msg_type;     # 1
  print $packet->transaction_id;
  my $cid = $packet->get_option(1);
  print $cid->duid->duid_type;

  # Tolerant parsing
  if (my $pkt = Net::DHCPv6->decode_or_null($bytes)) {
      ...
  }

  # Inspect-mode parsing
  my ($pkt, $err) = Net::DHCPv6->decode_with_error($bytes);
  if ($err) { warn $err->message; }

=head1 DESCRIPTION

Top-level module for the Net::DHCPv6 library. Provides three entry
points for decoding DHCPv6 wire bytes with different error-handling
behaviours. Also loads all sub-modules so a single C<use Net::DHCPv6>



( run in 1.513 second using v1.01-cache-2.11-cpan-140bd7fdf52 )