A Discrete-Event Network Simulator
API
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
62{
65
71 void DoSendData(Ptr<Socket> socket, std::string to);
77 void SendData(Ptr<Socket> socket, std::string to);
83 void DoSendData_IpHdr(Ptr<Socket> socket, std::string to);
89 void SendData_IpHdr(Ptr<Socket> socket, std::string to);
90
91 public:
92 void DoRun() override;
94
101 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
108 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
113 void ReceivePkt(Ptr<Socket> socket);
118 void ReceivePkt2(Ptr<Socket> socket);
119};
120
122 : TestCase("IPv4 Raw socket implementation")
123{
124}
125
126void
128{
129 m_receivedPacket = packet;
130}
131
132void
134{
135 m_receivedPacket2 = packet;
136}
137
138void
140{
141 uint32_t availableData;
142 availableData = socket->GetRxAvailable();
143 m_receivedPacket = socket->Recv(2, MSG_PEEK);
144 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket->GetSize(), 2, "ReceivedPacket size is not equal to 2");
146 NS_TEST_ASSERT_MSG_EQ(availableData,
148 "Received packet size is not equal to Rx buffer size");
149}
150
151void
153{
154 uint32_t availableData;
155 availableData = socket->GetRxAvailable();
156 m_receivedPacket2 = socket->Recv(2, MSG_PEEK);
157 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket2->GetSize(), 2, "ReceivedPacket size is not equal to 2");
159 NS_TEST_ASSERT_MSG_EQ(availableData,
161 "Received packet size is not equal to Rx buffer size");
162}
163
164void
166{
167 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
168 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, to);
169}
170
171void
173{
174 m_receivedPacket = Create<Packet>();
175 m_receivedPacket2 = Create<Packet>();
176 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
177 Seconds(0),
179 this,
180 socket,
181 to);
182 Simulator::Run();
183}
184
185void
187{
188 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 0);
189 socket->SetAttribute("IpHeaderInclude", BooleanValue(true));
190 Ptr<Packet> p = Create<Packet>(123);
191 Ipv4Header ipHeader;
192 ipHeader.SetSource(Ipv4Address("10.0.0.2"));
193 ipHeader.SetDestination(Ipv4Address(to.c_str()));
194 ipHeader.SetProtocol(0);
195 ipHeader.SetPayloadSize(p->GetSize());
196 ipHeader.SetTtl(255);
197 p->AddHeader(ipHeader);
198
199 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(p, 0, realTo), 143, to);
200 socket->SetAttribute("IpHeaderInclude", BooleanValue(false));
201}
202
203void
205{
206 m_receivedPacket = Create<Packet>();
207 m_receivedPacket2 = Create<Packet>();
208 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
209 Seconds(0),
211 this,
212 socket,
213 to);
214 Simulator::Run();
215}
216
217void
219{
220 // Create topology
221
222 // Receiver Node
223 Ptr<Node> rxNode = CreateObject<Node>();
224 // Sender Node
225 Ptr<Node> txNode = CreateObject<Node>();
226
227 NodeContainer nodes(rxNode, txNode);
228
229 SimpleNetDeviceHelper helperChannel1;
230 helperChannel1.SetNetDevicePointToPointMode(true);
231 NetDeviceContainer net1 = helperChannel1.Install(nodes);
232
233 SimpleNetDeviceHelper helperChannel2;
234 helperChannel2.SetNetDevicePointToPointMode(true);
235 NetDeviceContainer net2 = helperChannel2.Install(nodes);
236
237 InternetStackHelper internet;
238 internet.Install(nodes);
239
240 Ptr<Ipv4> ipv4;
241 uint32_t netdev_idx;
242 Ipv4InterfaceAddress ipv4Addr;
243
244 // Receiver Node
245 ipv4 = rxNode->GetObject<Ipv4>();
246 netdev_idx = ipv4->AddInterface(net1.Get(0));
247 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
248 ipv4->AddAddress(netdev_idx, ipv4Addr);
249 ipv4->SetUp(netdev_idx);
250
251 netdev_idx = ipv4->AddInterface(net2.Get(0));
252 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.1"), Ipv4Mask(0xffff0000U));
253 ipv4->AddAddress(netdev_idx, ipv4Addr);
254 ipv4->SetUp(netdev_idx);
255
256 // Sender Node
257 ipv4 = txNode->GetObject<Ipv4>();
258 netdev_idx = ipv4->AddInterface(net1.Get(1));
259 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
260 ipv4->AddAddress(netdev_idx, ipv4Addr);
261 ipv4->SetUp(netdev_idx);
262
263 netdev_idx = ipv4->AddInterface(net2.Get(1));
264 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.2"), Ipv4Mask(0xffff0000U));
265 ipv4->AddAddress(netdev_idx, ipv4Addr);
266 ipv4->SetUp(netdev_idx);
267
268 // Create the IPv4 Raw sockets
269 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory>();
270 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
271 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 0)),
272 0,
273 "trivial");
274 rxSocket->SetRecvCallback(MakeCallback(&Ipv4RawSocketImplTest::ReceivePkt, this));
275
276 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
278 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(InetSocketAddress(Ipv4Address("10.0.1.1"), 0)),
279 0,
280 "trivial");
281
282 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory>();
283 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
284
285 // ------ Now the tests ------------
286
287 // Unicast test
288 SendData(txSocket, "10.0.0.1");
289 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv: 10.0.0.1");
291 0,
292 "second interface should not receive it");
293
296
297 // Unicast w/ header test
298 SendData_IpHdr(txSocket, "10.0.0.1");
299 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv(hdrincl): 10.0.0.1");
301 0,
302 "second interface should not receive it");
303
306
307#if 0
308 // Simple broadcast test
309
310 SendData (txSocket, "255.255.255.255");
311 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
312 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");
313
316#endif
317
318 // Simple Link-local multicast test
319
320 txSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.2"), 0));
321 SendData(txSocket, "224.0.0.9");
322 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 143, "recv: 224.0.0.9");
324 0,
325 "second socket should not receive it (it is bound specifically to the "
326 "second interface's address");
327
330
331#if 0
332 // Broadcast test with multiple receiving sockets
333
334 // When receiving broadcast packets, all sockets sockets bound to
335 // the address/port should receive a copy of the same packet -- if
336 // the socket address matches.
337 rxSocket2->Dispose ();
338 rxSocket2 = rxSocketFactory->CreateSocket ();
340 NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
341
342 SendData (txSocket, "255.255.255.255");
343 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
344 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
345#endif
346
347 m_receivedPacket = nullptr;
348 m_receivedPacket2 = nullptr;
349
350 // Simple getpeername tests
351
352 Address peerAddress;
353 int err = txSocket->GetPeerName(peerAddress);
354 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
355 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
356 Socket::ERROR_NOTCONN,
357 "socket error code should be ERROR_NOTCONN");
358
359 InetSocketAddress peer("10.0.0.1", 1234);
360 err = txSocket->Connect(peer);
361 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
362
363 err = txSocket->GetPeerName(peerAddress);
364 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
365 peer.SetPort(0);
366 NS_TEST_EXPECT_MSG_EQ(peerAddress,
367 peer,
368 "address from socket GetPeerName() should equal the connected address");
369
370 Simulator::Destroy();
371}
372
380{
381 public:
383 : TestSuite("ipv4-raw", UNIT)
384 {
385 AddTestCase(new Ipv4RawSocketImplTest, TestCase::QUICK);
386 }
387};
388
#define max(a, b)
Definition: 80211b.c:43
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:92
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.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
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:79
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
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 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
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
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 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
#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 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:691