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::MIHBaseListTLVParser
45 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
48 protect(), listSize_().capacity( maxl);
49 maxLength( maxl + senf::bytes(listSize_()));
52 ///////////////////////////////////////////////////////////////////////////
53 // senf::MIHFIdTLVParser
55 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
58 senf::format::IndentHelper indent;
59 os << indent << "type: " << unsigned (type()) << std::endl
60 << indent << "length: " << unsigned (length()) << std::endl
61 << indent << "value:\n";
62 std::string src_mihfId (valueAsString());
63 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
66 prefix_ void senf::MIHFIdTLVParser::finalize()
68 protect(), idLength_().finalize();
69 length_() << idLength() + senf::bytes(idLength_());
70 MIHBaseTLVParser::finalize();
73 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
76 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
78 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
79 protect(), idLength_().capacity( maxl);
80 maxLength( maxl + senf::bytes(idLength_()));
83 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
84 MIHTLVLengthParser::value_type size)
86 MIHTLVLengthParser::value_type current_length ( idLength());
88 length_() << size + idLength_().bytes();
90 safe_data_iterator si (data(), valueBegin());
91 if (current_length > size)
92 data().erase( si, boost::next(si, current_length-size));
94 data().insert( si, size-current_length, 0);
98 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
100 size_type str_size (id.size());
101 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
103 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
104 safe_data_iterator si = resizeValueField( str_size);
105 std::copy( id.begin(), id.end(), si);
108 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
110 safe_data_iterator si = resizeValueField(6*2);
111 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
114 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
116 safe_data_iterator si = resizeValueField(4*2);
117 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
120 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
122 safe_data_iterator si = resizeValueField(16*2);
123 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
126 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
128 safe_data_iterator si = resizeValueField(8*2);
129 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
132 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
134 boost::apply_visitor( ValueSetterVisitor(*this), id);
137 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
140 if (length() == 0) return MIHFId();
144 case MIHFId::MACAddress:
145 return MIHFId( valueAsMACAddress());
146 case MIHFId::INet4Address:
147 return MIHFId( valueAsINet4Address());
148 case MIHFId::INet6Address:
149 return MIHFId( valueAsINet6Address());
151 return MIHFId( valueAsString());
153 return MIHFId( valueAsEUI64());
159 ///////////////////////////////////////////////////////////////////////////
160 // senf::MIHFSrcIdTLVParser
162 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
165 senf::format::IndentHelper indent;
166 os << indent << "source MIHF_Id TLV:\n";
167 MIHFIdTLVParser::dump(os);
170 ///////////////////////////////////////////////////////////////////////////
171 // senf::MIHFDstIdTLVParser
173 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
176 senf::format::IndentHelper indent;
177 os << indent << "destination MIHF_Id TLV:\n";
178 MIHFIdTLVParser::dump(os);
181 ///////////////////////////////////////////////////////////////////////////
182 // senf::MIHStatusTLVParser
184 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
187 senf::format::IndentHelper indent;
188 os << indent << "Status TLV:" << std::endl;
190 os << indent << "type: " << unsigned( type()) << std::endl
191 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
192 << indent << "value: " << unsigned( value());
195 os << " (Success)" << std::endl;
197 case UnspecifiedFailure:
198 os << " (Unspecified Failure)" << std::endl;
201 os << " (Rejected)" << std::endl;
203 case AuthorizationFailure:
204 os << " (Authorization Failure)" << std::endl;
207 os << " (Network Error)" << std::endl;
210 os << " (???; invalid value!)" << std::endl;
213 ///////////////////////////////////////////////////////////////////////////
214 // senf::MIHRegisterReqCodeTLVParser
216 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
219 senf::format::IndentHelper indent;
220 os << indent << "Register Request Code 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 << " (Registration)" << std::endl;
230 os << " (Re-Registration)" << std::endl;
233 os << " (???; invalid value!)" << std::endl;
236 ///////////////////////////////////////////////////////////////////////////
237 // senf::MIHValidTimeIntervalTLVParser
239 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
242 senf::format::IndentHelper indent;
243 os << indent << "Valid Time Interval TLV:" << std::endl;
245 os << indent << "type: " << unsigned( type()) << std::endl
246 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
247 << indent << "value: " << unsigned( value())
248 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
251 ///////////////////////////////////////////////////////////////////////////
252 // senf::MIHTLVLengthParser
254 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
258 return length_field().value();
260 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
262 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
264 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
266 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
268 throw( MIHTLVLengthException());
272 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
276 if (v > 128) throw( MIHTLVLengthException());
280 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
281 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
284 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
285 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
288 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
289 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
292 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
295 throw( MIHTLVLengthException());
297 underflow_flag() = (v <= 128);
300 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
307 return UInt8Parser::max_value + 128;
309 return UInt16Parser::max_value + 128;
311 return UInt24Parser::max_value + 128;
313 return UInt32Parser::max_value;
315 throw( MIHTLVLengthException());
319 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
325 prefix_ void senf::MIHTLVLengthParser::init() const
328 extended_length_flag() = false;
331 prefix_ void senf::MIHTLVLengthParser::finalize()
333 value_type v = value();
334 size_type b = bytes();
336 if (b != 1) resize_(1);
339 if (v <= UInt8Parser::max_value + 128) {
340 if (b != 2) resize_(2);
343 if (v <= UInt16Parser::max_value + 128) {
344 if (b != 3) resize_(3);
347 if (v <= UInt24Parser::max_value + 128 ) {
348 if (b != 4) resize_(4);
351 if (b != 5) resize_(5);
354 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
358 size_type b = bytes();
359 if (v <= UInt8Parser::max_value + 128) {
360 if (b < 2) resize_(2);
363 if (v <= UInt16Parser::max_value + 128) {
364 if (b < 3) resize_(3);
367 if (v <= UInt24Parser::max_value + 128) {
368 if (b < 4) resize_(4);
371 if (b < 5) resize_(5);
374 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
376 value_type v = value();
377 resize(bytes(), size);
379 extended_length_flag() = true;
380 fixed_length_field() = size - 1;
382 extended_length_flag() = false;
388 ///////////////////////////////cc.e////////////////////////////////////////
395 // c-file-style: "senf"
396 // indent-tabs-mode: nil
397 // ispell-local-dictionary: "american"
398 // compile-command: "scons -u test"
399 // comment-column: 40