From 9ba8668751cecf04bc3fb844ae8fd0da3d11cd44 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Mon, 17 Jan 2011 23:45:09 +0000 Subject: [PATCH] - win32 winsock bugfixes causing havok and grief for win32 users (corrupt winsock stack due to extra call to wsacleanup) --- .../glest_game/menu/menu_state_masterserver.cpp | 12 +++++++++--- .../shared_lib/include/platform/posix/socket.h | 2 +- .../sources/feathery_ftp/ftpTargetPosix.c | 4 ++-- .../sources/feathery_ftp/ftpTargetWin32.c | 16 ++++++++++++---- .../shared_lib/sources/platform/posix/socket.cpp | 5 ++++- source/shared_lib/sources/util/util.cpp | 3 +++ 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index e3575fd7..98e3fd8c 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -695,7 +695,9 @@ void MenuStateMasterserver::simpleTask(BaseThread *callingThread) { if(announcementURL != "") { safeMutex.ReleaseLock(true); - std::string announcementTxt = SystemFlags::getHTTP(announcementURL); + CURL *handle = SystemFlags::initHTTP(); + std::string announcementTxt = SystemFlags::getHTTP(announcementURL,handle); + SystemFlags::cleanupHTTP(&handle); if(callingThread->getQuitStatus() == true) { return; } @@ -728,7 +730,9 @@ void MenuStateMasterserver::simpleTask(BaseThread *callingThread) { //printf("\nversionURL=%s\n",versionURL.c_str()); if(versionURL != "") { safeMutex.ReleaseLock(true); - std::string versionTxt = SystemFlags::getHTTP(versionURL); + CURL *handle = SystemFlags::initHTTP(); + std::string versionTxt = SystemFlags::getHTTP(versionURL,handle); + SystemFlags::cleanupHTTP(&handle); if(callingThread->getQuitStatus() == true) { return; } @@ -769,7 +773,9 @@ void MenuStateMasterserver::simpleTask(BaseThread *callingThread) { if(Config::getInstance().getString("Masterserver","") != "") { safeMutex.ReleaseLock(true); - std::string localServerInfoString = SystemFlags::getHTTP(Config::getInstance().getString("Masterserver") + "showServersForGlest.php"); + CURL *handle = SystemFlags::initHTTP(); + std::string localServerInfoString = SystemFlags::getHTTP(Config::getInstance().getString("Masterserver") + "showServersForGlest.php",handle); + SystemFlags::cleanupHTTP(&handle); if(callingThread->getQuitStatus() == true) { return; } diff --git a/source/shared_lib/include/platform/posix/socket.h b/source/shared_lib/include/platform/posix/socket.h index 2eb4e8b1..3b6ad639 100644 --- a/source/shared_lib/include/platform/posix/socket.h +++ b/source/shared_lib/include/platform/posix/socket.h @@ -104,7 +104,7 @@ class Socket : public SimpleTaskCallbackInterface { protected: #ifdef WIN32 - SocketManager wsaManager; + static SocketManager wsaManager; #endif PLATFORM_SOCKET sock; time_t lastDebugEvent; diff --git a/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c b/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c index 7a714171..4192db12 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c +++ b/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c @@ -396,7 +396,7 @@ socket_t ftpCreateServerSocket(int portNumber) if(bind(theServer, (struct sockaddr *)&serverinfo, len)) { - if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d\n",theServer); + if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d\n",theServer); ftpUntrackSocket(theServer); ftpCloseSocket(&theServer); @@ -405,7 +405,7 @@ socket_t ftpCreateServerSocket(int portNumber) if(listen(theServer, 100)) { - if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n",theServer); + if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n",theServer); ftpUntrackSocket(theServer); ftpCloseSocket(&theServer); diff --git a/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c b/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c index 2c0cacf5..92537239 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c +++ b/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c @@ -39,6 +39,7 @@ ip_t ownIp; LOCAL fd_set watchedSockets; LOCAL fd_set signaledSockets; LOCAL int maxSockNr; +int wsaInitCount=0; void ftpArchInit() { @@ -46,12 +47,19 @@ void ftpArchInit() ownIp = 0; maxSockNr = 0; FD_ZERO(&watchedSockets); + if(VERBOSE_MODE_ENABLED) printf("Feathery calling WSAStartup...\n"); WSAStartup(MAKEWORD(2, 0),&wsaData); + wsaInitCount++; } void ftpArchCleanup(void) { - WSACleanup(); // WINSOCK freigeben + if(VERBOSE_MODE_ENABLED) printf("Feathery calling WSACleanup...[%d]\n",wsaInitCount); + if(wsaInitCount > 0) + { + WSACleanup(); // WINSOCK freigeben + wsaInitCount--; + } } int ftpGetLocalTime(ftpTime_S *t) @@ -319,7 +327,7 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses myAddr.sin_port = htons(passivePort); //myAddr.sin_port = htons(ftpGetPassivePort() + sessionId); - if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) + if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on))) { if(VERBOSE_MODE_ENABLED) printf("PASSIVE CONNECTION In ftpEstablishDataConnection setsockopt failed about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId); @@ -428,7 +436,7 @@ socket_t ftpCreateServerSocket(int portNumber) if(bind(theServer, (struct sockaddr *)&serverinfo, len)) { - if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d\n",theServer); + if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d\n",theServer); ftpUntrackSocket(theServer); ftpCloseSocket(&theServer); @@ -437,7 +445,7 @@ socket_t ftpCreateServerSocket(int portNumber) if(listen(theServer, 100)) { - if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n",theServer); + if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n",theServer); ftpUntrackSocket(theServer); ftpCloseSocket(&theServer); diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 845ff62a..bd677a94 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -214,17 +214,20 @@ bool UPNP_Tools::enabledUPNP = false; return acErrorBuffer; } - //SocketManager Socket::socketManager; + // keeps in scope for duration of the application + SocketManager Socket::wsaManager; SocketManager::SocketManager() { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 0); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("SocketManager calling WSAStartup...\n"); WSAStartup(wVersionRequested, &wsaData); //dont throw exceptions here, this is a static initializacion SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Winsock initialized.\n"); } SocketManager::~SocketManager() { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("SocketManager calling WSACleanup...\n"); WSACleanup(); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Winsock cleanup complete.\n"); } diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index df79714f..fc4e8b62 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -171,6 +171,9 @@ CURL *SystemFlags::initHTTP() { //printf("In [%s::%s Line %d] curl_global_init called and returned: result %d [%s]\n",__FILE__,__FUNCTION__,__LINE__,result,curl_easy_strerror(result)); } CURL *handle = curl_easy_init(); + if(handle == NULL) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] ERROR handle = NULL\n",__FILE__,__FUNCTION__,__LINE__); + } curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1); return handle; }