OpenTTD Source: src/network/network_admin.h Source File

OpenTTD Source 15.0-RC2

network_admin.h
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef NETWORK_ADMIN_H
11#define NETWORK_ADMIN_H
12
13#include "network_internal.h"
14#include "core/tcp_listen.h"
15#include "core/tcp_admin.h"
16
17extern AdminID _redirect_console_to_admin;
18
19class ServerNetworkAdminSocketHandler;
21 using NetworkAdminSocketPool = Pool<ServerNetworkAdminSocketHandler, AdminID, 2, PoolType::NetworkAdmin>;
22extern NetworkAdminSocketPool _networkadminsocket_pool;
23
25 class ServerNetworkAdminSocketHandler : public NetworkAdminSocketPool::PoolItem<&_networkadminsocket_pool>, public NetworkAdminSocketHandler, public TCPListenHandler<ServerNetworkAdminSocketHandler, ADMIN_PACKET_SERVER_FULL, ADMIN_PACKET_SERVER_BANNED> {
26private:
27 std::unique_ptr<NetworkAuthenticationServerHandler> authentication_handler = nullptr;
28protected:
29 NetworkRecvStatus Receive_ADMIN_JOIN(Packet &p) override;
30 NetworkRecvStatus Receive_ADMIN_QUIT(Packet &p) override;
31 NetworkRecvStatus Receive_ADMIN_UPDATE_FREQUENCY(Packet &p) override;
32 NetworkRecvStatus Receive_ADMIN_POLL(Packet &p) override;
33 NetworkRecvStatus Receive_ADMIN_CHAT(Packet &p) override;
34 NetworkRecvStatus Receive_ADMIN_EXTERNAL_CHAT(Packet &p) override;
35 NetworkRecvStatus Receive_ADMIN_RCON(Packet &p) override;
36 NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet &p) override;
37 NetworkRecvStatus Receive_ADMIN_PING(Packet &p) override;
38 NetworkRecvStatus Receive_ADMIN_JOIN_SECURE(Packet &p) override;
39 NetworkRecvStatus Receive_ADMIN_AUTH_RESPONSE(Packet &p) override;
40
41 NetworkRecvStatus SendProtocol();
42 NetworkRecvStatus SendPong(uint32_t d1);
43 NetworkRecvStatus SendAuthRequest();
44 NetworkRecvStatus SendEnableEncryption();
45public:
46 std::array<AdminUpdateFrequencies, ADMIN_UPDATE_END> update_frequency{};
47 std::chrono::steady_clock::time_point connect_time{};
48 NetworkAddress address{};
49
50 ServerNetworkAdminSocketHandler(SOCKET s);
51 ~ServerNetworkAdminSocketHandler();
52
53 NetworkRecvStatus SendError(NetworkErrorCode error);
54 NetworkRecvStatus SendWelcome();
55 NetworkRecvStatus SendNewGame();
56 NetworkRecvStatus SendShutdown();
57
58 NetworkRecvStatus SendDate();
59 NetworkRecvStatus SendClientJoin(ClientID client_id);
60 NetworkRecvStatus SendClientInfo(const NetworkClientSocket *cs, const NetworkClientInfo *ci);
61 NetworkRecvStatus SendClientUpdate(const NetworkClientInfo *ci);
62 NetworkRecvStatus SendClientQuit(ClientID client_id);
63 NetworkRecvStatus SendClientError(ClientID client_id, NetworkErrorCode error);
64 NetworkRecvStatus SendCompanyNew(CompanyID company_id);
65 NetworkRecvStatus SendCompanyInfo(const Company *c);
66 NetworkRecvStatus SendCompanyUpdate(const Company *c);
67 NetworkRecvStatus SendCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr);
68 NetworkRecvStatus SendCompanyEconomy();
69 NetworkRecvStatus SendCompanyStats();
70
71 NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, std::string_view msg, int64_t data);
72 NetworkRecvStatus SendRcon(uint16_t colour, std::string_view command);
73 NetworkRecvStatus SendConsole(std::string_view origin, std::string_view command);
74 NetworkRecvStatus SendGameScript(std::string_view json);
75 NetworkRecvStatus SendCmdNames();
76 NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket &cp);
77 NetworkRecvStatus SendRconEnd(std::string_view command);
78
79 static void Send();
80 static void AcceptConnection(SOCKET s, const NetworkAddress &address);
81 static bool AllowConnection();
82 static void WelcomeAll();
83
88 static std::string_view GetName()
89 {
90 return "admin";
91 }
92
93 struct ServerNetworkAdminSocketHandlerFilter {
94 bool operator() (size_t index) { return ServerNetworkAdminSocketHandler::Get(index)->GetAdminStatus() == ADMIN_STATUS_ACTIVE; }
95 };
96
102 static Pool::IterateWrapperFiltered<ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter> IterateActive(size_t from = 0)
103 {
104 return Pool::IterateWrapperFiltered<ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter>(from, ServerNetworkAdminSocketHandlerFilter{});
105 }
106};
107
108void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client = false);
109void NetworkAdminClientUpdate(const NetworkClientInfo *ci);
110void NetworkAdminClientQuit(ClientID client_id);
111void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code);
112void NetworkAdminCompanyNew(const Company *company);
113void NetworkAdminCompanyUpdate(const Company *company);
114void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr);
115
116void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, std::string_view msg, int64_t data = 0, bool from_admin = false);
117void NetworkAdminUpdate(AdminUpdateFrequency freq);
118void NetworkServerSendAdminRcon(AdminID admin_index, TextColour colour_code, std::string_view string);
119void NetworkAdminConsole(std::string_view origin, std::string_view string);
120void NetworkAdminGameScript(std::string_view json);
121void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp);
122
123#endif /* NETWORK_ADMIN_H */
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
NetworkAdminSocketHandler
Main socket handler for admin related connections.
Definition tcp_admin.h:116
ServerNetworkAdminSocketHandler
Class for handling the server side of the game connection.
Definition network_admin.h:25
ServerNetworkAdminSocketHandler::Send
static void Send()
Send the packets for the server sockets.
Definition network_admin.cpp:97
ServerNetworkAdminSocketHandler::SendGameScript
NetworkRecvStatus SendGameScript(std::string_view json)
Send GameScript JSON output.
Definition network_admin.cpp:550
ServerNetworkAdminSocketHandler::SendPong
NetworkRecvStatus SendPong(uint32_t d1)
Send ping-reply (pong) to admin.
Definition network_admin.cpp:561
ServerNetworkAdminSocketHandler::SendDate
NetworkRecvStatus SendDate()
Tell the admin the date.
Definition network_admin.cpp:207
ServerNetworkAdminSocketHandler::Receive_ADMIN_JOIN_SECURE
NetworkRecvStatus Receive_ADMIN_JOIN_SECURE(Packet &p) override
Join the admin network using a secure authentication method: string Name of the application being use...
Definition network_admin.cpp:795
ServerNetworkAdminSocketHandler::SendClientUpdate
NetworkRecvStatus SendClientUpdate(const NetworkClientInfo *ci)
Send an update for some client's information.
Definition network_admin.cpp:260
ServerNetworkAdminSocketHandler::SendClientInfo
NetworkRecvStatus SendClientInfo(const NetworkClientSocket *cs, const NetworkClientInfo *ci)
Send an initial set of data from some client's information.
Definition network_admin.cpp:236
ServerNetworkAdminSocketHandler::SendRconEnd
NetworkRecvStatus SendRconEnd(std::string_view command)
Send a notification indicating the rcon command has completed.
Definition network_admin.cpp:461
ServerNetworkAdminSocketHandler::Receive_ADMIN_UPDATE_FREQUENCY
NetworkRecvStatus Receive_ADMIN_UPDATE_FREQUENCY(Packet &p) override
Register updates to be sent at certain frequencies (as announced in the PROTOCOL packet): uint16_t Up...
Definition network_admin.cpp:660
ServerNetworkAdminSocketHandler::SendNewGame
NetworkRecvStatus SendNewGame()
Tell the admin we started a new game.
Definition network_admin.cpp:191
ServerNetworkAdminSocketHandler::SendCompanyRemove
NetworkRecvStatus SendCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Tell the admin that a company got removed.
Definition network_admin.cpp:365
ServerNetworkAdminSocketHandler::Receive_ADMIN_RCON
NetworkRecvStatus Receive_ADMIN_RCON(Packet &p) override
Execute a command on the servers console: string Command to be executed.
Definition network_admin.cpp:487
ServerNetworkAdminSocketHandler::update_frequency
std::array< AdminUpdateFrequencies, ADMIN_UPDATE_END > update_frequency
Admin requested update intervals.
Definition network_admin.h:46
ServerNetworkAdminSocketHandler::SendClientError
NetworkRecvStatus SendClientError(ClientID client_id, NetworkErrorCode error)
Tell the admin that a client made an error.
Definition network_admin.cpp:292
ServerNetworkAdminSocketHandler::SendCmdNames
NetworkRecvStatus SendCmdNames()
Send the names of the commands.
Definition network_admin.cpp:572
ServerNetworkAdminSocketHandler::Receive_ADMIN_POLL
NetworkRecvStatus Receive_ADMIN_POLL(Packet &p) override
Poll the server for certain updates, an invalid poll (e.g.
Definition network_admin.cpp:680
ServerNetworkAdminSocketHandler::Receive_ADMIN_JOIN
NetworkRecvStatus Receive_ADMIN_JOIN(Packet &p) override
Join the admin network using an unsecured password exchange: string Unsecured password the server is ...
Definition network_admin.cpp:625
ServerNetworkAdminSocketHandler::SendShutdown
NetworkRecvStatus SendShutdown()
Tell the admin we're shutting down.
Definition network_admin.cpp:199
ServerNetworkAdminSocketHandler::SendCompanyStats
NetworkRecvStatus SendCompanyStats()
Send statistics about the companies.
Definition network_admin.cpp:409
ServerNetworkAdminSocketHandler::SendWelcome
NetworkRecvStatus SendWelcome()
Send a welcome message to the admin.
Definition network_admin.cpp:170
ServerNetworkAdminSocketHandler::SendCompanyInfo
NetworkRecvStatus SendCompanyInfo(const Company *c)
Send the admin some information about a company.
Definition network_admin.cpp:321
ServerNetworkAdminSocketHandler::WelcomeAll
static void WelcomeAll()
Send a Welcome packet to all connected admins.
Definition network_admin.cpp:1050
ServerNetworkAdminSocketHandler::AcceptConnection
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the acceptance of a connection.
Definition network_admin.cpp:116
ServerNetworkAdminSocketHandler::SendCmdLogging
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket &cp)
Send a command for logging purposes.
Definition network_admin.cpp:606
ServerNetworkAdminSocketHandler::authentication_handler
std::unique_ptr< NetworkAuthenticationServerHandler > authentication_handler
The handler for the authentication.
Definition network_admin.h:27
ServerNetworkAdminSocketHandler::AllowConnection
static bool AllowConnection()
Whether a connection is allowed or not at this moment.
Definition network_admin.cpp:91
ServerNetworkAdminSocketHandler::Receive_ADMIN_CHAT
NetworkRecvStatus Receive_ADMIN_CHAT(Packet &p) override
Send chat as the server: uint8_t Action such as NETWORK_ACTION_CHAT_CLIENT (see NetworkAction).
Definition network_admin.cpp:746
ServerNetworkAdminSocketHandler::IterateActive
static Pool::IterateWrapperFiltered< ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter > IterateActive(size_t from=0)
Returns an iterable ensemble of all active admin sockets.
Definition network_admin.h:102
ServerNetworkAdminSocketHandler::SendRcon
NetworkRecvStatus SendRcon(uint16_t colour, std::string_view command)
Send the reply of an rcon command.
Definition network_admin.cpp:476
ServerNetworkAdminSocketHandler::SendConsole
NetworkRecvStatus SendConsole(std::string_view origin, std::string_view command)
Send console output of other clients.
Definition network_admin.cpp:529
ServerNetworkAdminSocketHandler::SendCompanyEconomy
NetworkRecvStatus SendCompanyEconomy()
Send economic information of all companies.
Definition network_admin.cpp:378
ServerNetworkAdminSocketHandler::Receive_ADMIN_AUTH_RESPONSE
NetworkRecvStatus Receive_ADMIN_AUTH_RESPONSE(Packet &p) override
Admin responds to ADMIN_PACKET_SERVER_AUTH_REQUEST with the appropriate data given the agreed upon Ne...
Definition network_admin.cpp:845
ServerNetworkAdminSocketHandler::SendChat
NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, std::string_view msg, int64_t data)
Send a chat message.
Definition network_admin.cpp:443
ServerNetworkAdminSocketHandler::SendCompanyUpdate
NetworkRecvStatus SendCompanyUpdate(const Company *c)
Send an update about a company.
Definition network_admin.cpp:344
ServerNetworkAdminSocketHandler::address
NetworkAddress address
Address of the admin.
Definition network_admin.h:48
ServerNetworkAdminSocketHandler::SendClientQuit
NetworkRecvStatus SendClientQuit(ClientID client_id)
Tell the admin that a client quit.
Definition network_admin.cpp:277
ServerNetworkAdminSocketHandler::SendCompanyNew
NetworkRecvStatus SendCompanyNew(CompanyID company_id)
Tell the admin that a new company was founded.
Definition network_admin.cpp:307
ServerNetworkAdminSocketHandler::Receive_ADMIN_GAMESCRIPT
NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet &p) override
Send a JSON string to the current active GameScript.
Definition network_admin.cpp:501
ServerNetworkAdminSocketHandler::SendProtocol
NetworkRecvStatus SendProtocol()
Send the protocol version to the admin.
Definition network_admin.cpp:148
ServerNetworkAdminSocketHandler::SendClientJoin
NetworkRecvStatus SendClientJoin(ClientID client_id)
Tell the admin that a client joined.
Definition network_admin.cpp:221
ServerNetworkAdminSocketHandler::connect_time
std::chrono::steady_clock::time_point connect_time
Time of connection.
Definition network_admin.h:47
ServerNetworkAdminSocketHandler::Receive_ADMIN_PING
NetworkRecvStatus Receive_ADMIN_PING(Packet &p) override
Ping the server, requiring the server to reply with a pong packet.
Definition network_admin.cpp:513
ServerNetworkAdminSocketHandler::Receive_ADMIN_EXTERNAL_CHAT
NetworkRecvStatus Receive_ADMIN_EXTERNAL_CHAT(Packet &p) override
Send chat from the external source: string Name of the source this message came from.
Definition network_admin.cpp:772
ServerNetworkAdminSocketHandler::Receive_ADMIN_QUIT
NetworkRecvStatus Receive_ADMIN_QUIT(Packet &p) override
Notification to the server that this admin is quitting.
Definition network_admin.cpp:654
ServerNetworkAdminSocketHandler::~ServerNetworkAdminSocketHandler
~ServerNetworkAdminSocketHandler()
Clear everything related to this admin.
Definition network_admin.cpp:76
ServerNetworkAdminSocketHandler::SendError
NetworkRecvStatus SendError(NetworkErrorCode error)
Send an error to the admin.
Definition network_admin.cpp:130
ServerNetworkAdminSocketHandler::GetName
static std::string_view GetName()
Get the name used by the listener.
Definition network_admin.h:88
TCPListenHandler
Template for TCP listeners.
Definition tcp_listen.h:30
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:23
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
_networkadminsocket_pool
NetworkAdminSocketPool _networkadminsocket_pool("NetworkAdminSocket")
The pool with sockets/clients.
NetworkAdminClientUpdate
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
Notify the admin network of a client update (if they did opt in for the respective update).
Definition network_admin.cpp:896
NetworkAdminClientQuit
void NetworkAdminClientQuit(ClientID client_id)
Notify the admin network that a client quit (if they have opt in for the respective update).
Definition network_admin.cpp:909
NetworkAdminCompanyUpdate
void NetworkAdminCompanyUpdate(const Company *company)
Notify the admin network of company updates.
Definition network_admin.cpp:955
NetworkAdminUpdate
void NetworkAdminUpdate(AdminUpdateFrequency freq)
Send (push) updates to the admin network as they have registered for these updates.
Definition network_admin.cpp:1061
NetworkAdminCmdLogging
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket &cp)
Distribute CommandPacket details over the admin network for logging purposes.
Definition network_admin.cpp:1036
NetworkServerSendAdminRcon
void NetworkServerSendAdminRcon(AdminID admin_index, TextColour colour_code, std::string_view string)
Pass the rcon reply to the admin.
Definition network_admin.cpp:999
NetworkAdminClientError
void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code)
Notify the admin network of a client error (if they have opt in for the respective update).
Definition network_admin.cpp:923
NetworkAdminConsole
void NetworkAdminConsole(std::string_view origin, std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
Definition network_admin.cpp:1009
NetworkAdminGameScript
void NetworkAdminGameScript(std::string_view json)
Send GameScript JSON to the admin network (if they did opt in for the respective update).
Definition network_admin.cpp:1022
NetworkAdminCompanyNew
void NetworkAdminCompanyNew(const Company *company)
Notify the admin network of a new company.
Definition network_admin.cpp:936
NetworkAdminChat
void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, std::string_view msg, int64_t data=0, bool from_admin=false)
Send chat to the admin network (if they did opt in for the respective update).
Definition network_admin.cpp:982
NetworkAdminClientInfo
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client=false)
Notify the admin network of a new client (if they did opt in for the respective update).
Definition network_admin.cpp:880
NetworkAdminCompanyRemove
void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Notify the admin network of a company to be removed (including the reason why).
Definition network_admin.cpp:971
_redirect_console_to_admin
AdminID _redirect_console_to_admin
Redirection of the (remote) console to the admin.
Definition network_admin.cpp:34
network_internal.h
Variables and function used internally.
DestType
DestType
Destination of our chat messages.
Definition network_type.h:68
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition network_type.h:99
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition network_type.h:79
ClientID
ClientID
'Unique' identifier to be given to clients
Definition network_type.h:43
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition network_internal.h:95
Company
Definition company_base.h:127
NetworkClientInfo
Container for all information known about a client.
Definition network_base.h:24
Packet
Internal entity of a packet.
Definition packet.h:43
PoolID
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
Pool::IterateWrapperFiltered
Definition pool_type.hpp:271
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool::PoolItem<&_networkadminsocket_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem<&_networkadminsocket_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool
Base class for all pools.
Definition pool_type.hpp:133
ServerNetworkAdminSocketHandler::ServerNetworkAdminSocketHandlerFilter
Definition network_admin.h:93
tcp_admin.h
Basic functions to receive and send TCP packets to and from the admin network.
AdminCompanyRemoveReason
AdminCompanyRemoveReason
Reasons for removing a company - communicated to admins.
Definition tcp_admin.h:107
AdminUpdateFrequency
AdminUpdateFrequency
Update frequencies an admin can register.
Definition tcp_admin.h:95
ADMIN_STATUS_ACTIVE
@ ADMIN_STATUS_ACTIVE
The admin is active.
Definition tcp_admin.h:75
tcp_listen.h
Basic functions to listen for TCP connections.