Commit 9ba80d98 authored by Romain Bignon's avatar Romain Bignon

correctly close socket when connect() fails

parent 27e476e2
......@@ -294,6 +294,11 @@ int LSPmsCommand::Exec(PlayerList players, EC_Client *me, ParvList parv)
if(connect(sock, (struct sockaddr *) &fsocket, sizeof fsocket) < 0)
{
Debug(W_DEBUG, "Unable to connect to %s (%d: %s)", parv[2].c_str(), errno, strerror(errno));
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
return 0;
}
......
......@@ -283,14 +283,6 @@ bool EC_Client::Connect(const char *hostname, unsigned int port)
{
if(connected || sock) return false;
/* Création du socket
* Note: pour l'initialisation, comme = {0} n'est pas compatible partout, on va attribuer la
* valeur d'une variable statique qui s'initialise elle toute seule.
*/
static struct sockaddr_in fsocket_init;
struct sockaddr_in fsocket = fsocket_init;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
const char* ip = hostname;
error = false;
......@@ -311,6 +303,14 @@ bool EC_Client::Connect(const char *hostname, unsigned int port)
ip = inet_ntoa(*(struct in_addr *)(*(hp->h_addr_list)));
}
/* Création du socket
* Note: pour l'initialisation, comme = {0} n'est pas compatible partout, on va attribuer la
* valeur d'une variable statique qui s'initialise elle toute seule.
*/
static struct sockaddr_in fsocket_init;
struct sockaddr_in fsocket = fsocket_init;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
// Set the timeout
struct timeval timeout;
memset(&timeout, 0, sizeof(timeout));
......@@ -337,9 +337,12 @@ bool EC_Client::Connect(const char *hostname, unsigned int port)
{
#ifdef WIN32
SetCantConnect(std::string("API WinSock #" + TypToStr(WSAGetLastError())));
closesocket(sock);
#else
SetCantConnect(strerror(errno));
close(sock);
#endif
sock = 0;
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment