A Discrete-Event Network Simulator
API
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 
34 namespace ns3
35 {
36 
46 class TcpHeader : public Header
47 {
48  public:
49  typedef std::list<Ptr<const TcpOption>> TcpOptionList;
50 
58  friend std::ostream& operator<<(std::ostream& os, const TcpHeader& tc);
59 
75  static std::string FlagsToString(uint8_t flags, const std::string& delimiter = "|");
76 
82  void EnableChecksums();
83 
84  // Setters
85 
90  void SetSourcePort(uint16_t port);
91 
96  void SetDestinationPort(uint16_t port);
97 
102  void SetSequenceNumber(SequenceNumber32 sequenceNumber);
103 
108  void SetAckNumber(SequenceNumber32 ackNumber);
109 
114  void SetFlags(uint8_t flags);
115 
120  void SetWindowSize(uint16_t windowSize);
121 
126  void SetUrgentPointer(uint16_t urgentPointer);
127 
128  // Getters
129 
134  uint16_t GetSourcePort() const;
135 
140  uint16_t GetDestinationPort() const;
141 
147 
153 
162  uint8_t GetLength() const;
163 
168  uint8_t GetFlags() const;
169 
174  uint16_t GetWindowSize() const;
175 
180  uint16_t GetUrgentPointer() const;
181 
187  Ptr<const TcpOption> GetOption(uint8_t kind) const;
188 
193  const TcpOptionList& GetOptionList() const;
194 
199  uint8_t GetOptionLength() const;
200 
205  uint8_t GetMaxOptionLength() const;
206 
212  bool HasOption(uint8_t kind) const;
213 
219  bool AppendOption(Ptr<const TcpOption> option);
220 
235  void InitializeChecksum(const Ipv4Address& source,
236  const Ipv4Address& destination,
237  uint8_t protocol);
238 
253  void InitializeChecksum(const Ipv6Address& source,
254  const Ipv6Address& destination,
255  uint8_t protocol);
256 
271  void InitializeChecksum(const Address& source, const Address& destination, uint8_t protocol);
272 
276  enum Flags_t
277  {
278  NONE = 0,
279  FIN = 1,
280  SYN = 2,
281  RST = 4,
282  PSH = 8,
283  ACK = 16,
284  URG = 32,
285  ECE = 64,
286  CWR = 128
287  };
288 
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 
304  bool IsChecksumOk() const;
305 
312  friend bool operator==(const TcpHeader& lhs, const TcpHeader& rhs);
313 
314  private:
320  uint16_t CalculateHeaderChecksum(uint16_t size) const;
321 
330  uint8_t CalculateHeaderLength() const;
331 
332  uint16_t m_sourcePort{0};
333  uint16_t m_destinationPort{0};
336  uint8_t m_length{5};
337  uint8_t m_flags{0};
338  uint16_t m_windowSize{0xffff};
339  uint16_t m_urgentPointer{0};
340 
343  uint8_t m_protocol{6};
344 
345  bool m_calcChecksum{false};
346  bool m_goodChecksum{true};
347 
348  static const uint8_t m_maxOptionsLen = 40;
350  uint8_t m_optionsLen{0};
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
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
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
friend std::ostream & operator<<(std::ostream &os, const TcpHeader &tc)
Print a TCP header into an output stream.
Definition: tcp-header.cc:502
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
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
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.