Packets/80221Bundle: extended MIHMessageType / MIHMessageRegistry to validate MIH...
[senf.git] / senf / Packets / 80221Bundle / TLVParser.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief TLVParser non-inline non-template implementation */
25
26 #include "TLVParser.hh"
27 //#include "TLVParser.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <senf/Utils/hexdump.hh>
32 #include <senf/Utils/Format.hh>
33 #include <senf/Utils/String.hh>
34
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFSrcIdTLVParser );
39 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHFDstIdTLVParser );
40 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHStatusTLVParser );
41 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHValidTimeIntervalTLVParser );
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::MIHBaseListTLVParser
45
46 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
47     const
48 {
49     protect(), listSize_().capacity( maxl);
50     maxLength( maxl + senf::bytes(listSize_()));
51 }
52
53 //-/////////////////////////////////////////////////////////////////////////////////////////////////
54 // senf::MIHFIdTLVParser
55
56 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
57     const
58 {
59     senf::format::IndentHelper indent;
60     os << indent << "type:   " << unsigned (type()) << std::endl
61        << indent << "length: " << unsigned (length()) << std::endl
62        << indent << "value:\n";
63     std::string src_mihfId (valueAsString());
64     hexdump(src_mihfId.begin(), src_mihfId.end(), os);
65 }
66
67 prefix_ void senf::MIHFIdTLVParser::finalize()
68 {
69     protect(), idLength_().finalize();
70     length_() << idLength() + senf::bytes(idLength_());
71     MIHBaseTLVParser::finalize();
72 }
73
74 prefix_ void senf::MIHFIdTLVParser::maxIdLength(boost::uint8_t maxl)
75     const
76 {
77     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
78     if (maxl > 253)
79         throw std::length_error("maximum length of a MIHF_ID is 253 octets");
80     protect(), idLength_().capacity( maxl);
81     maxLength( maxl + senf::bytes(idLength_()));
82 }
83
84 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
85         MIHTLVLengthParser::value_type size)
86 {
87     MIHTLVLengthParser::value_type current_length ( idLength());
88     idLength_() << size;
89     length_() << size + idLength_().bytes();
90
91     safe_data_iterator si (data(), valueBegin());
92     if (current_length > size)
93         data().erase( si, boost::next(si, current_length-size));
94     else
95         data().insert( si, size-current_length, 0);
96     return si;
97 }
98
99 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
100 {
101     size_type str_size (id.size());
102     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
103     if (str_size > 253)
104         throw std::length_error("maximum length of a MIHF_ID is 253 octets");
105     safe_data_iterator si = resizeValueField( str_size);
106     std::copy( id.begin(), id.end(), si);
107 }
108
109 prefix_ void senf::MIHFIdTLVParser::value(senf::MACAddress const & addr)
110 {
111     safe_data_iterator si = resizeValueField(6*2);
112     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
113 }
114
115 prefix_ void senf::MIHFIdTLVParser::value(senf::INet4Address const & addr)
116 {
117     safe_data_iterator si = resizeValueField(4*2);
118     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
119 }
120
121 prefix_ void senf::MIHFIdTLVParser::value(senf::INet6Address const & addr)
122 {
123     safe_data_iterator si = resizeValueField(16*2);
124     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
125 }
126
127 prefix_ void senf::MIHFIdTLVParser::value(senf::EUI64 const & addr)
128 {
129     safe_data_iterator si = resizeValueField(8*2);
130     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
131 }
132
133 prefix_ void senf::MIHFIdTLVParser::value( MIHFId const & id)
134 {
135     boost::apply_visitor( ValueSetterVisitor(*this), id);
136 }
137
138 prefix_ senf::MIHFId senf::MIHFIdTLVParser::valueAs(MIHFId::Type type)
139     const
140 {
141     if (length() == 0) return MIHFId();
142     switch (type) {
143     case MIHFId::MulticastType:
144         return MIHFId();
145     case MIHFId::MACAddress:
146         return MIHFId( valueAsMACAddress());
147     case MIHFId::INet4Address:
148         return MIHFId( valueAsINet4Address());
149     case MIHFId::INet6Address:
150         return MIHFId( valueAsINet6Address());
151     case MIHFId::String:
152         return MIHFId( valueAsString());
153     case MIHFId::EUI64:
154         return MIHFId( valueAsEUI64());
155     }
156     return MIHFId();
157 }
158
159
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161 // senf::MIHFSrcIdTLVParser
162
163 prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
164     const
165 {
166     senf::format::IndentHelper indent;
167     os << indent << "source MIHF_Id TLV:\n";
168     MIHFIdTLVParser::dump(os);
169 }
170
171 //-/////////////////////////////////////////////////////////////////////////////////////////////////
172 // senf::MIHFDstIdTLVParser
173
174 prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
175     const
176 {
177     senf::format::IndentHelper indent;
178     os << indent << "destination MIHF_Id TLV:\n";
179     MIHFIdTLVParser::dump(os);
180 }
181
182 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 // senf::MIHStatusTLVParser
184
185 prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
186     const
187 {
188     senf::format::IndentHelper indent;
189     os << indent << "Status TLV:" << std::endl;
190     indent.increase();
191     os << indent <<   "type:   " << unsigned( type()) << std::endl
192        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
193        << indent <<   "value:  " << unsigned( value());
194     switch (value()) {
195     case Success:
196         os << " (Success)" << std::endl;
197         return;
198     case UnspecifiedFailure:
199         os << " (Unspecified Failure)" << std::endl;
200         return;
201     case Rejected:
202         os << " (Rejected)" << std::endl;
203         return;
204     case AuthorizationFailure:
205         os << " (Authorization Failure)" << std::endl;
206         return;
207     case NetworkError:
208         os << " (Network Error)" << std::endl;
209         return;
210     }
211     os << " (???; invalid value!)" << std::endl;
212 }
213
214 //-/////////////////////////////////////////////////////////////////////////////////////////////////
215 // senf::MIHRegisterReqCodeTLVParser
216
217 prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
218     const
219 {
220     senf::format::IndentHelper indent;
221     os << indent << "Register Request Code TLV:" << std::endl;
222     indent.increase();
223     os << indent <<   "type:   " << unsigned( type()) << std::endl
224        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
225        << indent <<   "value:  " << unsigned( value());
226     switch (value()) {
227     case Registration:
228         os << " (Registration)" << std::endl;
229         return;
230     case ReRegistration:
231         os << " (Re-Registration)" << std::endl;
232         return;
233     }
234     os << " (???; invalid value!)" << std::endl;
235 }
236
237 prefix_ std::pair<bool, std::string> senf::MIHRegisterReqCodeTLVParser::validate()
238     const
239 {
240     if (length() != 1) return std::make_pair(false, "invalid length in MIHRegisterReqCodeTLV " + senf::str(length()));
241     if (value()  >= 2) return std::make_pair(false, "invalid value in MIHRegisterReqCodeTLV " + senf::str(value()));
242     return std::make_pair(true, "");
243 }
244
245 //-/////////////////////////////////////////////////////////////////////////////////////////////////
246 // senf::MIHValidTimeIntervalTLVParser
247
248 prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
249     const
250 {
251     senf::format::IndentHelper indent;
252     os << indent << "Valid Time Interval TLV:" << std::endl;
253     indent.increase();
254     os << indent <<   "type:   " << unsigned( type()) << std::endl
255        << indent <<   "length: " << unsigned( length()) << " byte(s)" << std::endl
256        << indent <<   "value:  " << unsigned( value())
257        << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
258 }
259
260 prefix_ std::pair<bool, std::string> senf::MIHValidTimeIntervalTLVParser::validate()
261     const
262 {
263     if (length() != 4) return std::make_pair(false, "invalid length in MIHValidTimeIntervalTLV " + senf::str(length()));
264     return std::make_pair(true, "");
265 }
266
267 //-/////////////////////////////////////////////////////////////////////////////////////////////////
268 // senf::MIHTLVLengthParser
269
270 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const
271 {
272     switch (bytes() ) {
273     case 1:
274         return length_field().value();
275     case 2:
276         return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
277     case 3:
278         return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
279     case 4:
280         return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
281     case 5:
282         return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
283     default:
284         throw( MIHTLVLengthException());
285     };
286 }
287
288 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v)
289 {
290     switch (bytes() ) {
291     case 1:
292         if (v > 128) throw( MIHTLVLengthException());
293         length_field() = v;
294         return;
295     case 2:
296         if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
297         parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
298         break;
299     case 3:
300         if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
301         parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
302         break;;
303     case 4:
304         if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
305         parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
306         break;
307     case 5:
308         parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
309         break;
310     default:
311         throw( MIHTLVLengthException());
312     };
313     underflow_flag() = (v <= 128);
314 }
315
316 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::capacity()
317     const
318 {
319     switch (bytes() ) {
320     case 1:
321         return 128;
322     case 2:
323         return UInt8Parser::max_value + 128;
324     case 3:
325         return UInt16Parser::max_value + 128;
326     case 4:
327         return UInt24Parser::max_value + 128;
328     case 5:
329         return UInt32Parser::max_value;
330     default:
331         throw( MIHTLVLengthException());
332     };
333 }
334
335 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other)
336 {
337     value(other);
338     return *this;
339 }
340
341 prefix_ void senf::MIHTLVLengthParser::init() const
342 {
343     defaultInit();
344     extended_length_flag() = false;
345 }
346
347 prefix_ void senf::MIHTLVLengthParser::finalize()
348 {
349     value_type v = value();
350     size_type b = bytes();
351     if (v <= 128) {
352         if (b != 1) resize_(1);
353         return;
354     }
355     if (v <= UInt8Parser::max_value + 128) {
356         if (b != 2) resize_(2);
357         return;
358     }
359     if (v <= UInt16Parser::max_value + 128) {
360         if (b != 3) resize_(3);
361         return;
362     }
363     if (v <= UInt24Parser::max_value + 128 ) {
364         if (b != 4) resize_(4);
365         return;
366     }
367     if (b != 5) resize_(5);
368 }
369
370 prefix_ void senf::MIHTLVLengthParser::capacity(MIHTLVLengthParser::value_type v)
371 {
372     if (v <= 128)
373         return;
374     size_type b = bytes();
375     if (v <= UInt8Parser::max_value + 128) {
376         if (b < 2) resize_(2);
377         return;
378     }
379     if (v <= UInt16Parser::max_value + 128) {
380         if (b < 3) resize_(3);
381         return;
382     }
383     if (v <= UInt24Parser::max_value + 128) {
384         if (b < 4) resize_(4);
385         return;
386     }
387     if (b < 5) resize_(5);
388 }
389
390 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
391 {
392     value_type v = value();
393     resize(bytes(), size);
394     if (size > 1) {
395         extended_length_flag() = true;
396         fixed_length_field() = size - 1;
397     } else {
398         extended_length_flag() = false;
399     }
400     value(v);
401 }
402
403
404 //-/////////////////////////////////////////////////////////////////////////////////////////////////
405 #undef prefix_
406
407 \f
408 // Local Variables:
409 // mode: c++
410 // fill-column: 100
411 // c-file-style: "senf"
412 // indent-tabs-mode: nil
413 // ispell-local-dictionary: "american"
414 // compile-command: "scons -u test"
415 // comment-column: 40
416 // End: