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-routing-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
* Modified by: Tommaso Pecorella <tommaso.pecorella@unifi.it>
8
* Lorenzo Bartolini <l.bartolini02@gmail.com>
9
*
10
* James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
11
* ResiliNets Research Group https://resilinets.org/
12
* Information and Telecommunication Technology Center (ITTC)
13
* and Department of Electrical Engineering and Computer Science
14
* The University of Kansas Lawrence, KS USA.
15
*
16
* Work supported in part by NSF FIND (Future Internet Design) Program
17
* under grant CNS-0626918 (Postmodern Internet Architecture),
18
* NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
19
* US Department of Defense (DoD), and ITTC at The University of Kansas.
20
*/
21
22
#include "
dsr-routing-header.h
"
23
24
#include "
dsr-options.h
"
25
#include "
dsr-routing.h
"
26
27
#include "ns3/assert.h"
28
#include "ns3/header.h"
29
#include "ns3/iana-internet-protocol-numbers.h"
30
#include "ns3/log.h"
31
32
#include <vector>
33
34
namespace
ns3
35
{
36
37
NS_LOG_COMPONENT_DEFINE
(
"DsrRoutingHeader"
);
38
39
namespace
dsr
40
{
41
42
NS_OBJECT_ENSURE_REGISTERED
(
DsrRoutingHeader
);
43
44
TypeId
45
DsrRoutingHeader::GetTypeId
()
46
{
47
static
TypeId
tid =
TypeId
(
"ns3::DsrRoutingHeader"
)
48
.
AddConstructor
<
DsrRoutingHeader
>()
49
.SetParent<Header>()
50
.SetGroupName(
"Dsr"
);
51
return
tid;
52
}
53
54
TypeId
55
DsrRoutingHeader::GetInstanceTypeId
()
const
56
{
57
return
GetTypeId
();
58
}
59
60
DsrRoutingHeader::DsrRoutingHeader
()
61
:
m_optionData
(0),
62
m_nextHeader
(
iana
::internetprotocolnumbers::NO_NEXT_HEADER)
63
{
64
}
65
66
DsrRoutingHeader::~DsrRoutingHeader
()
67
{
68
}
69
70
void
71
DsrRoutingHeader::Print
(std::ostream& os)
const
72
{
73
os <<
" nextHeader: "
<< (
uint32_t
)
GetNextHeader
()
74
<<
" length: "
<< (
uint32_t
)
GetPayloadLength
();
75
}
76
77
void
78
DsrRoutingHeader::SetNextHeader
(uint8_t protocol)
79
{
80
m_nextHeader
= protocol;
81
}
82
83
uint8_t
84
DsrRoutingHeader::GetNextHeader
()
const
85
{
86
return
m_nextHeader
;
87
}
88
89
uint32_t
90
DsrRoutingHeader::GetSerializedSize
()
const
91
{
92
// 4 bytes is the DsrFsHeader length: 1 byte for next header, 1 byte reserved, 2 bytes for
93
// payload length
94
95
DsrOptionHeader::Alignment
align = {4, 0};
96
return
4 +
m_optionData
.GetSize() +
CalculatePad
(align);
97
}
98
99
void
100
DsrRoutingHeader::Serialize
(
Buffer::Iterator
start)
const
101
{
102
Buffer::Iterator
i = start;
103
104
i.
WriteU8
(
m_nextHeader
);
105
i.
WriteU8
(0);
// reserved field
106
i.
WriteHtonU16
(
GetPayloadLength
());
107
108
SerializeOptions
(i);
109
}
110
111
uint32_t
112
DsrRoutingHeader::Deserialize
(
Buffer::Iterator
start)
113
{
114
Buffer::Iterator
i = start;
115
116
SetNextHeader
(i.
ReadU8
());
117
i.
ReadU8
();
// reserved field
118
uint16_t payloadLength = i.
ReadNtohU16
();
119
120
DeserializeOptions
(i, payloadLength);
121
122
return
GetSerializedSize
();
123
}
124
125
uint16_t
126
DsrRoutingHeader::GetPayloadLength
()
const
127
{
128
// Return the actual size of the options field including padding
129
// This is the correct value for the Payload Length field per RFC 4728
130
return
static_cast<
uint16_t
>
(
GetSerializedSize
() - 4);
131
}
132
133
void
134
DsrRoutingHeader::SerializeOptions
(
Buffer::Iterator
start)
const
135
{
136
start.Write(
m_optionData
.Begin(),
m_optionData
.End());
137
DsrOptionHeader::Alignment
align = {4, 0};
138
uint32_t
fill =
CalculatePad
(align);
139
NS_LOG_LOGIC
(
"fill with "
<< fill <<
" bytes padding"
);
140
switch
(fill)
141
{
142
case
0:
143
return
;
144
case
1:
145
DsrOptionPad1Header
().
Serialize
(start);
146
return
;
147
default
:
148
DsrOptionPadnHeader
(fill).
Serialize
(start);
149
return
;
150
}
151
}
152
153
uint32_t
154
DsrRoutingHeader::DeserializeOptions
(
Buffer::Iterator
start,
uint32_t
length)
155
{
156
std::vector<uint8_t> buf(length);
157
start.Read(buf.data(), length);
158
m_optionData
=
Buffer
();
159
m_optionData
.AddAtEnd(length);
160
m_optionData
.Begin().Write(buf.data(), buf.size());
161
return
length;
162
}
163
164
void
165
DsrRoutingHeader::AddDsrOption
(
const
DsrOptionHeader
& option)
166
{
167
NS_LOG_FUNCTION_NOARGS
();
168
169
uint32_t
pad =
CalculatePad
(option.
GetAlignment
());
170
NS_LOG_LOGIC
(
"need "
<< pad <<
" bytes padding"
);
171
switch
(pad)
172
{
173
case
0:
174
break
;
// no padding needed
175
case
1:
176
AddDsrOption
(
DsrOptionPad1Header
());
177
break
;
178
default
:
179
AddDsrOption
(
DsrOptionPadnHeader
(pad));
180
break
;
181
}
182
183
m_optionData
.AddAtEnd(option.
GetSerializedSize
());
184
Buffer::Iterator
it =
m_optionData
.End();
185
it.
Prev
(option.
GetSerializedSize
());
186
option.
Serialize
(it);
187
}
188
189
uint32_t
190
DsrRoutingHeader::GetDsrOptionsOffset
()
const
191
{
192
return
4;
193
}
194
195
uint32_t
196
DsrRoutingHeader::CalculatePad
(
DsrOptionHeader::Alignment
alignment)
const
197
{
198
return
(alignment.
offset
- (
m_optionData
.GetSize() + 4)) % alignment.
factor
;
199
}
200
201
}
/* namespace dsr */
202
}
/* namespace ns3 */
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:109
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8()
Definition
buffer.h:1090
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition
buffer.h:944
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition
buffer.h:978
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16()
Definition
buffer.h:1017
ns3::Buffer::Iterator::Prev
void Prev()
go backward by one byte
Definition
buffer.h:923
ns3::Buffer
automatically resized byte buffer
Definition
buffer.h:103
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:50
ns3::TypeId::AddConstructor
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition
type-id.h:719
ns3::dsr::DsrOptionHeader
header for Dsr Options.
Definition
dsr-option-header.h:44
ns3::dsr::DsrOptionHeader::GetAlignment
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
Definition
dsr-option-header.cc:135
ns3::dsr::DsrOptionHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-option-header.cc:107
ns3::dsr::DsrOptionHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
dsr-option-header.cc:101
ns3::dsr::DsrOptionPad1Header
Header of Dsr Option Pad1.
Definition
dsr-option-header.h:144
ns3::dsr::DsrOptionPad1Header::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-option-header.cc:181
ns3::dsr::DsrOptionPadnHeader
Header of Dsr Option Padn.
Definition
dsr-option-header.h:192
ns3::dsr::DsrOptionPadnHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-option-header.cc:240
ns3::dsr::DsrRoutingHeader
Dsr fixed size header Format.
Definition
dsr-routing-header.h:56
ns3::dsr::DsrRoutingHeader::SetNextHeader
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
Definition
dsr-routing-header.cc:78
ns3::dsr::DsrRoutingHeader::AddDsrOption
void AddDsrOption(const DsrOptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
Definition
dsr-routing-header.cc:165
ns3::dsr::DsrRoutingHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition
dsr-routing-header.cc:112
ns3::dsr::DsrRoutingHeader::DsrRoutingHeader
DsrRoutingHeader()
Constructor.
Definition
dsr-routing-header.cc:60
ns3::dsr::DsrRoutingHeader::GetPayloadLength
uint16_t GetPayloadLength() const
Get the payload length of the header.
Definition
dsr-routing-header.cc:126
ns3::dsr::DsrRoutingHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
dsr-routing-header.cc:90
ns3::dsr::DsrRoutingHeader::DeserializeOptions
uint32_t DeserializeOptions(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
Definition
dsr-routing-header.cc:154
ns3::dsr::DsrRoutingHeader::~DsrRoutingHeader
~DsrRoutingHeader() override
Destructor.
Definition
dsr-routing-header.cc:66
ns3::dsr::DsrRoutingHeader::CalculatePad
uint32_t CalculatePad(DsrOptionHeader::Alignment alignment) const
Calculate padding.
Definition
dsr-routing-header.cc:196
ns3::dsr::DsrRoutingHeader::GetDsrOptionsOffset
uint32_t GetDsrOptionsOffset() const
Get the offset where the options begin, measured from the start of the extension header.
Definition
dsr-routing-header.cc:190
ns3::dsr::DsrRoutingHeader::GetNextHeader
uint8_t GetNextHeader() const
Get the next header.
Definition
dsr-routing-header.cc:84
ns3::dsr::DsrRoutingHeader::Print
void Print(std::ostream &os) const override
Print some information about the packet.
Definition
dsr-routing-header.cc:71
ns3::dsr::DsrRoutingHeader::m_nextHeader
uint8_t m_nextHeader
The "next header" field.
Definition
dsr-routing-header.h:166
ns3::dsr::DsrRoutingHeader::SerializeOptions
void SerializeOptions(Buffer::Iterator start) const
Serialize all added options.
Definition
dsr-routing-header.cc:134
ns3::dsr::DsrRoutingHeader::GetTypeId
static TypeId GetTypeId()
Get the type identificator.
Definition
dsr-routing-header.cc:45
ns3::dsr::DsrRoutingHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the instance type ID.
Definition
dsr-routing-header.cc:55
ns3::dsr::DsrRoutingHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
dsr-routing-header.cc:100
ns3::dsr::DsrRoutingHeader::m_optionData
Buffer m_optionData
Data payload.
Definition
dsr-routing-header.h:161
uint32_t
dsr-options.h
dsr-routing-header.h
dsr-routing.h
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:201
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition
log.h:281
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition
log-macros-enabled.h:176
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
ns3::dsr
Definition
dsr-errorbuff.cc:37
ns3::iana
Definition
iana-ieee802-numbers.h:29
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::dsr::DsrOptionHeader::Alignment
Represents the alignment requirements of an option header.
Definition
dsr-option-header.h:51
ns3::dsr::DsrOptionHeader::Alignment::factor
uint8_t factor
Factor.
Definition
dsr-option-header.h:52
ns3::dsr::DsrOptionHeader::Alignment::offset
uint8_t offset
Offset.
Definition
dsr-option-header.h:53
src
dsr
model
dsr-routing-header.cc
Generated on
for ns-3 by
1.16.1