Added ICMPv6 Packet Parser and Unittests for different ICMP Packages.
[senf.git] / Packets / DefaultBundle / ICMPv6Packet.cc
1 // Copyright (C) 2008
2 // Fraunhofer Institute for Open Communication Systems (FOKUS)
3 // Competence Center NETwork research (NET), St. Augustin, GERMANY
4 //     Philipp Batroff <Philipp.Batroff@fokus.fraunhofer.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 // Custom includes
22 #include "../../Packets/Packets.hh"
23 #include "ICMPv6Packet.hh"
24 #include <boost/io/ios_state.hpp>
25 #include "../../Packets/DefaultBundle/IPv6Packet.hh"
26 #include "../../Utils/IpChecksum.hh"
27
28 #define prefix_
29
30 namespace {
31     senf::PacketRegistry<senf::IpTypes>::RegistrationProxy<senf::ICMPv6Packet>
32         registerICMPv6Packet (58);
33 }
34
35 prefix_ void senf::ICMPV6PacketParser::calcChecksum() const {
36
37     senf::IpChecksum summer;
38     senf::IPv6Packet ipv6 (packet().rfind<senf::IPv6Packet>(senf::nothrow));
39     
40     summer.feed( ipv6->source().i(), 
41                     ipv6->source().i() + senf::IPv6Packet::Parser::source_t::fixed_bytes );
42     // The destination used here must be the *final* destination ...
43     summer.feed( ipv6->destination().i(), ipv6->destination().i() + senf::IPv6PacketParser::destination_t::fixed_bytes );
44
45     // This is a simplification. The value is really 32bit to support UDP Jumbograms
46     // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
47     summer.feed( i() + ipv6->length(), i() + ipv6->length() + 2 );
48     // --> http://www.iana.org/assignments/protocol-numbers 
49     // need to insert the correct protocol number here, NOT static 17!!
50     summer.feed( 0u );
51     summer.feed( 58u );
52     // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum 
53     // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
54     summer.feed( i(), i()+checksum_offset );
55     summer.feed( i()+checksum_offset+2, data().end() );
56
57     boost::uint16_t rv (summer.sum());
58     this->checksum() << (rv ? rv : 0xffffu);
59 }
60
61 prefix_ void senf::ICMPV6PacketType::dump(packet p, std::ostream &os)
62 {
63     boost::io::ios_all_saver ias(os);
64     os << "ICMPv6 protocol:\n"
65         << "Type                    : " << p->type() <<"\n"
66         << "Code                    : " << p->code() <<"\n"
67         << "Checksum                : " << p->checksumOutput() << "\n";
68 }
69
70 #undef prefix_