1# 2# $Id: SLL.pm 1640 2009-11-09 17:58:27Z gomor $ 3# 4package Net::Packet::SLL; 5use strict; 6use warnings; 7 8require Net::Packet::Layer2; 9our @ISA = qw(Net::Packet::Layer2); 10 11use Net::Packet::Consts qw(:sll :layer); 12 13our @AS = qw( 14 packetType 15 addressType 16 addressLength 17 source 18 protocol 19); 20__PACKAGE__->cgBuildIndices; 21__PACKAGE__->cgBuildAccessorsScalar(\@AS); 22 23no strict 'vars'; 24 25sub new { 26 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 36sub getLength { NP_SLL_HDR_LEN } 37 38sub pack { 39 my $self = shift; 40 41 $self->[$__raw] = $self->SUPER::pack('nnnH16n', 42 $self->[$__packetType], 43 $self->[$__addressType], 44 $self->[$__addressLength], 45 $self->[$__source], 46 $self->[$__protocol], 47 ) or return undef; 48 49 1; 50} 51 52sub unpack { 53 my $self = shift; 54 55 my ($pt, $at, $al, $s, $p, $payload) = 56 $self->SUPER::unpack('nnnH16n a*', $self->[$__raw]) 57 or return undef; 58 59 $self->[$__packetType] = $pt; 60 $self->[$__addressType] = $at; 61 $self->[$__addressLength] = $al; 62 $self->[$__source] = $s; 63 $self->[$__protocol] = $p; 64 $self->[$__payload] = $payload; 65 66 1; 67} 68 69sub encapsulate { 70 my $types = { 71 NP_SLL_PROTOCOL_IPv4() => NP_LAYER_IPv4(), 72 NP_SLL_PROTOCOL_IPv6() => NP_LAYER_IPv6(), 73 NP_SLL_PROTOCOL_ARP() => NP_LAYER_ARP(), 74 NP_SLL_PROTOCOL_VLAN() => NP_LAYER_VLAN(), 75 }; 76 77 $types->{shift->[$__protocol]} || NP_LAYER_UNKNOWN(); 78} 79 80sub print { 81 my $self = shift; 82 83 my $l = $self->layer; 84 my $i = $self->is; 85 sprintf "$l:+$i: packetType:0x%04x addressType:0x%04x ". 86 "addressLength:0x%04x\n". 87 "$l: $i: source:%d protocol:0x%04x", 88 $self->[$__packetType], $self->[$__addressType], 89 $self->[$__addressLength], $self->[$__source], $self->[$__protocol]; 90} 91 92# 93# Helpers 94# 95 96sub _isProtocol { shift->[$__protocol] == shift() } 97sub isProtocolIpv4 { shift->_isProtocol(NP_SLL_PROTOCOL_IPv4) } 98sub isProtocolIpv6 { shift->_isProtocol(NP_SLL_PROTOCOL_IPv6) } 99sub isProtocolIp { 100 my $self = shift; $self->isProtocolIpv4 || $self->isProtocolIpv6; 101} 102 1031; 104 105__END__ 106 107=head1 NAME 108 109Net::Packet::SLL - Linux cooked capture layer 2 object 110 111=head1 SYNOPSIS 112 113 # 114 # Usually, you do not use this module directly 115 # 116 use Net::Packet::Consts qw(:sll); 117 require Net::Packet::SLL; 118 119 #�Build a layer 120 my $layer = Net::Packet::SLL->new; 121 $layer->pack; 122 123 print 'RAW: '.unpack('H*', $layer->raw)."\n"; 124 125 # Read a raw layer 126 my $layer = Net::Packet::SLL->new(raw => $raw); 127 128 print $layer->print."\n"; 129 print 'PAYLOAD: '.unpack('H*', $layer->payload)."\n" 130 if $layer->payload; 131 132=head1 DESCRIPTION 133 134This modules implements the encoding and decoding of the Linux cooked capture layer. 135 136See also B<Net::Packet::Layer> and B<Net::Packet::Layer2> for other attributes and methods. 137 138=head1 ATTRIBUTES 139 140=over 4 141 142=item B<packetType> 143 144Stores the packet type (unicast to us, sent by us ...). 145 146=item B<addressType> 147 148The address type. 149 150=item B<addressLength> 151 152The length of the previously specified address. 153 154=item B<source> 155 156Source address. 157 158=item B<protocol> 159 160Encapsulated protocol. 161 162=back 163 164=head1 METHODS 165 166=over 4 167 168=item B<new> 169 170Object constructor. You can pass attributes that will overwrite default ones. Default values: 171 172packetType: NP_SLL_PACKET_TYPE_SENT_BY_US 173 174addressType: NP_SLL_ADDRESS_TYPE_512 175 176addressLength: 0 177 178source: 0 179 180protocol: NP_SLL_PROTOCOL_IPv4 181 182=item B<pack> 183 184Packs all attributes into a raw format, in order to inject to network. Returns 1 on success, undef otherwise. 185 186=item B<unpack> 187 188Unpacks raw data from network and stores attributes into the object. Returns 1 on success, undef otherwise. 189 190=item B<isProtocolIpv4> 191 192=item B<isProtocolIpv6> 193 194=item B<isProtocolIp> - is type IPv4 or IPv6 195 196Helper methods. Return true is the encapsulated layer is of specified type, false otherwise. 197 198=back 199 200=head1 CONSTANTS 201 202Load them: use Net::Packet::Consts qw(:sll); 203 204=over 4 205 206=item B<NP_SLL_PACKET_TYPE_SENT_BY_US> 207 208=item B<NP_SLL_PACKET_TYPE_UNICAST_TO_US> 209 210Various possible packet types. 211 212=item B<NP_SLL_PROTOCOL_IPv4> 213 214=item B<NP_SLL_PROTOCOL_IPv6> 215 216=item B<NP_SLL_PROTOCOL_ARP> 217 218=item B<NP_SLL_PROTOCOL_VLAN> 219 220Various supported encapsulated layer types. 221 222=item B<NP_SLL_HDR_LEN> 223 224=item B<NP_SLL_ADDRESS_TYPE_512> 225 226=back 227 228=head1 AUTHOR 229 230Patrice E<lt>GomoRE<gt> Auffret 231 232=head1 COPYRIGHT AND LICENSE 233 234Copyright (c) 2004-2009, Patrice E<lt>GomoRE<gt> Auffret 235 236You may distribute this module under the terms of the Artistic license. 237See LICENSE.Artistic file in the source distribution archive. 238 239=head1 RELATED MODULES 240 241L<NetPacket>, L<Net::RawIP>, L<Net::RawSock> 242 243=cut 244