2
# NetPacket::ICMP -Decode and encode ICMP (Internet Control Message
5
# Comments/suggestions to tpot@samba.org
7
# Encode and checksum by Stephanie Wehner <atrak@itsx.com>
9
# $Id: ICMP.pm,v 1.11 2001/07/29 23:45:00 tpot Exp $
12
package NetPacket::ICMP;
15
# Copyright (c) 2001 Tim Potter.
17
# This package is free software and is provided "as is" without express
18
# or implied warranty. It may be used, redistributed and/or modified
19
# under the terms of the Perl Artistic License (see
20
# http://www.perl.com/perl/misc/Artistic.html)
22
# Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of
23
# the participants in the CRC for Advanced Computational Systems
26
# ACSys makes this software and all associated data and documentation
27
# ('Software') available free of charge. You may make copies of the
28
# Software but you must include all of this notice on any copy.
30
# The Software was developed for research purposes and ACSys does not
31
# warrant that it is error free or fit for any purpose. ACSys
32
# disclaims any liability for all claims, expenses, losses, damages
33
# and costs any user may incur as a result of using, copying or
34
# modifying the Software.
36
# Copyright (c) 2001 Stephanie Wehner
40
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
44
$myclass = __PACKAGE__;
47
sub Version () { "$myclass v$VERSION" }
50
@ISA = qw(Exporter NetPacket);
52
# Items to export into callers namespace by default
53
# (move infrequently used names to @EXPORT_OK below)
58
# Other items we are prepared to export if requested
60
@EXPORT_OK = qw(icmp_strip
61
ICMP_ECHOREPLY ICMP_UNREACH ICMP_SOURCEQUENCH
62
ICMP_REDIRECT ICMP_ECHO ICMP_ROUTERADVERT
63
ICMP_ROUTERSOLICIT ICMP_TIMXCEED ICMP_PARAMPROB
64
ICMP_TSTAMP ICMP_TSTAMPREPLY ICMP_IREQ ICMP_IREQREPLY
65
ICMP_MASREQ ICMP_MASKREPLY
71
ALL => [@EXPORT, @EXPORT_OK],
72
types => [qw(ICMP_ECHOREPLY ICMP_UNREACH ICMP_SOURCEQUENCH
73
ICMP_REDIRECT ICMP_ECHO ICMP_ROUTERADVERT
74
ICMP_ROUTERSOLICIT ICMP_TIMXCEED ICMP_PARAMPROB
75
ICMP_TSTAMP ICMP_TSTAMPREPLY ICMP_IREQ ICMP_IREQREPLY
76
ICMP_MASREQ ICMP_MASKREPLY)],
77
strip => [qw(icmp_strip)],
84
use constant ICMP_ECHOREPLY => 0;
85
use constant ICMP_UNREACH => 3;
86
use constant ICMP_SOURCEQUENCH => 4;
87
use constant ICMP_REDIRECT => 5;
88
use constant ICMP_ECHO => 8;
89
use constant ICMP_ROUTERADVERT => 9;
90
use constant ICMP_ROUTERSOLICIT => 10;
91
use constant ICMP_TIMXCEED => 11;
92
use constant ICMP_PARAMPROB => 12;
93
use constant ICMP_TSTAMP => 13;
94
use constant ICMP_TSTAMPREPLY => 14;
95
use constant ICMP_IREQ => 15;
96
use constant ICMP_IREQREPLY => 16;
97
use constant ICMP_MASKREQ => 17;
98
use constant ICMP_MASKREPLY => 18;
106
my($pkt, $parent, @rest) = @_;
111
$self->{_parent} = $parent;
112
$self->{_frame} = $pkt;
118
($self->{type}, $self->{code}, $self->{cksum}, $self->{data}) =
119
unpack("CCna*", $pkt);
122
# Return a blessed object
124
bless($self, $class);
129
# Strip a packet of its header and return the data
133
*icmpstrip = \&strip;
136
my ($pkt, @rest) = @_;
138
my $icmp_obj = decode($pkt);
139
return $icmp_obj->data;
151
# Checksum the packet
154
# Put the packet together
155
$packet = pack("CCna*", $self->{type}, $self->{code},
156
$self->{cksum}, $self->{data});
162
# Calculate ICMP checksum
169
# Put the packet together for checksumming
171
$packet = pack("CCna*", $self->{type}, $self->{code},
172
$zero, $self->{data});
174
$self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet));
179
# Module initialisation
184
# autoloaded methods go after the END token (&& pod) below
190
C<NetPacket::ICMP> - Assemble and disassemble ICMP (Internet Control
191
Message Protocol) packets.
197
$icmp_obj = NetPacket::ICMP->decode($raw_pkt);
198
$icmp_pkt = NetPacket::ICMP->encode();
199
$icmp_data = NetPacket::ICMP::strip($raw_pkt);
203
C<NetPacket::ICMP> provides a set of routines for assembling and
204
disassembling packets using ICMP (Internet Control Message Protocol).
210
=item C<NetPacket::ICMP-E<gt>decode([RAW PACKET])>
212
Decode the raw packet data given and return an object containing
213
instance data. This method will quite happily decode garbage input.
214
It is the responsibility of the programmer to ensure valid packet data
215
is passed to this method.
217
=item C<NetPacket::ICMP-E<gt>encode()>
219
Return an ICMP packet encoded with the instance data specified.
227
=item C<NetPacket::ICMP::strip([RAW PACKET])>
229
Return the encapsulated data (or payload) contained in the ICMP
236
The instance data for the C<NetPacket::ICMP> object consists of
237
the following fields.
243
The ICMP message type of this packet.
247
The ICMP message code of this packet.
251
The checksum for this packet.
255
The encapsulated data (payload) for this packet.
270
ICMP_ECHOREPLY ICMP_UNREACH ICMP_SOURCEQUENCH
271
ICMP_REDIRECT ICMP_ECHO ICMP_ROUTERADVERT
272
ICMP_ROUTERSOLICIT ICMP_TIMXCEED ICMP_PARAMPROB
273
ICMP_TSTAMP ICMP_TSTAMPREPLY ICMP_IREQ ICMP_IREQREPLY
274
ICMP_MASREQ ICMP_MASKREPLY
279
The following tags group together related exportable items.
285
Import the strip function C<icmp_strip>.
289
All the above exportable items.
301
=item Create constants
309
Copyright (c) 2001 Tim Potter.
311
This package is free software and is provided "as is" without express
312
or implied warranty. It may be used, redistributed and/or modified
313
under the terms of the Perl Artistic License (see
314
http://www.perl.com/perl/misc/Artistic.html)
316
Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of
317
the participants in the CRC for Advanced Computational Systems
320
ACSys makes this software and all associated data and documentation
321
('Software') available free of charge. You may make copies of the
322
Software but you must include all of this notice on any copy.
324
The Software was developed for research purposes and ACSys does not
325
warrant that it is error free or fit for any purpose. ACSys
326
disclaims any liability for all claims, expenses, losses, damages
327
and costs any user may incur as a result of using, copying or
328
modifying the Software.
332
Tim Potter E<lt>tpot@samba.orgE<gt>
334
Stephanie Wehner E<lt>atrak@itsx.comE<gt>
338
# any real autoloaded methods go after this line