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 
53 using 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 
125 void
127 {
128  m_receivedPacket = packet;
129 }
130 
131 void
133 {
134  m_receivedPacket2 = packet;
135 }
136 
137 void
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");
145  NS_TEST_ASSERT_MSG_EQ(availableData,
147  "Received packet size is not equal to Rx buffer size");
148 }
149 
150 void
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");
158  NS_TEST_ASSERT_MSG_EQ(availableData,
160  "Received packet size is not equal to Rx buffer size");
161 }
162 
163 void
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 
170 void
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);
181  Simulator::Run();
182 }
183 
184 void
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 
202 void
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);
213  Simulator::Run();
214 }
215 
216 void
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 
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();
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 ();
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(),
355  Socket::ERROR_NOTCONN,
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 
369  Simulator::Destroy();
370 }
371 
378 {
379  public:
381  : TestSuite("ipv4-raw", UNIT)
382  {
383  AddTestCase(new Ipv4RawSocketImplTest, TestCase::QUICK);
384  }
385 };
386 
#define max(a, b)
Definition: 80211b.c:42
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
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 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:204
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:861
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:128
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
void ReceivePacket(Ptr< Socket > socket)
#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: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