diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/console_cmds.cpp | 119 | ||||
-rw-r--r-- | src/network/network_func.h | 2 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 2 | ||||
-rw-r--r-- | src/network/network_server.cpp | 15 |
4 files changed, 59 insertions, 79 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index df57479e0..7cb96d3d1 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -408,54 +408,69 @@ DEF_CONSOLE_CMD(ConClearBuffer) **********************************/ #ifdef ENABLE_NETWORK -DEF_CONSOLE_CMD(ConBan) +static bool ConKickOrBan(const char *argv, bool ban) { - NetworkClientInfo *ci; - const char *banip = NULL; - ClientID client_id; + const char *ip = argv; - if (argc == 0) { - IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'"); - IConsoleHelp("For client-id's, see the command 'clients'"); - IConsoleHelp("If the client is no longer online, you can still ban his/her IP"); - return true; - } + if (strchr(argv, '.') == NULL && strchr(argv, ':') == NULL) { // banning with ID + ClientID client_id = (ClientID)atoi(argv); - if (argc != 2) return false; + if (client_id == CLIENT_ID_SERVER) { + IConsolePrintF(CC_ERROR, "ERROR: Silly boy, you can not %s yourself!", ban ? "ban" : "kick"); + return true; + } - if (strchr(argv[1], '.') == NULL && strchr(argv[1], ':') == NULL) { // banning with ID - client_id = (ClientID)atoi(argv[1]); - ci = NetworkFindClientInfoFromClientID(client_id); - } else { // banning IP - ci = NetworkFindClientInfoFromIP(argv[1]); + NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id); if (ci == NULL) { - banip = argv[1]; - client_id = (ClientID)-1; - } else { - client_id = ci->client_id; + IConsoleError("Invalid client"); + return true; } + + if (!ban) { + /* Kick only this client, not all clients with that IP */ + NetworkServerKickClient(client_id); + return true; + } + + /* When banning, kick+ban all clients with that IP */ + ip = GetClientIP(ci); } - if (client_id == CLIENT_ID_SERVER) { - IConsoleError("Silly boy, you can not ban yourself!"); - return true; + uint n = NetworkServerKickOrBanIP(ip, ban); + if (n == 0) { + IConsolePrint(CC_DEFAULT, ban ? "Client not online, address added to banlist" : "Client not found"); + } else { + IConsolePrintF(CC_DEFAULT, "%sed %u client(s)", ban ? "Bann" : "Kick", n); } - if (client_id == INVALID_CLIENT_ID || (ci == NULL && client_id != (ClientID)-1)) { - IConsoleError("Invalid client"); + return true; +} + +DEF_CONSOLE_CMD(ConKick) +{ + if (argc == 0) { + IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'"); + IConsoleHelp("For client-id's, see the command 'clients'"); return true; } - if (ci != NULL) { - IConsolePrint(CC_DEFAULT, "Client banned"); - banip = GetClientIP(ci); - } else { - IConsolePrint(CC_DEFAULT, "Client not online, banned IP"); + if (argc != 2) return false; + + return ConKickOrBan(argv[1], false); +} + +DEF_CONSOLE_CMD(ConBan) +{ + if (argc == 0) { + IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'"); + IConsoleHelp("For client-id's, see the command 'clients'"); + IConsoleHelp("If the client is no longer online, you can still ban his/her IP"); + return true; } - NetworkServerBanIP(banip); + if (argc != 2) return false; - return true; + return ConKickOrBan(argv[1], true); } DEF_CONSOLE_CMD(ConUnBan) @@ -612,46 +627,6 @@ DEF_CONSOLE_CMD(ConClientNickChange) return true; } -DEF_CONSOLE_CMD(ConKick) -{ - NetworkClientInfo *ci; - ClientID client_id; - - if (argc == 0) { - IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'"); - IConsoleHelp("For client-id's, see the command 'clients'"); - return true; - } - - if (argc != 2) return false; - - if (strchr(argv[1], '.') == NULL) { - client_id = (ClientID)atoi(argv[1]); - ci = NetworkFindClientInfoFromClientID(client_id); - } else { - ci = NetworkFindClientInfoFromIP(argv[1]); - client_id = (ci == NULL) ? INVALID_CLIENT_ID : ci->client_id; - } - - if (client_id == CLIENT_ID_SERVER) { - IConsoleError("Silly boy, you can not kick yourself!"); - return true; - } - - if (client_id == INVALID_CLIENT_ID) { - IConsoleError("Invalid client"); - return true; - } - - if (ci != NULL) { - NetworkServerKickClient(client_id); - } else { - IConsoleError("Client not found"); - } - - return true; -} - DEF_CONSOLE_CMD(ConJoinCompany) { if (argc < 2) { diff --git a/src/network/network_func.h b/src/network/network_func.h index 72539e501..70fe3afb7 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -79,7 +79,7 @@ void NetworkServerSendError(ClientID client_id, NetworkErrorCode error); void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, ClientID from_id, int64 data = 0); void NetworkServerKickClient(ClientID client_id); -void NetworkServerBanIP(const char *banip); +uint NetworkServerKickOrBanIP(const char *ip, bool ban); void NetworkInitChatMessage(); void CDECL NetworkAddChatMessage(TextColour colour, uint8 duration, const char *message, ...) WARN_FORMAT(3, 4); diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index eb8bb0982..f2c70f1eb 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1786,7 +1786,7 @@ static void ClientList_Ban(byte client_no) if (ci == NULL) return; - NetworkServerBanIP(GetClientIP(ci)); + NetworkServerKickOrBanIP(GetClientIP(ci), true); } static void ClientList_GiveMoney(byte client_no) diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 6f1f3b4b5..b75489f2a 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1808,19 +1808,24 @@ void NetworkServerKickClient(ClientID client_id) NetworkServerSendError(client_id, NETWORK_ERROR_KICKED); } -void NetworkServerBanIP(const char *banip) +uint NetworkServerKickOrBanIP(const char *ip, bool ban) { - NetworkClientInfo *ci; + /* Add address to ban-list */ + if (ban) *_network_ban_list.Append() = strdup(ip); + + uint n = 0; /* There can be multiple clients with the same IP, kick them all */ + NetworkClientInfo *ci; FOR_ALL_CLIENT_INFOS(ci) { - if (ci->client_address.IsInNetmask(const_cast<char *>(banip))) { + if (ci->client_id == CLIENT_ID_SERVER) continue; + if (ci->client_address.IsInNetmask(const_cast<char *>(ip))) { NetworkServerKickClient(ci->client_id); + n++; } } - /* Add user to ban-list */ - *_network_ban_list.Append() = strdup(banip); + return n; } bool NetworkCompanyHasClients(CompanyID company) |