# # $Id: Layer.pm 1640 2009-11-09 17:58:27Z gomor $ # package Net::Packet::Layer; use strict; use warnings; use Carp; require Class::Gomor::Array; our @ISA = qw(Class::Gomor::Array); use Net::Packet::Consts qw(:layer); our @AS = qw( raw payload ); __PACKAGE__->cgBuildIndices; __PACKAGE__->cgBuildAccessorsScalar(\@AS); no strict 'vars'; sub new { my $self = shift->SUPER::new(@_); $self->unpack if $self->raw; $self; } sub is { my $layer = ref(shift); $layer =~ s/^Net::Packet:://; $layer; } sub pack { my $self = shift; my ($fmt, @args) = @_; my $res; eval { $res = CORE::pack($fmt, @args) }; $@ ? do { carp("@{[ref($self)]}: unable to pack structure\n"); undef } : $res; } sub unpack { my $self = shift; my ($fmt, $arg) = @_; my @res; eval { @res = CORE::unpack($fmt, $arg) }; $@ ? do { carp("@{[ref($self)]}: unable to unpack structure\n"); () } : @res; } sub getPayloadLength { my $self = shift; $self->[$__payload] ? length($self->[$__payload]) : 0; } sub layer { NP_LAYER_N_UNKNOWN } sub encapsulate { NP_LAYER_NONE } sub getKey { shift->is } sub getKeyReverse { shift->is } sub computeLengths { 1 } sub computeChecksums { 1 } sub print { "" } sub getLength { 0 } sub dump { my $self = shift; my $hex = CORE::unpack('H*', $self->raw); $hex =~ s/(..)/\\x$1/g; $hex =~ s/\\x$//; sprintf "@{[$self->layer]}:+@{[$self->is]}: \"$hex\""; } sub _isLayer { shift->layer eq shift() } sub isLayer2 { shift->_isLayer(NP_LAYER_N_2) } sub isLayer3 { shift->_isLayer(NP_LAYER_N_3) } sub isLayer4 { shift->_isLayer(NP_LAYER_N_4) } sub isLayer7 { shift->_isLayer(NP_LAYER_N_7) } 1; __END__ =head1 NAME Net::Packet::Layer - base class for all layer modules =head1 DESCRIPTION This is the base class for B, B, B and B modules. It just provides those layers with inheritable attributes and methods. =head1 ATTRIBUTES =over 4 =item B Stores the raw layer (as captured from the network, or packed to send to network). =item B Stores what is not part of the layer, that is the encapsulated part to be decoded by upper layers. =back =head1 METHODS =over 4 =item B Returns the string describing the layer type (example: 'IPv4'). =item B Returns the string describing the layer number (example: 'L3' for IPv4). =item B Returns the next layer type (parsed from payload). This is the same string as returned by B method. =item B =item B Generally, when a layer is built, some attributes are not yet known until the full Net::Packet::Frame is assembled. Those methods computes various lengths and checksums attributes found in a specific layer. Return 1 on success, undef otherwise. =item B Just returns a string in a human readable format describing attributes found in the layer. =item B Just returns a string in hexadecimal format which is how the layer appears on the network. =item B =item B These methods are used to respectively store and retrieve analyzed frames respectively to and from a hashref. This is to make it quick to get possible responses from a probe. =item B Will pack all attributes into raw network format. This method MUST be implemented into each supported layers. =item B Will unpack raw network format to respective attributes. This method MUST be implemented into each supported layers. =item B Returns the layer length in bytes. =item B Returns the total length of remaining raw data in bytes (without calling layer length). =item B =item B =item B =item B Returns true if the calling object is, respectively, layer 2, 3, 4 or 7. =back =head1 CONSTANTS Load them: use Net::Packet::Consts qw(:layer); =over 4 =item B Base layer string. =item B =item B =item B =item B Layer 2 strings. =item B =item B =item B =item B =item B =item B =item B Layer 3 strings. =item B =item B =item B =item B =item B Layer 4 strings. =item B Layer 7 string. =item B =item B Other strings. =item B =item B =item B =item B =item B Layer number N strings. =back =head1 AUTHOR Patrice EGomoRE Auffret =head1 COPYRIGHT AND LICENSE Copyright (c) 2004-2009, Patrice EGomoRE Auffret You may distribute this module under the terms of the Artistic license. See LICENSE.Artistic file in the source distribution archive. =head1 RELATED MODULES L, L, L =cut