Click to See Complete Forum and Search --> : POP3 Protocol Program Explanation Needed


Inferno2k
December 26th, 2004, 10:20 AM
Hi There,

This is my first post here, am doing this to help a very close friend that does not have access to the internet at the moment and has this project for college that she is not able to understand, if someone can explaing what exactly is happening here in the 1st 2 functions before main that are marked **EXPLAIN** i would be very grateful and any help is appericiated.


A basic summary of the program would also be very very helpful as to how exactly the protocol is working.

Regards
Inferno

--------------------------------------------------------------------------------------------------------
pop3 client


#include "header.h"
#include "help.c"


**EXPLAIN THIS FUNCTION***
int sockfd;
int NoAndSize(char* response,int* MailList long* Size)
{
int i=0;
int k=0;
int No=0;
while(response[i]!='\n')i++;
while(response[i]!='\0')
{
char temp[10];
if(response[i]=='\n')
{
i++;
if(response[i]!='\0')
{
k=0;
char temp[4];
while(response[i]!= ' ')
{
temp[k]=response[i];
k++;
i++;
}
temp[k]=0;
MailList[No]=atoi(temp);
No++;
k=0;
while(response[i]!='\r')
{
i++;
temp[k]=response[0]);
k++;
}
temp[k]=0;
k=No-1;
Size[k]=atol(temp);
}
else
break;
}
i++;
}
return(No);
}

***END OF 1st FUNCTION***

***EXPLAIN THIS 2nd FUNCTION***
void SubDatFrom(int No,char* Sub char* Date,char *From)
{
char buff[80];
char temp[10];
char Msg[750];
int n,i=0,k=0;
char c[2];

Sub[0]=0;
Date[0]=0;
From[0]=0;
strcpy(buff,"RETR");
sprintf(temp,"%d\r\n",No);
strcat(buff,temp);
write(sockfd,buff,strlen(buff));
Msg[0]=0;
while(1)
{
int i=0;
char ch;
char line[80];
while(i<80)
{
read(sockfd,&ch,1);
if(ch=='\n')
break;
else
line[i++]=ch;
}
line[i++]='\n';
line[i]=0;
if(strcmp(line,"\r\n"))
strcat(Msg,line);
else
break;
}

while(!strstr(buff,"\r\n.\r\n"))
{
n=read(sockfd,buff,80);
}
i=0;
while(Msg[i]!='\0')
{
int NoB;
if(Msg[i]=='\n' && Msg[i+1]=='F')
{
while(Msg[i]!=' ') i++;
i++;
while(Msg[i]!='\n')
{
From[k]=Msg[i];
k++;
i++;
}
From[k-1]=0;
k=0;
}
else if(Msg[i]=='\n' && Msg[i+1]=='S' && Msg[i+2]='u')
{
while(Msg[i]!=' ') i++;
i++;
While(Msg[i]!='\n')
{
Sub[k]=Msg[i];
k++;
i++;
}
Sub[k-1]=0;
k=0;
}
else if(Msg[i]=='\n' && Msg[i+1]=='D')
{
NoB=0;
while(Msg[i]!=' ' ) i++;
i++;
while(NoB<4)
{
Date[k]=Msg[i];
k++;
i++;
if(Msg[i]==' ')
NoB++;
}
Date[k-1]=0;
k=0;
}
else
i++;
}
}

**END OF 2nd FUNCTION***

main()
{

struct sockaddr_in servaddr;
char buff[80];
char user[30];
char response[1024];
char password[30];
char c;
int ch,n,i=0,j;
int authenticated=0,attempts=0;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
perror("Socket error");
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(POP3PORT);
servaddr.sin_addr.s_addr=inet_addr("178.1.10.5");
if(connect(sockfd,(struct sockaddr*) &servaddr,sizeof(servaddr,sizeof(servaddr))<0)
{
perror("Connect error");
exit(1);
}
n=read(sockfd,buff,80);
buff[n]='\0';
fprintf(stderr,(buff+4)); **WHAT EXACTLY IS THIS LINE DOING***
do
{
attempts++;
fprintf(stderr,"Username : " );
n=read(1,buff,30);
buff[n-1]=0;
strcpy(user,"USER ");
strcat(user,buff);
strcat(user,"\r\n");
write(sockfd,userstrlen(user));
n=read(sockfd,buff,80);
buff[n-1]='\0';
if(strncmp(buff,"-ERR",4))
{
fprintf(stderr,"%s\n",(buff+4));
strcpy(password,"PASS");
strcat(password,getpass("Password: "));
strcat(password,"\r\n");
write(sockfd,password,strlen(password));
n=read(sockfd,buff,80);
buff[n]='\0';
if(strcmp(buff,"-ERR",4))
{
fprintf(stderr,"Login succeeded\n");
fprintf(stderr,"%s\n",(buff+4));
authenticated =1;
}
else
{
fprintf(stderr,"Error : %s",buff+5);
authenticated=0;
}
}
else
{
fprintf(stderr,"No such user\n");
} while(authenticated==0 && attempts<3);

if(attempts>=3)
{
fprintf(stderr,"Number of attempts exceeded\n Better Luck next time\n Closing connection\n");
write(sockfd,"quit\r\n",6);
n=read(sockfd,buff,80);
buff[n]='\0';
exit[1];
}

while(1)
{
fprintf(stderr,"pop3bc> ");
n=read(1,buff,80);
buff[n]=0;
if(!strcasecmp(buff,"list\n"))
{
char* Sub;
char* Date;
char* From;
long* Size;
int no;
int* MailList;
MailList=(int*)malloc(100);
sub=(char*)malloc(80);
From=(char*)malloc(25);
Date=(char*)malloc(50);
Size=(long*)malloc(100);
write(sockfd,"LIST\r\n",6);
n=read(sockfd,response,255);
response='[n-3]=0;
fprintf(stderr,"MailBox Scan listing follows\n");
No=NoAndsize(response,MailList,Size);
fprintf(stderr,"\n\nNo. Date \t\tFrom\t\t\t\tSize (KB) Subject\t\t\t\t\t\t\t\n");
fprintf(stderr,"-------------------------------------------------------------\n");
for(i=0;i<No;i++)
{
SubDataFrom(MailList[i],Sub,Date,From);
fprintf(stderr,"%d %s%s%ld\t %s\t\t\t\t\n",i+1,Date,From,Size[i]/1024+1,sub);
}
}
else if(strstr(buff,"help))
{
char cmd[7];
int j=0;
i=4;
while(buff[i]==' ')i++;
if(buff[i]=='\n')
show_cmds();
else
{
while(buff[i]!='\n')
Cmd[j++]=buff[i++];
Cmd[j]='\0';
show_help(Cmd);
}
}
else if(!strcasecmp(buff,"reset\n"))
{
write(sockfd,"RSET\r\n",6);
n=read(sockfd,buff,30);
buff[n]=0;
fprintf(stderr,"%s",buff+4);
}
else if(!strcasecmp(buff,"noop\n"))
{
write(sockfd,"NOOP\r\n",6);
n=read(sockfd,buff,30);
buff[n]=0;
fprintf(stderr,"NOOP successful\n ");
}
else if(strstr(buff,"read"))
{
int Header=1;
i=4;
while(buff[i]==' ') i++;
if(buff[i]=='\n')
{
fprintf(stderr,"Message number: ");
scanf("%d",&n);
sprintf(buff,"RETR %d\r\n",n);
}
else
{
buff[strlen(buff)-1]='\0';
sprintf(buff,"RETR %s \r\n",buff+i);
}
i=0;
write(sockfd,buff,strlen(buff));
n=read(sockfd,buff,40);
fprintf(stderr,"Error: No such message\n");
continue;
}
fprintf(stderr,"\n*****************************************Message Starts Here*****\n\n");
while(1)
{
i=0;
while(1)
{
n=read(sockfd,&c,1);
buff[i]=c;
i++;
if(c=='\n')
{
buff[i]=0;
break;
}
}

if(Header)
{
if(strstr(buff,"From: ") || strstr(buff,"Cc: ") || strstr(buff,"Date: ") | strstr(buff,"Subject"))
fprintf(stderr,"%s",buff);
else if(!strcmp(buff,"\r\n"))
{
fprintf(stderr,"\nContents : \n");
Header=0;
}
}
else if(!strcmp(buff,".\r\n"))
{
fprintf(stderr,"\n\n************************Message finished*****************\n");
break;
}
else
fprintf(stderr,"%s",buff);
}
}
elseif(strstr(buff,"delete"))
{
i=6;
while(buff[i]==' ') i++;
if(buff[i]=='\n')
else
{
buff[strlen](buff)-1]='\0';
sprintf(buff,"DELETE\s\r\n",buff+1);
}
write(sockfd,buff,strlen(buff));
n=read(sockfd,buff,80);
buff[n]='\0';
if(buff[o]=='-')
fprintf(stderr,"%s",buff+4);
}
else if(!strcmp(buff,"quit\n"))
{
write(sockfd,"QUIT\r\n",6);
n=read(sockfd,response,80);
response[n]=0;
fprintf(stderr,"Have a Nice Day\n%s",(response-4));
exit(0);
}
else
{
fprintf(stderr,"No such command\n");
}
}
}
--------------------------------------------------------------------------------------------------------