From dc1f3fbd050f321a02cd63b2865bca7712d37e65 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Tue, 18 Mar 2025 15:19:22 -0400 Subject: [PATCH] Adding procedure soc_openonesock --- lib/devsoc.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++- lib/devsoc.h | 7 +++ lib/subrou.c | 2 +- 3 files changed, 125 insertions(+), 2 deletions(-) diff --git a/lib/devsoc.c b/lib/devsoc.c index 5b42147..314631e 100644 --- a/lib/devsoc.c +++ b/lib/devsoc.c @@ -530,6 +530,122 @@ socptr=(SOCPTR **)rou_addlist((void **)socptr,(void *)soc); return socptr; } /* + +*/ +/********************************************************/ +/* */ +/* 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; diff --git a/lib/devsoc.h b/lib/devsoc.h index c79278d..495b26b 100644 --- a/lib/devsoc.h +++ b/lib/devsoc.h @@ -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); diff --git a/lib/subrou.c b/lib/subrou.c index dc6e098..acf0389 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -21,7 +21,7 @@ //version definition #define VERSION "0.6" -#define RELEASE "5" +#define RELEASE "6" //Public variables PUBLIC int debug=0; //debug level -- 2.47.3