Changeset - r11560:52827dc8eb76
[Not reviewed]
master
0 8 0
rubidium - 15 years ago 2009-04-03 12:49:58
rubidium@openttd.org
(svn r15931) -Codechange: let the host and ban lists use of SmallVector.
8 files changed with 41 insertions and 78 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -422,7 +422,6 @@ DEF_CONSOLE_CMD(ConBan)
 

	
 
DEF_CONSOLE_CMD(ConUnBan)
 
{
 
	uint i, index;
 

	
 
	if (argc == 0) {
 
		IConsoleHelp("Unban a client from a network game. Usage: 'unban <ip | client-id>'");
 
@@ -432,15 +431,14 @@ DEF_CONSOLE_CMD(ConUnBan)
 

	
 
	if (argc != 2) return false;
 

	
 
	index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0;
 
	uint index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0;
 
	index--;
 
	uint i = 0;
 

	
 
	for (i = 0; i < lengthof(_network_ban_list); i++) {
 
		if (_network_ban_list[i] == NULL) continue;
 

	
 
	for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) {
 
		if (strcmp(_network_ban_list[i], argv[1]) == 0 || index == i) {
 
			free(_network_ban_list[i]);
 
			_network_ban_list[i] = NULL;
 
			_network_ban_list.Erase(iter);
 
			IConsolePrint(CC_DEFAULT, "IP unbanned.");
 
			return true;
 
		}
 
@@ -452,8 +450,6 @@ DEF_CONSOLE_CMD(ConUnBan)
 

	
 
DEF_CONSOLE_CMD(ConBanList)
 
{
 
	uint i;
 

	
 
	if (argc == 0) {
 
		IConsoleHelp("List the IP's of banned clients: Usage 'banlist'");
 
		return true;
 
@@ -461,9 +457,9 @@ DEF_CONSOLE_CMD(ConBanList)
 

	
 
	IConsolePrint(CC_DEFAULT, "Banlist: ");
 

	
 
	for (i = 0; i < lengthof(_network_ban_list); i++) {
 
		if (_network_ban_list[i] != NULL)
 
			IConsolePrintF(CC_DEFAULT, "  %d) %s", i + 1, _network_ban_list[i]);
 
	uint i = 1;
 
	for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) {
 
		IConsolePrintF(CC_DEFAULT, "  %d) %s", i, *iter);
 
	}
 

	
 
	return true;
src/core/smallvec_type.hpp
Show inline comments
 
@@ -267,4 +267,6 @@ public:
 
	}
 
};
 

	
 
typedef AutoFreeSmallVector<char*, 4> StringList;
 

	
 
#endif /* SMALLVEC_TYPE_HPP */
src/network/network.cpp
Show inline comments
 
@@ -55,8 +55,8 @@ ClientID _redirect_console_to_client;
 
bool _network_need_advertise;
 
uint32 _network_last_advertise_frame;
 
uint8 _network_reconnect;
 
char *_network_host_list[10];
 
char *_network_ban_list[25];
 
StringList _network_host_list;
 
StringList _network_ban_list;
 
uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
 
uint32 _frame_counter_max; // To where we may go with our clients
 
uint32 _frame_counter;
 
@@ -492,11 +492,9 @@ static void NetworkAcceptClients()
 

	
 
		/* Check if the client is banned */
 
		banned = false;
 
		for (i = 0; i < lengthof(_network_ban_list); i++) {
 
			if (_network_ban_list[i] == NULL) continue;
 

	
 
		for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++) {
 
			/* Check for CIDR separator */
 
			char *chr_cidr = strchr(_network_ban_list[i], '/');
 
			char *chr_cidr = strchr(*iter, '/');
 
			if (chr_cidr != NULL) {
 
				int cidr = atoi(chr_cidr + 1);
 

	
 
@@ -505,7 +503,7 @@ static void NetworkAcceptClients()
 

	
 
				/* Remove and then replace the / so that inet_addr() works on the IP portion */
 
				*chr_cidr = '\0';
 
				uint32 ban_ip = inet_addr(_network_ban_list[i]);
 
				uint32 ban_ip = inet_addr(*iter);
 
				*chr_cidr = '/';
 

	
 
				/* Convert CIDR to mask in network format */
 
@@ -513,14 +511,14 @@ static void NetworkAcceptClients()
 
				if ((sin.sin_addr.s_addr & mask) == (ban_ip & mask)) banned = true;
 
			} else {
 
				/* No CIDR used, so just perform a simple IP test */
 
				if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) banned = true;
 
				if (sin.sin_addr.s_addr == inet_addr(*iter)) banned = true;
 
			}
 

	
 
			if (banned) {
 
				Packet p(PACKET_SERVER_BANNED);
 
				p.PrepareToSend();
 

	
 
				DEBUG(net, 1, "Banned ip tried to join (%s), refused", _network_ban_list[i]);
 
				DEBUG(net, 1, "Banned ip tried to join (%s), refused", *iter);
 

	
 
				send(s, (const char*)p.buffer, p.size, 0);
 
				closesocket(s);
 
@@ -690,19 +688,10 @@ void NetworkAddServer(const char *b)
 
 * by the function that generates the config file. */
 
void NetworkRebuildHostList()
 
{
 
	uint i = 0;
 
	const NetworkGameList *item = _network_game_list;
 
	while (item != NULL && i != lengthof(_network_host_list)) {
 
		if (item->manually) {
 
			free(_network_host_list[i]);
 
			_network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->address.GetPort());
 
		}
 
		item = item->next;
 
	}
 
	_network_host_list.Clear();
 

	
 
	for (; i < lengthof(_network_host_list); i++) {
 
		free(_network_host_list[i]);
 
		_network_host_list[i] = NULL;
 
	for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
 
		if (item->manually) *_network_host_list.Append() = strdup(item->address.GetAddressAsString());
 
	}
 
}
 

	
src/network/network_func.h
Show inline comments
 
@@ -11,6 +11,7 @@
 
#include "network_type.h"
 
#include "../console_type.h"
 
#include "../gfx_type.h"
 
#include "../core/smallvec_type.hpp"
 

	
 
extern NetworkServerGameInfo _network_game_info;
 
extern NetworkCompanyState *_network_company_states;
 
@@ -20,8 +21,8 @@ extern ClientID _redirect_console_to_cli
 
extern bool _network_need_advertise;
 
extern uint32 _network_last_advertise_frame;
 
extern uint8 _network_reconnect;
 
extern char *_network_host_list[10];
 
extern char *_network_ban_list[25];
 
extern StringList _network_host_list;
 
extern StringList _network_ban_list;
 

	
 
byte NetworkSpectatorCount();
 
void NetworkUpdateClientName();
src/network/network_gamelist.cpp
Show inline comments
 
@@ -50,7 +50,7 @@ static void NetworkGameListHandleDelayed
 
				strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
 
				item->online = false;
 
			}
 
			item->manually = ins_item->manually;
 
			item->manually |= ins_item->manually;
 
			UpdateNetworkGameWindow(false);
 
		}
 
		free(ins_item);
src/network/network_gui.cpp
Show inline comments
 
@@ -951,13 +951,11 @@ void ShowNetworkGameWindow()
 

	
 
	/* Only show once */
 
	if (first) {
 
		char * const *srv;
 

	
 
		first = false;
 
		/* add all servers from the config file to our list */
 
		for (srv = &_network_host_list[0]; srv != endof(_network_host_list) && *srv != NULL; srv++) {
 
			NetworkAddServer(*srv);
 
		}
 
		for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
 
			NetworkAddServer(*iter);
 
  }
 
	}
 

	
 
	new NetworkGameWindow(&_network_game_window_desc);
src/network/network_server.cpp
Show inline comments
 
@@ -1798,12 +1798,7 @@ void NetworkServerBanIP(const char *bani
 
	}
 

	
 
	/* Add user to ban-list */
 
	for (uint index = 0; index < lengthof(_network_ban_list); index++) {
 
		if (_network_ban_list[index] == NULL) {
 
			_network_ban_list[index] = strdup(banip);
 
			break;
 
		}
 
	}
 
	*_network_ban_list.Append() = strdup(banip);
 
}
 

	
 
bool NetworkCompanyHasClients(CompanyID company)
src/settings.cpp
Show inline comments
 
@@ -68,9 +68,8 @@ ClientSettings _settings_client;
 
GameSettings _settings_game;
 
GameSettings _settings_newgame;
 

	
 
typedef const char *SettingListCallbackProc(const IniItem *item, uint index);
 
typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
 
typedef void SettingDescProcList(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc);
 
typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
 

	
 
static bool IsSignedVarMemType(VarType vt);
 

	
 
@@ -598,27 +597,18 @@ static void ini_save_settings(IniFile *i
 
 * @param ini IniFile handle to the ini file with the source data
 
 * @param grpname character string identifying the section-header of the ini
 
 * file that will be parsed
 
 * @param list pointer to an string(pointer) array that will store the parsed
 
 * entries of the given section
 
 * @param len the maximum number of items available for the above list
 
 * @param proc callback function that can override how the values are stored
 
 * inside the list */
 
static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc)
 
 */
 
static void ini_load_setting_list(IniFile *ini, const char *grpname, StringList *list)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 
	IniItem *item;
 
	const char *entry;
 
	uint i, j;
 

	
 
	if (group == NULL) return;
 
	if (group == NULL || list == NULL) return;
 

	
 
	for (i = j = 0, item = group->item; item != NULL; item = item->next) {
 
		entry = (proc != NULL) ? proc(item, i++) : item->name;
 
	list->Clear();
 

	
 
		if (entry == NULL || list == NULL) continue;
 

	
 
		if (j == len) break;
 
		list[j++] = strdup(entry);
 
	for (const IniItem *item = group->item; item != NULL; item = item->next) {
 
		if (item->name != NULL) *list->Append() = strdup(item->name);
 
	}
 
}
 

	
 
@@ -629,24 +619,16 @@ static void ini_load_setting_list(IniFil
 
 * @param grpname character string identifying the section-header of the ini file
 
 * @param list pointer to an string(pointer) array that will be used as the
 
 *             source to be saved into the relevant ini section
 
 * @param len the maximum number of items available for the above list
 
 * @param proc callback function that can will provide the source data if defined */
 
static void ini_save_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc)
 
 */
 
static void ini_save_setting_list(IniFile *ini, const char *grpname, StringList *list)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 
	const char *entry;
 
	uint i;
 

	
 
	if (proc == NULL && list == NULL) return;
 
	if (group == NULL) return;
 
	if (group == NULL || list == NULL) return;
 
	group->Clear();
 

	
 
	for (i = 0; i != len; i++) {
 
		entry = (proc != NULL) ? proc(NULL, i) : list[i];
 

	
 
		if (entry == NULL || *entry == '\0') continue;
 

	
 
		group->GetItem(entry, true)->SetValue("");
 
	for (char **iter = list->Begin(); iter != list->End(); iter++) {
 
		group->GetItem(*iter, true)->SetValue("");
 
	}
 
}
 

	
 
@@ -1295,8 +1277,8 @@ static void HandleSettingDescs(IniFile *
 
	proc(ini, _currency_settings,"currency", &_custom_currency);
 

	
 
#ifdef ENABLE_NETWORK
 
	proc_list(ini, "servers", _network_host_list, lengthof(_network_host_list), NULL);
 
	proc_list(ini, "bans",    _network_ban_list,  lengthof(_network_ban_list), NULL);
 
	proc_list(ini, "servers", &_network_host_list);
 
	proc_list(ini, "bans",    &_network_ban_list);
 
#endif /* ENABLE_NETWORK */
 
}
 

	
0 comments (0 inline, 0 general)