OpenTTD
network_admin.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 
12 #include "../stdafx.h"
13 #include "../strings_func.h"
14 #include "../date_func.h"
15 #include "network_admin.h"
16 #include "network_base.h"
17 #include "network_server.h"
18 #include "../command_func.h"
19 #include "../company_base.h"
20 #include "../console_func.h"
21 #include "../core/pool_func.hpp"
22 #include "../map_func.h"
23 #include "../rev.h"
24 #include "../game/game.hpp"
25 
26 #include "../safeguards.h"
27 
28 
29 /* This file handles all the admin network commands. */
30 
33 
36 
39 INSTANTIATE_POOL_METHODS(NetworkAdminSocket)
40 
41 
42 static const int ADMIN_AUTHORISATION_TIMEOUT = 10000;
43 
44 
57 };
60 
66 {
70 }
71 
76 {
78  DEBUG(net, 1, "[admin] '%s' (%s) has disconnected", this->admin_name, this->admin_version);
80 }
81 
87 {
89  /* We can't go over the MAX_ADMINS limit here. However, if we accept
90  * the connection, there has to be space in the pool. */
93  return accept;
94 }
95 
98 {
102  DEBUG(net, 1, "[admin] Admin did not send its authorisation within %d seconds", ADMIN_AUTHORISATION_TIMEOUT / 1000);
103  as->CloseConnection(true);
104  continue;
105  }
106  if (as->writable) {
107  as->SendPackets();
108  }
109  }
110 }
111 
118 {
120  as->address = address; // Save the IP of the client
121 }
122 
123 /***********
124  * Sending functions for admin network
125  ************/
126 
132 {
134 
135  p->Send_uint8(error);
136  this->SendPacket(p);
137 
138  char str[100];
139  StringID strid = GetNetworkErrorMsg(error);
140  GetString(str, strid, lastof(str));
141 
142  DEBUG(net, 1, "[admin] the admin '%s' (%s) made an error and has been disconnected. Reason: '%s'", this->admin_name, this->admin_version, str);
143 
144  return this->CloseConnection(true);
145 }
146 
149 {
151 
152  /* announce the protocol version */
154 
155  for (int i = 0; i < ADMIN_UPDATE_END; i++) {
156  p->Send_bool (true);
157  p->Send_uint16(i);
159  }
160 
161  p->Send_bool(false);
162  this->SendPacket(p);
163 
164  return this->SendWelcome();
165 }
166 
169 {
171 
175 
180  p->Send_uint16(MapSizeX());
181  p->Send_uint16(MapSizeY());
182 
183  this->SendPacket(p);
184 
186 }
187 
190 {
192  this->SendPacket(p);
194 }
195 
198 {
200  this->SendPacket(p);
202 }
203 
206 {
208 
209  p->Send_uint32(_date);
210  this->SendPacket(p);
211 
213 }
214 
220 {
222 
223  p->Send_uint32(client_id);
224  this->SendPacket(p);
225 
227 }
228 
235 {
236  /* Only send data when we're a proper client, not just someone trying to query the server. */
237  if (ci == nullptr) return NETWORK_RECV_STATUS_OKAY;
238 
240 
241  p->Send_uint32(ci->client_id);
242  p->Send_string(cs == nullptr ? "" : const_cast<NetworkAddress &>(cs->client_address).GetHostname());
243  p->Send_string(ci->client_name);
244  p->Send_uint8 (ci->client_lang);
245  p->Send_uint32(ci->join_date);
246  p->Send_uint8 (ci->client_playas);
247 
248  this->SendPacket(p);
249 
251 }
252 
253 
259 {
261 
262  p->Send_uint32(ci->client_id);
263  p->Send_string(ci->client_name);
264  p->Send_uint8 (ci->client_playas);
265 
266  this->SendPacket(p);
267 
269 }
270 
276 {
278 
279  p->Send_uint32(client_id);
280  this->SendPacket(p);
281 
283 }
284 
291 {
293 
294  p->Send_uint32(client_id);
295  p->Send_uint8 (error);
296  this->SendPacket(p);
297 
299 }
300 
306 {
308  p->Send_uint8(company_id);
309 
310  this->SendPacket(p);
311 
313 }
314 
320 {
321  char company_name[NETWORK_COMPANY_NAME_LENGTH];
322  char manager_name[NETWORK_COMPANY_NAME_LENGTH];
323 
324  SetDParam(0, c->index);
325  GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
326 
327  SetDParam(0, c->index);
328  GetString(manager_name, STR_PRESIDENT_NAME, lastof(manager_name));
329 
331 
332  p->Send_uint8 (c->index);
333  p->Send_string(company_name);
334  p->Send_string(manager_name);
335  p->Send_uint8 (c->colour);
338  p->Send_bool (c->is_ai);
339  p->Send_uint8 (CeilDiv(c->months_of_bankruptcy, 3)); // send as quarters_of_bankruptcy
340 
341  for (size_t i = 0; i < lengthof(c->share_owners); i++) {
342  p->Send_uint8(c->share_owners[i]);
343  }
344 
345  this->SendPacket(p);
346 
348 }
349 
350 
356 {
357  char company_name[NETWORK_COMPANY_NAME_LENGTH];
358  char manager_name[NETWORK_COMPANY_NAME_LENGTH];
359 
360  SetDParam(0, c->index);
361  GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
362 
363  SetDParam(0, c->index);
364  GetString(manager_name, STR_PRESIDENT_NAME, lastof(manager_name));
365 
367 
368  p->Send_uint8 (c->index);
369  p->Send_string(company_name);
370  p->Send_string(manager_name);
371  p->Send_uint8 (c->colour);
373  p->Send_uint8 (CeilDiv(c->months_of_bankruptcy, 3)); // send as quarters_of_bankruptcy
374 
375  for (size_t i = 0; i < lengthof(c->share_owners); i++) {
376  p->Send_uint8(c->share_owners[i]);
377  }
378 
379  this->SendPacket(p);
380 
382 }
383 
390 {
392 
393  p->Send_uint8(company_id);
394  p->Send_uint8(acrr);
395 
396  this->SendPacket(p);
397 
399 }
400 
403 {
404  const Company *company;
405  FOR_ALL_COMPANIES(company) {
406  /* Get the income. */
407  Money income = 0;
408  for (uint i = 0; i < lengthof(company->yearly_expenses[0]); i++) {
409  income -= company->yearly_expenses[0][i];
410  }
411 
413 
414  p->Send_uint8(company->index);
415 
416  /* Current information. */
417  p->Send_uint64(company->money);
418  p->Send_uint64(company->current_loan);
419  p->Send_uint64(income);
420  p->Send_uint16(min(UINT16_MAX, company->cur_economy.delivered_cargo.GetSum<OverflowSafeInt64>()));
421 
422  /* Send stats for the last 2 quarters. */
423  for (uint i = 0; i < 2; i++) {
424  p->Send_uint64(company->old_economy[i].company_value);
426  p->Send_uint16(min(UINT16_MAX, company->old_economy[i].delivered_cargo.GetSum<OverflowSafeInt64>()));
427  }
428 
429  this->SendPacket(p);
430  }
431 
432 
434 }
435 
438 {
439  /* Fetch the latest version of the stats. */
440  NetworkCompanyStats company_stats[MAX_COMPANIES];
441  NetworkPopulateCompanyStats(company_stats);
442 
443  const Company *company;
444 
445  /* Go through all the companies. */
446  FOR_ALL_COMPANIES(company) {
448 
449  /* Send the information. */
450  p->Send_uint8(company->index);
451 
452  for (uint i = 0; i < NETWORK_VEH_END; i++) {
453  p->Send_uint16(company_stats[company->index].num_vehicle[i]);
454  }
455 
456  for (uint i = 0; i < NETWORK_VEH_END; i++) {
457  p->Send_uint16(company_stats[company->index].num_station[i]);
458  }
459 
460  this->SendPacket(p);
461  }
462 
464 }
465 
474 NetworkRecvStatus ServerNetworkAdminSocketHandler::SendChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data)
475 {
477 
478  p->Send_uint8 (action);
479  p->Send_uint8 (desttype);
480  p->Send_uint32(client_id);
481  p->Send_string(msg);
482  p->Send_uint64(data);
483 
484  this->SendPacket(p);
486 }
487 
493 {
495 
496  p->Send_string(command);
497  this->SendPacket(p);
498 
500 }
501 
508 {
510 
511  p->Send_uint16(colour);
512  p->Send_string(result);
513  this->SendPacket(p);
514 
516 }
517 
519 {
520  if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
521 
522  char command[NETWORK_RCONCOMMAND_LENGTH];
523 
524  p->Recv_string(command, sizeof(command));
525 
526  DEBUG(net, 2, "[admin] Rcon command from '%s' (%s): '%s'", this->admin_name, this->admin_version, command);
527 
529  IConsoleCmdExec(command);
531  return this->SendRconEnd(command);
532 }
533 
535 {
536  if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
537 
539 
540  p->Recv_string(json, sizeof(json));
541 
542  DEBUG(net, 2, "[admin] GameScript JSON from '%s' (%s): '%s'", this->admin_name, this->admin_version, json);
543 
544  Game::NewEvent(new ScriptEventAdminPort(json));
546 }
547 
549 {
550  if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
551 
552  uint32 d1 = p->Recv_uint32();
553 
554  DEBUG(net, 2, "[admin] Ping from '%s' (%s): '%d'", this->admin_name, this->admin_version, d1);
555 
556  return this->SendPong(d1);
557 }
558 
565 {
566  /* If the length of both strings, plus the 2 '\0' terminations and 3 bytes of the packet
567  * are bigger than the MTU, just ignore the message. Better safe than sorry. It should
568  * never occur though as the longest strings are chat messages, which are still 30%
569  * smaller than SEND_MTU. */
570  if (strlen(origin) + strlen(string) + 2 + 3 >= SEND_MTU) return NETWORK_RECV_STATUS_OKAY;
571 
573 
574  p->Send_string(origin);
575  p->Send_string(string);
576  this->SendPacket(p);
577 
579 }
580 
586 {
587  /* At the moment we cannot transmit anything larger than MTU. So we limit
588  * the maximum amount of json data that can be sent. Account also for
589  * the trailing \0 of the string */
590  if (strlen(json) + 1 >= NETWORK_GAMESCRIPT_JSON_LENGTH) return NETWORK_RECV_STATUS_OKAY;
591 
593 
594  p->Send_string(json);
595  this->SendPacket(p);
596 
598 }
599 
602 {
604 
605  p->Send_uint32(d1);
606  this->SendPacket(p);
607 
609 }
610 
613 {
615 
616  for (uint i = 0; i < CMD_END; i++) {
617  const char *cmdname = GetCommandName(i);
618 
619  /* Should SEND_MTU be exceeded, start a new packet
620  * (magic 5: 1 bool "more data" and one uint16 "command id", one
621  * byte for string '\0' termination and 1 bool "no more data" */
622  if (p->size + strlen(cmdname) + 5 >= SEND_MTU) {
623  p->Send_bool(false);
624  this->SendPacket(p);
625 
627  }
628 
629  p->Send_bool(true);
630  p->Send_uint16(i);
631  p->Send_string(cmdname);
632  }
633 
634  /* Marker to notify the end of the packet has been reached. */
635  p->Send_bool(false);
636  this->SendPacket(p);
637 
639 }
640 
647 {
649 
650  p->Send_uint32(client_id);
651  p->Send_uint8 (cp->company);
652  p->Send_uint16(cp->cmd & CMD_ID_MASK);
653  p->Send_uint32(cp->p1);
654  p->Send_uint32(cp->p2);
655  p->Send_uint32(cp->tile);
656  p->Send_string(cp->text);
657  p->Send_uint32(cp->frame);
658 
659  this->SendPacket(p);
660 
662 }
663 
664 /***********
665  * Receiving functions
666  ************/
667 
669 {
670  if (this->status != ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
671 
672  char password[NETWORK_PASSWORD_LENGTH];
673  p->Recv_string(password, sizeof(password));
674 
676  strcmp(password, _settings_client.network.admin_password) != 0) {
677  /* Password is invalid */
678  return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
679  }
680 
681  p->Recv_string(this->admin_name, sizeof(this->admin_name));
682  p->Recv_string(this->admin_version, sizeof(this->admin_version));
683 
684  if (StrEmpty(this->admin_name) || StrEmpty(this->admin_version)) {
685  /* no name or version supplied */
686  return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
687  }
688 
689  this->status = ADMIN_STATUS_ACTIVE;
690 
691  DEBUG(net, 1, "[admin] '%s' (%s) has connected", this->admin_name, this->admin_version);
692 
693  return this->SendProtocol();
694 }
695 
697 {
698  /* The admin is leaving nothing else to do */
699  return this->CloseConnection();
700 }
701 
703 {
704  if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
705 
708 
709  if (type >= ADMIN_UPDATE_END || (_admin_update_type_frequencies[type] & freq) != freq) {
710  /* The server does not know of this UpdateType. */
711  DEBUG(net, 3, "[admin] Not supported update frequency %d (%d) from '%s' (%s).", type, freq, this->admin_name, this->admin_version);
712  return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
713  }
714 
715  this->update_frequency[type] = freq;
716 
718 }
719 
721 {
722  if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
723 
725  uint32 d1 = p->Recv_uint32();
726 
727  switch (type) {
728  case ADMIN_UPDATE_DATE:
729  /* The admin is requesting the current date. */
730  this->SendDate();
731  break;
732 
734  /* The admin is requesting client info. */
735  const NetworkClientSocket *cs;
736  if (d1 == UINT32_MAX) {
739  this->SendClientInfo(cs, cs->GetInfo());
740  }
741  } else {
742  if (d1 == CLIENT_ID_SERVER) {
744  } else {
745  cs = NetworkClientSocket::GetByClientID((ClientID)d1);
746  if (cs != nullptr) this->SendClientInfo(cs, cs->GetInfo());
747  }
748  }
749  break;
750 
752  /* The admin is asking for company info. */
753  const Company *company;
754  if (d1 == UINT32_MAX) {
755  FOR_ALL_COMPANIES(company) {
756  this->SendCompanyInfo(company);
757  }
758  } else {
759  company = Company::GetIfValid(d1);
760  if (company != nullptr) this->SendCompanyInfo(company);
761  }
762  break;
763 
765  /* The admin is requesting economy info. */
766  this->SendCompanyEconomy();
767  break;
768 
770  /* the admin is requesting company stats. */
771  this->SendCompanyStats();
772  break;
773 
775  /* The admin is requesting the names of DoCommands. */
776  this->SendCmdNames();
777  break;
778 
779  default:
780  /* An unsupported "poll" update type. */
781  DEBUG(net, 3, "[admin] Not supported poll %d (%d) from '%s' (%s).", type, d1, this->admin_name, this->admin_version);
782  return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
783  }
784 
786 }
787 
789 {
790  if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
791 
792  NetworkAction action = (NetworkAction)p->Recv_uint8();
793  DestType desttype = (DestType)p->Recv_uint8();
794  int dest = p->Recv_uint32();
795 
796  char msg[NETWORK_CHAT_LENGTH];
798 
799  switch (action) {
800  case NETWORK_ACTION_CHAT:
801  case NETWORK_ACTION_CHAT_CLIENT:
802  case NETWORK_ACTION_CHAT_COMPANY:
803  case NETWORK_ACTION_SERVER_MESSAGE:
804  NetworkServerSendChat(action, desttype, dest, msg, _network_own_client_id, 0, true);
805  break;
806 
807  default:
808  DEBUG(net, 3, "[admin] Invalid chat action %d from admin '%s' (%s).", action, this->admin_name, this->admin_version);
809  return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET);
810  }
811 
813 }
814 
815 /*
816  * Useful wrapper functions
817  */
818 
824 void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client)
825 {
829  as->SendClientInfo(cs, cs->GetInfo());
830  if (new_client) {
831  as->SendClientJoin(cs->client_id);
832  }
833  }
834  }
835 }
836 
842 {
846  as->SendClientUpdate(ci);
847  }
848  }
849 }
850 
856 {
860  as->SendClientQuit(client_id);
861  }
862  }
863 }
864 
871 {
875  as->SendClientError(client_id, error_code);
876  }
877  }
878 }
879 
885 void NetworkAdminCompanyInfo(const Company *company, bool new_company)
886 {
887  if (company == nullptr) {
888  DEBUG(net, 1, "[admin] Empty company given for update");
889  return;
890  }
891 
895 
896  as->SendCompanyInfo(company);
897  if (new_company) {
898  as->SendCompanyNew(company->index);
899  }
900  }
901 }
902 
907 void NetworkAdminCompanyUpdate(const Company *company)
908 {
909  if (company == nullptr) return;
910 
914 
915  as->SendCompanyUpdate(company);
916  }
917 }
918 
925 {
928  as->SendCompanyRemove(company_id, bcrr);
929  }
930 }
931 
932 
936 void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data, bool from_admin)
937 {
938  if (from_admin) return;
939 
943  as->SendChat(action, desttype, client_id, msg, data);
944  }
945  }
946 }
947 
954 void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
955 {
956  ServerNetworkAdminSocketHandler::Get(admin_index)->SendRcon(colour_code, string);
957 }
958 
964 void NetworkAdminConsole(const char *origin, const char *string)
965 {
969  as->SendConsole(origin, string);
970  }
971  }
972 }
973 
978 void NetworkAdminGameScript(const char *json)
979 {
983  as->SendGameScript(json);
984  }
985  }
986 }
987 
993 void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
994 {
995  ClientID client_id = owner == nullptr ? _network_own_client_id : owner->client_id;
996 
1000  as->SendCmdLogging(client_id, cp);
1001  }
1002  }
1003 }
1004 
1009 {
1012  as->SendWelcome();
1013  }
1014 }
1015 
1021 {
1024  for (int i = 0; i < ADMIN_UPDATE_END; i++) {
1025  if (as->update_frequency[i] & freq) {
1026  /* Update the admin for the required details */
1027  switch (i) {
1028  case ADMIN_UPDATE_DATE:
1029  as->SendDate();
1030  break;
1031 
1033  as->SendCompanyEconomy();
1034  break;
1035 
1037  as->SendCompanyStats();
1038  break;
1039 
1040  default: NOT_REACHED();
1041  }
1042  }
1043  }
1044  }
1045 }
Everything is okay.
Definition: core.h:25
char admin_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the admin.
Definition: tcp_admin.h:114
The admin would like to have console messages.
Definition: tcp_admin.h:83
Owner
Enum for all companies/owners.
Definition: company_type.h:20
static const byte NETWORK_GAME_ADMIN_VERSION
What version of the admin network do we use?
Definition: config.h:37
void NetworkAdminCompanyUpdate(const Company *company)
Notify the admin network of company updates.
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:74
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:81
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:143
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
const char * GetCommandName(uint32 cmd)
Definition: command.cpp:400
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:84
#define FOR_ALL_ACTIVE_ADMIN_SOCKETS(var)
Iterate over all the active sockets.
Container for all information known about a client.
Definition: network_base.h:25
NetworkRecvStatus SendClientUpdate(const NetworkClientInfo *ci)
Send an update for some client&#39;s information.
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:50
Internal entity of a packet.
Definition: packet.h:42
The server replies to a ping request from the admin.
Definition: tcp_admin.h:63
AdminCompanyRemoveReason
Reasons for removing a company - communicated to admins.
Definition: tcp_admin.h:103
byte landscape
the landscape we&#39;re currently in
static void WelcomeAll()
Send a Welcome packet to all connected admins.
The admin would like to have chat messages.
Definition: tcp_admin.h:82
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:248
The server gives the admin information from the GameScript in JSON.
Definition: tcp_admin.h:61
char server_name[NETWORK_NAME_LENGTH]
name of the server
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
int32 performance_history
Company score (scale 0-1000)
Definition: company_base.h:27
The server tells the admin its going to start a new game.
Definition: tcp_admin.h:41
void NetworkAdminGameScript(const char *json)
Send GameScript JSON to the admin network (if they did opt in for the respective update).
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:150
Year inaugurated_year
Year of starting the company.
Definition: company_base.h:80
The server tells the admin its shutting down.
Definition: tcp_admin.h:42
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: tcp_admin.cpp:43
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
Populate the company stats.
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:102
The server received a chat message and relays it.
Definition: tcp_admin.h:56
The server tells the admin that a new company has started.
Definition: tcp_admin.h:50
The server tells the admin its protocol version.
Definition: tcp_admin.h:39
ClientID client_id
Client identifier (same as ClientState->client_id)
Definition: network_base.h:26
The admin would like a list of all DoCommand names.
Definition: tcp_admin.h:84
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:98
NetworkRecvStatus SendWelcome()
Send a welcome message to the admin.
uint32 p2
parameter p2.
Definition: command_type.h:480
The server tells the admin that a client quit.
Definition: tcp_admin.h:48
static void Send()
Send the packets for the server sockets.
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:29
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
byte months_of_bankruptcy
Number of months that the company is unable to pay its debts.
Definition: company_base.h:82
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
Definition: network_type.h:56
The server tells the admin an error has occurred.
Definition: tcp_admin.h:38
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:119
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the acception of a connection.
Updates about the information of clients.
Definition: tcp_admin.h:78
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
Definition: network.cpp:223
char admin_version[NETWORK_REVISION_LENGTH]
Version string of the admin.
Definition: tcp_admin.h:115
Base core network types and some helper functions to access them.
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
const char * GetNetworkRevisionString()
Get the network version string used by this build.
Definition: network.cpp:1111
uint32 realtime_connect
Time of connection.
Definition: network_admin.h:42
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:41
The admin gets information about this on a daily basis.
Definition: tcp_admin.h:93
char text[32 *MAX_CHAR_LENGTH]
possible text sent for name changes etc, in bytes including &#39;\0&#39;.
Definition: command_type.h:483
The server tells the admin what the current game date is.
Definition: tcp_admin.h:44
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:97
The admin would like to have DoCommand information.
Definition: tcp_admin.h:85
AdminUpdateFrequency
Update frequencies an admin can register.
Definition: tcp_admin.h:91
NetworkRecvStatus SendConsole(const char *origin, const char *command)
Send console output of other clients.
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
Definition: network.cpp:308
The admin gets information about this on a monthly basis.
Definition: tcp_admin.h:95
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:57
NetworkRecvStatus SendCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Tell the admin that a company got removed.
The server gives the admin some statistics about a company.
Definition: tcp_admin.h:55
uint16 num_vehicle[NETWORK_VEH_END]
How many vehicles are there of this type?
Definition: network_type.h:60
static const size_t MAX_SIZE
Make template parameter accessible from outside.
Definition: pool_type.hpp:87
void NetworkAdminClientQuit(ClientID client_id)
Notify the admin network that a client quit (if they have opt in for the respective update)...
void NetworkAdminConsole(const char *origin, const char *string)
Send console to the admin network (if they did opt in for the respective update). ...
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
Distribute CommandPacket details over the admin network for logging purposes.
void NetworkAdminUpdate(AdminUpdateFrequency freq)
Send (push) updates to the admin network as they have registered for these updates.
NetworkSettings network
settings related to the network
NetworkRecvStatus SendDate()
Tell the admin the date.
NetworkRecvStatus Receive_ADMIN_QUIT(Packet *p) override
Notification to the server that this admin is quitting.
CompanyID client_playas
As which company is this client playing (CompanyID)
Definition: network_base.h:29
static const uint NETWORK_RCONCOMMAND_LENGTH
The maximum length of a rconsole command, in bytes including &#39;\0&#39;.
Definition: config.h:50
AdminStatus status
Status of this admin.
Definition: tcp_admin.h:116
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:132
The admin can poll this.
Definition: tcp_admin.h:92
The server&#39;s reply to a remove console command.
Definition: tcp_admin.h:57
NetworkRecvStatus SendCompanyEconomy()
Send economic information of all companies.
char client_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the client.
Definition: network_base.h:27
Updates about the statistics of companies.
Definition: tcp_admin.h:81
Server part of the network protocol.
The server gives the admin an information update on a client.
Definition: tcp_admin.h:47
NetworkRecvStatus SendRcon(uint16 colour, const char *command)
Send the reply of an rcon command.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:108
Updates about the economy of companies.
Definition: tcp_admin.h:80
mask for the command ID
Definition: command_type.h:380
uint32 p1
parameter p1.
Definition: command_type.h:479
NetworkRecvStatus SendClientInfo(const NetworkClientSocket *cs, const NetworkClientInfo *ci)
Send an initial set of data from some client&#39;s information.
The admin is not connected nor active.
Definition: tcp_admin.h:70
AdminUpdateFrequency update_frequency[ADMIN_UPDATE_END]
Admin requested update intervals.
Definition: network_admin.h:41
NetworkRecvStatus SendClientQuit(ClientID client_id)
Tell the admin that a client quit.
bool writable
Can we write to this socket?
Definition: tcp.h:35
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)...
Money current_loan
Amount of money borrowed from the bank.
Definition: company_base.h:69
void IConsoleCmdExec(const char *cmdstr)
Execute a given command passed to us.
Definition: console.cpp:403
~ServerNetworkAdminSocketHandler()
Clear everything related to this admin.
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:61
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
NetworkRecvStatus SendRconEnd(const char *command)
Send a notification indicating the rcon command has completed.
NetworkRecvStatus SendClientJoin(ClientID client_id)
Tell the admin that a client joined.
NetworkRecvStatus Receive_ADMIN_JOIN(Packet *p) override
Join the admin network: string Password the server is expecting for this network. ...
The admin gets information about this on a quarterly basis.
Definition: tcp_admin.h:96
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
NetworkRecvStatus Receive_ADMIN_PING(Packet *p) override
Ping the server, requiring the server to reply with a pong packet.
The admin would like to have gamescript messages.
Definition: tcp_admin.h:86
NetworkRecvStatus SendNewGame()
Tell the admin we started a new game.
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:316
NetworkRecvStatus SendCmdNames()
Send the names of the commands.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:24
The server indicates that the remote console command has completed.
Definition: tcp_admin.h:62
NetworkRecvStatus SendCompanyUpdate(const Company *c)
Send an update about a company.
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including &#39;\0&#39;.
Definition: config.h:52
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Definition: company_base.h:95
Money money
Money owned by the company.
Definition: company_base.h:67
Updates about the date of the game.
Definition: tcp_admin.h:77
void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data, bool from_admin)
Send chat to the admin network (if they did opt in for the respective update).
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:50
NetworkRecvStatus SendCompanyInfo(const Company *c)
Send the admin some information about a company.
CompanyID company
company that is executing the command
byte _network_admins_connected
The amount of admins connected.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
NetworkServerGameInfo _network_game_info
Information about our game.
Definition: network.cpp:59
NetworkRecvStatus Receive_ADMIN_RCON(Packet *p) override
Execute a command on the servers console: string Command to be executed.
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
static bool AllowConnection()
Whether a connection is allowed or not at this moment.
The server tells the admin that a company was removed.
Definition: tcp_admin.h:53
static const AdminIndex MAX_ADMINS
Maximum number of allowed admins.
Definition: network_type.h:54
The server sends out the names of the DoCommands to the admins.
Definition: tcp_admin.h:59
#define FOR_ALL_CLIENT_SOCKETS(var)
Iterate over all the sockets.
void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client)
Notify the admin network of a new client (if they did opt in for the respective update).
uint32 frame
the frame in which this packet is executed
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
The server tells the admin that a client caused an error.
Definition: tcp_admin.h:49
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
Pass the rcon reply to the admin.
The server gives the admin the data that got printed to its console.
Definition: tcp_admin.h:58
uint8 AdminIndex
Indices into the admin tables.
Definition: network_type.h:51
static const uint NETWORK_GAMESCRIPT_JSON_LENGTH
The maximum length of a gamescript json string, in bytes including &#39;\0&#39;. Must not be longer than SEND...
Definition: config.h:51
Date join_date
Gamedate the client has joined.
Definition: network_base.h:30
void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr)
Notify the admin network of a company to be removed (including the reason why).
Base class for all pools.
Definition: pool_type.hpp:83
NetworkRecvStatus SendError(NetworkErrorCode error)
Send an error to the admin.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
The server gives the admin an information update on a company.
Definition: tcp_admin.h:52
char map_name[NETWORK_NAME_LENGTH]
Map which is played ["random" for a randomized map].
Definition: game.h:27
assert_compile(lengthof(_admin_update_type_frequencies)==ADMIN_UPDATE_END)
Sanity check.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:226
Owner share_owners[4]
Owners of the 4 shares of the company. INVALID_OWNER if nobody has bought them yet.
Definition: company_base.h:78
NetworkRecvStatus SendProtocol()
Send the protocol version to the admin.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:219
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, ClientID from_id, int64 data=0, bool from_admin=false)
Send an actual chat message.
uint32 generation_seed
noise seed for world generation
NetworkRecvStatus SendClientError(ClientID client_id, NetworkErrorCode error)
Tell the admin that a client made an error.
The admin gets information about this on a yearly basis.
Definition: tcp_admin.h:97
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
static const AdminUpdateFrequency _admin_update_type_frequencies[]
Frequencies, which may be registered for a certain update type.
Must ALWAYS be on the end of this list!! (period)
Definition: command_type.h:336
The server tells the admin that a client has joined.
Definition: tcp_admin.h:45
The admin gets information about this on a weekly basis.
Definition: tcp_admin.h:94
byte colour
Company colour.
Definition: company_base.h:71
CargoArray delivered_cargo
The amount of delivered cargo.
Definition: company_base.h:26
NetworkRecvStatus SendGameScript(const char *json)
Send GameScript JSON output.
static const uint NETWORK_COMPANY_NAME_LENGTH
The maximum length of the company name, in bytes including &#39;\0&#39;.
Definition: config.h:43
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:114
ServerNetworkAdminSocketHandler(SOCKET s)
Create a new socket for the server side of the admin network.
uint16 num_station[NETWORK_VEH_END]
How many stations are there of this type?
Definition: network_type.h:61
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition: company_base.h:99
Class for handling the server side of the game connection.
Definition: network_admin.h:27
Must ALWAYS be on the end of this list!! (period)
Definition: tcp_admin.h:87
void NetworkAdminCompanyInfo(const Company *company, bool new_company)
Notify the admin network of company details.
Maximum number of companies.
Definition: company_type.h:25
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
The server gives the admin information about a client.
Definition: tcp_admin.h:46
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Definition: config.h:35
char admin_password[NETWORK_PASSWORD_LENGTH]
password for the admin network
Main socket handler for admin related connections.
Definition: tcp_admin.h:112
NetworkRecvStatus SendCompanyStats()
Send statistics about the companies.
TileIndex tile
tile command being executed on.
Definition: command_type.h:478
The server welcomes the admin to a game.
Definition: tcp_admin.h:40
NetworkRecvStatus Receive_ADMIN_POLL(Packet *p) override
Poll the server for certain updates, an invalid poll (e.g.
NetworkRecvStatus SendChat(NetworkAction action, DestType desttype, ClientID client_id, const char *msg, int64 data)
Send a chat message.
Everything we need to know about a command to be able to execute it.
Money yearly_expenses[3][EXPENSES_END]
Expenses of the company for the last three years, in every ExpensesType category. ...
Definition: company_base.h:97
NetworkRecvStatus SendShutdown()
Tell the admin we&#39;re shutting down.
The admin gets information about this when it changes.
Definition: tcp_admin.h:98
The admin is active.
Definition: tcp_admin.h:71
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:63
Simple calculated statistics of a company.
Definition: network_type.h:59
static const uint NETWORK_PASSWORD_LENGTH
The maximum length of the password, in bytes including &#39;\0&#39; (must be >= NETWORK_SERVER_ID_LENGTH) ...
Definition: config.h:47
void NetworkAdminClientUpdate(const NetworkClientInfo *ci)
Notify the admin network of a client update (if they did opt in for the respective update)...
The server gives the admin copies of incoming command packets.
Definition: tcp_admin.h:60
Servers always have this ID.
Definition: network_type.h:43
GameCreationSettings game_creation
settings used during the creation of a game (map)
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:149
#define FOR_ALL_ADMIN_SOCKETS(var)
Iterate over all the sockets.
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:233
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:89
Money company_value
The value of the company.
Definition: company_base.h:28
NetworkRecvStatus SendCmdLogging(ClientID client_id, const CommandPacket *cp)
Send a command for logging purposes.
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it&#39;s client-identifier.
Definition: network.cpp:124
NetworkAdminSocketPool _networkadminsocket_pool("NetworkAdminSocket")
The pool with sockets/clients.
Updates about the generic information of companies.
Definition: tcp_admin.h:79
The server gives the admin some economy related company information.
Definition: tcp_admin.h:54
CompanyEconomyEntry cur_economy
Economic data of the company of this quarter.
Definition: company_base.h:98
Date _date
Current date in days (day counter)
Definition: date.cpp:28
NetworkRecvStatus SendCompanyNew(CompanyID company_id)
Tell the admin that a new company was founded.
AdminUpdateType
Update types an admin can register a frequency for.
Definition: tcp_admin.h:76
NetworkRecvStatus Receive_ADMIN_UPDATE_FREQUENCY(Packet *p) override
Register updates to be sent at certain frequencies (as announced in the PROTOCOL packet): uint16 Upda...
NetworkRecvStatus Receive_ADMIN_CHAT(Packet *p) override
Send chat as the server: uint8 Action such as NETWORK_ACTION_CHAT_CLIENT (see NetworkAction).
Year starting_year
starting date
uint32 cmd
command being executed.
Definition: command_type.h:481
DestType
Destination of our chat messages.
Definition: network_type.h:80
byte client_lang
The language of the client.
Definition: network_base.h:28
static const int ADMIN_AUTHORISATION_TIMEOUT
The timeout for authorisation of the client.
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:87
NetworkRecvStatus SendPong(uint32 d1)
Send ping-reply (pong) to admin.
const T GetSum() const
Get the sum of all cargo amounts.
Definition: cargo_type.h:123
NetworkRecvStatus Receive_ADMIN_GAMESCRIPT(Packet *p) override
Send a JSON string to the current active GameScript.
NetworkAddress address
Address of the admin.
Definition: network_admin.h:43
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a &#39;\0&#39; in the stream.
Definition: packet.cpp:288
Server part of the admin network protocol.
The server gives the admin information about a company.
Definition: tcp_admin.h:51
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201