Changeset - r15179:de4f52bbb07f
[Not reviewed]
master
0 4 0
smatz - 14 years ago 2010-05-13 16:00:50
smatz@openttd.org
(svn r19818) -Fix [FS#3784](r16004): kicking clients by IP didn't work
4 files changed with 64 insertions and 84 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -405,60 +405,75 @@ DEF_CONSOLE_CMD(ConClearBuffer)
 

	
 
/**********************************
 
 * Network Core Console Commands
 
 **********************************/
 
#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'");
 
		IConsoleHelp("If the client is no longer online, you can still ban his/her IP");
 
		return true;
 
	}
 

	
 
	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)
 
{
 

	
 
	if (argc == 0) {
 
@@ -609,52 +624,12 @@ DEF_CONSOLE_CMD(ConClientNickChange)
 
		IConsoleError("Cannot give a client a duplicate name");
 
	}
 

	
 
	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) {
 
		IConsoleHelp("Request joining another company. Usage: join <company-id> [<password>]");
 
		IConsoleHelp("For valid company-id see company list, use 255 for spectator");
 
		return true;
src/network/network_func.h
Show inline comments
 
@@ -76,13 +76,13 @@ const char *GetClientIP(NetworkClientInf
 
void NetworkServerDoMove(ClientID client_id, CompanyID company_id);
 
void NetworkServerSendRcon(ClientID client_id, ConsoleColour colour_code, const char *string);
 
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);
 
void NetworkUndrawChatMessage();
 
void NetworkChatMessageDailyLoop();
 

	
src/network/network_gui.cpp
Show inline comments
 
@@ -1783,13 +1783,13 @@ static void ClientList_Kick(byte client_
 
static void ClientList_Ban(byte client_no)
 
{
 
	NetworkClientInfo *ci = NetworkFindClientInfo(client_no);
 

	
 
	if (ci == NULL) return;
 

	
 
	NetworkServerBanIP(GetClientIP(ci));
 
	NetworkServerKickOrBanIP(GetClientIP(ci), true);
 
}
 

	
 
static void ClientList_GiveMoney(byte client_no)
 
{
 
	if (NetworkFindClientInfo(client_no) != NULL) {
 
		ShowNetworkGiveMoneyWindow(NetworkFindClientInfo(client_no)->client_playas);
src/network/network_server.cpp
Show inline comments
 
@@ -1805,25 +1805,30 @@ void NetworkServerSendError(ClientID cli
 
void NetworkServerKickClient(ClientID client_id)
 
{
 
	if (client_id == CLIENT_ID_SERVER) return;
 
	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)
 
{
 
	const NetworkClientInfo *ci;
 
	FOR_ALL_CLIENT_INFOS(ci) {
0 comments (0 inline, 0 general)