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>
35 ///////////////////////////////cc.p////////////////////////////////////////
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 ///////////////////////////////////////////////////////////////////////////
43 // senf::MIHFIdTLVParser
45 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
48 senf::format::IndentHelper indent;
49 os << indent << "type: " << unsigned (type()) << std::endl
50 << indent << "length: " << unsigned (length()) << std::endl
51 << indent << "value:\n";
52 std::string src_mihfId (valueAsString());
53 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
56 prefix_ void senf::MIHFIdTLVParser::finalize()
58 protect(), idLength_().finalize();
59 length_() << idLength() + idLength_().bytes();
60 MIHBaseTLVParser::finalize();
63 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxLength)
65 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
67 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
68 protect(), idLength_().maxValue( maxLength);
69 maxLengthValue( maxLength + senf::bytes(idLength_()));
72 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
73 MIHTLVLengthParser::value_type size)
75 MIHTLVLengthParser::value_type current_length ( idLength());
77 length_() << size + idLength_().bytes();
79 safe_data_iterator si (data(), valueBegin());
80 if (current_length > size)
81 data().erase( si, boost::next(si, current_length-size));
83 data().insert( si, size-current_length, 0);
87 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
89 size_type str_size (id.size());
90 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
92 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
93 safe_data_iterator si = resizeValueField( str_size);
94 std::copy( id.begin(), id.end(), si);
97 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
99 safe_data_iterator si = resizeValueField(6*2);
100 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
103 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
105 safe_data_iterator si = resizeValueField(4*2);
106 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
109 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
111 safe_data_iterator si = resizeValueField(16*2);
112 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
115 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
117 safe_data_iterator si = resizeValueField(8*2);
118 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
121 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
123 boost::apply_visitor( ValueSetterVisitor(*this), id);
126 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
129 if (length() == 0) return MIHFId();
133 case MIHFId::MACAddress:
134 return MIHFId( valueAsMACAddress());
135 case MIHFId::INet4Address:
136 return MIHFId( valueAsINet4Address());
137 case MIHFId::INet6Address:
138 return MIHFId( valueAsINet6Address());
140 return MIHFId( valueAsString());
142 return MIHFId( valueAsEUI64());
148 ///////////////////////////////////////////////////////////////////////////
149 // senf::MIHFSrcIdTLVParser
151 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
154 senf::format::IndentHelper indent;
155 os << indent << "source MIHF_Id TLV:\n";
156 MIHFIdTLVParser::dump(os);
159 ///////////////////////////////////////////////////////////////////////////
160 // senf::MIHFDstIdTLVParser
162 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
165 senf::format::IndentHelper indent;
166 os << indent << "destination MIHF_Id TLV:\n";
167 MIHFIdTLVParser::dump(os);
170 ///////////////////////////////////////////////////////////////////////////
171 // senf::MIHStatusTLVParser
173 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
176 senf::format::IndentHelper indent;
177 os << indent << "Status TLV:" << std::endl;
179 os << indent << "type: " << unsigned( type()) << std::endl
180 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
181 << indent << "value: " << unsigned( value());
184 os << " (Success)" << std::endl;
186 case UnspecifiedFailure:
187 os << " (Unspecified Failure)" << std::endl;
190 os << " (Rejected)" << std::endl;
192 case AuthorizationFailure:
193 os << " (Authorization Failure)" << std::endl;
196 os << " (Network Error)" << std::endl;
199 os << " (???; invalid value!)" << std::endl;
202 ///////////////////////////////////////////////////////////////////////////
203 // senf::MIHRegisterReqCodeTLVParser
205 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
208 senf::format::IndentHelper indent;
209 os << indent << "Register Request Code TLV:" << std::endl;
211 os << indent << "type: " << unsigned( type()) << std::endl
212 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
213 << indent << "value: " << unsigned( value());
216 os << " (Registration)" << std::endl;
219 os << " (Re-Registration)" << std::endl;
222 os << " (???; invalid value!)" << std::endl;
225 ///////////////////////////////////////////////////////////////////////////
226 // senf::MIHValidTimeIntervalTLVParser
228 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
231 senf::format::IndentHelper indent;
232 os << indent << "Valid Time Interval TLV:" << std::endl;
234 os << indent << "type: " << unsigned( type()) << std::endl
235 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
236 << indent << "value: " << unsigned( value())
237 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
240 ///////////////////////////////////////////////////////////////////////////
241 // senf::MIHTLVLengthParser
243 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
247 return length_field().value();
249 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
251 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
253 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
255 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
257 throw( MIHTLVLengthException());
261 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
265 if (v > 128) throw( MIHTLVLengthException());
269 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
270 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
273 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
274 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
277 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
278 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
281 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
284 throw( MIHTLVLengthException());
286 underflow_flag() = (v <= 128);
289 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::maxValue()
296 return UInt8Parser::max_value + 128;
298 return UInt16Parser::max_value + 128;
300 return UInt24Parser::max_value + 128;
302 return UInt32Parser::max_value;
304 throw( MIHTLVLengthException());
308 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
314 prefix_ void senf::MIHTLVLengthParser::init() const
317 extended_length_flag() = false;
320 prefix_ void senf::MIHTLVLengthParser::finalize()
322 value_type v = value();
323 size_type b = bytes();
325 if (b != 1) resize_(1);
328 if (v <= UInt8Parser::max_value + 128) {
329 if (b != 2) resize_(2);
332 if (v <= UInt16Parser::max_value + 128) {
333 if (b != 3) resize_(3);
336 if (v <= UInt24Parser::max_value + 128 ) {
337 if (b != 4) resize_(4);
340 if (b != 5) resize_(5);
343 prefix_ void senf::MIHTLVLengthParser::maxValue(MIHTLVLengthParser::value_type v)
347 size_type b = bytes();
348 if (v <= UInt8Parser::max_value + 128) {
349 if (b < 2) resize_(2);
352 if (v <= UInt16Parser::max_value + 128) {
353 if (b < 3) resize_(3);
356 if (v <= UInt24Parser::max_value + 128) {
357 if (b < 4) resize_(4);
360 if (b < 5) resize_(5);
363 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
365 value_type v = value();
366 resize(bytes(), size);
368 extended_length_flag() = true;
369 fixed_length_field() = size - 1;
371 extended_length_flag() = false;
377 ///////////////////////////////cc.e////////////////////////////////////////
384 // c-file-style: "senf"
385 // indent-tabs-mode: nil
386 // ispell-local-dictionary: "american"
387 // compile-command: "scons -u test"
388 // comment-column: 40