Packets/DefaultBundle: moved MPLSPacket to NetEmu/Carmen project
[senf.git] / Packets / 80221Bundle / MIHPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief MIH protocol public header */
25
26 #ifndef HH_SENF_Packets_80221Bundle_MIHPacket_
27 #define HH_SENF_Packets_80221Bundle_MIHPacket_ 1
28
29 // Custom includes
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
38
39 //#include "MIHPacket.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
41
42 namespace senf {
43     
44     struct MIHMessageRegistry {
45         // MIH messages registry
46         typedef boost::uint16_t key_t;
47     };
48     
49 #   define SENF_MIH_PACKET_REGISTRY_REGISTER( sid, opcode, aid, type )                             \
50         SENF_PACKET_REGISTRY_REGISTER(                                                             \
51             senf::MIHMessageRegistry,                                                              \
52             boost::uint16_t((boost::uint16_t(sid) << 12) | (boost::uint16_t(opcode) << 10) | aid), \
53             type )
54     
55     /** \brief Parse a MIHF_ID
56
57          the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
58          we could set maxLengthValue in init(), but for the most MIHF_IDs the default
59          maximum length of 127 should be enough.
60          
61          \note you must call mihfIdPacket.maxLengthValue( 253) *before*
62          setting longer MIHF_IDs values.
63     */
64     class MIHFId_TLVParser : public BaseTLVPacketParser
65     {
66     #   include SENF_PARSER()
67         SENF_PARSER_INHERIT  ( BaseTLVPacketParser );
68         SENF_PARSER_SKIP     ( length(), 0         );
69         SENF_PARSER_FINALIZE ( MIHFId_TLVParser    );
70
71         std::string asString() const;
72         void setString(std::string const &id);
73
74         senf::MACAddress asMACAddress() const;
75         void setMACAddress(senf::MACAddress const &mac);
76
77         senf::INet4Address asINet4Address() const;
78         void setINet4Address(senf::INet4Address const &addr);
79
80         senf::INet6Address asINet6Address() const;
81         void setINet6Address(senf::INet6Address const &addr);
82
83     private:
84         template <class OutputIterator>
85         struct binaryNAIEncoder {
86             binaryNAIEncoder(OutputIterator &i) : i_(i) {}
87             void operator()(const boost::uint8_t &v) const {
88                 *i_++ = '\\';
89                 *i_++ = v;
90             }
91             OutputIterator &i_;
92         };
93         template <class OutputIterator>
94         static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > getNAIEncodedOutputIterator(OutputIterator i) {
95             return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
96         }
97
98         struct binaryNAIDecoder {
99             binaryNAIDecoder() : readNextByte_(true) {}
100             bool operator()(const boost::uint8_t &v) {
101                 readNextByte_ = readNextByte_ ? false : true;
102                 return readNextByte_;
103             }
104             bool readNextByte_;
105         };
106         template <class Iterator>
107         static boost::filter_iterator<binaryNAIDecoder, Iterator> getNAIDecodedIterator(Iterator begin, Iterator end) {
108             return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
109         }
110     };
111  
112     /** \brief Parse a MIH packet
113
114         Parser implementing the MIH header. The fields implemented are:
115         \image html MIHPacket.png
116
117         \see MIHPacketType
118      */
119     struct MIHPacketParser : public PacketParserBase
120     {
121     #   include SENF_PARSER()
122
123         SENF_PARSER_BITFIELD_RO ( version,       4,  unsigned );
124         SENF_PARSER_BITFIELD    ( ackRequest,    1,  bool     );
125         SENF_PARSER_BITFIELD    ( ackResponse,   1,  bool     );
126         SENF_PARSER_BITFIELD    ( uir,           1,  bool     );
127         SENF_PARSER_BITFIELD    ( moreFragment,  1,  bool     );
128         SENF_PARSER_BITFIELD    ( fragmentNr,    7,  unsigned );
129         SENF_PARSER_SKIP_BITS   ( 1                           );
130
131         // MIH message ID (MID)
132         SENF_PARSER_FIELD    ( messageId, UInt16Parser ); //<pkgdraw:hide
133         SENF_PARSER_GOTO     ( messageId               );
134         SENF_PARSER_BITFIELD ( sid,     4,  unsigned   );
135         SENF_PARSER_BITFIELD ( opcode,  2,  unsigned   );
136         SENF_PARSER_BITFIELD ( aid,    10,  unsigned   );
137         
138         SENF_PARSER_SKIP_BITS ( 4                           );
139         SENF_PARSER_BITFIELD  ( transactionId, 12, unsigned );
140         SENF_PARSER_FIELD_RO  ( payloadLength, UInt16Parser );
141
142         
143         // Source MIHF Id
144         SENF_PARSER_FIELD ( src_mihfId, MIHFId_TLVParser );
145         // Destination MIHF Id
146         SENF_PARSER_FIELD ( dst_mihfId, MIHFId_TLVParser );
147
148         SENF_PARSER_FINALIZE ( MIHPacketParser );
149
150         SENF_PARSER_INIT() {
151             version_() = 1;
152             src_mihfId().type() = 1;
153             dst_mihfId().type() = 2;
154         }
155
156         friend class MIHPacketType;
157     };
158
159     /** \brief MIH packet
160
161         \par Packet type (typedef):
162             \ref MIHPacket
163
164         \par Fields:
165             \ref MIHPacketParser
166
167         \ingroup protocolbundle_80221
168      */
169     struct MIHPacketType
170         : public PacketTypeBase,
171           public PacketTypeMixin<MIHPacketType, MIHMessageRegistry>
172     {
173 #ifndef DOXYGEN
174         typedef PacketTypeMixin<MIHPacketType, MIHMessageRegistry> mixin;
175 #endif
176         typedef ConcretePacket<MIHPacketType> packet; ///< MIH packet typedef
177         typedef MIHPacketParser parser;               ///< typedef to the parser of MIH packet
178
179         using mixin::nextPacketRange;
180         using mixin::init;
181         using mixin::initSize;
182
183         /** \brief Dump given MIH packet in readable form to given output stream */
184         static void dump(packet p, std::ostream &os);
185         static void finalize(packet p);
186         static factory_t nextPacketType(packet p);
187         
188         enum ResponseStatus { Success, UnspecifiedFailure, Rejected, AuthorizationFailure, NetworkError };
189     };
190
191     /** \brief MIH packet typedef */
192     typedef ConcretePacket<MIHPacketType> MIHPacket;
193
194
195     struct MIHPayloadPacketParser : public PacketParserBase
196     {
197     #   include SENF_PARSER()
198         SENF_PARSER_LIST ( tlv_list, packetSize(), GenericTLVPacketParser );
199
200         SENF_PARSER_FINALIZE ( MIHPayloadPacketParser );
201     };
202
203     struct MIHPayloadPacketType
204         : public PacketTypeBase,
205           public PacketTypeMixin<MIHPayloadPacketType>
206     {
207 #ifndef DOXYGEN
208         typedef PacketTypeMixin<MIHPayloadPacketType> mixin;
209 #endif
210         typedef ConcretePacket<MIHPayloadPacketType> packet; ///< MIH Payload packet typedef
211         typedef MIHPayloadPacketParser parser; ///< typedef to the parser of MIH Payload packet
212
213         using mixin::nextPacketRange;
214         using mixin::init;
215         using mixin::initSize;
216
217         /** \brief Dump given MIHPayload in readable form to given output stream */
218         static void dump(packet p, std::ostream &os);
219     };
220
221      /** \brief MIH Payload packet typedef */
222     typedef ConcretePacket<MIHPayloadPacketType> MIHPayloadPacket;
223 }
224
225
226 ///////////////////////////////hh.e////////////////////////////////////////
227 #endif
228 #ifndef SENF_PACKETS_DECL_ONLY
229 //#include "MIHPacket.cci"
230 //#include "MIHPacket.ct"
231 //#include "MIHPacket.cti"
232 #endif
233
234
235 \f
236 // Local Variables:
237 // mode: c++
238 // fill-column: 100
239 // c-file-style: "senf"
240 // indent-tabs-mode: nil
241 // ispell-local-dictionary: "american"
242 // compile-command: "scons -u test"
243 // comment-column: 40
244 // End: