added useful table for IPv4 header
[senf.git] / Packets / DefaultBundle / IpV4Packet.hh
index c856644..004efaf 100644 (file)
 // Free Software Foundation, Inc.,
 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
+/** \file
+    \brief IpV4Packet public header */
+
 #ifndef HH_IpV4Packet_
 #define HH_IpV4Packet_ 1
 
 // Custom includes
-#include "Packets/Packet.hh"
-#include "Packets/ParseInt.hh"
-#include "Packets/ParseArray.hh"
-#include "Packets/PacketRegistry.hh"
+#include "../../Socket/Protocols/INet/INet4Address.hh"
+#include "../../Packets/Packets.hh"
 
 //#include "IpV4Packet.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
 
 namespace senf {
 
+    /** \brief Parse in IpV4 address
 
-    template <class Iterator=nil, class IpV4Packet=nil>
-    struct Parse_IpV4 : public ParserBase<Iterator,IpV4Packet>
+        \see INet4Address
+     */
+    struct Parse_INet4Address : public PacketParserBase
     {
-        template <class I, class P=nil>
-        struct rebind { typedef Parse_IpV4<I,P> parser; };
-        typedef Iterator byte_iterator;
+        Parse_INet4Address(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
 
-        Parse_IpV4() {}
-        Parse_IpV4(Iterator const & i) : ParserBase<Iterator,IpV4Packet>(i) {}
+        ///////////////////////////////////////////////////////////////////////////
 
-        static unsigned bytes() { return 20; }
+        typedef INet4Address value_type;
+        static const size_type fixed_bytes = 4u;
 
-        ///////////////////////////////////////////////////////////////////////////
+        value_type value() const { return value_type::from_data(i()); }
+        void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
+        operator value_type() { return value(); }
+        byte & operator[](size_type index) { return *boost::next(i(),index); }
+        Parse_INet4Address const & operator= (value_type const & other) 
+            { value(other); return *this; }
+    };
 
-        typedef Parse_UIntField <  0,  4, Iterator > Parse_Version;
-        typedef Parse_UIntField <  4,  8, Iterator > Parse_IHL;
-        typedef Parse_UInt8     <         Iterator > Parse_8bit;
-        typedef Parse_UInt16    <         Iterator > Parse_16bit;
-        typedef Parse_Flag      <  0,     Iterator > Parse_R;
-        typedef Parse_Flag      <  1,     Iterator > Parse_DF;
-        typedef Parse_Flag      <  2,     Iterator > Parse_MF;
-        typedef Parse_UIntField <  3, 16, Iterator > Parse_Frag;
-        typedef Parse_UInt32    <         Iterator > Parse_32bit;
-
-        Parse_Version  version()     const { return Parse_Version (this->i()      ); }
-        Parse_IHL      ihl()         const { return Parse_IHL     (this->i()      ); }
-        Parse_8bit     tos()         const { return Parse_8bit    (this->i() + 1  ); }
-        Parse_16bit    length()      const { return Parse_16bit   (this->i() + 2  ); }
-        Parse_16bit    identifier()  const { return Parse_16bit   (this->i() + 4  ); }
-        Parse_R        reserved()    const { return Parse_R       (this->i() + 6  ); }
-        Parse_DF       df()          const { return Parse_DF      (this->i() + 6  ); }
-        Parse_MF       mf()          const { return Parse_MF      (this->i() + 6  ); }
-        Parse_Frag     frag()        const { return Parse_Frag    (this->i() + 6  ); }
-        Parse_8bit     ttl()         const { return Parse_8bit    (this->i() + 8  ); }
-        Parse_8bit     protocol()    const { return Parse_8bit    (this->i() + 9  ); }
-        Parse_16bit    crc()         const { return Parse_16bit   (this->i() + 10 ); }
-        Parse_32bit    source()      const { return Parse_32bit   (this->i() + 12 ); }
-        Parse_32bit    destination() const { return Parse_32bit   (this->i() + 16 ); }
+    /** \brief Parse an IpV4 packet
+
+        Parser implementing the IpV4 header.
+        
+        \see IpV4PacketType \n
+            <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
+
+        \todo Implement options
+     */
+    struct Parse_IpV4 : public PacketParserBase
+    {
+#       include SENF_FIXED_PARSER()
+
+        SENF_PARSER_BITFIELD( version,   4, unsigned );
+        SENF_PARSER_BITFIELD( ihl,       4, unsigned );
+
+        SENF_PARSER_FIELD( tos,         Parse_UInt8        );
+        SENF_PARSER_FIELD( length,      Parse_UInt16       );
+        SENF_PARSER_FIELD( identifier,  Parse_UInt16       );
+
+        SENF_PARSER_BITFIELD( reserved,  1, bool     );
+        SENF_PARSER_BITFIELD( df,        1, bool     );
+        SENF_PARSER_BITFIELD( mf,        1, bool     );
+        SENF_PARSER_BITFIELD( frag,     13, unsigned );
+
+        SENF_PARSER_FIELD( ttl,         Parse_UInt8        );
+        SENF_PARSER_FIELD( protocol,    Parse_UInt8        );
+        SENF_PARSER_FIELD( checksum,    Parse_UInt16       );
+        SENF_PARSER_FIELD( source,      Parse_INet4Address );
+        SENF_PARSER_FIELD( destination, Parse_INet4Address );
+
+        SENF_PARSER_INIT() {
+            version() = 4;
+            // We don't support option headers at the moment ...
+            ihl() = 5;
+        }
+
+        SENF_PARSER_FINALIZE(Parse_IpV4);
+        
+        boost::uint16_t calcChecksum() const;
+
+        bool validateChecksum() const {
+            return checksum() == calcChecksum();
+        }
     };
 
+    /** \brief IP protocol number registry
+
+        This registeres packets with their IP protocol number.
+
+        \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
+            PacketRegistry
+     */
     struct IpTypes {
-        // See http://www.iana.org/assignments/protocol-numbers
-        // Also used by IPv6
         typedef boost::uint16_t key_t;
     };
 
-    class IpV4Packet
-        : public Packet,
-          public Parse_IpV4<Packet::iterator,IpV4Packet>,
-          public PacketRegistryMixin<IpTypes,IpV4Packet>
+    /** \brief IpV4 packet
+
+        <table class="packet" cellpadding="5" cellspacing="1" border="1">
+          <tr>
+            <th width="12%">0</th> <th width="12%">4</th> <th width="12%">8</th>
+            <th width="12%">12</th> <th width="3%">16</th>
+            <th width="3%"></th> <th width="3%"></th> <th width="3%"></th>
+            <th width="12%">20</th> <th width="12%">24</th> <th width="6%">28</th>
+            <th style="text-align:right" width="6%">31</th>
+          </tr><tr>
+            <td>\ref Parse_IpV4::version() "Version"</td>
+            <td>\ref Parse_IpV4::ihl() "IHL"</td>
+            <td colspan="2">\ref Parse_IpV4::tos() "TOS"</td>
+            <td colspan="8">\ref Parse_IpV4::length() "Length"</td> 
+          </tr><tr>
+            <td colspan="4">\ref Parse_IpV4::identifier() "Identifier"</td>
+            <td>\ref Parse_IpV4::reserved() "R"</td>
+            <td>\ref Parse_IpV4::df() "DF"</td>
+            <td>\ref Parse_IpV4::mf() "MF"</td>
+            <td colspan="5">\ref Parse_IpV4::frag() "Fragment Offset"</td>
+          </tr><tr>
+            <td colspan="2">\ref Parse_IpV4::ttl() "Time to Live (ttl)"</td>
+            <td colspan="2">\ref Parse_IpV4::protocol() "Protocol"</td>
+            <td colspan="8">\ref Parse_IpV4::checksum() "Header Checksum"</td>
+          </tr><tr>
+            <td colspan="12">\ref Parse_IpV4::source() "Source Address"</td>
+          </tr><tr>
+            <td colspan="12">\ref Parse_IpV4::destination() "Destination Address"</td>
+          </tr>
+        </table>
+        
+        \par Packet type (typedef):
+            \ref IpV4Packet
+
+        \par Fields:
+            \ref Parse_IpV4
+
+        \par Associated registries:
+            \ref IpTypes
+
+        \par Finalize action:
+            Set \a length from payload size\n
+            Set \a protocol from type of next packet if found in \ref IpTypes\n
+            Calculate \a checksum
+
+        \ingroup protocolbundle_default
+     */
+    struct IpV4PacketType
+        : public PacketTypeBase,
+          public PacketTypeMixin<IpV4PacketType, IpTypes>
     {
-        using PacketRegistryMixin<IpTypes,IpV4Packet>::registerInterpreter;
-    public:
-        ///////////////////////////////////////////////////////////////////////////
-        // Types
-
-        typedef ptr_t<IpV4Packet>::ptr ptr;
-
-        ///////////////////////////////////////////////////////////////////////////
-
-    private:
-        template <class Arg>
-        IpV4Packet(Arg const & arg);
+#ifndef DOXYGEN
+        typedef PacketTypeMixin<IpV4PacketType, IpTypes> mixin;
+        typedef ConcretePacket<IpV4PacketType> packet;
+        typedef Parse_IpV4 parser;
+#endif
+        using mixin::nextPacketRange;
+        using mixin::nextPacketType;
+        using mixin::initSize;
+        using mixin::init;
 
-        virtual void v_nextInterpreter() const;
-        virtual void v_finalize();
-        virtual void v_dump(std::ostream & os) const;
+        static registry_key_t nextPacketKey(packet p) 
+            { return p->protocol(); }
 
-        friend class Packet;
+        static void dump(packet p, std::ostream & os);
+        static void finalize(packet p);
     };
+        
+    /** \brief IpV4 packet typedef */
+    typedef ConcretePacket<IpV4PacketType> IpV4Packet;
 }
 
 
 ///////////////////////////////hh.e////////////////////////////////////////
+#endif
+#ifndef SENF_PACKETS_DECL_ONLY
 //#include IpV4Packet.cci"
 //#include "IpV4Packet.ct"
-#include "IpV4Packet.cti"
+//#include "IpV4Packet.cti"
 #endif
 
 \f
@@ -122,4 +202,5 @@ namespace senf {
 // indent-tabs-mode: nil
 // ispell-local-dictionary: "american"
 // compile-command: "scons -u test"
+// comment-column: 40
 // End: