mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-03 21:06:58 +00:00
(svn r19818) -Fix [FS#3784](r16004): kicking clients by IP didn't work
This commit is contained in:
parent
241dd9a9f5
commit
80fd67a314
@ -408,12 +408,59 @@ DEF_CONSOLE_CMD(ConClearBuffer)
|
||||
**********************************/
|
||||
#ifdef ENABLE_NETWORK
|
||||
|
||||
static bool ConKickOrBan(const char *argv, bool ban)
|
||||
{
|
||||
const char *ip = argv;
|
||||
|
||||
if (strchr(argv, '.') == NULL && strchr(argv, ':') == NULL) { // banning with ID
|
||||
ClientID client_id = (ClientID)atoi(argv);
|
||||
|
||||
if (client_id == CLIENT_ID_SERVER) {
|
||||
IConsolePrintF(CC_ERROR, "ERROR: Silly boy, you can not %s yourself!", ban ? "ban" : "kick");
|
||||
return true;
|
||||
}
|
||||
|
||||
NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
|
||||
if (ci == NULL) {
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 (argc != 2) return false;
|
||||
|
||||
return ConKickOrBan(argv[1], false);
|
||||
}
|
||||
|
||||
DEF_CONSOLE_CMD(ConBan)
|
||||
{
|
||||
NetworkClientInfo *ci;
|
||||
const char *banip = NULL;
|
||||
ClientID client_id;
|
||||
|
||||
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'");
|
||||
@ -423,39 +470,7 @@ DEF_CONSOLE_CMD(ConBan)
|
||||
|
||||
if (argc != 2) return false;
|
||||
|
||||
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]);
|
||||
if (ci == NULL) {
|
||||
banip = argv[1];
|
||||
client_id = (ClientID)-1;
|
||||
} else {
|
||||
client_id = ci->client_id;
|
||||
}
|
||||
}
|
||||
|
||||
if (client_id == CLIENT_ID_SERVER) {
|
||||
IConsoleError("Silly boy, you can not ban yourself!");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (client_id == INVALID_CLIENT_ID || (ci == NULL && client_id != (ClientID)-1)) {
|
||||
IConsoleError("Invalid client");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ci != NULL) {
|
||||
IConsolePrint(CC_DEFAULT, "Client banned");
|
||||
banip = GetClientIP(ci);
|
||||
} else {
|
||||
IConsolePrint(CC_DEFAULT, "Client not online, banned IP");
|
||||
}
|
||||
|
||||
NetworkServerBanIP(banip);
|
||||
|
||||
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) {
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user