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"
31 #include <senf/Utils/hexdump.hh>
32 #include <senf/Utils/Format.hh>
33 #include <senf/Utils/String.hh>
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFSrcIdTLVParser );
39 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFDstIdTLVParser );
40 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHStatusTLVParser );
41 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHValidTimeIntervalTLVParser );
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::MIHBaseListTLVParser
46 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
49 protect(), listSize_().capacity( maxl);
50 maxLength( maxl + senf::bytes(listSize_()));
53 //-/////////////////////////////////////////////////////////////////////////////////////////////////
54 // senf::MIHFIdTLVParser
56 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
59 senf::format::IndentHelper indent;
60 os << indent << "type: " << unsigned (type()) << std::endl
61 << indent << "length: " << unsigned (length()) << std::endl
62 << indent << "value:\n";
63 std::string src_mihfId (valueAsString());
64 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
67 prefix_ void senf::MIHFIdTLVParser::finalize()
69 protect(), idLength_().finalize();
70 length_() << idLength() + senf::bytes(idLength_());
71 MIHBaseTLVParser::finalize();
74 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
77 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
79 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
80 protect(), idLength_().capacity( maxl);
81 maxLength( maxl + senf::bytes(idLength_()));
84 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
85 MIHTLVLengthParser::value_type size)
87 MIHTLVLengthParser::value_type current_length ( idLength());
89 length_() << size + idLength_().bytes();
91 safe_data_iterator si (data(), valueBegin());
92 if (current_length > size)
93 data().erase( si, boost::next(si, current_length-size));
95 data().insert( si, size-current_length, 0);
99 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
101 size_type str_size (id.size());
102 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
104 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
105 safe_data_iterator si = resizeValueField( str_size);
106 std::copy( id.begin(), id.end(), si);
109 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
111 safe_data_iterator si = resizeValueField(6*2);
112 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
115 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
117 safe_data_iterator si = resizeValueField(4*2);
118 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
121 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
123 safe_data_iterator si = resizeValueField(16*2);
124 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
127 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
129 safe_data_iterator si = resizeValueField(8*2);
130 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
133 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
135 boost::apply_visitor( ValueSetterVisitor(*this), id);
138 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
141 if (length() == 0) return MIHFId();
143 case MIHFId::MulticastType:
145 case MIHFId::MACAddress:
146 return MIHFId( valueAsMACAddress());
147 case MIHFId::INet4Address:
148 return MIHFId( valueAsINet4Address());
149 case MIHFId::INet6Address:
150 return MIHFId( valueAsINet6Address());
152 return MIHFId( valueAsString());
154 return MIHFId( valueAsEUI64());
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161 // senf::MIHFSrcIdTLVParser
163 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
166 senf::format::IndentHelper indent;
167 os << indent << "source MIHF_Id TLV:\n";
168 MIHFIdTLVParser::dump(os);
171 //-/////////////////////////////////////////////////////////////////////////////////////////////////
172 // senf::MIHFDstIdTLVParser
174 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
177 senf::format::IndentHelper indent;
178 os << indent << "destination MIHF_Id TLV:\n";
179 MIHFIdTLVParser::dump(os);
182 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 // senf::MIHStatusTLVParser
185 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
188 senf::format::IndentHelper indent;
189 os << indent << "Status TLV:" << std::endl;
191 os << indent << "type: " << unsigned( type()) << std::endl
192 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
193 << indent << "value: " << unsigned( value());
196 os << " (Success)" << std::endl;
198 case UnspecifiedFailure:
199 os << " (Unspecified Failure)" << std::endl;
202 os << " (Rejected)" << std::endl;
204 case AuthorizationFailure:
205 os << " (Authorization Failure)" << std::endl;
208 os << " (Network Error)" << std::endl;
211 os << " (???; invalid value!)" << std::endl;
214 //-/////////////////////////////////////////////////////////////////////////////////////////////////
215 // senf::MIHRegisterReqCodeTLVParser
217 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
220 senf::format::IndentHelper indent;
221 os << indent << "Register Request Code TLV:" << std::endl;
223 os << indent << "type: " << unsigned( type()) << std::endl
224 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
225 << indent << "value: " << unsigned( value());
228 os << " (Registration)" << std::endl;
231 os << " (Re-Registration)" << std::endl;
234 os << " (???; invalid value!)" << std::endl;
237 prefix_ std::pair<bool, std::string> senf::MIHRegisterReqCodeTLVParser::validate()
240 if (length() != 1) return std::make_pair(false, "invalid length in MIHRegisterReqCodeTLV " + senf::str(length()));
241 if (value() >= 2) return std::make_pair(false, "invalid value in MIHRegisterReqCodeTLV " + senf::str(value()));
242 return std::make_pair(true, "");
245 //-/////////////////////////////////////////////////////////////////////////////////////////////////
246 // senf::MIHValidTimeIntervalTLVParser
248 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
251 senf::format::IndentHelper indent;
252 os << indent << "Valid Time Interval TLV:" << std::endl;
254 os << indent << "type: " << unsigned( type()) << std::endl
255 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
256 << indent << "value: " << unsigned( value())
257 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
260 prefix_ std::pair<bool, std::string> senf::MIHValidTimeIntervalTLVParser::validate()
263 if (length() != 4) return std::make_pair(false, "invalid length in MIHValidTimeIntervalTLV " + senf::str(length()));
264 return std::make_pair(true, "");
267 //-/////////////////////////////////////////////////////////////////////////////////////////////////
268 // senf::MIHTLVLengthParser
270 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
274 return length_field().value();
276 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
278 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
280 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
282 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
284 throw( MIHTLVLengthException());
288 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
292 if (v > 128) throw( MIHTLVLengthException());
296 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
297 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
300 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
301 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
304 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
305 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
308 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
311 throw( MIHTLVLengthException());
313 underflow_flag() = (v <= 128);
316 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
323 return UInt8Parser::max_value + 128;
325 return UInt16Parser::max_value + 128;
327 return UInt24Parser::max_value + 128;
329 return UInt32Parser::max_value;
331 throw( MIHTLVLengthException());
335 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
341 prefix_ void senf::MIHTLVLengthParser::init() const
344 extended_length_flag() = false;
347 prefix_ void senf::MIHTLVLengthParser::finalize()
349 value_type v = value();
350 size_type b = bytes();
352 if (b != 1) resize_(1);
355 if (v <= UInt8Parser::max_value + 128) {
356 if (b != 2) resize_(2);
359 if (v <= UInt16Parser::max_value + 128) {
360 if (b != 3) resize_(3);
363 if (v <= UInt24Parser::max_value + 128 ) {
364 if (b != 4) resize_(4);
367 if (b != 5) resize_(5);
370 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
374 size_type b = bytes();
375 if (v <= UInt8Parser::max_value + 128) {
376 if (b < 2) resize_(2);
379 if (v <= UInt16Parser::max_value + 128) {
380 if (b < 3) resize_(3);
383 if (v <= UInt24Parser::max_value + 128) {
384 if (b < 4) resize_(4);
387 if (b < 5) resize_(5);
390 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
392 value_type v = value();
393 resize(bytes(), size);
395 extended_length_flag() = true;
396 fixed_length_field() = size - 1;
398 extended_length_flag() = false;
404 //-/////////////////////////////////////////////////////////////////////////////////////////////////
411 // c-file-style: "senf"
412 // indent-tabs-mode: nil
413 // ispell-local-dictionary: "american"
414 // compile-command: "scons -u test"
415 // comment-column: 40