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>
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFSrcIdTLVParser );
37 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFDstIdTLVParser );
38 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHStatusTLVParser );
39 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHValidTimeIntervalTLVParser );
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 prefix_ void senf::MIHBaseTLVParser::validateType(boost::uint8_t expectedType)
47 if (! check( 1 + senf::bytes(length_()) + length()) )
48 throw InvalidMIHPacketException("truncated TLV.") << " Type: " << unsigned(type());
49 if (type() != expectedType)
50 throw InvalidMIHPacketException("wrong TLV type. expected ") << unsigned(expectedType) << " got " << unsigned(type());
53 prefix_ void senf::MIHBaseTLVParser::validateTypeLength(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength)
56 validateType( expectedType);
57 if (length() != expectedLength)
58 throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << unsigned(type());
61 //-/////////////////////////////////////////////////////////////////////////////////////////////////
62 // senf::MIHBaseListTLVParser
64 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
67 protect(), listSize_().capacity( maxl);
68 maxLength( maxl + senf::bytes(listSize_()));
71 //-/////////////////////////////////////////////////////////////////////////////////////////////////
72 // senf::MIHFIdTLVParser
74 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
77 senf::format::IndentHelper indent;
78 os << indent << "type: " << unsigned (type()) << std::endl
79 << indent << "length: " << unsigned (length()) << std::endl
80 << indent << "value:\n";
81 std::string src_mihfId (valueAsString());
82 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
85 prefix_ void senf::MIHFIdTLVParser::finalize()
87 protect(), idLength_().finalize();
88 length_() << idLength() + senf::bytes(idLength_());
89 MIHBaseTLVParser::finalize();
92 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
95 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
97 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
98 protect(), idLength_().capacity( maxl);
99 maxLength( maxl + senf::bytes(idLength_()));
102 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
103 MIHTLVLengthParser::value_type size)
105 MIHTLVLengthParser::value_type current_length ( idLength());
107 length_() << size + idLength_().bytes();
109 safe_data_iterator si (data(), valueBegin());
110 if (current_length > size)
111 data().erase( si, boost::next(si, current_length-size));
113 data().insert( si, size-current_length, 0);
117 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
119 size_type str_size (id.size());
120 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
122 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
123 safe_data_iterator si = resizeValueField( str_size);
124 std::copy( id.begin(), id.end(), si);
127 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
129 safe_data_iterator si = resizeValueField(6*2);
130 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
133 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
135 safe_data_iterator si = resizeValueField(4*2);
136 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
139 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
141 safe_data_iterator si = resizeValueField(16*2);
142 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
145 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
147 safe_data_iterator si = resizeValueField(8*2);
148 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
151 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
153 boost::apply_visitor( ValueSetterVisitor(*this), id);
156 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
159 if (length() == 0) return MIHFId();
161 case MIHFId::MulticastType:
163 case MIHFId::MACAddress:
164 return MIHFId( valueAsMACAddress());
165 case MIHFId::INet4Address:
166 return MIHFId( valueAsINet4Address());
167 case MIHFId::INet6Address:
168 return MIHFId( valueAsINet6Address());
170 return MIHFId( valueAsString());
172 return MIHFId( valueAsEUI64());
178 //-/////////////////////////////////////////////////////////////////////////////////////////////////
179 // senf::MIHFSrcIdTLVParser
181 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
184 senf::format::IndentHelper indent;
185 os << indent << "source MIHF_Id TLV:\n";
186 MIHFIdTLVParser::dump(os);
189 prefix_ void senf::MIHFSrcIdTLVParser::validate()
192 validateType( typeId);
196 //-/////////////////////////////////////////////////////////////////////////////////////////////////
197 // senf::MIHFDstIdTLVParser
199 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
202 senf::format::IndentHelper indent;
203 os << indent << "destination MIHF_Id TLV:\n";
204 MIHFIdTLVParser::dump(os);
207 prefix_ void senf::MIHFDstIdTLVParser::validate()
210 validateType( typeId);
213 //-/////////////////////////////////////////////////////////////////////////////////////////////////
214 // senf::MIHStatusTLVParser
216 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
219 senf::format::IndentHelper indent;
220 os << indent << "Status TLV:" << std::endl;
222 os << indent << "type: " << unsigned( type()) << std::endl
223 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
224 << indent << "value: " << unsigned( value());
227 os << " (Success)" << std::endl;
229 case UnspecifiedFailure:
230 os << " (Unspecified Failure)" << std::endl;
233 os << " (Rejected)" << std::endl;
235 case AuthorizationFailure:
236 os << " (Authorization Failure)" << std::endl;
239 os << " (Network Error)" << std::endl;
242 os << " (???; invalid value!)" << std::endl;
245 prefix_ void senf::MIHStatusTLVParser::validate()
248 validateTypeLength( typeId, 1);
250 throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << unsigned( value());
253 //-/////////////////////////////////////////////////////////////////////////////////////////////////
254 // senf::MIHRegisterReqCodeTLVParser
256 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
259 senf::format::IndentHelper indent;
260 os << indent << "Register Request Code TLV:" << std::endl;
262 os << indent << "type: " << unsigned( type()) << std::endl
263 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
264 << indent << "value: " << unsigned( value());
267 os << " (Registration)" << std::endl;
270 os << " (Re-Registration)" << std::endl;
273 os << " (???; invalid value!)" << std::endl;
276 prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
279 validateTypeLength( typeId, 1);
281 throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << unsigned( value());
284 //-/////////////////////////////////////////////////////////////////////////////////////////////////
285 // senf::MIHValidTimeIntervalTLVParser
287 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
290 senf::format::IndentHelper indent;
291 os << indent << "Valid Time Interval TLV:" << std::endl;
293 os << indent << "type: " << unsigned( type()) << std::endl
294 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
295 << indent << "value: " << unsigned( value())
296 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
299 prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
302 validateTypeLength( typeId, 4);
305 //-/////////////////////////////////////////////////////////////////////////////////////////////////
306 // senf::MIHTLVLengthParser
308 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
312 return length_field().value();
314 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
316 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
318 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
320 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
322 throw( MIHTLVLengthException());
326 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
330 if (v > 128) throw( MIHTLVLengthException());
334 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
335 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
338 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
339 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
342 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
343 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
346 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
349 throw( MIHTLVLengthException());
351 underflow_flag() = (v <= 128);
354 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
361 return UInt8Parser::max_value + 128;
363 return UInt16Parser::max_value + 128;
365 return UInt24Parser::max_value + 128;
367 return UInt32Parser::max_value;
369 throw( MIHTLVLengthException());
373 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
379 prefix_ void senf::MIHTLVLengthParser::init() const
382 extended_length_flag() = false;
385 prefix_ void senf::MIHTLVLengthParser::finalize()
387 value_type v = value();
388 size_type b = bytes();
390 if (b != 1) resize_(1);
393 if (v <= UInt8Parser::max_value + 128) {
394 if (b != 2) resize_(2);
397 if (v <= UInt16Parser::max_value + 128) {
398 if (b != 3) resize_(3);
401 if (v <= UInt24Parser::max_value + 128 ) {
402 if (b != 4) resize_(4);
405 if (b != 5) resize_(5);
408 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
412 size_type b = bytes();
413 if (v <= UInt8Parser::max_value + 128) {
414 if (b < 2) resize_(2);
417 if (v <= UInt16Parser::max_value + 128) {
418 if (b < 3) resize_(3);
421 if (v <= UInt24Parser::max_value + 128) {
422 if (b < 4) resize_(4);
425 if (b < 5) resize_(5);
428 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
430 value_type v = value();
431 resize(bytes(), size);
433 extended_length_flag() = true;
434 fixed_length_field() = size - 1;
436 extended_length_flag() = false;
442 //-/////////////////////////////////////////////////////////////////////////////////////////////////
449 // c-file-style: "senf"
450 // indent-tabs-mode: nil
451 // ispell-local-dictionary: "american"
452 // compile-command: "scons -u test"
453 // comment-column: 40