A Discrete-Event Network Simulator
API
ipv4-raw-test.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2010 Hajime Tazaki
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
19 */
24#include "ns3/test.h"
25#include "ns3/socket-factory.h"
26#include "ns3/ipv4-raw-socket-factory.h"
27#include "ns3/simulator.h"
28#include "ns3/simple-channel.h"
29#include "ns3/simple-net-device.h"
30#include "ns3/simple-net-device-helper.h"
31#include "ns3/socket.h"
32
33#include "ns3/log.h"
34#include "ns3/node.h"
35#include "ns3/inet-socket-address.h"
36#include "ns3/boolean.h"
37
38#include "ns3/arp-l3-protocol.h"
39#include "ns3/ipv4-l3-protocol.h"
40#include "ns3/icmpv4-l4-protocol.h"
41#include "ns3/ipv4-list-routing.h"
42#include "ns3/ipv4-static-routing.h"
43#include "ns3/internet-stack-helper.h"
44
45#include <string>
46#include <limits>
47#include <netinet/in.h>
48#include <sys/socket.h>
49#include <sys/types.h>
50
51using namespace ns3;
52
53
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
90public:
91 virtual void DoRun (void);
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
120
122 : TestCase ("IPv4 Raw socket implementation")
123{
124}
125
127{
128 m_receivedPacket = packet;
129}
130
132{
133 m_receivedPacket2 = packet;
134}
135
137{
138 uint32_t availableData;
139 availableData = socket->GetRxAvailable ();
140 m_receivedPacket = socket->Recv (2, MSG_PEEK);
141 NS_TEST_ASSERT_MSG_EQ (m_receivedPacket->GetSize (), 2, "ReceivedPacket size is not equal to 2");
143 NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Received packet size is not equal to Rx buffer size");
144}
145
147{
148 uint32_t availableData;
149 availableData = socket->GetRxAvailable ();
150 m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
151 NS_TEST_ASSERT_MSG_EQ (m_receivedPacket2->GetSize (), 2, "ReceivedPacket size is not equal to 2");
153 NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket2->GetSize (), "Received packet size is not equal to Rx buffer size");
154}
155
156void
158{
159 Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
160 NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
161 123, to);
162}
163
164void
166{
167 m_receivedPacket = Create<Packet> ();
168 m_receivedPacket2 = Create<Packet> ();
169 Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
170 &Ipv4RawSocketImplTest::DoSendData, this, socket, to);
171 Simulator::Run ();
172}
173
174void
176{
177 Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
178 socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
179 Ptr<Packet> p = Create<Packet> (123);
180 Ipv4Header ipHeader;
181 ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
182 ipHeader.SetDestination (Ipv4Address (to.c_str ()));
183 ipHeader.SetProtocol (0);
184 ipHeader.SetPayloadSize (p->GetSize ());
185 ipHeader.SetTtl (255);
186 p->AddHeader (ipHeader);
187
188 NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
189 143, to);
190 socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
191}
192
193void
195{
196 m_receivedPacket = Create<Packet> ();
197 m_receivedPacket2 = Create<Packet> ();
198 Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
199 &Ipv4RawSocketImplTest::DoSendData_IpHdr, this, socket, to);
200 Simulator::Run ();
201}
202
203void
205{
206 // Create topology
207
208 // Receiver Node
209 Ptr<Node> rxNode = CreateObject<Node> ();
210 // Sender Node
211 Ptr<Node> txNode = CreateObject<Node> ();
212
213 NodeContainer nodes (rxNode, txNode);
214
215 SimpleNetDeviceHelper helperChannel1;
216 helperChannel1.SetNetDevicePointToPointMode (true);
217 NetDeviceContainer net1 = helperChannel1.Install (nodes);
218
219 SimpleNetDeviceHelper helperChannel2;
220 helperChannel2.SetNetDevicePointToPointMode (true);
221 NetDeviceContainer net2 = helperChannel2.Install (nodes);
222
223 InternetStackHelper internet;
224 internet.Install (nodes);
225
226 Ptr<Ipv4> ipv4;
227 uint32_t netdev_idx;
228 Ipv4InterfaceAddress ipv4Addr;
229
230 // Receiver Node
231 ipv4 = rxNode->GetObject<Ipv4> ();
232 netdev_idx = ipv4->AddInterface (net1.Get (0));
233 ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
234 ipv4->AddAddress (netdev_idx, ipv4Addr);
235 ipv4->SetUp (netdev_idx);
236
237 netdev_idx = ipv4->AddInterface (net2.Get (0));
238 ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.1"), Ipv4Mask (0xffff0000U));
239 ipv4->AddAddress (netdev_idx, ipv4Addr);
240 ipv4->SetUp (netdev_idx);
241
242 // Sender Node
243 ipv4 = txNode->GetObject<Ipv4> ();
244 netdev_idx = ipv4->AddInterface (net1.Get (1));
245 ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
246 ipv4->AddAddress (netdev_idx, ipv4Addr);
247 ipv4->SetUp (netdev_idx);
248
249 netdev_idx = ipv4->AddInterface (net2.Get (1));
250 ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.2"), Ipv4Mask (0xffff0000U));
251 ipv4->AddAddress (netdev_idx, ipv4Addr);
252 ipv4->SetUp (netdev_idx);
253
254 // Create the IPv4 Raw sockets
255 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
256 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
257 NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
258 rxSocket->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt, this));
259
260 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
262 NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.1.1"), 0)), 0, "trivial");
263
264 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
265 Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
266
267 // ------ Now the tests ------------
268
269 // Unicast test
270 SendData (txSocket, "10.0.0.1");
271 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 10.0.0.1");
272 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
273
276
277 // Unicast w/ header test
278 SendData_IpHdr (txSocket, "10.0.0.1");
279 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
280 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
281
284
285#if 0
286 // Simple broadcast test
287
288 SendData (txSocket, "255.255.255.255");
289 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
290 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");
291
294#endif
295
296 // Simple Link-local multicast test
297
298 txSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 0));
299 SendData (txSocket, "224.0.0.9");
300 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 224.0.0.9");
301 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");
302
305
306#if 0
307 // Broadcast test with multiple receiving sockets
308
309 // When receiving broadcast packets, all sockets sockets bound to
310 // the address/port should receive a copy of the same packet -- if
311 // the socket address matches.
312 rxSocket2->Dispose ();
313 rxSocket2 = rxSocketFactory->CreateSocket ();
315 NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
316
317 SendData (txSocket, "255.255.255.255");
318 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
319 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
320#endif
321
324
325 // Simple getpeername tests
326
327 Address peerAddress;
328 int err = txSocket->GetPeerName (peerAddress);
329 NS_TEST_EXPECT_MSG_EQ (err, -1, "socket GetPeerName() should fail when socket is not connected");
330 NS_TEST_EXPECT_MSG_EQ (txSocket->GetErrno (), Socket::ERROR_NOTCONN, "socket error code should be ERROR_NOTCONN");
331
332 InetSocketAddress peer ("10.0.0.1", 1234);
333 err = txSocket->Connect (peer);
334 NS_TEST_EXPECT_MSG_EQ (err, 0, "socket Connect() should succeed");
335
336 err = txSocket->GetPeerName (peerAddress);
337 NS_TEST_EXPECT_MSG_EQ (err, 0, "socket GetPeerName() should succeed when socket is connected");
338 peer.SetPort (0);
339 NS_TEST_EXPECT_MSG_EQ (peerAddress, peer, "address from socket GetPeerName() should equal the connected address");
340
341 Simulator::Destroy ();
342}
343
344
352{
353public:
354 Ipv4RawTestSuite () : TestSuite ("ipv4-raw", UNIT)
355 {
356 AddTestCase (new Ipv4RawSocketImplTest, TestCase::QUICK);
357 }
358};
359
#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).
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
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:91
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:41
Packet header for IPv4.
Definition: ipv4-header.h:34
void SetDestination(Ipv4Address destination)
Definition: ipv4-header.cc:298
void SetPayloadSize(uint16_t size)
Definition: ipv4-header.cc:56
void SetTtl(uint8_t ttl)
Definition: ipv4-header.cc:259
void SetProtocol(uint8_t num)
Definition: ipv4-header.cc:278
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:285
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
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(void) const
Definition: node.cc:109
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
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< Node > GetNode(void) const =0
Return the node this socket is associated with.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
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 int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
#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:141
#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:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static Ipv4RawTestSuite g_ipv4rawTestSuite
Static variable for test initialization.
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648