Packet/ICMPv4.pm untested

filter: Statementstmt 0.0% 0 / 168 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 59 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 47 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 354 CC 22 SCAR 34.2
CC
22
Cov
0%
recv
156.0
new
12.0
getDataLength
6.0
CRAP 30.6
1 #
2 # $Id: ICMPv4.pm,v 1.3.2.4 2006/06/04 13:36:37 gomor Exp $
3 #
4 package Net::Packet::ICMPv4;
5 use strict;
6 use warnings;
7 use Carp;
8  
9 require Net::Packet::Layer4;
10 our @ISA = qw(Net::Packet::Layer4);
11  
12 use Net::Packet::Utils qw(getRandom16bitsInt getRandom32bitsInt inetChecksum
13 inetAton inetNtoa);
14 use Net::Packet::Consts qw(:icmpv4 :layer);
15 require Net::Packet::IPv4;
16 require Net::Packet::Frame;
17  
18 our @AS = qw(
19 type
20 code
21 checksum
22 identifier
23 sequenceNumber
24 originateTimestamp
25 receiveTimestamp
26 transmitTimestamp
27 addressMask
28 gateway
29 unused
30 error
31 data
32 );
33 __PACKAGE__->cgBuildIndices;
34 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
35  
36 no strict 'vars';
37  
38 sub new {
39 my $self = shift->SUPER::new(
40 type => NP_ICMPv4_TYPE_ECHO_REQUEST,
41 code => NP_ICMPv4_CODE_ZERO,
42 checksum => 0,
43 identifier => getRandom16bitsInt(),
44 sequenceNumber => getRandom16bitsInt(),
45 originateTimestamp => time(),
46 receiveTimestamp => 0,
47 transmitTimestamp => 0,
48 addressMask => 0,
49 gateway => "127.0.0.1",
50 unused => 0,
51 data => "",
52 @_,
53 );
54  
55 unless ($self->[$__raw]) {
56 # This line handles the packing of ICMPv4 Destination unreach IPv4 Frame
57 my $error = $self->[$__error];
58 $self->[$__data] = $error->raw if $error;
59 }
60  
61 $self;
62 }
63  
64 sub getKey { 'ICMP' }
65 sub getKeyReverse { 'ICMP' }
66  
67 sub recv {
68 my $self = shift;
69 my ($frame) = @_;
70  
71 my $env = $frame->env;
72  
73 for ($env->dump->frames) {
74 next unless $_->timestamp ge $frame->timestamp;
75  
76 if ($frame->l3) {
77 if ($_->isIcmpv4 && $_->l3->src eq $frame->l3->dst) {
78 if ($self->[$__type] == NP_ICMPv4_TYPE_ECHO_REQUEST
79 && $_->l4->type == NP_ICMPv4_TYPE_ECHO_REPLY) {
80 return $_;
81 }
82 elsif ($self->[$__type] == NP_ICMPv4_TYPE_TIMESTAMP_REQUEST
83 && $_->l4->type == NP_ICMPv4_TYPE_TIMESTAMP_REPLY) {
84 return $_;
85 }
86 elsif ($self->[$__type] == NP_ICMPv4_TYPE_INFORMATION_REQUEST
87 && $_->l4->type == NP_ICMPv4_TYPE_INFORMATION_REPLY) {
88 return $_;
89 }
90 elsif ($self->[$__type] == NP_ICMPv4_TYPE_ADDRESS_MASK_REQUEST
91 && $_->l4->type == NP_ICMPv4_TYPE_ADDRESS_MASK_REPLY) {
92 return $_;
93 }
94 }
95 }
96 # DescL4 recv, warning, it may receive a packet targetted at another
97 # host, since no L3 headers is kept at D4 for packet matching
98 else {
99 if ($self->[$__type] == NP_ICMPv4_TYPE_ECHO_REQUEST
100 && $_->l4->type == NP_ICMPv4_TYPE_ECHO_REPLY) {
101 return $_;
102 }
103 elsif ($self->[$__type] == NP_ICMPv4_TYPE_TIMESTAMP_REQUEST
104 && $_->l4->type == NP_ICMPv4_TYPE_TIMESTAMP_REPLY) {
105 return $_;
106 }
107 elsif ($self->[$__type] == NP_ICMPv4_TYPE_INFORMATION_REQUEST
108 && $_->l4->type == NP_ICMPv4_TYPE_INFORMATION_REPLY) {
109 return $_;
110 }
111 elsif ($self->[$__type] == NP_ICMPv4_TYPE_ADDRESS_MASK_REQUEST
112 && $_->l4->type == NP_ICMPv4_TYPE_ADDRESS_MASK_REPLY) {
113 return $_;
114 }
115 }
116 }
117  
118 undef;
119 }
120  
121 my $packTypes = {
122 NP_ICMPv4_TYPE_ECHO_REQUEST() => \&_packEcho,
123 NP_ICMPv4_TYPE_ECHO_REPLY() => \&_packEcho,
124 NP_ICMPv4_TYPE_TIMESTAMP_REQUEST() => \&_packTimestamp,
125 NP_ICMPv4_TYPE_TIMESTAMP_REPLY() => \&_packTimestamp,
126 NP_ICMPv4_TYPE_INFORMATION_REQUEST() => \&_packInformation,
127 NP_ICMPv4_TYPE_INFORMATION_REPLY() => \&_packInformation,
128 NP_ICMPv4_TYPE_ADDRESS_MASK_REQUEST() => \&_packAddressMask,
129 NP_ICMPv4_TYPE_ADDRESS_MASK_REPLY() => \&_packAddressMask,
130 NP_ICMPv4_TYPE_DESTINATION_UNREACHABLE() => \&_packDestUnreach,
131 NP_ICMPv4_TYPE_REDIRECT() => \&_packRedirect,
132 NP_ICMPv4_TYPE_TIME_EXCEEDED() => \&_packTimeExceed,
133 };
134  
135 my $unpackTypes = {
136 NP_ICMPv4_TYPE_ECHO_REQUEST() => \&_unpackEcho,
137 NP_ICMPv4_TYPE_ECHO_REPLY() => \&_unpackEcho,
138 NP_ICMPv4_TYPE_TIMESTAMP_REQUEST() => \&_unpackTimestamp,
139 NP_ICMPv4_TYPE_TIMESTAMP_REPLY() => \&_unpackTimestamp,
140 NP_ICMPv4_TYPE_INFORMATION_REQUEST() => \&_unpackInformation,
141 NP_ICMPv4_TYPE_INFORMATION_REPLY() => \&_unpackInformation,
142 NP_ICMPv4_TYPE_ADDRESS_MASK_REQUEST() => \&_unpackAddressMask,
143 NP_ICMPv4_TYPE_ADDRESS_MASK_REPLY() => \&_unpackAddressMask,
144 NP_ICMPv4_TYPE_DESTINATION_UNREACHABLE() => \&_unpackDestUnreach,
145 NP_ICMPv4_TYPE_REDIRECT() => \&_unpackRedirect,
146 NP_ICMPv4_TYPE_TIME_EXCEEDED() => \&_unpackTimeExceed,
147 };
148  
149 sub getDataLength {
150 my $self = shift;
151 my $data = $self->[$__data];
152 $data ? length($data) : 0;
153 }
154  
155 sub getLength {
156 my $self = shift;
157  
158 my $dataLength = $self->getDataLength;
159  
160 my $hdrLengths = {
161 NP_ICMPv4_TYPE_ECHO_REQUEST() => 8 + $dataLength,
162 NP_ICMPv4_TYPE_ECHO_REPLY() => 8 + $dataLength,
163 NP_ICMPv4_TYPE_TIMESTAMP_REQUEST() => 20 + $dataLength,
164 NP_ICMPv4_TYPE_TIMESTAMP_REPLY() => 20 + $dataLength,
165 NP_ICMPv4_TYPE_INFORMATION_REQUEST() => 8 + $dataLength,
166 NP_ICMPv4_TYPE_INFORMATION_REPLY() => 8 + $dataLength,
167 NP_ICMPv4_TYPE_ADDRESS_MASK_REQUEST() => 12 + $dataLength,
168 NP_ICMPv4_TYPE_ADDRESS_MASK_REPLY() => 12 + $dataLength,
169 NP_ICMPv4_TYPE_DESTINATION_UNREACHABLE() => 8 + $dataLength,
170 NP_ICMPv4_TYPE_REDIRECT() => 8 + $dataLength,
171 NP_ICMPv4_TYPE_TIME_EXCEEDED() => 8 + $dataLength,
172 };
173  
174 $hdrLengths->{$self->[$__type]} || 0;
175 }
176  
177 sub _handleType {
178 my $self = shift;
179 my ($format, $fields, $values) = @_;
180  
181 if (@$values) {
182 return($self->SUPER::pack($format, @$values) || undef);
183 }
184 else {
185 my @elts = $self->SUPER::unpack($format, $self->[$__payload])
186 or return undef;
187 my $n = 0;
188 return { map { $_ => $elts[$n++] } @$fields };
189 }
190 }
191  
192 sub _packEcho {
193 my $self = shift;
194 $self->_handleType(
195 'nn', [], [ $self->[$__identifier], $self->[$__sequenceNumber] ]
196 );
197 }
198  
199 sub _unpackEcho {
200 shift->_handleType('nn a*', [ qw(identifier sequenceNumber data) ], []);
201 }
202  
203 sub _packTimestamp {
204 my $self = shift;
205 $self->_handleType('nnNNN', [],
206 [ $self->[$__identifier], $self->[$__sequenceNumber],
207 $self->[$__originateTimestamp], $self->[$__receiveTimestamp],
208 $self->[$__transmitTimestamp]
209 ],
210 );
211 }
212  
213 sub _unpackTimestamp {
214 shift->_handleType(
215 'nnNNN a*',
216 [ qw(identifier sequenceNumber originateTimestamp receiveTimestamp
217 transmitTimestamp data) ],
218 [],
219 );
220 }
221  
222 # It has same fields as ICMP echo
223 sub _packInformation { shift->_packEcho }
224 sub _unpackInformation { shift->_unpackEcho }
225  
226 sub _packAddressMask {
227 my $self = shift;
228 $self->_handleType('nnN', [],
229 [ $self->[$__identifier], $self->[$__sequenceNumber],
230 $self->[$__addressMask]
231 ],
232 );
233 }
234  
235 sub _unpackAddressMask {
236 shift->_handleType(
237 'nnN a*',
238 [ qw(identifier sequenceNumber addressMask data) ],
239 [],
240 );
241 }
242  
243 # Pad ICMP error returned to achieve IP length (from IP request),
244 # and put it as a Frame into error instance data
245 sub _dataToFrame {
246 my $data = shift;
247 my $ip = Net::Packet::IPv4->new(raw => $data);
248 $data .= "\x00" x $ip->length;
249 Net::Packet::Frame->new(raw => $data) or return undef;
250 }
251  
252 sub _packDestUnreach {
253 my $self = shift;
254 $self->_handleType(
255 'N',
256 [],
257 [ $self->[$__unused] ],
258 );
259 }
260  
261 sub _unpackDestUnreach {
262 my $href = shift->_handleType(
263 'N a*',
264 [ qw(unused data) ],
265 [],
266 );
267 $href->{error} = _dataToFrame($href->{data}) if $href->{data};
268 $href;
269 }
270  
271 sub _packRedirect {
272 my $self = shift;
273 $self->_handleType(
274 'a4',
275 [],
276 [ inetAton($self->[$__gateway]) ],
277 );
278 }
279  
280 sub _unpackRedirect {
281 my $href = shift->_handleType(
282 'a4 a*',
283 [ qw(gateway data) ],
284 [],
285 );
286 $href->{gateway} = inetNtoa($href->{gateway});
287 $href->{error} = _dataToFrame($href->{data}) if $href->{data};
288 $href;
289 }
290  
291 sub _packTimeExceed {
292 my $self = shift;
293 $self->_handleType(
294 'N',
295 [],
296 [ $self->unused ],
297 );
298 }
299  
300 sub _unpackTimeExceed {
301 my $href = shift->_handleType(
302 'N a*',
303 [ qw(unused data) ],
304 [],
305 );
306 $href->{error} = _dataToFrame($href->{data}) if $href->{data};
307 $href;
308 }
309  
310 sub _decodeError {
311 my $self = shift;
312 carp("@{[(caller(0))[3]]}: unknown ICMPv4: ".
313 "type: @{[$self->type]}, code: @{[$self->code]}\n");
314 undef;
315 }
316  
317 sub pack {
318 my $self = shift;
319  
320 $self->[$__raw] = $self->SUPER::pack('CCn',
321 $self->[$__type],
322 $self->[$__code],
323 $self->[$__checksum],
324 ) or return undef;
325  
326 my $sub = $packTypes->{$self->[$__type]} || \&_decodeError;
327 my $raw = $self->$sub or return undef;
328  
329 if (my $data = $self->[$__data]) {
330 $raw .= $self->SUPER::pack('a*', $data)
331 or return undef;
332 }
333  
334 $self->[$__raw] = $self->[$__raw].$raw;
335  
336 1;
337 }
338  
339 sub unpack {
340 my $self = shift;
341  
342 my ($type, $code, $checksum, $payload) =
343 $self->SUPER::unpack('CCS a*', $self->[$__raw])
344 or return undef;
345  
346 $self->[$__type] = $type;
347 $self->[$__code] = $code;
348 $self->[$__checksum] = $checksum;
349 $self->[$__payload] = $payload;
350  
351 # unpack specific ICMPv4 types
352 my $sub = $unpackTypes->{$self->[$__type]} || \&_decodeError;
353 my $href = $self->$sub or return undef;
354  
355 $self->$_($href->{$_}) for keys %$href;
356  
357 # payload has been handled by previous chunk of code
358 $self->[$__payload] = undef;
359  
360 1;
361 }
362  
363 sub computeChecksums {
364 my $self = shift;
365  
366 my $sub = $packTypes->{$self->[$__type]} || \&_decodeError;
367 my $raw = $self->$sub or return undef;
368  
369 if (my $data = $self->[$__data]) {
370 $raw .= $self->SUPER::pack('a*', $data)
371 or return undef;
372 }
373  
374 my $packed = $self->SUPER::pack('CCn', $self->[$__type], $self->[$__code], 0)
375 or return undef;
376  
377 $self->[$__checksum] = inetChecksum($packed.$raw);
378  
379 1;
380 }
381  
382 sub encapsulate { NP_LAYER_NONE }
383  
384 sub print {
385 my $self = shift;
386  
387 my $i = $self->is;
388 my $l = $self->layer;
389 my $buf = sprintf
390 "$l:+$i: type:%d code:%d checksum:0x%.4x size:%d",
391 $self->type,
392 $self->code,
393 $self->checksum,
394 $self->getLength,
395 ;
396  
397 if ($self->data) {
398 $buf .= sprintf("\n$l: $i: dataLength:%d data:%s",
399 $self->getDataLength,
400 $self->SUPER::unpack('H*', $self->data))
401 or return undef;
402 }
403  
404 $buf;
405 }
406  
407 #
408 # Helpers
409 #
410  
411 sub _isType { shift->[$__type] == shift() }
412 sub isTypeEchoRequest { shift->_isType(NP_ICMPv4_TYPE_ECHO_REQUEST) }
413 sub isTypeEchoReply { shift->_isType(NP_ICMPv4_TYPE_ECHO_REPLY) }
414 sub isTypeTimestampRequest {
415 shift->_isType(NP_ICMPv4_TYPE_TIMESTAMP_REQUEST);
416 }
417 sub isTypeTimestampReply {
418 shift->_isType(NP_ICMPv4_TYPE_TIMESTAMP_REPLY);
419 }
420 sub isTypeInformationRequest {
421 shift->_isType(NP_ICMPv4_TYPE_INFORMATION_REQUEST);
422 }
423 sub isTypeInformationReply {
424 shift->_isType(NP_ICMPv4_TYPE_INFORMATION_REPLY);
425 }
426 sub isTypeAddressMaskRequest {
427 shift->_isType(NP_ICMPv4_TYPE_ADDRESS_MASK_REQUEST);
428 }
429 sub isTypeAddressMaskReply {
430 shift->_isType(NP_ICMPv4_TYPE_ADDRESS_MASK_REPLY);
431 }
432 sub isTypeDestinationUnreachable {
433 shift->_isType(NP_ICMPv4_TYPE_DESTINATION_UNREACHABLE);
434 }
435  
436 1;
437  
438 __END__