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::validateTL(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength)
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") << " Type: " << senf::str(type());
52 if (length() != expectedLength)
53 throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << senf::str(type());
56 //-/////////////////////////////////////////////////////////////////////////////////////////////////
57 // senf::MIHBaseListTLVParser
59 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
62 protect(), listSize_().capacity( maxl);
63 maxLength( maxl + senf::bytes(listSize_()));
67 //-/////////////////////////////////////////////////////////////////////////////////////////////////
68 // senf::MIHFIdTLVParser
70 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
73 senf::format::IndentHelper indent;
74 os << indent << "type: " << unsigned (type()) << std::endl
75 << indent << "length: " << unsigned (length()) << std::endl
76 << indent << "value:\n";
77 std::string src_mihfId (valueAsString());
78 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
81 prefix_ void senf::MIHFIdTLVParser::finalize()
83 protect(), idLength_().finalize();
84 length_() << idLength() + senf::bytes(idLength_());
85 MIHBaseTLVParser::finalize();
88 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
91 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
93 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
94 protect(), idLength_().capacity( maxl);
95 maxLength( maxl + senf::bytes(idLength_()));
98 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
99 MIHTLVLengthParser::value_type size)
101 MIHTLVLengthParser::value_type current_length ( idLength());
103 length_() << size + idLength_().bytes();
105 safe_data_iterator si (data(), valueBegin());
106 if (current_length > size)
107 data().erase( si, boost::next(si, current_length-size));
109 data().insert( si, size-current_length, 0);
113 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
115 size_type str_size (id.size());
116 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
118 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
119 safe_data_iterator si = resizeValueField( str_size);
120 std::copy( id.begin(), id.end(), si);
123 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
125 safe_data_iterator si = resizeValueField(6*2);
126 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
129 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
131 safe_data_iterator si = resizeValueField(4*2);
132 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
135 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
137 safe_data_iterator si = resizeValueField(16*2);
138 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
141 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
143 safe_data_iterator si = resizeValueField(8*2);
144 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
147 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
149 boost::apply_visitor( ValueSetterVisitor(*this), id);
152 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
155 if (length() == 0) return MIHFId();
157 case MIHFId::MulticastType:
159 case MIHFId::MACAddress:
160 return MIHFId( valueAsMACAddress());
161 case MIHFId::INet4Address:
162 return MIHFId( valueAsINet4Address());
163 case MIHFId::INet6Address:
164 return MIHFId( valueAsINet6Address());
166 return MIHFId( valueAsString());
168 return MIHFId( valueAsEUI64());
174 //-/////////////////////////////////////////////////////////////////////////////////////////////////
175 // senf::MIHFSrcIdTLVParser
177 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
180 senf::format::IndentHelper indent;
181 os << indent << "source MIHF_Id TLV:\n";
182 MIHFIdTLVParser::dump(os);
185 prefix_ void senf::MIHFSrcIdTLVParser::validate()
188 if (! check( 1 + senf::bytes(length_()) + length()) )
189 throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type());
190 if (type() != typeId)
191 throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type());
195 //-/////////////////////////////////////////////////////////////////////////////////////////////////
196 // senf::MIHFDstIdTLVParser
198 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
201 senf::format::IndentHelper indent;
202 os << indent << "destination MIHF_Id TLV:\n";
203 MIHFIdTLVParser::dump(os);
206 prefix_ void senf::MIHFDstIdTLVParser::validate()
209 if (! check( 1 + senf::bytes(length_()) + length()) )
210 throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type());
211 if (type() != typeId)
212 throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type());
215 //-/////////////////////////////////////////////////////////////////////////////////////////////////
216 // senf::MIHStatusTLVParser
218 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
221 senf::format::IndentHelper indent;
222 os << indent << "Status TLV:" << std::endl;
224 os << indent << "type: " << unsigned( type()) << std::endl
225 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
226 << indent << "value: " << unsigned( value());
229 os << " (Success)" << std::endl;
231 case UnspecifiedFailure:
232 os << " (Unspecified Failure)" << std::endl;
235 os << " (Rejected)" << std::endl;
237 case AuthorizationFailure:
238 os << " (Authorization Failure)" << std::endl;
241 os << " (Network Error)" << std::endl;
244 os << " (???; invalid value!)" << std::endl;
247 prefix_ void senf::MIHStatusTLVParser::validate()
250 validateTL( typeId, 1);
252 throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << senf::str(value());
255 //-/////////////////////////////////////////////////////////////////////////////////////////////////
256 // senf::MIHRegisterReqCodeTLVParser
258 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
261 senf::format::IndentHelper indent;
262 os << indent << "Register Request Code TLV:" << std::endl;
264 os << indent << "type: " << unsigned( type()) << std::endl
265 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
266 << indent << "value: " << unsigned( value());
269 os << " (Registration)" << std::endl;
272 os << " (Re-Registration)" << std::endl;
275 os << " (???; invalid value!)" << std::endl;
278 prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
281 validateTL( typeId, 1);
283 throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << senf::str(value());
286 //-/////////////////////////////////////////////////////////////////////////////////////////////////
287 // senf::MIHValidTimeIntervalTLVParser
289 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
292 senf::format::IndentHelper indent;
293 os << indent << "Valid Time Interval TLV:" << std::endl;
295 os << indent << "type: " << unsigned( type()) << std::endl
296 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
297 << indent << "value: " << unsigned( value())
298 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
301 prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
304 validateTL( typeId, 4);
307 //-/////////////////////////////////////////////////////////////////////////////////////////////////
308 // senf::MIHTLVLengthParser
310 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
314 return length_field().value();
316 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
318 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
320 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
322 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
324 throw( MIHTLVLengthException());
328 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
332 if (v > 128) throw( MIHTLVLengthException());
336 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
337 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
340 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
341 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
344 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
345 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
348 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
351 throw( MIHTLVLengthException());
353 underflow_flag() = (v <= 128);
356 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
363 return UInt8Parser::max_value + 128;
365 return UInt16Parser::max_value + 128;
367 return UInt24Parser::max_value + 128;
369 return UInt32Parser::max_value;
371 throw( MIHTLVLengthException());
375 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
381 prefix_ void senf::MIHTLVLengthParser::init() const
384 extended_length_flag() = false;
387 prefix_ void senf::MIHTLVLengthParser::finalize()
389 value_type v = value();
390 size_type b = bytes();
392 if (b != 1) resize_(1);
395 if (v <= UInt8Parser::max_value + 128) {
396 if (b != 2) resize_(2);
399 if (v <= UInt16Parser::max_value + 128) {
400 if (b != 3) resize_(3);
403 if (v <= UInt24Parser::max_value + 128 ) {
404 if (b != 4) resize_(4);
407 if (b != 5) resize_(5);
410 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
414 size_type b = bytes();
415 if (v <= UInt8Parser::max_value + 128) {
416 if (b < 2) resize_(2);
419 if (v <= UInt16Parser::max_value + 128) {
420 if (b < 3) resize_(3);
423 if (v <= UInt24Parser::max_value + 128) {
424 if (b < 4) resize_(4);
427 if (b < 5) resize_(5);
430 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
432 value_type v = value();
433 resize(bytes(), size);
435 extended_length_flag() = true;
436 fixed_length_field() = size - 1;
438 extended_length_flag() = false;
444 //-/////////////////////////////////////////////////////////////////////////////////////////////////
451 // c-file-style: "senf"
452 // indent-tabs-mode: nil
453 // ispell-local-dictionary: "american"
454 // compile-command: "scons -u test"
455 // comment-column: 40