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