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
53using namespace ns3;
54
62{
65
71 void DoSendData (Ptr<Socket> socket, std::string to);
77 void SendData (Ptr<Socket> socket, std::string to);
78
79public:
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);
130 NS_TEST_ASSERT_MSG_EQ (m_receivedPacket->GetSize (), 2, "ReceivedPacket size is not equal to 2");
132 NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Received packet size is not equal to Rx buffer size");
133}
134
136{
137 uint32_t availableData;
138 Address addr;
139 availableData = socket->GetRxAvailable ();
140 m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
141 NS_TEST_ASSERT_MSG_EQ (m_receivedPacket2->GetSize (), 2, "ReceivedPacket size is not equal to 2");
143 NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket2->GetSize (), "Received packet size is not equal to Rx buffer size");
144 Inet6SocketAddress v6addr = Inet6SocketAddress::ConvertFrom (addr);
145 NS_TEST_EXPECT_MSG_EQ (v6addr.GetIpv6 (), Ipv6Address ("2001:db8::2"), "recvfrom");
146}
147
148void
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
156void
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
166void
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 ipv6->SetForwarding (ifIndex, true);
218
219 device = net2.Get (1);
220 ifIndex = ipv6->GetInterfaceForDevice (device);
221 ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:db8:1::4"), Ipv6Prefix (64));
222 ipv6->AddAddress (ifIndex, ipv6Addr);
223 ipv6->SetForwarding (ifIndex, true);
224
225 // Create the Ipv6 Raw sockets
226 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv6RawSocketFactory> ();
227 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
228 NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
229 rxSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
230 rxSocket->SetRecvCallback (MakeCallback (&Ipv6RawSocketImplTest::ReceivePkt, this));
231
232 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
234 rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
235 NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8:1::3"), 0)), 0, "trivial");
236
237 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv6RawSocketFactory> ();
238 Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
239 txSocket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
240
241 // ------ Now the tests ------------
242
243 // Unicast test
244 SendData (txSocket, "2001:db8::1");
245
246 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: 2001:db8::1");
247 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
248
251
252 // Simple Link-local multicast test
253 txSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:db8::2"), 0));
254 SendData (txSocket, "ff02::1");
255 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
256 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");
257
260
261 // Broadcast test with multiple receiving sockets
262
263 // When receiving broadcast packets, all sockets sockets bound to
264 // the address/port should receive a copy of the same packet -- if
265 // the socket address matches.
266 rxSocket2->Dispose ();
267 rxSocket2 = rxSocketFactory->CreateSocket ();
269 rxSocket2->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
270 NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (Inet6SocketAddress (Ipv6Address::GetAny (), 0)), 0, "trivial");
271
272 SendData (txSocket, "ff02::1");
273 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 163, "recv: ff02::1");
274 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 163, "recv: ff02::1");
275
278
279 // Simple getpeername tests
280
281 Address peerAddress;
282 int err = txSocket->GetPeerName (peerAddress);
283 NS_TEST_EXPECT_MSG_EQ (err, -1, "socket GetPeerName() should fail when socket is not connected");
284 NS_TEST_EXPECT_MSG_EQ (txSocket->GetErrno (), Socket::ERROR_NOTCONN, "socket error code should be ERROR_NOTCONN");
285
286 Inet6SocketAddress peer ("2001:db8::1", 1234);
287 err = txSocket->Connect (peer);
288 NS_TEST_EXPECT_MSG_EQ (err, 0, "socket Connect() should succeed");
289
290 err = txSocket->GetPeerName (peerAddress);
291 NS_TEST_EXPECT_MSG_EQ (err, 0, "socket GetPeerName() should succeed when socket is connected");
292 peer.SetPort (0);
293 NS_TEST_EXPECT_MSG_EQ (peerAddress, peer, "address from socket GetPeerName() should equal the connected address");
294
295 Simulator::Destroy ();
296}
297
298
306{
307public:
308 Ipv6RawTestSuite () : TestSuite ("ipv6-raw", UNIT)
309 {
310 AddTestCase (new Ipv6RawSocketImplTest, TestCase::QUICK);
311 }
312};
313
315
#define max(a, b)
Definition: 80211b.c:43
IPv6 RAW Socket Test.
Ptr< Packet > m_receivedPacket2
Received packet (2).
virtual void DoRun(void)
Implementation to actually run this TestCase.
Ptr< Packet > m_receivedPacket
Received packet (1).
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void ReceivePkt2(Ptr< Socket > socket)
Receive data.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void SendData(Ptr< Socket > socket, std::string to)
Send data.
IPv6 RAW Socket TestSuite.
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An implementation of the ICMPv6 protocol.
An Inet6 address class.
void SetPort(uint16_t port)
Set the port.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
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.
Keep track of a set of IPv6 interfaces.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
API to create IPv6 RAW socket instances.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
uint32_t GetId(void) const
Definition: node.cc:109
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
build a set of SimpleNetDevice objects
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
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.
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
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
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:141
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static Ipv6RawTestSuite g_ipv6rawTestSuite
Static variable for test initialization.
nodes
Definition: first.py:32
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