UDPPacket.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 "UDPPacket.hh"
18 //#include "UDPPacket.ih"
19 
20 // Custom includes
21 #include <iomanip>
22 #include <boost/io/ios_state.hpp>
23 #include <senf/Utils/IpChecksum.hh>
24 #include "IPv4Packet.hh"
25 #include "IPv6Packet.hh"
26 
27 #define prefix_
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 
32 
33 //-/////////////////////////////////////////////////////////////////////////////////////////////////
34 // senf::UDPPacketParser
35 
37  const
38 {
39  IpChecksum summer;
40  // first on to the awkward part: the IP pseudo header
41  IPv4Packet ipv4 (packet().rfind<IPv4Packet>(nothrow));
42  if (ipv4) {
43  // Pseudo header defined in RFC768
44  summer.feed( ipv4->source().i(),
45  ipv4->source().i() + IPv4Packet::Parser::source_t::fixed_bytes );
47  summer.feed( ipv4->destination().i(),
48  ipv4->destination().i() + IPv4PacketParser::destination_t::fixed_bytes );
49  summer.feed( 0u );
52  summer.feed( 17u );
53  summer.feed( i() + length_offset, i() + length_offset + 2 );
54  }
55  else {
56  // Pseudo header defined in RFC2460
57  IPv6Packet ipv6 (packet().rfind<IPv6Packet>(nothrow));
58  if (ipv6) {
59  summer.feed( ipv6->source().i(),
60  ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
62  // The destination used here must be the *final* destination ...
63  summer.feed( ipv6->destination().i(),
64  ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
65  // This is a simplification. The value is really 32bit to support UDP Jumbograms
66  // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
67  summer.feed( i() + length_offset, i() + length_offset + 2 );
68  // RFC2460 specifies, that this must always be 17, not the value used in the ipv6
69  // header
70  summer.feed( 0u );
71  summer.feed( 17u );
72  }
73  }
74 
75  // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum
76  // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
77  summer.feed( i(), i()+checksum_offset );
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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
85 // senf::UDPPacketType
86 
87 prefix_ void senf::UDPPacketType::dump(packet p, std::ostream & os)
88 {
89  boost::io::ios_all_saver ias(os);
90  os << "UDP:\n"
91  << senf::fieldName("source port") << p->source() << "\n"
92  << senf::fieldName("dest port") << p->destination() << "\n"
93  << senf::fieldName("length") << p->length() << "\n"
94  << senf::fieldName("checksum")
95  << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->checksum() << "\n";
96 }
97 
99 {
100  p->length() << p.size();
101  p->checksum() << p->calcChecksum();
102 }
103 
104 //-/////////////////////////////////////////////////////////////////////////////////////////////////
105 #undef prefix_
106 
107 
108 // Local Variables:
109 // mode: c++
110 // fill-column: 100
111 // c-file-style: "senf"
112 // indent-tabs-mode: nil
113 // ispell-local-dictionary: "american"
114 // compile-command: "scons -u test"
115 // comment-column: 40
116 // End:
IPv6Packet public header.
Packet packet() const
Get packet this parser is parsing from.
#define prefix_
Definition: UDPPacket.cc:27
static void dump(packet p, std::ostream &os)
Dump given UDPPacket in readable form to given output stream.
Definition: UDPPacket.cc:87
std::string fieldName(std::string const &s)
Definition: DumpFormat.cc:28
SENF_PACKET_REGISTRY_REGISTER(senf::IPTypes, senf::UDPPacketType::ipType, senf::UDPPacket)
Protocol specific packet handle.
Definition: Packet.hh:87
data_iterator i() const
Return beginning of data to parse.
UDPPacket public header.
static void finalize(packet p)
Finalize packet.
Definition: UDPPacket.cc:98
nothrow
size_type size() const
Return size of packet in bytes.
PacketData & data() const
Access the packets raw data container.
IPv4Packet public header.
IP protocol number registry.
Definition: Registries.hh:60
boost::uint16_t sum() const
boost::uint16_t calcChecksum() const
calculate (pseudo-)header checksum
Definition: UDPPacket.cc:36
SENF_PACKET_INSTANTIATE_TEMPLATE(senf::UDPPacket)
static const IPTypes::key_t ipType
Definition: UDPPacket.hh:102
void feed(boost::uint8_t byte)