Professional Documents
Culture Documents
CN Lab Manual
CN Lab Manual
Bit Stuffing.
AIM: Write a C program to implement the data link layer framing
methods such as bit stuffing.
Description:
Bit stuffing is the mechanism of inserting one or more non-information bits into
a message to be transmitted, to break up the message sequence, for
synchronization purpose.
A pattern of bits of arbitrary length is stuffed in the message to differentiate
from the delimiter. This is also called bit - oriented framing.
Source Code:
#include<stdio.H>
#include<conio.H>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
clrscr();
printf("enter frame
length:");
scanf("%d",&n);
pritf("enter input
frame(0's&1's only):");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0;count=1;j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;a[k]==1&&k<n&&count<5;k++)
Department of Computer Science and Engineering Page 1
COMPUTER NETWORKS AND WEB TECHNOLOGIES LAB
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After stuffing the frame is:"); for(i=0;i<j;i++)
printf("%d",b[i]); getch();
}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]); getch();
}
Output:
Enter the
number of bits:
10
1
0
1
0
1
1
1
1
1
1
Data after stuffing: 10101111101
OUTPUT:
Character Stuffing:
AIM: Write a C program to Implement the data link layer framing method such
as character stuffing.
Source Code:
//program for character
stuffing #include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char
a[20],b[50],ch;
clrscr();
printf("enter
string:\n");
scanf("%s",&a);
n=strlen(a);
printf("enter
position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position,Enter again:");
scanf("%d",&pos);
}
printf("enter the
character\n"); ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
Department of Computer Science and Engineering Page 4
COMPUTER NETWORKS AND WEB TECHNOLOGIES LAB
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d'&&a[i+1]=='l'&& a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e;
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\n frame after stuffing: \n");
printf("%s",b);
getch();
}
OUTPUT:
Enter String:
haiarchana
Enter
position:
4
Enter the
Character K
Frame after stuffing:
dlestxhaidlekdlearchanadleetx
OUTPUT :
Character Count:
AIM: Write a C program to implement data link layer framing method character count.
Description: This method uses a field in the header to specify the number of characters in
the frame. When the data link layer at the destination sees the character count,it knows
how many characters follow, and hence where the end of the frame is. The disadvantage is
that if the count is garbled by a transmission error, the destination will lose
synchronization and will be unable to locate the start of the next frame. So, this method is
rarely used.
Source Code:
#include<stdio.h>
#include<conio.h
>
#include<string.h
> char
data[20][20]; int
n;
void main()
{
int i,ch,j;
char
tmp[20][20];
clrscr();
printf("Enter the number of
frames:"); scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i!=0)
{
printf("frame%d:",i);
fflush(stdin);
gets(data[i]);
}
}
/*saving frame with count and
data*/ for(i=0;i<=n;i++)
{
tmp[i][0]=49+strlen(data[i]);
tmp[i][1]='\0';
strcat(tmp[i],data[i]);
}
printf("\n\t\tAT THE SENDER:\n");
printf("Data as
frames:\n");
for(i=1;i<=n;i++)
{
printf("Frame%d:",i);
puts(tmp[i]);
}
printf("Data
transmitted:");
for(i=1;i<=n;i++)
printf("%s",tmp[i]);
printf("\n\t\tAT THE
RECEIVER\n"); printf("The data
received:"); for(i=1;i<=n;i++)
{
ch=(int)(tmp[i][0]-49);
for(j=1;j<=ch;j++)
data[i][j-1]=tmp[i][j];
data[i][j-1]='\0';
}
printf("\n The data after removing count char:");
for(i=1;i<=n;i++)
printf("%s",data[i]);
printf("\n The data in frame
form:\n"); for(i=1;i<=n;i++)
{
printf("Frame%d:",i);
puts(data[i]);
}
getch();
}
OUTPUT:
Enter the no. Of
frames: 2 Frame1:
computer Frame2:
networks
AT THE SENDER
Data as frames:
Frame1:9comput
er
Frame2:9networ
ks
Data transmitted
:9computer9networks AT THE
RECEIVER
The data received.
The data after removing count char: computer
networks The data in frame form:
Frame1:
computer
Frame2:
networks
OUTPUT
2.Write a program to compute CRC code for the polynomials CRC-12, CRC-16
and CRC CCIP
AIM: Write a C program to implement on a data set characters the three CRC polynomials
– CRC 12, CRC 16, and CRC CCIP.
Source Code:
// program for Cyclic Redundancy Check
#include<stdio.h
>#include<conio.
h> int main(void)
{
int
data[50],div[16],rem[16];
int datalen, divlen, i,j,k;
int ch;
clrscr()
;
printf("Enter the data:
"); i = 0;
while((ch = fgetc(stdin)) != '\n')
{
if(ch == '1')
data[i] =
1; else
data[i] =
0; i++;
}
datalen = i;
printf("\nEnter the
divisor: "); i = 0;
while((ch = fgetc(stdin)) != '\n')
{
if(ch == '1')
div[i] = 1;
else
div[i] = 0;
i++;
}
divlen = i;
for(i = datalen ; i<datalen + divlen - 1 ;
i++) data[i] = 0;
datalen = datalen + divlen
- 1; for(i = 0 ; i<divlen ;
i++) rem[i] = data[i];
k = divlen-1;
while(k <datalen)
if(rem[0] == 1)
{
for(i = 0 ; i<divlen ; i++)
rem[i] = rem[i] ^ div[i];
}
else
{
if(k == datalen-
1) break;
for(i = 0 ; i< divlen-1 ; i++)
{
rem[i] = rem[i+1];
printf("%d",rem[i]);
}
rem[i] = data[++k];
printf("%d\n",rem[i]);
}
j=1;
for(i = datalen - divlen + 1 ; i<datalen ; i++)
{
data[i] = rem[j++];
}
printf("\nThe data to be sent
is\n"); for(i = 0 ; i<datalen ;
i++) printf("%d",data[i]);
getch();
return
0;
Department of Computer Science and Engineering Page 11
COMPUTER NETWORKS AND WEB TECHNOLOGIES LAB
OUTPUT:
Enter the data:
10101111 Enter the
divisor: 1011 0011
0111
1111
1001
0100
1000
0110
The data to be sent is
10101111110
OUTPUT :
3.Develop a simple data link layer that performs the flow control using the
sliding window protocol, and loss recovery using the Go-Back-N mechanism.
AIM: Develop a simple data link layer that performs the flow control using the
sliding window protocol, and loss recovery using the Go- Back- N mechanism.
Description: Sliding window protocol is applied on the Data Link Layer of OSI model. At
data link layer data is in the form of frames. In Networking, Window simply means a buffer
which has data frames that needs to be transmitted.
Both sender and receiver agrees on some window size. If window size=w then after sending
w frames sender waits for the acknowledgement (ack) of the first frame.
Go back n: Sender transmits all frames present in the window that occurs after the
error bit including error bit also.
SOURCE CODE:
#include<stdio.h>
int main ()
{
int w,i,f,frames[50];
printf("Enter window size: ");
scanf("%d",&w);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
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;
}
OUTPUT:
Enter window size: 3
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
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#define INFINITY
9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int
n,intstartnode); int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting
node:"); scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,intstartnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int
visited[MAX],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[] and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][
i]; pred[i]=startnode;
visited[i]=0;
}
distance[startnode]
=0;
visited[startnode]=1
; count=1;
while(count<n-1)
{
mindistance=INFINITY;
//nextnode gives the node at minimum
distance for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through
nextnode visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
//print the path and distance of each node
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of
node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-
%d",j);
}while(j!=startnode);
}
}
OUTPUT
5.Take an example subnet of hosts and obtain a broadcast tree for the subnet
AIM: To write a C program on to obtain a broadcast tree for the subnet with an example
subnet of hosts.
AIM: Write a C program Implement Broadcast Tree for a given subnet hosts.
SOURCE CODE:
#include<stdio.h>
int a[10][10],n;
main()
{
int
i,j,root;
clrscr();
printf("Enter no.of
nodes:"); scanf("%d",&n);
printf("Enter adjacent
matrix\n"); for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("Enter root
node:");
scanf("%d",&root);
adj(root);
}
adj(intk)
int i,j;
printf("Adjacent node of root
node::\n"); printf("%d\n",k);
for(j=1;j<=n;j++)
if(a[k][j]==1 ||
a[j][k]==1)
printf("%d\t",j);
}
printf("\n");
for(i=1;i<=n;i++)
{
OUTPUT:
Enter no.of
nodes:5 Enter
adjacentmatrix
Enter connecting of1–
>1::0 Enter connecting
of1–>2::1 Enter
connecting of1–>3::1
Enter connecting of1–
>4::0 Enter connecting
of1–>5::0 Enter
connecting of2–>1::1
Enter connecting of2–
>2::0 Enter connecting
of2–>3::1 Enter
connecting of2–>4::1
Enter connecting of2–
>5::0 Enter connecting
of3–>1::1 Enter
connecting of3–>2::1
Enter connecting of3–
>3::0 Enter connecting
of3–>4::0 Enter
connecting of3–>5::0
Enter connecting of4–
>1::0 Enter connecting
of4–>2::1 Enter
connecting of4–>3::0
Enter connecting of4–
>4::0 Enter connecting
of4–>5::1 Enter
connecting of5–>1::0
Enter connecting of5–
>2::0
OUTPUT :
6. Implement distance vector routing algorithm for obtaining routing tables at each
node.
DESCRIPTION: A distance-vector routing (DVR) protocol requires that a router inform its
neighbors of topology changes periodically. Historically known as the old
ARPANET routing algorithm (or known as Bellman-Ford algorithm).Distances,based on a
chosen metric, are computed using information from the neighbors' distance vectors.
SOURCE CODE:
#include<stdio.h>
structnode
{
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++)
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("\t\nnode %d via %d
Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
printf("\n\n");
OUTPUT :
Output (2):
Encryption Technique: If L is the length of the string, then take two values,
one the ceil of √L (say b), and the other floor of √L (say a), and make a two -
dimensional matrix having rows = a, and columns = b.
If rows*columns < L, then increase the value of a or b, whichever is minimum.
Fill the matrix with the characters of the original string sequentially. After
obtaining the matrix, read the matrix column-wise and print the obtained
string.
SOURCECODE:
#include<stdio.h
>
#include<conio.
h>#include<stri
ng.h>
int p10[]={3,5,2,7,4,10,1,9,8,6},
p8[]={6,3,7,4,8,5,10,9},
p4[]={2,4,3,1};
intip[]={2,6,3,1,4,8,5,7},
ipinv[]={4,1,3,5,7,2,8,6},
ep[]={4,1,2,3,2,3,4,1};
int s0[][4]={{1,0,3,2,},{3,2,1,0},{0,2,1,3,},{3,1,3,2}};
int s1[][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
void permute(char op[],char ip[],int p[], int
n){ int i;
for(i=0;i<n;i++
) op[i]=ip[p[i]-
1]; op[i]='\0';
}
void circularls(char pr[],int
n){ int i;
char ch=pr[0];
Department of Computer Science and Engineering Page 26
COMPUTER NETWORKS AND WEB TECHNOLOGIES LAB
for(i=0;i<n-
1;i++)
pr[i]=pr[i+1];
pr[i]=ch;
}
void keygen(char k1[],char k2[],char
key[]){ char keytemp[11];
permute(keytemp,key,p10,10);
circularls(keytemp,5);
circularls(keytemp+5,5);
permute(k1,keytemp,p8,8);
circularls(keytemp,5);
circularls(keytemp,5);
circularls(keytemp+5,5);
circularls(keytemp+5,5);
permute(k2,keytemp,p8,8);
}
void xor(char op[],char ip[]){
int i;
for(i=0;i<strlen(op)&&i<strlen(ip);i+
+) op[i]=(op[i]-'0')^(ip[i]-'0')+'0';
}
void sbox(char op[],char ip[],int
s[][4]) { int value;
value=s[(ip[0]-'0')*2+(ip[3]-'0')][(ip[1]-'0')*2+(ip[2]-'0')];
op[0]=value/2+'0';
op[1]=value%2+'0';
op[2]='\0';
}
void fk(char op[],char ip[],char k[])
{
char
l[5],r[5],tmp[9],tmp1[9],tmp2[9];
strncpy(l,ip,4);
l[4]='\0';
strncpy(r,ip+4,4);
r[4]='\0';
permute(tmp,r,ep,
8); xor(tmp,k);
sbox(tmp1,tmp,s0);
sbox(tmp2,tmp+4,s1);
strcat(tmp1,tmp2);
permute(tmp,tmp1,p4,
4); xor(tmp,l);
strcat(tmp,r);
strcpy(op,tmp);
}
void sw(char pr[]) {
char tmp[9];
strncpy(tmp,pr+4,
4);
strncpy(tmp+4,pr,
4); tmp[8]='\0';
strcpy(pr,tmp);
}
void main()
{
char key[11],k1[9],k2[9],plain[9],cipher[9],tmp[9];
clrscr();
printf("enter 10 bit key:");
gets(key);
if(strlen(key)!=10) printf("invalid key length
!!"); else
{
keygen(k1,k2,key);
printf("sub key
k1::"); puts(k1);
printf("subkey
k2::"); puts(k2);
printf("enter 8 bit plain
text:"); gets(plain);
if(strlen(plain)!=8) printf("invalid length plain text
!!"); permute(tmp,plain,ip,8);
fk(cipher,tmp,k1);
sw(cipher);
fk(tmp,cipher,k2);
permute(cipher,tmp,ipinv,
8); printf("cipher teaxt
is::"); puts(cipher);
/* decryption process*/
permute(tmp,cipher,ip,8
); fk(plain,tmp,k2);
sw(plain);
fk(tmp,plain,k1);
permute(plain,tmp,ipinv
,8); printf("decrypted
textis::"); puts(plain);
}
getch();
}
OUTPUT:
Enter 10
bitkey:1456987203
Sub key k1::17062538
Sub keyk2::25893401
Enter 8 bit plain text:
computer Cipher text
is::epfnmrct
Decrypted text is::computer
OUTPUT CONSOLE:
SOURCE CODE:
#include<stdio.h
>#include<conio.
h>
#include<string.
h>
int p10[]={3,5,2,7,4,10,1,9,8,6},
p8[]={6,3,7,4,8,5,10,9},
p4[]={2,4,3,1};
intip[]={2,6,3,1,4,8,5,7},
ipinv[]={4,1,3,5,7,2,8,6},
ep[]={4,1,2,3,2,3,4,1};
int s0[][4]={{1,0,3,2,},{3,2,1,0},{0,2,1,3,},{3,1,3,2}};
int s1[][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
void permute(char op[],char ip[],int p[], int n)
{
int i;
for(i=0;i<n;i++
) op[i]=ip[p[i]-
1]; op[i]='\0';
}
void circularls(char pr[],int n)
{
int i;
char ch=pr[0];
for(i=0;i<n-
1;i++)
pr[i]=pr[i+1];
pr[i]=ch;
}
void keygen(char k1[],char k2[],char key[])
{
char keytemp[11];
permute(keytemp,key,p10,10);
circularls(keytemp,5);
circularls(keytemp+5,5);
permute(k1,keytemp,p8,8);
circularls(keytemp,5);
circularls(keytemp,5);
circularls(keytemp+5,5);
circularls(keytemp+5,5);
permute(k2,keytemp,p8,8);
}
void xor(char op[],char ip[])
{
int i;
for(i=0;i<strlen(op)&&i<strlen(ip);i+
+) op[i]=(op[i]-'0')^(ip[i]-'0')+'0';
}
void sbox(char op[],char ip[],int s[][4])
{
int value;
value=s[(ip[0]-'0')*2+(ip[3]-'0')][(ip[1]-'0')*2+(ip[2]-'0')];
op[0]=value/2+'0';
op[1]=value%2+'0';
op[2]='\0';
}
void fk(char op[],char ip[],char k[])
{
char l[5],r[5],tmp[9],tmp1[9],tmp2[9];
strncpy(l,ip,4);
l[4]='\0';
strncpy(r,ip+4,4);
r[4]='\0';
permute(tmp,r,ep,8);
xor(tmp,k);
sbox(tmp1,tmp,s0);
sbox(tmp2,tmp+4,s1);
strcat(tmp1,tmp2);
permute(tmp,tmp1,p4,
4); xor(tmp,l);
strcat(tmp,r);
strcpy(op,tmp);
}
void sw(char pr[])
{
Department of Computer Science and Engineering Page 31
COMPUTER NETWORKS AND WEB TECHNOLOGIES LAB
char tmp[9];
strncpy(tmp,pr+4,
4);
strncpy(tmp+4,pr,
4); tmp[8]='\0';
strcpy(pr,tmp);
}
void main()
{
char key[11],k1[9],k2[9],plain[9],cipher[9],tmp[9];
clrscr();
printf("enter 10 bit key:");
gets(key);
if(strlen(key)!=10) printf("invalid key length
!!"); else
{
keygen(k1,k2,key);
printf("sub key
k1::"); puts(k1);
printf("subkey
k2::"); puts(k2);
printf("enter 8 bit plain
text:"); gets(plain);
if(strlen(plain)!=8) printf("invalid length plain text
!!"); permute(tmp,plain,ip,8);
fk(cipher,tmp,k1);
sw(cipher);
fk(tmp,cipher,k2);
permute(cipher,tmp,ipinv,
8); printf("cipher teaxt
is::"); puts(cipher);
/* decryption process*/
permute(tmp,cipher,ip,8
); fk(plain,tmp,k2);
sw(plain);
fk(tmp,plain,k1);
permute(plain,tmp,ipinv
,8); printf("decrypted
textis::"); puts(plain);
}
getch();
}
OUTPUT:
Enter 10 bit key:
1234567892 Sub
keyk1::17948326
Sub keyk2::83652291
Enter 8 bit plain text: computer
Cipher text is::epuomict
Decrypted text is :: computer
OUTPUT:
AIM: Using RSA algorithm encrypts a text data and Decrypt the same.
DESCRIPTION:Given a string S, the task is to encrypt the string and decrypt the string
again to the original form.
Decryption Technique: If L is the length of the encrypted string, then again find the
two values a and b, where a is the ceil value of the √L and b is the floor value of the √L.
Similarly create a 2D Matrix in which store the string column wise and read the matrix
row-wise to get the string in the original form
SOUECE CODE:
#include<stdio.
h>#include<stri
ng.h>#include<c
onio.h>
#include<math.
h>
int mult(unsigned int x,unsigned int y,unsigned int n)
{
unsigned long int
k=1; int j;
for(j=1;j<=y;j++)
k=(k*x)%n;
return(unsigned int)
k;
}
void main()
{
char msg[100];
unsigned int
pt[100],ct[100],n,d,e,p,q,i;
printf("enter the plain text:");
gets(msg);
//strcpy(pt,msg);
for(i=0;i<strlen(msg);i++)
pt[i]=msg[i];
n=253;d=17;e=13;
printf("\nCT=");
for(i=0;i<strlen(msg);i++)
ct[i]=mult(pt[i],e,n);
for(i=0;i<strlen(msg);i++)
printf("%d",ct[i]);
printf("\nPT=");
for(i=0;i<strlen(msg);i++)
printf("%c",pt[i]);
for(i=0;i<strlen(msg);i++)
pt[i]=mult(ct[i],d,n);
}
OUTPUT:
Enter the plain text: computer
CT=66 119 10 129211 139 173
229
PT=computer
OUTPUT:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define NOF_PACKETS 10
int rand(int a)
{
intrn = (random() % 10) % a;
returnrn == 0 ? 1 :rn;
}
int main()
{
intpacket_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);
OUTPUT:
SOURCE CODE:
#include<stdio.h>
#include<string.h>
#define FRAM_TXT_SIZ 3
#define MAX_NOF_FRAM 127
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
struct frame // structure maintained to hold frames
{ char text[FRAM_TXT_SIZ];
int seq_no;
}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
int assign_seq_no() //function which splits message
{ int k=0,i,j; //into frames and assigns sequence no
for(i=0; i < strlen(str); k++)
{ fr[k].seq_no = k;
for(j=0; j < FRAM_TXT_SIZ && str[i]!='\0'; j++)
fr[k].text[j] = str[i++];
}
printf("\nAfter assigning sequence numbers:\n");
for(i=0; i < k; i++)
printf("%d:%s ",i,fr[i].text);
return k; //k gives no of frames
}
void generate(int *random_ary, const int limit) //generate array of
random nos
{ int r, i=0, j;
while(i < limit)
{ r = random() % limit;
for(j=0; j < i; j++)
if( random_ary[j] == r )
break;
if( i==j ) random_ary[i++] = r;
}}
void shuffle( const int no_frames ) // function shuffles the frames
{
int i, k=0, random_ary[no_frames];
generate(random_ary, no_frames);
for(i=0; i < no_frames; i++)
shuf_ary[i] = fr[random_ary[i]];
printf("\n\nAFTER SHUFFLING:\n");
for(i=0; i < no_frames; i++)
printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);
}
void sort(const int no_frames) // sorts the frames
{
int i,j,flag=1;
struct frame hold;
for(i=0; i < no_frames-1 && flag==1; i++) // search for frames in
sequence
{
flag=0;
for(j=0; j < no_frames-1-i; j++) //(based on seq no.) and display
if(shuf_ary[j].seq_no > shuf_ary[j+1].seq_no)
{
hold = shuf_ary[j];
shuf_ary[j] = shuf_ary[j+1];
shuf_ary[j+1] = hold;
flag=1;
}
}
}
int main()
{
int no_frames,i;
printf("Enter the message: ");
gets(str);
no_frames = assign_seq_no();
shuffle(no_frames);
sort(no_frames);
printf("\n\nAFTER SORTING\n");
for(i=0;i<no_frames;i++)
printf("%s",shuf_ary[i].text);
printf("\n\n");
}
OUTPUT:
Enter the message:Welcomto Nalla Narasimha Reddy
0:Wel 1:com 2:e T 3:o 4:nal 5:la 6: na 7:ras 8:im 9:ha 10:R 11:ed 12:dy
AFTER SHUFFLING:
1:com 4:nal 9:ha 5:la 3:o 10:R 2:e T 6: na 11:ed 0:Wel 8:im 12:dy 7:ras
AFTER SORTING
10. Wireshark
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.
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.
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.
To get started, download and install Nmap from the nmap.org website and then
launch a command prompt.
Host Discovery performs a check to see if the host is online. In a large IP range,
this is useful for identifying only active or interesting hosts, rather than scanning
every single port on every single IP in the range (a lot of which may not even be
there).
There are five separate probes being performed. Each probe may consist of one or
more packets. The response to each packet by the target system helps to determine
the OS type.
1. Sequence Generation
2. ICMP Echo
3. TCP Explicit Congestion Notification
4. TCP
5. UDP
Now we can look at these individually to see what they are doing.
Sequence Generation
The Sequence Generation Probe consists of six packets. The six packets are sent
100 ms apart and are all TCP SYN packets.
The result of each TCP SYN packet will help NMAP determine the OS type.
ICMP Echo
Two ICMP Request packets are sent to the target system with varying settings in
the packet.
When a lot of packets are being generated and passing through a router causing it
to be burdened is known as congestion. The result is that systems slow down to
reduce congestion so the router is not dropping packets.
The packet being sent is only to get a response from the target system. Specific
values returned are used to determine the specific OS since each OS handles the
packets in different ways.
TCP
Some packets are sent to open or closed ports with specific packet settings. Again,
the results will vary depending on the target OS.
The TCP Packets are all sent with varying flags as follows:
1. no flags
2. SYN, FIN, URG and PSH
3. ACK
4. SYN
5. ACK
6. FIN, PSH, and URG
UDP
If the port used on the target system is closed and an ICMP Port Unreachable
message is returned then there is no Firewall.
Now we need to run the actual command to perform an OS Detection. If you have
read any of the other of my NMAP articles then it is best not to perform a PING. To
skip the PING we use the parameter ‘-Pn’. To see the extra information we may
require you should use the ‘-v’ parameter for adding verbosity. Specifically to get
the OS Detection the parameter ‘-O’ is needed.
For the command to complete properly and perform the TCP SYN Scan you need
to perform the command as ROOT. In my case, I will perform the scan on one
system only and not the whole network so the command would be:
The results of the scan are shown in Figure 2. The scan shows that there are
seven open ports using a SYN Stealth Scan.
Features of NS2
1. It is a discrete event simulator for networking research.
Basic Architecture
NS2 consists of two key languages: C++ and Object-oriented Tool Command
Language (OTcl). While the C++ defines the internal mechanism (i.e., a backend) of
the simulation objects, the OTcl sets up simulation by assembling and configuring
the objects as well as scheduling discrete events. The C++ and the OTcl are linked
together using TclCL
Column 11 gives the reason for a packet drop and the various reasons possible for
a packet drop are as follows.
Topology:-
Sender:-
stcp –p 3000 –l 1024 1.0.1.3
stg –u 1024 1.0.1.3
Receiver:-
rtcp –p 3000 –l 1024
rtg –u 3000
Parameters:-
Throughput of incoming
and outgoing Packets
Step1: Drawing topology
1. Select/click the HOST icon on the toolbar and click the left
mouse button on the editor, to place a host on the editor. Repeat the
above procedure and place two other hosts “HOST2” and “HOST3”
on theeditor.
2. Select/click the HUB (or SWITCH) icon on the toolbar and click
the left mouse button on the editor, to place a HUB (or SWITCH) on
the editor.
3. Click on the LINK icon on the toolbar and connect HOST1 to
HUB, HOST2 to HUB and HUB to HOST3
4. Click on the “E” icon on the toolbar to save the current topology e.g: file2.tpl
(Look forthe
******.tpl extension.)
NOTE: Changes cannot / (should not) be done after selecting the “E” icon.
Step2: Configuration
1. Double click the left mouse button while cursor is on HOST1 to open the
HOSTwindow.
2. Select Add button on the HOST window to invoke the command window
and providethe
following command in the command textbox. stcp –p 21 –l 1024 1.0.1.3
5. Select LOG STATISTICS and select checkbox for output throughput in the
MACwindow
7. Double click the left mouse button while cursor is on HOST2 to open the
HOSTwindow.
10. Click NODE EDITOR Button on the HOST window and select
the MAC tab from themodal window that popsup.
11. Select LOG STATISTICS and select checkbox for output throughput in the
MACwindow
12. Click OK button on the MAC window to exit and once again
click on the OK button on the HOST window to exit.
13. Double click the left mouse button while cursor is on HOST3 to open the
HOSTwindow.
17. Click NODE EDITOR Button on the HOST window and select
the MAC tab from themodal window that popsup.
18. Select LOG STATISTICS and select checkbox for input and
output throughput in theMAC window
19. Click OK button on the MAC window to exit and once again
click on the OK button onthe HOST window to exit.
Step3: Simulate
i. Click “R” icon on the toolbar
ii. Select Simulation in the menu bar and click/ select RUN in
the dropdown list to execute the simulation.
iii. To start playback select “►” icon located at the bottom right corner of
theeditor.
Name of the Program: Simulate to Find the Number of Packets Dropped due
toCongestion.
Topology:-
Sender:-
stcp –p 2000 –l 1024 1.0.1.4
Receiver:-
rtcp –p 2000 –l 1024 Command
Console:-
Goto tools-> simulation time and change Simulation time to 100.
During run mode, double click host 2 and then click command
console. And execute the following command.
Drop Packets and Collision Packets.
Step1: Drawing topology
2. Select/click the SUBNET icon on the toolbar and click the left
mouse button on the editor, to place a SUBNET on theeditor.
3. A pop up window appears requesting the number of
nodes and radius for the subnet Set number ofnodes=6;
Set radius of subnet >150
4. Click on the “E” icon on the toolbar to save the current topology e.g: file4.tpl
(Look forthe
******.tpl extension.)
NOTE: Changes cannot / (should not) be done after selecting
Step2 : Configuration
5. Double click the left mouse button while cursor is on a HOST to open the
HOSTwindow.
10. Also click NODE EDITOR Button on the HOST window and select
the MAC tab from themodal window that popsup.
11. Select LOG STATISTICS and select checkbox for drop and
collision log statistics in theMAC window
12. Click OK button on the MAC window to exit and once again
click on the OK button onthe HOST window to exit.
Step3: Simulate
i. Click “R” icon on the toolbar
ii. Select Simulation in the menu bar and click/ select RUN in
the dropdown list to execute the simulation.