From a11f46fed48fa6606f2d84c8f96d79efbad83197 Mon Sep 17 00:00:00 2001 From: truelight Date: Sun, 2 Jan 2005 12:03:43 +0000 Subject: (svn r1322) -Add: banning system (mostly tnx to guru3) A server can ban people via ClientList Both server and dedicated can do it via console: 'ban', 'unban', 'banlist'. --- console_cmds.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lang/english.txt | 1 + network.c | 44 +++++++++++++++++++++++----- network.h | 2 ++ network_client.c | 10 +++++++ network_data.h | 2 ++ network_gui.c | 21 ++++++++++---- network_server.c | 1 + settings.c | 4 ++- 9 files changed, 160 insertions(+), 13 deletions(-) diff --git a/console_cmds.c b/console_cmds.c index b929cf013..2b56d4f24 100644 --- a/console_cmds.c +++ b/console_cmds.c @@ -144,6 +144,87 @@ DEF_CONSOLE_CMD(ConScrollToTile) // ********************************* // #ifdef ENABLE_NETWORK +DEF_CONSOLE_CMD(ConBan) +{ + NetworkClientInfo *ci; + + if (argc == 2) { + uint32 index = atoi(argv[1]); + if (index == NETWORK_SERVER_INDEX && !_network_dedicated) { + IConsolePrint(_iconsole_color_default, "Silly boy, you can not ban yourself!"); + return NULL; + } + if (index == 0) { + IConsoleError("Invalid Client-ID"); + return NULL; + } + + ci = NetworkFindClientInfoFromIndex(index); + + if (ci != NULL) { + uint i; + /* Add user to ban-list */ + for (i = 0; i < lengthof(_network_ban_list); i++) { + if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') { + _network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ci->client_ip)); + break; + } + } + + SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED); + return NULL; + } else { + IConsoleError("Client-ID not found"); + return NULL; + } + } + + IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: ban . For client-ids, see 'clients'."); + + return NULL; +} + +DEF_CONSOLE_CMD(ConUnBan) +{ + if (argc == 2) { + uint i; + for (i = 0; i < lengthof(_network_ban_list); i++) { + if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') + continue; + + if (strncmp(_network_ban_list[i], argv[1], strlen(_network_ban_list[i])) == 0) { + _network_ban_list[i][0] = '\0'; + IConsolePrint(_iconsole_color_default, "IP unbanned."); + return NULL; + } + } + + IConsolePrint(_iconsole_color_default, "IP not in ban-list."); + + return NULL; + } + + IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: unban ."); + + return NULL; +} + +DEF_CONSOLE_CMD(ConBanList) +{ + uint i; + + IConsolePrint(_iconsole_color_default, "Banlist: "); + + for (i = 0; i < lengthof(_network_ban_list); i++) { + if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') + continue; + + IConsolePrintF(_iconsole_color_default, " %d) %s", i + 1, _network_ban_list[i]); + } + + return NULL; +} + DEF_CONSOLE_CMD(ConStatus) { const char *status; @@ -978,6 +1059,13 @@ void IConsoleStdLibRegister(void) IConsoleCmdHook("status", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient); IConsoleCmdHook("resetengines", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetwork); + IConsoleCmdRegister("ban", ConBan); + IConsoleCmdHook("ban", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient); + IConsoleCmdRegister("unban", ConUnBan); + IConsoleCmdHook("unban", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient); + IConsoleCmdRegister("banlist", ConBanList); + IConsoleCmdHook("banlist", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient); + IConsoleAliasRegister("clean_company", "reset_company %A"); IConsoleVarRegister("net_frame_freq", &_network_frame_freq, ICONSOLE_VAR_UINT8); diff --git a/lang/english.txt b/lang/english.txt index 78d8c881b..f12cb61ea 100644 --- a/lang/english.txt +++ b/lang/english.txt @@ -1343,6 +1343,7 @@ STR_NETWORK_ERR_SERVER_ERROR :{WHITE} A protocol-error was made and the conn STR_NETWORK_ERR_WRONG_REVISION :{WHITE} The revision of this client does not match the server's revision STR_NETWORK_ERR_WRONG_PASSWORD :{WHITE} Wrong password STR_NETWORK_ERR_SERVER_FULL :{WHITE} The server is full +STR_NETWORK_ERR_SERVER_BANNED :{WHITE} You are banned from this server STR_NETWORK_ERR_KICKED :{WHITE} You were kicked out of the game STR_NETWORK_ERR_CHEATER :{WHITE} Cheating is not allowed on this server diff --git a/network.c b/network.c index 5f4b82815..c26969860 100644 --- a/network.c +++ b/network.c @@ -219,7 +219,8 @@ static void NetworkClientError(byte res, NetworkClientState *cs) { default: errorno = NETWORK_ERROR_GENERAL; } // This means we fucked up and the server closed the connection - if (res != NETWORK_RECV_STATUS_SERVER_ERROR && res != NETWORK_RECV_STATUS_SERVER_FULL) { + if (res != NETWORK_RECV_STATUS_SERVER_ERROR && res != NETWORK_RECV_STATUS_SERVER_FULL && + res != NETWORK_RECV_STATUS_SERVER_BANNED) { SEND_COMMAND(PACKET_CLIENT_ERROR)(errorno); // Dequeue all commands before closing the socket @@ -615,6 +616,8 @@ static void NetworkAcceptClients(void) #else LONG sin_len; // for some reason we need a 'LONG' under MorphOS #endif + int i; + bool banned; // Should never ever happen.. is it possible?? assert(_listensocket != INVALID_SOCKET); @@ -639,6 +642,33 @@ static void NetworkAcceptClients(void) {int b = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b));} #endif + /* Check if the client is banned */ + banned = false; + for (i = 0; i < lengthof(_network_ban_list); i++) { + if (_network_ban_list[i] == NULL) + continue; + + if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) { + Packet *p = NetworkSend_Init(PACKET_SERVER_BANNED); + + DEBUG(net, 1)("[NET] Banned ip tried to join (%s), refused", _network_ban_list[i]); + + p->buffer[0] = p->size & 0xFF; + p->buffer[1] = p->size >> 8; + + send(s, p->buffer, p->size, 0); + closesocket(s); + + free(p); + + banned = true; + break; + } + } + /* If this client is banned, continue with next client */ + if (banned) + continue; + cs = NetworkAllocClient(s); if (cs == NULL) { // no more clients allowed? @@ -844,15 +874,15 @@ void NetworkAddServer(const byte *b) * by the function that generates the config file. */ void NetworkRebuildHostList() { - int i=0; + int i = 0; NetworkGameList *item = _network_game_list; - while (item != NULL && i!=lengthof(_network_host_list)) { + while (item != NULL && i != lengthof(_network_host_list)) { if (item->manually) _network_host_list[i++] = strdup(item->info.hostname); item = item->next; } - for (; iclient_ip; + + for (i = 0; i < lengthof(_network_ban_list); i++) { + if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') { + _network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ip)); + break; + } + } + + if (client_no < MAX_PLAYERS) + SEND_COMMAND(PACKET_SERVER_ERROR)(&_clients[client_no], NETWORK_ERROR_KICKED); +} static void ClientList_GiveMoney(byte client_no) { @@ -1090,8 +1101,8 @@ static Window *PopupClientList(Window *w, int client_no, int x, int y) GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_KICK); _clientlist_proc[i++] = &ClientList_Kick; -/* sprintf(clientlist_action[i],"Ban"); - clientlist_proc[i++] = &ClientList_Ban;*/ + sprintf(_clientlist_action[i],"Ban"); + _clientlist_proc[i++] = &ClientList_Ban; } if (i == 0) { diff --git a/network_server.c b/network_server.c index 553d518dd..a294ea518 100644 --- a/network_server.c +++ b/network_server.c @@ -1069,6 +1069,7 @@ typedef void NetworkServerPacket(NetworkClientState *cs, Packet *p); // packet is found. static NetworkServerPacket* const _network_server_packet[] = { NULL, /*PACKET_SERVER_FULL,*/ + NULL, /*PACKET_SERVER_BANNED,*/ RECEIVE_COMMAND(PACKET_CLIENT_JOIN), NULL, /*PACKET_SERVER_ERROR,*/ RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO), diff --git a/settings.c b/settings.c index 3c9819e3d..816f73dca 100644 --- a/settings.c +++ b/settings.c @@ -123,7 +123,7 @@ static IniGroup *ini_group_alloc(IniFile *ini, const char *grpt, int len) IniGroup *grp = pool_alloc(&ini->pool, sizeof(IniGroup)); grp->ini = ini; grp->name = pool_strdup(&ini->pool, grpt, len); - if(!strcmp(grp->name, "newgrf") || !strcmp(grp->name, "servers") ) + if(!strcmp(grp->name, "newgrf") || !strcmp(grp->name, "servers") || !strcmp(grp->name, "bans") ) grp->type = IGT_LIST; else grp->type = IGT_VARIABLES; @@ -983,6 +983,7 @@ void LoadFromConfig() HandleSettingDescs(ini, load_setting_desc); LoadList(ini, "newgrf", _newgrf_files, lengthof(_newgrf_files)); LoadList(ini, "servers", _network_host_list, lengthof(_network_host_list)); + LoadList(ini, "bans", _network_ban_list, lengthof(_network_ban_list)); ini_free(ini); } @@ -991,6 +992,7 @@ void SaveToConfig() IniFile *ini = ini_load(_config_file); HandleSettingDescs(ini, save_setting_desc); SaveList(ini, "servers", _network_host_list, lengthof(_network_host_list)); + SaveList(ini, "bans", _network_ban_list, lengthof(_network_ban_list)); ini_save(_config_file, ini); ini_free(ini); } -- cgit v1.2.3-70-g09d2