Maintain a circular buffer of recent commands, add to crashlog.
[openttd-joker.git] / src / network / core / tcp_admin.cpp
blob85eb7b0b70ea95950cd0b20d0ad30be6fb6d9bc5
1 /* $Id: tcp_admin.cpp 26056 2013-11-22 21:50:43Z rubidium $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /**
11 * @file tcp_admin.cpp Basic functions to receive and send TCP packets to and from the admin network.
14 #ifdef ENABLE_NETWORK
16 #include "../../stdafx.h"
18 #include "../network_internal.h"
19 #include "tcp_admin.h"
20 #include "../../debug.h"
22 #include "../../safeguards.h"
24 /* Make sure that these enums match. */
25 assert_compile((int)CRR_MANUAL == (int)ADMIN_CRR_MANUAL);
26 assert_compile((int)CRR_AUTOCLEAN == (int)ADMIN_CRR_AUTOCLEAN);
27 assert_compile((int)CRR_BANKRUPT == (int)ADMIN_CRR_BANKRUPT);
28 assert_compile((int)CRR_END == (int)ADMIN_CRR_END);
30 /**
31 * Create the admin handler for the given socket.
32 * @param s The socket to communicate over.
34 NetworkAdminSocketHandler::NetworkAdminSocketHandler(SOCKET s) : status(ADMIN_STATUS_INACTIVE)
36 this->sock = s;
37 this->admin_name[0] = '\0';
38 this->admin_version[0] = '\0';
41 NetworkAdminSocketHandler::~NetworkAdminSocketHandler()
45 NetworkRecvStatus NetworkAdminSocketHandler::CloseConnection(bool error)
47 delete this;
48 return NETWORK_RECV_STATUS_CONN_LOST;
51 /**
52 * Handle the given packet, i.e. pass it to the right parser receive command.
53 * @param p the packet to handle.
54 * @return #NetworkRecvStatus of handling.
56 NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p)
58 PacketAdminType type = (PacketAdminType)p->Recv_uint8();
60 switch (this->HasClientQuit() ? INVALID_ADMIN_PACKET : type) {
61 case ADMIN_PACKET_ADMIN_JOIN: return this->Receive_ADMIN_JOIN(p);
62 case ADMIN_PACKET_ADMIN_QUIT: return this->Receive_ADMIN_QUIT(p);
63 case ADMIN_PACKET_ADMIN_UPDATE_FREQUENCY: return this->Receive_ADMIN_UPDATE_FREQUENCY(p);
64 case ADMIN_PACKET_ADMIN_POLL: return this->Receive_ADMIN_POLL(p);
65 case ADMIN_PACKET_ADMIN_CHAT: return this->Receive_ADMIN_CHAT(p);
66 case ADMIN_PACKET_ADMIN_RCON: return this->Receive_ADMIN_RCON(p);
67 case ADMIN_PACKET_ADMIN_GAMESCRIPT: return this->Receive_ADMIN_GAMESCRIPT(p);
68 case ADMIN_PACKET_ADMIN_PING: return this->Receive_ADMIN_PING(p);
70 case ADMIN_PACKET_SERVER_FULL: return this->Receive_SERVER_FULL(p);
71 case ADMIN_PACKET_SERVER_BANNED: return this->Receive_SERVER_BANNED(p);
72 case ADMIN_PACKET_SERVER_ERROR: return this->Receive_SERVER_ERROR(p);
73 case ADMIN_PACKET_SERVER_PROTOCOL: return this->Receive_SERVER_PROTOCOL(p);
74 case ADMIN_PACKET_SERVER_WELCOME: return this->Receive_SERVER_WELCOME(p);
75 case ADMIN_PACKET_SERVER_NEWGAME: return this->Receive_SERVER_NEWGAME(p);
76 case ADMIN_PACKET_SERVER_SHUTDOWN: return this->Receive_SERVER_SHUTDOWN(p);
78 case ADMIN_PACKET_SERVER_DATE: return this->Receive_SERVER_DATE(p);
79 case ADMIN_PACKET_SERVER_CLIENT_JOIN: return this->Receive_SERVER_CLIENT_JOIN(p);
80 case ADMIN_PACKET_SERVER_CLIENT_INFO: return this->Receive_SERVER_CLIENT_INFO(p);
81 case ADMIN_PACKET_SERVER_CLIENT_UPDATE: return this->Receive_SERVER_CLIENT_UPDATE(p);
82 case ADMIN_PACKET_SERVER_CLIENT_QUIT: return this->Receive_SERVER_CLIENT_QUIT(p);
83 case ADMIN_PACKET_SERVER_CLIENT_ERROR: return this->Receive_SERVER_CLIENT_ERROR(p);
84 case ADMIN_PACKET_SERVER_COMPANY_NEW: return this->Receive_SERVER_COMPANY_NEW(p);
85 case ADMIN_PACKET_SERVER_COMPANY_INFO: return this->Receive_SERVER_COMPANY_INFO(p);
86 case ADMIN_PACKET_SERVER_COMPANY_UPDATE: return this->Receive_SERVER_COMPANY_UPDATE(p);
87 case ADMIN_PACKET_SERVER_COMPANY_REMOVE: return this->Receive_SERVER_COMPANY_REMOVE(p);
88 case ADMIN_PACKET_SERVER_COMPANY_ECONOMY: return this->Receive_SERVER_COMPANY_ECONOMY(p);
89 case ADMIN_PACKET_SERVER_COMPANY_STATS: return this->Receive_SERVER_COMPANY_STATS(p);
90 case ADMIN_PACKET_SERVER_CHAT: return this->Receive_SERVER_CHAT(p);
91 case ADMIN_PACKET_SERVER_RCON: return this->Receive_SERVER_RCON(p);
92 case ADMIN_PACKET_SERVER_CONSOLE: return this->Receive_SERVER_CONSOLE(p);
93 case ADMIN_PACKET_SERVER_CMD_NAMES: return this->Receive_SERVER_CMD_NAMES(p);
94 case ADMIN_PACKET_SERVER_CMD_LOGGING: return this->Receive_SERVER_CMD_LOGGING(p);
95 case ADMIN_PACKET_SERVER_RCON_END: return this->Receive_SERVER_RCON_END(p);
96 case ADMIN_PACKET_SERVER_PONG: return this->Receive_SERVER_PONG(p);
98 default:
99 if (this->HasClientQuit()) {
100 DEBUG(net, 0, "[tcp/admin] received invalid packet type %d from '%s' (%s)", type, this->admin_name, this->admin_version);
101 } else {
102 DEBUG(net, 0, "[tcp/admin] received illegal packet from '%s' (%s)", this->admin_name, this->admin_version);
105 this->CloseConnection();
106 return NETWORK_RECV_STATUS_MALFORMED_PACKET;
111 * Do the actual receiving of packets.
112 * As long as HandlePacket returns OKAY packets are handled. Upon
113 * failure, or no more packets to process the last result of
114 * HandlePacket is returned.
115 * @return #NetworkRecvStatus of the last handled packet.
117 NetworkRecvStatus NetworkAdminSocketHandler::ReceivePackets()
119 Packet *p;
120 while ((p = this->ReceivePacket()) != NULL) {
121 NetworkRecvStatus res = this->HandlePacket(p);
122 if (res != NETWORK_RECV_STATUS_OKAY) return res;
125 return NETWORK_RECV_STATUS_OKAY;
129 * Helper for logging receiving invalid packets.
130 * @param type The received packet type.
131 * @return The status the network should have, in this case: "malformed packet error".
133 NetworkRecvStatus NetworkAdminSocketHandler::ReceiveInvalidPacket(PacketAdminType type)
135 DEBUG(net, 0, "[tcp/admin] received illegal packet type %d from admin %s (%s)", type, this->admin_name, this->admin_version);
136 return NETWORK_RECV_STATUS_MALFORMED_PACKET;
139 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_JOIN(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_JOIN); }
140 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_QUIT(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_QUIT); }
141 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_UPDATE_FREQUENCY(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_UPDATE_FREQUENCY); }
142 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_POLL(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_POLL); }
143 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_CHAT(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_CHAT); }
144 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_RCON(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_RCON); }
145 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_GAMESCRIPT(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_GAMESCRIPT); }
146 NetworkRecvStatus NetworkAdminSocketHandler::Receive_ADMIN_PING(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_ADMIN_PING); }
148 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_FULL(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_FULL); }
149 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_BANNED(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_BANNED); }
150 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_ERROR(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_ERROR); }
151 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_PROTOCOL(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_PROTOCOL); }
152 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_WELCOME(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_WELCOME); }
153 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_NEWGAME(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_NEWGAME); }
154 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_SHUTDOWN(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_SHUTDOWN); }
156 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_DATE(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_DATE); }
157 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CLIENT_JOIN(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CLIENT_JOIN); }
158 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CLIENT_INFO(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CLIENT_INFO); }
159 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CLIENT_UPDATE(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CLIENT_UPDATE); }
160 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CLIENT_QUIT(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CLIENT_QUIT); }
161 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CLIENT_ERROR(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CLIENT_ERROR); }
162 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_COMPANY_NEW(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_COMPANY_NEW); }
163 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_COMPANY_INFO(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_COMPANY_INFO); }
164 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_COMPANY_UPDATE(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_COMPANY_UPDATE); }
165 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_COMPANY_REMOVE(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_COMPANY_REMOVE); }
166 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_COMPANY_ECONOMY(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_COMPANY_ECONOMY); }
167 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_COMPANY_STATS(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_COMPANY_STATS); }
168 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CHAT(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CHAT); }
169 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_RCON(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_RCON); }
170 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CONSOLE(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CONSOLE); }
171 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CMD_NAMES(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CMD_NAMES); }
172 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_CMD_LOGGING(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_CMD_LOGGING); }
173 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_RCON_END(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_RCON_END); }
174 NetworkRecvStatus NetworkAdminSocketHandler::Receive_SERVER_PONG(Packet *p) { return this->ReceiveInvalidPacket(ADMIN_PACKET_SERVER_PONG); }
176 #endif /* ENABLE_NETWORK */