Skip to content

Commit

Permalink
Network: Resolve asumption that is_open checks for actual connection
Browse files Browse the repository at this point in the history
  • Loading branch information
killerwife committed May 31, 2024
1 parent 77ce758 commit 2709cf1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
24 changes: 22 additions & 2 deletions src/game/Server/WorldSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ bool WorldSocket::ProcessIncomingData()
auto self(shared_from_this());
Read((char*)header.get(), sizeof(ClientPktHeader), [self, header](const boost::system::error_code& error, std::size_t read) -> void
{
if (error) return;
if (error)
{
self->Close();
return;
}

// thread safe due to always being called from service context
self->m_crypt.DecryptRecv((uint8*)header.get(), sizeof(ClientPktHeader));

Expand All @@ -183,7 +188,12 @@ bool WorldSocket::ProcessIncomingData()

self->Read(reinterpret_cast<char*>(packetBuffer->data()), packetBuffer->size(), [self, packetBuffer, opcode = opcode](const boost::system::error_code& error, std::size_t read) -> void
{
if (error) return;
if (error)
{
self->Close();
return;
}

std::unique_ptr<WorldPacket> pct = std::make_unique<WorldPacket>(opcode, packetBuffer->size());
pct->append(*packetBuffer.get());
if (sPacketLog->CanLogPacket() && self->IsLoggingPackets())
Expand All @@ -194,6 +204,7 @@ bool WorldSocket::ProcessIncomingData()
if (WorldSocket::m_packetCooldowns.size() <= size_t(opcode))
{
sLog.outError("WorldSocket::ProcessIncomingData: Received opcode beyond range of opcodes: %u", opcode);
self->Close();
return;
}

Expand All @@ -217,15 +228,22 @@ bool WorldSocket::ProcessIncomingData()
if (self->m_session)
{
sLog.outError("WorldSocket::ProcessIncomingData: Player send CMSG_AUTH_SESSION again");
self->Close();
return;
}

if (!self->HandleAuthSession(*pct))
{
self->Close();
return;
}
break;
case CMSG_PING:
if (!self->HandlePing(*pct))
{
self->Close();
return;
}
break;
case CMSG_KEEP_ALIVE:
DEBUG_LOG("CMSG_KEEP_ALIVE, size: " SIZEFMTD " ", pct->size());
Expand All @@ -242,6 +260,7 @@ bool WorldSocket::ProcessIncomingData()
if (!self->m_session)
{
sLog.outError("WorldSocket::ProcessIncomingData: Client not authed opcode = %u", uint32(opcode));
self->Close();
return;
}

Expand All @@ -265,6 +284,7 @@ bool WorldSocket::ProcessIncomingData()
{
DETAIL_LOG("Disconnecting session [account id %i / address %s] for badly formatted packet.",
self->m_session ? self->m_session->GetAccountId() : -1, self->GetRemoteAddress().c_str());
self->Close();
return;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/mangosd/RASocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ bool RASocket::ProcessIncomingData()
auto self = shared_from_this();
ReadUntil(*buffer.get(), '\n', [self, buffer](const boost::system::error_code& error, std::size_t read)
{
if (error)
{
self->Close();
return;
}

static const std::string NEWLINE = "\n\r";

auto pos = 0;
Expand Down
51 changes: 41 additions & 10 deletions src/realmd/AuthSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ bool AuthSocket::ProcessIncomingData()
std::shared_ptr<eAuthCmd> cmd = std::make_shared<eAuthCmd>();
Read((char*)cmd.get(), sizeof(eAuthCmd), [self = shared_from_this(), cmd, tableLength](const boost::system::error_code& error, std::size_t read)
{
if (error) return;
if (error)
{
self->Close();
return;
}

int i;
// Circle through known commands and call the correct command handler
Expand Down Expand Up @@ -319,7 +323,11 @@ bool AuthSocket::_HandleLogonChallenge()

Read((char*)header.get(), sizeof(sAuthLogonChallengeHeader), [self = shared_from_this(), header](const boost::system::error_code& error, std::size_t read)
{
if (error) return;
if (error)
{
self->Close();
return;
}

uint16* pUint16 = reinterpret_cast<uint16*>(header.get());
EndianConvert(*pUint16);
Expand All @@ -337,7 +345,11 @@ bool AuthSocket::_HandleLogonChallenge()
///- Read the remaining of the packet
self->Read((char*)body.get(), remaining, [self, header, body](const boost::system::error_code& error, std::size_t read)
{
if (error) return;
if (error)
{
self->Close();
return;
}

if (body->userName_len > AUTH_LOGON_MAX_NAME)
return;
Expand Down Expand Up @@ -529,7 +541,11 @@ bool AuthSocket::_HandleLogonProof()
std::shared_ptr<sAuthLogonProof_C> lp = std::make_shared<sAuthLogonProof_C>();
Read((char*)lp.get(), sizeof(sAuthLogonProof_C), [self = shared_from_this(), lp](const boost::system::error_code& error, std::size_t read)
{
if (error) return;
if (error)
{
self->Close();
return;
}

///- Session is closed unless overriden
self->_status = STATUS_CLOSED;
Expand All @@ -544,7 +560,7 @@ bool AuthSocket::_HandleLogonProof()
*pkt << uint8(AUTH_LOGON_FAILED_VERSION_INVALID);

BASIC_LOG("[AuthChallenge] Account %s tried to login with invalid client version %u!", self->_login.c_str(), self->_build);
self->Write((const char*)pkt->contents(), pkt->size(), [self, pkt](const boost::system::error_code& error, std::size_t read) {});
self->Write((const char*)pkt->contents(), pkt->size(), [self, pkt](const boost::system::error_code& error, std::size_t read) { self->Close();});
return;
}
/// </ul>
Expand All @@ -569,7 +585,7 @@ bool AuthSocket::_HandleLogonProof()
{
if (error)
{
self->Write(logonProofUnknownAccountPinInvalid, sizeof(logonProofUnknownAccountPinInvalid), [self](const boost::system::error_code& error, std::size_t read) {});
self->Write(logonProofUnknownAccountPinInvalid, sizeof(logonProofUnknownAccountPinInvalid), [self](const boost::system::error_code& error, std::size_t read) { self->Close();});
return;
}

Expand All @@ -578,7 +594,7 @@ bool AuthSocket::_HandleLogonProof()
{
if (error)
{
self->Write(logonProofUnknownAccountPinInvalid, sizeof(logonProofUnknownAccountPinInvalid), [self](const boost::system::error_code& error, std::size_t read) {});
self->Write(logonProofUnknownAccountPinInvalid, sizeof(logonProofUnknownAccountPinInvalid), [self](const boost::system::error_code& error, std::size_t read) { self->Close();});
return;
}

Expand Down Expand Up @@ -668,7 +684,11 @@ bool AuthSocket::_HandleReconnectChallenge()

Read((char*)header.get(), sizeof(sAuthLogonChallengeHeader), [self = shared_from_this(), header](const boost::system::error_code& error, std::size_t read)
{
if (error) return;
if (error)
{
self->Close();
return;
}

uint16* pUint16 = reinterpret_cast<uint16*>(header.get());
EndianConvert(*pUint16);
Expand All @@ -685,7 +705,11 @@ bool AuthSocket::_HandleReconnectChallenge()
// Read the remaining of the packet
self->Read((char*)body.get(), remaining, [self, header, body](const boost::system::error_code& error, std::size_t read) -> void
{
if (error) return;
if (error)
{
self->Close();
return;
}

if (body->userName_len > 10)
return;
Expand Down Expand Up @@ -742,7 +766,11 @@ bool AuthSocket::_HandleReconnectProof()
std::shared_ptr<sAuthReconnectProof_C> lp = std::make_shared<sAuthReconnectProof_C>();
Read((char*)lp.get(), sizeof(sAuthReconnectProof_C), [self = shared_from_this(), lp](const boost::system::error_code& error, std::size_t read)
{
if (error) return;
if (error)
{
self->Close();
return;
}

///- Session is closed unless overriden
self->_status = STATUS_CLOSED;
Expand Down Expand Up @@ -795,7 +823,10 @@ bool AuthSocket::_HandleRealmList()
ReadSkip(4, [self = shared_from_this()](const boost::system::error_code& error, std::size_t read) -> void
{
if (error)
{
self->Close();
return;
}

// Get the user id (else close the connection)
// No SQL injection (escaped user name)
Expand Down

0 comments on commit 2709cf1

Please sign in to comment.