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>
34 ///////////////////////////////cc.p////////////////////////////////////////
36 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFSrcIdTLVParser );
37 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFDstIdTLVParser );
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::MIHFIdTLVParser
42 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
45 os << senf::fieldName(" type") << unsigned (type()) << "\n"
46 << senf::fieldName(" length") << unsigned (length()) << "\n"
48 std::string src_mihfId (asString());
49 hexdump(src_mihfId.begin(), src_mihfId.end(), os);
52 prefix_ void senf::MIHFIdTLVParser::setString(std::string const &id)
54 size_type str_size (id.size());
55 // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
57 throw std::length_error("maximum length of a MIHF_ID is 253 octets");
58 safe_data_iterator si = resizeValueField( str_size);
59 std::copy( id.begin(), id.end(), si);
62 prefix_ void senf::MIHFIdTLVParser::setMACAddress(senf::MACAddress const &mac)
64 safe_data_iterator si = resizeValueField(12);
65 std::copy( mac.begin(), mac.end(), getNAIEncodedOutputIterator(si));
68 prefix_ void senf::MIHFIdTLVParser::setINet4Address(senf::INet4Address const &addr)
70 safe_data_iterator si = resizeValueField(8);
71 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
74 prefix_ void senf::MIHFIdTLVParser::setINet6Address(senf::INet6Address const &addr)
76 safe_data_iterator si = resizeValueField(32);
77 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
80 prefix_ void senf::MIHFIdTLVParser::setEUI64(senf::EUI64 const &addr)
82 safe_data_iterator si = resizeValueField(16);
83 std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
86 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
89 if (length() == 0) return MIHFId();
93 case MIHFId::MACAddress:
94 return MIHFId( asMACAddress());
95 case MIHFId::INet4Address:
96 return MIHFId( asINet4Address());
97 case MIHFId::INet6Address:
98 return MIHFId( asINet6Address());
100 return MIHFId( asINet6Address());
102 return MIHFId( asINet6Address());
108 ///////////////////////////////////////////////////////////////////////////
109 // senf::MIHFSrcIdTLVParser
111 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
114 os << " source MIHF_Id TLV:\n";
115 MIHFIdTLVParser::dump(os);
118 ///////////////////////////////////////////////////////////////////////////
119 // senf::MIHFDstIdTLVParser
121 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
124 os << " destination MIHF_Id TLV:\n";
125 MIHFIdTLVParser::dump(os);
128 ///////////////////////////////////////////////////////////////////////////
131 prefix_ senf::safe_data_iterator senf::MIHBaseTLVParser::resizeValueField(
132 MIHTLVLengthParser::value_type size)
134 MIHTLVLengthParser::value_type current_length ( length());
137 safe_data_iterator si (data(), boost::next(i(), 1 + length_().bytes() ));
138 if (current_length > size)
139 data().erase( si, boost::next(si, current_length-size));
141 data().insert( si, size-current_length, 0);
146 ///////////////////////////////////////////////////////////////////////////
147 // senf::MIHTLVLengthParser
149 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
153 return length_field().value();
155 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
157 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
159 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
161 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
163 throw( MIHTLVLengthException());
167 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
171 if (v > 128) throw( MIHTLVLengthException());
175 if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
176 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
179 if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
180 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
183 if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
184 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
187 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
190 throw( MIHTLVLengthException());
192 underflow_flag() = (v <= 128);
195 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::maxValue()
202 return UInt8Parser::max_value + 128;
204 return UInt16Parser::max_value + 128;
206 return UInt24Parser::max_value + 128;
208 return UInt32Parser::max_value;
210 throw( MIHTLVLengthException());
214 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
220 prefix_ void senf::MIHTLVLengthParser::init() const
223 extended_length_flag() = false;
226 prefix_ void senf::MIHTLVLengthParser::finalize()
228 value_type v = value();
229 size_type b = bytes();
231 if (b != 1) resize_(1);
234 if (v <= UInt8Parser::max_value + 128) {
235 if (b != 2) resize_(2);
238 if (v <= UInt16Parser::max_value + 128) {
239 if (b != 3) resize_(3);
242 if (v <= UInt24Parser::max_value + 128 ) {
243 if (b != 4) resize_(4);
246 if (b != 5) resize_(5);
249 prefix_ void senf::MIHTLVLengthParser:: maxValue(MIHTLVLengthParser::value_type v)
253 size_type b = bytes();
254 if (v <= UInt8Parser::max_value + 128) {
255 if (b < 2) resize_(2);
258 if (v <= UInt16Parser::max_value + 128) {
259 if (b < 3) resize_(3);
262 if (v <= UInt24Parser::max_value + 128) {
263 if (b < 4) resize_(4);
266 if (b < 5) resize_(5);
269 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
271 value_type v = value();
272 resize(bytes(), size);
274 extended_length_flag() = true;
275 fixed_length_field() = size - 1;
277 extended_length_flag() = false;
283 ///////////////////////////////cc.e////////////////////////////////////////
290 // c-file-style: "senf"
291 // indent-tabs-mode: nil
292 // ispell-local-dictionary: "american"
293 // compile-command: "scons -u test"
294 // comment-column: 40