A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-header.cc
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#include "tcp-header.h"
21
22#include "tcp-option.h"
23
24#include "ns3/address-utils.h"
25#include "ns3/buffer.h"
26#include "ns3/log.h"
27
28#include <iostream>
29#include <stdint.h>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("TcpHeader");
35
37
39 : m_sourcePort(0),
40 m_destinationPort(0),
41 m_sequenceNumber(0),
42 m_ackNumber(0),
43 m_length(5),
44 m_flags(0),
45 m_windowSize(0xffff),
46 m_urgentPointer(0),
47 m_calcChecksum(false),
48 m_goodChecksum(true),
49 m_optionsLen(0)
50{
51}
52
54{
55}
56
57std::string
58TcpHeader::FlagsToString(uint8_t flags, const std::string& delimiter)
59{
60 static const char* flagNames[8] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECE", "CWR"};
61 std::string flagsDescription = "";
62 for (uint8_t i = 0; i < 8; ++i)
63 {
64 if (flags & (1 << i))
65 {
66 if (flagsDescription.length() > 0)
67 {
68 flagsDescription += delimiter;
69 }
70 flagsDescription.append(flagNames[i]);
71 }
72 }
73 return flagsDescription;
74}
75
76void
78{
79 m_calcChecksum = true;
80}
81
82void
84{
86}
87
88void
90{
92}
93
94void
96{
97 m_sequenceNumber = sequenceNumber;
98}
99
100void
102{
103 m_ackNumber = ackNumber;
104}
105
106void
108{
109 m_flags = flags;
110}
111
112void
113TcpHeader::SetWindowSize(uint16_t windowSize)
114{
115 m_windowSize = windowSize;
116}
117
118void
119TcpHeader::SetUrgentPointer(uint16_t urgentPointer)
120{
121 m_urgentPointer = urgentPointer;
122}
123
124uint16_t
126{
127 return m_sourcePort;
128}
129
130uint16_t
132{
133 return m_destinationPort;
134}
135
138{
139 return m_sequenceNumber;
140}
141
144{
145 return m_ackNumber;
146}
147
148uint8_t
150{
151 return m_length;
152}
153
154uint8_t
156{
157 return m_optionsLen;
158}
159
160uint8_t
162{
163 return m_maxOptionsLen;
164}
165
166uint8_t
168{
169 return m_flags;
170}
171
172uint16_t
174{
175 return m_windowSize;
176}
177
178uint16_t
180{
181 return m_urgentPointer;
182}
183
184void
186 const Ipv4Address& destination,
187 uint8_t protocol)
188{
189 m_source = source;
190 m_destination = destination;
191 m_protocol = protocol;
192}
193
194void
196 const Ipv6Address& destination,
197 uint8_t protocol)
198{
199 m_source = source;
200 m_destination = destination;
201 m_protocol = protocol;
202}
203
204void
205TcpHeader::InitializeChecksum(const Address& source, const Address& destination, uint8_t protocol)
206{
207 m_source = source;
208 m_destination = destination;
209 m_protocol = protocol;
210}
211
212uint16_t
214{
215 /* Buffer size must be at least as large as the largest IP pseudo-header */
216 /* [per RFC2460, but without consideration for IPv6 extension hdrs] */
217 /* Src address 16 bytes (more generally, Address::MAX_SIZE) */
218 /* Dst address 16 bytes (more generally, Address::MAX_SIZE) */
219 /* Upper layer pkt len 4 bytes */
220 /* Zero 3 bytes */
221 /* Next header 1 byte */
222
223 uint32_t maxHdrSz = (2 * Address::MAX_SIZE) + 8;
224 Buffer buf = Buffer(maxHdrSz);
225 buf.AddAtStart(maxHdrSz);
226 Buffer::Iterator it = buf.Begin();
227 uint32_t hdrSize = 0;
228
229 WriteTo(it, m_source);
232 {
233 it.WriteU8(0); /* protocol */
234 it.WriteU8(m_protocol); /* protocol */
235 it.WriteU8(size >> 8); /* length */
236 it.WriteU8(size & 0xff); /* length */
237 hdrSize = 12;
238 }
239 else
240 {
241 it.WriteU16(0);
242 it.WriteU8(size >> 8); /* length */
243 it.WriteU8(size & 0xff); /* length */
244 it.WriteU16(0);
245 it.WriteU8(0);
246 it.WriteU8(m_protocol); /* protocol */
247 hdrSize = 40;
248 }
249
250 it = buf.Begin();
251 /* we don't CompleteChecksum ( ~ ) now */
252 return ~(it.CalculateIpChecksum(hdrSize));
253}
254
255bool
257{
258 return m_goodChecksum;
259}
260
261TypeId
263{
264 static TypeId tid = TypeId("ns3::TcpHeader")
265 .SetParent<Header>()
266 .SetGroupName("Internet")
267 .AddConstructor<TcpHeader>();
268 return tid;
269}
270
271TypeId
273{
274 return GetTypeId();
275}
276
277void
278TcpHeader::Print(std::ostream& os) const
279{
280 os << m_sourcePort << " > " << m_destinationPort;
281
282 if (m_flags != 0)
283 {
284 os << " [" << FlagsToString(m_flags) << "]";
285 }
286
287 os << " Seq=" << m_sequenceNumber << " Ack=" << m_ackNumber << " Win=" << m_windowSize;
288
289 TcpOptionList::const_iterator op;
290
291 for (op = m_options.begin(); op != m_options.end(); ++op)
292 {
293 os << " " << (*op)->GetInstanceTypeId().GetName() << "(";
294 (*op)->Print(os);
295 os << ")";
296 }
297}
298
301{
302 return CalculateHeaderLength() * 4;
303}
304
305void
307{
308 Buffer::Iterator i = start;
313 i.WriteHtonU16(GetLength() << 12 | m_flags); // reserved bits are all zero
315 i.WriteHtonU16(0);
317
318 // Serialize options if they exist
319 // This implementation does not presently try to align options on word
320 // boundaries using NOP options
321 uint32_t optionLen = 0;
322 TcpOptionList::const_iterator op;
323 for (op = m_options.begin(); op != m_options.end(); ++op)
324 {
325 optionLen += (*op)->GetSerializedSize();
326 (*op)->Serialize(i);
327 i.Next((*op)->GetSerializedSize());
328 }
329
330 // padding to word alignment; add ENDs and/or pad values (they are the same)
331 while (optionLen % 4)
332 {
334 ++optionLen;
335 }
336
337 // Make checksum
338 if (m_calcChecksum)
339 {
340 uint16_t headerChecksum = CalculateHeaderChecksum(start.GetSize());
341 i = start;
342 uint16_t checksum = i.CalculateIpChecksum(start.GetSize(), headerChecksum);
343
344 i = start;
345 i.Next(16);
346 i.WriteU16(checksum);
347 }
348}
349
352{
353 m_optionsLen = 0;
354 Buffer::Iterator i = start;
359 uint16_t field = i.ReadNtohU16();
360 m_flags = field & 0xFF;
361 m_length = field >> 12;
363 i.Next(2);
365
366 // Deserialize options if they exist
367 m_options.clear();
368 uint32_t optionLen = (m_length - 5) * 4;
369 if (optionLen > m_maxOptionsLen)
370 {
371 NS_LOG_ERROR("Illegal TCP option length " << optionLen << "; options discarded");
372 return 20;
373 }
374 while (optionLen)
375 {
376 uint8_t kind = i.PeekU8();
378 uint32_t optionSize;
379 if (TcpOption::IsKindKnown(kind))
380 {
381 op = TcpOption::CreateOption(kind);
382 }
383 else
384 {
386 NS_LOG_WARN("Option kind " << static_cast<int>(kind) << " unknown, skipping.");
387 }
388 optionSize = op->Deserialize(i);
389 if (optionSize != op->GetSerializedSize())
390 {
391 NS_LOG_ERROR("Option did not deserialize correctly");
392 break;
393 }
394 if (optionLen >= optionSize)
395 {
396 optionLen -= optionSize;
397 i.Next(optionSize);
398 m_options.emplace_back(op);
399 m_optionsLen += optionSize;
400 }
401 else
402 {
403 NS_LOG_ERROR("Option exceeds TCP option space; option discarded");
404 break;
405 }
406 if (op->GetKind() == TcpOption::END)
407 {
408 while (optionLen)
409 {
410 // Discard padding bytes without adding to option list
411 i.Next(1);
412 --optionLen;
413 ++m_optionsLen;
414 }
415 }
416 }
417
419 {
420 NS_LOG_ERROR("Mismatch between calculated length and in-header value");
421 }
422
423 // Do checksum
424 if (m_calcChecksum)
425 {
426 uint16_t headerChecksum = CalculateHeaderChecksum(start.GetSize());
427 i = start;
428 uint16_t checksum = i.CalculateIpChecksum(start.GetSize(), headerChecksum);
429 m_goodChecksum = (checksum == 0);
430 }
431
432 return GetSerializedSize();
433}
434
435uint8_t
437{
438 uint32_t len = 20;
439 TcpOptionList::const_iterator i;
440
441 for (i = m_options.begin(); i != m_options.end(); ++i)
442 {
443 len += (*i)->GetSerializedSize();
444 }
445 // Option list may not include padding; need to pad up to word boundary
446 if (len % 4)
447 {
448 len += 4 - (len % 4);
449 }
450 return len >> 2;
451}
452
453bool
455{
456 if (m_optionsLen + option->GetSerializedSize() <= m_maxOptionsLen)
457 {
458 if (!TcpOption::IsKindKnown(option->GetKind()))
459 {
460 NS_LOG_WARN("The option kind " << static_cast<int>(option->GetKind()) << " is unknown");
461 return false;
462 }
463
464 if (option->GetKind() != TcpOption::END)
465 {
466 m_options.push_back(option);
467 m_optionsLen += option->GetSerializedSize();
468
469 uint32_t totalLen = 20 + 3 + m_optionsLen;
470 m_length = totalLen >> 2;
471 }
472
473 return true;
474 }
475
476 return false;
477}
478
481{
482 return m_options;
483}
484
486TcpHeader::GetOption(uint8_t kind) const
487{
488 TcpOptionList::const_iterator i;
489
490 for (i = m_options.begin(); i != m_options.end(); ++i)
491 {
492 if ((*i)->GetKind() == kind)
493 {
494 return (*i);
495 }
496 }
497
498 return nullptr;
499}
500
501bool
502TcpHeader::HasOption(uint8_t kind) const
503{
504 TcpOptionList::const_iterator i;
505
506 for (i = m_options.begin(); i != m_options.end(); ++i)
507 {
508 if ((*i)->GetKind() == kind)
509 {
510 return true;
511 }
512 }
513
514 return false;
515}
516
517bool
518operator==(const TcpHeader& lhs, const TcpHeader& rhs)
519{
520 return (lhs.m_sourcePort == rhs.m_sourcePort &&
522 lhs.m_sequenceNumber == rhs.m_sequenceNumber && lhs.m_ackNumber == rhs.m_ackNumber &&
523 lhs.m_flags == rhs.m_flags && lhs.m_windowSize == rhs.m_windowSize &&
525}
526
527std::ostream&
528operator<<(std::ostream& os, const TcpHeader& tc)
529{
530 tc.Print(os);
531 return os;
532}
533
534} // namespace ns3
a polymophic address class
Definition: address.h:100
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
Definition: address.h:106
iterator in a Buffer instance
Definition: buffer.h:100
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition: buffer.cc:1141
void WriteU8(uint8_t data)
Definition: buffer.h:881
void WriteU16(uint16_t data)
Definition: buffer.cc:865
void WriteHtonU16(uint16_t data)
Definition: buffer.h:915
uint32_t ReadNtohU32()
Definition: buffer.h:978
uint8_t PeekU8()
Definition: buffer.h:1006
void WriteHtonU32(uint32_t data)
Definition: buffer.h:933
uint16_t ReadNtohU16()
Definition: buffer.h:954
void Next()
go forward by one byte
Definition: buffer.h:853
automatically resized byte buffer
Definition: buffer.h:94
void AddAtStart(uint32_t start)
Definition: buffer.cc:314
Buffer::Iterator Begin() const
Definition: buffer.h:1074
Protocol header serialization and deserialization.
Definition: header.h:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static bool IsMatchingType(const Address &address)
Describes an IPv6 address.
Definition: ipv6-address.h:49
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
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
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
Definition: tcp-header.cc:137
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
uint32_t Deserialize(Buffer::Iterator start) override
Definition: tcp-header.cc:351
Ptr< const TcpOption > GetOption(uint8_t kind) const
Get the option specified.
Definition: tcp-header.cc:486
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
static Ptr< TcpOption > CreateOption(uint8_t kind)
Creates an option.
Definition: tcp-option.cc:62
static bool IsKindKnown(uint8_t kind)
Check if the option is implemented.
Definition: tcp-option.cc:95
@ UNKNOWN
not a standardized value; for unknown recv'd options
Definition: tcp-option.h:65
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.