4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief UDPPacket non-inline non-template implementation */
31 #include "UDPPacket.hh"
32 //#include "UDPPacket.ih"
36 #include <boost/io/ios_state.hpp>
37 #include <senf/Utils/IpChecksum.hh>
38 #include "IPv6Packet.hh"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes, 17, senf::UDPPacket);
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 // senf::UDPPacketParser
50 prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum()
54 // first on to the awkward part: the IP pseudo header
55 IPv4Packet ipv4 (packet().rfind<IPv4Packet>(nothrow));
57 // Pseudo header defined in RFC768
58 summer.feed( ipv4->source().i(),
59 ipv4->source().i() + IPv4Packet::Parser::source_t::fixed_bytes );
60 ///\fixme What about a hop-by-hop routing option? Which destination is used in IPv4 ?
61 summer.feed( ipv4->destination().i(),
62 ipv4->destination().i() + IPv4PacketParser::destination_t::fixed_bytes );
64 ///\fixme May there be another header between the IPv4 header and UDP? if so, we
65 /// need to hack the correct protocol number here ...
67 summer.feed( i() + length_offset, i() + length_offset + 2 );
70 // Pseudo header defined in RFC2460
71 IPv6Packet ipv6 (packet().rfind<IPv6Packet>(nothrow));
73 summer.feed( ipv6->source().i(),
74 ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
75 ///\todo Implement routing header support
76 // The destination used here must be the *final* destination ...
77 summer.feed( ipv6->destination().i(),
78 ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
79 // This is a simplification. The value is really 32bit to support UDP Jumbograms
80 // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
81 summer.feed( i() + length_offset, i() + length_offset + 2 );
82 // RFC2460 specifies, that this must always be 17, not the value used in the ipv6
89 // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum
90 // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
91 summer.feed( i(), i()+checksum_offset );
92 summer.feed( i()+checksum_offset+2, data().end() );
94 boost::uint16_t rv (summer.sum());
95 return rv ? rv : 0xffffu;
98 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 // senf::UDPPacketType
101 prefix_ void senf::UDPPacketType::dump(packet p, std::ostream & os)
103 boost::io::ios_all_saver ias(os);
105 << senf::fieldName("source port") << p->source() << "\n"
106 << senf::fieldName("dest port") << p->destination() << "\n"
107 << senf::fieldName("length") << p->length() << "\n"
108 << senf::fieldName("checksum")
109 << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->checksum() << "\n";
112 prefix_ void senf::UDPPacketType::finalize(packet p)
114 p->length() << p.size();
115 p->checksum() << p->calcChecksum();
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
125 // c-file-style: "senf"
126 // indent-tabs-mode: nil
127 // ispell-local-dictionary: "american"
128 // compile-command: "scons -u test"
129 // comment-column: 40