A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-fs-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
7 *
8 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
9 * ResiliNets Research Group https://resilinets.org/
10 * Information and Telecommunication Technology Center (ITTC)
11 * and Department of Electrical Engineering and Computer Science
12 * The University of Kansas Lawrence, KS USA.
13 *
14 * Work supported in part by NSF FIND (Future Internet Design) Program
15 * under grant CNS-0626918 (Postmodern Internet Architecture),
16 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
17 * US Department of Defense (DoD), and ITTC at The University of Kansas.
18 */
19
20#include "dsr-fs-header.h"
21
22#include "ns3/assert.h"
23#include "ns3/header.h"
24#include "ns3/log.h"
25
26#include <vector>
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("DsrFsHeader");
32
33namespace dsr
34{
35
37
38TypeId
40{
41 static TypeId tid = TypeId("ns3::dsr::DsrFsHeader")
43 .SetParent<Header>()
44 .SetGroupName("Dsr");
45 return tid;
46}
47
50{
51 return GetTypeId();
52}
53
55 : m_nextHeader(0),
56 m_messageType(0),
57 m_payloadLen(0),
58 m_sourceId(0),
59 m_destId(0),
60 m_data(0)
61{
62}
63
67
68void
70{
71 m_nextHeader = protocol;
72}
73
74uint8_t
76{
77 return m_nextHeader;
78}
79
80void
81DsrFsHeader::SetMessageType(uint8_t messageType)
82{
83 m_messageType = messageType;
84}
85
86uint8_t
88{
89 return m_messageType;
90}
91
92void
94{
95 m_payloadLen = length;
96}
97
98uint16_t
100{
101 return m_payloadLen;
102}
103
104void
105DsrFsHeader::SetSourceId(uint16_t sourceId)
106{
107 m_sourceId = sourceId;
108}
109
110uint16_t
112{
113 return m_sourceId;
114}
115
116void
118{
119 m_destId = destId;
120}
121
122uint16_t
124{
125 return m_destId;
126}
127
128void
129DsrFsHeader::Print(std::ostream& os) const
130{
131 os << "nextHeader: " << (uint32_t)GetNextHeader()
132 << " messageType: " << (uint32_t)GetMessageType() << " sourceId: " << (uint32_t)GetSourceId()
133 << " destinationId: " << (uint32_t)GetDestId()
134 << " length: " << (uint32_t)GetPayloadLength();
135}
136
139{
140 return 8;
141}
142
143void
156
159{
160 Buffer::Iterator i = start;
161
162 m_nextHeader = i.ReadU8();
163 m_messageType = i.ReadU8();
164 m_sourceId = i.ReadU16();
165 m_destId = i.ReadU16();
166 m_payloadLen = i.ReadU16();
167
168 const uint32_t dataLength = GetPayloadLength();
169 std::vector<uint8_t> data(dataLength);
170 i.Read(data.data(), dataLength);
171
172 if (dataLength > m_data.GetSize())
173 {
174 m_data.AddAtEnd(dataLength - m_data.GetSize());
175 }
176 else
177 {
178 m_data.RemoveAtEnd(m_data.GetSize() - dataLength);
179 }
180
181 i = m_data.Begin();
182 i.Write(data.data(), data.size());
183
184 return GetSerializedSize();
185}
186
188 : m_optionData(0),
189 m_optionsOffset(optionsOffset)
190{
191}
192
196
199{
200 DsrOptionHeader::Alignment align = {4, 0};
201 return m_optionData.GetSize() + CalculatePad(align);
202}
203
204void
206{
207 start.Write(m_optionData.Begin(), m_optionData.End());
208 DsrOptionHeader::Alignment align = {4, 0};
209 uint32_t fill = CalculatePad(align);
210 NS_LOG_LOGIC("fill with " << fill << " bytes padding");
211 switch (fill)
212 {
213 case 0:
214 return;
215 case 1:
217 return;
218 default:
219 DsrOptionPadnHeader(fill).Serialize(start);
220 return;
221 }
222}
223
226{
227 std::vector<uint8_t> buf(length);
228 start.Read(buf.data(), length);
230 m_optionData.AddAtEnd(length);
231 m_optionData.Begin().Write(buf.data(), buf.size());
232 return length;
233}
234
235void
237{
239
240 uint32_t pad = CalculatePad(option.GetAlignment());
241 NS_LOG_LOGIC("need " << pad << " bytes padding");
242 switch (pad)
243 {
244 case 0:
245 break; // no padding needed
246 case 1:
248 break;
249 default:
251 break;
252 }
253
256 it.Prev(option.GetSerializedSize());
257 option.Serialize(it);
258}
259
262{
263 return (alignment.offset - (m_optionData.GetSize() + m_optionsOffset)) % alignment.factor;
264}
265
271
272Buffer
277
279
280TypeId
282{
283 static TypeId tid =
284 TypeId("ns3::DsrRoutingHeader").AddConstructor<DsrRoutingHeader>().SetParent<DsrFsHeader>();
285 return tid;
286}
287
288TypeId
290{
291 return GetTypeId();
292}
293
298
302
303void
304DsrRoutingHeader::Print(std::ostream& os) const
305{
306 os << " nextHeader: " << (uint32_t)GetNextHeader()
307 << " messageType: " << (uint32_t)GetMessageType() << " sourceId: " << (uint32_t)GetSourceId()
308 << " destinationId: " << (uint32_t)GetDestId()
309 << " length: " << (uint32_t)GetPayloadLength();
310}
311
314{
315 // 8 bytes is the DsrFsHeader length
317}
318
319void
332
348
349} /* namespace dsr */
350} /* namespace ns3 */
iterator in a Buffer instance
Definition buffer.h:89
void WriteU8(uint8_t data)
Definition buffer.h:870
void Write(const uint8_t *buffer, uint32_t size)
Definition buffer.cc:937
void WriteU16(uint16_t data)
Definition buffer.cc:848
void Read(uint8_t *buffer, uint32_t size)
Definition buffer.cc:1114
void Prev()
go backward by one byte
Definition buffer.h:849
uint16_t ReadU16()
Definition buffer.h:1024
automatically resized byte buffer
Definition buffer.h:83
void RemoveAtEnd(uint32_t end)
Definition buffer.cc:482
uint32_t GetSize() const
Definition buffer.h:1057
Buffer::Iterator Begin() const
Definition buffer.h:1063
void AddAtEnd(uint32_t end)
Definition buffer.cc:349
Buffer::Iterator End() const
Definition buffer.h:1070
const uint8_t * PeekData() const
Definition buffer.cc:692
a unique identifier for an interface.
Definition type-id.h:48
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition type-id.h:670
Dsr fixed size header Format.
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.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
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 Deserialize(Buffer::Iterator start) override
Deserialize the packet.
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:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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:35
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.