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