2
# NetPacket::Ethernet - Decode and encode ethernet packets.
4
# Comments/suggestions to tpot@samba.org
6
# $Id: Ethernet.pm,v 1.12 2001/07/29 23:45:00 tpot Exp $
9
package NetPacket::Ethernet;
12
# Copyright (c) 2001 Tim Potter.
14
# This package is free software and is provided "as is" without express
15
# or implied warranty. It may be used, redistributed and/or modified
16
# under the terms of the Perl Artistic License (see
17
# http://www.perl.com/perl/misc/Artistic.html)
19
# Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of
20
# the participants in the CRC for Advanced Computational Systems
23
# ACSys makes this software and all associated data and documentation
24
# ('Software') available free of charge. You may make copies of the
25
# Software but you must include all of this notice on any copy.
27
# The Software was developed for research purposes and ACSys does not
28
# warrant that it is error free or fit for any purpose. ACSys
29
# disclaims any liability for all claims, expenses, losses, damages
30
# and costs any user may incur as a result of using, copying or
31
# modifying the Software.
35
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
39
$myclass = __PACKAGE__;
42
sub Version () { "$myclass v$VERSION" }
45
@ISA = qw(Exporter NetPacket);
47
# Items to export into callers namespace by default
48
# (move infrequently used names to @EXPORT_OK below)
53
# Other items we are prepared to export if requested
55
@EXPORT_OK = qw(eth_strip
56
ETH_TYPE_IP ETH_TYPE_ARP ETH_TYPE_APPLETALK
57
ETH_TYPE_SNMP ETH_TYPE_IPv6 ETH_TYPE_PPP
63
ALL => [@EXPORT, @EXPORT_OK],
64
strip => [qw(eth_strip)],
65
types => [qw(ETH_TYPE_IP ETH_TYPE_ARP ETH_TYPE_APPLETALK
66
ETH_TYPE_SNMP ETH_TYPE_IPv6 ETH_TYPE_PPP)],
72
# Partial list of ethernet protocol types from
73
# http://www.isi.edu/in-notes/iana/assignments/ethernet-numbers
76
use constant ETH_TYPE_IP => 0x0800;
77
use constant ETH_TYPE_ARP => 0x0806;
78
use constant ETH_TYPE_APPLETALK => 0x809b;
79
use constant ETH_TYPE_RARP => 0x8035;
80
use constant ETH_TYPE_SNMP => 0x814c;
81
use constant ETH_TYPE_IPv6 => 0x86dd;
82
use constant ETH_TYPE_PPP => 0x880b;
90
my($pkt, $parent, @rest) = @_;
95
$self->{_parent} = $parent;
96
$self->{_frame} = $pkt;
98
# Decode ethernet packet
102
my($sm_lo, $sm_hi, $dm_lo, $dm_hi);
104
($dm_hi, $dm_lo, $sm_hi, $sm_lo, $self->{type}, $self->{data}) =
105
unpack('NnNnna*' , $pkt);
107
# Convert MAC addresses to hex string to avoid representation
110
$self->{src_mac} = sprintf("%08x%04x", $sm_hi, $sm_lo);
111
$self->{dest_mac} = sprintf("%08x%04x", $dm_hi, $dm_lo);
114
# Return a blessed object
116
bless($self, $class);
121
# Strip header from packet and return the data contained in it
124
undef ð_strip; # Create eth_strip alias
125
*eth_strip = \&strip;
128
my ($pkt, @rest) = @_;
130
my $eth_obj = NetPacket::Ethernet->decode($pkt);
131
return $eth_obj->{data};
135
# Encode a packet - not implemented!
139
die("Not implemented");
143
# Module initialisation
148
# autoloaded methods go after the END token (&& pod) below
154
C<NetPacket::Ethernet> - Assemble and disassemble ethernet packets.
158
use NetPacket::Ethernet;
160
$eth_obj = NetPacket::Ethernet->decode($raw_pkt);
161
$eth_pkt = NetPacket::Ethernet->encode(params...); # Not implemented
162
$eth_data = NetPacket::Ethernet::strip($raw_pkt);
166
C<NetPacket::Ethernet> provides a set of routines for assembling and
167
disassembling packets using the Ethernet protocol.
173
=item C<NetPacket::Ethernet-E<gt>decode([RAW PACKET])>
175
Decode the raw packet data given and return an object containing
176
instance data. This method will quite happily decode garbage input.
177
It is the responsibility of the programmer to ensure valid packet data
178
is passed to this method.
180
=item C<NetPacket::Ethernet-E<gt>encode(param =E<gt> value)>
182
Return an ethernet packet encoded with the instance data specified.
191
=item C<NetPacket::Ethernet::strip([RAW PACKET])>
193
Return the encapsulated data (or payload) contained in the ethernet
194
packet. This data is suitable to be used as input for other
195
C<NetPacket::*> modules.
197
This function is equivalent to creating an object using the
198
C<decode()> constructor and returning the C<data> field of that
205
The instance data for the C<NetPacket::Ethernet> object consists of
206
the following fields.
212
The source MAC address for the ethernet packet as a hex string.
216
The destination MAC address for the ethernet packet as a hex string.
220
The protocol type for the ethernet packet.
224
The payload for the ethernet packet.
238
ETH_TYPE_IP ETH_TYPE_ARP ETH_TYPE_APPLETALK ETH_TYPE_SNMP
239
ETH_TYPE_IPv6 ETH_TYPE_PPP
243
The following tags group together related exportable items.
249
ETH_TYPE_IP ETH_TYPE_ARP ETH_TYPE_APPLETALK ETH_TYPE_SNMP
250
ETH_TYPE_IPv6 ETH_TYPE_PPP
254
Import the strip function C<eth_strip> which is an alias for
255
C<NetPacket::Ethernet::strip>
259
All the above exportable items.
267
The following script dumps ethernet frames by mac address and protocol
274
use NetPacket::Ethernet;
277
my($arg, $hdr, $pkt) = @_;
279
my $eth_obj = NetPacket::Ethernet->decode($pkt);
280
print("$eth_obj->{src_mac}:$eth_obj->{dest_mac} $eth_obj->{type}\n");
283
Net::PcapUtils::loop(\&process_pkt);
289
=item Implement C<encode()> function
295
Copyright (c) 2001 Tim Potter.
297
This package is free software and is provided "as is" without express
298
or implied warranty. It may be used, redistributed and/or modified
299
under the terms of the Perl Artistic License (see
300
http://www.perl.com/perl/misc/Artistic.html)
302
Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of
303
the participants in the CRC for Advanced Computational Systems
306
ACSys makes this software and all associated data and documentation
307
('Software') available free of charge. You may make copies of the
308
Software but you must include all of this notice on any copy.
310
The Software was developed for research purposes and ACSys does not
311
warrant that it is error free or fit for any purpose. ACSys
312
disclaims any liability for all claims, expenses, losses, damages
313
and costs any user may incur as a result of using, copying or
314
modifying the Software.
318
Tim Potter E<lt>tpot@samba.orgE<gt>
322
# any real autoloaded methods go after this line