mirror of
https://git.citron-emu.org/citron/emulator
synced 2026-02-01 07:13:33 +00:00
Merge pull request 'fix/network-zstd-packet-handling' (#77) from fix/network-zstd-packet-handling into main
Reviewed-on: https://git.citron-emu.org/Citron/Emulator/pulls/77
This commit is contained in:
@@ -45,14 +45,73 @@ std::vector<u8> DecompressDataZSTD(std::span<const u8> compressed) {
|
||||
// 16 MB is a very generous limit for a single game packet.
|
||||
constexpr u64 MAX_REASONABLE_PACKET_SIZE = 16 * 1024 * 1024;
|
||||
|
||||
// ZSTD_getFrameContentSize can return special values if the size isn't in the header
|
||||
// or if there's an error. We must check for these AND our own sanity limit.
|
||||
if (decompressed_size == ZSTD_CONTENTSIZE_ERROR ||
|
||||
decompressed_size == ZSTD_CONTENTSIZE_UNKNOWN ||
|
||||
decompressed_size > MAX_REASONABLE_PACKET_SIZE) {
|
||||
// ZSTD_CONTENTSIZE_ERROR indicates a corrupted frame or invalid data - reject it
|
||||
// ZSTD_CONTENTSIZE_UNKNOWN means the size isn't in the header but decompression can still work
|
||||
if (decompressed_size == ZSTD_CONTENTSIZE_ERROR) {
|
||||
LOG_ERROR(Common, "Received network packet with corrupted or invalid ZSTD frame");
|
||||
return {};
|
||||
}
|
||||
|
||||
LOG_ERROR(Common, "Received network packet with invalid or oversized decompressed_size: {}", decompressed_size);
|
||||
return {}; // Return an empty vector to signal a graceful failure.
|
||||
// Reject packets that claim to be larger than reasonable
|
||||
if (decompressed_size != ZSTD_CONTENTSIZE_UNKNOWN && decompressed_size > MAX_REASONABLE_PACKET_SIZE) {
|
||||
LOG_ERROR(Common, "Received network packet with oversized decompressed_size: {}", decompressed_size);
|
||||
return {};
|
||||
}
|
||||
|
||||
// When size is unknown, use streaming decompression with a reasonable initial buffer
|
||||
if (decompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) {
|
||||
// Use streaming decompression for unknown size
|
||||
ZSTD_DCtx* dctx = ZSTD_createDCtx();
|
||||
if (!dctx) {
|
||||
LOG_ERROR(Common, "Failed to create ZSTD decompression context");
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<u8> decompressed;
|
||||
decompressed.resize(64 * 1024); // Start with 64KB buffer
|
||||
|
||||
ZSTD_inBuffer input = {compressed.data(), compressed.size(), 0};
|
||||
ZSTD_outBuffer output = {decompressed.data(), decompressed.size(), 0};
|
||||
|
||||
while (input.pos < input.size) {
|
||||
const size_t ret = ZSTD_decompressStream(dctx, &output, &input);
|
||||
if (ZSTD_isError(ret)) {
|
||||
LOG_ERROR(Common, "ZSTD streaming decompression failed with error: {}", ZSTD_getErrorName(ret));
|
||||
ZSTD_freeDCtx(dctx);
|
||||
return {};
|
||||
}
|
||||
|
||||
// If ret == 0, decompression is complete
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// If output buffer is full but we haven't consumed all input, need more space
|
||||
if (output.pos >= output.size && input.pos < input.size) {
|
||||
// Double the buffer size, up to maximum
|
||||
if (decompressed.size() > MAX_REASONABLE_PACKET_SIZE) {
|
||||
LOG_ERROR(Common, "ZSTD decompressed size exceeds maximum reasonable packet size");
|
||||
ZSTD_freeDCtx(dctx);
|
||||
return {};
|
||||
}
|
||||
const size_t old_size = decompressed.size();
|
||||
decompressed.resize(std::min(old_size * 2, static_cast<size_t>(MAX_REASONABLE_PACKET_SIZE)));
|
||||
output.dst = decompressed.data();
|
||||
output.size = decompressed.size();
|
||||
// Keep output.pos as is - it points to where we continue writing
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure all data was consumed
|
||||
if (input.pos < input.size) {
|
||||
LOG_ERROR(Common, "ZSTD streaming decompression: not all input was consumed");
|
||||
ZSTD_freeDCtx(dctx);
|
||||
return {};
|
||||
}
|
||||
|
||||
decompressed.resize(output.pos);
|
||||
ZSTD_freeDCtx(dctx);
|
||||
return decompressed;
|
||||
}
|
||||
|
||||
std::vector<u8> decompressed(decompressed_size);
|
||||
|
||||
@@ -85,6 +85,14 @@ static void GenerateErrorReport(Core::System& system, Result error_code, const F
|
||||
"Error code: 2056-{:04d} (0x{:08X})\n"
|
||||
"This may be related to online services or network functionality.\n\n",
|
||||
description, error_code.raw);
|
||||
} else if (module == 359) {
|
||||
module_note = fmt::format(
|
||||
"\n⚠️ WARNING: Error module 359 is undefined/unknown!\n"
|
||||
"This error may be game-generated or from an unimplemented service.\n"
|
||||
"Error code: 2359-{:04d} (0x{:08X})\n"
|
||||
"This is commonly reported by Super Smash Bros. Ultimate during multiplayer connections.\n"
|
||||
"It may be related to network connectivity or online service availability.\n\n",
|
||||
description, error_code.raw);
|
||||
}
|
||||
|
||||
std::string crash_report = fmt::format(
|
||||
@@ -139,13 +147,18 @@ static void ThrowFatalError(Core::System& system, Result error_code, FatalType f
|
||||
GenerateErrorReport(system, error_code, info);
|
||||
[[fallthrough]];
|
||||
case FatalType::ErrorScreen:
|
||||
// For Module 56 errors (unknown/game-generated), log and continue instead of crashing
|
||||
// These are often related to online services being unavailable
|
||||
// For unknown module errors (game-generated), log and continue instead of crashing
|
||||
// These are often related to online services being unavailable or network issues
|
||||
if (module == 56) {
|
||||
LOG_WARNING(Service_Fatal,
|
||||
"Module 56 error detected - likely game-generated due to unavailable "
|
||||
"online services. Continuing execution instead of crashing.");
|
||||
break;
|
||||
} else if (module == 359) {
|
||||
LOG_WARNING(Service_Fatal,
|
||||
"Module 359 error detected - likely game-generated by SSBU during multiplayer "
|
||||
"connection attempts. Continuing execution instead of crashing.");
|
||||
break;
|
||||
}
|
||||
// Since we have no fatal:u error screen. We should just kill execution instead
|
||||
ASSERT(false);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
||||
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "core/hle/service/ldn/lan_discovery.h"
|
||||
@@ -487,16 +488,34 @@ void LANDiscovery::ReceivePacket(const Network::LDNPacket& packet) {
|
||||
break;
|
||||
}
|
||||
case Network::LDNPacketType::ScanResp: {
|
||||
LOG_INFO(Frontend, "ScanResp packet received!");
|
||||
LOG_INFO(Frontend, "ScanResp packet received! Data size: {}", packet.data.size());
|
||||
|
||||
if (packet.data.size() < sizeof(NetworkInfo)) {
|
||||
LOG_WARNING(Service_LDN, "ScanResp packet data too small: {} bytes, expected: {} bytes",
|
||||
packet.data.size(), sizeof(NetworkInfo));
|
||||
break;
|
||||
}
|
||||
|
||||
NetworkInfo info{};
|
||||
std::memcpy(&info, packet.data.data(), sizeof(NetworkInfo));
|
||||
scan_results.insert({info.common.bssid, info});
|
||||
|
||||
LOG_DEBUG(Service_LDN, "ScanResp added to results. BSSID: {:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}, "
|
||||
"Total results: {}",
|
||||
info.common.bssid.raw[0], info.common.bssid.raw[1], info.common.bssid.raw[2],
|
||||
info.common.bssid.raw[3], info.common.bssid.raw[4], info.common.bssid.raw[5],
|
||||
scan_results.size());
|
||||
|
||||
break;
|
||||
}
|
||||
case Network::LDNPacketType::Connect: {
|
||||
LOG_INFO(Frontend, "Connect packet received!");
|
||||
LOG_INFO(Frontend, "Connect packet received! Data size: {}", packet.data.size());
|
||||
|
||||
if (packet.data.size() < sizeof(NodeInfo)) {
|
||||
LOG_WARNING(Service_LDN, "Connect packet data too small: {} bytes, expected: {} bytes",
|
||||
packet.data.size(), sizeof(NodeInfo));
|
||||
break;
|
||||
}
|
||||
|
||||
NodeInfo info{};
|
||||
std::memcpy(&info, packet.data.data(), sizeof(NodeInfo));
|
||||
@@ -516,12 +535,18 @@ void LANDiscovery::ReceivePacket(const Network::LDNPacket& packet) {
|
||||
break;
|
||||
}
|
||||
case Network::LDNPacketType::Disconnect: {
|
||||
LOG_INFO(Frontend, "Disconnect packet received!");
|
||||
LOG_INFO(Frontend, "Disconnect packet received! Data size: {}", packet.data.size());
|
||||
|
||||
connected_clients.erase(
|
||||
std::remove(connected_clients.begin(), connected_clients.end(), packet.local_ip),
|
||||
connected_clients.end());
|
||||
|
||||
if (packet.data.size() < sizeof(NodeInfo)) {
|
||||
LOG_WARNING(Service_LDN, "Disconnect packet data too small: {} bytes, expected: {} bytes",
|
||||
packet.data.size(), sizeof(NodeInfo));
|
||||
break;
|
||||
}
|
||||
|
||||
NodeInfo info{};
|
||||
std::memcpy(&info, packet.data.data(), sizeof(NodeInfo));
|
||||
|
||||
|
||||
@@ -936,10 +936,10 @@ void Socket::HandleProxyPacket(const ProxyPacket& packet) {
|
||||
LOG_WARNING(Network,
|
||||
"ProxyPacket received on regular socket (not ProxySocket). "
|
||||
"This may indicate socket type mismatch. "
|
||||
"Packet from {}:{} to {}:{}, protocol={}, reliable={}",
|
||||
"Packet from {}:{} to {}:{}, protocol={}",
|
||||
packet.local_endpoint.ip[0], packet.local_endpoint.portno,
|
||||
packet.remote_endpoint.ip[0], packet.remote_endpoint.portno,
|
||||
static_cast<int>(packet.protocol), packet.reliable);
|
||||
static_cast<int>(packet.protocol));
|
||||
}
|
||||
|
||||
} // namespace Network
|
||||
|
||||
@@ -46,6 +46,14 @@ void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) {
|
||||
auto decompressed = packet;
|
||||
decompressed.data = Common::Compression::DecompressDataZSTD(packet.data);
|
||||
|
||||
// Check if decompression failed (returns empty vector on error)
|
||||
if (decompressed.data.empty() && !packet.data.empty()) {
|
||||
stats.packets_dropped++;
|
||||
LOG_WARNING(Network, "Dropped packet: ZSTD decompression failed. Stats: sent={}, recv={}, dropped={}",
|
||||
stats.packets_sent, stats.packets_received, stats.packets_dropped);
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard guard(packets_mutex);
|
||||
received_packets.push(decompressed);
|
||||
stats.packets_received++;
|
||||
@@ -204,8 +212,18 @@ void ProxySocket::SendPacket(ProxyPacket& packet) {
|
||||
if (auto room_member = room_network.GetRoomMember().lock()) {
|
||||
if (room_member->IsConnected()) {
|
||||
const size_t original_size = packet.data.size();
|
||||
const std::vector<u8> original_data = packet.data; // Save original for potential fallback
|
||||
packet.data = Common::Compression::CompressDataZSTDDefault(packet.data.data(),
|
||||
packet.data.size());
|
||||
|
||||
// Check if compression failed (returns empty vector on error)
|
||||
if (packet.data.empty() && !original_data.empty()) {
|
||||
stats.packets_dropped++;
|
||||
LOG_ERROR(Network, "Failed to compress packet: ZSTD compression failed. Dropping packet. Stats: sent={}, dropped={}",
|
||||
stats.packets_sent, stats.packets_dropped);
|
||||
return;
|
||||
}
|
||||
|
||||
room_member->SendProxyPacket(packet);
|
||||
|
||||
stats.packets_sent++;
|
||||
@@ -260,14 +278,7 @@ std::pair<s32, Errno> ProxySocket::SendTo(u32 flags, std::span<const u8> message
|
||||
packet.data.clear();
|
||||
std::copy(message.begin(), message.end(), std::back_inserter(packet.data));
|
||||
|
||||
// Determine if packet should use unreliable delivery for better latency
|
||||
// Use unreliable delivery for:
|
||||
// 1. Small, frequent game data packets (< 1200 bytes for typical MTU)
|
||||
// 2. UDP protocol packets (most game traffic)
|
||||
// 3. Non-broadcast packets (broadcast should be reliable for coordination)
|
||||
const bool is_game_data = protocol == Protocol::UDP && message.size() < 1200 && !packet.broadcast;
|
||||
packet.reliable = !is_game_data;
|
||||
|
||||
// All packets use reliable delivery
|
||||
SendPacket(packet);
|
||||
|
||||
return {static_cast<s32>(message.size()), Errno::SUCCESS};
|
||||
|
||||
@@ -341,8 +341,8 @@ void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
|
||||
}
|
||||
|
||||
if (client_version != network_version) {
|
||||
SendVersionMismatch(event->peer);
|
||||
return;
|
||||
LOG_WARNING(Network, "Version mismatch: client version {} != server version {}, but allowing connection for backwards compatibility",
|
||||
client_version, network_version);
|
||||
}
|
||||
|
||||
// At this point the client is ready to be added to the room.
|
||||
@@ -832,14 +832,10 @@ void Room::RoomImpl::HandleProxyPacket(const ENetEvent* event) {
|
||||
bool broadcast;
|
||||
in_packet.Read(broadcast); // Broadcast
|
||||
|
||||
bool reliable;
|
||||
in_packet.Read(reliable); // Reliability flag
|
||||
|
||||
Packet out_packet;
|
||||
out_packet.Append(event->packet->data, event->packet->dataLength);
|
||||
const u32 enet_flags = reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED;
|
||||
ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
|
||||
enet_flags);
|
||||
ENET_PACKET_FLAG_RELIABLE);
|
||||
|
||||
const auto& destination_address = remote_ip;
|
||||
if (broadcast) { // Send the data to everyone except the sender
|
||||
@@ -890,14 +886,10 @@ void Room::RoomImpl::HandleLdnPacket(const ENetEvent* event) {
|
||||
bool broadcast;
|
||||
in_packet.Read(broadcast); // Broadcast
|
||||
|
||||
bool reliable;
|
||||
in_packet.Read(reliable); // Reliability flag
|
||||
|
||||
Packet out_packet;
|
||||
out_packet.Append(event->packet->data, event->packet->dataLength);
|
||||
const u32 enet_flags = reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED;
|
||||
ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
|
||||
enet_flags);
|
||||
ENET_PACKET_FLAG_RELIABLE);
|
||||
|
||||
const auto& destination_address = remote_ip;
|
||||
if (broadcast) { // Send the data to everyone except the sender
|
||||
|
||||
@@ -47,14 +47,8 @@ public:
|
||||
/// Thread that receives and dispatches network packets
|
||||
std::unique_ptr<std::thread> loop_thread;
|
||||
|
||||
/// Structure to hold a packet and its reliability flag
|
||||
struct PacketWithReliability {
|
||||
Packet packet;
|
||||
bool reliable;
|
||||
};
|
||||
|
||||
std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable.
|
||||
std::list<PacketWithReliability> send_list; ///< A list that stores all packets to send the async
|
||||
std::list<Packet> send_list; ///< A list that stores all packets to send the async
|
||||
|
||||
template <typename T>
|
||||
using CallbackSet = std::set<CallbackHandle<T>>;
|
||||
@@ -82,11 +76,10 @@ public:
|
||||
void StartLoop();
|
||||
|
||||
/**
|
||||
* Sends data to the room. It will be send on channel 0 with specified reliability
|
||||
* Sends data to the room. It will be send on channel 0 with flag RELIABLE
|
||||
* @param packet The data to send
|
||||
* @param reliable Whether to use reliable delivery (true) or unreliable/unsequenced (false)
|
||||
*/
|
||||
void Send(Packet&& packet, bool reliable = true);
|
||||
void Send(Packet&& packet);
|
||||
|
||||
/**
|
||||
* Sends a request to the server, asking for permission to join a room with the specified
|
||||
@@ -267,17 +260,14 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::list<PacketWithReliability> packets;
|
||||
std::list<Packet> packets;
|
||||
{
|
||||
std::lock_guard send_lock(send_list_mutex);
|
||||
packets.swap(send_list);
|
||||
}
|
||||
for (const auto& packet_data : packets) {
|
||||
const u32 enet_flags = packet_data.reliable ? ENET_PACKET_FLAG_RELIABLE
|
||||
: ENET_PACKET_FLAG_UNSEQUENCED;
|
||||
ENetPacket* enetPacket = enet_packet_create(packet_data.packet.GetData(),
|
||||
packet_data.packet.GetDataSize(),
|
||||
enet_flags);
|
||||
for (const auto& packet : packets) {
|
||||
ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(),
|
||||
ENET_PACKET_FLAG_RELIABLE);
|
||||
enet_peer_send(server, 0, enetPacket);
|
||||
}
|
||||
enet_host_flush(client);
|
||||
@@ -289,9 +279,9 @@ void RoomMember::RoomMemberImpl::StartLoop() {
|
||||
loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this);
|
||||
}
|
||||
|
||||
void RoomMember::RoomMemberImpl::Send(Packet&& packet, bool reliable) {
|
||||
void RoomMember::RoomMemberImpl::Send(Packet&& packet) {
|
||||
std::lock_guard lock(send_list_mutex);
|
||||
send_list.push_back({std::move(packet), reliable});
|
||||
send_list.push_back(std::move(packet));
|
||||
}
|
||||
|
||||
void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname_,
|
||||
@@ -390,7 +380,6 @@ void RoomMember::RoomMemberImpl::HandleProxyPackets(const ENetEvent* event) {
|
||||
proxy_packet.protocol = static_cast<Protocol>(protocol_type);
|
||||
|
||||
packet.Read(proxy_packet.broadcast);
|
||||
packet.Read(proxy_packet.reliable);
|
||||
packet.Read(proxy_packet.data);
|
||||
|
||||
Invoke<ProxyPacket>(proxy_packet);
|
||||
@@ -411,7 +400,6 @@ void RoomMember::RoomMemberImpl::HandleLdnPackets(const ENetEvent* event) {
|
||||
packet.Read(ldn_packet.local_ip);
|
||||
packet.Read(ldn_packet.remote_ip);
|
||||
packet.Read(ldn_packet.broadcast);
|
||||
packet.Read(ldn_packet.reliable);
|
||||
|
||||
packet.Read(ldn_packet.data);
|
||||
|
||||
@@ -665,10 +653,9 @@ void RoomMember::SendProxyPacket(const ProxyPacket& proxy_packet) {
|
||||
|
||||
packet.Write(static_cast<u8>(proxy_packet.protocol));
|
||||
packet.Write(proxy_packet.broadcast);
|
||||
packet.Write(proxy_packet.reliable);
|
||||
packet.Write(proxy_packet.data);
|
||||
|
||||
room_member_impl->Send(std::move(packet), proxy_packet.reliable);
|
||||
room_member_impl->Send(std::move(packet));
|
||||
}
|
||||
|
||||
void RoomMember::SendLdnPacket(const LDNPacket& ldn_packet) {
|
||||
@@ -680,11 +667,10 @@ void RoomMember::SendLdnPacket(const LDNPacket& ldn_packet) {
|
||||
packet.Write(ldn_packet.local_ip);
|
||||
packet.Write(ldn_packet.remote_ip);
|
||||
packet.Write(ldn_packet.broadcast);
|
||||
packet.Write(ldn_packet.reliable);
|
||||
|
||||
packet.Write(ldn_packet.data);
|
||||
|
||||
room_member_impl->Send(std::move(packet), ldn_packet.reliable);
|
||||
room_member_impl->Send(std::move(packet));
|
||||
}
|
||||
|
||||
void RoomMember::SendChatMessage(const std::string& message) {
|
||||
|
||||
@@ -32,7 +32,6 @@ struct LDNPacket {
|
||||
IPv4Address local_ip;
|
||||
IPv4Address remote_ip;
|
||||
bool broadcast;
|
||||
bool reliable = true; // Control packets use reliable delivery by default
|
||||
std::vector<u8> data;
|
||||
};
|
||||
|
||||
@@ -42,7 +41,6 @@ struct ProxyPacket {
|
||||
SockAddrIn remote_endpoint;
|
||||
Protocol protocol;
|
||||
bool broadcast;
|
||||
bool reliable = true; // Use reliable delivery by default for compatibility
|
||||
std::vector<u8> data;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user