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 TLVParser public header */
26 #ifndef HH_SENF_Packets_80221Bundle_TLVParser_
27 #define HH_SENF_Packets_80221Bundle_TLVParser_ 1
30 #include <senf/Packets/Packets.hh>
31 #include "MIHTypes.hh"
33 //#include "TLVParser.mpp"
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 class MIHTLVLengthParser
39 : public detail::packet::IntParserOps<MIHTLVLengthParser, boost::uint32_t>,
40 public PacketParserBase
43 MIHTLVLengthParser(data_iterator i, state_type s) : PacketParserBase(i,s) {}
45 typedef boost::uint32_t value_type;
46 static const size_type init_bytes = 1;
47 static value_type const min_value = 0;
48 static value_type const max_value = 4294967295u;
50 value_type value() const;
51 void value(value_type const & v);
53 MIHTLVLengthParser const & operator= (value_type other);
54 size_type bytes() const;
57 # include SENF_PARSER()
58 SENF_PARSER_PRIVATE_FIELD ( length_field, UInt8Parser );
59 SENF_PARSER_GOTO( length_field );
60 SENF_PARSER_PRIVATE_BITFIELD ( extended_length_flag, 1, bool );
61 SENF_PARSER_PRIVATE_BITFIELD ( underflow_flag, 1, bool );
62 SENF_PARSER_PRIVATE_BITFIELD ( fixed_length_field, 6, unsigned );
65 void capacity(value_type v);
66 value_type capacity() const;
69 void resize_(size_type size);
73 /** \brief Base class for MIH TLV parsers
75 MIHBaseTLVParser is the abstract base class for MIH TLV parsers. It defines the
76 \ref type() field as an \ref senf::UInt8Parser and the \ref length() field as a
77 MIHTLVLengthParser. The length field is read-only.
79 To create your own \c TLVParser you have to inherit from MIHBaseTLVParser (don't
80 forget \ref SENF_PARSER_INHERIT) and define the \c value field. In the following example
81 the value is a vector of MacAddresses:
83 struct MacAddressesTLVParser : public MIHBaseTLVParser {
84 # include SENF_PARSER()
85 SENF_PARSER_INHERIT ( MIHBaseTLVParser );
86 SENF_PARSER_VECTOR ( value, bytes(length), senf::MACAddressParser );
87 SENF_PARSER_FINALIZE( MacAddressesTLVParser );
91 You have to adjust the maximum length value with the \ref maxLength function
92 before the length value is set. The default maximum value is 128. So, in the above
93 example adding more than 21 MACAddresses to the vector will throw a TLVLengthException
94 if you don't call \c maxLength( \e some_value) before.
96 \see MIHTLVLengthParser \n
97 MIHGenericTLVParser \n
99 class MIHBaseTLVParser : public PacketParserBase
102 # include SENF_PARSER()
103 SENF_PARSER_FIELD ( type, UInt8Parser );
104 SENF_PARSER_FIELD_RO ( length, MIHTLVLengthParser );
105 SENF_PARSER_FINALIZE ( MIHBaseTLVParser );
107 /** \brief shrink size of the TLV length field to minimum
109 The size of the length field will be decreased to minimum necessary to hold
110 the current length value.
114 typedef GenericTLVParserRegistry<MIHBaseTLVParser> Registry;
117 /** \brief set maximum value of TLV length field
119 The size of the length field will be increased if necessary.
120 \param v maximum value of length field
122 void maxLength(MIHTLVLengthParser::value_type maxl) const;
124 void validateType(boost::uint8_t type) const;
125 void validateTypeLength(boost::uint8_t type, MIHTLVLengthParser::value_type length) const;
130 /** \brief Parser for a generic TLV packet
132 struct MIHGenericTLVParser
133 : public GenericTLVParserBase<MIHBaseTLVParser>
135 typedef senf::GenericTLVParserBase<MIHBaseTLVParser> base;
136 MIHGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
140 maxLength( MIHTLVLengthParser::max_value);
144 using base::maxLength;
148 /** \brief Base class for list TLV parser
150 struct MIHBaseListTLVParser
151 : public MIHBaseTLVParser
153 # include SENF_PARSER()
154 SENF_PARSER_INHERIT ( MIHBaseTLVParser );
155 SENF_PARSER_FIELD_RO ( listSize, MIHTLVLengthParser );
156 SENF_PARSER_FINALIZE ( MIHBaseListTLVParser );
158 void maxListSize(MIHTLVLengthParser::value_type maxl) const;
161 template <class Self>
162 struct MIHListTLVParserMixin
168 /** \brief Parse a MIHF_ID
170 Note that the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
171 We could set maxLength in init(), but for the most MIHF_IDs the default
172 maximum length of 128 should be enough.
174 \note you must call maxIdLength( 253) *before* setting MIHF_IDs values longer
179 class MIHFIdTLVParser : public MIHBaseTLVParser
181 # include SENF_PARSER()
182 SENF_PARSER_INHERIT ( MIHBaseTLVParser );
183 SENF_PARSER_FIELD_RO ( idLength, MIHTLVLengthParser );
184 SENF_PARSER_LABEL ( idValue );
185 SENF_PARSER_SKIP ( idLength(), 0 );
186 SENF_PARSER_FINALIZE ( MIHFIdTLVParser );
189 ///\name Value setters
191 void value( MIHFId const & id);
193 void value( std::string const & id );
194 void value( senf::MACAddress const & addr);
195 void value( senf::INet4Address const & addr);
196 void value( senf::INet6Address const & addr);
197 void value( senf::EUI64 const & addr);
200 ///\name Value getters
202 MIHFId valueAs( MIHFId::Type type) const;
204 std::string valueAsString() const;
205 senf::MACAddress valueAsMACAddress() const;
206 senf::INet4Address valueAsINet4Address() const;
207 senf::INet6Address valueAsINet6Address() const;
208 senf::EUI64 valueAsEUI64() const;
211 ///\name Value comparisons
213 bool valueEquals( MIHFId const & id) const;
215 bool valueEquals( std::string const & id ) const;
216 bool valueEquals( senf::MACAddress const & addr) const;
217 bool valueEquals( senf::INet4Address const & addr) const;
218 bool valueEquals( senf::INet6Address const & addr) const;
219 bool valueEquals( senf::EUI64 const & addr) const;
222 void dump(std::ostream & os) const;
223 void maxIdLength(boost::uint8_t maxl) const;
227 /// resize the packet after the length field to given size
228 senf::safe_data_iterator resizeValueField(MIHTLVLengthParser::value_type size);
230 data_iterator valueBegin() const;
231 data_iterator valueEnd() const;
233 template <class OutputIterator>
234 struct binaryNAIEncoder {
235 binaryNAIEncoder(OutputIterator & i);
236 void operator()(boost::uint8_t v);
240 template <class OutputIterator>
241 static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> >
242 getNAIEncodedOutputIterator(OutputIterator i);
244 struct binaryNAIDecoder {
246 bool operator()(boost::uint8_t v);
250 template <class Iterator>
251 static boost::filter_iterator<binaryNAIDecoder, Iterator>
252 getNAIDecodedIterator(Iterator begin, Iterator end);
254 struct ValueSetterVisitor : public boost::static_visitor<> {
255 MIHFIdTLVParser & parser;
256 ValueSetterVisitor( MIHFIdTLVParser & p) : parser(p) {}
257 void operator()( boost::blank ) const {
258 parser.value( std::string());
260 template <typename MIHFIdType>
261 void operator()( MIHFIdType const & id ) const {
266 struct ValueEqualsVisitor : public boost::static_visitor<bool> {
267 MIHFIdTLVParser const & parser;
268 ValueEqualsVisitor( MIHFIdTLVParser const & p) : parser(p) {}
269 bool operator()( boost::blank ) const {
270 return parser.idLength() == 0;
272 template <typename MIHFIdType>
273 bool operator()( MIHFIdType const & id ) const {
274 return parser.valueEquals( id);
279 /** \brief Parser for 802.21 source MIHF_ID TLV
281 struct MIHFSrcIdTLVParser : public MIHFIdTLVParser
283 MIHFSrcIdTLVParser(data_iterator i, state_type s) : MIHFIdTLVParser(i,s) {}
289 static type_t::value_type const typeId = 1;
290 void dump(std::ostream & os) const;
291 void validate() const;
294 /** \brief Parser for 802.21 destination MIHF_ID TLV
296 struct MIHFDstIdTLVParser : public MIHFIdTLVParser
298 MIHFDstIdTLVParser(data_iterator i, state_type s) : MIHFIdTLVParser(i,s) {}
304 static type_t::value_type const typeId = 2;
305 void dump(std::ostream & os) const;
306 void validate() const;
309 /** \brief Parser for 802.21 Status TLV
311 struct MIHStatusTLVParser : public MIHBaseTLVParser
313 # include SENF_PARSER()
314 SENF_PARSER_INHERIT ( MIHBaseTLVParser );
315 SENF_PARSER_FIELD ( value, UInt8Parser );
316 SENF_PARSER_FINALIZE( MIHStatusTLVParser );
323 static type_t::value_type const typeId = 3;
324 void dump(std::ostream & os) const; ///< dump string representation to given stream
325 void validate() const;
328 Success, UnspecifiedFailure, Rejected, AuthorizationFailure, NetworkError };
331 struct MIHRegisterReqCodeTLVParser : public MIHBaseTLVParser
333 # include SENF_PARSER()
334 SENF_PARSER_INHERIT ( MIHBaseTLVParser );
335 SENF_PARSER_FIELD ( value, UInt8Parser );
336 SENF_PARSER_FINALIZE ( MIHRegisterReqCodeTLVParser );
343 static type_t::value_type const typeId = 11;
344 void dump(std::ostream & os) const; ///< dump string representation to given stream
345 void validate() const;
347 enum RequestCode { Registration, ReRegistration };
350 struct MIHValidTimeIntervalTLVParser : public MIHBaseTLVParser
352 # include SENF_PARSER()
353 SENF_PARSER_INHERIT ( MIHBaseTLVParser );
354 SENF_PARSER_FIELD ( value, UInt32Parser );
355 SENF_PARSER_FINALIZE ( MIHValidTimeIntervalTLVParser );
362 static type_t::value_type const typeId = 12;
363 void dump(std::ostream & os) const; ///< dump string representation to given stream
364 void validate() const;
370 //-/////////////////////////////////////////////////////////////////////////////////////////////////
371 #include "TLVParser.cci"
372 #include "TLVParser.ct"
373 #include "TLVParser.cti"
380 // c-file-style: "senf"
381 // indent-tabs-mode: nil
382 // ispell-local-dictionary: "american"
383 // compile-command: "scons -u test"
384 // comment-column: 40