diff options
| author | lizzie <lizzie@eden-emu.dev> | 2025-10-31 17:38:04 +0100 |
|---|---|---|
| committer | crueter <crueter@eden-emu.dev> | 2025-10-31 17:38:04 +0100 |
| commit | 36b736482aebae6b61b059dc4272241d5e2323cb (patch) | |
| tree | 14f1c3c851cd9e2d0e3fce9e9e2ed05509a1489d /src/network/room.cpp | |
| parent | dbc98f758e1bd46594b2c7ade0509fba16c775e5 (diff) | |
[network] Fix potential infinite hang when generating fake IPs (#2898)
ALWAYS bound your loops :)
Signed-off-by: lizzie <lizzie@eden-emu.dev>
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2898
Reviewed-by: Caio Oliveira <caiooliveirafarias0@gmail.com>
Reviewed-by: MaranBr <maranbr@eden-emu.dev>
Co-authored-by: lizzie <lizzie@eden-emu.dev>
Co-committed-by: lizzie <lizzie@eden-emu.dev>
Diffstat (limited to 'src/network/room.cpp')
| -rw-r--r-- | src/network/room.cpp | 17 |
1 files 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<u8>(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) { |
