EC3401 LAB Manual - Network Security - 2022-2023
EC3401 LAB Manual - Network Security - 2022-2023
EC3401 LAB Manual - Network Security - 2022-2023
INDEX
1
EC3401- Networks and Security Department of ECE 2022 - 2023
LIST OF EXPERIMENTS
S.No. CYCLE I
1. Network Topology - Star, Bus and Ring Using Trainer Kit
2. Implement the Data Link Layer framing methods
i) Bit stuffing, (ii) Character stuffing
3. Implementation of Error Detection / Correction Techniques
(i) CRC, (ii) Hamming code
4. Implementation of Stop and Wait Protocols
5. Implementation of Go back-N and Selective Repeat Protocols.
6. Implementation of Distance Vector Routing algorithm (Routing Information
Protocol) (Bellman-Ford).
7. Implementation of Link State Routing algorithm (Open Shortest Path First) with 5
nodes (Dijkstra's).
8. Data encryption and decryption using Data Encryption Standard algorithm.
9. Data encryption and decryption using RSA (Rivest, Shamir and Adleman) algorithm.
10. Implement Client Server model using FTP protocol.
11. Implement and realize the Network Topology - Star, Bus and Ring using NS2.
12. Implement and perform the operation of CSMA/CD and CSMA/CA using NS2.
2
EC3401- Networks and Security Department of ECE 2022 - 2023
To create scenario and study the performance of network topologies through simulation and using
trainer kit.
APPARATUS REQUIRED
THEORY:
3
EC3401- Networks and Security Department of ECE 2022 - 2023
PROCEDURE:
1. Connect 2 computer LAN ports using RJ-45 to RJ-45 LAN connecting cables provided with the
system to LTS-01 star topology ports.
2. Switch on the LTS-01 & Computers.
3. Run L-SIM software on all the computers, one should be server and othersshould be clients.
4. On the server computer select type of network as LAN and select the topology as STAR, and
select protocol as Stop & Wait & click on create network button.
5. Remote computer details will appear on the computers connected in network, the server will be
able to see all clients and all clients will be able to see only the server.
6. Select the computer to whom data file is to be transferred, by using the load button.
7. File size will appear in the software window, select the packet size, interpacket delay and click
OK.
8. Total packets formed for that file will be indicated on computers.
9. Start the transfer of file, and during file transfer process you can insert errors into data Packets
being transmitted through software window.
10. Verify the formation of error packet and also the process of re-transmission of the error packet
using stop and wait protocol.
11. File transfer from one computer to another will take place.
12. Multiple file transfer between various server-client combinations should be performed to observe
throughput v/s packet size graph on transmitter computer.
13. Close file transfer window and click on protocol analyzer and Network analyzer buttons on
transmitter computer to view details of the log created.
14. Calculate throughput by clicking on Plot graph button.
15. Detailed graph of throughput v/s packet size for the total file transfer activity will appear on graph
window.
16. This plot can be printed by clicking on print button
17. The screen shot of the screen during transmission will look like the below picture.
RESULT:
Thus the network topologies through simulation and using trainer kit was executed and the
output was verified.
4
EC3401- Networks and Security Department of ECE 2022 - 2023
APPARATUS REQUIRED:
1. Personal Computer
2. C Complier
THEORY:
Bit Stuffing is a process of inserting an extra bit as 0, once the frame sequence
encountered 5 consecutive 1’s. Given an array, arr[] of size N consisting of 0’s and 1’s, the task is to
return an array after the bit stuffing.
5
EC3401- Networks and Security Department of ECE 2022 - 2023
Programme
#include <stdio.h>
#include <string.h>
// Function for bit stuffing
void bitStuffing(int N, int arr[])
{
// Stores the stuffed array
int brr[30];
// Variables to traverse arrays
int i, j, k;
i = 0;
j = 0;
// Loop to traverse in the range [0, N)
while (i < N) {
// If the current bit is a set bit
if (arr[i] == 1) {
// Stores the count of consecutive ones
int count = 1;
// Insert into array brr[]
brr[j] = arr[i];
// Loop to check for
// next 5 bits
for (k = i + 1;
arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
// If 5 consecutive set bits
// are found insert a 0 bit
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
}
}
// Otherwise insert arr[i] into
// the array brr[]
else {
brr[j] = arr[i];
}
i++;
j++;
}
// Print Answer
for (i = 0; i < j; i++)
printf("%d", brr[i]);
}
// Driver Code
int main()
6
EC3401- Networks and Security Department of ECE 2022 - 2023
{
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
return 0;
}
Input: N=6,arr[]={1,1,1,1,1,1}
Output: 1111101
Programme
#include<stdio.h>
#include<string.h>
int main()
{
char a[30], fs[50] = " ", t[3], sd, ed, x[3], s[3], d[3], y[3];
int i, j, p = 0, q = 0;
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter a character that represents ending delimiter:");
scanf(" %c", &ed);
x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0';
y[0] = d[0] = d[1] = ed;
d[2] = y[1] = '\0';
strcat(fs, x);
for(i = 0; i < strlen(a); i++)
{
t[0] = a[i];
t[1] = '\0';
if(t[0] == sd)
strcat(fs, s);
else if(t[0] == ed)
strcat(fs, d);
else
strcat(fs, t);
}
strcat(fs, y);
printf("\n After stuffing:%s", fs);
getchar();
}
7
EC3401- Networks and Security Department of ECE 2022 - 2023
Output :
RESULT:
Thus the implementation of data link layer framing method using bit stuffing and charater
stuffing was executed and the output was verified.
8
EC3401- Networks and Security Department of ECE 2022 - 2023
APPARATUS REQUIRED:
1. Personal Computer
2. C Complier
THEORY:
When sending data from a sender to a receiver, there are possibilities that the data sent
as digital signals may get changed due to noise during the process of sending the data. Due to
this, data received by the receiver may be different from the data sent by the sender. This
change in data bit is called an error and to prevent such an error, the data received by the
receiver is checked using different error detection algorithms. One such algorithm is
the Cyclic Redundancy Check algorithm.
PROGRAM:
// Include headers
#include<stdio.h>
#include<string.h>
// length of the generator polynomial
#define N strlen(gen_poly)
// data to be transmitted and received
char data[28];
// CRC value
char check_value[28];
// generator polynomial
char gen_poly[10];
// variables
int data_length,i,j;
// function that performs XOR operation
void XOR(){
// if both bits are the same, the output is 0
// if the bits are different the output is 1
for(j = 1;j < N; j++)
check_value[j] = (( check_value[j] == gen_poly[j])?'0':'1');
}
// Function to check for errors on the receiver side
void receiver(){
// get the received data
printf("Enter the received data: ");
scanf("%s", data);
printf("\n-----------------------------\n");
printf("Data received: %s", data);
// Cyclic Redundancy Check
crc();
9
EC3401- Networks and Security Department of ECE 2022 - 2023
void crc(){
// initializing check_value
for(i=0;i<N;i++)
check_value[i]=data[i];
do{
// check if the first bit is 1 and calls XOR function
if(check_value[0]=='1')
XOR();
// Move the bits by 1 position for the next computation
for(j=0;j<N-1;j++)
check_value[j]=check_value[j+1];
// appending a bit from data
check_value[j]=data[i++];
}while(i<=data_length+N-1);
// loop until the data ends
}
int main()
{
// get the data to be transmitted
printf("\nEnter data to be transmitted: ");
scanf("%s",data);
printf("\n Enter the Generating polynomial: ");
// get the generator polynomial
scanf("%s",gen_poly);
// find the length of data
data_length=strlen(data);
// appending n-1 zeros to the data
for(i=data_length;i<data_length+N-1;i++)
data[i]='0';
printf("\n----------------------------------------");
// print the data with padded zeros
printf("\n Data padded with n-1 zeros : %s",data);
printf("\n----------------------------------------");
// Cyclic Redundancy Check
crc();
// print the computed check value
printf("\nCRC or Check value is : %s",check_value);
// Append data with check_value(CRC)
for(i=data_length;i<data_length+N-1;i++)
data[i]=check_value[i-data_length];
printf("\n----------------------------------------");
// printing the final data to be sent
printf("\n Final data to be sent : %s",data);
printf("\n----------------------------------------\n");
// Calling the receiver function to check errors
receiver();
10
EC3401- Networks and Security Department of ECE 2022 - 2023
return 0;
}
Output
Uncorrupted data
Enter data to be transmitted: 1001101
Enter the Generating polynomial: 1011
----------------------------------------
Data padded with n-1 zeros : 1001101000
----------------------------------------
CRC or Check value is : 101
----------------------------------------
Final data to be sent : 1001101101
----------------------------------------
Enter the received data: 1001101101
-----------------------------
Data received: 1001101101
No error detected
11
EC3401- Networks and Security Department of ECE 2022 - 2023
Programme
for(i=3;i>=0;i--)
scanf("%d",&b[i]);
for(i=2; i>=0;i--)
{
printf("%d",s[i]);
}
if((s[2]==0) && (s[1]==0) && (s[0]==0))
printf("\n RECIEVED WORD IS ERROR FREE\n");
if((s[2]==1) && (s[1]==0) && (s[0]==1))
printf("\nrecieved word is error with error in 1bit(b0) position from right\n");
if((s[2]==1) && (s[1]==1) && (s[0]==1))
printf("\nrecieved word is error with error in 2bit(b1) position from right\n");
if((s[2]==0) && (s[1]==1) && (s[0]==1))
printf("\nrecieved word is error with error in 3bit(b2) position from right\n");
if((s[2]==1) && (s[1]==1) && (s[0]==0))
printf("\nrecieved word is error with error in 4bit(b3) position from right\n");
return(1);
}
//End of Hamming code program
OUTPUT
enter 4 bit data word:
1
0
1
12
EC3401- Networks and Security Department of ECE 2022 - 2023
1
1
the 7bit hamming code word:
1 0 1 1 1 0 0
RESULT:
Thus the implementation of Error Detection and Error Correction using CRC and
Hamming code was executed and the output was verified.
13
EC3401- Networks and Security Department of ECE 2022 - 2023
AIM:
To write a java program to implement the file encryption and decryption using java.
REQUIREMENTS:
Java Software
ALGORITHM:
PROGRAM:
Encrypt.java:
import java.io.*;
class encrypt
{
public static void main(String args[]) throws Exception
{
DataInputStream in= new DataInputStream(System.in);
FileInputStream f1=new FileInputStream(“ori.txt”);
FileOutputStream f2=new FileOutputStream(“encr.txt”);
int a, key, a1;
try
{
System.out.println(“Enter the key value for Encryption”);
key= Integer.parseInt(in.readLine());
a=f1.read();
while(a!=-1)
{
a1=a+key;
f2.write(a1);
a=f1.read();
}
System.out.println(“the Message was successfully Encrypted”);
f1.close();
f2.close();
14
EC3401- Networks and Security Department of ECE 2022 - 2023
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Decrypt.java:
import java.io.*;
class decrypt
{
public static void main(String args[]) throws Exception
{
DataInputStream in=new DataInputStream(System.in);
FileInputStream f1=new FileInputStream(“encr.txt”);
FileOutputStream f2=new FileOutputStream(“decr.txt”);
int a, key, a1
try
{
System.out.println(“Enter the key value for decryption”);
key=Integer.parseInt(in.readLine());
a=f1.read();
while(a!=-1)
{
a1=a-key;
f2.write(a1);
a=f1.read();
}
System.out.println(“The Message was successfully decrypted”);
f1.close();
f2.close();
}
catch(Exception e)
{
System.out.printlln(e);
}
}
}
15
EC3401- Networks and Security Department of ECE 2022 - 2023
OUTPUT:
RESULT:
Thus the implementation of the file encryption and decryption using java was
executed and the output was verified.
16
EC3401- Networks and Security Department of ECE 2022 - 2023
APPARTUS REQUIRED:
1.Personal computer
2. C Complier
PROGRAM
#include<stdio.h>
#include<math.h>
void encrypt();
void decrypt();
long int findGCD(long int, long int);
long int multipicative_inverse(long int, long int);
void key_pair(long int e, long int n, long int d) {
printf("\nEncryption key(e,n):%ld\t%ld\n", e, n);
printf("\nDecryption key(d,n):%ld\t%ld\n", d, n);
}
void encrypt() {
long int pt, p, q, n, fn, e = 2, temp, d, ct;
printf("Enter your message to encrypt:\n");
scanf("%d", & pt);
printf("Enter two prime numbers p and q:\n");
scanf("%ld %ld", & p, & q);
n = p * q;
fn = (p - 1) * (q - 1);
temp = findGCD(e, fn);
while (temp != 1) {
e++;
temp = findGCD(e, fn);
//printf("e=%ld\ttemp=%d\t",e,temp);
}
//printf("\ne=%ld\n",e);
d = multipicative_inverse(fn, e);
if (d == 0) {
printf("\nrsa is not possible for given p and q.\n");
exit(0);
}
ct = fmod(pow(pt, e), n);
key_pair(e, n, d);
printf("\nencrypted text:\t%ld\n", ct);
}
void decrypt() {
long int pt, d, ct, n;
printf("Enter message to decrypt:\n");
scanf("%ld", & ct);
printf("Enter key pair(d,n):\n");
scanf("%ld %ld", & d, & n);
pt = fmod(pow(ct, d), n);
17
EC3401- Networks and Security Department of ECE 2022 - 2023
18
EC3401- Networks and Security Department of ECE 2022 - 2023
OUTPUT
RESULT:
19
EC3401- Networks and Security Department of ECE 2022 - 2023
AIM:
APPARTUS REQUIRED:
1.Personal computer
2. C Complier
PROGRAM
#include <stdio.h>
#include <string.h>
int main()
{
int count,src_router,i,j,k,w,v,min;
int cost_matrix[100][100],dist[100],last[100];
int flag[100];
printf("\n Enter the no of routers");
scanf("%d",&count);
printf("\n Enter the cost matrix values:");
for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
printf("\n%d->%d:",i,j);
scanf("%d",&cost_matrix[i][j]);
if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;
}
}
printf("\n Enter the source router:");
scanf("%d",&src_router);
for(v=0;v<count;v++)
{
flag[v]=0;
last[v]=src_router;
dist[v]=cost_matrix[src_router][v];
}
flag[src_router]=1;
for(i=0;i<count;i++)
{
min=1000;
20
EC3401- Networks and Security Department of ECE 2022 - 2023
for(w=0;w<count;w++)
{
if(!flag[w])
if(dist[w]<min)
{
v=w;
min=dist[w];
}
}
flag[v]=1;
for(w=0;w<count;w++)
{
if(!flag[w])
if(min+cost_matrix[v][w]<dist[w])
{
dist[w]=min+cost_matrix[v][w];
last[w]=v;
}
}
}
for(i=0;i<count;i++)
{
printf("\n%d==>%d:Path taken:%d",src_router,i,i);
w=i;
while(w!=src_router)
{
printf("\n<--%d",last[w]);w=last[w];
}
printf("\n Shortest path cost:%d",dist[i]);
}
}
OUTPUT:
21
EC3401- Networks and Security Department of ECE 2022 - 2023
AIM:
APPARTUS REQUIRED:
1.Personal computer
2. C Complier
ALGORITHM:
SERVER:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Bind the IP address and Port number
STEP 6: Listen and accept the client’s request for the connection
STEP 7: Establish the connection with the client
STEP 8: Close the socket
STEP 9: Stop
CLIENT:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Call the connect() function
STEP 6: Close the socket
STEP 7: Stop
PROGRAMME
SERVER:
#include<stdio.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
#define MAX 60
22
EC3401- Networks and Security Department of ECE 2022 - 2023
int i, j, tem;
char buff[4096], t;
FILE *f1;
int main(int afg, char *argv)
{
int sockfd, newsockfd, clength;
struct sockaddr_in serv_addr,cli_addr;
char t[MAX], str[MAX];
strcpy(t,"exit");
sockfd=socket(AF_INET, SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nBinded");
bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr));
printf("\nListening...");
listen(sockfd, 5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*) &cli_addr,&clength);
close(sockfd);
read(newsockfd, &str, MAX);
printf("\nClient message\n File Name : %s\n", str);
f1=fopen(str, "r");
while(fgets(buff, 4096, f1)!=NULL)
{
write(newsockfd, buff,MAX);
printf("\n");
}
fclose(f1);
printf("\nFile Transferred\n");
return 0;
}
CLIENT:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
#define MAX 60
int main(int arg,char*argv[])
{
int sockfd,n;
struct sockaddr_in serv_addr;
struct hostent*server;
char send[MAX],recvline[MAX],s[MAX],name[MAX];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nEnter the source file name : \n");
scanf("%s",send);
write(sockfd,send,MAX);
while((n=read(sockfd,recvline,MAX))!=0)
23
EC3401- Networks and Security Department of ECE 2022 - 2023
{
printf("%s",recvline);
}
close(sockfd);
return 0;
}
OUTPUT: SERVER
CLIENT
RESULT:
Thus the program for FTP client server was executed and the output was verified.
24
EC3401- Networks and Security Department of ECE 2022 - 2023
INTRODUCTION:
Network Simulator (Version 2), widely known as NS2, is simply an eventdriven simulation tool that
has proved useful in studying the dynamic nature of communication networks. Simulation of wired as
well as wireless network functions and protocols (e.g., routing algorithms, TCP, UDP) can be done
using NS2. In general, NS2 provides users with a way of specifying such network protocols and
simulating their corresponding behaviors. Due to its flexibility and modular nature, NS2 has gained
constant popularity in the networking research community since its birth in 1989. Ever since, several
revolutions and revisions have marked the growing maturity of the tool, thanks to substantial
contributions from the players in the field. Among these are the University of California and Cornell
University who developed the REAL network simulator,1 the foundation which NS is based on.
Since 1995 the Defense Advanced Research Projects Agency (DARPA) supported development of
NS through the Virtual InterNetwork Testbed (VINT) project [9].2 Currently the National Science
Foundation (NSF) has joined the ride in development. Last but not the least, the group of Researchers
and developers in the community are constantly working to keep NS2 strong and versatile.
Basic Architecture
25
EC3401- Networks and Security Department of ECE 2022 - 2023
and OTcl languages. We refer the readers to [14] for the detail of C++, while a brief tutorial of Tcl
and OTcl tutorial are given in Appendices A.1 and A.2, respectively.
NS2 provides a large number of built-in C++ objects. It is advisable to use these C++ objects to set up
a simulation using a Tcl simulation script. However, advance users may find these objects
insufficient. They need to develop their own C++ objects, and use a OTcl configuration interface to
put together these objects. After simulation, NS2 outputs either text-based or animation-based
simulation results. To interpret these results graphically and interactively, tools such as NAM
(Network AniMator) and XGraph are used. To analyze a particular behaviour of the network, users
can extract a relevant subset of text-based data and transform it to a more conceivable presentation.
CONCEPT OVERVIEW:
NS uses two languages because simulator has two different kinds of things it needs to do. On one
hand,detailed simulations of protocols requires a systems programming language which can
efficiently manipulate bytes, packet headers, and implement algorithms that run over large data sets.
For these tasks run-time speed is important and turn-around time (run simulation, find bug, fix bug,
recompile, re-run) is less important. On the other hand, a large part of network research involves
slightly varying parameters or configurations, or quickly exploring a number of scenarios.
In these cases, iteration time (change the model and re-run) is more important. Since configuration
runs once (at the beginning of the simulation), run-time of this part of the task is less important. ns
meets both of these needs with two languages, C++ and OTcl.
SIMULATOR INITIALIZATION:
When a new simulation object is created in tcl, the initialization procedure performs the following
operations:
• initialize the packet format (calls create_packetformat)
• create a scheduler (defaults to a calendar scheduler)
• create a “null agent” (a discard sink used in various places)
The simulator is an event-driven simulator. There are presently four schedulers available in the
simulator, each of which is implemented using a different data structure: a simple linked-list, heap,
calendar queue (default), and a special type call called “real-time”. Each of these are described below.
The scheduler runs by selecting the next earliest event, executing it to completion, and returning to
execute the next event.Unit of time used by scheduler is seconds. Presently, the simulator is single-
threaded, and only one event in execution at any given time. If more than one event are scheduled to
execute at the same time, their execution is performed on the first scheduled – first dispatched
manner. Simultaneous events are not reordered anymore by schedulers (as it was in earlier versions)
and all schedulers should yeild the same order of dispatching given the same input.
NODE BASICS:
The basic primitive for creating a node is set ns [new Simulator] $ns node. The instance procedure
node constructs a node out of more simple classifier objects (Section 5.4). The Node itself is a
standalone class in OTcl. However, most of the components of the node are themselves TclObjects.
The typical structure of a (unicast) node is as shown in Figure 5.1. This simple structure consists of
two TclObjects: an address classifer (classifer_) and a port classifier (dmux_). The function of these
classifiers is to distribute incoming packets to the correct agent or outgoing link.
All nodes contain at least the following components:
• an address or id_, monotonically increasing by 1 (from initial value 0) across the simulation
namespace as nodes are created,
• a list of neighbors (neighbor_)
Set ns [new Simulator]: generates an NS simulator object instance, and assigns it to variable ns. The
"Simulator" object has member functions that do the following:
26
EC3401- Networks and Security Department of ECE 2022 - 2023
Most of member functions are for simulation setup (referred to as plumbing functions in the Overview
section) and scheduling, however some of them are for the NAM display. The "Simulator" object
member function implementations are located in the "ns-2/tcl/lib/ns-lib.tcl" file.
$ns color fid color: is to set color of the packets for a flow specified by the flow id (fid). This member
function of "Simulator" object is for the NAM display, and has no effect on the actual simulation.
$ns namtrace-all file-descriptor: This member function tells the simulator to record simulation traces
in NAM input format. It also gives the file name that the trace will be written to later by the command
$ns flush-trace. Similarly, the member function trace-all is for recording the simulation trace in a
general format.
proc finish {}: is called after this simulation is over by the command $ns at 5.0 "finish". In this
function, post-simulation processes are specified.
set n0 [$ns node]: The member function node creates a node. A node in NS is compound object made
of address and port classifiers (described in a later section). Users can create a node by separately
creating an address and a port classifier objects and connecting them together. However, this member
function of Simulator object makes the job easier. To see how a node is created, look at the files: "ns-
2/tcl/libs/ns-lib.tcl"andns-2/tcl/libs/ns-node.tcl".
$ns duplex-link node1 node2 bandwidth delay queue-type: creates two simplex links of specified
bandwidth and delay, and connects the two specified nodes. In NS, the output queue of a node is
implemented as a part of a link, therefore users should specify the queue-type when creating links. In
the above simulation script, DropTail queue is used. If the reader wants to use a RED queue, simply
replace the word DropTail with RED. The NS implementation of a link is shown in a later section.
Like a node, a link is a compound object, and users can create its sub-objects and connect them and
the nodes. Link source codes can be found in "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl"
files. One thing to note is that you can insert error modules in a link component to simulate a lossy
link (actually users can make and insert any network objects). Refer to the NS documentation to find
out how to do this.
$ns queue-limit node1 node2 number: This line sets the queue limit of the two simplex links that
connect node1 and node2 to the number specified. At this point, the authors do not know how many
of these kinds of member functions of Simulator objects are available and what they are. Please take a
look at "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl", or NS.
$ns duplex-link-op node1 node2 ...: The next couple of lines are used for the NAM display. To see the
effects of these lines, users can comment these lines out and try the simulation.
Now that the basic network setup is done, the next thing to do is to setup traffic agents such as TCP
and UDP, traffic sources such as FTP and CBR, and attach them to nodes and agents respectively.
set tcp [new Agent/TCP]: This line shows how to create a TCP agent. But in general, users can create
any agent or traffic sources in this way. Agents and traffic sources are in fact basic objects (not
compound objects), mostly implemented in C++ and linked to OTcl. Therefore, there are no specific
Simulator object member functions that create these object instances.
To create agents or traffic sources, a user should know the class names these objects (Agent/TCP,
Agnet/TCPSink, Application/FTP and so on). This information can be found in the NS documentation
or partly in this documentation. But one shortcut is to look at the "ns2/tcl/libs/ns-default.tcl" file. This
file contains the default configurable parameter value settings for available network objects.
Therefore, it works as a good indicator of what kind of network objects are available in NS and what
are the configurable parameters.
$ns attach-agent node agent: The attach-agent member function attaches an agent object created to a
node object. Actually, what this function does is call the attach member function of specified node,
which attaches the given agent to itself. Therefore, a user can do the same thing by, for example, $n0
attach $tcp. Similarly, each agent object has a member function.
$ns connect agent1 agent2: After two agents that will communicate with each other are created, the
next thing is to establish a logical network connection between them. This line establishes a network
connection by setting the destination address to each others' network and port address pair.
27
EC3401- Networks and Security Department of ECE 2022 - 2023
Assuming that all the network configuration is done, the next thing to do is write a simulation
scenario (i.e. simulation scheduling). The Simulator object has many scheduling member functions.
However, the one that is mostly used in the following:$ns at time "string": This member function of a
Simulator object makes the scheduler (scheduler_ is the variable that points the scheduler object
created by [new Scheduler] command at the beginning of the script) to schedule the execution of the
specified string at given simulation time.
After all network configuration, scheduling and post-simulation procedure specifications are done, the
only thing left is to run the simulation. This is done by $ns run.
RESULT:
28
EC3401- Networks and Security Department of ECE 2022 - 2023
SOFTWARE REQUIREMENTS:
NS-2 Simulator
THEORY:
Stop and Wait is a reliable transmission flow control protocol. This protocol works only in
Connection Oriented (Point to Point) Transmission. The Source node has window size of ONE. After
transmission of a frame the transmitting (Source) node waits for an Acknowledgement from the
destination node. If the transmitted frame reaches the destination without error, the destination
transmits a positive acknowledgement. If the transmitted frame reaches the Destination with error, the
receiver destination does not transmit an acknowledgement. If the transmitter receives a positive
acknowledgement it transmits the next frame if any. Else if its acknowledgement receive timer
expires, it retransmits the same frame.
1. Start with the window size of 1 from the transmitting (Source) node
2. After transmission of a frame the transmitting (Source) node waits for a reply (Acknowledgement)
from the receiving (Destination) node.
3. If the transmitted frame reaches the receiver (Destination) without error, the receiver (Destination)
transmits a Positive Acknowledgement.
4. If the transmitted frame reaches the receiver (Destination) with error, the receiver (Destination) do
not transmit acknowledgement.
5. If the transmitter receives a positive acknowledgement it transmits the next frame if any. Else if
the transmission timer expires, it retransmits the same frame again.
6. If the transmitted acknowledgment reaches the Transmitter (Destination) without error, the
Transmitter (Destination) transmits the next frame if any.
7. If the transmitted frame reaches the Transmitter (Destination) with error, the Transmitter
(Destination) transmits the same frame.
8. This concept of the Transmitting (Source) node waiting after transmission for a reply from the
receiver is known as STOP and WAIT.
29
EC3401- Networks and Security Department of ECE 2022 - 2023
Algorithm
30
EC3401- Networks and Security Department of ECE 2022 - 2023
OUTPUT:
RESULT:
31
EC3401- Networks and Security Department of ECE 2022 - 2023
AIM:
To Simulate and to study of Go Back N protocol
SOFTWARE REQUIREMENTS:
NS-2 Simulator
THEORY: Go Back N is a connection oriented transmission. The sender transmits the frames
continuously. Each frame in the buffer has a sequence number starting from 1 and increasing up to the
window size. The sender has a window i.e. a buffer to store the frames. This buffer size is the number
of frames to be transmitted continuously. The size of the window depends on the protocol designer.
OPERATIONS:
32
EC3401- Networks and Security Department of ECE 2022 - 2023
33
EC3401- Networks and Security Department of ECE 2022 - 2023
OUTPUT:
RESULT:
34
EC3401- Networks and Security Department of ECE 2022 - 2023
EX.NO: Date:
SIMULATION OF SELECTIVE REPEAT
PROTOCOLS
35
EC3401- Networks and Security Department of ECE 2022 - 2023
8. If acknowledgment is transmitted with error, all the frames of window are retransmitted. Else
ordinary window sliding takes place. (* In implementation part, Acknowledgment error is not
considered)
9. If all the frames transmitted are errorless the next transmission is carried out for the new window.
10. This concept of repeating the transmission for the error frames only is called Selective Repeat
transmission flow control protocol.
11. If there is an error in frame 3, an acknowledgement for the frame 2 is sent and then only Frame 3
is retransmitted. Now the window slides to get the next frames to the window.
12. If acknowledgment is transmitted with error, all the frames of window are retransmitted. Else
ordinary window sliding takes place. (* In implementation part, Acknowledgment error is not
considered)
13. If all the frames transmitted are errorless the next transmission is carried out for the new window.
15. This concept of repeating the transmission for the error frames only is called Selective Repeat
transmission flow control protocol.
#PROGRAM FOR SELECTIVE REPEAT:
#send packets one by one
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$n0 color "red"
$n1 color "red"
$n2 color "green"
$n3 color "green"
$n4 color "black"
$n5 color "black"
$n0 shape circle ;
$n1 shape circle ;
$n2 shape circle ;
$n3 shape circle ;
$n4 shape circle ;
$n5 shape circle ;
$ns at 0.0 "$n0 label SYS1"
$ns at 0.0 "$n1 label SYS2"
$ns at 0.0 "$n2 label SYS3"
$ns at 0.0 "$n3 label SYS4"
$ns at 0.0 "$n4 label SYS5"
$ns at 0.0 "$n5 label SYS6"
set nf [open Srepeat.nam w]
$ns namtrace-all $nf
set f [open Srepeat.tr w]
36
EC3401- Networks and Security Department of ECE 2022 - 2023
$ns trace-all $f
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-down
$ns queue-limit $n0 $n2 5
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link $n3 $n5 1Mb 10ms DropTail
$ns duplex-link-op $n3 $n5 orient right-down
Agent/TCP set_nam_tracevar_true
set tcp [new Agent/TCP]
$tcp set fid 1
$ns attach-agent $n1 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 0.05 "$ftp start"
$ns at 0.06 "$tcp set windowlnit 8"
$ns at 0.06 "$tcp set maxcwnd 8"
$ns at 0.25 "$ns queue-limit $n3 $n4 0"
$ns at 0.26 "$ns queue-limit $n3 $n4 10"
$ns at 0.30 "$tcp set windowlnit 1"
$ns at 0.30 "$tcp set maxcwnd 1"
$ns at 0.30 "$ns queue-limit $n3 $n4 10"
$ns at 0.47 "$ns detach-agent $n1 $tcp;$ns detach-agent $n4 $sink"
$ns at 1.75 "finish"
$ns at 0.0 "$ns trace-annotate \"Select and repeat\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.01\""
$ns at 0.06 "$ns trace-annotate \"Send 8Packets from SYS1 to SYS4\""
$ns at 0.26 "$ns trace-annotate \"Error Occurs in 4th packet \""
$ns at 0.30 "$ns trace-annotate \"Retransmit Packet_4 from SYS1 to SYS4\""
$ns at 1.5 "$ns trace-annotate \"FTP stops\""
proc finish {} {
global ns nf
$ns flush-trace
close $nf
puts "filtering..."
#exec tclsh../bin/namfilter.tcl srepeat.nam
#puts "running nam..."
exec nam Srepeat.nam &
exit 0
}
$ns run
OUTPUT:
RESULT:
REVIEW QUESTIONS
1. What do you mean by Selective Repeat flow Control?
2. What is size of the send and receive window in Selective repeat protocol?
37
EC3401- Networks and Security Department of ECE 2022 - 2023
REQUIREMENTS:
NS-2.28
Computer with windows XP
ALGORITHM:
Step 1: Start the program
Step 2: Create the trace file and NAM file.
Step 3: Setup the topology object
Step 4: Create nodes and set duplex links between the nodes.
Step 5: Create nodes and attach them to the queue, DROPTAIL
Step 6: Configure the nodes and provides initial location of nodes. Generation of movements
is done.
Step 7: Setup a TCP Connection between nodes
Step 8: Define a initialize positions for the nam window.
Step 9: Telling nodes when the simulation ends
Step 10: Ending nam and the simulation
PROGRAM:
Distance vector
38
EC3401- Networks and Security Department of ECE 2022 - 2023
OUTPUT:
RESULT:
39
EC3401- Networks and Security Department of ECE 2022 - 2023
EX. NO: 9
Date:
IMPLEMENT AND REALIZE THE NETWORK TOPOLOGY
STAR, BUS AND RING USING NS2.
AIM:
To implement and realize the network topologies.
REQUIREMENTS:
NS-2.28
Computer with windows XP
ALGORITHM:
Step 1: Start the program
Step 2: Create the trace file and NAM file.
Step 3: Setup the topology object
Step 4: Create nodes and set duplex links between the nodes.
Step 5: Create nodes and attach them to the queue, DROPTAIL
Step 6: Configure the nodes and provides initial location of nodes. Generation of movements
is done.
Step 7: Setup a TCP Connection between nodes
Step 8: Define a initialize positions for the nam window.
Step 9: Telling nodes when the simulation ends
Step 10: Ending nam and the simulation
#Create a simulator object
set ns [new Simulator]
40
EC3401- Networks and Security Department of ECE 2022 - 2023
BUS TOPOLOGY:
#Creating five nodes
set node1 [$ns node]
setnode2 [$ns node]
set node3 [$ns node]
setnode4 [$ns node]
set node5 [$ns node]
#Creating Lan connection between the nodes
set lan0 [$ns newLan “$node1 $node2$node3 $node4 $node5” 0.7Mb 20ms LL Queue/FQ MAC/
Csma/Cd Channel]
#Creating a TCP agent and attaching it to node 1
set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $node1 $tcp0
#Creating a TCP Sink agent for TCP and attaching it to node 3
set sink0 [new Agent/TCPSink]
$ns attach-agent $node3 $sink0
#Connecting the traffic sources with the traffic sink
41
EC3401- Networks and Security Department of ECE 2022 - 2023
$ns connect $tcp0 $sink0
# Creating a CBR traffic source and attach it to tcp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$ cbr0 set interval_ 0.05
$cbr0 attach-agent $tcp0
#Schedule events for the CBR agents
$ns at 0.5 “$cbr0 start time”
$ ns at 5.5 “$cbr0 stop time”
#Here we call the finish procedure after 10 seconds of simulation time
$ns at 10.0 “End”
#Finally run the simulation
$ns run
RING TOPOLOGY:
#Creating six nodes
setnode1 [$ns node]
set node2 [$ns node]
setnode3 [$ns node]
set node4 [$ns node]
setnode5 [$ns node]
set node6 [$ns node]
#Creating links between the nodes
$ ns duplex-link $node1 $node2 1Mb 15ms FQ
$ns duplex-link $node2 $node3 1Mb 15ms FQ
$ ns duplex-link $node3 $node4 1Mb 15ms FQ
$ns duplex-link $node4 $node5 1Mb 15ms FQ
$ ns duplex-link $node5 $node6 1Mb 15ms FQ
$ns duplex-link $node6 $node1 1Mb 15ms FQ\”Forms Ring Topology”
42
EC3401- Networks and Security Department of ECE 2022 - 2023
RESULT:
REVIEW QUESTIONS:
1. What is menat by topology
2. Define Ring topology
3. Define Star topology
4. Define Bus topology
5. Which is the suitable topology for real time network.
43