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  // Traffic Control
124  Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
125  node->AggregateObject (tc);
126 
127  //ARP
128  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
129  node->AggregateObject (arp);
130  arp->SetTrafficControl (tc);
131 
132  //IPV4
133  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
134  //Routing for Ipv4
135  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
136  ipv4->SetRoutingProtocol (ipv4Routing);
137  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
138  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
139  node->AggregateObject (ipv4);
140 
141  //ICMPv4
142  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
143  node->AggregateObject (icmp);
144 
145  //UDP
146  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
147  node->AggregateObject (udp);
148 
149  //TCP
150  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
151  node->AggregateObject (tcp);
152 
153  //IPV6
154  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
155 
156  //Routing for Ipv6
157  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
158  ipv6->SetRoutingProtocol (ipv6Routing);
159  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
160  ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
161  node->AggregateObject (ipv6);
162 
163  //ICMPv6
164  Ptr<Icmpv6L4Protocol> icmp6 = CreateObject<Icmpv6L4Protocol> ();
165  node->AggregateObject (icmp6);
166 
167  //Ipv6 Extensions
168  ipv6->RegisterExtensions ();
169  ipv6->RegisterOptions ();
170 
171  return node;
172 }
173 
176  Ipv6Address v6Addr, Ipv6Prefix v6Prefix)
177 {
178  Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice> ();
179  dev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
180  node->AddDevice (dev);
181 
182  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
183  uint32_t ndid = ipv4->AddInterface (dev);
184  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (v4Addr, v4Mask);
185  ipv4->AddAddress (ndid, ipv4Addr);
186  ipv4->SetUp (ndid);
187 
188  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
189  ndid = ipv6->AddInterface (dev);
190  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (v6Addr, v6Prefix);
191  ipv6->AddAddress (ndid, ipv6Addr);
192  ipv6->SetUp (ndid);
193 
194  return dev;
195 }
196 
197 void
199 {
200  node0 = CreateDualStackNode ();
201  node1 = CreateDualStackNode ();
202 
203  Ptr<SimpleNetDevice> dev0 = AddSimpleNetDevice(node0, Ipv4Address("10.0.0.1"),
204  Ipv4Mask(0xffffff00), Ipv6Address("2001::1"), Ipv6Prefix(64));
205  Ptr<SimpleNetDevice> dev1 = AddSimpleNetDevice(node1, Ipv4Address("10.0.0.2"),
206  Ipv4Mask(0xffffff00), Ipv6Address("2001::2"), Ipv6Prefix(64));
207 
208  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
209  dev0->SetChannel (channel);
210  dev1->SetChannel (channel);
211 
212  Ptr<SocketFactory> sockFactory0 = node0->GetObject<TcpSocketFactory> ();
213  Ptr<SocketFactory> sockFactory1 = node1->GetObject<TcpSocketFactory> ();
214 
215  server1 = sockFactory0->CreateSocket ();
216  server2 = sockFactory0->CreateSocket ();
217  server3 = sockFactory0->CreateSocket ();
218  server4 = sockFactory0->CreateSocket ();
219 
220  source1 = sockFactory1->CreateSocket ();
221  source2 = sockFactory1->CreateSocket ();
222  source3 = sockFactory1->CreateSocket ();
223  source4 = sockFactory1->CreateSocket ();
224 
225  return;
226 }
227 
228 void
230 {
231  receivedAddr1 = addr;
232  return;
233 }
234 
235 void
237 {
238  receivedAddr2 = addr;
239  return;
240 }
241 
242 void
244 {
245  receivedAddr3 = addr;
246  return;
247 }
248 
249 void
251 {
252  receivedAddr4 = addr;
253  return;
254 }
255 
256 
258  : TestCase ("DualStackTestCase")
259 {
260  receivedAddr1 = Address ();
261  receivedAddr2 = Address ();
262  receivedAddr3 = Address ();
263  receivedAddr4 = Address ();
264 }
265 
266 void
268 {
269  SetUpSim ();
270 
271  uint16_t port1 = 5000;
272  uint16_t port2 = 5001;
273  uint16_t port3 = 5002;
274  uint16_t port4 = 5003;
275 
276  /* Server 1: listen on 0.0.0.0 for IPv4 connection */
277  server1->Bind (InetSocketAddress(Ipv4Address::GetAny(), port1));
278  server1->Listen ();
281 
282  /* Server 2: listen on 0.0.0.0 for IPv4 connection - should reject IPv6 */
283  server2->Bind (InetSocketAddress(Ipv4Address::GetAny(), port2));
284  server2->Listen ();
287 
288  /* Server 3: listen on :: for IPv4 (mapped into IPv6 address) connection */
289  server3->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port3));
290  server3->Listen ();
293 
294  /* Server 4: listen on :: for IPv6 connection */
295  server4->Bind (Inet6SocketAddress(Ipv6Address::GetAny(), port4));
296  server4->Listen ();
299 
300 
301  /* Source 1: connect to server 1 via IPv4 */
302  source1->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port1));
303 
304  /* Source 2: connect to server 2 via IPv6 */
305  source2->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port2));
306 
307  /* Source 3: connect to server 3 via IPv4 */
308  source3->Connect(InetSocketAddress(Ipv4Address("10.0.0.1"), port3));
309 
310  /* Source 4: connect to server 4 via IPv6 */
311  source4->Connect(Inet6SocketAddress(Ipv6Address("2001::1"), port4));
312 
313  Simulator::Run();
314 
315  /* Server 1: should have connection from Ipv4 address of Node 1 */
316  NS_TEST_EXPECT_MSG_EQ (InetSocketAddress::IsMatchingType(receivedAddr1), true, "Accepted address is of proper type");
317  NS_TEST_EXPECT_MSG_EQ(InetSocketAddress::ConvertFrom(receivedAddr1).GetIpv4 (), Ipv4Address("10.0.0.2"), "Accepted address is correct");
318 
319  /* Server 2: should have no connection */
320  NS_TEST_EXPECT_MSG_EQ(receivedAddr2.IsInvalid(), true, "IPv4 socket correctly ignored IPv6 connection");
321 
322  /* Server 3: should have connection from Ipv4-mapped IPv6 address of Node 1 */
323  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr3), true, "Accepted address is of proper type");
324  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr3).GetIpv6 (), Ipv6Address("::ffff:0a00:0002"), "Accepted address is correct");
325 
326  /* Server 4: should have connection from IPv6 address of Node 1 */
327  NS_TEST_EXPECT_MSG_EQ (Inet6SocketAddress::IsMatchingType(receivedAddr4), true, "Accepted address is of proper type");
328  NS_TEST_EXPECT_MSG_EQ(Inet6SocketAddress::ConvertFrom(receivedAddr4).GetIpv6 (), Ipv6Address("2001::2"), "Accepted address is correct");
329 }
330 
331 void
333 {
334  Simulator::Destroy ();
335 }
336 
344 {
345 public:
347  : TestSuite ("ipv6-dual-stack", UNIT)
348  {
349  AddTestCase (new DualStackTestCase(), TestCase::QUICK);
350  }
351 };
352 
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).
virtual void DoRun(void)
Implementation to actually run this TestCase.
void ServerHandleConnectionCreated2(Ptr< Socket > s, const Address &addr)
Handle connection created (2).
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Ptr< Socket > source2
Sending socket (2).
Ptr< Socket > server1
Server socket (1).
Address receivedAddr2
Received address (2).
Address receivedAddr1
Received address (1).
Ptr< Socket > source3
Sending socket (3).
Ptr< Socket > source4
Sending socket (4).
IPv6 dual stack TestSuite.
a polymophic address class
Definition: address.h:91
bool IsInvalid(void) const
Definition: address.cc:68
An Inet6 address class.
an Inet address class
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
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
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
static Mac48Address ConvertFrom(const Address &address)
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
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 int Listen(void)=0
Listen for incoming connections.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
API to create TCP socket instances.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1688
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:240
Ptr< Node > CreateDualStackNode()
static Ipv6DualStackTestSuite g_ipv6DualStackTestSuite
Static variable for test initialization.
Ptr< SimpleNetDevice > AddSimpleNetDevice(Ptr< Node > node, Ipv4Address v4Addr, Ipv4Mask v4Mask, Ipv6Address v6Addr, Ipv6Prefix v6Prefix)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
channel
Definition: third.py:92