Professional Documents
Culture Documents
CN LAB MANUAL-1
CN LAB MANUAL-1
CN LAB MANUAL-1
Experment-1
Aim: Implement the data link layer framing methods such as Character Stuffing
and Bit Stuffing.
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf(" 01111110 ");
getch();
}
Output:
Program for Character Stuffing:
#include
#include
#include
void charc(void);
void main()
{
int choice;
while(1)
{
printf("\n\n\n1.character stuffing");
printf("\n\n2.exit");
printf("\n\n\nenter choice");
scanf("%d",&choice);
printf("%d",choice);
if(choice>2)
printf("\n\n invalid option....please renter");
switch(choice)
{
case 1:
charc();
break;
case 2:
exit(0);
}
}
}
void charc(void)
{
char c[50],d[50],t[50];
inti,m,j;
clrscr();
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf("\n\nreceived data:");
for(i=0;i<j;i++)
{printf("%c",t[i]);
}
}
Output:
Experment-02
Aim: To write a C program to develop a DNS client server to resolve the given
hostname.
#include
#include
#include
#include
#include
#include
#include intmain(intargc,char *argv[1])
{
structhostent *hen;
if(argc!=2)
hen=gethostbyname(argv[1]);
if(hen==NULL)
printf("Hostname is %s \n",hen->h_name);
OUTPUT: Thus the above program udp performance using domain name server was
executed and successfully.
Experment-03
AIM: Implement on a data set of characters the three CRC polynomials – CRC
12, CRC 16 and CRC CCIP.
Program:
#include
#include
intn,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("\nThetimedelays of %d: ",nbs[i]);
for(j=1;j<=n;j++)
printf("%3d",delay[i][j]);
}
min();
getch();
}
void min()
{
intsum,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:
Experment-05
Aim: Take an example subnet graph with weights indicating delay between
nodes. Now obtain Routing table at each node using Distance Vector Routing.
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++)
OUTPUT:
Experment-06
Program:
a)Network Address:
#include
#include
void main()
{
unsigned intcompad[4];
unsigned int mask[4];
unsigned intnetadr[4];
inti;
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:
b)Network address with automatic subnet address generation:
#include
#include
void main()
{
unsigned intcompad[4];
unsigned int mask[4];
unsigned intnetadr[4];
unsigned long int ma=0;
inti,pre;
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 prefix:\n");
scanf("%ul",&pre);
for(i=(32-pre);i<32;i++)
ma=ma|(1<<i);
for(i=0;i<4;i++)
{
mask[i]=ma%256;
ma=ma/256;
}
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:
Experment-7
Program:
ALGORITHM: CLIENT
3. The client accept the connection and to send the data from client to server and vice
versa.
4. The client communicate the server to send the end of the message.
ALGORITHM: SERVER
3. The server accept the connection and to send the data from server to client and vice
versa.
4. The server communicate the client to send the end of the message
Outcome: To implement a client server application using connection less service i.e.
UDP.
Experment-08
AIM: To implement programs using raw sockets (like packet capturing and
filtering).
Program:
#include
#include
#include
#include
#include
// Packet length #define PCKT_LEN 8192
// May create separate header file (.h) for all
// headers' structures
// IP header's structure
struct ipheader {
unsigned char iph_ihl:5, /* Little-
endian */iph_ver:4;
unsigned char iph_tos;
/* Structure of a TCP
header */struct tcpheader
{
unsigned short int
tcph_srcport; unsigned
short int tcph_destport;
unsigned int tcph_seqnum;
unsigned int tcph_acknum;
unsigned char tcph_reserved:4, tcph_offset:4;
// unsigned char
tcph_flags;unsigned int
tcp_res1:4, /*little-endian*/
tcph_hlen:4, /*length of tcp header in
32-bitwords*/
tcph_fin:1, /*Finish flag "fin"*/
tcph_syn:1, /*Synchronize sequence numbers to
start a connection*/
tcph_rst:1, /*Reset
flag */
tcph_psh:1, /*Push, sends data
to theapplication*/
tcph_ack:1,
/*acknowledge*/
tcph_urg:1, /*urgent
pointer*/tcph_res2:2;
unsigned short int
tcph_win; unsigned short
int tcph_chksum;unsigned
short int tcph_urgptr;
};
// Simple checksum function, may use others such as
CyclicRedundancy Check, CRC
unsigned short csum(unsigned short *buf, int len)
{
int sd;
// No data, just
datagram char
buffer[PCKT_LEN];
// The size of the headers
struct ipheader *ip = (struct ipheader *) buffer;
sizeof(struct ipheader));
perror("socket()
error");exit(-1);
}
else
printf("socket()-SOCK_RAW and tcp protocol is OK.\n");
// The source is redundant, may be used later if needed
// Address family
sin.sin_family =
AF_INET;din.sin_family
= AF_INET;
// Source port, can be any, modify as
neededsin.sin_port =
htons(atoi(argv[2])); din.sin_port =
htons(atoi(argv[4]));
// Source IP, can be any, modify as
neededsin.sin_addr.s_addr =
inet_addr(argv[1]);
din.sin_addr.s_addr =
inet_addr(argv[3]);
// IP
structure ip-
>iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16;
ip->iph_len = sizeof(struct ipheader) +
sizeof(structtcpheader);
ip->iph_ident =
htons(54321);ip-
>iph_offset = 0;
ip->iph_ttl = 64;
ip->iph_protocol = 6; // TCP
ip->iph_chksum = 0; // Done by kernel
// Source IP, modify as needed, spoofed, we accept
throughcommand line argument
ip->iph_sourceip = inet_addr(argv[1]);
// Destination IP, modify as needed, but here we
acceptthrough command line argument
ip->iph_destip = inet_addr(argv[3]);
// The TCP structure. The source port, spoofed, we
acceptthrough the command line
tcp->tcph_srcport = htons(atoi(argv[2]));
// The destination port, we accept through
command linetcp->tcph_destport =
htons(atoi(argv[4]));
tcp->tcph_seqnum =
htonl(1);tcp-
>tcph_acknum = 0;
tcp->tcph_offset = 5;
tcp->tcph_syn = 1;
tcp->tcph_ack = 0;
tcp->tcph_win = htons(32767);
tcp->tcph_chksum = 0; // Done by
kerneltcp->tcph_urgptr = 0;
// IP checksum calculation
ip->iph_chksum = csum((unsigned short *)
buffer, (sizeof(struct ipheader) + sizeof(struct
tcpheader)));
perror("setsockopt()
error");exit(-1);
}
else
printf("setsockopt() is OK\n");
printf("Using:::::Source IP: %s port: %u, Target IP:
%sport: %u.\n", argv[1], atoi(argv[2]), argv[3],
atoi(argv[4]));
// sendto() loop, send every 2 second for 50
countsunsigned int count;
for(count = 0; count < 20; count++)
{
perror("sendto()
error");exit(-1);
}
else
printf("Count #%u - sendto() is OK\n",
count);sleep(2);
}
close(s
d);
return
0;
}
RESULT :
Thus the Above programs using raw sockets TCP \IP (like packet capturing and filtering)
wasexecuted and successfully.
EXPERMENT- 09
PROGRAM :
// SLIDING WINDOW PROTOCOL
Client :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/msg.h> struct
mymsgbuf
{
long mtype;
char
mtext[25];
};
FILE
*fp;int
main()
{
struct mymsgbuf
buf;int msgid;
int i=0,s;
int
count=0,frmsz;
int a[100];
char d;
if((msgid=msgget(89,IPC_CREAT|0666))==
-1)
{
printf("\n ERROR IN
MSGGET");exit(0);
}
printf("\n Enter the frame
size:"); scanf("%d",&frmsz);
if((fp=fopen("check","r"))==NUL
L)printf("\n FILE NOT OPENED");
else
printf("\n FILE
OPENED");
while(!feof(fp))
{
d=getc(f
p);
a[i]=d;
i++;
}
s=i;
for(i=0;i<frmsz;i++) //print from the
check fileprintf("\t %c",a[i]);
for(i=0;i<frmsz;i++)
{ if((msgrcv(msgid,&buf,sizeof(buf),0,1))==-1)
{
printf("\n ERROR IN
MSGRCV");exit(0);
}
for(i=0;i<frmsz;i++)
{
if(a[i]==buf.mtext[i]
)count++;
} if(count==0)
{
Server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/msg.h> struct
mymsgbuf
{ long
mtype; char
mtext[25];
};
FILE
*fp;int
main()
{s
truct mymsgbuf
buf;int si,ei,sz;
int
msgid;
int i=0,s;
int
a[100];
char d;
if((fp=fopen("send","r"))==NULL
)printf("\n FILE NOT OPENED");
else
printf("\n FILE OPENED");
printf("\n Enter starting and ending index of frame
array:");scanf("%d%d",&si,&ei);
sz=ei-si;
if((msgid=msgget(89,IPC_CREAT|0666))==
-1)
{
printf("\n ERROR IN
MSGGET");exit(0);
}
while(!feof(fp))
{
d=getc(f
p);
a[i]=d;
i++;
}s
=i;
buf.mtype
=1;
for(i=si;i<=ei;i++)
{
buf.mtext[i]=a[i];
}
printf("\n ERROR IN
MSGSND");exit(0);
}}
printf("\n FRAMES
SENT");return 0;
}
OUTPUT
Thus the above program sliding window protocol was executed and successfully.
Experment-10
AIM: To get the MAC or Physical address of the system using Address
ResolutionProtocol.
Program:
#include<unistd.h>
#include<sys/types.h
>
#include<sys/socket.
h>
#include<netinet/in.
h>
#include<net/if_arp.
h>#include<stdlib.h>
#include<stdio.h>
#include<netdb.h>
#include<sys/ioctl.h
>
#include<arpa/inet.h
>
int main(int argc,char *argv[])
{
int sd;
unsigned char *ptr;
struct arpreq
myarp={{0}}; struct
sockaddr_in sin={0};
sin.sin_family=AF_INET;
if(inet_aton(argv[1],&sin.sin_addr)==0)
{
int n;
int
i,j,k;
int a[10][10],b[10][10];
printf("\n Enter the number of nodes:");
scanf("%d",&
n);
for(i=0;i<n;i++
)
{
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
b[i][j]=a[i][j
];if(i==j)
{
b[i][j]=
0;
}
}}
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT
Thus the above program to simulate the Implementing Routing Protocols using border
gatewayprotocol was executed and successfully.
Experment-12
AIM: To simulate the OPEN SHORTEST PATH FIRST routing protocol based on
thecost assigned to the path.
PROGRAM:
#include<stdio.
h>
#include<conio.
h>int
a[5][5],n,i,j;
void main()
{
void getdata();
void
shortest();
void
display();
clrscr();
printf("\n\n PROGRAM TO FIND SHORTEST PATH BETWEEN TWO
NODES\n");
getdata
();
shortes
t();
display(
);
getch();
}
void getdata()
{
clrscr();
printf("\n\nENTER THE NUMBER OF HOST IN THE GRAPH\n");
scanf("%d",&n);
printf("\n\nIF THERE IS NO DIRECT PATH \n");
printf(" \n\nASSIGN THE HIGHEST DISTANCE VALUE 1000 \n");
for(i=0;i<n;i++)
{
a[i][j]=
0;
for(j=0;j<n;j++)
{
if(i!=j)
{
void shortest()
{
int i,j,k;
for(k=0;k<n;k+
+)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][k]+a[k][j]<a[i][j])
a[i][j]=a[i][k]+a[k][j];
}
void display()
{
int i,j;
for(i=0;i<n;i+
+)
for(j=0;j<n;j+
+)if(i!=j)
{
getch();
}
OUTPUT
Thus the above program to simulate the Implementing Routing Protocols using open
shortestpath first (OSPF) was executed and successfully.