Mercurial > hg > openttd
changeset 2914:c616450cd4a0 draft
(svn r3469) - Fix: plug a possible memleak with subsequential strdup's without freeing the previous value and make it possible to ban offline clients
author | Darkvater <Darkvater@openttd.org> |
---|---|
date | Sun, 29 Jan 2006 18:04:52 +0000 |
parents | d2aeb63e3ed6 |
children | 53ad8a24d448 |
files | console_cmds.c network_gui.c |
diffstat | 2 files changed, 29 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/console_cmds.c +++ b/console_cmds.c @@ -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 +++ b/network_gui.c @@ -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; }