4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief MIH protocol public header */
26 #ifndef HH_SENF_Packets_80221Bundle_MIHPacket_
27 #define HH_SENF_Packets_80221Bundle_MIHPacket_ 1
30 #include <senf/Packets/Packets.hh>
31 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
32 #include <senf/Socket/Protocols/INet/INet4Address.hh>
33 #include <senf/Socket/Protocols/INet/INet6Address.hh>
34 #include "TLVPacket.hh"
35 #include <boost/function_output_iterator.hpp>
36 #include <boost/iterator/filter_iterator.hpp>
37 #include <boost/variant.hpp>
40 //#include "MIHPacket.mpp"
41 ///////////////////////////////hh.p////////////////////////////////////////
45 struct MIHMessageRegistry {
46 // MIH messages registry
47 typedef boost::uint16_t key_t;
50 # define SENF_MIH_PACKET_REGISTRY_REGISTER( packet ) \
51 SENF_PACKET_REGISTRY_REGISTER( \
52 senf::MIHMessageRegistry, packet::type::MESSAGE_ID, packet )
55 : public boost::variant< boost::blank, senf::MACAddress, senf::INet4Address,
56 senf::INet6Address, std::string, senf::EUI64 >,
57 public boost::less_than_comparable<MIHFId>,
58 public boost::equality_comparable<MIHFId>
61 enum Type { Empty, MACAddress, INet4Address, INet6Address, String, EUI64 };
63 MIHFId(); ///< Create empty instance.
64 MIHFId(senf::MACAddress const & addr); ///< Construct id with given MACAddress
65 MIHFId(senf::INet4Address const & addr); ///< Construct id with given INet4Address
66 MIHFId(senf::INet6Address const & addr); ///< Construct id with given INet6Address
67 MIHFId(std::string const & addr); ///< Construct id with given String
68 MIHFId(senf::EUI64 const & addr); ///< Construct id with given EUI64
71 bool operator==(MIHFId const & other) const;
72 bool operator<(MIHFId const & other) const;
75 struct GetTypeVisitor : public boost::static_visitor<Type> {
76 Type operator()(boost::blank const &) const { return Empty; }
77 Type operator()(senf::MACAddress const &) const { return MACAddress; }
78 Type operator()(senf::INet4Address const &) const { return INet4Address; }
79 Type operator()(senf::INet6Address const &) const { return INet6Address; }
80 Type operator()(std::string const & ) const { return String; }
81 Type operator()(senf::EUI64 const &) const { return EUI64; }
83 struct EqualsVisitor : public boost::static_visitor<bool> {
84 template <typename T, typename U>
85 bool operator()(T const &, U const &) const {
89 bool operator()( const T & lhs, const T & rhs ) const {
93 struct LessThanVisitor : public boost::static_visitor<bool> {
94 template <typename T, typename U>
95 bool operator()(T const &, U const &) const {
99 bool operator()( const T & lhs, const T & rhs ) const {
106 /** \brief Parse a MIHF_ID
108 the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
109 we could set maxLengthValue in init(), but for the most MIHF_IDs the default
110 maximum length of 127 should be enough.
112 \note you must call mihfIdPacket.maxLengthValue( 253) *before*
113 setting longer MIHF_IDs values.
115 class MIHFId_TLVParser : public BaseTLVPacketParser
117 # include SENF_PARSER()
118 SENF_PARSER_INHERIT ( BaseTLVPacketParser );
119 SENF_PARSER_SKIP ( length(), 0 );
120 SENF_PARSER_FINALIZE ( MIHFId_TLVParser );
123 std::string asString() const;
124 void setString(std::string const &id);
126 senf::MACAddress asMACAddress() const;
127 void setMACAddress(senf::MACAddress const &mac);
129 senf::INet4Address asINet4Address() const;
130 void setINet4Address(senf::INet4Address const &addr);
132 senf::INet6Address asINet6Address() const;
133 void setINet6Address(senf::INet6Address const &addr);
135 senf::EUI64 asEUI64() const;
136 void setEUI64(senf::EUI64 const &addr);
138 MIHFId valueAs(MIHFId::Type type) const;
141 template <class OutputIterator>
142 struct binaryNAIEncoder {
143 binaryNAIEncoder(OutputIterator &i) : i_(i) {}
144 void operator()(const boost::uint8_t &v) const {
150 template <class OutputIterator>
151 static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > getNAIEncodedOutputIterator(OutputIterator i) {
152 return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
155 struct binaryNAIDecoder {
156 binaryNAIDecoder() : readNextByte_(true) {}
157 bool operator()(const boost::uint8_t &v) {
158 readNextByte_ = readNextByte_ ? false : true;
159 return readNextByte_;
163 template <class Iterator>
164 static boost::filter_iterator<binaryNAIDecoder, Iterator> getNAIDecodedIterator(Iterator begin, Iterator end) {
165 return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
169 /** \brief Parse a MIH packet
171 Parser implementing the MIH header. The fields implemented are:
172 \image html MIHPacket.png
176 struct MIHPacketParser : public PacketParserBase
178 # include SENF_PARSER()
180 SENF_PARSER_BITFIELD_RO ( version, 4, unsigned );
181 SENF_PARSER_BITFIELD ( ackRequest, 1, bool );
182 SENF_PARSER_BITFIELD ( ackResponse, 1, bool );
183 SENF_PARSER_BITFIELD ( uir, 1, bool );
184 SENF_PARSER_BITFIELD ( moreFragment, 1, bool );
185 SENF_PARSER_BITFIELD ( fragmentNr, 7, unsigned );
186 SENF_PARSER_SKIP_BITS ( 1 );
188 // MIH message ID (MID)
189 SENF_PARSER_FIELD ( messageId, UInt16Parser ); //<pkgdraw:hide
190 SENF_PARSER_GOTO ( messageId );
191 SENF_PARSER_BITFIELD ( sid, 4, unsigned );
192 SENF_PARSER_BITFIELD ( opcode, 2, unsigned );
193 SENF_PARSER_BITFIELD ( aid, 10, unsigned );
195 SENF_PARSER_SKIP_BITS ( 4 );
196 SENF_PARSER_BITFIELD ( transactionId, 12, unsigned );
197 SENF_PARSER_FIELD_RO ( payloadLength, UInt16Parser );
199 SENF_PARSER_GOTO_OFFSET( 8, 8); // just to limit the offset calculation
202 SENF_PARSER_FIELD ( src_mihfId, MIHFId_TLVParser );
203 // Destination MIHF Id
204 SENF_PARSER_FIELD ( dst_mihfId, MIHFId_TLVParser );
206 SENF_PARSER_FINALIZE ( MIHPacketParser );
210 src_mihfId().type() = 1;
211 dst_mihfId().type() = 2;
214 friend class MIHPacketType;
217 /** \brief MIH packet
219 \par Packet type (typedef):
225 \ingroup protocolbundle_80221
228 : public PacketTypeBase,
229 public PacketTypeMixin<MIHPacketType, MIHMessageRegistry>
232 typedef PacketTypeMixin<MIHPacketType, MIHMessageRegistry> mixin;
234 typedef ConcretePacket<MIHPacketType> packet; ///< MIH packet typedef
235 typedef MIHPacketParser parser; ///< typedef to the parser of MIH packet
237 using mixin::nextPacketRange;
239 using mixin::initSize;
241 /** \brief Dump given MIH packet in readable form to given output stream */
242 static void dump(packet p, std::ostream &os);
243 static void finalize(packet p);
244 static factory_t nextPacketType(packet p);
247 /** \brief MIH packet typedef */
248 typedef ConcretePacket<MIHPacketType> MIHPacket;
251 struct MIHPayloadPacketParser : public PacketParserBase
253 # include SENF_PARSER()
254 SENF_PARSER_LIST ( tlv_list, packetSize(), GenericTLVPacketParser );
255 SENF_PARSER_FINALIZE ( MIHPayloadPacketParser );
258 struct MIHPayloadPacketType
259 : public PacketTypeBase,
260 public PacketTypeMixin<MIHPayloadPacketType>
263 typedef PacketTypeMixin<MIHPayloadPacketType> mixin;
265 typedef ConcretePacket<MIHPayloadPacketType> packet; ///< MIH Payload packet typedef
266 typedef MIHPayloadPacketParser parser; ///< typedef to the parser of MIH Payload packet
268 using mixin::nextPacketRange;
270 using mixin::initSize;
272 /** \brief Dump given MIHPayload in readable form to given output stream */
273 static void dump(packet p, std::ostream &os);
276 /** \brief MIH Payload packet typedef */
277 typedef ConcretePacket<MIHPayloadPacketType> MIHPayloadPacket;
281 ///////////////////////////////hh.e////////////////////////////////////////
282 #include "MIHPacket.cci"
283 //#include "MIHPacket.ct"
284 //#include "MIHPacket.cti"
292 // c-file-style: "senf"
293 // indent-tabs-mode: nil
294 // ispell-local-dictionary: "american"
295 // compile-command: "scons -u test"
296 // comment-column: 40