A Discrete-Event Network Simulator
API
dsr-fs-header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Yufei Cheng
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: Yufei Cheng <yfcheng@ittc.ku.edu>
18  *
19  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20  * ResiliNets Research Group https://resilinets.org/
21  * Information and Telecommunication Technology Center (ITTC)
22  * and Department of Electrical Engineering and Computer Science
23  * The University of Kansas Lawrence, KS USA.
24  *
25  * Work supported in part by NSF FIND (Future Internet Design) Program
26  * under grant CNS-0626918 (Postmodern Internet Architecture),
27  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28  * US Department of Defense (DoD), and ITTC at The University of Kansas.
29  */
30 
31 #include "dsr-fs-header.h"
32 
33 #include "ns3/assert.h"
34 #include "ns3/header.h"
35 #include "ns3/log.h"
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE("DsrFsHeader");
41 
42 namespace dsr
43 {
44 
45 NS_OBJECT_ENSURE_REGISTERED(DsrFsHeader);
46 
47 TypeId
49 {
50  static TypeId tid = TypeId("ns3::dsr::DsrFsHeader")
52  .SetParent<Header>()
53  .SetGroupName("Dsr");
54  return tid;
55 }
56 
57 TypeId
59 {
60  return GetTypeId();
61 }
62 
64  : m_nextHeader(0),
65  m_messageType(0),
66  m_payloadLen(0),
67  m_sourceId(0),
68  m_destId(0),
69  m_data(0)
70 {
71 }
72 
74 {
75 }
76 
77 void
78 DsrFsHeader::SetNextHeader(uint8_t protocol)
79 {
80  m_nextHeader = protocol;
81 }
82 
83 uint8_t
85 {
86  return m_nextHeader;
87 }
88 
89 void
90 DsrFsHeader::SetMessageType(uint8_t messageType)
91 {
92  m_messageType = messageType;
93 }
94 
95 uint8_t
97 {
98  return m_messageType;
99 }
100 
101 void
103 {
104  m_payloadLen = length;
105 }
106 
107 uint16_t
109 {
110  return m_payloadLen;
111 }
112 
113 void
114 DsrFsHeader::SetSourceId(uint16_t sourceId)
115 {
116  m_sourceId = sourceId;
117 }
118 
119 uint16_t
121 {
122  return m_sourceId;
123 }
124 
125 void
126 DsrFsHeader::SetDestId(uint16_t destId)
127 {
128  m_destId = destId;
129 }
130 
131 uint16_t
133 {
134  return m_destId;
135 }
136 
137 void
138 DsrFsHeader::Print(std::ostream& os) const
139 {
140  os << "nextHeader: " << (uint32_t)GetNextHeader()
141  << " messageType: " << (uint32_t)GetMessageType() << " sourceId: " << (uint32_t)GetSourceId()
142  << " destinationId: " << (uint32_t)GetDestId()
143  << " length: " << (uint32_t)GetPayloadLength();
144 }
145 
146 uint32_t
148 {
149  return 8;
150 }
151 
152 void
154 {
156 
159  i.WriteU16(m_sourceId);
160  i.WriteU16(m_destId);
162 
164 }
165 
166 uint32_t
168 {
170 
171  m_nextHeader = i.ReadU8();
172  m_messageType = i.ReadU8();
173  m_sourceId = i.ReadU16();
174  m_destId = i.ReadU16();
175  m_payloadLen = i.ReadU16();
176 
177  uint32_t dataLength = GetPayloadLength();
178  uint8_t data[dataLength];
179  i.Read(data, dataLength);
180 
181  if (dataLength > m_data.GetSize())
182  {
183  m_data.AddAtEnd(dataLength - m_data.GetSize());
184  }
185  else
186  {
187  m_data.RemoveAtEnd(m_data.GetSize() - dataLength);
188  }
189 
190  i = m_data.Begin();
191  i.Write(data, dataLength);
192 
193  return GetSerializedSize();
194 }
195 
196 DsrOptionField::DsrOptionField(uint32_t optionsOffset)
197  : m_optionData(0),
198  m_optionsOffset(optionsOffset)
199 {
200 }
201 
203 {
204 }
205 
206 uint32_t
208 {
209  DsrOptionHeader::Alignment align = {4, 0};
210  return m_optionData.GetSize() + CalculatePad(align);
211 }
212 
213 void
215 {
217  DsrOptionHeader::Alignment align = {4, 0};
218  uint32_t fill = CalculatePad(align);
219  NS_LOG_LOGIC("fill with " << fill << " bytes padding");
220  switch (fill)
221  {
222  case 0:
223  return;
224  case 1:
226  return;
227  default:
229  return;
230  }
231 }
232 
233 uint32_t
235 {
236  uint8_t buf[length];
237  start.Read(buf, length);
238  m_optionData = Buffer();
239  m_optionData.AddAtEnd(length);
240  m_optionData.Begin().Write(buf, length);
241  return length;
242 }
243 
244 void
246 {
248 
249  uint32_t pad = CalculatePad(option.GetAlignment());
250  NS_LOG_LOGIC("need " << pad << " bytes padding");
251  switch (pad)
252  {
253  case 0:
254  break; // no padding needed
255  case 1:
257  break;
258  default:
260  break;
261  }
262 
265  it.Prev(option.GetSerializedSize());
266  option.Serialize(it);
267 }
268 
269 uint32_t
271 {
272  return (alignment.offset - (m_optionData.GetSize() + m_optionsOffset)) % alignment.factor;
273 }
274 
275 uint32_t
277 {
278  return m_optionsOffset;
279 }
280 
281 Buffer
283 {
284  return m_optionData;
285 }
286 
288 
289 TypeId
291 {
292  static TypeId tid =
293  TypeId("ns3::DsrRoutingHeader").AddConstructor<DsrRoutingHeader>().SetParent<DsrFsHeader>();
294  return tid;
295 }
296 
297 TypeId
299 {
300  return GetTypeId();
301 }
302 
304  : DsrOptionField(8)
305 {
306 }
307 
309 {
310 }
311 
312 void
313 DsrRoutingHeader::Print(std::ostream& os) const
314 {
315  os << " nextHeader: " << (uint32_t)GetNextHeader()
316  << " messageType: " << (uint32_t)GetMessageType() << " sourceId: " << (uint32_t)GetSourceId()
317  << " destinationId: " << (uint32_t)GetDestId()
318  << " length: " << (uint32_t)GetPayloadLength();
319 }
320 
321 uint32_t
323 {
324  // 8 bytes is the DsrFsHeader length
326 }
327 
328 void
330 {
332 
333  i.WriteU8(GetNextHeader());
334  i.WriteU8(GetMessageType());
335  i.WriteU16(GetSourceId());
336  i.WriteU16(GetDestId());
338 
340 }
341 
342 uint32_t
344 {
346 
347  SetNextHeader(i.ReadU8());
348  SetMessageType(i.ReadU8());
349  SetSourceId(i.ReadU16());
350  SetDestId(i.ReadU16());
352 
354 
355  return GetSerializedSize();
356 }
357 
358 } /* namespace dsr */
359 } /* namespace ns3 */
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
void Write(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:948
void WriteU16(uint16_t data)
Definition: buffer.cc:859
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1125
void Prev()
go backward by one byte
Definition: buffer.h:860
uint16_t ReadU16()
Definition: buffer.h:1035
automatically resized byte buffer
Definition: buffer.h:94
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:493
uint32_t GetSize() const
Definition: buffer.h:1068
Buffer::Iterator Begin() const
Definition: buffer.h:1074
void AddAtEnd(uint32_t end)
Definition: buffer.cc:360
Buffer::Iterator End() const
Definition: buffer.h:1081
const uint8_t * PeekData() const
Definition: buffer.cc:703
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition: type-id.h:651
Dsr fixed size header Format.
Definition: dsr-fs-header.h:82
void SetSourceId(uint16_t sourceId)
brief Set the source ID of the header.
DsrFsHeader()
Constructor.
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
void SetDestId(uint16_t destId)
brief Set the dest ID of the header.
uint8_t GetMessageType() const
brief Get the message type of the header.
static TypeId GetTypeId()
Get the type identificator.
uint8_t GetNextHeader() const
Get the next header.
uint16_t m_destId
The destination node id.
uint16_t m_sourceId
The source node id.
uint16_t GetSourceId() const
brief Get the source ID of the header.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint16_t GetDestId() const
brief Get the dest ID of the header.
uint8_t m_messageType
The type of the message.
uint16_t m_payloadLen
The "payload length" field.
void SetMessageType(uint8_t messageType)
brief Set the message type of the header.
uint16_t GetPayloadLength() const
Get the payload length of the header.
void SetPayloadLength(uint16_t length)
brief Set the payload length of the header.
uint8_t m_nextHeader
The "next header" field.
Buffer m_data
The data of the extension.
void Print(std::ostream &os) const override
Print some information about the packet.
~DsrFsHeader() override
Destructor.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Option field for an DsrFsHeader Enables adding options to an DsrFsHeader.
void Serialize(Buffer::Iterator start) const
Serialize all added options.
void AddDsrOption(const DsrOptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
Buffer m_optionData
Data payload.
Buffer GetDsrOptionBuffer()
Get the buffer.
uint32_t m_optionsOffset
Offset.
uint32_t GetDsrOptionsOffset() const
Get the offset where the options begin, measured from the start of the extension header.
uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint32_t CalculatePad(DsrOptionHeader::Alignment alignment) const
Calculate padding.
DsrOptionField(uint32_t optionsOffset)
Constructor.
Header for Dsr Options.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Header of Dsr Option Pad1.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of Dsr Option Padn.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of Dsr Routing.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
~DsrRoutingHeader() override
Destructor.
void Print(std::ostream &os) const override
Print some information about the packet.
static TypeId GetTypeId()
Get the type identificator.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
represents the alignment requirements of an option header