ICMPv6Packet.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "ICMPv6Packet.hh"
18 //#include "ICMPv6Packet.ih"
19 
20 // Custom includes
21 #include <boost/io/ios_state.hpp>
23 #include <senf/Utils/IpChecksum.hh>
24 #include <iomanip>
25 
26 #define prefix_
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 
31 
33  const
34 {
35  IPv6Packet ipv6 (packet().rfind<IPv6Packet>(senf::nothrow));
36  if (! ipv6) return 0u;
37 
38  IpChecksum summer;
39 
40  //-/////////////////////////////////////////////////////////////////////////////////////////////
41  // IPv6 pseudo header
42  summer.feed( ipv6->source().i(),
43  ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
44  // need support for HopByHop routing header -> the destination used here must be the *final*
45  // destination ...
46  summer.feed( ipv6->destination().i(),
47  ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
48  // packet length
49  boost::uint32_t size (data().size());
50  summer.feed((size>>24)&0xff);
51  summer.feed((size>>16)&0xff);
52  summer.feed((size>> 8)&0xff);
53  summer.feed((size )&0xff);
54  // protocol number
55  // summer.feed( 0u );
56  // summer.feed( 0u );
57  summer.feed( 0u );
58  summer.feed( 58u );
59 
60  //-/////////////////////////////////////////////////////////////////////////////////////////////
61  // ICMP Packet
62  summer.feed( i(), i()+checksum_offset );
63  // checksum
64  // summer.feed(0); summer.feed(0);
65  summer.feed( i()+checksum_offset+2, data().end() );
66 
67  boost::uint16_t rv (summer.sum());
68  return rv ? rv : 0xffffu;
69 }
70 
71 prefix_ void senf::ICMPv6PacketType::dump(packet p, std::ostream & os)
72 {
73  boost::io::ios_all_saver ias(os);
74  os << "ICMPv6 protocol:\n"
75  << senf::fieldName("type") << unsigned(p->type()) << "\n"
76  << senf::fieldName("code") << unsigned(p->code()) << "\n"
77  << senf::fieldName("checksum")
78  << "0x" << std::hex << std::setw(4) << unsigned(p->checksum()) << "\n";
79 }
80 
81 //-/////////////////////////////////////////////////////////////////////////////////////////////////
82 #undef prefix_
83 
84 
85 // Local Variables:
86 // mode: c++
87 // fill-column: 100
88 // c-file-style: "senf"
89 // indent-tabs-mode: nil
90 // ispell-local-dictionary: "american"
91 // compile-command: "scons -u test"
92 // comment-column: 40
93 // End:
IPv6Packet public header.
static void dump(packet p, std::ostream &os)
Definition: ICMPv6Packet.cc:71
Packet packet() const
Get packet this parser is parsing from.
std::string fieldName(std::string const &s)
Definition: DumpFormat.cc:28
#define prefix_
Definition: ICMPv6Packet.cc:26
SENF_PACKET_INSTANTIATE_TEMPLATE(senf::ICMPv6Packet)
Protocol specific packet handle.
Definition: Packet.hh:87
data_iterator i() const
Return beginning of data to parse.
nothrow
SENF_PACKET_REGISTRY_REGISTER(senf::IPTypes, senf::ICMPv6PacketType::ipType, senf::ICMPv6Packet)
boost::uint16_t calcChecksum() const
Definition: ICMPv6Packet.cc:32
PacketData & data() const
Access the packets raw data container.
ICMPv6Packet public header.
static const IPTypes::key_t ipType
Definition: ICMPv6Packet.hh:77
IP protocol number registry.
Definition: Registries.hh:60
boost::uint16_t sum() const
void feed(boost::uint8_t byte)