A Discrete-Event Network Simulator
API
ipv6-raw-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Hajime Tazaki
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
18 */
23#include "ns3/boolean.h"
24#include "ns3/icmpv6-l4-protocol.h"
25#include "ns3/inet6-socket-address.h"
26#include "ns3/internet-stack-helper.h"
27#include "ns3/ipv6-address-helper.h"
28#include "ns3/ipv6-l3-protocol.h"
29#include "ns3/ipv6-list-routing.h"
30#include "ns3/ipv6-raw-socket-factory.h"
31#include "ns3/ipv6-static-routing.h"
32#include "ns3/log.h"
33#include "ns3/node-container.h"
34#include "ns3/node.h"
35#include "ns3/simple-channel.h"
36#include "ns3/simple-net-device-helper.h"
37#include "ns3/simple-net-device.h"
38#include "ns3/simulator.h"
39#include "ns3/socket-factory.h"
40#include "ns3/socket.h"
41#include "ns3/test.h"
42#include "ns3/uinteger.h"
43
44#ifdef __WIN32__
45#include "ns3/win32-internet.h"
46#else
47#include <netinet/in.h>
48#include <sys/socket.h>
49#endif
50
51#include <limits>
52#include <string>
53#include <sys/types.h>
54
55using namespace ns3;
56
64{
67
73 void DoSendData(Ptr<Socket> socket, std::string to);
79 void SendData(Ptr<Socket> socket, std::string to);
80
81 public:
82 void DoRun() override;
84
91 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
98 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
103 void ReceivePkt(Ptr<Socket> socket);
108 void ReceivePkt2(Ptr<Socket> socket);
109};
110
112 : TestCase("Ipv6 Raw socket implementation")
113{
114}
115
116void
118{
119 m_receivedPacket = packet;
120}
121
122void
124{
125 m_receivedPacket2 = packet;
126}
127
128void
130{
131 uint32_t availableData;
132 availableData = socket->GetRxAvailable();
133 m_receivedPacket = socket->Recv(2, MSG_PEEK);
134 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket->GetSize(), 2, "ReceivedPacket size is not equal to 2");
136 NS_TEST_ASSERT_MSG_EQ(availableData,
138 "Received packet size is not equal to Rx buffer size");
139}
140
141void
143{
144 uint32_t availableData;
145 Address addr;
146 availableData = socket->GetRxAvailable();
147 m_receivedPacket2 = socket->Recv(2, MSG_PEEK);
148 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket2->GetSize(), 2, "ReceivedPacket size is not equal to 2");
150 NS_TEST_ASSERT_MSG_EQ(availableData,
152 "Received packet size is not equal to Rx buffer size");
153 Inet6SocketAddress v6addr = Inet6SocketAddress::ConvertFrom(addr);
154 NS_TEST_EXPECT_MSG_EQ(v6addr.GetIpv6(), Ipv6Address("2001:db8::2"), "recvfrom");
155}
156
157void
159{
160 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 0);
161 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, to);
162}
163
164void
166{
167 m_receivedPacket = Create<Packet>();
168 m_receivedPacket2 = Create<Packet>();
169 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
170 Seconds(0),
172 this,
173 socket,
174 to);
175 Simulator::Run();
176}
177
178void
180{
181 // Create topology
182
183 // Receiver Node
184 Ptr<Node> rxNode = CreateObject<Node>();
185 // Sender Node
186 Ptr<Node> txNode = CreateObject<Node>();
187
188 NodeContainer nodes(rxNode, txNode);
189
190 SimpleNetDeviceHelper helperChannel1;
191 helperChannel1.SetNetDevicePointToPointMode(true);
192 NetDeviceContainer net1 = helperChannel1.Install(nodes);
193
194 SimpleNetDeviceHelper helperChannel2;
195 helperChannel2.SetNetDevicePointToPointMode(true);
196 NetDeviceContainer net2 = helperChannel2.Install(nodes);
197
198 InternetStackHelper internetv6;
199 internetv6.Install(nodes);
200
201 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
202 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
203
204 Ipv6AddressHelper ipv6helper;
205 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
206 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
207
208 Ptr<NetDevice> device;
209 Ptr<Ipv6> ipv6;
210 int32_t ifIndex;
211 Ipv6InterfaceAddress ipv6Addr;
212
213 ipv6 = rxNode->GetObject<Ipv6>();
214 device = net1.Get(0);
215 ifIndex = ipv6->GetInterfaceForDevice(device);
216 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8::1"), Ipv6Prefix(64));
217 ipv6->AddAddress(ifIndex, ipv6Addr);
218
219 device = net2.Get(0);
220 ifIndex = ipv6->GetInterfaceForDevice(device);
221 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8:1::3"), Ipv6Prefix(64));
222 ipv6->AddAddress(ifIndex, ipv6Addr);
223
224 ipv6 = txNode->GetObject<Ipv6>();
225 device = net1.Get(1);
226 ifIndex = ipv6->GetInterfaceForDevice(device);
227 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8::2"), Ipv6Prefix(64));
228 ipv6->AddAddress(ifIndex, ipv6Addr);
229 ipv6->SetForwarding(ifIndex, true);
230
231 device = net2.Get(1);
232 ifIndex = ipv6->GetInterfaceForDevice(device);
233 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8:1::4"), Ipv6Prefix(64));
234 ipv6->AddAddress(ifIndex, ipv6Addr);
235 ipv6->SetForwarding(ifIndex, true);
236
237 // Create the Ipv6 Raw sockets
238 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv6RawSocketFactory>();
239 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
240 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address::GetAny(), 0)),
241 0,
242 "trivial");
243 rxSocket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
244 rxSocket->SetRecvCallback(MakeCallback(&Ipv6RawSocketImplTest::ReceivePkt, this));
245
246 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
248 rxSocket2->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
249 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address("2001:db8:1::3"), 0)),
250 0,
251 "trivial");
252
253 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv6RawSocketFactory>();
254 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
255 txSocket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
256
257 // ------ Now the tests ------------
258
259 // Unicast test
260 SendData(txSocket, "2001:db8::1");
261
262 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: 2001:db8::1");
264 0,
265 "second interface should not receive it");
266
269
270 // Simple Link-local multicast test
271 txSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:db8::2"), 0));
272 SendData(txSocket, "ff02::1");
273 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: ff02::1");
275 0,
276 "second socket should not receive it (it is bound specifically to the "
277 "second interface's address");
278
281
282 // Broadcast test with multiple receiving sockets
283
284 // When receiving broadcast packets, all sockets sockets bound to
285 // the address/port should receive a copy of the same packet -- if
286 // the socket address matches.
287 rxSocket2->Dispose();
288 rxSocket2 = rxSocketFactory->CreateSocket();
290 rxSocket2->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
291 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address::GetAny(), 0)),
292 0,
293 "trivial");
294
295 SendData(txSocket, "ff02::1");
296 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: ff02::1");
297 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 163, "recv: ff02::1");
298
299 m_receivedPacket = nullptr;
300 m_receivedPacket2 = nullptr;
301
302 // Simple getpeername tests
303
304 Address peerAddress;
305 int err = txSocket->GetPeerName(peerAddress);
306 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
307 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
308 Socket::ERROR_NOTCONN,
309 "socket error code should be ERROR_NOTCONN");
310
311 Inet6SocketAddress peer("2001:db8::1", 1234);
312 err = txSocket->Connect(peer);
313 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
314
315 err = txSocket->GetPeerName(peerAddress);
316 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
317 peer.SetPort(0);
318 NS_TEST_EXPECT_MSG_EQ(peerAddress,
319 peer,
320 "address from socket GetPeerName() should equal the connected address");
321
322 Simulator::Destroy();
323}
324
332{
333 public:
335 : TestSuite("ipv6-raw", UNIT)
336 {
337 AddTestCase(new Ipv6RawSocketImplTest, TestCase::QUICK);
338 }
339};
340
#define max(a, b)
Definition: 80211b.c:43
IPv6 RAW Socket Test.
Ptr< Packet > m_receivedPacket2
Received packet (2).
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 DoRun() override
Implementation to actually run this TestCase.
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:92
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() 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() const
Definition: node.cc:117
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:258
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void Dispose()
Dispose of this Object.
Definition: object.cc:219
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
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 uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
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 Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Hold an unsigned integer type.
Definition: uinteger.h:45
#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:144
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
static Ipv6RawTestSuite g_ipv6rawTestSuite
Static variable for test initialization.
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:691