Add missing check

This fixes the bug where RCON address couldn't be set from the '[::]:port' format.
This commit is contained in:
Kawe Mazidjatari 2023-04-19 01:52:05 +02:00
parent e51336dd5a
commit 39445eecc4
2 changed files with 9 additions and 3 deletions

View File

@ -14,7 +14,7 @@ public:
virtual bool Encode(google::protobuf::MessageLite* pMsg, char* pMsgBuf, size_t nMsgLen) const;
virtual bool Decode(google::protobuf::MessageLite* pMsg, const char* pMsgBuf, size_t nMsgLen) const;
virtual bool Connect(const char* pHostAdr, const int nHostPort = -1);
virtual bool Connect(const char* pHostAdr, const int nHostPort = SOCKET_ERROR);
virtual void Disconnect(const char* szReason = nullptr) { NOTE_UNUSED(szReason); };
virtual bool Send(const SocketHandle_t hSocket, const char* pMsgBuf, int nMsgLen) const;

View File

@ -48,7 +48,9 @@ bool CL_NetConSerialize(const CNetConBase* pBase, vector<char>& vecBuf, const ch
bool CL_NetConConnect(CNetConBase* pBase, const char* pHostAdr, const int nHostPort)
{
string svLocalHost;
if (nHostPort != -1 && strcmp(pHostAdr, "localhost") == 0)
const bool bValidSocket = nHostPort != SOCKET_ERROR;
if (bValidSocket && strcmp(pHostAdr, "localhost") == 0)
{
char szHostName[512];
if (!gethostname(szHostName, sizeof(szHostName)))
@ -65,7 +67,11 @@ bool CL_NetConConnect(CNetConBase* pBase, const char* pHostAdr, const int nHostP
return false;
}
pNetAdr->SetPort(htons(u_short(nHostPort)));
// Pass 'SOCKET_ERROR' if you want to set port from address string instead.
if (bValidSocket)
{
pNetAdr->SetPort(htons(u_short(nHostPort)));
}
CSocketCreator* pCreator = pBase->GetSocketCreator();
if (pCreator->ConnectSocket(*pNetAdr, true) == SOCKET_ERROR)