| 1 | # | ||
| 2 | # $Id: VLAN.pm,v 1.2.2.3 2006/06/04 13:37:07 gomor Exp $ | ||
| 3 | # | ||
| 4 | package Net::Packet::VLAN; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | require Net::Packet::Layer3; | ||
| 9 | our @ISA = qw(Net::Packet::Layer3); | ||
| 10 | |||
| 11 | use Net::Packet::Consts qw(:vlan :layer); | ||
| 12 | require Net::Packet::Frame; | ||
| 13 | require Bit::Vector; | ||
| 14 | |||
| 15 | our @AS = qw( | ||
| 16 | priority | ||
| 17 | cfi | ||
| 18 | id | ||
| 19 | type | ||
| 20 | frame | ||
| 21 | ); | ||
| 22 | __PACKAGE__->cgBuildIndices; | ||
| 23 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 24 | |||
| 25 | no strict 'vars'; | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my $self = shift->SUPER::new( | ||
| 29 | priority => 0, | ||
| 30 | cfi => 0, | ||
| 31 | id => 0, | ||
| 32 | type => NP_VLAN_TYPE_IPv4, | ||
| 33 | @_, | ||
| 34 | ); | ||
| 35 | |||
| 36 | $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub getLength { | ||
| 40 | my $self = shift; | ||
| 41 | my $frame = $self->[$__frame]; | ||
| 42 | return(length($frame->raw) + NP_VLAN_HDR_LEN) if $frame; | ||
| 43 | NP_VLAN_HDR_LEN; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub pack { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | my $v3 = Bit::Vector->new_Dec(3, $self->[$__priority]); | ||
| 50 | my $v1 = Bit::Vector->new_Dec(1, $self->[$__cfi]); | ||
| 51 | my $v12 = Bit::Vector->new_Dec(12, $self->[$__id]); | ||
| 52 | my $v16 = $v3->Concat_List($v1, $v12); | ||
| 53 | |||
| 54 | $self->[$__raw] = $self->SUPER::pack('nna*', | ||
| 55 | $v16->to_Dec, | ||
| 56 | $self->[$__type], | ||
| 57 | $self->[$__frame]->raw, | ||
| 58 | ) or return undef; | ||
| 59 | |||
| 60 | 1; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub unpack { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | my ($pCfiId, $type, $payload) = | ||
| 67 | $self->SUPER::unpack('nn a*', $self->[$__raw]) | ||
| 68 | or return undef; | ||
| 69 | |||
| 70 | my $v16 = Bit::Vector->new_Dec(16, $pCfiId); | ||
| 71 | |||
| 72 | $self->[$__priority] = $v16->Chunk_Read(3, 13); | ||
| 73 | $self->[$__cfi] = $v16->Chunk_Read(1, 12); | ||
| 74 | $self->[$__id] = $v16->Chunk_Read(12, 0); | ||
| 75 | $self->[$__type] = $type; | ||
| 76 | $self->[$__frame] = Net::Packet::Frame->new(raw => $payload); | ||
| 77 | |||
| 78 | 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub print { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | my $l = $self->layer; | ||
| 85 | my $i = $self->is; | ||
| 86 | sprintf "$l:+$i: priority:%d cfi:%d id:%d type:0x%04x", | ||
| 87 | $self->priority, $self->cfi, $self->id, $self->type; | ||
| 88 | } | ||
| 89 | |||
| 90 | # | ||
| 91 | # Helpers | ||
| 92 | # | ||
| 93 | |||
| 94 | sub _isType { shift->[$__type] == shift() } | ||
| 95 | sub isTypeArp { shift->_isType(NP_VLAN_TYPE_ARP) } | ||
| 96 | sub isTypeIpv4 { shift->_isType(NP_VLAN_TYPE_IPv4) } | ||
| 97 | sub isTypeIpv6 { shift->_isType(NP_VLAN_TYPE_IPv6) } | ||
| 98 | sub isTypeIp { my $self = shift; $self->isIpv4 || $self->isIpv6 } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | __END__ |