]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Improving gestcp.c (tcp_smtp_command)
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sun, 8 Jun 2025 11:37:57 +0000 (07:37 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sun, 8 Jun 2025 11:37:57 +0000 (07:37 -0400)
app/feeder.c
lib/Makefile
lib/devsoc.c
lib/devsoc.h
lib/gestcp.c
lib/gestcp.h
lib/lvleml.c
lib/subafn.h

index 01592bdd3c91a44551b9e65b394c4883cab25e79..86f47fba0dc160b437c6d88193eb41b30b37c178 100644 (file)
@@ -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
index 86f9100405ce74645552e4a7491c9fc350028367..1810d86ad92e6b8963b10531f17ab06df0432bd9 100644 (file)
@@ -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:                                      \
index c78fd08c1ddf0d1e09cb1ad92475d1ac722ae692..a3e07726ed0739121acd271eba22b363c24c2aee 100644 (file)
@@ -18,6 +18,7 @@
 #include        <unistd.h>
 
 #include       "subrou.h"
+#include       "subafn.h"
 #include       "unieml.h"
 #include       "uniprc.h"
 #include       "unitls.h"
index b0ffad5c1f51ecf74417d1e8e6e104ba65ce04bb..7e6eb223f77cecd470546c0bcca060277b1e499e 100644 (file)
 #include        <stdbool.h>
 #include        <time.h>
 
-//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;
 
index eaeabd251405dc02e7ab2e43467439bde69efb8e..3bdce19061d5984037c4dd957d7f38f5db845dd2 100644 (file)
@@ -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);
+}
index dfdb5790b27cc658843a15f1980f2ee67f1b5ca3..191aaca1b3265bad7327f98193956c605115979e 100644 (file)
 #include        <stdbool.h>
 
 #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
index 4a65ec2371cce7f267130909a46e71583996557e..a9362fb3e867373d22aff30d66122b74f8948759 100644 (file)
 #include        <unistd.h>
 
 #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;
index 4c84d905150f4f783a6a5391cb20c17662e0e553..d24537e006e9c1e41c07418803fb94be6621448d 100644 (file)
 //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