A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-error-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Natale Patriciello <natale.patriciello@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7#include "tcp-error-model.h"
8
9#include "ns3/ipv4-header.h"
10#include "ns3/log.h"
11#include "ns3/packet.h"
12
13namespace ns3
14{
15
16NS_LOG_COMPONENT_DEFINE("TcpGeneralErrorModel");
17
18NS_OBJECT_ENSURE_REGISTERED(TcpGeneralErrorModel);
19
20TypeId
22{
23 static TypeId tid = TypeId("ns3::TcpGeneralErrorModel").SetParent<ErrorModel>();
24 return tid;
25}
26
31
32bool
34{
35 NS_LOG_FUNCTION(this << p);
36
37 if (!IsEnabled())
38 {
39 return false;
40 }
41
42 Ipv4Header ipHeader;
43 TcpHeader tcpHeader;
44
45 p->RemoveHeader(ipHeader);
46 p->RemoveHeader(tcpHeader);
47
48 bool toDrop = ShouldDrop(ipHeader, tcpHeader, p->GetSize());
49
50 if (toDrop && !m_dropCallback.IsNull())
51 {
52 m_dropCallback(ipHeader, tcpHeader, p);
53 }
54
55 p->AddHeader(tcpHeader);
56 p->AddHeader(ipHeader);
57
58 return toDrop;
59}
60
62
65{
66 static TypeId tid = TypeId("ns3::TcpSeqErrorModel")
68 .AddConstructor<TcpSeqErrorModel>();
69 return tid;
70}
71
72bool
74 const TcpHeader& tcpHeader,
76{
77 NS_LOG_FUNCTION(this << ipHeader << tcpHeader);
78
79 bool toDrop = false;
80
81 if (m_seqToKill.begin() != m_seqToKill.end() && packetSize != 0)
82 {
83 SequenceNumber32 toKill = m_seqToKill.front();
84 NS_LOG_INFO("Analyzing seq=" << tcpHeader.GetSequenceNumber() << " killing=" << toKill);
85 if (tcpHeader.GetSequenceNumber() == toKill)
86 {
87 NS_LOG_INFO("segment " << toKill << " dropped");
88 toDrop = true;
89 m_seqToKill.pop_front();
90 }
91 }
92
93 return toDrop;
94}
95
96void
98{
99 m_seqToKill.erase(m_seqToKill.begin(), m_seqToKill.end());
100}
101
103
104TypeId
106{
107 static TypeId tid = TypeId("ns3::TcpFlagErrorModel")
109 .AddConstructor<TcpFlagErrorModel>();
110 return tid;
111}
112
115 m_flagsToKill(TcpHeader::NONE),
116 m_killNumber(0)
117{
118}
119
120bool
122 const TcpHeader& tcpHeader,
123 uint32_t packetSize [[maybe_unused]])
124{
125 NS_LOG_FUNCTION(this << ipHeader << tcpHeader);
126
127 bool toDrop = false;
128
129 if ((tcpHeader.GetFlags() & m_flagsToKill) == m_flagsToKill)
130 {
131 if (m_killNumber > 0)
132 {
133 m_killNumber--;
134 if (m_killNumber > 0)
135 {
136 toDrop = true;
137 }
138 }
139 else if (m_killNumber < 0)
140 {
141 toDrop = true;
142 }
143 }
144
145 return toDrop;
146}
147
148void
154
155} // namespace ns3
General error model that can be used to corrupt packets.
bool IsEnabled() const
Packet header for IPv4.
Definition ipv4-header.h:23
Smart pointer class similar to boost::intrusive_ptr.
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:36
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
@ NONE
No flags.
Definition tcp-header.h:267
uint8_t GetFlags() const
Get the flags.
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:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize
Packet size generated at the AP.