NW - Pallavi (1) - 1
NW - Pallavi (1) - 1
NW - Pallavi (1) - 1
Practical No: 1
Theory: NS-3 has been developed to provide an open, extensible network simulation
platform, for networking research and education. In brief, ns-3 provides models of how packet
data networks work and perform and provides a simulation engine for users to conduct simulation
experiments. NS-3 is primarily developed on GNU/Linux and macOS platforms, and the minimal
requirements to run basic simulations are a C++ compiler; either g++ or clang++ compiler, and
Python (version 3) interpreter.
Program: After Installing Ubuntu 20.04 LTS successfully, you can now start with installing of
NS3 packages.
Netanim animator: qt5 development tools are needed for Netanim animator; apt-get
install qt5-default mercurial
ns-3-pyviz visualizer
apt-get install gir1.2-goocanvas-2.0 python-gi python-gi- cairo python-pygraphviz python3-gi
python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython ipython3
The ns-3 manual and tutorial are written in reStructuredText for Sphinx (doc/tutorial,
doc/manual, doc/models), and figures typically in dia (also needs the texlive packages
above):
apt-get install python3-sphinx dia
After installing the required packages, create a folder named workspace in the home
directory and then put the NS3 tar package into the workspace.
Go to terminal and input these commands consecutively after each command finishes executing:
cd
cd workspace tar xjf <name of NS3 downloaded file name>
cd <name of extracted NS3>
./build.py --enable-examples --enable-tests It
takes time be patient !!
Test the NS3 build and installation success by running test.py in the ns directory using
the following commands: cd ns-<version number>
./test.py
Practical No: 2
Theory: NetAnim is an offline animator based on the Qt toolkit. It currently animates the
simulation using an XML trace file collected during simulation. The NetAnim application requires
a custom trace file for animation. This trace file is created by AnimationInterface in ns-3.
Installation: After Installing Ubuntu 20.04 LTS successfully, you can now start with installing of
Net anim packages.
Netanim animator:
qt5 development tools are needed for Netanim animator; apt-get install qt5-default mercurial
ns-3-pyviz visualizer
apt-get install gir1.2-goocanvas-2.0 python-gi python-gi- cairo python-pygraphviz python3-gi
python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython ipython3
Debugging:
apt-get install gdb valgrind
The ns-3 manual and tutorial are written in reStructuredText for Sphinx (doc/tutorial,
doc/manual, doc/models), and figures typically in dia (also needs the texlive packages
above): apt-get install python3-sphinx dia
After installing the required packages, create a folder named workspace in the home
directory and then put the NS3 tar package into the workspace.
Go to terminal and input these commands consecutively after each command finishes executing:
cd
cd workspace tar xjf <name of NS3 downloaded file name>
cd <name of extracted NS3>
./build.py --enable-examples --enable-tests It
takes time be patient !!
Test the NS3 build and installation success by running test.py in the ns directory using
the following commands: cd ns-<version number>
./test.py
Practical No: 3
Installation Process:
Practical No: 4
Objective: To learn simulate traffic between two nodes and print String .
Theory: The Simulation Generate node provides an easy way to generate simulated data, either without
historical data using user specified statistical distributions, or automatically using the distributions obtained
from running a Simulation Fitting node on existing historical data.
Program:
First.cc
#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h" #include "ns3/applications-module.h" // Default Network
Topology
//
// 10.1.1.0
// n0 n1
// point-to-point
//
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); int main
(int argc, char *argv[])
{
CommandLine cmd ( FILE ); cmd.Parse (argc, argv); Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication",
LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes; nodes.Create (2); PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes); InternetStackHelper stack; stack.Install (nodes); Ipv4AddressHelper
address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
//server
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0));
//client
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval",
TimeValue (Seconds (1.0)));
Practical No: 5
Theory: Star topology is a network topology in which each network component is physically
connected to a central node such as a router, hub or switch. In a star topology, the central hub acts
like a server and the connecting nodes act like clients.
When a computer sends data to other computers on the network, it is sent along the cable to a central hub or
switch, which then determines which port it needs to send the data through for it to reach the proper
destination.
Program:
Practical No: 6
Theory: Bus topology, also known as line topology, is a type of network topology in which all
devices in the network are connected by one central RJ-45 network cable or coaxial cable.
The single cable, where all data is transmitted between devices, is referred to as the bus, backbone, or trunk.
A bus topology connects computers along a single or more cable to connect linearly. A
network that uses a bus topology is referred to as a "bus network" which was the original form of Ethernet
networks.
Line of Code:
Bus.cc
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/netanim-module.h"
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================ //
LAN 10.1.2.0 using namespace ns3;
NS_LOG_COMPONENT_DEFINE
("SecondScriptExample");
int
main (int argc, char *argv[])
{ bool verbose =
true; uint32_t nCsma
= 3;
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
} nCsma = nCsma == 0 ? 1 :
nCsma;
NodeContainer p2pNodes;
p2pNodes.Create (2);
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
InternetStackHelper stack;
stack.Install (p2pNodes.Get (0));
stack.Install (csmaNodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces; p2pInterfaces
= address.Assign (p2pDevices);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);
AnimationInterface anim("bus.xml");
Simulator::Run ();
Simulator::Destroy ();
return 0; }
Program:
Output Screen:
Practical No: 7
Theory: In a mesh topology there is no central connection point. Instead, each node is
connected to at least one other node and usually to more than one. Each node is capable
of sending messages to and receiving messages from other nodes. The nodes act as
relays, passing on a message towards its final destination.
Line of Code:
Mesh.cc
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as * published
by the Free Software Foundation; *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General
Public License for more details. *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <iostream>
#include <sstream>
#include <fstream>
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mesh-module.h"
#include "ns3/mobility-module.h"
#include "ns3/mesh-helper.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/netanim-module.h" using
namespace ns3;
NS_LOG_COMPONENT_DEFINE ("MeshExample");
void
TxTrace (Ptr<const Packet> p)
{
NS_LOG_DEBUG ("Sent " << p->GetSize () << " bytes");
g_udpTxCount++;
}
void
RxTrace (Ptr<const Packet> p)
{
NS_LOG_DEBUG ("Received " << p->GetSize () << " bytes");
g_udpRxCount++;
}
/**
* \ingroup mesh
* \brief MeshTest class
*/
class MeshTest
{ public: ///
Init test
MeshTest ();
/**
* Configure test from command line arguments
*
* \param argc command line argument count
* \param argv command line arguments
*/
void Configure (int argc, char ** argv);
/**
* Run test
* \returns the test status
*/ int
Run ();
private:
int m_xSize; ///< X size int
m_ySize; ///< Y size double m_step; ///< step
double m_randomStart; ///< random start
double m_totalTime; ///< total time double
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0), "MinY",
DoubleValue (0.0),
"DeltaX", DoubleValue (m_step),
"DeltaY", DoubleValue (m_step),
"GridWidth", UintegerValue (m_xSize),
"LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel
("ns3::ConstantPositionMobilityModel"); mobility.Install (nodes); if
(m_pcap) wifiPhy.EnablePcapAll (std::string ("mp")); if (m_ascii)
{
AsciiTraceHelper ascii;
wifiPhy.EnableAsciiAll (ascii.CreateFileStream ("mesh.tr"));
}
}
void
MeshTest::InstallInternetStack ()
{
InternetStackHelper internetStack;
internetStack.Install (nodes);
Ipv4AddressHelper address; address.SetBase
("10.1.1.0", "255.255.255.0"); interfaces =
address.Assign (meshDevices);
}
void
MeshTest::InstallApplication ()
{
uint16_t portNumber = 9;
UdpEchoServerHelper echoServer (portNumber);
uint16_t sinkNodeId = m_xSize * m_ySize - 1;
ApplicationContainer serverApps = echoServer.Install (nodes.Get (sinkNodeId));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (m_totalTime + 1));
UdpEchoClientHelper echoClient (interfaces.GetAddress (sinkNodeId), portNumber);
echoClient.SetAttribute ("MaxPackets", UintegerValue ((uint32_t)
(m_totalTime*(1/m_packetInterval))));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (m_packetInterval)));
echoClient.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
Ptr<UdpEchoClient> app = clientApps.Get (0)->GetObject<UdpEchoClient> ();
app->TraceConnectWithoutContext ("Tx", MakeCallback (&TxTrace)); app-
>TraceConnectWithoutContext ("Rx", MakeCallback (&RxTrace)); clientApps.Start
(Seconds (1.0));
clientApps.Stop (Seconds (m_totalTime + 1.5));
} int
MeshTest::Run ()
{
CreateNodes ();
InstallInternetStack ();
InstallApplication ();
Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this);
Simulator::Stop (Seconds (m_totalTime + 2));
AnimationInterface anim("Mesh.xml");
Program:
Output Screen:
Practical No: 8
Theory: A hybrid topology is a type of network topology that uses two or more differing
network topologies. These topologies can include a mix of bus topology, mesh topology, ring topology,
star topology, and tree topology. Its usage and choice are dependent on its deployments and
requirements like the performance of the desired network, and the number of computers, their
location. The below figure is describing the structure of hybrid topology that contains more than one
topology.
Program:
Line of Code:
Hybrid.cc
#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/ssid.h"
# include "ns3/netanim-module.h"
// Default Network Topology
//
// Wifi 10.1.3.0
// AP
// * * * *
// | | | | 10.1.1.0
// n5 n6 n7 n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0 using
namespace ns3;
NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");
int
main (int argc, char *argv[])
{ bool verbose =
true; uint32_t nCsma
= 3; uint32_t nWifi =
3; bool tracing =
false;
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); }
NodeContainer p2pNodes;
p2pNodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices; csmaDevices = csma.Install (csmaNodes);
NodeContainer wifiStaNodes;
wifiStaNodes.Create (nWifi);
NodeContainer wifiApNode = p2pNodes.Get (0);
WifiMacHelper mac;
Ssid ssid = Ssid ("ns-3-ssid");
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid),
"ActiveProbing", BooleanValue (false));
NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes);
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (10.0),
"GridWidth", UintegerValue (3),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
mobility.Install (wifiStaNodes);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiApNode);
InternetStackHelper stack;
stack.Install (csmaNodes); stack.Install
(wifiApNode); stack.Install
(wifiStaNodes); Ipv4AddressHelper
address;
ApplicationContainer clientApps =
echoClient.Install (wifiStaNodes.Get (nWifi - 1));
clientApps.Start (Seconds (2.0)); clientApps.Stop
(Seconds (10.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
if (tracing == true)
{
pointToPoint.EnablePcapAll ("third");
phy.EnablePcap ("third", apDevices.Get (0));
csma.EnablePcap ("third", csmaDevices.Get (0), true); }
AnimationInterface anim("hybrid.xml");
Simulator::Run ();
Simulator::Destroy ();
return 0; }
Output Screen:
Practical No: 9
Theory: In UDP, the client does not form a connection with the server like in TCP and
instead just sends a datagram. Similarly, the server need not accept a connection and just waits for
datagrams to arrive. Datagrams upon arrival contain the address of the sender which the server uses to send
data to the correct client.
Program:
Line of Code:
UDP.cc
//
// Now, do the actual simulation.
//
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done."); }
Output: WireShark:
Practical No: 10
Theory: A DHCP Server is a network server that automatically provides and assigns IP
addresses, default gateways and other network parameters to client devices. It relies on the
standard protocol known as Dynamic Host Configuration Protocol or DHCP to respond to broadcast queries
by clients.
Program:
Line of Code:
DHCP.cc
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2011 UPB
* Copyright (c) 2017 NITK Surathkal
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation; *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General
Public License for more details. *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
/
//
* Network layout:
*
* R0 is a DHCP server. The DHCP server announced R1 as the default router.
* Nodes N1 will send UDP Echo packets to node A.
*
*
* ┌-------------------------------------------------┐
* | DHCP Clients |
* | 172.30.0.14 |
* | DHCP static |
* | ┌──────┐ ┌──────┐ ┌──────┐ |
* | │ N0 │ │ N1 │ │ N2 │ | ┌──────┐
* | └──────┘ └──────┘ └──────┘ | ┌────│ A │
* | │ │ │ | │ └──────┘
* └-------│--------------│---------------│----------┘ │ 172.30.1.2
* DHCP Server │ │ │ │
* ┌──────┐ │ │ │ ┌──────┐ │
* │ R0
│────────┴──────────────┴───────────────┴──────│ R1 │────┘
* └──────┘ └──────┘172.30.1.1
* 172.30.0.12 172.30.0.17
*
* Things to notice:
* 1) The routes in A are manually set to have R1 as the default router,
* just because using a dynamic outing in this example is an overkill.
* 2) R1's address is set statically though the DHCP server helper interface.
* This is useful to prevent address conflicts with the dynamic pool.
* Not necessary if the DHCP pool is not conflicting with static addresses. * 3)
N2 has a dynamically-assigned, static address (i.e., a fixed address assigned
via DHCP).
*
*/
#include "ns3/core-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h" #include
"ns3/netanim-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("DhcpExample");
int main (int argc, char *argv[])
{
CommandLine cmd (__FILE__);
bool verbose = false; bool
tracing = false;
std::string animFile = "dhcp-server-client-animation.xml";
cmd.AddValue ("verbose", "turn on the logs", verbose); cmd.AddValue
("tracing", "turn on the tracing", tracing); cmd.Parse (argc,
argv);
// GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true)); if
(verbose)
{