line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package NetPacket::ICMP; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
3
|
|
|
3
|
|
26488
|
$NetPacket::ICMP::AUTHORITY = 'cpan:YANICK'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Assemble and disassemble ICMP (Internet Control Message Protocol) packets. |
6
|
|
|
|
|
|
|
$NetPacket::ICMP::VERSION = '1.5.0'; |
7
|
3
|
|
|
3
|
|
21
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
105
|
|
8
|
3
|
|
|
3
|
|
16
|
use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
3
|
|
|
|
|
83
|
|
|
3
|
|
|
|
|
465
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
BEGIN { |
12
|
3
|
|
|
3
|
|
68
|
@ISA = qw(Exporter NetPacket); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Items to export into callers namespace by default |
15
|
|
|
|
|
|
|
# (move infrequently used names to @EXPORT_OK below) |
16
|
|
|
|
|
|
|
|
17
|
3
|
|
|
|
|
11
|
@EXPORT = qw( |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Other items we are prepared to export if requested |
21
|
|
|
|
|
|
|
|
22
|
3
|
|
|
|
|
11
|
@EXPORT_OK = qw(icmp_strip |
23
|
|
|
|
|
|
|
ICMP_ECHOREPLY ICMP_UNREACH ICMP_SOURCEQUENCH |
24
|
|
|
|
|
|
|
ICMP_REDIRECT ICMP_ECHO ICMP_ROUTERADVERT |
25
|
|
|
|
|
|
|
ICMP_ROUTERSOLICIT ICMP_TIMXCEED ICMP_PARAMPROB |
26
|
|
|
|
|
|
|
ICMP_TSTAMP ICMP_TSTAMPREPLY ICMP_IREQ ICMP_IREQREPLY |
27
|
|
|
|
|
|
|
ICMP_MASKREQ ICMP_MASKREPLY |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Tags: |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
105
|
%EXPORT_TAGS = ( |
33
|
|
|
|
|
|
|
ALL => [@EXPORT, @EXPORT_OK], |
34
|
|
|
|
|
|
|
types => [qw(ICMP_ECHOREPLY ICMP_UNREACH ICMP_SOURCEQUENCH |
35
|
|
|
|
|
|
|
ICMP_REDIRECT ICMP_ECHO ICMP_ROUTERADVERT |
36
|
|
|
|
|
|
|
ICMP_ROUTERSOLICIT ICMP_TIMXCEED ICMP_PARAMPROB |
37
|
|
|
|
|
|
|
ICMP_TSTAMP ICMP_TSTAMPREPLY ICMP_IREQ ICMP_IREQREPLY |
38
|
|
|
|
|
|
|
ICMP_MASKREQ ICMP_MASKREPLY)], |
39
|
|
|
|
|
|
|
strip => [qw(icmp_strip)], |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# ICMP Types |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
3
|
|
18
|
use constant ICMP_ECHOREPLY => 0; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
279
|
|
47
|
3
|
|
|
3
|
|
17
|
use constant ICMP_UNREACH => 3; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
124
|
|
48
|
3
|
|
|
3
|
|
16
|
use constant ICMP_SOURCEQUENCH => 4; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
119
|
|
49
|
3
|
|
|
3
|
|
23
|
use constant ICMP_REDIRECT => 5; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
134
|
|
50
|
3
|
|
|
3
|
|
14
|
use constant ICMP_ECHO => 8; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
119
|
|
51
|
3
|
|
|
3
|
|
13
|
use constant ICMP_ROUTERADVERT => 9; |
|
3
|
|
|
|
|
29
|
|
|
3
|
|
|
|
|
143
|
|
52
|
3
|
|
|
3
|
|
14
|
use constant ICMP_ROUTERSOLICIT => 10; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
123
|
|
53
|
3
|
|
|
3
|
|
13
|
use constant ICMP_TIMXCEED => 11; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
120
|
|
54
|
3
|
|
|
3
|
|
13
|
use constant ICMP_PARAMPROB => 12; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
135
|
|
55
|
3
|
|
|
3
|
|
15
|
use constant ICMP_TSTAMP => 13; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
113
|
|
56
|
3
|
|
|
3
|
|
14
|
use constant ICMP_TSTAMPREPLY => 14; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
129
|
|
57
|
3
|
|
|
3
|
|
15
|
use constant ICMP_IREQ => 15; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
3198
|
|
58
|
3
|
|
|
3
|
|
24
|
use constant ICMP_IREQREPLY => 16; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
220
|
|
59
|
3
|
|
|
3
|
|
15
|
use constant ICMP_MASKREQ => 17; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
718
|
|
60
|
3
|
|
|
3
|
|
21
|
use constant ICMP_MASKREPLY => 18; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1259
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# |
63
|
|
|
|
|
|
|
# Decode the packet |
64
|
|
|
|
|
|
|
# |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub decode { |
67
|
1
|
|
|
1
|
1
|
14
|
my $class = shift; |
68
|
1
|
|
|
|
|
3
|
my($pkt, $parent) = @_; |
69
|
1
|
|
|
|
|
2
|
my $self = {}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Class fields |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
3
|
$self->{_parent} = $parent; |
74
|
1
|
|
|
|
|
2
|
$self->{_frame} = $pkt; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Decode ICMP packet |
77
|
|
|
|
|
|
|
|
78
|
1
|
50
|
|
|
|
5
|
if (defined($pkt)) { |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
6
|
($self->{type}, $self->{code}, $self->{cksum}, $self->{data}) = |
81
|
|
|
|
|
|
|
unpack("CCna*", $pkt); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Return a blessed object |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
3
|
bless($self, $class); |
87
|
1
|
|
|
|
|
3
|
return $self; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# |
91
|
|
|
|
|
|
|
# Strip a packet of its header and return the data |
92
|
|
|
|
|
|
|
# |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
undef &icmp_strip; |
95
|
|
|
|
|
|
|
*icmpstrip = \&strip; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub strip { |
98
|
0
|
|
|
0
|
1
|
0
|
my ($pkt) = @_; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
my $icmp_obj = decode($pkt); |
101
|
0
|
|
|
|
|
0
|
return $icmp_obj->{data}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# |
105
|
|
|
|
|
|
|
# Encode a packet |
106
|
|
|
|
|
|
|
# |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub encode { |
109
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
110
|
0
|
|
|
|
|
0
|
my ($ip) = @_; |
111
|
0
|
|
|
|
|
0
|
my ($packet); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Checksum the packet |
114
|
0
|
|
|
|
|
0
|
$self->checksum(); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Put the packet together |
117
|
0
|
|
|
|
|
0
|
$packet = pack("CCna*", $self->{type}, $self->{code}, |
118
|
|
|
|
|
|
|
$self->{cksum}, $self->{data}); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
0
|
return($packet); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# |
124
|
|
|
|
|
|
|
# Calculate ICMP checksum |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub checksum { |
127
|
1
|
|
|
1
|
0
|
916
|
my $self = shift; |
128
|
1
|
|
|
|
|
3
|
my ($ip) = @_; |
129
|
1
|
|
|
|
|
2
|
my ($packet,$zero); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Put the packet together for checksumming |
132
|
1
|
|
|
|
|
1
|
$zero = 0; |
133
|
1
|
|
|
|
|
10
|
$packet = pack("CCna*", $self->{type}, $self->{code}, |
134
|
|
|
|
|
|
|
$zero, $self->{data}); |
135
|
|
|
|
|
|
|
|
136
|
1
|
|
|
|
|
7
|
$self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet)); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# |
141
|
|
|
|
|
|
|
# Module initialisation |
142
|
|
|
|
|
|
|
# |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# autoloaded methods go after the END token (&& pod) below |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=pod |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 NAME |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
NetPacket::ICMP - Assemble and disassemble ICMP (Internet Control Message Protocol) packets. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 VERSION |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
version 1.5.0 |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 SYNOPSIS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
use NetPacket::ICMP; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$icmp_obj = NetPacket::ICMP->decode($raw_pkt); |
163
|
|
|
|
|
|
|
$icmp_pkt = NetPacket::ICMP->encode(); |
164
|
|
|
|
|
|
|
$icmp_data = NetPacket::ICMP::strip($raw_pkt); |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 DESCRIPTION |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
C provides a set of routines for assembling and |
169
|
|
|
|
|
|
|
disassembling packets using ICMP (Internet Control Message Protocol). |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 Methods |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=over |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item Cdecode([RAW PACKET])> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Decode the raw packet data given and return an object containing |
178
|
|
|
|
|
|
|
instance data. This method will quite happily decode garbage input. |
179
|
|
|
|
|
|
|
It is the responsibility of the programmer to ensure valid packet data |
180
|
|
|
|
|
|
|
is passed to this method. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item Cencode()> |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Return an ICMP packet encoded with the instance data specified. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=back |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 Functions |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=over |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item C |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Return the encapsulated data (or payload) contained in the ICMP |
195
|
|
|
|
|
|
|
packet. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=back |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 Instance data |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
The instance data for the C object consists of |
202
|
|
|
|
|
|
|
the following fields. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=over |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item type |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
The ICMP message type of this packet. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item code |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
The ICMP message code of this packet. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item cksum |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
The checksum for this packet. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item data |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
The encapsulated data (payload) for this packet. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=back |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 Exports |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=over |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item default |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
none |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item exportable |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
ICMP message types: |
235
|
|
|
|
|
|
|
ICMP_ECHOREPLY ICMP_UNREACH ICMP_SOURCEQUENCH |
236
|
|
|
|
|
|
|
ICMP_REDIRECT ICMP_ECHO ICMP_ROUTERADVERT |
237
|
|
|
|
|
|
|
ICMP_ROUTERSOLICIT ICMP_TIMXCEED ICMP_PARAMPROB |
238
|
|
|
|
|
|
|
ICMP_TSTAMP ICMP_TSTAMPREPLY ICMP_IREQ ICMP_IREQREPLY |
239
|
|
|
|
|
|
|
ICMP_MASKREQ ICMP_MASKREPLY |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=item tags |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
The following tags group together related exportable items. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=over |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item C<:types> |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
ICMP_ECHOREPLY ICMP_UNREACH ICMP_SOURCEQUENCH |
250
|
|
|
|
|
|
|
ICMP_REDIRECT ICMP_ECHO ICMP_ROUTERADVERT |
251
|
|
|
|
|
|
|
ICMP_ROUTERSOLICIT ICMP_TIMXCEED ICMP_PARAMPROB |
252
|
|
|
|
|
|
|
ICMP_TSTAMP ICMP_TSTAMPREPLY ICMP_IREQ |
253
|
|
|
|
|
|
|
ICMP_IREQREPLY ICMP_MASKREQ ICMP_MASKREPLY |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item C<:strip> |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
Import the strip function C. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item C<:ALL> |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
All the above exportable items. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=back |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=back |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head1 EXAMPLE |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
The following example prints the ICMP type, code, and checksum |
270
|
|
|
|
|
|
|
fields. |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
use strict; |
275
|
|
|
|
|
|
|
use Net::PcapUtils; |
276
|
|
|
|
|
|
|
use NetPacket::Ethernet qw(:strip); |
277
|
|
|
|
|
|
|
use NetPacket::IP qw(:strip); |
278
|
|
|
|
|
|
|
use NetPacket::ICMP; |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub process_pkt { |
281
|
|
|
|
|
|
|
my ($user, $hdr, $pkt) = @_; |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
my $ip_obj = NetPacket::IP->decode(eth_strip($pkt)); |
284
|
|
|
|
|
|
|
my $icmp_obj = NetPacket::ICMP->decode(ip_strip($ip_obj)); |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
print("Type: $icmp_obj->{type}\n"); |
287
|
|
|
|
|
|
|
print("Code: $icmp_obj->{code}\n"); |
288
|
|
|
|
|
|
|
print("Checksum: $icmp_obj->{cksum}\n\n"); |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Net::PcapUtils::loop(\&process_pkt, FILTER => 'icmp'); |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head1 TODO |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=over |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=item Create constants |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=back |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=head1 COPYRIGHT |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
Copyright (c) 2001 Tim Potter and Stephanie Wehner. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of |
306
|
|
|
|
|
|
|
the participants in the CRC for Advanced Computational Systems |
307
|
|
|
|
|
|
|
('ACSys'). |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
This module is free software. You can redistribute it and/or |
310
|
|
|
|
|
|
|
modify it under the terms of the Artistic License 2.0. |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
313
|
|
|
|
|
|
|
but without any warranty; without even the implied warranty of |
314
|
|
|
|
|
|
|
merchantability or fitness for a particular purpose. |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head1 AUTHOR |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Tim Potter Etpot@samba.orgE |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
Stephanie Wehner Eatrak@itsx.comE |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=cut |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
__END__ |