diff options
Diffstat (limited to 'src/network/core')
-rw-r--r-- | src/network/core/config.h | 2 | ||||
-rw-r--r-- | src/network/core/tcp_admin.cpp | 119 | ||||
-rw-r--r-- | src/network/core/tcp_admin.h | 137 |
3 files changed, 258 insertions, 0 deletions
diff --git a/src/network/core/config.h b/src/network/core/config.h index 2df6162e2..584e0382a 100644 --- a/src/network/core/config.h +++ b/src/network/core/config.h @@ -29,10 +29,12 @@ static const uint16 NETWORK_MASTER_SERVER_PORT = 3978; ///< The default port static const uint16 NETWORK_CONTENT_SERVER_PORT = 3978; ///< The default port of the content server (TCP) static const uint16 NETWORK_CONTENT_MIRROR_PORT = 80; ///< The default port of the content mirror (TCP) static const uint16 NETWORK_DEFAULT_PORT = 3979; ///< The default port of the game server (TCP & UDP) +static const uint16 NETWORK_ADMIN_PORT = 3977; ///< The default port for admin network static const uint16 NETWORK_DEFAULT_DEBUGLOG_PORT = 3982; ///< The default port debug-log is sent too (TCP) static const uint16 SEND_MTU = 1460; ///< Number of bytes we can pack in a single packet +static const byte NETWORK_GAME_ADMIN_VERSION = 1; ///< What version of the admin network do we use? static const byte NETWORK_GAME_INFO_VERSION = 4; ///< What version of game-info do we use? static const byte NETWORK_COMPANY_INFO_VERSION = 6; ///< What version of company info is this? static const byte NETWORK_MASTER_SERVER_VERSION = 2; ///< What version of master-server-protocol do we use? diff --git a/src/network/core/tcp_admin.cpp b/src/network/core/tcp_admin.cpp new file mode 100644 index 000000000..2d68fc68b --- /dev/null +++ b/src/network/core/tcp_admin.cpp @@ -0,0 +1,119 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** + * @file tcp_admin.cpp Basic functions to receive and send TCP packets to and from the admin network. + */ + +#ifdef ENABLE_NETWORK + +#include "../../stdafx.h" + +#include "../network_internal.h" +#include "tcp_admin.h" +#include "../../debug.h" + +NetworkAdminSocketHandler::NetworkAdminSocketHandler(SOCKET s) +{ + this->sock = s; +} + +NetworkAdminSocketHandler::~NetworkAdminSocketHandler() +{ +} + +NetworkRecvStatus NetworkAdminSocketHandler::CloseConnection(bool error) +{ + delete this; + return NETWORK_RECV_STATUS_CONN_LOST; +} + +/** + * Defines a simple (switch) case for each network packet. + * @param type the packet type to create the case for. + */ +#define ADMIN_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break; + +/** + * Handle the given packet, i.e. pass it to the right parser receive command. + * @param p the packet to handle. + * @return #NetworkRecvStatus of handling. + */ +NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p) +{ + PacketAdminType type = (PacketAdminType)p->Recv_uint8(); + + switch (this->HasClientQuit() ? INVALID_ADMIN_PACKET : type) { + ADMIN_COMMAND(ADMIN_PACKET_ADMIN_JOIN) + ADMIN_COMMAND(ADMIN_PACKET_ADMIN_QUIT) + + ADMIN_COMMAND(ADMIN_PACKET_SERVER_FULL) + ADMIN_COMMAND(ADMIN_PACKET_SERVER_BANNED) + ADMIN_COMMAND(ADMIN_PACKET_SERVER_ERROR) + ADMIN_COMMAND(ADMIN_PACKET_SERVER_PROTOCOL) + ADMIN_COMMAND(ADMIN_PACKET_SERVER_WELCOME) + ADMIN_COMMAND(ADMIN_PACKET_SERVER_NEWGAME) + ADMIN_COMMAND(ADMIN_PACKET_SERVER_SHUTDOWN) + + default: + if (this->HasClientQuit()) { + DEBUG(net, 0, "[tcp/admin] received invalid packet type %d from '%s' (%s)", type, this->admin_name, this->admin_version); + } else { + DEBUG(net, 0, "[tcp/admin] received illegal packet from '%s' (%s)", this->admin_name, this->admin_version); + } + + this->CloseConnection(); + return NETWORK_RECV_STATUS_MALFORMED_PACKET; + } +} + +/** + * Do the actual receiving of packets. + * As long as HandlePacket returns OKAY packets are handled. Upon + * failure, or no more packets to process the last result of + * HandlePacket is returned. + * @return #NetworkRecvStatus of the last handled packet. + */ +NetworkRecvStatus NetworkAdminSocketHandler::Recv_Packets() +{ + Packet *p; + while ((p = this->Recv_Packet()) != NULL) { + NetworkRecvStatus res = HandlePacket(p); + if (res != NETWORK_RECV_STATUS_OKAY) return res; + } + + return NETWORK_RECV_STATUS_OKAY; +} + +/** + * Create stub implementations for all receive commands that only + * show a warning that the given command is not available for the + * socket where the packet came from. + * @param type the packet type to create the stub for. + */ +#define DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(type) \ +NetworkRecvStatus NetworkAdminSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \ +{ \ + DEBUG(net, 0, "[tcp/admin] received illegal packet type %d from admin %s (%s)", \ + type, this->admin_name, this->admin_version); \ + return NETWORK_RECV_STATUS_MALFORMED_PACKET; \ +} + +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_JOIN) +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_QUIT) + +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_FULL) +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_BANNED) +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_ERROR) +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_PROTOCOL) +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_WELCOME) +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_NEWGAME) +DEFINE_UNAVAILABLE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_SHUTDOWN) + +#endif /* ENABLE_NETWORK */ diff --git a/src/network/core/tcp_admin.h b/src/network/core/tcp_admin.h new file mode 100644 index 000000000..dae41c5f5 --- /dev/null +++ b/src/network/core/tcp_admin.h @@ -0,0 +1,137 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** + * @file tcp_admin.h Basic functions to receive and send TCP packets to and from the admin network. + */ + +#ifndef NETWORK_CORE_TCP_ADMIN_H +#define NETWORK_CORE_TCP_ADMIN_H + +#include "os_abstraction.h" +#include "tcp.h" +#include "../network_type.h" +#include "../../core/pool_type.hpp" + +#ifdef ENABLE_NETWORK + +/** + * Enum with types of TCP packets specific to the admin network. + * This protocol may only be extended to ensure stability. + */ +enum PacketAdminType { + ADMIN_PACKET_ADMIN_JOIN, ///< The admin announces and authenticates itself to the server. + ADMIN_PACKET_ADMIN_QUIT, ///< The admin tells the server that it is quitting. + + ADMIN_PACKET_SERVER_FULL = 100, ///< The server tells the admin it cannot accept the admin. + ADMIN_PACKET_SERVER_BANNED, ///< The server tells the admin it is banned. + ADMIN_PACKET_SERVER_ERROR, ///< The server tells the admin an error has occurred. + ADMIN_PACKET_SERVER_PROTOCOL, ///< The server tells the admin its protocol version. + ADMIN_PACKET_SERVER_WELCOME, ///< The server welcomes the admin to a game. + ADMIN_PACKET_SERVER_NEWGAME, ///< The server tells the admin its going to start a new game. + ADMIN_PACKET_SERVER_SHUTDOWN, ///< The server tells the admin its shutting down. + + INVALID_ADMIN_PACKET = 0xFF, ///< An invalid marker for admin packets. +}; + +/** Status of an admin. */ +enum AdminStatus { + ADMIN_STATUS_INACTIVE, ///< The admin is not connected nor active. + ADMIN_STATUS_ACTIVE, ///< The admin is active. + ADMIN_STATUS_END ///< Must ALWAYS be on the end of this list!! (period) +}; + +#define DECLARE_ADMIN_RECEIVE_COMMAND(type) virtual NetworkRecvStatus NetworkPacketReceive_## type ##_command(Packet *p) +#define DEF_ADMIN_RECEIVE_COMMAND(cls, type) NetworkRecvStatus cls ##NetworkAdminSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p) + +/** Main socket handler for admin related connections. */ +class NetworkAdminSocketHandler : public NetworkTCPSocketHandler { +protected: + char admin_name[NETWORK_CLIENT_NAME_LENGTH]; ///< Name of the admin. + char admin_version[NETWORK_REVISION_LENGTH]; ///< Version string of the admin. + AdminStatus status; ///< Status of this admin. + + /** + * Join the admin network: + * string Password the server is expecting for this network. + * string Name of the application being used to connect. + * string Version string of the application being used to connect. + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_JOIN); + + /** + * Notification to the server that this admin is quitting. + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_ADMIN_QUIT); + + /** + * The server is full (connection gets closed). + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_FULL); + + /** + * The source IP address is banned (connection gets closed). + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_BANNED); + + /** + * An error was caused by this admin connection (connection gets closed). + * uint8 NetworkErrorCode the error caused. + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_ERROR); + + /** + * Inform a just joined admin about the protocol specifics: + * uint8 Protocol version. + * bool Further protocol data follows (repeats through all update packet types). + * uint16 Update packet type. + * uint16 Frequencies allowed for this update packet (bitwise). + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_PROTOCOL); + + /** + * Welcome a connected admin to the game: + * string Name of the Server (e.g. as advertised to master server). + * string OpenTTD version string. + * bool Server is dedicated. + * string Name of the Map. + * uint32 Random seed of the Map. + * uint8 Landscape of the Map. + * uint32 Start date of the Map. + * uint16 Map width. + * uint16 Map height. + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_WELCOME); + + /** + * Notification about a newgame. + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_NEWGAME); + + /** + * Notification about the server shutting down. + */ + DECLARE_ADMIN_RECEIVE_COMMAND(ADMIN_PACKET_SERVER_SHUTDOWN); + + NetworkRecvStatus HandlePacket(Packet *p); +public: + NetworkRecvStatus CloseConnection(bool error = true); + + NetworkAdminSocketHandler(SOCKET s); + ~NetworkAdminSocketHandler(); + + NetworkRecvStatus Recv_Packets(); + + const char *Recv_Command(Packet *p, CommandPacket *cp); + void Send_Command(Packet *p, const CommandPacket *cp); +}; + +#endif /* ENABLE_NETWORK */ + +#endif /* NETWORK_CORE_TCP_ADMIN_H */ |