Packets/80221Bundle: added MIHFId class
[senf.git] / Packets / DefaultBundle / UDPPacket.hh
index bd0fc57..456b325 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 // Free Software Foundation, Inc.,
 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#ifndef HH_UDPPacket_
-#define HH_UDPPacket_ 1
+/** \file
+    \brief UDPPacket public header */
+
+#ifndef HH_SENF_Packets_DefaultBundle_UDPPacket_
+#define HH_SENF_Packets_DefaultBundle_UDPPacket_ 1
 
 // Custom includes
-#include "Packets/Packet.hh"
-#include "Packets/ParseInt.hh"
-#include "Packets/ParseArray.hh"
-#include "Packets/PacketRegistry.hh"
+#include "../../Packets/Packets.hh"
 
 //#include "UDPPacket.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
 
 namespace senf {
 
-    // See RFC768
-    template <class Iterator=nil, class IPacket=nil>
-    struct Parse_UDP : public ParserBase<Iterator,IPacket>
-    {
-        template <class I, class P=nil>
-        struct rebind { typedef Parse_UDP<I,P> parser; };
-        typedef Iterator byte_iterator;
-
-        Parse_UDP() {}
-        Parse_UDP(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
-
-        static unsigned bytes() { return 8; }
-
-        ///////////////////////////////////////////////////////////////////////////
-
-        typedef Parse_UInt16 < Iterator > Parse_16bit;
-
-        Parse_16bit source()          const { return Parse_16bit      (this->i()     ); }
-        Parse_16bit destination()     const { return Parse_16bit      (this->i() + 2 ); }
-        Parse_16bit length()          const { return Parse_16bit      (this->i() + 4 ); }
-        Parse_16bit crc()             const { return Parse_16bit      (this->i() + 6 ); }
+    /** \brief Parse a UDP packet
 
+        Parser implementing the UDP header. The fields implemented are:
+        \image html UDPPacket.png
+        
+        \see UDPPacketType \n
+            <a href="http://tools.ietf.org/html/rfc768">RFC 768</a>
+     */
+    struct UDPPacketParser : public PacketParserBase
+    {
+#       include SENF_FIXED_PARSER()
+
+        SENF_PARSER_FIELD( source,      senf::UInt16Parser );
+        SENF_PARSER_FIELD( destination, senf::UInt16Parser );
+        SENF_PARSER_FIELD( length,      senf::UInt16Parser );
+        SENF_PARSER_FIELD( checksum,    senf::UInt16Parser );
+
+        SENF_PARSER_FINALIZE(UDPPacketParser);
+
+        boost::uint16_t calcChecksum() const; ///< calculate (pseudo-)header checksum
+                                              /**< calculate and return the checksum of the
+                                                   (pseudo-)header \see \ref senf::IpChecksum */
+        bool validateChecksum() const {
+            return checksum() == 0u || checksum() == calcChecksum();
+        }                               ///< validate header checksum
+                                        /**< return \c true if the \ref checksum() "checksum"
+                                             field is equal to the \ref calcChecksum() "calculated checksum" */
     };
 
-    class UDPPacket
-        : public Packet,
-          public Parse_UDP<Packet::iterator, UDPPacket>
+    /** \brief UDP packet
+        
+        \par Packet type (typedef):
+            \ref UDPPacket
+
+        \par Fields:
+            \ref UDPPacketParser
+
+        <table class="packet" cellpadding="5" cellspacing="1" border="1">
+          <tr>
+            <th width="25%">0</th> <th width="25%">8</th> <th width="25%">16</th>
+            <th width="15%">24</th> <th style="text-align:right" width="10%">31</th>
+          </tr><tr>
+            <td colspan="2">\ref UDPPacketParser::source() "Source Port"</td>
+            <td colspan="3">\ref UDPPacketParser::destination() "Destination Port"</td>
+          </tr><tr>
+            <td colspan="2">\ref UDPPacketParser::length() "Length"</td>
+            <td colspan="3">\ref UDPPacketParser::checksum() "Checksum"</td>
+          </tr>
+        </table>
+          
+        \par Finalize action:
+            \copydetails finalize()
+
+        \ingroup protocolbundle_default
+     */
+    struct UDPPacketType
+        : public PacketTypeBase,
+          public PacketTypeMixin<UDPPacketType>
     {
-    public:
-        ///////////////////////////////////////////////////////////////////////////
-        // Types
-
-        typedef ptr_t<UDPPacket>::ptr ptr;
-
-        ///////////////////////////////////////////////////////////////////////////
-
-    private:
-        template <class Arg>
-        UDPPacket(Arg const & arg);
-
-        virtual void v_nextInterpreter() const;
-        virtual void v_finalize();
-        virtual void v_dump(std::ostream & os) const;
-
-        friend class Packet;
+#ifndef DOXYGEN
+        typedef PacketTypeMixin<UDPPacketType> mixin;
+#endif
+        typedef ConcretePacket<UDPPacketType> packet; ///< UDP packet typedef
+        typedef UDPPacketParser parser;               ///< typedef to the parser of UDP packet
+
+        using mixin::nextPacketRange;
+        using mixin::initSize;
+        using mixin::init;
+
+        /** \brief Dump given UDPPacket in readable form to given output stream */
+        static void dump(packet p, std::ostream & os);
+        
+        static void finalize(packet p); ///< Finalize packet.
+                                        /**< \li set \ref UDPPacketParser::length() "length" from 
+                                               payload size
+                                             \li calculate and set \ref UDPPacketParser::checksum() 
+                                               "checksum" */
     };
+
+    /** \brief UDP packet typedef */
+    typedef ConcretePacket<UDPPacketType> UDPPacket;
 }
 
 
 ///////////////////////////////hh.e////////////////////////////////////////
-//#include UDPPacket.cci"
+//#include "UDPPacket.cci"
 //#include "UDPPacket.ct"
-#include "UDPPacket.cti"
+//#include "UDPPacket.cti"
 #endif
 
 \f
@@ -96,4 +131,6 @@ namespace senf {
 // c-file-style: "senf"
 // indent-tabs-mode: nil
 // ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
 // End: