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/drop-tail-queue.h"
32 #include "ns3/socket.h"
33 
34 #include "ns3/log.h"
35 #include "ns3/node.h"
36 #include "ns3/node-container.h"
37 #include "ns3/inet6-socket-address.h"
38 #include "ns3/boolean.h"
39 #include "ns3/uinteger.h"
40 
41 #include "ns3/internet-stack-helper.h"
42 #include "ns3/ipv6-l3-protocol.h"
43 #include "ns3/icmpv6-l4-protocol.h"
44 #include "ns3/ipv6-list-routing.h"
45 #include "ns3/ipv6-static-routing.h"
46 #include "ns3/ipv6-address-helper.h"
47 
48 #include <string>
49 #include <limits>
50 #include <netinet/in.h>
51 #include <sys/socket.h>
52 #include <sys/types.h>
53 
54 using namespace ns3;
55 
57 {
60  void DoSendData (Ptr<Socket> socket, std::string to);
61  void SendData (Ptr<Socket> socket, std::string to);
62 
63 public:
64  virtual void DoRun (void);
66 
67  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
68  void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
69  void ReceivePkt (Ptr<Socket> socket);
70  void ReceivePkt2 (Ptr<Socket> socket);
71 };
72 
73 
75  : TestCase ("Ipv6 Raw socket implementation")
76 {
77 }
78 
80 {
81  m_receivedPacket = packet;
82 }
83 
85 {
86  m_receivedPacket2 = packet;
87 }
88 
90 {
91  uint32_t availableData;
92  availableData = socket->GetRxAvailable ();
93  m_receivedPacket = socket->Recv (2, MSG_PEEK);
96  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
97 }
98 
100 {
101  uint32_t availableData;
102  Address addr;
103  availableData = socket->GetRxAvailable ();
104  m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
107  NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
108  Inet6SocketAddress v6addr = Inet6SocketAddress::ConvertFrom (addr);
109  NS_TEST_EXPECT_MSG_EQ (v6addr.GetIpv6 (), Ipv6Address ("2001:db8::2"), "recvfrom");
110 }
111 
112 void
114 {
115  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 0);
116  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
117  123, to);
118 }
119 
120 void
122 {
123  m_receivedPacket = Create<Packet> ();
124  m_receivedPacket2 = Create<Packet> ();
125  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
126  &Ipv6RawSocketImplTest::DoSendData, this, socket, to);
127  Simulator::Run ();
128 }
129 
130 void
132 {
133  // Create topology
134 
135  // Receiver Node
136  Ptr<Node> rxNode = CreateObject<Node> ();
137  // Sender Node
138  Ptr<Node> txNode = CreateObject<Node> ();
139 
140  NodeContainer nodes (rxNode, txNode);
141 
142  SimpleNetDeviceHelper helperChannel1;
143  helperChannel1.SetNetDevicePointToPointMode (true);
144  NetDeviceContainer net1 = helperChannel1.Install (nodes);
145 
146  SimpleNetDeviceHelper helperChannel2;
147  helperChannel2.SetNetDevicePointToPointMode (true);
148  NetDeviceContainer net2 = helperChannel2.Install (nodes);
149 
150  InternetStackHelper internetv6;
151  internetv6.Install (nodes);
152 
153  txNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
154  rxNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
155 
156  Ipv6AddressHelper ipv6helper;
157  Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress (net1);
158  Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress (net2);
159 
160  Ptr<NetDevice> device;
161  Ptr<Ipv6> ipv6;
162  int32_t ifIndex;
163  Ipv6InterfaceAddress ipv6Addr;
164 
165  ipv6 = rxNode->GetObject<Ipv6> ();
166  device = net1.Get (0);
167  ifIndex = ipv6->GetInterfaceForDevice (device);
168  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8::1"), Ipv6Prefix (64));
169  ipv6->AddAddress (ifIndex, ipv6Addr);
170 
171  device = net2.Get (0);
172  ifIndex = ipv6->GetInterfaceForDevice (device);
173  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:1::3"), Ipv6Prefix (64));
174  ipv6->AddAddress (ifIndex, ipv6Addr);
175 
176  ipv6 = txNode->GetObject<Ipv6> ();
177  device = net1.Get (1);
178  ifIndex = ipv6->GetInterfaceForDevice (device);
179  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8::2"), Ipv6Prefix (64));
180  ipv6->AddAddress (ifIndex, ipv6Addr);
181 
182  device = net2.Get (1);
183  ifIndex = ipv6->GetInterfaceForDevice (device);
184  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:1::4"), Ipv6Prefix (64));
185  ipv6->AddAddress (ifIndex, ipv6Addr);
186 
187  // Create the Ipv6 Raw sockets
188  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv6RawSocketFactory> ();
189  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
190  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
191  rxSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
192  rxSocket->SetRecvCallback (MakeCallback (&Ipv6RawSocketImplTest::ReceivePkt, this));
193 
194  Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
196  rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
197  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8:1::3"), 0)), 0, "trivial");
198 
199  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv6RawSocketFactory> ();
200  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
201  txSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
202 
203  // ------ Now the tests ------------
204 
205  // Unicast test
206  SendData (txSocket, "2001:db8::1");
207  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: 2001:db8::1");
208  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
209 
212 
213  // Simple Link-local multicast test
214  txSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8::2"), 0));
215  SendData (txSocket, "ff02::1");
216  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
217  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");
218 
221 
222  // Broadcast test with multiple receiving sockets
223 
224  // When receiving broadcast packets, all sockets sockets bound to
225  // the address/port should receive a copy of the same packet -- if
226  // the socket address matches.
227  rxSocket2->Dispose ();
228  rxSocket2 = rxSocketFactory->CreateSocket ();
230  rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
231  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
232 
233  SendData (txSocket, "ff02::1");
234  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
235  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 163, "recv: ff02::1");
236 
237  m_receivedPacket = 0;
238  m_receivedPacket2 = 0;
239 
240  // Simple getpeername tests
241 
242  Address peerAddress;
243  int err = txSocket->GetPeerName (peerAddress);
244  NS_TEST_EXPECT_MSG_EQ (err, -1, "socket GetPeerName() should fail when socket is not connected");
245  NS_TEST_EXPECT_MSG_EQ (txSocket->GetErrno (), Socket::ERROR_NOTCONN, "socket error code should be ERROR_NOTCONN");
246 
247  Inet6SocketAddress peer ("2001:db8::1", 1234);
248  err = txSocket->Connect (peer);
249  NS_TEST_EXPECT_MSG_EQ (err, 0, "socket Connect() should succeed");
250 
251  err = txSocket->GetPeerName (peerAddress);
252  NS_TEST_EXPECT_MSG_EQ (err, 0, "socket GetPeerName() should succeed when socket is connected");
253  peer.SetPort (0);
254  NS_TEST_EXPECT_MSG_EQ (peerAddress, peer, "address from socket GetPeerName() should equal the connected address");
255 
256  Simulator::Destroy ();
257 }
258 //-----------------------------------------------------------------------------
260 {
261 public:
262  Ipv6RawTestSuite () : TestSuite ("ipv6-raw", UNIT)
263  {
264  AddTestCase (new Ipv6RawSocketImplTest, TestCase::QUICK);
265  }
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
void ReceivePkt(Ptr< Socket > socket)
AttributeValue implementation for Boolean.
Definition: boolean.h:34
Keep track of a set of IPv6 interfaces.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
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< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
A suite of tests to run.
Definition: test.h:1333
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:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
void ReceivePkt2(Ptr< Socket > socket)
Ipv6RawTestSuite g_ipv6rawTestSuite
a polymophic address class
Definition: address.h:90
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
tuple nodes
Definition: first.py:25
API to create IPv6 RAW socket instances.
#define max(a, b)
Definition: 80211b.c:45
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
Hold an unsigned integer type.
Definition: uinteger.h:44
void SendData(Ptr< Socket > socket, std::string to)
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
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)
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void DoSendData(Ptr< Socket > socket, std::string to)
Helper class to auto-assign global IPv6 unicast addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:48
uint32_t GetId(void) const
Definition: node.cc:107
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:895
Ptr< Packet > m_receivedPacket
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:349
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
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
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:191
virtual int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const =0
Get the interface index of the specified NetDevice.
Ptr< Packet > m_receivedPacket2
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.