Changeset - r14985:6c2d89816318
[Not reviewed]
master
0 5 0
smatz - 15 years ago 2010-04-11 17:17:12
smatz@openttd.org
(svn r19607) -Codechange: use different packet types instead of packet subtypes
5 files changed with 126 insertions and 75 deletions:
0 comments (0 inline, 0 general)
src/network/core/tcp_game.h
Show inline comments
 
@@ -32,14 +32,16 @@ enum {
 
	PACKET_SERVER_BANNED,
 
	PACKET_CLIENT_JOIN,
 
	PACKET_SERVER_ERROR,
 
	PACKET_CLIENT_COMPANY_INFO,
 
	PACKET_SERVER_COMPANY_INFO,
 
	PACKET_SERVER_CLIENT_INFO,
 
	PACKET_SERVER_NEED_PASSWORD,
 
	PACKET_CLIENT_PASSWORD,
 
	PACKET_SERVER_NEED_GAME_PASSWORD,
 
	PACKET_SERVER_NEED_COMPANY_PASSWORD,
 
	PACKET_CLIENT_GAME_PASSWORD,
 
	PACKET_CLIENT_COMPANY_PASSWORD,
 
	PACKET_SERVER_WELCOME,
 
	PACKET_CLIENT_GETMAP,
 
	PACKET_SERVER_WAIT,
 
	PACKET_SERVER_MAP,
 
	PACKET_CLIENT_MAP_OK,
 
	PACKET_SERVER_JOIN,
 
@@ -72,13 +74,14 @@ enum {
 
/** Packet that wraps a command */
 
struct CommandPacket;
 

	
 
/** Status of a client */
 
enum ClientStatus {
 
	STATUS_INACTIVE,   ///< The client is not connected nor active
 
	STATUS_AUTHORIZING,///< The client is authorizing
 
	STATUS_AUTH_GAME,  ///< The client is authorizing with game (server) password
 
	STATUS_AUTH_COMPANY, ///< The client is authorizing with company password
 
	STATUS_AUTH,       ///< The client is authorized
 
	STATUS_MAP_WAIT,   ///< The client is waiting as someone else is downloading the map
 
	STATUS_MAP,        ///< The client is downloading the map
 
	STATUS_DONE_MAP,   ///< The client has downloaded the map
 
	STATUS_PRE_ACTIVE, ///< The client is catching up the delayed frames
 
	STATUS_ACTIVE,     ///< The client is active within in the game
src/network/network_client.cpp
Show inline comments
 
@@ -164,24 +164,38 @@ DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_NE
 

	
 
	Packet *p = new Packet(PACKET_CLIENT_NEWGRFS_CHECKED);
 
	MY_CLIENT->Send_Packet(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_PASSWORD)(NetworkPasswordType type, const char *password)
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_GAME_PASSWORD)(const char *password)
 
{
 
	/*
 
	 * Packet: CLIENT_PASSWORD
 
	 * Packet: CLIENT_GAME_PASSWORD
 
	 * Function: Send a password to the server to authorize
 
	 * Data:
 
	 *    uint8:  NetworkPasswordType
 
	 *    String: Password
 
	 */
 
	Packet *p = new Packet(PACKET_CLIENT_PASSWORD);
 
	p->Send_uint8 (type);
 
	p->Send_string(type == NETWORK_GAME_PASSWORD ? password : GenerateCompanyPasswordHash(password));
 
	Packet *p = new Packet(PACKET_CLIENT_GAME_PASSWORD);
 
	p->Send_string(password);
 
	MY_CLIENT->Send_Packet(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMPANY_PASSWORD)(const char *password)
 
{
 
	/*
 
	 * Packet: CLIENT_COMPANY_PASSWORD
 
	 * Function: Send a password to the server to authorize
 
	 * Data:
 
	 *    uint8:  NetworkPasswordType
 
	 *    String: Password
 
	 */
 
	Packet *p = new Packet(PACKET_CLIENT_COMPANY_PASSWORD);
 
	p->Send_string(GenerateCompanyPasswordHash(password));
 
	MY_CLIENT->Send_Packet(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_GETMAP)
 
{
 
@@ -532,36 +546,38 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 

	
 
	/* NewGRF mismatch, bail out */
 
	_switch_mode_errorstr = STR_NETWORK_ERROR_NEWGRF_MISMATCH;
 
	return ret;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD)
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD)
 
{
 
	NetworkPasswordType type = (NetworkPasswordType)p->Recv_uint8();
 
	const char *password = _network_join_server_password;
 
	if (!StrEmpty(password)) {
 
		return SEND_COMMAND(PACKET_CLIENT_GAME_PASSWORD)(password);
 
	}
 

	
 
	const char *password = _network_join_server_password;
 
	ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
	switch (type) {
 
		case NETWORK_COMPANY_PASSWORD:
 
			/* Initialize the password hash salting variables. */
 
			_password_game_seed = p->Recv_uint32();
 
			p->Recv_string(_password_server_id, sizeof(_password_server_id));
 
			if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 
			password = _network_join_company_password;
 
			/* FALL THROUGH */
 
		case NETWORK_GAME_PASSWORD:
 
			if (StrEmpty(password)) {
 
				ShowNetworkNeedPassword(type);
 
			} else {
 
				return SEND_COMMAND(PACKET_CLIENT_PASSWORD)(type, password);
 
			}
 
			return NETWORK_RECV_STATUS_OKAY;
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD)
 
{
 
	_password_game_seed = p->Recv_uint32();
 
	p->Recv_string(_password_server_id, sizeof(_password_server_id));
 
	if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 

	
 
		default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 
	const char *password = _network_join_company_password;
 
	if (!StrEmpty(password)) {
 
		return SEND_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD)(password);
 
	}
 

	
 
	ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
 
{
 
	_network_own_client_id = (ClientID)p->Recv_uint32();
 

	
 
@@ -918,14 +934,16 @@ static NetworkClientPacket * const _netw
 
	RECEIVE_COMMAND(PACKET_SERVER_BANNED),
 
	NULL, // PACKET_CLIENT_JOIN,
 
	RECEIVE_COMMAND(PACKET_SERVER_ERROR),
 
	NULL, // PACKET_CLIENT_COMPANY_INFO,
 
	RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO),
 
	RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO),
 
	RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD),
 
	NULL, // PACKET_CLIENT_PASSWORD,
 
	RECEIVE_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD),
 
	RECEIVE_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD),
 
	NULL, // PACKET_CLIENT_GAME_PASSWORD,
 
	NULL, // PACKET_CLIENT_COMPANY_PASSWORD,
 
	RECEIVE_COMMAND(PACKET_SERVER_WELCOME),
 
	NULL, // PACKET_CLIENT_GETMAP,
 
	RECEIVE_COMMAND(PACKET_SERVER_WAIT),
 
	RECEIVE_COMMAND(PACKET_SERVER_MAP),
 
	NULL, // PACKET_CLIENT_MAP_OK,
 
	RECEIVE_COMMAND(PACKET_SERVER_JOIN),
src/network/network_client.h
Show inline comments
 
@@ -19,13 +19,14 @@
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_GAME_INFO);
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMMAND)(const CommandPacket *cp);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_ERROR)(NetworkErrorCode errorno);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_QUIT)();
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType desttype, int dest, const char *msg, int64 data);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_PASSWORD)(NetworkPasswordType type, const char *password);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_GAME_PASSWORD)(const char *password);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMPANY_PASSWORD)(const char *password);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_PASSWORD)(const char *password);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_NAME)(const char *name);
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_ACK);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_RCON)(const char *pass, const char *command);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_MOVE)(CompanyID company, const char *pass);
 

	
src/network/network_gui.cpp
Show inline comments
 
@@ -2207,14 +2207,19 @@ struct NetworkJoinStatusWindow : Window 
 

	
 
	virtual void OnQueryTextFinished(char *str)
 
	{
 
		if (StrEmpty(str)) {
 
			NetworkDisconnect();
 
			ShowNetworkGameWindow();
 
		} else {
 
			SEND_COMMAND(PACKET_CLIENT_PASSWORD)(this->password_type, str);
 
			return;
 
		}
 

	
 
		switch (this->password_type) {
 
			case NETWORK_GAME_PASSWORD:    SEND_COMMAND(PACKET_CLIENT_GAME_PASSWORD)   (str); break;
 
			case NETWORK_COMPANY_PASSWORD: SEND_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD)(str); break;
 
			default: NOT_REACHED();
 
		}
 
	}
 
};
 

	
 
static const NWidgetPart _nested_network_join_status_window_widgets[] = {
 
	NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_CONNECTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
src/network/network_server.cpp
Show inline comments
 
@@ -206,28 +206,45 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SER
 
	}
 

	
 
	cs->Send_Packet(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(NetworkClientSocket *cs, NetworkPasswordType type)
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_GAME_PASSWORD)(NetworkClientSocket *cs)
 
{
 
	/*
 
	 * Packet: SERVER_NEED_PASSWORD
 
	 * Function: Indication to the client that the server needs a password
 
	 * Data:
 
	 *    uint8:  Type of password
 
	 * Packet: PACKET_SERVER_NEED_GAME_PASSWORD
 
	 * Function: Indication to the client that the server needs a game password
 
	 */
 

	
 
	/* Invalid packet when status is AUTH or higher */
 
	if (cs->status >= STATUS_AUTH) return NetworkCloseClient(cs, NETWORK_RECV_STATUS_MALFORMED_PACKET);
 
	/* Invalid packet when status is STATUS_AUTH_GAME or higher */
 
	if (cs->status >= STATUS_AUTH_GAME) return NetworkCloseClient(cs, NETWORK_RECV_STATUS_MALFORMED_PACKET);
 

	
 
	cs->status = STATUS_AUTH_GAME;
 

	
 
	Packet *p = new Packet(PACKET_SERVER_NEED_GAME_PASSWORD);
 
	cs->Send_Packet(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
	cs->status = STATUS_AUTHORIZING;
 
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_COMPANY_PASSWORD)(NetworkClientSocket *cs)
 
{
 
	/*
 
	 * Packet: PACKET_SERVER_NEED_COMPANY_PASSWORD
 
	 * Function: Indication to the client that the server needs a company password
 
	 * Data:
 
	 *    uint32:  Generation seed
 
	 *    string:  Network ID of the server
 
	 */
 

	
 
	Packet *p = new Packet(PACKET_SERVER_NEED_PASSWORD);
 
	p->Send_uint8(type);
 
	/* Invalid packet when status is STATUS_AUTH_COMPANY or higher */
 
	if (cs->status >= STATUS_AUTH_COMPANY) return NetworkCloseClient(cs, NETWORK_RECV_STATUS_MALFORMED_PACKET);
 

	
 
	cs->status = STATUS_AUTH_COMPANY;
 

	
 
	Packet *p = new Packet(PACKET_SERVER_NEED_COMPANY_PASSWORD);
 
	p->Send_uint32(_settings_game.game_creation.generation_seed);
 
	p->Send_string(_settings_client.network.network_id);
 
	cs->Send_Packet(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
@@ -644,17 +661,17 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
	}
 

	
 
	NetworkClientInfo *ci = cs->GetInfo();
 

	
 
	/* We now want a password from the client else we do not allow him in! */
 
	if (!StrEmpty(_settings_client.network.server_password)) {
 
		return SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD);
 
		return SEND_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD)(cs);
 
	}
 

	
 
	if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
 
		return SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
 
		return SEND_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD)(cs);
 
	}
 

	
 
	return SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
 
}
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
 
@@ -725,48 +742,53 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
		return RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL);
 
	}
 

	
 
	return SEND_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)(cs);
 
}
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GAME_PASSWORD)
 
{
 
	NetworkPasswordType type;
 
	if (cs->status != STATUS_AUTH_GAME) {
 
		return SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	char password[NETWORK_PASSWORD_LENGTH];
 
	const NetworkClientInfo *ci;
 

	
 
	type = (NetworkPasswordType)p->Recv_uint8();
 
	p->Recv_string(password, sizeof(password));
 

	
 
	if (cs->status == STATUS_AUTHORIZING && type == NETWORK_GAME_PASSWORD) {
 
		/* Check game-password */
 
		if (strcmp(password, _settings_client.network.server_password) != 0) {
 
			/* Password is invalid */
 
			return SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
 
		}
 

	
 
		ci = cs->GetInfo();
 
	/* Check game password */
 
	if (strcmp(password, _settings_client.network.server_password) != 0) {
 
		/* Password is invalid */
 
		return SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
 
	}
 

	
 
		if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
 
			return SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
 
		}
 

	
 
		/* Valid password, allow user */
 
		return SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
 
	} else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) {
 
		ci = cs->GetInfo();
 

	
 
		if (strcmp(password, _network_company_states[ci->client_playas].password) != 0) {
 
			/* Password is invalid */
 
			return SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
 
		}
 

	
 
		return SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
 
	const NetworkClientInfo *ci = cs->GetInfo();
 
	if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
 
		return SEND_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD)(cs);
 
	}
 

	
 
	return SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
 
	/* Valid password, allow user */
 
	return SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
 
}
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD)
 
{
 
	if (cs->status != STATUS_AUTH_COMPANY) {
 
		return SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	char password[NETWORK_PASSWORD_LENGTH];
 
	p->Recv_string(password, sizeof(password));
 

	
 
	/* Check company password */
 
	const NetworkClientInfo *ci = cs->GetInfo();
 
	if (strcmp(password, _network_company_states[ci->client_playas].password) != 0) {
 
		/* Password is invalid */
 
		return SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
 
	}
 

	
 
	return SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
 
}
 

	
 
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
 
{
 
	NetworkClientSocket *new_cs;
 

	
 
@@ -1268,14 +1290,16 @@ static NetworkServerPacket * const _netw
 
	NULL, // PACKET_SERVER_BANNED,
 
	RECEIVE_COMMAND(PACKET_CLIENT_JOIN),
 
	NULL, // PACKET_SERVER_ERROR,
 
	RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO),
 
	NULL, // PACKET_SERVER_COMPANY_INFO,
 
	NULL, // PACKET_SERVER_CLIENT_INFO,
 
	NULL, // PACKET_SERVER_NEED_PASSWORD,
 
	RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD),
 
	NULL, // PACKET_SERVER_NEED_GAME_PASSWORD,
 
	NULL, // PACKET_SERVER_NEED_COMPANY_PASSWORD,
 
	RECEIVE_COMMAND(PACKET_CLIENT_GAME_PASSWORD),
 
	RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD),
 
	NULL, // PACKET_SERVER_WELCOME,
 
	RECEIVE_COMMAND(PACKET_CLIENT_GETMAP),
 
	NULL, // PACKET_SERVER_WAIT,
 
	NULL, // PACKET_SERVER_MAP,
 
	RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK),
 
	NULL, // PACKET_SERVER_JOIN,
0 comments (0 inline, 0 general)