0% found this document useful (0 votes)
66 views9 pages

NST32031-Practical For Wireless Network: Department of ICT Faculty of Technology South Eastern University of Sri Lanka

This document provides instructions for building a bus network topology simulation using NS-3 and analyzing the network traffic. It includes: 1. An explanation of the C++ source code used to set up the network simulation with 4 CSMA nodes connected in a bus topology via a point-to-point link. 2. Instructions for using Wireshark and tcpdump to analyze the pcap files capturing network traffic from the simulation. 3. Modifications to the code to enable visualization of the network using the NetAnim module, including positioning nodes.

Uploaded by

aiz15563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views9 pages

NST32031-Practical For Wireless Network: Department of ICT Faculty of Technology South Eastern University of Sri Lanka

This document provides instructions for building a bus network topology simulation using NS-3 and analyzing the network traffic. It includes: 1. An explanation of the C++ source code used to set up the network simulation with 4 CSMA nodes connected in a bus topology via a point-to-point link. 2. Instructions for using Wireshark and tcpdump to analyze the pcap files capturing network traffic from the simulation. 3. Modifications to the code to enable visualization of the network using the NetAnim module, including positioning nodes.

Uploaded by

aiz15563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

2021/05/07

NST32031-Practical for Wireless Network


Department of ICT
Faculty of Technology
South Eastern University of Sri Lanka
Lab Sheet 03

Title: Building a Bus Network Topology


Source Code:
 it is saved inside your downloaded ns3 file example/tutorial location
 copy second.cc to ‘ns-330/scratch’ folder

Task 01: Source Code Explanation (second.cc or second.py):


//Header files adding
#include "ns3/core-module.h"
#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"

// Default Network Topology


// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0

// This groups all ns-3-related declarations in a scope outside the global namespace,
using namespace ns3;

//Doxygen documentation system. this line declares a logging component called


SecondScriptExample that allows you to enable and disable console message logging by
reference to the name.
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

//Declaration of the main function of your program (script)


int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 3; //unsigned integer 32bits
//number of csma nodes = 3
//n1 is point-to-point
CommandLine cmd;
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); // 3 csma
nodes
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); //whatever we
are doing is recorded

cmd.Parse (argc,argv);

if (verbose) //if verbose value is true boolean


{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
nCsma = nCsma == 0 ? 1 : nCsma; //if there is no nodes in csma, it takes at total node == 1

//Topology Helpers
//NodeContainer topology helper provides a convenient way to create, manage and access
any Node objects that we create in order to run a simulation
NodeContainer p2pNodes;
//point-to-point connection between n0 and n1
//asks the container to create two nodes.
p2pNodes.Create (2);

NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma); //nCsma = 3

//constructing a point to point link


PointToPointHelper pointToPoint; //instantiates a PointToPointHelper object on the stack
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

//NetDeviceContainer holds a list of all of the NetDevice objects that are created
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);

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 (p2pNodes.Get (1))there is no need to
install
stack.Install (csmaNodes); //p2pNodes.Get(1)= csmaNodes.Get(0)
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);

address.SetBase ("10.1.2.0", "255.255.255.0");


Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));


//The server is csmaNodes.Get(3) - n4 is the server
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);


echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));


clientApps.Start (Seconds (2.0)); //n0 is the client
clientApps.Stop (Seconds (10.0));

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

pointToPoint.EnablePcapAll ("second");
//pcap will be opened using either wireshark or tcpdump
csma.EnablePcap ("second", csmaDevices.Get (1), true);
//if you want to get packets to other nodes;
//csma.EnablePcap ("second", csmaDevices.Get (3), true);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}

To run this;
First copy paste second.cc to scratch folder. Then run below codes in terminal;

1. >> cd ns-allinone-3.30/
2. >> cd ns-3.30/
3. >>. /waf --run scratch/second.cc

Task 02: To implement network simulation using Wireshark;


 if you have not installed Wireshark follow below command
o >> sudo apt-get install wireshark

 here is the ‘.pcap’ files that were created inside ns3;

 run below code to see network simulation in Wireshark;


o >> wireshark second-1-0.pcap
Task 03: To implement network simulation using tcpdump:
>> tcpdump –n –t –r second-1-0.pcap

Task 04: To implement network simulation NetAnim to below changes:


#include "ns3/core-module.h"
#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"

using namespace ns3;


NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 3;

CommandLine cmd;
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);

cmd.Parse (argc,argv);

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"));

NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);

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);

address.SetBase ("10.1.2.0", "255.255.255.0");


Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));


serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);


echoClient.SetAttribute ("MaxPackets", UintegerValue (3));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));


clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);

//Network Animator
AnimationInterface anim(“second.xml”);
anim.SetConstantPosition(p2pNodes.Get(0), 10.0,10.0);
anim.SetConstantPosition(p2pNodes.Get(1), 20.0,20.0);
anim.SetConstantPosition(csmaNodes.Get(1), 30.0,30.0);
anim.SetConstantPosition(csmaNodes.Get(2), 40.0,40.0);
anim.SetConstantPosition(csmaNodes.Get(3), 50.0,50.0);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}

To run this;

1. ./waf –run scratch/second


2. cd ns-allinone-3.30/netaim-3.108/
3. /NetAnim

You might also like