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 "ns3/buffer.h"
24 #include "ns3/header.h"
25 #include "ns3/ipv4-address.h"
26 #include "ns3/ipv6-address.h"
27 #include "ns3/sequence-number.h"
28 #include "ns3/tcp-option.h"
29 #include "ns3/tcp-socket-factory.h"
30 
31 #include <stdint.h>
32 
33 namespace ns3
34 {
35 
45 class TcpHeader : public Header
46 {
47  public:
48  TcpHeader();
49  ~TcpHeader() override;
50 
51  typedef std::list<Ptr<const TcpOption>> TcpOptionList;
52 
60  friend std::ostream& operator<<(std::ostream& os, const TcpHeader& tc);
61 
77  static std::string FlagsToString(uint8_t flags, const std::string& delimiter = "|");
78 
84  void EnableChecksums();
85 
86  // Setters
87 
92  void SetSourcePort(uint16_t port);
93 
98  void SetDestinationPort(uint16_t port);
99 
104  void SetSequenceNumber(SequenceNumber32 sequenceNumber);
105 
110  void SetAckNumber(SequenceNumber32 ackNumber);
111 
116  void SetFlags(uint8_t flags);
117 
122  void SetWindowSize(uint16_t windowSize);
123 
128  void SetUrgentPointer(uint16_t urgentPointer);
129 
130  // Getters
131 
136  uint16_t GetSourcePort() const;
137 
142  uint16_t GetDestinationPort() const;
143 
149 
155 
164  uint8_t GetLength() const;
165 
170  uint8_t GetFlags() const;
171 
176  uint16_t GetWindowSize() const;
177 
182  uint16_t GetUrgentPointer() const;
183 
189  Ptr<const TcpOption> GetOption(uint8_t kind) const;
190 
195  const TcpOptionList& GetOptionList() const;
196 
201  uint8_t GetOptionLength() const;
202 
207  uint8_t GetMaxOptionLength() const;
208 
214  bool HasOption(uint8_t kind) const;
215 
221  bool AppendOption(Ptr<const TcpOption> option);
222 
237  void InitializeChecksum(const Ipv4Address& source,
238  const Ipv4Address& destination,
239  uint8_t protocol);
240 
255  void InitializeChecksum(const Ipv6Address& source,
256  const Ipv6Address& destination,
257  uint8_t protocol);
258 
273  void InitializeChecksum(const Address& source, const Address& destination, uint8_t protocol);
274 
278  enum Flags_t
279  {
280  NONE = 0,
281  FIN = 1,
282  SYN = 2,
283  RST = 4,
284  PSH = 8,
285  ACK = 16,
286  URG = 32,
287  ECE = 64,
288  CWR = 128
289  };
290 
295  static TypeId GetTypeId();
296  TypeId GetInstanceTypeId() const override;
297  void Print(std::ostream& os) const override;
298  uint32_t GetSerializedSize() const override;
299  void Serialize(Buffer::Iterator start) const override;
300  uint32_t Deserialize(Buffer::Iterator start) override;
301 
306  bool IsChecksumOk() const;
307 
314  friend bool operator==(const TcpHeader& lhs, const TcpHeader& rhs);
315 
316  private:
322  uint16_t CalculateHeaderChecksum(uint16_t size) const;
323 
332  uint8_t CalculateHeaderLength() const;
333 
334  uint16_t m_sourcePort;
335  uint16_t m_destinationPort;
338  uint8_t m_length;
339  uint8_t m_flags;
340  uint16_t m_windowSize;
341  uint16_t m_urgentPointer;
342 
345  uint8_t m_protocol;
346 
349 
350  static const uint8_t m_maxOptionsLen = 40;
352  uint8_t m_optionsLen;
353 };
354 
355 } // namespace ns3
356 
357 #endif /* TCP_HEADER */
a polymophic address class
Definition: address.h:100
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:43
Describes an IPv6 address.
Definition: ipv6-address.h:50
Header for the Transmission Control Protocol.
Definition: tcp-header.h:46
uint16_t m_urgentPointer
Urgent pointer.
Definition: tcp-header.h:341
void SetUrgentPointer(uint16_t urgentPointer)
Set the urgent pointer.
Definition: tcp-header.cc:119
Address m_source
Source IP address.
Definition: tcp-header.h:343
void Print(std::ostream &os) const override
Definition: tcp-header.cc:278
void SetDestinationPort(uint16_t port)
Set the destination port.
Definition: tcp-header.cc:89
void SetSequenceNumber(SequenceNumber32 sequenceNumber)
Set the sequence Number.
Definition: tcp-header.cc:95
uint8_t m_optionsLen
Tcp options length.
Definition: tcp-header.h:352
friend std::ostream & operator<<(std::ostream &os, const TcpHeader &tc)
Print a TCP header into an output stream.
Definition: tcp-header.cc:528
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
Definition: tcp-header.cc:137
friend bool operator==(const TcpHeader &lhs, const TcpHeader &rhs)
Comparison operator.
Definition: tcp-header.cc:518
uint8_t GetLength() const
Get the length in words.
Definition: tcp-header.cc:149
uint8_t GetMaxOptionLength() const
Get maximum option length.
Definition: tcp-header.cc:161
uint16_t m_sourcePort
Source port.
Definition: tcp-header.h:334
uint16_t GetDestinationPort() const
Get the destination port.
Definition: tcp-header.cc:131
std::list< Ptr< const TcpOption > > TcpOptionList
List of TcpOption.
Definition: tcp-header.h:51
uint8_t CalculateHeaderLength() const
Calculates the header length (in words)
Definition: tcp-header.cc:436
uint8_t m_length
Length (really a uint4_t) in words.
Definition: tcp-header.h:338
static const uint8_t m_maxOptionsLen
Maximum options length.
Definition: tcp-header.h:350
Ptr< const TcpOption > GetOption(uint8_t kind) const
Get the option specified.
Definition: tcp-header.cc:486
Flags_t
TCP flag field values.
Definition: tcp-header.h:279
@ URG
Urgent.
Definition: tcp-header.h:286
@ NONE
No flags.
Definition: tcp-header.h:280
void SetFlags(uint8_t flags)
Set flags of the header.
Definition: tcp-header.cc:107
void SetWindowSize(uint16_t windowSize)
Set the window size.
Definition: tcp-header.cc:113
uint32_t GetSerializedSize() const override
Definition: tcp-header.cc:300
const TcpOptionList & GetOptionList() const
Get the list of option in this header.
Definition: tcp-header.cc:480
bool m_calcChecksum
Flag to calculate checksum.
Definition: tcp-header.h:347
uint16_t GetWindowSize() const
Get the window size.
Definition: tcp-header.cc:173
void InitializeChecksum(const Ipv4Address &source, const Ipv4Address &destination, uint8_t protocol)
Initialize the TCP checksum.
Definition: tcp-header.cc:185
Address m_destination
Destination IP address.
Definition: tcp-header.h:344
~TcpHeader() override
Definition: tcp-header.cc:53
uint8_t m_protocol
Protocol number.
Definition: tcp-header.h:345
uint8_t GetOptionLength() const
Get the total length of appended options.
Definition: tcp-header.cc:155
SequenceNumber32 m_sequenceNumber
Sequence number.
Definition: tcp-header.h:336
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition: tcp-header.cc:213
bool AppendOption(Ptr< const TcpOption > option)
Append an option to the TCP header.
Definition: tcp-header.cc:454
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:58
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: tcp-header.cc:272
uint16_t m_windowSize
Window size.
Definition: tcp-header.h:340
bool HasOption(uint8_t kind) const
Check if the header has the option specified.
Definition: tcp-header.cc:502
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition: tcp-header.h:348
void Serialize(Buffer::Iterator start) const override
Definition: tcp-header.cc:306
uint16_t GetSourcePort() const
Get the source port.
Definition: tcp-header.cc:125
void SetSourcePort(uint16_t port)
Set the source port.
Definition: tcp-header.cc:83
SequenceNumber32 m_ackNumber
ACK number.
Definition: tcp-header.h:337
void EnableChecksums()
Enable checksum calculation for TCP.
Definition: tcp-header.cc:77
void SetAckNumber(SequenceNumber32 ackNumber)
Set the ACK number.
Definition: tcp-header.cc:101
uint16_t GetUrgentPointer() const
Get the urgent pointer.
Definition: tcp-header.cc:179
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:167
SequenceNumber32 GetAckNumber() const
Get the ACK number.
Definition: tcp-header.cc:143
uint8_t m_flags
Flags (really a uint6_t)
Definition: tcp-header.h:339
bool IsChecksumOk() const
Is the TCP checksum correct ?
Definition: tcp-header.cc:256
uint16_t m_destinationPort
Destination port.
Definition: tcp-header.h:335
TcpOptionList m_options
TcpOption present in the header.
Definition: tcp-header.h:351
static TypeId GetTypeId()
Get the type ID.
Definition: tcp-header.cc:262
a unique identifier for an interface.
Definition: type-id.h:60
uint16_t port
Definition: dsdv-manet.cc:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.