IPv4Packet.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 "IPv4Packet.hh"
18 //#include "IPv4Packet.ih"
19 
20 // Custom includes
21 #include <iomanip>
22 #include <boost/io/ios_state.hpp>
23 #include <senf/Utils/IpChecksum.hh>
24 #include "Registries.hh"
25 
26 #define prefix_
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 
30 SENF_PACKET_REGISTRY_REGISTER( senf::IPTypes, 4, senf::IPv4Packet); // IP-in-IP encapsulation
31 
33 
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35 // senf::IPv4PacketParser
36 
38  const
39 {
40  validate(bytes(*this));
41  IpChecksum summer;
42  summer.feed( i(), i()+checksum_offset );
43  summer.feed( i()+checksum_offset+2, i()+bytes(*this) );
44  return summer.sum();
45 }
46 
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 // senf::IPv4PacketType
49 
50 prefix_ void senf::IPv4PacketType::dump(packet p, std::ostream & os)
51 {
52  boost::io::ios_all_saver ias(os);
53  os << "Internet protocol Version 4:\n"
54  << senf::fieldName("version") << p->version() << "\n"
55  << senf::fieldName("ip header length") << p->ihl() << "\n"
56  << senf::fieldName("dscp") << (p->dscp()) << "\n"
57  << senf::fieldName("ecn") << (p->ecn()) << "\n"
58  << senf::fieldName("length") << p->length() << "\n"
59  << senf::fieldName("identifier") << p->identifier() << "\n"
60  << senf::fieldName("dont fragment") << p->df() << "\n"
61  << senf::fieldName("more fragments") << p->mf() << "\n"
62  << senf::fieldName("fragment") << p->frag() << "\n"
63  << senf::fieldName("ttl") << unsigned(p->ttl()) << "\n"
64  << senf::fieldName("protocol") << unsigned(p->protocol()) << "\n"
65  << senf::fieldName("checksum")
66  << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->checksum() << std::dec << "\n"
67  << senf::fieldName("source") << p->source() << "\n"
68  << senf::fieldName("destination") << p->destination() << "\n";
69 }
70 
72 {
73  p->length() << p.size();
74  p->protocol() << key(p.next(nothrow));
75  p->checksum() << p->calcChecksum();
76 }
77 
78 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79 #undef prefix_
80 
81 
82 // Local Variables:
83 // mode: c++
84 // fill-column: 100
85 // c-file-style: "senf"
86 // indent-tabs-mode: nil
87 // ispell-local-dictionary: "american"
88 // compile-command: "scons -u test"
89 // comment-column: 40
90 // End:
static const EtherTypes::key_t etherType
Definition: IPv4Packet.hh:170
boost::uint16_t calcChecksum() const
calculate header checksum
Definition: IPv4Packet.cc:37
std::string fieldName(std::string const &s)
Definition: DumpFormat.cc:28
static void dump(packet p, std::ostream &os)
Dump given IPv4Packet in readable form to given output stream.
Definition: IPv4Packet.cc:50
DefaultBundle packet registries public header.
PacketParserBase::size_type bytes(Parser const &p)
Return raw size parsed by the given parser object.
static void finalize(packet p)
Finalize packet.
Definition: IPv4Packet.cc:71
Protocol specific packet handle.
Definition: Packet.hh:87
Packet next() const
Get next packet in chain.
data_iterator i() const
Return beginning of data to parse.
#define prefix_
Definition: IPv4Packet.cc:26
EtherType registry.
Definition: Registries.hh:34
nothrow
SENF_PACKET_REGISTRY_REGISTER(senf::EtherTypes, senf::IPv4PacketType::etherType, senf::IPv4Packet)
size_type size() const
Return size of packet in bytes.
SENF_PACKET_INSTANTIATE_TEMPLATE(senf::IPv4Packet)
IPv4Packet public header.
void validate(size_type size) const
Validate size of data container.
IP protocol number registry.
Definition: Registries.hh:60
boost::uint16_t sum() const
void feed(boost::uint8_t byte)