A Discrete-Event Network Simulator
API
epc-gtpu-header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
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: Jaume Nin <jnin@cttc.cat>
18  */
19 
20 #include "epc-gtpu-header.h"
21 
22 #include "ns3/log.h"
23 #include "ns3/packet.h"
24 
25 namespace ns3
26 {
27 
28 NS_LOG_COMPONENT_DEFINE("GtpuHeader");
29 
30 /********************************************************
31  * GTP-U-v1 Header
32  ********************************************************/
33 
34 NS_OBJECT_ENSURE_REGISTERED(GtpuHeader);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId("ns3::GtpuHeader")
40  .SetParent<Header>()
41  .SetGroupName("Lte")
42  .AddConstructor<GtpuHeader>();
43  return tid;
44 }
45 
47  : m_version(1),
48  m_protocolType(true),
49  m_extensionHeaderFlag(false),
50  m_sequenceNumberFlag(true),
51  m_nPduNumberFlag(true),
52  m_messageType(255),
53  m_length(0),
54  m_teid(0),
55  m_sequenceNumber(0),
56  m_nPduNumber(0),
57  m_nextExtensionType(0)
58 {
59 }
60 
62 {
63 }
64 
65 TypeId
67 {
68  return GetTypeId();
69 }
70 
71 uint32_t
73 {
74  return 12;
75 }
76 
77 void
79 {
81  uint8_t firstByte = m_version << 5 | m_protocolType << 4 | 0x1 << 3;
83  i.WriteU8(firstByte);
90 }
91 
92 uint32_t
94 {
96  uint8_t firstByte = i.ReadU8();
97  m_version = firstByte >> 5 & 0x7;
98  m_protocolType = firstByte >> 4 & 0x1;
99  m_extensionHeaderFlag = firstByte >> 2 & 0x1;
100  m_sequenceNumberFlag = firstByte >> 1 & 0x1;
101  m_nPduNumberFlag = firstByte & 0x1;
102  m_messageType = i.ReadU8();
103  m_length = i.ReadNtohU16();
104  m_teid = i.ReadNtohU32();
106  m_nPduNumber = i.ReadU8();
108  return GetSerializedSize();
109 }
110 
111 void
112 GtpuHeader::Print(std::ostream& os) const
113 {
114  os << " version=" << (uint32_t)m_version << " [";
115  if (m_protocolType)
116  {
117  os << " PT ";
118  }
120  {
121  os << " E ";
122  }
124  {
125  os << " S ";
126  }
127  if (m_nPduNumberFlag)
128  {
129  os << " PN ";
130  }
131  os << "], messageType=" << (uint32_t)m_messageType << ", length=" << (uint32_t)m_length;
132  os << ", teid=" << (uint32_t)m_teid << ", sequenceNumber=" << (uint32_t)m_sequenceNumber;
133  os << ", nPduNumber=" << (uint32_t)m_nPduNumber
134  << ", nextExtensionType=" << (uint32_t)m_nextExtensionType;
135 }
136 
137 bool
139 {
140  return m_extensionHeaderFlag;
141 }
142 
143 uint16_t
145 {
146  return m_length;
147 }
148 
149 uint8_t
151 {
152  return m_messageType;
153 }
154 
155 uint8_t
157 {
158  return m_nPduNumber;
159 }
160 
161 bool
163 {
164  return m_nPduNumberFlag;
165 }
166 
167 uint8_t
169 {
170  return m_nextExtensionType;
171 }
172 
173 bool
175 {
176  return m_protocolType;
177 }
178 
179 uint16_t
181 {
182  return m_sequenceNumber;
183 }
184 
185 bool
187 {
188  return m_sequenceNumberFlag;
189 }
190 
191 uint32_t
193 {
194  return m_teid;
195 }
196 
197 uint8_t
199 {
200  return m_version;
201 }
202 
203 void
204 GtpuHeader::SetExtensionHeaderFlag(bool m_extensionHeaderFlag)
205 {
206  this->m_extensionHeaderFlag = m_extensionHeaderFlag;
207 }
208 
209 void
210 GtpuHeader::SetLength(uint16_t m_length)
211 {
212  this->m_length = m_length;
213 }
214 
215 void
216 GtpuHeader::SetMessageType(uint8_t m_messageType)
217 {
218  this->m_messageType = m_messageType;
219 }
220 
221 void
222 GtpuHeader::SetNPduNumber(uint8_t m_nPduNumber)
223 {
224  this->m_nPduNumber = m_nPduNumber;
225 }
226 
227 void
228 GtpuHeader::SetNPduNumberFlag(bool m_nPduNumberFlag)
229 {
230  this->m_nPduNumberFlag = m_nPduNumberFlag;
231 }
232 
233 void
234 GtpuHeader::SetNextExtensionType(uint8_t m_nextExtensionType)
235 {
236  this->m_nextExtensionType = m_nextExtensionType;
237 }
238 
239 void
240 GtpuHeader::SetProtocolType(bool m_protocolType)
241 {
242  this->m_protocolType = m_protocolType;
243 }
244 
245 void
246 GtpuHeader::SetSequenceNumber(uint16_t m_sequenceNumber)
247 {
248  this->m_sequenceNumber = m_sequenceNumber;
249 }
250 
251 void
252 GtpuHeader::SetSequenceNumberFlag(bool m_sequenceNumberFlag)
253 {
254  this->m_sequenceNumberFlag = m_sequenceNumberFlag;
255 }
256 
257 void
258 GtpuHeader::SetTeid(uint32_t m_teid)
259 {
260  this->m_teid = m_teid;
261 }
262 
263 void
264 GtpuHeader::SetVersion(uint8_t m_version)
265 {
266  // m_version is a uint3_t
267  this->m_version = m_version & 0x7;
268 }
269 
270 bool
272 {
273  return m_version == b.m_version && m_protocolType == b.m_protocolType &&
279 }
280 
281 } // 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
Implementation of the GPRS Tunnelling Protocol header according to GTPv1-U Release 10 as per 3Gpp TS ...
void SetSequenceNumber(uint16_t sequenceNumber)
Set sequence number function.
uint8_t m_nextExtensionType
This field defines the type of Extension Header that follows this field in the GTP-PDU.
void SetExtensionHeaderFlag(bool extensionHeaderFlag)
Set extension header flag function.
uint32_t m_teid
This field unambiguously identifies a tunnel endpoint in the receiving GTP-U protocol entity.
bool m_nPduNumberFlag
This flag indicates the presence of a meaningful value of the N-PDU Number field.
bool operator==(const GtpuHeader &b) const
Equality operator.
bool m_extensionHeaderFlag
This flag indicates the presence of a meaningful value of the Next Extension Header field.
void SetTeid(uint32_t teid)
Set TEID function.
~GtpuHeader() override
bool GetExtensionHeaderFlag() const
Get extension header flag function.
void SetVersion(uint8_t version)
Set version function.
bool m_protocolType
This bit is used as a protocol discriminator between GTP (when PT is '1') and GTP' (when PT is '0').
uint8_t m_messageType
This field indicates the type of GTP-U message.
uint8_t GetVersion() const
Get version function.
uint32_t GetSerializedSize() const override
bool GetSequenceNumberFlag() const
Get sequence number flag function.
bool GetNPduNumberFlag() const
Get type of GTP-U message function.
void SetNPduNumber(uint8_t nPduNumber)
Set NPDU number function.
uint8_t m_nPduNumber
This field is used at the Inter SGSN Routeing Area Update procedure and some inter-system handover pr...
void SetNPduNumberFlag(bool nPduNumberFlag)
Sets the flag that indicates the presence of a meaningful value of the N-PDU Number field.
uint16_t m_sequenceNumber
If Sequence Number field is used for G-PDUs (T-PDUs+headers), an increasing sequence number for T-PDU...
uint8_t GetNPduNumber() const
Get NPDU number function.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint16_t GetLength() const
Get length function.
void Print(std::ostream &os) const override
void SetMessageType(uint8_t messageType)
Set message type function.
uint8_t m_version
This field is used to determine the version of the GTPU-U protocol.
uint16_t m_length
This field indicates the length in octets of the payload, i.e.
static TypeId GetTypeId()
Get the type ID.
void SetSequenceNumberFlag(bool sequenceNumberFlag)
Set sequence number flag function.
uint16_t GetSequenceNumber() const
Get protocol type function.
uint32_t GetTeid() const
Get a tunnel endpoint identificator (TEID)
bool GetProtocolType() const
Get protocol type function.
void SetProtocolType(bool protocolType)
Set protocol type function.
uint8_t GetMessageType() const
Get message type function.
uint8_t GetNextExtensionType() const
Get next extension type function.
bool m_sequenceNumberFlag
This flag indicates the presence of a meaningful value of the Sequence Number field.
void Serialize(Buffer::Iterator start) const override
void SetNextExtensionType(uint8_t nextExtensionType)
Set next extension type function.
void SetLength(uint16_t length)
Set the length in octets of the payload.
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
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_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.