A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
tcp-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 Georgia Tech Research Corporation
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: Raj Bhattacharjea <raj.b@gatech.edu>
19
*/
20
21
#include <stdint.h>
22
#include <iostream>
23
#include "
tcp-header.h
"
24
#include "ns3/buffer.h"
25
#include "ns3/address-utils.h"
26
27
namespace
ns3 {
28
29
NS_OBJECT_ENSURE_REGISTERED
(TcpHeader)
30
;
31
32
TcpHeader::TcpHeader
()
33
: m_sourcePort (0),
34
m_destinationPort (0),
35
m_sequenceNumber (0),
36
m_ackNumber (0),
37
m_length (5),
38
m_flags (0),
39
m_windowSize (0xffff),
40
m_urgentPointer (0),
41
m_calcChecksum (false),
42
m_goodChecksum (true)
43
{
44
}
45
46
TcpHeader::~TcpHeader
()
47
{
48
}
49
50
void
51
TcpHeader::EnableChecksums
(
void
)
52
{
53
m_calcChecksum
=
true
;
54
}
55
56
void
TcpHeader::SetSourcePort
(uint16_t
port
)
57
{
58
m_sourcePort
=
port
;
59
}
60
void
TcpHeader::SetDestinationPort
(uint16_t
port
)
61
{
62
m_destinationPort
=
port
;
63
}
64
void
TcpHeader::SetSequenceNumber
(
SequenceNumber32
sequenceNumber)
65
{
66
m_sequenceNumber
= sequenceNumber;
67
}
68
void
TcpHeader::SetAckNumber
(
SequenceNumber32
ackNumber)
69
{
70
m_ackNumber
= ackNumber;
71
}
72
void
TcpHeader::SetLength
(uint8_t length)
73
{
74
m_length
= length;
75
}
76
void
TcpHeader::SetFlags
(uint8_t flags)
77
{
78
m_flags
= flags;
79
}
80
void
TcpHeader::SetWindowSize
(uint16_t windowSize)
81
{
82
m_windowSize
= windowSize;
83
}
84
void
TcpHeader::SetUrgentPointer
(uint16_t urgentPointer)
85
{
86
m_urgentPointer
= urgentPointer;
87
}
88
89
uint16_t
TcpHeader::GetSourcePort
()
const
90
{
91
return
m_sourcePort
;
92
}
93
uint16_t
TcpHeader::GetDestinationPort
()
const
94
{
95
return
m_destinationPort
;
96
}
97
SequenceNumber32
TcpHeader::GetSequenceNumber
()
const
98
{
99
return
m_sequenceNumber
;
100
}
101
SequenceNumber32
TcpHeader::GetAckNumber
()
const
102
{
103
return
m_ackNumber
;
104
}
105
uint8_t
TcpHeader::GetLength
()
const
106
{
107
return
m_length
;
108
}
109
uint8_t
TcpHeader::GetFlags
()
const
110
{
111
return
m_flags
;
112
}
113
uint16_t
TcpHeader::GetWindowSize
()
const
114
{
115
return
m_windowSize
;
116
}
117
uint16_t
TcpHeader::GetUrgentPointer
()
const
118
{
119
return
m_urgentPointer
;
120
}
121
122
void
123
TcpHeader::InitializeChecksum
(
Ipv4Address
source,
124
Ipv4Address
destination,
125
uint8_t protocol)
126
{
127
m_source
= source;
128
m_destination
= destination;
129
m_protocol
= protocol;
130
}
131
132
void
133
TcpHeader::InitializeChecksum
(
Ipv6Address
source,
134
Ipv6Address
destination,
135
uint8_t protocol)
136
{
137
m_source
= source;
138
m_destination
= destination;
139
m_protocol
= protocol;
140
}
141
142
void
143
TcpHeader::InitializeChecksum
(
Address
source,
144
Address
destination,
145
uint8_t protocol)
146
{
147
m_source
= source;
148
m_destination
= destination;
149
m_protocol
= protocol;
150
}
151
152
uint16_t
153
TcpHeader::CalculateHeaderChecksum
(uint16_t size)
const
154
{
155
/* Buffer size must be at least as large as the largest IP pseudo-header */
156
/* [per RFC2460, but without consideration for IPv6 extension hdrs] */
157
/* Src address 16 bytes (more generally, Address::MAX_SIZE) */
158
/* Dst address 16 bytes (more generally, Address::MAX_SIZE) */
159
/* Upper layer pkt len 4 bytes */
160
/* Zero 3 bytes */
161
/* Next header 1 byte */
162
163
uint32_t maxHdrSz = (2 *
Address::MAX_SIZE
) + 8;
164
Buffer
buf =
Buffer
(maxHdrSz);
165
buf.
AddAtStart
(maxHdrSz);
166
Buffer::Iterator
it = buf.
Begin
();
167
uint32_t hdrSize = 0;
168
169
WriteTo
(it,
m_source
);
170
WriteTo
(it,
m_destination
);
171
if
(
Ipv4Address::IsMatchingType
(
m_source
))
172
{
173
it.
WriteU8
(0);
/* protocol */
174
it.
WriteU8
(
m_protocol
);
/* protocol */
175
it.
WriteU8
(size >> 8);
/* length */
176
it.
WriteU8
(size & 0xff);
/* length */
177
hdrSize = 12;
178
}
179
else
180
{
181
it.
WriteU16
(0);
182
it.
WriteU8
(size >> 8);
/* length */
183
it.
WriteU8
(size & 0xff);
/* length */
184
it.
WriteU16
(0);
185
it.
WriteU8
(0);
186
it.
WriteU8
(
m_protocol
);
/* protocol */
187
hdrSize = 40;
188
}
189
190
it = buf.
Begin
();
191
/* we don't CompleteChecksum ( ~ ) now */
192
return
~(it.
CalculateIpChecksum
(hdrSize));
193
}
194
195
bool
196
TcpHeader::IsChecksumOk
(
void
)
const
197
{
198
return
m_goodChecksum
;
199
}
200
201
TypeId
202
TcpHeader::GetTypeId
(
void
)
203
{
204
static
TypeId
tid =
TypeId
(
"ns3::TcpHeader"
)
205
.
SetParent
<
Header
> ()
206
.AddConstructor<TcpHeader> ()
207
;
208
return
tid;
209
}
210
TypeId
211
TcpHeader::GetInstanceTypeId
(
void
)
const
212
{
213
return
GetTypeId
();
214
}
215
void
TcpHeader::Print
(std::ostream &os)
const
216
{
217
os <<
m_sourcePort
<<
" > "
<<
m_destinationPort
;
218
if
(
m_flags
!=0)
219
{
220
os<<
" ["
;
221
if
((
m_flags
&
FIN
) != 0)
222
{
223
os<<
" FIN "
;
224
}
225
if
((
m_flags
&
SYN
) != 0)
226
{
227
os<<
" SYN "
;
228
}
229
if
((
m_flags
&
RST
) != 0)
230
{
231
os<<
" RST "
;
232
}
233
if
((
m_flags
&
PSH
) != 0)
234
{
235
os<<
" PSH "
;
236
}
237
if
((
m_flags
&
ACK
) != 0)
238
{
239
os<<
" ACK "
;
240
}
241
if
((
m_flags
&
URG
) != 0)
242
{
243
os<<
" URG "
;
244
}
245
if
((
m_flags
&
ECE
) != 0)
246
{
247
os<<
" ECE "
;
248
}
249
if
((
m_flags
&
CWR
) != 0)
250
{
251
os<<
" CWR "
;
252
}
253
os<<
"]"
;
254
}
255
os<<
" Seq="
<<
m_sequenceNumber
<<
" Ack="
<<
m_ackNumber
<<
" Win="
<<
m_windowSize
;
256
}
257
uint32_t
TcpHeader::GetSerializedSize
(
void
)
const
258
{
259
return
4*
m_length
;
260
}
261
void
TcpHeader::Serialize
(
Buffer::Iterator
start
)
const
262
{
263
Buffer::Iterator
i =
start
;
264
i.
WriteHtonU16
(
m_sourcePort
);
265
i.
WriteHtonU16
(
m_destinationPort
);
266
i.
WriteHtonU32
(
m_sequenceNumber
.
GetValue
());
267
i.
WriteHtonU32
(
m_ackNumber
.
GetValue
());
268
i.
WriteHtonU16
(
m_length
<< 12 |
m_flags
);
//reserved bits are all zero
269
i.
WriteHtonU16
(
m_windowSize
);
270
i.
WriteHtonU16
(0);
271
i.
WriteHtonU16
(
m_urgentPointer
);
272
273
if
(
m_calcChecksum
)
274
{
275
uint16_t headerChecksum =
CalculateHeaderChecksum
(start.
GetSize
());
276
i =
start
;
277
uint16_t checksum = i.CalculateIpChecksum (start.
GetSize
(), headerChecksum);
278
279
i =
start
;
280
i.Next (16);
281
i.WriteU16 (checksum);
282
}
283
}
284
uint32_t
TcpHeader::Deserialize
(
Buffer::Iterator
start
)
285
{
286
Buffer::Iterator
i =
start
;
287
m_sourcePort
= i.
ReadNtohU16
();
288
m_destinationPort
= i.
ReadNtohU16
();
289
m_sequenceNumber
= i.
ReadNtohU32
();
290
m_ackNumber
= i.
ReadNtohU32
();
291
uint16_t field = i.
ReadNtohU16
();
292
m_flags
= field & 0x3F;
293
m_length
= field>>12;
294
m_windowSize
= i.
ReadNtohU16
();
295
i.
Next
(2);
296
m_urgentPointer
= i.
ReadNtohU16
();
297
298
if
(
m_calcChecksum
)
299
{
300
uint16_t headerChecksum =
CalculateHeaderChecksum
(start.
GetSize
());
301
i =
start
;
302
uint16_t checksum = i.CalculateIpChecksum (start.
GetSize
(), headerChecksum);
303
m_goodChecksum
= (checksum == 0);
304
}
305
306
return
GetSerializedSize
();
307
}
308
309
310
}
// namespace ns3
ns3::Header
Protocol header serialization and deserialization.
Definition:
header.h:42
ns3::Buffer::Iterator::CalculateIpChecksum
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition:
buffer.cc:1158
ns3::TcpHeader::URG
Definition:
tcp-header.h:182
ns3::TcpHeader::GetDestinationPort
uint16_t GetDestinationPort() const
Definition:
tcp-header.cc:93
ns3::TcpHeader::GetSequenceNumber
SequenceNumber32 GetSequenceNumber() const
Definition:
tcp-header.cc:97
tcp-header.h
ns3::Address::MAX_SIZE
Definition:
address.h:94
ns3::TcpHeader::InitializeChecksum
void InitializeChecksum(Ipv4Address source, Ipv4Address destination, uint8_t protocol)
Initialize the TCP checksum.
Definition:
tcp-header.cc:123
ns3::SequenceNumber::GetValue
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
Definition:
sequence-number.h:109
ns3::TcpHeader::GetFlags
uint8_t GetFlags() const
Definition:
tcp-header.cc:109
ns3::TcpHeader::GetAckNumber
SequenceNumber32 GetAckNumber() const
Definition:
tcp-header.cc:101
ns3::Buffer
automatically resized byte buffer
Definition:
buffer.h:92
ns3::NS_OBJECT_ENSURE_REGISTERED
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
ns3::WriteTo
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
Definition:
address-utils.cc:28
ns3::TcpHeader::m_source
Address m_source
Source IP address.
Definition:
tcp-header.h:217
visualizer.core.start
def start
Definition:
core.py:1482
ns3::TcpHeader::TcpHeader
TcpHeader()
Definition:
tcp-header.cc:32
ns3::Buffer::Iterator::ReadNtohU32
uint32_t ReadNtohU32(void)
Definition:
buffer.h:791
ns3::TcpHeader::m_destinationPort
uint16_t m_destinationPort
Destination port.
Definition:
tcp-header.h:209
ns3::TcpHeader::m_calcChecksum
bool m_calcChecksum
Flag to calculate checksum.
Definition:
tcp-header.h:221
port
uint16_t port
Definition:
dsdv-manet.cc:44
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:98
ns3::Address
a polymophic address class
Definition:
address.h:86
ns3::TcpHeader::Print
virtual void Print(std::ostream &os) const
Definition:
tcp-header.cc:215
ns3::TcpHeader::SetSequenceNumber
void SetSequenceNumber(SequenceNumber32 sequenceNumber)
Definition:
tcp-header.cc:64
ns3::TcpHeader::RST
Definition:
tcp-header.h:181
ns3::TcpHeader::GetWindowSize
uint16_t GetWindowSize() const
Definition:
tcp-header.cc:113
ns3::Buffer::Iterator::WriteU16
void WriteU16(uint16_t data)
Definition:
buffer.cc:895
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition:
buffer.h:726
ns3::TcpHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Definition:
tcp-header.cc:211
ns3::TcpHeader::CalculateHeaderChecksum
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition:
tcp-header.cc:153
ns3::Buffer::Iterator::Next
void Next(void)
go forward by one byte
Definition:
buffer.h:666
ns3::TcpHeader::SYN
Definition:
tcp-header.h:181
ns3::Ipv4Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition:
ipv4-address.cc:315
ns3::Buffer::Begin
Buffer::Iterator Begin(void) const
Definition:
buffer.h:875
ns3::TcpHeader::m_goodChecksum
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition:
tcp-header.h:222
ns3::TcpHeader::SetDestinationPort
void SetDestinationPort(uint16_t port)
Definition:
tcp-header.cc:60
ns3::TcpHeader::SetFlags
void SetFlags(uint8_t flags)
Definition:
tcp-header.cc:76
ns3::TcpHeader::m_windowSize
uint16_t m_windowSize
Window size.
Definition:
tcp-header.h:214
ns3::TcpHeader::m_sourcePort
uint16_t m_sourcePort
Source port.
Definition:
tcp-header.h:208
ns3::SequenceNumber< uint32_t, int32_t >
ns3::TcpHeader::SetSourcePort
void SetSourcePort(uint16_t port)
Definition:
tcp-header.cc:56
ns3::TcpHeader::~TcpHeader
virtual ~TcpHeader()
Definition:
tcp-header.cc:46
ns3::TcpHeader::m_destination
Address m_destination
Destination IP address.
Definition:
tcp-header.h:218
ns3::TcpHeader::SetUrgentPointer
void SetUrgentPointer(uint16_t urgentPointer)
Definition:
tcp-header.cc:84
ns3::Buffer::Iterator::WriteHtonU32
void WriteHtonU32(uint32_t data)
Definition:
buffer.h:745
ns3::TcpHeader::FIN
Definition:
tcp-header.h:181
ns3::TcpHeader::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
tcp-header.cc:257
ns3::TcpHeader::GetLength
uint8_t GetLength() const
Definition:
tcp-header.cc:105
ns3::TcpHeader::m_protocol
uint8_t m_protocol
Protocol number.
Definition:
tcp-header.h:219
ns3::TcpHeader::CWR
Definition:
tcp-header.h:182
ns3::TcpHeader::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition:
tcp-header.cc:284
ns3::TcpHeader::m_urgentPointer
uint16_t m_urgentPointer
Urgent pointer.
Definition:
tcp-header.h:215
ns3::Ipv6Address
Describes an IPv6 address.
Definition:
ipv6-address.h:46
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition:
ipv4-address.h:38
ns3::TcpHeader::m_ackNumber
SequenceNumber32 m_ackNumber
ACK number.
Definition:
tcp-header.h:211
ns3::TcpHeader::m_flags
uint8_t m_flags
Flags (really a uint6_t)
Definition:
tcp-header.h:213
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition:
buffer.h:690
ns3::TcpHeader::m_length
uint8_t m_length
Length (really a uint4_t)
Definition:
tcp-header.h:212
ns3::TcpHeader::ECE
Definition:
tcp-header.h:182
ns3::TcpHeader::SetAckNumber
void SetAckNumber(SequenceNumber32 ackNumber)
Definition:
tcp-header.cc:68
ns3::TcpHeader::GetSourcePort
uint16_t GetSourcePort() const
Definition:
tcp-header.cc:89
ns3::TcpHeader::PSH
Definition:
tcp-header.h:181
ns3::Buffer::AddAtStart
bool AddAtStart(uint32_t start)
Definition:
buffer.cc:305
ns3::TcpHeader::SetLength
void SetLength(uint8_t length)
Definition:
tcp-header.cc:72
ns3::TcpHeader::IsChecksumOk
bool IsChecksumOk(void) const
Is the TCP checksum correct ?
Definition:
tcp-header.cc:196
ns3::TcpHeader::EnableChecksums
void EnableChecksums(void)
Enable checksum calculation for TCP.
Definition:
tcp-header.cc:51
ns3::TcpHeader::ACK
Definition:
tcp-header.h:181
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16(void)
Definition:
buffer.h:767
ns3::TcpHeader::SetWindowSize
void SetWindowSize(uint16_t windowSize)
Definition:
tcp-header.cc:80
ns3::Buffer::Iterator::GetSize
uint32_t GetSize(void) const
Definition:
buffer.cc:1183
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Definition:
type-id.cc:611
ns3::TcpHeader::GetUrgentPointer
uint16_t GetUrgentPointer() const
Definition:
tcp-header.cc:117
ns3::TcpHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Definition:
tcp-header.cc:261
ns3::TcpHeader::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
tcp-header.cc:202
ns3::TcpHeader::m_sequenceNumber
SequenceNumber32 m_sequenceNumber
Sequence number.
Definition:
tcp-header.h:210
src
internet
model
tcp-header.cc
Generated on Sat Apr 19 2014 14:06:57 for ns-3 by
1.8.6