4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
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.
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.
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.
24 \brief UDPPacket non-inline non-template implementation */
26 #include "UDPPacket.hh"
27 //#include "UDPPacket.ih"
28 #include "IPv4Packet.hh"
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"
39 ///////////////////////////////cc.p////////////////////////////////////////
42 senf::PacketRegistry<senf::IpTypes>::RegistrationProxy<senf::UDPPacket>
43 registerUDPPacket (17);
46 ///////////////////////////////////////////////////////////////////////////
47 // senf::UDPPacketParser
49 prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum()
53 summer.feed( i(), i()+checksum_offset );
54 summer.feed( i()+checksum_offset+2, data().end() );
56 // Now on to the awkward part: the IP pseudo header
57 IPv4Packet ipv4 (packet().prev<IPv4Packet>(nothrow));
59 // Pseudo header defined in RFC768
60 summer.feed( ipv4->source().i(),
61 ipv4->source().i() + IPv4PacketParser::source_t::fixed_bytes );
62 ///\fixme What about a hop-by-hop routing option? Which destination is used in IPv4 ?
63 summer.feed( ipv4->destination().i(),
64 ipv4->destination().i() + IPv4PacketParser::destination_t::fixed_bytes );
66 ///\fixme May there be another header between the IPv4 header and UDP? if so, we
67 /// need to hack the correct protocol number here ...
69 summer.feed( i() + length_offset, i() + length_offset + 2 );
72 // Pseudo header defined in RFC2460
73 IPv6Packet ipv6 (packet().prev<IPv6Packet>(nothrow));
75 summer.feed( ipv6->source().i(),
76 ipv6->source().i() + IPv6PacketParser::source_t::fixed_bytes );
77 ///\todo Implement routing header support
78 // The destination used here must be the *final* destination ...
79 summer.feed( ipv6->destination().i(),
80 ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
81 /// This is a simplification. The value is really 32bit to support UDP Jumbograms
82 /// (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
83 summer.feed( i() + length_offset, i() + length_offset + 2 );
84 // RFC2460 specifies, that this must always be 17, not the value used in the ipv6
91 boost::uint16_t rv (summer.sum());
92 return rv ? rv : 0xffffu;
95 ///////////////////////////////////////////////////////////////////////////
96 // senf::UDPPacketType
98 prefix_ void senf::UDPPacketType::dump(packet p, std::ostream & os)
100 boost::io::ios_all_saver ias(os);
102 << " source port : " << p->source() << "\n"
103 << " dest port : " << p->destination() << "\n"
104 << " length : " << p->length() << "\n"
106 << std::hex << std::setw(4) << std::setfill('0') << p->checksum() << "\n";
109 prefix_ void senf::UDPPacketType::finalize(packet p)
111 p->length() << p.size();
112 p->checksum() << p->calcChecksum();
115 ///////////////////////////////cc.e////////////////////////////////////////
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // compile-command: "scons -u test"
126 // comment-column: 40