CCN Lab Manual1
CCN Lab Manual1
CCN Lab Manual1
Of E&C bldecet
CREDITS – 02
Course objectives: This course will enable students to:
Choose suitable tools to model a network and understand the protocols at various
OSI reference levels.
Design a suitable network and simulate using a Network simulator tool.
Simulate the networking concepts and protocols using C/C++ programming.
Model the networks for different configurations and analyze the results.
Laboratory Experiments
PART-A: Simulation experiments using NS2/ NS3/ OPNET/ NCTUNS/
NetSim/ QualNet or any other equivalent tool
1. Implement a point to point network with four nodes and duplex links between
them. Analyze the network performance by setting the queue size and varying
the bandwidth.
2. Implement a four node point to point network with links n0-n2, n1-n2 and n2-
n3. Apply TCP agent between n0-n3 and UDP between n1-n3. Apply relevant
applications over TCP and UDP agents changing the parameter and determine
the number of packets sent by TCP/UDP.
104
3. Implement Dijkstra’s algorithm to compute the shortest routing path.
4. For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify
the program for the cases
a. Without error
b. With error
5. Implementation of Stop and Wait Protocol and Sliding Window Protocol
6. Write a program for congestion control using leaky bucket algorithm.
Course outcomes: On the completion of this laboratory course, the students will be
able to:
Use the network simulator for learning and practice of networking algorithms.
Illustrate the operations of network protocols and algorithms using
C programming.
NOTE:awk -f programme name.awk programme name.tr (To generate o/p from trace
file)
ABID H SYED dept. Of E&C bldecet
Programme1:
//Implement a point to point network with four nodes and duplex links between them.
Analyze the network performance by setting the queue size and varying the bandwidth.
prog1.tcl
set ns [new Simulator]
set nf [open prog1.nam w]
$ns namtrace-all $nf
set nd [open prog1.tr w]
$ns trace-all $nd
proc finish { } {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog1.nam &
exit 0
}
prog1.awk
BEGIN {
dcount = 0;
rcount = 0;
}
{
event = $1;
if(event == "d")
{
dcount++;
}
if(event == "r")
{
rcount++;
}
}
END {
printf("The no.of packets dropped : %d\n ",dcount);
printf("The no.of packets recieved : %d\n ",rcount);
}
ABID H SYED dept of E&Cbldecet
Programme2:
//Implement a four node point to point network with links n0-n2, n1-n2 and n2-n3. Apply
TCP agent between n0-n3 and UDP between n1-n3. Apply relevant applications over TCP
and UDP agents changing the parameter and determine the number of packets sent by
TCP/UDP.
prog2.tcl
set ns [new Simulator]
set nf [open prog2.nam w]
$ns namtrace-all $nf
set nd [open prog2.tr w]
$ns trace-all $nd
proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
exec nam prog2.nam &
exit 0
}
prog2.awk
BEGIN {
ctcp=0;
cudp=0;
}
{
pkt=$5;
if(pkt=="cbr") { cudp++;}
if(pkt=="tcp") { ctcp++;}
}
END {
printf("No of packets sent\nTcp : %d\nUdp : %d\n",ctcp,cudp);
}
ABID H SYED dept of E&Cbldecet
Programme3:
Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by
changing the error rate and data rate.
set ns [new Simulator]
set nf [open prog5.nam w]
$ns namtrace-all $nf
set nd [open prog5.tr w]
$ns trace-all $nd
proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog5.nam &
exit 0
}
$ns make-lan "$n0 $n1 $n2 $n3 $n4 $n5 $n6" 0.2Mb 40ms LL Queue/DropTail
Mac/802_3
prog3.awk
BEGIN {
ABID H SYED dept of E&Cbldecet
sSize=0;
startTime = 5.0;
stopTime = 0.1;
Tput = 0;
}
{
event = $1;
time = $2;
from = $3;
to = $4;
pkt = $5;
size = $6;
fid = $7;
src = $8;
dst = $9;
seqn = $10;
pid = $11;
if (event == "+") {
if(time < startTime) {
startTime = time;
}
}
if (event == "r") {
if(time > stopTime) {
stopTime = time;
}
sSize+=size;
}
Tput = (sSize/(stopTime-startTime))*(8/1000);
printf("%f\t%.2f\n",time,Tput);
}
END {
}
$ns make-lan "$n0 $n1 $n2 $n3" 100Mb 300ms LL Queue/ DropTail Mac/802_3
$ns make-lan "$n4 $n5 $n6 $n7" 100Mb 300ms LL Queue/ DropTail Mac/802_3
$ns duplex-link $n3 $n4 100Mb 300ms DropTail
$ns duplex-link-op $n3 $n4 color "green"
# set error rate. Here ErrorModel is a class and it is single word and space should
not be given between Error and Model
# lossmodel is a command and it is single word. Space should not be given between
loss and model
set err [new ErrorModel]
proc finish { } {
ABID H SYED dept of E&Cbldecet
global ns nf tf
$ns flush-trace
close $nf
close $tf
exit 0 }
$ns run
AWK file: (Open a new editor using “vi command” and write awk file and save with
“.awk” extension)
BEGIN{
pkt=0;
time=0;
}
{
if($1= ="r" && $3= ="9" && $4= ="7"){
pkt = pkt + $6;
time =$2;
}
}
END {
printf("throughput:%fMbps",(( pkt / time) * (8 / 1000000)));
ABID H SYED dept of E&Cbldecet
Programme4:
Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes
and obtain congestion window for different sources/ destinations.
set ns [new Simulator]
set nf [open prog7.nam w]
$ns namtrace-all $nf
set nd [open prog7.tr w]
$ns trace-all $nd
proc finish { } {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog7.nam &
exit 0
}
$ns make-lan "$n3 $n4 $n5 $n6 $n7 $n8" 512Kb 40ms LL Queue/DropTail Mac/802_3
prog4.awk
BEGIN {
}
{
if($6=="cwnd_") {
printf("%f\t%f\n",$1,$7);
}
}
END {
}
ABID H SYED dept of E&Cbldecet
Programme5:
Implement ESS with transmission nodes in Wireless LAN and obtain the
performance parameters.
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
ABID H SYED dept of E&Cbldecet
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam
lab4.nam &
close $tf
exit 0
}
$ns at 250 "finish"
$ns run
ABID H SYED dept of E&Cbldecet
AWK file
BEGIN{
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1= ="r"&& $3= ="_1_" && $4= ="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1= ="r" && $3= ="_2_" && $4= ="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %f Mbps \n”, ((count1*pack1*8)/
(time1*1000000)));
printf("The Throughput from n1 to n2: %f Mbps", ((count2*pack2*8)/
(time2*1000000)));
}
ABID H SYED dept of E&Cbldecet
Programme6:
Implementation of Link state routing algorithm
set ns [new Simulator]
proc finish {} {
global nf ns tr
$ns flush-trace
close $tr
exit 0
$ns rtproto LS
$ns run
ABID H SYED dept of E&Cbldecet
PART-B
Security and Error detection are the most prominent features that are to be provided by
any application which transfers data from one end to the other end. One of such a
mechanism in tracking errors which may add up to the original data during transfer is
known as Stuffing. It is of two types namely Bit Stuffing and the other Character
Stuffing. Coming to the Bit Stuffing, a flag(ex01111110) is appended within the original
data while transfer of it. The following program describes how it is stuffed at the sender
end and de-stuffed at the reciever end
C Programme
#include<stdio.h>
#include<string.h>
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;
clrscr();
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);
getch();
ABID H SYED dept of E&Cbldecet
Output:-
Enter characters to be stuffed: goodday
Enter a character that represents starting delimiter: d
Enter a character that represents ending delimiter: g
After stuffing: dggooddddayg.
int main()
{
char *p,*q;
char temp;
char in[MAXSIZE];
char stuff[MAXSIZE];
char destuff[MAXSIZE];
int count=0;
p=in;
q=stuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
ABID H SYED dept of E&Cbldecet
q++;
p++;
}
if(count==5)
{
*q='0';
q++;
}
count=0;
}
}
*q='\0';
printf("\nthe stuffed character string is");
printf("\n%s",stuff);
p=stuff;
q=destuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}
if(count==5)
{
p++;
}
count=0;
}
}
*q='\0';
printf("\nthe destuffed character string is");
printf("\n%s\n",destuff);
return 0;
}
ABID H SYED dept of E&Cbldecet
Output:
1 0 1 0 1 1 1 1 1 1
ABID H SYED dept. Of E&C bldecet
This is first padded with zeroes corresponding to the bit length n of the CRC. Here is the
first calculation for computing a 3-bit CRC:
11010011101100 000 <--- input right padded by 3 bits
1011 <--- divisor (4 bits) = x³+x+1
------------------
01100011101100 000 <--- result
If the input bit above the leftmost divisor bit is 0, do nothing. If the input bit above the
leftmost divisor bit is 1, the divisor is XORed into the input (in other words, the input bit
above each 1-bit in the divisor is toggled). The divisor is then shifted one bit to the right,
and the process is repeated until the divisor reaches the right-hand end of the input row.
Here is the entire calculation:
11010011101100 000 <--- input right padded by 3 bits
1011 <--- divisor
01100011101100 000 <--- result
1011 <--- divisor ...
00111011101100 000
1011
00010111101100 000
1011
00000001101100 000
1011
00000000110100 000
ABID H SYED dept. Of E&C bldecet
1011
00000000011000 000
1011
00000000001110 000
1011
00000000000101 000
101 1
-----------------
00000000000000 100 <---remainder (3 bits)
Since the leftmost divisor bit zeroed every input bit it touched, when this process ends the
only bits in the input row that can be nonzero are the n bits at the right-hand end of the
row. These n bits are the remainder of the division step, and will also be the value of the
CRC function (unless the chosen CRC specification calls for some postprocessing).
The validity of a received message can easily be verified by performing the above
calculation again, this time with the check value added instead of zeroes. The remainder
should equal zero if there are no detectable errors.
11010011101100 100 <--- input with check value
1011 <--- divisor
01100011101100 100 <--- result
1011 <--- divisor ...
00111011101100 100
......
00000000001110 100
1011
00000000000101 100
101 1
------------------
c programme
#include<stdio.h>
int a[100],b[100],i,j,len,k,count=0;
//Generator Polynomial:g(x)=x^16+x^12+x^5+1
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,};
int main()
{
void div();
//system("clear");
ABID H SYED dept. Of E&C bldecet
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
ABID H SYED dept. Of E&C bldecet
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
} while(count!=0);
for(i=0;i<n;i++)
{
printf("\n\nState value for router %d is
\n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d Distance
%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}
Example output
Enter the number of nodes : 4
In sliding window protocol the receiver has to have some memory to compensate any loss
in transmission or if the frames are received unordered.
Efficiency of Sliding Window Protocol
η = (W*tx)/(tx+2tp)
ABID H SYED dept. Of E&C bldecet
W = Window Size
tx = Transmission time
tp = Propagation delay
Sliding window works in full duplex mode
It is of two types:-
1. Selective Repeat: Sender transmits only that frame which is erroneous or is lost.
2. Go back n: Sender transmits all frames present in the window that occurs after the
error bit including error bit also.
Output
Enter window size: 3
Enter number of frames to transmit: 5
Enter 5 frames: 12 5 89 4 6
With sliding window protocol the frames will be sent in the following manner (assuming
no corruption of frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the
receiver
12 5 89
Acknowledgement of above frames sent is received by sender
46
Acknowledgement of above frames sent is received by sender
ABID H SYED dept. Of E&C bldecet
Description
The congesting control algorithms are basically divided into two groups: open loop and
closed loop. Open loop solutions attempt to solve the problem by good design, in
essence, to make sure it does not occur in the first place. Once the system is up and
running, midcourse corrections are not made. Open loop algorithms are further divided
into ones that act at source versus ones that act at the destination.
In contrast, closed loop solutions are based on the concept of a feedback loop if there is
any congestion. Closed loop algorithms are also divided into two sub categories: explicit
feedback and implicit feedback. In explicit feedback algorithms, packets are sent back
from the point of congestion to warn the source. In implicit algorithm, the source deduces
the existence of congestion by making local observation, such as the time needed for
acknowledgment to come back.
The presence of congestion means that the load is (temporarily) greater than the resources
(in part of the system) can handle. For subnets that use virtual circuits internally, these
methods can be used at the network layer.
Another open loop method to help manage congestion is forcing the packet to be
transmitted at a more predictable rate. This approach to congestion management is widely
used in ATM networks and is called traffic shaping.
The other method is the leaky bucket algorithm. Each host is connected to the network by
an interface containing a leaky bucket, that is, a finite internal queue. If a packet arrives
at the queue when it is full, the packet is discarded. In other words, if one or more process
are already queued, the new packet is unceremoniously discarded. This arrangement can
be built into the hardware interface or simulate d by the host operating system. In fact it is
nothing other than a single server queuing system with constant service time.
The host is allowed to put one packet per clock tick onto the network. This mechanism
turns an uneven flow of packet from the user process inside the host into an even flow of
packet onto the network, smoothing out bursts and greatly reducing the chances of
congestion.
Algorithm:
1. Start
2. Set the bucket size or the buffer size.
3. Set the output rate.
ABID H SYED dept. Of E&C bldecet
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define NOF_PACKETS 10
int rand(int a)
{
int rn = (random() % 10) % a;
return rn == 0 ? 1 : rn;
}
int main()
{
int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
for(i = 0; i<NOF_PACKETS; ++i)
packet_sz[i] = rand(6) * 10;
for(i = 0; i<NOF_PACKETS; ++i)
printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d", &o_rate);
printf("Enter the Bucket Size:");
scanf("%d", &b_size);
for(i = 0; i<NOF_PACKETS; ++i)
{
if( (packet_sz[i] + p_sz_rm) > b_size)
if(packet_sz[i] > b_size)/*compare the packet siz with bucket size*/
printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity
(%dbytes)-PACKET REJECTED", packet_sz[i], b_size);
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
else
{
p_sz_rm += packet_sz[i];
printf("\n\nIncoming Packet size: %d", packet_sz[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_rm);
ABID H SYED dept. Of E&C bldecet
Dijkstra's algorithm
Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a
graph, which may represent, for example, road networks. It was conceived by computer
scientist Edsger W. Dijkstra in 1956 and published three years later.
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like
Prim’s MST, we generate a SPT (shortest path tree) with given source as root. We
maintain two sets, one set contains vertices included in shortest path tree, other set
includes vertices not yet included in shortest path tree. At every step of the algorithm, we
find a vertex which is in the other set (set of not yet included) and has minimum distance
from source.
ABID H SYED dept. Of E&C bldecet
Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from a
single source vertex to all other vertices in the given graph.
Algorithm
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in
shortest path tree, i.e., whose minimum distance from source is calculated and finalized.
Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values
as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSetand has minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance values,
iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value
of u (from source) and weight of edge u-v, is less than the distance value of v, then update
the distance value of v.
Let us understand with the following example:
The set sptSetis initially empty and distances assigned to vertices are {0, INF, INF, INF,
INF, INF, INF, INF} where INF indicates infinite. Now pick the vertex with minimum
distance value. The vertex 0 is picked, include it in sptSet. So sptSet becomes {0}. After
including 0 to sptSet, update distance values of its adjacent vertices. Adjacent vertices of
0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and 8. Following subgraph
shows vertices and their distance values, only the vertices with finite distance values are
shown. The vertices included in SPT are shown in green color.
ABID H SYED dept. Of E&C bldecet
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). The vertex 1 is picked and added to sptSet. So sptSet now becomes {0, 1}.
Update the distance values of adjacent vertices of 1. The distance value of vertex 2
becomes 12.
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}. Update the distance values
of adjacent vertices of 7. The distance value of vertex 6 and 8 becomes finite (15 and 9
respectively).
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}. Update the distance
values of adjacent vertices of 6. The distance value of vertex 5 and 8 are updated.
ABID H SYED dept. Of E&C bldecet
We repeat the above steps until sptSet doesn’t include all vertices of given graph. Finally,
we get the following Shortest Path Tree (SPT).
{
printf("\nEnter destination vertex(-1 to quit): ");
scanf("%d",&v);
if(v == -1)
break;
if(v < 0 || v >= n )
printf("\nThis vertex does not exist\n");
else if(v == s)
printf("\nSource and destination vertices are same\n");
else if( pathLength[v] == infinity )
printf("\nThere is no path from source to destination vertex\n");
else
findPath(s,v);
}
return 0;
}/*End of main()*/
void Dijkstra( int s)
{
int i,current;
/* Make all vertices temporary */
for(i=0; i<n; i++)
{
predecessor[i] =NIL;
pathLength[i] = infinity;
status[i] = TEMP;
}
/* Make pathLength of source vertex equal to 0 */
pathLength[s] = 0;
while(1)
{
/*Search for temporary vertex with minimum pathLength
and make it current vertex*/
current = min_temp( );
if( current == NIL )
return;
status[current] = PERM;
for(i=0; i<n; i++)
{
/*Checks for adjacent temporary vertices */
if ( adj[current][i] !=0 && status[i] == TEMP )
if( pathLength[current] + adj[current][i] < pathLength[i] )
{
predecessor[i] = current;/*Relabel*/
pathLength[i] = pathLength[current] + adj[current][i];
ABID H SYED dept. Of E&C bldecet
}
}
}
}/*End of Dijkstra( )*/
/*Returns the temporary vertex with minimum value of pathLength
Returns NIL if no temporary vertex left or
all temporary vertices left have pathLength infinity*/
int min_temp( )
{
int i;
int min = infinity;
int k = NIL;
for(i=0;i<n;i++)
{
if(status[i] == TEMP && pathLength[i] < min)
{
min = pathLength[i];
k = i;
}
}
return k;
}/*End of min_temp( )*/
void findPath(int s, int v )
{
int i,u;
int path[MAX]; /*stores the shortest path*/
int shortdist = 0; /*length of shortest path*/
int count = 0; /*number of vertices in the shortest path*/
/*Store the full path in the array path*/
while( v != s )
{
count++;
path[count] = v;
u = predecessor[v];
shortdist += adj[u][v];
v = u;
}
count++;
path[count]=s;
printf("\nShortest Path is : ");
for(i=count; i>=1; i--)
printf("%d",path[i]);
printf("\nShortest distance is : %d\n", shortdist);
}/*End of findPath()*/
ABID H SYED dept. Of E&C bldecet
void create_graph()
{
int i,max_edges,origin,destin, wt;
printf("\nEnter number of vertices : ");
scanf("%d",&n);
max_edges = n*(n-1);
for(i=1;i<=max_edges;i++)
{
printf("\nEnter edge %d( -1 -1 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin == -1) && (destin == -1) )
break;
printf("\nEnter weight for this edge : ");
scanf("%d",&wt);
if( origin >= n || destin >= n || origin<0 || destin<0)
{
printf("\nInvalid edge!\n");
i--;
}
else
adj[origin][destin] = wt;
}}