A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/drop-tail-queue.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 
44 #include <string>
45 #include <limits>
46 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
49 
50 using namespace ns3;
51 
52 static void
54 {
55  //ARP
56  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
57  node->AggregateObject (arp);
58  //IPV4
59  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
60  //Routing for Ipv4
61  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
62  ipv4->SetRoutingProtocol (ipv4Routing);
63  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
64  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
65  node->AggregateObject (ipv4);
66  //ICMP
67  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
68  node->AggregateObject (icmp);
69  // //Ipv4Raw
70  // Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
71  // node->AggregateObject(udp);
72 }
73 
74 
76 {
79  void DoSendData (Ptr<Socket> socket, std::string to);
80  void SendData (Ptr<Socket> socket, std::string to);
81  void DoSendData_IpHdr (Ptr<Socket> socket, std::string to);
82  void SendData_IpHdr (Ptr<Socket> socket, std::string to);
83 
84 public:
85  virtual void DoRun (void);
87 
88  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
89  void ReceivePacket2 (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
90  void ReceivePkt (Ptr<Socket> socket);
91  void ReceivePkt2 (Ptr<Socket> socket);
92 };
93 
94 
96  : TestCase ("IPv4 Raw socket implementation")
97 {
98 }
99 
101 {
102  m_receivedPacket = packet;
103 }
104 
106 {
107  m_receivedPacket2 = packet;
108 }
109 
111 {
112  uint32_t availableData;
113  availableData = socket->GetRxAvailable ();
114  m_receivedPacket = socket->Recv (2, MSG_PEEK);
116  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
117  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
118 }
119 
121 {
122  uint32_t availableData;
123  availableData = socket->GetRxAvailable ();
124  m_receivedPacket2 = socket->Recv (2, MSG_PEEK);
126  m_receivedPacket2 = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
127  NS_ASSERT (availableData == m_receivedPacket2->GetSize ());
128 }
129 
130 void
132 {
133  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
134  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
135  123, to);
136 }
137 
138 void
140 {
141  m_receivedPacket = Create<Packet> ();
142  m_receivedPacket2 = Create<Packet> ();
143  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
144  &Ipv4RawSocketImplTest::DoSendData, this, socket, to);
145  Simulator::Run ();
146 }
147 
148 void
150 {
151  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0);
152  socket->SetAttribute ("IpHeaderInclude", BooleanValue (true));
153  Ptr<Packet> p = Create<Packet> (123);
154  Ipv4Header ipHeader;
155  ipHeader.SetSource (Ipv4Address ("10.0.0.2"));
156  ipHeader.SetDestination (Ipv4Address (to.c_str ()));
157  ipHeader.SetProtocol (0);
158  ipHeader.SetPayloadSize (p->GetSize ());
159  ipHeader.SetTtl (255);
160  p->AddHeader (ipHeader);
161 
162  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo),
163  143, to);
164  socket->SetAttribute ("IpHeaderInclude", BooleanValue (false));
165 }
166 
167 void
169 {
170  m_receivedPacket = Create<Packet> ();
171  m_receivedPacket2 = Create<Packet> ();
172  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
173  &Ipv4RawSocketImplTest::DoSendData_IpHdr, this, socket, to);
174  Simulator::Run ();
175 }
176 
177 void
179 {
180  // Create topology
181 
182  // Receiver Node
183  Ptr<Node> rxNode = CreateObject<Node> ();
184  AddInternetStack (rxNode);
185  Ptr<SimpleNetDevice> rxDev1, rxDev2;
186  { // first interface
187  rxDev1 = CreateObject<SimpleNetDevice> ();
188  rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
189  rxNode->AddDevice (rxDev1);
190  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
191  uint32_t netdev_idx = ipv4->AddInterface (rxDev1);
192  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
193  ipv4->AddAddress (netdev_idx, ipv4Addr);
194  ipv4->SetUp (netdev_idx);
195  }
196 
197  { // second interface
198  rxDev2 = CreateObject<SimpleNetDevice> ();
199  rxDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
200  rxNode->AddDevice (rxDev2);
201  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
202  uint32_t netdev_idx = ipv4->AddInterface (rxDev2);
203  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.1"), Ipv4Mask (0xffff0000U));
204  ipv4->AddAddress (netdev_idx, ipv4Addr);
205  ipv4->SetUp (netdev_idx);
206  }
207 
208  // Sender Node
209  Ptr<Node> txNode = CreateObject<Node> ();
210  AddInternetStack (txNode);
211  Ptr<SimpleNetDevice> txDev1;
212  {
213  txDev1 = CreateObject<SimpleNetDevice> ();
214  txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
215  txNode->AddDevice (txDev1);
216  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
217  uint32_t netdev_idx = ipv4->AddInterface (txDev1);
218  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
219  ipv4->AddAddress (netdev_idx, ipv4Addr);
220  ipv4->SetUp (netdev_idx);
221  }
222  Ptr<SimpleNetDevice> txDev2;
223  {
224  txDev2 = CreateObject<SimpleNetDevice> ();
225  txDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
226  txNode->AddDevice (txDev2);
227  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
228  uint32_t netdev_idx = ipv4->AddInterface (txDev2);
229  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.1.2"), Ipv4Mask (0xffff0000U));
230  ipv4->AddAddress (netdev_idx, ipv4Addr);
231  ipv4->SetUp (netdev_idx);
232  }
233 
234  // link the two nodes
235  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
236  rxDev1->SetChannel (channel1);
237  txDev1->SetChannel (channel1);
238 
239  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
240  rxDev2->SetChannel (channel2);
241  txDev2->SetChannel (channel2);
242 
243 
244  // Create the IPv4 Raw sockets
245  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> ();
246  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
247  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
248  rxSocket->SetRecvCallback (MakeCallback (&Ipv4RawSocketImplTest::ReceivePkt, this));
249 
250  Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket ();
252  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("10.0.1.1"), 0)), 0, "trivial");
253 
254  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> ();
255  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
256 
257  // ------ Now the tests ------------
258 
259  // Unicast test
260  SendData (txSocket, "10.0.0.1");
261  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 10.0.0.1");
262  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
263 
266 
267  // Unicast w/ header test
268  SendData_IpHdr (txSocket, "10.0.0.1");
269  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1");
270  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 0, "second interface should not receive it");
271 
274 
275 #if 0
276  // Simple broadcast test
277 
278  SendData (txSocket, "255.255.255.255");
279  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
280  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");
281 
284 #endif
285 
286  // Simple Link-local multicast test
287 
288  txSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 0));
289  SendData (txSocket, "224.0.0.9");
290  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 224.0.0.9");
291  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");
292 
295 
296 #if 0
297  // Broadcast test with multiple receiving sockets
298 
299  // When receiving broadcast packets, all sockets sockets bound to
300  // the address/port should receive a copy of the same packet -- if
301  // the socket address matches.
302  rxSocket2->Dispose ();
303  rxSocket2 = rxSocketFactory->CreateSocket ();
305  NS_TEST_EXPECT_MSG_EQ (rxSocket2->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial");
306 
307  SendData (txSocket, "255.255.255.255");
308  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv: 255.255.255.255");
309  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket2->GetSize (), 143, "recv: 255.255.255.255");
310 #endif
311 
312  m_receivedPacket = 0;
313  m_receivedPacket2 = 0;
314 
315  Simulator::Destroy ();
316 }
317 //-----------------------------------------------------------------------------
319 {
320 public:
321  Ipv4RawTestSuite () : TestSuite ("ipv4-raw", UNIT)
322  {
323  AddTestCase (new Ipv4RawSocketImplTest, TestCase::QUICK);
324  }
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:285
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)
Hold a bool native type.
Definition: boolean.h:38
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
void DoSendData(Ptr< Socket > socket, std::string to)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
A suite of tests to run.
Definition: test.h:1025
virtual void DoRun(void)
Implementation to actually run this TestCase.
void ReceivePacket(Ptr< Socket > socket)
#define NS_ASSERT(condition)
Definition: assert.h:64
uint32_t GetSize(void) const
Definition: packet.h:650
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:849
API to create RAW socket instances.
a polymophic address class
Definition: address.h:86
Packet header for IPv4.
Definition: ipv4-header.h:31
void ReceivePkt2(Ptr< Socket > socket)
virtual void SetUp(uint32_t interface)=0
#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:244
void SendData(Ptr< Socket > socket, std::string to)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
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
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Ptr< Packet > m_receivedPacket
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
Ipv4RawTestSuite g_ipv4rawTestSuite
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
void SendData_IpHdr(Ptr< Socket > socket, std::string to)
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
uint32_t GetId(void) const
Definition: node.cc:104
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.
virtual void SetAddress(Address address)
Set the address of this interface.
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:361
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
static void AddInternetStack(Ptr< Node > node)
This test suite implements a Unit Test.
Definition: test.h:1035
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
Ptr< T > GetObject(void) const
Definition: object.h:361
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:205
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.