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
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
15 \brief PacketInterpreter non-inline template implementation */
17 //#include "PacketInterpreter.ih"
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::PacketInterpreterBase
28 // Interpreter chain access
31 prefix_ typename senf::PacketInterpreter<Type>::ptr
32 senf::PacketInterpreterBase::parseNextAs()
34 optional_range r (nextPacketRange());
36 throw InvalidPacketChainException();
39 impl().truncateInterpreters(nextP());
41 typename PacketInterpreter<Type>::ptr pi
42 (PacketInterpreter<Type>::create(&impl(),r->begin(),r->end(),Append));
47 prefix_ typename senf::PacketInterpreter<Type>::ptr
48 senf::PacketInterpreterBase::replaceAs(difference_type offset, difference_type tailOffset)
50 { static typename PacketInterpreter<Type>::factory_t _ (PacketInterpreter<Type>::factory()); (void) _;}
51 iterator b (data().begin() + offset);
52 size_type sz (data().size() - offset + tailOffset);
53 detail::PacketImpl::Guard p (&impl());
54 p.p->clearInterpreters();
55 b = p.p->truncateRawDataBackwards(b);
56 return senf::PacketInterpreter<Type>::create(p.p, b, b + sz, Append);
59 //-/////////////////////////////////////////////////////////////////////////////////////////////////
60 // senf::PacketInterpreter<PacketType>
62 // Create completely new packet
64 template <class PacketType>
65 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
66 senf::PacketInterpreter<PacketType>::create(size_type size)
68 if (size < initSize())
69 throw TruncatedPacketException();
70 ptr pi (create(size,senf::noinit));
75 template <class PacketType>
76 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
77 senf::PacketInterpreter<PacketType>::create(size_type size, senf::NoInit_t)
79 detail::PacketImpl::Guard p (new detail::PacketImpl(size,0));
80 return create(p.p,p.p->begin(),p.p->end(),Append);
83 template <class PacketType>
84 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
85 senf::PacketInterpreter<PacketType>::create(byte * data, size_type size, size_type chunkSize,
88 detail::PacketImpl::Guard p (new detail::PacketImpl(data, size, chunkSize, offset));
89 return create(p.p,p.p->begin(),p.p->end(),Append);
92 // Create packet as new packet after a given packet
94 template <class PacketType>
95 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
96 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr const & packet,
99 if (size < initSize())
100 throw TruncatedPacketException();
101 ptr pi (createAfter(packet,size,senf::noinit));
102 std::fill(pi->data().begin(), pi->data().end(),0);
107 template <class PacketType>
108 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
109 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr const & packet,
110 size_type size, senf::NoInit_t)
112 optional_range r (packet->nextPacketRange());
114 throw InvalidPacketChainException();
117 packet->impl().truncateInterpreters(packet->nextP());
119 ptr pi (create(&packet->impl(),r->begin(),r->end(),Append));
120 pi->data().resize(size);
124 template <class PacketType>
125 template <class ForwardReadableRange>
126 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
127 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr const & packet,
128 ForwardReadableRange const & range)
130 optional_range r (packet->nextPacketRange());
132 throw InvalidPacketChainException();
135 packet->impl().truncateInterpreters(packet->nextP());
137 ptr pi (create(&packet->impl(),r->begin(),r->end(),Append));
138 pi->data().resize(boost::size(range));
139 std::copy(boost::begin(range), boost::end(range), pi->data().begin());
143 // Create packet as new packet (header) before a given packet
145 template <class PacketType>
146 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
147 senf::PacketInterpreter<PacketType>::createBeforeNoZero(PacketInterpreterBase::ptr const & packet)
149 ptr pi (createBefore(packet, senf::noinit, initHeadSize(), initSize() - initHeadSize()));
154 template <class PacketType>
155 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
156 senf::PacketInterpreter<PacketType>::createBefore(PacketInterpreterBase::ptr const & packet)
158 ptr pi (createBefore(packet, senf::noinit, initHeadSize(), initSize() - initHeadSize()));
159 pi->data().zero(pi->data().begin(), packet->data().begin());
160 pi->data().zero(packet->data().end(), pi->data().end());
165 template <class PacketType>
166 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
167 senf::PacketInterpreter<PacketType>::createBefore(PacketInterpreterBase::ptr const & packet,
168 senf::NoInit_t, size_type space,
172 packet->impl().truncateInterpretersBackwards(packet->prevP());
173 size_type room (std::distance(packet->impl().begin(), packet->data().begin()));
174 ptr pi (create(&packet->impl(), packet->impl().begin(), packet->data().end(), Prepend));
176 packet->impl().truncateDataBackwards(& pi->data(), packet->data().begin() - space);
177 else if (room < space)
178 pi->data().insert(pi->data().begin(), space - room, byte(0x00u));
179 // For the tail-space it's much simpler: since we have pruned the interpreter chain with the
180 // current packet, pi must be the most-outside packet interpreter. This means, there can be no
181 // packet which has data after pi. We can thus change the underlying data container without
182 // updating any other packet interpreter (that is the data offsets therein).
183 packet->impl().truncateRawDataForward(pi->data().end());
184 pi->data().insert(pi->data().end(), tailSpace, byte(0x00u));
188 template <class PacketType>
189 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
190 senf::PacketInterpreter<PacketType>::createInsertBefore(PacketInterpreterBase::ptr const & packet)
192 ptr pi (createInsertBefore(packet, senf::noinit));
193 pi->data().insert(pi->data().begin(),initHeadSize(),byte(0x00u));
194 pi->data().insert(pi->data().end(),initSize()-initHeadSize(),byte(0x00u));
199 template <class PacketType>
200 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
201 senf::PacketInterpreter<PacketType>::createInsertBefore(PacketInterpreterBase::ptr const & packet,
204 return create(&packet->impl(),packet->data().begin(),packet->data().end(),packet);
207 //-/////////////////////////////////////////////////////////////////////////////////////////////////
212 template <class PacketType>
213 prefix_ senf::PacketInterpreterBase::optional_range
214 senf::PacketInterpreter<PacketType>::v_nextPacketRange()
216 return type::nextPacketRange(ConcretePacket<PacketType>(ptr(this)));
219 template <class PacketType>
220 prefix_ senf::PacketInterpreterBase::ptr
221 senf::PacketInterpreter<PacketType>::v_appendClone(detail::PacketImpl * impl, iterator base,
225 boost::next(new_base,std::distance(base,begin())),
226 boost::next(new_base,std::distance(base,end())),
230 template <class PacketType>
231 prefix_ senf::PacketInterpreterBase::ptr
232 senf::PacketInterpreter<PacketType>::v_appendClone(detail::PacketImpl * impl, range r)
234 return create(impl, r.begin(), r.end(), Append);
237 template <class PacketType>
238 prefix_ void senf::PacketInterpreter<PacketType>::v_finalize()
240 type::finalize(ConcretePacket<PacketType>(ptr(this)));
243 template <class PacketType>
244 prefix_ void senf::PacketInterpreter<PacketType>::v_dump(std::ostream & os)
246 type::dump(ConcretePacket<PacketType>(ptr(this)),os);
249 template <class PacketType>
250 prefix_ senf::TypeIdValue senf::PacketInterpreter<PacketType>::v_type()
252 return typeIdValue< ConcretePacket<PacketType> >();
255 template <class PacketType>
256 prefix_ senf::PacketInterpreterBase::factory_t
257 senf::PacketInterpreter<PacketType>::v_factory()
262 template <class PacketType>
263 prefix_ senf::PacketInterpreterBase::factory_t
264 senf::PacketInterpreter<PacketType>::v_nextPacketType()
266 return type::nextPacketType(ConcretePacket<PacketType>(ptr(this)));
270 //-/////////////////////////////////////////////////////////////////////////////////////////////////
271 // senf::PacketInterpreterBase::Factory
273 template <class ForwardReadableRange>
274 prefix_ senf::PacketInterpreterBase::ptr
275 senf::PacketInterpreterBase::Factory::create(ForwardReadableRange const & range)
278 ptr pi (create(boost::size(range),senf::noinit));
279 std::copy(boost::begin(range), boost::end(range), pi->data().begin());
283 template <class ForwardReadableRange>
284 prefix_ senf::PacketInterpreterBase::ptr
285 senf::PacketInterpreterBase::Factory::createAfter(PacketInterpreterBase::ptr const & packet,
286 ForwardReadableRange const & range)
289 ptr pi (createAfter(packet,boost::size(range),senf::noinit));
290 std::copy(boost::begin(range), boost::end(range), pi->data().begin());
294 //-/////////////////////////////////////////////////////////////////////////////////////////////////
295 // senf::PacketInterpreter<PacketType>::FactoryImpl
297 // Create completely new packet
299 template <class PacketType>
300 prefix_ typename senf::PacketInterpreterBase::ptr
301 senf::PacketInterpreter<PacketType>::FactoryImpl::create()
304 return senf::PacketInterpreter<PacketType>::create();
307 template <class PacketType>
308 prefix_ typename senf::PacketInterpreterBase::ptr
309 senf::PacketInterpreter<PacketType>::FactoryImpl::create(senf::NoInit_t)
312 return senf::PacketInterpreter<PacketType>::create(senf::noinit);
315 template <class PacketType>
316 prefix_ typename senf::PacketInterpreterBase::ptr
317 senf::PacketInterpreter<PacketType>::FactoryImpl::create(size_type size)
320 return senf::PacketInterpreter<PacketType>::create(size);
323 template <class PacketType>
324 prefix_ typename senf::PacketInterpreterBase::ptr
325 senf::PacketInterpreter<PacketType>::FactoryImpl::create(size_type size, senf::NoInit_t)
328 return senf::PacketInterpreter<PacketType>::create(size, senf::noinit);
331 template <class PacketType>
332 prefix_ typename senf::PacketInterpreterBase::ptr
333 senf::PacketInterpreter<PacketType>::FactoryImpl::create(byte * data, size_type size,
334 size_type chunkSize, size_type offset)
336 return senf::PacketInterpreter<PacketType>::create(data, size, chunkSize, offset);
339 // Create packet as new packet after a given packet
341 template <class PacketType>
342 prefix_ typename senf::PacketInterpreterBase::ptr
343 senf::PacketInterpreter<PacketType>::FactoryImpl::createAfter(PacketInterpreterBase::ptr const & packet)
346 return senf::PacketInterpreter<PacketType>::createAfter(packet);
349 template <class PacketType>
350 prefix_ typename senf::PacketInterpreterBase::ptr
351 senf::PacketInterpreter<PacketType>::FactoryImpl::createAfter(PacketInterpreterBase::ptr const & packet,
355 return senf::PacketInterpreter<PacketType>::createAfter(packet,senf::noinit);
358 template <class PacketType>
359 prefix_ typename senf::PacketInterpreterBase::ptr
360 senf::PacketInterpreter<PacketType>::FactoryImpl::createAfter(PacketInterpreterBase::ptr const & packet,
364 return senf::PacketInterpreter<PacketType>::createAfter(packet,size);
367 template <class PacketType>
368 prefix_ typename senf::PacketInterpreterBase::ptr
369 senf::PacketInterpreter<PacketType>::FactoryImpl::createAfter(PacketInterpreterBase::ptr const & packet,
370 size_type size, senf::NoInit_t)
373 return senf::PacketInterpreter<PacketType>::createAfter(packet,size,senf::noinit);
376 // Create packet as new packet (header) before a given packet
378 template <class PacketType>
379 prefix_ typename senf::PacketInterpreterBase::ptr
380 senf::PacketInterpreter<PacketType>::FactoryImpl::
381 createBefore(PacketInterpreterBase::ptr const & packet)
384 return senf::PacketInterpreter<PacketType>::createBefore(packet);
387 template <class PacketType>
388 prefix_ senf::PacketInterpreterBase::ptr
389 senf::PacketInterpreter<PacketType>::FactoryImpl::
390 createBefore(PacketInterpreterBase::ptr const & packet, senf::NoInit_t)
393 return senf::PacketInterpreter<PacketType>::createBefore(packet,senf::noinit);
396 template <class PacketType>
397 prefix_ senf::PacketInterpreterBase::ptr
398 senf::PacketInterpreter<PacketType>::FactoryImpl::
399 createInsertBefore(PacketInterpreterBase::ptr const & packet)
402 return senf::PacketInterpreter<PacketType>::createInsertBefore(packet);
405 template <class PacketType>
406 prefix_ senf::PacketInterpreterBase::ptr
407 senf::PacketInterpreter<PacketType>::FactoryImpl::
408 createInsertBefore(PacketInterpreterBase::ptr const & packet, senf::NoInit_t)
411 return senf::PacketInterpreter<PacketType>::createInsertBefore(packet,senf::noinit);
414 // Parse next packet in chain
416 template <class PacketType>
417 prefix_ typename senf::PacketInterpreterBase::ptr
418 senf::PacketInterpreter<PacketType>::FactoryImpl::parseNext(PacketInterpreterBase::ptr const & packet, PacketInterpreterBase::optional_range const & range)
422 throw InvalidPacketChainException();
425 packet->impl().truncateInterpreters(packet->nextP());
427 return senf::PacketInterpreter<PacketType>::create(&packet->impl(),range->begin(),range->end(),Append);
430 //-/////////////////////////////////////////////////////////////////////////////////////////////////
437 // c-file-style: "senf"
438 // indent-tabs-mode: nil
439 // ispell-local-dictionary: "american"
440 // compile-command: "scons -u test"
441 // comment-column: 40