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 "../../Packets/Packets.hh"
31 #include "../../Socket/Protocols/Raw/MACAddress.hh"
32 #include "../../Socket/Protocols/INet/INet4Address.hh"
33 #include "../../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 };
64 MIHFId(senf::MACAddress const & addr);
65 MIHFId(senf::INet4Address const & addr);
66 MIHFId(senf::INet6Address const & addr);
67 MIHFId(std::string const & addr);
68 MIHFId(senf::EUI64 const & addr);
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 );
122 std::string asString() const;
123 void setString(std::string const &id);
125 senf::MACAddress asMACAddress() const;
126 void setMACAddress(senf::MACAddress const &mac);
128 senf::INet4Address asINet4Address() const;
129 void setINet4Address(senf::INet4Address const &addr);
131 senf::INet6Address asINet6Address() const;
132 void setINet6Address(senf::INet6Address const &addr);
134 senf::EUI64 asEUI64() const;
135 void setEUI64(senf::EUI64 const &addr);
137 MIHFId valueAs(MIHFId::Type type) const;
140 template <class OutputIterator>
141 struct binaryNAIEncoder {
142 binaryNAIEncoder(OutputIterator &i) : i_(i) {}
143 void operator()(const boost::uint8_t &v) const {
149 template <class OutputIterator>
150 static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > getNAIEncodedOutputIterator(OutputIterator i) {
151 return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
154 struct binaryNAIDecoder {
155 binaryNAIDecoder() : readNextByte_(true) {}
156 bool operator()(const boost::uint8_t &v) {
157 readNextByte_ = readNextByte_ ? false : true;
158 return readNextByte_;
162 template <class Iterator>
163 static boost::filter_iterator<binaryNAIDecoder, Iterator> getNAIDecodedIterator(Iterator begin, Iterator end) {
164 return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
168 /** \brief Parse a MIH packet
170 Parser implementing the MIH header. The fields implemented are:
171 \image html MIHPacket.png
175 struct MIHPacketParser : public PacketParserBase
177 # include SENF_PARSER()
179 SENF_PARSER_BITFIELD_RO ( version, 4, unsigned );
180 SENF_PARSER_BITFIELD ( ackRequest, 1, bool );
181 SENF_PARSER_BITFIELD ( ackResponse, 1, bool );
182 SENF_PARSER_BITFIELD ( uir, 1, bool );
183 SENF_PARSER_BITFIELD ( moreFragment, 1, bool );
184 SENF_PARSER_BITFIELD ( fragmentNr, 7, unsigned );
185 SENF_PARSER_SKIP_BITS ( 1 );
187 // MIH message ID (MID)
188 SENF_PARSER_FIELD ( messageId, UInt16Parser ); //<pkgdraw:hide
189 SENF_PARSER_GOTO ( messageId );
190 SENF_PARSER_BITFIELD ( sid, 4, unsigned );
191 SENF_PARSER_BITFIELD ( opcode, 2, unsigned );
192 SENF_PARSER_BITFIELD ( aid, 10, unsigned );
194 SENF_PARSER_SKIP_BITS ( 4 );
195 SENF_PARSER_BITFIELD ( transactionId, 12, unsigned );
196 SENF_PARSER_FIELD_RO ( payloadLength, UInt16Parser );
198 SENF_PARSER_GOTO_OFFSET( 8, 8); // just to limit the offset calculation
201 SENF_PARSER_FIELD ( src_mihfId, MIHFId_TLVParser );
202 // Destination MIHF Id
203 SENF_PARSER_FIELD ( dst_mihfId, MIHFId_TLVParser );
205 SENF_PARSER_FINALIZE ( MIHPacketParser );
209 src_mihfId().type() = 1;
210 dst_mihfId().type() = 2;
213 friend class MIHPacketType;
216 /** \brief MIH packet
218 \par Packet type (typedef):
224 \ingroup protocolbundle_80221
227 : public PacketTypeBase,
228 public PacketTypeMixin<MIHPacketType, MIHMessageRegistry>
231 typedef PacketTypeMixin<MIHPacketType, MIHMessageRegistry> mixin;
233 typedef ConcretePacket<MIHPacketType> packet; ///< MIH packet typedef
234 typedef MIHPacketParser parser; ///< typedef to the parser of MIH packet
236 using mixin::nextPacketRange;
238 using mixin::initSize;
240 /** \brief Dump given MIH packet in readable form to given output stream */
241 static void dump(packet p, std::ostream &os);
242 static void finalize(packet p);
243 static factory_t nextPacketType(packet p);
246 /** \brief MIH packet typedef */
247 typedef ConcretePacket<MIHPacketType> MIHPacket;
250 struct MIHPayloadPacketParser : public PacketParserBase
252 # include SENF_PARSER()
253 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