A Discrete-Event Network Simulator
API
ipv6-raw-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 Hajime Tazaki
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  * Author: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
19  */
24 #include "ns3/test.h"
25 #include "ns3/socket-factory.h"
26 #include "ns3/ipv6-raw-socket-factory.h"
27 #include "ns3/simulator.h"
28 #include "ns3/simple-channel.h"
29 #include "ns3/simple-net-device.h"
30 #include "ns3/simple-net-device-helper.h"
31 #include "ns3/socket.h"
32 
33 #include "ns3/log.h"
34 #include "ns3/node.h"
35 #include "ns3/node-container.h"
36 #include "ns3/inet6-socket-address.h"
37 #include "ns3/boolean.h"
38 #include "ns3/uinteger.h"
39 
40 #include "ns3/internet-stack-helper.h"
41 #include "ns3/ipv6-l3-protocol.h"
42 #include "ns3/icmpv6-l4-protocol.h"
43 #include "ns3/ipv6-list-routing.h"
44 #include "ns3/ipv6-static-routing.h"
45 #include "ns3/ipv6-address-helper.h"
46 
47 #include <string>
48 #include <limits>
49 #include <netinet/in.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
52 
53 using namespace ns3;
54 
62 {
65 
71  void DoSendData (Ptr<Socket> socket, std::string to);
77  void SendData (Ptr<Socket> socket, std::string to);
78 
79 public:
80  virtual void DoRun (void);
82 
89  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
96  void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
101  void ReceivePkt (Ptr<Socket> socket);
106  void ReceivePkt2 (Ptr<Socket> socket);
107 };
108 
109 
111  : TestCase ("Ipv6 Raw socket implementation")
112 {
113 }
114 
116 {
117  m_receivedPacket = packet;
118 }
119 
121 {
122  m_receivedPacket2 = packet;
123 }
124 
126 {
127  uint32_t availableData;
128  availableData = socket->GetRxAvailable ();
129  m_receivedPacket = socket->Recv (2, MSG_PEEK);
132  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
133 }
134 
136 {
137  uint32_t availableData;
138  Address addr;
139  availableData = socket->GetRxAvailable ();
140  m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
143  NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
144  Inet6SocketAddress v6addr = Inet6SocketAddress::ConvertFrom (addr);
145  NS_TEST_EXPECT_MSG_EQ (v6addr.GetIpv6 (), Ipv6Address ("2001:db8::2"), "recvfrom");
146 }
147 
148 void
150 {
151  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 0);
152  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
153  123, to);
154 }
155 
156 void
158 {
159  m_receivedPacket = Create<Packet> ();
160  m_receivedPacket2 = Create<Packet> ();
161  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
162  &Ipv6RawSocketImplTest::DoSendData, this, socket, to);
163  Simulator::Run ();
164 }
165 
166 void
168 {
169  // Create topology
170 
171  // Receiver Node
172  Ptr<Node> rxNode = CreateObject<Node> ();
173  // Sender Node
174  Ptr<Node> txNode = CreateObject<Node> ();
175 
176  NodeContainer nodes (rxNode, txNode);
177 
178  SimpleNetDeviceHelper helperChannel1;
179  helperChannel1.SetNetDevicePointToPointMode (true);
180  NetDeviceContainer net1 = helperChannel1.Install (nodes);
181 
182  SimpleNetDeviceHelper helperChannel2;
183  helperChannel2.SetNetDevicePointToPointMode (true);
184  NetDeviceContainer net2 = helperChannel2.Install (nodes);
185 
186  InternetStackHelper internetv6;
187  internetv6.Install (nodes);
188 
189  txNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
190  rxNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
191 
192  Ipv6AddressHelper ipv6helper;
193  Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress (net1);
194  Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress (net2);
195 
196  Ptr<NetDevice> device;
197  Ptr<Ipv6> ipv6;
198  int32_t ifIndex;
199  Ipv6InterfaceAddress ipv6Addr;
200 
201  ipv6 = rxNode->GetObject<Ipv6> ();
202  device = net1.Get (0);
203  ifIndex = ipv6->GetInterfaceForDevice (device);
204  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8::1"), Ipv6Prefix (64));
205  ipv6->AddAddress (ifIndex, ipv6Addr);
206 
207  device = net2.Get (0);
208  ifIndex = ipv6->GetInterfaceForDevice (device);
209  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:1::3"), Ipv6Prefix (64));
210  ipv6->AddAddress (ifIndex, ipv6Addr);
211 
212  ipv6 = txNode->GetObject<Ipv6> ();
213  device = net1.Get (1);
214  ifIndex = ipv6->GetInterfaceForDevice (device);
215  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8::2"), Ipv6Prefix (64));
216  ipv6->AddAddress (ifIndex, ipv6Addr);
217 
218  device = net2.Get (1);
219  ifIndex = ipv6->GetInterfaceForDevice (device);
220  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:1::4"), Ipv6Prefix (64));
221  ipv6->AddAddress (ifIndex, ipv6Addr);
222 
223  // Create the Ipv6 Raw sockets
224  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv6RawSocketFactory> ();
225  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
226  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
227  rxSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
228  rxSocket->SetRecvCallback (MakeCallback (&Ipv6RawSocketImplTest::ReceivePkt, this));
229 
230  Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
232  rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
233  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8:1::3"), 0)), 0, "trivial");
234 
235  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv6RawSocketFactory> ();
236  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
237  txSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
238 
239  // ------ Now the tests ------------
240 
241  // Unicast test
242  SendData (txSocket, "2001:db8::1");
243  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: 2001:db8::1");
244  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
245 
248 
249  // Simple Link-local multicast test
250  txSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8::2"), 0));
251  SendData (txSocket, "ff02::1");
252  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
253  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second socket should not receive it (it is bound specifically to the second interface's address");
254 
257 
258  // Broadcast test with multiple receiving sockets
259 
260  // When receiving broadcast packets, all sockets sockets bound to
261  // the address/port should receive a copy of the same packet -- if
262  // the socket address matches.
263  rxSocket2->Dispose ();
264  rxSocket2 = rxSocketFactory->CreateSocket ();
266  rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
267  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
268 
269  SendData (txSocket, "ff02::1");
270  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
271  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 163, "recv: ff02::1");
272 
273  m_receivedPacket = 0;
274  m_receivedPacket2 = 0;
275 
276  // Simple getpeername tests
277 
278  Address peerAddress;
279  int err = txSocket->GetPeerName (peerAddress);
280  NS_TEST_EXPECT_MSG_EQ (err, -1, "socket GetPeerName() should fail when socket is not connected");
281  NS_TEST_EXPECT_MSG_EQ (txSocket->GetErrno (), Socket::ERROR_NOTCONN, "socket error code should be ERROR_NOTCONN");
282 
283  Inet6SocketAddress peer ("2001:db8::1", 1234);
284  err = txSocket->Connect (peer);
285  NS_TEST_EXPECT_MSG_EQ (err, 0, "socket Connect() should succeed");
286 
287  err = txSocket->GetPeerName (peerAddress);
288  NS_TEST_EXPECT_MSG_EQ (err, 0, "socket GetPeerName() should succeed when socket is connected");
289  peer.SetPort (0);
290  NS_TEST_EXPECT_MSG_EQ (peerAddress, peer, "address from socket GetPeerName() should equal the connected address");
291 
292  Simulator::Destroy ();
293 }
294 
295 
303 {
304 public:
305  Ipv6RawTestSuite () : TestSuite ("ipv6-raw", UNIT)
306  {
307  AddTestCase (new Ipv6RawSocketImplTest, TestCase::QUICK);
308  }
309 };
310 
312 
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
void ReceivePkt(Ptr< Socket > socket)
Receive data.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
uint32_t GetId(void) const
Definition: node.cc:107
Keep track of a set of IPv6 interfaces.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
A suite of tests to run.
Definition: test.h:1342
void ReceivePacket(Ptr< Socket > socket)
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
IPv6 address associated with an interface.
aggregate IP/TCP/UDP functionality to existing Nodes.
#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
IPv6 RAW Socket TestSuite.
encapsulates test code
Definition: test.h:1155
void ReceivePkt2(Ptr< Socket > socket)
Receive data.
a polymophic address class
Definition: address.h:90
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
API to create IPv6 RAW socket instances.
nodes
Definition: first.py:25
#define max(a, b)
Definition: 80211b.c:43
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Hold an unsigned integer type.
Definition: uinteger.h:44
void SendData(Ptr< Socket > socket, std::string to)
Send data.
virtual void DoRun(void)
Implementation to actually run this TestCase.
holds a vector of ns3::NetDevice pointers
An Inet6 address class.
An implementation of the ICMPv6 protocol.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
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 Ipv6RawTestSuite g_ipv6rawTestSuite
Static variable for test initialization.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
Helper class to auto-assign global IPv6 unicast addresses.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Describes an IPv6 address.
Definition: ipv6-address.h:49
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
Ptr< Packet > m_receivedPacket
Received packet (1).
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
IPv6 RAW Socket Test.
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
build a set of SimpleNetDevice objects
This test suite implements a Unit Test.
Definition: test.h:1352
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
virtual int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const =0
Get the interface index of the specified NetDevice.
Ptr< Packet > m_receivedPacket2
Received packet (2).
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.