OpenTTD
tcp_admin.cpp
Go to the documentation of this file.
1 /* $Id: tcp_admin.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
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  */
9 
14 #ifdef ENABLE_NETWORK
15 
16 #include "../../stdafx.h"
17 
18 #include "../network_internal.h"
19 #include "tcp_admin.h"
20 #include "../../debug.h"
21 
22 #include "../../safeguards.h"
23 
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);
29 
35 {
36  this->sock = s;
37  this->admin_name[0] = '\0';
38  this->admin_version[0] = '\0';
39 }
40 
41 NetworkAdminSocketHandler::~NetworkAdminSocketHandler()
42 {
43 }
44 
46 {
47  delete this;
49 }
50 
57 {
59 
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);
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);
68  case ADMIN_PACKET_ADMIN_PING: return this->Receive_ADMIN_PING(p);
69 
70  case ADMIN_PACKET_SERVER_FULL: return this->Receive_SERVER_FULL(p);
72  case ADMIN_PACKET_SERVER_ERROR: return this->Receive_SERVER_ERROR(p);
77 
78  case ADMIN_PACKET_SERVER_DATE: return this->Receive_SERVER_DATE(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);
96  case ADMIN_PACKET_SERVER_PONG: return this->Receive_SERVER_PONG(p);
97 
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);
103  }
104 
105  this->CloseConnection();
107  }
108 }
109 
118 {
119  Packet *p;
120  while ((p = this->ReceivePacket()) != NULL) {
121  NetworkRecvStatus res = this->HandlePacket(p);
122  if (res != NETWORK_RECV_STATUS_OKAY) return res;
123  }
124 
126 }
127 
134 {
135  DEBUG(net, 0, "[tcp/admin] received illegal packet type %d from admin %s (%s)", type, this->admin_name, this->admin_version);
137 }
138 
147 
155 
175 
176 #endif /* ENABLE_NETWORK */
Everything is okay.
Definition: core.h:27
char admin_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the admin.
Definition: tcp_admin.h:116
The admin sends a chat message to be distributed.
Definition: tcp_admin.h:33
virtual NetworkRecvStatus Receive_ADMIN_POLL(Packet *p)
Poll the server for certain updates, an invalid poll (e.g.
Definition: tcp_admin.cpp:142
virtual NetworkRecvStatus Receive_ADMIN_PING(Packet *p)
Ping the server, requiring the server to reply with a pong packet.
Definition: tcp_admin.cpp:146
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
Definition: core.h:71
SOCKET sock
The socket currently connected to.
Definition: tcp.h:36
Internal entity of a packet.
Definition: packet.h:44
The server replies to a ping request from the admin.
Definition: tcp_admin.h:65
virtual NetworkRecvStatus Receive_SERVER_PROTOCOL(Packet *p)
Inform a just joined admin about the protocol specifics: uint8 Protocol version.
Definition: tcp_admin.cpp:151
virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p)
Result of an rcon command: uint16 Colour as it would be used on the server or a client.
Definition: tcp_admin.cpp:169
The admin explicitly polls for a piece of information.
Definition: tcp_admin.h:32
The server tells the admin its going to start a new game.
Definition: tcp_admin.h:43
The server tells the admin its shutting down.
Definition: tcp_admin.h:44
The server received a chat message and relays it.
Definition: tcp_admin.h:58
The server tells the admin that a new company has started.
Definition: tcp_admin.h:52
The server tells the admin its protocol version.
Definition: tcp_admin.h:41
The company is manually removed.
Definition: company_type.h:60
The admin announces and authenticates itself to the server.
Definition: tcp_admin.h:29
The server tells the admin that a client quit.
Definition: tcp_admin.h:50
virtual NetworkRecvStatus Receive_SERVER_COMPANY_STATS(Packet *p)
Company statistics on stations and vehicles: uint8 ID of the company.
Definition: tcp_admin.cpp:167
An invalid marker for admin packets.
Definition: tcp_admin.h:67
virtual NetworkRecvStatus Receive_SERVER_DATE(Packet *p)
Send the current date of the game: uint32 Current game date.
Definition: tcp_admin.cpp:156
virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p)
An error was caused by this admin connection (connection gets closed).
Definition: tcp_admin.cpp:150
The server tells the admin an error has occurred.
Definition: tcp_admin.h:40
char admin_version[NETWORK_REVISION_LENGTH]
Version string of the admin.
Definition: tcp_admin.h:117
virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p)
Notification about the server shutting down.
Definition: tcp_admin.cpp:154
virtual NetworkRecvStatus Receive_SERVER_CLIENT_QUIT(Packet *p)
Notification about a client leaving the game.
Definition: tcp_admin.cpp:160
The admin sends a JSON string for the GameScript.
Definition: tcp_admin.h:35
NetworkRecvStatus ReceiveInvalidPacket(PacketAdminType type)
Helper for logging receiving invalid packets.
Definition: tcp_admin.cpp:133
The server tells the admin what the current game date is.
Definition: tcp_admin.h:46
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:149
virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p)
Client information of a specific client: uint32 ID of the client.
Definition: tcp_admin.cpp:158
virtual NetworkRecvStatus Receive_SERVER_CLIENT_ERROR(Packet *p)
Notification about a client error (and thus the clients disconnection).
Definition: tcp_admin.cpp:161
The server gives the admin some statistics about a company.
Definition: tcp_admin.h:57
NetworkRecvStatus ReceivePackets()
Do the actual receiving of packets.
Definition: tcp_admin.cpp:117
The company is removed due to autoclean.
Definition: company_type.h:61
The server&#39;s reply to a remove console command.
Definition: tcp_admin.h:59
virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p)
The server is full (connection gets closed).
Definition: tcp_admin.cpp:148
The server gives the admin an information update on a client.
Definition: tcp_admin.h:49
NetworkRecvStatus HandlePacket(Packet *p)
Handle the given packet, i.e.
Definition: tcp_admin.cpp:56
virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p)
Company information of a specific company: uint8 ID of the company.
Definition: tcp_admin.cpp:164
The admin is not connected nor active.
Definition: tcp_admin.h:72
The server tells the admin it cannot accept the admin.
Definition: tcp_admin.h:38
virtual NetworkRecvStatus Receive_SERVER_CONSOLE(Packet *p)
Send what would be printed on the server&#39;s console also into the admin network.
Definition: tcp_admin.cpp:170
virtual NetworkRecvStatus Receive_ADMIN_CHAT(Packet *p)
Send chat as the server: uint8 Action such as NETWORK_ACTION_CHAT_CLIENT (see NetworkAction).
Definition: tcp_admin.cpp:143
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:26
The server indicates that the remote console command has completed.
Definition: tcp_admin.h:64
The company is manually removed.
Definition: tcp_admin.h:106
Sentinel for end.
Definition: company_type.h:64
virtual NetworkRecvStatus Receive_SERVER_COMPANY_REMOVE(Packet *p)
Notification about a removed company (e.g.
Definition: tcp_admin.cpp:165
virtual NetworkRecvStatus Receive_ADMIN_RCON(Packet *p)
Execute a command on the servers console: string Command to be executed.
Definition: tcp_admin.cpp:144
virtual NetworkRecvStatus Receive_ADMIN_JOIN(Packet *p)
Join the admin network: string Password the server is expecting for this network. ...
Definition: tcp_admin.cpp:139
The server tells the admin that a company was removed.
Definition: tcp_admin.h:55
The server sends out the names of the DoCommands to the admins.
Definition: tcp_admin.h:61
virtual NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet *p)
Send a JSON string to the current active GameScript.
Definition: tcp_admin.cpp:145
The server tells the admin that a client caused an error.
Definition: tcp_admin.h:51
virtual NetworkRecvStatus Receive_SERVER_PONG(Packet *p)
Send a ping-reply (pong) to the admin that sent us the ping packet.
Definition: tcp_admin.cpp:174
The admin tells the server that it is quitting.
Definition: tcp_admin.h:30
The server gives the admin the data that got printed to its console.
Definition: tcp_admin.h:60
The connection is &#39;just&#39; lost.
Definition: core.h:31
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
virtual NetworkRecvStatus Receive_SERVER_CLIENT_UPDATE(Packet *p)
Client update details on a specific client (e.g.
Definition: tcp_admin.cpp:159
The server gives the admin an information update on a company.
Definition: tcp_admin.h:54
virtual NetworkRecvStatus Receive_SERVER_CMD_NAMES(Packet *p)
Send DoCommand names to the bot upon request only.
Definition: tcp_admin.cpp:171
virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p)
Send chat from the game into the admin network: uint8 Action such as NETWORK_ACTION_CHAT_CLIENT (see ...
Definition: tcp_admin.cpp:168
virtual NetworkRecvStatus Receive_ADMIN_UPDATE_FREQUENCY(Packet *p)
Register updates to be sent at certain frequencies (as announced in the PROTOCOL packet): uint16 Upda...
Definition: tcp_admin.cpp:141
The company is removed due to autoclean.
Definition: tcp_admin.h:107
PacketAdminType
Enum with types of TCP packets specific to the admin network.
Definition: tcp_admin.h:28
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p)
Company information on a specific company: uint8 ID of the company.
Definition: tcp_admin.cpp:163
virtual NetworkRecvStatus Receive_SERVER_COMPANY_NEW(Packet *p)
Notification of a new company: uint8 ID of the new company.
Definition: tcp_admin.cpp:162
virtual NetworkRecvStatus Receive_ADMIN_QUIT(Packet *p)
Notification to the server that this admin is quitting.
Definition: tcp_admin.cpp:140
The server tells the admin that a client has joined.
Definition: tcp_admin.h:47
virtual NetworkRecvStatus Receive_SERVER_RCON_END(Packet *p)
Notify the admin connection that the rcon command has finished.
Definition: tcp_admin.cpp:173
The company went belly-up.
Definition: company_type.h:62
The admin sends a remote console command.
Definition: tcp_admin.h:34
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
The admin sends a ping to the server, expecting a ping-reply (PONG) packet.
Definition: tcp_admin.h:36
virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p)
Welcome a connected admin to the game: string Name of the Server (e.g.
Definition: tcp_admin.cpp:152
The server gives the admin information about a client.
Definition: tcp_admin.h:48
NetworkAdminSocketHandler(SOCKET s)
Create the admin handler for the given socket.
Definition: tcp_admin.cpp:34
The server welcomes the admin to a game.
Definition: tcp_admin.h:42
The company went belly-up.
Definition: tcp_admin.h:108
We apparently send a malformed packet.
Definition: core.h:32
virtual NetworkRecvStatus Receive_SERVER_COMPANY_ECONOMY(Packet *p)
Economy update of a specific company: uint8 ID of the company.
Definition: tcp_admin.cpp:166
virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p)
The source IP address is banned (connection gets closed).
Definition: tcp_admin.cpp:149
The server gives the admin copies of incoming command packets.
Definition: tcp_admin.h:62
Basic functions to receive and send TCP packets to and from the admin network.
virtual NetworkRecvStatus Receive_SERVER_CLIENT_JOIN(Packet *p)
Notification of a new client: uint32 ID of the new client.
Definition: tcp_admin.cpp:157
virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p)
Notification about a newgame.
Definition: tcp_admin.cpp:153
Sentinel for end.
Definition: tcp_admin.h:110
The server gives the admin some economy related company information.
Definition: tcp_admin.h:56
The admin tells the server the update frequency of a particular piece of information.
Definition: tcp_admin.h:31
NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: tcp_admin.cpp:45
The server tells the admin it is banned.
Definition: tcp_admin.h:39
The server gives the admin information about a company.
Definition: tcp_admin.h:53
virtual NetworkRecvStatus Receive_SERVER_CMD_LOGGING(Packet *p)
Send incoming command packets to the admin network.
Definition: tcp_admin.cpp:172