switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / UDPPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief UDPPacket non-inline non-template implementation */
30
31 #include "UDPPacket.hh"
32 //#include "UDPPacket.ih"
33
34 // Custom includes
35 #include <iomanip>
36 #include <boost/io/ios_state.hpp>
37 #include <senf/Utils/IpChecksum.hh>
38 #include "IPv6Packet.hh"
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace {
44     SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes, 17, senf::UDPPacket);
45 }
46
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 // senf::UDPPacketParser
49
50 prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum()
51     const
52 {
53     IpChecksum summer;
54     // first on to the awkward part: the IP pseudo header
55     IPv4Packet ipv4 (packet().rfind<IPv4Packet>(nothrow));
56     if (ipv4) {
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 );
63         summer.feed( 0u );
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 ...
66         summer.feed( 17u );
67         summer.feed( i() + length_offset, i() + length_offset + 2 );
68     }
69     else {
70         // Pseudo header defined in RFC2460
71         IPv6Packet ipv6 (packet().rfind<IPv6Packet>(nothrow));
72         if (ipv6) {
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
83             // header
84             summer.feed( 0u );
85             summer.feed( 17u );
86         }
87     }
88
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() );
93
94     boost::uint16_t rv (summer.sum());
95     return rv ? rv : 0xffffu;
96 }
97
98 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 // senf::UDPPacketType
100
101 prefix_ void senf::UDPPacketType::dump(packet p, std::ostream & os)
102 {
103     boost::io::ios_all_saver ias(os);
104     os << "UDP:\n"
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";
110 }
111
112 prefix_ void senf::UDPPacketType::finalize(packet p)
113 {
114     p->length() << p.size();
115     p->checksum() << p->calcChecksum();
116 }
117
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
119 #undef prefix_
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
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
130 // End: