III CSE_1_ Computer Network Lab Manual R18[1]
III CSE_1_ Computer Network Lab Manual R18[1]
INSTITUTE
Parvathapur, Uppal, Hyderabad-98
LAB MANUAL
B. Tech – III Year
R18 Regulations
NAME : _________________________________________
ROLL NO :__________________________________
BRANCH :______________________________________
Page: 1
INDEX
1 Lab Objective 3
2 Guidelines to Students 4
Page: 2
LAB OBJECTIVE
Course Description
Learning Objectives
Being a graduate course, the objective of this course is not simply to introduce a long
succession of protocols. It is, rather, to provide students with the ability to become an acting
contributor in the field of computer networking, both from a developer perspective and from
a research perspective.
Page: 3
Guidelines to Students
Equipment in the lab for the use of student community. Students need to maintain a
proper decorum in the computer lab. Students must use the equipment with care. Any
damage is caused is punishable.
Students are required to carry their observation / programs book with completed
exercises while entering the lab.
Students are supposed to occupy the machines allotted to them and are not supposed
to talk or make noise in the lab. The allocation is put up on the lab notice board.
Lab can be used in free time / lunch hours by the students who need to use the
systems should take prior permission from the lab in-charge.
Page: 4
List of Syllabus Programs ( JNTUH )
Wireshark
Page: 5
13 How to run Nmap scan 74
15 i. NS2 simulator-introduction 82
ii. Simulate to find the number of packets dropped 83
Page: 6
Lab Program: 1
Implement the data link layer framing method such as bit stuffing.
Algorithm:
1. start
2. Read a bit string
3. Trace 5 bits in the string
4. Check if 5 bits are 11111 then shift a ‘0’ bit after it
5. Repeat 3 & 4 until end of string
6. Stop
Page: 7
FLOWCHART
Start
Bits are
11111
End of string
Stop
Page: 8
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
clrscr();
printf("enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a)-4;i++)
{
for(j=i;j<i+5;j++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<strlen(a);q++)
{
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}
Page: 9
OUTPUT
Page: 10
Lab Program: 2
Implement the data link layer framing method such as Character Stuffing.
Algorithm:
1. Start
6. Stop
Page: 11
FLOWCHART
Start
Character is same
as starting or
ending delimiter
End of string
Stop
Page: 12
PROGRAM
#include<stdio.h>
#include<string.h>
#include<conio.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("\nAfter stuffing : %s",fs);
getch();
}
Page: 13
OUTPUT
Page: 14
Program: 3
Implement on a data set of characters the CRC Polynomial CRC 12.
Algorithm:
1. Start
2. Read value for both transmitted and received data
3. Concatenate 12 0’s at the transmitted data
4. Perform binary division where crc-12 generating polynomial, is the divisor and
dividend is concatenated string
5. If length of the remainder is 12 replace the 12 0s with the obtained crc otherwise add
required no of 0s to the beginning of the remainder such that length is 12
6. Perform binary division on this divisor as generating polynomial.
7. If remainder is 0 transmitted data is correct otherwise incorrect
Page: 15
FLOW CHART
Start
Printf message
Remainder==0 “data transmitted
incorrectly”
Printf message
“data transmitted
correctly”
Stop
Page: 16
PROGRAM
#include<stdio.h>
#include<stdio.h>
const char * bindiv(const char *,const char *);
const char * binsub(const char *,const char *);
int f=0,ll=0;
main()
{
char *a,p[30]="1100000001011",g[30],g1[30],yy[30]="",td[30],*aa;
int l=0,i;
clrscr();
printf("enter transfered data : ");
scanf("%s",g);
printf("enter received data : ");
scanf("%s",td);
strcpy(g1,g);
strcat(g,"000000000000");
printf("\n%s ) %s (",p,g);
a=bindiv(g,p);
if(strlen(a)<12)
{
for(i=strlen(a);i<12;i++)
{
yy[l++]='0';
}
yy[l]='\0';
}
strcat(yy,a);
strcat(g1,yy);
printf("\ncrc is %s",yy);
printf("\n ------------------");
strcat(td,yy);
printf("\n\n%s ) %s (",p,td);
ll=0;
aa=bindiv(td,p);
strcpy(a,aa);
printf("\n %s",a);
printf("\n -----------------");
if(f==1)
printf("\ndata transfered correctly");
else
printf("\ndata transfered incorrectly");
getch();
}
Page: 17
const char * bindiv(const char *s,const char *d)
{
int i,j,k=0,x=13,h,p=0,l;
char q[15]="",b[30],*w;
for(i=0;i<strlen(s);i++)
{
if((i+x)>strlen(s))
x=(i+x)-strlen(s)+1;
for(j=i;j<(i+x);j++)
{
b[k++]=s[j];
}
b[k]='\0';
if(ll!=0)
printf("\n %s",b);
ll=1;
if(strlen(b)==12)
{
break;
}
printf("\n %s",d);
printf("\n -----------------");
w=binsub(b,d);
k=0;i=j-1;
for(l=0;l<strlen(w);l++)
{
if(w[l]=='1')
break;
}
if(l==strlen(w))
{
f=1;
return(w);
}
for(h=l;h<strlen(w);h++)
{
q[p++]=w[h];
}
q[p]='\0';
x=13-strlen(q);
strcpy(b,"");
strcat(b,q);
k=strlen(q); p=0;
}
return(b);
}
Page: 18
const char * binsub(const char *x,const char *y)
{
int i,j=0;
char w[15]="",e[3],f[3],n[3];
e[0]='1';
e[1]='\0';
f[0]='0';
f[1]='\0';
for(i=0;i<strlen(x);i++)
{
if((x[i]=='1')&&(y[i]=='1'))
strcat(w,f);
else
if((x[i]=='0')&&(y[i]=='0'))
strcat(w,f);
else
strcat(w,e);
}
n[0]='\0';
n[1]='\0';
strcat(w,n);
return(w);
}
Page: 19
OUTPUT
Page: 20
Lab Program: 4
Implement on a data set of characters the CRC Polynomial CRC 16
Algorithm:
1. Start
2. Read value for both transmitted and received data
3. Concatenate 16 0’s at the transmitted data
4. Perform binary division where crc-16 generating polynomial, is the divisor and
dividend is concatenated string
5. If length of the remainder is 16 replace the 16 0s with the obtained crc otherwise add
required no of 0s to the beginning of the remainder such that length is 16
6. Perform binary division on this divisor as generating polynomial.
7. If remainder is 0 transmitted data is correct otherwise incorrect
Page: 21
FLOW CHART
Start
Stop
Page: 22
PROGRAM
#include<stdio.h>
const char * bindiv(const char *,const char *);
const char * binsub(const char *,const char *);
int f=0,ll=0;
main()
{
char *a,p[20]="10001000000100001",
g[30],g1[30],yy[30]="",td[30],*aa;
int l=0,i;
clrscr();
printf("enter transfered data : ");
scanf("%s",g);
printf("enter received data : ");
scanf("%s",td);
strcpy(g1,g);
strcat(g,"0000000000000000");
printf("\n%s ) %s (",p,g);
a=bindiv(g,p);
if(strlen(a)<16)
{
for(i=strlen(a);i<16;i++)
{
yy[l++]='0';
}
yy[l]='\0';
}
strcat(yy,a);
strcat(g1,yy);
printf("\n ------------------");
printf("\ncrc is %s",yy);
strcat(td,yy);
printf("\n\n%s ) %s (",p,td);
ll=0;
aa=bindiv(td,p);
strcpy(a,aa);
printf("\n %s",a);
printf("\n -----------------");
if(f==1)
printf("\ndata transfered correctly");
else
printf("\ndata transfered incorrectly");
getch();
}
const char * bindiv(const char *s,const char *d)
{
int i,j,k=0,x=17,h,p=0,l;
Page: 23
char q[25]="",b[30],*w;
for(i=0;i<strlen(s);i++)
{
if((i+x)>strlen(s))
x=(i+x)-strlen(s)+1;
for(j=i;j<(i+x);j++)
{
b[k++]=s[j];
}
b[k]='\0';
if(ll!=0)
printf("\n %s",b);
ll=1;
if(strlen(b)==16)
{
break;
}
printf("\n %s",d);
printf("\n -----------------");
w=binsub(b,d);
k=0;i=j-1;
for(l=0;l<strlen(w);l++)
{
if(w[l]=='1')
break;
}
if(l==strlen(w))
{
f=1;
return(w);
}
for(h=l;h<strlen(w);h++)
{
q[p++]=w[h];
}
q[p]='\0';
x=17-strlen(q);
strcpy(b,"");
strcat(b,q);
k=strlen(q); p=0;
}
return(b);
}
const char * binsub(const char *x,const char *y)
{
int i,j=0;
char w[25]="",e[3],f[3],n[3];
Page: 24
e[0]='1';
e[1]='\0';
f[0]='0';
f[1]='\0';
for(i=0;i<strlen(x);i++)
{
if((x[i]=='1')&&(y[i]=='1'))
strcat(w,f);
else
if((x[i]=='0')&&(y[i]=='0'))
strcat(w,f);
else
strcat(w,e);
}
n[0]='\0';
n[1]='\0';
strcat(w,n);
return(w); }
Page: 25
OUTPUT
Page: 26
Lab Program: 5
Develop a simple data link layer that performs the flow control using the sliding window
protocol,and loss recovery using Go-Back-N mechanism.
PROGRAM
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by
the receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}
Page: 27
Page: 28
Lab Program: 6
Implement Dijkstra’s algorithm to compute the shortest path through a graph.
Algorithm:
1. Start
2. Read data from the user:
nodes=no. of nodes;
dsp[nodes][nodes]=adjacency matrix of the graph;
src=source;
dest=destination
3. Two struct arrays permanent, temp are used to store the permanent and tentative
nodes. A function sort is used to sort the temp array in decreasing order.
4. permanent [0].src=src;
permanent[0].dest=src;
permanent[0].length=0;
5. Store all the neighbouring nodes to src in temp
6. while temp array is not null, then A otherwise for the node recently added to
permanent list,
7. Find the neighbouring nodes.
8. if a node is not already present in the temp add the node to the temp array , otherwise
if the length of the node is smaller than the previous value then update the temp to
the new value, else ignore the new value
9. find the destination node in the permanent array. trace the path to the source and store
it an array.
10. Display the shortest path from src to dest and the total delay to reach the dest.
11. sort temp each item is compared to all the other items.
12. if the item is lesser than swap place the last item i.e., the node with less delay in the
permanent array
13. Return
Page: 29
FLOW CHART
Start
Src=Source
Dest=Destination
Permanent[0].src=src;
Permanent[0].dest=src;
permanent[0].length=0;
SORT
While temp
A
array is not
if a node is not
already present in the
temp
B C
Page: 30
B C
SORT
END
SORT
SORT
Page: 31
PROGRAM
#include<stdio.h>
void sort(void);
void main()
{
int i,j,k,l,m,n=0,point;
char initial,dest,path[10]={' '};
clrscr();
printf("\t\t Shortest Path (Dijkstra's algorithm)");
printf("\n*******************************************************");
printf("\nEnter the number of nodes:");
scanf("%d",&nodes);
printf("\nEnter the adjacency matrix for the graph:\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
scanf("%d",&dsp[i][j]);
}
fflush(stdin);
printf("\n enter the source node:");
scanf("%c",&initial);fflush(stdin);
printf("\n Enter the destination node:");
scanf("%c",&dest);
permanent[perm].src=initial;
permanent[perm].dest=initial;
permanent[perm++].length=0;
i=permanent[perm-1].dest-97;
for(j=0;j<nodes;j++)
{
if(i!=j)
{
if(dsp[i][j]>0)
{
temp[tem].src=permanent[perm-1].src;
temp[tem].dest=j+97;
temp[tem++].length=dsp[i][j];
}
Page: 32
}
}
sort();
while(tem>=0)
{
j=permanent[perm-1].dest-97;
for(i=0;i<nodes;i++)
{
if(i!=initial-97)
{
if(dsp[j][i]>0)
{
l=-1;
for(k=0;k<perm;k++)
{
if(permanent[k].dest==(i+97))
l=k;
}
for(k=0;k<=tem;k++)
{
if(temp[k].dest==(i+97))
l=k;
}
if(l<0)
{
temp[tem].src=j+97;
temp[tem].dest=i+97;
for(m=0;m<perm;m++)
{
if(permanent[m].dest==temp[tem].src)
n=permanent[m].length;
}
temp[tem++].length=dsp[j][i]+n;
}
else
{
for(m=0;m<perm;m++)
{
if(permanent[m].dest==j+97)
{
n=permanent[m].length+dsp[j][i];break;
}
else
n=dsp[j][i];
}
if((n<temp[l].length))
{
Page: 33
temp[l].length=n;
temp[l].src=j+97;
temp[l].dest=i+97;
}} }
}
}
sort();
}
printf("\nShortest path:\n");
printf("From %c to %c is:",initial,dest);
for(i=0;i<perm-1;i++)
{
if(permanent[i].dest==dest)
{
point=i;n=i; break;
} } i=0;
for(j=perm;j>0;j--)
{
if(permanent[j-1].dest==permanent[point].src)
{
path[i++]=permanent[point].dest;
point=j-1;
}}
path[i]=initial;
for(j=i;j>=0;j--)
printf("%c ",path[j]);
printf("\t length=%d",permanent[n].length);
getch();
}
void sort()
{
int i,j,k;
for(i=0;i<=tem;i++)
{
k=1;
for(j=0;j<=tem;j++)
{
if((temp[j].length <= temp[j+1].length))
{
stemp=temp[j];
temp[j]=temp[j+1];
temp[j+1]=stemp; k=0;
} }
if(k)
break;
}
permanent[perm++]=temp[tem-1];
Page: 34
temp[tem-1].src=' ';temp[tem-1].dest=' ';
temp[tem-1].length=-1; tem--;
}
OUTPUT:
Network topology:
------------------------ 1
1 b c 1
a 1 e 3 f
2 2
d
Output of execution1:
----------------------------
Page: 35
Network Topology:
B 7 C
2 2 3 3
A E 2 F D
6 1 2 2
G 4 H
Output of execution2:
----------------------------
Page: 36
Lab Program: 7
Take an example subnet of hosts. Obtain broadcast tree for it.
Algorithm:
1. Start
2. Read data from user
No. of nodes of graph : n
Adjacency matrix with weights :
Adj [n] [n], source for broadcast : src
3. If (i<n) then
4. If(j<n) then distance[j] = adj [root][j] otherwise Mini: min value in the distance[n]
5. If(k<n) then when distance[k]=mini otherwise go to 3
when distance[k]=mini
spantre[i][k]=mini
spantre[i][k]=mini otherwise spantre[i][k]=0
6. Count ! = y, If (i<n) , otherwise stop
8. spantre[root]
Page: 37
FLOW CHART
Start
Stop
Page: 38
PROGRAM
#include<stdio.h>
#include<conio.h>
int max();
int distance[20];
int n;
main()
{
int adj[20][20],adj1[20][20],flag[30];
int i,j,root,x;
int source,count=1,y=0;
clrscr();
printf("enter no of nodes");
scanf("%d",&n);
printf("Enter The adjecent list\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&adj[i][j]);
}
}
printf("enter the source for broadcasting");
scanf("%d",&source);
for(i=0;i<n;i++)
{
flag[i]=0;
}
for(root=0;root<n;root++)
{
for(i=0;i<n;i++)
{
distance[i]=adj[root][i];
}
x=min();
for(i=0;i<n;i++)
{
if(distance[i]==x)
{
adj1[root][i]=x;
adj1[i][root]=x;
}
else
{
adj1[root][i]=0;
}
}
}
Page: 39
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(adj1[i][j]!=0)
{
adj1[j][i]=adj[i][j];
}
}
}
printf("given adjacency matrix is");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d",adj[i][j]);
}
printf("\n");
}
printf("minimal spanning tree\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",adj1[i][j]);
}
printf("\n");
}
root=source;
flag[root]=1;
while(count!=y)
{
for(i=0;i<n;i++)
{
if(adj1[root][i]!=0 && flag[root]==1 && flag[i]!=1)
{
printf("%d sends message to %d \n",root,i);
flag[i]=1;
}
}
if(root<n-1)
{
root++;
}
else
{
root=0;
Page: 40
}
for(i=0;i<n;i++)
{
if(flag[i]==0)
{
break;
}
}
if(i==n)
{
count=y;
}
}
}
int min()
{
int i,j=0;
int mini;
int distance1[10];
for(i=0;i<n;i++)
{
if(distance[i]!=0)
{
distance1[j]=distance[i];
j++;
}
}
mini=distance1[0];
for(i=1;i<j;i++)
{
if(distance1[i]<mini)
{
mini=distance1[i];
}
}
return(mini);
Page: 41
OUTPUT
1
0 1
2 5
0 1 1
2 2
Page: 42
3 4 2
7 8 5
0 1
6
3 4 2
0 1
6
Page: 43
enter no of nodes5
enter the adjacency matrix
02050
20360
03079
56708
00980
enter the source for broadcasting2
given adjacency matrix is
02050
20360
03079
56708
00980
3
2
1
2
6 7 9
0
5 3 4
8
3 2
1
2
0
5 3 4
8
2 sends message to 1
1 sends message to 0
0 sends message to 3
3 sends message to 4
030400
704050
Page: 44
600507
000070
enter the source for broadcasting3
given adjacency matrix is
020760
203000
030400
704050
600507
000070
6 7
4 5
0
2 8 5
1 3
3 4
2
1 3
3 4
2
3 sends message to 2
3 sends message to 4
4 sends message to 5
2 sends message to 1
1 sends message to 0
Page: 45
Lab Program: 8
Take an example subnet graph with weights indicating delay between nodes.
Now obtain Routing table art each node using distance vector routing algorithm.
Algorithm:
1. Start
2. Read data from user
nodes=no.of nodes
src=node for which distance vector is to be computed;
neighb=no. of neighbours to src;
neigh[]={src,neighbouring node names};
topology[nodes][nodes]=distance vectors from all neighbours are
read from user and stored. for nodes that are not neighbours
infinity is stored as the value. dv[] stores the distance vector of src
3. For I = 0 To Nodes
4. if(I==neigh[n])
5. add1=topology[src][I];
add add1 to the distance vector of I, otherwise go to 3
5. compare the values in distace vector of the source.to that of I
6. If the value in distance vector of I less than the original then store the new value and
the line to use as I.
7. Display the distance vector of src
8. Stop
Page: 46
FLOW CHART
START
for(I=0;I<nodes;I++)
if(I==neigh[n])
yes
add1=topology[src][I];
add add1 to the distance vector of I
end
Page: 47
PROGRAM
#include<stdio.h>
#define e 10000
#define nodes1 40
int topology[nodes1][nodes1];
static int l,n,neighb,nodes;
static char neigh[nodes1];
struct {
char name;
int delay;
} dv[nodes1];
void main()
{
int i,j,k[nodes1*nodes1],add=0,src1;
char src='a';
clrscr();
printf("\t\t\tDistance Vector Routing\n");
printf("***********************************************************");
printf("\nEnter the number of nodes:");
scanf("%d",&nodes);fflush(stdin);
printf("\nEnter the node for which Distance Vector table is needed:");
scanf("%c",&src);fflush(stdin);
printf("\nEnter the no. of neighbours to %c:",src);
scanf("%d",&neighb); fflush(stdin);
printf("\nenter the names of the neighbours(in alphabetic order):");
neigh[0]=src;
for(i=1;i<neighb+1;i++)
{
scanf("%c",&neigh[i]);
fflush(stdin);
}
printf("\n enter the distance vectors of the source and the neighbouring nodes\n");
printf("starting with source,then with neighbouring nodes in alphabetical order:\n");
for(i=0;i<nodes*(neighb+1);i++)
{
scanf("%d",&k[i]);
}
src1=src;
l=nodes;n=1;
for(i=0;i<nodes;i++)
{
if(i==src1-97)
{
for(j=0;j<nodes;j++)
{
if(k[j]<0)
topology[i][j]=e;
else
topology[i][j]=k[j];
Page: 48
}
}
else if(i==neigh[n]-97)
{
for(j=0;j<nodes;j++)
{ if(k[l]<0)
topology[i][j]=e;
else
topology[i][j]=k[l];
l++;
} n++;
}
else
{
for(j=0;j<nodes;j++)
topology[i][j]=e;
}
}
i=src1-97;
for(j=0;j<nodes;j++)
{ dv[j].name=src1;
dv[j].delay=topology[i][j];
}
k[nodes*nodes]=e; n=1;
for(i=0;i<nodes;i++)
{
if((i==neigh[n]-97))
{
add=topology[src1-97][i];
for(j=0;j<nodes;j++)
{
k[j]=add+topology[i][j];
}
for(j=0;j<nodes;j++)
{
if(k[j]<dv[j].delay)
{
dv[j].name=i+97;
dv[j].delay=k[j];
}
}
if(i!=src1-97)
n++;
printf("\n%c's distance vector after receiving %c's vector:\n",src1,i+97);
for(j=0;j<nodes;j++)
printf("To %c:\tfrom: %c \t %d\n",j+97,dv[j].name,dv[j].delay);
getch();
}
}
}
Page: 49
OUTPUT:
A D
Page: 50
Network Graph for Output2:
A B C D
E F G H
I J K L
Output from second execution:
Page: 51
j's distance vector after receiving i's vector:
To a: from: j 8
To b: from: a 20
To c: from: i 28
To d: from: h 20
To e: from: i 17
To f: from: i 30
To g: from: h 18
To h: from: j 12
To i: from: j 10
To j: from: j 0
To k: from: j 6
To l: from: h 21
j's distance vector after receiving k's vector:
To a: from: j 8
To b: from: a 20
To c: from: i 28
To d: from: h 20
To e: from: i 17
To f: from: i 30
To g: from: h 18
To h: from: j 12
To i: from: j 10
To j: from: j 0
To k: from: j 6
Page: 52
Lab Program: 9
Implement data encryption and data decryption
Algorithm:
1. Start
2. Read p.q,d
3. If p,q,d are prime
4. Perform n=p*q, z=(p-1)*(q-1), e=1 mod z
5. Print n,z,e
6. Read Plain Text(p1)
7. Perform Cipher Text C=p^e(mod n)
8. Print Cipher ValuesI
9. Perform Decrypted Text P2=c^d(mod n)
10. Print Decrypted Values(P2)
11. Stop
Page: 53
FLOW CHART
Start
Read p,q,d
If
p,q,d are
prime
n=p*q
z=(p-1)*(q-1)
e=1 mod z
Print n, z , e
Cipher Text
C=p^e (mod n)
Print
Cipher
values (c)
Decrypted Text
P2=c^d ( mod n )
Stop
Page: 54
PROGRAM
#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
char a[]={“0ABCDEFGHIJKLMNOPQRSTUVWXYZ”};
int n,I,j,s,n2,k1,p,q,d,m1,e1,l5,z,p2[30],s1,c[30];
unsigned long int l3,m,l4,k2;
double l2,l1,l6;
float e,l;
char p1[30];
clrscr();
printf(“enter two prime numbers p and q\n”);
scanf(“%d %d”,&p,&q);
do{
n=p*q;
if(n<26)
{ printf(“\n n value is not large enough.\nplease select p, q value such that p*q is greater
than 26”);
scanf(“%d %d”,&p,&q);}
}while(n<26);
z=((p-1)*(q-1));
printf(“enter the value of d:\n”);
scanf(“%d”,&d);
for(j=1;j<z;j++)
{
if((j*d)%z==1)
break;
}
e=j;
printf(“%d %d %f\n”,n,z,e);
printf(“ENCRYPTION-CIPHERTEXT”);
printf(“enter the plain text\n”);
scanf(“%s”,p1);
for(i=0;i<strlen(p1);i++)
{
for(j=1;j<strlen(a);j++)
{
if(a[j]==p1[i])
{
s=j;
break;
Page: 55
}
else
continue;
}
printf(“%d”,s);
e1=(int)e;
l1=pow(((double)s),((double)e1));
k2=fmod(l1,(double)n);
printf(“\n%lu\n”,k2);
c[i]=(int)k2;
printf(“cipher:%d\n”,c[i]);
}
printf(“\n”);
for(i=0;i<strlen(p1);i++)
{
l2=(pow(((double)c[i]),((double)d)));
m=fmod(l2,(double)n);
m1=(int)m;
printf(“ %c\n”,a[m1]);}
getch();
}
OUTPUT
enter two prime numbers p and q
3 11
enter the value of d:
7
33 20 3.000000
ENCRYPTION-CIPHERTEXT
enter the plain text
SUZANNE
19
28
cipher:28
21
21
cipher:21
26
20
cipher:20
1
1
cipher:1
14
5
cipher:5
14
5
cipher:5
5
Page: 56
26
cipher:26
S
U
Z
A
N
N
E
RSA output:
enter two prime numbers p and q
5 13
enter the value of d:
7
65 48 7.000000
ENCRYPTION-CIPHERTEXTenter the plain text
NAINA
14
14
cipher:14
1
1
cipher:1
9
9
cipher:9
14
14
cipher:14
1
1
cipher:1
N
A
I
N A
Page: 57
Lab Program: 10
Write a program for congestion control using Leaky bucket Algorithm
Algorithm:
1. Start
2. Set the bucket size or the buffer size.
3. Set the output rate.
4. Transmit the packets such that there is no overflow.
5. Repeat the process of transmission until all packets are transmitted.
(Reject packets where its size is greater than the bucket size)
6. Stop
PROGRAM
#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)
{
Page: 58
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);
p_time = rand(4) * 10;
printf("\nTime left for transmission: %d units", p_time);
for(clk = 10; clk <= p_time; clk += 10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)/*packet size remaining comparing with
output rate*/
op = p_sz_rm, p_sz_rm = 0;
else
op = o_rate, p_sz_rm -= o_rate;
printf("\nPacket of size %d Transmitted", op);
printf("----Bytes Remaining to Transmit: %d", p_sz_rm);
}
else
{
printf("\nTime left for transmission: %d units", p_time-clk);
printf("\nNo packets to transmit!!");
}
}
}
}
}
Page: 59
Output:
Page: 60
Lab Program: 11
Write a program for frame sorting technique used in buffers.
PROGRAM
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define FSize 3
typedef struct packet{int SeqNum; char Data[FSize+1];}packet;
struct packet *readdata, *transdata;
int divide(char *msg) {
int msglen, NoOfPacket, i, j;
msglen = strlen(msg);
NoOfPacket = msglen/FSize;
if ((msglen%FSize)!=0) NoOfPacket++;
readdata = (struct packet *)malloc(sizeof(packet) * NoOfPacket);
for(i = 0; i < NoOfPacket; i++) {
readdata[i].SeqNum = i + 1;
for (j = 0; (j < FSize) && (*msg != '\0'); j++, msg++)
readdata[i].Data[j] = *msg;
readdata[i].Data[j] = '\0';
}
printf("\nThe Message has been divided as follows\n");
printf("\nPacket No. Data\n\n");
for (i = 0; i < NoOfPacket; i++)
printf(" %2d %s\n", readdata[i].SeqNum,
readdata[i].Data);
return NoOfPacket;
}
void shuffle(int NoOfPacket) {
int *Status;
int i, j, trans;
randomize();
Status=(int * )calloc(NoOfPacket, sizeof(int));
transdata = (struct packet *)malloc(sizeof(packet) * NoOfPacket);
for (i = 0; i < NoOfPacket;) {
trans = rand()%NoOfPacket;
if (Status[trans]!=1) {
transdata[i].SeqNum = readdata[trans].SeqNum;
strcpy(transdata[i].Data, readdata[trans].Data);
i++; Status[trans] = 1;
}
}
free(Status);
}
Page: 61
void sortframes(int NoOfPacket) {
packet temp;
int i, j;
for (i = 0; i < NoOfPacket; i++)
for (j = 0; j < NoOfPacket – i-1; j++)
if (transdata[j].SeqNum > transdata[j + 1].SeqNum) {
temp.SeqNum = transdata[j].SeqNum;
strcpy(temp.Data, transdata[j].Data);
transdata[j].SeqNum = transdata[j + 1].SeqNum;
strcpy(transdata[j].Data, transdata[j + 1].Data);
transdata[j + 1].SeqNum = temp.SeqNum;
strcpy(transdata[j + 1].Data, temp.Data);
}
}
void receive(int NoOfPacket) {
int i;
printf("\nPackets received in the following order\n");
for (i = 0; i < NoOfPacket; i++) printf("%4d", transdata[i].SeqNum);
sortframes(NoOfPacket);
printf("\n\nPackets in order after sorting..\n");
for (i = 0; i < NoOfPacket; i++) printf("%4d", transdata[i].SeqNum);
printf("\n\nMessage received is :\n");
for (i = 0; i < NoOfPacket; i++) printf("%s", transdata[i].Data);
}
void main() {
char *msg;
int NoOfPacket;
clrscr();
printf("\nEnter The message to be Transmitted :\n");
scanf("%[^\n]", msg);
NoOfPacket = divide(msg);
shuffle(NoOfPacket);
receive(NoOfPacket);
free(readdata);
free(transdata);
getch();
}
Output
Enter The messgae to be Transmitted :
hi, it was nice meeting u on sunday
The Message has been divided as follows
Packet No. Data
1 hi,
2 it
3 wa
4sn
5 ice
6 me
7 eti
Page: 62
8 ng
9uo
10 n s
11 und
12 ay
Packets received in the following order
4 2 6 3 5 1 8 9 11 7 12 10
Packets in order after sorting..
1 2 3 4 5 6 7 8 9 10 11 12
Message received is :
hi, it was nice meeting u
on sunday
Page: 63
Lab Program: 12
WIRE SHARK
1. Click View > Wireless Toolbar. The Wireless Toolbar will appear just below the Main
toolbar.
2. Use the Wireless Toolbar to configure the desired channel and channel width.
Page: 64
3. Under Capture, click on AirPcap USB wireless capture adapter to select the capture
interface.
Note: If the AirPcap isn't listed, press F5 to refresh the list of available packet capture
interfaces.
Note: The AirPcap has been discontinued by RiverBed and is 802.11n only.
Page: 65
5. When you are finished capturing, click the Stop button.
Page: 66
2. Name the file, and click Save.
Note: .Pcap and .Pcap-ng are good filetypes to use for the capture if you plan to use Eye P.A.
to open the capture.
Page: 67
3. Eye P.A. can now open the capture file.
Two different methods for starting Wireshark are available. These include
the Start menu and the Run command box.
Method 1 - Start Menu
To start Wireshark using the Start menu:
Page: 68
iii)Viewing Captured Traffic
Once you have captured some packets or you have opened a previously
saved capture file, you can view the packets that are displayed in the packet
list pane by simply clicking on a packet in the packet list pane, which will
bring up the selected packet in the tree view and byte view panes.
You can then expand any part of the tree to view detailed information about
each protocol in each packet. Clicking on an item in the tree will highlight
the corresponding bytes in the byte view. An example with a TCP packet
selected is shown in Figure 6.1, “Wireshark with a TCP packet selected for
viewing”. It also has the Acknowledgment number in the TCP header
selected, which shows up in the byte view as the selected bytes.
You can also select and view packets the same way while Wireshark is
capturing if you selected “Update list of packets in real time” in the
“Capture Preferences” dialog box.
Page: 69
double-clicking on an item in the packet list or by selecting the packet in
which you are interested in the packet list pane and selecting View → Show
Packet in New Window. This allows you to easily compare two or more
packets, even across multiple files.
Along with double-clicking the packet list and using the main menu there
are a number of other ways to open a new packet window:
Hold down the shift key and double-click on a frame link in the packet details.
Page: 70
Filtering Packets
If you’re trying to inspect something specific, such as the traffic a program sends when
phoning home, it helps to close down all other applications using the network so you
can narrow down the traffic. Still, you’ll likely have a large amount of packets to sift
through. That’s where Wireshark’s filters come in.
The most basic way to apply a filter is by typing it into the filter box at the top of the
window and clicking Apply (or pressing Enter). For example, type “dns” and you’ll see
only DNS packets. When you start typing, Wireshark will help you autocomplete your
filter.
Page: 71
You can also click Analyze > Display Filters to choose a filter from among
the default filters included in Wireshark. From here, you can add your own
custom filters and save them to easily access them in the future.
You’ll see the full TCP conversation between the client and the server.
You can also click other protocols in the Follow menu to see the full
conversations for other protocols, if applicable.
Page: 72
Close the window and you’ll find a filter has been applied automatically.
Wireshark is showing you the packets that make up the conversation.
Page: 73
LAB PROGRAM:13
Download the Nmap installer. This can be found for free from the developer’s website. It is
highly recommended that you download directly from the developer to avoid any potential
viruses or fake files. Downloading the Nmap installer includes Zenmap, the graphical
interface for Nmap which makes it easy for newcomers to perform scans without having to
learn command lines.
Install Nmap. Run the installer once it is finished downloading. You will be asked
which components you would like to install. In order to get the full benefit of Nmap,
keep all of these checked. Nmap will not install any adware or spyware.
Page: 74
Run the “Nmap – Zenmap” GUI program. If you left your settings at default
during installation, you should be able to see an icon for it on your desktop. If not,
look in your Start menu. Opening Zenmap will start the program.
Page: 75
Enter in the target for your scan. The Zenmap program makes scanning a fairly
simple process. The first step to running a scan is choosing your target. You can enter
a domain (example.com), an IP address (127.0.0.1), a network (192.168.1.0/24), or a
combination of those.
Depending on the intensity and target of your scan, running an Nmap
scan may be against the terms of your internet service provider, and
may land you in hot water. Always check your local laws and your
ISP contract before performing Nmap scans on targets other than your
own network.
Page: 76
Choose your Profile. Profiles are preset groupings of modifiers that change what is
scanned. The profiles allow you to quickly select different types of scans without
having to type in the modifiers on the command line. Choose the profile that best fits
your needs:[1]
Intense scan - A comprehensive scan. Contains Operating System
(OS) detection, version detection, script scanning, traceroute, and has
aggressive scan timing. This is considered an intrusive scan.
Ping scan - This scan simply detects if the targets are online, it does
not scan any ports.
Quick scan - This is quicker than a regular scan due to aggressive
timing and only scanning select ports.
Regular scan - This is the standard Nmap scan without any modifiers.
It will return ping and return open ports on the target.
Page: 77
Click Scan to start scanning. The active results of the scan will be displayed in the
Nmap Output tab. The time the scan takes will depend on the scan profile you chose,
the physical distance to the target, and the target’s network configuration.
Page: 78
Read your results. Once the scan is finished, you’ll see the message “Nmap done” at
the bottom of the Nmap Output tab. You can now check your results, depending on
the type of scan you performed. All of the results will be listed in the main Nmap
Output tab, but you can use the other tabs to get a better look at specific data.[2]
Ports/Hosts - This tab will show the results of your port scan,
including the services for those ports.
Topology - This shows the traceroute for the scan you performed. You
can see how many hops your data goes through to reach the target.
Host Details - This shows a summary of your target learned through
scans, such as the number of ports, IP addresses, hostnames, operating
systems, and more.
Scans - This tab stores the commands of your previously-run scans.
This allows you to quickly re-scan with a specific set of parameters.
Page: 79
LAB PROGRAM:14
If Nmap is unable to guess the OS of a machine, and conditions are good (e.g.
at least one open port and one closed port were found), Nmap will provide a URL you
can use to submit the fingerprint if you know (for sure) the OS running on the machine.
By doing this you contribute to the pool of operating systems known to Nmap and thus
it will be more accurate for everyone.
OS detection enables some other tests which make use of information that is
gathered during the process anyway. One of these is TCP Sequence Predictability
Classification. This measures approximately how hard it is to establish a forged TCP
connection against the remote host. It is useful for exploiting source-IP based trust
relationships (rlogin, firewall filters, etc) or for hiding the source of an attack. This sort
of spoofing is rarely performed any more, but many machines are still vulnerable to it.
The actual difficulty number is based on statistical sampling and may fluctuate. It is
generally better to use the English classification such as “worthy challenge” or “trivial
joke”. This is only reported in normal output in verbose (-v) mode. When verbose
mode is enabled along with -O, IP ID sequence generation is also reported. Most
machines are in the “incremental” class, which means that they increment the ID field
in the IP header for each packet they send. This makes them vulnerable to several
advanced information gathering and spoofing attacks.
-O (Enable OS detection)
Page: 80
--osscan-limit (Limit OS detection to promising targets)
OS detection is far more effective if at least one open and one closed TCP port
are found. Set this option and Nmap will not even try OS detection against
hosts that do not meet this criteria. This can save substantial time, particularly
on -Pn scans against many hosts. It only matters when OS detection is
requested with -O or -A.
When Nmap performs OS detection against a target and fails to find a perfect
match, it usually repeats the attempt. By default, Nmap tries five times if
conditions are favorable for OS fingerprint submission, and twice when
conditions aren't so good. Specifying a lower --max-os-tries value (such as 1)
speeds Nmap up, though you miss out on retries which could potentially
identify the OS. Alternatively, a high value may be set to allow even more
retries when conditions are favorable. This is rarely done, except to generate
better fingerprints for submission and integration into the Nmap OS database.
Page: 81
LAB PROGRAM:15
i)NS2 simulator-Introduction
Tcl is a very simple programming language. If you have programmed before, you can
learn enough to write interesting Tcl programs within a few hours. This page provides a quick
overview of the main features of Tcl. After reading this you'll probably be able to start
writing simple Tcl scripts on your own; however, we recommend that you consult one of the
many available Tcl books for more complete information.
Basic syntax
expr 20 + 10
This command computes the sum of 20 and 10 and returns the result, 30. You can try
out this example and all the others in this page by typing them to a Tcl application such as
tclsh; after a command completes, tclsh prints its result.
Each Tcl command consists of one or more words separated by spaces. In this
example there are four words:
The first word is the name of a command and the other words are arguments to that
command. All Tcl commands consist of words, but different commands treat their arguments
differently. The expr command treats all of its arguments together as an arithmetic
expression, computes the result of that expression, and returns the result as a string. In the
expr command the division into words isn't significant: you could just as easily have invoked
the same command as
expr 20+10
However, for most commands the word structure is important, with each word used for a
distinct purpose.
All Tcl commands return results. If a command has no meaningful result then it returns an
empty string as its result.
Page: 82
Variables
Tcl allows you to store values in variables and use the values later in commands. The
set command is used to write and read variables. For example, the following command
modifies the variable x to hold the value 32:
set x 32
The command returns the new value of the variable. You can read the value of a
variable by invoking set with only a single argument:
set x
You don't need to declare variables in Tcl: a variable is created automatically the first
time it is set. Tcl variables don't have types: any variable can hold any value. To use the value
of a variable in a command, use variable substitution as in the following example:
expr $x*3
When a $ appears in a command, Tcl treats the letters and digits following it as a
variable name, and substitutes the value of the variable in place of the name. In this example,
the actual argument received by the expr command will be
32*3 (assuming that variable x was set as in the previous example).
You can use variable substitution in any word of any command, or even multiple times within
a word:
$0 ~/^r.* AGT/ {
recvLine ++ ;
$0 ~/^f.* RTR/ {
fowardLine ++ ;
$0 ~/^D.* cbr/ {
dropLine ++ ;
END {
printf "Packet Sent: %d\n,sendLine"
printf "Packet Received: %d\n,recvLine"
printf "Packet Drop: %d\n,dropLine"
Page: 83