A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tcp-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 Georgia Tech Research Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Raj Bhattacharjea <raj.b@gatech.edu>
19  */
20 
21 #include <stdint.h>
22 #include <iostream>
23 #include "tcp-header.h"
24 #include "ns3/buffer.h"
25 #include "ns3/address-utils.h"
26 
27 namespace ns3 {
28 
30  ;
31 
33  : m_sourcePort (0),
34  m_destinationPort (0),
35  m_sequenceNumber (0),
36  m_ackNumber (0),
37  m_length (5),
38  m_flags (0),
39  m_windowSize (0xffff),
40  m_urgentPointer (0),
41  m_calcChecksum (false),
42  m_goodChecksum (true)
43 {
44 }
45 
47 {
48 }
49 
50 void
52 {
53  m_calcChecksum = true;
54 }
55 
57 {
59 }
61 {
63 }
65 {
66  m_sequenceNumber = sequenceNumber;
67 }
69 {
70  m_ackNumber = ackNumber;
71 }
72 void TcpHeader::SetLength (uint8_t length)
73 {
74  m_length = length;
75 }
76 void TcpHeader::SetFlags (uint8_t flags)
77 {
78  m_flags = flags;
79 }
80 void TcpHeader::SetWindowSize (uint16_t windowSize)
81 {
82  m_windowSize = windowSize;
83 }
84 void TcpHeader::SetUrgentPointer (uint16_t urgentPointer)
85 {
86  m_urgentPointer = urgentPointer;
87 }
88 
89 uint16_t TcpHeader::GetSourcePort () const
90 {
91  return m_sourcePort;
92 }
94 {
95  return m_destinationPort;
96 }
98 {
99  return m_sequenceNumber;
100 }
102 {
103  return m_ackNumber;
104 }
105 uint8_t TcpHeader::GetLength () const
106 {
107  return m_length;
108 }
109 uint8_t TcpHeader::GetFlags () const
110 {
111  return m_flags;
112 }
113 uint16_t TcpHeader::GetWindowSize () const
114 {
115  return m_windowSize;
116 }
118 {
119  return m_urgentPointer;
120 }
121 
122 void
124  Ipv4Address destination,
125  uint8_t protocol)
126 {
127  m_source = source;
128  m_destination = destination;
129  m_protocol = protocol;
130 }
131 
132 void
134  Ipv6Address destination,
135  uint8_t protocol)
136 {
137  m_source = source;
138  m_destination = destination;
139  m_protocol = protocol;
140 }
141 
142 void
144  Address destination,
145  uint8_t protocol)
146 {
147  m_source = source;
148  m_destination = destination;
149  m_protocol = protocol;
150 }
151 
152 uint16_t
154 {
155  /* Buffer size must be at least as large as the largest IP pseudo-header */
156  /* [per RFC2460, but without consideration for IPv6 extension hdrs] */
157  /* Src address 16 bytes (more generally, Address::MAX_SIZE) */
158  /* Dst address 16 bytes (more generally, Address::MAX_SIZE) */
159  /* Upper layer pkt len 4 bytes */
160  /* Zero 3 bytes */
161  /* Next header 1 byte */
162 
163  uint32_t maxHdrSz = (2 * Address::MAX_SIZE) + 8;
164  Buffer buf = Buffer (maxHdrSz);
165  buf.AddAtStart (maxHdrSz);
166  Buffer::Iterator it = buf.Begin ();
167  uint32_t hdrSize = 0;
168 
169  WriteTo (it, m_source);
170  WriteTo (it, m_destination);
172  {
173  it.WriteU8 (0); /* protocol */
174  it.WriteU8 (m_protocol); /* protocol */
175  it.WriteU8 (size >> 8); /* length */
176  it.WriteU8 (size & 0xff); /* length */
177  hdrSize = 12;
178  }
179  else
180  {
181  it.WriteU16 (0);
182  it.WriteU8 (size >> 8); /* length */
183  it.WriteU8 (size & 0xff); /* length */
184  it.WriteU16 (0);
185  it.WriteU8 (0);
186  it.WriteU8 (m_protocol); /* protocol */
187  hdrSize = 40;
188  }
189 
190  it = buf.Begin ();
191  /* we don't CompleteChecksum ( ~ ) now */
192  return ~(it.CalculateIpChecksum (hdrSize));
193 }
194 
195 bool
197 {
198  return m_goodChecksum;
199 }
200 
201 TypeId
203 {
204  static TypeId tid = TypeId ("ns3::TcpHeader")
205  .SetParent<Header> ()
206  .AddConstructor<TcpHeader> ()
207  ;
208  return tid;
209 }
210 TypeId
212 {
213  return GetTypeId ();
214 }
215 void TcpHeader::Print (std::ostream &os) const
216 {
217  os << m_sourcePort << " > " << m_destinationPort;
218  if(m_flags!=0)
219  {
220  os<<" [";
221  if((m_flags & FIN) != 0)
222  {
223  os<<" FIN ";
224  }
225  if((m_flags & SYN) != 0)
226  {
227  os<<" SYN ";
228  }
229  if((m_flags & RST) != 0)
230  {
231  os<<" RST ";
232  }
233  if((m_flags & PSH) != 0)
234  {
235  os<<" PSH ";
236  }
237  if((m_flags & ACK) != 0)
238  {
239  os<<" ACK ";
240  }
241  if((m_flags & URG) != 0)
242  {
243  os<<" URG ";
244  }
245  if((m_flags & ECE) != 0)
246  {
247  os<<" ECE ";
248  }
249  if((m_flags & CWR) != 0)
250  {
251  os<<" CWR ";
252  }
253  os<<"]";
254  }
255  os<<" Seq="<<m_sequenceNumber<<" Ack="<<m_ackNumber<<" Win="<<m_windowSize;
256 }
257 uint32_t TcpHeader::GetSerializedSize (void) const
258 {
259  return 4*m_length;
260 }
262 {
268  i.WriteHtonU16 (m_length << 12 | m_flags); //reserved bits are all zero
270  i.WriteHtonU16 (0);
272 
273  if(m_calcChecksum)
274  {
275  uint16_t headerChecksum = CalculateHeaderChecksum (start.GetSize ());
276  i = start;
277  uint16_t checksum = i.CalculateIpChecksum (start.GetSize (), headerChecksum);
278 
279  i = start;
280  i.Next (16);
281  i.WriteU16 (checksum);
282  }
283 }
285 {
287  m_sourcePort = i.ReadNtohU16 ();
290  m_ackNumber = i.ReadNtohU32 ();
291  uint16_t field = i.ReadNtohU16 ();
292  m_flags = field & 0x3F;
293  m_length = field>>12;
294  m_windowSize = i.ReadNtohU16 ();
295  i.Next (2);
297 
298  if(m_calcChecksum)
299  {
300  uint16_t headerChecksum = CalculateHeaderChecksum (start.GetSize ());
301  i = start;
302  uint16_t checksum = i.CalculateIpChecksum (start.GetSize (), headerChecksum);
303  m_goodChecksum = (checksum == 0);
304  }
305 
306  return GetSerializedSize ();
307 }
308 
309 
310 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition: buffer.cc:1158
uint16_t GetDestinationPort() const
Definition: tcp-header.cc:93
SequenceNumber32 GetSequenceNumber() const
Definition: tcp-header.cc:97
void InitializeChecksum(Ipv4Address source, Ipv4Address destination, uint8_t protocol)
Initialize the TCP checksum.
Definition: tcp-header.cc:123
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
uint8_t GetFlags() const
Definition: tcp-header.cc:109
SequenceNumber32 GetAckNumber() const
Definition: tcp-header.cc:101
automatically resized byte buffer
Definition: buffer.h:92
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
Address m_source
Source IP address.
Definition: tcp-header.h:217
uint32_t ReadNtohU32(void)
Definition: buffer.h:791
uint16_t m_destinationPort
Destination port.
Definition: tcp-header.h:209
bool m_calcChecksum
Flag to calculate checksum.
Definition: tcp-header.h:221
uint16_t port
Definition: dsdv-manet.cc:44
iterator in a Buffer instance
Definition: buffer.h:98
a polymophic address class
Definition: address.h:86
virtual void Print(std::ostream &os) const
Definition: tcp-header.cc:215
void SetSequenceNumber(SequenceNumber32 sequenceNumber)
Definition: tcp-header.cc:64
uint16_t GetWindowSize() const
Definition: tcp-header.cc:113
void WriteU16(uint16_t data)
Definition: buffer.cc:895
void WriteHtonU16(uint16_t data)
Definition: buffer.h:726
virtual TypeId GetInstanceTypeId(void) const
Definition: tcp-header.cc:211
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition: tcp-header.cc:153
void Next(void)
go forward by one byte
Definition: buffer.h:666
static bool IsMatchingType(const Address &address)
Buffer::Iterator Begin(void) const
Definition: buffer.h:875
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition: tcp-header.h:222
void SetDestinationPort(uint16_t port)
Definition: tcp-header.cc:60
void SetFlags(uint8_t flags)
Definition: tcp-header.cc:76
uint16_t m_windowSize
Window size.
Definition: tcp-header.h:214
uint16_t m_sourcePort
Source port.
Definition: tcp-header.h:208
void SetSourcePort(uint16_t port)
Definition: tcp-header.cc:56
virtual ~TcpHeader()
Definition: tcp-header.cc:46
Address m_destination
Destination IP address.
Definition: tcp-header.h:218
void SetUrgentPointer(uint16_t urgentPointer)
Definition: tcp-header.cc:84
void WriteHtonU32(uint32_t data)
Definition: buffer.h:745
virtual uint32_t GetSerializedSize(void) const
Definition: tcp-header.cc:257
uint8_t GetLength() const
Definition: tcp-header.cc:105
uint8_t m_protocol
Protocol number.
Definition: tcp-header.h:219
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: tcp-header.cc:284
uint16_t m_urgentPointer
Urgent pointer.
Definition: tcp-header.h:215
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
SequenceNumber32 m_ackNumber
ACK number.
Definition: tcp-header.h:211
uint8_t m_flags
Flags (really a uint6_t)
Definition: tcp-header.h:213
void WriteU8(uint8_t data)
Definition: buffer.h:690
uint8_t m_length
Length (really a uint4_t)
Definition: tcp-header.h:212
void SetAckNumber(SequenceNumber32 ackNumber)
Definition: tcp-header.cc:68
uint16_t GetSourcePort() const
Definition: tcp-header.cc:89
bool AddAtStart(uint32_t start)
Definition: buffer.cc:305
void SetLength(uint8_t length)
Definition: tcp-header.cc:72
bool IsChecksumOk(void) const
Is the TCP checksum correct ?
Definition: tcp-header.cc:196
void EnableChecksums(void)
Enable checksum calculation for TCP.
Definition: tcp-header.cc:51
uint16_t ReadNtohU16(void)
Definition: buffer.h:767
void SetWindowSize(uint16_t windowSize)
Definition: tcp-header.cc:80
uint32_t GetSize(void) const
Definition: buffer.cc:1183
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
uint16_t GetUrgentPointer() const
Definition: tcp-header.cc:117
virtual void Serialize(Buffer::Iterator start) const
Definition: tcp-header.cc:261
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-header.cc:202
SequenceNumber32 m_sequenceNumber
Sequence number.
Definition: tcp-header.h:210