EMANE  1.0.1
upstreampacket.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2016 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2008 - DRS CenGen, LLC, Columbia, Maryland
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of DRS CenGen, LLC nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "emane/upstreampacket.h"
35 #include "emane/net.h"
36 
37 #include <memory>
38 
39 class EMANE::UpstreamPacket::Implementation
40 {
41 public:
42  Implementation():
43  head_{},
44  pShared_{std::make_shared<Shared>()}{}
45 
46  Implementation(const PacketInfo & info, const void * buf, size_t len):
47  head_{},
48  pShared_{std::make_shared<Shared>()}
49  {
50  const unsigned char * c = static_cast<const unsigned char *>(buf);
51  pShared_->info_ = info;
52  pShared_->packetSegment_.reserve(len);
53  pShared_->packetSegment_.insert(pShared_->packetSegment_.end(),&c[0],&c[len]);
54  }
55 
56  Implementation(const PacketInfo & info,const Utils::VectorIO & vectorIO):
57  head_{},
58  pShared_{std::make_shared<Shared>()}
59  {
60  pShared_->info_ = info;
61 
62  for(const auto & iov : vectorIO)
63  {
64  pShared_->packetSegment_.insert(pShared_->packetSegment_.end(),
65  &static_cast<const std::uint8_t *>(iov.iov_base)[0],
66  &static_cast<const std::uint8_t *>(iov.iov_base)[iov.iov_len]);
67  }
68  }
69 
70  size_t strip(size_t size)
71  {
72  if(head_ + size < pShared_->packetSegment_.size())
73  {
74  head_ += size;
75  }
76  else
77  {
78  size = pShared_->packetSegment_.size() - head_;
79  head_ += size;
80  }
81 
82  return size;
83  }
84 
85 
86  std::uint16_t stripLengthPrefixFraming()
87  {
88  std::uint16_t u16LengthPrefixFraming{};
89 
90  if(head_ + sizeof(uint16_t) < pShared_->packetSegment_.size())
91  {
92  u16LengthPrefixFraming =
93  NTOHS(*reinterpret_cast<const std::uint16_t *>(&pShared_->packetSegment_[head_]));
94 
95  head_ += sizeof(uint16_t);
96  }
97 
98  return u16LengthPrefixFraming;
99  }
100 
101 
102  const void * get() const
103  {
104  return(head_ < pShared_->packetSegment_.size()) ? &pShared_->packetSegment_[head_] : 0;
105  }
106 
107 
108  size_t length() const
109  {
110  return pShared_->packetSegment_.size() - head_;
111  }
112 
113 
114  const PacketInfo & getPacketInfo() const
115  {
116  return pShared_->info_;
117  }
118 
119 private:
120  typedef std::vector<unsigned char> PacketSegment;
121 
122  class Shared
123  {
124  public:
125  PacketSegment packetSegment_;
126  PacketInfo info_{0,0,0,{}};
127  };
128 
129  PacketSegment::size_type head_;
130  std::shared_ptr<Shared> pShared_;
131 };
132 
133 
135  const void * buf,
136  size_t size):
137  pImpl_{new Implementation{info,buf,size}}{}
138 
140  const Utils::VectorIO & vectorIO):
141  pImpl_{new Implementation{info,vectorIO}}{}
142 
144  pImpl_{new Implementation{*pkt.pImpl_}}{}
145 
147  pImpl_{std::move(pkt.pImpl_)}{}
148 
150 
152 {
153  pImpl_.reset(new Implementation{*pkt.pImpl_});
154  return *this;
155 }
156 
158 {
159  pImpl_ = std::move(pkt.pImpl_);
160  return *this;
161 }
162 
163 
164 size_t EMANE::UpstreamPacket::strip(size_t size)
165 {
166  return pImpl_->strip(size);
167 }
168 
169 
171 {
172  return pImpl_->stripLengthPrefixFraming();
173 }
174 
175 
176 const void * EMANE::UpstreamPacket::get() const
177 {
178  return pImpl_->get();
179 }
180 
181 
183 {
184  return pImpl_->length();
185 }
186 
187 
189 {
190  return pImpl_->getPacketInfo();
191 }
A Packet class that allows upstream processing to strip layer headers as the packet travels up the st...
UpstreamPacket(const PacketInfo &info, const void *buf, size_t len)
std::uint16_t stripLengthPrefixFraming()
size_t strip(size_t size)
std::vector< iovec > VectorIO
Definition: vectorio.h:43
Store source, destination, creation time and priority information for a packet.
Definition: packetinfo.h:50
const PacketInfo & getPacketInfo() const
const void * get() const
constexpr std::uint16_t NTOHS(std::uint16_t x)
Definition: net.h:136
UpstreamPacket & operator=(const UpstreamPacket &pkt)