Go to the documentation of this file.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 #include <stdio.h>
00027 #include <string.h>
00028 #include <stdlib.h>
00029
00030 #ifdef WIN32
00031 #include <sys\types.h>
00032 #include <winsock.h>
00033 #include <windows.h>
00034 #include <process.h>
00035 #else
00036 #include <sys/types.h>
00037 #include <errno.h>
00038 #include <unistd.h>
00039 #include <netdb.h>
00040
00041 #ifdef Lynx
00042 #include <netinet/in.h>
00043 #include <socket.h>
00044 #elif Linux
00045 #include <arpa/inet.h>
00046 #include <sys/socket.h>
00047 #else
00048 #include <netinet/in.h>
00049 #include <sys/socket.h>
00050 #endif
00051
00052 #endif
00053
00054 extern int imySigS;
00055
00056
00057
00058 int rconnect( char *cNode,
00059 int iPort,
00060 int *piMaxTime,
00061
00062
00063
00064
00065 int *piSocket)
00066 {
00067 char cModule[32] = "rconnect";
00068 int iDebug = 0;
00069 int iSocket = 0;
00070 int iMaxTime = *piMaxTime;
00071 int iTime = 0;
00072 int iError;
00073 int iSleep = 0;
00074 int iMaxSleep = 10;
00075
00076 struct hostent *pHE = NULL;
00077 struct hostent sHE;
00078 struct sockaddr_in sSockAddr;
00079 unsigned long lAddr;
00080
00081 if (iDebug)
00082 {
00083 printf("\n-D- begin %s: try connection to %s:%d",
00084 cModule, cNode, iPort);
00085 if (iMaxTime < 0)
00086 printf(" (1 trial)\n");
00087 else if (iMaxTime == 0)
00088 printf(" (until success)\n");
00089 else
00090 printf(" (for %d sec)\n", iMaxTime);
00091 }
00092
00093 if ( ( pHE = gethostbyname(cNode) ) == NULL )
00094 {
00095 lAddr = inet_addr(cNode);
00096 if ( ( pHE = gethostbyaddr(
00097 (char *)&lAddr, sizeof(lAddr), AF_INET ) ) == NULL )
00098 {
00099 printf("-E- %s: unknown host %s\n", cModule, cNode );
00100 iError = -1;
00101 goto gError;
00102 }
00103 if (iDebug)
00104 printf(" %s: gethostbyaddr succeeded\n", cModule);
00105 }
00106 else if (iDebug)
00107 printf(" %s: gethostbyname succeeded\n", cModule);
00108 sHE = *pHE;
00109
00110 gRetryConnect:;
00111
00112 if ( ( iSocket = socket(
00113 AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
00114 {
00115 printf("-E- %s: socket failed\n", cModule);
00116 if (errno)
00117 {
00118 printf(" %s\n", strerror(errno));
00119 errno = 0;
00120 }
00121
00122 iError = -1;
00123 goto gError;
00124 }
00125
00126 sSockAddr.sin_family = AF_INET;
00127 sSockAddr.sin_port = 0;
00128 sSockAddr.sin_addr.s_addr = INADDR_ANY;
00129
00130 if ( bind ( iSocket,
00131 (struct sockaddr *) &sSockAddr,
00132 sizeof(sSockAddr) ) == -1 )
00133 {
00134 printf("-E- %s: bind failed\n", cModule);
00135 if (errno)
00136 {
00137 printf(" %s\n", strerror(errno));
00138 errno = 0;
00139 }
00140
00141 iError = -1;
00142 goto gError;
00143 }
00144
00145 sSockAddr.sin_family = AF_INET;
00146 sSockAddr.sin_port = htons( (short) iPort );
00147 sSockAddr.sin_addr = * ( (struct in_addr *) sHE.h_addr );
00148
00149 if ( connect( iSocket,
00150 (struct sockaddr *) &sSockAddr,
00151 sizeof(sSockAddr) ) == -1 )
00152 {
00153 #ifdef WIN32
00154 iError = -1;
00155 #else
00156 printf("-E- %s: connect to %s:%d failed\n", cModule, cNode, iPort);
00157 if (errno)
00158 {
00159 printf(" %s\n", strerror(errno));
00160 errno = 0;
00161 }
00162 #endif
00163
00164
00165 if ( (iTime < iMaxTime) || (iMaxTime == -1) )
00166 {
00167 #ifdef WIN32
00168 closesocket(iSocket);
00169 iSocket = -1;
00170 #else
00171 close(iSocket);
00172 iSocket = -1;
00173 if (imySigS)
00174 goto gError;
00175 #endif
00176
00177 if (iMaxTime == -1)
00178 goto gError;
00179
00180 if (iSleep < iMaxSleep)
00181 iSleep++;
00182 iTime += iSleep;
00183
00184 if (iDebug) printf(
00185 " time %d of %d, sleep %d\n",
00186 iTime, iMaxTime, iSleep);
00187 #ifdef WIN32
00188 Sleep(iSleep*1000);
00189 #else
00190 sleep((unsigned) iSleep);
00191 #endif
00192 goto gRetryConnect;
00193 }
00194
00195 goto gError;
00196 }
00197
00198 *piMaxTime = iTime;
00199 *piSocket = iSocket;
00200 if (iDebug)
00201 printf("-D- end %s (success after %d sec)\n\n", cModule, *piMaxTime);
00202
00203 return(0);
00204
00205 gError:
00206 if (iSocket)
00207 {
00208 shutdown(iSocket, 2);
00209 #ifdef WIN32
00210 closesocket(iSocket);
00211 #else
00212 close(iSocket);
00213 #endif
00214 }
00215
00216 *piSocket = -1;
00217 if (iMaxTime >= 0)
00218 *piMaxTime = iTime;
00219 else
00220 *piMaxTime = 0;
00221
00222 if (iDebug)
00223 {
00224 printf("-D- end %s", cModule);
00225 if (iMaxTime >= 0)
00226 printf(" (after %d sec)\n\n", iTime);
00227 else
00228 printf("\n\n");
00229 }
00230
00231 return iError;
00232 }