A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-endpoint-bug2211.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Alexander Krotov <ilabdsf@yandex.ru>
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
19#include "ns3/core-module.h"
20#include "ns3/internet-module.h"
21#include "ns3/network-module.h"
22#include "ns3/test.h"
23
24#include <iostream>
25
26using namespace ns3;
27
28/**
29 * \ingroup internet-test
30 *
31 * \brief Test for bug 2211.
32 *
33 * https://www.nsnam.org/bugzilla/show_bug.cgi?id=2211
34 *
35 * NOTE: It is a valgrind testcase, it contains no ASSERTs.
36 *
37 * Test creates one node and sets up two TCP sockets on the loopback
38 * with default parameters: CWND = 1, MTU = 536.
39 * Sender socket sends 3 segments.
40 * When first segment is acknowledged, cwnd is raised to 2.
41 * Then, two segments are sent and arrive into receive buffer.
42 * Until bugfix, the following happened:
43 * Ipv4EndPoint::ForwardUp was called for both second and third segment.
44 * These calls scheduled two Ipv4EndPoint::DoForwardUp events.
45 * To demonstrate the bug, test case closes the receiver socket after
46 * receiving the second segment. As a result, Ipv4EndPoint is destroyed.
47 * However, Ipv4EndPoint::DoForwardUp is already scheduled for third segment.
48 * It is a use-after-free bug.
49 */
51{
52 public:
53 /**
54 * Constructor.
55 * \param desc Test description.
56 * \param ipVersion True to use IPv6.
57 */
58 TcpEndPointBug2211Test(std::string desc, bool ipVersion);
59
60 /**
61 * \brief Receive a packet.
62 * \param socket The receiving socket.
63 */
64 void Recv(Ptr<Socket> socket);
65 /**
66 * \brief Handle an incoming connection.
67 * \param s The receiving socket.
68 * \param from The other node IP address.
69 */
70 void HandleAccept(Ptr<Socket> s, const Address& from);
71 /**
72 * \brief Handle a connection establishment.
73 * \param socket The receiving socket.
74 */
75 void HandleConnect(Ptr<Socket> socket);
76 void DoRun() override;
77
78 private:
79 bool m_v6; //!< True to use IPv6.
80};
81
82void
84{
85 if (socket->GetRxAvailable() == 536 * 2)
86 {
87 socket->Close();
88 }
89}
90
91void
93{
94 s->SetRecvCallback(MakeCallback(&TcpEndPointBug2211Test::Recv, this));
95}
96
97void
99{
100 socket->Send(Create<Packet>(536));
101 socket->Send(Create<Packet>(536));
102 socket->Send(Create<Packet>(536));
103 socket->Close();
104}
105
106TcpEndPointBug2211Test::TcpEndPointBug2211Test(std::string desc, bool ipVersion)
107 : TestCase(desc)
108{
109 m_v6 = ipVersion;
110}
111
112void
114{
115 Ptr<Node> node = CreateObject<Node>();
116
117 InternetStackHelper internet;
118 internet.Install(node);
119
122 if (!m_v6)
123 {
125 }
126 else
127 {
129 }
130 sink->Listen();
131 sink->SetAcceptCallback(MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
133
134 Ptr<Socket> source = Socket::CreateSocket(node, tid);
135 source->Bind();
136 source->SetConnectCallback(MakeCallback(&TcpEndPointBug2211Test::HandleConnect, this),
138 if (!m_v6)
139 {
140 source->Connect(InetSocketAddress(Ipv4Address::GetLoopback(), 9));
141 }
142 else
143 {
144 source->Connect(Inet6SocketAddress(Ipv6Address::GetLoopback(), 9));
145 }
146
149}
150
151/**
152 * \ingroup internet-test
153 *
154 * \brief TestSuite for bug 2211 - It must be used with valgrind.
155 */
157{
158 public:
160 : TestSuite("tcp-endpoint-bug2211-test", Type::UNIT)
161 {
162 AddTestCase(new TcpEndPointBug2211Test("Bug 2211 testcase IPv4", false),
163 TestCase::Duration::QUICK);
164 AddTestCase(new TcpEndPointBug2211Test("Bug 2211 testcase IPv6", true),
165 TestCase::Duration::QUICK);
166 }
167};
168
170 g_TcpEndPoint2211TestSuite; //!< Static variable for test initialization
bool m_v6
True to use IPv6.
void DoRun() override
Implementation to actually run this TestCase.
void Recv(Ptr< Socket > socket)
Receive a packet.
void HandleAccept(Ptr< Socket > s, const Address &from)
Handle an incoming connection.
void HandleConnect(Ptr< Socket > socket)
Handle a connection establishment.
TcpEndPointBug2211Test(std::string desc, bool ipVersion)
Constructor.
TestSuite for bug 2211 - It must be used with valgrind.
a polymophic address class
Definition: address.h:101
An Inet6 address class.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
static Ipv4Address GetLoopback()
static Ipv4Address GetAny()
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
static Ipv6Address GetLoopback()
Get the loopback address.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
static TypeId GetTypeId()
Get the type ID.
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
a unique identifier for an interface.
Definition: type-id.h:59
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:749
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:706
static TcpEndpointBug2211TestSuite g_TcpEndPoint2211TestSuite
Static variable for test initialization.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55