499ac929441b373d9fa5c5b050e240e7a5aff54a
[senf.git] / senf / Packets / DefaultBundle / UDPPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief UDPPacket non-inline non-template implementation */
25
26 #include "UDPPacket.hh"
27 //#include "UDPPacket.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <boost/io/ios_state.hpp>
32 #include <senf/Utils/IpChecksum.hh>
33 #include "IPv6Packet.hh"
34
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 namespace {
39     SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes, 17, senf::UDPPacket);
40 }
41
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 // senf::UDPPacketParser
44
45 prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum()
46     const
47 {
48     IpChecksum summer;
49     // first on to the awkward part: the IP pseudo header
50     IPv4Packet ipv4 (packet().rfind<IPv4Packet>(nothrow));
51     if (ipv4) {
52         // Pseudo header defined in RFC768
53         summer.feed( ipv4->source().i(),
54                      ipv4->source().i() + IPv4Packet::Parser::source_t::fixed_bytes );
55         ///\fixme What about a hop-by-hop routing option? Which destination is used in IPv4 ?
56         summer.feed( ipv4->destination().i(),
57                      ipv4->destination().i() + IPv4PacketParser::destination_t::fixed_bytes );
58         summer.feed( 0u );
59         ///\fixme May there be another header between the IPv4 header and UDP? if so, we
60         /// need to hack the correct protocol number here ...
61         summer.feed( 17u );
62         summer.feed( i() + length_offset, i() + length_offset + 2 );
63     }
64     else {
65         // Pseudo header defined in RFC2460
66         IPv6Packet ipv6 (packet().rfind<IPv6Packet>(nothrow));
67         if (ipv6) {
68             summer.feed( ipv6->source().i(),
69                          ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
70             ///\todo Implement routing header support
71             // The destination used here must be the *final* destination ...
72             summer.feed( ipv6->destination().i(),
73                          ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
74             // This is a simplification. The value is really 32bit to support UDP Jumbograms
75             // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
76             summer.feed( i() + length_offset, i() + length_offset + 2 );
77             // RFC2460 specifies, that this must always be 17, not the value used in the ipv6
78             // header
79             summer.feed( 0u );
80             summer.feed( 17u );
81         }
82     }
83
84     // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum
85     // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
86     summer.feed( i(), i()+checksum_offset );
87     summer.feed( i()+checksum_offset+2, data().end() );
88
89     boost::uint16_t rv (summer.sum());
90     return rv ? rv : 0xffffu;
91 }
92
93 //-/////////////////////////////////////////////////////////////////////////////////////////////////
94 // senf::UDPPacketType
95
96 prefix_ void senf::UDPPacketType::dump(packet p, std::ostream & os)
97 {
98     boost::io::ios_all_saver ias(os);
99     os << "UDP:\n"
100        << senf::fieldName("source port")               << p->source() << "\n"
101        << senf::fieldName("dest port")                 << p->destination() << "\n"
102        << senf::fieldName("length")                    << p->length() << "\n"
103        << senf::fieldName("checksum")
104        << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->checksum() << "\n";
105 }
106
107 prefix_ void senf::UDPPacketType::finalize(packet p)
108 {
109     p->length() << p.size();
110     p->checksum() << p->calcChecksum();
111 }
112
113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // comment-column: 40
125 // End: