| 1 | # | ||
| 2 | # $Id: SLL.pm,v 1.2.2.1 2006/05/01 17:26:06 gomor Exp $ | ||
| 3 | # | ||
| 4 | package Net::Packet::SLL; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | require Net::Packet::Layer2; | ||
| 9 | our @ISA = qw(Net::Packet::Layer2); | ||
| 10 | |||
| 11 | use Net::Packet::Consts qw(:sll :layer); | ||
| 12 | |||
| 13 | our @AS = qw( | ||
| 14 | packetType | ||
| 15 | addressType | ||
| 16 | addressLength | ||
| 17 | source | ||
| 18 | protocol | ||
| 19 | ); | ||
| 20 | __PACKAGE__->cgBuildIndices; | ||
| 21 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 22 | |||
| 23 | no strict 'vars'; | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $self = shift->SUPER::new( | ||
| 27 | packetType => NP_SLL_PACKET_TYPE_SENT_BY_US, | ||
| 28 | addressType => NP_SLL_ADDRESS_TYPE_512, | ||
| 29 | addressLength => 0, | ||
| 30 | source => 0, | ||
| 31 | protocol => NP_SLL_PROTOCOL_IPv4, | ||
| 32 | @_, | ||
| 33 | ); | ||
| 34 | |||
| 35 | $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub getLength { NP_SLL_HDR_LEN } | ||
| 39 | |||
| 40 | sub pack { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | $self->[$__raw] = $self->SUPER::pack('nnnH16n', | ||
| 44 | $self->[$__packetType], | ||
| 45 | $self->[$__addressType], | ||
| 46 | $self->[$__addressLength], | ||
| 47 | $self->[$__source], | ||
| 48 | $self->[$__protocol], | ||
| 49 | ) or return undef; | ||
| 50 | |||
| 51 | 1; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub unpack { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | my ($pt, $at, $al, $s, $p, $payload) = | ||
| 58 | $self->SUPER::unpack('nnnH16n a*', $self->[$__raw]) | ||
| 59 | or return undef; | ||
| 60 | |||
| 61 | $self->[$__packetType] = $pt; | ||
| 62 | $self->[$__addressType] = $at; | ||
| 63 | $self->[$__addressLength] = $al; | ||
| 64 | $self->[$__source] = $s; | ||
| 65 | $self->[$__protocol] = $p; | ||
| 66 | $self->[$__payload] = $payload; | ||
| 67 | |||
| 68 | 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub encapsulate { | ||
| 72 | my $types = { | ||
| 73 | NP_SLL_PROTOCOL_IPv4() => NP_LAYER_IPv4(), | ||
| 74 | NP_SLL_PROTOCOL_IPv6() => NP_LAYER_IPv6(), | ||
| 75 | }; | ||
| 76 | |||
| 77 | $types->{shift->[$__protocol]} || NP_LAYER_UNKNOWN(); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub print { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | my $l = $self->layer; | ||
| 84 | my $i = $self->is; | ||
| 85 | sprintf "$l:+$i: packetType:0x%04x addressLength:0x%04x protocol:0x%04x\n". | ||
| 86 | "$l: $i: source: %d", | ||
| 87 | $self->packetType, | ||
| 88 | $self->addressLength, | ||
| 89 | $self->protocol, | ||
| 90 | $self->source, | ||
| 91 | ; | ||
| 92 | } | ||
| 93 | |||
| 94 | # | ||
| 95 | # Helpers | ||
| 96 | # | ||
| 97 | |||
| 98 | sub _isProtocol { shift->[$__protocol] == shift() } | ||
| 99 | sub isProtocolIpv4 { shift->_isProtocol(NP_SLL_PROTOCOL_IPv4) } | ||
| 100 | sub isProtocolIpv6 { shift->_isProtocol(NP_SLL_PROTOCOL_IPv6) } | ||
| 101 | sub isProtocolIp { | ||
| 102 | my $self = shift; $self->isProtocolIpv4 || $self->isProtocolIpv6; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||
| 106 | |||
| 107 | __END__ |