EMANE  1.2.1
commonmacheader.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014,2016 - Adjacent Link LLC, Bridgewater,
3  * New Jersey
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 Adjacent Link 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/commonmacheader.h"
35 #include "commonmacheader.pb.h"
36 
37 class EMANE::CommonMACHeader::Implementation
38 {
39 public:
40  Implementation(RegistrationId registrationId,
41  std::uint64_t u64SequenceNumber):
42  registrationId_{registrationId},
43  u64SequenceNumber_{u64SequenceNumber}
44  {}
45 
47  {
48  return registrationId_;
49  }
50 
51  std::uint64_t getSequenceNumber() const
52  {
53  return u64SequenceNumber_;
54  }
55 
56 private:
57  RegistrationId registrationId_;
58  std::uint64_t u64SequenceNumber_;
59 };
60 
62 {
63  if(pkt.length() >= sizeof(decltype(pkt.stripLengthPrefixFraming())))
64  {
65  std::uint16_t u16HeaderLength{pkt.stripLengthPrefixFraming()};
66 
67  if(pkt.length() >= u16HeaderLength)
68  {
69  EMANEMessage::CommonMACHeader msg;
70 
71  if(!msg.ParseFromArray(pkt.get(),u16HeaderLength))
72  {
73  throw SerializationException("unable to deserialize CommonMACHeader");
74  }
75 
76  pImpl_.reset(new Implementation{static_cast<RegistrationId>(msg.registrationid()),
77  msg.sequencenumber()});
78 
79  }
80  else
81  {
82  throw SerializationException("CommonMACHeader not large enough for header data");
83  }
84 
85  // strip common mac header from pkt
86  pkt.strip(u16HeaderLength);
87  }
88  else
89  {
90  throw SerializationException("CommonMACHeader not large enough for header size data");
91  }
92 }
93 
94 
96  std::uint64_t u64SequenceNumber):
97  pImpl_{new Implementation{registrationId,
98  u64SequenceNumber}}
99 {}
100 
101 
103  pImpl_(std::move(rvalue.pImpl_))
104 {}
105 
107 
109 {
110  return pImpl_->getRegistrationId();
111 }
112 
114 {
115  return pImpl_->getSequenceNumber();
116 }
117 
118 
120 {
121  EMANEMessage::CommonMACHeader msg{};
122 
123  msg.set_registrationid(pImpl_->getRegistrationId());
124  msg.set_sequencenumber(pImpl_->getSequenceNumber());
125 
126  std::string sSerialization;
127 
128  if(!msg.SerializeToString(&sSerialization))
129  {
130  throw SerializationException("unable to serialize CommonMACHeader");
131  }
132 
133  // prepend order is important
134  pkt.prepend(sSerialization.c_str(),sSerialization.size());
135 
136  pkt.prependLengthPrefixFraming(sSerialization.size());
137 }
138 
140 {
141  Strings sFormat{"regid",
142  std::to_string(pImpl_->getRegistrationId()),
143  "seq",
144  std::to_string(pImpl_->getSequenceNumber())};
145 
146  return sFormat;
147 }
A Packet class that allows upstream processing to strip layer headers as the packet travels up the st...
SerializationException is thrown when an exception occurs during serialization or deserialization of ...
CommonMACHeader(RegistrationId registrationId, std::uint64_t u64SequenceNumber)
RegistrationId getRegistrationId() const
std::uint16_t stripLengthPrefixFraming()
size_t strip(size_t size)
void prepend(const void *buf, size_t size)
Specialized packet the allows downstream processing to add layer specific headers as the packet trave...
std::uint16_t RegistrationId
Definition: types.h:59
void prependTo(DownstreamPacket &pkt) const
std::list< std::string > Strings
Definition: types.h:66
std::uint64_t getSequenceNumber() const
const void * get() const
void prependLengthPrefixFraming(std::uint16_t u16Length)