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