A Discrete-Event Network Simulator
API
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 * 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 * Authors: Ken Renard <kenneth.d.renard.ctr@mail.mil>
19 *
20 */
21
22#include "ns3/arp-l3-protocol.h"
23#include "ns3/config.h"
24#include "ns3/icmpv4-l4-protocol.h"
25#include "ns3/icmpv6-l4-protocol.h"
26#include "ns3/inet-socket-address.h"
27#include "ns3/inet6-socket-address.h"
28#include "ns3/ipv4-l3-protocol.h"
29#include "ns3/ipv4-list-routing.h"
30#include "ns3/ipv4-static-routing.h"
31#include "ns3/ipv6-l3-protocol.h"
32#include "ns3/ipv6-list-routing.h"
33#include "ns3/ipv6-static-routing.h"
34#include "ns3/log.h"
35#include "ns3/node.h"
36#include "ns3/simple-channel.h"
37#include "ns3/simple-net-device.h"
38#include "ns3/simulator.h"
39#include "ns3/socket-factory.h"
40#include "ns3/tcp-l4-protocol.h"
41#include "ns3/tcp-socket-factory.h"
42#include "ns3/test.h"
43#include "ns3/traffic-control-layer.h"
44#include "ns3/udp-l4-protocol.h"
45#include "ns3/uinteger.h"
46
47#include <string>
48
49using namespace ns3;
50
51NS_LOG_COMPONENT_DEFINE("Ipv6DualStackTestSuite");
52
60{
61 public:
63
64 private:
65 void DoRun() override;
66 void DoTeardown() override;
67
71 void SetUpSim();
72
75
100
105
110
115};
116
119{
120 Ptr<Node> node = CreateObject<Node>();
121
122 // Traffic Control
123 Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer>();
124 node->AggregateObject(tc);
125
126 // ARP
127 Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol>();
128 node->AggregateObject(arp);
129 arp->SetTrafficControl(tc);
130
131 // IPV4
132 Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol>();
133 // Routing for Ipv4
134 Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting>();
135 ipv4->SetRoutingProtocol(ipv4Routing);
136 Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting>();
137 ipv4Routing->AddRoutingProtocol(ipv4staticRouting, 0);
138 node->AggregateObject(ipv4);
139
140 // ICMPv4
141 Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol>();
142 node->AggregateObject(icmp);
143
144 // UDP
145 Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol>();
146 node->AggregateObject(udp);
147
148 // TCP
149 Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol>();
150 node->AggregateObject(tcp);
151
152 // IPV6
153 Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol>();
154
155 // Routing for Ipv6
156 Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting>();
157 ipv6->SetRoutingProtocol(ipv6Routing);
158 Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting>();
159 ipv6Routing->AddRoutingProtocol(ipv6staticRouting, 0);
160 node->AggregateObject(ipv6);
161
162 // ICMPv6
163 Ptr<Icmpv6L4Protocol> icmp6 = CreateObject<Icmpv6L4Protocol>();
164 node->AggregateObject(icmp6);
165
166 // Ipv6 Extensions
167 ipv6->RegisterExtensions();
168 ipv6->RegisterOptions();
169
170 return node;
171}
172
175 Ipv4Address v4Addr,
176 Ipv4Mask v4Mask,
177 Ipv6Address v6Addr,
178 Ipv6Prefix v6Prefix)
179{
180 Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice>();
181 dev->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
182 node->AddDevice(dev);
183
184 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
185 uint32_t ndid = ipv4->AddInterface(dev);
186 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(v4Addr, v4Mask);
187 ipv4->AddAddress(ndid, ipv4Addr);
188 ipv4->SetUp(ndid);
189
190 Ptr<Ipv6> ipv6 = node->GetObject<Ipv6>();
191 ndid = ipv6->AddInterface(dev);
192 Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress(v6Addr, v6Prefix);
193 ipv6->AddAddress(ndid, ipv6Addr);
194 ipv6->SetUp(ndid);
195
196 return dev;
197}
198
199void
201{
204
206 Ipv4Address("10.0.0.1"),
207 Ipv4Mask(0xffffff00),
208 Ipv6Address("2001::1"),
209 Ipv6Prefix(64));
211 Ipv4Address("10.0.0.2"),
212 Ipv4Mask(0xffffff00),
213 Ipv6Address("2001::2"),
214 Ipv6Prefix(64));
215
216 Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
217 dev0->SetChannel(channel);
218 dev1->SetChannel(channel);
219
222
223 server1 = sockFactory0->CreateSocket();
224 server2 = sockFactory0->CreateSocket();
225 server3 = sockFactory0->CreateSocket();
226 server4 = sockFactory0->CreateSocket();
227
228 source1 = sockFactory1->CreateSocket();
229 source2 = sockFactory1->CreateSocket();
230 source3 = sockFactory1->CreateSocket();
231 source4 = sockFactory1->CreateSocket();
232}
233
234void
236{
237 receivedAddr1 = addr;
238}
239
240void
242{
243 receivedAddr2 = addr;
244}
245
246void
248{
249 receivedAddr3 = addr;
250}
251
252void
254{
255 receivedAddr4 = addr;
256}
257
259 : TestCase("DualStackTestCase")
260{
265}
266
267void
269{
270 SetUpSim();
271
272 uint16_t port1 = 5000;
273 uint16_t port2 = 5001;
274 uint16_t port3 = 5002;
275 uint16_t port4 = 5003;
276
277 /* Server 1: listen on 0.0.0.0 for IPv4 connection */
278 server1->Bind(InetSocketAddress(Ipv4Address::GetAny(), port1));
279 server1->Listen();
281 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
283
284 /* Server 2: listen on 0.0.0.0 for IPv4 connection - should reject IPv6 */
285 server2->Bind(InetSocketAddress(Ipv4Address::GetAny(), port2));
286 server2->Listen();
288 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
290
291 /* Server 3: listen on :: for IPv4 (mapped into IPv6 address) connection */
292 server3->Bind(Inet6SocketAddress(Ipv6Address::GetAny(), port3));
293 server3->Listen();
295 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
297
298 /* Server 4: listen on :: for IPv6 connection */
299 server4->Bind(Inet6SocketAddress(Ipv6Address::GetAny(), port4));
300 server4->Listen();
302 MakeNullCallback<bool, Ptr<Socket>, const Address&>(),
304
305 /* Source 1: connect to server 1 via IPv4 */
306 source1->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port1));
307
308 /* Source 2: connect to server 2 via IPv6 */
309 source2->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port2));
310
311 /* Source 3: connect to server 3 via IPv4 */
312 source3->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port3));
313
314 /* Source 4: connect to server 4 via IPv6 */
315 source4->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port4));
316
317 Simulator::Run();
318
319 /* Server 1: should have connection from Ipv4 address of Node 1 */
320 NS_TEST_EXPECT_MSG_EQ(InetSocketAddress::IsMatchingType(receivedAddr1),
321 true,
322 "Accepted address is of proper type");
323 NS_TEST_EXPECT_MSG_EQ(InetSocketAddress::ConvertFrom(receivedAddr1).GetIpv4(),
324 Ipv4Address("10.0.0.2"),
325 "Accepted address is correct");
326
327 /* Server 2: should have no connection */
329 true,
330 "IPv4 socket correctly ignored IPv6 connection");
331
332 /* Server 3: should have connection from Ipv4-mapped IPv6 address of Node 1 */
333 NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::IsMatchingType(receivedAddr3),
334 true,
335 "Accepted address is of proper type");
336 NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr3).GetIpv6(),
337 Ipv6Address("::ffff:0a00:0002"),
338 "Accepted address is correct");
339
340 /* Server 4: should have connection from IPv6 address of Node 1 */
341 NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::IsMatchingType(receivedAddr4),
342 true,
343 "Accepted address is of proper type");
344 NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr4).GetIpv6(),
345 Ipv6Address("2001::2"),
346 "Accepted address is correct");
347}
348
349void
351{
352 Simulator::Destroy();
353}
354
362{
363 public:
365 : TestSuite("ipv6-dual-stack", UNIT)
366 {
367 AddTestCase(new DualStackTestCase(), TestCase::QUICK);
368 }
369};
370
IPv6 dual stack Test.
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:92
bool IsInvalid() const
Definition: address.cc:71
An Inet6 address class.
an Inet address class
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
Describes an IPv6 address.
Definition: ipv6-address.h:50
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
IPv6 address associated with an interface.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
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:103
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:72
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:734
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:251
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:691
channel
Definition: third.py:81