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