| 1 | # | ||
| 2 | # $Id: Layer.pm,v 1.3.2.2 2006/05/25 12:17:48 gomor Exp $ | ||
| 3 | # | ||
| 4 | package Net::Packet::Layer; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | require Class::Gomor::Array; | ||
| 10 | our @ISA = qw(Class::Gomor::Array); | ||
| 11 | |||
| 12 | use Net::Packet::Consts qw(:layer); | ||
| 13 | |||
| 14 | our @AS = qw( | ||
| 15 | raw | ||
| 16 | payload | ||
| 17 | ); | ||
| 18 | __PACKAGE__->cgBuildIndices; | ||
| 19 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $self = shift->SUPER::new(@_); | ||
| 23 | $self->unpack if $self->raw; | ||
| 24 | $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub is { | ||
| 28 | my $layer = ref(shift); | ||
| 29 | $layer =~ s/^Net::Packet:://; | ||
| 30 | $layer; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub pack { | ||
| 34 | my $self = shift; | ||
| 35 | my ($fmt, @args) = @_; | ||
| 36 | my $res; | ||
| 37 | eval { $res = CORE::pack($fmt, @args) }; | ||
| 38 | $@ ? do { carp("@{[ref($self)]}: unable to pack structure\n"); undef } | ||
| 39 | : $res; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub unpack { | ||
| 43 | my $self = shift; | ||
| 44 | my ($fmt, $arg) = @_; | ||
| 45 | my @res; | ||
| 46 | eval { @res = CORE::unpack($fmt, $arg) }; | ||
| 47 | $@ ? do { carp("@{[ref($self)]}: unable to unpack structure\n"); () } | ||
| 48 | : @res; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub layer { NP_LAYER_N_UNKNOWN } | ||
| 52 | sub encapsulate { NP_LAYER_NONE } | ||
| 53 | sub getKey { shift->is } | ||
| 54 | sub getKeyReverse { shift->is } | ||
| 55 | sub computeLengths { 1 } | ||
| 56 | sub computeChecksums { 1 } | ||
| 57 | sub print { "" } | ||
| 58 | sub getLength { 0 } | ||
| 59 | |||
| 60 | sub dump { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $hex = CORE::unpack('H*', $self->raw); | ||
| 64 | $hex =~ s/(..)/\\x$1/g; | ||
| 65 | $hex =~ s/\\x$//; | ||
| 66 | sprintf "@{[$self->layer]}:+@{[$self->is]}: \"$hex\""; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |