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