# tcp_admin.cpp

Go to the documentation of this file.

    00001 /* $Id: tcp_admin.cpp 23052 2011-10-22 20:54:23Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00014 #ifdef ENABLE_NETWORK
    00015
    00016 #include "../../stdafx.h"
    00017
    00018 #include "../network_internal.h"
    00019 #include "tcp_admin.h"
    00020 #include "../../debug.h"
    00021
    00022 /* Make sure that these enums match. */
    00023 assert_compile((int)CRR_MANUAL    == (int)ADMIN_CRR_MANUAL);
    00024 assert_compile((int)CRR_AUTOCLEAN == (int)ADMIN_CRR_AUTOCLEAN);
    00025 assert_compile((int)CRR_BANKRUPT  == (int)ADMIN_CRR_BANKRUPT);
    00026 assert_compile((int)CRR_END       == (int)ADMIN_CRR_END);
    00027
    00028 NetworkAdminSocketHandler::NetworkAdminSocketHandler(SOCKET s)
    00029 {
    00030   this->sock = s;
    00031 }
    00032
    00033 NetworkAdminSocketHandler::~NetworkAdminSocketHandler()
    00034 {
    00035 }
    00036
    00037 NetworkRecvStatus NetworkAdminSocketHandler::CloseConnection(bool error)
    00038 {
    00039   delete this;
    00040   return NETWORK_RECV_STATUS_CONN_LOST;
    00041 }
    00042
    00047 #define ADMIN_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
    00048
    00054 NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p)
    00055 {
    00056   PacketAdminType type = (PacketAdminType)p->Recv_uint8();
    00057
    00058   switch (this->HasClientQuit() ? INVALID_ADMIN_PACKET : type) {
    00059     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_JOIN)
    00060     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_QUIT)
    00061     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_UPDATE_FREQUENCY)
    00062     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_POLL)
    00063     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_CHAT)
    00064     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_RCON)
    00065
    00066     ADMIN_COMMAND(ADMIN_PACKET_SERVER_FULL)
    00067     ADMIN_COMMAND(ADMIN_PACKET_SERVER_BANNED)
    00068     ADMIN_COMMAND(ADMIN_PACKET_SERVER_ERROR)
    00069     ADMIN_COMMAND(ADMIN_PACKET_SERVER_PROTOCOL)
    00070     ADMIN_COMMAND(ADMIN_PACKET_SERVER_WELCOME)
    00071     ADMIN_COMMAND(ADMIN_PACKET_SERVER_NEWGAME)
    00072     ADMIN_COMMAND(ADMIN_PACKET_SERVER_SHUTDOWN)
    00073
    00074     ADMIN_COMMAND(ADMIN_PACKET_SERVER_DATE)
    00075     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_JOIN)
    00076     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_INFO)
    00077     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_UPDATE)
    00078     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_QUIT)
    00079     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_ERROR)
    00080     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_NEW)
    00081     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_INFO)
    00082     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_UPDATE)
    00083     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_REMOVE)
    00084     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_ECONOMY)
    00085     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_STATS)
    00086     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CHAT)
    00087     ADMIN_COMMAND(ADMIN_PACKET_SERVER_RCON)
    00088     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CONSOLE)
    00089     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CMD_NAMES)
    00090     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CMD_LOGGING)
    00091
    00092     default:
    00093       if (this->HasClientQuit()) {
    00094         DEBUG(net, 0, "[tcp/admin] received invalid packet type %d from '%s' (%s)", type, this->admin_name, this->admin_version);
    00095       } else {
    00096         DEBUG(net, 0, "[tcp/admin] received illegal packet from '%s' (%s)", this->admin_name, this->admin_version);
    00097       }
    00098
    00099       this->CloseConnection();
    00100       return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00101   }
    00102 }
    00103
    00111 NetworkRecvStatus NetworkAdminSocketHandler::ReceivePackets()
    00112 {
    00113   Packet *p;
    00114   while ((p = this->ReceivePacket()) != NULL) {
    00115     NetworkRecvStatus res = this->HandlePacket(p);
    00116     if (res != NETWORK_RECV_STATUS_OKAY) return res;
    00117   }
    00118
    00119   return NETWORK_RECV_STATUS_OKAY;
    00120 }
    00121
    00128 #define DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(type) \
    00129 NetworkRecvStatus NetworkAdminSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \
    00130 { \
    00131   DEBUG(net, 0, "[tcp/admin] received illegal packet type %d from admin %s (%s)", \
    00132       type, this->admin_name, this->admin_version); \
    00133   return NETWORK_RECV_STATUS_MALFORMED_PACKET; \
    00134 }
    00135
    00136 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_JOIN)
    00137 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_QUIT)
    00138 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_UPDATE_FREQUENCY)
    00139 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_POLL)
    00140 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_CHAT)
    00141 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_RCON)
    00142
    00143 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_FULL)
    00144 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_BANNED)
    00145 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_ERROR)
    00146 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_PROTOCOL)
    00147 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_WELCOME)
    00148 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_NEWGAME)
    00149 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_SHUTDOWN)
    00150
    00151 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_DATE)
    00152 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_JOIN)
    00153 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_INFO)
    00154 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_UPDATE)
    00155 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_QUIT)
    00156 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_ERROR)
    00157 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_NEW)
    00158 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_INFO)
    00159 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_UPDATE)
    00160 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_REMOVE)
    00161 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_ECONOMY)
    00162 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_STATS)
    00163 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CHAT)
    00164 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_RCON)
    00165 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CONSOLE)
    00166 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CMD_NAMES)
    00167 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CMD_LOGGING)
    00168
    00169 #endif /* ENABLE_NETWORK */