A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
28
namespace
ns3
29
{
30
31
NS_LOG_COMPONENT_DEFINE
(
"DsrFsHeader"
);
32
33
namespace
dsr
34
{
35
36
NS_OBJECT_ENSURE_REGISTERED
(DsrFsHeader);
37
38
TypeId
39
DsrFsHeader::GetTypeId
()
40
{
41
static
TypeId
tid =
TypeId
(
"ns3::dsr::DsrFsHeader"
)
42
.
AddConstructor
<
DsrFsHeader
>()
43
.SetParent<Header>()
44
.SetGroupName(
"Dsr"
);
45
return
tid;
46
}
47
48
TypeId
49
DsrFsHeader::GetInstanceTypeId
()
const
50
{
51
return
GetTypeId
();
52
}
53
54
DsrFsHeader::DsrFsHeader
()
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
64
DsrFsHeader::~DsrFsHeader
()
65
{
66
}
67
68
void
69
DsrFsHeader::SetNextHeader
(uint8_t protocol)
70
{
71
m_nextHeader
= protocol;
72
}
73
74
uint8_t
75
DsrFsHeader::GetNextHeader
()
const
76
{
77
return
m_nextHeader
;
78
}
79
80
void
81
DsrFsHeader::SetMessageType
(uint8_t messageType)
82
{
83
m_messageType
= messageType;
84
}
85
86
uint8_t
87
DsrFsHeader::GetMessageType
()
const
88
{
89
return
m_messageType
;
90
}
91
92
void
93
DsrFsHeader::SetPayloadLength
(uint16_t length)
94
{
95
m_payloadLen
= length;
96
}
97
98
uint16_t
99
DsrFsHeader::GetPayloadLength
()
const
100
{
101
return
m_payloadLen
;
102
}
103
104
void
105
DsrFsHeader::SetSourceId
(uint16_t sourceId)
106
{
107
m_sourceId
= sourceId;
108
}
109
110
uint16_t
111
DsrFsHeader::GetSourceId
()
const
112
{
113
return
m_sourceId
;
114
}
115
116
void
117
DsrFsHeader::SetDestId
(uint16_t destId)
118
{
119
m_destId
= destId;
120
}
121
122
uint16_t
123
DsrFsHeader::GetDestId
()
const
124
{
125
return
m_destId
;
126
}
127
128
void
129
DsrFsHeader::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
137
uint32_t
138
DsrFsHeader::GetSerializedSize
()
const
139
{
140
return
8;
141
}
142
143
void
144
DsrFsHeader::Serialize
(
Buffer::Iterator
start)
const
145
{
146
Buffer::Iterator
i = start;
147
148
i.
WriteU8
(
m_nextHeader
);
149
i.
WriteU8
(
m_messageType
);
150
i.
WriteU16
(
m_sourceId
);
151
i.
WriteU16
(
m_destId
);
152
i.
WriteU16
(
m_payloadLen
);
153
154
i.
Write
(
m_data
.
PeekData
(),
m_data
.
GetSize
());
155
}
156
157
uint32_t
158
DsrFsHeader::Deserialize
(
Buffer::Iterator
start)
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
187
DsrOptionField::DsrOptionField
(
uint32_t
optionsOffset)
188
: m_optionData(0),
189
m_optionsOffset(optionsOffset)
190
{
191
}
192
193
DsrOptionField::~DsrOptionField
()
194
{
195
}
196
197
uint32_t
198
DsrOptionField::GetSerializedSize
()
const
199
{
200
DsrOptionHeader::Alignment
align = {4, 0};
201
return
m_optionData
.
GetSize
() +
CalculatePad
(align);
202
}
203
204
void
205
DsrOptionField::Serialize
(
Buffer::Iterator
start)
const
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:
216
DsrOptionPad1Header
().
Serialize
(start);
217
return
;
218
default
:
219
DsrOptionPadnHeader
(fill).
Serialize
(start);
220
return
;
221
}
222
}
223
224
uint32_t
225
DsrOptionField::Deserialize
(
Buffer::Iterator
start,
uint32_t
length)
226
{
227
std::vector<uint8_t> buf(length);
228
start.Read(buf.data(), length);
229
m_optionData
=
Buffer
();
230
m_optionData
.
AddAtEnd
(length);
231
m_optionData
.
Begin
().
Write
(buf.data(), buf.size());
232
return
length;
233
}
234
235
void
236
DsrOptionField::AddDsrOption
(
const
DsrOptionHeader
& option)
237
{
238
NS_LOG_FUNCTION_NOARGS
();
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:
247
AddDsrOption
(
DsrOptionPad1Header
());
248
break
;
249
default
:
250
AddDsrOption
(
DsrOptionPadnHeader
(pad));
251
break
;
252
}
253
254
m_optionData
.
AddAtEnd
(option.
GetSerializedSize
());
255
Buffer::Iterator
it =
m_optionData
.
End
();
256
it.
Prev
(option.
GetSerializedSize
());
257
option.
Serialize
(it);
258
}
259
260
uint32_t
261
DsrOptionField::CalculatePad
(
DsrOptionHeader::Alignment
alignment)
const
262
{
263
return
(alignment.
offset
- (
m_optionData
.
GetSize
() +
m_optionsOffset
)) % alignment.
factor
;
264
}
265
266
uint32_t
267
DsrOptionField::GetDsrOptionsOffset
()
const
268
{
269
return
m_optionsOffset
;
270
}
271
272
Buffer
273
DsrOptionField::GetDsrOptionBuffer
()
274
{
275
return
m_optionData
;
276
}
277
278
NS_OBJECT_ENSURE_REGISTERED
(
DsrRoutingHeader
);
279
280
TypeId
281
DsrRoutingHeader::GetTypeId
()
282
{
283
static
TypeId
tid =
284
TypeId
(
"ns3::DsrRoutingHeader"
).
AddConstructor
<
DsrRoutingHeader
>().SetParent<DsrFsHeader>();
285
return
tid;
286
}
287
288
TypeId
289
DsrRoutingHeader::GetInstanceTypeId
()
const
290
{
291
return
GetTypeId
();
292
}
293
294
DsrRoutingHeader::DsrRoutingHeader
()
295
:
DsrOptionField
(8)
296
{
297
}
298
299
DsrRoutingHeader::~DsrRoutingHeader
()
300
{
301
}
302
303
void
304
DsrRoutingHeader::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
312
uint32_t
313
DsrRoutingHeader::GetSerializedSize
()
const
314
{
315
// 8 bytes is the DsrFsHeader length
316
return
8 +
DsrOptionField::GetSerializedSize
();
317
}
318
319
void
320
DsrRoutingHeader::Serialize
(
Buffer::Iterator
start)
const
321
{
322
Buffer::Iterator
i = start;
323
324
i.
WriteU8
(
GetNextHeader
());
325
i.
WriteU8
(
GetMessageType
());
326
i.
WriteU16
(
GetSourceId
());
327
i.
WriteU16
(
GetDestId
());
328
i.
WriteU16
(
GetPayloadLength
());
329
330
DsrOptionField::Serialize
(i);
331
}
332
333
uint32_t
334
DsrRoutingHeader::Deserialize
(
Buffer::Iterator
start)
335
{
336
Buffer::Iterator
i = start;
337
338
SetNextHeader
(i.
ReadU8
());
339
SetMessageType
(i.
ReadU8
());
340
SetSourceId
(i.
ReadU16
());
341
SetDestId
(i.
ReadU16
());
342
SetPayloadLength
(i.
ReadU16
());
343
344
DsrOptionField::Deserialize
(i,
GetPayloadLength
());
345
346
return
GetSerializedSize
();
347
}
348
349
}
/* namespace dsr */
350
}
/* namespace ns3 */
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8()
Definition
buffer.h:1016
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition
buffer.h:870
ns3::Buffer::Iterator::Write
void Write(const uint8_t *buffer, uint32_t size)
Definition
buffer.cc:937
ns3::Buffer::Iterator::WriteU16
void WriteU16(uint16_t data)
Definition
buffer.cc:848
ns3::Buffer::Iterator::Read
void Read(uint8_t *buffer, uint32_t size)
Definition
buffer.cc:1114
ns3::Buffer::Iterator::Prev
void Prev()
go backward by one byte
Definition
buffer.h:849
ns3::Buffer::Iterator::ReadU16
uint16_t ReadU16()
Definition
buffer.h:1024
ns3::Buffer
automatically resized byte buffer
Definition
buffer.h:83
ns3::Buffer::RemoveAtEnd
void RemoveAtEnd(uint32_t end)
Definition
buffer.cc:482
ns3::Buffer::GetSize
uint32_t GetSize() const
Definition
buffer.h:1057
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition
buffer.h:1063
ns3::Buffer::AddAtEnd
void AddAtEnd(uint32_t end)
Definition
buffer.cc:349
ns3::Buffer::End
Buffer::Iterator End() const
Definition
buffer.h:1070
ns3::Buffer::PeekData
const uint8_t * PeekData() const
Definition
buffer.cc:692
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
ns3::TypeId::AddConstructor
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition
type-id.h:670
ns3::dsr::DsrFsHeader
Dsr fixed size header Format.
Definition
dsr-fs-header.h:71
ns3::dsr::DsrFsHeader::SetSourceId
void SetSourceId(uint16_t sourceId)
brief Set the source ID of the header.
Definition
dsr-fs-header.cc:105
ns3::dsr::DsrFsHeader::DsrFsHeader
DsrFsHeader()
Constructor.
Definition
dsr-fs-header.cc:54
ns3::dsr::DsrFsHeader::SetNextHeader
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
Definition
dsr-fs-header.cc:69
ns3::dsr::DsrFsHeader::SetDestId
void SetDestId(uint16_t destId)
brief Set the dest ID of the header.
Definition
dsr-fs-header.cc:117
ns3::dsr::DsrFsHeader::GetMessageType
uint8_t GetMessageType() const
brief Get the message type of the header.
Definition
dsr-fs-header.cc:87
ns3::dsr::DsrFsHeader::GetTypeId
static TypeId GetTypeId()
Get the type identificator.
Definition
dsr-fs-header.cc:39
ns3::dsr::DsrFsHeader::GetNextHeader
uint8_t GetNextHeader() const
Get the next header.
Definition
dsr-fs-header.cc:75
ns3::dsr::DsrFsHeader::m_destId
uint16_t m_destId
The destination node id.
Definition
dsr-fs-header.h:183
ns3::dsr::DsrFsHeader::m_sourceId
uint16_t m_sourceId
The source node id.
Definition
dsr-fs-header.h:179
ns3::dsr::DsrFsHeader::GetSourceId
uint16_t GetSourceId() const
brief Get the source ID of the header.
Definition
dsr-fs-header.cc:111
ns3::dsr::DsrFsHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the instance type ID.
Definition
dsr-fs-header.cc:49
ns3::dsr::DsrFsHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-fs-header.cc:144
ns3::dsr::DsrFsHeader::GetDestId
uint16_t GetDestId() const
brief Get the dest ID of the header.
Definition
dsr-fs-header.cc:123
ns3::dsr::DsrFsHeader::m_messageType
uint8_t m_messageType
The type of the message.
Definition
dsr-fs-header.h:171
ns3::dsr::DsrFsHeader::m_payloadLen
uint16_t m_payloadLen
The "payload length" field.
Definition
dsr-fs-header.h:175
ns3::dsr::DsrFsHeader::SetMessageType
void SetMessageType(uint8_t messageType)
brief Set the message type of the header.
Definition
dsr-fs-header.cc:81
ns3::dsr::DsrFsHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition
dsr-fs-header.cc:158
ns3::dsr::DsrFsHeader::GetPayloadLength
uint16_t GetPayloadLength() const
Get the payload length of the header.
Definition
dsr-fs-header.cc:99
ns3::dsr::DsrFsHeader::SetPayloadLength
void SetPayloadLength(uint16_t length)
brief Set the payload length of the header.
Definition
dsr-fs-header.cc:93
ns3::dsr::DsrFsHeader::m_nextHeader
uint8_t m_nextHeader
The "next header" field.
Definition
dsr-fs-header.h:167
ns3::dsr::DsrFsHeader::m_data
Buffer m_data
The data of the extension.
Definition
dsr-fs-header.h:187
ns3::dsr::DsrFsHeader::Print
void Print(std::ostream &os) const override
Print some information about the packet.
Definition
dsr-fs-header.cc:129
ns3::dsr::DsrFsHeader::~DsrFsHeader
~DsrFsHeader() override
Destructor.
Definition
dsr-fs-header.cc:64
ns3::dsr::DsrFsHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
dsr-fs-header.cc:138
ns3::dsr::DsrOptionField
Option field for an DsrFsHeader Enables adding options to an DsrFsHeader.
Definition
dsr-fs-header.h:201
ns3::dsr::DsrOptionField::Serialize
void Serialize(Buffer::Iterator start) const
Serialize all added options.
Definition
dsr-fs-header.cc:205
ns3::dsr::DsrOptionField::AddDsrOption
void AddDsrOption(const DsrOptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
Definition
dsr-fs-header.cc:236
ns3::dsr::DsrOptionField::Deserialize
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
Definition
dsr-fs-header.cc:225
ns3::dsr::DsrOptionField::m_optionData
Buffer m_optionData
Data payload.
Definition
dsr-fs-header.h:256
ns3::dsr::DsrOptionField::GetDsrOptionBuffer
Buffer GetDsrOptionBuffer()
Get the buffer.
Definition
dsr-fs-header.cc:273
ns3::dsr::DsrOptionField::m_optionsOffset
uint32_t m_optionsOffset
Offset.
Definition
dsr-fs-header.h:260
ns3::dsr::DsrOptionField::GetDsrOptionsOffset
uint32_t GetDsrOptionsOffset() const
Get the offset where the options begin, measured from the start of the extension header.
Definition
dsr-fs-header.cc:267
ns3::dsr::DsrOptionField::GetSerializedSize
uint32_t GetSerializedSize() const
Get the serialized size of the packet.
Definition
dsr-fs-header.cc:198
ns3::dsr::DsrOptionField::CalculatePad
uint32_t CalculatePad(DsrOptionHeader::Alignment alignment) const
Calculate padding.
Definition
dsr-fs-header.cc:261
ns3::dsr::DsrOptionField::DsrOptionField
DsrOptionField(uint32_t optionsOffset)
Constructor.
Definition
dsr-fs-header.cc:187
ns3::dsr::DsrOptionField::~DsrOptionField
~DsrOptionField()
Destructor.
Definition
dsr-fs-header.cc:193
ns3::dsr::DsrOptionHeader
header for Dsr Options.
Definition
dsr-option-header.h:42
ns3::dsr::DsrOptionHeader::GetAlignment
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
Definition
dsr-option-header.cc:131
ns3::dsr::DsrOptionHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-option-header.cc:103
ns3::dsr::DsrOptionHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
dsr-option-header.cc:97
ns3::dsr::DsrOptionPad1Header
Header of Dsr Option Pad1.
Definition
dsr-option-header.h:142
ns3::dsr::DsrOptionPad1Header::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-option-header.cc:177
ns3::dsr::DsrOptionPadnHeader
Header of Dsr Option Padn.
Definition
dsr-option-header.h:190
ns3::dsr::DsrOptionPadnHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-option-header.cc:236
ns3::dsr::DsrRoutingHeader
Header of Dsr Routing.
Definition
dsr-fs-header.h:268
ns3::dsr::DsrRoutingHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition
dsr-fs-header.cc:334
ns3::dsr::DsrRoutingHeader::DsrRoutingHeader
DsrRoutingHeader()
Constructor.
Definition
dsr-fs-header.cc:294
ns3::dsr::DsrRoutingHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
dsr-fs-header.cc:313
ns3::dsr::DsrRoutingHeader::~DsrRoutingHeader
~DsrRoutingHeader() override
Destructor.
Definition
dsr-fs-header.cc:299
ns3::dsr::DsrRoutingHeader::Print
void Print(std::ostream &os) const override
Print some information about the packet.
Definition
dsr-fs-header.cc:304
ns3::dsr::DsrRoutingHeader::GetTypeId
static TypeId GetTypeId()
Get the type identificator.
Definition
dsr-fs-header.cc:281
ns3::dsr::DsrRoutingHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the instance type ID.
Definition
dsr-fs-header.cc:289
ns3::dsr::DsrRoutingHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-fs-header.cc:320
uint32_t
dsr-fs-header.h
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition
log.h:271
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition
log-macros-enabled.h:195
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
data
uint8_t data[writeSize]
Definition
socket-bound-tcp-static-routing.cc:41
ns3::dsr::DsrOptionHeader::Alignment
Represents the alignment requirements of an option header.
Definition
dsr-option-header.h:49
ns3::dsr::DsrOptionHeader::Alignment::factor
uint8_t factor
Factor.
Definition
dsr-option-header.h:50
ns3::dsr::DsrOptionHeader::Alignment::offset
uint8_t offset
Offset.
Definition
dsr-option-header.h:51
src
dsr
model
dsr-fs-header.cc
Generated on Fri Nov 1 2024 09:20:38 for ns-3 by
1.11.0