PacketParser.cci
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 
14 /** \file
15  \brief PacketParser inline non-template implementation */
16 
17 // Custom includes
18 #include <iterator>
19 #include "PacketData.hh"
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::PacketParserBase
26 
27 // public members
28 
29 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i()
30  const
31 {
32  return i_;
33 }
34 
35 // private members
36 
37 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::end()
38  const
39 {
40  return data_->end();
41 }
42 
43 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(PacketParserBase const * p)
44  : safe_i_( *p), parser_(p)
45 {}
46 
47 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(ParserProtector const & other_)
48  : safe_i_( *other_.parser_), parser_(other_.parser_)
49 {
50  other_.parser_ = nullptr;
51 }
52 
53 prefix_ senf::PacketParserBase::ParserProtector::~ParserProtector()
54 {
55  if (parser_) const_cast<PacketParserBase *>(parser_)->i_ = safe_i_;
56 }
57 
58 // protected members
59 
60 prefix_ senf::PacketParserBase::ParserProtector senf::PacketParserBase::protect()
61  const
62 {
63  return ParserProtector(this);
64 }
65 
66 prefix_ bool senf::PacketParserBase::check(size_type size)
67  const
68 {
69  return std::distance(i(),end()) < 0 ? false : size <= size_type(std::distance(i(),end()));
70 }
71 
72 prefix_ void senf::PacketParserBase::validate(size_type size)
73  const
74 {
75  if (SENF_UNLIKELY(!check(size)))
76  throw TruncatedPacketException();
77 }
78 
79 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s)
80  : i_ (i), data_ (s)
81 {}
82 
83 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s,
84  size_type size)
85  : i_ (i), data_ (s)
86 {
87  validate(size);
88 }
89 
90 // public members
91 
92 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i(size_type offset)
93  const
94 {
95  validate(offset);
96  return boost::next(i_, offset);
97 }
98 
99 prefix_ senf::PacketParserBase::state_type senf::PacketParserBase::state()
100  const
101 {
102  return data_;
103 }
104 
105 prefix_ senf::PacketData & senf::PacketParserBase::data()
106  const
107 {
108  return * data_;
109 }
110 
111 prefix_ void senf::PacketParserBase::init()
112  const
113 {}
114 
115 prefix_ void senf::PacketParserBase::defaultInit()
116  const
117 {}
118 
119 prefix_ senf::Packet senf::PacketParserBase::packet()
120  const
121 {
122  // OUCH ... I hate this but for some awkward packet types, access to the packet
123  // from the parser is really needed (e.g. UDP when building the pseudo-header
124  // for calculating the checksum).
125  return Packet(PacketInterpreterBase::ptr(static_cast<PacketInterpreterBase*>(&data())));
126 }
127 
128 //-/////////////////////////////////////////////////////////////////////////////////////////////////
129 #undef prefix_
130 
131 
132 // Local Variables:
133 // mode: c++
134 // fill-column: 100
135 // c-file-style: "senf"
136 // indent-tabs-mode: nil
137 // ispell-local-dictionary: "american"
138 // compile-command: "scons -u test"
139 // comment-column: 40
140 // End: