A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-raw-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hajime Tazaki
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
7 */
8/**
9 * This is the test code for ipv4-raw-socket-impl.cc.
10 */
11
12#include "ns3/arp-l3-protocol.h"
13#include "ns3/boolean.h"
14#include "ns3/icmpv4-l4-protocol.h"
15#include "ns3/inet-socket-address.h"
16#include "ns3/internet-stack-helper.h"
17#include "ns3/ipv4-l3-protocol.h"
18#include "ns3/ipv4-list-routing.h"
19#include "ns3/ipv4-raw-socket-factory.h"
20#include "ns3/ipv4-static-routing.h"
21#include "ns3/log.h"
22#include "ns3/node.h"
23#include "ns3/simple-channel.h"
24#include "ns3/simple-net-device-helper.h"
25#include "ns3/simple-net-device.h"
26#include "ns3/simulator.h"
27#include "ns3/socket-factory.h"
28#include "ns3/socket.h"
29#include "ns3/test.h"
30
31#ifdef __WIN32__
32#include "ns3/win32-internet.h"
33#else
34#include <netinet/in.h>
35#include <sys/socket.h>
36#endif
37
38#include <limits>
39#include <string>
40#include <sys/types.h>
41
42using namespace ns3;
43
44/**
45 * @ingroup internet-test
46 *
47 * @brief IPv4 RAW Socket Test
48 */
50{
51 Ptr<Packet> m_receivedPacket; //!< Received packet (1).
52 Ptr<Packet> m_receivedPacket2; //!< Received packet (2).
53
54 /**
55 * @brief Send data.
56 * @param socket The sending socket.
57 * @param to Destination address.
58 */
59 void DoSendData(Ptr<Socket> socket, std::string to);
60 /**
61 * @brief Send data.
62 * @param socket The sending socket.
63 * @param to Destination address.
64 */
65 void SendData(Ptr<Socket> socket, std::string to);
66 /**
67 * @brief Send data.
68 * @param socket The sending socket.
69 * @param to Destination address.
70 */
71 void DoSendData_IpHdr(Ptr<Socket> socket, std::string to);
72 /**
73 * @brief Send data.
74 * @param socket The sending socket.
75 * @param to Destination address.
76 */
77 void SendData_IpHdr(Ptr<Socket> socket, std::string to);
78
79 public:
80 void DoRun() override;
82
83 /**
84 * @brief Receive data.
85 * @param socket The receiving socket.
86 * @param packet The received packet.
87 * @param from The sender.
88 */
89 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
90 /**
91 * @brief Receive data.
92 * @param socket The receiving socket.
93 * @param packet The received packet.
94 * @param from The sender.
95 */
96 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
97 /**
98 * @brief Receive data.
99 * @param socket The receiving socket.
100 */
101 void ReceivePkt(Ptr<Socket> socket);
102 /**
103 * @brief Receive data.
104 * @param socket The receiving socket.
105 */
106 void ReceivePkt2(Ptr<Socket> socket);
107};
108
110 : TestCase("IPv4 Raw socket implementation")
111{
112}
113
114void
116{
117 m_receivedPacket = packet;
118}
119
120void
122{
123 m_receivedPacket2 = packet;
124}
125
126void
128{
129 uint32_t availableData;
130 availableData = socket->GetRxAvailable();
131 m_receivedPacket = socket->Recv(2, MSG_PEEK);
132 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket->GetSize(), 2, "ReceivedPacket size is not equal to 2");
133 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
134 NS_TEST_ASSERT_MSG_EQ(availableData,
136 "Received packet size is not equal to Rx buffer size");
137}
138
139void
141{
142 uint32_t availableData;
143 availableData = socket->GetRxAvailable();
144 m_receivedPacket2 = socket->Recv(2, MSG_PEEK);
145 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket2->GetSize(), 2, "ReceivedPacket size is not equal to 2");
146 m_receivedPacket2 = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
147 NS_TEST_ASSERT_MSG_EQ(availableData,
149 "Received packet size is not equal to Rx buffer size");
150}
151
152void
154{
155 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
156 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, to);
157}
158
159void
161{
164 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
165 Seconds(0),
167 this,
168 socket,
169 to);
171}
172
173void
175{
176 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
177 socket->SetAttribute("IpHeaderInclude", BooleanValue(true));
179 Ipv4Header ipHeader;
180 ipHeader.SetSource(Ipv4Address("10.0.0.2"));
181 ipHeader.SetDestination(Ipv4Address(to.c_str()));
182 ipHeader.SetProtocol(0);
183 ipHeader.SetPayloadSize(p->GetSize());
184 ipHeader.SetTtl(255);
185 p->AddHeader(ipHeader);
186
187 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(p, 0, realTo), 143, to);
188 socket->SetAttribute("IpHeaderInclude", BooleanValue(false));
189}
190
191void
193{
196 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
197 Seconds(0),
199 this,
200 socket,
201 to);
203}
204
205void
207{
208 // Create topology
209
210 // Receiver Node
211 Ptr<Node> rxNode = CreateObject<Node>();
212 // Sender Node
213 Ptr<Node> txNode = CreateObject<Node>();
214
215 NodeContainer nodes(rxNode, txNode);
216
217 SimpleNetDeviceHelper helperChannel1;
218 helperChannel1.SetNetDevicePointToPointMode(true);
219 NetDeviceContainer net1 = helperChannel1.Install(nodes);
220
221 SimpleNetDeviceHelper helperChannel2;
222 helperChannel2.SetNetDevicePointToPointMode(true);
223 NetDeviceContainer net2 = helperChannel2.Install(nodes);
224
225 InternetStackHelper internet;
226 internet.Install(nodes);
227
228 Ptr<Ipv4> ipv4;
229 uint32_t netdev_idx;
230 Ipv4InterfaceAddress ipv4Addr;
231
232 // Receiver Node
233 ipv4 = rxNode->GetObject<Ipv4>();
234 netdev_idx = ipv4->AddInterface(net1.Get(0));
235 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
236 ipv4->AddAddress(netdev_idx, ipv4Addr);
237 ipv4->SetUp(netdev_idx);
238
239 netdev_idx = ipv4->AddInterface(net2.Get(0));
240 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.1"), Ipv4Mask(0xffff0000U));
241 ipv4->AddAddress(netdev_idx, ipv4Addr);
242 ipv4->SetUp(netdev_idx);
243
244 // Sender Node
245 ipv4 = txNode->GetObject<Ipv4>();
246 netdev_idx = ipv4->AddInterface(net1.Get(1));
247 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
248 ipv4->AddAddress(netdev_idx, ipv4Addr);
249 ipv4->SetUp(netdev_idx);
250
251 netdev_idx = ipv4->AddInterface(net2.Get(1));
252 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.2"), Ipv4Mask(0xffff0000U));
253 ipv4->AddAddress(netdev_idx, ipv4Addr);
254 ipv4->SetUp(netdev_idx);
255
256 // Create the IPv4 Raw sockets
257 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory>();
258 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
259 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 0)),
260 0,
261 "trivial");
262 rxSocket->SetRecvCallback(MakeCallback(&Ipv4RawSocketImplTest::ReceivePkt, this));
263
264 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
265 rxSocket2->SetRecvCallback(MakeCallback(&Ipv4RawSocketImplTest::ReceivePkt2, this));
266 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(InetSocketAddress(Ipv4Address("10.0.1.1"), 0)),
267 0,
268 "trivial");
269
270 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory>();
271 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
272
273 // ------ Now the tests ------------
274
275 // Unicast test
276 SendData(txSocket, "10.0.0.1");
277 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv: 10.0.0.1");
279 0,
280 "second interface should not receive it");
281
284
285 // Unicast w/ header test
286 SendData_IpHdr(txSocket, "10.0.0.1");
287 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
289 0,
290 "second interface should not receive it");
291
294
295#if 0
296 // Simple broadcast test
297
298 SendData (txSocket, "255.255.255.255");
299 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
300 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");
301
304#endif
305
306 // Simple Link-local multicast test
307
308 txSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.2"), 0));
309 SendData(txSocket, "224.0.0.9");
310 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv: 224.0.0.9");
312 0,
313 "second socket should not receive it (it is bound specifically to the "
314 "second interface's address");
315
318
319#if 0
320 // Broadcast test with multiple receiving sockets
321
322 // When receiving broadcast packets, all sockets sockets bound to
323 // the address/port should receive a copy of the same packet -- if
324 // the socket address matches.
325 rxSocket2->Dispose ();
326 rxSocket2 = rxSocketFactory->CreateSocket ();
327 rxSocket2->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt2, this));
328 NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
329
330 SendData (txSocket, "255.255.255.255");
331 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
332 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
333#endif
334
335 m_receivedPacket = nullptr;
336 m_receivedPacket2 = nullptr;
337
338 // Simple getpeername tests
339
340 Address peerAddress;
341 int err = txSocket->GetPeerName(peerAddress);
342 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
343 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
345 "socket error code should be ERROR_NOTCONN");
346
347 InetSocketAddress peer("10.0.0.1", 1234);
348 err = txSocket->Connect(peer);
349 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
350
351 err = txSocket->GetPeerName(peerAddress);
352 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
353 peer.SetPort(0);
354 NS_TEST_EXPECT_MSG_EQ(peerAddress,
355 peer,
356 "address from socket GetPeerName() should equal the connected address");
357
359}
360
361/**
362 * @ingroup internet-test
363 *
364 * @brief IPv4 RAW Socket TestSuite
365 */
367{
368 public:
370 : TestSuite("ipv4-raw", Type::UNIT)
371 {
372 AddTestCase(new Ipv4RawSocketImplTest, TestCase::Duration::QUICK);
373 }
374};
375
376static Ipv4RawTestSuite g_ipv4rawTestSuite; //!< Static variable for test initialization
IPv4 RAW Socket Test.
void SendData_IpHdr(Ptr< Socket > socket, std::string to)
Send data.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
Ptr< Packet > m_receivedPacket
Received packet (1).
void DoRun() override
Implementation to actually run this TestCase.
Ptr< Packet > m_receivedPacket2
Received packet (2).
void SendData(Ptr< Socket > socket, std::string to)
Send data.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void DoSendData_IpHdr(Ptr< Socket > socket, std::string to)
Send data.
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void ReceivePkt2(Ptr< Socket > socket)
Receive data.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
IPv4 RAW Socket TestSuite.
a polymophic address class
Definition address.h:90
AttributeValue implementation for Boolean.
Definition boolean.h:26
an Inet address class
void SetPort(uint16_t port)
aggregate IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
Packet header for IPv4.
Definition ipv4-header.h:23
void SetDestination(Ipv4Address destination)
void SetPayloadSize(uint16_t size)
void SetTtl(uint8_t ttl)
void SetProtocol(uint8_t num)
void SetSource(Ipv4Address source)
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
API to create 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 GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
Smart pointer class similar to boost::intrusive_ptr.
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...
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
@ ERROR_NOTCONN
Definition socket.h:76
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
static Ipv4RawTestSuite g_ipv4rawTestSuite
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:684