]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Adding procedure soc_openonesock
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Tue, 18 Mar 2025 19:19:22 +0000 (15:19 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Tue, 18 Mar 2025 19:19:22 +0000 (15:19 -0400)
lib/devsoc.c
lib/devsoc.h
lib/subrou.c

index 5b42147e2dca1b7ecfcbacde219341d7dc3128a5..314631e8de9d4db029ae92fa4b40975240272776 100644 (file)
@@ -530,6 +530,122 @@ socptr=(SOCPTR **)rou_addlist((void **)socptr,(void *)soc);
 return socptr;
 }
 /*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*     Procedure to open a channel with a remote smtp  */
+/*      server. Return a socptr if successful.          */
+/*                                                     */
+/********************************************************/
+PUBLIC SOCPTR *soc_openonesock(PROTYP proto,const char *ip,const char *port)
+
+{
+#define OPEP    "devsoc.c:soc_openonesoc"
+
+SOCTYP *soc;
+int status;
+int handle;
+fd_set rset;
+fd_set wset;
+int mxtime;
+struct timeval timeout;
+struct addrinfo hints;
+struct addrinfo *ai;
+int phase;
+_Bool proceed;
+
+soc=(SOCTYP *)0;
+status=0;
+handle=0;
+FD_ZERO(&rset);
+FD_ZERO(&wset);
+mxtime=10;
+timeout.tv_usec=0;;
+timeout.tv_sec=mxtime;
+(void) memset(&hints,'\000',sizeof(hints));
+hints.ai_family=PF_UNSPEC;
+hints.ai_flags=HINTFLG;
+hints.ai_socktype=SOCK_STREAM;
+ai=(struct addrinfo *)0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //Do we have parameters
+      if ((ip!=(const char *)0)&&(port!=(const char *)0)) {
+        (void) rou_alert(0,"%s, ip (%s) or port (%s) missing (config?)",
+                            OPEP,ip,port);
+        phase=999;      //no need to go further
+        }
+      break;
+    case 2      :       //is address a good one
+      if ((status=getaddrinfo(ip,port,&hints,&ai))!=0) {
+        (void) rou_alert(0,"%s, Unable to find a address about "
+                           "IP:port '%s:%s' (error='%s')",
+                            OPEP,ip,port,gai_strerror(status));
+        phase=999;      //no need to go further
+        }
+      break;
+    case 3      :       //lets create socket
+      if ((handle=socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol))<0) {
+        (void) rou_alert(0,"%s Unable to open socket for <%s> (error='%s')",
+                          OPEP,ai->ai_canonname,strerror(errno));
+        phase=999;      //no need to go further
+        }
+      break;
+    case 4      :       //connecting to remote
+      if (connect(handle,ai->ai_addr,ai->ai_addrlen)<0) {
+        switch (errno) {
+          case EINPROGRESS      :       //its acceptable
+            break;
+          default               :
+            (void) rou_alert(1,"%s unable to make connection with '%s.%s' "
+                               "(error=<%s>)",
+                              OPEP,ip,port,strerror(errno));
+            phase=999;
+            break;
+          }
+        }
+      break;
+    case 5      :       //wait for connect completion
+      switch (select(handle+1,&rset,&wset,(fd_set *)0,&timeout)) {
+        case -1         :
+          (void) rou_alert(1,"%s trouble to establish connection with '%s.%s' "
+                             "(error=<%s>)",
+                             OPEP,ip,port,strerror(errno));
+          (void) close(handle);
+          phase=999;            //no ned to go further
+          break;
+        case 0          :
+          (void) rou_alert(1,"%s Unable establish connection with '%s.%s' "
+                              "within %d sec, (error=<%s>)",
+                              OPEP,ip,port,mxtime,strerror(ETIMEDOUT));
+          (void) close(handle);
+          phase=999;            //no ned to go further
+          break;
+        default         :       //everything fine
+          break;
+        }
+      break;
+    case 6      :       //socket is now ready
+      soc=newsocket();
+      soc->proto=proto;
+      soc->handle=handle;
+      soc->ip=strdup(ip);
+      soc->port=strdup(port);
+      soc->iteration=1;
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return (SOCPTR *)soc;
+#undef  OPEP
+}
+/*
 ^L
 */
 /********************************************************/
@@ -609,7 +725,7 @@ while (proceed==true) {
       if ((status=getaddrinfo(soc->ip,soc->port,&hints,&tobind))!=0) {
         (void) rou_alert(0,"%s, Unable to find a address about "
                            "IP:port '%s:%s' (error='%s')",
-                            soc->ip,soc->port,gai_strerror(status));
+                            OPEP,soc->ip,soc->port,gai_strerror(status));
         phase=999;      //no need to go further
         }
       break;
index c79278d6d984ceb46d0bc2fbf323c304c7071ecc..495b26b1189eec2fd346b90d6fac7d066ab8a563 100644 (file)
@@ -38,6 +38,13 @@ extern SOCPTR **soc_freebindinf(SOCPTR **socptr);
 extern SOCPTR **soc_mkbindinf(SOCPTR **s,PROTYP proto,
                               const char *ip,const char *port,int iteration);
 
+//procedure to open one exchange socket
+//to connect a remote smtp server
+extern SOCPTR *soc_openonesock(PROTYP proto,const char *ip,const char *port);
+
+//procedure to cloe and exchange socket connected to a remote smtp server
+extern SOCPTR *soc_closeonesock(SOCPTR *socptr);
+
 //procedure to return the number of channel to open on the soc
 extern int soc_getiterations(SOCPTR *socptr);
 
index dc6e098b190d8957060666bd1fe30f8586907ed4..acf03892818f0954c70c29ccee4c3e3c599f647f 100644 (file)
@@ -21,7 +21,7 @@
 
 //version definition 
 #define VERSION "0.6"
-#define RELEASE "5"
+#define RELEASE "6"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level