Packets/80221Bundle: fixed error in MIHF_ID-TLV; restructured TLV finalize()
[senf.git] / senf / Packets / 80221Bundle / TLVParser.cc
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 non-inline non-template implementation */
25
26 #include "TLVParser.hh"
27 //#include "TLVParser.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <senf/Utils/hexdump.hh>
32 #include <senf/Utils/Format.hh>
33
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFSrcIdTLVParser );
38 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFDstIdTLVParser );
39 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHStatusTLVParser );
40 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHValidTimeIntervalTLVParser );
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::MIHFIdTLVParser
44
45 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
46     const
47 {
48     senf::format::IndentHelper indent;
49     os << indent << "type:   " << unsigned (type()) << std::endl
50        << indent << "length: " << unsigned (length()) << std::endl
51        << indent << "value:\n";
52     std::string src_mihfId (valueAsString());
53     hexdump(src_mihfId.begin(), src_mihfId.end(), os);
54 }
55
56 prefix_ void senf::MIHFIdTLVParser::finalize()
57 {
58     protect(), idLength_().finalize();
59     length_() << idLength() + idLength_().bytes();
60     MIHBaseTLVParser::finalize();
61 }
62
63 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxLength)
64 {
65     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
66     if (maxLength > 253) 
67         throw std::length_error("maximum length of a MIHF_ID is 253 octets");
68     protect(), idLength_().maxValue( maxLength);
69     maxLengthValue( maxLength + senf::bytes(idLength_()));
70 }
71
72 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
73         MIHTLVLengthParser::value_type size) 
74 {
75     MIHTLVLengthParser::value_type current_length ( idLength());
76     idLength_() << size;
77     length_() << size + idLength_().bytes();
78
79     safe_data_iterator si (data(), valueBegin());
80     if (current_length > size)
81         data().erase( si, boost::next(si, current_length-size));
82     else
83         data().insert( si, size-current_length, 0);
84     return si;
85 }
86
87 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
88 {
89     size_type str_size (id.size());
90     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
91     if (str_size > 253) 
92         throw std::length_error("maximum length of a MIHF_ID is 253 octets");
93     safe_data_iterator si = resizeValueField( str_size);   
94     std::copy( id.begin(), id.end(), si);
95 }
96
97 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
98 {
99     safe_data_iterator si = resizeValueField(6*2);
100     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
101 }
102
103 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
104 {
105     safe_data_iterator si = resizeValueField(4*2);
106     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
107 }
108
109 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
110 {
111     safe_data_iterator si = resizeValueField(16*2);
112     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
113 }
114
115 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
116 {
117     safe_data_iterator si = resizeValueField(8*2);
118     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
119 }
120
121 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
122 {
123     boost::apply_visitor( ValueSetterVisitor(*this), id);
124 }
125
126 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
127     const
128 {
129     if (length() == 0) return MIHFId();
130     switch (type) {
131     case MIHFId::Empty:
132         return MIHFId();
133     case MIHFId::MACAddress:
134         return MIHFId( valueAsMACAddress());
135     case MIHFId::INet4Address:
136         return MIHFId( valueAsINet4Address());
137     case MIHFId::INet6Address:
138         return MIHFId( valueAsINet6Address());
139     case MIHFId::String:
140         return MIHFId( valueAsString());
141     case MIHFId::EUI64:
142         return MIHFId( valueAsEUI64());
143     }
144     return MIHFId();
145 }
146
147
148 ///////////////////////////////////////////////////////////////////////////
149 // senf::MIHFSrcIdTLVParser
150
151 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
152     const
153 {
154     senf::format::IndentHelper indent;
155     os << indent << "source MIHF_Id TLV:\n";
156     MIHFIdTLVParser::dump(os);
157 }
158
159 ///////////////////////////////////////////////////////////////////////////
160 // senf::MIHFDstIdTLVParser
161
162 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
163     const
164 {
165     senf::format::IndentHelper indent;
166     os << indent << "destination MIHF_Id TLV:\n";
167     MIHFIdTLVParser::dump(os);
168 }
169
170 ///////////////////////////////////////////////////////////////////////////
171 // senf::MIHStatusTLVParser
172
173 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
174     const
175 {
176     senf::format::IndentHelper indent;
177     os << indent << "Status TLV:" << std::endl;
178     indent.increase();
179     os << indent <<   "type:   " << unsigned( type()) << std::endl
180        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
181        << indent <<   "value:  " << unsigned( value());
182     switch (value()) {
183     case Success:
184         os << " (Success)" << std::endl;
185         return;
186     case UnspecifiedFailure:  
187         os << " (Unspecified Failure)" << std::endl;
188         return;
189     case Rejected:
190         os << " (Rejected)" << std::endl;
191         return;
192     case AuthorizationFailure:
193         os << " (Authorization Failure)" << std::endl;
194         return;
195     case NetworkError:
196         os << " (Network Error)" << std::endl;
197         return;
198     }
199     os << " (???; invalid value!)" << std::endl;
200 }
201
202 ///////////////////////////////////////////////////////////////////////////
203 // senf::MIHRegisterReqCodeTLVParser
204
205 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
206     const
207 {
208     senf::format::IndentHelper indent;
209     os << indent << "Register Request Code TLV:" << std::endl;
210     indent.increase();
211     os << indent <<   "type:   " << unsigned( type()) << std::endl
212        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
213        << indent <<   "value:  " << unsigned( value());
214     switch (value()) {
215     case Registration:
216         os << " (Registration)" << std::endl;
217         return;
218     case ReRegistration:
219         os << " (Re-Registration)" << std::endl;
220         return;
221     }
222     os << " (???; invalid value!)" << std::endl;
223 }
224
225 ///////////////////////////////////////////////////////////////////////////
226 // senf::MIHValidTimeIntervalTLVParser
227
228 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
229     const
230 {
231     senf::format::IndentHelper indent;
232     os << indent << "Valid Time Interval TLV:" << std::endl;
233     indent.increase();
234     os << indent <<   "type:   " << unsigned( type()) << std::endl
235        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
236        << indent <<   "value:  " << unsigned( value())
237        << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
238 }
239
240 ///////////////////////////////////////////////////////////////////////////
241 // senf::MIHTLVLengthParser
242
243 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const 
244 {
245     switch (bytes() ) {
246     case 1:
247         return length_field().value();
248     case 2:
249         return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
250     case 3:
251         return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
252     case 4:
253         return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
254     case 5:
255         return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
256     default:
257         throw( MIHTLVLengthException());
258     };
259 }
260
261 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v) 
262 {
263     switch (bytes() ) {
264     case 1:
265         if (v > 128) throw( MIHTLVLengthException());
266         length_field() = v;
267         return;
268     case 2:
269         if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
270         parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
271         break;
272     case 3:
273         if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
274         parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
275         break;;
276     case 4:
277         if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
278         parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
279         break;
280     case 5:
281         parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
282         break;
283     default:
284         throw( MIHTLVLengthException());
285     };
286     underflow_flag() = (v <= 128);
287 }
288
289 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::maxValue()
290     const
291 {
292     switch (bytes() ) {
293     case 1:
294         return 128;
295     case 2:
296         return UInt8Parser::max_value + 128;
297     case 3:
298         return UInt16Parser::max_value + 128;
299     case 4:
300         return UInt24Parser::max_value + 128;
301     case 5:
302         return UInt32Parser::max_value; 
303     default:
304         throw( MIHTLVLengthException());
305     };
306 }
307
308 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other) 
309 {
310     value(other);
311     return *this; 
312 }
313
314 prefix_ void senf::MIHTLVLengthParser::init() const 
315 {
316     defaultInit();
317     extended_length_flag() = false;
318 }
319
320 prefix_ void senf::MIHTLVLengthParser::finalize()
321 {
322     value_type v = value();
323     size_type b = bytes();
324     if (v <= 128) {
325         if (b != 1) resize_(1);
326         return;
327     }
328     if (v <= UInt8Parser::max_value + 128) {
329         if (b != 2) resize_(2);
330         return;
331     }
332     if (v <= UInt16Parser::max_value + 128) {
333         if (b != 3) resize_(3);
334         return;
335     }
336     if (v <= UInt24Parser::max_value + 128 ) {
337         if (b != 4) resize_(4);
338         return;
339     }
340     if (b != 5) resize_(5);
341 }
342
343 prefix_ void senf::MIHTLVLengthParser::maxValue(MIHTLVLengthParser::value_type v)
344 {
345     if (v <= 128)
346         return;
347     size_type b = bytes();
348     if (v <= UInt8Parser::max_value + 128) {
349         if (b < 2) resize_(2);
350         return;
351     }
352     if (v <= UInt16Parser::max_value + 128) {
353         if (b < 3) resize_(3);
354         return;
355     }
356     if (v <= UInt24Parser::max_value + 128) {
357         if (b < 4) resize_(4);
358         return;
359     }
360     if (b < 5) resize_(5);
361 }
362
363 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
364 {
365     value_type v = value();
366     resize(bytes(), size);
367     if (size > 1) {
368         extended_length_flag() = true;
369         fixed_length_field() = size - 1;
370     } else {
371         extended_length_flag() = false;
372     }
373     value(v);
374 }
375
376
377 ///////////////////////////////cc.e////////////////////////////////////////
378 #undef prefix_
379
380 \f
381 // Local Variables:
382 // mode: c++
383 // fill-column: 100
384 // c-file-style: "senf"
385 // indent-tabs-mode: nil
386 // ispell-local-dictionary: "american"
387 // compile-command: "scons -u test"
388 // comment-column: 40
389 // End: