Net::Packet::NULL - BSD loopback layer 2 object


Net-Packet documentation Contained in the Net-Packet distribution.

Index


Code Index:

NAME

Top

Net::Packet::NULL - BSD loopback layer 2 object

SYNOPSIS

Top

   #
   # Usually, you do not use this module directly
   #
   use Net::Packet::Consts qw(:null);
   require Net::Packet::NULL;

   # Build a layer
   my $layer = Net::Packet::NULL->new;
   $layer->pack;

   print 'RAW: '.unpack('H*', $layer->raw)."\n";

   # Read a raw layer
   my $layer = Net::Packet::NULL->new(raw => $raw);

   print $layer->print."\n";
   print 'PAYLOAD: '.unpack('H*', $layer->payload)."\n"
      if $layer->payload;

DESCRIPTION

Top

This modules implements the encoding and decoding of the BSD loopback layer.

See also Net::Packet::Layer and Net::Packet::Layer2 for other attributes and methods.

ATTRIBUTES

Top

type

Stores the type of encapsulated layer.

METHODS

Top

new

Object constructor. You can pass attributes that will overwrite default ones. Default values:

type: NP_NULL_TYPE_IPv4

pack

Packs all attributes into a raw format, in order to inject to network. Returns 1 on success, undef otherwise.

unpack

Unpacks raw data from network and stores attributes into the object. Returns 1 on success, undef otherwise.

isTypeIpv4
isTypeIpv6
isTypeIp - is type IPv4 or IPv6

Helper methods. Return true is the encapsulated layer is of specified type, false otherwise.

CONSTANTS

Top

Load them: use Net::Packet::Consts qw(:null);

NP_NULL_HDR_LEN

NULL header length in bytes.

NP_NULL_TYPE_IPv4
NP_NULL_TYPE_IPv6

Various supported encapsulated layer types.

AUTHOR

Top

Patrice <GomoR> Auffret

COPYRIGHT AND LICENSE

Top

RELATED MODULES

Top


Net-Packet documentation Contained in the Net-Packet distribution.

#
# $Id: NULL.pm 1640 2009-11-09 17:58:27Z gomor $
#
package Net::Packet::NULL;
use strict;
use warnings;

require Net::Packet::Layer2;
our @ISA = qw(Net::Packet::Layer2);

use Net::Packet::Consts qw(:null :layer);

our @AS = qw(
   type
);
__PACKAGE__->cgBuildIndices;
__PACKAGE__->cgBuildAccessorsScalar(\@AS);

no strict 'vars';

sub new {
   shift->SUPER::new(
      type => NP_NULL_TYPE_IPv4,
      @_,
   );
}

sub getLength { NP_NULL_HDR_LEN }

sub pack {
   my $self = shift;
   $self->[$__raw] = $self->SUPER::pack('N', $self->[$__type])
      or return undef;
   1;
}

sub unpack {
   my $self = shift;

   my ($type, $payload) = $self->SUPER::unpack('N a*', $self->[$__raw])
      or return undef;

   $self->[$__type]    = $type;
   $self->[$__payload] = $payload;

   1;
}

sub encapsulate {
   my $types = {
      NP_NULL_TYPE_IPv4() => NP_LAYER_IPv4(),
      NP_NULL_TYPE_IPv6() => NP_LAYER_IPv6(),
   };

   $types->{shift->[$__type]} || NP_LAYER_UNKNOWN();
}

sub print {
   my $self = shift;

   my $l = $self->layer;
   my $i = $self->is;
   sprintf "$l:+$i: type:0x%04x", $self->type;
}

#
# Helpers
#

sub _isType    { shift->[$__type] == shift()                              }
sub isTypeIpv4 { shift->_isType(NP_NULL_TYPE_IPv4)                        }
sub isTypeIpv6 { shift->_isType(NP_NULL_TYPE_IPv6)                        }
sub isTypeIp   { my $self = shift; $self->isTypeIpv4 || $self->isTypeIpv6 }

1;

__END__