[go: up one dir, main page]

0% found this document useful (0 votes)
60 views61 pages

CN Lab Manual On 23-09-2021

Uploaded by

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

CN Lab Manual On 23-09-2021

Uploaded by

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

III B.

Tech I SEM
Computer Networks Lab List of Experiments

LIST OF EXPERIMENTS SOFTWARES/ ISSUED SUBMISSION


TOOLS USED DATE DATE
1. Implement the data link layer framing Turbo C 10-12-2020 14-12-2020
methods such as character, character-
stuffing and bit stuffing
2. Write a program to compute CRC code Turbo C 14-12-2020 18-12-2020
for the polynomials CRC-12, CRC-16 and
CRC CCIP
3. Develop a simple data link layer that Turbo C 18-12-2020 22-12-2020
performs the flow control using the sliding
window protocol, and loss recovery using
the Go-Back-N mechanism.
4. Implement Dijsktra’s algorithm to Turbo C 22-12-2020 26-12-2020
compute the shortest path through a
network
5. Take an example subnet of hosts and Turbo C 26-12-2020 30-12-2020
obtain a broadcast tree for the subnet
6. Implement distance vector routing Turbo C 30-12-2020 2-01-2021
algorithm for obtaining routing tables at
each node
7. Implement data encryption and data Turbo C 02-01-2021 06-01-2021
decryption
8. Write a program for congestion control Turbo C 06-01-2021 09-01-2021
using Leaky bucket algorithm
9. Write a program for frame sorting Turbo C 09-01-2021 13-01-2021
technique used in buffers
10. Wireshark 15-01-2021 19-01-2021
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters
11. How to run Nmap scan 19-01-2021 22-01-2021
12. Operating System Detection using 22-01-2021 27-01-2021
Nmap
13. Do the following using NS2 Simulator 27-01-2021 31-01-2021
i. NS2 Simulator-Introduction ii. Simulate
to Find the Number of Packets Dropped
iii. Simulate to Find the Number of
Packets Dropped by TCP/UDP iv.
Simulate to Find the Number of Packets
Dropped due to Congestion v. Simulate to
Compare Data Rate& Throughput. vi.
Simulate to Plot Congestion for Different
Source/Destination vii. Simulate to
Determine the Performance with respect
to Transmission of Packets

EXPERIMENT NO: 1

Aim: Implement the datalink layer framing methods such as


character and bit stuffing.

Purpose of Bit Stuffing

In Data Link layer, the stream of bits from the physical layer is divided into data frames. The
data frames can be of fixed length or variable length. In variable - length framing, the size of
each frame to be transmitted may be different. So, a pattern of bits is used as a delimiter to mark
the end of one frame and the beginning of the next frame. However, if the pattern occurs in the
message, then mechanisms needs to be incorporated so that this situation is avoided.

The two common approaches are −

● Byte - Stuffing − A byte or character is stuffed in the message to differentiate from the
delimiter for synchronization. This is also called character-oriented framing.
● Bit - Stuffing − 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.

In bit-oriented protocols, the message is coded as a sequence of bits, which are interpreted in the
upper layers as text, graphics, audio, video etc. A frame has the following parts −

● Frame Header − It contains the source and the destination addresses of the frame.
● Payload field − It contains the message to be delivered.
● Trailer − It contains the error detection and error correction bits.
● Flags − A bit pattern that defines the beginning and end bits in a frame. It is generally of 8-
bits. Most protocols use the 8-bit pattern 01111110 as flag.
Bit Stuffing Mechanism

In a data link frame, the delimiting flag sequence generally contains six or more consecutive 1s.
In order to differentiate the message from the flag in case of the same sequence, a single bit is
stuffed in the message. Whenever a 0 bit is followed by five consecutive 1bits in the message, an
extra 0 bit is stuffed at the end of the five 1s.

When the receiver receives the message, it removes the stuffed 0s after each sequence of five 1s.
The un-stuffed message is then sent to the upper layers.
Purpose of Byte Stuffing

In Data Link layer, the stream of bits from physical layer is divided into data frames. The data
frames can be of fixed length or variable length. In variable – length framing, the size of each
frame to be transmitted may be different. So, a pattern of bits is used as a delimiter to mark the
end of one frame and the beginning of the next frame. However, if the pattern occurs in the
message, then mechanisms needs to be incorporated so that this situation is avoided.

Frame in a Character – Oriented Framing


In character – oriented protocols, the message is coded as 8-bit characters, using codes like
ASCII codes.

A frame has the following parts −

● Frame Header − It contains the source and the destination addresses of the frame.
● Payload field − It contains the message to be delivered.
● Trailer − It contains the error detection and error correction bits.
● Flags − 1- byte (8-bits) flag at the beginning and at end of the frame. It is a protocol –
dependent special character, signalling the start and end of the frame.

Byte Stuffing Mechanism

If the pattern of the flag byte is present in the message byte, there should be a strategy so that the
receiver does not consider the pattern as the end of the frame. In character – oriented protocol,
the mechanism adopted is byte stuffing.

In byte stuffing, a special byte called the escape character (ESC) is stuffed before every byte in
the message with the same pattern as the flag byte. If the ESC sequence is found in the message
byte, then another ESC byte is stuffed before it.
Implement the data link layer farming methods such as character, character stuffing, and bit
stuffing.

Program:(bit-stuffing)

#include<stdio.h>
main()
{
int i,k,a,count=0;
char c[50];
printf("\nEnter the data to be send:");
fflush(stdin);
gets(c);
a=strlen(c);
for(i=0;i<a;i++)
{
if(c[i]=='1')
{
count++;
if(count==6)
{
for(k=a;k>i+1;k--)
c[k]=c[k-1];
a=a+1;
count=0;
c[i+1]='0';
}
}
else
count=0;
}
printf("\nData after stuffing:");
puts(c);
count=0;
for(i=0;i<a;i++)
{
if(c[i]=='1')
{
count++;
if(count==5)
{
for(k=i+1;k<a;k++)
c[k]=c[k+1];
count=0;
}
}
}
}

Output:

Enter the data to be send:11111111


Data after stuffing:111110111
Program:(character-stuffing)

#include<stdio.h>
main()
{
char a[50],b[50],c[50];
int i,j,k,m=0,count,l1,l2;
printf("\nEnter the data to send:");
fflush(stdin);
gets(a);
printf("\nEnter the delimeter:");
fflush(stdin);
gets(b);
l1=strlen(a);
l2=strlen(b);
c[0]='S';
for(i=0;i<l2;i++)
c[i+1]=b[i];
for(j=0;j<l1;j++)
{
count=0;
for(k=0,m=j;k<l2;k++,m++)
{
if(a[m]!=b[k])
count=1;
}
if(count==0)
{
for(k=l1+l2-1;k>m;k--)
a[k]=a[k-l2];
for(k=0;k<l2;k++)
a[m++]=b[k];
j=j+l2;
l1=l1+l2;
}
}
for(k=0;k<l1;k++)
c[++i]=a[k];
for(k=0;k<l2;k++)
c[++i]=b[k];
c[++i]='E';
printf("\nData after stuffing:");
for(k=0;k<=i;k++)
printf("%c",c[k]);
for(j=0;j<l1-l2;j++)
{
count=0;
m=j;
for(k=0;k<l2;k++)
{
if(a[m++]!=b[k])
count=1;
}
if(count==0)
{
for(k=m;k<l1-l2;k++)
a[k]=a[k+l2];
l1=l1-l2;
}
}
printf("\nData after destuffing:");
for(i=0;i<l1;i++)
printf("%c",a[i]);
}

Output:

Enter the data to send : hi hello hai


Enter the delimeter: l
Data after stuffing: slhi hellllo hai le
Data after destuffing: hi hello hai

EXPERIMENT NO: 2

Aim: write a c-program for implement the Cyclic Redundancy Check(CRC).

Cyclic redundancy check (CRC):

Error:
A condition when the receiver’s information does not matches with the sender’s
information. During transmission, digital signals suffer from noise that can introduce
errors in the binary bits travelling from sender to receiver. That means a 0 bit may
change to 1 or a 1 bit may change to 0.
Error Detecting Codes (Implemented either at Data link layer or Transport Layer
of OSI Model):
Whenever a message is transmitted, it may get scrambled by noise or data may
get corrupted. To avoid this, we use error-detecting codes which are additional data
added to a given digital message to help us detect if any error has occurred during
transmission of the message.
Basic approach used for error detection is the use of redundancy bits, where additional
bits are added to facilitate detection of errors.

Cyclic redundancy check (CRC)


● Unlike checksum scheme, which is based on addition, CRC is based on binary
division.
● In CRC, a sequence of redundant bits, called cyclic redundancy check bits, are
appended to the end of data unit so that the resulting data unit becomes exactly
divisible by a second, predetermined binary number.
● At the destination, the incoming data unit is divided by the same number. If at this
step there is no remainder, the data unit is assumed to be correct and is therefore
accepted.
● A remainder indicates that the data unit has been damaged in transit and therefore
must be rejected.

EXAMPLE
ALGORITHM FOR CRC

BEGIN
step 1: declare i,j,keylen,mesglen and char arrays
input[100],temp[30],quot[100],rem[30],key[30],key1[30];
step 2: enter the data and scan the data into input[]
step 3: enter the key and scan the data into key[]
step 4: save the length of input in msglen and length of key in keylen
step 5: copy key[] to key1[]
step 6: initialize i=0 and repeat step 7 until i<keylen-1
step 7: input[msglen+i]=0 and inc i
step 8: initialize i=0 and repeat step 9 until i<keylen
step 9: temp[i]=input[i] and inc i
step 10: initialize i=0 and repeat step 11-22 until i<msglen
step 11: quot[i]=temp[0] and inc i
step 12: if quot[i] equal to 0 then goto step 13 else step 15
step 13: initialize j=0 and repeat step 14 until j<keylen and goto step 17
step 14: key[j]=0 and inc j
step 15: initialize j=0 and repeat step 16 until j<keylen
step 16: key[j]=key1[j] and inc j
step 17: initialize j=keylen-1 and repeat step 18-20 until j>0
step 18: if temp[j] equal to key[j] goto step 19 else step 20
step 19: rem[j-1]='0' and dec j and goto step 21
step 20: rem[j-1]='1' and dec j
step 21: rem[keylen-1]=input[i+keylen]
step 22: copy rem to temp[]
step 23: copy temp[] to rem[]
step 24: print the final data from input[] and rem[]

step 25: enter the frame and save in input


step 26: initialize j=0 and repeat step 27 until j<=msglen and then k=0
step 27: rem[j]=input[j] and inc j
step 28: initialize i=msglen and repeat step 29 until i<=keylen
step 29: if i>msglen then initialize j=0 and repeat step 30 until j<msglen
step 30: rem[j]=rem[j+1] and rem[j]=input[j] and inc j
step 31: if rem[0] then quot[k++]=1 else quot[k++]=0
step 32: initialize j=0 and repeat step 32 until j<=msglen
step 33: rem[j]=rem[j]^input[j]
step 34: print quotient and reminder
step 35: initialize i=1 repeat step 36 until i<=msglen
step 36: if rem[i] then flag=0
step 37: if flag is positive print "no error" else print "error"
END

Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,k=0;
int flag=1,a[16],g[16],r[20],div[16],n,m;
system(“clear”);
printf(“Enter degree of generator:”);
scanf(“%d”,&n);
printf(“\n Enter the generator:\n”);
for(i=0;i<=n;i++)
scanf(“%d”,&g[i]);
printf(“\nEnter the degree of the frame:”);
scanf(“%d”,&m);
printf(“Enter the frame:\n”);
for(i=0;i<=m;i++)
scanf(“%d”,&a[i]);
if(m<n || g[0]==0)
{
printf(“Not a proper generator:\n”);
exit(0);
}
for(i=m+1;i<=m+n;i++)
a[i]=0;
for(j=0;j<=n;j++)
r[j]=a[j];
for(i=n;i<=m+n;i++)
{
if(i>n)
{
for(j=0;j<n;j++)
r[j]=r[j+1];
r[j]=a[i];
}
if(r[0])
div[k++]=1;
else
{
div[k++]=0;
continue;
}
for(j=0;j<=n;j++)
r[j]=r[j]^g[j];
}
printf(“\n Quotient is:”);
for(j=0;j<k;j++)
printf(“%d”,div[j]);
printf(“\n Remainder is:”);
for(i=1;i<=n;i++)
printf(“%d”,r[i]);
printf(“\n Transmitted frame is:”);
for(i=m+1,j=1;i<=m+n;i++)
printf(“%d”,a[i]);
printf(“\n”);
printf(“\n Enter the degree of frame:”);
scanf(“%d”,&m);
printf(“Enter the frame:\n”);
for(i=0;i<=m;i++)
scanf(“%d”,&a[i]);
for(j=0;j<=n;j++)
r[j]=a[j];
k=0;
for(i=n;i<=m;i++)
{
if(i>n)
{
for(j=0;j<n;j++)
r[j]=r[j+1];
r[j]=a[i];
}
if(r[0])
div[k++]=1;
else
{
div[k++]=0;
continue;
}
for(j=0;j<=n;j++)
r[j]=r[j]^g[j];
}
printf(“\n Quotient is:”);
for(j=0;j<k;j++)
printf(“%d”,div[j]);
printf(“\n Remainder is:”);
for(i=1;i<=n;i++)
printf(“%d”,r[i]);
for(i=1;i<=n;i++)
{
if(r[i])
flag=0;
}
if(flag)
printf(“\n No Error \n”);
else
printf(“\n Error”);
return 0;
}

OUTPUT:
Enter the degree of the generator:3
Enter the generator:
1001
Enter the degree of the frame:7
Enter the frame :
1
1
1
1
1
1
1
1
Quotient is :11100011
Remainder is:011
Transmitted frame is:11111111011

Enter the degree of the frame:10


Enter the frame:
1
1
1
1
1
1
1
1
0
1
1
Quotient is:11100011
Remainder is:000
No Error

EXPERIMENT NO: 3

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.

Sliding Window Protocol :

In computer networks sliding window protocol is a method to transmit data on a network. 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.

As soon as sender receives the acknowledgement of a frame it is replaced by the next frames to be
transmitted by the sender. If receiver sends a collective or cumulative acknowledgement to sender then it
understands that more than one frames are properly received, for eg:- if ack of frame 3 is received it
understands that frame 1 and frame 2 are received properly.

Image Source

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)

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.
Program:
#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 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
EXPERIMENT NO: 4

Aim: write a c-program for implement the dijikstra’s Algorithm.

Dijikstra’s Algorithm (Theory):


Dijkstra's Algorithm allows you to calculate the shortest path between one node
(you pick which one) and every other node in the graph. You'll find a description
of the algorithm at the end of this page, but, let's study the algorithm with an
explained example! Let's calculate the shortest path between node C and the
other nodes in our graph:

During the algorithm execution, we'll mark every node with its minimum
distance to node C (our selected node). For node C, this distance is 0. For the
rest of nodes, as we still don't know that minimum distance, it starts being infinity
(∞):
We'll also have a current node. Initially, we set it to C (our selected node). In the
image, we mark the current node with a red dot.

Now, we check the neighbours of our current node (A, B and D) in no specific
order. Let's begin with B. We add the minimum distance of the current node (in
this case, 0) with the weight of the edge that connects our current node with B (in
this case, 7), and we obtain 0 + 7 = 7. We compare that value with the minimum
distance of B (infinity); the lowest value is the one that remains as the minimum
distance of B (in this case, 7 is less than infinity):

So far, so good. Now, let's check neighbour A. We add 0 (the minimum distance
of C, our current node) with 1 (the weight of the edge connecting our current
node with A) to obtain 1. We compare that 1 with the minimum distance of A
(infinity), and leave the smallest value:

OK. Repeat the same procedure for D:


Great. We have checked all the neighbours of C. Because of that, we mark it
as visited. Let's represent visited nodes with a green check mark:

We now need to pick a new current node. That node must be the unvisited
node with the smallest minimum distance (so, the node with the smallest
number and no check mark). That's A. Let's mark it with the red dot:
And now we repeat the algorithm. We check the neighbours of our current node,
ignoring the visited nodes. This means we only check B.

For B, we add 1 (the minimum distance of A, our current node) with 3 (the weight
of the edge connecting A and B) to obtain 4. We compare that 4 with the
minimum distance of B (7) and leave the smallest value: 4.

Afterwards, we mark A as visited and pick a new current node: D, which is


the non-visited node with the smallest current distance.

We repeat the algorithm again. This time, we check B and E.

For B, we obtain 2 + 5 = 7. We compare that value with B's minimum distance (4)
and leave the smallest value (4). For E, we obtain 2 + 7 = 9, compare it with the
minimum distance of E (infinity) and leave the smallest one (9).

We mark D as visited and set our current node to B.


Almost there. We only need to check E. 4 + 1 = 5, which is less than E's
minimum distance (9), so we leave the 5. Then, we mark B as visited and set E
as the current node.

E doesn't have any non-visited neighbours, so we don't need to check anything.


We mark it as visited.

As there are not univisited nodes, we're done! The minimum distance of each
node now actually represents the minimum distance from that node to node
C (the node we picked as our initial node)!
Here's a description of the algorithm:

1. Mark your selected initial node with a current distance of 0 and the rest
with infinity.
2. Set the non-visited node with the smallest current distance as the current node C.
3. For each neighbour N of your current node C: add the current distance of C with
the weight of the edge connecting C-N. If it's smaller than the current distance of
N, set it as the new current distance of N.
4. Mark the current node C as visited.
5. If there are non-visited nodes, go to step 2.

Program:

#include<stdio.h>
#include<stdlib.h>
//#include<string.h>
#define M_NODES 10
#define INFINITY 1000
int n,dis[M_NODES][M_NODES];
void main()
{
void spath(int,int);
int i,j,g[100][100],s,t,c; printf("\
nEnter number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
printf("\nEnter weight between %d-->%d\t",i,j);
scanf("%d",&c);
if(c>=0)
dis[i][j]=c;
else
exit(0);
}
}
printf("\nEnter source and destination nodes:");
scanf("%d%d",&t,&s);
spath(s,t);
//getch();
}
void spath(int s,int t)
{
struct state
{
int pre,len;
enum{permanet,tentative} label;
}state[M_NODES];
int i,k,min,path[20],c=0;

struct state *p;


for(p=&state[0];p<&state[n];p++)
{
p->pre=-1;
p->len=INFINITY;
p->label=tentative;
}
state[t].len=0;
state[t].label=permanet;
k=t;
do
{
for(i=0;i<n;i++)
if(dis[k][i]!=0&&state[i].label==tentative)
{
if(state[k].len+dis[k][i]<state[i].len)
{
state[i].pre=k;
state[i].len=state[k].len+dis[k][i];
}
}
k=0;
min=INFINITY;
for(i=0;i<n;i++)
{
if(state[i].label==tentative&&state[i].len<min )
{
min=state[i].len;
k=i;
}
}
state[k].label=permanet;
}while(k!=s);
i=0;
k=s;
do
{
path[i++]=k;
k=state[k].pre;
c++;
}while(k>=0);
printf("\nMinimum Cost is=%d",min);
printf("\nShortest path is=");
for(k=c-1;k>=0;k--)
printf("%d-->",path[k]);
}

Output:
EXPERIMENT NO: 5

AIM: Implement broadcast tree for a given subnet of hosts

THEORY:
Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree
and connects all the vertices together. A single graph can have many different spanning
trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted,
connected and undirected graph is a spanning tree with weight less than or equal to the weight of
every other spanning tree. The weight of a spanning tree is the sum of weights given to each
edge of the spanning tree.
A minimum spanning tree has (V – 1) edges where V is the number of vertices in the given
graph.

the steps for finding MST using Kruskal’s algorithm

1. Sort all the edges in non-decreasing order of their weight.


2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed
so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
the steps for finding MST using Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed
so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.

Now pick all edges one by one from sorted list of edges
1. Pick edge 7-6: No cycle is formed, include it.

2. Pick edge 8-2: No cycle is formed, include it.

3. Pick edge 6-5: No cycle is formed, include it.

4. Pick edge 0-1: No cycle is formed, include it.

5. Pick edge 2-5: No cycle is formed, include it.

6. Pick edge 8-6: Since including this edge results in cycle, discard it.

7. Pick edge 2-3: No cycle is formed, include it.


8. Pick edge 7-8: Since including this edge results in cycle, discard it.

9. Pick edge 0-7: No cycle is formed, include it.

10. Pick edge 1-2: Since including this edge results in cycle, discard it.
11. Pick edge 3-4: No cycle is formed, include it.

ALGORITHM
step 1: declare variable as int p,q,u,v,n;
step 2: Initialize min=99,mincost=0;
step 3: declare variable as int t[50][2],i,j;
step 4: declare variable as int parent[50],edge[50][50];
step 5: Begin
step 6: write "Enter the number of nodes"
step 7: read "n"
step 8: Initialize i=0
step 9: repeat step(10-12) until i<n
step10: increment i
step11: write"65+i"
step12: Initialize parent[i]=-1
step13:wite "\n"
step14: Initialize i=0
step15: repeat step(15-21) until i<n
step16: increment i
step17: write"65+i"
step18: Initialize j=0
step19: repeat until j<n
step20: increment j
step21: read edge[i][j]

step22: Initialize i=0


step23: repeat step(23-43) until i<n
step24: increment i
step25: Initialize j=0
step26: repeat until j<n
step27: increment j
step28: if'edge[i]j]!=99
step29: if'min>edge[i][j] repeat step (29-32)
step30: intialize min=edge[i][j]
step31: intialize u=i
step32: intialize v=j
step33: calling function p=find(u);
step34: calling function q=find(v);
step35: if'P!=q repeat steps(35-39)
step36: intialize t[i][0]=u
step37: intialize t[i][1]=v
step38: initialize mincost=mincost+edge[u][v]
step39: call function sunion(p,q)
step40: else repeat steps(40-42)
step41: Intialize t[i][0]=-1;
step42: Intialize t[i][1]=-1;
step43: intialize min=99;
step44; write"Minimum cost is %d\n Minimum spanning tree is",mincost
step45: Initialize i=0
step46: repeat until i<n
step47: increment i
step48: if't[i][0]!=-1 && t[i][1]!=-1'repeat step(48-50)
step49: write "%c %c %d", 65+t[i][0], 65+t[i][1], edge[t[i][0]][t[i][1]]
step50: write"\n"
step51: end
step52: called function sunion(int l,int m) repeat step(51-52)
step53: intialize parent[l]=m
step54: called function find(int l) repeat step(53-56)
step55: if parent([l]>0)
step56: initialize l=parent
step57: return l

PROGRAM:

#include<stdio.h>
int p,q,u,v,n;
int min=99,mincost=0;
int t[50][2],i,j;
int parent[50],edge[50][50];
main()
{
printf("\n Enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\t",65+i);
parent[i]=-1;
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i][j];
u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{ t[i]
[0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
t[i][1]=-1;
}
min=99;
}
printf("Minimum cost is %d\n Minimum spanning tree is\n" ,mincost);
for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf("%c %c %d", 65+t[i][0], 65+t[i][1],
edge[t[i][0]][t[i][1]]);
printf("\n");
}
}
sunion(int l,int m)
{
parent[l]=m;
}
find(int l)
{
if(parent[l]>0)
l=parent[l];
return l;
}
OUTPUT
Enter the number of nodes 4

A B C D
A 1 3 5 6
B 6 7 8 9
C 2 3 5 6
D 1 2 3 7
Minimum cost is 9
Minimum spanning tree is
BA6
CA2
DA1

EXPERIMENT NO: 6

Aim: Take an example subnet graph with weights indicating delay between nodes.
Now obtain routing table at each node using vector routing algorithm.

Theory:

https://www.gatevidyalay.com/distance-vector-routing-routing-algorithms/

Program:

#include<stdio.h>
#include<conio.h>
#define MN 100
#define INFINITY 500

char graph[30][30];

struct st
{
int a[20][20];
}st[20];

int n;
void main()
{
int i,j;
void disk(int,int);
//clrscr();
printf("\nEnter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
printf("\nEnter distance between %d -->%d",i,j);
scanf("%d",&graph[i][j]);
graph[j][i]=graph[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
disk(i,j);
}
}
for(i=0;i<n;i++)
{
printf("\nRouting table for node %d",i);
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("%d | %d | %d",j,st[i].a[j][i],st[i].a[j][i+1]);
printf("\n \n");
}
}
}
//getch();
}
void disk(int t,int s)
{
struct state
{
int predecessor;
int length;
enum{permanent,tentative} label;
}
state[MN];

struct state *p;


int path[MN];
int i,j,k,min;
for(p=&state[0];p<&state[n];p++)
{
p->predecessor=-1;
p->length=INFINITY;
p->label=tentative;
}
state[t].length=0;
state[t].label=permanent;
k=t;
do
{
for(i=0;i<n;i++)
{
if((graph[k][i]!=0)&&(state[i].label==tentative))
{
if(state[k].length+graph[k][i]<state[i].length)
{
state[i].predecessor=k;
state[i].length=state[k].length+graph[k][i];
}
}
}
k=0;
min=INFINITY;
for(i=0;i<n;i++)
{
if((state[i].label==tentative)&&(state[i].length<min))
{
min=state[i].length;
k=i;
}
}
state[k].label=permanent;
}while(k!=s);
i=0;
k=s;
do
{
path[i++]=k;
k=state[k].predecessor;
}while(k>=0);
st[t].a[s][t]=path[i-2];
st[t].a[s][t+1]=state[s].length;
}

Output
:
EXPERIMENT NO: 7

AIM: Take a 64 bit playing text and encrypt the same using DES
algorithm.
The Data Encryption Standard (DES) is a symmetric-key block cipher published by the National
Institute of Standards and Technology (NIST).

DES is an implementation of a Feistel Cipher. It uses 16 round Feistel structure. The block size
is 64-bit. Though, key length is 64-bit, DES has an effective key length of 56 bits, since 8 of the
64 bits of the key are not used by the encryption algorithm (function as check bits only). General
Structure of DES is depicted in the following illustration −
Since DES is based on the Feistel Cipher, all that is required to specify DES is −
· Round function
· Key schedule
· Any additional processing − Initial and final permutation

Initial and Final Permutation

The initial and final permutations are straight Permutation boxes (P-boxes) that are inverses of
each other. They have no cryptography significance in DES. The initial and final permutations
are shown as follows −
Round Function

The heart of this cipher is the DES function, f. The DES function applies a 48-bit key to the
rightmost 32 bits to produce a 32-bit output.
Expansion Permutation Box − Since right input is 32-bit and round key is a 48-bit,· we
first need to expand right input to 48 bits. Permutation logic is graphically depicted in the
following illustration −

The graphically depicted permutation logic is generally described as table in DES· specification
illustrated as shown −
XOR (Whitener). − After the expansion permutation, DES does XOR operation on the
expanded right section and the round key. The round key is used only in this operation.
Substitution Boxes. − The S-boxes carry out the real mixing (confusion). DES uses· 8
S-boxes, each with a 6-bit input and a 4-bit output. Refer the following illustration −

The S-box rule is illustrated below −


· There are a total of eight S-box tables. The output of all eight s-boxes is then· combined in
to 32 bit section.

· Straight Permutation − The 32 bit output of S-boxes is then subjected to the


straight· permutation with rule shown in the following illustration:
Key Generation

The round-key generator creates sixteen 48-bit keys out of a 56-bit cipher key. The process of
key generation is depicted in the following illustration −

The logic for Parity drop, shifting, and Compression P-box is given in the DES description.
DES Analysis

The DES satisfies both the desired properties of block cipher. These two properties make cipher
very strong.
· Avalanche effect − A small change in plaintext results in the very great change in
the ciphertext.
· Completeness − Each bit of ciphertext depends on many bits of plaintext.
ALGORITHM(DES):
Begin
Step 1: Declare integer i,ch,lp
Step 2: Declare character array’s cipher[50],plain[50] and character key
Step 3: while(TRUE) do step(4-31)
Step 4: write(“\n- - -MENU- - -\n”)
Step 5: write(“\n1:Data encryption\t\n2:Data Decryption\t\n\n:Exit”)
Step 6: write(“\n\nEnter your choice:”)
Step 7: scanf(“%d”,&ch);
Step 8: switch(ch) do
Step 9: case 1:
Step 10:write(“\nData Encryption”)
Step 11:write(“\nEnter plain text”)
Step 12:fflush(stdin);
Step 13:Input the string into plain to encrypt
Step 14:write(“\nEnter theencryption key:”)
Step 15:input the value into key
Step 16:load the length of the key into lp
Step 17:fori=0 to strlen(plain)-1 step do
Step 18:cipher[i]=plain[i]^lp;
Step 19:cipher[i]=’\0’;
Step 20:write(“\nEncrypted text is:”)
Step 21:puts(cipher)
Step 22:break;
Step 23:case 2:
Step 24:write(“\nDataDecryption”)
Step 25:fori=0 to strlen(plain)-1 step do
Step 26:plain[i]=cipher[i]^lp;
Step 27:write(“\nDecrypted text is:”)
Step 28:puts(cipher);
Step 29:break;
Step 30:case 3:
Step 31:exit(0);
Program:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i, ch, lp;
char cipher[50],plain[50];
char key[50];
while(1)
{
printf("\n-----MENU- - - -\n");
printf("\n1:Data Encryption\t\n\n2:Data Decryption\t\n\n3:Exit");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nData Encryption");
printf("\nEnter the plain text:");
fflush(stdin);
scanf("%s",&plain);
printf("\nEnter the encryption key:");
scanf("%s",&key);
lp=strlen(key);
for(i=0;plain[i]!='\0';i++)
cipher[i]=plain[i]^lp
break;
cipher[i]='\0';
printf("\nThe encrypted text is:"); puts(cipher);
case 2: printf("\nData decryption");
for(i=0;cipher[i]!='\0';i++)
plain[i]=cipher[i]^lp;
printf("\nDecrypted text is:");
puts(plain);
break;
case 3: exit(0);
}
}
}
OUTPUT:
EXPERIMENT NO: 7

NAME OF THE EXPERIMENT: Decrypting DES.


AIM: Write a program to break the above DES coding

THEORY:
Data encryption standard was widely adopted by the industry in security products. Plain text is
encrypted in blocks of 64 bits yielding 64 bits of cipher text. The algorithm which is
parameterized by a 56 bit key has 19 distinct stages. The first stage is a key independent
transposition and the last stage is exactly inverse of the transposition. The remaining stages are
functionally identical but are parameterized by different functions of the key. The algorithm has
been designed to allow decryption to be done with the same key as encryption

Program:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char pwd[20];
char alpha[26]="abcdefghijklmnopqrstuvwxyz";
int num[20],i,n,key;
printf("\nEnter the password:");
scanf("%s",&pwd);
n=strlen(pwd);
for(i=0;i<n;i++)
num[i]=toascii(tolower(pwd[i]))-'a';
printf("\nEnter the key:");
scanf("%d",&key); for(i=0;i<n;i+
+) num[i]=(num[i]+key)%26;
for(i=0;i<n;i++)
pwd[i]=alpha[num[i]]; printf("\
nThe key is:%d",key);
printf("\nEncrypted text is:%s",pwd);
for(i=0;i<n;i++)
{
num[i]=(num[i]-key)%26;
if(num[i]<0)
num[i]=26+num[i];
pwd[i]=alpha[num[i]];
}
printf("\nDecrypted text is:%s",pwd);
}

Output:

8. Write a program for congestion control using Leaky bucket algorithm.

AIM:Implement leaky bucket algorithm for congestion control

THEORY:
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/FLOWCHART:

Step 1: Start

Step 2: Set the bucket size or the buffer size.

Step 3: Set the output rate.

Step 4: Transmit the packets such that there is no overflow.

Step 5: Repeat the process of transmission until all packets are transmitted. (Reject packets where its size is greater
than the bucket size)

Step 6: Stop

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

#define NOF_PACKETS 10

intrand(int a)
{
intrn = (random() % 10) % a;
returnrn == 0 ? 1 :rn;
}

intmain()
{
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);
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);
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!!");
}
}
}
}
}

Output
8. Write a program for frame sorting technique used in buffers.

Theory

The data link layer divides the stream of bits received from the network layer into manageable
data units called frames.If frames are to be distributed to different systems on the network, the Data link layer adds
a header to the frame to define the sender and/or receiver of the frame.
Each Data link layer has its own frame format. One of the fields defined in the format is the
maximum size of the data field. In other words, when datagram is encapsulated in a frame, the
total size of the datagram must be less than this maximum size, which is defined by restriction
imposed by the hardware and software used in the network.

The value of MTU differs from one physical network to anotherIn order to make IP protocol
portable/independent of the physical network, the packagers decided to make the maximum length of the IP
datagram equal to the largest Maximum Transfer Unit (MTU) defined so far. However for other physical networks
we must divide the datagrams to make it possible to pass through these networks. This is called fragmentation.

When a datagram is fragmented, each fragmented has its own header. A fragmented datagrammay itself be
fragmented if it encounters a network with an even smaller MTU. In another words, a datagram may be
fragmented several times before it reached the final destination and also, the datagrams referred to as (frames in
Data link layer) may arrives out of order at destination. Hence sorting of frames need to be done at the destination
to recover the original data.

ALGORITHM/FLOWCHART:

Step 1: Start
Step 2: Enter the Message to be transmitted(read in msg)
Step3:Caluculate no of packets
Step4: Goto Step 5,6,7,8
Step5:void shuffle(intNoOfPacket)
Step 5.1: Calculate status,transdata
Step 5.2:for i = 0; i <NoOfPacket;
Step 5.2.1: calculate trans
trans = rand()%NoOfPacket;
Step 5.2.2: if Status[trans]!=1
Copy read data in transmitted data and goto step 6
Step 6: void receive(intNoOfPacket)
Step 6.1:Print the packets received
Step 6.2: Print the frame in sorted order go to Step 7
Step 7: void sortframes(intNoOfPacket)
Step 7.1 : if transdata[j].SeqNum>transdata[j + 1].SeqNumgoto 7.1.1
Step 7.1.1: Sort the packets
Step 8: Stop

Source Code

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define FSize 3
typedefstruct packet
{
intSeqNum;
char Data[FSize+1];
}packet;

struct packet *readdata, *transdata;


int divide(char *msg)
{
intmsglen, 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);
returnNoOfPacket;
}

void shuffle(intNoOfPacket)
{
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);
}
voidsortframes(intNoOfPacket)
{
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(intNoOfPacket)
{
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;
intNoOfPacket;
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
4 s n
5 ice
6 me
7 eti
8 ng
9 u o
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

10. Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters.
i. Packet Capture Using 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.

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.

4. Click the Start Capture button to begin the capture.


5. When you are finished capturing, click the Stop button.

ii. Starting Wire shark

You can start Wireshark from the command line, but it can also be started from most Window managers as well. In
this section we will look at starting it from the command line.
Wireshark supports a large number of command line parameters. To see what they are, simply enter the
command wireshark -h and the help information shown in Help information available from Wireshark (or something
similar) should be printed.

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, “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.
Figure 1. Wireshark with a TCP packet selected for viewing
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.
In addition you can view individual packets in a separate window as shown in Figure , “Viewing a packet in a
separate window”. You can do this by 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.
Figure2. Viewing a packet in a separate window
iv. Analysis and Statistics & Filters.

One of Wireshark’s strengths is its statistical tools. When using Wireshark, we


have various types of tools, starting from the simple tools for listing end-nodes and
conversations, to the more sophisticated tools such as flow and I/O graphs.

To start statistics tools, start Wireshark, and choose Statistics from the main menu.

Using the statistics for

capture file properties menu


1. From the Statistics menu, choose Capture File Properties:

What you will get is the Capture File Properties window (displayed in the following
screenshot).

As you can see in the following screenshot, we have the following:


 File: Provides file data, such as filename and path, length, and so on
 Time: Start time, end time, and duration of capture
 Capture: Hardware information for the PC that Wireshark is installed on
 Interfaces: Interface information—the interface registry identifier on the left, if
capture filter is turned on, interface type and packet size limit
 Statistics: General capture statistics, including captured and displayed
packets:

You might also like