Lines 27-32
Link Here
|
27 |
|
27 |
|
28 |
#include "Pop3Client.h" |
28 |
#include "Pop3Client.h" |
29 |
|
29 |
|
|
|
30 |
/* receive full responce */ |
31 |
int do_recv(int s, void *ibuf, size_t len, int flags) |
32 |
{ |
33 |
size_t ret, total; |
34 |
char *p, *buf = ibuf; |
35 |
|
36 |
total = 0; |
37 |
while (1) |
38 |
{ |
39 |
/* left one byte for null termination */ |
40 |
ret = recv(s, buf + total, len - 1 - total, flags); |
41 |
/* if we got error or close, then brea */ |
42 |
if (ret <= 0) |
43 |
{ |
44 |
break; |
45 |
} |
46 |
/* increase size of received data */ |
47 |
total += ret; |
48 |
/* null terminating received data */ |
49 |
buf[total] = 0; |
50 |
/* left one byte for null termination |
51 |
* if out of buffer, return */ |
52 |
if (len - total <= 1) |
53 |
{ |
54 |
break; |
55 |
} |
56 |
/* if we found end of line signal, then stop */ |
57 |
p = strstr(buf, "\r\n"); |
58 |
printf("p == %p\n", p); |
59 |
if (p != 0) |
60 |
{ |
61 |
break; |
62 |
} |
63 |
} |
64 |
|
65 |
/* if there wasn't any data, then return error code */ |
66 |
if (total == 0) |
67 |
{ |
68 |
return ret; |
69 |
} |
70 |
return total; |
71 |
} |
72 |
|
30 |
/* return size if all goes well, -1 if not expected answer */ |
73 |
/* return size if all goes well, -1 if not expected answer */ |
31 |
int send_command(char *exp_answer, char **retour, Pop3 pc) |
74 |
int send_command(char *exp_answer, char **retour, Pop3 pc) |
32 |
{ |
75 |
{ |
Lines 61-78
Link Here
|
61 |
return (pc); |
104 |
return (pc); |
62 |
} |
105 |
} |
63 |
int pop3MakeConnection(Pop3 pc, char *serverName, int port){ |
106 |
int pop3MakeConnection(Pop3 pc, char *serverName, int port){ |
|
|
107 |
struct timeval t; |
64 |
|
108 |
|
65 |
pc->s = socket(AF_INET, SOCK_STREAM, 0 ); |
109 |
pc->s = socket(AF_INET, SOCK_STREAM, 0 ); |
66 |
memset( &(pc->server), 0 , sizeof(pc->server)); |
110 |
memset( &(pc->server), 0 , sizeof(pc->server)); |
67 |
pc->server.sin_family = AF_INET; |
111 |
pc->server.sin_family = AF_INET; |
68 |
pc->hp = gethostbyname(serverName); |
112 |
pc->hp = gethostbyname(serverName); |
69 |
if( pc->hp == 0) |
113 |
if( pc->hp == 0) |
|
|
114 |
{ |
115 |
close(pc->s); |
70 |
return -1; |
116 |
return -1; |
|
|
117 |
} |
71 |
memcpy( &(pc->server.sin_addr), pc->hp->h_addr, pc->hp->h_length); |
118 |
memcpy( &(pc->server.sin_addr), pc->hp->h_addr, pc->hp->h_length); |
72 |
pc->server.sin_port = htons(port); |
119 |
pc->server.sin_port = htons(port); |
73 |
if ( connect(pc->s, (struct sockaddr *)&(pc->server) |
120 |
if ( connect(pc->s, (struct sockaddr *)&(pc->server) |
74 |
, sizeof(pc->server)) < 0 ) |
121 |
, sizeof(pc->server)) < 0 ) |
|
|
122 |
{ |
123 |
close(pc->s); |
75 |
return -1; |
124 |
return -1; |
|
|
125 |
} |
126 |
t.tv_sec = 60; |
127 |
t.tv_usec = 0; |
128 |
setsockopt(pc->s, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t)); |
129 |
setsockopt(pc->s, SOL_SOCKET, SO_SNDTIMEO, &t, sizeof(t)); |
76 |
pc->connected = CONNECTED; |
130 |
pc->connected = CONNECTED; |
77 |
return 0; |
131 |
return 0; |
78 |
} |
132 |
} |
Lines 94-100
Link Here
|
94 |
return -1; |
148 |
return -1; |
95 |
} |
149 |
} |
96 |
|
150 |
|
97 |
size = recv(pc->s,&pc->inBuf,1024,0); |
151 |
size = do_recv(pc->s,&pc->inBuf,1024,0); |
98 |
memset(temp,0,1024); |
152 |
memset(temp,0,1024); |
99 |
memcpy(temp,pc->inBuf,size); |
153 |
memcpy(temp,pc->inBuf,size); |
100 |
if( temp[0] != '+' ){ |
154 |
if( temp[0] != '+' ){ |
Lines 104-113
Link Here
|
104 |
|
158 |
|
105 |
sprintf(pc->outBuf,"USER %s\r\n",name); |
159 |
sprintf(pc->outBuf,"USER %s\r\n",name); |
106 |
send(pc->s, &pc->outBuf,strlen(pc->outBuf),0); |
160 |
send(pc->s, &pc->outBuf,strlen(pc->outBuf),0); |
107 |
size =recv(pc->s,pc->inBuf,1024,0); |
161 |
size = do_recv(pc->s,pc->inBuf,1024,0); |
108 |
memset(temp,0,1024); |
162 |
memset(temp,0,1024); |
109 |
memcpy(temp,pc->inBuf,size); |
163 |
memcpy(temp,pc->inBuf,size); |
110 |
if( temp[0] != '+' ){ |
164 |
if( temp[0] != '+' && temp[0] != '\r' ){ |
111 |
fprintf(stderr,"Invalid User Name\n"); |
165 |
fprintf(stderr,"Invalid User Name\n"); |
112 |
return -1; |
166 |
return -1; |
113 |
} |
167 |
} |
Lines 115-124
Link Here
|
115 |
memset(pc->outBuf,0,1024); |
169 |
memset(pc->outBuf,0,1024); |
116 |
sprintf(pc->outBuf,"PASS %s\r\n",pass); |
170 |
sprintf(pc->outBuf,"PASS %s\r\n",pass); |
117 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
171 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
118 |
size =recv(pc->s,&pc->inBuf,1024,0); |
172 |
size = do_recv(pc->s,&pc->inBuf,1024,0); |
119 |
memset(temp,0,1024); |
173 |
memset(temp,0,1024); |
120 |
memcpy(temp,pc->inBuf,size); |
174 |
memcpy(temp,pc->inBuf,size); |
121 |
if( temp[0] != '+' ){ |
175 |
if( temp[0] != '+' && temp[0] != '\r'){ |
122 |
fprintf(stderr,"Incorrect Password\n"); |
176 |
fprintf(stderr,"Incorrect Password\n"); |
123 |
return -1; |
177 |
return -1; |
124 |
} |
178 |
} |
Lines 213-219
Link Here
|
213 |
/* Find total number of messages in mail box */ |
267 |
/* Find total number of messages in mail box */ |
214 |
sprintf(pc->outBuf,"STAT\r\n"); |
268 |
sprintf(pc->outBuf,"STAT\r\n"); |
215 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
269 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
216 |
size = recv(pc->s,pc->inBuf,1024,0); |
270 |
size = do_recv(pc->s,pc->inBuf,1024,0); |
217 |
if( pc->inBuf[0] != '+' ){ |
271 |
if( pc->inBuf[0] != '+' ){ |
218 |
perror("Error Receiving Stats"); |
272 |
perror("Error Receiving Stats"); |
219 |
return (-1); |
273 |
return (-1); |
Lines 266-272
Link Here
|
266 |
/* Find total number of messages in mail box */ |
320 |
/* Find total number of messages in mail box */ |
267 |
sprintf(pc->outBuf,"STAT\r\n"); |
321 |
sprintf(pc->outBuf,"STAT\r\n"); |
268 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
322 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
269 |
size = recv(pc->s,pc->inBuf,1024,0); |
323 |
size = do_recv(pc->s,pc->inBuf,1024,0); |
270 |
pc->inBuf[size] = '\0'; |
324 |
pc->inBuf[size] = '\0'; |
271 |
#ifdef _DEBUG |
325 |
#ifdef _DEBUG |
272 |
printf(" pop3CheckMail, stat received buf (size=%d): [%s]\n", |
326 |
printf(" pop3CheckMail, stat received buf (size=%d): [%s]\n", |
Lines 313-319
Link Here
|
313 |
|
367 |
|
314 |
sprintf(pc->outBuf,"LAST\r\n"); |
368 |
sprintf(pc->outBuf,"LAST\r\n"); |
315 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
369 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
316 |
size = recv(pc->s,pc->inBuf,1024,0); |
370 |
size = do_recv(pc->s,pc->inBuf,1024,0); |
317 |
pc->inBuf[size] = '\0'; |
371 |
pc->inBuf[size] = '\0'; |
318 |
#ifdef _DEBUG |
372 |
#ifdef _DEBUG |
319 |
printf(" pop3CheckMail, last received buf (size=%d): [%s]\n", |
373 |
printf(" pop3CheckMail, last received buf (size=%d): [%s]\n", |
Lines 325-333
Link Here
|
325 |
#ifdef _DEBUG |
379 |
#ifdef _DEBUG |
326 |
printf(" Error Receiving LAST: [%s]\n", temp); |
380 |
printf(" Error Receiving LAST: [%s]\n", temp); |
327 |
#endif |
381 |
#endif |
328 |
pc->numOfUnreadMessages = pc->numOfMessages; |
382 |
/* TRY STAT instead LAST */ |
|
|
383 |
sprintf(pc->outBuf,"STAT\r\n"); |
384 |
send(pc->s, pc->outBuf, strlen(pc->outBuf),0 ); |
385 |
size = do_recv(pc->s,pc->inBuf,1024,0); |
386 |
pc->inBuf[size] = '\0'; |
387 |
#ifdef _DEBUG |
388 |
printf(" pop3CheckMail, last received buf (size=%d): [%s]\n", |
389 |
size, pc->inBuf); |
390 |
#endif |
391 |
memset(temp,0,1024); |
392 |
memcpy(temp,pc->inBuf,size); |
393 |
if( temp[0] != '+' ){ |
394 |
#ifdef _DEBUG |
395 |
printf(" Error Receiving STAT: [%s]\n", temp); |
396 |
#endif |
397 |
pc->numOfUnreadMessages = pc->numOfMessages; |
398 |
} |
329 |
} |
399 |
} |
330 |
else { |
400 |
if( temp[0] != '+' ){ |
331 |
ptr = strtok(temp, " "); |
401 |
ptr = strtok(temp, " "); |
332 |
ptr = strtok( 0," "); |
402 |
ptr = strtok( 0," "); |
333 |
pc->numOfUnreadMessages = pc->numOfMessages - atoi(ptr); |
403 |
pc->numOfUnreadMessages = pc->numOfMessages - atoi(ptr); |
Lines 545-551
Link Here
|
545 |
printf(" %s\n", pc->outBuf); |
615 |
printf(" %s\n", pc->outBuf); |
546 |
#endif |
616 |
#endif |
547 |
send(pc->s, pc->outBuf, strlen(pc->outBuf), 0); |
617 |
send(pc->s, pc->outBuf, strlen(pc->outBuf), 0); |
548 |
size = recv(pc->s, pc->inBuf, 4096, 0); |
618 |
size = do_recv(pc->s, pc->inBuf, 4096, 0); |
549 |
if ('+' != pc->inBuf[0]) { |
619 |
if ('+' != pc->inBuf[0]) { |
550 |
perror("error while deleting mail"); |
620 |
perror("error while deleting mail"); |
551 |
return (1); |
621 |
return (1); |
Lines 579-585
Link Here
|
579 |
if( pc->connected == NOT_CONNECTED ) |
649 |
if( pc->connected == NOT_CONNECTED ) |
580 |
return -1; |
650 |
return -1; |
581 |
send(pc->s, "quit\r\n", 6,0 ); |
651 |
send(pc->s, "quit\r\n", 6,0 ); |
582 |
size =recv(pc->s,&pc->inBuf,1024,0); |
652 |
size = do_recv(pc->s,&pc->inBuf,1024,0); |
583 |
pc->connected = NOT_CONNECTED; |
653 |
pc->connected = NOT_CONNECTED; |
584 |
if(pc->s != 0) |
654 |
if(pc->s != 0) |
585 |
close(pc->s); |
655 |
close(pc->s); |