Packets: Add PacketParserBase::i(size_type) utility
[senf.git] / Packets / DefaultBundle / EthernetPacket.hh
index 5dbec31..ad9c1f9 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 
+//     Thorsten Horstmann <tho@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.
 
+/** \file
+    \brief EthernetPacket public header */
+
 #ifndef HH_EthernetPacket_
 #define HH_EthernetPacket_ 1
 
 // Custom includes
-#include "Packets/Packet.hh"
-#include "Packets/ParseInt.hh"
-#include "Packets/ParseArray.hh"
-#include "Packets/PacketRegistry.hh"
+#include <algorithm>
+#include "../../Socket/Protocols/Raw/MACAddress.hh"
+#include "../../Packets/Packets.hh"
 
 //#include "EthernetPacket.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
 
 namespace senf {
 
+    /** \brief Parse an Ethernet MAC address 
+
+        The ethernet MAC is returned by value as a 6-byte sequence
 
-    template <class Iterator=nil, class IPacket=nil>
-    struct Parse_Ethernet : public ParserBase<Iterator,IPacket>
+        \see MACAddress \n
+            EthernetPacket
+     */
+    struct MACAddressParser : public PacketParserBase
     {
-        template <class I, class P=nil>
-        struct rebind { typedef Parse_Ethernet<I,P> parser; };
-        typedef Iterator byte_iterator;
+        MACAddressParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
+       
+        ///////////////////////////////////////////////////////////////////////////
 
-        Parse_Ethernet() {}
-        Parse_Ethernet(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
+        typedef MACAddress value_type;
+        static const size_type fixed_bytes = 6u;
 
-        static unsigned bytes() { return 14; }
+        value_type value() const { return MACAddress::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);  }
 
-        ///////////////////////////////////////////////////////////////////////////
+        MACAddressParser const & operator= (value_type const & other) { value(other); return *this; }
+    };
+    
+    /** \brief Parse an Ethernet packet
+
+        Parser implementing an ethernet header.
 
-        typedef Parse_Array  < 6, Parse_UInt8<>, Iterator > Parse_MAC;
-        typedef Parse_UInt16 < Iterator  >                  Parse_Type;
+        \see EthernetPacketType
+     */
+    struct EthernetPacketParser : public PacketParserBase
+    {
+#       include SENF_FIXED_PARSER()
 
-        Parse_MAC  destination() const { return Parse_MAC  (this->i() ); }
-        Parse_MAC  source()      const { return Parse_MAC  (this->i() + Parse_MAC::size() ); }
-        Parse_Type type()        const { return Parse_Type (this->i() + 2*Parse_MAC::size() ); }
+        SENF_PARSER_FIELD( destination, MACAddressParser    );
+        SENF_PARSER_FIELD( source,      MACAddressParser    );
+        SENF_PARSER_FIELD( type_length, UInt16Parser );
+
+        SENF_PARSER_FINALIZE(EthernetPacketParser);
     };
 
+    /** \brief EtherType registry
+
+        This registry registers packet types with their EtherType number.
+        
+        \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a> \n
+            \ref PacketRegistry
+     */
     struct EtherTypes {
-        // See http://www.iana.org/assignments/ethernet-numbers
+        // See 
         typedef boost::uint16_t key_t;
     };
 
-    class EthernetPacket
-        : public Packet,
-          public Parse_Ethernet<Packet::iterator, EthernetPacket>,
-          public PacketRegistryMixin<EtherTypes,EthernetPacket>
-    {
-        using PacketRegistryMixin<EtherTypes,EthernetPacket>::registerInterpreter;
-    public:
-        ///////////////////////////////////////////////////////////////////////////
-        // Types
+    /** \brief Ethernet packet
 
-        typedef ptr_t<EthernetPacket>::ptr ptr;
+        \par Packet type (typedef):
+            \ref EthernetPacket
 
-        ///////////////////////////////////////////////////////////////////////////
+        \par Fields:
+            \ref EthernetPacketParser
+
+        \par Associated registries:
+            \ref EtherTypes
 
-    private:
-        template <class Arg>
-        EthernetPacket(Arg const & arg);
+        \par Finalize action:
+            Set \a type from type of next packet if found in \ref EtherTypes
 
-        virtual void v_nextInterpreter() const;
-        virtual void v_finalize();
-        virtual void v_dump(std::ostream & os) const;
+        \ingroup protocolbundle_default
+     */
+    struct EthernetPacketType
+        : public PacketTypeBase,
+          public PacketTypeMixin<EthernetPacketType, EtherTypes>
+    {
+#ifndef DOXYGEN
+        typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
+        typedef ConcretePacket<EthernetPacketType> packet;
+        typedef EthernetPacketParser parser;
+#endif
+        using mixin::nextPacketRange;
+        using mixin::initSize;
+        using mixin::init;
 
-        friend class Packet;
+        static factory_t nextPacketType(packet p);
+        static void dump(packet p, std::ostream & os);
+        static void finalize(packet p);
     };
 
-    template <class Iterator=nil, class IPacket=nil>
-    struct Parse_EthVLan : public ParserBase<Iterator,IPacket>
-    {
-        template <class I, class P=nil>
-        struct rebind { typedef Parse_Ethernet<I,P> parser; };
-        typedef Iterator byte_iterator;
+    /** \brief Ethernet packet typedef */
+    typedef ConcretePacket<EthernetPacketType> EthernetPacket;
 
-        Parse_EthVLan() {}
-        Parse_EthVLan(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
+    /** \brief Parse an ethernet VLAN tag
+        
+        Parser interpreting the ethernet VLAN tag. Fields are
 
-        static unsigned bytes() { return 4; }
+        \see EthVLanPacketType
+     */
+    struct EthVLanPacketParser : public PacketParserBase
+    {
+#       include SENF_FIXED_PARSER()
 
-        ///////////////////////////////////////////////////////////////////////////
+        SENF_PARSER_BITFIELD( priority,  3, unsigned );
+        SENF_PARSER_BITFIELD( cfi,       1, bool     );
+        SENF_PARSER_BITFIELD( vlanId,   12, unsigned );
 
-        typedef Parse_UIntField < 0,  3, Iterator > Parse_Priority;
-        typedef Parse_Flag          < 3, Iterator > Parse_CFI;
-        typedef Parse_UIntField < 4, 16, Iterator > Parse_VLanId;
-        typedef Parse_UInt16           < Iterator > Parse_Type;
+        SENF_PARSER_FIELD( type, UInt16Parser );
 
-        Parse_Priority priority() const { return Parse_Priority(this->i()); }
-        Parse_CFI      cfi()      const { return Parse_CFI(this->i()); }
-        Parse_VLanId   vlanId()   const { return Parse_VLanId(this->i()); }
-        Parse_Type     type()     const { return Parse_Type(this->i()+2); }
+        SENF_PARSER_FINALIZE(EthVLanPacketParser);
     };
 
-    class EthVLanPacket
-        : public Packet,
-          public Parse_EthVLan<Packet::iterator, EthVLanPacket>,
-          public PacketRegistryMixin<EtherTypes, EthVLanPacket>
-    {
-        using PacketRegistryMixin<EtherTypes, EthVLanPacket>::registerInterpreter;
-    public:
-        ///////////////////////////////////////////////////////////////////////////
-        // Types
+    /** \brief Ethernet VLAN tag
 
-        typedef ptr_t<EthVLanPacket>::ptr ptr;
+        \par Packet type (typedef):
+            \ref EthVLanPacket
 
-        ///////////////////////////////////////////////////////////////////////////
+        \par Fields:
+            \ref EthVLanPacketParser
 
-    private:
-        template <class Arg>
-        EthVLanPacket(Arg const & arg);
+        \par Associated registries:
+            \ref EtherTypes
 
-        virtual void v_nextInterpreter() const;
-        virtual void v_finalize();
-        virtual void v_dump(std::ostream & os) const;
+        \par Finalize action:
+            Set \a type from type of next packet if found in \ref EtherTypes
 
-        friend class Packet;
+        \ingroup protocolbundle_default
+     */
+    struct EthVLanPacketType
+        : public PacketTypeBase, 
+          public PacketTypeMixin<EthVLanPacketType, EtherTypes>
+    {
+#ifndef DOXYGEN
+        typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
+        typedef ConcretePacket<EthVLanPacketType> packet;
+        typedef EthVLanPacketParser parser;
+#endif
+        using mixin::nextPacketRange;
+        using mixin::nextPacketType;
+        using mixin::initSize;
+        using mixin::init;
+
+        /** \todo Add LLC/SNAP support -> only use the registry
+            for type() values >=1536, otherwise expect an LLC header */
+        static key_t nextPacketKey(packet p) 
+            { return p->type(); }
+
+        static void dump(packet p, std::ostream & os);
+        static void finalize(packet p);
     };
 
-}
+    /** \brief Ethernet VLAN tag typedef */
+    typedef ConcretePacket<EthVLanPacketType> EthVLanPacket;
 
+}
 
 ///////////////////////////////hh.e////////////////////////////////////////
+#endif
+#ifndef SENF_PACKETS_DECL_ONLY
 //#include "EthernetPacket.cci"
 //#include "EthernetPacket.ct"
-#include "EthernetPacket.cti"
+//#include "EthernetPacket.cti"
 #endif
 
 \f
@@ -153,4 +204,6 @@ namespace senf {
 // c-file-style: "senf"
 // indent-tabs-mode: nil
 // ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
 // End: