Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gamenetwork): stop the packet router succession reading past its plan

Olcay Seygane42f7c2ab710
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b7ea2bec..4a3d56465 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -270,6 +270,14 @@ map. Ctrl+A is still everything at once.
once. A stall there had to sit through the retry and then the disconnect screen instead of being
repaired on the spot. It counts properly now.
+- **When the player relaying everyone's orders left, the game could pick a relay that does not
+ exist.** One player in every online game quietly carries the traffic for everybody, and there is a
+ standing order of who takes over when they drop out. Reading the next name off that list could
+ read past the end of it and come back with a number that was never a player at all, and the game
+ then tried to talk to them twice a second. That is a crash rather than a stall, and it lands at the
+ worst moment in a match — right after someone has already dropped. The list is now read properly,
+ and when it genuinely runs out the game says so instead of inventing somebody.
+
## Sharper textures, for free
Zero Hour shipped downscaled copies of a lot of the base game's textures, and because of the order
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
index f06e956eb..dbc7ce53f 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
@@ -89,4 +89,14 @@ Int settleRoomFrameRate( Int minFps, Int fpsLimit );
/** The rate to actually command: one probe step above the settled rate, never above the limit. */
Int probeRoomFrameRate( Int settledFps, Int fpsLimit );
+/* Every machine keeps the same packet router fallback plan - the list of who relays for everybody,
+ in the order they take over - built from the shared slot list, so the succession is identical on
+ every machine without anyone having to agree about it at run time. Walking that list is the one
+ place a disagreement would be fatal, and it is also where the walk ran off the end. */
+
+/** The slot that takes over relaying after currentSlot, or maxSlots when there is nobody left.
+ fallback holds maxSlots entries, valid slots are below maxSlots, and anything else (the -1
+ padding an emptied entry is left with) ends the list. */
+UnsignedInt nextPacketRouterSlot( const UnsignedInt *fallback, Int maxSlots, UnsignedInt currentSlot );
+
#endif // _CUSHION_METRICS_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
index 39e97b3c0..b4d1c5dff 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@@ -1379,7 +1379,12 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS
} else {
//DEBUG_LOG(("ConnectionManager::updateRunAhead - average latency = %f, average fps = %d, didSelfSlug = false\n", m_frameMetrics.getAverageLatency(), m_frameMetrics.getAverageFPS()));
}
- m_connections[m_packetRouterSlot]->sendNetCommandMsg(msg, 1 << m_packetRouterSlot);
+ // Every other m_connections[m_packetRouterSlot] in this file is bounds tested first; this one
+ // was not, and it is the one that runs twice a second. m_packetRouterSlot is MAX_SLOTS when
+ // the fallback plan has run out, and used to be able to hold a whole IP address.
+ if ((m_packetRouterSlot < MAX_SLOTS) && (m_connections[m_packetRouterSlot] != NULL)) {
+ m_connections[m_packetRouterSlot]->sendNetCommandMsg(msg, 1 << m_packetRouterSlot);
+ }
msg->detach();
}
lasttimesent = curTime;
@@ -1783,12 +1788,10 @@ PlayerLeaveCode ConnectionManager::disconnectPlayer(Int slot) {
// }
if (slot == m_packetRouterSlot) {
- Int index = 0;
- while ((index < (MAX_SLOTS-1)) && (m_packetRouterFallback[index] != m_packetRouterSlot)) {
- ++index;
- }
- ++index;
- m_packetRouterSlot = m_packetRouterFallback[index];
+ // The same walk as getNextPacketRouterSlot, and it used to run off the end of the plan in the
+ // same way - see nextPacketRouterSlot in CushionMetrics.cpp. MAX_SLOTS out means the plan is
+ // exhausted, which every reader of m_packetRouterSlot already tests for.
+ m_packetRouterSlot = nextPacketRouterSlot(m_packetRouterFallback, MAX_SLOTS, m_packetRouterSlot);
DEBUG_LOG(("Packet router left. New packet router is slot %d\n", m_packetRouterSlot));
retval = PLAYERLEAVECODE_PACKETROUTER;
}
@@ -1939,6 +1942,13 @@ void ConnectionManager::parseUserList(const GameInfo *game)
}
}
+
+ // The router is the head of the fallback plan, which is what EA's own commented-out
+ // determineRouterFallbackPlan did. init() hardcodes slot 0 instead, and slot 0 is only in the
+ // plan at all if slot 0 holds a human player - otherwise the router was a slot nobody relays for,
+ // and the first succession then walked a plan the router was not in. Built from the shared slot
+ // list, so every machine computes the same head without exchanging anything.
+ m_packetRouterSlot = m_packetRouterFallback[0];
#ifdef MEMORYPOOL_DEBUG
TheMemoryPoolFactory->debugSetInitFillerIndex(m_localSlot);
#endif
@@ -2492,12 +2502,7 @@ void ConnectionManager::sendSingleFrameToPlayer(UnsignedInt playerID, UnsignedIn
}
UnsignedInt ConnectionManager::getNextPacketRouterSlot(UnsignedInt playerID) {
- Int index = 0;
- while ((index < (MAX_SLOTS-1)) && (m_packetRouterFallback[index] != playerID)) {
- ++index;
- }
- ++index;
- return m_packetRouterFallback[index];
+ return nextPacketRouterSlot(m_packetRouterFallback, MAX_SLOTS, playerID);
}
void ConnectionManager::requestFrameDataResend(Int playerID, UnsignedInt frame) {
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
index eb86a2849..f520bca8e 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
@@ -106,3 +106,31 @@ Int probeRoomFrameRate( Int settledFps, Int fpsLimit )
return probed;
}
+
+UnsignedInt nextPacketRouterSlot( const UnsignedInt *fallback, Int maxSlots, UnsignedInt currentSlot )
+{
+ /* EA wrote this as `while ((index < (MAX_SLOTS-1)) && (fallback[index] != playerID)) ++index;`
+ followed by `++index; return fallback[index];`. The loop stops at MAX_SLOTS-1 whether it found
+ anything or not, so the increment can leave index at MAX_SLOTS and the return reads one entry
+ past the end of the array. In ConnectionManager that entry is m_localAddr, the local IP: the
+ "new packet router" comes out as a number around three billion, and the next metrics send
+ indexes m_connections[] with it. Two ways in - the current router sitting in the last entry
+ of a full eight player plan, or not being in the plan at all - and the same walk is written
+ out twice, in getNextPacketRouterSlot and in disconnectPlayer. */
+ Int index = 0;
+ while( (index < maxSlots) && (fallback[index] != currentSlot) )
+ ++index;
+
+ if( index >= maxSlots )
+ return (UnsignedInt)maxSlots; // not in the plan, so nothing follows it
+
+ ++index;
+ if( index >= maxSlots )
+ return (UnsignedInt)maxSlots; // last in the plan, nobody left to take over
+
+ UnsignedInt next = fallback[index];
+ if( next >= (UnsignedInt)maxSlots )
+ return (UnsignedInt)maxSlots; // the -1 padding an emptied entry carries
+
+ return next;
+}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 23bd18326..2af8c8e16 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -2608,6 +2608,64 @@ TEST(resend_window_is_measured_from_the_start_of_the_match_not_four_billion)
CHECK( frameIsTooOldToResend( 5, 0xFFFFFF00u, KEEP ) );
}
+/* The packet router fallback plan: who relays for everybody, and in what order they take over.
+ Walked in two places, both of which used to be able to read one entry past the end of it. */
+TEST(packet_router_succession_never_walks_off_the_end_of_the_plan)
+{
+ const Int SLOTS = 8;
+ const UnsignedInt NONE = (UnsignedInt)SLOTS; // the "plan is exhausted" answer
+ const UnsignedInt EMPTY = (UnsignedInt)-1; // what an emptied entry is left holding
+
+ /* A full eight player game. The last entry is the case EA's walk got wrong: the loop stopped at
+ MAX_SLOTS-1 whether it matched or not, the unconditional ++ then made the index MAX_SLOTS, and
+ the read landed on the member after the array. */
+ const UnsignedInt full[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
+ CHECK_EQ( nextPacketRouterSlot( full, SLOTS, 0 ), 1u );
+ CHECK_EQ( nextPacketRouterSlot( full, SLOTS, 5 ), 6u );
+ CHECK_EQ( nextPacketRouterSlot( full, SLOTS, 6 ), 7u );
+ CHECK_EQ( nextPacketRouterSlot( full, SLOTS, 7 ), NONE );
+
+ /* A four player game leaves the rest of the plan as -1. Reading that as a slot number gives
+ four billion, which is the same wrong answer by a different route. */
+ const UnsignedInt four[8] = { 0, 1, 2, 3, EMPTY, EMPTY, EMPTY, EMPTY };
+ CHECK_EQ( nextPacketRouterSlot( four, SLOTS, 0 ), 1u );
+ CHECK_EQ( nextPacketRouterSlot( four, SLOTS, 2 ), 3u );
+ CHECK_EQ( nextPacketRouterSlot( four, SLOTS, 3 ), NONE );
+
+ /* Every leave compacts the plan, so the entries are slot numbers with gaps and the tail fills
+ with -1 from the end. */
+ const UnsignedInt compacted[8] = { 1, 2, 4, 5, 6, 7, EMPTY, EMPTY };
+ CHECK_EQ( nextPacketRouterSlot( compacted, SLOTS, 1 ), 2u );
+ CHECK_EQ( nextPacketRouterSlot( compacted, SLOTS, 2 ), 4u );
+ CHECK_EQ( nextPacketRouterSlot( compacted, SLOTS, 7 ), NONE );
+
+ /* Asking about a slot that is not in the plan at all - the state the hardcoded initial router
+ could produce when slot 0 held no human player. */
+ CHECK_EQ( nextPacketRouterSlot( compacted, SLOTS, 0 ), NONE );
+ CHECK_EQ( nextPacketRouterSlot( compacted, SLOTS, 3 ), NONE );
+
+ /* Last player standing. */
+ const UnsignedInt alone[8] = { 3, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY };
+ CHECK_EQ( nextPacketRouterSlot( alone, SLOTS, 3 ), NONE );
+
+ /* The succession is a walk of the plan, never a re-election: repeated calls march down the plan
+ in order and stop, so two machines running it on the same plan cannot end up disagreeing
+ about who is relaying. That property is why the plan is not reordered from local latency
+ measurements - see MULTIPLAYER.md 2.5. */
+ UnsignedInt router = compacted[0];
+ Int steps = 0;
+ while( router < (UnsignedInt)SLOTS )
+ {
+ UnsignedInt next = nextPacketRouterSlot( compacted, SLOTS, router );
+ CHECK( next == NONE || next > router );
+ router = next;
+ ++steps;
+ CHECK( steps <= SLOTS );
+ }
+ CHECK_EQ( steps, 6 ); // six live entries, then the plan is out
+
+}
+
TEST(selfslug_is_monotonic_in_the_cushion)
{
// once there is enough margin the answer must stay no, or the frame rate would oscillate