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 if (type() != expectedType)
56 throw InvalidMIHPacketException("wrong TLV type. expected ") << unsigned(expectedType) << " got " << unsigned(type());
59 prefix_ void senf::MIHBaseTLVParser::validateTypeLength(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength)
62 validateType( expectedType);
63 if (length() != expectedLength)
64 throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << unsigned(type());
67 //-/////////////////////////////////////////////////////////////////////////////////////////////////
68 // senf::MIHBaseListTLVParser
70 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
73 protect(), listSize_().capacity( maxl);
74 maxLength( maxl + senf::bytes(listSize_()));
77 //-/////////////////////////////////////////////////////////////////////////////////////////////////
78 // senf::MIHFIdTLVParser
80 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
83 senf::format::IndentHelper indent;
84 os << indent << "type: " << unsigned (type()) << std::endl
85 << indent << "length: " << unsigned (length()) << std::endl
86 << indent << "value:\n";
87 std::string src_mihfId (valueAsString());
88 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
91 prefix_ void senf::MIHFIdTLVParser::finalize()
93 protect(), idLength_().finalize();
94 length_() << idLength() + senf::bytes(idLength_());
95 MIHBaseTLVParser::finalize();
98 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
101 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
103 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
104 protect(), idLength_().capacity( maxl);
105 maxLength( maxl + senf::bytes(idLength_()));
108 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
109 MIHTLVLengthParser::value_type size)
111 MIHTLVLengthParser::value_type current_length ( idLength());
113 length_() << size + idLength_().bytes();
115 safe_data_iterator si (data(), valueBegin());
116 if (current_length > size)
117 data().erase( si, boost::next(si, current_length-size));
119 data().insert( si, size-current_length, 0);
123 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
125 size_type str_size (id.size());
126 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
128 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
129 safe_data_iterator si = resizeValueField( str_size);
130 std::copy( id.begin(), id.end(), si);
133 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
135 safe_data_iterator si = resizeValueField(6*2);
136 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
139 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
141 safe_data_iterator si = resizeValueField(4*2);
142 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
145 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
147 safe_data_iterator si = resizeValueField(16*2);
148 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
151 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
153 safe_data_iterator si = resizeValueField(8*2);
154 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
157 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
159 boost::apply_visitor( ValueSetterVisitor(*this), id);
162 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
165 if (length() == 0) return MIHFId();
167 case MIHFId::MulticastType:
169 case MIHFId::MACAddress:
170 return MIHFId( valueAsMACAddress());
171 case MIHFId::INet4Address:
172 return MIHFId( valueAsINet4Address());
173 case MIHFId::INet6Address:
174 return MIHFId( valueAsINet6Address());
176 return MIHFId( valueAsString());
178 return MIHFId( valueAsEUI64());
184 //-/////////////////////////////////////////////////////////////////////////////////////////////////
185 // senf::MIHFSrcIdTLVParser
187 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
190 senf::format::IndentHelper indent;
191 os << indent << "source MIHF_Id TLV:\n";
192 MIHFIdTLVParser::dump(os);
195 prefix_ void senf::MIHFSrcIdTLVParser::validate()
198 validateType( typeId);
202 //-/////////////////////////////////////////////////////////////////////////////////////////////////
203 // senf::MIHFDstIdTLVParser
205 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
208 senf::format::IndentHelper indent;
209 os << indent << "destination MIHF_Id TLV:\n";
210 MIHFIdTLVParser::dump(os);
213 prefix_ void senf::MIHFDstIdTLVParser::validate()
216 validateType( typeId);
219 //-/////////////////////////////////////////////////////////////////////////////////////////////////
220 // senf::MIHStatusTLVParser
222 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
225 senf::format::IndentHelper indent;
226 os << indent << "Status TLV:" << std::endl;
228 os << indent << "type: " << unsigned( type()) << std::endl
229 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
230 << indent << "value: " << unsigned( value());
233 os << " (Success)" << std::endl;
235 case UnspecifiedFailure:
236 os << " (Unspecified Failure)" << std::endl;
239 os << " (Rejected)" << std::endl;
241 case AuthorizationFailure:
242 os << " (Authorization Failure)" << std::endl;
245 os << " (Network Error)" << std::endl;
248 os << " (???; invalid value!)" << std::endl;
251 prefix_ void senf::MIHStatusTLVParser::validate()
254 validateTypeLength( typeId, 1);
256 throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << unsigned( value());
259 //-/////////////////////////////////////////////////////////////////////////////////////////////////
260 // senf::MIHRegisterReqCodeTLVParser
262 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
265 senf::format::IndentHelper indent;
266 os << indent << "Register Request Code TLV:" << std::endl;
268 os << indent << "type: " << unsigned( type()) << std::endl
269 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
270 << indent << "value: " << unsigned( value());
273 os << " (Registration)" << std::endl;
276 os << " (Re-Registration)" << std::endl;
279 os << " (???; invalid value!)" << std::endl;
282 prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
285 validateTypeLength( typeId, 1);
287 throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << unsigned( value());
290 //-/////////////////////////////////////////////////////////////////////////////////////////////////
291 // senf::MIHValidTimeIntervalTLVParser
293 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
296 senf::format::IndentHelper indent;
297 os << indent << "Valid Time Interval TLV:" << std::endl;
299 os << indent << "type: " << unsigned( type()) << std::endl
300 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
301 << indent << "value: " << unsigned( value())
302 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
305 prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
308 validateTypeLength( typeId, 4);
311 //-/////////////////////////////////////////////////////////////////////////////////////////////////
312 // senf::MIHTLVLengthParser
314 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
318 return length_field().value();
320 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
322 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
324 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
326 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
328 throw( MIHTLVLengthException());
332 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
336 if (v > 128) throw( MIHTLVLengthException());
340 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
341 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
344 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
345 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
348 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
349 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
352 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
355 throw( MIHTLVLengthException());
357 underflow_flag() = (v <= 128);
360 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
367 return UInt8Parser::max_value + 128;
369 return UInt16Parser::max_value + 128;
371 return UInt24Parser::max_value + 128;
373 return UInt32Parser::max_value;
375 throw( MIHTLVLengthException());
379 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
385 prefix_ void senf::MIHTLVLengthParser::init() const
388 extended_length_flag() = false;
391 prefix_ void senf::MIHTLVLengthParser::finalize()
393 value_type v = value();
394 size_type b = bytes();
396 if (b != 1) resize_(1);
399 if (v <= UInt8Parser::max_value + 128) {
400 if (b != 2) resize_(2);
403 if (v <= UInt16Parser::max_value + 128) {
404 if (b != 3) resize_(3);
407 if (v <= UInt24Parser::max_value + 128 ) {
408 if (b != 4) resize_(4);
411 if (b != 5) resize_(5);
414 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
418 size_type b = bytes();
419 if (v <= UInt8Parser::max_value + 128) {
420 if (b < 2) resize_(2);
423 if (v <= UInt16Parser::max_value + 128) {
424 if (b < 3) resize_(3);
427 if (v <= UInt24Parser::max_value + 128) {
428 if (b < 4) resize_(4);
431 if (b < 5) resize_(5);
434 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
436 value_type v = value();
437 resize(bytes(), size);
439 extended_length_flag() = true;
440 fixed_length_field() = size - 1;
442 extended_length_flag() = false;
448 //-/////////////////////////////////////////////////////////////////////////////////////////////////
455 // c-file-style: "senf"
456 // indent-tabs-mode: nil
457 // ispell-local-dictionary: "american"
458 // compile-command: "scons -u test"
459 // comment-column: 40