4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@berlios.de>
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.
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.
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.
24 \brief TLVParser non-inline non-template implementation */
26 #include "TLVParser.hh"
27 //#include "TLVParser.ih"
30 #include <senf/Utils/hexdump.hh>
31 #include <senf/Utils/Format.hh>
32 #include <senf/Utils/String.hh>
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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 );
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 prefix_ void senf::MIHBaseTLVParser::validateType(boost::uint8_t expectedType)
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());
54 prefix_ void senf::MIHBaseTLVParser::validateTypeLength(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength)
57 validateType( expectedLength);
58 if (length() != expectedLength)
59 throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << senf::str(type());
62 //-/////////////////////////////////////////////////////////////////////////////////////////////////
63 // senf::MIHBaseListTLVParser
65 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
68 protect(), listSize_().capacity( maxl);
69 maxLength( maxl + senf::bytes(listSize_()));
72 //-/////////////////////////////////////////////////////////////////////////////////////////////////
73 // senf::MIHFIdTLVParser
75 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
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);
86 prefix_ void senf::MIHFIdTLVParser::finalize()
88 protect(), idLength_().finalize();
89 length_() << idLength() + senf::bytes(idLength_());
90 MIHBaseTLVParser::finalize();
93 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
96 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
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_()));
103 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
104 MIHTLVLengthParser::value_type size)
106 MIHTLVLengthParser::value_type current_length ( idLength());
108 length_() << size + idLength_().bytes();
110 safe_data_iterator si (data(), valueBegin());
111 if (current_length > size)
112 data().erase( si, boost::next(si, current_length-size));
114 data().insert( si, size-current_length, 0);
118 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
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)
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);
128 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
130 safe_data_iterator si = resizeValueField(6*2);
131 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
134 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
136 safe_data_iterator si = resizeValueField(4*2);
137 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
140 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
142 safe_data_iterator si = resizeValueField(16*2);
143 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
146 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
148 safe_data_iterator si = resizeValueField(8*2);
149 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
152 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
154 boost::apply_visitor( ValueSetterVisitor(*this), id);
157 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
160 if (length() == 0) return MIHFId();
162 case MIHFId::MulticastType:
164 case MIHFId::MACAddress:
165 return MIHFId( valueAsMACAddress());
166 case MIHFId::INet4Address:
167 return MIHFId( valueAsINet4Address());
168 case MIHFId::INet6Address:
169 return MIHFId( valueAsINet6Address());
171 return MIHFId( valueAsString());
173 return MIHFId( valueAsEUI64());
179 //-/////////////////////////////////////////////////////////////////////////////////////////////////
180 // senf::MIHFSrcIdTLVParser
182 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
185 senf::format::IndentHelper indent;
186 os << indent << "source MIHF_Id TLV:\n";
187 MIHFIdTLVParser::dump(os);
190 prefix_ void senf::MIHFSrcIdTLVParser::validate()
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());
200 //-/////////////////////////////////////////////////////////////////////////////////////////////////
201 // senf::MIHFDstIdTLVParser
203 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
206 senf::format::IndentHelper indent;
207 os << indent << "destination MIHF_Id TLV:\n";
208 MIHFIdTLVParser::dump(os);
211 prefix_ void senf::MIHFDstIdTLVParser::validate()
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());
220 //-/////////////////////////////////////////////////////////////////////////////////////////////////
221 // senf::MIHStatusTLVParser
223 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
226 senf::format::IndentHelper indent;
227 os << indent << "Status TLV:" << std::endl;
229 os << indent << "type: " << unsigned( type()) << std::endl
230 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
231 << indent << "value: " << unsigned( value());
234 os << " (Success)" << std::endl;
236 case UnspecifiedFailure:
237 os << " (Unspecified Failure)" << std::endl;
240 os << " (Rejected)" << std::endl;
242 case AuthorizationFailure:
243 os << " (Authorization Failure)" << std::endl;
246 os << " (Network Error)" << std::endl;
249 os << " (???; invalid value!)" << std::endl;
252 prefix_ void senf::MIHStatusTLVParser::validate()
255 validateTypeLength( typeId, 1);
257 throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << senf::str(value());
260 //-/////////////////////////////////////////////////////////////////////////////////////////////////
261 // senf::MIHRegisterReqCodeTLVParser
263 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
266 senf::format::IndentHelper indent;
267 os << indent << "Register Request Code TLV:" << std::endl;
269 os << indent << "type: " << unsigned( type()) << std::endl
270 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
271 << indent << "value: " << unsigned( value());
274 os << " (Registration)" << std::endl;
277 os << " (Re-Registration)" << std::endl;
280 os << " (???; invalid value!)" << std::endl;
283 prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
286 validateTypeLength( typeId, 1);
288 throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << senf::str(value());
291 //-/////////////////////////////////////////////////////////////////////////////////////////////////
292 // senf::MIHValidTimeIntervalTLVParser
294 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
297 senf::format::IndentHelper indent;
298 os << indent << "Valid Time Interval TLV:" << std::endl;
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;
306 prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
309 validateTypeLength( typeId, 4);
312 //-/////////////////////////////////////////////////////////////////////////////////////////////////
313 // senf::MIHTLVLengthParser
315 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
319 return length_field().value();
321 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
323 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
325 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
327 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
329 throw( MIHTLVLengthException());
333 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
337 if (v > 128) throw( MIHTLVLengthException());
341 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
342 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
345 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
346 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
349 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
350 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
353 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
356 throw( MIHTLVLengthException());
358 underflow_flag() = (v <= 128);
361 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
368 return UInt8Parser::max_value + 128;
370 return UInt16Parser::max_value + 128;
372 return UInt24Parser::max_value + 128;
374 return UInt32Parser::max_value;
376 throw( MIHTLVLengthException());
380 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
386 prefix_ void senf::MIHTLVLengthParser::init() const
389 extended_length_flag() = false;
392 prefix_ void senf::MIHTLVLengthParser::finalize()
394 value_type v = value();
395 size_type b = bytes();
397 if (b != 1) resize_(1);
400 if (v <= UInt8Parser::max_value + 128) {
401 if (b != 2) resize_(2);
404 if (v <= UInt16Parser::max_value + 128) {
405 if (b != 3) resize_(3);
408 if (v <= UInt24Parser::max_value + 128 ) {
409 if (b != 4) resize_(4);
412 if (b != 5) resize_(5);
415 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
419 size_type b = bytes();
420 if (v <= UInt8Parser::max_value + 128) {
421 if (b < 2) resize_(2);
424 if (v <= UInt16Parser::max_value + 128) {
425 if (b < 3) resize_(3);
428 if (v <= UInt24Parser::max_value + 128) {
429 if (b < 4) resize_(4);
432 if (b < 5) resize_(5);
435 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
437 value_type v = value();
438 resize(bytes(), size);
440 extended_length_flag() = true;
441 fixed_length_field() = size - 1;
443 extended_length_flag() = false;
449 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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