A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
tcp-header.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2007 Georgia Tech Research Corporation
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Raj Bhattacharjea <raj.b@gatech.edu>
7
*/
8
9
#ifndef TCP_HEADER_H
10
#define TCP_HEADER_H
11
12
#include "
tcp-option.h
"
13
#include "
tcp-socket-factory.h
"
14
15
#include "ns3/buffer.h"
16
#include "ns3/header.h"
17
#include "ns3/ipv4-address.h"
18
#include "ns3/ipv6-address.h"
19
#include "ns3/sequence-number.h"
20
21
#include <stdint.h>
22
23
namespace
ns3
24
{
25
26
/**
27
* \ingroup tcp
28
* \brief Header for the Transmission Control Protocol
29
*
30
* This class has fields corresponding to those in a network TCP header
31
* (port numbers, sequence and acknowledgement numbers, flags, etc) as well
32
* as methods for serialization to and deserialization from a byte buffer.
33
*/
34
35
class
TcpHeader
:
public
Header
36
{
37
public
:
38
typedef
std::list<Ptr<const TcpOption>>
TcpOptionList
;
//!< List of TcpOption
39
40
/**
41
* \brief Print a TCP header into an output stream
42
*
43
* \param os output stream
44
* \param tc TCP header to print
45
* \return The ostream passed as first argument
46
*/
47
friend
std::ostream&
operator<<
(std::ostream& os,
const
TcpHeader
& tc);
48
49
/**
50
* \brief Converts an integer into a human readable list of Tcp flags
51
*
52
* \param flags Bitfield of TCP flags to convert to a readable string
53
* \param delimiter String to insert between flags
54
*
55
* FIN=0x1, SYN=0x2, RST=0x4, PSH=0x8, ACK=0x10, URG=0x20, ECE=0x40, CWR=0x80
56
* TcpHeader::FlagsToString (0x1) should return the following string;
57
* "FIN"
58
*
59
* TcpHeader::FlagsToString (0xff) should return the following string;
60
* "FIN|SYN|RST|PSH|ACK|URG|ECE|CWR";
61
*
62
* \return the generated string
63
**/
64
static
std::string
FlagsToString
(uint8_t flags,
const
std::string& delimiter =
"|"
);
65
66
/**
67
* \brief Enable checksum calculation for TCP
68
*
69
* \todo currently has no effect
70
*/
71
void
EnableChecksums
();
72
73
// Setters
74
75
/**
76
* \brief Set the source port
77
* \param port The source port for this TcpHeader
78
*/
79
void
SetSourcePort
(uint16_t
port
);
80
81
/**
82
* \brief Set the destination port
83
* \param port the destination port for this TcpHeader
84
*/
85
void
SetDestinationPort
(uint16_t
port
);
86
87
/**
88
* \brief Set the sequence Number
89
* \param sequenceNumber the sequence number for this TcpHeader
90
*/
91
void
SetSequenceNumber
(
SequenceNumber32
sequenceNumber);
92
93
/**
94
* \brief Set the ACK number
95
* \param ackNumber the ACK number for this TcpHeader
96
*/
97
void
SetAckNumber
(
SequenceNumber32
ackNumber);
98
99
/**
100
* \brief Set flags of the header
101
* \param flags the flags for this TcpHeader
102
*/
103
void
SetFlags
(uint8_t flags);
104
105
/**
106
* \brief Set the window size
107
* \param windowSize the window size for this TcpHeader
108
*/
109
void
SetWindowSize
(uint16_t windowSize);
110
111
/**
112
* \brief Set the urgent pointer
113
* \param urgentPointer the urgent pointer for this TcpHeader
114
*/
115
void
SetUrgentPointer
(uint16_t urgentPointer);
116
117
// Getters
118
119
/**
120
* \brief Get the source port
121
* \return The source port for this TcpHeader
122
*/
123
uint16_t
GetSourcePort
()
const
;
124
125
/**
126
* \brief Get the destination port
127
* \return the destination port for this TcpHeader
128
*/
129
uint16_t
GetDestinationPort
()
const
;
130
131
/**
132
* \brief Get the sequence number
133
* \return the sequence number for this TcpHeader
134
*/
135
SequenceNumber32
GetSequenceNumber
()
const
;
136
137
/**
138
* \brief Get the ACK number
139
* \return the ACK number for this TcpHeader
140
*/
141
SequenceNumber32
GetAckNumber
()
const
;
142
143
/**
144
* \brief Get the length in words
145
*
146
* A word is 4 bytes; without Tcp Options, header is 5 words (20 bytes).
147
* With options, it can reach up to 15 words (60 bytes).
148
*
149
* \return the length of this TcpHeader
150
*/
151
uint8_t
GetLength
()
const
;
152
153
/**
154
* \brief Get the flags
155
* \return the flags for this TcpHeader
156
*/
157
uint8_t
GetFlags
()
const
;
158
159
/**
160
* \brief Get the window size
161
* \return the window size for this TcpHeader
162
*/
163
uint16_t
GetWindowSize
()
const
;
164
165
/**
166
* \brief Get the urgent pointer
167
* \return the urgent pointer for this TcpHeader
168
*/
169
uint16_t
GetUrgentPointer
()
const
;
170
171
/**
172
* \brief Get the option specified
173
* \param kind the option to retrieve
174
* \return Whether the header contains a specific kind of option, or 0
175
*/
176
Ptr<const TcpOption>
GetOption
(uint8_t kind)
const
;
177
178
/**
179
* \brief Get the list of option in this header
180
* \return a const reference to the option list
181
*/
182
const
TcpOptionList
&
GetOptionList
()
const
;
183
184
/**
185
* \brief Get the total length of appended options
186
* \return the total length of options appended to this TcpHeader
187
*/
188
uint8_t
GetOptionLength
()
const
;
189
190
/**
191
* \brief Get maximum option length
192
* \return the maximum option length
193
*/
194
uint8_t
GetMaxOptionLength
()
const
;
195
196
/**
197
* \brief Check if the header has the option specified
198
* \param kind Option to check for
199
* \return true if the header has the option, false otherwise
200
*/
201
bool
HasOption
(uint8_t kind)
const
;
202
203
/**
204
* \brief Append an option to the TCP header
205
* \param option The option to append
206
* \return true if option has been appended, false otherwise
207
*/
208
bool
AppendOption
(
Ptr<const TcpOption>
option);
209
210
/**
211
* \brief Initialize the TCP checksum.
212
*
213
* If you want to use tcp checksums, you should call this
214
* method prior to adding the header to a packet.
215
*
216
* \param source the IP source to use in the underlying
217
* IP packet.
218
* \param destination the IP destination to use in the
219
* underlying IP packet.
220
* \param protocol the protocol number to use in the underlying
221
* IP packet.
222
*
223
*/
224
void
InitializeChecksum
(
const
Ipv4Address
& source,
225
const
Ipv4Address
& destination,
226
uint8_t protocol);
227
228
/**
229
* \brief Initialize the TCP checksum.
230
*
231
* If you want to use tcp checksums, you should call this
232
* method prior to adding the header to a packet.
233
*
234
* \param source the IP source to use in the underlying
235
* IP packet.
236
* \param destination the IP destination to use in the
237
* underlying IP packet.
238
* \param protocol the protocol number to use in the underlying
239
* IP packet.
240
*
241
*/
242
void
InitializeChecksum
(
const
Ipv6Address
& source,
243
const
Ipv6Address
& destination,
244
uint8_t protocol);
245
246
/**
247
* \brief Initialize the TCP checksum.
248
*
249
* If you want to use tcp checksums, you should call this
250
* method prior to adding the header to a packet.
251
*
252
* \param source the IP source to use in the underlying
253
* IP packet.
254
* \param destination the IP destination to use in the
255
* underlying IP packet.
256
* \param protocol the protocol number to use in the underlying
257
* IP packet.
258
*
259
*/
260
void
InitializeChecksum
(
const
Address
& source,
const
Address
& destination, uint8_t protocol);
261
262
/**
263
* \brief TCP flag field values
264
*/
265
enum
Flags_t
266
{
267
NONE
= 0,
//!< No flags
268
FIN
= 1,
//!< FIN
269
SYN
= 2,
//!< SYN
270
RST
= 4,
//!< Reset
271
PSH
= 8,
//!< Push
272
ACK
= 16,
//!< Ack
273
URG
= 32,
//!< Urgent
274
ECE
= 64,
//!< ECE
275
CWR
= 128
//!< CWR
276
};
277
278
/**
279
* \brief Get the type ID.
280
* \return the object TypeId
281
*/
282
static
TypeId
GetTypeId
();
283
TypeId
GetInstanceTypeId
()
const override
;
284
void
Print
(std::ostream& os)
const override
;
285
uint32_t
GetSerializedSize
()
const override
;
286
void
Serialize
(
Buffer::Iterator
start)
const override
;
287
uint32_t
Deserialize
(
Buffer::Iterator
start)
override
;
288
289
/**
290
* \brief Is the TCP checksum correct ?
291
* \returns true if the checksum is correct, false otherwise.
292
*/
293
bool
IsChecksumOk
()
const
;
294
295
/**
296
* Comparison operator
297
* \param lhs left operand
298
* \param rhs right operand
299
* \return true if the operands are equal
300
*/
301
friend
bool
operator==
(
const
TcpHeader
& lhs,
const
TcpHeader
& rhs);
302
303
private
:
304
/**
305
* \brief Calculate the header checksum
306
* \param size packet size
307
* \returns the checksum
308
*/
309
uint16_t
CalculateHeaderChecksum
(uint16_t size)
const
;
310
311
/**
312
* \brief Calculates the header length (in words)
313
*
314
* Given the standard size of the header, the method checks for options
315
* and calculates the real length (in words).
316
*
317
* \return header length in 4-byte words
318
*/
319
uint8_t
CalculateHeaderLength
()
const
;
320
321
uint16_t
m_sourcePort
{0};
//!< Source port
322
uint16_t
m_destinationPort
{0};
//!< Destination port
323
SequenceNumber32
m_sequenceNumber
{0};
//!< Sequence number
324
SequenceNumber32
m_ackNumber
{0};
//!< ACK number
325
uint8_t
m_length
{5};
//!< Length (really a uint4_t) in words.
326
uint8_t
m_flags
{0};
//!< Flags (really a uint6_t)
327
uint16_t
m_windowSize
{0xffff};
//!< Window size
328
uint16_t
m_urgentPointer
{0};
//!< Urgent pointer
329
330
Address
m_source
;
//!< Source IP address
331
Address
m_destination
;
//!< Destination IP address
332
uint8_t
m_protocol
{6};
//!< Protocol number
333
334
bool
m_calcChecksum
{
false
};
//!< Flag to calculate checksum
335
bool
m_goodChecksum
{
true
};
//!< Flag to indicate that checksum is correct
336
337
static
const
uint8_t
m_maxOptionsLen
= 40;
//!< Maximum options length
338
TcpOptionList
m_options
;
//!< TcpOption present in the header
339
uint8_t
m_optionsLen
{0};
//!< Tcp options length.
340
};
341
342
}
// namespace ns3
343
344
#endif
/* TCP_HEADER */
ns3::Address
a polymophic address class
Definition
address.h:90
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:33
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::Ipv6Address
Describes an IPv6 address.
Definition
ipv6-address.h:38
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::SequenceNumber< uint32_t, int32_t >
ns3::TcpHeader
Header for the Transmission Control Protocol.
Definition
tcp-header.h:36
ns3::TcpHeader::m_urgentPointer
uint16_t m_urgentPointer
Urgent pointer.
Definition
tcp-header.h:328
ns3::TcpHeader::SetUrgentPointer
void SetUrgentPointer(uint16_t urgentPointer)
Set the urgent pointer.
Definition
tcp-header.cc:89
ns3::TcpHeader::m_source
Address m_source
Source IP address.
Definition
tcp-header.h:330
ns3::TcpHeader::Print
void Print(std::ostream &os) const override
Definition
tcp-header.cc:248
ns3::TcpHeader::SetDestinationPort
void SetDestinationPort(uint16_t port)
Set the destination port.
Definition
tcp-header.cc:59
ns3::TcpHeader::SetSequenceNumber
void SetSequenceNumber(SequenceNumber32 sequenceNumber)
Set the sequence Number.
Definition
tcp-header.cc:65
ns3::TcpHeader::m_optionsLen
uint8_t m_optionsLen
Tcp options length.
Definition
tcp-header.h:339
ns3::TcpHeader::GetSequenceNumber
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
Definition
tcp-header.cc:107
ns3::TcpHeader::operator==
friend bool operator==(const TcpHeader &lhs, const TcpHeader &rhs)
Comparison operator.
Definition
tcp-header.cc:481
ns3::TcpHeader::GetLength
uint8_t GetLength() const
Get the length in words.
Definition
tcp-header.cc:119
ns3::TcpHeader::GetMaxOptionLength
uint8_t GetMaxOptionLength() const
Get maximum option length.
Definition
tcp-header.cc:131
ns3::TcpHeader::m_sourcePort
uint16_t m_sourcePort
Source port.
Definition
tcp-header.h:321
ns3::TcpHeader::GetDestinationPort
uint16_t GetDestinationPort() const
Get the destination port.
Definition
tcp-header.cc:101
ns3::TcpHeader::TcpOptionList
std::list< Ptr< const TcpOption > > TcpOptionList
List of TcpOption.
Definition
tcp-header.h:38
ns3::TcpHeader::CalculateHeaderLength
uint8_t CalculateHeaderLength() const
Calculates the header length (in words)
Definition
tcp-header.cc:404
ns3::TcpHeader::m_length
uint8_t m_length
Length (really a uint4_t) in words.
Definition
tcp-header.h:325
ns3::TcpHeader::m_maxOptionsLen
static const uint8_t m_maxOptionsLen
Maximum options length.
Definition
tcp-header.h:337
ns3::TcpHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition
tcp-header.cc:319
ns3::TcpHeader::GetOption
Ptr< const TcpOption > GetOption(uint8_t kind) const
Get the option specified.
Definition
tcp-header.cc:453
ns3::TcpHeader::Flags_t
Flags_t
TCP flag field values.
Definition
tcp-header.h:266
ns3::TcpHeader::URG
@ URG
Urgent.
Definition
tcp-header.h:273
ns3::TcpHeader::ACK
@ ACK
Ack.
Definition
tcp-header.h:272
ns3::TcpHeader::PSH
@ PSH
Push.
Definition
tcp-header.h:271
ns3::TcpHeader::CWR
@ CWR
CWR.
Definition
tcp-header.h:275
ns3::TcpHeader::SYN
@ SYN
SYN.
Definition
tcp-header.h:269
ns3::TcpHeader::ECE
@ ECE
ECE.
Definition
tcp-header.h:274
ns3::TcpHeader::RST
@ RST
Reset.
Definition
tcp-header.h:270
ns3::TcpHeader::NONE
@ NONE
No flags.
Definition
tcp-header.h:267
ns3::TcpHeader::FIN
@ FIN
FIN.
Definition
tcp-header.h:268
ns3::TcpHeader::operator<<
friend std::ostream & operator<<(std::ostream &os, const TcpHeader &tc)
Print a TCP header into an output stream.
Definition
tcp-header.cc:491
ns3::TcpHeader::SetFlags
void SetFlags(uint8_t flags)
Set flags of the header.
Definition
tcp-header.cc:77
ns3::TcpHeader::SetWindowSize
void SetWindowSize(uint16_t windowSize)
Set the window size.
Definition
tcp-header.cc:83
ns3::TcpHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
tcp-header.cc:268
ns3::TcpHeader::GetOptionList
const TcpOptionList & GetOptionList() const
Get the list of option in this header.
Definition
tcp-header.cc:447
ns3::TcpHeader::m_calcChecksum
bool m_calcChecksum
Flag to calculate checksum.
Definition
tcp-header.h:334
ns3::TcpHeader::GetWindowSize
uint16_t GetWindowSize() const
Get the window size.
Definition
tcp-header.cc:143
ns3::TcpHeader::InitializeChecksum
void InitializeChecksum(const Ipv4Address &source, const Ipv4Address &destination, uint8_t protocol)
Initialize the TCP checksum.
Definition
tcp-header.cc:155
ns3::TcpHeader::m_destination
Address m_destination
Destination IP address.
Definition
tcp-header.h:331
ns3::TcpHeader::m_protocol
uint8_t m_protocol
Protocol number.
Definition
tcp-header.h:332
ns3::TcpHeader::GetOptionLength
uint8_t GetOptionLength() const
Get the total length of appended options.
Definition
tcp-header.cc:125
ns3::TcpHeader::m_sequenceNumber
SequenceNumber32 m_sequenceNumber
Sequence number.
Definition
tcp-header.h:323
ns3::TcpHeader::CalculateHeaderChecksum
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition
tcp-header.cc:183
ns3::TcpHeader::AppendOption
bool AppendOption(Ptr< const TcpOption > option)
Append an option to the TCP header.
Definition
tcp-header.cc:421
ns3::TcpHeader::FlagsToString
static std::string FlagsToString(uint8_t flags, const std::string &delimiter="|")
Converts an integer into a human readable list of Tcp flags.
Definition
tcp-header.cc:28
ns3::TcpHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
tcp-header.cc:242
ns3::TcpHeader::m_windowSize
uint16_t m_windowSize
Window size.
Definition
tcp-header.h:327
ns3::TcpHeader::HasOption
bool HasOption(uint8_t kind) const
Check if the header has the option specified.
Definition
tcp-header.cc:467
ns3::TcpHeader::m_goodChecksum
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition
tcp-header.h:335
ns3::TcpHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
tcp-header.cc:274
ns3::TcpHeader::GetSourcePort
uint16_t GetSourcePort() const
Get the source port.
Definition
tcp-header.cc:95
ns3::TcpHeader::SetSourcePort
void SetSourcePort(uint16_t port)
Set the source port.
Definition
tcp-header.cc:53
ns3::TcpHeader::m_ackNumber
SequenceNumber32 m_ackNumber
ACK number.
Definition
tcp-header.h:324
ns3::TcpHeader::EnableChecksums
void EnableChecksums()
Enable checksum calculation for TCP.
Definition
tcp-header.cc:47
ns3::TcpHeader::SetAckNumber
void SetAckNumber(SequenceNumber32 ackNumber)
Set the ACK number.
Definition
tcp-header.cc:71
ns3::TcpHeader::GetUrgentPointer
uint16_t GetUrgentPointer() const
Get the urgent pointer.
Definition
tcp-header.cc:149
ns3::TcpHeader::GetFlags
uint8_t GetFlags() const
Get the flags.
Definition
tcp-header.cc:137
ns3::TcpHeader::GetAckNumber
SequenceNumber32 GetAckNumber() const
Get the ACK number.
Definition
tcp-header.cc:113
ns3::TcpHeader::m_flags
uint8_t m_flags
Flags (really a uint6_t)
Definition
tcp-header.h:326
ns3::TcpHeader::IsChecksumOk
bool IsChecksumOk() const
Is the TCP checksum correct ?
Definition
tcp-header.cc:226
ns3::TcpHeader::m_destinationPort
uint16_t m_destinationPort
Destination port.
Definition
tcp-header.h:322
ns3::TcpHeader::m_options
TcpOptionList m_options
TcpOption present in the header.
Definition
tcp-header.h:338
ns3::TcpHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
tcp-header.cc:232
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
uint32_t
port
uint16_t port
Definition
dsdv-manet.cc:33
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
tcp-option.h
tcp-socket-factory.h
src
internet
model
tcp-header.h
Generated on Fri Nov 8 2024 13:59:01 for ns-3 by
1.11.0