Changeset - r25569:ae23e84953e3
[Not reviewed]
master
0 5 0
rubidium42 - 3 years ago 2021-05-29 17:47:58
rubidium@openttd.org
Codechange: [Network] Use std::string for server side logic of kicking and banning clients
5 files changed with 16 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -470,13 +470,13 @@ DEF_CONSOLE_CMD(ConClearBuffer)
 

	
 

	
 
/**********************************
 
 * Network Core Console Commands
 
 **********************************/
 

	
 
static bool ConKickOrBan(const char *argv, bool ban, const char *reason)
 
static bool ConKickOrBan(const char *argv, bool ban, const std::string &reason)
 
{
 
	uint n;
 

	
 
	if (strchr(argv, '.') == nullptr && strchr(argv, ':') == nullptr) { // banning with ID
 
		ClientID client_id = (ClientID)atoi(argv);
 

	
 
@@ -524,13 +524,13 @@ DEF_CONSOLE_CMD(ConKick)
 
		return true;
 
	}
 

	
 
	if (argc != 2 && argc != 3) return false;
 

	
 
	/* No reason supplied for kicking */
 
	if (argc == 2) return ConKickOrBan(argv[1], false, nullptr);
 
	if (argc == 2) return ConKickOrBan(argv[1], false, {});
 

	
 
	/* Reason for kicking supplied */
 
	size_t kick_message_length = strlen(argv[2]);
 
	if (kick_message_length >= 255) {
 
		IConsolePrintF(CC_ERROR, "ERROR: Maximum kick message length is 254 characters. You entered " PRINTF_SIZE " characters.", kick_message_length);
 
		return false;
 
@@ -548,13 +548,13 @@ DEF_CONSOLE_CMD(ConBan)
 
		return true;
 
	}
 

	
 
	if (argc != 2 && argc != 3) return false;
 

	
 
	/* No reason supplied for kicking */
 
	if (argc == 2) return ConKickOrBan(argv[1], true, nullptr);
 
	if (argc == 2) return ConKickOrBan(argv[1], true, {});
 

	
 
	/* Reason for kicking supplied */
 
	size_t kick_message_length = strlen(argv[2]);
 
	if (kick_message_length >= 255) {
 
		IConsolePrintF(CC_ERROR, "ERROR: Maximum kick message length is 254 characters. You entered " PRINTF_SIZE " characters.", kick_message_length);
 
		return false;
src/network/network_func.h
Show inline comments
 
@@ -77,15 +77,15 @@ bool NetworkServerChangeClientName(Clien
 

	
 

	
 
void NetworkServerDoMove(ClientID client_id, CompanyID company_id);
 
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string);
 
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const std::string &msg, ClientID from_id, int64 data = 0, bool from_admin = false);
 

	
 
void NetworkServerKickClient(ClientID client_id, const char *reason);
 
uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const char *reason);
 
uint NetworkServerKickOrBanIP(const char *ip, bool ban, const char *reason);
 
void NetworkServerKickClient(ClientID client_id, const std::string &reason);
 
uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &reason);
 
uint NetworkServerKickOrBanIP(const std::string &ip, bool ban, const std::string &reason);
 

	
 
void NetworkInitChatMessage();
 
void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const std::string &message);
 
void NetworkUndrawChatMessage();
 
void NetworkChatMessageLoop();
 

	
src/network/network_gui.cpp
Show inline comments
 
@@ -1658,23 +1658,23 @@ enum DropDownAdmin {
 
 * Callback function for admin command to kick client.
 
 * @param w The window which initiated the confirmation dialog.
 
 * @param confirmed Iff the user pressed Yes.
 
 */
 
static void AdminClientKickCallback(Window *w, bool confirmed)
 
{
 
	if (confirmed) NetworkServerKickClient(_admin_client_id, nullptr);
 
	if (confirmed) NetworkServerKickClient(_admin_client_id, {});
 
}
 

	
 
/**
 
 * Callback function for admin command to ban client.
 
 * @param w The window which initiated the confirmation dialog.
 
 * @param confirmed Iff the user pressed Yes.
 
 */
 
static void AdminClientBanCallback(Window *w, bool confirmed)
 
{
 
	if (confirmed) NetworkServerKickOrBanIP(_admin_client_id, true, nullptr);
 
	if (confirmed) NetworkServerKickOrBanIP(_admin_client_id, true, {});
 
}
 

	
 
/**
 
 * Callback function for admin command to reset company.
 
 * @param w The window which initiated the confirmation dialog.
 
 * @param confirmed Iff the user pressed Yes.
src/network/network_server.cpp
Show inline comments
 
@@ -426,31 +426,31 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
/**
 
 * Send an error to the client, and close its connection.
 
 * @param error The error to disconnect for.
 
 * @param reason In case of kicking a client, specifies the reason for kicking the client.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const char *reason)
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const std::string &reason)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_ERROR);
 

	
 
	p->Send_uint8(error);
 
	if (reason != nullptr) p->Send_string(reason);
 
	if (!reason.empty()) p->Send_string(reason);
 
	this->SendPacket(p);
 

	
 
	StringID strid = GetNetworkErrorMsg(error);
 

	
 
	/* Only send when the current client was in game */
 
	if (this->status > STATUS_AUTHORIZED) {
 
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
 

	
 
		this->GetClientName(client_name, lastof(client_name));
 

	
 
		DEBUG(net, 1, "'%s' made an error and has been disconnected: %s", client_name, GetString(strid).c_str());
 

	
 
		if (error == NETWORK_ERROR_KICKED && reason != nullptr) {
 
		if (error == NETWORK_ERROR_KICKED && !reason.empty()) {
 
			NetworkTextMessage(NETWORK_ACTION_KICKED, CC_DEFAULT, false, client_name, reason, strid);
 
		} else {
 
			NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
 
		}
 

	
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
@@ -2051,36 +2051,36 @@ void NetworkServerSendRcon(ClientID clie
 

	
 
/**
 
 * Kick a single client.
 
 * @param client_id The client to kick.
 
 * @param reason In case of kicking a client, specifies the reason for kicking the client.
 
 */
 
void NetworkServerKickClient(ClientID client_id, const char *reason)
 
void NetworkServerKickClient(ClientID client_id, const std::string &reason)
 
{
 
	if (client_id == CLIENT_ID_SERVER) return;
 
	NetworkClientSocket::GetByClientID(client_id)->SendError(NETWORK_ERROR_KICKED, reason);
 
}
 

	
 
/**
 
 * Ban, or kick, everyone joined from the given client's IP.
 
 * @param client_id The client to check for.
 
 * @param ban Whether to ban or kick.
 
 * @param reason In case of kicking a client, specifies the reason for kicking the client.
 
 */
 
uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const char *reason)
 
uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &reason)
 
{
 
	return NetworkServerKickOrBanIP(NetworkClientSocket::GetByClientID(client_id)->GetClientIP(), ban, reason);
 
}
 

	
 
/**
 
 * Kick or ban someone based on an IP address.
 
 * @param ip The IP address/range to ban/kick.
 
 * @param ban Whether to ban or just kick.
 
 * @param reason In case of kicking a client, specifies the reason for kicking the client.
 
 */
 
uint NetworkServerKickOrBanIP(const char *ip, bool ban, const char *reason)
 
uint NetworkServerKickOrBanIP(const std::string &ip, bool ban, const std::string &reason)
 
{
 
	/* Add address to ban-list */
 
	if (ban) {
 
		bool contains = false;
 
		for (const auto &iter : _network_ban_list) {
 
			if (iter == ip) {
 
@@ -2098,13 +2098,13 @@ uint NetworkServerKickOrBanIP(const char
 
	 * and subsequently free the connection related instances, which we would be reading from
 
	 * and writing to after returning. So we would read or write data from freed memory up till
 
	 * the segfault triggers. */
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->client_id == CLIENT_ID_SERVER) continue;
 
		if (cs->client_id == _redirect_console_to_client) continue;
 
		if (cs->client_address.IsInNetmask(ip)) {
 
		if (cs->client_address.IsInNetmask(ip.c_str())) {
 
			NetworkServerKickClient(cs->client_id, reason);
 
			n++;
 
		}
 
	}
 

	
 
	return n;
src/network/network_server.h
Show inline comments
 
@@ -90,13 +90,13 @@ public:
 
	NetworkRecvStatus SendShutdown();
 
	NetworkRecvStatus SendNewGame();
 
	NetworkRecvStatus SendRConResult(uint16 colour, const std::string &command);
 
	NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
 

	
 
	NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
 
	NetworkRecvStatus SendError(NetworkErrorCode error, const char *reason = nullptr);
 
	NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason = {});
 
	NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64 data);
 
	NetworkRecvStatus SendJoin(ClientID client_id);
 
	NetworkRecvStatus SendFrame();
 
	NetworkRecvStatus SendSync();
 
	NetworkRecvStatus SendCommand(const CommandPacket *cp);
 
	NetworkRecvStatus SendCompanyUpdate();
0 comments (0 inline, 0 general)