0% found this document useful (0 votes)
513 views

Content Beyond Syllabus Network Lab

The document describes Dijkstra's shortest path algorithm. It includes code to implement the algorithm in C using an adjacency matrix to represent a graph. The code takes a graph and source vertex as input, implements Dijkstra's algorithm to find the shortest path from the source to all other vertices, and outputs the shortest path and distance to each vertex. It uses arrays to track visited nodes, distances, and predecessors in the shortest path.

Uploaded by

yousuf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
513 views

Content Beyond Syllabus Network Lab

The document describes Dijkstra's shortest path algorithm. It includes code to implement the algorithm in C using an adjacency matrix to represent a graph. The code takes a graph and source vertex as input, implements Dijkstra's algorithm to find the shortest path from the source to all other vertices, and outputs the shortest path and distance to each vertex. It uses arrays to track visited nodes, distances, and predecessors in the shortest path.

Uploaded by

yousuf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

DIJKSTRA’S Shortest Path Algorithm

#include<stdio.h>

#include<stdlib.h>

void main()

int graph[15][15],s[15],path[15],mark[15];

int n,src,i,j,u,pre[15];

int count=0;

int minimum(int a[],int m[],int k);

void printpath(int,int,int[]);

clrscr();

printf(“\nEnter the no.of vertices\n”);

scanf(” %d”,&n);

if(n<=0)

printf(“\n Invalid \n”);

exit(1);

printf(“\nEnter the Adjacent Matrix\n”);

for(i=1;i<=n;i++)

printf(“\nEnter the elements of row %d\n”,i);

for(j=1;j<=n;j++)
{

scanf(“%d”,&graph[i][j]);

printf(“\nEnter the source vertex\n”);

scanf(“%d”,&src);

for(j=1;j<=n;j++)

mark[j]=0;

path[j]=999;

pre[j]=0;

path[src]=0;

while(count<n)

u=minimum(path,mark,n);

s[++count]=u;

mark[u]=1;

for(i=1;i<=n;i++)

if(graph[u][i]>0)

if(mark[i]!=1)
{

if(path[i]>path[u]+graph[u][i])

path[i]=path[u]+graph[u][i];

pre[i]=u;

for(i=1;i<=n;i++)

printpath(src,i,pre);

if(path[i]!=999)

printf(” —> ( %d )\n”,path[i]);

getch();

int minimum(int a[],int m[],int k)

int mi=999;

int i,t;

for(i=1;i<=k;i++)
{

if(m[i]!=1)

if(mi>=a[i])

mi=a[i];

t=i;

return t;

void printpath(int x,int i,int p[])

if(i==x)

printf(“%d”,x);

else if(p[i]==0)

printf(“no path from %d to %d”,x,i);

else

printpath(x,p[i],p);

printf(” ….. %d”,i);

}
}

Simulation of Stop and Wait Protocol


Sender.c
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main()

int i, n, ch;

char input[20];

FILE *in;

randomize();

printf(“\n\t\t Stop and Wait Protocol \n”);

printf(“\n 1. Send \n2. Check ACK\n3.EOT\n”);

while(1)

printf(“Enter your choice….”);

scanf(“%d”, &ch);

switch(ch)

case 1:

in = fopen(“data.txt”, “w”);

printf(“Enter the Data: “);


scanf(“%s”, input);

n = strlen(input);

for(i=0; i<n+1; i++)

fprintf(in, “%s”, input);

fclose(in);

printf(“——->Data Sent\n”);

break;

getch();

Receiver.c
#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int i, n,;

char output[20];

FILE *out;

clrscr();

out=fopen(“data_parity.txt”, “r”);

fscanf(out, “%s”, output);

n = strlen(output);
for(i=0; i<n; i++)

if(output[i]==’1’)

one++;

if(one%2==0)

printf(“Received Data has Even Parity\n”);

printf(“Data Accepted\n”);

else

printf(“Received Data has Odd Parity\n”);

printf(“Data Rejected\n”);

printf(“Received Data: “);

for(i=0;i<n-1; i++)

printf(“%c”, output[i];

fclose(out);

getch();

Packet Sniffer
/* Packet Capture Program */
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <signal.h>
#define BUFFSIZE 2048
struct iphdr *ip;
struct tcphdr *tcp;
void help();
void data(char *data, int nbytes);
void flags();
static void get_intr(int sig);
void get_intr(int sig)
{
sleep(1);
printf("got interrupt signal- exiting...\n");
exit(0);
}
void data(char data, int nbytes) /* shows u the data */
{
char *ptr = &data[0];
int n = 0;
printf("data: %d\n",nbytes);
while(nbytes-- > 0) {
n++;
if((n%25)==0) printf("\n");
if(isgraph(*ptr++)) printf("%c",*ptr);
else printf(".");
}
}
void flags()
{
printf(" ");
if(tcp->syn == 1)
printf("syn=1 ");
if(tcp->ack == 1)
printf("ack=1 ");
if(tcp->rst == 1)
printf("rst=1 ");
if(tcp->fin == 1)
printf("fin=1 ");
if(tcp->urg == 1)
printf("urg=1 ");
if(tcp->psh == 1)
printf("psh=1 ");
printf("\n");
}
void help() {
puts("\n------\npsniff\n------\n");
puts("l0om - psniff is a smal tcp port(/protocol) sniffer.\n Example:");
puts("-h: prints out this help menue");
puts("-P: next argument is portnumber u want to sniff");
puts("-D: prints out all (ASCII)data from all packets");
puts("-F: shows the set flags");
puts("example: ./psniff -P 7 -D -F");
puts("sniffs packets for/from port 7. prints out all data and set flags");
puts(" u want to sniff 7 && 80? ");
puts("./psniff -P 7 -D -F &; ./psniff -P 80 -F -D &");
}
int main(int argc, char **argv)
{
int sockfd;
int i;
int port = 80; /* standard */
int r_flags, r_data;
size_t bytes;
char buffer[BUFFSIZE];
char *dats;
if( (getuid()|getgid()) != 0) {
printf("error: u must be root\n");
return -1;
}
r_flags=r_data=0;
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if(sockfd < 0) {
fprintf(stderr, "error: cannot creat socket\n");
return -1;
}
if(argc == 1) {
printf("using standard settings...\n\n");
r_flags = 1;
}
for(i = 0; i < argc; i++) {
if(strncmp(argv[i], "-D", 2) == 0)
r_data = 1;
if(strncmp(argv[i], "-F", 2) == 0)
r_flags = 1;
if(strncmp(argv[i], "-P", 2) == 0)
port = atoi(argv[++i]);
if(strncmp(argv[i], "-h", 2) == 0) {
help();
exit(0);
}
}
if(signal(SIGINT, get_intr) == SIG_ERR)
printf("cannot install Interrupt catcher\n");
ip = (struct iphdr *)buffer;
tcp = (struct tcphdr *) (buffer + sizeof(struct iphdr));
dats = buffer+(sizeof(struct iphdr)+sizeof(struct tcphdr));
while( (bytes = read(sockfd, buffer, BUFFSIZE)) > 0) {
if(ntohs(tcp->dest) == port || ntohs(tcp->source) == port) {
printf("tcp: dport=%d, sport=%d, from=%s",ntohs(tcp-> dest),ntohs
(tcp-> source), inet_ntoa(ip->saddr));
if(r_flags)
flags();
else printf("\n");
if(r_data) {
printf("\n");
data(dats, bytes-(sizeof(struct iphdr)+ sizeof(struct tcphdr)));
printf("\n\n");
}
}
memset(buffer, '', BUFFSIZE);
}
return (0);
}

1.crc-citt

2.dsdv

Difference between routing protocol and routed protocol?

Routing Protocol: It is used to recognise and start the best path for the data communication
Routed Protocol: It is used to transfer the data for the selected path

3.tcp/ip client server

4.fifo

5.rsa

6.leaky bucket

What is RSA algorithm?

RSA is short for Rivest-Shamir-Adleman algorithm. It is the most commonly used public key encryption
algorithm in use today.

How do you use RSA for both authentication and secrecy?

RSA is a public key encryption algorithm. The RSA algorithms are based on the mathematical
part that it is easy to find and multiply large prime numbers together, but it is extremely difficult
to factor their product.
For authentication: One can encrypt the hash (MD4/SHA) of the data with a private key. This is
known as digital signature.
For Secrecy: Secrecy/confidentiality is achieved by encrypting the data with public key and
decrypting with private key.

You might also like