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