(svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
This commit is contained in:
		
							
								
								
									
										32
									
								
								network.c
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								network.c
									
									
									
									
									
								
							@@ -256,6 +256,36 @@ static void NetworkClientError(byte res, NetworkClientState *cs) {
 | 
				
			|||||||
	_networking = false;
 | 
						_networking = false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** Retrieve a string representation of an internal error number
 | 
				
			||||||
 | 
					 * @param buf buffer where the error message will be stored
 | 
				
			||||||
 | 
					 * @param err NetworkErrorCode (integer)
 | 
				
			||||||
 | 
					 * @return returns a pointer to the error message (buf) */
 | 
				
			||||||
 | 
					char *GetNetworkErrorMsg(char *buf, NetworkErrorCode err)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						/* List of possible network errors, used by
 | 
				
			||||||
 | 
						 * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
 | 
				
			||||||
 | 
						static const StringID network_error_strings[] = {
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_GENERAL,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_DESYNC,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_SAVEGAME,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_CONNECTION_LOST,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_PROTOCOL_ERROR,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_NOT_AUTHORIZED,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_NOT_EXPECTED,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_WRONG_REVISION,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_NAME_IN_USE,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_WRONG_PASSWORD,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_PLAYER_MISMATCH,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_KICKED,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_CHEATER,
 | 
				
			||||||
 | 
							STR_NETWORK_ERR_CLIENT_SERVER_FULL,
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (err >= lengthof(network_error_strings)) err = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return GetString(buf, network_error_strings[err]);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Find all IP-aliases for this host
 | 
					// Find all IP-aliases for this host
 | 
				
			||||||
static void NetworkFindIPs(void)
 | 
					static void NetworkFindIPs(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -524,7 +554,7 @@ void NetworkCloseClient(NetworkClientState *cs)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		NetworkGetClientName(client_name, sizeof(client_name), cs);
 | 
							NetworkGetClientName(client_name, sizeof(client_name), cs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		GetString(str, STR_NETWORK_ERR_CLIENT_GENERAL + errorno);
 | 
							GetNetworkErrorMsg(str, errorno);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str);
 | 
							NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -676,15 +676,12 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
 | 
					DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int errorno;
 | 
					 | 
				
			||||||
	char str[100];
 | 
						char str[100];
 | 
				
			||||||
	uint16 index;
 | 
						uint16 index;
 | 
				
			||||||
	NetworkClientInfo *ci;
 | 
						NetworkClientInfo *ci;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	index = NetworkRecv_uint16(MY_CLIENT, p);
 | 
						index = NetworkRecv_uint16(MY_CLIENT, p);
 | 
				
			||||||
	errorno = NetworkRecv_uint8(MY_CLIENT, p);
 | 
						GetNetworkErrorMsg(str, NetworkRecv_uint8(MY_CLIENT, p));
 | 
				
			||||||
 | 
					 | 
				
			||||||
	GetString(str, STR_NETWORK_ERR_CLIENT_GENERAL + errorno);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ci = NetworkFindClientInfoFromIndex(index);
 | 
						ci = NetworkFindClientInfoFromIndex(index);
 | 
				
			||||||
	if (ci != NULL) {
 | 
						if (ci != NULL) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -229,6 +229,7 @@ NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index);
 | 
				
			|||||||
NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip);
 | 
					NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip);
 | 
				
			||||||
NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index);
 | 
					NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index);
 | 
				
			||||||
unsigned long NetworkResolveHost(const char *hostname);
 | 
					unsigned long NetworkResolveHost(const char *hostname);
 | 
				
			||||||
 | 
					char *GetNetworkErrorMsg(char *buf, NetworkErrorCode err);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* ENABLE_NETWORK */
 | 
					#endif /* ENABLE_NETWORK */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,24 +26,6 @@
 | 
				
			|||||||
static void NetworkHandleCommandQueue(NetworkClientState* cs);
 | 
					static void NetworkHandleCommandQueue(NetworkClientState* cs);
 | 
				
			||||||
void NetworkPopulateCompanyInfo(void);
 | 
					void NetworkPopulateCompanyInfo(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* List of possible network errors, used by PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
 | 
					 | 
				
			||||||
static const StringID _network_error_strings[] = {
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_GENERAL,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_DESYNC,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_SAVEGAME,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_CONNECTION_LOST,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_PROTOCOL_ERROR,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_NOT_AUTHORIZED,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_NOT_EXPECTED,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_WRONG_REVISION,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_NAME_IN_USE,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_WRONG_PASSWORD,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_PLAYER_MISMATCH,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_KICKED,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_CHEATER,
 | 
					 | 
				
			||||||
	STR_NETWORK_ERR_CLIENT_SERVER_FULL,
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// **********
 | 
					// **********
 | 
				
			||||||
// Sending functions
 | 
					// Sending functions
 | 
				
			||||||
//   DEF_SERVER_SEND_COMMAND has parameter: NetworkClientState *cs
 | 
					//   DEF_SERVER_SEND_COMMAND has parameter: NetworkClientState *cs
 | 
				
			||||||
@@ -162,12 +144,10 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientState *cs, Netwo
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR);
 | 
						Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (error >= lengthof(_network_error_strings)) error = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	NetworkSend_uint8(p, error);
 | 
						NetworkSend_uint8(p, error);
 | 
				
			||||||
	NetworkSend_Packet(p, cs);
 | 
						NetworkSend_Packet(p, cs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	GetString(str, _network_error_strings[error]);
 | 
						GetNetworkErrorMsg(str, error);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Only send when the current client was in game
 | 
						// Only send when the current client was in game
 | 
				
			||||||
	if (cs->status > STATUS_AUTH) {
 | 
						if (cs->status > STATUS_AUTH) {
 | 
				
			||||||
@@ -901,8 +881,8 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
 | 
				
			|||||||
	// This packets means a client noticed an error and is reporting this
 | 
						// This packets means a client noticed an error and is reporting this
 | 
				
			||||||
	//  to us. Display the error and report it to the other clients
 | 
						//  to us. Display the error and report it to the other clients
 | 
				
			||||||
	NetworkClientState *new_cs;
 | 
						NetworkClientState *new_cs;
 | 
				
			||||||
	NetworkErrorCode errorno = NetworkRecv_uint8(cs, p);
 | 
					 | 
				
			||||||
	char str[100];
 | 
						char str[100];
 | 
				
			||||||
 | 
						NetworkErrorCode errorno = NetworkRecv_uint8(cs, p);
 | 
				
			||||||
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
 | 
						char client_name[NETWORK_CLIENT_NAME_LENGTH];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// The client was never joined.. thank the client for the packet, but ignore it
 | 
						// The client was never joined.. thank the client for the packet, but ignore it
 | 
				
			||||||
@@ -913,9 +893,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	NetworkGetClientName(client_name, sizeof(client_name), cs);
 | 
						NetworkGetClientName(client_name, sizeof(client_name), cs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (errorno >= lengthof(_network_error_strings)) errorno = 0;
 | 
						GetNetworkErrorMsg(str, errorno);
 | 
				
			||||||
 | 
					 | 
				
			||||||
	GetString(str, _network_error_strings[errorno]);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	DEBUG(net, 2)("[NET] %s reported an error and is closing his connection (%s)", client_name, str);
 | 
						DEBUG(net, 2)("[NET] %s reported an error and is closing his connection (%s)", client_name, str);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user