switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / ICMPv6Packet.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Philipp Batroff <pug@berlios.de>
27
28 /** \file
29     \brief ICMPv6Packet non-inline non-template implementation */
30
31 #include "ICMPv6Packet.hh"
32 //#include "ICMPv6Packet.ih"
33
34 // Custom includes
35 #include <boost/io/ios_state.hpp>
36 #include <senf/Packets/DefaultBundle/IPv6Packet.hh>
37 #include <senf/Utils/IpChecksum.hh>
38 #include <iomanip>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes, 58, senf::ICMPv6Packet);
44
45 prefix_ boost::uint16_t senf::ICMPv6PacketParser::calcChecksum()
46     const
47 {
48     IPv6Packet ipv6 (packet().rfind<IPv6Packet>(senf::nothrow));
49     if (! ipv6) return 0u;
50
51     IpChecksum summer;
52
53     //-/////////////////////////////////////////////////////////////////////////////////////////////
54     // IPv6 pseudo header
55     summer.feed( ipv6->source().i(),
56                  ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
57     // need support for HopByHop routing header -> the destination used here must be the *final*
58     // destination ...
59     summer.feed( ipv6->destination().i(),
60                  ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
61     // packet length
62     boost::uint32_t size (data().size());
63     summer.feed((size>>24)&0xff);
64     summer.feed((size>>16)&0xff);
65     summer.feed((size>> 8)&0xff);
66     summer.feed((size    )&0xff);
67     // protocol number
68     // summer.feed( 0u );
69     // summer.feed( 0u );
70     summer.feed( 0u );
71     summer.feed( 58u );
72
73     //-/////////////////////////////////////////////////////////////////////////////////////////////
74     // ICMP Packet
75     summer.feed( i(), i()+checksum_offset );
76     // checksum
77     // summer.feed(0); summer.feed(0);
78     summer.feed( i()+checksum_offset+2, data().end() );
79
80     boost::uint16_t rv (summer.sum());
81     return rv ? rv : 0xffffu;
82 }
83
84 prefix_ void senf::ICMPv6PacketType::dump(packet p, std::ostream & os)
85 {
86     boost::io::ios_all_saver ias(os);
87     os << "ICMPv6 protocol:\n"
88        << senf::fieldName("type") << unsigned(p->type()) << "\n"
89        << senf::fieldName("code") << unsigned(p->code()) << "\n"
90        << senf::fieldName("checksum")
91        << "0x" << std::hex << std::setw(4) << unsigned(p->checksum()) << "\n";
92 }
93
94 //-/////////////////////////////////////////////////////////////////////////////////////////////////
95 #undef prefix_
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // c-file-style: "senf"
102 // indent-tabs-mode: nil
103 // ispell-local-dictionary: "american"
104 // compile-command: "scons -u test"
105 // comment-column: 40
106 // End: