Changeset - r14312:be27b5f00b31
[Not reviewed]
master
0 6 0
rubidium - 15 years ago 2010-01-21 11:17:40
rubidium@openttd.org
(svn r18875) -Codechange: remove some unneeded bits from the network protocol and improve the naming of some variables
6 files changed with 20 insertions and 27 deletions:
0 comments (0 inline, 0 general)
src/network/core/config.h
Show inline comments
 
@@ -33,15 +33,15 @@ enum {
 
	NETWORK_COMPANY_INFO_VERSION  =    6, ///< What version of company info is this?
 
	NETWORK_MASTER_SERVER_VERSION =    2, ///< What version of master-server-protocol do we use?
 

	
 
	NETWORK_NAME_LENGTH           =   80, ///< The maximum length of the server name and map name, in bytes including '\0'
 
	NETWORK_COMPANY_NAME_LENGTH   =   31, ///< The maximum length of the company name, in bytes including '\0'
 
	NETWORK_HOSTNAME_LENGTH       =   80, ///< The maximum length of the host name, in bytes including '\0'
 
	NETWORK_UNIQUE_ID_LENGTH      =   33, ///< The maximum length of the unique id of the clients, in bytes including '\0'
 
	NETWORK_SERVER_ID_LENGTH      =   33, ///< The maximum length of the network id of the servers, in bytes including '\0'
 
	NETWORK_REVISION_LENGTH       =   15, ///< The maximum length of the revision, in bytes including '\0'
 
	NETWORK_PASSWORD_LENGTH       =   33, ///< The maximum length of the password, in bytes including '\0' (must be >= NETWORK_UNIQUE_ID_LENGTH)
 
	NETWORK_PASSWORD_LENGTH       =   33, ///< The maximum length of the password, in bytes including '\0' (must be >= NETWORK_SERVER_ID_LENGTH)
 
	NETWORK_CLIENTS_LENGTH        =  200, ///< The maximum length for the list of clients that controls a company, in bytes including '\0'
 
	NETWORK_CLIENT_NAME_LENGTH    =   25, ///< The maximum length of a client's name, in bytes including '\0'
 
	NETWORK_RCONCOMMAND_LENGTH    =  500, ///< The maximum length of a rconsole command, in bytes including '\0'
 
	NETWORK_CHAT_LENGTH           =  900, ///< The maximum length of a chat message, in bytes including '\0'
 

	
 
	NETWORK_GRF_NAME_LENGTH       =   80, ///< Maximum length of the name of a GRF
src/network/network.cpp
Show inline comments
 
@@ -848,13 +848,12 @@ static void NetworkInitGameInfo()
 
	sockaddr_in sock;
 
	memset(&sock, 0, sizeof(sock));
 
	sock.sin_family = AF_INET;
 
	ci->client_address = NetworkAddress((sockaddr*)&sock, sizeof(sock));
 

	
 
	strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
 
	strecpy(ci->unique_id, _settings_client.network.network_id, lastof(ci->unique_id));
 
}
 

	
 
bool NetworkServerStart()
 
{
 
	if (!_network_available) return false;
 

	
 
@@ -1143,31 +1142,31 @@ void NetworkGameLoop()
 
		}
 
	}
 

	
 
	NetworkSend();
 
}
 

	
 
static void NetworkGenerateUniqueId()
 
static void NetworkGenerateServerId()
 
{
 
	Md5 checksum;
 
	uint8 digest[16];
 
	char hex_output[16 * 2 + 1];
 
	char coding_string[NETWORK_NAME_LENGTH];
 
	int di;
 

	
 
	snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Unique ID");
 
	snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
 

	
 
	/* Generate the MD5 hash */
 
	checksum.Append((const uint8*)coding_string, strlen(coding_string));
 
	checksum.Finish(digest);
 

	
 
	for (di = 0; di < 16; ++di) {
 
		sprintf(hex_output + di * 2, "%02x", digest[di]);
 
	}
 

	
 
	/* _network_unique_id is our id */
 
	/* _settings_client.network.network_id is our id */
 
	snprintf(_settings_client.network.network_id, sizeof(_settings_client.network.network_id), "%s", hex_output);
 
}
 

	
 
void NetworkStartDebugLog(NetworkAddress address)
 
{
 
	extern SOCKET _debug_socket;  // Comes from debug.c
 
@@ -1194,14 +1193,14 @@ void NetworkStartUp()
 
	_network_available = NetworkCoreInitialize();;
 
	_network_dedicated = false;
 
	_network_last_advertise_frame = 0;
 
	_network_need_advertise = true;
 
	_network_advertise_retries = 0;
 

	
 
	/* Generate an unique id when there is none yet */
 
	if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateUniqueId();
 
	/* Generate an server id when there is none yet */
 
	if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
 

	
 
	memset(&_network_game_info, 0, sizeof(_network_game_info));
 

	
 
	NetworkInitialize();
 
	DEBUG(net, 3, "[core] network online, multiplayer available");
 
	NetworkFindBroadcastIPs(&_broadcast_list);
src/network/network_base.h
Show inline comments
 
@@ -26,13 +26,12 @@ struct NetworkClientInfo : NetworkClient
 
	ClientID client_id;                             ///< Client identifier (same as ClientState->client_id)
 
	char client_name[NETWORK_CLIENT_NAME_LENGTH];   ///< Name of the client
 
	byte client_lang;                               ///< The language of the client
 
	CompanyID client_playas;                        ///< As which company is this client playing (CompanyID)
 
	NetworkAddress client_address;                  ///< IP-address of the client (so he can be banned)
 
	Date join_date;                                 ///< Gamedate the client has joined
 
	char unique_id[NETWORK_UNIQUE_ID_LENGTH];       ///< Every play sends an unique id so we can indentify him
 

	
 
	NetworkClientInfo(ClientID client_id = INVALID_CLIENT_ID) : client_id(client_id) {}
 
	~NetworkClientInfo();
 
};
 

	
 
#define FOR_ALL_CLIENT_INFOS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientInfo, clientinfo_index, var, start)
src/network/network_client.cpp
Show inline comments
 
@@ -39,13 +39,13 @@
 

	
 
static uint32 last_ack_frame;
 

	
 
/** One bit of 'entropy' used to generate a salt for the company passwords. */
 
static uint32 _password_game_seed;
 
/** The other bit of 'entropy' used to generate a salt for the company passwords. */
 
static char _password_server_unique_id[NETWORK_UNIQUE_ID_LENGTH];
 
static char _password_server_id[NETWORK_SERVER_ID_LENGTH];
 

	
 
/** Maximum number of companies of the currently joined server. */
 
static uint8 _network_server_max_companies;
 
/** Maximum number of spectators of the currently joined server. */
 
static uint8 _network_server_max_spectators;
 

	
 
@@ -54,34 +54,34 @@ CompanyID _network_join_as;
 

	
 
/** Login password from -p argument */
 
const char *_network_join_server_password = NULL;
 
/** Company password from -P argument */
 
const char *_network_join_company_password = NULL;
 

	
 
/** Make sure the unique ID length is the same as a md5 hash. */
 
assert_compile(NETWORK_UNIQUE_ID_LENGTH == 16 * 2 + 1);
 
/** Make sure the server ID length is the same as a md5 hash. */
 
assert_compile(NETWORK_SERVER_ID_LENGTH == 16 * 2 + 1);
 

	
 
/**
 
 * Generates a hashed password for the company name.
 
 * @param password the password to 'encrypt'.
 
 * @return the hashed password.
 
 */
 
static const char *GenerateCompanyPasswordHash(const char *password)
 
{
 
	if (StrEmpty(password)) return password;
 

	
 
	char salted_password[NETWORK_UNIQUE_ID_LENGTH];
 
	char salted_password[NETWORK_SERVER_ID_LENGTH];
 

	
 
	memset(salted_password, 0, sizeof(salted_password));
 
	snprintf(salted_password, sizeof(salted_password), "%s", password);
 
	/* Add the game seed and the server's unique ID as the salt. */
 
	for (uint i = 0; i < NETWORK_UNIQUE_ID_LENGTH - 1; i++) salted_password[i] ^= _password_server_unique_id[i] ^ (_password_game_seed >> i);
 
	/* Add the game seed and the server's ID as the salt. */
 
	for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) salted_password[i] ^= _password_server_id[i] ^ (_password_game_seed >> i);
 

	
 
	Md5 checksum;
 
	uint8 digest[16];
 
	static char hashed_password[NETWORK_UNIQUE_ID_LENGTH];
 
	static char hashed_password[NETWORK_SERVER_ID_LENGTH];
 

	
 
	/* Generate the MD5 hash */
 
	checksum.Append((const uint8*)salted_password, sizeof(salted_password) - 1);
 
	checksum.Finish(digest);
 

	
 
	for (int di = 0; di < 16; di++) sprintf(hashed_password + di * 2, "%02x", digest[di]);
 
@@ -93,13 +93,13 @@ static const char *GenerateCompanyPasswo
 
/**
 
 * Hash the current company password; used when the server 'company' sets his/her password.
 
 */
 
void HashCurrentCompanyPassword(const char *password)
 
{
 
	_password_game_seed = _settings_game.game_creation.generation_seed;
 
	strecpy(_password_server_unique_id, _settings_client.network.network_id, lastof(_password_server_unique_id));
 
	strecpy(_password_server_id, _settings_client.network.network_id, lastof(_password_server_id));
 

	
 
	const char *new_pw = GenerateCompanyPasswordHash(password);
 
	strecpy(_network_company_states[_local_company].password, new_pw, lastof(_network_company_states[_local_company].password));
 

	
 
	if (_network_server) {
 
		NetworkServerUpdateCompanyPassworded(_local_company, !StrEmpty(_network_company_states[_local_company].password));
 
@@ -135,25 +135,23 @@ DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_JO
 
	 * Function: Try to join the server
 
	 * Data:
 
	 *    String: OpenTTD Revision (norev000 if no revision)
 
	 *    String: Client Name (max NETWORK_NAME_LENGTH)
 
	 *    uint8:  Play as Company id (1..MAX_COMPANIES)
 
	 *    uint8:  Language ID
 
	 *    String: Unique id to find the client back in server-listing
 
	 */
 

	
 
	Packet *p;
 
	_network_join_status = NETWORK_JOIN_STATUS_AUTHORIZING;
 
	SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	p = new Packet(PACKET_CLIENT_JOIN);
 
	p->Send_string(_openttd_revision);
 
	p->Send_string(_settings_client.network.client_name); // Client name
 
	p->Send_uint8 (_network_join_as);     // PlayAs
 
	p->Send_uint8 (NETLANG_ANY);          // Language
 
	p->Send_string(_settings_client.network.network_id);
 
	MY_CLIENT->Send_Packet(p);
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
 
{
 
	/*
 
@@ -529,13 +527,13 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 
	const char *password = _network_join_server_password;
 

	
 
	switch (type) {
 
		case NETWORK_COMPANY_PASSWORD:
 
			/* Initialize the password hash salting variables. */
 
			_password_game_seed = p->Recv_uint32();
 
			p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id));
 
			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);
 
@@ -551,13 +549,13 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
 
{
 
	_network_own_client_id = (ClientID)p->Recv_uint32();
 

	
 
	/* Initialize the password hash salting variables, even if they were previously. */
 
	_password_game_seed = p->Recv_uint32();
 
	p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id));
 
	p->Recv_string(_password_server_id, sizeof(_password_server_id));
 

	
 
	/* Start receiving the map */
 
	SEND_COMMAND(PACKET_CLIENT_GETMAP)();
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
src/network/network_server.cpp
Show inline comments
 
@@ -655,13 +655,12 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
		/* Illegal call, return error and ignore the packet */
 
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	char name[NETWORK_CLIENT_NAME_LENGTH];
 
	char unique_id[NETWORK_UNIQUE_ID_LENGTH];
 
	NetworkClientInfo *ci;
 
	CompanyID playas;
 
	NetworkLanguage client_lang;
 
	char client_revision[NETWORK_REVISION_LENGTH];
 

	
 
	p->Recv_string(client_revision, sizeof(client_revision));
 
@@ -673,13 +672,12 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	p->Recv_string(name, sizeof(name));
 
	playas = (Owner)p->Recv_uint8();
 
	client_lang = (NetworkLanguage)p->Recv_uint8();
 
	p->Recv_string(unique_id, sizeof(unique_id));
 

	
 
	if (cs->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
 

	
 
	/* join another company does not affect these values */
 
	switch (playas) {
 
		case COMPANY_NEW_COMPANY: // New company
 
@@ -711,13 +709,12 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	ci = cs->GetInfo();
 

	
 
	strecpy(ci->client_name, name, lastof(ci->client_name));
 
	strecpy(ci->unique_id, unique_id, lastof(ci->unique_id));
 
	ci->client_playas = playas;
 
	ci->client_lang = client_lang;
 

	
 
	/* Make sure companies to which people try to join are not autocleaned */
 
	if (Company::IsValidID(playas)) _network_company_states[playas].months_empty = 0;
 

	
 
@@ -1730,16 +1727,16 @@ void NetworkServerShowStatusToConsole()
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
		int lag = NetworkCalculateLag(cs);
 
		NetworkClientInfo *ci = cs->GetInfo();
 
		const char *status;
 

	
 
		status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
 
		IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  status: '%s'  frame-lag: %3d  company: %1d  IP: %s  unique-id: '%s'",
 
		IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  status: '%s'  frame-lag: %3d  company: %1d  IP: %s",
 
			cs->client_id, ci->client_name, status, lag,
 
			ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
 
			GetClientIP(ci), ci->unique_id);
 
			GetClientIP(ci));
 
	}
 
}
 

	
 
/**
 
 * Send Config Update
 
 */
src/settings_type.h
Show inline comments
 
@@ -130,13 +130,13 @@ struct NetworkSettings {
 
	char   rcon_password[NETWORK_PASSWORD_LENGTH];        ///< passowrd for rconsole (server side)
 
	bool   server_advertise;                              ///< advertise the server to the masterserver
 
	uint8  lan_internet;                                  ///< search on the LAN or internet for servers
 
	char   client_name[NETWORK_CLIENT_NAME_LENGTH];       ///< name of the player (as client)
 
	char   default_company_pass[NETWORK_PASSWORD_LENGTH]; ///< default password for new companies in encrypted form
 
	char   connect_to_ip[NETWORK_HOSTNAME_LENGTH];        ///< default for the "Add server" query
 
	char   network_id[NETWORK_UNIQUE_ID_LENGTH];          ///< semi-unique ID of the client
 
	char   network_id[NETWORK_SERVER_ID_LENGTH];          ///< network ID for servers
 
	bool   autoclean_companies;                           ///< automatically remove companies that are not in use
 
	uint8  autoclean_unprotected;                         ///< remove passwordless companies after this many months
 
	uint8  autoclean_protected;                           ///< remove the password from passworded companies after this many months
 
	uint8  autoclean_novehicles;                          ///< remove companies with no vehicles after this many months
 
	uint8  max_companies;                                 ///< maximum amount of companies
 
	uint8  max_clients;                                   ///< maximum amount of clients
0 comments (0 inline, 0 general)