switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / 80221Bundle / MIHPacket.hh
index 75ef61b..af90a4b 100644 (file)
@@ -2,23 +2,28 @@
 //
 // Copyright (C) 2009
 // 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
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
+// The contents of this file are subject to the Fraunhofer FOKUS Public License
+// Version 1.0 (the "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at 
+// http://senf.berlios.de/license.html
 //
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
+// The Fraunhofer FOKUS Public License Version 1.0 is based on, 
+// but modifies the Mozilla Public License Version 1.1.
+// See the full license text for the amendments.
 //
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the
-// Free Software Foundation, Inc.,
-// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+// Software distributed under the License is distributed on an "AS IS" basis, 
+// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
+// for the specific language governing rights and limitations under the License.
+//
+// The Original Code is Fraunhofer FOKUS code.
+//
+// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
+// (registered association), Hansastraße 27 c, 80686 Munich, Germany.
+// All Rights Reserved.
+//
+// Contributor(s):
+//   Thorsten Horstmann <tho@berlios.de>
 
 /** \file
     \brief MIH protocol public header */
 
 // Custom includes
 #include <senf/Packets/Packets.hh>
-#include <senf/Socket/Protocols/Raw/MACAddress.hh>
-#include <senf/Socket/Protocols/INet/INet4Address.hh>
-#include <senf/Socket/Protocols/INet/INet6Address.hh>
-#include "TLVPacket.hh"
-#include <boost/function_output_iterator.hpp>
-#include <boost/iterator/filter_iterator.hpp>
-#include <boost/variant.hpp>
-
+#include "TLVParser.hh"
+#include "MIHMessageRegistry.hh"
 
 //#include "MIHPacket.mpp"
-///////////////////////////////hh.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 
 namespace senf {
 
-    struct MIHMessageRegistry {
-        // MIH messages registry
-        typedef boost::uint16_t key_t;
-    };
-
-#   define SENF_MIH_PACKET_REGISTRY_REGISTER( packet )                    \
-        SENF_PACKET_REGISTRY_REGISTER(                                    \
-            senf::MIHMessageRegistry, packet::type::MESSAGE_ID, packet )
-    
-    class MIHFId 
-        : public boost::variant< boost::blank, senf::MACAddress, senf::INet4Address, 
-                senf::INet6Address, std::string, senf::EUI64 >,
-          public boost::less_than_comparable<MIHFId>,
-          public boost::equality_comparable<MIHFId>
-    {
-    public:
-        enum Type { Empty, MACAddress, INet4Address, INet6Address, String, EUI64 };
-      
-        MIHFId();                                   ///< Create empty instance.
-        MIHFId(senf::MACAddress const & addr);      ///< Construct id with given MACAddress 
-        MIHFId(senf::INet4Address const & addr);    ///< Construct id with given INet4Address
-        MIHFId(senf::INet6Address const & addr);    ///< Construct id with given INet6Address
-        MIHFId(std::string const & addr);           ///< Construct id with given String
-        MIHFId(senf::EUI64 const & addr);           ///< Construct id with given EUI64
-        
-        Type type() const;
-        bool operator==(MIHFId const & other) const;
-        bool operator<(MIHFId const & other) const; 
-        
-    private:
-        struct GetTypeVisitor : public boost::static_visitor<Type> {
-            Type operator()(boost::blank const &) const { return Empty; }
-            Type operator()(senf::MACAddress const &) const { return MACAddress; }
-            Type operator()(senf::INet4Address const &) const { return INet4Address; }
-            Type operator()(senf::INet6Address const &) const { return INet6Address; }
-            Type operator()(std::string const & ) const { return String; }
-            Type operator()(senf::EUI64 const &) const { return EUI64; }
-        };
-        struct EqualsVisitor : public boost::static_visitor<bool> {
-            template <typename T, typename U>
-            bool operator()(T const &, U const &) const {
-                return false;
-            }
-            template <typename T>
-            bool operator()( const T & lhs, const T & rhs ) const {
-                return lhs == rhs;
-            }
-        };
-        struct LessThanVisitor : public boost::static_visitor<bool> {
-            template <typename T, typename U>
-            bool operator()(T const &, U const &) const {
-                return false;
-            }
-            template <typename T>
-            bool operator()( const T & lhs, const T & rhs ) const {
-                return lhs < rhs;
-            }
-        };
-    };
-    
-    
-    /** \brief Parse a MIHF_ID
-
-         the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
-         we could set maxLengthValue in init(), but for the most MIHF_IDs the default
-         maximum length of 127 should be enough.
-         
-         \note you must call mihfIdPacket.maxLengthValue( 253) *before*
-         setting longer MIHF_IDs values.
-    */
-    class MIHFId_TLVParser : public BaseTLVPacketParser
-    {
-    #   include SENF_PARSER()
-        SENF_PARSER_INHERIT  ( BaseTLVPacketParser );
-        SENF_PARSER_SKIP     ( length(), 0         );
-        SENF_PARSER_FINALIZE ( MIHFId_TLVParser    );
-        
-    public:
-        std::string asString() const;
-        void setString(std::string const &id);
-
-        senf::MACAddress asMACAddress() const;
-        void setMACAddress(senf::MACAddress const &mac);
-
-        senf::INet4Address asINet4Address() const;
-        void setINet4Address(senf::INet4Address const &addr);
-
-        senf::INet6Address asINet6Address() const;
-        void setINet6Address(senf::INet6Address const &addr);
-        
-        senf::EUI64 asEUI64() const;
-        void setEUI64(senf::EUI64 const &addr);
-
-        MIHFId valueAs(MIHFId::Type type) const;
-        
-    private:
-        template <class OutputIterator>
-        struct binaryNAIEncoder {
-            binaryNAIEncoder(OutputIterator &i) : i_(i) {}
-            void operator()(const boost::uint8_t &v) const {
-                *i_++ = '\\';
-                *i_++ = v;
-            }
-            OutputIterator &i_;
-        };
-        template <class OutputIterator>
-        static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > getNAIEncodedOutputIterator(OutputIterator i) {
-            return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
-        }
-
-        struct binaryNAIDecoder {
-            binaryNAIDecoder() : readNextByte_(true) {}
-            bool operator()(const boost::uint8_t &v) {
-                readNextByte_ = readNextByte_ ? false : true;
-                return readNextByte_;
-            }
-            bool readNextByte_;
-        };
-        template <class Iterator>
-        static boost::filter_iterator<binaryNAIDecoder, Iterator> getNAIDecodedIterator(Iterator begin, Iterator end) {
-            return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
-        }
-    };
     /** \brief Parse a MIH packet
 
         Parser implementing the MIH header. The fields implemented are:
@@ -191,24 +66,23 @@ namespace senf {
         SENF_PARSER_BITFIELD ( sid,     4,  unsigned   );
         SENF_PARSER_BITFIELD ( opcode,  2,  unsigned   );
         SENF_PARSER_BITFIELD ( aid,    10,  unsigned   );
-        
+
         SENF_PARSER_SKIP_BITS ( 4                           );
         SENF_PARSER_BITFIELD  ( transactionId, 12, unsigned );
         SENF_PARSER_FIELD_RO  ( payloadLength, UInt16Parser );
-        
+
         SENF_PARSER_GOTO_OFFSET( 8, 8); // just to limit the offset calculation
-        
+
         // Source MIHF Id
-        SENF_PARSER_FIELD ( src_mihfId, MIHFId_TLVParser );
+        SENF_PARSER_FIELD ( src_mihfId, MIHFSrcIdTLVParser );
         // Destination MIHF Id
-        SENF_PARSER_FIELD ( dst_mihfId, MIHFId_TLVParser );
+        SENF_PARSER_FIELD ( dst_mihfId, MIHFDstIdTLVParser );
 
         SENF_PARSER_FINALIZE ( MIHPacketParser );
 
         SENF_PARSER_INIT() {
+            defaultInit();
             version_() = 1;
-            src_mihfId().type() = 1;
-            dst_mihfId().type() = 2;
         }
 
         friend class MIHPacketType;
@@ -228,9 +102,7 @@ namespace senf {
         : public PacketTypeBase,
           public PacketTypeMixin<MIHPacketType, MIHMessageRegistry>
     {
-#ifndef DOXYGEN
         typedef PacketTypeMixin<MIHPacketType, MIHMessageRegistry> mixin;
-#endif
         typedef ConcretePacket<MIHPacketType> packet; ///< MIH packet typedef
         typedef MIHPacketParser parser;               ///< typedef to the parser of MIH packet
 
@@ -239,47 +111,53 @@ namespace senf {
         using mixin::initSize;
 
         /** \brief Dump given MIH packet in readable form to given output stream */
-        static void dump(packet p, std::ostream &os);
+        static void dump(packet p, std::ostream & os);
         static void finalize(packet p);
         static factory_t nextPacketType(packet p);
+        static void validate(packet p);
+
+        static const boost::uint16_t etherType = 0x8917;
     };
 
-    /** \brief MIH packet typedef */
+    /** \brief MIH packet typedef
+        \ingroup protocolbundle_80221
+     */
     typedef ConcretePacket<MIHPacketType> MIHPacket;
 
 
-    struct MIHPayloadPacketParser : public PacketParserBase
+    struct MIHGenericPayloadPacketParser : public PacketParserBase
     {
     #   include SENF_PARSER()
-        SENF_PARSER_LIST ( tlv_list, packetSize(), GenericTLVPacketParser );
-        SENF_PARSER_FINALIZE ( MIHPayloadPacketParser );
+        SENF_PARSER_LIST ( tlvList, packetSize(), MIHGenericTLVParser );
+        SENF_PARSER_FINALIZE ( MIHGenericPayloadPacketParser );
     };
 
-    struct MIHPayloadPacketType
+    struct MIHGenericPayloadPacketType
         : public PacketTypeBase,
-          public PacketTypeMixin<MIHPayloadPacketType>
+          public PacketTypeMixin<MIHGenericPayloadPacketType>
     {
-#ifndef DOXYGEN
-        typedef PacketTypeMixin<MIHPayloadPacketType> mixin;
-#endif
-        typedef ConcretePacket<MIHPayloadPacketType> packet; ///< MIH Payload packet typedef
-        typedef MIHPayloadPacketParser parser; ///< typedef to the parser of MIH Payload packet
+        typedef PacketTypeMixin<MIHGenericPayloadPacketType> mixin;
+        typedef ConcretePacket<MIHGenericPayloadPacketType> packet; ///< MIH Payload packet typedef
+        typedef MIHGenericPayloadPacketParser parser; ///< typedef to the parser of MIH Payload packet
 
         using mixin::nextPacketRange;
         using mixin::init;
         using mixin::initSize;
 
-        /** \brief Dump given MIHPayload in readable form to given output stream */
-        static void dump(packet p, std::ostream &os);
+        /** \brief Dump given MIHGenericPayload in readable form to given output stream */
+        static void dump(packet p, std::ostream & os);
+        static void finalize(packet p);
     };
 
-     /** \brief MIH Payload packet typedef */
-    typedef ConcretePacket<MIHPayloadPacketType> MIHPayloadPacket;
+     /** \brief MIH Payload packet typedef
+         \ingroup protocolbundle_80221
+      */
+    typedef ConcretePacket<MIHGenericPayloadPacketType> MIHGenericPayloadPacket;
 }
 
 
-///////////////////////////////hh.e////////////////////////////////////////
-#include "MIHPacket.cci"
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
+//#include "MIHPacket.cci"
 //#include "MIHPacket.ct"
 //#include "MIHPacket.cti"
 #endif