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, MIHTLVLengthParser::value_type expectedLength)
64 validateType( expectedType);
65 if (length() != expectedLength)
66 throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << unsigned(type());
69 //-/////////////////////////////////////////////////////////////////////////////////////////////////
70 // senf::MIHBaseListTLVParser
72 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
75 protect(), listSize_().capacity( maxl);
76 maxLength( maxl + senf::bytes(listSize_()));
79 //-/////////////////////////////////////////////////////////////////////////////////////////////////
80 // senf::MIHFIdTLVParser
82 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
85 senf::format::IndentHelper indent;
86 os << indent << "type: " << unsigned (type()) << std::endl
87 << indent << "length: " << unsigned (length()) << std::endl
88 << indent << "value:\n";
89 std::string src_mihfId (valueAsString());
90 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
93 prefix_ void senf::MIHFIdTLVParser::finalize()
95 protect(), idLength_().finalize();
96 length_() << idLength() + senf::bytes(idLength_());
97 MIHBaseTLVParser::finalize();
100 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
103 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
105 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
106 protect(), idLength_().capacity( maxl);
107 maxLength( maxl + senf::bytes(idLength_()));
110 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
111 MIHTLVLengthParser::value_type size)
113 MIHTLVLengthParser::value_type current_length ( idLength());
115 length_() << size + idLength_().bytes();
117 safe_data_iterator si (data(), valueBegin());
118 if (current_length > size)
119 data().erase( si, boost::next(si, current_length-size));
121 data().insert( si, size-current_length, 0);
125 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
127 size_type str_size (id.size());
128 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
130 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
131 safe_data_iterator si = resizeValueField( str_size);
132 std::copy( id.begin(), id.end(), si);
135 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
137 safe_data_iterator si = resizeValueField(6*2);
138 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
141 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
143 safe_data_iterator si = resizeValueField(4*2);
144 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
147 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
149 safe_data_iterator si = resizeValueField(16*2);
150 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
153 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
155 safe_data_iterator si = resizeValueField(8*2);
156 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
159 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
161 boost::apply_visitor( ValueSetterVisitor(*this), id);
164 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
167 if (length() == 0) return MIHFId();
169 case MIHFId::MulticastType:
171 case MIHFId::MACAddress:
172 return MIHFId( valueAsMACAddress());
173 case MIHFId::INet4Address:
174 return MIHFId( valueAsINet4Address());
175 case MIHFId::INet6Address:
176 return MIHFId( valueAsINet6Address());
178 return MIHFId( valueAsString());
180 return MIHFId( valueAsEUI64());
186 //-/////////////////////////////////////////////////////////////////////////////////////////////////
187 // senf::MIHFSrcIdTLVParser
189 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
192 senf::format::IndentHelper indent;
193 os << indent << "source MIHF_Id TLV:\n";
194 MIHFIdTLVParser::dump(os);
197 prefix_ void senf::MIHFSrcIdTLVParser::validate()
200 validateType( typeId);
204 //-/////////////////////////////////////////////////////////////////////////////////////////////////
205 // senf::MIHFDstIdTLVParser
207 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
210 senf::format::IndentHelper indent;
211 os << indent << "destination MIHF_Id TLV:\n";
212 MIHFIdTLVParser::dump(os);
215 prefix_ void senf::MIHFDstIdTLVParser::validate()
218 validateType( typeId);
221 //-/////////////////////////////////////////////////////////////////////////////////////////////////
222 // senf::MIHStatusTLVParser
224 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
227 senf::format::IndentHelper indent;
228 os << indent << "Status TLV:" << std::endl;
230 os << indent << "type: " << unsigned( type()) << std::endl
231 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
232 << indent << "value: " << unsigned( value());
235 os << " (Success)" << std::endl;
237 case UnspecifiedFailure:
238 os << " (Unspecified Failure)" << std::endl;
241 os << " (Rejected)" << std::endl;
243 case AuthorizationFailure:
244 os << " (Authorization Failure)" << std::endl;
247 os << " (Network Error)" << std::endl;
250 os << " (???; invalid value!)" << std::endl;
253 prefix_ void senf::MIHStatusTLVParser::validate()
256 validateTypeLength( typeId, 1);
258 throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << unsigned( value());
261 //-/////////////////////////////////////////////////////////////////////////////////////////////////
262 // senf::MIHRegisterReqCodeTLVParser
264 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
267 senf::format::IndentHelper indent;
268 os << indent << "Register Request Code TLV:" << std::endl;
270 os << indent << "type: " << unsigned( type()) << std::endl
271 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
272 << indent << "value: " << unsigned( value());
275 os << " (Registration)" << std::endl;
278 os << " (Re-Registration)" << std::endl;
281 os << " (???; invalid value!)" << std::endl;
284 prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
287 validateTypeLength( typeId, 1);
289 throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << unsigned( value());
292 //-/////////////////////////////////////////////////////////////////////////////////////////////////
293 // senf::MIHValidTimeIntervalTLVParser
295 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
298 senf::format::IndentHelper indent;
299 os << indent << "Valid Time Interval TLV:" << std::endl;
301 os << indent << "type: " << unsigned( type()) << std::endl
302 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
303 << indent << "value: " << unsigned( value())
304 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
307 prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
310 validateTypeLength( typeId, 4);
313 //-/////////////////////////////////////////////////////////////////////////////////////////////////
314 // senf::MIHTLVLengthParser
316 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
320 return length_field().value();
322 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
324 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
326 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
328 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
330 throw( MIHTLVLengthException());
334 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
338 if (v > 128) throw( MIHTLVLengthException());
342 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
343 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
346 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
347 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
350 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
351 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
354 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
357 throw( MIHTLVLengthException());
359 underflow_flag() = (v <= 128);
362 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
369 return UInt8Parser::max_value + 128;
371 return UInt16Parser::max_value + 128;
373 return UInt24Parser::max_value + 128;
375 return UInt32Parser::max_value;
377 throw( MIHTLVLengthException());
381 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
387 prefix_ void senf::MIHTLVLengthParser::init() const
390 extended_length_flag() = false;
393 prefix_ void senf::MIHTLVLengthParser::finalize()
395 value_type v = value();
396 size_type b = bytes();
398 if (b != 1) resize_(1);
401 if (v <= UInt8Parser::max_value + 128) {
402 if (b != 2) resize_(2);
405 if (v <= UInt16Parser::max_value + 128) {
406 if (b != 3) resize_(3);
409 if (v <= UInt24Parser::max_value + 128 ) {
410 if (b != 4) resize_(4);
413 if (b != 5) resize_(5);
416 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
420 size_type b = bytes();
421 if (v <= UInt8Parser::max_value + 128) {
422 if (b < 2) resize_(2);
425 if (v <= UInt16Parser::max_value + 128) {
426 if (b < 3) resize_(3);
429 if (v <= UInt24Parser::max_value + 128) {
430 if (b < 4) resize_(4);
433 if (b < 5) resize_(5);
436 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
438 value_type v = value();
439 resize(bytes(), size);
441 extended_length_flag() = true;
442 fixed_length_field() = size - 1;
444 extended_length_flag() = false;
450 //-/////////////////////////////////////////////////////////////////////////////////////////////////
457 // c-file-style: "senf"
458 // indent-tabs-mode: nil
459 // ispell-local-dictionary: "american"
460 // compile-command: "scons -u test"
461 // comment-column: 40