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/drop-tail-queue.h"
32 #include "ns3/socket.h"
33 
34 #include "ns3/log.h"
35 #include "ns3/node.h"
36 #include "ns3/inet-socket-address.h"
37 #include "ns3/boolean.h"
38 
39 #include "ns3/arp-l3-protocol.h"
40 #include "ns3/ipv4-l3-protocol.h"
41 #include "ns3/icmpv4-l4-protocol.h"
42 #include "ns3/ipv4-list-routing.h"
43 #include "ns3/ipv4-static-routing.h"
44 #include "ns3/internet-stack-helper.h"
45 
46 #include <string>
47 #include <limits>
48 #include <netinet/in.h>
49 #include <sys/socket.h>
50 #include <sys/types.h>
51 
52 using namespace ns3;
53 
54 
56 {
59  void DoSendData (Ptr<Socket> socket, std::string to);
60  void SendData (Ptr<Socket> socket, std::string to);
61  void DoSendData_IpHdr (Ptr<Socket> socket, std::string to);
62  void SendData_IpHdr (Ptr<Socket> socket, std::string to);
63 
64 public:
65  virtual void DoRun (void);
67 
68  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
69  void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
70  void ReceivePkt (Ptr<Socket> socket);
71  void ReceivePkt2 (Ptr<Socket> socket);
72 };
73 
74 
76  : TestCase ("IPv4 Raw socket implementation")
77 {
78 }
79 
81 {
82  m_receivedPacket = packet;
83 }
84 
86 {
87  m_receivedPacket2 = packet;
88 }
89 
91 {
92  uint32_t availableData;
93  availableData = socket->GetRxAvailable ();
94  m_receivedPacket = socket->Recv (2, MSG_PEEK);
97  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
98 }
99 
101 {
102  uint32_t availableData;
103  availableData = socket->GetRxAvailable ();
104  m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
107  NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
108 }
109 
110 void
112 {
113  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
114  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
115  123, to);
116 }
117 
118 void
120 {
121  m_receivedPacket = Create<Packet> ();
122  m_receivedPacket2 = Create<Packet> ();
123  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
124  &Ipv4RawSocketImplTest::DoSendData, this, socket, to);
125  Simulator::Run ();
126 }
127 
128 void
130 {
131  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
132  socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
133  Ptr<Packet> p = Create<Packet> (123);
134  Ipv4Header ipHeader;
135  ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
136  ipHeader.SetDestination (Ipv4Address (to.c_str ()));
137  ipHeader.SetProtocol (0);
138  ipHeader.SetPayloadSize (p->GetSize ());
139  ipHeader.SetTtl (255);
140  p->AddHeader (ipHeader);
141 
142  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
143  143, to);
144  socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
145 }
146 
147 void
149 {
150  m_receivedPacket = Create<Packet> ();
151  m_receivedPacket2 = Create<Packet> ();
152  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
153  &Ipv4RawSocketImplTest::DoSendData_IpHdr, this, socket, to);
154  Simulator::Run ();
155 }
156 
157 void
159 {
160  // Create topology
161 
162  // Receiver Node
163  Ptr<Node> rxNode = CreateObject<Node> ();
164  // Sender Node
165  Ptr<Node> txNode = CreateObject<Node> ();
166 
167  NodeContainer nodes (rxNode, txNode);
168 
169  SimpleNetDeviceHelper helperChannel1;
170  helperChannel1.SetNetDevicePointToPointMode (true);
171  NetDeviceContainer net1 = helperChannel1.Install (nodes);
172 
173  SimpleNetDeviceHelper helperChannel2;
174  helperChannel2.SetNetDevicePointToPointMode (true);
175  NetDeviceContainer net2 = helperChannel2.Install (nodes);
176 
177  InternetStackHelper internet;
178  internet.Install (nodes);
179 
180  Ptr<Ipv4> ipv4;
181  uint32_t netdev_idx;
182  Ipv4InterfaceAddress ipv4Addr;
183 
184  // Receiver Node
185  ipv4 = rxNode->GetObject<Ipv4> ();
186  netdev_idx = ipv4->AddInterface (net1.Get (0));
187  ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
188  ipv4->AddAddress (netdev_idx, ipv4Addr);
189  ipv4->SetUp (netdev_idx);
190 
191  netdev_idx = ipv4->AddInterface (net2.Get (0));
192  ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.1"), Ipv4Mask (0xffff0000U));
193  ipv4->AddAddress (netdev_idx, ipv4Addr);
194  ipv4->SetUp (netdev_idx);
195 
196  // Sender Node
197  ipv4 = txNode->GetObject<Ipv4> ();
198  netdev_idx = ipv4->AddInterface (net1.Get (1));
199  ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
200  ipv4->AddAddress (netdev_idx, ipv4Addr);
201  ipv4->SetUp (netdev_idx);
202 
203  netdev_idx = ipv4->AddInterface (net2.Get (1));
204  ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.2"), Ipv4Mask (0xffff0000U));
205  ipv4->AddAddress (netdev_idx, ipv4Addr);
206  ipv4->SetUp (netdev_idx);
207 
208  // Create the IPv4 Raw sockets
209  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
210  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
211  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
212  rxSocket->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt, this));
213 
214  Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
216  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.1.1"), 0)), 0, "trivial");
217 
218  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
219  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
220 
221  // ------ Now the tests ------------
222 
223  // Unicast test
224  SendData (txSocket, "10.0.0.1");
225  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 10.0.0.1");
226  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
227 
230 
231  // Unicast w/ header test
232  SendData_IpHdr (txSocket, "10.0.0.1");
233  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
234  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
235 
238 
239 #if 0
240  // Simple broadcast test
241 
242  SendData (txSocket, "255.255.255.255");
243  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
244  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");
245 
248 #endif
249 
250  // Simple Link-local multicast test
251 
252  txSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 0));
253  SendData (txSocket, "224.0.0.9");
254  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 224.0.0.9");
255  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");
256 
259 
260 #if 0
261  // Broadcast test with multiple receiving sockets
262 
263  // When receiving broadcast packets, all sockets sockets bound to
264  // the address/port should receive a copy of the same packet -- if
265  // the socket address matches.
266  rxSocket2->Dispose ();
267  rxSocket2 = rxSocketFactory->CreateSocket ();
269  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
270 
271  SendData (txSocket, "255.255.255.255");
272  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
273  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
274 #endif
275 
276  m_receivedPacket = 0;
277  m_receivedPacket2 = 0;
278 
279  // Simple getpeername tests
280 
281  Address peerAddress;
282  int err = txSocket->GetPeerName (peerAddress);
283  NS_TEST_EXPECT_MSG_EQ (err, -1, "socket GetPeerName() should fail when socket is not connected");
284  NS_TEST_EXPECT_MSG_EQ (txSocket->GetErrno (), Socket::ERROR_NOTCONN, "socket error code should be ERROR_NOTCONN");
285 
286  InetSocketAddress peer ("10.0.0.1", 1234);
287  err = txSocket->Connect (peer);
288  NS_TEST_EXPECT_MSG_EQ (err, 0, "socket Connect() should succeed");
289 
290  err = txSocket->GetPeerName (peerAddress);
291  NS_TEST_EXPECT_MSG_EQ (err, 0, "socket GetPeerName() should succeed when socket is connected");
292  peer.SetPort (0);
293  NS_TEST_EXPECT_MSG_EQ (peerAddress, peer, "address from socket GetPeerName() should equal the connected address");
294 
295  Simulator::Destroy ();
296 }
297 //-----------------------------------------------------------------------------
299 {
300 public:
301  Ipv4RawTestSuite () : TestSuite ("ipv4-raw", UNIT)
302  {
303  AddTestCase (new Ipv4RawSocketImplTest, TestCase::QUICK);
304  }
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:285
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
void ReceivePkt(Ptr< Socket > socket)
void DoSendData_IpHdr(Ptr< Socket > socket, std::string to)
AttributeValue implementation for Boolean.
Definition: boolean.h:34
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void DoSendData(Ptr< Socket > socket, std::string to)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
A suite of tests to run.
Definition: test.h:1333
virtual void DoRun(void)
Implementation to actually run this TestCase.
void ReceivePacket(Ptr< Socket > socket)
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
aggregate IP/TCP/UDP functionality to existing Nodes.
#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:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
API to create RAW socket instances.
a polymophic address class
Definition: address.h:90
tuple nodes
Definition: first.py:25
Packet header for IPv4.
Definition: ipv4-header.h:33
#define max(a, b)
Definition: 80211b.c:45
void ReceivePkt2(Ptr< Socket > socket)
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
virtual void SetUp(uint32_t interface)=0
void SendData(Ptr< Socket > socket, std::string to)
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Ptr< Packet > m_receivedPacket
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Ipv4RawTestSuite g_ipv4rawTestSuite
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
void SendData_IpHdr(Ptr< Socket > socket, std::string to)
uint32_t GetId(void) const
Definition: node.cc:107
a class to store IPv4 address information on an interface
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:349
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
Ptr< Packet > m_receivedPacket2
build a set of SimpleNetDevice objects
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:191
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.