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 (asString());
53 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
56 prefix_ void senf::MIHFIdTLVParser::setString(std::string const &id)
58 size_type str_size (id.size());
59 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
61 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
62 safe_data_iterator si = resizeValueField( str_size);
63 std::copy( id.begin(), id.end(), si);
66 prefix_ void senf::MIHFIdTLVParser::setMACAddress(senf::MACAddress const &mac)
68 safe_data_iterator si = resizeValueField(12);
69 std::copy( mac.begin(), mac.end(), getNAIEncodedOutputIterator(si));
72 prefix_ void senf::MIHFIdTLVParser::setINet4Address(senf::INet4Address const &addr)
74 safe_data_iterator si = resizeValueField(8);
75 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
78 prefix_ void senf::MIHFIdTLVParser::setINet6Address(senf::INet6Address const &addr)
80 safe_data_iterator si = resizeValueField(32);
81 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
84 prefix_ void senf::MIHFIdTLVParser::setEUI64(senf::EUI64 const &addr)
86 safe_data_iterator si = resizeValueField(16);
87 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
90 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
93 if (length() == 0) return MIHFId();
97 case MIHFId::MACAddress:
98 return MIHFId( asMACAddress());
99 case MIHFId::INet4Address:
100 return MIHFId( asINet4Address());
101 case MIHFId::INet6Address:
102 return MIHFId( asINet6Address());
104 return MIHFId( asINet6Address());
106 return MIHFId( asINet6Address());
112 ///////////////////////////////////////////////////////////////////////////
113 // senf::MIHFSrcIdTLVParser
115 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
118 senf::format::IndentHelper indent;
119 os << indent << "source MIHF_Id TLV:\n";
120 MIHFIdTLVParser::dump(os);
123 ///////////////////////////////////////////////////////////////////////////
124 // senf::MIHFDstIdTLVParser
126 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
129 senf::format::IndentHelper indent;
130 os << indent << "destination MIHF_Id TLV:\n";
131 MIHFIdTLVParser::dump(os);
134 ///////////////////////////////////////////////////////////////////////////
135 // senf::MIHStatusTLVParser
137 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
140 senf::format::IndentHelper indent;
141 os << indent << "Status TLV:" << std::endl;
143 os << indent << "type: " << unsigned( type()) << std::endl
144 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
145 << indent << "value: " << unsigned( value());
148 os << " (Success)" << std::endl;
150 case UnspecifiedFailure:
151 os << " (Unspecified Failure)" << std::endl;
154 os << " (Rejected)" << std::endl;
156 case AuthorizationFailure:
157 os << " (Authorization Failure)" << std::endl;
160 os << " (Network Error)" << std::endl;
163 os << " (???; invalid value!)" << std::endl;
166 ///////////////////////////////////////////////////////////////////////////
167 // senf::MIHRegisterReqCodeTLVParser
169 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
172 senf::format::IndentHelper indent;
173 os << indent << "Register Request Code TLV:" << std::endl;
175 os << indent << "type: " << unsigned( type()) << std::endl
176 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
177 << indent << "value: " << unsigned( value());
180 os << " (Registration)" << std::endl;
183 os << " (Re-Registration)" << std::endl;
186 os << " (???; invalid value!)" << std::endl;
189 ///////////////////////////////////////////////////////////////////////////
190 // senf::MIHValidTimeIntervalTLVParser
192 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
195 senf::format::IndentHelper indent;
196 os << indent << "Valid Time Interval TLV:" << std::endl;
198 os << indent << "type: " << unsigned( type()) << std::endl
199 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
200 << indent << "value: " << unsigned( value())
201 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
205 ///////////////////////////////////////////////////////////////////////////
206 // senf::MIHBaseTLVParser
208 prefix_ senf::safe_data_iterator senf::MIHBaseTLVParser::resizeValueField(
209 MIHTLVLengthParser::value_type size)
211 MIHTLVLengthParser::value_type current_length ( length());
214 safe_data_iterator si (data(), boost::next(i(), 1 + length_().bytes() ));
215 if (current_length > size)
216 data().erase( si, boost::next(si, current_length-size));
218 data().insert( si, size-current_length, 0);
223 ///////////////////////////////////////////////////////////////////////////
224 // senf::MIHTLVLengthParser
226 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
230 return length_field().value();
232 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
234 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
236 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
238 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
240 throw( MIHTLVLengthException());
244 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
248 if (v > 128) throw( MIHTLVLengthException());
252 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
253 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
256 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
257 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
260 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
261 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
264 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
267 throw( MIHTLVLengthException());
269 underflow_flag() = (v <= 128);
272 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::maxValue()
279 return UInt8Parser::max_value + 128;
281 return UInt16Parser::max_value + 128;
283 return UInt24Parser::max_value + 128;
285 return UInt32Parser::max_value;
287 throw( MIHTLVLengthException());
291 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
297 prefix_ void senf::MIHTLVLengthParser::init() const
300 extended_length_flag() = false;
303 prefix_ void senf::MIHTLVLengthParser::finalize()
305 value_type v = value();
306 size_type b = bytes();
308 if (b != 1) resize_(1);
311 if (v <= UInt8Parser::max_value + 128) {
312 if (b != 2) resize_(2);
315 if (v <= UInt16Parser::max_value + 128) {
316 if (b != 3) resize_(3);
319 if (v <= UInt24Parser::max_value + 128 ) {
320 if (b != 4) resize_(4);
323 if (b != 5) resize_(5);
326 prefix_ void senf::MIHTLVLengthParser:: maxValue(MIHTLVLengthParser::value_type v)
330 size_type b = bytes();
331 if (v <= UInt8Parser::max_value + 128) {
332 if (b < 2) resize_(2);
335 if (v <= UInt16Parser::max_value + 128) {
336 if (b < 3) resize_(3);
339 if (v <= UInt24Parser::max_value + 128) {
340 if (b < 4) resize_(4);
343 if (b < 5) resize_(5);
346 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
348 value_type v = value();
349 resize(bytes(), size);
351 extended_length_flag() = true;
352 fixed_length_field() = size - 1;
354 extended_length_flag() = false;
360 ///////////////////////////////cc.e////////////////////////////////////////
367 // c-file-style: "senf"
368 // indent-tabs-mode: nil
369 // ispell-local-dictionary: "american"
370 // compile-command: "scons -u test"
371 // comment-column: 40