From 939b637a8a054c3bde36b24083ee467f54c389c0 Mon Sep 17 00:00:00 2001 From: lizzie Date: Thu, 30 Oct 2025 19:48:39 +0000 Subject: [PATCH] [network] Fix potential infinite hang Signed-off-by: lizzie --- src/network/room.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/network/room.cpp b/src/network/room.cpp index 1a3ad75d2b..ca3946b521 100644 --- a/src/network/room.cpp +++ b/src/network/room.cpp @@ -810,15 +810,16 @@ void Room::RoomImpl::BroadcastRoomInformation() { } IPv4Address Room::RoomImpl::GenerateFakeIPAddress() { - IPv4Address result_ip{192, 168, 0, 0}; - std::uniform_int_distribution<> dis(0x01, 0xFE); // Random byte between 1 and 0xFE - do { - for (std::size_t i = 2; i < result_ip.size(); ++i) { - result_ip[i] = static_cast(dis(random_gen)); + // An IP address is valid if it is not already taken by anybody else in the room. + std::lock_guard lock(member_mutex); + for (u8 i = 0x01; i < 0xFF; ++i) + for (u8 j = 0x01; j < 0xFF; ++j) { + IPv4Address addr{192, 168, i, j}; + if (std::all_of(members.begin(), members.end(), [&addr](auto const& member) { return member.fake_ip != addr; })) + return addr; } - } while (!IsValidFakeIPAddress(result_ip)); - - return result_ip; + LOG_ERROR(Network, "All addresses are taken"); + return IPv4Address{192, 168, 0, 0}; } void Room::RoomImpl::HandleProxyPacket(const ENetEvent* event) {