A Discrete-Event Network Simulator
API
tcp-option-sack.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Adrian Sai-wah Tam
4  * Copyright (c) 2015 ResiliNets, ITTC, University of Kansas
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Original Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
20  * Documentation, test cases: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * The University of Kansas
23  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
24  */
25 
26 #include "tcp-option-sack.h"
27 #include "ns3/log.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("TcpOptionSack");
32 
33 NS_OBJECT_ENSURE_REGISTERED (TcpOptionSack);
34 
36  : TcpOption ()
37 {
38 }
39 
41 {
42 }
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::TcpOptionSack")
48  .SetParent<TcpOption> ()
49  .SetGroupName ("Internet")
50  .AddConstructor<TcpOptionSack> ()
51  ;
52  return tid;
53 }
54 
55 TypeId
57 {
58  return GetTypeId ();
59 }
60 
61 void
62 TcpOptionSack::Print (std::ostream &os) const
63 {
64  os << "blocks: " << GetNumSackBlocks () << ",";
65  for (SackList::const_iterator it = m_sackList.begin (); it != m_sackList.end (); ++it)
66  {
67  os << "[" << it->first << "," << it->second << "]";
68  }
69 }
70 
71 uint32_t
73 {
74  NS_LOG_FUNCTION (this);
75  NS_LOG_LOGIC ("Serialized size: " << 2 + GetNumSackBlocks () * 8);
76  return 2 + GetNumSackBlocks () * 8;
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION (this);
84  i.WriteU8 (GetKind ()); // Kind
85  uint8_t length = static_cast<uint8_t> (GetNumSackBlocks () * 8 + 2);
86  i.WriteU8 (length); // Length
87 
88  for (SackList::const_iterator 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 
97 uint32_t
99 {
100  NS_LOG_FUNCTION (this);
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.empty ();
112  uint8_t sackCount = (size - 2) / 8;
113  while (sackCount)
114  {
116  SequenceNumber32 rightEdge = SequenceNumber32 (i.ReadNtohU32 ());
117  SackBlock s (leftEdge, rightEdge);
118  AddSackBlock (s);
119  sackCount--;
120  }
121 
122  return GetSerializedSize ();
123 }
124 
125 uint8_t
127 {
128  return TcpOption::SACK;
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION (this);
135  m_sackList.push_back (s);
136 }
137 
138 uint32_t
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 
146 void
148 {
149  m_sackList.clear ();
150 }
151 
154 {
155  NS_LOG_FUNCTION (this);
156  return m_sackList;
157 }
158 
159 std::ostream &
160 operator<< (std::ostream & os, TcpOptionSack const & 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 
173 std::ostream &
174 operator<< (std::ostream & os, TcpOptionSack::SackBlock const & sackBlock)
175 {
176  std::stringstream ss;
177  ss << "[" << sackBlock.first << ";" << sackBlock.second << "]";
178  os << ss.str ();
179  return os;
180 }
181 
182 } // namespace ns3
virtual uint8_t GetKind(void) const
Get the ‘kind’ (as in RFC 793) of this option.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
def start()
Definition: core.py:1844
std::list< SackBlock > SackList
SACK list definition.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void AddSackBlock(SackBlock s)
Add a SACK block.
uint32_t ReadNtohU32(void)
Definition: buffer.h:970
iterator in a Buffer instance
Definition: buffer.h:98
uint32_t GetNumSackBlocks(void) const
Count the total number of SACK blocks.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
SackList m_sackList
the list of SACK blocks
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
Defines the TCP option of kind 5 (selective acknowledgment option) as in RFC 2018 ...
void WriteHtonU32(uint32_t data)
Definition: buffer.h:924
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
std::pair< SequenceNumber32, SequenceNumber32 > SackBlock
SACK block definition.
void WriteU8(uint8_t data)
Definition: buffer.h:869
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:262
uint8_t ReadU8(void)
Definition: buffer.h:1021
void ClearSackList(void)
Clear the SACK list.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
Base class for all kinds of TCP options.
Definition: tcp-option.h:36
static TypeId GetTypeId(void)
Get the type ID.
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
virtual void Print(std::ostream &os) const
Print the Option contents.
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
SackList GetSackList(void) const
Get the SACK list.