Skip to content
Snippets Groups Projects
MIHPacket.hh 8.26 KiB
Newer Older
// $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 public header */

#ifndef HH_SENF_Packets_80221Bundle_MIHPacket_
#define HH_SENF_Packets_80221Bundle_MIHPacket_ 1

// Custom includes
#include "../../Packets/Packets.hh"
#include "../../Socket/Protocols/Raw/MACAddress.hh"
#include "../../Socket/Protocols/INet/INet4Address.hh"
#include "../../Socket/Protocols/INet/INet6Address.hh"
tho's avatar
tho committed
#include "TLVPacket.hh"
#include <boost/function_output_iterator.hpp>
#include <boost/iterator/filter_iterator.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( packetType )                                         \
        SENF_PACKET_REGISTRY_REGISTER(                                                             \
            senf::MIHMessageRegistry, packetType::type::MESSAGE_ID, packetType )
    /** \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
tho's avatar
tho committed
    {
    #   include SENF_PARSER()
        SENF_PARSER_INHERIT  ( BaseTLVPacketParser );
        SENF_PARSER_SKIP     ( length(), 0         );
        SENF_PARSER_FINALIZE ( MIHFId_TLVParser    );
        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);

    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);
        }
tho's avatar
tho committed
    };
tho's avatar
tho committed
    /** \brief Parse a MIH packet

        Parser implementing the MIH header. The fields implemented are:
        \image html MIHPacket.png
tho's avatar
tho committed
        \see MIHPacketType
     */
tho's avatar
tho committed
    struct MIHPacketParser : public PacketParserBase
tho's avatar
tho committed
    {
    #   include SENF_PARSER()
tho's avatar
tho committed
        SENF_PARSER_BITFIELD_RO ( version,       4,  unsigned );
        SENF_PARSER_BITFIELD    ( ackRequest,    1,  bool     );
        SENF_PARSER_BITFIELD    ( ackResponse,   1,  bool     );
        SENF_PARSER_BITFIELD    ( uir,           1,  bool     );
        SENF_PARSER_BITFIELD    ( moreFragment,  1,  bool     );
        SENF_PARSER_BITFIELD    ( fragmentNr,    7,  unsigned );
        SENF_PARSER_SKIP_BITS   ( 1                           );
tho's avatar
tho committed
        // MIH message ID (MID)
        SENF_PARSER_FIELD    ( messageId, UInt16Parser ); //<pkgdraw:hide
        SENF_PARSER_GOTO     ( messageId               );
        SENF_PARSER_BITFIELD ( sid,     4,  unsigned   );
        SENF_PARSER_BITFIELD ( opcode,  2,  unsigned   );
        SENF_PARSER_BITFIELD ( aid,    10,  unsigned   );
        
tho's avatar
tho committed
        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
tho's avatar
tho committed
        // Source MIHF Id
        SENF_PARSER_FIELD ( src_mihfId, MIHFId_TLVParser );
tho's avatar
tho committed
        // Destination MIHF Id
        SENF_PARSER_FIELD ( dst_mihfId, MIHFId_TLVParser );
tho's avatar
tho committed
        SENF_PARSER_FINALIZE ( MIHPacketParser );
tho's avatar
tho committed
        SENF_PARSER_INIT() {
            version_() = 1;
            src_mihfId().type() = 1;
            dst_mihfId().type() = 2;
tho's avatar
tho committed
        }
tho's avatar
tho committed
        friend class MIHPacketType;
tho's avatar
tho committed
    };
tho's avatar
tho committed
    /** \brief MIH packet

        \par Packet type (typedef):
            \ref MIHPacket

        \par Fields:
            \ref MIHPacketParser
tho's avatar
tho committed
        \ingroup protocolbundle_80221
     */
tho's avatar
tho committed
    struct MIHPacketType
        : public PacketTypeBase,
          public PacketTypeMixin<MIHPacketType, MIHMessageRegistry>
tho's avatar
tho committed
    {
tho's avatar
tho committed
#ifndef DOXYGEN
        typedef PacketTypeMixin<MIHPacketType, MIHMessageRegistry> mixin;
tho's avatar
tho committed
#endif
        typedef ConcretePacket<MIHPacketType> packet; ///< MIH packet typedef
        typedef MIHPacketParser parser;               ///< typedef to the parser of MIH packet
tho's avatar
tho committed

        using mixin::nextPacketRange;
        using mixin::init;
        using mixin::initSize;

tho's avatar
tho committed
        /** \brief Dump given MIH packet in readable form to given output stream */
tho's avatar
tho committed
        static void dump(packet p, std::ostream &os);
tho's avatar
tho committed
        static void finalize(packet p);
tho's avatar
tho committed
        static factory_t nextPacketType(packet p);
        
        enum ResponseStatus { Success, UnspecifiedFailure, Rejected, AuthorizationFailure, NetworkError };
tho's avatar
tho committed
    };

tho's avatar
tho committed
    /** \brief MIH packet typedef */
tho's avatar
tho committed
    typedef ConcretePacket<MIHPacketType> MIHPacket;
tho's avatar
tho committed
    struct MIHPayloadPacketParser : public PacketParserBase
    {
    #   include SENF_PARSER()
        SENF_PARSER_LIST ( tlv_list, packetSize(), GenericTLVPacketParser );
tho's avatar
tho committed
        SENF_PARSER_FINALIZE ( MIHPayloadPacketParser );
    };
tho's avatar
tho committed
    struct MIHPayloadPacketType
        : public PacketTypeBase,
          public PacketTypeMixin<MIHPayloadPacketType>
    {
tho's avatar
tho committed
#ifndef DOXYGEN
tho's avatar
tho committed
        typedef PacketTypeMixin<MIHPayloadPacketType> mixin;
tho's avatar
tho committed
#endif
        typedef ConcretePacket<MIHPayloadPacketType> packet; ///< MIH Payload packet typedef
tho's avatar
tho committed
        typedef MIHPayloadPacketParser parser; ///< typedef to the parser of MIH Payload packet
tho's avatar
tho committed

        using mixin::nextPacketRange;
        using mixin::init;
        using mixin::initSize;

tho's avatar
tho committed
        /** \brief Dump given MIHPayload in readable form to given output stream */
tho's avatar
tho committed
        static void dump(packet p, std::ostream &os);
    };
tho's avatar
tho committed
     /** \brief MIH Payload packet typedef */
tho's avatar
tho committed
    typedef ConcretePacket<MIHPayloadPacketType> MIHPayloadPacket;
}


///////////////////////////////hh.e////////////////////////////////////////
#endif
#ifndef SENF_PACKETS_DECL_ONLY
//#include "MIHPacket.cci"
//#include "MIHPacket.ct"
//#include "MIHPacket.cti"
#endif

// 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: