Packets/80221Bundle: added MIHFId class
tho [Fri, 17 Jul 2009 17:12:55 +0000 (17:12 +0000)]
Packets/DefaultBundle: removed obsoleted SENF_PACKETS_DECL_ONLY ifdefs

git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1273 270642c3-0616-0410-b53a-bc976706d245

Packets/80221Bundle/MIHPacket.cc
Packets/80221Bundle/MIHPacket.cci [new file with mode: 0644]
Packets/80221Bundle/MIHPacket.hh
Packets/80221Bundle/MIHPacket.test.cc
Packets/DefaultBundle/EthernetPacket.hh
Packets/DefaultBundle/ICMPv6Packet.hh
Packets/DefaultBundle/IPv4Packet.hh
Packets/DefaultBundle/IPv6Extensions.hh
Packets/DefaultBundle/IPv6Packet.hh
Packets/DefaultBundle/LlcSnapPacket.hh
Packets/DefaultBundle/UDPPacket.hh

index a81d0fa..3e8b050 100644 (file)
 #include <boost/io/ios_state.hpp>
 
 #define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
 
 
-prefix_ std::string senf::MIHFId_TLVParser::asString()
-    const
-{
-    return std::string( i(1+length_bytes()), i(1+length_bytes()+length()) );
-}
+
+///////////////////////////////////////////////////////////////////////////
+// MIHFId_TLVParser
 
 prefix_ void senf::MIHFId_TLVParser::setString(std::string const &id)
 {
@@ -50,46 +49,55 @@ prefix_ void senf::MIHFId_TLVParser::setString(std::string const &id)
     std::copy( id.begin(), id.end(), si);
 }
 
-prefix_ senf::MACAddress senf::MIHFId_TLVParser::asMACAddress()
-    const
-{
-    return MACAddress::from_data( 
-            getNAIDecodedIterator( i(1+length_bytes()), i(1+length_bytes()+12) ));
-}
-
 prefix_ void senf::MIHFId_TLVParser::setMACAddress(senf::MACAddress const &mac)
 {
     safe_data_iterator si = resizeValueField(12);
     std::copy( mac.begin(), mac.end(), getNAIEncodedOutputIterator(si));
 }
 
-
-prefix_ senf::INet4Address senf::MIHFId_TLVParser::asINet4Address()
-    const
-{
-    return INet4Address::from_data( 
-            getNAIDecodedIterator( i(1+length_bytes()), i(1+length_bytes()+8) ));
-}
-
 prefix_ void senf::MIHFId_TLVParser::setINet4Address(senf::INet4Address const &addr)
 {
     safe_data_iterator si = resizeValueField(8);
     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
 }
 
-prefix_ senf::INet6Address senf::MIHFId_TLVParser::asINet6Address()
-    const
+prefix_ void senf::MIHFId_TLVParser::setINet6Address(senf::INet6Address const &addr)
 {
-    return INet6Address::from_data( 
-            getNAIDecodedIterator( i(1+length_bytes()), i(1+length_bytes()+32) ));
+    safe_data_iterator si = resizeValueField(32);
+    std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
 }
 
-prefix_ void senf::MIHFId_TLVParser::setINet6Address(senf::INet6Address const &addr)
+prefix_ void senf::MIHFId_TLVParser::setEUI64(senf::EUI64 const &addr)
 {
-    safe_data_iterator si = resizeValueField(32);
+    safe_data_iterator si = resizeValueField(16);
     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
 }
 
+prefix_ senf::MIHFId senf::MIHFId_TLVParser::valueAs(MIHFId::Type type)
+    const
+{
+    if (length() == 0) return MIHFId();
+    switch (type) {
+    case MIHFId::Empty:
+        return MIHFId();
+    case MIHFId::MACAddress:
+        return MIHFId( asMACAddress());
+    case MIHFId::INet4Address:
+        return MIHFId( asINet4Address());
+    case MIHFId::INet6Address:
+        return MIHFId( asINet6Address());
+    case MIHFId::String:
+        return MIHFId( asINet6Address());
+    case MIHFId::EUI64:
+        return MIHFId( asINet6Address());
+    }
+    return MIHFId();
+}
+
+
+///////////////////////////////////////////////////////////////////////////
+// MIHPacketType
+
 prefix_ void senf::MIHPacketType::dump(packet p, std::ostream &os)
 {
     boost::io::ios_all_saver ias(os);
@@ -144,6 +152,7 @@ prefix_ void senf::MIHPayloadPacketType::dump(packet p, std::ostream &os)
        << "  ToDo!\n";
 }
 
+///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 
 \f
diff --git a/Packets/80221Bundle/MIHPacket.cci b/Packets/80221Bundle/MIHPacket.cci
new file mode 100644 (file)
index 0000000..cee4c2c
--- /dev/null
@@ -0,0 +1,133 @@
+// $Id$
+//
+// 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.
+//
+// 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.
+//
+// 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.
+
+/** \file
+    \brief MIH protocol inline non-template implementation */
+
+//#include "MIHPacket.ih"
+
+// Custom includes
+
+#define prefix_ inline
+///////////////////////////////cci.p////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// MIHFId
+prefix_ senf::MIHFId::MIHFId()
+    :  boost::variant< boost::blank, senf::MACAddress, senf::INet4Address, 
+            senf::INet6Address, std::string, senf::EUI64 > ( boost::blank())
+{ }
+
+prefix_ senf::MIHFId::MIHFId(senf::MACAddress const & addr)
+    :  boost::variant< boost::blank, senf::MACAddress, senf::INet4Address, 
+            senf::INet6Address, std::string, senf::EUI64 > ( addr)
+{ }
+
+prefix_ senf::MIHFId::MIHFId(senf::INet4Address const & addr)
+    :  boost::variant< boost::blank, senf::MACAddress, senf::INet4Address, 
+            senf::INet6Address, std::string, senf::EUI64 > ( addr)
+{ }
+
+prefix_ senf::MIHFId::MIHFId(senf::INet6Address const & addr)
+    :  boost::variant< boost::blank, senf::MACAddress, senf::INet4Address, 
+            senf::INet6Address, std::string, senf::EUI64 > ( addr)
+{ }
+
+prefix_ senf::MIHFId::MIHFId(std::string const & addr)
+    :  boost::variant< boost::blank, senf::MACAddress, senf::INet4Address, 
+            senf::INet6Address, std::string, senf::EUI64 > ( addr)
+{ }
+
+prefix_ senf::MIHFId::MIHFId(senf::EUI64 const & addr)
+    :  boost::variant< boost::blank, senf::MACAddress, senf::INet4Address, 
+            senf::INet6Address, std::string, senf::EUI64 > ( addr)
+{ }
+
+prefix_ senf::MIHFId::Type senf::MIHFId::type()
+    const
+{
+    return boost::apply_visitor( GetTypeVisitor(), *this );
+}
+
+prefix_ bool senf::MIHFId::operator==(senf::MIHFId const & other)
+    const
+{
+    return boost::apply_visitor( EqualsVisitor(), *this, other);
+}
+
+prefix_ bool senf::MIHFId::operator<(senf::MIHFId const & other)
+    const 
+{
+    if (type() != other.type()) return type() < other.type();
+    return boost::apply_visitor( LessThanVisitor(), *this, other);
+}
+
+///////////////////////////////////////////////////////////////////////////
+// MIHFId_TLVParser
+
+prefix_ std::string senf::MIHFId_TLVParser::asString()
+    const
+{
+    return std::string( i(1+length_bytes()), i(1+length_bytes()+length()) );
+}
+
+prefix_ senf::MACAddress senf::MIHFId_TLVParser::asMACAddress()
+    const
+{
+    return MACAddress::from_data( 
+            getNAIDecodedIterator( i(1+length_bytes()), i(1+length_bytes()+12) ));
+}
+
+prefix_ senf::INet4Address senf::MIHFId_TLVParser::asINet4Address()
+    const
+{
+    return INet4Address::from_data( 
+            getNAIDecodedIterator( i(1+length_bytes()), i(1+length_bytes()+8) ));
+}
+
+prefix_ senf::INet6Address senf::MIHFId_TLVParser::asINet6Address()
+    const
+{
+    return INet6Address::from_data( 
+            getNAIDecodedIterator( i(1+length_bytes()), i(1+length_bytes()+32) ));
+}
+
+prefix_ senf::EUI64 senf::MIHFId_TLVParser::asEUI64()
+    const
+{
+    return EUI64::from_data( 
+            getNAIDecodedIterator( i(1+length_bytes()), i(1+length_bytes()+16) ));
+}
+
+///////////////////////////////cci.e////////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End:
index b5c7f94..cafac5a 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( packetType )                                         \
-        SENF_PACKET_REGISTRY_REGISTER(                                                             \
-            senf::MIHMessageRegistry, packetType::type::MESSAGE_ID, packetType )
     
     /** \brief Parse a MIHF_ID
 
@@ -77,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 {
@@ -223,9 +281,7 @@ namespace senf {
 
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
-//#include "MIHPacket.cci"
+#include "MIHPacket.cci"
 //#include "MIHPacket.ct"
 //#include "MIHPacket.cti"
 #endif
index 91c6dc5..e9a040e 100644 (file)
 ///////////////////////////////cc.p////////////////////////////////////////
 using namespace senf;
 
+BOOST_AUTO_UNIT_TEST(MIHPacket_MIHFId)
+{
+    MIHFId id ( MACAddress::from_string("01:02:03:04:05:06"));
+    BOOST_CHECK_EQUAL( id.type(), MIHFId::MACAddress);
+    BOOST_CHECK_EQUAL( id, MIHFId( MACAddress::from_string("01:02:03:04:05:06")));
+    BOOST_CHECK( id != MIHFId( MACAddress::from_string("01:02:03:04:05:07")));
+    BOOST_CHECK( id != MIHFId( INet4Address::from_string("128.129.130.131")));
+    BOOST_CHECK( id < MIHFId( MACAddress::from_string("01:02:03:04:05:07")));
+    BOOST_CHECK( id < MIHFId( INet4Address::from_string("128.129.130.131")));
+}
+
 BOOST_AUTO_UNIT_TEST(MIHPacket_msgId)
 {
     MIHPacket mihPacket (MIHPacket::create());
index 4332391..51a2389 100644 (file)
@@ -195,8 +195,6 @@ namespace senf {
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
 //#include "EthernetPacket.cci"
 //#include "EthernetPacket.ct"
 //#include "EthernetPacket.cti"
index 032bfa3..bc7d9c2 100644 (file)
@@ -93,8 +93,6 @@ namespace senf
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
 //#include "IPv4Packet.cci"
 //#include "IPv4Packet.ct"
 //#include "IPv4Packet.cti"
index e124ee4..0cdae50 100644 (file)
@@ -201,8 +201,6 @@ namespace senf {
 
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
 //#include "IPv4Packet.cci"
 //#include "IPv4Packet.ct"
 //#include "IPv4Packet.cti"
index aa2d331..197f5c9 100644 (file)
@@ -351,8 +351,6 @@ The Type 0 Routing header has the following format: (RFC 2460)
     
 } //namespace senf
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
 //#include "IPv6Extensions.cci"
 //#include "IPv6Extensions.ct"
 //#include "IPv6Extensions.cti"
index 2ce41a6..c8a06d5 100644 (file)
@@ -159,8 +159,6 @@ namespace senf {
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
 //#include "IPv6Packet.cci"
 //#include "IPv6Packet.ct"
 //#include "IPv6Packet.cti"
index 5d0f7e9..33c0069 100644 (file)
@@ -106,8 +106,6 @@ namespace senf {
 
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
 //#include "LlcSnapPacket.cci"
 //#include "LlcSnapPacket.ct"
 //#include "LlcSnapPacket.cti"
index e85c182..456b325 100644 (file)
@@ -119,8 +119,6 @@ namespace senf {
 
 
 ///////////////////////////////hh.e////////////////////////////////////////
-#endif
-#ifndef SENF_PACKETS_DECL_ONLY
 //#include "UDPPacket.cci"
 //#include "UDPPacket.ct"
 //#include "UDPPacket.cti"