A Discrete-Event Network Simulator
API
ipv6-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2008 Louis Pasteur University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 #include "ns3/header.h"
24 
25 #include "ns3/address-utils.h"
26 #include "ipv6-header.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("Ipv6Header");
31 
32 NS_OBJECT_ENSURE_REGISTERED (Ipv6Header);
33 
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 Ipv6Header::SetTrafficClass (uint8_t traffic)
46 {
47  m_trafficClass = traffic;
48 }
49 
51 {
52  return m_trafficClass;
53 }
54 
55 void Ipv6Header::SetFlowLabel (uint32_t flow)
56 {
57  m_flowLabel = flow;
58 }
59 
60 uint32_t Ipv6Header::GetFlowLabel () const
61 {
62  return m_flowLabel;
63 }
64 
65 void Ipv6Header::SetPayloadLength (uint16_t len)
66 {
67  m_payloadLength = len;
68 }
69 
71 {
72  return m_payloadLength;
73 }
74 
75 void Ipv6Header::SetNextHeader (uint8_t next)
76 {
77  m_nextHeader = next;
78 }
79 
80 uint8_t Ipv6Header::GetNextHeader () const
81 {
82  return m_nextHeader;
83 }
84 
85 void Ipv6Header::SetHopLimit (uint8_t limit)
86 {
87  m_hopLimit = limit;
88 }
89 
90 uint8_t Ipv6Header::GetHopLimit () const
91 {
92  return m_hopLimit;
93 }
94 
96 {
97  m_sourceAddress = src;
98 }
99 
101 {
102  SetSource (src);
103 }
104 
106 {
107  return m_sourceAddress;
108 }
109 
111 {
112  return GetSource ();
113 }
114 
116 {
117  m_destinationAddress = dst;
118 }
119 
121 {
122  SetDestination (dst);
123 }
124 
126 {
127  return m_destinationAddress;
128 }
129 
131 {
132  return GetDestination ();
133 }
134 
136 {
137  static TypeId tid = TypeId ("ns3::Ipv6Header")
138  .SetParent<Header> ()
139  .SetGroupName ("Internet")
140  .AddConstructor<Ipv6Header> ()
141  ;
142  return tid;
143 }
144 
146 {
147  return GetTypeId ();
148 }
149 
150 void Ipv6Header::Print (std::ostream& os) const
151 {
152  os << "(Version 6 "
153  << "Traffic class 0x" << std::hex << m_trafficClass << std::dec << " "
154  << "DSCP " << DscpTypeToString (GetDscp ()) << " "
155  << "Flow Label 0x" << std::hex << m_flowLabel << std::dec << " "
156  << "Payload Length " << m_payloadLength << " "
157  << "Next Header " << std::dec << (uint32_t) m_nextHeader << " "
158  << "Hop Limit " << std::dec << (uint32_t)m_hopLimit << " )"
160  ;
161 }
162 
164 {
165  return 10 * 4;
166 }
167 
169 {
171  uint32_t vTcFl = 0; /* version, Traffic Class and Flow Label fields */
172 
173  vTcFl = (6 << 28) | (m_trafficClass << 20) | (m_flowLabel);
174 
175  i.WriteHtonU32 (vTcFl);
177  i.WriteU8 (m_nextHeader);
178  i.WriteU8 (m_hopLimit);
179 
182 }
183 
185 {
187  uint32_t vTcFl = 0;
188 
189  vTcFl = i.ReadNtohU32 ();
190  if ((vTcFl >> 28) != 6)
191  {
192  NS_LOG_WARN ("Trying to decode a non-IPv6 header, refusing to do it.");
193  return 0;
194  }
195 
196  m_trafficClass = (uint8_t)((vTcFl >> 20) & 0x000000ff);
197  m_flowLabel = vTcFl & 0xfff00000;
199  m_nextHeader = i.ReadU8 ();
200  m_hopLimit = i.ReadU8 ();
201 
204 
205  return GetSerializedSize ();
206 }
207 
209 {
210  NS_LOG_FUNCTION (this << dscp);
211  m_trafficClass &= 0x3; // Clear out the DSCP part, retain 2 bits of ECN
212  m_trafficClass |= (dscp << 2);
213 }
214 
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 
223 {
224  NS_LOG_FUNCTION (this);
225  // Extract only first 6 bits of TOS byte, i.e 0xFC
226  return DscpType ((m_trafficClass & 0xFC) >> 2);
227 }
228 
229 std::string Ipv6Header::DscpTypeToString (DscpType dscp) const
230 {
231  NS_LOG_FUNCTION (this << dscp);
232  switch (dscp)
233  {
234  case DscpDefault:
235  return "Default";
236  case DSCP_CS1:
237  return "CS1";
238  case DSCP_AF11:
239  return "AF11";
240  case DSCP_AF12:
241  return "AF12";
242  case DSCP_AF13:
243  return "AF13";
244  case DSCP_CS2:
245  return "CS2";
246  case DSCP_AF21:
247  return "AF21";
248  case DSCP_AF22:
249  return "AF22";
250  case DSCP_AF23:
251  return "AF23";
252  case DSCP_CS3:
253  return "CS3";
254  case DSCP_AF31:
255  return "AF31";
256  case DSCP_AF32:
257  return "AF32";
258  case DSCP_AF33:
259  return "AF33";
260  case DSCP_CS4:
261  return "CS4";
262  case DSCP_AF41:
263  return "AF41";
264  case DSCP_AF42:
265  return "AF42";
266  case DSCP_AF43:
267  return "AF43";
268  case DSCP_CS5:
269  return "CS5";
270  case DSCP_EF:
271  return "EF";
272  case DSCP_CS6:
273  return "CS6";
274  case DSCP_CS7:
275  return "CS7";
276  default:
277  return "Unrecognized DSCP";
278  };
279 }
280 
282 Ipv6Header::GetEcn (void) const
283 {
284  NS_LOG_FUNCTION (this);
285  // Extract only last 2 bits of Traffic Class byte, i.e 0x3
286  return EcnType (m_trafficClass & 0x3);
287 }
288 
289 std::string Ipv6Header::EcnTypeToString (EcnType ecn) const
290 {
291  NS_LOG_FUNCTION (this << ecn);
292  switch (ecn)
293  {
294  case ECN_NotECT:
295  return "Not-ECT";
296  case ECN_ECT1:
297  return "ECT (1)";
298  case ECN_ECT0:
299  return "ECT (0)";
300  case ECN_CE:
301  return "CE";
302  default:
303  return "Unknown ECN codepoint";
304  };
305 }
306 
307 } /* namespace ns3 */
308 
ns3::Ipv6Header::DSCP_CS2
@ DSCP_CS2
Definition: ipv6-header.h:56
ns3::Ipv6Header::ECN_ECT1
@ ECN_ECT1
Definition: ipv6-header.h:156
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::Ipv6Header::Ipv6Header
Ipv6Header(void)
Constructor.
Definition: ipv6-header.cc:34
ns3::Ipv6Header::DSCP_AF21
@ DSCP_AF21
Definition: ipv6-header.h:57
ns3::Ipv6Header::DscpType
DscpType
DiffServ Code Points Code Points defined in Assured Forwarding (AF) RFC 2597 Expedited Forwarding (EF...
Definition: ipv6-header.h:47
ns3::Ipv6Header::SetSourceAddress
NS_DEPRECATED_3_35 void SetSourceAddress(Ipv6Address src)
Set the "Source address" field.
Definition: ipv6-header.cc:100
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::Ipv6Header::DSCP_CS5
@ DSCP_CS5
Definition: ipv6-header.h:71
ns3::Ipv6Header::SetHopLimit
void SetHopLimit(uint8_t limit)
Set the "Hop limit" field (TTL).
Definition: ipv6-header.cc:85
ns3::Ipv6Header::SetTrafficClass
void SetTrafficClass(uint8_t traffic)
Set the "Traffic class" field.
Definition: ipv6-header.cc:45
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv6Header::DSCP_AF41
@ DSCP_AF41
Definition: ipv6-header.h:67
ns3::Ipv6Header::DscpTypeToString
std::string DscpTypeToString(DscpType dscp) const
Definition: ipv6-header.cc:229
ns3::Ipv6Header::DSCP_AF13
@ DSCP_AF13
Definition: ipv6-header.h:54
ns3::Ipv6Header::DSCP_AF22
@ DSCP_AF22
Definition: ipv6-header.h:58
ns3::Ipv6Header::DSCP_CS7
@ DSCP_CS7
Definition: ipv6-header.h:75
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8(void)
Definition: buffer.h:1021
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16(void)
Definition: buffer.h:946
ns3::Ipv6Header::DSCP_EF
@ DSCP_EF
Definition: ipv6-header.h:72
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
ns3::Ipv6Header::GetEcn
EcnType GetEcn(void) const
Definition: ipv6-header.cc:282
ns3::Ipv6Header::DSCP_AF33
@ DSCP_AF33
Definition: ipv6-header.h:64
ns3::Ipv6Header::ECN_NotECT
@ ECN_NotECT
Definition: ipv6-header.h:155
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition: buffer.h:869
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::Ipv6Header::GetTypeId
static TypeId GetTypeId(void)
Get the type identifier.
Definition: ipv6-header.cc:135
ns3::Ipv6Header::m_nextHeader
uint8_t m_nextHeader
The Next header number.
Definition: ipv6-header.h:327
ns3::Ipv6Header::DSCP_AF11
@ DSCP_AF11
Definition: ipv6-header.h:52
ns3::Ipv6Header::GetFlowLabel
uint32_t GetFlowLabel(void) const
Get the "Flow label" field.
Definition: ipv6-header.cc:60
ns3::Ipv6Header::GetDestinationAddress
NS_DEPRECATED_3_35 Ipv6Address GetDestinationAddress(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:130
ns3::Ipv6Header::m_sourceAddress
Ipv6Address m_sourceAddress
The source address.
Definition: ipv6-header.h:337
ns3::Ipv6Header
Packet header for IPv6.
Definition: ipv6-header.h:36
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition: buffer.h:905
ns3::Ipv6Header::m_trafficClass
uint32_t m_trafficClass
The traffic class.
Definition: ipv6-header.h:311
ns3::Ipv6Header::SetDestination
void SetDestination(Ipv6Address dst)
Set the "Destination address" field.
Definition: ipv6-header.cc:115
ipv6-header.h
ns3::Ipv6Header::GetTrafficClass
uint8_t GetTrafficClass(void) const
Get the "Traffic class" field.
Definition: ipv6-header.cc:50
ns3::Ipv6Header::Serialize
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: ipv6-header.cc:168
visualizer.core.start
def start()
Definition: core.py:1855
ns3::Ipv6Header::ECN_ECT0
@ ECN_ECT0
Definition: ipv6-header.h:157
ns3::Ipv6Header::DSCP_CS6
@ DSCP_CS6
Definition: ipv6-header.h:74
ns3::Ipv6Header::m_payloadLength
uint16_t m_payloadLength
The payload length.
Definition: ipv6-header.h:322
ns3::Ipv6Header::DSCP_AF43
@ DSCP_AF43
Definition: ipv6-header.h:69
ns3::Ipv6Header::ECN_CE
@ ECN_CE
Definition: ipv6-header.h:158
ns3::Ipv6Header::DSCP_CS4
@ DSCP_CS4
Definition: ipv6-header.h:66
ns3::Ipv6Header::SetPayloadLength
void SetPayloadLength(uint16_t len)
Set the "Payload length" field.
Definition: ipv6-header.cc:65
ns3::Ipv6Header::m_destinationAddress
Ipv6Address m_destinationAddress
The destination address.
Definition: ipv6-header.h:342
ns3::Ipv6Header::EcnTypeToString
std::string EcnTypeToString(EcnType ecn) const
Definition: ipv6-header.cc:289
ns3::Ipv6Header::DscpDefault
@ DscpDefault
Definition: ipv6-header.h:48
ns3::Ipv6Header::Print
virtual void Print(std::ostream &os) const
Print some information about the packet.
Definition: ipv6-header.cc:150
ns3::Header
Protocol header serialization and deserialization.
Definition: header.h:43
ns3::Ipv6Header::SetDestinationAddress
NS_DEPRECATED_3_35 void SetDestinationAddress(Ipv6Address dst)
Set the "Destination address" field.
Definition: ipv6-header.cc:120
ns3::Ipv6Header::DSCP_AF12
@ DSCP_AF12
Definition: ipv6-header.h:53
ns3::Ipv6Header::DSCP_AF23
@ DSCP_AF23
Definition: ipv6-header.h:59
ns3::Ipv6Header::m_hopLimit
uint8_t m_hopLimit
The Hop limit value.
Definition: ipv6-header.h:332
ns3::Ipv6Header::SetDscp
void SetDscp(DscpType dscp)
Set DSCP Field.
Definition: ipv6-header.cc:208
ns3::Header::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
ns3::Ipv6Header::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ipv6-header.cc:163
ns3::Ipv6Header::DSCP_CS1
@ DSCP_CS1
Definition: ipv6-header.h:51
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition: buffer.h:99
ns3::Ipv6Header::DSCP_AF31
@ DSCP_AF31
Definition: ipv6-header.h:62
ns3::Ipv6Header::GetHopLimit
uint8_t GetHopLimit(void) const
Get the "Hop limit" field (TTL).
Definition: ipv6-header.cc:90
ns3::Ipv6Header::GetSourceAddress
NS_DEPRECATED_3_35 Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:110
ns3::Ipv6Header::GetDscp
DscpType GetDscp(void) const
Definition: ipv6-header.cc:222
ns3::Ipv6Header::GetNextHeader
uint8_t GetNextHeader(void) const
Get the next header.
Definition: ipv6-header.cc:80
ns3::Ipv6Header::SetNextHeader
void SetNextHeader(uint8_t next)
Set the "Next header" field.
Definition: ipv6-header.cc:75
ns3::Ipv6Header::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: ipv6-header.cc:145
ns3::WriteTo
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
Definition: address-utils.cc:28
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:244
ns3::Ipv6Header::GetSource
Ipv6Address GetSource(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:105
ns3::Ipv6Header::DSCP_AF42
@ DSCP_AF42
Definition: ipv6-header.h:68
ns3::Ipv6Header::SetSource
void SetSource(Ipv6Address src)
Set the "Source address" field.
Definition: ipv6-header.cc:95
ns3::Ipv6Header::GetDestination
Ipv6Address GetDestination(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:125
ns3::Ipv6Header::DSCP_CS3
@ DSCP_CS3
Definition: ipv6-header.h:61
ns3::Ipv6Header::SetEcn
void SetEcn(EcnType ecn)
Set ECN field bits.
Definition: ipv6-header.cc:215
ns3::Ipv6Header::EcnType
EcnType
ECN field bits.
Definition: ipv6-header.h:153
ns3::Buffer::Iterator::ReadNtohU32
uint32_t ReadNtohU32(void)
Definition: buffer.h:970
ns3::Ipv6Header::GetPayloadLength
uint16_t GetPayloadLength(void) const
Get the "Payload length" field.
Definition: ipv6-header.cc:70
ns3::Ipv6Header::SetFlowLabel
void SetFlowLabel(uint32_t flow)
Set the "Flow label" field.
Definition: ipv6-header.cc:55
ns3::Ipv6Header::m_flowLabel
uint32_t m_flowLabel
The flow label.
Definition: ipv6-header.h:317
ns3::Buffer::Iterator::WriteHtonU32
void WriteHtonU32(uint32_t data)
Definition: buffer.h:924
ns3::Ipv6Header::DSCP_AF32
@ DSCP_AF32
Definition: ipv6-header.h:63
ns3::ReadFrom
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
Definition: address-utils.cc:70