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 
27 namespace ns3
28 {
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("::"));
43 }
44 
45 void
47 {
48  m_trafficClass = traffic;
49 }
50 
51 uint8_t
53 {
54  return m_trafficClass;
55 }
56 
57 void
59 {
60  m_flowLabel = flow;
61 }
62 
63 uint32_t
65 {
66  return m_flowLabel;
67 }
68 
69 void
71 {
72  m_payloadLength = len;
73 }
74 
75 uint16_t
77 {
78  return m_payloadLength;
79 }
80 
81 void
83 {
84  m_nextHeader = next;
85 }
86 
87 uint8_t
89 {
90  return m_nextHeader;
91 }
92 
93 void
95 {
96  m_hopLimit = limit;
97 }
98 
99 uint8_t
101 {
102  return m_hopLimit;
103 }
104 
105 void
107 {
108  m_sourceAddress = src;
109 }
110 
113 {
114  return m_sourceAddress;
115 }
116 
117 void
119 {
120  m_destinationAddress = dst;
121 }
122 
125 {
126  return m_destinationAddress;
127 }
128 
129 TypeId
131 {
132  static TypeId tid = TypeId("ns3::Ipv6Header")
133  .SetParent<Header>()
134  .SetGroupName("Internet")
135  .AddConstructor<Ipv6Header>();
136  return tid;
137 }
138 
139 TypeId
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 << " > "
156 }
157 
158 uint32_t
160 {
161  return 10 * 4;
162 }
163 
164 void
166 {
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);
175  i.WriteU8(m_hopLimit);
176 
179 }
180 
181 uint32_t
183 {
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;
197  m_nextHeader = i.ReadU8();
198  m_hopLimit = i.ReadU8();
199 
202 
203  return GetSerializedSize();
204 }
205 
206 void
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
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 
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
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 
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
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 */
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
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Packet header for IPv6.
Definition: ipv6-header.h:35
void SetDestination(Ipv6Address dst)
Set the "Destination address" field.
Definition: ipv6-header.cc:118
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:215
DscpType GetDscp() const
Definition: ipv6-header.cc:223
Ipv6Address m_destinationAddress
The destination address.
Definition: ipv6-header.h:308
void SetSource(Ipv6Address src)
Set the "Source address" field.
Definition: ipv6-header.cc:106
void Print(std::ostream &os) const override
Print some information about the packet.
Definition: ipv6-header.cc:146
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:130
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:124
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:283
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
Definition: ipv6-header.cc:140
EcnType GetEcn() const
Definition: ipv6-header.cc:284
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:159
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:303
Ipv6Address GetSource() const
Get the "Source address" field.
Definition: ipv6-header.cc:112
EcnType
ECN field bits.
Definition: ipv6-header.h:151
uint16_t m_payloadLength
The payload length.
Definition: ipv6-header.h:288
uint8_t m_nextHeader
The Next header number.
Definition: ipv6-header.h:293
std::string DscpTypeToString(DscpType dscp) const
Definition: ipv6-header.cc:231
std::string EcnTypeToString(EcnType ecn) const
Definition: ipv6-header.cc:292
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:207
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition: ipv6-header.cc:165
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:277
uint8_t m_hopLimit
The Hop limit value.
Definition: ipv6-header.h:298
DscpType
DiffServ Code Points Code Points defined in Assured Forwarding (AF) RFC 2597 Expedited Forwarding (EF...
Definition: ipv6-header.h:46
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:46
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.