A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv4-forwarding-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Universita' di Firenze
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/socket-factory.h"
23 #include "ns3/udp-socket-factory.h"
24 #include "ns3/simulator.h"
25 #include "ns3/simple-channel.h"
26 #include "ns3/simple-net-device.h"
27 #include "ns3/drop-tail-queue.h"
28 #include "ns3/socket.h"
29 #include "ns3/boolean.h"
30 
31 #include "ns3/log.h"
32 #include "ns3/node.h"
33 #include "ns3/inet-socket-address.h"
34 
35 #include "ns3/arp-l3-protocol.h"
36 #include "ns3/ipv4-l3-protocol.h"
37 #include "ns3/icmpv4-l4-protocol.h"
38 #include "ns3/udp-l4-protocol.h"
39 #include "ns3/ipv4-static-routing.h"
40 
41 #include <string>
42 #include <limits>
43 
44 using namespace ns3;
45 
46 static void
48 {
49  //ARP
50  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
51  node->AggregateObject (arp);
52  //IPV4
53  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
54  //Routing for Ipv4
55  Ptr<Ipv4StaticRouting> ipv4Routing = CreateObject<Ipv4StaticRouting> ();
56  ipv4->SetRoutingProtocol (ipv4Routing);
57  node->AggregateObject (ipv4);
58  node->AggregateObject (ipv4Routing);
59  //ICMP
60  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
61  node->AggregateObject (icmp);
62  //UDP
63  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
64  node->AggregateObject (udp);
65 }
66 
67 
69 {
71  void DoSendData (Ptr<Socket> socket, std::string to);
72  void SendData (Ptr<Socket> socket, std::string to);
73 
74 public:
75  virtual void DoRun (void);
77 
78  void ReceivePkt (Ptr<Socket> socket);
79 };
80 
82  : TestCase ("UDP socket implementation")
83 {
84 }
85 
87 {
88  uint32_t availableData;
89  availableData = socket->GetRxAvailable ();
90  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
91  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
92 }
93 
94 void
96 {
97  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);
98  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
99  123, "100");
100 }
101 
102 void
104 {
105  m_receivedPacket = Create<Packet> ();
106  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
107  &Ipv4ForwardingTest::DoSendData, this, socket, to);
108  Simulator::Run ();
109 }
110 
111 void
113 {
114  // Create topology
115 
116  // Receiver Node
117  Ptr<Node> rxNode = CreateObject<Node> ();
118  AddInternetStack (rxNode);
119  Ptr<SimpleNetDevice> rxDev;
120  { // first interface
121  rxDev = CreateObject<SimpleNetDevice> ();
122  rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
123  rxNode->AddDevice (rxDev);
124  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
125  uint32_t netdev_idx = ipv4->AddInterface (rxDev);
126  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
127  ipv4->AddAddress (netdev_idx, ipv4Addr);
128  ipv4->SetUp (netdev_idx);
129  }
130 
131  // Forwarding Node
132  Ptr<Node> fwNode = CreateObject<Node> ();
133  AddInternetStack (fwNode);
134  Ptr<SimpleNetDevice> fwDev1, fwDev2;
135  { // first interface
136  fwDev1 = CreateObject<SimpleNetDevice> ();
137  fwDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
138  fwNode->AddDevice (fwDev1);
139  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
140  uint32_t netdev_idx = ipv4->AddInterface (fwDev1);
141  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
142  ipv4->AddAddress (netdev_idx, ipv4Addr);
143  ipv4->SetUp (netdev_idx);
144  }
145 
146  { // second interface
147  fwDev2 = CreateObject<SimpleNetDevice> ();
148  fwDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
149  fwNode->AddDevice (fwDev2);
150  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
151  uint32_t netdev_idx = ipv4->AddInterface (fwDev2);
152  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.1.0.1"), Ipv4Mask (0xffff0000U));
153  ipv4->AddAddress (netdev_idx, ipv4Addr);
154  ipv4->SetUp (netdev_idx);
155  }
156 
157  // Sender Node
158  Ptr<Node> txNode = CreateObject<Node> ();
159  AddInternetStack (txNode);
160  Ptr<SimpleNetDevice> txDev;
161  {
162  txDev = CreateObject<SimpleNetDevice> ();
163  txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
164  txNode->AddDevice (txDev);
165  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
166  uint32_t netdev_idx = ipv4->AddInterface (txDev);
167  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.1.0.2"), Ipv4Mask (0xffff0000U));
168  ipv4->AddAddress (netdev_idx, ipv4Addr);
169  ipv4->SetUp (netdev_idx);
170  Ptr<Ipv4StaticRouting> ipv4StaticRouting = txNode->GetObject<Ipv4StaticRouting> ();
171  ipv4StaticRouting->SetDefaultRoute(Ipv4Address("10.1.0.1"), netdev_idx);
172  }
173 
174  // link the two nodes
175  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
176  rxDev->SetChannel (channel1);
177  fwDev1->SetChannel (channel1);
178 
179  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
180  fwDev2->SetChannel (channel2);
181  txDev->SetChannel (channel2);
182 
183  // Create the UDP sockets
184  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
185  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
186  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
187  rxSocket->SetRecvCallback (MakeCallback (&Ipv4ForwardingTest::ReceivePkt, this));
188 
189  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
190  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
191  txSocket->SetAllowBroadcast (true);
192 
193  // ------ Now the tests ------------
194 
195  // Unicast test
196  SendData (txSocket, "10.0.0.2");
197  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv4 Forwarding on");
198 
200  m_receivedPacket = 0;
201 
202  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
203  ipv4->SetAttribute("IpForward", BooleanValue (false));
204  SendData (txSocket, "10.0.0.2");
205  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv4 Forwarding off");
206 
207  Simulator::Destroy ();
208 
209 }
210 
211 
212 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
215 {
216 public:
217  Ipv4ForwardingTestSuite () : TestSuite ("ipv4-forwarding", UNIT)
218  {
219  AddTestCase (new Ipv4ForwardingTest, TestCase::QUICK);
220  }
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
Hold a bool native type.
Definition: boolean.h:38
void DoSendData(Ptr< Socket > socket, std::string to)
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
Ptr< Packet > m_receivedPacket
A suite of tests to run.
Definition: test.h:1025
#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
virtual void DoRun(void)
Implementation to actually run this TestCase.
a polymophic address class
Definition: address.h:86
void SendData(Ptr< Socket > socket, std::string to)
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
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
Static routing protocol for IP version 4 stacks.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
static void AddInternetStack(Ptr< Node > node)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ReceivePkt(Ptr< Socket > socket)
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.
API to create UDP socket instances.
This test suite implements a Unit Test.
Definition: test.h:1035
Ipv4ForwardingTestSuite g_ipv4forwardingTestSuite
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
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.