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 
56 {
57 public:
59 private:
60  virtual void DoRun (void);
61  virtual void DoTeardown (void);
62 
63  void SetUpSim ();
66 
67  void ServerHandleConnectionCreated1 (Ptr<Socket> s, const Address & addr);
68  void ServerHandleConnectionCreated2 (Ptr<Socket> s, const Address & addr);
69  void ServerHandleConnectionCreated3 (Ptr<Socket> s, const Address & addr);
70  void ServerHandleConnectionCreated4 (Ptr<Socket> s, const Address & addr);
71 
76 
81 
86 };
87 
90 {
91  Ptr<Node> node = CreateObject<Node> ();
92 
93  //ARP
94  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
95  node->AggregateObject (arp);
96 
97  //IPV4
98  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
99  //Routing for Ipv4
100  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
101  ipv4->SetRoutingProtocol (ipv4Routing);
102  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
103  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
104  node->AggregateObject (ipv4);
105 
106  //ICMPv4
107  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
108  node->AggregateObject (icmp);
109 
110  //UDP
111  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
112  node->AggregateObject (udp);
113 
114  //TCP
115  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
116  node->AggregateObject (tcp);
117 
118  //IPV6
119  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
120 
121  //Routing for Ipv6
122  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
123  ipv6->SetRoutingProtocol (ipv6Routing);
124  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
125  ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
126  node->AggregateObject (ipv6);
127 
128  //ICMPv6
129  Ptr<Icmpv6L4Protocol> icmp6 = CreateObject<Icmpv6L4Protocol> ();
130  node->AggregateObject (icmp6);
131 
132  //Ipv6 Extensions
133  ipv6->RegisterExtensions ();
134  ipv6->RegisterOptions ();
135 
136  // Traffic Control
137  Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
138  node->AggregateObject (tc);
139 
140  return node;
141 }
142 
145  Ipv6Address v6Addr, Ipv6Prefix v6Prefix)
146 {
147  Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice> ();
149  node->AddDevice (dev);
150 
151  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
152  uint32_t ndid = ipv4->AddInterface (dev);
153  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (v4Addr, v4Mask);
154  ipv4->AddAddress (ndid, ipv4Addr);
155  ipv4->SetUp (ndid);
156 
157  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
158  ndid = ipv6->AddInterface (dev);
159  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (v6Addr, v6Prefix);
160  ipv6->AddAddress (ndid, ipv6Addr);
161  ipv6->SetUp (ndid);
162 
163  return dev;
164 }
165 
166 void
168 {
169  node0 = CreateDualStackNode ();
170  node1 = CreateDualStackNode ();
171 
172  Ptr<SimpleNetDevice> dev0 = AddSimpleNetDevice(node0, Ipv4Address("10.0.0.1"),
173  Ipv4Mask(0xffffff00), Ipv6Address("2001::1"), Ipv6Prefix(64));
174  Ptr<SimpleNetDevice> dev1 = AddSimpleNetDevice(node1, Ipv4Address("10.0.0.2"),
175  Ipv4Mask(0xffffff00), Ipv6Address("2001::2"), Ipv6Prefix(64));
176 
177  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
178  dev0->SetChannel (channel);
179  dev1->SetChannel (channel);
180 
181  Ptr<SocketFactory> sockFactory0 = node0->GetObject<TcpSocketFactory> ();
182  Ptr<SocketFactory> sockFactory1 = node1->GetObject<TcpSocketFactory> ();
183 
184  server1 = sockFactory0->CreateSocket ();
185  server2 = sockFactory0->CreateSocket ();
186  server3 = sockFactory0->CreateSocket ();
187  server4 = sockFactory0->CreateSocket ();
188 
189  source1 = sockFactory1->CreateSocket ();
190  source2 = sockFactory1->CreateSocket ();
191  source3 = sockFactory1->CreateSocket ();
192  source4 = sockFactory1->CreateSocket ();
193 
194  return;
195 }
196 
197 void
199 {
200  receivedAddr1 = addr;
201  return;
202 }
203 
204 void
206 {
207  receivedAddr2 = addr;
208  return;
209 }
210 
211 void
213 {
214  receivedAddr3 = addr;
215  return;
216 }
217 
218 void
220 {
221  receivedAddr4 = addr;
222  return;
223 }
224 
225 
227  : TestCase ("DualStackTestCase")
228 {
229  receivedAddr1 = Address ();
230  receivedAddr2 = Address ();
231  receivedAddr3 = Address ();
232  receivedAddr4 = Address ();
233 }
234 
235 void
237 {
238  SetUpSim ();
239 
240  uint16_t port1 = 5000;
241  uint16_t port2 = 5001;
242  uint16_t port3 = 5002;
243  uint16_t port4 = 5003;
244 
245  /* Server 1: listen on 0.0.0.0 for IPv4 connection */
246  server1->Bind (InetSocketAddress(Ipv4Address::GetAny(), port1));
247  server1->Listen ();
250 
251  /* Server 2: listen on 0.0.0.0 for IPv4 connection - should reject IPv6 */
252  server2->Bind (InetSocketAddress(Ipv4Address::GetAny(), port2));
253  server2->Listen ();
256 
257  /* Server 3: listen on :: for IPv4 (mapped into IPv6 address) connection */
258  server3->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port3));
259  server3->Listen ();
262 
263  /* Server 4: listen on :: for IPv6 connection */
264  server4->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port4));
265  server4->Listen ();
268 
269 
270  /* Source 1: connect to server 1 via IPv4 */
271  source1->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port1));
272 
273  /* Source 2: connect to server 2 via IPv6 */
274  source2->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port2));
275 
276  /* Source 3: connect to server 3 via IPv4 */
277  source3->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port3));
278 
279  /* Source 4: connect to server 4 via IPv6 */
280  source4->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port4));
281 
282  Simulator::Run();
283 
284  /* Server 1: should have connection from Ipv4 address of Node 1 */
285  NS_TEST_EXPECT_MSG_EQ (InetSocketAddress::IsMatchingType(receivedAddr1), true, "Accepted address is of proper type");
286  NS_TEST_EXPECT_MSG_EQ(InetSocketAddress::ConvertFrom(receivedAddr1).GetIpv4 (), Ipv4Address("10.0.0.2"), "Accepted address is correct");
287 
288  /* Server 2: should have no connection */
289  NS_TEST_EXPECT_MSG_EQ(receivedAddr2.IsInvalid(), true, "IPv4 socket correctly ignored IPv6 connection");
290 
291  /* Server 3: should have connection from Ipv4-mapped IPv6 address of Node 1 */
292  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr3), true, "Accepted address is of proper type");
293  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr3).GetIpv6 (), Ipv6Address("::ffff:0a00:0002"), "Accepted address is correct");
294 
295  /* Server 4: should have connection from IPv6 address of Node 1 */
296  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr4), true, "Accepted address is of proper type");
297  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr4).GetIpv6 (), Ipv6Address("2001::2"), "Accepted address is correct");
298 }
299 
300 void
302 {
303  Simulator::Destroy ();
304 }
305 
306 
307 static class Ipv6DualStackTestSuite : public TestSuite
308 {
309 public:
311  : TestSuite ("ipv6-dual-stack", UNIT)
312  {
313  AddTestCase (new DualStackTestCase(), TestCase::QUICK);
314  }
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:462
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:257
A suite of tests to run.
Definition: test.h:1333
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:278
virtual Ptr< Socket > CreateSocket(void)=0
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
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:298
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: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
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: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.
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.
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.