Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

12.Simulate the OPEN SHORTEST PATH FIRST routing protocol based on the cost assigned to the path.

1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. int count,src_router,i,j,k,w,v,min;
6. int cost_matrix[100][100],dist[100],last[100];
7. int flag[100];
8. printf("\n Enter the no of routers");
9. scanf("%d",&count);
10. printf("\n Enter the cost matrix values:");
11. for(i=0;i<count;i++)
12. {
13. for(j=0;j<count;j++)
14. {
15. printf("\n%d->%d:",i,j);
16. scanf("%d",&cost_matrix[i][j]);
17. if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;
18. }
19. }
20. printf("\n Enter the source router:");
21. scanf("%d",&src_router);
22. for(v=0;v<count;v++)
23. {
24. flag[v]=0;
25. last[v]=src_router;
26. dist[v]=cost_matrix[src_router][v];
27. }
28. flag[src_router]=1;
29. for(i=0;i<count;i++)
30. {
31. min=1000;
32. for(w=0;w<count;w++)
33. {
34. if(!flag[w])
35. if(dist[w]<min)
36. {
37. v=w;
38. min=dist[w];
39. }
40. }
41. flag[v]=1;
42. for(w=0;w<count;w++)
43. {
44. if(!flag[w])
45. if(min+cost_matrix[v][w]<dist[w])
46. {
47. dist[w]=min+cost_matrix[v][w];
48. last[w]=v;
49. }
50. }
51. }
52. for(i=0;i<count;i++)
53. {
54. printf("\n%d==>%d:Path taken:%d",src_router,i,i);
55. w=i;
56. while(w!=src_router)
57. {
58. printf("\n<--%d",last[w]);w=last[w];
59. }
60. printf("\n Shortest path cost:%d",dist[i]);
61. }
62. }

11.Simulate the Implementing Routing Protocols using border gateway protocol(BGP)

1. #include <stdio.h>
2. #include<conio.h>
3. int main()
4. {
5. int n;
6. int i,j,k;
7. int a[10][10],b[10][10];
8. printf("\n Enter the number of nodes:");
9. scanf("%d",&n);
10. for(i=0;i<n;i++)
11. {
12. for(j=0;j<n;j++)
13. {
14. printf("\n Enter the distance between the host %d - %d:",i+1,j+1);
15. scanf("%d",&a[i][j]);
16. }}
17. for(i=0;i<n;i++)
18. {
19. for(j=0;j<n;j++)
20. {
21. printf("%d\t",a[i][j]);
22. }
23. printf("\n");
24. }
25. for(k=0;k<n;k++)
26. {
27. for(i=0;i<n;i++)
28. {
29. for(j=0;j<n;j++)
30. {
31. if(a[i][j]>a[i][k]+a[k][j])
32. {
33. a[i][j]=a[i][k]+a[k][j];
34. }}}}
35. for(i=0;i<n;i++)
36. {
37. for(j=0;j<n;j++)
38. {
39. b[i][j]=a[i][j];
40. if(i==j)
41. {
42. b[i][j]=0;
43. }
44. }}
45. printf("\n The output matrix:\n");
46. for(i=0;i<n;i++)
47. {
48. for(j=0;j<n;j++)
49. {
50. printf("%d\t",b[i][j]);
51. }
52. printf("\n");
53. }
54. getch();
55. }

10 . Get the MAC or Physical address of the system using Address Resolution Protocol.

#include<sys/types.h>
#include<sys/socket.h>
#include<net/if_arp.h>
#include<sys/ioctl.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<math.h>
#include<complex.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<netinet/if_ether.h>
#include<net/ethernet.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
struct sockaddr_in sin={0};
struct arpreq myarp={{0}};
unsigned char *ptr;
int sd;
sin.sin_family=AF_INET;
if(inet_aton(argv[1],&sin.sin_addr)==0)
{
printf("IP address Entered '%s' is not valid \n",argv[1]);
exit(0);
}
memcpy(&myarp.arp_pa,&sin,sizeof(myarp.arp_pa));
strcpy(myarp.arp_dev,"echo");
sd=socket(AF_INET,SOCK_DGRAM,0);

if(ioctl(sd,SIOCGARP,&myarp)==1)
{
printf("No Entry in ATP cache for '%s'\n",argv[1]);
exit(0);
}
ptr=&myarp.arp_pa.sa_data[0];
printf("\nMAC Address for '%s' : ",argv[1]);
printf("%x:%x:%x:%x:%x:%x\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
printf("\n\t\t\t\t%x:%x:%x:%x:%x:%x\n", myarp.arp_ha.sa_data[0],
myarp.arp_ha.sa_data[1], myarp.arp_ha.sa_data[2],
myarp.arp_ha.sa_data[3], myarp.arp_ha.sa_data[4], myarp.arp_ha.sa_data[5]);
return 0;
}
9 . Write a C program to perform sliding window protocol.

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;
}

8 . Implement programs using raw sockets (like packet capturing and filtering)

7 . Write a client-server application for chat using UDP


Server

// Server side implementation of UDP client-server model

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#define PORT 8080

#define MAXLINE 1024

// Driver code

int main() {

int sockfd;

char buffer[MAXLINE];

char *hello = "Hello from server";

struct sockaddr_in servaddr, cliaddr;

// Creating socket file descriptor

if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {

perror("socket creation failed");

exit(EXIT_FAILURE);

}
memset(&servaddr, 0, sizeof(servaddr));

memset(&cliaddr, 0, sizeof(cliaddr));

// Filling server information

servaddr.sin_family = AF_INET; // IPv4

servaddr.sin_addr.s_addr = INADDR_ANY;

servaddr.sin_port = htons(PORT);

// Bind the socket with the server address

if ( bind(sockfd, (const struct sockaddr *)&servaddr,

sizeof(servaddr)) < 0 )

perror("bind failed");

exit(EXIT_FAILURE);

int len, n;

len = sizeof(cliaddr); //len is value/resuslt

n = recvfrom(sockfd, (char *)buffer, MAXLINE,

MSG_WAITALL, ( struct sockaddr *) &cliaddr,

&len);

buffer[n] = '\0';

printf("Client : %s\n", buffer);

sendto(sockfd, (const char *)hello, strlen(hello),

MSG_CONFIRM, (const struct sockaddr *) &cliaddr,

len);

printf("Hello message sent.\n");


return 0;

Client

// Client side implementation of UDP client-server model

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#define PORT 8080

#define MAXLINE 1024

// Driver code

int main() {

int sockfd;

char buffer[MAXLINE];

char *hello = "Hello from client";

struct sockaddr_in servaddr;

// Creating socket file descriptor

if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {

perror("socket creation failed");


exit(EXIT_FAILURE);

memset(&servaddr, 0, sizeof(servaddr));

// Filling server information

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(PORT);

servaddr.sin_addr.s_addr = INADDR_ANY;

int n, len;

sendto(sockfd, (const char *)hello, strlen(hello),

MSG_CONFIRM, (const struct sockaddr *) &servaddr,

sizeof(servaddr));

printf("Hello message sent.\n");

n = recvfrom(sockfd, (char *)buffer, MAXLINE,

MSG_WAITALL, (struct sockaddr *) &servaddr,

&len);

buffer[n] = '\0';

printf("Server : %s\n", buffer);

close(sockfd);

return 0;

Output :
$ ./server
Client : Hello from client
Hello message sent.
$ ./client
Hello message sent.
Server : Hello from server
6 . Take an example subnet of hosts. Obtain broadcast tree for it.

Aim:
Take an example subnet of hosts. Obtain broadcast tree for it.

Theory
IP addressing is the allocation of unique ID to each and every system connected in a network to
maintan communication among them through out the affixed network. There are 5 classes of IP
Addresses namely A through E with the range varying from one class to the other class.
A subnet is a network allocation to similar systems or same hierarchial systems present in a
allocated network like an organisation. Each and every system can be reachd through a client-
server computing environment where the server acts as the Master and the clients acts as the
Slaves to form a Master-Slave computing environment. Below programs show the calculation of
network addresses with subnet predefinition and subnet generation.

Program:

a)Network Address:

#include
#include
void main()
{
unsigned int compad[4];
unsigned int mask[4];
unsigned int netadr[4];
int i;
clrscr();
printf("Enter the ip address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&compad[3],&compad[2],&compad[1],&compad[0]);
printf("Enter the subnet address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&mask[3],&mask[2],&mask[1],&mask[0]);
for(i=0;i<4;i++)
{
netadr[i]= compad[i]&mask[i];
}
printf("\nNetwork address is:\n");
printf("%u.%u.%u.%u",netadr[3],netadr[2],netadr[1],netadr[0]);
printf("\nsubnet address is:\n");
printf("%u.%u.%u.%u",mask[3],mask[2],mask[1],mask[0]);
printf("\nip address is:\n");
printf("%u.%u.%u.%u",compad[3],compad[2],compad[1],compad[0]);
getch();
}

Output:

5 . Take an example subnet graph with weights indicating delay between nodes. Now obtain Routing
table art each node using distance vector routing algorithm

Aim:
Take an example subnet graph with weights indicating delay between nodes. Now obtain
Routing table at each node using Distance Vector Routing.

Theory
Distance Vector routing (DVR) algorithm is unlike Dijkstra's algorithm which is a non-adaptive
routing algorithm and means that it is purely static, that is pre-destined and fixed, not flexible in
networks where congestions are more prone to occur. DVR is an adaptive routing algorithm in
which the information from neighbours is maitained well by each and every node and this helps
us to determine the simplest path possible in a changing network. Though, one of the node may
fail, still, the destined node is reachable through other possible intermediate nodes that are
found out by the DVR algorithm. The perfectly executing program below shows it live below.

Program:

#include
struct node
{
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("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}

</n;j++)
</n;i++)
</n;k++)
</n;j++)
</n;i++)
</n;j++)
</n;i++)

4 . Implement Dijkstra’s algorithm to compute the Shortest path in a graph.

Aim:
Implement Dijkstra's algorithm to compute the Shortest path through a graph.

Theory
Dijkstra's algorithm is a non-adaptive routing algorithm which is very widely used to route
packets from source to detination through various routers available during the transmission. It is
implemented at the network layer of the architecture where data packets are sent through
routers which maitain routing tables that help to denote the exact location to where the destined
packets need to be delivered. Major advantage in using Dijkstra's algorithm is that it forwards
the data packets from source to destination through the most optimized path in terms of both the
distance and cost observed. It prompts the user to enter the number of nodes and the source
and destination nodes among them. In addition, the algorithm written below also asks for the
neighbours to each node with the distances to reach to them from each node is also prompted.
All this data is stored and used further to calculate and estimate the best path possible for data
packets to reach their destination from source. Program below explains it in a much better way.

Program:

#include
#include
int n,s,nb,nbs[15],snbs[15],delay[15][15],i,j,temp[15],ze=0;
void min();
void main()
{
clrscr();
printf("Enter the no.of nodes:");
scanf("%d",&n);
printf("\nEnter the source node:");
scanf("%d",&s);
printf("\nEnter the no.of Neighbours to %d:",s);
scanf("%d",&nb);
printf("\nEnter the Neighbours:");
for(i=1;i<=nb;i++)
scanf("%d",&nbs[i]);
printf("\nEnter the timedelay form source to nbs:");
for(i=1;i<=nb;i++)
scanf("%d",&snbs[i]);
for(i=1;i<=nb;i++)
{
printf("\nEnter the timedelay of %d: ",nbs[i]);
for(j=1;j<=n;j++)
scanf("%d",&delay[i][j]);
}
for(i=1;i<=nb;i++)
{
printf("\nThe timedelays of %d: ",nbs[i]);
for(j=1;j<=n;j++)
printf("%3d",delay[i][j]);
}
min();
getch();
}
void min()
{
int sum,k,y=1,store=1;
printf("\n\t\t\tnew- rout");
printf("\n\t\t\ttime-");
printf("\n\t\t\tdelay");
printf("\n");
for(i=1;i<=n;i++)
{
sum=0;
k=1;
for(j=1;j<=nb;j++)
{
temp[k++]=delay[j][i];
}

sum=temp[1]+snbs[1];
for(y=2;y<=nb;y++)
{
if(sum>temp[y]+snbs[y])
{
sum=temp[y]+snbs[y];
store=y;
}
}

if(s==i)
printf("\n\t%d+\t%d =\t%d --",ze,ze,ze);
else
printf("\n\t%d +\t%d =\t%d\t%d",temp[store],snbs[store],sum,nbs[store]);
}
}

Output:
3 . Implement on a data set of characters the three CRC polynomials – CRC-12, CRC-16 and CRC-CCIP.

Aim:
Implement on a data set of characters the CRC polynomials.

Theory
CRC means Cyclic Redundancy Check. It is the most famous and traditionally successful
mechanism used in error detection through the parity bits installed within the data and obtaining
checksum which acts as the verifier to check whether the data retreived at the reciever end is
genuine or not. Various operations are involved in implementing CRC on a data set through
CRC generating polynomials. In the program, I have also provided the user to opt for Error
detection whereby he can proceed for it. Understand the program below as it is much simpler
than pretented to be so.

Program:

#include
#include
#include
#define N strlen(g)

char t[128], cs[128], g[]="100010000";


int a, e, c;

void xor() {
for(c=1;c}

void crc() {
for(e=0;e do {
if(cs[0]=='1') xor();
for(c=0;c cs[c]=t[e++];
}while(e<=a+N-1);
}

void main() {
clrscr();
printf("\nEnter poly : "); scanf("%s",t);
printf("\nGenerating Polynomial is : %s",g);
a=strlen(t);
for(e=a;e
printf("\nModified t[u] is : %s",t);
crc();
printf("\nChecksum is : %s",cs);
for(e=a;e printf("\nFinal Codeword is : %s",t);
printf("\nTest Error detection 0(yes) 1(no) ? : ");
scanf("%d",&e);
if(e==0) {
printf("Enter position where error is to inserted : ");
scanf("%d",&e);
t[e]=(t[e]=='0')?'1':'0';
printf("Errorneous data : %s\n",t);
}
crc();
for (e=0;(e<n-1)&&(cs[e]!='1');e++);
if(e
else printf("No Error Detected.");
getch();
}

Output:

</n-1)&&(cs[e]!='1');e++);
2 . Write op a DNS client server to resolve the given hostname.

// C program to display hostname

// and IP address

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <netdb.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

// Returns hostname for the local computer

void checkHostName(int hostname)

if (hostname == -1)

perror("gethostname");

exit(1);

// Returns host information corresponding to host name

void checkHostEntry(struct hostent * hostentry)

if (hostentry == NULL)

{
perror("gethostbyname");

exit(1);

// Converts space-delimited IPv4 addresses

// to dotted-decimal format

void checkIPbuffer(char *IPbuffer)

if (NULL == IPbuffer)

perror("inet_ntoa");

exit(1);

// Driver code

int main()

char hostbuffer[256];

char *IPbuffer;

struct hostent *host_entry;

int hostname;

// To retrieve hostname

hostname = gethostname(hostbuffer, sizeof(hostbuffer));

checkHostName(hostname);

// To retrieve host information


host_entry = gethostbyname(hostbuffer);

checkHostEntry(host_entry);

// To convert an Internet network

// address into ASCII string

IPbuffer = inet_ntoa(*((struct in_addr*)

host_entry->h_addr_list[0]));

printf("Hostname: %s\n", hostbuffer);

printf("Host IP: %s", IPbuffer);

return 0;

Output:
Hostname: cContainer
Host IP: 10.98.162.101
1 . Implement the data link layer framing methods such as character stuffing and bit stuffing.

Implement the data link layer framing methods


such as and bit stuffing.

Introduction to bit stuffing framing method used in


Data Link layer:
The new technique allows data frames to contain an
arbitrary number of bits and allows character codes with an
arbitrary no of bits per character. Each frame begins and
ends with the special bit pattern, 01111110, called a flag byte.
Whenever the sender's data link layer encounters five
consecutive ones in the data, it automatically stuffs a 0 bit
into the outgoing bit stream. This bit stuffing is analogous
to character stuffing, in which a DLE is stuffed into the
outgoing character stream before DLE in the data.

Program Algorithm:
Begin
Step 1: Read frame length n
Step 2: Repeat step (3 to 4) until i<n(: Read values into the
input frame (0’s and
1’s) i.e.
Step 3: initialize I i=0;
Step 4: read a[i] and increment i
Step 5: Initialize i=0, j=0,count =0
Step 6: repeat step (7 to 22) until i<n
Step 7: If a[i] == 1 then
Step 8: b[j] = a[i]
Step 9: Repeat step (10 to 18) until (a[k] =1 and k<n and
count <5)
Step 10: Initialize k=i+1;
Step 11: Increment j and b[j]= a[k];
Step 12: Increment count ;
Step 13: if count =5 then
Step 14: increment j,
Step 15: b[j] =0
Step 16: end if
Step 17: i=k;
Step 18: increment k
Step 19: else
Step 20: b[j] = a[i]
Step 21: end if
Step 22: increment I and j
Step 23: print the frame after bit stuffing
Step 24: repeat step (25 to 26) until i< j
Step 25: print b[i]
Step 26: increment i
End

Program Code: // BIT Stuffing program


#include<stdio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame length:");
scanf("%d",&n);
printf("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++)
{
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]);
}

Program Output:
Enter frame length:5
Enter input frame (0's & 1's only):
1
1
1
1
1
After stuffing the frame is:111110
------------------
Implement the data link layer framing methods
such as character stuffing.

Introduction to character stuffing method of framing for Data Link


Layer: The framing method gets around the problem of
resynchronization after an error by having each frame start
with the ASCII character sequence DLE STX and the
sequence DLE ETX. If the destination ever losses the track
of the frame boundaries all it has to do is look for DLE STX
or DLE ETX characters to figure out. The data link layer on
the receiving end removes the DLE before the data are given
to the network layer. This technique is called character
stuffing.

Program Algorithm:
Begin
Step 1: Initialize I and j as 0
Step 2: Declare n and pos as integer and a[20],b[50],ch as
character
Step 3: read the string a
Step 4: find the length of the string n, i.e n-strlen(a)
Step 5: read the position, pos
Step 6: if pos > n then
Step 7: print invalid position and read again the position,
pos
Step 8: endif
Step 9: read the character, ch
Step 10: Initialize the array b, b[0…5] as ’d’, ’l’, ’e’, ’s’, ’t’,’x’
respectively
Step 11: j=6;
Step 12: Repeat step[(13to22) until i<n
Step 13: if i==pos-1 then
Step 14: initialize b array,b[j],b[j+1]…b[j+6] as‘d’, ‘l’, ‘e’ ,’ch,
’d’, ‘l’,‘e’ respectively
Step 15: increment j by 7, i.e j=j+7
Step 16: endif
Step 17: if a[i]==’d’ and a[i+1]==’l’ and a[i+2]==’e’ then
Step 18: initialize array b, b[13…15]=’d’, ‘l’, ‘e’ respectively
Step 19: increment j by 3, i.e j=j+3
Step 20: endif
Step 21: b[j]=a[i]
Step 22: increment I and j;
Step 23: initialize b array,b[j],b[j+1]…b[j+6] as‘d’,
‘l’,‘e’,’e’,‘t’, ‘x’,‘\0’ respectively
Step 24: print frame after stuffing
Step 25: print b
End

Program Code: //Program for Character Stuffing


#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
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)
{
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("\nframe after stuffing:\n");
printf("%s",b);
}

Program Output:
Enter string
MLRITM
Enter position
2
Enter the character
frame after stuffing:
dlestxMdldleLRITMdleetx
------------------

You might also like