~ubuntu-branches/ubuntu/saucy/libnetpacket-perl/saucy

« back to all changes in this revision

Viewing changes to NetPacket/ICMP.pm

  • Committer: Bazaar Package Importer
  • Author(s): Benoit Mortier
  • Date: 2009-11-21 15:18:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20091121151828-2jq4kk0shmm0ti3e
Tags: 0.41.1-1
New Upstream Version (Closes: #557161)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# NetPacket::ICMP -Decode and encode ICMP (Internet Control Message
3
 
# Protocol) packets.
4
 
#
5
 
# Comments/suggestions to tpot@samba.org
6
 
#
7
 
# Encode and checksum by Stephanie Wehner <atrak@itsx.com>
8
 
#
9
 
# $Id: ICMP.pm,v 1.11 2001/07/29 23:45:00 tpot Exp $
10
 
#
11
 
 
12
 
package NetPacket::ICMP;
13
 
 
14
 
#
15
 
# Copyright (c) 2001 Tim Potter.
16
 
#
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)
21
 
#
22
 
# Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of 
23
 
# the participants in the CRC for Advanced Computational Systems
24
 
# ('ACSys').
25
 
#
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.
29
 
#
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.
35
 
#
36
 
# Copyright (c) 2001 Stephanie Wehner
37
 
#
38
 
 
39
 
use strict;
40
 
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
41
 
 
42
 
my $myclass;
43
 
BEGIN {
44
 
    $myclass = __PACKAGE__;
45
 
    $VERSION = "0.04";
46
 
}
47
 
sub Version () { "$myclass v$VERSION" }
48
 
 
49
 
BEGIN {
50
 
    @ISA = qw(Exporter NetPacket);
51
 
 
52
 
# Items to export into callers namespace by default
53
 
# (move infrequently used names to @EXPORT_OK below)
54
 
 
55
 
    @EXPORT = qw(
56
 
    );
57
 
 
58
 
# Other items we are prepared to export if requested
59
 
 
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
66
 
    );
67
 
 
68
 
# Tags:
69
 
 
70
 
    %EXPORT_TAGS = (
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)],
78
 
);
79
 
 
80
 
}
81
 
 
82
 
# ICMP Types
83
 
 
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;
99
 
 
100
 
#
101
 
# Decode the packet
102
 
#
103
 
 
104
 
sub decode {
105
 
    my $class = shift;
106
 
    my($pkt, $parent, @rest) = @_;
107
 
    my $self = {};
108
 
 
109
 
    # Class fields
110
 
 
111
 
    $self->{_parent} = $parent;
112
 
    $self->{_frame} = $pkt;
113
 
 
114
 
    # Decode ICMP packet
115
 
 
116
 
    if (defined($pkt)) {
117
 
 
118
 
        ($self->{type}, $self->{code}, $self->{cksum}, $self->{data}) =
119
 
            unpack("CCna*", $pkt);
120
 
    }
121
 
 
122
 
    # Return a blessed object
123
 
 
124
 
    bless($self, $class);
125
 
    return $self;
126
 
}
127
 
 
128
 
#
129
 
# Strip a packet of its header and return the data
130
 
#
131
 
 
132
 
undef &icmp_strip;
133
 
*icmpstrip = \&strip;
134
 
 
135
 
sub strip {
136
 
    my ($pkt, @rest) = @_;
137
 
 
138
 
    my $icmp_obj = decode($pkt);
139
 
    return $icmp_obj->data;
140
 
}
141
 
 
142
 
#
143
 
# Encode a packet
144
 
#
145
 
 
146
 
sub encode {
147
 
    my $self = shift;
148
 
    my ($ip) = @_;
149
 
    my ($packet);
150
 
    
151
 
    # Checksum the packet
152
 
    $self->checksum();
153
 
 
154
 
    # Put the packet together
155
 
    $packet = pack("CCna*", $self->{type}, $self->{code}, 
156
 
                $self->{cksum}, $self->{data});
157
 
 
158
 
    return($packet); 
159
 
}
160
 
 
161
 
#
162
 
# Calculate ICMP checksum
163
 
 
164
 
sub checksum {
165
 
    my $self = shift;
166
 
    my ($ip) = @_;
167
 
    my ($packet,$zero);
168
 
 
169
 
    # Put the packet together for checksumming
170
 
    $zero = 0;
171
 
    $packet = pack("CCna*", $self->{type}, $self->{code},
172
 
                $zero, $self->{data});
173
 
 
174
 
    $self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet));
175
 
}
176
 
 
177
 
 
178
 
#
179
 
# Module initialisation
180
 
#
181
 
 
182
 
1;
183
 
 
184
 
# autoloaded methods go after the END token (&& pod) below
185
 
 
186
 
__END__
187
 
 
188
 
=head1 NAME
189
 
 
190
 
C<NetPacket::ICMP> - Assemble and disassemble ICMP (Internet Control
191
 
Message Protocol) packets. 
192
 
 
193
 
=head1 SYNOPSIS
194
 
 
195
 
  use NetPacket::ICMP;
196
 
 
197
 
  $icmp_obj = NetPacket::ICMP->decode($raw_pkt);
198
 
  $icmp_pkt = NetPacket::ICMP->encode();
199
 
  $icmp_data = NetPacket::ICMP::strip($raw_pkt);
200
 
 
201
 
=head1 DESCRIPTION
202
 
 
203
 
C<NetPacket::ICMP> provides a set of routines for assembling and
204
 
disassembling packets using ICMP (Internet Control Message Protocol). 
205
 
 
206
 
=head2 Methods
207
 
 
208
 
=over
209
 
 
210
 
=item C<NetPacket::ICMP-E<gt>decode([RAW PACKET])>
211
 
 
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.
216
 
 
217
 
=item C<NetPacket::ICMP-E<gt>encode()>
218
 
 
219
 
Return an ICMP packet encoded with the instance data specified. 
220
 
 
221
 
=back
222
 
 
223
 
=head2 Functions
224
 
 
225
 
=over
226
 
 
227
 
=item C<NetPacket::ICMP::strip([RAW PACKET])>
228
 
 
229
 
Return the encapsulated data (or payload) contained in the ICMP
230
 
packet.
231
 
 
232
 
=back
233
 
 
234
 
=head2 Instance data
235
 
 
236
 
The instance data for the C<NetPacket::ICMP> object consists of
237
 
the following fields.
238
 
 
239
 
=over
240
 
 
241
 
=item type
242
 
 
243
 
The ICMP message type of this packet.
244
 
 
245
 
=item code
246
 
 
247
 
The ICMP message code of this packet.
248
 
 
249
 
=item cksum
250
 
 
251
 
The checksum for this packet.
252
 
 
253
 
=item data
254
 
 
255
 
The encapsulated data (payload) for this packet.
256
 
 
257
 
=back
258
 
 
259
 
=head2 Exports
260
 
 
261
 
=over
262
 
 
263
 
=item default
264
 
 
265
 
none
266
 
 
267
 
=item exportable
268
 
 
269
 
Icmp message types: 
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
275
 
 
276
 
 
277
 
=item tags
278
 
 
279
 
The following tags group together related exportable items.
280
 
 
281
 
=over
282
 
 
283
 
=item C<:strip>
284
 
 
285
 
Import the strip function C<icmp_strip>.
286
 
 
287
 
=item C<:ALL>
288
 
 
289
 
All the above exportable items.
290
 
 
291
 
=back
292
 
 
293
 
=back
294
 
 
295
 
=head1 EXAMPLE
296
 
 
297
 
=head1 TODO
298
 
 
299
 
=over
300
 
 
301
 
=item Create constants
302
 
 
303
 
=item Write example
304
 
 
305
 
=back
306
 
 
307
 
=head1 COPYRIGHT
308
 
 
309
 
  Copyright (c) 2001 Tim Potter.
310
 
 
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)
315
 
 
316
 
  Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of 
317
 
  the participants in the CRC for Advanced Computational Systems
318
 
  ('ACSys').
319
 
 
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.
323
 
 
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.
329
 
 
330
 
=head1 AUTHOR
331
 
 
332
 
Tim Potter E<lt>tpot@samba.orgE<gt>
333
 
 
334
 
Stephanie Wehner E<lt>atrak@itsx.comE<gt>
335
 
 
336
 
=cut
337
 
 
338
 
# any real autoloaded methods go after this line