4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Thorsten Horstmann <tho@berlios.de>
29 \brief TLVParser non-inline non-template implementation */
31 #include "TLVParser.hh"
32 //#include "TLVParser.ih"
35 #include <senf/Utils/hexdump.hh>
36 #include <senf/Utils/Format.hh>
37 #include "Exceptions.hh"
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFSrcIdTLVParser );
43 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFDstIdTLVParser );
44 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHStatusTLVParser );
45 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHValidTimeIntervalTLVParser );
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
50 prefix_ void senf::MIHBaseTLVParser::validateType(boost::uint8_t expectedType)
53 if (! check( 1 + senf::bytes(length_()) + length()) )
54 throw InvalidMIHPacketException("truncated TLV.") << " Type: " << unsigned(type())
55 << ", size:" << 1 + senf::bytes(length_()) + length()
56 << ", available: " << std::distance(i(), data().end());
57 if (type() != expectedType)
58 throw InvalidMIHPacketException("wrong TLV type. expected ") << unsigned(expectedType) << " got " << unsigned(type());
61 prefix_ void senf::MIHBaseTLVParser::validateTypeLength(boost::uint8_t expectedType, MIHLengthParser::value_type expectedLength)
64 validateType( expectedType);
65 if (length() != expectedLength)
66 throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << unsigned(type())
67 << ", expected length: " << expectedLength << " got " << length();
70 //-/////////////////////////////////////////////////////////////////////////////////////////////////
71 // senf::MIHBaseListTLVParser
73 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHLengthParser::value_type maxl)
76 protect(), listSize_().capacity( maxl);
77 maxLength( maxl + senf::bytes(listSize_()));
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::MIHFIdTLVParser
83 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
86 senf::format::IndentHelper indent;
87 os << indent << "type: " << unsigned (type()) << std::endl
88 << indent << "length: " << unsigned (length()) << std::endl
89 << indent << "value:\n";
90 std::string src_mihfId (valueAsString());
91 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
94 prefix_ void senf::MIHFIdTLVParser::finalize()
96 protect(), idLength_().finalize();
97 length_() << idLength() + senf::bytes(idLength_());
98 MIHBaseTLVParser::finalize();
101 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
104 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
106 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
107 protect(), idLength_().capacity( maxl);
108 maxLength( maxl + senf::bytes(idLength_()));
111 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
112 MIHLengthParser::value_type size)
114 MIHLengthParser::value_type current_length ( idLength());
116 length_() << size + idLength_().bytes();
118 safe_data_iterator si (data(), valueBegin());
119 if (current_length > size)
120 data().erase( si, boost::next(si, current_length-size));
122 data().insert( si, size-current_length, 0);
126 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
128 size_type str_size (id.size());
129 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
131 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
132 safe_data_iterator si = resizeValueField( str_size);
133 std::copy( id.begin(), id.end(), si);
136 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
138 safe_data_iterator si = resizeValueField(6*2);
139 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
142 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
144 safe_data_iterator si = resizeValueField(4*2);
145 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
148 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
150 safe_data_iterator si = resizeValueField(16*2);
151 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
154 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
156 safe_data_iterator si = resizeValueField(8*2);
157 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
160 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
162 boost::apply_visitor( ValueSetterVisitor(*this), id);
165 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
168 if (length() == 0) return MIHFId();
170 case MIHFId::MulticastType:
172 case MIHFId::MACAddress:
173 return MIHFId( valueAsMACAddress());
174 case MIHFId::INet4Address:
175 return MIHFId( valueAsINet4Address());
176 case MIHFId::INet6Address:
177 return MIHFId( valueAsINet6Address());
179 return MIHFId( valueAsString());
181 return MIHFId( valueAsEUI64());
187 //-/////////////////////////////////////////////////////////////////////////////////////////////////
188 // senf::MIHFSrcIdTLVParser
190 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
193 senf::format::IndentHelper indent;
194 os << indent << "source MIHF_Id TLV:\n";
195 MIHFIdTLVParser::dump(os);
198 prefix_ void senf::MIHFSrcIdTLVParser::validate()
201 validateType( typeId);
205 //-/////////////////////////////////////////////////////////////////////////////////////////////////
206 // senf::MIHFDstIdTLVParser
208 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
211 senf::format::IndentHelper indent;
212 os << indent << "destination MIHF_Id TLV:\n";
213 MIHFIdTLVParser::dump(os);
216 prefix_ void senf::MIHFDstIdTLVParser::validate()
219 validateType( typeId);
222 //-/////////////////////////////////////////////////////////////////////////////////////////////////
223 // senf::MIHStatusTLVParser
225 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
228 senf::format::IndentHelper indent;
229 os << indent << "Status TLV:" << std::endl;
231 os << indent << "type: " << unsigned( type()) << std::endl
232 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
233 << indent << "value: " << unsigned( value());
236 os << " (Success)" << std::endl;
238 case UnspecifiedFailure:
239 os << " (Unspecified Failure)" << std::endl;
242 os << " (Rejected)" << std::endl;
244 case AuthorizationFailure:
245 os << " (Authorization Failure)" << std::endl;
248 os << " (Network Error)" << std::endl;
251 os << " (???; invalid value!)" << std::endl;
254 prefix_ void senf::MIHStatusTLVParser::validate()
257 validateTypeLength( typeId, 1);
259 throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << unsigned( value());
262 //-/////////////////////////////////////////////////////////////////////////////////////////////////
263 // senf::MIHRegisterReqCodeTLVParser
265 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
268 senf::format::IndentHelper indent;
269 os << indent << "Register Request Code TLV:" << std::endl;
271 os << indent << "type: " << unsigned( type()) << std::endl
272 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
273 << indent << "value: " << unsigned( value());
276 os << " (Registration)" << std::endl;
279 os << " (Re-Registration)" << std::endl;
282 os << " (???; invalid value!)" << std::endl;
285 prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
288 validateTypeLength( typeId, 1);
290 throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << unsigned( value());
293 //-/////////////////////////////////////////////////////////////////////////////////////////////////
294 // senf::MIHValidTimeIntervalTLVParser
296 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
299 senf::format::IndentHelper indent;
300 os << indent << "Valid Time Interval TLV:" << std::endl;
302 os << indent << "type: " << unsigned( type()) << std::endl
303 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
304 << indent << "value: " << unsigned( value())
305 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
308 prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
311 validateTypeLength( typeId, 4);
314 //-/////////////////////////////////////////////////////////////////////////////////////////////////
315 // senf::MIHLengthParser
317 prefix_ senf::MIHLengthParser::value_type senf::MIHLengthParser::value() const
321 return length_field().value();
323 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
325 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
327 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
329 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
331 throw( MIHLengthException());
335 prefix_ void senf::MIHLengthParser::value(value_type const & v)
339 if (v > 128) throw( MIHLengthException());
343 if (v > UInt8Parser::max_value + 128) throw( MIHLengthException());
344 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
347 if (v > UInt16Parser::max_value + 128) throw( MIHLengthException());
348 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
351 if (v > UInt24Parser::max_value + 128) throw( MIHLengthException());
352 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
355 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
358 throw( MIHLengthException());
360 underflow_flag() = (v <= 128);
363 prefix_ senf::MIHLengthParser::value_type senf::MIHLengthParser::capacity()
370 return UInt8Parser::max_value + 128;
372 return UInt16Parser::max_value + 128;
374 return UInt24Parser::max_value + 128;
376 return UInt32Parser::max_value;
378 throw( MIHLengthException());
382 prefix_ senf::MIHLengthParser const & senf::MIHLengthParser::operator= (value_type other)
388 prefix_ void senf::MIHLengthParser::init() const
391 extended_length_flag() = false;
394 prefix_ void senf::MIHLengthParser::finalize()
396 value_type v = value();
397 size_type b = bytes();
399 if (b != 1) resize_(1);
402 if (v <= UInt8Parser::max_value + 128) {
403 if (b != 2) resize_(2);
406 if (v <= UInt16Parser::max_value + 128) {
407 if (b != 3) resize_(3);
410 if (v <= UInt24Parser::max_value + 128 ) {
411 if (b != 4) resize_(4);
414 if (b != 5) resize_(5);
417 prefix_ void senf::MIHLengthParser::capacity(MIHLengthParser::value_type v)
421 size_type b = bytes();
422 if (v <= UInt8Parser::max_value + 128) {
423 if (b < 2) resize_(2);
426 if (v <= UInt16Parser::max_value + 128) {
427 if (b < 3) resize_(3);
430 if (v <= UInt24Parser::max_value + 128) {
431 if (b < 4) resize_(4);
434 if (b < 5) resize_(5);
437 prefix_ void senf::MIHLengthParser::resize_(size_type size)
439 value_type v = value();
440 resize(bytes(), size);
442 extended_length_flag() = true;
443 fixed_length_field() = size - 1;
445 extended_length_flag() = false;
451 //-/////////////////////////////////////////////////////////////////////////////////////////////////
458 // c-file-style: "senf"
459 // indent-tabs-mode: nil
460 // ispell-local-dictionary: "american"
461 // compile-command: "scons -u test"
462 // comment-column: 40