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