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