From 8ccdd3bdb43b313d729279dbbb267528fc191615 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 26 Nov 2012 23:58:27 +0200 Subject: Ranged support of protocol version on server side --- src/clientserver.h | 13 +++++++++---- src/server.cpp | 34 ++++++++++++++++++++++++++-------- src/server.h | 2 +- src/test.cpp | 3 ++- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/clientserver.h b/src/clientserver.h index 6d4998550..d77772416 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -75,14 +75,19 @@ SharedBuffer makePacket_TOCLIENT_TIME_OF_DAY(u16 time, float time_speed); GENERIC_CMD_SET_ANIMATION GENERIC_CMD_SET_BONE_POSITION GENERIC_CMD_SET_ATTACHMENT + PROTOCOL_VERSION 15: + Serialization format changes */ -// Server always only supports one version -#define SERVER_PROTOCOL_VERSION 14 +#define LATEST_PROTOCOL_VERSION 15 -// Client can support older versions too +// Server's supported network protocol range +#define SERVER_PROTOCOL_VERSION_MIN 14 +#define SERVER_PROTOCOL_VERSION_MAX LATEST_PROTOCOL_VERSION + +// Client's supported network protocol range #define CLIENT_PROTOCOL_VERSION_MIN 13 -#define CLIENT_PROTOCOL_VERSION_MAX SERVER_PROTOCOL_VERSION +#define CLIENT_PROTOCOL_VERSION_MAX LATEST_PROTOCOL_VERSION // Constant that differentiates the protocol from random data and other protocols #define PROTOCOL_ID 0x4f457403 diff --git a/src/server.cpp b/src/server.cpp index f29155ba0..41765ed19 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2042,14 +2042,26 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(datasize >= 2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2) min_net_proto_version = readU16(&data[2+1+PLAYERNAME_SIZE+PASSWORD_SIZE]); - // Use min if version field doesn't exist (backwards compatibility) + // Use same version as minimum and maximum if maximum version field + // doesn't exist (backwards compatibility) u16 max_net_proto_version = min_net_proto_version; if(datasize >= 2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2+2) max_net_proto_version = readU16(&data[2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2]); + // Start with client's maximum version u16 net_proto_version = max_net_proto_version; - if(max_net_proto_version != SERVER_PROTOCOL_VERSION && min_net_proto_version <= SERVER_PROTOCOL_VERSION) - net_proto_version = SERVER_PROTOCOL_VERSION; + + // Figure out a working version if it is possible at all + if(max_net_proto_version >= SERVER_PROTOCOL_VERSION_MIN || + min_net_proto_version <= SERVER_PROTOCOL_VERSION_MAX) + { + // If maximum is larger than our maximum, go with our maximum + if(max_net_proto_version > SERVER_PROTOCOL_VERSION_MAX) + net_proto_version = SERVER_PROTOCOL_VERSION_MAX; + // Else go with client's maximum + else + net_proto_version = max_net_proto_version; + } verbosestream<<"Server: "<getBool("strict_protocol_version_checking")) { - if(net_proto_version != SERVER_PROTOCOL_VERSION) + if(net_proto_version < SERVER_PROTOCOL_VERSION_MIN || + net_proto_version > SERVER_PROTOCOL_VERSION_MAX) { actionstream<<"Server: A mismatched client tried to connect" <<" from "<net_proto_version < SERVER_PROTOCOL_VERSION) + if(getClient(peer_id)->net_proto_version < LATEST_PROTOCOL_VERSION) { - SendChatMessage(peer_id, L"# Server: WARNING: YOUR CLIENT IS OLD AND MAY WORK PROPERLY WITH THIS SERVER!"); + SendChatMessage(peer_id, L"# Server: WARNING: YOUR CLIENT IS OLD " + L"AND MAY NOT FULLY WORK WITH THIS SERVER!"); } /* diff --git a/src/server.h b/src/server.h index 223c1b0ff..f770fa3d4 100644 --- a/src/server.h +++ b/src/server.h @@ -602,7 +602,7 @@ private: static void SendItemDef(con::Connection &con, u16 peer_id, IItemDefManager *itemdef); static void SendNodeDef(con::Connection &con, u16 peer_id, - INodeDefManager *nodedef); + INodeDefManager *nodedef, u16 protocol_version); /* Non-static send methods. diff --git a/src/test.cpp b/src/test.cpp index 52782fa36..bc0692a78 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -41,6 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "util/serialize.h" #include "noise.h" // PseudoRandom used for random data for compression +#include "clientserver.h" // LATEST_PROTOCOL_VERSION /* Asserts that the exception occurs @@ -324,7 +325,7 @@ struct TestNodedefSerialization: public TestBase f.tiledef[i].name = "default_stone.png"; f.is_ground_content = true; std::ostringstream os(std::ios::binary); - f.serialize(os); + f.serialize(os, LATEST_PROTOCOL_VERSION); verbosestream<<"Test ContentFeatures size: "<