fa59596e5cee28dd33a5991a13c139b7b78a7535
[senf.git] / senf / 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 /** \file
24     \brief ICMPv6Packet non-inline non-template implementation */
25
26 #include "ICMPv6Packet.hh"
27 //#include "ICMPv6Packet.ih"
28
29 // Custom includes
30 #include <boost/io/ios_state.hpp>
31 #include <senf/Packets/DefaultBundle/IPv6Packet.hh>
32 #include <senf/Utils/IpChecksum.hh>
33 #include <iomanip>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes, 58, senf::ICMPv6Packet);
39
40 prefix_ boost::uint16_t senf::ICMPv6PacketParser::calcChecksum()
41     const
42 {
43     senf::IPv6Packet ipv6 (packet().rfind<senf::IPv6Packet>(senf::nothrow));
44     if (! ipv6) return 0u;
45
46     senf::IpChecksum summer;
47
48     ////////////////////////////////////////
49     // IPv6 pseudo header
50     summer.feed( ipv6->source().i(),
51                  ipv6->source().i() + senf::IPv6Packet::Parser::source_t::fixed_bytes );
52     // need support for HopByHop routing header -> the destination used here must be the *final*
53     // destination ...
54     summer.feed( ipv6->destination().i(),
55                  ipv6->destination().i() + senf::IPv6PacketParser::destination_t::fixed_bytes );
56     // packet length
57     boost::uint32_t size (data().size());
58     summer.feed((size>>24)&0xff);
59     summer.feed((size>>16)&0xff);
60     summer.feed((size>> 8)&0xff);
61     summer.feed((size    )&0xff);
62     // protocol number
63     // summer.feed( 0u );
64     // summer.feed( 0u );
65     summer.feed( 0u );
66     summer.feed( 58u );
67
68     ////////////////////////////////////////
69     // ICMP Packet
70     summer.feed( i(), i()+checksum_offset );
71     // checksum
72     // summer.feed(0); summer.feed(0);
73     summer.feed( i()+checksum_offset+2, data().end() );
74
75     boost::uint16_t rv (summer.sum());
76     return rv ? rv : 0xffffu;
77 }
78
79 prefix_ void senf::ICMPv6PacketType::dump(packet p, std::ostream &os)
80 {
81     boost::io::ios_all_saver ias(os);
82     os << "ICMPv6 protocol:\n"
83        << senf::fieldName("type")                      << unsigned(p->type()) <<"\n"
84        << senf::fieldName("code")                      << unsigned(p->code()) <<"\n"
85        << senf::fieldName("checksum")
86        << "0x" << std::hex << std::setw(4) << unsigned(p->checksum()) << "\n";
87 }
88
89 ///////////////////////////////cc.e////////////////////////////////////////
90 #undef prefix_
91
92 \f
93 // Local Variables:
94 // mode: c++
95 // fill-column: 100
96 // c-file-style: "senf"
97 // indent-tabs-mode: nil
98 // ispell-local-dictionary: "american"
99 // compile-command: "scons -u test"
100 // comment-column: 40
101 // End: