A Discrete-Event Network Simulator
API
tcp-error-model.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 Natale Patriciello <natale.patriciello@gmail.com>
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  */
19 #include "tcp-error-model.h"
20 #include "ns3/ipv4-header.h"
21 #include "ns3/packet.h"
22 #include "ns3/log.h"
23 namespace ns3 {
24 
25 NS_LOG_COMPONENT_DEFINE ("TcpGeneralErrorModel");
26 
27 NS_OBJECT_ENSURE_REGISTERED (TcpGeneralErrorModel);
28 
29 TypeId
31 {
32  static TypeId tid = TypeId ("ns3::TcpGeneralErrorModel")
34  ;
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  ;
81  return tid;
82 }
83 
84 bool
85 TcpSeqErrorModel::ShouldDrop (const Ipv4Header &ipHeader, 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 () <<
96  " killing=" << toKill);
97  if (tcpHeader.GetSequenceNumber() == toKill)
98  {
99  NS_LOG_INFO ("segment " << toKill << " dropped");
100  toDrop = true;
101  m_seqToKill.pop_front();
102  }
103  }
104 
105  return toDrop;
106 }
107 
108 void
110 {
111  m_seqToKill.erase (m_seqToKill.begin(), m_seqToKill.end());
112 }
113 
115 
116 TypeId
118 {
119  static TypeId tid = TypeId ("ns3::TcpFlagErrorModel")
121  .AddConstructor<TcpFlagErrorModel> ()
122  ;
123  return tid;
124 }
125 
128  m_flagsToKill (TcpHeader::NONE),
129  m_killNumber (0)
130 {
131 }
132 
133 bool
134 TcpFlagErrorModel::ShouldDrop (const Ipv4Header &ipHeader, const TcpHeader &tcpHeader,
135  uint32_t packetSize)
136 {
137  NS_LOG_FUNCTION (this << ipHeader << tcpHeader);
138 
139  (void) packetSize;
140 
141  bool toDrop = false;
142 
143  if ((tcpHeader.GetFlags () & m_flagsToKill) == m_flagsToKill)
144  {
145  if (m_killNumber > 0)
146  {
147  m_killNumber--;
148  if (m_killNumber > 0)
149  {
150  toDrop = true;
151  }
152  }
153  else if (m_killNumber < 0)
154  {
155  toDrop = true;
156  }
157  }
158 
159  return toDrop;
160 }
161 
162 void
164 {
166  m_killNumber = 0;
167 }
168 
169 } //namespace ns3
170 
General error model that can be used to corrupt packets.
Definition: error-model.h:116
bool IsEnabled(void) const
Definition: error-model.cc:139
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:280
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
Error model which drop packets with specified TCP flags.
virtual bool ShouldDrop(const Ipv4Header &ipHeader, const TcpHeader &tcpHeader, uint32_t packetSize)
Check if the packet should be dropped.
TcpHeader::Flags_t m_flagsToKill
Flags a packet should have to be dropped.
virtual void DoReset(void)
Re-initialize any state.
static TypeId GetTypeId(void)
Get the type ID.
int16_t m_killNumber
The number of times the packet should be killed.
A general (TCP-aware) error model.
static TypeId GetTypeId(void)
Get the type ID.
virtual bool DoCorrupt(Ptr< Packet > p)
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.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:45
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
Definition: tcp-header.cc:143
@ NONE
No flags.
Definition: tcp-header.h:281
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:173
An error model TCP aware: it drops the sequence number declared.
virtual bool ShouldDrop(const Ipv4Header &ipHeader, const TcpHeader &tcpHeader, uint32_t packetSize)
Check if the packet should be dropped.
virtual void DoReset(void)
Re-initialize any state.
std::list< SequenceNumber32 > m_seqToKill
List of the sequence numbers to be dropped.
static TypeId GetTypeId(void)
Get the type ID.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize