Socket/Protocols/Raw: EUI64 documentation
[senf.git] / 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 #include "IPv4Packet.hh"
29
30 // Custom includes
31 #include <iomanip>
32 #include <boost/io/ios_state.hpp>
33 #include "../../Packets/Packets.hh"
34 #include "../../Utils/IpChecksum.hh"
35 #include "IPv4Packet.hh"
36 #include "IPv6Packet.hh"
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42     senf::PacketRegistry<senf::IpTypes>::RegistrationProxy<senf::UDPPacket>
43         registerUDPPacket (17);
44 }
45
46 ///////////////////////////////////////////////////////////////////////////
47 // senf::UDPPacketParser
48
49 prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum()
50     const
51 {
52     IpChecksum summer;
53     // first on to the awkward part: the IP pseudo header
54     IPv4Packet ipv4 (packet().rfind<IPv4Packet>(nothrow));
55     if (ipv4) {
56         // Pseudo header defined in RFC768
57         summer.feed( ipv4->source().i(), 
58                      ipv4->source().i() + IPv4Packet::Parser::source_t::fixed_bytes );
59         ///\fixme What about a hop-by-hop routing option? Which destination is used in IPv4 ?
60         summer.feed( ipv4->destination().i(), 
61                      ipv4->destination().i() + IPv4PacketParser::destination_t::fixed_bytes );
62         summer.feed( 0u );
63         ///\fixme May there be another header between the IPv4 header and UDP? if so, we
64         /// need to hack the correct protocol number here ...
65         summer.feed( 17u );
66         summer.feed( i() + length_offset, i() + length_offset + 2 );
67     } 
68     else {
69         // Pseudo header defined in RFC2460
70         IPv6Packet ipv6 (packet().rfind<IPv6Packet>(nothrow));
71         if (ipv6) {
72             summer.feed( ipv6->source().i(), 
73                          ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
74             ///\todo Implement routing header support
75             // The destination used here must be the *final* destination ...
76             summer.feed( ipv6->destination().i(), 
77                          ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
78             // This is a simplification. The value is really 32bit to support UDP Jumbograms
79             // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
80             summer.feed( i() + length_offset, i() + length_offset + 2 );
81             // RFC2460 specifies, that this must always be 17, not the value used in the ipv6
82             // header
83             summer.feed( 0u );
84             summer.feed( 17u );
85         }
86     }
87     
88     // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum 
89     // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
90     summer.feed( i(), i()+checksum_offset );
91     summer.feed( i()+checksum_offset+2, data().end() );
92
93     boost::uint16_t rv (summer.sum());
94     return rv ? rv : 0xffffu;
95 }
96
97 ///////////////////////////////////////////////////////////////////////////
98 // senf::UDPPacketType
99
100 prefix_ void senf::UDPPacketType::dump(packet p, std::ostream & os)
101 {
102     boost::io::ios_all_saver ias(os);
103     os << "UDP:\n"
104        << "  source port   : " << p->source() << "\n"
105        << "  dest port     : " << p->destination() << "\n"
106        << "  length        : " << p->length() << "\n"
107        << "  checksum      : " 
108          << std::hex << std::setw(4) << std::setfill('0') << p->checksum() << "\n";
109 }
110
111 prefix_ void senf::UDPPacketType::finalize(packet p)
112 {
113     p->length() << p.size();
114     p->checksum() << p->calcChecksum();
115 }
116
117 ///////////////////////////////cc.e////////////////////////////////////////
118 #undef prefix_
119
120 \f
121 // Local Variables:
122 // mode: c++
123 // fill-column: 100
124 // c-file-style: "senf"
125 // indent-tabs-mode: nil
126 // ispell-local-dictionary: "american"
127 // compile-command: "scons -u test"
128 // comment-column: 40
129 // End: