1use strict;
2use warnings;
3
4use Test::More tests => 2;                      # last test to print
5
6use NetPacket::Ethernet;
7use NetPacket::IP;
8use NetPacket::ICMP;
9
10my $datagram = binarize( <<'END_DATAGRAM' );
1100 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00
1200 54 00 00 40 00 40 01 3c a7 7f 00 00 01 7f 00
1300 01 08 00 d8 2f b6 6f 00 00 f8 11 c9 45 ba 05
1403 00 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
1516 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25
1626 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35
1736 37
18END_DATAGRAM
19
20my $eth = NetPacket::Ethernet->decode( $datagram );
21my $ip = NetPacket::IP->decode( $eth->{data} );
22my $icmp = NetPacket::ICMP->decode( $ip->{data} );
23
24is $icmp->{cksum} => 55343, 'ICMP checksum';
25
26# recompute the checksum
27$icmp->checksum;
28
29is $icmp->{cksum} => 55343, 'recomputed ICMP checksum';
30
31sub binarize {
32    my $string = shift;
33
34    return join '' => map { chr hex } split ' ', $string;
35}
36
37