From: tho Date: Fri, 17 Jul 2009 17:12:55 +0000 (+0000) Subject: Packets/80221Bundle: added MIHFId class X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=8674fb1d184e7d620eb7c604957b5b4203df37a6;p=senf.git Packets/80221Bundle: added MIHFId class 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 --- diff --git a/Packets/80221Bundle/MIHPacket.cc b/Packets/80221Bundle/MIHPacket.cc index a81d0fa..3e8b050 100644 --- a/Packets/80221Bundle/MIHPacket.cc +++ b/Packets/80221Bundle/MIHPacket.cc @@ -32,13 +32,12 @@ #include #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_ diff --git a/Packets/80221Bundle/MIHPacket.cci b/Packets/80221Bundle/MIHPacket.cci new file mode 100644 index 0000000..cee4c2c --- /dev/null +++ b/Packets/80221Bundle/MIHPacket.cci @@ -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 +// +// 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_ + + +// 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: diff --git a/Packets/80221Bundle/MIHPacket.hh b/Packets/80221Bundle/MIHPacket.hh index b5c7f94..cafac5a 100644 --- a/Packets/80221Bundle/MIHPacket.hh +++ b/Packets/80221Bundle/MIHPacket.hh @@ -34,21 +34,74 @@ #include "TLVPacket.hh" #include #include +#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, + public boost::equality_comparable + { + 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 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 { + template + bool operator()(T const &, U const &) const { + return false; + } + template + bool operator()( const T & lhs, const T & rhs ) const { + return lhs == rhs; + } + }; + struct LessThanVisitor : public boost::static_visitor { + template + bool operator()(T const &, U const &) const { + return false; + } + template + 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 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 diff --git a/Packets/80221Bundle/MIHPacket.test.cc b/Packets/80221Bundle/MIHPacket.test.cc index 91c6dc5..e9a040e 100644 --- a/Packets/80221Bundle/MIHPacket.test.cc +++ b/Packets/80221Bundle/MIHPacket.test.cc @@ -36,6 +36,17 @@ ///////////////////////////////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()); diff --git a/Packets/DefaultBundle/EthernetPacket.hh b/Packets/DefaultBundle/EthernetPacket.hh index 4332391..51a2389 100644 --- a/Packets/DefaultBundle/EthernetPacket.hh +++ b/Packets/DefaultBundle/EthernetPacket.hh @@ -195,8 +195,6 @@ namespace senf { } ///////////////////////////////hh.e//////////////////////////////////////// -#endif -#ifndef SENF_PACKETS_DECL_ONLY //#include "EthernetPacket.cci" //#include "EthernetPacket.ct" //#include "EthernetPacket.cti" diff --git a/Packets/DefaultBundle/ICMPv6Packet.hh b/Packets/DefaultBundle/ICMPv6Packet.hh index 032bfa3..bc7d9c2 100644 --- a/Packets/DefaultBundle/ICMPv6Packet.hh +++ b/Packets/DefaultBundle/ICMPv6Packet.hh @@ -93,8 +93,6 @@ namespace senf } ///////////////////////////////hh.e//////////////////////////////////////// -#endif -#ifndef SENF_PACKETS_DECL_ONLY //#include "IPv4Packet.cci" //#include "IPv4Packet.ct" //#include "IPv4Packet.cti" diff --git a/Packets/DefaultBundle/IPv4Packet.hh b/Packets/DefaultBundle/IPv4Packet.hh index e124ee4..0cdae50 100644 --- a/Packets/DefaultBundle/IPv4Packet.hh +++ b/Packets/DefaultBundle/IPv4Packet.hh @@ -201,8 +201,6 @@ namespace senf { ///////////////////////////////hh.e//////////////////////////////////////// -#endif -#ifndef SENF_PACKETS_DECL_ONLY //#include "IPv4Packet.cci" //#include "IPv4Packet.ct" //#include "IPv4Packet.cti" diff --git a/Packets/DefaultBundle/IPv6Extensions.hh b/Packets/DefaultBundle/IPv6Extensions.hh index aa2d331..197f5c9 100644 --- a/Packets/DefaultBundle/IPv6Extensions.hh +++ b/Packets/DefaultBundle/IPv6Extensions.hh @@ -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" diff --git a/Packets/DefaultBundle/IPv6Packet.hh b/Packets/DefaultBundle/IPv6Packet.hh index 2ce41a6..c8a06d5 100644 --- a/Packets/DefaultBundle/IPv6Packet.hh +++ b/Packets/DefaultBundle/IPv6Packet.hh @@ -159,8 +159,6 @@ namespace senf { } ///////////////////////////////hh.e//////////////////////////////////////// -#endif -#ifndef SENF_PACKETS_DECL_ONLY //#include "IPv6Packet.cci" //#include "IPv6Packet.ct" //#include "IPv6Packet.cti" diff --git a/Packets/DefaultBundle/LlcSnapPacket.hh b/Packets/DefaultBundle/LlcSnapPacket.hh index 5d0f7e9..33c0069 100644 --- a/Packets/DefaultBundle/LlcSnapPacket.hh +++ b/Packets/DefaultBundle/LlcSnapPacket.hh @@ -106,8 +106,6 @@ namespace senf { ///////////////////////////////hh.e//////////////////////////////////////// -#endif -#ifndef SENF_PACKETS_DECL_ONLY //#include "LlcSnapPacket.cci" //#include "LlcSnapPacket.ct" //#include "LlcSnapPacket.cti" diff --git a/Packets/DefaultBundle/UDPPacket.hh b/Packets/DefaultBundle/UDPPacket.hh index e85c182..456b325 100644 --- a/Packets/DefaultBundle/UDPPacket.hh +++ b/Packets/DefaultBundle/UDPPacket.hh @@ -119,8 +119,6 @@ namespace senf { ///////////////////////////////hh.e//////////////////////////////////////// -#endif -#ifndef SENF_PACKETS_DECL_ONLY //#include "UDPPacket.cci" //#include "UDPPacket.ct" //#include "UDPPacket.cti"