Socket/SocketHandle.test: bugfix of Rev. 1277
[senf.git] / Packets / 80221Bundle / MIHPacket.hh
index 90765c3..fa819b5 100644 (file)
 #include "TLVPacket.hh"
 #include <boost/function_output_iterator.hpp>
 #include <boost/iterator/filter_iterator.hpp>
+#include "boost/variant.hpp"
 
 
 //#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();
+        MIHFId(senf::MACAddress const & addr);
+        MIHFId(senf::INet4Address const & addr);
+        MIHFId(senf::INet6Address const & addr);
+        MIHFId(std::string const & addr);
+        MIHFId(senf::EUI64 const & addr);
+        
+        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;
+            }
+        };
+    };
     
-#   define SENF_MIH_PACKET_REGISTRY_REGISTER( sid, opcode, aid, type )                             \
-        SENF_PACKET_REGISTRY_REGISTER(                                                             \
-            senf::MIHMessageRegistry,                                                              \
-            boost::uint16_t((boost::uint16_t(sid) << 12) | (boost::uint16_t(opcode) << 10) | aid), \
-            type )
     
     /** \brief Parse a MIHF_ID
 
@@ -79,7 +130,12 @@ namespace senf {
 
         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 {
@@ -138,7 +194,8 @@ namespace senf {
         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 );
@@ -186,6 +243,7 @@ namespace senf {
         static factory_t nextPacketType(packet p);
         
         enum ResponseStatus { Success, UnspecifiedFailure, Rejected, AuthorizationFailure, NetworkError };
+        enum Opcode { Request=1, Response, Indication }; 
     };
 
     /** \brief MIH packet typedef */
@@ -224,9 +282,7 @@ namespace senf {
 
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
-//#include "MIHPacket.cci"
+#include "MIHPacket.cci"
 //#include "MIHPacket.ct"
 //#include "MIHPacket.cti"
 #endif