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