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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
/*
GameSpy GHTTP SDK
Dan "Mr. Pants" Schoenblum
dan@gamespy.com
Copyright 1999-2007 GameSpy Industries, Inc
devsupport@gamespy.com
*/
#include "ghttpCommon.h"
// Disable compiler warnings for issues that are unavoidable.
/////////////////////////////////////////////////////////////
#if defined(_MSC_VER) // DevStudio
// Level4, "conditional expression is constant".
// Occurs with use of the MS provided macro FD_SET
#pragma warning ( disable: 4127 )
#endif // _MSC_VER
#ifdef WIN32
// A lock.
//////////
typedef void * GLock;
// The lock used by ghttp.
//////////////////////////
static GLock ghiGlobalLock;
#endif
// Proxy server.
////////////////
char * ghiProxyAddress;
unsigned short ghiProxyPort;
// Throttle settings.
/////////////////////
int ghiThrottleBufferSize = 125;
gsi_time ghiThrottleTimeDelay = 250;
// Number of connections
/////////////////////
extern int ghiNumConnections;
#ifdef WIN32
// Creates a lock.
//////////////////
static GLock GNewLock(void)
{
CRITICAL_SECTION * criticalSection;
criticalSection = (CRITICAL_SECTION *)gsimalloc(sizeof(CRITICAL_SECTION));
if(!criticalSection)
return NULL;
InitializeCriticalSection(criticalSection);
return (GLock)criticalSection;
}
// Frees a lock.
////////////////
static void GFreeLock(GLock lock)
{
CRITICAL_SECTION * criticalSection = (CRITICAL_SECTION *)lock;
if(!lock)
return;
DeleteCriticalSection(criticalSection);
gsifree(criticalSection);
}
// Locks a lock.
////////////////
static void GLockLock(GLock lock)
{
CRITICAL_SECTION * criticalSection = (CRITICAL_SECTION *)lock;
if(!lock)
return;
EnterCriticalSection(criticalSection);
}
// Unlocks a lock.
//////////////////
static void GUnlockLock(GLock lock)
{
CRITICAL_SECTION * criticalSection = (CRITICAL_SECTION *)lock;
if(!lock)
return;
LeaveCriticalSection(criticalSection);
}
#endif
// Creates the ghttp lock.
//////////////////////////
void ghiCreateLock(void)
{
#ifdef WIN32
// We shouldn't already have a lock.
////////////////////////////////////
assert(!ghiGlobalLock);
// Create the lock.
///////////////////
ghiGlobalLock = GNewLock();
#endif
}
// Frees the ghttp lock.
////////////////////////
void ghiFreeLock(void)
{
#ifdef WIN32
if(!ghiGlobalLock)
return;
GFreeLock(ghiGlobalLock);
ghiGlobalLock = NULL;
#endif
}
// Locks the ghttp lock.
////////////////////////
void ghiLock
(
void
)
{
#ifdef WIN32
if(!ghiGlobalLock)
return;
GLockLock(ghiGlobalLock);
#endif
}
// Unlocks the ghttp lock.
//////////////////////////
void ghiUnlock
(
void
)
{
#ifdef WIN32
if(!ghiGlobalLock)
return;
GUnlockLock(ghiGlobalLock);
#endif
}
// Logs traffic.
////////////////
#ifdef HTTP_LOG
void ghiLogToFile(const char * buffer, int len, const char* fileName)
{
#ifdef _NITRO
int i;
if(!buffer || !len)
return;
for(i = 0 ; i < len ; i++)
OS_PutChar(buffer[i]);
#else
FILE * file;
if(!buffer || !len)
return;
file = fopen(fileName, "ab");
if(file)
{
fwrite(buffer, 1, len, file);
fclose(file);
}
#endif
}
#endif
// Reads encrypted data from decodeBuffer
// Appends decrypted data to recvBuffer
// Returns GHTTPFalse if there was a fatal error
////////////////////////////////////////////////
GHTTPBool ghiDecryptReceivedData(struct GHIConnection * connection)
{
// Decrypt data from decodeBuffer to recvBuffer
GHIEncryptionResult aResult = GHIEncryptionResult_None;
// data to be decrypted
char* aReadPos = NULL;
char* aWritePos = NULL;
int aReadLen = 0;
int aWriteLen = 0;
do
{
// Call the decryption func
do
{
aReadPos = connection->decodeBuffer.data + connection->decodeBuffer.pos;
aReadLen = connection->decodeBuffer.len - connection->decodeBuffer.pos;
aWritePos = connection->recvBuffer.data + connection->recvBuffer.len;
aWriteLen = connection->recvBuffer.size - connection->recvBuffer.len; // the amount of room in recvbuffer
aResult = (connection->encryptor.mDecryptFunc)(connection, &connection->encryptor,
aReadPos, &aReadLen, aWritePos, &aWriteLen);
if (aResult == GHIEncryptionResult_BufferTooSmall)
{
// Make some more room
if (GHTTPFalse == ghiResizeBuffer(&connection->recvBuffer, connection->recvBuffer.sizeIncrement))
return GHTTPFalse; // error
}
else if(aResult == GHIEncryptionResult_Error)
{
return GHTTPFalse;
}
} while (aResult == GHIEncryptionResult_BufferTooSmall && aWriteLen == 0);
// Adjust GHIBuffer sizes so they account for transfered data
if(aReadLen > connection->decodeBuffer.len)
{
gsDebugFormat(GSIDebugCat_HTTP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"ghiDecryptReceivedData read past the end of connection->decodeBuffer! (%d\\%d bytes)\r\n",
aReadLen, connection->decodeBuffer.len);
return GHTTPFalse;
}
connection->decodeBuffer.pos += aReadLen;
connection->recvBuffer.len += aWriteLen;
} while(aWriteLen > 0);
// Discard data from the decodedBuffer in chunks
if (connection->decodeBuffer.pos > 0xFF)
{
int bytesToKeep = connection->decodeBuffer.len - connection->decodeBuffer.pos;
if (bytesToKeep == 0)
ghiResetBuffer(&connection->decodeBuffer);
else
{
memmove(connection->decodeBuffer.data,
connection->decodeBuffer.data + connection->decodeBuffer.pos,
(size_t)bytesToKeep);
connection->decodeBuffer.pos = 0;
connection->decodeBuffer.len = bytesToKeep;
}
}
return GHTTPTrue;
}
// Receive some data.
/////////////////////
GHIRecvResult ghiDoReceive
(
GHIConnection * connection,
char buffer[],
int * bufferLen
)
{
int rcode;
int socketError;
int len;
// How much to try and receive.
///////////////////////////////
len = (*bufferLen - 1);
// Are we throttled?
////////////////////
if(connection->throttle)
{
unsigned long now;
// Don't receive too often.
///////////////////////////
now = current_time();
if(now < (connection->lastThrottleRecv + ghiThrottleTimeDelay))
return GHINoData;
// Update the receive time.
///////////////////////////
connection->lastThrottleRecv = (unsigned int)now;
// Don't receive too much.
//////////////////////////
len = min(len, ghiThrottleBufferSize);
}
// Receive some data.
/////////////////////
if (connection->encryptor.mEngine != GHTTPEncryptionEngine_None &&
connection->encryptor.mSessionEstablished == GHTTPTrue &&
connection->encryptor.mEncryptOnSend == GHTTPTrue )
{
GHIEncryptionResult result;
int recvLength = len;
result = ghiEncryptorSslDecryptRecv(connection, &connection->encryptor, buffer, &recvLength);
if (result == GHIEncryptionResult_Success)
rcode = recvLength;
else
rcode = -1; // signal termination of connection
}
else
{
rcode = recv(connection->socket, buffer, len, 0);
}
// There was an error.
//////////////////////
if(gsiSocketIsError(rcode))
{
// Get the error code.
//////////////////////
socketError = GOAGetLastError(connection->socket);
// Check for a closed connection.
/////////////////////////////////
if(socketError == WSAENOTCONN)
{
connection->connectionClosed = GHTTPTrue;
return GHIConnClosed;
}
// Check for nothing waiting.
/////////////////////////////
if((socketError == WSAEWOULDBLOCK) || (socketError == WSAEINPROGRESS) || (socketError == WSAETIMEDOUT))
return GHINoData;
// There was a real error.
//////////////////////////
connection->completed = GHTTPTrue;
connection->result = GHTTPSocketFailed;
connection->socketError = socketError;
connection->connectionClosed = GHTTPTrue;
return GHIError;
}
// The connection was closed.
/////////////////////////////
if(rcode == 0)
{
connection->connectionClosed = GHTTPTrue;
return GHIConnClosed;
}
// Cap the buffer.
//////////////////
buffer[rcode] = '\0';
*bufferLen = rcode;
gsDebugFormat(GSIDebugCat_HTTP, GSIDebugType_Network, GSIDebugLevel_RawDump, "Received %d bytes\n", rcode);
// Notify app.
//////////////
return GHIRecvData;
}
int ghiDoSend
(
struct GHIConnection * connection,
const char * buffer,
int len
)
{
int rcode;
if (buffer == NULL || len == 0)
return 0;
// Do the send.
///////////////
if (connection->encryptor.mEngine != GHTTPEncryptionEngine_None &&
connection->encryptor.mSessionEstablished == GHTTPTrue &&
connection->encryptor.mEncryptOnSend == GHTTPTrue)
{
int bytesSent = 0;
GHIEncryptionResult result;
// send through encryption engine
result = ghiEncryptorSslEncryptSend(connection, &connection->encryptor, buffer, len, &bytesSent);
// Check for an error.
//////////////////////
if(result != GHIEncryptionResult_Success)
rcode = -1; // signal termination of connection
else
rcode = bytesSent;
}
else
{
// send directly to socket
rcode = send(connection->socket, buffer, len, 0);
}
// Check for an error.
//////////////////////
if(gsiSocketIsError(rcode))
{
int error;
// Would block just means 0 bytes sent.
///////////////////////////////////////
error = GOAGetLastError(connection->socket);
if((error == WSAEWOULDBLOCK) || (error == WSAEINPROGRESS) || (error == WSAETIMEDOUT))
return 0;
connection->completed = GHTTPTrue;
connection->result = GHTTPSocketFailed;
connection->socketError = error;
return -1;
}
//do not add CRLF as part of bytes posted - make sure waitPostContinue is false
if(connection->state == GHTTPPosting && connection->postingState.waitPostContinue == GHTTPFalse)
{
connection->postingState.bytesPosted += rcode;
ghiLogRequest(buffer, rcode);
}
return rcode;
}
GHITrySendResult ghiTrySendThenBuffer
(
GHIConnection * connection,
const char * buffer,
int len
)
{
int rcode = 0;
// **SSL pattern 1: buffer everything into an SSL record**
if (connection->encryptor.mEngine != GHTTPEncryptionEngine_None &&
connection->encryptor.mSessionEstablished == GHTTPTrue &&
connection->encryptor.mEncryptOnBuffer == GHTTPTrue)
{
if (!ghiEncryptDataToBuffer(&connection->sendBuffer, buffer + rcode, len - rcode))
return GHITrySendError;
// Try to send immediately
if (ghiSendBufferedData(connection) == GHTTPFalse)
return GHITrySendError;
if (connection->sendBuffer.pos >= connection->sendBuffer.len)
{
ghiResetBuffer(&connection->sendBuffer);
return GHITrySendSent; // everything sent
}
return GHITrySendBuffered;
}
// **Plain text or SSL encrypt on send**
// If we already have something buffered, don't send.
/////////////////////////////////////////////////////
if(connection->sendBuffer.pos >= connection->sendBuffer.len)
{
// Try and send.
////////////////
rcode = ghiDoSend(connection, buffer, len);
if(gsiSocketIsError(rcode))
return GHITrySendError;
// Was it all sent?
///////////////////
if(rcode == len)
return GHITrySendSent;
}
// Buffer whatever wasn't sent.
///////////////////////////////
if(!ghiAppendDataToBuffer(&connection->sendBuffer, buffer + rcode, len - rcode))
return GHITrySendError;
return GHITrySendBuffered;
}
static GHTTPBool ghiParseProxyServer
(
const char * server,
char ** proxyAddress, // [out] the proxy address
unsigned short * proxyPort // [out] the proxy port
)
{
char * strPort;
// Make sure each pointer is valid as well as what it points to
assert(server && *server);
assert(proxyAddress && !*proxyAddress);
assert(proxyPort);
// Copy off the server address.
///////////////////////////////
*proxyAddress = goastrdup(server);
if(!*proxyAddress)
return GHTTPFalse;
// Check for a port.
////////////////////
if((strPort = strchr(*proxyAddress, ':')) != NULL)
{
*strPort++ = '\0';
// Try getting the port.
////////////////////////
*proxyPort = (unsigned short)atoi(strPort);
if(!*proxyPort)
{
gsifree(*proxyAddress);
*proxyAddress = NULL;
return GHTTPFalse;
}
}
else
{
*proxyPort = GHI_DEFAULT_PORT;
}
return GHTTPTrue;
}
GHTTPBool ghiSetProxy
(
const char * server
)
{
// Free any existing proxy address.
///////////////////////////////////
if(ghiProxyAddress)
{
gsifree(ghiProxyAddress);
ghiProxyAddress = NULL;
}
ghiProxyPort = 0;
// If a server was supplied, try to parse it
if(server && *server)
return ghiParseProxyServer(server, &ghiProxyAddress, &ghiProxyPort);
// No server supplied results in proxy being cleared
return GHTTPTrue;
}
GHTTPBool ghiSetRequestProxy
(
GHTTPRequest request,
const char * server
)
{
// Obtain the connection for this request
GHIConnection* connection = ghiRequestToConnection(request);
if (connection == NULL)
return GHTTPFalse;
// Free any existing proxy address.
///////////////////////////////////
if(connection->proxyOverrideServer)
{
gsifree(connection->proxyOverrideServer);
connection->proxyOverrideServer = NULL;
connection->proxyOverridePort = GHI_DEFAULT_PORT;
}
// If a server was supplied, try to parse it
if(server && *server)
return ghiParseProxyServer(server, &connection->proxyOverrideServer, &connection->proxyOverridePort);
// No server supplied results in proxy being cleared
return GHTTPTrue;
}
void ghiThrottleSettings
(
int bufferSize,
gsi_time timeDelay
)
{
ghiThrottleBufferSize = bufferSize;
ghiThrottleTimeDelay = timeDelay;
}
// Re-enable previously disabled compiler warnings
///////////////////////////////////////////////////
#if defined(_MSC_VER)
#pragma warning ( default: 4127 )
#endif // _MSC_VER
|