# tcp_admin.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 NetworkAdminSocketHandler::NetworkAdminSocketHandler(SOCKET s)
    00023 {
    00024   this->sock = s;
    00025 }
    00026
    00027 NetworkAdminSocketHandler::~NetworkAdminSocketHandler()
    00028 {
    00029 }
    00030
    00031 NetworkRecvStatus NetworkAdminSocketHandler::CloseConnection(bool error)
    00032 {
    00033   delete this;
    00034   return NETWORK_RECV_STATUS_CONN_LOST;
    00035 }
    00036
    00041 #define ADMIN_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
    00042
    00048 NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p)
    00049 {
    00050   PacketAdminType type = (PacketAdminType)p->Recv_uint8();
    00051
    00052   switch (this->HasClientQuit() ? INVALID_ADMIN_PACKET : type) {
    00053     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_JOIN)
    00054     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_QUIT)
    00055     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_UPDATE_FREQUENCY)
    00056     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_POLL)
    00057     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_CHAT)
    00058     ADMIN_COMMAND(ADMIN_PACKET_ADMIN_RCON)
    00059
    00060     ADMIN_COMMAND(ADMIN_PACKET_SERVER_FULL)
    00061     ADMIN_COMMAND(ADMIN_PACKET_SERVER_BANNED)
    00062     ADMIN_COMMAND(ADMIN_PACKET_SERVER_ERROR)
    00063     ADMIN_COMMAND(ADMIN_PACKET_SERVER_PROTOCOL)
    00064     ADMIN_COMMAND(ADMIN_PACKET_SERVER_WELCOME)
    00065     ADMIN_COMMAND(ADMIN_PACKET_SERVER_NEWGAME)
    00066     ADMIN_COMMAND(ADMIN_PACKET_SERVER_SHUTDOWN)
    00067
    00068     ADMIN_COMMAND(ADMIN_PACKET_SERVER_DATE)
    00069     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_JOIN)
    00070     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_INFO)
    00071     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_UPDATE)
    00072     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_QUIT)
    00073     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CLIENT_ERROR)
    00074     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_NEW)
    00075     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_INFO)
    00076     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_UPDATE)
    00077     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_REMOVE)
    00078     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_ECONOMY)
    00079     ADMIN_COMMAND(ADMIN_PACKET_SERVER_COMPANY_STATS)
    00080     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CHAT)
    00081     ADMIN_COMMAND(ADMIN_PACKET_SERVER_RCON)
    00082     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CONSOLE)
    00083     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CMD_NAMES)
    00084     ADMIN_COMMAND(ADMIN_PACKET_SERVER_CMD_LOGGING)
    00085
    00086     default:
    00087       if (this->HasClientQuit()) {
    00088         DEBUG(net, 0, "[tcp/admin] received invalid packet type %d from '%s' (%s)", type, this->admin_name, this->admin_version);
    00089       } else {
    00090         DEBUG(net, 0, "[tcp/admin] received illegal packet from '%s' (%s)", this->admin_name, this->admin_version);
    00091       }
    00092
    00093       this->CloseConnection();
    00094       return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00095   }
    00096 }
    00097
    00105 NetworkRecvStatus NetworkAdminSocketHandler::ReceivePackets()
    00106 {
    00107   Packet *p;
    00108   while ((p = this->ReceivePacket()) != NULL) {
    00109     NetworkRecvStatus res = this->HandlePacket(p);
    00110     if (res != NETWORK_RECV_STATUS_OKAY) return res;
    00111   }
    00112
    00113   return NETWORK_RECV_STATUS_OKAY;
    00114 }
    00115
    00122 #define DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(type) \
    00123 NetworkRecvStatus NetworkAdminSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \
    00124 { \
    00125   DEBUG(net, 0, "[tcp/admin] received illegal packet type %d from admin %s (%s)", \
    00126       type, this->admin_name, this->admin_version); \
    00127   return NETWORK_RECV_STATUS_MALFORMED_PACKET; \
    00128 }
    00129
    00130 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_JOIN)
    00131 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_QUIT)
    00132 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_UPDATE_FREQUENCY)
    00133 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_POLL)
    00134 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_CHAT)
    00135 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_RCON)
    00136
    00137 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_FULL)
    00138 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_BANNED)
    00139 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_ERROR)
    00140 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_PROTOCOL)
    00141 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_WELCOME)
    00142 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_NEWGAME)
    00143 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_SHUTDOWN)
    00144
    00145 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_DATE)
    00146 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_JOIN)
    00147 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_INFO)
    00148 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_UPDATE)
    00149 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_QUIT)
    00150 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CLIENT_ERROR)
    00151 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_NEW)
    00152 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_INFO)
    00153 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_UPDATE)
    00154 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_REMOVE)
    00155 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_ECONOMY)
    00156 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_COMPANY_STATS)
    00157 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CHAT)
    00158 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_RCON)
    00159 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CONSOLE)
    00160 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CMD_NAMES)
    00161 DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_CMD_LOGGING)
    00162
    00163 #endif /* ENABLE_NETWORK */