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
*/
/********************************************************/
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;