A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-option-sack.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Adrian Sai-wah Tam
3 * Copyright (c) 2015 ResiliNets, ITTC, University of Kansas
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 * Original Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
19 * Documentation, test cases: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
20 * ResiliNets Research Group https://resilinets.org/
21 * The University of Kansas
22 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
23 */
24
25#include "tcp-option-sack.h"
26
27#include "ns3/log.h"
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("TcpOptionSack");
33
34NS_OBJECT_ENSURE_REGISTERED(TcpOptionSack);
35
37 : TcpOption()
38{
39}
40
42{
43}
44
47{
48 static TypeId tid = TypeId("ns3::TcpOptionSack")
50 .SetGroupName("Internet")
51 .AddConstructor<TcpOptionSack>();
52 return tid;
53}
54
57{
58 return GetTypeId();
59}
60
61void
62TcpOptionSack::Print(std::ostream& os) const
63{
64 os << "blocks: " << GetNumSackBlocks() << ",";
65 for (auto it = m_sackList.begin(); it != m_sackList.end(); ++it)
66 {
67 os << "[" << it->first << "," << it->second << "]";
68 }
69}
70
73{
74 NS_LOG_FUNCTION(this);
75 NS_LOG_LOGIC("Serialized size: " << 2 + GetNumSackBlocks() * 8);
76 return 2 + GetNumSackBlocks() * 8;
77}
78
79void
81{
82 NS_LOG_FUNCTION(this);
83 Buffer::Iterator i = start;
84 i.WriteU8(GetKind()); // Kind
85 auto length = static_cast<uint8_t>(GetNumSackBlocks() * 8 + 2);
86 i.WriteU8(length); // Length
87
88 for (auto it = m_sackList.begin(); it != m_sackList.end(); ++it)
89 {
90 SequenceNumber32 leftEdge = it->first;
91 SequenceNumber32 rightEdge = it->second;
92 i.WriteHtonU32(leftEdge.GetValue()); // Left edge of the block
93 i.WriteHtonU32(rightEdge.GetValue()); // Right edge of the block
94 }
95}
96
99{
100 NS_LOG_FUNCTION(this);
101 Buffer::Iterator i = start;
102 uint8_t readKind = i.ReadU8();
103 if (readKind != GetKind())
104 {
105 NS_LOG_WARN("Malformed SACK option, wrong type");
106 return 0;
107 }
108
109 uint8_t size = i.ReadU8();
110 NS_LOG_LOGIC("Size: " << static_cast<uint32_t>(size));
111 m_sackList.clear();
112 uint8_t sackCount = (size - 2) / 8;
113 while (sackCount)
114 {
115 SequenceNumber32 leftEdge(i.ReadNtohU32());
116 SequenceNumber32 rightEdge(i.ReadNtohU32());
117 SackBlock s(leftEdge, rightEdge);
118 AddSackBlock(s);
119 sackCount--;
120 }
121
122 return GetSerializedSize();
123}
124
125uint8_t
127{
128 return TcpOption::SACK;
129}
130
131void
133{
134 NS_LOG_FUNCTION(this);
135 m_sackList.push_back(s);
136}
137
140{
141 NS_LOG_FUNCTION(this);
142 NS_LOG_LOGIC("Number of SACK blocks appended: " << m_sackList.size());
143 return static_cast<uint32_t>(m_sackList.size());
144}
145
146void
148{
149 m_sackList.clear();
150}
151
154{
155 NS_LOG_FUNCTION(this);
156 return m_sackList;
157}
158
159std::ostream&
160operator<<(std::ostream& os, const TcpOptionSack& sackOption)
161{
162 std::stringstream ss;
163 ss << "{";
164 for (auto it = sackOption.m_sackList.begin(); it != sackOption.m_sackList.end(); ++it)
165 {
166 ss << *it;
167 }
168 ss << "}";
169 os << ss.str();
170 return os;
171}
172
173std::ostream&
174operator<<(std::ostream& os, const TcpOptionSack::SackBlock& sackBlock)
175{
176 std::stringstream ss;
177 ss << "[" << sackBlock.first << ";" << sackBlock.second << "]";
178 os << ss.str();
179 return os;
180}
181
182} // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
uint32_t ReadNtohU32()
Definition: buffer.h:978
void WriteHtonU32(uint32_t data)
Definition: buffer.h:933
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
Base class for all kinds of TCP options.
Definition: tcp-option.h:38
Defines the TCP option of kind 5 (selective acknowledgment option) as in RFC 2018
void ClearSackList()
Clear the SACK list.
std::list< SackBlock > SackList
SACK list definition.
uint32_t GetNumSackBlocks() const
Count the total number of SACK blocks.
uint8_t GetKind() const override
Get the ‘kind’ (as in RFC 793) of this option.
void Print(std::ostream &os) const override
Print the Option contents.
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
std::pair< SequenceNumber32, SequenceNumber32 > SackBlock
SACK block definition.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
static TypeId GetTypeId()
Get the type ID.
void AddSackBlock(SackBlock s)
Add a SACK block.
~TcpOptionSack() override
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
SackList m_sackList
the list of SACK blocks
SackList GetSackList() const
Get the SACK list.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#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.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159