TLVParser.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "TLVParser.hh"
18 //#include "TLVParser.ih"
19 
20 // Custom includes
21 #include <senf/Utils/Format.hh>
22 #include "Exceptions.hh"
23 
24 #define prefix_
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
31 
32 //-/////////////////////////////////////////////////////////////////////////////////////////////////
33 // senf::MIHFIdTLVParser
34 
35 prefix_ void senf::MIHFIdTLVParser::dump(std::ostream & os)
36  const
37 {
39  os << indent << "type: " << unsigned (type()) << std::endl
40  << indent << "length: " << unsigned (length()) << std::endl
41  << indent << "value:\n";
42  std::string src_mihfId (valueAsString());
43  hexdump(src_mihfId.begin(), src_mihfId.end(), os);
44 }
45 
47 {
48  protect(), idLength_().finalize();
51 }
52 
54  const
55 {
56  // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
57  if (maxl > 253)
58  throw std::length_error("maximum length of a MIHF_ID is 253 octets");
59  protect(), idLength_().capacity( maxl);
60  maxLength( maxl + senf::bytes(idLength_()));
61 }
62 
63 prefix_ senf::safe_data_iterator senf::MIHFIdTLVParser::resizeValueField(
65 {
66  MIHLengthParser::value_type current_length ( idLength());
67  idLength_() << size;
68  length_() << size + idLength_().bytes();
69 
70  safe_data_iterator si (data(), valueBegin());
71  if (current_length > size)
72  data().erase( si, boost::next(si, current_length-size));
73  else
74  data().insert( si, size-current_length, 0);
75  return si;
76 }
77 
78 prefix_ void senf::MIHFIdTLVParser::value(std::string const & id)
79 {
80  size_type str_size (id.size());
81  // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
82  if (str_size > 253)
83  throw std::length_error("maximum length of a MIHF_ID is 253 octets");
84  safe_data_iterator si = resizeValueField( str_size);
85  std::copy( id.begin(), id.end(), si);
86 }
87 
89 {
90  safe_data_iterator si = resizeValueField(6*2);
91  std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
92 }
93 
95 {
96  safe_data_iterator si = resizeValueField(4*2);
97  std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
98 }
99 
101 {
102  safe_data_iterator si = resizeValueField(16*2);
103  std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
104 }
105 
107 {
108  safe_data_iterator si = resizeValueField(8*2);
109  std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
110 }
111 
113 {
114  boost::apply_visitor( ValueSetterVisitor(*this), id);
115 }
116 
118  const
119 {
120  if (length() == 0) return MIHFId();
121  switch (type) {
123  return MIHFId();
124  case MIHFId::MACAddress:
125  return MIHFId( valueAsMACAddress());
127  return MIHFId( valueAsINet4Address());
129  return MIHFId( valueAsINet6Address());
130  case MIHFId::String:
131  return MIHFId( valueAsString());
132  case MIHFId::EUI64:
133  return MIHFId( valueAsEUI64());
134  }
135  return MIHFId();
136 }
137 
139 {
140  std::string d (dst.valueAsString());
142  dst << src.valueAsString();
143  *safe_src << d;
144 }
145 
146 
147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
148 // senf::MIHFSrcIdTLVParser
149 
151  const
152 {
154  os << indent << "source MIHF_Id TLV:\n";
156 }
157 
159  const
160 {
161  validateType( typeId);
162 }
163 
164 
165 //-/////////////////////////////////////////////////////////////////////////////////////////////////
166 // senf::MIHFDstIdTLVParser
167 
169  const
170 {
172  os << indent << "destination MIHF_Id TLV:\n";
174 }
175 
177  const
178 {
179  validateType( typeId);
180 }
181 
182 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 // senf::MIHStatusTLVParser
184 
186  const
187 {
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 
215  const
216 {
217  validateTypeLength( typeId, 1);
218  if (value() >= 4)
219  throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << unsigned( value());
220 }
221 
222 //-/////////////////////////////////////////////////////////////////////////////////////////////////
223 // senf::MIHRegisterReqCodeTLVParser
224 
226  const
227 {
229  os << indent << "Register Request Code TLV:" << std::endl;
230  indent.increase();
231  os << indent << "type: " << unsigned( type()) << std::endl
232  << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
233  << indent << "value: " << unsigned( value());
234  switch (value()) {
235  case Registration:
236  os << " (Registration)" << std::endl;
237  return;
238  case ReRegistration:
239  os << " (Re-Registration)" << std::endl;
240  return;
241  }
242  os << " (???; invalid value!)" << std::endl;
243 }
244 
246  const
247 {
248  validateTypeLength( typeId, 1);
249  if (value() >= 2)
250  throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << unsigned( value());
251 }
252 
253 //-/////////////////////////////////////////////////////////////////////////////////////////////////
254 // senf::MIHValidTimeIntervalTLVParser
255 
257  const
258 {
260  os << indent << "Valid Time Interval TLV:" << std::endl;
261  indent.increase();
262  os << indent << "type: " << unsigned( type()) << std::endl
263  << indent << "length: " << unsigned( length()) << " byte(s)" << std::endl
264  << indent << "value: " << unsigned( value())
265  << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
266 }
267 
269  const
270 {
271  validateTypeLength( typeId, 4);
272 }
273 
274 //-/////////////////////////////////////////////////////////////////////////////////////////////////
275 #undef prefix_
276 
277 
278 // Local Variables:
279 // mode: c++
280 // fill-column: 100
281 // c-file-style: "senf"
282 // indent-tabs-mode: nil
283 // ispell-local-dictionary: "american"
284 // compile-command: "scons -u test"
285 // comment-column: 40
286 // End:
void hexdump(Iterator i, Iterator i_end, std::ostream &stream, unsigned block_size=16)
void dump(std::ostream &os) const
Definition: TLVParser.cc:150
std::string valueAsString() const
void copy(ListParser< ListPolicy > const &listParser, OutputIterator result)
Definition: algorithms.hh:58
void dump(std::ostream &os) const
dump string representation to given stream
Definition: TLVParser.cc:185
void dump(std::ostream &os) const
dump string representation to given stream
Definition: TLVParser.cc:225
size_type bytes() const
void maxLength(MIHLengthParser::value_type maxl) const
set maximum value of TLV length field
senf::EUI64 valueAsEUI64() const
void validateTypeLength(boost::uint8_t type, MIHLengthParser::value_type length) const
void maxIdLength(boost::uint8_t maxl) const
Definition: TLVParser.cc:53
Parser for 802.21 source MIHF_ID TLV.
Definition: TLVParser.hh:160
void dump(std::ostream &os) const
dump string representation to given stream
Definition: TLVParser.cc:256
PacketParserBase::size_type bytes(Parser const &p)
Return raw size parsed by the given parser object.
Parser for 802.21 Status TLV.
Definition: TLVParser.hh:192
void finalize()
shrink size of the TLV length field to minimum
senf::INet6Address valueAsINet6Address() const
void capacity(value_type v)
UInt8Parser type() const
SENF_PACKET_TLV_REGISTRY_REGISTER(senf::MIHFSrcIdTLVParser)
boost::uint32_t value_type
MIHLengthParser idLength_() const
MIHFId valueAs(MIHFId::Type type) const
Definition: TLVParser.cc:117
#define prefix_
Definition: TLVParser.cc:24
MIHLengthParser ::value_type length() const
void swap(MIHFSrcIdTLVParser src, MIHFDstIdTLVParser dst)
Definition: TLVParser.cc:138
Re-validating data iterator.
Definition: SafeIterator.hh:47
detail::packet::size_type size_type
Unsigned integral type.
void dump(std::ostream &os) const
Definition: TLVParser.cc:168
MIHLengthParser ::value_type idLength() const
TLVParser public header.
senf::MACAddress valueAsMACAddress() const
senf::INet4Address valueAsINet4Address() const
PacketData & data() const
Access the packets raw data container.
void erase(iterator pos)
void insert(iterator pos, byte v)
Iterator re-validating Parser wrapper.
Parser for 802.21 destination MIHF_ID TLV.
Definition: TLVParser.hh:175
ParserProtector protect() const
void dump(std::ostream &os) const
Definition: TLVParser.cc:35
void value(MIHFId const &id)
Definition: TLVParser.cc:112
void validateType(boost::uint8_t type) const
MIHLengthParser length_() const