c337aecbcb3e826746254735caa849e0d29603a9
[senf.git] / Packets / 80221Bundle / TLVPacket.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 TLVPacket public header */
25
26 #ifndef HH_SENF_Packets_80221Bundle_TLVPacket_
27 #define HH_SENF_Packets_80221Bundle_TLVPacket_ 1
28
29 // Custom includes
30 #include <algorithm>
31 #include "../../Packets/Packets.hh"
32
33 //#include "TLVPacket.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37
38
39     struct TLVLengthException : public senf::Exception
40     { TLVLengthException() 
41           : senf::Exception("TLVLengthException"){} };
42
43     
44     class DynamicTLVLengthParser 
45         : public detail::packet::IntParserOps<DynamicTLVLengthParser, boost::uint32_t>,
46           public PacketParserBase
47     {
48     public:
49         DynamicTLVLengthParser(data_iterator i, state_type s) : PacketParserBase(i,s) {}
50
51         typedef boost::uint32_t value_type;
52         static const size_type init_bytes = 1;
53         static value_type const min_value = 0;
54         static value_type const max_value = 4294967295u;
55
56         value_type value() const;
57         void value(value_type const & v);
58         
59         DynamicTLVLengthParser const & operator= (value_type other);
60         size_type bytes() const;
61         void init() const;
62
63 #       include SENF_PARSER()
64         SENF_PARSER_PRIVATE_BITFIELD ( extended_length_flag, 1,  bool     );
65         SENF_PARSER_PRIVATE_BITFIELD ( fixed_length_field,   7,  unsigned );
66
67         void shrink();
68         void maxValue(DynamicTLVLengthParser::value_type v);
69     private:
70         void resize(size_type size);
71     };  
72         
73
74     class BaseTLVPacketParser : public PacketParserBase
75     {
76 #       include SENF_PARSER()
77         SENF_PARSER_FIELD    ( type,   UInt8Parser            );
78         SENF_PARSER_FIELD_RO ( length, DynamicTLVLengthParser );
79         SENF_PARSER_FINALIZE ( BaseTLVPacketParser            );
80         
81         void maxLengthValue(DynamicTLVLengthParser::value_type v) const {
82             length_().maxValue(v);
83         }
84         void shrinkLength() { 
85             length_().shrink(); 
86         };
87         
88     protected:
89         size_type length_bytes() const { return length_().bytes(); };
90         void length(DynamicTLVLengthParser::value_type &v) { length_() = v; };
91         senf::safe_data_iterator resizeValue(DynamicTLVLengthParser::value_type size);
92     };
93
94         
95     struct GenericTLVPacketParser : public BaseTLVPacketParser
96     {
97 #       include SENF_PARSER()        
98         SENF_PARSER_INHERIT  ( BaseTLVPacketParser    );
99         SENF_PARSER_SKIP     ( length(), 0            );
100         SENF_PARSER_FINALIZE ( GenericTLVPacketParser );
101         
102         SENF_PARSER_INIT() {
103             maxLengthValue( DynamicTLVLengthParser::max_value);
104         }
105         
106         senf::PacketInterpreterBase::range value() const;
107         
108         template <class ForwardReadableRange>
109         void value(ForwardReadableRange const &range);
110     };
111     
112     struct GenericTLVPacketType
113         : public PacketTypeBase,
114           public PacketTypeMixin<GenericTLVPacketType>
115     {
116         typedef PacketTypeMixin<GenericTLVPacketType> mixin;
117         typedef ConcretePacket<GenericTLVPacketType> packet;
118         typedef GenericTLVPacketParser parser;
119
120         using mixin::nextPacketRange;
121         using mixin::init;
122         using mixin::initSize;
123         
124         static void finalize(packet p);
125         static void dump(packet p, std::ostream & os);
126     };
127     
128     typedef ConcretePacket<GenericTLVPacketType> GenericTLVPacket;
129 }
130
131
132 ///////////////////////////////hh.e////////////////////////////////////////
133 //#include "TLVPacket.cci"
134 #include "TLVPacket.ct"
135 //#include "TLVPacket.cti"
136 #endif
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // comment-column: 40
147 // End: