Sei sulla pagina 1di 12

Simulation of Sliding Window Protocol

#include"stdio.h" #include"conio.h" #include"stdlib.h" void getdata() { FILE *f1; char c; clrscr(); printf("\n\t\tEnter the data\n\t\t"); f1=fopen("sc.txt", "w"); while((c=getchar())!='\n') putc(c, f1); putc('\n', f1); fclose(f1); }

void send() { FILE *f1, *f2; char c; int i, k=0, windowsize=2, randno, k1; f1=fopen("sc.txt", "r"); f2=fopen("ms.txt", "w"); loop: for(i=0; i < windowsize;i++)<WINDOWSIZE;I++)</WINDOWSIZE;I++) { c=getc(f1); if(c!='\n') { printf("%c\n", c); putc(c, f2); k++; } else goto end; } fclose(f1); randomize(); randno=random(k);//generates a random number f1=fopen("sc.txt", "a"); putc(randno, f1); fclose(f1);

k1=randno; printf("The Frames of data sent %d\n", k); getch(); f1=fopen("sc.txt", "r"); fseek(f1, k, SEEK_SET); goto loop; end: fclose(f1); fclose(f2); printf("The Frames of data sent %d\n", k); } void disp() { int choice; printf("\n\tChoose an option\n"); printf("\n\t1.Send"); printf("\n\t2.Quit"); scanf("%d", &choice); if(choice==1) send(); } void main() { clrscr(); getdata(); disp(); getch(); }

c-Program using TCP Sockets(date and time server & client)


Server: #include"netinet/in.h" #include"sys/socket.h" #include"stdio.h" #include"string.h" #include"time.h" main( ) {

struct sockaddr_in sa; struct sockaddr_in cli; int sockfd,conntfd; int len,ch; char str[100]; time_t tick; sockfd=socket(AF_INET,SOCK_STREAM,0); if(socket<0) { printf("error in socket\n"); exit(0); } else printf("Socket Opened"); bzero(&sa,sizeof(sa)); sa.sin_port=htons(5600); sa.sin_addr.s_addr=htonl(0); if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) { printf("Error in binding\n"); } else printf("Binded Successfully"); listen(sockfd,50); for(;;) { len=sizeof(ch);

conntfd=accept(sockfd,(struct sockaddr*)&cli,&len); printf("Accepted"); tick=time(NULL); snprintf(str,sizeof(str),"%s",ctime(&tick)); printf("%s",str); write(conntfd,str,100); } } client: #include"netinet/in.h" #include"sys/socket.h" #include"stdio.h" main() { struct sockaddr_in sa,cli; int n,sockfd; int len; char buff[100]; sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) { printf("\nError in Socket"); exit(0); } else printf("\nSocket is Opened"); bzero(&sa,sizeof(sa));

sa.sin_family=AF_INET; sa.sin_port=htons(5600); if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) { printf("\nError in connection failed"); exit(0); } else printf("\nconnected successfully"); if(n=read(sockfd,buff,sizeof(buff))<0) { printf("\nError in Reading"); exit(0); } else { printf("\nMessage Read %s",buff); } } Program coding: //UDP DNS Server: #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<string.h> #include<netdb.h> #include<sys/types.h> #include<arpa/inet.h> int main(int argc,char **argv) { int sockfd; char mesg2[100];

int n,flag=0; socklen_t len; char mesg[100],ip[25],host[100],mesgr[100]; FILE *fp; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(9889); bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); bzero(mesg,100); bzero(ip,25); len=sizeof(servaddr); n=recvfrom(sockfd,mesg,100,0,(struct sockaddr*)&servaddr,&len); printf("Requested host name: %s\n",mesg); fp=fopen("dns.txt","r"); while(!feof(fp) && (flag==0)) { fscanf(fp,"%s%s",ip,host); if(strcmp(host,mesg)==0) flag=1; } sendto(sockfd,ip,sizeof(ip),0,(struct sockaddr*)&servaddr,len); } Client: #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<string.h> #include<netdb.h> #include<sys/types.h> #include<arpa/inet.h> int main(int argc,char **argv) { int sockfd,n;char sendline[100],recvline[101]; struct sockaddr_in servaddr; if(argc!=2) { printf("usage: updcli "); exit(0); } bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9889);

inet_pton(AF_INET,argv[1],&servaddr.sin_addr); sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); printf("enter the host name:"); scanf("%s",sendline); sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); n=recvfrom(sockfd,recvline,100,0,(struct sockaddr*)&servaddr,sizeof(servaddr)); printf("Requesed IP address:%s\n",recvline); } OUTPUT: Contents of dns.txt: 192.168.1.1 gateway 127.0.0.1 localhost 209.85.231.104 www.google.com 93.137.149.56 www.yahoo.com 125.56.222 17 www.rediff.com 209.85.153.85 www.orkut.com 69.63.181.12 www.facebook.com 208.73.210.28 www.backtracklinux.org SERVER: root@bt:~# gcc udps1.c -o udps root@bt:~# ./udps Requested host name: www.google.com CLIENT: root@bt:~# gcc udpc.c -o udpc root@bt:~# ./udpc 127.0.0.1 enter the host name:www.google.com Requesed IP address:209.85.231.104

c-Program using TCP Sockets(echo server and client)


server: #include"stdio.h" #include"sys/socket.h" #include"sys/types.h" #include"netinet/in.h" #include"unistd.h" void str_echo(int sockfd); int main(int argc,char **argv) { int listenfd,connfd;

pid_t childpid; socklen_t clilen; struct sockaddr_in cliaddr,servaddr; listenfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(9877); bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); listen(listenfd,1024); for(;;) { clilen=sizeof(cliaddr); connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen); if((childpid=fork())==0) { close(listenfd); str_echo(connfd); exit(0); } close(connfd); } } void str_echo(int sockfd) { ssize_t n; char line[100]; for(;;) { if((n=read(sockfd,line,100))==0) return; //connection closed by other end write(sockfd,line,100); } }

client: #include #include #include #include #include #include void str_cli(FILE *fp,int sockfd);

int main(int argc,char **argv) { int sockfd; struct sockaddr_in servaddr; if(argc!=2) { printf("usage: ./filename "); exit(0); } sockfd=socket(AF_INET,SOCK_STREAM,0); bzero((char*)&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9877); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); str_cli(stdin,sockfd); exit(0); } void str_cli(FILE *fp,int sockfd) { char sendline[100],recvline[100]; while(fgets(sendline,100,fp)!=NULL) { write(sockfd,sendline,strlen(sendline)); if(read(sockfd,recvline,100)==0) { printf("server closed prematurely\n"); exit(0); } fputs(recvline,stdout); } }

Program Using Raw Sockets(capturing and sniffing UDP packets)


#include<netinet/in.h> #include<errno.h> #include<netdb.h> #include<stdio.h> #include<netinet/ip_icmp.h> #include<netinet/udp.h> #include<netinet/tcp.h> #include<netinet/ip.h> #include<sys/socket.h> #include<arpa/inet.h> #include<sys/ioctl.h> #include<sys/time.h> #include<sys/types.h> #include<unistd.h>

#include<stdlib.h> void processpacket(unsigned char*,int ); void print_udp_header(unsigned char*,int ); FILE *logfile; int udp=0,total=0,i,j,others=0; struct sockaddr_in source,dest; int main() { int sock_raw; int saddr_size,data_size; struct sockaddr saddr; struct in_addr in; unsigned char *buffer=(unsigned char*)malloc(65536); logfile=fopen("log.txt","w"); if(logfile==NULL) printf("unable to create file"); printf("starting...\n"); sock_raw=socket(AF_INET,SOCK_RAW,IPPROTO_UDP); printf("%d\n",sock_raw); if(sock_raw < 0) { printf("error creating socket\n"); return 1; } while(1) { saddr_size=sizeof(saddr); data_size=recvfrom(sock_raw,buffer,65536,0,&saddr,&saddr_size); if(data_size < 0) { printf("Recvfrom error, failed to get output"); return 1; } processpacket(buffer,data_size); } close(sock_raw); printf("Finished"); return 0; } void processpacket(unsigned char *buffer,int size) { struct iphdr *iph=(struct iphdr*)buffer; ++total; switch(iph->protocol) { case 17: ++udp; print_udp_packet(buffer,size); break; default: ++others; break; } printf("udp: %d,total:%d\n",udp,total); } void print_ip_header(unsigned char *buffer, int size) {

unsigned short iphdrlen; struct iphdr *iph=(struct iphdr*)buffer; iphdrlen=iph->ihl*4; memset(&source,0,sizeof(source)); source.sin_addr.s_addr=iph->saddr; memset(&dest,0,sizeof(dest)); dest.sin_addr.s_addr=iph->daddr; fprintf(logfile,"\n"); fprintf(logfile,"ip header"); fprintf(logfile,"IP version:%d\n",(unsigned int)iph->version); fprintf(logfile,"ip header length: %d Dwords or %d Bytes\n",(unsigned int)iph->ihl,((unsigned int)(iph->ihl))*4); fprintf(logfile,"type of service: %d\n",(unsigned int)iph->tos); fprintf(logfile,"TTL :%d\n",(unsigned int)iph->ttl); fprintf(logfile,"protocol:%d\n",(unsigned int)iph->protocol); fprintf(logfile,"source ip:%s\n",inet_ntoa(source.sin_addr)); fprintf(logfile,"destination ip:%s\n",inet_ntoa(dest.sin_addr)); } void print_udp_packet(unsigned char *buffer,int size) { unsigned short iphdrlen; struct iphdr *iph=(struct iphdr *)buffer; iphdrlen=iph->ihl*4; struct udphdr *udph=(struct udphdr*)(buffer+iphdrlen); fprintf(logfile,"\n\n**************************udp packet*************************\n"); print_ip_header(buffer,size); fprintf(logfile,"\nudp header\n"); fprintf(logfile,"source port:%d\n",ntohs(udph->source)); fprintf(logfile,"destination port:%d\n",ntohs(udph->dest)); fprintf(logfile,"udp length:%d\n",ntohs(udph->len)); fprintf(logfile,"udp check sum:%d\n",ntohs(udph->check)); fprintf(logfile,"\n"); fprintf(logfile,"IP Header\n"); printdata(buffer,iphdrlen); fprintf(logfile,"udp header\n"); printdata(buffer+iphdrlen,sizeof(udph)); fprintf(logfile,"day payload\n"); printdata(buffer+iphdrlen+sizeof(udph),(size-sizeof(udph)-iph->ihl*4)); fprintf(logfile,"\n###################################################"); } void printdata(unsigned char *data,int size) { for(i=0;i<size;i++) { if(i!=0 && i%16==0) { fprintf(logfile," "); for(j=i-16;j<i;j++) { if(data[j]>=32 && data[j]<=128) fprintf(logfile,"%c",(unsigned char)data[j]); else fprintf(logfile,"."); } fprintf(logfile,"\n"); }

if(i%16==0) fprintf(logfile,"%02x",(unsigned int)data[i]); if(i==size-1) { for(j=0;j<15-i%16;j++) fprintf(logfile," "); for(j=i-i%16;j<=i;j++) { if(data[j]>=32 && data[j]<=128) fprintf(logfile,"%c",(unsigned char)data[j]); else fprintf(logfile,"."); } fprintf(logfile,"\n"); } } }

Potrebbero piacerti anche