Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

DefaultBundle/UDPPacket.cc

Go to the documentation of this file.
00001 // $Id: UDPPacket.cc 1792 2011-06-09 15:49:59Z tho $
00002 //
00003 // Copyright (C) 2006
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "UDPPacket.hh"
00027 //#include "UDPPacket.ih"
00028 
00029 // Custom includes
00030 #include <iomanip>
00031 #include <boost/io/ios_state.hpp>
00032 #include <senf/Utils/IpChecksum.hh>
00033 #include "IPv6Packet.hh"
00034 
00035 #define prefix_
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 
00038 namespace {
00039     SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes, 17, senf::UDPPacket);
00040 }
00041 
00042 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00043 // senf::UDPPacketParser
00044 
00045 prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum()
00046     const
00047 {
00048     IpChecksum summer;
00049     // first on to the awkward part: the IP pseudo header
00050     IPv4Packet ipv4 (packet().rfind<IPv4Packet>(nothrow));
00051     if (ipv4) {
00052         // Pseudo header defined in RFC768
00053         summer.feed( ipv4->source().i(),
00054                      ipv4->source().i() + IPv4Packet::Parser::source_t::fixed_bytes );
00056         summer.feed( ipv4->destination().i(),
00057                      ipv4->destination().i() + IPv4PacketParser::destination_t::fixed_bytes );
00058         summer.feed( 0u );
00061         summer.feed( 17u );
00062         summer.feed( i() + length_offset, i() + length_offset + 2 );
00063     }
00064     else {
00065         // Pseudo header defined in RFC2460
00066         IPv6Packet ipv6 (packet().rfind<IPv6Packet>(nothrow));
00067         if (ipv6) {
00068             summer.feed( ipv6->source().i(),
00069                          ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
00071             // The destination used here must be the *final* destination ...
00072             summer.feed( ipv6->destination().i(),
00073                          ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
00074             // This is a simplification. The value is really 32bit to support UDP Jumbograms
00075             // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
00076             summer.feed( i() + length_offset, i() + length_offset + 2 );
00077             // RFC2460 specifies, that this must always be 17, not the value used in the ipv6
00078             // header
00079             summer.feed( 0u );
00080             summer.feed( 17u );
00081         }
00082     }
00083 
00084     // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum
00085     // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
00086     summer.feed( i(), i()+checksum_offset );
00087     summer.feed( i()+checksum_offset+2, data().end() );
00088 
00089     boost::uint16_t rv (summer.sum());
00090     return rv ? rv : 0xffffu;
00091 }
00092 
00093 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00094 // senf::UDPPacketType
00095 
00096 prefix_ void senf::UDPPacketType::dump(packet p, std::ostream & os)
00097 {
00098     boost::io::ios_all_saver ias(os);
00099     os << "UDP:\n"
00100        << senf::fieldName("source port")               << p->source() << "\n"
00101        << senf::fieldName("dest port")                 << p->destination() << "\n"
00102        << senf::fieldName("length")                    << p->length() << "\n"
00103        << senf::fieldName("checksum")
00104        << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->checksum() << "\n";
00105 }
00106 
00107 prefix_ void senf::UDPPacketType::finalize(packet p)
00108 {
00109     p->length() << p.size();
00110     p->checksum() << p->calcChecksum();
00111 }
00112 
00113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00114 #undef prefix_
00115 
00116 
00117 // Local Variables:
00118 // mode: c++
00119 // fill-column: 100
00120 // c-file-style: "senf"
00121 // indent-tabs-mode: nil
00122 // ispell-local-dictionary: "american"
00123 // compile-command: "scons -u test"
00124 // comment-column: 40
00125 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research