~ubuntu-branches/ubuntu/lucid/libnetpacket-perl/lucid

« back to all changes in this revision

Viewing changes to NetPacket/Ethernet.pm

  • Committer: Bazaar Package Importer
  • Author(s): Cajus Pollmeier
  • Date: 2009-06-24 10:11:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090624101100-mysadcke1taprhap
Tags: upstream-0.04
ImportĀ upstreamĀ versionĀ 0.04

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# NetPacket::Ethernet - Decode and encode ethernet packets.
 
3
#
 
4
# Comments/suggestions to tpot@samba.org
 
5
#
 
6
# $Id: Ethernet.pm,v 1.12 2001/07/29 23:45:00 tpot Exp $
 
7
#
 
8
 
 
9
package NetPacket::Ethernet;
 
10
 
 
11
#
 
12
# Copyright (c) 2001 Tim Potter.
 
13
#
 
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)
 
18
#
 
19
# Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of 
 
20
# the  participants in the CRC for Advanced Computational Systems
 
21
# ('ACSys').
 
22
#
 
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.
 
26
#
 
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.
 
32
#
 
33
 
 
34
use strict;
 
35
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
36
 
 
37
my $myclass;
 
38
BEGIN {
 
39
    $myclass = __PACKAGE__;
 
40
    $VERSION = "0.04";
 
41
}
 
42
sub Version () { "$myclass v$VERSION" }
 
43
 
 
44
BEGIN {
 
45
    @ISA = qw(Exporter NetPacket);
 
46
 
 
47
# Items to export into callers namespace by default
 
48
# (move infrequently used names to @EXPORT_OK below)
 
49
 
 
50
    @EXPORT = qw(
 
51
    );
 
52
 
 
53
# Other items we are prepared to export if requested
 
54
 
 
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
 
58
    );
 
59
 
 
60
# Tags:
 
61
 
 
62
    %EXPORT_TAGS = (
 
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)],
 
67
);
 
68
 
 
69
}
 
70
 
 
71
#
 
72
# Partial list of ethernet protocol types from
 
73
# http://www.isi.edu/in-notes/iana/assignments/ethernet-numbers
 
74
#
 
75
 
 
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;
 
83
 
 
84
#
 
85
# Decode the packet
 
86
#
 
87
 
 
88
sub decode {
 
89
    my $class = shift;
 
90
    my($pkt, $parent, @rest) = @_;
 
91
    my $self = {};
 
92
 
 
93
    # Class fields
 
94
 
 
95
    $self->{_parent} = $parent;
 
96
    $self->{_frame} = $pkt;
 
97
 
 
98
    # Decode ethernet packet
 
99
 
 
100
    if (defined($pkt)) {
 
101
 
 
102
        my($sm_lo, $sm_hi, $dm_lo, $dm_hi);
 
103
 
 
104
        ($dm_hi, $dm_lo, $sm_hi, $sm_lo, $self->{type}, $self->{data}) = 
 
105
            unpack('NnNnna*' , $pkt);
 
106
 
 
107
        # Convert MAC addresses to hex string to avoid representation
 
108
        # problems
 
109
 
 
110
        $self->{src_mac} = sprintf("%08x%04x", $sm_hi, $sm_lo);
 
111
        $self->{dest_mac} = sprintf("%08x%04x", $dm_hi, $dm_lo);
 
112
    }
 
113
 
 
114
    # Return a blessed object
 
115
 
 
116
    bless($self, $class);
 
117
    return $self;
 
118
}
 
119
 
 
120
#
 
121
# Strip header from packet and return the data contained in it
 
122
#
 
123
 
 
124
undef &eth_strip;        # Create eth_strip alias
 
125
*eth_strip = \&strip;
 
126
 
 
127
sub strip {
 
128
    my ($pkt, @rest) = @_;
 
129
 
 
130
    my $eth_obj = NetPacket::Ethernet->decode($pkt);
 
131
    return $eth_obj->{data};
 
132
}   
 
133
 
 
134
#
 
135
# Encode a packet - not implemented!
 
136
#
 
137
 
 
138
sub encode {
 
139
    die("Not implemented");
 
140
}
 
141
 
 
142
#
 
143
# Module initialisation
 
144
#
 
145
 
 
146
1;
 
147
 
 
148
# autoloaded methods go after the END token (&& pod) below
 
149
 
 
150
__END__
 
151
 
 
152
=head1 NAME
 
153
 
 
154
C<NetPacket::Ethernet> - Assemble and disassemble ethernet packets.
 
155
 
 
156
=head1 SYNOPSIS
 
157
 
 
158
  use NetPacket::Ethernet;
 
159
 
 
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);
 
163
 
 
164
=head1 DESCRIPTION
 
165
 
 
166
C<NetPacket::Ethernet> provides a set of routines for assembling and
 
167
disassembling packets using the Ethernet protocol.  
 
168
 
 
169
=head2 Methods
 
170
 
 
171
=over
 
172
 
 
173
=item C<NetPacket::Ethernet-E<gt>decode([RAW PACKET])>
 
174
 
 
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.
 
179
 
 
180
=item C<NetPacket::Ethernet-E<gt>encode(param =E<gt> value)>
 
181
 
 
182
Return an ethernet packet encoded with the instance data specified.
 
183
Not implemented.
 
184
 
 
185
=back
 
186
 
 
187
=head2 Functions
 
188
 
 
189
=over
 
190
 
 
191
=item C<NetPacket::Ethernet::strip([RAW PACKET])>
 
192
 
 
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.
 
196
 
 
197
This function is equivalent to creating an object using the
 
198
C<decode()> constructor and returning the C<data> field of that
 
199
object.
 
200
 
 
201
=back
 
202
 
 
203
=head2 Instance data
 
204
 
 
205
The instance data for the C<NetPacket::Ethernet> object consists of
 
206
the following fields.
 
207
 
 
208
=over
 
209
 
 
210
=item src_mac
 
211
 
 
212
The source MAC address for the ethernet packet as a hex string.
 
213
 
 
214
=item dest_mac
 
215
 
 
216
The destination MAC address for the ethernet packet as a hex string.
 
217
 
 
218
=item type
 
219
 
 
220
The protocol type for the ethernet packet.
 
221
 
 
222
=item data
 
223
 
 
224
The payload for the ethernet packet.
 
225
 
 
226
=back
 
227
 
 
228
=head2 Exports
 
229
 
 
230
=over
 
231
 
 
232
=item default
 
233
 
 
234
none
 
235
 
 
236
=item exportable
 
237
 
 
238
ETH_TYPE_IP ETH_TYPE_ARP ETH_TYPE_APPLETALK ETH_TYPE_SNMP
 
239
ETH_TYPE_IPv6 ETH_TYPE_PPP 
 
240
 
 
241
=item tags
 
242
 
 
243
The following tags group together related exportable items.
 
244
 
 
245
=over
 
246
 
 
247
=item C<:types>
 
248
 
 
249
ETH_TYPE_IP ETH_TYPE_ARP ETH_TYPE_APPLETALK ETH_TYPE_SNMP
 
250
ETH_TYPE_IPv6 ETH_TYPE_PPP 
 
251
 
 
252
=item C<:strip>
 
253
 
 
254
Import the strip function C<eth_strip> which is an alias for
 
255
C<NetPacket::Ethernet::strip>
 
256
 
 
257
=item C<:ALL>
 
258
 
 
259
All the above exportable items.
 
260
 
 
261
=back
 
262
 
 
263
=back
 
264
 
 
265
=head1 EXAMPLE
 
266
 
 
267
The following script dumps ethernet frames by mac address and protocol
 
268
to standard output.
 
269
 
 
270
  #!/usr/bin/perl -w
 
271
 
 
272
  use strict;
 
273
  use Net::PcapUtils;
 
274
  use NetPacket::Ethernet;
 
275
 
 
276
  sub process_pkt {
 
277
      my($arg, $hdr, $pkt) = @_;
 
278
 
 
279
      my $eth_obj = NetPacket::Ethernet->decode($pkt);
 
280
      print("$eth_obj->{src_mac}:$eth_obj->{dest_mac} $eth_obj->{type}\n");
 
281
  }
 
282
 
 
283
  Net::PcapUtils::loop(\&process_pkt);
 
284
 
 
285
=head1 TODO
 
286
 
 
287
=over
 
288
 
 
289
=item Implement C<encode()> function
 
290
 
 
291
=back
 
292
 
 
293
=head1 COPYRIGHT
 
294
 
 
295
  Copyright (c) 2001 Tim Potter.
 
296
 
 
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)
 
301
 
 
302
  Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of 
 
303
  the participants in the CRC for Advanced Computational Systems
 
304
  ('ACSys').
 
305
 
 
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.
 
309
 
 
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.
 
315
 
 
316
=head1 AUTHOR
 
317
 
 
318
Tim Potter E<lt>tpot@samba.orgE<gt>
 
319
 
 
320
=cut
 
321
 
 
322
# any real autoloaded methods go after this line