#include "INET1.h"#include <string.h>#include <curses.h>//#include <alloc.h>#include <stdlib.h>#include<unistd.h>struct character{char ch;struct character *next;};struct message{int length;struct message * down;struct character *next;};struct userpass{ char name[20]; char pass[20];}up[4];//struct userpass abc[4];strcpy(up[0].name,"sirish");/*up[0].pass="sjce";up[1].name="varun";up[1].pass="sjce";up[2].name="satish";up[2].pass="sjce";up[3].name="abhi";up[3].pass="sjce";*/typedef struct message MESS;typedef struct character CHAR;MESS *Head,*lastmess;CHAR *lastchar;/* this part is for the handling of the database management*/void createdatabase(){FILE * f;char ch,word[5];int i,comp=0;int k,z;void addcharacter(char);void addmessage();void display();int countmessage();int sizeofeachmessage();int sizeofdata();Head=(MESS *)malloc(sizeof(MESS));Head->next=NULL;Head->down=NULL;f=fopen("./root1","r");lastmess=Head;if(f==NULL)printf("EROOR WHILE OPENING THE FILE");else{while((ch=fgetc(f))!=EOF){ i=0; printf("%c",ch); word[i++]=ch; while(ch!='\n'&&ch!=EOF) { ch=fgetc(f); printf("%c",ch); if(i<4) { word[i++]=ch; } else if (i<5) { word[i]='\0'; if(strcmp(word,"From")==0) { if(comp==0) { printf("new mail starting"); lastchar=NULL; addmessage(); } if(comp==1) comp=0; else if(comp==0) comp=1; }//end of word comparison i=10; addcharacter(word[0]); addcharacter(word[1]); addcharacter(word[2]); addcharacter(word[3]); }//END OF ELSE IS IT IS NOT WORD else { addcharacter(ch); } }//END OF WHILE FOR END OF NEW LINE}}fclose(f);}void addcharacter(char ch){CHAR *newchar;newchar=(CHAR *) malloc(sizeof(CHAR));if(newchar==NULL)printf("error in creating character\n");newchar->ch=ch;newchar->next=NULL;if((lastmess->next)==NULL)lastmess->next=newchar;elselastchar->next=newchar;lastchar=newchar;}void addmessage(){MESS *newmess;newmess=(MESS *) malloc(sizeof(MESS));newmess->next=NULL;newmess->down=NULL;lastmess->down=newmess;lastmess=newmess;}/*void display(int num){MESS *Mtemp;CHAR *Ctemp;Mtemp=Head;int count=1;while(count<num){ count++; Mtemp=Mtemp->down;} Ctemp=Mtemp->next;while(Ctemp!=NULL) { strcat(buffer,Ctemp->ch); //printf("%c",Ctemp->ch); Ctemp=Ctemp->next; }}*/int countmessage(){MESS * temp;int count=0;temp=Head;while(temp!=NULL){if(temp->next!=NULL)count++;temp=temp->down;}return(count);}int sizeofeachmessage(int messageno){MESS * temp;CHAR *temp1;int count=0,i;temp=Head;for(i=1;i<=messageno;i++){if(temp==NULL)return(0);elsetemp=temp->down;}temp1=temp->next;while(temp1!=NULL){count++;temp1=temp1->next;}return(count);}int sizeofdata(){int nomess,size,total=0,i;int countmessage();int sizeofeachmessage(int);nomess=countmessage();for(i=1;i<=nomess;i++){size=sizeofeachmessage(i);total+=size;}return(total);}/*end of the database management *//*this section is to findout wheater there is list number in message*/int findlistnumber(char buffer[]){int num=0;char val[5];int i,j;i=5;j=0;if(buffer[i]=='\0')return(0);while(1){ if(buffer[i]=='\0') break; else val[j++]=buffer[i++];}i=1;j--;while(j>=0){num=(val[j--]-48)*i;i=i*10;}return(num);}/*end of this section to findout wherther there is a list number*//*this section is for to handle the list command*/void handleLIST(int sockfd, char buffer[]){int i,j,msgno,total;char data[512];msgno=findlistnumber(buffer);printf("The list no is %d",msgno);if(msgno!=0){ if(msgno>countmessage()) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { memset((void *)data,'\0',512); strcpy(data,"+OK "); addinteger(msgno,data); strcat(data," "); addinteger(sizeofeachmessage(msgno),data); writedata(sockfd,data); }}else{ total=countmessage(); memset((void *)data,'\0',512); strcpy(data,"+OK "); addinteger(total,data); strcat(data," messages ("); addinteger(sizeofdata(),data); strcat(data," octets)" ); writedata(sockfd,data); for(i=1;i<=total;i++) { memset((void *)data,'\0',512); addinteger(i,data); strcat(data," "); addinteger(sizeofeachmessage(i),data); writedata(sockfd,data); } writedata(sockfd,"046.");}}/*end of this section which handles the list command*/void handleRETR(int sockfd, char buffer[]){ int i,j,msgno,count,k; char data[512]; MESS *Mtemp; CHAR *Ctemp; msgno=findlistnumber(buffer); printf("The msg no is %d",msgno); if((msgno<=0)||(msgno>countmessage())) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { memset((void *)data,'\0',512); strcpy(data,"+OK "); addinteger(msgno,data); strcat(data,"\n"); //addinteger(sizeofeachmessage(msgno),data); //MESS *Mtemp; //CHAR *Ctemp; Mtemp=Head; count=1; while(count<=msgno) { count++; Mtemp=Mtemp->down; } Ctemp=Mtemp->next; while(Ctemp!=NULL) { // strcat(data,Ctemp->ch); k=strlen(data); data[k]=Ctemp->ch; printf("%c",Ctemp->ch); Ctemp=Ctemp->next; } writedata(sockfd,data); }}void handleDELE(int sockfd,char buffer[]){ int i,j,msgno,count; char data[512]; MESS *Mtemp,*Ptemp; CHAR *Ctemp,*Dtemp; msgno=findlistnumber(buffer); printf("The msg no is %d",msgno); if((msgno<=0)||(msgno>countmessage())) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { memset((void *)data,'\0',512); strcpy(data,"+OK"); strcat(data,"\n"); //addinteger(sizeofeachmessage(msgno),data); //MESS *Mtemp; //CHAR *Ctemp; Ptemp=NULL; Mtemp=Head; count=1; while(count<=msgno) { count++; Ptemp=Mtemp; Mtemp=Mtemp->down; } Ctemp=Mtemp->next; while(Ctemp!=NULL) { //strcat(data,Ctemp->ch); //printf("%c",Ctemp->ch); Dtemp=Ctemp; Ctemp=Ctemp->next; free(Dtemp); } Ptemp->down=(Ptemp->down)->down; free(Mtemp); writedata(sockfd,data); }}void handleupdate(int sockfd){ FILE *f; //int i,j,msgno,count; char data[512],ch; MESS *Mtemp; CHAR *Ctemp; f=fopen("./root2","w"); //msgno=findlistnumber(buffer); //printf("The msg no is %d",msgno); /*if((msgno<=0)||(msgno>countmessage())) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { */memset((void *)data,'\0',512); strcpy(data,"+OK"); //addinteger(msgno,data); // strcat(data,"\n"); //addinteger(sizeofeachmessage(msgno),data); //MESS *Mtemp; //CHAR *Ctemp; Mtemp=Head; //count=1; while((Mtemp)!=NULL) { Ctemp=Mtemp->next; while(Ctemp!=NULL) { //strcat(data,Ctemp->ch); //ch=Ctemp->ch; fprintf(f,"%c",Ctemp->ch); Ctemp=Ctemp->next; } Mtemp=Mtemp->down; } fclose(f); writedata(sockfd,data);}/* this section is for to handle the STAT command */void handleSTAT(int newsockfd){char buffer[512];memset((void *)buffer,'\0',512);strcpy(buffer,"+OK");strcat(buffer," ");addinteger(countmessage(),buffer);strcat(buffer," ");addinteger(sizeofdata(),buffer);writedata(newsockfd,buffer);}/* end of section for to handle the list command*//*THIS SECTION IS FOR TO HANDLE THE NOOP COMMAND*/void handleNOOP(int newsockfd){char buffer[512];memset((void *)buffer,'\0',512);strcpy(buffer,"+OK");writedata(newsockfd,buffer);}/*END OF SESSION TO HANDLE THE NOOP COMMAND*/main(int argc,char * argv){int i,j,k;int sockfd,newsockfd=0,clilen,childpid;struct sockaddr_in cli_addr,serv_addr;if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) printf("error which creating socket");bzero((char *) &serv_addr,sizeof(serv_addr));serv_addr.sin_family =AF_INET;serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);serv_addr.sin_port=htons(SERV_TCP_PORT);if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))<0) printf("error while binding");listen(sockfd,5);for(;;){ clilen=sizeof(cli_addr); printf("before accept%d",newsockfd);// waddstr(stdscr,"before accept"); newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); printf("after accept%d",newsockfd); if(newsockfd<0) printf("error while accepting"); if((childpid=fork())<0) printf("error while forking"); else if(childpid==0) { int n=-1,i; char line[512],buffer[512],ptr[512],data[512],temp[512]; close(sockfd); /*this is the initial process of making the database*/ createdatabase(); /*end of initial phase*//*this is the the first stage 1 indicating that connection has been established properly*/ /*STAGE 1*/ memset((void *)buffer,'\0',512); strcpy(buffer,"+OK POP3 server ready"); if(!(write(newsockfd,buffer,sizeof(buffer)))) printf("eror while writing\n");/* end of stage 1 for sending the greeting to the client that he has successfully connected to the server*/ /* beging of stage 2*/ /* This the authorization stage of the process*/ k=0; //this is to hang on in the loop till a valid thing //does not happen while(k==0) //this for username { readdata(newsockfd,buffer); memset((void *)data,'\0',512); strncpy(data,buffer,4); /* writedata(1,"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); puts(data); writedata(1,"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); */i=0;for(j=4;j<strlen(buffer);j++){ temp[i++]=buffer[j];}temp[i]='\0';if(strcmp(data,"USER")==0){ for(i=0;i<4;i++) { if(strcmp(temp,up[i].name)) { k=1; //strcpy(data,"+OK"); writedata(newsockfd,"+OK"); break; } }} /* if(strcmp(data,"USER")==0) { //he has send the username writedata(newsockfd,"+OK"); k=1; }*/ else { if(strcmp(data,"QUIT")==0) { exit(0); } else { writedata(newsockfd,"-ERR NO MESSAGE"); } }} //END OF WHILE LOOP FOR USERNAME k=0; //this is to hang on in the loop till a valid thing //does not happen while(k==0) //this loop is for password { readdata(newsockfd,buffer); memset((void *)data,'\0',512); strncpy(data,buffer,4); /* writedata(1,"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); puts(data); writedata(1,"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); if(strcmp(data,"PASS")==0) { //he has send the username writedata(newsockfd,"+OK username is valid"); k=1; } */ i=0; for(j=4;j<strlen(buffer);j++) { temp[i++]=buffer[j]; } temp[i]='\0'; if(strcmp(data,"PASS")==0) { for(i=0;i<4;i++) { if(strcmp(temp,up[i].pass)) { k=1; //strcpy(data,"+OK"); writedata(newsockfd,"+OK"); break; } } } else { if(strcmp(data,"QUIT")==0) { exit(0); } else { writedata(newsockfd,"-ERR NO MESSAGE"); } }}//END OF WHILE LOOP FOR PASSWORD /* end of the stage 2 for the authorization */ /*end of AUTHORIZATION*/i=0; for(j=4;j<strlen(buffer);j++){ temp[i++]=buffer[j];} temp[i]='\0';if(strcmp(data,"USER")==0){ for(i=0;i<4;i++) { if(strcmp(temp,up[i].name)) { k=1; //strcpy(data,"+OK"); writedata(newsockfd,"+OK"); break; } }} //if(strcmp(data,"PASS")==0)//{ /* BEGING OF THE NEXT PHASE i.e TRANSACTION */ /*STAGE 3*/for(;;){ readdata(newsockfd,buffer); //reading the request from the the client memset((void *)data,'\0',512); strncpy(data,buffer,4); if(strcmp(data,"STAT")==0) { //SENDING RESPOND FOR stat handleSTAT(newsockfd); } else if(strcmp(data,"NOOP")==0) { handleNOOP(newsockfd); } else if(strcmp(data,"LIST")==0) { handleLIST(newsockfd,buffer); } else if(strcmp(data,"RETR")==0) { handleRETR(newsockfd,buffer); } else if(strcmp(data,"DELE")==0) { handleDELE(newsockfd,buffer); } else if(strcmp(data,"UPDT")==0) { handleupdate(newsockfd); }} /*END OF THE TRANSACTION PHASE*/ /*STAGE 3*//* this is for testing wheather it is reading or not *//* printf("The value of n is %d",n); strncpy(temp,line,4); if(i==1) strcpy(ptr,"+OK received1"); if(i==2) strcpy(ptr,"+OK received2"); write(newsockfd,ptr,512); if(strcmp(temp,"USER")==0) { printf("username is given"); } else if(strcmp(temp,"QUIT")==0) { printf("he is wishing to quit the terminal"); } else { printf("it has read %s",temp); } i++;/*This is indiacate the end of testing of wheather server is reading */ exit(0); } close(newsockfd);}}