From 67f74559fc886f6d5afdd3cdd1475fad46d2b4f9 Mon Sep 17 00:00:00 2001 From: rubidium Date: Fri, 26 Nov 2010 22:25:02 +0000 Subject: (svn r21334) -Fix [FS#4271]: make (more) sure that the savegame and transferred file are the same file and not different ones --- src/network/network_client.cpp | 14 ++++++++++++-- src/network/network_client.h | 3 ++- src/network/network_server.cpp | 11 ++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 0882c644b..6a0254b44 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -55,6 +55,7 @@ ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler() /* If for whatever reason the handle isn't closed, do it now. */ if (this->download_file != NULL) fclose(this->download_file); + free(this->download_filename); } NetworkRecvStatus ClientNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status) @@ -692,11 +693,16 @@ DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_BEGIN) if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST; if (this->download_file != NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET; - this->download_file = FioFOpenFile("network_client.tmp", "wb", AUTOSAVE_DIR); + char filename[MAX_PATH]; + FioGetDirectory(filename, lengthof(filename), AUTOSAVE_DIR); + strecat(filename, "network_client.tmp", lastof(filename)); + + this->download_file = FioFOpenFile(filename, "wb", NO_DIRECTORY); if (this->download_file == NULL) { _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR; return NETWORK_RECV_STATUS_SAVEGAME; } + this->download_filename = strdup(filename); _frame_counter = _frame_counter_server = _frame_counter_max = p->Recv_uint32(); @@ -748,7 +754,11 @@ DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_DONE) SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0); /* The map is done downloading, load it */ - if (!SafeSaveOrLoad("network_client.tmp", SL_LOAD, GM_NORMAL, AUTOSAVE_DIR)) { + bool load_success = SafeSaveOrLoad(this->download_filename, SL_LOAD, GM_NORMAL, NO_DIRECTORY); + free(this->download_filename); + this->download_filename = NULL; + + if (!load_success) { DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR; return NETWORK_RECV_STATUS_SAVEGAME; diff --git a/src/network/network_client.h b/src/network/network_client.h index 597aec193..bfe4c7613 100644 --- a/src/network/network_client.h +++ b/src/network/network_client.h @@ -19,7 +19,8 @@ /** Class for handling the client side of the game connection. */ class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler { private: - FILE *download_file; ///< Handle used for downloading the savegame. + FILE *download_file; ///< Handle used for downloading the savegame. + char *download_filename; ///< File name of the downloading savegame, so we open the right one. /** Status of the connection with the server. */ enum ServerStatus { diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 42de4cd2f..9ccd4399d 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -371,22 +371,23 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() } if (this->status == STATUS_AUTHORIZED) { - const char *filename = "network_server.tmp"; - Packet *p; + char filename[MAX_PATH]; + FioGetDirectory(filename, lengthof(filename), AUTOSAVE_DIR); + strecat(filename, "network_server.tmp", lastof(filename)); /* Make a dump of the current game */ - if (SaveOrLoad(filename, SL_SAVE, AUTOSAVE_DIR) != SL_OK) usererror("network savedump failed"); + if (SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY) != SL_OK) usererror("network savedump failed"); if (file_pointer != NULL) fclose(file_pointer); - file_pointer = FioFOpenFile(filename, "rb", AUTOSAVE_DIR); + file_pointer = FioFOpenFile(filename, "rb", NO_DIRECTORY); if (file_pointer == NULL) usererror("network savedump failed - could not open just saved dump"); fseek(file_pointer, 0, SEEK_END); if (ftell(file_pointer) == 0) usererror("network savedump failed - zero sized savegame?"); /* Now send the _frame_counter and how many packets are coming */ - p = new Packet(PACKET_SERVER_MAP_BEGIN); + Packet *p = new Packet(PACKET_SERVER_MAP_BEGIN); p->Send_uint32(_frame_counter); p->Send_uint32(ftell(file_pointer)); this->Send_Packet(p); -- cgit v1.2.3-54-g00ecf