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_ std::pair<bool, std::string> senf::MIHBaseTLVParser::validateTL(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength)
48 if (! check( 1 + senf::bytes(length_()) + length()) )
49 return std::make_pair(false, "truncated TLV. Type: " + senf::str(type()));
50 if (type() != expectedType)
51 return std::make_pair(false, "invalid TLV type: " + senf::str(type()));
52 if (length() != expectedLength)
53 return std::make_pair(false, "invalid length in TLV. Type: " + senf::str(type()));
54 return std::make_pair(true, "");
57 //-/////////////////////////////////////////////////////////////////////////////////////////////////
58 // senf::MIHBaseListTLVParser
60 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
63 protect(), listSize_().capacity( maxl);
64 maxLength( maxl + senf::bytes(listSize_()));
68 //-/////////////////////////////////////////////////////////////////////////////////////////////////
69 // senf::MIHFIdTLVParser
71 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
74 senf::format::IndentHelper indent;
75 os << indent << "type: " << unsigned (type()) << std::endl
76 << indent << "length: " << unsigned (length()) << std::endl
77 << indent << "value:\n";
78 std::string src_mihfId (valueAsString());
79 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
82 prefix_ void senf::MIHFIdTLVParser::finalize()
84 protect(), idLength_().finalize();
85 length_() << idLength() + senf::bytes(idLength_());
86 MIHBaseTLVParser::finalize();
89 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
92 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
94 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
95 protect(), idLength_().capacity( maxl);
96 maxLength( maxl + senf::bytes(idLength_()));
99 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
100 MIHTLVLengthParser::value_type size)
102 MIHTLVLengthParser::value_type current_length ( idLength());
104 length_() << size + idLength_().bytes();
106 safe_data_iterator si (data(), valueBegin());
107 if (current_length > size)
108 data().erase( si, boost::next(si, current_length-size));
110 data().insert( si, size-current_length, 0);
114 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
116 size_type str_size (id.size());
117 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
119 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
120 safe_data_iterator si = resizeValueField( str_size);
121 std::copy( id.begin(), id.end(), si);
124 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
126 safe_data_iterator si = resizeValueField(6*2);
127 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
130 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
132 safe_data_iterator si = resizeValueField(4*2);
133 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
136 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
138 safe_data_iterator si = resizeValueField(16*2);
139 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
142 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
144 safe_data_iterator si = resizeValueField(8*2);
145 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
148 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
150 boost::apply_visitor( ValueSetterVisitor(*this), id);
153 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
156 if (length() == 0) return MIHFId();
158 case MIHFId::MulticastType:
160 case MIHFId::MACAddress:
161 return MIHFId( valueAsMACAddress());
162 case MIHFId::INet4Address:
163 return MIHFId( valueAsINet4Address());
164 case MIHFId::INet6Address:
165 return MIHFId( valueAsINet6Address());
167 return MIHFId( valueAsString());
169 return MIHFId( valueAsEUI64());
175 //-/////////////////////////////////////////////////////////////////////////////////////////////////
176 // senf::MIHFSrcIdTLVParser
178 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
181 senf::format::IndentHelper indent;
182 os << indent << "source MIHF_Id TLV:\n";
183 MIHFIdTLVParser::dump(os);
186 prefix_ std::pair<bool, std::string> senf::MIHFSrcIdTLVParser::validate()
189 if (! check( 1 + senf::bytes(length_()) + length()) )
190 return std::make_pair(false, "truncated TLV. Type: " + senf::str(type()));
191 if (type() != typeId)
192 return std::make_pair(false, "invalid TLV type: " + senf::str(type()));
193 return std::make_pair(true, "");
197 //-/////////////////////////////////////////////////////////////////////////////////////////////////
198 // senf::MIHFDstIdTLVParser
200 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
203 senf::format::IndentHelper indent;
204 os << indent << "destination MIHF_Id TLV:\n";
205 MIHFIdTLVParser::dump(os);
208 prefix_ std::pair<bool, std::string> senf::MIHFDstIdTLVParser::validate()
211 if (! check( 1 + senf::bytes(length_()) + length()) )
212 return std::make_pair(false, "truncated TLV. Type: " + senf::str(type()));
213 if (type() != typeId)
214 return std::make_pair(false, "invalid TLV type: " + senf::str(type()));
215 return std::make_pair(true, "");
218 //-/////////////////////////////////////////////////////////////////////////////////////////////////
219 // senf::MIHStatusTLVParser
221 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
224 senf::format::IndentHelper indent;
225 os << indent << "Status TLV:" << std::endl;
227 os << indent << "type: " << unsigned( type()) << std::endl
228 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
229 << indent << "value: " << unsigned( value());
232 os << " (Success)" << std::endl;
234 case UnspecifiedFailure:
235 os << " (Unspecified Failure)" << std::endl;
238 os << " (Rejected)" << std::endl;
240 case AuthorizationFailure:
241 os << " (Authorization Failure)" << std::endl;
244 os << " (Network Error)" << std::endl;
247 os << " (???; invalid value!)" << std::endl;
250 prefix_ std::pair<bool, std::string> senf::MIHStatusTLVParser::validate()
253 if (! validateTL( typeId, 1).first)
254 return validateTL( typeId, 1);
256 return std::make_pair(false, "invalid value in MIHStatusTLV " + senf::str(value()));
257 return std::make_pair(true, "");
260 //-/////////////////////////////////////////////////////////////////////////////////////////////////
261 // senf::MIHRegisterReqCodeTLVParser
263 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
266 senf::format::IndentHelper indent;
267 os << indent << "Register Request Code TLV:" << std::endl;
269 os << indent << "type: " << unsigned( type()) << std::endl
270 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
271 << indent << "value: " << unsigned( value());
274 os << " (Registration)" << std::endl;
277 os << " (Re-Registration)" << std::endl;
280 os << " (???; invalid value!)" << std::endl;
283 prefix_ std::pair<bool, std::string> senf::MIHRegisterReqCodeTLVParser::validate()
286 if (! validateTL( typeId, 1).first)
287 return validateTL( typeId, 1);
289 return std::make_pair(false, "invalid value in MIHRegisterReqCodeTLV " + senf::str(value()));
290 return std::make_pair(true, "");
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_ std::pair<bool, std::string> senf::MIHValidTimeIntervalTLVParser::validate()
311 if (! validateTL( typeId, 4).first) return validateTL( typeId, 4);
312 return std::make_pair(true, "");
315 //-/////////////////////////////////////////////////////////////////////////////////////////////////
316 // senf::MIHTLVLengthParser
318 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
322 return length_field().value();
324 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
326 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
328 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
330 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
332 throw( MIHTLVLengthException());
336 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
340 if (v > 128) throw( MIHTLVLengthException());
344 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
345 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
348 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
349 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
352 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
353 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
356 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
359 throw( MIHTLVLengthException());
361 underflow_flag() = (v <= 128);
364 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
371 return UInt8Parser::max_value + 128;
373 return UInt16Parser::max_value + 128;
375 return UInt24Parser::max_value + 128;
377 return UInt32Parser::max_value;
379 throw( MIHTLVLengthException());
383 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
389 prefix_ void senf::MIHTLVLengthParser::init() const
392 extended_length_flag() = false;
395 prefix_ void senf::MIHTLVLengthParser::finalize()
397 value_type v = value();
398 size_type b = bytes();
400 if (b != 1) resize_(1);
403 if (v <= UInt8Parser::max_value + 128) {
404 if (b != 2) resize_(2);
407 if (v <= UInt16Parser::max_value + 128) {
408 if (b != 3) resize_(3);
411 if (v <= UInt24Parser::max_value + 128 ) {
412 if (b != 4) resize_(4);
415 if (b != 5) resize_(5);
418 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
422 size_type b = bytes();
423 if (v <= UInt8Parser::max_value + 128) {
424 if (b < 2) resize_(2);
427 if (v <= UInt16Parser::max_value + 128) {
428 if (b < 3) resize_(3);
431 if (v <= UInt24Parser::max_value + 128) {
432 if (b < 4) resize_(4);
435 if (b < 5) resize_(5);
438 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
440 value_type v = value();
441 resize(bytes(), size);
443 extended_length_flag() = true;
444 fixed_length_field() = size - 1;
446 extended_length_flag() = false;
452 //-/////////////////////////////////////////////////////////////////////////////////////////////////
459 // c-file-style: "senf"
460 // indent-tabs-mode: nil
461 // ispell-local-dictionary: "american"
462 // compile-command: "scons -u test"
463 // comment-column: 40