A Discrete-Event Network Simulator
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
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("Ipv6Header");
20
22
24 : m_trafficClass(0),
25 m_flowLabel(1),
26 m_payloadLength(0),
27 m_nextHeader(0),
28 m_hopLimit(0)
29{
32}
33
34void
36{
37 m_trafficClass = traffic;
38}
39
40uint8_t
45
46void
51
54{
55 return m_flowLabel;
56}
57
58void
60{
61 m_payloadLength = len;
62}
63
64uint16_t
69
70void
72{
73 m_nextHeader = next;
74}
75
76uint8_t
78{
79 return m_nextHeader;
80}
81
82void
84{
85 m_hopLimit = limit;
86}
87
88uint8_t
90{
91 return m_hopLimit;
92}
93
94void
99
102{
103 return m_sourceAddress;
104}
105
106void
111
117
118TypeId
120{
121 static TypeId tid = TypeId("ns3::Ipv6Header")
122 .SetParent<Header>()
123 .SetGroupName("Internet")
124 .AddConstructor<Ipv6Header>();
125 return tid;
126}
127
128TypeId
130{
131 return GetTypeId();
132}
133
134void
135Ipv6Header::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 << " > "
145}
146
149{
150 return 10 * 4;
151}
152
153void
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);
165
168}
169
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;
186 m_nextHeader = i.ReadU8();
187 m_hopLimit = i.ReadU8();
188
191
192 return GetSerializedSize();
193}
194
195void
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
203void
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
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
219std::string
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
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
280std::string
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 */
iterator in a Buffer instance
Definition buffer.h:89
void WriteU8(uint8_t data)
Definition buffer.h:870
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint32_t ReadNtohU32()
Definition buffer.h:967
void WriteHtonU32(uint32_t data)
Definition buffer.h:922
uint16_t ReadNtohU16()
Definition buffer.h:943
Protocol header serialization and deserialization.
Definition header.h:33
Describes an IPv6 address.
Packet header for IPv6.
Definition ipv6-header.h:24
void SetDestination(Ipv6Address dst)
Set the "Destination address" field.
uint32_t GetFlowLabel() const
Get the "Flow label" field.
void SetEcn(EcnType ecn)
Set ECN field bits.
DscpType GetDscp() const
Ipv6Address m_destinationAddress
The destination address.
void SetSource(Ipv6Address src)
Set the "Source address" field.
void Print(std::ostream &os) const override
Print some information about the packet.
uint8_t GetHopLimit() const
Get the "Hop limit" field (TTL).
uint8_t GetNextHeader() const
Get the next header.
static TypeId GetTypeId()
Get the type identifier.
void SetHopLimit(uint8_t limit)
Set the "Hop limit" field (TTL).
Ipv6Address GetDestination() const
Get the "Destination address" field.
uint16_t GetPayloadLength() const
Get the "Payload length" field.
uint8_t GetTrafficClass() const
Get the "Traffic class" field.
uint32_t m_flowLabel
The flow label.
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
EcnType GetEcn() const
void SetPayloadLength(uint16_t len)
Set the "Payload length" field.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void SetFlowLabel(uint32_t flow)
Set the "Flow label" field.
Ipv6Header()
Constructor.
Ipv6Address m_sourceAddress
The source address.
Ipv6Address GetSource() const
Get the "Source address" field.
EcnType
ECN field bits.
uint16_t m_payloadLength
The payload length.
uint8_t m_nextHeader
The Next header number.
std::string DscpTypeToString(DscpType dscp) const
std::string EcnTypeToString(EcnType ecn) const
void SetTrafficClass(uint8_t traffic)
Set the "Traffic class" field.
void SetDscp(DscpType dscp)
Set DSCP Field.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
void SetNextHeader(uint8_t next)
Set the "Next header" field.
uint32_t m_trafficClass
The traffic class.
uint8_t m_hopLimit
The Hop limit value.
DscpType
DiffServ Code Points Code Points defined in Assured Forwarding (AF) RFC 2597 Expedited Forwarding (EF...
Definition ipv6-header.h:35
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
#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.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.