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(gameengine): bound wire-supplied player ids and repair the network cluster

Olcay Seygan8ad6e989fc01
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 42d258d18..25b691ce6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -429,6 +429,9 @@ found and fixed — EA's own, not port damage.**
- One tree in ten stood dead still in the breeze. The number that picks which way a tree sways was drawn from a range one short at the bottom, and the bottom of that range is the slot meaning "no sway at all".
- Police car lights flash at their own speed instead of your monitor's. On a fast screen with the frame cap off they were cycling five and a half times too quickly.
- Ten more places that wrote terrain, roads, bridges, trees, foundations and scorch marks into a buffer the graphics card had refused to hand over. Every one of them wrote to address zero instead, and a graphics device pulled out from under the game is all it takes; the frame is simply skipped now.
+- Your Windows login and your computer's name stay on your computer. Both were broadcast to every machine on the network as part of the game announcement, so anyone in the lobby could read them; the tooltip they fed now shows the player name you chose, which is the part it was for.
+- Leaving a network game leaves it. Quitting cleared the other players' queued orders out of one connection and then stopped, because taking a message off the queue also cut its link to the next one, so the rest stayed and kept being passed along and you sat on the game over screen.
+- A malformed packet claiming to come from a player who is not in the game no longer reads and writes past the end of the game's player tables. The player number arrives over the network and was used to index them directly, in the chat, the file transfer progress, the frame relay and the disconnect screen.
- Confirming the "recorded by an older version" warning plays the replay you picked. The menu behind the warning stays live, so deleting a replay while it was up rebuilt the list, and the row number the warning had been opened on now belonged to something else - which is what you got, with nothing to tell you.
- The building outline and the target circle stay where they are when the cursor crosses the skyline. There is no ground above the horizon, and the game took the miss for an answer: the corner of the map. Every time the mouse went over the horizon the ghost you were placing shot across the world and back.
- The radar stops reading a hero who has just died. It kept the unit's own position rather than a copy of it, and only rebuilt that list every sixth frame, so the five frames after a hero fell were spent looking at a unit that was no longer there.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.h
index b4a286d85..5b72e223a 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.h
@@ -51,6 +51,15 @@ static const Int m_lanMaxOptionsLength = MAX_LANAPI_PACKET_SIZE - ( 8 + (g_lanGa
+ (g_lanLoginNameLength+1) + (g_lanHostNameLength+1) );
static const Int g_maxSerialLength = 23; // including the trailing '\0'
+//
+// Whether to put this machine's Windows login and computer name on the wire. EA cut the
+// LANMessage fields for them down to one character each (the two lengths above) to save packet
+// space, but the game options string still carried both in full, broadcast to every machine on
+// the network, for a tooltip. Off: the two names stay empty and the tooltip shows the player
+// name the player chose, which is the part anybody wanted.
+//
+#define TELL_COMPUTER_IDENTITY_IN_LAN_LOBBY 0
+
struct LANMessage;
/**
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp
index ab8e61956..042e17246 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp
@@ -303,18 +303,19 @@ void Connection::clearCommandsExceptFrom( Int playerIndex )
NetCommandRef *tmp = m_netCommandList->getFirstMessage();
while (tmp)
{
+ // removeMessage nulls the node's own next pointer (NetCommandList::removeMessage), so the
+ // successor has to be taken before the node leaves the list. Read after, and this loop
+ // stopped dead on the first command it cleared and left every later one in the queue.
+ NetCommandRef *next = tmp->getNext();
NetCommandMsg *msg = tmp->getCommand();
if (msg->getPlayerID() != playerIndex)
{
DEBUG_LOG(("Connection::clearCommandsExceptFrom(%d) - clearing a command from %d for frame %d\n",
playerIndex, tmp->getCommand()->getPlayerID(), tmp->getCommand()->getExecutionFrame()));
m_netCommandList->removeMessage(tmp);
- NetCommandRef *toDelete = tmp;
- tmp = tmp->getNext();
- toDelete->deleteInstance();
- } else {
- tmp = tmp->getNext();
+ tmp->deleteInstance();
}
+ tmp = next;
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
index d95843149..bfe4b0fe9 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@@ -330,6 +330,11 @@ Int ConnectionManager::getPingsRecieved()
Bool ConnectionManager::isPlayerConnected( Int playerID )
{
+ // Callers hand this a player id straight off the wire, and m_connections holds MAX_SLOTS of
+ // them. A packet claiming to be from player 200 used to index that far past the end.
+ if ( playerID < 0 || playerID >= MAX_SLOTS )
+ return FALSE;
+
return ( playerID == m_localSlot || (m_connections[playerID] && !m_connections[playerID]->isQuitting()) );
}
@@ -375,9 +380,6 @@ void ConnectionManager::destroyGameMessages() {
* assumption that a command will only be relayed once.
*/
void ConnectionManager::doRelay() {
- static Int numPackets = 0;
- static Int numCommands = 0;
-
NetPacket *packet = NULL;
for (Int i = 0; i < MAX_MESSAGES; ++i) {
@@ -413,10 +415,7 @@ void ConnectionManager::doRelay() {
sendRemoteCommand(cmd);
}
cmd = cmd->getNext();
-
- ++numCommands;
}
- ++numPackets;
// Delete this packet since we won't be needing it anymore.
packet->deleteInstance();
@@ -440,10 +439,7 @@ void ConnectionManager::doRelay() {
sendRemoteCommand(cmd);
}
cmd = cmd->getNext();
-
- ++numCommands;
}
- ++numPackets;
// Delete this packet since we won't be needing it anymore.
packet->deleteInstance();
@@ -680,6 +676,10 @@ void ConnectionManager::processChat(NetChatCommandMsg *msg)
UnicodeString unitext;
UnicodeString name;
UnsignedByte playerID = msg->getPlayerID();
+ // From the wire, and used below to index m_connections and to ask TheGameInfo for a slot.
+ if (playerID >= MAX_SLOTS) {
+ return;
+ }
//DEBUG_LOG(("processChat(): playerID = %d\n", playerID));
if (playerID == m_localSlot) {
name = m_localUser->GetName();
@@ -845,9 +845,17 @@ void ConnectionManager::processFileProgress(NetFileProgressCommandMsg *msg)
{
DEBUG_LOG(("ConnectionManager::processFileProgress() - command %d is at %d%%\n",
msg->getFileID(), msg->getProgress()));
- Int oldProgress = s_fileProgressMap[msg->getPlayerID()][msg->getFileID()];
- s_fileProgressMap[msg->getPlayerID()][msg->getFileID()] = max(oldProgress, msg->getProgress());
+ // s_fileProgressMap is an array of MAX_SLOTS maps and this index arrives in the packet.
+ // (The file id inside it is a map key, so that one cannot run off anything.)
+ const UnsignedInt playerID = msg->getPlayerID();
+ if (playerID >= MAX_SLOTS) {
+ return;
+ }
+
+ Int oldProgress = s_fileProgressMap[playerID][msg->getFileID()];
+
+ s_fileProgressMap[playerID][msg->getFileID()] = max(oldProgress, msg->getProgress());
}
void ConnectionManager::processProgress( NetProgressCommandMsg *msg )
@@ -1173,10 +1181,13 @@ void ConnectionManager::sendRemoteCommand(NetCommandRef *msg) {
msg->getCommand()->getID(), GetAsciiNetCommandType(msg->getCommand()->getNetCommandType()).str(), msg->getCommand()->getPlayerID(), msg->getRelay()));
UnsignedByte relay = msg->getRelay();
- if ((relay & (1 << m_localSlot)) && (m_frameData[msg->getCommand()->getPlayerID()] != NULL)) {
+ // The sender id is in the packet and m_frameData holds MAX_SLOTS entries.
+ const UnsignedInt senderID = msg->getCommand()->getPlayerID();
+ FrameDataManager *senderFrameData = (senderID < MAX_SLOTS) ? m_frameData[senderID] : NULL;
+ if ((relay & (1 << m_localSlot)) && (senderFrameData != NULL)) {
if (IsCommandSynchronized(msg->getCommand()->getNetCommandType())) {
DEBUG_LOG(("ConnectionManager::sendRemoteCommand - adding net command of type %s to player %d for frame %d\n", GetAsciiNetCommandType(msg->getCommand()->getNetCommandType()).str(), msg->getCommand()->getPlayerID(), msg->getCommand()->getExecutionFrame()));
- m_frameData[msg->getCommand()->getPlayerID()]->addNetCommandMsg(msg->getCommand());
+ senderFrameData->addNetCommandMsg(msg->getCommand());
}
}
@@ -1880,6 +1891,9 @@ void ConnectionManager::quitGame() {
NetDisconnectPlayerCommandMsg *disconnectMsg = newInstance(NetDisconnectPlayerCommandMsg);
disconnectMsg->setDisconnectSlot(m_localSlot);
disconnectMsg->setPlayerID(m_localSlot);
+ // Every other machine reads this to know which frame we left on; quitting through the
+ // disconnection menu was the one route that never filled it in.
+ disconnectMsg->setDisconnectFrame(TheGameLogic->getFrame());
if (DoesCommandRequireACommandID(disconnectMsg->getNetCommandType())) {
disconnectMsg->setID(GenerateNextCommandID());
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/DisconnectManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/DisconnectManager.cpp
index 804abaf90..a51359f2b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/DisconnectManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/DisconnectManager.cpp
@@ -381,6 +381,11 @@ void DisconnectManager::processDisconnectVote(NetCommandMsg *msg, ConnectionMana
void DisconnectManager::processDisconnectFrame(NetCommandMsg *msg, ConnectionManager *conMgr) {
NetDisconnectFrameCommandMsg *cmdMsg = (NetDisconnectFrameCommandMsg *)msg;
UnsignedInt playerID = cmdMsg->getPlayerID();
+ // Off the wire, and the index into m_disconnectFrames and m_disconnectFramesReceived, both
+ // MAX_SLOTS long.
+ if (playerID >= MAX_SLOTS) {
+ return;
+ }
if (m_disconnectFrames[playerID] >= cmdMsg->getDisconnectFrame()) {
// this message isn't valid, we have a disconnect frame that is later than this already.
return;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp
index 41522faae..43f64a98c 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp
@@ -463,9 +463,11 @@ struct GameSortStruct
// multiset calls its comparator through a const reference.
bool operator()(GameSpyStagingRoom *g1, GameSpyStagingRoom *g2) const
{
- // sort CRC mismatches to the bottom
- Bool g1Good = (g1->getExeCRC() != TheGlobalData->m_exeCRC || g1->getIniCRC() != TheGlobalData->m_iniCRC);
- Bool g2Good = (g1->getExeCRC() != TheGlobalData->m_exeCRC || g1->getIniCRC() != TheGlobalData->m_iniCRC);
+ // sort CRC mismatches to the bottom. Both lines were wrong: each said "good" while testing
+ // for a mismatch, and the second read g1 twice, so the two were always equal, the xor below
+ // was always false and this branch never sorted anything at all.
+ Bool g1Good = (g1->getExeCRC() == TheGlobalData->m_exeCRC && g1->getIniCRC() == TheGlobalData->m_iniCRC);
+ Bool g2Good = (g2->getExeCRC() == TheGlobalData->m_exeCRC && g2->getIniCRC() == TheGlobalData->m_iniCRC);
if ( g1Good ^ g2Good )
{
return g1Good;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
index b9b543b63..3e53b2420 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
@@ -118,6 +118,7 @@ void LANAPI::init( void )
m_lastGameopt = "";
+#if TELL_COMPUTER_IDENTITY_IN_LAN_LOBBY
unsigned long bufSize = UNLEN + 1;
char userName[UNLEN + 1];
if (!GetUserName(userName, &bufSize))
@@ -133,6 +134,10 @@ void LANAPI::init( void )
strcpy(computerName, "unknown");
}
m_hostName = computerName;
+#else
+ m_userName.clear();
+ m_hostName.clear();
+#endif
}
void LANAPI::reset( void )
@@ -451,11 +456,13 @@ void LANAPI::update( void )
}
else
{
+#if TELL_COMPUTER_IDENTITY_IN_LAN_LOBBY
AsciiString text;
text.format("User=%s", m_userName.str());
RequestGameOptions( text, true );
text.format("Host=%s", m_hostName.str());
RequestGameOptions( text, true );
+#endif
RequestGameOptions( "HELLO", false );
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp
index 97508d0a8..d50fdb624 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp
@@ -642,10 +642,12 @@ void LANAPI::OnGameJoin( ReturnType ret, LANGameInfo *theGame )
RequestGameOptions(options, true);
options.format("Color=%d", pref.getPreferredColor());
RequestGameOptions(options, true);
+#if TELL_COMPUTER_IDENTITY_IN_LAN_LOBBY
options.format("User=%s", m_userName.str());
RequestGameOptions( options, true );
options.format("Host=%s", m_hostName.str());
RequestGameOptions( options, true );
+#endif
options.format("NAT=%d", FirewallHelperClass::FIREWALL_TYPE_SIMPLE); // BGC: This is a LAN game, so there is no firewall.
RequestGameOptions( options, true );
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANGameInfo.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANGameInfo.cpp
index e29851a45..0a4dfe6fd 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANGameInfo.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANGameInfo.cpp
@@ -92,6 +92,10 @@ LANGameInfo::LANGameInfo()
//Initializtions missing and needed
m_lastHeard = 0;
m_next = NULL;
+ // A discovered game is addGame'd before the announcement's flag is copied into it
+ // (LANAPIhandlers.cpp:121-125), and the lobby list can be drawn in between, so this has to
+ // start somewhere. A LAN game is the answer that costs nothing if it turns out wrong.
+ m_isDirectConnect = FALSE;
//
for (Int i = 0; i< MAX_SLOTS; ++i)
setSlotPointer(i, &m_LANSlot[i]);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
index d922f0935..fbd9043cd 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
@@ -609,6 +609,7 @@ NetDisconnectKeepAliveCommandMsg::~NetDisconnectKeepAliveCommandMsg() {
NetDisconnectPlayerCommandMsg::NetDisconnectPlayerCommandMsg() : NetCommandMsg() {
m_commandType = NETCOMMANDTYPE_DISCONNECTPLAYER;
m_disconnectSlot = 0;
+ m_disconnectFrame = 0;
}
/**