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
ipv6-header.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2007-2008 Louis Pasteur University
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
7
*/
8
9
#include "
ipv6-header.h
"
10
11
#include "ns3/address-utils.h"
12
#include "ns3/assert.h"
13
#include "ns3/header.h"
14
#include "ns3/log.h"
15
16
namespace
ns3
17
{
18
19
NS_LOG_COMPONENT_DEFINE
(
"Ipv6Header"
);
20
21
NS_OBJECT_ENSURE_REGISTERED
(Ipv6Header);
22
23
Ipv6Header::Ipv6Header
()
24
: m_trafficClass(0),
25
m_flowLabel(1),
26
m_payloadLength(0),
27
m_nextHeader(0),
28
m_hopLimit(0)
29
{
30
SetSource
(
Ipv6Address
(
"::"
));
31
SetDestination
(
Ipv6Address
(
"::"
));
32
}
33
34
void
35
Ipv6Header::SetTrafficClass
(uint8_t traffic)
36
{
37
m_trafficClass
= traffic;
38
}
39
40
uint8_t
41
Ipv6Header::GetTrafficClass
()
const
42
{
43
return
m_trafficClass
;
44
}
45
46
void
47
Ipv6Header::SetFlowLabel
(
uint32_t
flow)
48
{
49
m_flowLabel
= flow;
50
}
51
52
uint32_t
53
Ipv6Header::GetFlowLabel
()
const
54
{
55
return
m_flowLabel
;
56
}
57
58
void
59
Ipv6Header::SetPayloadLength
(uint16_t len)
60
{
61
m_payloadLength
= len;
62
}
63
64
uint16_t
65
Ipv6Header::GetPayloadLength
()
const
66
{
67
return
m_payloadLength
;
68
}
69
70
void
71
Ipv6Header::SetNextHeader
(uint8_t next)
72
{
73
m_nextHeader
= next;
74
}
75
76
uint8_t
77
Ipv6Header::GetNextHeader
()
const
78
{
79
return
m_nextHeader
;
80
}
81
82
void
83
Ipv6Header::SetHopLimit
(uint8_t limit)
84
{
85
m_hopLimit
= limit;
86
}
87
88
uint8_t
89
Ipv6Header::GetHopLimit
()
const
90
{
91
return
m_hopLimit
;
92
}
93
94
void
95
Ipv6Header::SetSource
(
Ipv6Address
src)
96
{
97
m_sourceAddress
= src;
98
}
99
100
Ipv6Address
101
Ipv6Header::GetSource
()
const
102
{
103
return
m_sourceAddress
;
104
}
105
106
void
107
Ipv6Header::SetDestination
(
Ipv6Address
dst)
108
{
109
m_destinationAddress
= dst;
110
}
111
112
Ipv6Address
113
Ipv6Header::GetDestination
()
const
114
{
115
return
m_destinationAddress
;
116
}
117
118
TypeId
119
Ipv6Header::GetTypeId
()
120
{
121
static
TypeId
tid =
TypeId
(
"ns3::Ipv6Header"
)
122
.
SetParent
<
Header
>()
123
.SetGroupName(
"Internet"
)
124
.AddConstructor<
Ipv6Header
>();
125
return
tid;
126
}
127
128
TypeId
129
Ipv6Header::GetInstanceTypeId
()
const
130
{
131
return
GetTypeId
();
132
}
133
134
void
135
Ipv6Header::Print
(std::ostream& os)
const
136
{
137
os <<
"(Version 6 "
138
<<
"Traffic class 0x"
<< std::hex <<
m_trafficClass
<< std::dec <<
" "
139
<<
"DSCP "
<<
DscpTypeToString
(
GetDscp
()) <<
" "
140
<<
"Flow Label 0x"
<< std::hex <<
m_flowLabel
<< std::dec <<
" "
141
<<
"Payload Length "
<<
m_payloadLength
<<
" "
142
<<
"Next Header "
<< std::dec << (
uint32_t
)
m_nextHeader
<<
" "
143
<<
"Hop Limit "
<< std::dec << (
uint32_t
)
m_hopLimit
<<
" )"
<<
m_sourceAddress
<<
" > "
144
<<
m_destinationAddress
;
145
}
146
147
uint32_t
148
Ipv6Header::GetSerializedSize
()
const
149
{
150
return
10 * 4;
151
}
152
153
void
154
Ipv6Header::Serialize
(
Buffer::Iterator
start)
const
155
{
156
Buffer::Iterator
i = start;
157
uint32_t
vTcFl = 0;
/* version, Traffic Class and Flow Label fields */
158
159
vTcFl = (6 << 28) | (
m_trafficClass
<< 20) | (
m_flowLabel
);
160
161
i.
WriteHtonU32
(vTcFl);
162
i.
WriteHtonU16
(
m_payloadLength
);
163
i.
WriteU8
(
m_nextHeader
);
164
i.
WriteU8
(
m_hopLimit
);
165
166
WriteTo
(i,
m_sourceAddress
);
167
WriteTo
(i,
m_destinationAddress
);
168
}
169
170
uint32_t
171
Ipv6Header::Deserialize
(
Buffer::Iterator
start)
172
{
173
Buffer::Iterator
i = start;
174
uint32_t
vTcFl = 0;
175
176
vTcFl = i.
ReadNtohU32
();
177
if
((vTcFl >> 28) != 6)
178
{
179
NS_LOG_WARN
(
"Trying to decode a non-IPv6 header, refusing to do it."
);
180
return
0;
181
}
182
183
m_trafficClass
= (uint8_t)((vTcFl >> 20) & 0x000000ff);
184
m_flowLabel
= vTcFl & 0xfffff;
185
m_payloadLength
= i.
ReadNtohU16
();
186
m_nextHeader
= i.
ReadU8
();
187
m_hopLimit
= i.
ReadU8
();
188
189
ReadFrom
(i,
m_sourceAddress
);
190
ReadFrom
(i,
m_destinationAddress
);
191
192
return
GetSerializedSize
();
193
}
194
195
void
196
Ipv6Header::SetDscp
(
DscpType
dscp)
197
{
198
NS_LOG_FUNCTION
(
this
<< dscp);
199
m_trafficClass
&= 0x3;
// Clear out the DSCP part, retain 2 bits of ECN
200
m_trafficClass
|= (dscp << 2);
201
}
202
203
void
204
Ipv6Header::SetEcn
(
EcnType
ecn)
205
{
206
NS_LOG_FUNCTION
(
this
<< ecn);
207
m_trafficClass
&= 0xFC;
// Clear out the ECN part, retain 6 bits of DSCP
208
m_trafficClass
|= ecn;
209
}
210
211
Ipv6Header::DscpType
212
Ipv6Header::GetDscp
()
const
213
{
214
NS_LOG_FUNCTION
(
this
);
215
// Extract only first 6 bits of TOS byte, i.e 0xFC
216
return
DscpType
((
m_trafficClass
& 0xFC) >> 2);
217
}
218
219
std::string
220
Ipv6Header::DscpTypeToString
(
DscpType
dscp)
const
221
{
222
NS_LOG_FUNCTION
(
this
<< dscp);
223
switch
(dscp)
224
{
225
case
DscpDefault
:
226
return
"Default"
;
227
case
DSCP_CS1
:
228
return
"CS1"
;
229
case
DSCP_AF11
:
230
return
"AF11"
;
231
case
DSCP_AF12
:
232
return
"AF12"
;
233
case
DSCP_AF13
:
234
return
"AF13"
;
235
case
DSCP_CS2
:
236
return
"CS2"
;
237
case
DSCP_AF21
:
238
return
"AF21"
;
239
case
DSCP_AF22
:
240
return
"AF22"
;
241
case
DSCP_AF23
:
242
return
"AF23"
;
243
case
DSCP_CS3
:
244
return
"CS3"
;
245
case
DSCP_AF31
:
246
return
"AF31"
;
247
case
DSCP_AF32
:
248
return
"AF32"
;
249
case
DSCP_AF33
:
250
return
"AF33"
;
251
case
DSCP_CS4
:
252
return
"CS4"
;
253
case
DSCP_AF41
:
254
return
"AF41"
;
255
case
DSCP_AF42
:
256
return
"AF42"
;
257
case
DSCP_AF43
:
258
return
"AF43"
;
259
case
DSCP_CS5
:
260
return
"CS5"
;
261
case
DSCP_EF
:
262
return
"EF"
;
263
case
DSCP_CS6
:
264
return
"CS6"
;
265
case
DSCP_CS7
:
266
return
"CS7"
;
267
default
:
268
return
"Unrecognized DSCP"
;
269
};
270
}
271
272
Ipv6Header::EcnType
273
Ipv6Header::GetEcn
()
const
274
{
275
NS_LOG_FUNCTION
(
this
);
276
// Extract only last 2 bits of Traffic Class byte, i.e 0x3
277
return
EcnType
(
m_trafficClass
& 0x3);
278
}
279
280
std::string
281
Ipv6Header::EcnTypeToString
(
EcnType
ecn)
const
282
{
283
NS_LOG_FUNCTION
(
this
<< ecn);
284
switch
(ecn)
285
{
286
case
ECN_NotECT
:
287
return
"Not-ECT"
;
288
case
ECN_ECT1
:
289
return
"ECT (1)"
;
290
case
ECN_ECT0
:
291
return
"ECT (0)"
;
292
case
ECN_CE
:
293
return
"CE"
;
294
default
:
295
return
"Unknown ECN codepoint"
;
296
};
297
}
298
299
}
/* 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::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition
buffer.h:904
ns3::Buffer::Iterator::ReadNtohU32
uint32_t ReadNtohU32()
Definition
buffer.h:967
ns3::Buffer::Iterator::WriteHtonU32
void WriteHtonU32(uint32_t data)
Definition
buffer.h:922
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16()
Definition
buffer.h:943
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:33
ns3::Ipv6Address
Describes an IPv6 address.
Definition
ipv6-address.h:38
ns3::Ipv6Header
Packet header for IPv6.
Definition
ipv6-header.h:24
ns3::Ipv6Header::SetDestination
void SetDestination(Ipv6Address dst)
Set the "Destination address" field.
Definition
ipv6-header.cc:107
ns3::Ipv6Header::GetFlowLabel
uint32_t GetFlowLabel() const
Get the "Flow label" field.
Definition
ipv6-header.cc:53
ns3::Ipv6Header::SetEcn
void SetEcn(EcnType ecn)
Set ECN field bits.
Definition
ipv6-header.cc:204
ns3::Ipv6Header::GetDscp
DscpType GetDscp() const
Definition
ipv6-header.cc:212
ns3::Ipv6Header::m_destinationAddress
Ipv6Address m_destinationAddress
The destination address.
Definition
ipv6-header.h:297
ns3::Ipv6Header::SetSource
void SetSource(Ipv6Address src)
Set the "Source address" field.
Definition
ipv6-header.cc:95
ns3::Ipv6Header::Print
void Print(std::ostream &os) const override
Print some information about the packet.
Definition
ipv6-header.cc:135
ns3::Ipv6Header::GetHopLimit
uint8_t GetHopLimit() const
Get the "Hop limit" field (TTL).
Definition
ipv6-header.cc:89
ns3::Ipv6Header::GetNextHeader
uint8_t GetNextHeader() const
Get the next header.
Definition
ipv6-header.cc:77
ns3::Ipv6Header::GetTypeId
static TypeId GetTypeId()
Get the type identifier.
Definition
ipv6-header.cc:119
ns3::Ipv6Header::SetHopLimit
void SetHopLimit(uint8_t limit)
Set the "Hop limit" field (TTL).
Definition
ipv6-header.cc:83
ns3::Ipv6Header::GetDestination
Ipv6Address GetDestination() const
Get the "Destination address" field.
Definition
ipv6-header.cc:113
ns3::Ipv6Header::GetPayloadLength
uint16_t GetPayloadLength() const
Get the "Payload length" field.
Definition
ipv6-header.cc:65
ns3::Ipv6Header::GetTrafficClass
uint8_t GetTrafficClass() const
Get the "Traffic class" field.
Definition
ipv6-header.cc:41
ns3::Ipv6Header::m_flowLabel
uint32_t m_flowLabel
The flow label.
Definition
ipv6-header.h:272
ns3::Ipv6Header::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
Definition
ipv6-header.cc:129
ns3::Ipv6Header::GetEcn
EcnType GetEcn() const
Definition
ipv6-header.cc:273
ns3::Ipv6Header::SetPayloadLength
void SetPayloadLength(uint16_t len)
Set the "Payload length" field.
Definition
ipv6-header.cc:59
ns3::Ipv6Header::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
ipv6-header.cc:148
ns3::Ipv6Header::SetFlowLabel
void SetFlowLabel(uint32_t flow)
Set the "Flow label" field.
Definition
ipv6-header.cc:47
ns3::Ipv6Header::Ipv6Header
Ipv6Header()
Constructor.
Definition
ipv6-header.cc:23
ns3::Ipv6Header::m_sourceAddress
Ipv6Address m_sourceAddress
The source address.
Definition
ipv6-header.h:292
ns3::Ipv6Header::GetSource
Ipv6Address GetSource() const
Get the "Source address" field.
Definition
ipv6-header.cc:101
ns3::Ipv6Header::EcnType
EcnType
ECN field bits.
Definition
ipv6-header.h:140
ns3::Ipv6Header::ECN_NotECT
@ ECN_NotECT
Definition
ipv6-header.h:142
ns3::Ipv6Header::ECN_ECT1
@ ECN_ECT1
Definition
ipv6-header.h:143
ns3::Ipv6Header::ECN_ECT0
@ ECN_ECT0
Definition
ipv6-header.h:144
ns3::Ipv6Header::ECN_CE
@ ECN_CE
Definition
ipv6-header.h:145
ns3::Ipv6Header::m_payloadLength
uint16_t m_payloadLength
The payload length.
Definition
ipv6-header.h:277
ns3::Ipv6Header::m_nextHeader
uint8_t m_nextHeader
The Next header number.
Definition
ipv6-header.h:282
ns3::Ipv6Header::DscpTypeToString
std::string DscpTypeToString(DscpType dscp) const
Definition
ipv6-header.cc:220
ns3::Ipv6Header::EcnTypeToString
std::string EcnTypeToString(EcnType ecn) const
Definition
ipv6-header.cc:281
ns3::Ipv6Header::SetTrafficClass
void SetTrafficClass(uint8_t traffic)
Set the "Traffic class" field.
Definition
ipv6-header.cc:35
ns3::Ipv6Header::SetDscp
void SetDscp(DscpType dscp)
Set DSCP Field.
Definition
ipv6-header.cc:196
ns3::Ipv6Header::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
ipv6-header.cc:154
ns3::Ipv6Header::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition
ipv6-header.cc:171
ns3::Ipv6Header::SetNextHeader
void SetNextHeader(uint8_t next)
Set the "Next header" field.
Definition
ipv6-header.cc:71
ns3::Ipv6Header::m_trafficClass
uint32_t m_trafficClass
The traffic class.
Definition
ipv6-header.h:266
ns3::Ipv6Header::m_hopLimit
uint8_t m_hopLimit
The Hop limit value.
Definition
ipv6-header.h:287
ns3::Ipv6Header::DscpType
DscpType
DiffServ Code Points Code Points defined in Assured Forwarding (AF) RFC 2597 Expedited Forwarding (EF...
Definition
ipv6-header.h:35
ns3::Ipv6Header::DSCP_AF43
@ DSCP_AF43
Definition
ipv6-header.h:57
ns3::Ipv6Header::DSCP_AF23
@ DSCP_AF23
Definition
ipv6-header.h:47
ns3::Ipv6Header::DSCP_AF32
@ DSCP_AF32
Definition
ipv6-header.h:51
ns3::Ipv6Header::DSCP_AF21
@ DSCP_AF21
Definition
ipv6-header.h:45
ns3::Ipv6Header::DSCP_AF11
@ DSCP_AF11
Definition
ipv6-header.h:40
ns3::Ipv6Header::DscpDefault
@ DscpDefault
Definition
ipv6-header.h:36
ns3::Ipv6Header::DSCP_AF13
@ DSCP_AF13
Definition
ipv6-header.h:42
ns3::Ipv6Header::DSCP_CS2
@ DSCP_CS2
Definition
ipv6-header.h:44
ns3::Ipv6Header::DSCP_CS7
@ DSCP_CS7
Definition
ipv6-header.h:63
ns3::Ipv6Header::DSCP_CS3
@ DSCP_CS3
Definition
ipv6-header.h:49
ns3::Ipv6Header::DSCP_CS6
@ DSCP_CS6
Definition
ipv6-header.h:62
ns3::Ipv6Header::DSCP_CS1
@ DSCP_CS1
Definition
ipv6-header.h:39
ns3::Ipv6Header::DSCP_AF41
@ DSCP_AF41
Definition
ipv6-header.h:55
ns3::Ipv6Header::DSCP_AF42
@ DSCP_AF42
Definition
ipv6-header.h:56
ns3::Ipv6Header::DSCP_EF
@ DSCP_EF
Definition
ipv6-header.h:60
ns3::Ipv6Header::DSCP_AF22
@ DSCP_AF22
Definition
ipv6-header.h:46
ns3::Ipv6Header::DSCP_AF33
@ DSCP_AF33
Definition
ipv6-header.h:52
ns3::Ipv6Header::DSCP_AF12
@ DSCP_AF12
Definition
ipv6-header.h:41
ns3::Ipv6Header::DSCP_CS5
@ DSCP_CS5
Definition
ipv6-header.h:59
ns3::Ipv6Header::DSCP_AF31
@ DSCP_AF31
Definition
ipv6-header.h:50
ns3::Ipv6Header::DSCP_CS4
@ DSCP_CS4
Definition
ipv6-header.h:54
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
uint32_t
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition
log-macros-enabled.h:229
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition
log.h:250
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
ipv6-header.h
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::WriteTo
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
Definition
address-utils.cc:21
ns3::ReadFrom
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
Definition
address-utils.cc:74
src
internet
model
ipv6-header.cc
Generated on Sat Nov 2 2024 19:37:24 for ns-3 by
1.11.0