A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-dual-stack-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 Georgia Tech Research Corporation
3 * Copyright (c) 2009 INRIA
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Authors: Ken Renard <kenneth.d.renard.ctr@mail.mil>
8 *
9 */
10
11#include "ns3/arp-l3-protocol.h"
12#include "ns3/config.h"
13#include "ns3/icmpv4-l4-protocol.h"
14#include "ns3/icmpv6-l4-protocol.h"
15#include "ns3/inet-socket-address.h"
16#include "ns3/inet6-socket-address.h"
17#include "ns3/ipv4-l3-protocol.h"
18#include "ns3/ipv4-list-routing.h"
19#include "ns3/ipv4-static-routing.h"
20#include "ns3/ipv6-l3-protocol.h"
21#include "ns3/ipv6-list-routing.h"
22#include "ns3/ipv6-static-routing.h"
23#include "ns3/log.h"
24#include "ns3/node.h"
25#include "ns3/simple-channel.h"
26#include "ns3/simple-net-device.h"
27#include "ns3/simulator.h"
28#include "ns3/socket-factory.h"
29#include "ns3/tcp-l4-protocol.h"
30#include "ns3/tcp-socket-factory.h"
31#include "ns3/test.h"
32#include "ns3/traffic-control-layer.h"
33#include "ns3/udp-l4-protocol.h"
34#include "ns3/uinteger.h"
35
36#include <string>
37
38using namespace ns3;
39
40NS_LOG_COMPONENT_DEFINE("Ipv6DualStackTestSuite");
41
42/**
43 * \ingroup internet-test
44 *
45 * \brief IPv6 dual stack Test
46 */
48{
49 public:
51
52 private:
53 void DoRun() override;
54 void DoTeardown() override;
55
56 /**
57 * \brief Setup the test.
58 */
59 void SetUpSim();
60
61 Ptr<Node> node0; //!< Node 0.
62 Ptr<Node> node1; //!< Node 1.
63
64 /**
65 * Handle connection created (1).
66 * \param s The socket.
67 * \param addr The peer address.
68 */
70 /**
71 * Handle connection created (2).
72 * \param s The socket.
73 * \param addr The peer address.
74 */
76 /**
77 * Handle connection created (3).
78 * \param s The socket.
79 * \param addr The peer address.
80 */
82 /**
83 * Handle connection created (4).
84 * \param s The socket.
85 * \param addr The peer address.
86 */
88
89 Ptr<Socket> server1; //!< Server socket (1).
90 Ptr<Socket> server2; //!< Server socket (2).
91 Ptr<Socket> server3; //!< Server socket (3).
92 Ptr<Socket> server4; //!< Server socket (4).
93
94 Ptr<Socket> source1; //!< Sending socket (1).
95 Ptr<Socket> source2; //!< Sending socket (2).
96 Ptr<Socket> source3; //!< Sending socket (3).
97 Ptr<Socket> source4; //!< Sending socket (4).
98
99 Address receivedAddr1; //!< Received address (1).
100 Address receivedAddr2; //!< Received address (2).
101 Address receivedAddr3; //!< Received address (3).
102 Address receivedAddr4; //!< Received address (4).
103};
104
107{
109
110 // Traffic Control
112 node->AggregateObject(tc);
113
114 // ARP
116 node->AggregateObject(arp);
117 arp->SetTrafficControl(tc);
118
119 // IPV4
121 // Routing for Ipv4
123 ipv4->SetRoutingProtocol(ipv4Routing);
125 ipv4Routing->AddRoutingProtocol(ipv4staticRouting, 0);
126 node->AggregateObject(ipv4);
127
128 // ICMPv4
130 node->AggregateObject(icmp);
131
132 // UDP
134 node->AggregateObject(udp);
135
136 // TCP
138 node->AggregateObject(tcp);
139
140 // IPV6
142
143 // Routing for Ipv6
145 ipv6->SetRoutingProtocol(ipv6Routing);
147 ipv6Routing->AddRoutingProtocol(ipv6staticRouting, 0);
148 node->AggregateObject(ipv6);
149
150 // ICMPv6
152 node->AggregateObject(icmp6);
153
154 // Ipv6 Extensions
155 ipv6->RegisterExtensions();
156 ipv6->RegisterOptions();
157
158 return node;
159}
160
163 Ipv4Address v4Addr,
164 Ipv4Mask v4Mask,
165 Ipv6Address v6Addr,
166 Ipv6Prefix v6Prefix)
167{
170 node->AddDevice(dev);
171
172 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
173 uint32_t ndid = ipv4->AddInterface(dev);
174 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(v4Addr, v4Mask);
175 ipv4->AddAddress(ndid, ipv4Addr);
176 ipv4->SetUp(ndid);
177
178 Ptr<Ipv6> ipv6 = node->GetObject<Ipv6>();
179 ndid = ipv6->AddInterface(dev);
180 Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress(v6Addr, v6Prefix);
181 ipv6->AddAddress(ndid, ipv6Addr);
182 ipv6->SetUp(ndid);
183
184 return dev;
185}
186
187void
189{
192
194 Ipv4Address("10.0.0.1"),
195 Ipv4Mask(0xffffff00),
196 Ipv6Address("2001::1"),
197 Ipv6Prefix(64));
199 Ipv4Address("10.0.0.2"),
200 Ipv4Mask(0xffffff00),
201 Ipv6Address("2001::2"),
202 Ipv6Prefix(64));
203
205 dev0->SetChannel(channel);
206 dev1->SetChannel(channel);
207
210
211 server1 = sockFactory0->CreateSocket();
212 server2 = sockFactory0->CreateSocket();
213 server3 = sockFactory0->CreateSocket();
214 server4 = sockFactory0->CreateSocket();
215
216 source1 = sockFactory1->CreateSocket();
217 source2 = sockFactory1->CreateSocket();
218 source3 = sockFactory1->CreateSocket();
219 source4 = sockFactory1->CreateSocket();
220}
221
222void
227
228void
233
234void
239
240void
245
254
255void
257{
258 SetUpSim();
259
260 uint16_t port1 = 5000;
261 uint16_t port2 = 5001;
262 uint16_t port3 = 5002;
263 uint16_t port4 = 5003;
264
265 /* Server 1: listen on 0.0.0.0 for IPv4 connection */
267 server1->Listen();
269 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
271
272 /* Server 2: listen on 0.0.0.0 for IPv4 connection - should reject IPv6 */
274 server2->Listen();
276 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
278
279 /* Server 3: listen on :: for IPv4 (mapped into IPv6 address) connection */
281 server3->Listen();
283 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
285
286 /* Server 4: listen on :: for IPv6 connection */
288 server4->Listen();
290 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
292
293 /* Source 1: connect to server 1 via IPv4 */
294 source1->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port1));
295
296 /* Source 2: connect to server 2 via IPv6 */
297 source2->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port2));
298
299 /* Source 3: connect to server 3 via IPv4 */
300 source3->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port3));
301
302 /* Source 4: connect to server 4 via IPv6 */
303 source4->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port4));
304
306
307 /* Server 1: should have connection from Ipv4 address of Node 1 */
309 true,
310 "Accepted address is of proper type");
312 Ipv4Address("10.0.0.2"),
313 "Accepted address is correct");
314
315 /* Server 2: should have no connection */
317 true,
318 "IPv4 socket correctly ignored IPv6 connection");
319
320 /* Server 3: should have connection from Ipv4-mapped IPv6 address of Node 1 */
322 true,
323 "Accepted address is of proper type");
325 Ipv6Address("::ffff:0a00:0002"),
326 "Accepted address is correct");
327
328 /* Server 4: should have connection from IPv6 address of Node 1 */
330 true,
331 "Accepted address is of proper type");
333 Ipv6Address("2001::2"),
334 "Accepted address is correct");
335}
336
337void
342
343/**
344 * \ingroup internet-test
345 *
346 * \brief IPv6 dual stack TestSuite
347 */
349{
350 public:
352 : TestSuite("ipv6-dual-stack", Type::UNIT)
353 {
354 AddTestCase(new DualStackTestCase(), TestCase::Duration::QUICK);
355 }
356};
357
358static Ipv6DualStackTestSuite g_ipv6DualStackTestSuite; //!< Static variable for test initialization
void SetUpSim()
Setup the test.
void ServerHandleConnectionCreated1(Ptr< Socket > s, const Address &addr)
Handle connection created (1).
Ptr< Socket > server4
Server socket (4).
Ptr< Socket > server2
Server socket (2).
void ServerHandleConnectionCreated3(Ptr< Socket > s, const Address &addr)
Handle connection created (3).
Address receivedAddr3
Received address (3).
Ptr< Socket > server3
Server socket (3).
void ServerHandleConnectionCreated4(Ptr< Socket > s, const Address &addr)
Handle connection created (4).
Address receivedAddr4
Received address (4).
Ptr< Socket > source1
Sending socket (1).
void ServerHandleConnectionCreated2(Ptr< Socket > s, const Address &addr)
Handle connection created (2).
Ptr< Socket > source2
Sending socket (2).
Ptr< Socket > server1
Server socket (1).
Address receivedAddr2
Received address (2).
void DoRun() override
Implementation to actually run this TestCase.
Address receivedAddr1
Received address (1).
Ptr< Socket > source3
Sending socket (3).
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
Ptr< Socket > source4
Sending socket (4).
IPv6 dual stack TestSuite.
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
an Inet address class
static bool IsMatchingType(const Address &address)
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Describes an IPv6 address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
IPv6 address associated with an interface.
Describes an IPv6 prefix.
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address Allocate()
Allocate a new Mac48Address.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
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
void SetAcceptCallback(Callback< bool, Ptr< Socket >, const Address & > connectionRequest, Callback< void, Ptr< Socket >, const Address & > newConnectionCreated)
Accept connection requests from remote hosts.
Definition socket.cc:94
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
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
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int Listen()=0
Listen for incoming connections.
API to create TCP socket instances.
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
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
Ptr< SimpleNetDevice > AddSimpleNetDevice(Ptr< Node > node, Ipv4Address v4Addr, Ipv4Mask v4Mask, Ipv6Address v6Addr, Ipv6Prefix v6Prefix)
Ptr< Node > CreateDualStackNode()
static Ipv6DualStackTestSuite g_ipv6DualStackTestSuite
Static variable for test initialization.
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