1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* Modification History
*
* 2001-January-10 Jason Rohrer
* Created.
*
* 2001-January-15 Jason Rohrer
* Commented out a redundant print statement (it's better if these kinds
* of messages are handled at a higher level).
*
* 2001-January-28 Jason Rohrer
* Changed to comply with new initSocketFramework Socket interface.
*
* 2001-January-29 Jason Rohrer
* Fixed an endian bug with the port number.
*
* 2001-November-13 Jason Rohrer
* Added a missing include.
*
* 2001-December-12 Jason Rohrer
* Changed the include order to make BSD compatible.
*
* 2002-August-5 Jason Rohrer
* Removed use of obsolete call.
*
* 2002-October-13 Jason Rohrer
* Added support for timeout on connect.
*
* 2002-October-17 Jason Rohrer
* Added missing include.
* Added support for BSD's version of getsockopt.
*
* 2002-Novenber-27 Jason Rohrer
* Changed to print error number. There's a bug that needs to be fixed.
*
* 2002-December-2 Jason Rohrer
* Fixed resource leak when connection fails.
*
* 2003-March-18 Jason Rohrer
* Fixed a thread safety issue with gethostbyname.
*
* 2004-January-2 Jason Rohrer
* Fixed a memory leak when host name lookup fails.
*
* 2004-January-4 Jason Rohrer
* Added use of network function locks.
*
* 2006-April-25 Jason Rohrer
* Added missing check for a NULL outTimedOut parameter.
*
* 2008-September-30 Jason Rohrer
* Added support for non-blocking connect.
*
* 2009-April-3 Jason Rohrer
* OpenBSD support.
*
* 2009-June-15 Jason Rohrer
* Fixed bug when connect timeout not used.
*
* 2010-January-26 Jason Rohrer
* Fixed socklen_t on later versions of MacOSX.
*/
#include "minorGems/network/SocketClient.h"
#include "minorGems/network/NetworkFunctionLocks.h"
#include "minorGems/system/MutexLock.h"
#include <stdlib.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
// BSD does not define socklen_t
#ifdef BSD
#ifndef IPHONE
#ifndef __OpenBSD__
#ifndef _SOCKLEN_T // later versions of MacOS define it and mark it
typedef int socklen_t;
#endif
#endif
#endif
#endif
// prototypes
struct in_addr *nameToAddress( char *inAddress );
int timed_connect( int inSocketID,
struct sockaddr *inSocketAddress,
int inAddressLength,
int inTimeoutInMilliseconds );
Socket *SocketClient::connectToServer( HostAddress *inAddress,
long inTimeoutInMilliseconds,
char *outTimedOut ) {
if( !Socket::isFrameworkInitialized() ) {
// try to init the framework
int error = Socket::initSocketFramework();
if( error == -1 ) {
printf( "initializing network socket framework failed\n" );
return NULL;
}
}
int socketID = socket( AF_INET, SOCK_STREAM, 0 );
if( socketID == -1 ) {
printf( "Creating socket failed, error %d.\n", errno );
return NULL;
}
union sock {
struct sockaddr s;
struct sockaddr_in i;
} sock;
struct in_addr *internet_address =
nameToAddress( inAddress->mAddressString );
if( internet_address == NULL ) {
printf( "Host name lookup failed: " );
inAddress->print();
printf( "\n" );
close( socketID );
return NULL;
}
sock.i.sin_family = AF_INET;
sock.i.sin_port = htons( inAddress->mPort );
sock.i.sin_addr = *internet_address;
int error;
if( inTimeoutInMilliseconds != -1 ) {
// use timeout
error = timed_connect( socketID, &sock.s, sizeof( struct sockaddr ),
inTimeoutInMilliseconds );
if( outTimedOut != NULL ) {
if( error == -2 ) {
*outTimedOut = true;
if( inTimeoutInMilliseconds == 0 ) {
// caller didn't want to wait at all
error = 0;
}
else {
// treat timeout as error
error = -1;
}
}
else {
*outTimedOut = false;
}
}
}
else {
// don't use timeout
error = connect( socketID, &sock.s, sizeof( struct sockaddr ) );
}
delete internet_address;
if( error == -1 ) {
//printf( "Connecting to host failed: " );
//inAddress->print();
//printf( "\n" );
close( socketID );
return NULL;
}
// package into a Socket and return it
Socket *returnSocket = new Socket();
int *idSpace = new int[1];
idSpace[0] = socketID;
returnSocket->mNativeObjectPointer = (void *)idSpace;
if( outTimedOut != NULL &&
*outTimedOut ) {
// not connected yet
returnSocket->setConnected( false );
}
return returnSocket;
}
/* Converts ascii text to in_addr struct. NULL is returned if the
address can not be found.
Result must be destroyed by caller.
Adapted from the Unix Socket FAQ */
struct in_addr *nameToAddress( char *inAddress ) {
struct hostent *host;
static struct in_addr saddr;
struct in_addr *copiedSaddr = new struct in_addr;
/* First try it as aaa.bbb.ccc.ddd. */
// this is obsolete on linux
// saddr.s_addr = inet_addr( inAddress );
int error = inet_aton( inAddress, &saddr );
if( error != 0 ) {
// copy to avoid returning pointer to stack
memcpy( copiedSaddr, &saddr, sizeof( struct in_addr ) );
return copiedSaddr;
}
// must keep this locked until we are done copying the in_addr out
// of the returned hostent
NetworkFunctionLocks::mGetHostByNameLock.lock();
char hostFound = false;
host = gethostbyname( inAddress );
if( host != NULL ) {
memcpy( copiedSaddr,
*host->h_addr_list,
sizeof( struct in_addr ) );
hostFound = true;
}
NetworkFunctionLocks::mGetHostByNameLock.unlock();
if( hostFound ) {
return copiedSaddr;
}
else {
delete copiedSaddr;
}
return NULL;
}
/* timed_connect adapted from gnut, by Josh Pieper */
/* Josh Pieper, (c) 2000 */
/* This file is distributed under the GPL, see file COPYING for details */
// just like connect except that it times out after time secs
// returns -2 on timeout, otherwise same as connect
int timed_connect( int inSocketID,
struct sockaddr *inSocketAddress,
int inAddressLength,
int inTimeoutInMilliseconds ) {
int ret;
fd_set fsr;
struct timeval tv;
int val;
socklen_t len;
//g_debug(1,"entering sock=%i secs=%i\n",inSocketID,inTimeoutInSeconds);
ret = fcntl( inSocketID, F_SETFL, O_NONBLOCK );
//g_debug(5,"fcntl returned %i\n",ret);
if( ret < 0 ) {
return ret;
}
ret = connect( inSocketID, inSocketAddress, inAddressLength );
//g_debug(5,"connect returned %i\n",ret);
if( ret == 0 ) {
//g_debug(0,"immediate connection!\n");
// wooah! immediate connection
// return -2;
// changed from what Josh originally returned (-2)
// immediate connection *can* happen sometimes, right?
// for example, when connecting to localhost...
return 0;
}
// if (errno != EINPROGRESS) {
// perror("timed_connect, connect");
// return ret;
// }
FD_ZERO( &fsr );
FD_SET( inSocketID, &fsr );
tv.tv_sec = inTimeoutInMilliseconds / 1000;
int remainder = inTimeoutInMilliseconds % 1000;
tv.tv_usec = remainder * 1000;
ret = select( inSocketID+1, NULL, &fsr, NULL, &tv );
//g_debug(5,"select returned %i\n",ret);
if( ret==0 ) {
// timeout
//g_debug(1,"timeout\n");
fcntl( inSocketID, F_SETFL, 0 );
return -2;
}
len = 4;
ret = getsockopt( inSocketID, SOL_SOCKET, SO_ERROR, &val, &len );
//g_debug(5,"getsockopt returned %i val=%i\n",ret,val);
if( ret < 0 ) {
//g_debug(1,"getsockopt returned: %i\n",ret);
return ret;
}
if (val!=0) {
//g_debug(3,"returning failure!\n");
return -1;
}
ret = fcntl( inSocketID, F_SETFL, 0 );
//g_debug(1,"fcntl: %i\n",ret);
//g_debug(3,"returning success val=%i\n",val);
return 0;
}
|