From 9b6096e424942496e1367a812d08f5e40934dc88 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Sun, 8 Jun 2025 07:37:57 -0400 Subject: [PATCH] Improving gestcp.c (tcp_smtp_command) --- app/feeder.c | 2 +- lib/Makefile | 7 ++--- lib/devsoc.c | 1 + lib/devsoc.h | 7 ----- lib/gestcp.c | 65 ++++++++++++++++++++++++++++++++++++++++++ lib/gestcp.h | 17 ++++++++++- lib/lvleml.c | 79 +++++++++++----------------------------------------- lib/subafn.h | 5 ++++ 8 files changed, 108 insertions(+), 75 deletions(-) diff --git a/app/feeder.c b/app/feeder.c index 01592bd..86f47fb 100644 --- a/app/feeder.c +++ b/app/feeder.c @@ -453,7 +453,7 @@ while (proceed==true) { break; case 1 : //waiting for a line with CRLF received=(char *)0; - got=tcp_getline(socptr,WAITLINE,&received); + got=tcp_getline(socptr,WAITRMT,&received); (void) rou_alert(3,"%s, received=<%s>",OPEP,received); switch (got) { case 0 : //Reading timeout diff --git a/lib/Makefile b/lib/Makefile index 86f9100..1810d86 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -64,10 +64,8 @@ devlog.o: \ devlog.h devlog.c devsoc.o: \ - subrou.h \ - unieml.h \ - uniprc.h \ - unitls.h \ + subafn.h subrou.h \ + unieml.h uniprc.h unitls.h \ devsoc.h devsoc.c devsql.o: \ @@ -115,6 +113,7 @@ lvleml.h: \ gestcp.h: \ subrou.h \ + unidns.h \ devlog.h devsoc.h gesspf.h: \ diff --git a/lib/devsoc.c b/lib/devsoc.c index c78fd08..a3e0772 100644 --- a/lib/devsoc.c +++ b/lib/devsoc.c @@ -18,6 +18,7 @@ #include #include "subrou.h" +#include "subafn.h" #include "unieml.h" #include "uniprc.h" #include "unitls.h" diff --git a/lib/devsoc.h b/lib/devsoc.h index b0ffad5..7e6eb22 100644 --- a/lib/devsoc.h +++ b/lib/devsoc.h @@ -12,13 +12,6 @@ #include #include -//Need to have GNU_SOURCE define within CFLAGS -#ifdef AI_ALL -#define HINTFLG AI_ALL|AI_CANONNAME|AI_CANONIDN -#else /*AI_ALL is not defined in linux <= rh9 */ -#define HINTFLG AI_CANONNAME -#endif - //convenient adress structure typedef struct sockaddr SOCKADDR; diff --git a/lib/gestcp.c b/lib/gestcp.c index eaeabd2..3bdce19 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -112,3 +112,68 @@ if (socptr!=(SOCPTR *)0) { } return sent; } +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to wait answer from remote and log */ +/* all answer from remote. */ +/* return an SMTP reply code. */ +/* */ +/********************************************************/ +PUBLIC int tcp_get_smtp_reply(RMTTYP *rmt,int wait) + +{ +int code; +int maxlines; + +code=ERRPROC; +maxlines=20; //maximun number of line error +while (maxlines>0) { + char *line; + int sofar; + + line=(char *)0; + if (tcp_getline(rmt->socptr,wait,&line)<0) { + char cmt[100]; + + (void) snprintf(cmt,sizeof(cmt),"%d Timeout waiting '%d' sec for MX <%s>", + ERRPROC,wait,rmt->curmx->mxname); + line=strdup(cmt); + } + (void) log_fprintlog(rmt->logptr,false,line); + if (sscanf(line,"%d%n",&code,&sofar)==1) { + if (line[sofar]==' ') + maxlines=0; //found remote status + else + code=ERRPROC; + } + line=rou_freestr(line); + maxlines--; + } +return code; +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to build a command and send it to */ +/* remote server, log request and return the */ +/* remote response code. */ +/* */ +/********************************************************/ +PUBLIC int tcp_smtp_command(RMTTYP *rmt,char *fmt,...) + +{ +va_list args; +char strloc[300]; + +va_start(args,fmt); +(void) vsnprintf(strloc,sizeof(strloc),fmt,args); +va_end(args); +(void) tcp_write(rmt->socptr,strloc); +(void) log_fprintlog(rmt->logptr,false,strloc); +return tcp_get_smtp_reply(rmt,WAITRMT); +} diff --git a/lib/gestcp.h b/lib/gestcp.h index dfdb579..191aaca 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -10,10 +10,19 @@ #include #include "subrou.h" +#include "unidns.h" #include "devlog.h" #include "devsoc.h" -#define WAITLINE 30 //full line waiting time +//structure handling remote +typedef struct { + SOCPTR *socptr; //socket to remote pointer + LOGPTR *logptr; //Log pointer + char *orgdomain; //originator domain + char *dstdomain; //Domain to be reached + MXTYP *curmx; //Current MX number + MXTYP **mxs; //MX list form domain + }RMTTYP; //read a line from contact up to CRLF extern int tcp_getline(SOCPTR *socptr,u_int secwait,char **lineptr); @@ -21,4 +30,10 @@ extern int tcp_getline(SOCPTR *socptr,u_int secwait,char **lineptr); //Transmit formated data to the contact channel extern int tcp_write(SOCPTR *socptr,char *buffer); +//wait and answer from remote and return the reply code +extern int tcp_get_smtp_reply(RMTTYP *rmt,int wait); + +//Transmit a command to remote SMTP server +extern int tcp_smtp_command(RMTTYP *rmt,char *fmt,...); + #endif diff --git a/lib/lvleml.c b/lib/lvleml.c index 4a65ec2..a9362fb 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -18,20 +18,12 @@ #include #include "subrou.h" -#include "unidns.h" #include "unieml.h" #include "devlog.h" +#include "gestcp.h" #include "geseml.h" #include "lvleml.h" -typedef struct { - SOCPTR *socptr; //socket to remote pointer - LOGPTR *logptr; //Log pointer - char *orgdomain; //originator domain - char *dstdomain; //Domain to be reached - MXTYP *curmx; //Current MX number - MXTYP **mxs; //MX list form domain - }RMTTYP; /* ^L */ @@ -379,7 +371,7 @@ while (proceed==true) { char *line; completed=false; - got=tcp_getline(contact->socptr,WAITLINE,&line); + got=tcp_getline(contact->socptr,WAITRMT,&line); if (got<0) { //data timeout phase=999; //trouble trouble break; //exiting loop @@ -682,48 +674,6 @@ return true; */ /********************************************************/ /* */ -/* Procedure to check answer from remote SMTP */ -/* MX server. */ -/* Return the SMTP status code. */ -/* */ -/********************************************************/ -static int get_smtp_reply(RMTTYP *rmt,int wait) - -{ -int code; -int maxlines; - -code=ERRPROC; -maxlines=20; //maximun number of line error -while (maxlines>0) { - char *line; - int sofar; - - line=(char *)0; - if (tcp_getline(rmt->socptr,wait,&line)<0) { - char cmt[100]; - - (void) snprintf(cmt,sizeof(cmt),"%d Timeout waiting '%d' sec for MX <%s>", - ERRPROC,wait,rmt->curmx->mxname); - line=strdup(cmt); - } - (void) log_fprintlog(rmt->logptr,false,line); - if (sscanf(line,"%d%n",&code,&sofar)==1) { - if (line[sofar]==' ') - maxlines=0; //found remote status - else - code=ERRPROC; - } - line=rou_freestr(line); - maxlines--; - } -return code; -} -/* -^L -*/ -/********************************************************/ -/* */ /* Procedure to to send ehlo (or helo) to remote */ /* MX server. */ /* Return true if succesfull */ @@ -732,6 +682,8 @@ return code; static _Bool greetings_rmt(RMTTYP *rmt) { +#define OPEP "lvleml.c:greetings_rmt," + _Bool done; int phase; _Bool proceed; @@ -740,32 +692,33 @@ done=false; phase=0; proceed=true; while (proceed==true) { - char cmt[100]; + int rspcode; + rspcode=ERRPROC; switch (phase) { case 0 : //Sending EHLO - (void) snprintf(cmt,sizeof(cmt),"EHLO %s",rmt->orgdomain); - (void) tcp_write(rmt->socptr,cmt); - (void) log_fprintlog(rmt->logptr,false,cmt); - switch (get_smtp_reply(rmt,WAITRMT)) { + rspcode=tcp_smtp_command(rmt,"EHLO %s",rmt->orgdomain); + switch (rspcode) { case CMDOK : //So fare, so good done=true; phase=999; break; default : //Trouble + (void) rou_alert(0,"%s, EHLO Remote <%s> unexpected answer code '%d'", + OPEP,rmt->curmx,rspcode); break; } break; case 1 : //Sending HELO - (void) snprintf(cmt,sizeof(cmt),"HELO %s",rmt->orgdomain); - (void) tcp_write(rmt->socptr,cmt); - (void) log_fprintlog(rmt->logptr,false,cmt); - switch (get_smtp_reply(rmt,WAITRMT)) { + rspcode=tcp_smtp_command(rmt,"HELO %s",rmt->orgdomain); + switch (rspcode) { case CMDOK : //So fare, so good done=true; phase=999; break; default : //Trouble + (void) rou_alert(0,"%s, HELO Remote <%s> unexpected answer code '%d'", + OPEP,rmt->curmx,rspcode); break; } break; @@ -776,6 +729,8 @@ while (proceed==true) { phase++; } return done; + +#undef OPEP } /* ^L @@ -829,7 +784,7 @@ while (proceed==true) { case 2 : //waiting for signon int res; - res=get_smtp_reply(rmt,WAITRMT); + res=tcp_get_smtp_reply(rmt,WAITRMT); switch (res) { case SIGNON : //everything fine break; diff --git a/lib/subafn.h b/lib/subafn.h index 4c84d90..d24537e 100644 --- a/lib/subafn.h +++ b/lib/subafn.h @@ -16,7 +16,12 @@ //space to store (at least) IPV6 number #define AFT sizeof(struct in6_addr) +//Need to have GNU_SOURCE define within CFLAGS +#ifdef AI_ALL #define HINTFLG AI_ALL|AI_CANONNAME|AI_CANONIDN +#else /*AI_ALL is not defined in linux <= rh9 */ +#define HINTFLG AI_CANONNAME +#endif typedef struct { char *strnumip; //IP in string format -- 2.47.3