Codechange: unify behaviour of handling too short packets

This commit is contained in:
Rubidium
2026-01-16 21:37:30 +01:00
committed by rubidium42
parent 0b3c448ea1
commit 02438e9934
7 changed files with 12 additions and 34 deletions
+2 -2
View File
@@ -285,7 +285,8 @@ bool Packet::PrepareToRead()
bool valid = cs->receive_encryption_handler->Decrypt(std::span(&this->buffer[pos], mac_size), std::span(&this->buffer[pos + mac_size], this->buffer.size() - pos - mac_size));
this->pos += static_cast<PacketSize>(mac_size);
return valid;
/* Is the decryption valid *and* is the remaining data big enough to contain the packet type? */
return valid && this->CanReadFromPacket(EncodedLengthOfPacketType());
}
/**
@@ -294,7 +295,6 @@ bool Packet::PrepareToRead()
*/
PacketType Packet::GetPacketType() const
{
assert(this->Size() >= EncodedLengthOfPacketSize() + EncodedLengthOfPacketType());
size_t offset = EncodedLengthOfPacketSize();
if (cs != nullptr && cs->send_encryption_handler != nullptr) offset += cs->send_encryption_handler->MACSize();
return static_cast<PacketType>(buffer[offset]);
+1 -7
View File
@@ -34,13 +34,7 @@ NetworkRecvStatus NetworkAdminSocketHandler::CloseConnection(bool)
*/
NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet &p)
{
PacketAdminType type = (PacketAdminType)p.Recv_uint8();
if (this->HasClientQuit()) {
Debug(net, 0, "[tcp/admin] Received invalid packet from '{}' ({})", this->admin_name, this->admin_version);
this->CloseConnection();
return NETWORK_RECV_STATUS_MALFORMED_PACKET;
}
PacketAdminType type = static_cast<PacketAdminType>(p.Recv_uint8());
switch (type) {
case ADMIN_PACKET_ADMIN_JOIN: return this->Receive_ADMIN_JOIN(p);
+3 -7
View File
@@ -103,9 +103,9 @@ std::optional<std::string> ContentInfo::GetTextfile(TextfileType type) const
*/
bool NetworkContentSocketHandler::HandlePacket(Packet &p)
{
PacketContentType type = (PacketContentType)p.Recv_uint8();
PacketContentType type = static_cast<PacketContentType>(p.Recv_uint8());
switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
switch (type) {
case PACKET_CONTENT_CLIENT_INFO_LIST: return this->Receive_CLIENT_INFO_LIST(p);
case PACKET_CONTENT_CLIENT_INFO_ID: return this->Receive_CLIENT_INFO_ID(p);
case PACKET_CONTENT_CLIENT_INFO_EXTID: return this->Receive_CLIENT_INFO_EXTID(p);
@@ -115,11 +115,7 @@ bool NetworkContentSocketHandler::HandlePacket(Packet &p)
case PACKET_CONTENT_SERVER_CONTENT: return this->Receive_SERVER_CONTENT(p);
default:
if (this->HasClientQuit()) {
Debug(net, 0, "[tcp/content] Received invalid packet type {}", type);
} else {
Debug(net, 0, "[tcp/content] Received illegal packet");
}
Debug(net, 0, "[tcp/content] Received invalid packet type {}", type);
return false;
}
}
+1 -1
View File
@@ -22,7 +22,7 @@
*/
bool NetworkCoordinatorSocketHandler::HandlePacket(Packet &p)
{
PacketCoordinatorType type = (PacketCoordinatorType)p.Recv_uint8();
PacketCoordinatorType type = static_cast<PacketCoordinatorType>(p.Recv_uint8());
switch (type) {
case PACKET_COORDINATOR_GC_ERROR: return this->Receive_GC_ERROR(p);
+1 -7
View File
@@ -58,13 +58,7 @@ NetworkRecvStatus NetworkGameSocketHandler::CloseConnection([[maybe_unused]] boo
*/
NetworkRecvStatus NetworkGameSocketHandler::HandlePacket(Packet &p)
{
PacketGameType type = (PacketGameType)p.Recv_uint8();
if (this->HasClientQuit()) {
Debug(net, 0, "[tcp/game] Received invalid packet from client {}", this->client_id);
this->CloseConnection();
return NETWORK_RECV_STATUS_MALFORMED_PACKET;
}
PacketGameType type = static_cast<PacketGameType>(p.Recv_uint8());
this->last_packet = std::chrono::steady_clock::now();
+1 -1
View File
@@ -22,7 +22,7 @@
*/
bool NetworkTurnSocketHandler::HandlePacket(Packet &p)
{
PacketTurnType type = (PacketTurnType)p.Recv_uint8();
PacketTurnType type = static_cast<PacketTurnType>(p.Recv_uint8());
switch (type) {
case PACKET_TURN_TURN_ERROR: return this->Receive_TURN_ERROR(p);
+3 -9
View File
@@ -156,23 +156,17 @@ void NetworkUDPSocketHandler::ReceivePackets()
*/
void NetworkUDPSocketHandler::HandleUDPPacket(Packet &p, NetworkAddress &client_addr)
{
PacketUDPType type;
/* New packet == new client, which has not quit yet */
this->Reopen();
type = (PacketUDPType)p.Recv_uint8();
PacketUDPType type = static_cast<PacketUDPType>(p.Recv_uint8());
switch (this->HasClientQuit() ? PACKET_UDP_END : type) {
switch (type) {
case PACKET_UDP_CLIENT_FIND_SERVER: this->Receive_CLIENT_FIND_SERVER(p, client_addr); break;
case PACKET_UDP_SERVER_RESPONSE: this->Receive_SERVER_RESPONSE(p, client_addr); break;
default:
if (this->HasClientQuit()) {
Debug(net, 0, "[udp] Received invalid packet type {} from {}", type, client_addr.GetAddressAsString());
} else {
Debug(net, 0, "[udp] Received illegal packet from {}", client_addr.GetAddressAsString());
}
Debug(net, 0, "[udp] Received invalid packet type {} from {}", type, client_addr.GetAddressAsString());
break;
}
}