made some members non-inline to decrease size
[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::validateType(boost::uint8_t expectedType)
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: ") << senf::str(type());
52 }
53
54 prefix_ void senf::MIHBaseTLVParser::validateTypeLength(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength)
55     const
56 {
57     validateType( expectedLength);
58     if (length() != expectedLength)
59         throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << senf::str(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     if (! check( 1 + senf::bytes(length_()) + length()) )
194         throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type());
195     if (type() != typeId)
196         throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type());
197 }
198
199
200 //-/////////////////////////////////////////////////////////////////////////////////////////////////
201 // senf::MIHFDstIdTLVParser
202
203 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
204     const
205 {
206     senf::format::IndentHelper indent;
207     os << indent << "destination MIHF_Id TLV:\n";
208     MIHFIdTLVParser::dump(os);
209 }
210
211 prefix_ void senf::MIHFDstIdTLVParser::validate()
212     const
213 {
214     if (! check( 1 + senf::bytes(length_()) + length()) )
215         throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type());
216     if (type() != typeId)
217         throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type());
218 }
219
220 //-/////////////////////////////////////////////////////////////////////////////////////////////////
221 // senf::MIHStatusTLVParser
222
223 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
224     const
225 {
226     senf::format::IndentHelper indent;
227     os << indent << "Status TLV:" << std::endl;
228     indent.increase();
229     os << indent <<   "type:   " << unsigned( type()) << std::endl
230        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
231        << indent <<   "value:  " << unsigned( value());
232     switch (value()) {
233     case Success:
234         os << " (Success)" << std::endl;
235         return;
236     case UnspecifiedFailure:
237         os << " (Unspecified Failure)" << std::endl;
238         return;
239     case Rejected:
240         os << " (Rejected)" << std::endl;
241         return;
242     case AuthorizationFailure:
243         os << " (Authorization Failure)" << std::endl;
244         return;
245     case NetworkError:
246         os << " (Network Error)" << std::endl;
247         return;
248     }
249     os << " (???; invalid value!)" << std::endl;
250 }
251
252 prefix_ void senf::MIHStatusTLVParser::validate()
253     const
254 {
255     validateTypeLength( typeId, 1);
256     if (value() >= 4)
257         throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << senf::str(value());
258 }
259
260 //-/////////////////////////////////////////////////////////////////////////////////////////////////
261 // senf::MIHRegisterReqCodeTLVParser
262
263 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
264     const
265 {
266     senf::format::IndentHelper indent;
267     os << indent << "Register Request Code TLV:" << std::endl;
268     indent.increase();
269     os << indent <<   "type:   " << unsigned( type()) << std::endl
270        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
271        << indent <<   "value:  " << unsigned( value());
272     switch (value()) {
273     case Registration:
274         os << " (Registration)" << std::endl;
275         return;
276     case ReRegistration:
277         os << " (Re-Registration)" << std::endl;
278         return;
279     }
280     os << " (???; invalid value!)" << std::endl;
281 }
282
283 prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
284     const
285 {
286     validateTypeLength( typeId, 1);
287     if (value() >= 2)
288         throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << senf::str(value());
289 }
290
291 //-/////////////////////////////////////////////////////////////////////////////////////////////////
292 // senf::MIHValidTimeIntervalTLVParser
293
294 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
295     const
296 {
297     senf::format::IndentHelper indent;
298     os << indent << "Valid Time Interval TLV:" << std::endl;
299     indent.increase();
300     os << indent <<   "type:   " << unsigned( type()) << std::endl
301        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
302        << indent <<   "value:  " << unsigned( value())
303        << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
304 }
305
306 prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
307     const
308 {
309     validateTypeLength( typeId, 4);
310 }
311
312 //-/////////////////////////////////////////////////////////////////////////////////////////////////
313 // senf::MIHTLVLengthParser
314
315 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
316 {
317     switch (bytes() ) {
318     case 1:
319         return length_field().value();
320     case 2:
321         return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
322     case 3:
323         return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
324     case 4:
325         return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
326     case 5:
327         return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
328     default:
329         throw( MIHTLVLengthException());
330     };
331 }
332
333 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
334 {
335     switch (bytes() ) {
336     case 1:
337         if (v > 128) throw( MIHTLVLengthException());
338         length_field() = v;
339         return;
340     case 2:
341         if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
342         parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
343         break;
344     case 3:
345         if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
346         parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
347         break;;
348     case 4:
349         if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
350         parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
351         break;
352     case 5:
353         parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
354         break;
355     default:
356         throw( MIHTLVLengthException());
357     };
358     underflow_flag() = (v <= 128);
359 }
360
361 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
362     const
363 {
364     switch (bytes() ) {
365     case 1:
366         return 128;
367     case 2:
368         return UInt8Parser::max_value + 128;
369     case 3:
370         return UInt16Parser::max_value + 128;
371     case 4:
372         return UInt24Parser::max_value + 128;
373     case 5:
374         return UInt32Parser::max_value;
375     default:
376         throw( MIHTLVLengthException());
377     };
378 }
379
380 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
381 {
382     value(other);
383     return *this;
384 }
385
386 prefix_ void senf::MIHTLVLengthParser::init() const
387 {
388     defaultInit();
389     extended_length_flag() = false;
390 }
391
392 prefix_ void senf::MIHTLVLengthParser::finalize()
393 {
394     value_type v = value();
395     size_type b = bytes();
396     if (v <= 128) {
397         if (b != 1) resize_(1);
398         return;
399     }
400     if (v <= UInt8Parser::max_value + 128) {
401         if (b != 2) resize_(2);
402         return;
403     }
404     if (v <= UInt16Parser::max_value + 128) {
405         if (b != 3) resize_(3);
406         return;
407     }
408     if (v <= UInt24Parser::max_value + 128 ) {
409         if (b != 4) resize_(4);
410         return;
411     }
412     if (b != 5) resize_(5);
413 }
414
415 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
416 {
417     if (v <= 128)
418         return;
419     size_type b = bytes();
420     if (v <= UInt8Parser::max_value + 128) {
421         if (b < 2) resize_(2);
422         return;
423     }
424     if (v <= UInt16Parser::max_value + 128) {
425         if (b < 3) resize_(3);
426         return;
427     }
428     if (v <= UInt24Parser::max_value + 128) {
429         if (b < 4) resize_(4);
430         return;
431     }
432     if (b < 5) resize_(5);
433 }
434
435 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
436 {
437     value_type v = value();
438     resize(bytes(), size);
439     if (size > 1) {
440         extended_length_flag() = true;
441         fixed_length_field() = size - 1;
442     } else {
443         extended_length_flag() = false;
444     }
445     value(v);
446 }
447
448
449 //-/////////////////////////////////////////////////////////////////////////////////////////////////
450 #undef prefix_
451
452 \f
453 // Local Variables:
454 // mode: c++
455 // fill-column: 100
456 // c-file-style: "senf"
457 // indent-tabs-mode: nil
458 // ispell-local-dictionary: "american"
459 // compile-command: "scons -u test"
460 // comment-column: 40
461 // End: