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 {
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  return m_sourceAddress;
103 }
104 
106 {
107  m_destinationAddress = dst;
108 }
109 
111 {
112  return m_destinationAddress;
113 }
114 
116 {
117  static TypeId tid = TypeId ("ns3::Ipv6Header")
118  .SetParent<Header> ()
119  .SetGroupName ("Internet")
120  .AddConstructor<Ipv6Header> ()
121  ;
122  return tid;
123 }
124 
126 {
127  return GetTypeId ();
128 }
129 
130 void Ipv6Header::Print (std::ostream& os) const
131 {
132  os << "(Version 6 "
133  << "Traffic class 0x" << std::hex << m_trafficClass << std::dec << " "
134  << "DSCP " << DscpTypeToString (GetDscp ()) << " "
135  << "Flow Label 0x" << std::hex << m_flowLabel << std::dec << " "
136  << "Payload Length " << m_payloadLength << " "
137  << "Next Header " << std::dec << (uint32_t) m_nextHeader << " "
138  << "Hop Limit " << std::dec << (uint32_t)m_hopLimit << " )"
140  ;
141 }
142 
144 {
145  return 10 * 4;
146 }
147 
149 {
151  uint32_t vTcFl = 0; /* version, Traffic Class and Flow Label fields */
152 
153  vTcFl = (6 << 28) | (m_trafficClass << 20) | (m_flowLabel);
154 
155  i.WriteHtonU32 (vTcFl);
157  i.WriteU8 (m_nextHeader);
158  i.WriteU8 (m_hopLimit);
159 
162 }
163 
165 {
167  uint32_t vTcFl = 0;
168 
169  vTcFl = i.ReadNtohU32 ();
170  if ((vTcFl >> 28) != 6)
171  {
172  NS_LOG_WARN ("Trying to decode a non-IPv6 header, refusing to do it.");
173  return 0;
174  }
175 
176  m_trafficClass = (uint8_t)((vTcFl >> 20) & 0x000000ff);
177  m_flowLabel = vTcFl & 0xfff00000;
179  m_nextHeader = i.ReadU8 ();
180  m_hopLimit = i.ReadU8 ();
181 
184 
185  return GetSerializedSize ();
186 }
187 
189 {
190  NS_LOG_FUNCTION (this << dscp);
191  m_trafficClass &= 0x3; // Clear out the DSCP part, retain 2 bits of ECN
192  m_trafficClass |= (dscp << 2);
193 }
194 
196 {
197  NS_LOG_FUNCTION (this << ecn);
198  m_trafficClass &= 0xFC; // Clear out the ECN part, retain 6 bits of DSCP
199  m_trafficClass |= ecn;
200 }
201 
203 {
204  NS_LOG_FUNCTION (this);
205  // Extract only first 6 bits of TOS byte, i.e 0xFC
206  return DscpType ((m_trafficClass & 0xFC) >> 2);
207 }
208 
209 std::string Ipv6Header::DscpTypeToString (DscpType dscp) const
210 {
211  NS_LOG_FUNCTION (this << dscp);
212  switch (dscp)
213  {
214  case DscpDefault:
215  return "Default";
216  case DSCP_CS1:
217  return "CS1";
218  case DSCP_AF11:
219  return "AF11";
220  case DSCP_AF12:
221  return "AF12";
222  case DSCP_AF13:
223  return "AF13";
224  case DSCP_CS2:
225  return "CS2";
226  case DSCP_AF21:
227  return "AF21";
228  case DSCP_AF22:
229  return "AF22";
230  case DSCP_AF23:
231  return "AF23";
232  case DSCP_CS3:
233  return "CS3";
234  case DSCP_AF31:
235  return "AF31";
236  case DSCP_AF32:
237  return "AF32";
238  case DSCP_AF33:
239  return "AF33";
240  case DSCP_CS4:
241  return "CS4";
242  case DSCP_AF41:
243  return "AF41";
244  case DSCP_AF42:
245  return "AF42";
246  case DSCP_AF43:
247  return "AF43";
248  case DSCP_CS5:
249  return "CS5";
250  case DSCP_EF:
251  return "EF";
252  case DSCP_CS6:
253  return "CS6";
254  case DSCP_CS7:
255  return "CS7";
256  default:
257  return "Unrecognized DSCP";
258  };
259 }
260 
262 Ipv6Header::GetEcn (void) const
263 {
264  NS_LOG_FUNCTION (this);
265  // Extract only last 2 bits of Traffic Class byte, i.e 0x3
266  return EcnType (m_trafficClass & 0x3);
267 }
268 
269 std::string Ipv6Header::EcnTypeToString (EcnType ecn) const
270 {
271  NS_LOG_FUNCTION (this << ecn);
272  switch (ecn)
273  {
274  case ECN_NotECT:
275  return "Not-ECT";
276  case ECN_ECT1:
277  return "ECT (1)";
278  case ECN_ECT0:
279  return "ECT (0)";
280  case ECN_CE:
281  return "CE";
282  default:
283  return "Unknown ECN codepoint";
284  };
285 }
286 
287 } /* namespace ns3 */
288 
Protocol header serialization and deserialization.
Definition: header.h:42
void SetPayloadLength(uint16_t len)
Set the "Payload length" field.
Definition: ipv6-header.cc:65
uint16_t GetPayloadLength(void) const
Get the "Payload length" field.
Definition: ipv6-header.cc:70
uint32_t m_flowLabel
The flow label.
Definition: ipv6-header.h:285
Packet header for IPv6.
Definition: ipv6-header.h:34
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
def start()
Definition: core.py:1844
uint32_t m_trafficClass
The traffic class.
Definition: ipv6-header.h:279
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
virtual void Print(std::ostream &os) const
Print some information about the packet.
Definition: ipv6-header.cc:130
void SetNextHeader(uint8_t next)
Set the "Next header" field.
Definition: ipv6-header.cc:75
DscpType
DiffServ Code Points Code Points defined in Assured Forwarding (AF) RFC 2597 Expedited Forwarding (EF...
Definition: ipv6-header.h:45
Ipv6Address m_sourceAddress
The source address.
Definition: ipv6-header.h:305
uint32_t ReadNtohU32(void)
Definition: buffer.h:970
iterator in a Buffer instance
Definition: buffer.h:98
Ipv6Address m_destinationAddress
The destination address.
Definition: ipv6-header.h:310
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: ipv6-header.cc:125
void WriteHtonU16(uint16_t data)
Definition: buffer.h:905
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ipv6-header.cc:143
DscpType GetDscp(void) const
Definition: ipv6-header.cc:202
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:100
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: ipv6-header.cc:164
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetEcn(EcnType ecn)
Set ECN field bits.
Definition: ipv6-header.cc:195
EcnType GetEcn(void) const
Definition: ipv6-header.cc:262
std::string EcnTypeToString(EcnType ecn) const
Definition: ipv6-header.cc:269
uint8_t GetNextHeader(void) const
Get the next header.
Definition: ipv6-header.cc:80
uint32_t GetFlowLabel(void) const
Get the "Flow label" field.
Definition: ipv6-header.cc:60
void WriteHtonU32(uint32_t data)
Definition: buffer.h:924
Ipv6Header(void)
Constructor.
Definition: ipv6-header.cc:34
static TypeId GetTypeId(void)
Get the type identifier.
Definition: ipv6-header.cc:115
void SetSourceAddress(Ipv6Address src)
Set the "Source address" field.
Definition: ipv6-header.cc:95
Describes an IPv6 address.
Definition: ipv6-address.h:49
void SetHopLimit(uint8_t limit)
Set the "Hop limit" field (TTL).
Definition: ipv6-header.cc:85
EcnType
ECN field bits.
Definition: ipv6-header.h:151
void WriteU8(uint8_t data)
Definition: buffer.h:869
uint8_t GetTrafficClass(void) const
Get the "Traffic class" field.
Definition: ipv6-header.cc:50
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:262
std::string DscpTypeToString(DscpType dscp) const
Definition: ipv6-header.cc:209
void SetFlowLabel(uint32_t flow)
Set the "Flow label" field.
Definition: ipv6-header.cc:55
uint16_t m_payloadLength
The payload length.
Definition: ipv6-header.h:290
uint8_t ReadU8(void)
Definition: buffer.h:1021
void SetTrafficClass(uint8_t traffic)
Set the "Traffic class" field.
Definition: ipv6-header.cc:45
uint8_t m_hopLimit
The Hop limit value.
Definition: ipv6-header.h:300
uint8_t m_nextHeader
The Next header number.
Definition: ipv6-header.h:295
uint8_t GetHopLimit(void) const
Get the "Hop limit" field (TTL).
Definition: ipv6-header.cc:90
uint16_t ReadNtohU16(void)
Definition: buffer.h:946
Ipv6Address GetDestinationAddress(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:110
a unique identifier for an interface.
Definition: type-id.h:58
void SetDestinationAddress(Ipv6Address dst)
Set the "Destination address" field.
Definition: ipv6-header.cc:105
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: ipv6-header.cc:148
void SetDscp(DscpType dscp)
Set DSCP Field.
Definition: ipv6-header.cc:188