A Discrete-Event Network Simulator
API
ipv6-dual-stack-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 Georgia Tech Research Corporation
4  * Copyright (c) 2009 INRIA
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: Ken Renard <kenneth.d.renard.ctr@mail.mil>
20  *
21  */
22 
23 #include "ns3/test.h"
24 #include "ns3/socket-factory.h"
25 #include "ns3/tcp-socket-factory.h"
26 #include "ns3/simulator.h"
27 #include "ns3/simple-channel.h"
28 #include "ns3/simple-net-device.h"
29 #include "ns3/config.h"
30 #include "ns3/ipv4-static-routing.h"
31 #include "ns3/ipv4-list-routing.h"
32 #include "ns3/ipv6-static-routing.h"
33 #include "ns3/ipv6-list-routing.h"
34 #include "ns3/node.h"
35 #include "ns3/inet-socket-address.h"
36 #include "ns3/inet6-socket-address.h"
37 #include "ns3/uinteger.h"
38 #include "ns3/log.h"
39 
40 #include "ns3/arp-l3-protocol.h"
41 #include "ns3/ipv4-l3-protocol.h"
42 #include "ns3/ipv6-l3-protocol.h"
43 #include "ns3/icmpv4-l4-protocol.h"
44 #include "ns3/icmpv6-l4-protocol.h"
45 #include "ns3/udp-l4-protocol.h"
46 #include "ns3/tcp-l4-protocol.h"
47 #include "ns3/traffic-control-layer.h"
48 
49 #include <string>
50 
51 using namespace ns3;
52 
53 NS_LOG_COMPONENT_DEFINE ("Ipv6DualStackTestSuite");
54 
62 {
63 public:
65 private:
66  virtual void DoRun (void);
67  virtual void DoTeardown (void);
68 
72  void SetUpSim ();
73 
76 
82  void ServerHandleConnectionCreated1 (Ptr<Socket> s, const Address & addr);
88  void ServerHandleConnectionCreated2 (Ptr<Socket> s, const Address & addr);
94  void ServerHandleConnectionCreated3 (Ptr<Socket> s, const Address & addr);
100  void ServerHandleConnectionCreated4 (Ptr<Socket> s, const Address & addr);
101 
106 
111 
116 };
117 
118 Ptr<Node>
120 {
121  Ptr<Node> node = CreateObject<Node> ();
122 
123  //ARP
124  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
125  node->AggregateObject (arp);
126 
127  //IPV4
128  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
129  //Routing for Ipv4
130  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
131  ipv4->SetRoutingProtocol (ipv4Routing);
132  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
133  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
134  node->AggregateObject (ipv4);
135 
136  //ICMPv4
137  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
138  node->AggregateObject (icmp);
139 
140  //UDP
141  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
142  node->AggregateObject (udp);
143 
144  //TCP
145  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
146  node->AggregateObject (tcp);
147 
148  //IPV6
149  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
150 
151  //Routing for Ipv6
152  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
153  ipv6->SetRoutingProtocol (ipv6Routing);
154  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
155  ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
156  node->AggregateObject (ipv6);
157 
158  //ICMPv6
159  Ptr<Icmpv6L4Protocol> icmp6 = CreateObject<Icmpv6L4Protocol> ();
160  node->AggregateObject (icmp6);
161 
162  //Ipv6 Extensions
163  ipv6->RegisterExtensions ();
164  ipv6->RegisterOptions ();
165 
166  // Traffic Control
167  Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
168  node->AggregateObject (tc);
169 
170  return node;
171 }
172 
175  Ipv6Address v6Addr, Ipv6Prefix v6Prefix)
176 {
177  Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice> ();
179  node->AddDevice (dev);
180 
181  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
182  uint32_t ndid = ipv4->AddInterface (dev);
183  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (v4Addr, v4Mask);
184  ipv4->AddAddress (ndid, ipv4Addr);
185  ipv4->SetUp (ndid);
186 
187  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
188  ndid = ipv6->AddInterface (dev);
189  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (v6Addr, v6Prefix);
190  ipv6->AddAddress (ndid, ipv6Addr);
191  ipv6->SetUp (ndid);
192 
193  return dev;
194 }
195 
196 void
198 {
199  node0 = CreateDualStackNode ();
200  node1 = CreateDualStackNode ();
201 
202  Ptr<SimpleNetDevice> dev0 = AddSimpleNetDevice(node0, Ipv4Address("10.0.0.1"),
203  Ipv4Mask(0xffffff00), Ipv6Address("2001::1"), Ipv6Prefix(64));
204  Ptr<SimpleNetDevice> dev1 = AddSimpleNetDevice(node1, Ipv4Address("10.0.0.2"),
205  Ipv4Mask(0xffffff00), Ipv6Address("2001::2"), Ipv6Prefix(64));
206 
207  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
208  dev0->SetChannel (channel);
209  dev1->SetChannel (channel);
210 
211  Ptr<SocketFactory> sockFactory0 = node0->GetObject<TcpSocketFactory> ();
212  Ptr<SocketFactory> sockFactory1 = node1->GetObject<TcpSocketFactory> ();
213 
214  server1 = sockFactory0->CreateSocket ();
215  server2 = sockFactory0->CreateSocket ();
216  server3 = sockFactory0->CreateSocket ();
217  server4 = sockFactory0->CreateSocket ();
218 
219  source1 = sockFactory1->CreateSocket ();
220  source2 = sockFactory1->CreateSocket ();
221  source3 = sockFactory1->CreateSocket ();
222  source4 = sockFactory1->CreateSocket ();
223 
224  return;
225 }
226 
227 void
229 {
230  receivedAddr1 = addr;
231  return;
232 }
233 
234 void
236 {
237  receivedAddr2 = addr;
238  return;
239 }
240 
241 void
243 {
244  receivedAddr3 = addr;
245  return;
246 }
247 
248 void
250 {
251  receivedAddr4 = addr;
252  return;
253 }
254 
255 
257  : TestCase ("DualStackTestCase")
258 {
259  receivedAddr1 = Address ();
260  receivedAddr2 = Address ();
261  receivedAddr3 = Address ();
262  receivedAddr4 = Address ();
263 }
264 
265 void
267 {
268  SetUpSim ();
269 
270  uint16_t port1 = 5000;
271  uint16_t port2 = 5001;
272  uint16_t port3 = 5002;
273  uint16_t port4 = 5003;
274 
275  /* Server 1: listen on 0.0.0.0 for IPv4 connection */
276  server1->Bind (InetSocketAddress(Ipv4Address::GetAny(), port1));
277  server1->Listen ();
280 
281  /* Server 2: listen on 0.0.0.0 for IPv4 connection - should reject IPv6 */
282  server2->Bind (InetSocketAddress(Ipv4Address::GetAny(), port2));
283  server2->Listen ();
286 
287  /* Server 3: listen on :: for IPv4 (mapped into IPv6 address) connection */
288  server3->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port3));
289  server3->Listen ();
292 
293  /* Server 4: listen on :: for IPv6 connection */
294  server4->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port4));
295  server4->Listen ();
298 
299 
300  /* Source 1: connect to server 1 via IPv4 */
301  source1->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port1));
302 
303  /* Source 2: connect to server 2 via IPv6 */
304  source2->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port2));
305 
306  /* Source 3: connect to server 3 via IPv4 */
307  source3->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port3));
308 
309  /* Source 4: connect to server 4 via IPv6 */
310  source4->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port4));
311 
312  Simulator::Run();
313 
314  /* Server 1: should have connection from Ipv4 address of Node 1 */
315  NS_TEST_EXPECT_MSG_EQ (InetSocketAddress::IsMatchingType(receivedAddr1), true, "Accepted address is of proper type");
316  NS_TEST_EXPECT_MSG_EQ(InetSocketAddress::ConvertFrom(receivedAddr1).GetIpv4 (), Ipv4Address("10.0.0.2"), "Accepted address is correct");
317 
318  /* Server 2: should have no connection */
319  NS_TEST_EXPECT_MSG_EQ(receivedAddr2.IsInvalid(), true, "IPv4 socket correctly ignored IPv6 connection");
320 
321  /* Server 3: should have connection from Ipv4-mapped IPv6 address of Node 1 */
322  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr3), true, "Accepted address is of proper type");
323  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr3).GetIpv6 (), Ipv6Address("::ffff:0a00:0002"), "Accepted address is correct");
324 
325  /* Server 4: should have connection from IPv6 address of Node 1 */
326  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr4), true, "Accepted address is of proper type");
327  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr4).GetIpv6 (), Ipv6Address("2001::2"), "Accepted address is correct");
328 }
329 
330 void
332 {
333  Simulator::Destroy ();
334 }
335 
343 {
344 public:
346  : TestSuite ("ipv6-dual-stack", UNIT)
347  {
348  AddTestCase (new DualStackTestCase(), TestCase::QUICK);
349  }
350 };
351 
bool IsInvalid(void) const
Definition: address.cc:68
tuple channel
Definition: third.py:85
Address receivedAddr2
Received address (2).
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
Address receivedAddr1
Received address (1).
void ServerHandleConnectionCreated4(Ptr< Socket > s, const Address &addr)
Handle connection created (4).
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
Ptr< SimpleNetDevice > AddSimpleNetDevice(Ptr< Node > node, Ipv4Address v4Addr, Ipv4Mask v4Mask, Ipv6Address v6Addr, Ipv6Prefix v6Prefix)
API to create TCP socket instances.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
Ptr< Socket > server2
Server socket (2).
A suite of tests to run.
Definition: test.h:1342
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
IPv6 address associated with an interface.
#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:285
virtual Ptr< Socket > CreateSocket(void)=0
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
encapsulates test code
Definition: test.h:1155
This test suite implements a Unit Test.
Definition: test.h:1352
a polymophic address class
Definition: address.h:90
virtual int Listen(void)=0
Listen for incoming connections.
IPv6 dual stack Test.
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
virtual void SetUp(uint32_t interface)=0
void ServerHandleConnectionCreated1(Ptr< Socket > s, const Address &addr)
Handle connection created (1).
void ServerHandleConnectionCreated2(Ptr< Socket > s, const Address &addr)
Handle connection created (2).
Ptr< Socket > source1
Sending socket (1).
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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:71
static Mac48Address ConvertFrom(const Address &address)
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void ServerHandleConnectionCreated3(Ptr< Socket > s, const Address &addr)
Handle connection created (3).
Ptr< Socket > server3
Server socket (3).
Ptr< Socket > source3
Sending socket (3).
IPv6 dual stack TestSuite.
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:104
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
virtual void RegisterExtensions()
Register the IPv6 Extensions.
static Ipv6DualStackTestSuite g_ipv6DualStackTestSuite
Static variable for test initialization.
Ptr< Socket > source2
Sending socket (2).
Describes an IPv6 address.
Definition: ipv6-address.h:48
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
a class to store IPv4 address information on an interface
Ptr< Node > CreateDualStackNode()
virtual void DoRun(void)
Implementation to actually run this TestCase.
Ptr< Socket > server4
Server socket (4).
virtual void SetAddress(Address address)
Set the address of this interface.
virtual void RegisterOptions()
Register the IPv6 Options.
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
Address receivedAddr4
Received address (4).
Address receivedAddr3
Received address (3).
Ptr< Socket > server1
Server socket (1).
void SetUpSim()
Setup the test.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
Ptr< Socket > source4
Sending socket (4).
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.