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::value(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::value(senf::MACAddress const & addr)
68 safe_data_iterator si = resizeValueField(12);
69 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
72 prefix_ void senf::MIHFIdTLVParser::value(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::value(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::value(senf::EUI64 const & addr)
86 safe_data_iterator si = resizeValueField(16);
87 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
90 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
92 boost::apply_visitor( ValueSetterVisitor(*this), id);
95 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
98 if (length() == 0) return MIHFId();
102 case MIHFId::MACAddress:
103 return MIHFId( valueAsMACAddress());
104 case MIHFId::INet4Address:
105 return MIHFId( valueAsINet4Address());
106 case MIHFId::INet6Address:
107 return MIHFId( valueAsINet6Address());
109 return MIHFId( valueAsString());
111 return MIHFId( valueAsEUI64());
117 ///////////////////////////////////////////////////////////////////////////
118 // senf::MIHFSrcIdTLVParser
120 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
123 senf::format::IndentHelper indent;
124 os << indent << "source MIHF_Id TLV:\n";
125 MIHFIdTLVParser::dump(os);
128 ///////////////////////////////////////////////////////////////////////////
129 // senf::MIHFDstIdTLVParser
131 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
134 senf::format::IndentHelper indent;
135 os << indent << "destination MIHF_Id TLV:\n";
136 MIHFIdTLVParser::dump(os);
139 ///////////////////////////////////////////////////////////////////////////
140 // senf::MIHStatusTLVParser
142 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
145 senf::format::IndentHelper indent;
146 os << indent << "Status TLV:" << std::endl;
148 os << indent << "type: " << unsigned( type()) << std::endl
149 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
150 << indent << "value: " << unsigned( value());
153 os << " (Success)" << std::endl;
155 case UnspecifiedFailure:
156 os << " (Unspecified Failure)" << std::endl;
159 os << " (Rejected)" << std::endl;
161 case AuthorizationFailure:
162 os << " (Authorization Failure)" << std::endl;
165 os << " (Network Error)" << std::endl;
168 os << " (???; invalid value!)" << std::endl;
171 ///////////////////////////////////////////////////////////////////////////
172 // senf::MIHRegisterReqCodeTLVParser
174 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
177 senf::format::IndentHelper indent;
178 os << indent << "Register Request Code TLV:" << std::endl;
180 os << indent << "type: " << unsigned( type()) << std::endl
181 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
182 << indent << "value: " << unsigned( value());
185 os << " (Registration)" << std::endl;
188 os << " (Re-Registration)" << std::endl;
191 os << " (???; invalid value!)" << std::endl;
194 ///////////////////////////////////////////////////////////////////////////
195 // senf::MIHValidTimeIntervalTLVParser
197 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
200 senf::format::IndentHelper indent;
201 os << indent << "Valid Time Interval TLV:" << std::endl;
203 os << indent << "type: " << unsigned( type()) << std::endl
204 << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
205 << indent << "value: " << unsigned( value())
206 << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
210 ///////////////////////////////////////////////////////////////////////////
211 // senf::MIHBaseTLVParser
213 prefix_ senf::safe_data_iterator senf::MIHBaseTLVParser::resizeValueField(
214 MIHTLVLengthParser::value_type size)
216 MIHTLVLengthParser::value_type current_length ( length());
219 safe_data_iterator si (data(), boost::next(i(), 1 + length_().bytes() ));
220 if (current_length > size)
221 data().erase( si, boost::next(si, current_length-size));
223 data().insert( si, size-current_length, 0);
228 ///////////////////////////////////////////////////////////////////////////
229 // senf::MIHTLVLengthParser
231 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
235 return length_field().value();
237 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
239 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
241 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
243 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
245 throw( MIHTLVLengthException());
249 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
253 if (v > 128) throw( MIHTLVLengthException());
257 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
258 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
261 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
262 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
265 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
266 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
269 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
272 throw( MIHTLVLengthException());
274 underflow_flag() = (v <= 128);
277 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::maxValue()
284 return UInt8Parser::max_value + 128;
286 return UInt16Parser::max_value + 128;
288 return UInt24Parser::max_value + 128;
290 return UInt32Parser::max_value;
292 throw( MIHTLVLengthException());
296 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
302 prefix_ void senf::MIHTLVLengthParser::init() const
305 extended_length_flag() = false;
308 prefix_ void senf::MIHTLVLengthParser::finalize()
310 value_type v = value();
311 size_type b = bytes();
313 if (b != 1) resize_(1);
316 if (v <= UInt8Parser::max_value + 128) {
317 if (b != 2) resize_(2);
320 if (v <= UInt16Parser::max_value + 128) {
321 if (b != 3) resize_(3);
324 if (v <= UInt24Parser::max_value + 128 ) {
325 if (b != 4) resize_(4);
328 if (b != 5) resize_(5);
331 prefix_ void senf::MIHTLVLengthParser:: maxValue(MIHTLVLengthParser::value_type v)
335 size_type b = bytes();
336 if (v <= UInt8Parser::max_value + 128) {
337 if (b < 2) resize_(2);
340 if (v <= UInt16Parser::max_value + 128) {
341 if (b < 3) resize_(3);
344 if (v <= UInt24Parser::max_value + 128) {
345 if (b < 4) resize_(4);
348 if (b < 5) resize_(5);
351 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
353 value_type v = value();
354 resize(bytes(), size);
356 extended_length_flag() = true;
357 fixed_length_field() = size - 1;
359 extended_length_flag() = false;
365 ///////////////////////////////cc.e////////////////////////////////////////
372 // c-file-style: "senf"
373 // indent-tabs-mode: nil
374 // ispell-local-dictionary: "american"
375 // compile-command: "scons -u test"
376 // comment-column: 40