A Discrete-Event Network Simulator
API
tcp-error-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Natale Patriciello <natale.patriciello@gmail.com>
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  */
18 #include "tcp-error-model.h"
19 
20 #include "ns3/ipv4-header.h"
21 #include "ns3/log.h"
22 #include "ns3/packet.h"
23 
24 namespace ns3
25 {
26 
27 NS_LOG_COMPONENT_DEFINE("TcpGeneralErrorModel");
28 
29 NS_OBJECT_ENSURE_REGISTERED(TcpGeneralErrorModel);
30 
31 TypeId
33 {
34  static TypeId tid = TypeId("ns3::TcpGeneralErrorModel").SetParent<ErrorModel>();
35  return tid;
36 }
37 
39 {
40  NS_LOG_FUNCTION(this);
41 }
42 
43 bool
45 {
46  NS_LOG_FUNCTION(this << p);
47 
48  if (!IsEnabled())
49  {
50  return false;
51  }
52 
53  Ipv4Header ipHeader;
54  TcpHeader tcpHeader;
55 
56  p->RemoveHeader(ipHeader);
57  p->RemoveHeader(tcpHeader);
58 
59  bool toDrop = ShouldDrop(ipHeader, tcpHeader, p->GetSize());
60 
61  if (toDrop && !m_dropCallback.IsNull())
62  {
63  m_dropCallback(ipHeader, tcpHeader, p);
64  }
65 
66  p->AddHeader(tcpHeader);
67  p->AddHeader(ipHeader);
68 
69  return toDrop;
70 }
71 
73 
74 TypeId
76 {
77  static TypeId tid = TypeId("ns3::TcpSeqErrorModel")
79  .AddConstructor<TcpSeqErrorModel>();
80  return tid;
81 }
82 
83 bool
85  const TcpHeader& tcpHeader,
86  uint32_t packetSize)
87 {
88  NS_LOG_FUNCTION(this << ipHeader << tcpHeader);
89 
90  bool toDrop = false;
91 
92  if (m_seqToKill.begin() != m_seqToKill.end() && packetSize != 0)
93  {
94  SequenceNumber32 toKill = m_seqToKill.front();
95  NS_LOG_INFO("Analyzing seq=" << tcpHeader.GetSequenceNumber() << " killing=" << toKill);
96  if (tcpHeader.GetSequenceNumber() == toKill)
97  {
98  NS_LOG_INFO("segment " << toKill << " dropped");
99  toDrop = true;
100  m_seqToKill.pop_front();
101  }
102  }
103 
104  return toDrop;
105 }
106 
107 void
109 {
110  m_seqToKill.erase(m_seqToKill.begin(), m_seqToKill.end());
111 }
112 
114 
115 TypeId
117 {
118  static TypeId tid = TypeId("ns3::TcpFlagErrorModel")
120  .AddConstructor<TcpFlagErrorModel>();
121  return tid;
122 }
123 
126  m_flagsToKill(TcpHeader::NONE),
127  m_killNumber(0)
128 {
129 }
130 
131 bool
133  const TcpHeader& tcpHeader,
134  uint32_t packetSize [[maybe_unused]])
135 {
136  NS_LOG_FUNCTION(this << ipHeader << tcpHeader);
137 
138  bool toDrop = false;
139 
140  if ((tcpHeader.GetFlags() & m_flagsToKill) == m_flagsToKill)
141  {
142  if (m_killNumber > 0)
143  {
144  m_killNumber--;
145  if (m_killNumber > 0)
146  {
147  toDrop = true;
148  }
149  }
150  else if (m_killNumber < 0)
151  {
152  toDrop = true;
153  }
154  }
155 
156  return toDrop;
157 }
158 
159 void
161 {
163  m_killNumber = 0;
164 }
165 
166 } // namespace ns3
General error model that can be used to corrupt packets.
Definition: error-model.h:117
bool IsEnabled() const
Definition: error-model.cc:140
Packet header for IPv4.
Definition: ipv4-header.h:34
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
Error model which drop packets with specified TCP flags.
TcpHeader::Flags_t m_flagsToKill
Flags a packet should have to be dropped.
bool ShouldDrop(const Ipv4Header &ipHeader, const TcpHeader &tcpHeader, uint32_t packetSize) override
Check if the packet should be dropped.
void DoReset() override
Re-initialize any state.
int16_t m_killNumber
The number of times the packet should be killed.
static TypeId GetTypeId()
Get the type ID.
A general (TCP-aware) error model.
bool DoCorrupt(Ptr< Packet > p) override
Corrupt a packet according to the specified model.
Callback< void, const Ipv4Header &, const TcpHeader &, Ptr< const Packet > > m_dropCallback
Drop callback.
virtual bool ShouldDrop(const Ipv4Header &ipHeader, const TcpHeader &tcpHeader, uint32_t packetSize)=0
Check if the packet should be dropped.
static TypeId GetTypeId()
Get the type ID.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:47
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
Definition: tcp-header.cc:118
@ NONE
No flags.
Definition: tcp-header.h:278
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:148
An error model TCP aware: it drops the sequence number declared.
static TypeId GetTypeId()
Get the type ID.
bool ShouldDrop(const Ipv4Header &ipHeader, const TcpHeader &tcpHeader, uint32_t packetSize) override
Check if the packet should be dropped.
void DoReset() override
Re-initialize any state.
std::list< SequenceNumber32 > m_seqToKill
List of the sequence numbers to be dropped.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#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.
static const uint32_t packetSize
Packet size generated at the AP.