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
 
@@ -473,7 +473,7 @@ 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;
 

	
 
@@ -527,7 +527,7 @@ DEF_CONSOLE_CMD(ConKick)
 
	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]);
 
@@ -551,7 +551,7 @@ DEF_CONSOLE_CMD(ConBan)
 
	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]);
src/network/network_func.h
Show inline comments
 
@@ -80,9 +80,9 @@ void NetworkServerDoMove(ClientID client
 
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);
src/network/network_gui.cpp
Show inline comments
 
@@ -1661,7 +1661,7 @@ enum DropDownAdmin {
 
 */
 
static void AdminClientKickCallback(Window *w, bool confirmed)
 
{
 
	if (confirmed) NetworkServerKickClient(_admin_client_id, nullptr);
 
	if (confirmed) NetworkServerKickClient(_admin_client_id, {});
 
}
 

	
 
/**
 
@@ -1671,7 +1671,7 @@ static void AdminClientKickCallback(Wind
 
 */
 
static void AdminClientBanCallback(Window *w, bool confirmed)
 
{
 
	if (confirmed) NetworkServerKickOrBanIP(_admin_client_id, true, nullptr);
 
	if (confirmed) NetworkServerKickOrBanIP(_admin_client_id, true, {});
 
}
 

	
 
/**
src/network/network_server.cpp
Show inline comments
 
@@ -429,12 +429,12 @@ NetworkRecvStatus ServerNetworkGameSocke
 
 * @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);
 
@@ -447,7 +447,7 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
		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);
 
@@ -2054,7 +2054,7 @@ void NetworkServerSendRcon(ClientID clie
 
 * @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);
 
@@ -2066,7 +2066,7 @@ void NetworkServerKickClient(ClientID cl
 
 * @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);
 
}
 
@@ -2077,7 +2077,7 @@ uint NetworkServerKickOrBanIP(ClientID c
 
 * @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) {
 
@@ -2101,7 +2101,7 @@ uint NetworkServerKickOrBanIP(const char
 
	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++;
 
		}
src/network/network_server.h
Show inline comments
 
@@ -93,7 +93,7 @@ public:
 
	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();
0 comments (0 inline, 0 general)