Packet/ICMPv4.pm

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