00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include <stdlib.h>
00040
00041 #ifdef WIN32
00042 #include <sys\types.h>
00043 #include <winsock.h>
00044 #include <windows.h>
00045 #include <process.h>
00046 #else
00047
00048 #ifdef Lynx
00049 #include <sys/types.h>
00050 #else
00051 #include <sys/types.h>
00052 #endif
00053
00054 #include <sys/socket.h>
00055 #include <errno.h>
00056 #include <netdb.h>
00057
00058 #ifdef Linux
00059
00060
00061 #elif Lynx
00062 #include <netinet/in.h>
00063 #else
00064 #include <in.h>
00065 #endif
00066
00067 #endif
00068
00069 #if defined(VMS)
00070 #include <inet.h>
00071 #include <signal.h>
00072 #include <unixio.h>
00073 #endif
00074
00075 extern int imySigS;
00076
00077
00078
00079 int rconnect( char *cNode,
00080 int iPort,
00081 int *piMaxTime,
00082
00083
00084
00085
00086 int *piSocket)
00087 {
00088 char cModule[32] = "rconnect";
00089 int iDebug = 0;
00090 int iSocket = 0;
00091 int iMaxTime = *piMaxTime;
00092 int iTime = 0;
00093 int iError;
00094 int iSleep = 0;
00095 int iMaxSleep = 10;
00096
00097 struct hostent *pHE = NULL;
00098 struct hostent sHE;
00099 struct sockaddr_in sSockAddr;
00100 unsigned long lAddr;
00101
00102 if ( ( pHE = gethostbyname(cNode) ) == NULL )
00103 {
00104 lAddr = inet_addr(cNode);
00105 if ( ( pHE = gethostbyaddr(
00106 (char *)&lAddr, sizeof(lAddr), AF_INET ) ) == NULL )
00107 {
00108 fprintf(stderr,"-E- %s: unknown host %s\n", cModule, cNode );
00109 iError = -1;
00110 goto gError;
00111 }
00112 if (iDebug)
00113 printf("-D- %s: gethostbyaddr succeeded\n", cModule);
00114 }
00115 else if (iDebug)
00116 printf("-D- %s: gethostbyname succeeded\n", cModule);
00117 sHE = *pHE;
00118
00119 gRetryConnect:;
00120
00121
00122 if ( ( iSocket = socket(
00123 AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
00124 {
00125 perror("-E- rconnect(socket)");
00126 iError = errno;
00127 goto gError;
00128 }
00129
00130 sSockAddr.sin_family = AF_INET;
00131 sSockAddr.sin_port = 0;
00132 sSockAddr.sin_addr.s_addr = INADDR_ANY;
00133
00134 if ( bind ( iSocket,
00135 (struct sockaddr *) &sSockAddr,
00136 sizeof(sSockAddr) ) == -1 )
00137 {
00138 perror("-E- rconnect(bind)");
00139 iError = errno;
00140 goto gError;
00141 }
00142
00143 sSockAddr.sin_family = AF_INET;
00144 sSockAddr.sin_port = htons( (short) iPort );
00145 sSockAddr.sin_addr = * ( (struct in_addr *) sHE.h_addr );
00146
00147 if ( connect( iSocket,
00148 (struct sockaddr *) &sSockAddr,
00149 sizeof(sSockAddr) ) == -1 )
00150 {
00151
00152 #ifdef WIN32
00153 if ( (iTime < iMaxTime) || (iMaxTime == -1) )
00154 {
00155 closesocket(iSocket);
00156 iSocket = -1;
00157 #else
00158 if ( (errno == ECONNREFUSED) &&
00159 ( (iTime < iMaxTime) || (iMaxTime == -1) ) )
00160 {
00161 close(iSocket);
00162 iSocket = -1;
00163 if (imySigS) goto gError;
00164 #endif
00165
00166 if (iSleep < iMaxSleep) iSleep++;
00167 iTime += iSleep;
00168 if (iDebug)
00169 printf(" still trying (after %d sec) ...\n",iSleep);
00170 #ifdef WIN32
00171 Sleep(iSleep*1000);
00172 #else
00173 sleep(iSleep);
00174 #endif
00175 goto gRetryConnect;
00176 }
00177 #ifdef WIN32
00178 iError = -1;
00179 #else
00180 perror("-E- rconnect(connect)");
00181 iError = errno;
00182 #endif
00183 goto gError;
00184 }
00185
00186 *piMaxTime = iTime;
00187 *piSocket = iSocket;
00188 return(0);
00189
00190 gError:
00191 if (iSocket)
00192 {
00193 shutdown(iSocket, 2);
00194 #ifdef WIN32
00195 closesocket(iSocket);
00196 #else
00197 close(iSocket);
00198 #endif
00199 }
00200
00201 *piSocket = -1;
00202 *piMaxTime = -1;
00203 return iError;
00204 }
00205
00206
00207