Packets/GenericTLV: added some documentation
[senf.git] / senf / Packets / 80221Bundle / TLVParser.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
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 TLVParser public header */
25
26 #ifndef HH_SENF_Packets_80221Bundle_TLVParser_
27 #define HH_SENF_Packets_80221Bundle_TLVParser_ 1
28
29 // Custom includes
30 #include <algorithm>
31 #include <senf/Packets/Packets.hh>
32 #include "MIHTypes.hh"
33
34 //#include "TLVParser.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     struct MIHTLVLengthException : public senf::Exception
40     { 
41         MIHTLVLengthException() 
42           : senf::Exception("MIHTLVLengthException") {} 
43     };
44
45     
46     class MIHTLVLengthParser 
47         : public detail::packet::IntParserOps<MIHTLVLengthParser, boost::uint32_t>,
48           public PacketParserBase
49     {
50     public:
51         MIHTLVLengthParser(data_iterator i, state_type s) : PacketParserBase(i,s) {}
52
53         typedef boost::uint32_t value_type;
54         static const size_type init_bytes = 1;
55         static value_type const min_value = 0;
56         static value_type const max_value = 4294967295u;
57
58         value_type value() const;
59         void value(value_type const & v);
60         
61         MIHTLVLengthParser const & operator= (value_type other);
62         size_type bytes() const;
63         void init() const;
64
65 #       include SENF_PARSER()
66         SENF_PARSER_PRIVATE_FIELD ( length_field, UInt8Parser );
67         SENF_PARSER_GOTO( length_field );
68         SENF_PARSER_PRIVATE_BITFIELD ( extended_length_flag, 1,  bool     );
69         SENF_PARSER_PRIVATE_BITFIELD ( underflow_flag,       1,  bool     );
70         SENF_PARSER_PRIVATE_BITFIELD ( fixed_length_field,   6,  unsigned );
71
72         void finalize();
73         void maxValue(value_type v);
74         value_type maxValue() const;
75         
76     private:
77         void resize_(size_type size);
78     };  
79         
80
81     /** \brief Base class for MIH TLV parsers
82      
83          MIHBaseTLVParser is the abstract base class for MIH TLV parsers. It defines the
84          \ref type() field as an \ref senf::UInt8Parser and the \ref length() field as a 
85          MIHTLVLengthParser. The length field is read-only. 
86          
87          To create your own \c TLVParser you have to inherit from MIHBaseTLVParser (don't 
88          forget \ref SENF_PARSER_INHERIT) and define the \c value field. In the following example 
89          the value is a vector of MacAddresses: 
90          \code
91          struct MacAddressesTLVParser : public MIHBaseTLVParser {
92          #   include SENF_PARSER()        
93              SENF_PARSER_INHERIT ( MIHBaseTLVParser );
94              SENF_PARSER_VECTOR  ( value, bytes(length), senf::MACAddressParser );
95              SENF_PARSER_FINALIZE( MacAddressesTLVParser );
96          };
97          \endcode
98          
99          You have to adjust the maximum length value with the \ref maxLengthValue function 
100          before the length value is set. The default maximum value is 127. So, in the above
101          example adding more than 21 MACAddresses to the vector will throw a TLVLengthException
102          if you don't call \c macAddressesTLVPacket->maxLengthValue( \e some_value) before.
103          
104          \see MIHTLVLengthParser \n
105            MIHGenericTLVParser \n
106      */
107     class MIHBaseTLVParser : public PacketParserBase
108     {
109     public:
110 #       include SENF_PARSER()
111         SENF_PARSER_FIELD    ( type,   UInt8Parser        );
112         SENF_PARSER_FIELD_RO ( length, MIHTLVLengthParser );
113         SENF_PARSER_FINALIZE ( MIHBaseTLVParser           );
114         
115         /** \brief set maximum value of length field
116     
117             The size of the length field will be increased if necessary.
118             \param v maximum value of length field
119          */
120         void maxLengthValue(MIHTLVLengthParser::value_type v) const {
121             protect(), length_().maxValue(v);
122         }
123         
124         /** \brief shrink size of length field to minimum
125     
126             The size of the length field will be decreased to minimum necessary to hold
127             the current length value.
128          */
129         void finalizeLength() { 
130             protect(), length_().finalize();
131         };
132     
133         typedef GenericTLVParserRegistry<MIHBaseTLVParser> Registry;
134         
135     protected:
136         /// resize the packet after the length field to given size
137         senf::safe_data_iterator resizeValueField(MIHTLVLengthParser::value_type size);
138     };
139
140         
141     /** \brief Parser for a generic TLV packet
142      */
143     struct MIHGenericTLVParser
144         : public GenericTLVParserBase<MIHBaseTLVParser>
145     {
146         typedef senf::GenericTLVParserBase<MIHBaseTLVParser> base;
147         MIHGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
148
149         void init() const {
150             defaultInit();
151             maxLengthValue( MIHTLVLengthParser::max_value);
152         }
153         
154         using base::init;
155     };
156         
157     /** \brief Parse a MIHF_ID
158
159          the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
160          we could set maxLengthValue in init(), but for the most MIHF_IDs the default
161          maximum length of 127 should be enough.
162          
163          \note you must call mihfIdPacket.maxLengthValue( 253) *before*
164          setting longer MIHF_IDs values.
165     */
166     class MIHFIdTLVParser : public MIHBaseTLVParser
167     {
168     #   include SENF_PARSER()
169         SENF_PARSER_INHERIT  ( MIHBaseTLVParser );
170         SENF_PARSER_SKIP     ( length(), 0      );
171         SENF_PARSER_FINALIZE ( MIHFIdTLVParser  );
172         
173     public:
174         std::string asString() const;
175         void setString(std::string const &id);
176
177         senf::MACAddress asMACAddress() const;
178         void setMACAddress(senf::MACAddress const &mac);
179
180         senf::INet4Address asINet4Address() const;
181         void setINet4Address(senf::INet4Address const &addr);
182
183         senf::INet6Address asINet6Address() const;
184         void setINet6Address(senf::INet6Address const &addr);
185         
186         senf::EUI64 asEUI64() const;
187         void setEUI64(senf::EUI64 const &addr);
188
189         MIHFId valueAs(MIHFId::Type type) const;
190         
191         void dump(std::ostream & os) const;
192
193     private:
194         template <class OutputIterator>
195         struct binaryNAIEncoder {
196             binaryNAIEncoder(OutputIterator &i) : i_(i) {}
197             void operator()(const boost::uint8_t &v) const {
198                 *i_++ = '\\';
199                 *i_++ = v;
200             }
201             OutputIterator &i_;
202         };
203         template <class OutputIterator>
204         static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > getNAIEncodedOutputIterator(OutputIterator i) {
205             return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
206         }
207
208         struct binaryNAIDecoder {
209             binaryNAIDecoder() : readNextByte_(true) {}
210             bool operator()(const boost::uint8_t &v) {
211                 readNextByte_ = readNextByte_ ? false : true;
212                 return readNextByte_;
213             }
214             bool readNextByte_;
215         };
216         template <class Iterator>
217         static boost::filter_iterator<binaryNAIDecoder, Iterator> getNAIDecodedIterator(Iterator begin, Iterator end) {
218             return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
219         }
220     };
221
222     struct MIHFSrcIdTLVParser : public MIHFIdTLVParser
223     {
224         MIHFSrcIdTLVParser(data_iterator i, state_type s) : MIHFIdTLVParser(i,s) {}
225         
226         void init() const {
227             defaultInit();
228             type() << typeId+0;
229         }
230         static type_t::value_type const typeId = 1;
231         
232         void dump(std::ostream & os) const;
233     };
234     
235     struct MIHFDstIdTLVParser : public MIHFIdTLVParser
236     {
237         MIHFDstIdTLVParser(data_iterator i, state_type s) : MIHFIdTLVParser(i,s) {}
238         
239         void init() const {
240             defaultInit();
241             type() << typeId+0;
242         }
243         static type_t::value_type const typeId = 2;
244         
245         void dump(std::ostream & os) const;
246     };
247     
248     /** \brief Parser for 802.21 Status TLV
249      */
250     struct MIHStatusTLVParser : public MIHBaseTLVParser
251     {
252     #   include SENF_PARSER()
253         SENF_PARSER_INHERIT ( MIHBaseTLVParser   );
254         SENF_PARSER_FIELD   ( value, UInt8Parser );
255         SENF_PARSER_FINALIZE( MIHStatusTLVParser );
256         SENF_PARSER_INIT() {
257             defaultInit();
258             type() << typeId+0;
259             length_() = 1;
260         }
261         static type_t::value_type const typeId = 3;
262         void dump(std::ostream & os) const; ///< dump string representation to given stream
263         
264         enum StatusCode { 
265             Success, UnspecifiedFailure, Rejected, AuthorizationFailure, NetworkError };
266     };
267     
268     struct MIHRegisterReqCodeTLVParser : public MIHBaseTLVParser
269     {
270     #   include SENF_PARSER()
271         SENF_PARSER_INHERIT  ( MIHBaseTLVParser );
272         SENF_PARSER_FIELD    ( value, UInt8Parser );
273         SENF_PARSER_FINALIZE ( MIHRegisterReqCodeTLVParser );
274             
275         SENF_PARSER_INIT() {
276             defaultInit();
277             type() = typeId+0;
278             length_() = 1;
279         }
280         static type_t::value_type const typeId = 11;  
281         void dump(std::ostream & os) const; ///< dump string representation to given stream
282         
283         enum RequestCode { Registration, ReRegistration };
284     };
285     
286     struct MIHValidTimeIntervalTLVParser : public MIHBaseTLVParser
287     {
288     #   include SENF_PARSER()
289         SENF_PARSER_INHERIT  ( MIHBaseTLVParser );
290         SENF_PARSER_FIELD    ( value, UInt32Parser );
291         SENF_PARSER_FINALIZE ( MIHValidTimeIntervalTLVParser );
292             
293         SENF_PARSER_INIT() {
294             defaultInit();
295             type() = typeId+0;
296             length_() = 4;
297         }
298         static type_t::value_type const typeId = 12;
299         void dump(std::ostream & os) const; ///< dump string representation to given stream
300     };
301
302 }
303
304
305 ///////////////////////////////hh.e////////////////////////////////////////
306 #include "TLVParser.cci"
307 //#include "TLVParser.ct"
308 //#include "TLVParser.cti"
309 #endif
310
311 \f
312 // Local Variables:
313 // mode: c++
314 // fill-column: 100
315 // c-file-style: "senf"
316 // indent-tabs-mode: nil
317 // ispell-local-dictionary: "american"
318 // compile-command: "scons -u test"
319 // comment-column: 40
320 // End: