some unimportant 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() 
38     const
39 {
40
41     senf::IpChecksum summer;
42     senf::IPv6Packet ipv6 (packet().rfind<senf::IPv6Packet>(senf::nothrow));
43
44     if (! ipv6)
45         return 0u;
46     
47     summer.feed( ipv6->source().i(), 
48                     ipv6->source().i() + senf::IPv6Packet::Parser::source_t::fixed_bytes );
49     // The destination used here must be the *final* destination ...
50     summer.feed( ipv6->destination().i(), ipv6->destination().i() + senf::IPv6PacketParser::destination_t::fixed_bytes );
51
52     // This is a simplification. The value is really 32bit to support UDP Jumbograms
53     // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
54     summer.feed( i() + ipv6->length(), i() + ipv6->length() + 2 );
55     // --> http://www.iana.org/assignments/protocol-numbers 
56     // need to insert the correct protocol number here, NOT static 17!!
57     summer.feed( 0u );
58     summer.feed( 58u );
59     // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum 
60     // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
61     summer.feed( i(), i()+checksum_offset );
62     summer.feed( i()+checksum_offset+2, data().end() );
63
64     boost::uint16_t rv (summer.sum());
65     return rv ? rv : 0xffffu;
66 }
67
68 prefix_ void senf::ICMPv6PacketType::dump(packet p, std::ostream &os)
69 {
70     boost::io::ios_all_saver ias(os);
71     os << "ICMPv6 protocol:\n"
72        << "Type                    : " << p->type() <<"\n"
73        << "Code                    : " << p->code() <<"\n"
74        << "Checksum                : " << p->checksum() << "\n";
75 }
76
77 #undef prefix_
78
79 \f
80 // Local Variables:
81 // mode: c++
82 // fill-column: 100
83 // c-file-style: "senf"
84 // indent-tabs-mode: nil
85 // ispell-local-dictionary: "american"
86 // compile-command: "scons -u test"
87 // comment-column: 40
88 // End: