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 TLVPacket public header */
26 #ifndef HH_SENF_Packets_80221Bundle_TLVPacket_
27 #define HH_SENF_Packets_80221Bundle_TLVPacket_ 1
31 #include "../../Packets/Packets.hh"
33 //#include "TLVPacket.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
38 struct TLVLengthException : public senf::Exception
41 : senf::Exception("TLVLengthException") {}
45 class DynamicTLVLengthParser
46 : public detail::packet::IntParserOps<DynamicTLVLengthParser, boost::uint32_t>,
47 public PacketParserBase
50 DynamicTLVLengthParser(data_iterator i, state_type s) : PacketParserBase(i,s) {}
52 typedef boost::uint32_t value_type;
53 static const size_type init_bytes = 1;
54 static value_type const min_value = 0;
55 static value_type const max_value = 4294967295u;
57 value_type value() const;
58 void value(value_type const & v);
60 DynamicTLVLengthParser const & operator= (value_type other);
61 size_type bytes() const;
64 # include SENF_PARSER()
65 SENF_PARSER_PRIVATE_FIELD ( length_field, UInt8Parser );
66 SENF_PARSER_GOTO( length_field );
67 SENF_PARSER_PRIVATE_BITFIELD ( extended_length_flag, 1, bool );
68 SENF_PARSER_PRIVATE_BITFIELD ( underflow_flag, 1, bool );
69 SENF_PARSER_PRIVATE_BITFIELD ( fixed_length_field, 6, unsigned );
72 void maxValue(value_type v);
73 value_type maxValue() const;
75 void resize(size_type size);
79 /** \brief Base class for TLV-Packet-Parsers
81 BaseTLVPacketParser is the abstract base class for TLV-Packet-Parsers. It defines the
82 \ref type() field as an \ref senf::UInt8Parser and the \ref length() field as a
83 DynamicTLVLengthParser. The length field is read-only.
85 To create your own \c TLVParser you have to inherit from BaseTLVPacketParser (don't
86 forget \ref SENF_PARSER_INHERIT) and define the \c value field. In the following example
87 the value is a vector of MacAddresses:
89 struct MacAddressesTLVParser : public BaseTLVPacketParser {
90 # include SENF_PARSER()
91 SENF_PARSER_INHERIT ( BaseTLVPacketParser );
92 SENF_PARSER_VECTOR ( value, bytes(length), senf::MACAddressParser );
93 SENF_PARSER_FINALIZE( MacAddressesTLVParser );
96 struct MacAddressesTLVPacketType : public PacketTypeBase {
97 typedef MacAddressesTLVParser parser;
99 static void finalize(ConcretePacket<MacAddressesTLVPacketType> p) {
105 You have to adjust the maximum length value with the \ref maxLengthValue function
106 before the length value is set. The default maximum value is 127. So, in the above
107 example adding more than 21 MACAddresses to the vector will throw a TLVLengthException
108 if you don't call \c macAddressesTLVPacket->maxLengthValue( \e some_value) before.
110 \see DynamicTLVLengthParser \n
111 GenericTLVPacketParser \n
113 class BaseTLVPacketParser : public PacketParserBase
116 # include SENF_PARSER()
117 SENF_PARSER_FIELD ( type, UInt8Parser );
118 SENF_PARSER_FIELD_RO ( length, DynamicTLVLengthParser );
119 SENF_PARSER_FINALIZE ( BaseTLVPacketParser );
121 /** \brief set maximum value of length field
123 The size of the length field will be increased if necessary.
124 \param v maximum value of length field
126 void maxLengthValue(DynamicTLVLengthParser::value_type v) const {
127 length_().maxValue(v);
130 /** \brief shrink size of length field to minimum
132 The size of the length field will be decreased to minimum necessary to hold
133 the current length value.
135 void finalizeLength() {
136 length_().finalize();
140 /// return size of length field
141 size_type length_bytes() const { return length_().bytes(); };
142 /// set length field to given value
143 void length(DynamicTLVLengthParser::value_type &v) { length_() = v; };
144 /// resize the Packet after the length field to given size
145 senf::safe_data_iterator resizeValueField(DynamicTLVLengthParser::value_type size);
149 /** \brief Parser for a generic TLV packet
151 \see GenericTLVPacketType
153 struct GenericTLVPacketParser : public BaseTLVPacketParser
155 # include SENF_PARSER()
156 SENF_PARSER_INHERIT ( BaseTLVPacketParser );
157 SENF_PARSER_SKIP ( length(), 0 );
158 SENF_PARSER_FINALIZE ( GenericTLVPacketParser );
161 maxLengthValue( DynamicTLVLengthParser::max_value);
164 senf::PacketInterpreterBase::range value() const;
166 template <class ForwardReadableRange>
167 void value(ForwardReadableRange const &range);
170 /** \brief Generic TLV packet
172 \par Packet type (typedef):
173 \ref GenericTLVPacket
177 \ingroup protocolbundle_80221
179 struct GenericTLVPacketType
180 : public PacketTypeBase,
181 public PacketTypeMixin<GenericTLVPacketType>
184 typedef PacketTypeMixin<GenericTLVPacketType> mixin;
186 typedef ConcretePacket<GenericTLVPacketType> packet; ///< GenericTLV packet typedef
187 typedef GenericTLVPacketParser parser; ///< typedef to the parser of GenericTLV packet
189 using mixin::nextPacketRange;
191 using mixin::initSize;
193 /** \brief Dump given GenericTLVPacket in readable form to given output stream */
194 static void dump(packet p, std::ostream & os);
195 static void finalize(packet p); ///< Finalize packet.
196 /**< shrink size of length field to minimum
197 \see BaseTLVPacketParser::shrinkLength() */
201 /** \brief GenericTLV packet typedef */
202 typedef ConcretePacket<GenericTLVPacketType> GenericTLVPacket;
206 ///////////////////////////////hh.e////////////////////////////////////////
207 #include "TLVPacket.cci"
208 #include "TLVPacket.ct"
209 //#include "TLVPacket.cti"
216 // c-file-style: "senf"
217 // indent-tabs-mode: nil
218 // ispell-local-dictionary: "american"
219 // compile-command: "scons -u test"
220 // comment-column: 40