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 * 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/arp-l3-protocol.h"
24#include "ns3/boolean.h"
25#include "ns3/icmpv4-l4-protocol.h"
26#include "ns3/inet-socket-address.h"
27#include "ns3/internet-stack-helper.h"
28#include "ns3/ipv4-l3-protocol.h"
29#include "ns3/ipv4-list-routing.h"
30#include "ns3/ipv4-raw-socket-factory.h"
31#include "ns3/ipv4-static-routing.h"
32#include "ns3/log.h"
33#include "ns3/node.h"
34#include "ns3/simple-channel.h"
35#include "ns3/simple-net-device-helper.h"
36#include "ns3/simple-net-device.h"
37#include "ns3/simulator.h"
38#include "ns3/socket-factory.h"
39#include "ns3/socket.h"
40#include "ns3/test.h"
41
42#ifdef __WIN32__
43#include "ns3/win32-internet.h"
44#else
45#include <netinet/in.h>
46#include <sys/socket.h>
47#endif
48
49#include <limits>
50#include <string>
51#include <sys/types.h>
52
53using namespace ns3;
54
61{
64
70 void DoSendData(Ptr<Socket> socket, std::string to);
76 void SendData(Ptr<Socket> socket, std::string to);
82 void DoSendData_IpHdr(Ptr<Socket> socket, std::string to);
88 void SendData_IpHdr(Ptr<Socket> socket, std::string to);
89
90 public:
91 void DoRun() override;
93
100 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
107 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
112 void ReceivePkt(Ptr<Socket> socket);
117 void ReceivePkt2(Ptr<Socket> socket);
118};
119
121 : TestCase("IPv4 Raw socket implementation")
122{
123}
124
125void
127{
128 m_receivedPacket = packet;
129}
130
131void
133{
134 m_receivedPacket2 = packet;
135}
136
137void
139{
140 uint32_t availableData;
141 availableData = socket->GetRxAvailable();
142 m_receivedPacket = socket->Recv(2, MSG_PEEK);
143 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket->GetSize(), 2, "ReceivedPacket size is not equal to 2");
144 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
145 NS_TEST_ASSERT_MSG_EQ(availableData,
147 "Received packet size is not equal to Rx buffer size");
148}
149
150void
152{
153 uint32_t availableData;
154 availableData = socket->GetRxAvailable();
155 m_receivedPacket2 = socket->Recv(2, MSG_PEEK);
156 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket2->GetSize(), 2, "ReceivedPacket size is not equal to 2");
157 m_receivedPacket2 = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
158 NS_TEST_ASSERT_MSG_EQ(availableData,
160 "Received packet size is not equal to Rx buffer size");
161}
162
163void
165{
166 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
167 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, to);
168}
169
170void
172{
173 m_receivedPacket = Create<Packet>();
174 m_receivedPacket2 = Create<Packet>();
175 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
176 Seconds(0),
178 this,
179 socket,
180 to);
182}
183
184void
186{
187 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
188 socket->SetAttribute("IpHeaderInclude", BooleanValue(true));
189 Ptr<Packet> p = Create<Packet>(123);
190 Ipv4Header ipHeader;
191 ipHeader.SetSource(Ipv4Address("10.0.0.2"));
192 ipHeader.SetDestination(Ipv4Address(to.c_str()));
193 ipHeader.SetProtocol(0);
194 ipHeader.SetPayloadSize(p->GetSize());
195 ipHeader.SetTtl(255);
196 p->AddHeader(ipHeader);
197
198 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(p, 0, realTo), 143, to);
199 socket->SetAttribute("IpHeaderInclude", BooleanValue(false));
200}
201
202void
204{
205 m_receivedPacket = Create<Packet>();
206 m_receivedPacket2 = Create<Packet>();
207 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
208 Seconds(0),
210 this,
211 socket,
212 to);
214}
215
216void
218{
219 // Create topology
220
221 // Receiver Node
222 Ptr<Node> rxNode = CreateObject<Node>();
223 // Sender Node
224 Ptr<Node> txNode = CreateObject<Node>();
225
226 NodeContainer nodes(rxNode, txNode);
227
228 SimpleNetDeviceHelper helperChannel1;
229 helperChannel1.SetNetDevicePointToPointMode(true);
230 NetDeviceContainer net1 = helperChannel1.Install(nodes);
231
232 SimpleNetDeviceHelper helperChannel2;
233 helperChannel2.SetNetDevicePointToPointMode(true);
234 NetDeviceContainer net2 = helperChannel2.Install(nodes);
235
236 InternetStackHelper internet;
237 internet.Install(nodes);
238
239 Ptr<Ipv4> ipv4;
240 uint32_t netdev_idx;
241 Ipv4InterfaceAddress ipv4Addr;
242
243 // Receiver Node
244 ipv4 = rxNode->GetObject<Ipv4>();
245 netdev_idx = ipv4->AddInterface(net1.Get(0));
246 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
247 ipv4->AddAddress(netdev_idx, ipv4Addr);
248 ipv4->SetUp(netdev_idx);
249
250 netdev_idx = ipv4->AddInterface(net2.Get(0));
251 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.1"), Ipv4Mask(0xffff0000U));
252 ipv4->AddAddress(netdev_idx, ipv4Addr);
253 ipv4->SetUp(netdev_idx);
254
255 // Sender Node
256 ipv4 = txNode->GetObject<Ipv4>();
257 netdev_idx = ipv4->AddInterface(net1.Get(1));
258 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
259 ipv4->AddAddress(netdev_idx, ipv4Addr);
260 ipv4->SetUp(netdev_idx);
261
262 netdev_idx = ipv4->AddInterface(net2.Get(1));
263 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.2"), Ipv4Mask(0xffff0000U));
264 ipv4->AddAddress(netdev_idx, ipv4Addr);
265 ipv4->SetUp(netdev_idx);
266
267 // Create the IPv4 Raw sockets
268 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory>();
269 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
270 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 0)),
271 0,
272 "trivial");
273 rxSocket->SetRecvCallback(MakeCallback(&Ipv4RawSocketImplTest::ReceivePkt, this));
274
275 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
276 rxSocket2->SetRecvCallback(MakeCallback(&Ipv4RawSocketImplTest::ReceivePkt2, this));
277 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(InetSocketAddress(Ipv4Address("10.0.1.1"), 0)),
278 0,
279 "trivial");
280
281 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory>();
282 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
283
284 // ------ Now the tests ------------
285
286 // Unicast test
287 SendData(txSocket, "10.0.0.1");
288 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv: 10.0.0.1");
290 0,
291 "second interface should not receive it");
292
295
296 // Unicast w/ header test
297 SendData_IpHdr(txSocket, "10.0.0.1");
298 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
300 0,
301 "second interface should not receive it");
302
305
306#if 0
307 // Simple broadcast test
308
309 SendData (txSocket, "255.255.255.255");
310 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
311 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");
312
315#endif
316
317 // Simple Link-local multicast test
318
319 txSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.2"), 0));
320 SendData(txSocket, "224.0.0.9");
321 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv: 224.0.0.9");
323 0,
324 "second socket should not receive it (it is bound specifically to the "
325 "second interface's address");
326
329
330#if 0
331 // Broadcast test with multiple receiving sockets
332
333 // When receiving broadcast packets, all sockets sockets bound to
334 // the address/port should receive a copy of the same packet -- if
335 // the socket address matches.
336 rxSocket2->Dispose ();
337 rxSocket2 = rxSocketFactory->CreateSocket ();
338 rxSocket2->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt2, this));
339 NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
340
341 SendData (txSocket, "255.255.255.255");
342 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
343 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
344#endif
345
346 m_receivedPacket = nullptr;
347 m_receivedPacket2 = nullptr;
348
349 // Simple getpeername tests
350
351 Address peerAddress;
352 int err = txSocket->GetPeerName(peerAddress);
353 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
354 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
356 "socket error code should be ERROR_NOTCONN");
357
358 InetSocketAddress peer("10.0.0.1", 1234);
359 err = txSocket->Connect(peer);
360 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
361
362 err = txSocket->GetPeerName(peerAddress);
363 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
364 peer.SetPort(0);
365 NS_TEST_EXPECT_MSG_EQ(peerAddress,
366 peer,
367 "address from socket GetPeerName() should equal the connected address");
368
370}
371
378{
379 public:
381 : TestSuite("ipv4-raw", Type::UNIT)
382 {
383 AddTestCase(new Ipv4RawSocketImplTest, TestCase::Duration::QUICK);
384 }
385};
386
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:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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.
Definition: ipv4-address.h:42
Packet header for IPv4.
Definition: ipv4-header.h:34
void SetDestination(Ipv4Address destination)
Definition: ipv4-header.cc:309
void SetPayloadSize(uint16_t size)
Definition: ipv4-header.cc:57
void SetTtl(uint8_t ttl)
Definition: ipv4-header.cc:267
void SetProtocol(uint8_t num)
Definition: ipv4-header.cc:288
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:295
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
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:861
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
@ ERROR_NOTCONN
Definition: socket.h:87
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
#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:145
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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:704