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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#include "ns3/core-module.h"
9#include "ns3/internet-module.h"
10#include "ns3/network-module.h"
11#include "ns3/test.h"
12
13#include <iostream>
14
15using namespace ns3;
16
17/**
18 * @ingroup internet-test
19 *
20 * @brief Test for bug 2211.
21 *
22 * https://www.nsnam.org/bugzilla/show_bug.cgi?id=2211
23 *
24 * NOTE: It is a valgrind testcase, it contains no ASSERTs.
25 *
26 * Test creates one node and sets up two TCP sockets on the loopback
27 * with default parameters: CWND = 1, MTU = 536.
28 * Sender socket sends 3 segments.
29 * When first segment is acknowledged, cwnd is raised to 2.
30 * Then, two segments are sent and arrive into receive buffer.
31 * Until bugfix, the following happened:
32 * Ipv4EndPoint::ForwardUp was called for both second and third segment.
33 * These calls scheduled two Ipv4EndPoint::DoForwardUp events.
34 * To demonstrate the bug, test case closes the receiver socket after
35 * receiving the second segment. As a result, Ipv4EndPoint is destroyed.
36 * However, Ipv4EndPoint::DoForwardUp is already scheduled for third segment.
37 * It is a use-after-free bug.
38 */
40{
41 public:
42 /**
43 * Constructor.
44 * @param desc Test description.
45 * @param ipVersion True to use IPv6.
46 */
47 TcpEndPointBug2211Test(std::string desc, bool ipVersion);
48
49 /**
50 * @brief Receive a packet.
51 * @param socket The receiving socket.
52 */
53 void Recv(Ptr<Socket> socket);
54 /**
55 * @brief Handle an incoming connection.
56 * @param s The receiving socket.
57 * @param from The other node IP address.
58 */
59 void HandleAccept(Ptr<Socket> s, const Address& from);
60 /**
61 * @brief Handle a connection establishment.
62 * @param socket The receiving socket.
63 */
64 void HandleConnect(Ptr<Socket> socket);
65 void DoRun() override;
66
67 private:
68 bool m_v6; //!< True to use IPv6.
69};
70
71void
73{
74 if (socket->GetRxAvailable() == 536 * 2)
75 {
76 socket->Close();
77 }
78}
79
80void
85
86void
88{
89 socket->Send(Create<Packet>(536));
90 socket->Send(Create<Packet>(536));
91 socket->Send(Create<Packet>(536));
92 socket->Close();
93}
94
95TcpEndPointBug2211Test::TcpEndPointBug2211Test(std::string desc, bool ipVersion)
96 : TestCase(desc)
97{
98 m_v6 = ipVersion;
99}
100
101void
103{
105
106 InternetStackHelper internet;
107 internet.Install(node);
108
111 if (!m_v6)
112 {
114 }
115 else
116 {
118 }
119 sink->Listen();
120 sink->SetAcceptCallback(MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
122
123 Ptr<Socket> source = Socket::CreateSocket(node, tid);
124 source->Bind();
125 source->SetConnectCallback(MakeCallback(&TcpEndPointBug2211Test::HandleConnect, this),
127 if (!m_v6)
128 {
129 source->Connect(InetSocketAddress(Ipv4Address::GetLoopback(), 9));
130 }
131 else
132 {
133 source->Connect(Inet6SocketAddress(Ipv6Address::GetLoopback(), 9));
134 }
135
138}
139
140/**
141 * @ingroup internet-test
142 *
143 * @brief TestSuite for bug 2211 - It must be used with valgrind.
144 */
146{
147 public:
149 : TestSuite("tcp-endpoint-bug2211-test", Type::UNIT)
150 {
151 AddTestCase(new TcpEndPointBug2211Test("Bug 2211 testcase IPv4", false),
152 TestCase::Duration::QUICK);
153 AddTestCase(new TcpEndPointBug2211Test("Bug 2211 testcase IPv6", true),
154 TestCase::Duration::QUICK);
155 }
156};
157
159 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:90
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
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:61
static TypeId GetTypeId()
Get the type ID.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
a unique identifier for an interface.
Definition type-id.h:48
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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:684
static TcpEndpointBug2211TestSuite g_TcpEndPoint2211TestSuite
Static variable for test initialization.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44