mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-08 23:19:40 +00:00
(svn r15931) -Codechange: let the host and ban lists use of SmallVector.
This commit is contained in:
parent
d84fb358f5
commit
89d0eca6b7
@ -422,7 +422,6 @@ DEF_CONSOLE_CMD(ConBan)
|
|||||||
|
|
||||||
DEF_CONSOLE_CMD(ConUnBan)
|
DEF_CONSOLE_CMD(ConUnBan)
|
||||||
{
|
{
|
||||||
uint i, index;
|
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
IConsoleHelp("Unban a client from a network game. Usage: 'unban <ip | client-id>'");
|
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;
|
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--;
|
index--;
|
||||||
|
uint i = 0;
|
||||||
|
|
||||||
for (i = 0; i < lengthof(_network_ban_list); i++) {
|
for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) {
|
||||||
if (_network_ban_list[i] == NULL) continue;
|
|
||||||
|
|
||||||
if (strcmp(_network_ban_list[i], argv[1]) == 0 || index == i) {
|
if (strcmp(_network_ban_list[i], argv[1]) == 0 || index == i) {
|
||||||
free(_network_ban_list[i]);
|
free(_network_ban_list[i]);
|
||||||
_network_ban_list[i] = NULL;
|
_network_ban_list.Erase(iter);
|
||||||
IConsolePrint(CC_DEFAULT, "IP unbanned.");
|
IConsolePrint(CC_DEFAULT, "IP unbanned.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -452,8 +450,6 @@ DEF_CONSOLE_CMD(ConUnBan)
|
|||||||
|
|
||||||
DEF_CONSOLE_CMD(ConBanList)
|
DEF_CONSOLE_CMD(ConBanList)
|
||||||
{
|
{
|
||||||
uint i;
|
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
IConsoleHelp("List the IP's of banned clients: Usage 'banlist'");
|
IConsoleHelp("List the IP's of banned clients: Usage 'banlist'");
|
||||||
return true;
|
return true;
|
||||||
@ -461,9 +457,9 @@ DEF_CONSOLE_CMD(ConBanList)
|
|||||||
|
|
||||||
IConsolePrint(CC_DEFAULT, "Banlist: ");
|
IConsolePrint(CC_DEFAULT, "Banlist: ");
|
||||||
|
|
||||||
for (i = 0; i < lengthof(_network_ban_list); i++) {
|
uint i = 1;
|
||||||
if (_network_ban_list[i] != NULL)
|
for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) {
|
||||||
IConsolePrintF(CC_DEFAULT, " %d) %s", i + 1, _network_ban_list[i]);
|
IConsolePrintF(CC_DEFAULT, " %d) %s", i, *iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -267,4 +267,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef AutoFreeSmallVector<char*, 4> StringList;
|
||||||
|
|
||||||
#endif /* SMALLVEC_TYPE_HPP */
|
#endif /* SMALLVEC_TYPE_HPP */
|
||||||
|
@ -55,8 +55,8 @@ ClientID _redirect_console_to_client;
|
|||||||
bool _network_need_advertise;
|
bool _network_need_advertise;
|
||||||
uint32 _network_last_advertise_frame;
|
uint32 _network_last_advertise_frame;
|
||||||
uint8 _network_reconnect;
|
uint8 _network_reconnect;
|
||||||
char *_network_host_list[10];
|
StringList _network_host_list;
|
||||||
char *_network_ban_list[25];
|
StringList _network_ban_list;
|
||||||
uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
|
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_max; // To where we may go with our clients
|
||||||
uint32 _frame_counter;
|
uint32 _frame_counter;
|
||||||
@ -492,11 +492,9 @@ static void NetworkAcceptClients()
|
|||||||
|
|
||||||
/* Check if the client is banned */
|
/* Check if the client is banned */
|
||||||
banned = false;
|
banned = false;
|
||||||
for (i = 0; i < lengthof(_network_ban_list); i++) {
|
for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++) {
|
||||||
if (_network_ban_list[i] == NULL) continue;
|
|
||||||
|
|
||||||
/* Check for CIDR separator */
|
/* Check for CIDR separator */
|
||||||
char *chr_cidr = strchr(_network_ban_list[i], '/');
|
char *chr_cidr = strchr(*iter, '/');
|
||||||
if (chr_cidr != NULL) {
|
if (chr_cidr != NULL) {
|
||||||
int cidr = atoi(chr_cidr + 1);
|
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 */
|
/* Remove and then replace the / so that inet_addr() works on the IP portion */
|
||||||
*chr_cidr = '\0';
|
*chr_cidr = '\0';
|
||||||
uint32 ban_ip = inet_addr(_network_ban_list[i]);
|
uint32 ban_ip = inet_addr(*iter);
|
||||||
*chr_cidr = '/';
|
*chr_cidr = '/';
|
||||||
|
|
||||||
/* Convert CIDR to mask in network format */
|
/* 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;
|
if ((sin.sin_addr.s_addr & mask) == (ban_ip & mask)) banned = true;
|
||||||
} else {
|
} else {
|
||||||
/* No CIDR used, so just perform a simple IP test */
|
/* 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) {
|
if (banned) {
|
||||||
Packet p(PACKET_SERVER_BANNED);
|
Packet p(PACKET_SERVER_BANNED);
|
||||||
p.PrepareToSend();
|
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);
|
send(s, (const char*)p.buffer, p.size, 0);
|
||||||
closesocket(s);
|
closesocket(s);
|
||||||
@ -690,19 +688,10 @@ void NetworkAddServer(const char *b)
|
|||||||
* by the function that generates the config file. */
|
* by the function that generates the config file. */
|
||||||
void NetworkRebuildHostList()
|
void NetworkRebuildHostList()
|
||||||
{
|
{
|
||||||
uint i = 0;
|
_network_host_list.Clear();
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; i < lengthof(_network_host_list); i++) {
|
for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
|
||||||
free(_network_host_list[i]);
|
if (item->manually) *_network_host_list.Append() = strdup(item->address.GetAddressAsString());
|
||||||
_network_host_list[i] = NULL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "network_type.h"
|
#include "network_type.h"
|
||||||
#include "../console_type.h"
|
#include "../console_type.h"
|
||||||
#include "../gfx_type.h"
|
#include "../gfx_type.h"
|
||||||
|
#include "../core/smallvec_type.hpp"
|
||||||
|
|
||||||
extern NetworkServerGameInfo _network_game_info;
|
extern NetworkServerGameInfo _network_game_info;
|
||||||
extern NetworkCompanyState *_network_company_states;
|
extern NetworkCompanyState *_network_company_states;
|
||||||
@ -20,8 +21,8 @@ extern ClientID _redirect_console_to_client;
|
|||||||
extern bool _network_need_advertise;
|
extern bool _network_need_advertise;
|
||||||
extern uint32 _network_last_advertise_frame;
|
extern uint32 _network_last_advertise_frame;
|
||||||
extern uint8 _network_reconnect;
|
extern uint8 _network_reconnect;
|
||||||
extern char *_network_host_list[10];
|
extern StringList _network_host_list;
|
||||||
extern char *_network_ban_list[25];
|
extern StringList _network_ban_list;
|
||||||
|
|
||||||
byte NetworkSpectatorCount();
|
byte NetworkSpectatorCount();
|
||||||
void NetworkUpdateClientName();
|
void NetworkUpdateClientName();
|
||||||
|
@ -50,7 +50,7 @@ static void NetworkGameListHandleDelayedInsert()
|
|||||||
strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
|
strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
|
||||||
item->online = false;
|
item->online = false;
|
||||||
}
|
}
|
||||||
item->manually = ins_item->manually;
|
item->manually |= ins_item->manually;
|
||||||
UpdateNetworkGameWindow(false);
|
UpdateNetworkGameWindow(false);
|
||||||
}
|
}
|
||||||
free(ins_item);
|
free(ins_item);
|
||||||
|
@ -951,13 +951,11 @@ void ShowNetworkGameWindow()
|
|||||||
|
|
||||||
/* Only show once */
|
/* Only show once */
|
||||||
if (first) {
|
if (first) {
|
||||||
char * const *srv;
|
|
||||||
|
|
||||||
first = false;
|
first = false;
|
||||||
/* add all servers from the config file to our list */
|
/* add all servers from the config file to our list */
|
||||||
for (srv = &_network_host_list[0]; srv != endof(_network_host_list) && *srv != NULL; srv++) {
|
for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
|
||||||
NetworkAddServer(*srv);
|
NetworkAddServer(*iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
new NetworkGameWindow(&_network_game_window_desc);
|
new NetworkGameWindow(&_network_game_window_desc);
|
||||||
|
@ -1798,12 +1798,7 @@ void NetworkServerBanIP(const char *banip)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Add user to ban-list */
|
/* Add user to ban-list */
|
||||||
for (uint index = 0; index < lengthof(_network_ban_list); index++) {
|
*_network_ban_list.Append() = strdup(banip);
|
||||||
if (_network_ban_list[index] == NULL) {
|
|
||||||
_network_ban_list[index] = strdup(banip);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetworkCompanyHasClients(CompanyID company)
|
bool NetworkCompanyHasClients(CompanyID company)
|
||||||
|
@ -68,9 +68,8 @@ ClientSettings _settings_client;
|
|||||||
GameSettings _settings_game;
|
GameSettings _settings_game;
|
||||||
GameSettings _settings_newgame;
|
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 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);
|
static bool IsSignedVarMemType(VarType vt);
|
||||||
|
|
||||||
@ -598,27 +597,18 @@ static void ini_save_settings(IniFile *ini, const SettingDesc *sd, const char *g
|
|||||||
* @param ini IniFile handle to the ini file with the source data
|
* @param ini IniFile handle to the ini file with the source data
|
||||||
* @param grpname character string identifying the section-header of the ini
|
* @param grpname character string identifying the section-header of the ini
|
||||||
* file that will be parsed
|
* file that will be parsed
|
||||||
* @param list pointer to an string(pointer) array that will store the parsed
|
|
||||||
* entries of the given section
|
* 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
|
static void ini_load_setting_list(IniFile *ini, const char *grpname, StringList *list)
|
||||||
* inside the list */
|
|
||||||
static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc)
|
|
||||||
{
|
{
|
||||||
IniGroup *group = ini->GetGroup(grpname);
|
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) {
|
list->Clear();
|
||||||
entry = (proc != NULL) ? proc(item, i++) : item->name;
|
|
||||||
|
|
||||||
if (entry == NULL || list == NULL) continue;
|
for (const IniItem *item = group->item; item != NULL; item = item->next) {
|
||||||
|
if (item->name != NULL) *list->Append() = strdup(item->name);
|
||||||
if (j == len) break;
|
|
||||||
list[j++] = strdup(entry);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -629,24 +619,16 @@ static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list
|
|||||||
* @param grpname character string identifying the section-header of the ini file
|
* @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
|
* @param list pointer to an string(pointer) array that will be used as the
|
||||||
* source to be saved into the relevant ini section
|
* 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, StringList *list)
|
||||||
static void ini_save_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc)
|
|
||||||
{
|
{
|
||||||
IniGroup *group = ini->GetGroup(grpname);
|
IniGroup *group = ini->GetGroup(grpname);
|
||||||
const char *entry;
|
|
||||||
uint i;
|
|
||||||
|
|
||||||
if (proc == NULL && list == NULL) return;
|
if (group == NULL || list == NULL) return;
|
||||||
if (group == NULL) return;
|
|
||||||
group->Clear();
|
group->Clear();
|
||||||
|
|
||||||
for (i = 0; i != len; i++) {
|
for (char **iter = list->Begin(); iter != list->End(); iter++) {
|
||||||
entry = (proc != NULL) ? proc(NULL, i) : list[i];
|
group->GetItem(*iter, true)->SetValue("");
|
||||||
|
|
||||||
if (entry == NULL || *entry == '\0') continue;
|
|
||||||
|
|
||||||
group->GetItem(entry, true)->SetValue("");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1295,8 +1277,8 @@ static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescP
|
|||||||
proc(ini, _currency_settings,"currency", &_custom_currency);
|
proc(ini, _currency_settings,"currency", &_custom_currency);
|
||||||
|
|
||||||
#ifdef ENABLE_NETWORK
|
#ifdef ENABLE_NETWORK
|
||||||
proc_list(ini, "servers", _network_host_list, lengthof(_network_host_list), NULL);
|
proc_list(ini, "servers", &_network_host_list);
|
||||||
proc_list(ini, "bans", _network_ban_list, lengthof(_network_ban_list), NULL);
|
proc_list(ini, "bans", &_network_ban_list);
|
||||||
#endif /* ENABLE_NETWORK */
|
#endif /* ENABLE_NETWORK */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user