A Discrete-Event Network Simulator
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
23namespace 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
35class 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 */
136
137 /**
138 * @brief Get the ACK number
139 * @return the ACK number for this TcpHeader
140 */
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 */
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 */
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 */
a polymophic address class
Definition address.h:90
iterator in a Buffer instance
Definition buffer.h:89
Protocol header serialization and deserialization.
Definition header.h:33
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
Smart pointer class similar to boost::intrusive_ptr.
Header for the Transmission Control Protocol.
Definition tcp-header.h:36
uint16_t m_urgentPointer
Urgent pointer.
Definition tcp-header.h:328
void SetUrgentPointer(uint16_t urgentPointer)
Set the urgent pointer.
Definition tcp-header.cc:89
Address m_source
Source IP address.
Definition tcp-header.h:330
void Print(std::ostream &os) const override
void SetDestinationPort(uint16_t port)
Set the destination port.
Definition tcp-header.cc:59
void SetSequenceNumber(SequenceNumber32 sequenceNumber)
Set the sequence Number.
Definition tcp-header.cc:65
uint8_t m_optionsLen
Tcp options length.
Definition tcp-header.h:339
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
friend bool operator==(const TcpHeader &lhs, const TcpHeader &rhs)
Comparison operator.
uint8_t GetLength() const
Get the length in words.
uint8_t GetMaxOptionLength() const
Get maximum option length.
uint16_t m_sourcePort
Source port.
Definition tcp-header.h:321
uint16_t GetDestinationPort() const
Get the destination port.
std::list< Ptr< const TcpOption > > TcpOptionList
List of TcpOption.
Definition tcp-header.h:38
uint8_t CalculateHeaderLength() const
Calculates the header length (in words)
uint8_t m_length
Length (really a uint4_t) in words.
Definition tcp-header.h:325
static const uint8_t m_maxOptionsLen
Maximum options length.
Definition tcp-header.h:337
uint32_t Deserialize(Buffer::Iterator start) override
Ptr< const TcpOption > GetOption(uint8_t kind) const
Get the option specified.
Flags_t
TCP flag field values.
Definition tcp-header.h:266
@ NONE
No flags.
Definition tcp-header.h:267
friend std::ostream & operator<<(std::ostream &os, const TcpHeader &tc)
Print a TCP header into an output stream.
void SetFlags(uint8_t flags)
Set flags of the header.
Definition tcp-header.cc:77
void SetWindowSize(uint16_t windowSize)
Set the window size.
Definition tcp-header.cc:83
uint32_t GetSerializedSize() const override
const TcpOptionList & GetOptionList() const
Get the list of option in this header.
bool m_calcChecksum
Flag to calculate checksum.
Definition tcp-header.h:334
uint16_t GetWindowSize() const
Get the window size.
void InitializeChecksum(const Ipv4Address &source, const Ipv4Address &destination, uint8_t protocol)
Initialize the TCP checksum.
Address m_destination
Destination IP address.
Definition tcp-header.h:331
uint8_t m_protocol
Protocol number.
Definition tcp-header.h:332
uint8_t GetOptionLength() const
Get the total length of appended options.
SequenceNumber32 m_sequenceNumber
Sequence number.
Definition tcp-header.h:323
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
bool AppendOption(Ptr< const TcpOption > option)
Append an option to the TCP header.
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
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint16_t m_windowSize
Window size.
Definition tcp-header.h:327
bool HasOption(uint8_t kind) const
Check if the header has the option specified.
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition tcp-header.h:335
void Serialize(Buffer::Iterator start) const override
uint16_t GetSourcePort() const
Get the source port.
Definition tcp-header.cc:95
void SetSourcePort(uint16_t port)
Set the source port.
Definition tcp-header.cc:53
SequenceNumber32 m_ackNumber
ACK number.
Definition tcp-header.h:324
void EnableChecksums()
Enable checksum calculation for TCP.
Definition tcp-header.cc:47
void SetAckNumber(SequenceNumber32 ackNumber)
Set the ACK number.
Definition tcp-header.cc:71
uint16_t GetUrgentPointer() const
Get the urgent pointer.
uint8_t GetFlags() const
Get the flags.
SequenceNumber32 GetAckNumber() const
Get the ACK number.
uint8_t m_flags
Flags (really a uint6_t)
Definition tcp-header.h:326
bool IsChecksumOk() const
Is the TCP checksum correct ?
uint16_t m_destinationPort
Destination port.
Definition tcp-header.h:322
TcpOptionList m_options
TcpOption present in the header.
Definition tcp-header.h:338
static TypeId GetTypeId()
Get the type ID.
a unique identifier for an interface.
Definition type-id.h:48
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.