This documentation is not the Latest Release.
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 
48 #include <string>
49 
50 using namespace ns3;
51 
52 NS_LOG_COMPONENT_DEFINE ("Ipv6DualStackTestSuite");
53 
55 {
56 public:
58 private:
59  virtual void DoRun (void);
60  virtual void DoTeardown (void);
61 
62  void SetUpSim ();
65 
66  void ServerHandleConnectionCreated1 (Ptr<Socket> s, const Address & addr);
67  void ServerHandleConnectionCreated2 (Ptr<Socket> s, const Address & addr);
68  void ServerHandleConnectionCreated3 (Ptr<Socket> s, const Address & addr);
69  void ServerHandleConnectionCreated4 (Ptr<Socket> s, const Address & addr);
70 
75 
80 
85 };
86 
89 {
90  Ptr<Node> node = CreateObject<Node> ();
91 
92  //ARP
93  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
94  node->AggregateObject (arp);
95 
96  //IPV4
97  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
98  //Routing for Ipv4
99  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
100  ipv4->SetRoutingProtocol (ipv4Routing);
101  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
102  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
103  node->AggregateObject (ipv4);
104 
105  //ICMPv4
106  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
107  node->AggregateObject (icmp);
108 
109  //UDP
110  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
111  node->AggregateObject (udp);
112 
113  //TCP
114  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
115  node->AggregateObject (tcp);
116 
117  //IPV6
118  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
119 
120  //Routing for Ipv6
121  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
122  ipv6->SetRoutingProtocol (ipv6Routing);
123  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
124  ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
125  node->AggregateObject (ipv6);
126 
127  //ICMPv6
128  Ptr<Icmpv6L4Protocol> icmp6 = CreateObject<Icmpv6L4Protocol> ();
129  node->AggregateObject (icmp6);
130 
131  //Ipv6 Extensions
132  ipv6->RegisterExtensions ();
133  ipv6->RegisterOptions ();
134 
135  return node;
136 }
137 
140  Ipv6Address v6Addr, Ipv6Prefix v6Prefix)
141 {
142  Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice> ();
144  node->AddDevice (dev);
145 
146  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
147  uint32_t ndid = ipv4->AddInterface (dev);
148  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (v4Addr, v4Mask);
149  ipv4->AddAddress (ndid, ipv4Addr);
150  ipv4->SetUp (ndid);
151 
152  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
153  ndid = ipv6->AddInterface (dev);
154  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (v6Addr, v6Prefix);
155  ipv6->AddAddress (ndid, ipv6Addr);
156  ipv6->SetUp (ndid);
157 
158  return dev;
159 }
160 
161 void
163 {
164  node0 = CreateDualStackNode ();
165  node1 = CreateDualStackNode ();
166 
167  Ptr<SimpleNetDevice> dev0 = AddSimpleNetDevice(node0, Ipv4Address("10.0.0.1"),
168  Ipv4Mask(0xffffff00), Ipv6Address("2001::1"), Ipv6Prefix(64));
169  Ptr<SimpleNetDevice> dev1 = AddSimpleNetDevice(node1, Ipv4Address("10.0.0.2"),
170  Ipv4Mask(0xffffff00), Ipv6Address("2001::2"), Ipv6Prefix(64));
171 
172  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
173  dev0->SetChannel (channel);
174  dev1->SetChannel (channel);
175 
176  Ptr<SocketFactory> sockFactory0 = node0->GetObject<TcpSocketFactory> ();
177  Ptr<SocketFactory> sockFactory1 = node1->GetObject<TcpSocketFactory> ();
178 
179  server1 = sockFactory0->CreateSocket ();
180  server2 = sockFactory0->CreateSocket ();
181  server3 = sockFactory0->CreateSocket ();
182  server4 = sockFactory0->CreateSocket ();
183 
184  source1 = sockFactory1->CreateSocket ();
185  source2 = sockFactory1->CreateSocket ();
186  source3 = sockFactory1->CreateSocket ();
187  source4 = sockFactory1->CreateSocket ();
188 
189  return;
190 }
191 
192 void
194 {
195  receivedAddr1 = addr;
196  return;
197 }
198 
199 void
201 {
202  receivedAddr2 = addr;
203  return;
204 }
205 
206 void
208 {
209  receivedAddr3 = addr;
210  return;
211 }
212 
213 void
215 {
216  receivedAddr4 = addr;
217  return;
218 }
219 
220 
222  : TestCase ("DualStackTestCase")
223 {
224  receivedAddr1 = Address ();
225  receivedAddr2 = Address ();
226  receivedAddr3 = Address ();
227  receivedAddr4 = Address ();
228 }
229 
230 void
232 {
233  SetUpSim ();
234 
235  uint16_t port1 = 5000;
236  uint16_t port2 = 5001;
237  uint16_t port3 = 5002;
238  uint16_t port4 = 5003;
239 
240  /* Server 1: listen on 0.0.0.0 for IPv4 connection */
241  server1->Bind (InetSocketAddress(Ipv4Address::GetAny(), port1));
242  server1->Listen ();
245 
246  /* Server 2: listen on 0.0.0.0 for IPv4 connection - should reject IPv6 */
247  server2->Bind (InetSocketAddress(Ipv4Address::GetAny(), port2));
248  server2->Listen ();
251 
252  /* Server 3: listen on :: for IPv4 (mapped into IPv6 address) connection */
253  server3->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port3));
254  server3->Listen ();
257 
258  /* Server 4: listen on :: for IPv6 connection */
259  server4->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port4));
260  server4->Listen ();
263 
264 
265  /* Source 1: connect to server 1 via IPv4 */
266  source1->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port1));
267 
268  /* Source 2: connect to server 2 via IPv6 */
269  source2->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port2));
270 
271  /* Source 3: connect to server 3 via IPv4 */
272  source3->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port3));
273 
274  /* Source 4: connect to server 4 via IPv6 */
275  source4->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port4));
276 
277  Simulator::Run();
278 
279  /* Server 1: should have connection from Ipv4 address of Node 1 */
280  NS_TEST_EXPECT_MSG_EQ (InetSocketAddress::IsMatchingType(receivedAddr1), true, "Accepted address is of proper type");
281  NS_TEST_EXPECT_MSG_EQ(InetSocketAddress::ConvertFrom(receivedAddr1).GetIpv4 (), Ipv4Address("10.0.0.2"), "Accepted address is correct");
282 
283  /* Server 2: should have no connection */
284  NS_TEST_EXPECT_MSG_EQ(receivedAddr2.IsInvalid(), true, "IPv4 socket correctly ignored IPv6 connection");
285 
286  /* Server 3: should have connection from Ipv4-mapped IPv6 address of Node 1 */
287  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr3), true, "Accepted address is of proper type");
288  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr3).GetIpv6 (), Ipv6Address("::ffff:0a00:0002"), "Accepted address is correct");
289 
290  /* Server 4: should have connection from IPv6 address of Node 1 */
291  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr4), true, "Accepted address is of proper type");
292  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr4).GetIpv6 (), Ipv6Address("2001::2"), "Accepted address is correct");
293 }
294 
295 void
297 {
298  Simulator::Destroy ();
299 }
300 
301 
302 static class Ipv6DualStackTestSuite : public TestSuite
303 {
304 public:
306  : TestSuite ("ipv6-dual-stack", UNIT)
307  {
308  AddTestCase (new DualStackTestCase(), TestCase::QUICK);
309  }
bool IsInvalid(void) const
Definition: address.cc:68
tuple channel
Definition: third.py:85
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
void ServerHandleConnectionCreated4(Ptr< Socket > s, const Address &addr)
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:80
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:226
A suite of tests to run.
Definition: test.h:1333
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:246
#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:278
virtual Ptr< Socket > CreateSocket(void)=0
Callback< R > MakeNullCallback(void)
Definition: callback.h:1626
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
a polymophic address class
Definition: address.h:90
virtual int Listen(void)=0
Listen for incoming connections.
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:297
virtual void SetUp(uint32_t interface)=0
void ServerHandleConnectionCreated1(Ptr< Socket > s, const Address &addr)
void ServerHandleConnectionCreated2(Ptr< Socket > s, const Address &addr)
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1480
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
Ipv6DualStackTestSuite g_ipv6DualStackTestSuite
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)
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.
Describes an IPv6 address.
Definition: ipv6-address.h:47
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.
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:389
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.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
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.