CloseSocketProper instead of closesocket

This commit is contained in:
Lion Kortlepel 2020-11-12 23:20:37 +01:00
parent fedca58e8e
commit fa9bc16038
6 changed files with 27 additions and 22 deletions

View File

@ -16,9 +16,14 @@ inline void ZeroMemory(void* dst, size_t len) {
Assert(std::memset(dst, 0, len) != nullptr); Assert(std::memset(dst, 0, len) != nullptr);
} }
// provides unix equivalent of closesocket call in win32 // provides unix equivalent of closesocket call in win32
inline void closesocket(int socket) { inline void CloseSocketProper(int socket) {
#ifndef WIN32
shutdown(socket, SHUT_RDWR); shutdown(socket, SHUT_RDWR);
close(socket); close(socket);
#else // WIN32
shutdown(socket, SD_BOTH);
closesocket(socket);
#endif // WIN32
} }
#ifndef __try #ifndef __try

View File

@ -348,7 +348,7 @@ int lua_dropPlayer(lua_State* L) {
Respond(c, "C:Server:You have been Kicked from the server! " + Reason, true); Respond(c, "C:Server:You have been Kicked from the server! " + Reason, true);
c->SetStatus(-2); c->SetStatus(-2);
info(Sec("Closing socket due to kick")); info(Sec("Closing socket due to kick"));
closesocket(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
} else } else
SendError(L, Sec("DropPlayer not enough arguments")); SendError(L, Sec("DropPlayer not enough arguments"));

View File

@ -86,7 +86,7 @@ void Check(SOCKET TCPSock, std::reference_wrapper<std::atomic_bool> ok) {
accum += 100; accum += 100;
if (accum >= 5000) { if (accum >= 5000) {
error(Sec("Identification timed out (Check accum)")); error(Sec("Identification timed out (Check accum)"));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
} }
@ -142,14 +142,14 @@ void Identification(SOCKET TCPSock, RSA* Skey) {
std::string Name, DID, Role; std::string Name, DID, Role;
if (!Send(TCPSock, GenerateM(Skey))) { if (!Send(TCPSock, GenerateM(Skey))) {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
std::string msg = Rcv(TCPSock); std::string msg = Rcv(TCPSock);
auto Keys = Parse(msg); auto Keys = Parse(msg);
if (!Send(TCPSock, RSA_E("HC", Keys.second, Keys.first))) { if (!Send(TCPSock, RSA_E("HC", Keys.second, Keys.first))) {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
@ -161,23 +161,23 @@ void Identification(SOCKET TCPSock, RSA* Skey) {
Ver = Ver.substr(2); Ver = Ver.substr(2);
if (Ver.length() > 4 || Ver != GetCVer()) { if (Ver.length() > 4 || Ver != GetCVer()) {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
} else { } else {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
Res = RSA_D(Res, Skey); Res = RSA_D(Res, Skey);
if (Res.size() < 3 || Res.substr(0, 2) != Sec("NR")) { if (Res.size() < 3 || Res.substr(0, 2) != Sec("NR")) {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
if (Res.find(':') == std::string::npos) { if (Res.find(':') == std::string::npos) {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
Name = Res.substr(2, Res.find(':') - 2); Name = Res.substr(2, Res.find(':') - 2);
@ -185,7 +185,7 @@ void Identification(SOCKET TCPSock, RSA* Skey) {
Role = GetRole(DID); Role = GetRole(DID);
if (Role.empty() || Role.find(Sec("Error")) != std::string::npos) { if (Role.empty() || Role.find(Sec("Error")) != std::string::npos) {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
// DebugPrintTIDInternal(std::string("Client(") + Name + ")"); // DebugPrintTIDInternal(std::string("Client(") + Name + ")");
@ -194,7 +194,7 @@ void Identification(SOCKET TCPSock, RSA* Skey) {
if (c != nullptr) { if (c != nullptr) {
if (c->GetDID() == DID) { if (c->GetDID() == DID) {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
c->SetStatus(-2); c->SetStatus(-2);
break; break;
} }
@ -205,7 +205,7 @@ void Identification(SOCKET TCPSock, RSA* Skey) {
CreateClient(TCPSock, Name, DID, Role); CreateClient(TCPSock, Name, DID, Role);
} else { } else {
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
} }
} }
void Identify(SOCKET TCPSock) { void Identify(SOCKET TCPSock) {
@ -222,7 +222,7 @@ void Identify(SOCKET TCPSock) {
}__except(1){ }__except(1){
if(TCPSock != -1){ if(TCPSock != -1){
error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(TCPSock); CloseSocketProper(TCPSock);
} }
} }
#endif // WIN32*/ #endif // WIN32*/
@ -271,7 +271,7 @@ void TCPServerMain() {
} }
} while (client); } while (client);
closesocket(client); CloseSocketProper(client);
WSACleanup(); WSACleanup();
#else // unix #else // unix
// wondering why we need slightly different implementations of this? // wondering why we need slightly different implementations of this?
@ -313,6 +313,6 @@ void TCPServerMain() {
} while (client); } while (client);
debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__)); debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
closesocket(client); CloseSocketProper(client);
#endif // WIN32 #endif // WIN32
} }

View File

@ -33,7 +33,7 @@ void STCPSend(Client* c, std::string Data) {
if (c->GetStatus() > -1) if (c->GetStatus() > -1)
c->SetStatus(-1); c->SetStatus(-1);
info(Sec("Closing socket, BytesSent < 0")); info(Sec("Closing socket, BytesSent < 0"));
closesocket(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
} }
} }
void SendFile(Client* c, const std::string& Name) { void SendFile(Client* c, const std::string& Name) {
@ -108,13 +108,13 @@ bool STCPRecv(Client* c) {
if (c->GetStatus() > -1) if (c->GetStatus() > -1)
c->SetStatus(-1); c->SetStatus(-1);
info(Sec("Closing socket in STCP receive, BytesRcv == 0")); info(Sec("Closing socket in STCP receive, BytesRcv == 0"));
closesocket(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
return false; return false;
} else if (BytesRcv < 0) { } else if (BytesRcv < 0) {
if (c->GetStatus() > -1) if (c->GetStatus() > -1)
c->SetStatus(-1); c->SetStatus(-1);
info(Sec("Closing socket in STCP receive, BytesRcv < 0")); info(Sec("Closing socket in STCP receive, BytesRcv < 0"));
closesocket(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
return false; return false;
} }
if (strcmp(buf, "Done") == 0) if (strcmp(buf, "Done") == 0)

View File

@ -32,7 +32,7 @@ void TCPSend(Client* c, const std::string& Data) {
if (c->GetStatus() > -1) if (c->GetStatus() > -1)
c->SetStatus(-1); c->SetStatus(-1);
// info(Sec("Closing socket, Temp < 0")); // info(Sec("Closing socket, Temp < 0"));
closesocket(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
return; return;
} }
Sent += Temp; Sent += Temp;
@ -55,7 +55,7 @@ bool CheckBytes(Client* c, int32_t BytesRcv) {
if (c->GetStatus() > -1) if (c->GetStatus() > -1)
c->SetStatus(-1); c->SetStatus(-1);
info(Sec("Closing socket in CheckBytes, BytesRcv < 0")); info(Sec("Closing socket in CheckBytes, BytesRcv < 0"));
closesocket(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
return false; return false;
} }
return true; return true;

View File

@ -323,7 +323,7 @@ void LOOP() {
error(Sec("fatal: ") + std::string(e.what())); error(Sec("fatal: ") + std::string(e.what()));
} }
} }
/*closesocket(UDPSock); /*CloseSocketProper(UDPSock);
WSACleanup(); WSACleanup();
return;*/ return;*/
#else // unix #else // unix
@ -369,7 +369,7 @@ void LOOP() {
error(Sec("fatal: ") + std::string(e.what())); error(Sec("fatal: ") + std::string(e.what()));
} }
} }
/*closesocket(UDPSock); // TODO: Why not this? We did this in TCPServerMain? /*CloseSocketProper(UDPSock); // TODO: Why not this? We did this in TCPServerMain?
return; return;
*/ */
#endif // WIN32 #endif // WIN32