#include <stdio.h>
#include <unistd.h>
+#include <string.h>
#include "checkwait_smtpreply.h"
#include "config.h"
#define USEC_WAIT 1
#define LOOP_WAIT 10000
-int checkwait_smtpreply(int sockfd, int replytype)
+#define RFC_REPLY_SIZE 512
+
+char *checkwait_smtpreply(int sockfd, int replytype)
{
size_t len = 0;
- char smtpreply[READ_BUFSIZE];
+ char smtpreply[RFC_REPLY_SIZE + 1];
int timer = 0;
+ smtpreply[RFC_REPLY_SIZE] = '\0';
+
do {
if(replytype != MLMMJ_QUIT && timer > 10) {
usleep(USEC_WAIT);
timer++;
}
- len += read(sockfd, (smtpreply+len), READ_BUFSIZE-len);
+ len += read(sockfd, (smtpreply+len), RFC_REPLY_SIZE - len);
} while(smtpreply[len-1] != '\n' && timer < LOOP_WAIT);
- smtpreply[len] = 0;
+ smtpreply[len] = '\0';
#if 0
printf("replytype = [%d], smtpreply = [%s]\n", replytype, smtpreply);
#endif
if(timer > LOOP_WAIT) {
printf("Timed out in waiting for reply--will try later\n");
- return -1;
+ return (char *)-1;
}
- printf("%s", smtpreply);
+ fprintf(stderr, "%s", smtpreply);
+ /* This case might seem like (and is ATM) total overkill. But it's
+ * easy for us to extend it later on if needed.
+ */
switch(replytype) {
case MLMMJ_CONNECT:
if(smtpreply[0] != '2' || smtpreply[1] != '2')
- return MLMMJ_CONNECT;
+ return strdup(smtpreply);
break;
case MLMMJ_HELO:
if(smtpreply[0] != '2' || smtpreply[1] != '5')
- return MLMMJ_HELO;
+ return strdup(smtpreply);
break;
case MLMMJ_FROM:
if(smtpreply[0] != '2' || smtpreply[1] != '5')
- return MLMMJ_FROM;
+ return strdup(smtpreply);
break;
case MLMMJ_RCPTTO:
if(smtpreply[0] != '2' || smtpreply[1] != '5')
- return MLMMJ_RCPTTO;
+ return strdup(smtpreply);
break;
case MLMMJ_DATA:
if(smtpreply[0] != '3' || smtpreply[1] != '5')
- return MLMMJ_DATA;
+ return strdup(smtpreply);
break;
case MLMMJ_DOT:
if(smtpreply[0] != '2' || smtpreply[1] != '5')
- return MLMMJ_DOT;
+ return strdup(smtpreply);
break;
case MLMMJ_QUIT:
if(smtpreply[0] != '2' || smtpreply[1] != '2')
- return MLMMJ_QUIT;
+ return (char *)0xDEADBEEF;
break;
case MLMMJ_RSET:
if(smtpreply[0] != '2' || smtpreply[1] != '5')
- return MLMMJ_RSET;
+ return (char *)0xDEADBEEF;
break;
default:
break;
}
- return 0;
+ return NULL;
}