A Discrete-Event Network Simulator
API
mesh-information-element-vector.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 IITP RAS
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Pavel Boyko <boyko@iitp.ru>
18  */
19 
21 
22 #include "ns3/hwmp-protocol.h"
23 #include "ns3/packet.h"
24 
25 #include <algorithm>
26 // All information elements:
27 #include "ns3/ie-dot11s-beacon-timing.h"
28 #include "ns3/ie-dot11s-configuration.h"
29 #include "ns3/ie-dot11s-id.h"
30 #include "ns3/ie-dot11s-metric-report.h"
31 #include "ns3/ie-dot11s-peer-management.h"
32 #include "ns3/ie-dot11s-peering-protocol.h"
33 #include "ns3/ie-dot11s-perr.h"
34 #include "ns3/ie-dot11s-prep.h"
35 #include "ns3/ie-dot11s-preq.h"
36 #include "ns3/ie-dot11s-rann.h"
37 
38 namespace ns3
39 {
40 
41 NS_OBJECT_ENSURE_REGISTERED(MeshInformationElementVector);
42 
44  : m_maxSize(1500)
45 {
46 }
47 
49 {
50  for (auto i = m_elements.begin(); i != m_elements.end(); i++)
51  {
52  *i = nullptr;
53  }
54  m_elements.clear();
55 }
56 
57 TypeId
59 {
60  static TypeId tid = TypeId("ns3::MeshInformationElementVector")
61  .SetParent<Header>()
62  .SetGroupName("Mesh")
63  .AddConstructor<MeshInformationElementVector>();
64  return tid;
65 }
66 
67 TypeId
69 {
70  return GetTypeId();
71 }
72 
73 uint32_t
75 {
76  return GetSize();
77 }
78 
79 void
81 {
82  for (auto i = m_elements.begin(); i != m_elements.end(); i++)
83  {
84  start = (*i)->Serialize(start);
85  }
86 }
87 
88 uint32_t
90 {
91  NS_FATAL_ERROR("This variant should not be called on a variable-sized header");
92  return 0;
93 }
94 
95 uint32_t
97 {
98  uint32_t size = start.GetDistanceFrom(end);
99  uint32_t remaining = size;
100  while (remaining > 0)
101  {
102  uint32_t deserialized = DeserializeSingleIe(start);
103  start.Next(deserialized);
104  NS_ASSERT(deserialized <= remaining);
105  remaining -= deserialized;
106  }
107  NS_ASSERT_MSG(remaining == 0, "Error in deserialization");
108  return size;
109 }
110 
111 uint32_t
113 {
115  uint8_t id = i.ReadU8();
116  uint8_t length = i.ReadU8();
117  i.Prev(2);
118  Ptr<WifiInformationElement> newElement;
119  switch (id)
120  {
122  newElement = Create<dot11s::IeConfiguration>();
123  break;
124  case IE_MESH_ID:
125  newElement = Create<dot11s::IeMeshId>();
126  break;
128  newElement = Create<dot11s::IeLinkMetricReport>();
129  break;
131  newElement = Create<dot11s::IePeerManagement>();
132  break;
133  case IE_BEACON_TIMING:
134  newElement = Create<dot11s::IeBeaconTiming>();
135  break;
136  case IE_RANN:
137  newElement = Create<dot11s::IeRann>();
138  break;
139  case IE_PREQ:
140  newElement = Create<dot11s::IePreq>();
141  break;
142  case IE_PREP:
143  newElement = Create<dot11s::IePrep>();
144  break;
145  case IE_PERR:
146  newElement = Create<dot11s::IePerr>();
147  break;
149  newElement = Create<dot11s::IePeeringProtocol>();
150  break;
151  default:
152  NS_FATAL_ERROR("Information element " << +id << " is not implemented");
153  return 0;
154  }
155  if (GetSize() + length > m_maxSize)
156  {
157  NS_FATAL_ERROR("Check max size for information element!");
158  }
159  i = newElement->Deserialize(i);
160  m_elements.push_back(newElement);
161  return i.GetDistanceFrom(start);
162 }
163 
164 void
165 MeshInformationElementVector::Print(std::ostream& os) const
166 {
167  for (auto i = m_elements.begin(); i != m_elements.end(); i++)
168  {
169  os << "(";
170  (*i)->Print(os);
171  os << ")";
172  }
173 }
174 
177 {
178  return m_elements.begin();
179 }
180 
183 {
184  return m_elements.end();
185 }
186 
187 bool
189 {
190  if (element->GetSerializedSize() + GetSize() > m_maxSize)
191  {
192  return false;
193  }
194  m_elements.push_back(element);
195  return true;
196 }
197 
200 {
201  for (auto i = m_elements.begin(); i != m_elements.end(); i++)
202  {
203  if ((*i)->ElementId() == id)
204  {
205  return *i;
206  }
207  }
208  return nullptr;
209 }
210 
211 uint32_t
213 {
214  uint32_t size = 0;
215  for (auto i = m_elements.begin(); i != m_elements.end(); i++)
216  {
217  size += (*i)->GetSerializedSize();
218  }
219  return size;
220 }
221 
222 bool
224 {
225  if (m_elements.size() != a.m_elements.size())
226  {
227  NS_ASSERT(false);
228  return false;
229  }
230  // In principle we could bypass some of the faffing about (and speed
231  // the comparison) by simply serialising each IE vector into a
232  // buffer and memcmp'ing the two.
233  //
234  // I'm leaving it like this, however, so that there is the option of
235  // having individual Information Elements implement slightly more
236  // flexible equality operators.
237  auto j = a.m_elements.begin();
238  for (auto i = m_elements.begin(); i != m_elements.end(); i++, j++)
239  {
240  if (!(*(*i) == *(*j)))
241  {
242  return false;
243  }
244  }
245 
246  return true;
247 }
248 
249 } // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
uint32_t GetDistanceFrom(const Iterator &o) const
Definition: buffer.cc:780
void Prev()
go backward by one byte
Definition: buffer.h:860
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Iterator End()
Returns End of the vector.
bool AddInformationElement(Ptr< WifiInformationElement > element)
add an IE, if maxSize has exceeded, returns false
IE_VECTOR m_elements
Information element vector.
uint32_t GetSize() const
Current number of bytes.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
std::vector< Ptr< WifiInformationElement > >::iterator Iterator
As soon as this is a vector, we define an Iterator.
uint16_t m_maxSize
Size in bytes (actually, max packet length)
Ptr< WifiInformationElement > FindFirst(WifiInformationElementId id) const
vector of pointers to information elements is the body of IeVector
Iterator Begin()
Returns Begin of the vector.
void Serialize(Buffer::Iterator start) const override
void Print(std::ostream &os) const override
virtual bool operator==(const MeshInformationElementVector &a) const
Check if the given WifiInformationElementVectors are equivalent.
uint32_t DeserializeSingleIe(Buffer::Iterator start)
Needed when you try to deserialize a lonely IE inside other header.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
uint16_t GetSerializedSize() const
Get the size of the serialized IE including Element ID and length fields (for every element this IE i...
Buffer::Iterator Deserialize(Buffer::Iterator i)
Deserialize entire IE (which may possibly be fragmented into multiple elements), which must be presen...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
#define IE11S_MESH_PEERING_PROTOCOL_VERSION
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t WifiInformationElementId
This type is used to represent an Information Element ID.
#define IE_PREQ
#define IE_RANN
#define IE_MESH_CONFIGURATION
#define IE_PREP
#define IE_BEACON_TIMING
#define IE_MESH_LINK_METRIC_REPORT
#define IE_MESH_PEERING_MANAGEMENT
#define IE_MESH_ID
#define IE_PERR