Ec3401-Networks and Security Lab Manual (1)
Ec3401-Networks and Security Lab Manual (1)
OBJECTIVES:
PRACTICAL: 30 PERIOD
TABLE OF CONTENTS
PAGE
S.NO. DATE EXPERIMENT TITLE SIGN.
NO
i) Bit stuffing
Aim::
To write a c program using Implement the Data Link Layer framing methods in Bit
Stuffing.
Algorithm::
STEP-1: Read the Frame Size from the user.
STEP-2: Read the key value from the user.
STEP-3: If the size convert add the loop
values. STEP-4: Values convert the frames
STEP-5: Display the Bit Stuffing obtained above
Program::
#include<stdio.h>
#include<string.h>
int main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example:
8):"); scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
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++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
return 0;
}
Output:
Enter frame size (Example: 8):12
Enter the frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0 0 1
After Bit Stuffing :0101111101001
Result::
Thus, the above program was Successfully completed and verified.
(ii) Character stuffing
Aim::
To write a c program using Implement the Data Link Layer framing methods in Character
Stuffing.
Algorithm::
STEP-1: Read the Frame Size from the user.
STEP-2: Read the Character from the user.
STEP-3: convert the Starting and ending delimiter
STEP-4: Values convert the frames
STEP-5: Display the Character Stuffing obtained above
Program:
#include<stdio.h>
#include<string.h>
main()
{
char a[30], fs[50] = " ", t[3], sd, ed, x[3], s[3], d[3], y[3];
int i, j, p = 0, q = 0;
clrscr();
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter a character that represents ending delimiter:");
scanf(" %c", &ed);
x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0';
y[0] = d[0] = d[1] = ed;
d[2] = y[1] = '\0';
strcat(fs, x);
for(i = 0; i < strlen(a); i++)
{
t[0] = a[i];
t[1] = '\0';
if(t[0] == sd)
strcat(fs, s);
else if(t[0] ==
ed)
strcat(fs, d);
else
strcat(fs, t);
}
strcat(fs, y);
printf("\n After stuffing:%s", fs);
getch();
}
Output:-
Enter characters to be stuffed: goodday
Enter a character that represents starting delimiter:
d Enter a character that represents ending delimiter:
g After stuffing: dggooddddayg.
Result::
Thus, the above program was Successfully completed and verified.
2. Implementation of Error Detection / Correction Techniques
Aim::
To write a c program using Implement of Error Detection / Correction Techniques in
LRC,CRC.
Algorithm::
STEP 1 :Get the data and generator polynomial.
STEP 2 :Let n be the length of the generator polynomial.
STEP 3 :Append n-1 zeros to data.
STEP 4 :Call the CRC function.
STEP 5 :
STEP 6 :If the first bit is 1, then perform a xor operation with the first n bits of data and the
generator polynomial.
STEP 7 :Shift the bits by 1 position to leave the first bit.
STEP 8 :Append a bit from the data. Repeat the process until all the bits in the data are
appended.
i) LRC,
Program:
#include <stdio.h>
int main() {
int l1, bit[101], count = 0, i, choice;
switch (choice) {
case 1:
// Adding even parity
if (count % 2 == 0)
bit[l1] = 0;
else
bit[l1] = 1;
case 2:
// Checking even parity
if (count % 2 == 0)
printf("No error in the received data stream.\n");
else
printf("There is an error in the received data stream.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
return 0;
}
Output:
Enter the length of data stream: 10
Enter the data stream 1 1 0 1 0 1 1 1 0 1
Number of 1's are 7
Enter the choice to implement parity bit
1- Sender side
2- Receiver
side 1
Program::
#include <stdio.h>
#include <string.h>
int main() {
keylen = strlen(key);
msglen = strlen(input);
strcpy(key1, key);
input[msglen + i] = '0';
temp[i] = input[i];
}
for (i = 0; i < msglen; i++) {
quot[i] = temp[0];
if (quot[i] == '0') {
key[j] = '0';
} else {
key[j] = key1[j];
rem[keylen] = '\0';
strcpy(temp, rem);
printf("%c", quot[i]);
printf("%c", rem[i]);
printf("%c", input[i]);
printf("\n");
return 0;
Output::
Enter the Number::
1
0
1
0
0
0
0
1
Enter the divisor
1
0
0
1
The quotient is 10110111 and the remainder is 0111
Result::
Thus, the above program was Successfully completed and verified.
3. Implementation of Stop and Wait, and Sliding Window Protocols
Aim:
To write a c program using Implementation of Stop and Wait, and Sliding Window
Protocols
Algorithm:
Program:
#include <stdio.h>
#include <string.h> // Include for string manipulation functions like strlen
int main() // int main() is the standard return type for main
{
char sender[50], receiver[50];
int i, winsize;
printf("\n ENTER THE WINDOWS SIZE : ");
scanf("%d", &winsize);
printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
printf("\n ENTER THE DATA TO BE SENT: ");
fflush(stdin);
fgets(sender, sizeof(sender), stdin); // Use fgets for safer input
Output::
Enter the windows size : 10
Sender window is expanded to store message or window
Enter the data to be sent: forgetcode.com
Message send by the sender:
forgetcode.com
Window size of receiver is expanded
Acknowledgement from receiver
Ack:5
Message received by receiver is : forgetcode
Window size of receiver is shrinked
Result::
Thus, the above program was Successfully completed and verified.
4. Implementation of Go back-N and Selective Repeat Protocols.
Aim::
To write a c program using Implementation of Go back-N and Selective Repeat Protocols
Algorithm::
STEP 1. Start.
STEP 2. Establish connection (recommended UDP)
STEP 3. Accept the window size from the client(should be <=40)
STEP 4. Accept the packets from the network layer.
STEP 5. Calculate the total frames/windows required.
STEP 6. Send the details to the client(totalpackets,totalframes.)
STEP 7. Initialise the transmit buffer.
STEP 8. Built the frame/window depending on the windowsize.
STEP 9. Transmit the frame.
STEP 10. Wait for the acknowledgement frame.
STEP 11. Close the connection.
Program::
#include<stdio.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent); sent+
+;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);
if(ack == windowsize)
break;
return 0;
}
}
else sent = ack;
Output:-
enter window size
8
Frame 0 has been transmitted.
Frame 1 has been transmitted.
Frame 2 has been transmitted.
Frame 3 has been transmitted.
Frame 4 has been transmitted.
Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.
Result::
Thus, the above program was Successfully completed and verified.
Algorithm::
STEP 1. Open VI-RTSIM software from desktop
STEP 2. Click the Simulation menu bar
STEP 3. Select the “Distance – Vector Routing Algorithm” option from Routing
algorithm menu bar.
STEP Network with routers connected through link is drawn by using option in
editor(add router, join link, delete router, delete link, Add caption to link, add caption to
router)
STEP 5. Select any two nodes to find the shortest distance between them.
STEP 6:Click the Find path Button to run the program.
STEP 7. Now the shortest paths between the two nodes are calculated.
Program::
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes printf("\
nEnter the cost matrix :\n"); for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialise the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and we calculate the direct distance
from the node i to k using the cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
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<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nnode %d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}
/*
Output::
Result::
Thus, the above program was Successfully completed and verified.
6. Implementation of Link State Routing algorithm (Open Shortest Path First) with
5 nodes
(Dijkstra's).
Aim::
To write a c program using Implementation of Link State Routing algorithm (Open
Shortest Path First) with 5 nodes (Dijkstra's).
Algorithm::
STEP 1. Create a simulator object
STEP 2. Define different colors for different data flows
STEP 3. Open a nam trace file and define finish procedure then close the trace file, and execute
nam on trace file.
STEP 4. Create n number of nodes using for loop
STEP 5. Create duplex links between the nodes
STEP 6. Setup UDP Connection between n(0) and n(5)
STEP 7. Setup another UDP connection between n(1) and n(5)
STEP 8. Apply CBR Traffic over both UDP connections
STEP 9. Choose Link state routing protocol to transmit data from sender to receiver.
STEP 10. Schedule events and run the program.
Program::
#include <stdio.h>
#include <limits.h> // Include for INT_MAX
#include <stdbool.h> // Include for bool, true, false
#define V 9
return min_index;
}
return 0;
}
Output::
Result:
Thus, the above program was Successfully completed and verified.
7. Data encryption and decryption using Data Encryption Standard
Aim: algorithm.
To write a c program using Data encryption and decryption using Data Encryption
Standard algorithm.
Algorithm:
STEP-1: Read the 64-bit plain text.
STEP-2: Split it into two 32-bit blocks and store it in two different
arrays. STEP-3: Perform XOR operation between these two arrays.
STEP-4: The output obtained is stored as the second 32-bit sequence and the original second 32-
bit sequence forms the first part.
STEP-5: Thus the encrypted 64-bit cipher text is obtained in this way. Repeat the same process
for the remaining plain text characters.
Program:
//Simple C program to encrypt and decrypt a string
#include <stdio.h>
int main()
{
int i, x;
char str[100];
if (scanf("%d", &x) != 1) {
printf("Invalid input. Please enter a number.\n");
return 1;
}
// Clear the input buffer in case of non-integer input
while (getchar() != '\n');
Output:
#Encryption
#Decryption
Result:
Thus, the above program was Successfully completed and verified.
8. Data encryption and decryption using RSA (Rivest, Shamir and Adleman) algorithm.
Aim::
To write a c program using Data encryption and decryption using RSA (Rivest, Shamir
and Adleman) algorithm
Algorithm::
STEP-1: Select two co-prime numbers as p and q.
STEP-2: Compute n as the product of p and q.
STEP-3: Compute (p-1)*(q-1) and store it in z.
STEP-4: Select a random prime number e that is less than that of
z. STEP-5: Compute the private key, d as e * mod-1(z).
STEP-6: The cipher text is computed as messagee * mod n.
STEP-7: Decryption is done as cipherdmod n.
Program::
#include<stdio.h>
#include<math.h>
int main()
{
//2 random prime numbers
double p = 3;
double q = 7;
double n=p*q;
double count;
double totient = (p-1)*(q-1);
//public key
//e stands for encrypt
double e=2;
//private key
//d stands for decrypt
double d;
return 0;
}
Output:
Result::
Thus, the above program was Successfully completed and verified.
9.Implement Client Server model using FTP protocol.
Aim:
To write a c program using Implement Client Server model using FTP protocol.
Algorithm:
STEP 1. The server starts and waits for filename.
STEP 2. The client sends a filename.
STEP 3. The server receives filename.
If file is present,
server starts reading file
and continues to send a buffer filled with
file contents encrypted until file-end is reached.
STEP 4. End is marked by EOF.
STEP 5. File is received as buffers until EOF is
received. Then it is decrypted.
STEP 6. If Not present, a file not found is sent.
Program:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // Include for the close() function
#define FILENAME "a.txt"
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 65496
// #define BUFSIZ 1024 // Removed the redefinition of BUFSIZ
// Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
fprintf(stderr, "Error: Could not create socket.\n");
return 1;
}
server.sin_addr.s_addr = inet_addr(SERVER_IP);
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_PORT);
// Connect to server
if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
fprintf(stderr, "Error: Connection to server %s:%d failed.\n", SERVER_IP,
SERVER_PORT);
close(socket_desc); // Close the socket on failure
return 1;
}
Result:
Thus, the above program was Successfully completed and verified.