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