(svn r3469) - Fix: plug a possible memleak with subsequential strdup's without freeing the previous value and make it possible to ban offline clients
authorDarkvater
Sun, 29 Jan 2006 18:04:52 +0000
changeset 2914 04ce2485f05f
parent 2913 fca9b500c9f9
child 2915 c59cbb76b5b2
(svn r3469) - Fix: plug a possible memleak with subsequential strdup's without freeing the previous value and make it possible to ban offline clients
console_cmds.c
network_gui.c
--- a/console_cmds.c	Sun Jan 29 12:52:07 2006 +0000
+++ b/console_cmds.c	Sun Jan 29 18:04:52 2006 +0000
@@ -362,22 +362,29 @@
 DEF_CONSOLE_CMD(ConBan)
 {
 	NetworkClientInfo *ci;
+	const char *banip = NULL;
 	uint32 index;
 
 	if (argc == 0) {
 		IConsoleHelp("Ban a player 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) {
+	if (strchr(argv[1], '.') == NULL) { // banning with ID
 		index = atoi(argv[1]);
 		ci = NetworkFindClientInfoFromIndex(index);
-	} else {
+	} else { // banning IP
 		ci = NetworkFindClientInfoFromIP(argv[1]);
-		index = (ci == NULL) ? 0 : ci->client_index;
+		if (ci == NULL) {
+			banip = argv[1];
+			index = (uint32)-1;
+		} else {
+			index = ci->client_index;
+		}
 	}
 
 	if (index == NETWORK_SERVER_INDEX) {
@@ -385,24 +392,25 @@
 		return true;
 	}
 
-	if (index == 0) {
+	if (index == 0 || (ci == NULL && index != (uint32)-1)) {
 		IConsoleError("Invalid client");
 		return true;
 	}
 
 	if (ci != NULL) {
-		uint i;
-		/* Add user to ban-list */
-		for (i = 0; i < lengthof(_network_ban_list); i++) {
-			if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') {
-				_network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ci->client_ip));
-				break;
-			}
+		banip = inet_ntoa(*(struct in_addr *)&ci->client_ip);
+		SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED);
+		IConsolePrint(_icolour_def, "Client banned");
+	} else
+		IConsolePrint(_icolour_def, "Client not online, banned IP");
+
+	/* Add user to ban-list */
+	for (index = 0; index < lengthof(_network_ban_list); index++) {
+		if (_network_ban_list[index] == NULL) {
+			_network_ban_list[index] = strdup(banip);
+			break;
 		}
-
-		SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED);
-	} else
-		IConsoleError("Client not found");
+	}
 
 	return true;
 }
@@ -423,11 +431,11 @@
 	index--;
 
 	for (i = 0; i < lengthof(_network_ban_list); i++) {
-		if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0')
-			continue;
+		if (_network_ban_list[i] == NULL) continue;
 
 		if (strncmp(_network_ban_list[i], argv[1], strlen(_network_ban_list[i])) == 0 || index == i) {
-			_network_ban_list[i][0] = '\0';
+			free(_network_ban_list[i]);
+			_network_ban_list[i] = NULL;
 			IConsolePrint(_icolour_def, "IP unbanned.");
 			return true;
 		}
@@ -449,10 +457,8 @@
 	IConsolePrint(_icolour_def, "Banlist: ");
 
 	for (i = 0; i < lengthof(_network_ban_list); i++) {
-		if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0')
-			continue;
-
-		IConsolePrintF(_icolour_def, "  %d) %s", i + 1, _network_ban_list[i]);
+		if (_network_ban_list[i] != NULL)
+			IConsolePrintF(_icolour_def, "  %d) %s", i + 1, _network_ban_list[i]);
 	}
 
 	return true;
--- a/network_gui.c	Sun Jan 29 12:52:07 2006 +0000
+++ b/network_gui.c	Sun Jan 29 18:04:52 2006 +0000
@@ -1068,7 +1068,7 @@
 	uint32 ip = NetworkFindClientInfo(client_no)->client_ip;
 
 	for (i = 0; i < lengthof(_network_ban_list); i++) {
-		if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') {
+		if (_network_ban_list[i] == NULL) {
 			_network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ip));
 			break;
 		}