1 /* 2 * This file is part of OpenTTD. 3 * 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. 4 * 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. 5 * 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/>. 6 */ 7 8 /** 9 * @file tcp_admin.h Basic functions to receive and send TCP packets to and from the admin network. 10 */ 11 12 #ifndef NETWORK_CORE_TCP_ADMIN_H 13 #define NETWORK_CORE_TCP_ADMIN_H 14 15 #include "os_abstraction.h" 16 #include "tcp.h" 17 #include "../network_type.h" 18 #include "../../core/pool_type.hpp" 19 20 /** 21 * Enum with types of TCP packets specific to the admin network. 22 * This protocol may only be extended to ensure stability. 23 */ 24 enum PacketAdminType { 25 ADMIN_PACKET_ADMIN_JOIN, ///< The admin announces and authenticates itself to the server. 26 ADMIN_PACKET_ADMIN_QUIT, ///< The admin tells the server that it is quitting. 27 ADMIN_PACKET_ADMIN_UPDATE_FREQUENCY, ///< The admin tells the server the update frequency of a particular piece of information. 28 ADMIN_PACKET_ADMIN_POLL, ///< The admin explicitly polls for a piece of information. 29 ADMIN_PACKET_ADMIN_CHAT, ///< The admin sends a chat message to be distributed. 30 ADMIN_PACKET_ADMIN_RCON, ///< The admin sends a remote console command. 31 ADMIN_PACKET_ADMIN_GAMESCRIPT, ///< The admin sends a JSON string for the GameScript. 32 ADMIN_PACKET_ADMIN_PING, ///< The admin sends a ping to the server, expecting a ping-reply (PONG) packet. 33 ADMIN_PACKET_ADMIN_EXTERNAL_CHAT, ///< The admin sends a chat message from external source. 34 35 ADMIN_PACKET_SERVER_FULL = 100, ///< The server tells the admin it cannot accept the admin. 36 ADMIN_PACKET_SERVER_BANNED, ///< The server tells the admin it is banned. 37 ADMIN_PACKET_SERVER_ERROR, ///< The server tells the admin an error has occurred. 38 ADMIN_PACKET_SERVER_PROTOCOL, ///< The server tells the admin its protocol version. 39 ADMIN_PACKET_SERVER_WELCOME, ///< The server welcomes the admin to a game. 40 ADMIN_PACKET_SERVER_NEWGAME, ///< The server tells the admin its going to start a new game. 41 ADMIN_PACKET_SERVER_SHUTDOWN, ///< The server tells the admin its shutting down. 42 43 ADMIN_PACKET_SERVER_DATE, ///< The server tells the admin what the current game date is. 44 ADMIN_PACKET_SERVER_CLIENT_JOIN, ///< The server tells the admin that a client has joined. 45 ADMIN_PACKET_SERVER_CLIENT_INFO, ///< The server gives the admin information about a client. 46 ADMIN_PACKET_SERVER_CLIENT_UPDATE, ///< The server gives the admin an information update on a client. 47 ADMIN_PACKET_SERVER_CLIENT_QUIT, ///< The server tells the admin that a client quit. 48 ADMIN_PACKET_SERVER_CLIENT_ERROR, ///< The server tells the admin that a client caused an error. 49 ADMIN_PACKET_SERVER_COMPANY_NEW, ///< The server tells the admin that a new company has started. 50 ADMIN_PACKET_SERVER_COMPANY_INFO, ///< The server gives the admin information about a company. 51 ADMIN_PACKET_SERVER_COMPANY_UPDATE, ///< The server gives the admin an information update on a company. 52 ADMIN_PACKET_SERVER_COMPANY_REMOVE, ///< The server tells the admin that a company was removed. 53 ADMIN_PACKET_SERVER_COMPANY_ECONOMY, ///< The server gives the admin some economy related company information. 54 ADMIN_PACKET_SERVER_COMPANY_STATS, ///< The server gives the admin some statistics about a company. 55 ADMIN_PACKET_SERVER_CHAT, ///< The server received a chat message and relays it. 56 ADMIN_PACKET_SERVER_RCON, ///< The server's reply to a remove console command. 57 ADMIN_PACKET_SERVER_CONSOLE, ///< The server gives the admin the data that got printed to its console. 58 ADMIN_PACKET_SERVER_CMD_NAMES, ///< The server sends out the names of the DoCommands to the admins. 59 ADMIN_PACKET_SERVER_CMD_LOGGING, ///< The server gives the admin copies of incoming command packets. 60 ADMIN_PACKET_SERVER_GAMESCRIPT, ///< The server gives the admin information from the GameScript in JSON. 61 ADMIN_PACKET_SERVER_RCON_END, ///< The server indicates that the remote console command has completed. 62 ADMIN_PACKET_SERVER_PONG, ///< The server replies to a ping request from the admin. 63 64 INVALID_ADMIN_PACKET = 0xFF, ///< An invalid marker for admin packets. 65 }; 66 67 /** Status of an admin. */ 68 enum AdminStatus { 69 ADMIN_STATUS_INACTIVE, ///< The admin is not connected nor active. 70 ADMIN_STATUS_ACTIVE, ///< The admin is active. 71 ADMIN_STATUS_END, ///< Must ALWAYS be on the end of this list!! (period) 72 }; 73 74 /** Update types an admin can register a frequency for */ 75 enum AdminUpdateType { 76 ADMIN_UPDATE_DATE, ///< Updates about the date of the game. 77 ADMIN_UPDATE_CLIENT_INFO, ///< Updates about the information of clients. 78 ADMIN_UPDATE_COMPANY_INFO, ///< Updates about the generic information of companies. 79 ADMIN_UPDATE_COMPANY_ECONOMY, ///< Updates about the economy of companies. 80 ADMIN_UPDATE_COMPANY_STATS, ///< Updates about the statistics of companies. 81 ADMIN_UPDATE_CHAT, ///< The admin would like to have chat messages. 82 ADMIN_UPDATE_CONSOLE, ///< The admin would like to have console messages. 83 ADMIN_UPDATE_CMD_NAMES, ///< The admin would like a list of all DoCommand names. 84 ADMIN_UPDATE_CMD_LOGGING, ///< The admin would like to have DoCommand information. 85 ADMIN_UPDATE_GAMESCRIPT, ///< The admin would like to have gamescript messages. 86 ADMIN_UPDATE_END, ///< Must ALWAYS be on the end of this list!! (period) 87 }; 88 89 /** Update frequencies an admin can register. */ 90 enum AdminUpdateFrequency { 91 ADMIN_FREQUENCY_POLL = 0x01, ///< The admin can poll this. 92 ADMIN_FREQUENCY_DAILY = 0x02, ///< The admin gets information about this on a daily basis. 93 ADMIN_FREQUENCY_WEEKLY = 0x04, ///< The admin gets information about this on a weekly basis. 94 ADMIN_FREQUENCY_MONTHLY = 0x08, ///< The admin gets information about this on a monthly basis. 95 ADMIN_FREQUENCY_QUARTERLY = 0x10, ///< The admin gets information about this on a quarterly basis. 96 ADMIN_FREQUENCY_ANUALLY = 0x20, ///< The admin gets information about this on a yearly basis. 97 ADMIN_FREQUENCY_AUTOMATIC = 0x40, ///< The admin gets information about this when it changes. 98 }; 99 DECLARE_ENUM_AS_BIT_SET(AdminUpdateFrequency) 100 101 /** Reasons for removing a company - communicated to admins. */ 102 enum AdminCompanyRemoveReason { 103 ADMIN_CRR_MANUAL, ///< The company is manually removed. 104 ADMIN_CRR_AUTOCLEAN, ///< The company is removed due to autoclean. 105 ADMIN_CRR_BANKRUPT, ///< The company went belly-up. 106 107 ADMIN_CRR_END, ///< Sentinel for end. 108 }; 109 110 /** Main socket handler for admin related connections. */ 111 class NetworkAdminSocketHandler : public NetworkTCPSocketHandler { 112 protected: 113 std::string admin_name; ///< Name of the admin. 114 std::string admin_version; ///< Version string of the admin. 115 AdminStatus status; ///< Status of this admin. 116 117 NetworkRecvStatus ReceiveInvalidPacket(PacketAdminType type); 118 119 /** 120 * Join the admin network: 121 * string Password the server is expecting for this network. 122 * string Name of the application being used to connect. 123 * string Version string of the application being used to connect. 124 * @param p The packet that was just received. 125 * @return The state the network should have. 126 */ 127 virtual NetworkRecvStatus Receive_ADMIN_JOIN(Packet *p); 128 129 /** 130 * Notification to the server that this admin is quitting. 131 * @param p The packet that was just received. 132 * @return The state the network should have. 133 */ 134 virtual NetworkRecvStatus Receive_ADMIN_QUIT(Packet *p); 135 136 /** 137 * Register updates to be sent at certain frequencies (as announced in the PROTOCOL packet): 138 * uint16 Update type (see #AdminUpdateType). Note integer type - see "Certain Packet Information" in docs/admin_network.md. 139 * uint16 Update frequency (see #AdminUpdateFrequency), setting #ADMIN_FREQUENCY_POLL is always ignored. 140 * @param p The packet that was just received. 141 * @return The state the network should have. 142 */ 143 virtual NetworkRecvStatus Receive_ADMIN_UPDATE_FREQUENCY(Packet *p); 144 145 /** 146 * Poll the server for certain updates, an invalid poll (e.g. not existent id) gets silently dropped: 147 * uint8 #AdminUpdateType the server should answer for, only if #AdminUpdateFrequency #ADMIN_FREQUENCY_POLL is advertised in the PROTOCOL packet. Note integer type - see "Certain Packet Information" in docs/admin_network.md. 148 * uint32 ID relevant to the packet type, e.g. 149 * - the client ID for #ADMIN_UPDATE_CLIENT_INFO. Use UINT32_MAX to show all clients. 150 * - the company ID for #ADMIN_UPDATE_COMPANY_INFO. Use UINT32_MAX to show all companies. 151 * @param p The packet that was just received. 152 * @return The state the network should have. 153 */ 154 virtual NetworkRecvStatus Receive_ADMIN_POLL(Packet *p); 155 156 /** 157 * Send chat as the server: 158 * uint8 Action such as NETWORK_ACTION_CHAT_CLIENT (see #NetworkAction). 159 * uint8 Destination type such as DESTTYPE_BROADCAST (see #DestType). 160 * uint32 ID of the destination such as company or client id. 161 * string Message. 162 * @param p The packet that was just received. 163 * @return The state the network should have. 164 */ 165 virtual NetworkRecvStatus Receive_ADMIN_CHAT(Packet *p); 166 167 /** 168 * Send chat from the external source: 169 * string Name of the source this message came from. 170 * uint16 TextColour to use for the message. 171 * string Name of the user who sent the messsage. 172 * string Message. 173 * @param p The packet that was just received. 174 * @return The state the network should have. 175 */ 176 virtual NetworkRecvStatus Receive_ADMIN_EXTERNAL_CHAT(Packet *p); 177 178 /** 179 * Execute a command on the servers console: 180 * string Command to be executed. 181 * @param p The packet that was just received. 182 * @return The state the network should have. 183 */ 184 virtual NetworkRecvStatus Receive_ADMIN_RCON(Packet *p); 185 186 /** 187 * Send a JSON string to the current active GameScript. 188 * json JSON string for the GameScript. 189 * @param p The packet that was just received. 190 * @return The state the network should have. 191 */ 192 virtual NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet *p); 193 194 /** 195 * Ping the server, requiring the server to reply with a pong packet. 196 * uint32 Integer value to pass to the server, which is quoted in the reply. 197 * @param p The packet that was just received. 198 * @return The state the network should have. 199 */ 200 virtual NetworkRecvStatus Receive_ADMIN_PING(Packet *p); 201 202 /** 203 * The server is full (connection gets closed). 204 * @param p The packet that was just received. 205 * @return The state the network should have. 206 */ 207 virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p); 208 209 /** 210 * The source IP address is banned (connection gets closed). 211 * @param p The packet that was just received. 212 * @return The state the network should have. 213 */ 214 virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p); 215 216 /** 217 * An error was caused by this admin connection (connection gets closed). 218 * uint8 NetworkErrorCode the error caused. 219 * @param p The packet that was just received. 220 * @return The state the network should have. 221 */ 222 virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p); 223 224 /** 225 * Inform a just joined admin about the protocol specifics: 226 * uint8 Protocol version. 227 * bool Further protocol data follows (repeats through all update packet types). 228 * uint16 Update packet type. 229 * uint16 Frequencies allowed for this update packet (bitwise). 230 * @param p The packet that was just received. 231 * @return The state the network should have. 232 */ 233 virtual NetworkRecvStatus Receive_SERVER_PROTOCOL(Packet *p); 234 235 /** 236 * Welcome a connected admin to the game: 237 * string Name of the Server. 238 * string OpenTTD version string. 239 * bool Server is dedicated. 240 * string Name of the Map. 241 * uint32 Random seed of the Map. 242 * uint8 Landscape of the Map. 243 * uint32 Start date of the Map. 244 * uint16 Map width. 245 * uint16 Map height. 246 * @param p The packet that was just received. 247 * @return The state the network should have. 248 */ 249 virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p); 250 251 /** 252 * Notification about a newgame. 253 * @param p The packet that was just received. 254 * @return The state the network should have. 255 */ 256 virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p); 257 258 /** 259 * Notification about the server shutting down. 260 * @param p The packet that was just received. 261 * @return The state the network should have. 262 */ 263 virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p); 264 265 /** 266 * Send the current date of the game: 267 * uint32 Current game date. 268 * @param p The packet that was just received. 269 * @return The state the network should have. 270 */ 271 virtual NetworkRecvStatus Receive_SERVER_DATE(Packet *p); 272 273 /** 274 * Notification of a new client: 275 * uint32 ID of the new client. 276 * @param p The packet that was just received. 277 * @return The state the network should have. 278 */ 279 virtual NetworkRecvStatus Receive_SERVER_CLIENT_JOIN(Packet *p); 280 281 /** 282 * Client information of a specific client: 283 * uint32 ID of the client. 284 * string Network address of the client. 285 * string Name of the client. 286 * uint8 Language of the client. 287 * uint32 Date the client joined the game. 288 * uint8 ID of the company the client is playing as (255 for spectators). 289 * @param p The packet that was just received. 290 * @return The state the network should have. 291 */ 292 virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p); 293 294 /** 295 * Client update details on a specific client (e.g. after rename or move): 296 * uint32 ID of the client. 297 * string Name of the client. 298 * uint8 ID of the company the client is playing as (255 for spectators). 299 * @param p The packet that was just received. 300 * @return The state the network should have. 301 */ 302 virtual NetworkRecvStatus Receive_SERVER_CLIENT_UPDATE(Packet *p); 303 304 /** 305 * Notification about a client leaving the game. 306 * uint32 ID of the client that just left. 307 * @param p The packet that was just received. 308 * @return The state the network should have. 309 */ 310 virtual NetworkRecvStatus Receive_SERVER_CLIENT_QUIT(Packet *p); 311 312 /** 313 * Notification about a client error (and thus the clients disconnection). 314 * uint32 ID of the client that made the error. 315 * uint8 Error the client made (see NetworkErrorCode). 316 * @param p The packet that was just received. 317 * @return The state the network should have. 318 */ 319 virtual NetworkRecvStatus Receive_SERVER_CLIENT_ERROR(Packet *p); 320 321 /** 322 * Notification of a new company: 323 * uint8 ID of the new company. 324 * @param p The packet that was just received. 325 * @return The state the network should have. 326 */ 327 virtual NetworkRecvStatus Receive_SERVER_COMPANY_NEW(Packet *p); 328 329 /** 330 * Company information on a specific company: 331 * uint8 ID of the company. 332 * string Name of the company. 333 * string Name of the companies manager. 334 * uint8 Main company colour. 335 * bool Company is password protected. 336 * uint32 Year the company was inaugurated. 337 * bool Company is an AI. 338 * @param p The packet that was just received. 339 * @return The state the network should have. 340 */ 341 virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p); 342 343 /** 344 * Company information of a specific company: 345 * uint8 ID of the company. 346 * string Name of the company. 347 * string Name of the companies manager. 348 * uint8 Main company colour. 349 * bool Company is password protected. 350 * uint8 Quarters of bankruptcy. 351 * uint8 Owner of share 1. 352 * uint8 Owner of share 2. 353 * uint8 Owner of share 3. 354 * uint8 Owner of share 4. 355 * @param p The packet that was just received. 356 * @return The state the network should have. 357 */ 358 virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p); 359 360 /** 361 * Notification about a removed company (e.g. due to bankruptcy). 362 * uint8 ID of the company. 363 * uint8 Reason for being removed (see #AdminCompanyRemoveReason). 364 * @param p The packet that was just received. 365 * @return The state the network should have. 366 */ 367 virtual NetworkRecvStatus Receive_SERVER_COMPANY_REMOVE(Packet *p); 368 369 /** 370 * Economy update of a specific company: 371 * uint8 ID of the company. 372 * uint64 Money. 373 * uint64 Loan. 374 * int64 Income. 375 * uint16 Delivered cargo (this quarter). 376 * uint64 Company value (last quarter). 377 * uint16 Performance (last quarter). 378 * uint16 Delivered cargo (last quarter). 379 * uint64 Company value (previous quarter). 380 * uint16 Performance (previous quarter). 381 * uint16 Delivered cargo (previous quarter). 382 * @param p The packet that was just received. 383 * @return The state the network should have. 384 */ 385 virtual NetworkRecvStatus Receive_SERVER_COMPANY_ECONOMY(Packet *p); 386 387 /** 388 * Company statistics on stations and vehicles: 389 * uint8 ID of the company. 390 * uint16 Number of trains. 391 * uint16 Number of lorries. 392 * uint16 Number of busses. 393 * uint16 Number of planes. 394 * uint16 Number of ships. 395 * uint16 Number of train stations. 396 * uint16 Number of lorry stations. 397 * uint16 Number of bus stops. 398 * uint16 Number of airports and heliports. 399 * uint16 Number of harbours. 400 * @param p The packet that was just received. 401 * @return The state the network should have. 402 */ 403 virtual NetworkRecvStatus Receive_SERVER_COMPANY_STATS(Packet *p); 404 405 /** 406 * Send chat from the game into the admin network: 407 * uint8 Action such as NETWORK_ACTION_CHAT_CLIENT (see #NetworkAction). 408 * uint8 Destination type such as DESTTYPE_BROADCAST (see #DestType). 409 * uint32 ID of the client who sent this message. 410 * string Message. 411 * uint64 Money (only when it is a 'give money' action). 412 * @param p The packet that was just received. 413 * @return The state the network should have. 414 */ 415 virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p); 416 417 /** 418 * Result of an rcon command: 419 * uint16 Colour as it would be used on the server or a client. 420 * string Output of the executed command. 421 * @param p The packet that was just received. 422 * @return The state the network should have. 423 */ 424 virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p); 425 426 /** 427 * Send what would be printed on the server's console also into the admin network. 428 * string The origin of the text, e.g. "console" for console, or "net" for network related (debug) messages. 429 * string Text as found on the console of the server. 430 * @param p The packet that was just received. 431 * @return The state the network should have. 432 */ 433 virtual NetworkRecvStatus Receive_SERVER_CONSOLE(Packet *p); 434 435 /** 436 * Send DoCommand names to the bot upon request only. 437 * Multiple of these packets can follow each other in order to provide 438 * all known DoCommand names. 439 * 440 * NOTICE: Data provided with this packet is not stable and will not be 441 * treated as such. Do not rely on IDs or names to be constant 442 * across different versions / revisions of OpenTTD. 443 * Data provided in this packet is for logging purposes only. 444 * 445 * These three fields are repeated until the packet is full: 446 * bool Data to follow. 447 * uint16 ID of the DoCommand. 448 * string Name of DoCommand. 449 * @param p The packet that was just received. 450 * @return The state the network should have. 451 */ 452 virtual NetworkRecvStatus Receive_SERVER_CMD_NAMES(Packet *p); 453 454 /** 455 * Send incoming command packets to the admin network. 456 * This is for logging purposes only. 457 * 458 * NOTICE: Data provided with this packet is not stable and will not be 459 * treated as such. Do not rely on IDs or names to be constant 460 * across different versions / revisions of OpenTTD. 461 * Data provided in this packet is for logging purposes only. 462 * 463 * uint32 ID of the client sending the command. 464 * uint8 ID of the company (0..MAX_COMPANIES-1). 465 * uint16 ID of the command. 466 * uint32 P1 (variable data passed to the command). 467 * uint32 P2 (variable data passed to the command). 468 * uint32 Tile where this is taking place. 469 * string Text passed to the command. 470 * uint32 Frame of execution. 471 * @param p The packet that was just received. 472 * @return The state the network should have. 473 */ 474 virtual NetworkRecvStatus Receive_SERVER_CMD_LOGGING(Packet *p); 475 476 /** 477 * Send a ping-reply (pong) to the admin that sent us the ping packet. 478 * uint32 Integer identifier - should be the same as read from the admins ping packet. 479 * @param p The packet that was just received. 480 * @return The state the network should have. 481 */ 482 virtual NetworkRecvStatus Receive_SERVER_PONG(Packet *p); 483 484 /** 485 * Notify the admin connection that the rcon command has finished. 486 * string The command as requested by the admin connection. 487 * @param p The packet that was just received. 488 * @return The state the network should have. 489 */ 490 virtual NetworkRecvStatus Receive_SERVER_RCON_END(Packet *p); 491 492 NetworkRecvStatus HandlePacket(Packet *p); 493 public: 494 NetworkRecvStatus CloseConnection(bool error = true) override; 495 496 NetworkAdminSocketHandler(SOCKET s); 497 498 NetworkRecvStatus ReceivePackets(); 499 500 /** 501 * Get the status of the admin. 502 * @return The status of the admin. 503 */ GetAdminStatus()504 AdminStatus GetAdminStatus() const 505 { 506 return this->status; 507 } 508 }; 509 510 #endif /* NETWORK_CORE_TCP_ADMIN_H */ 511