Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / network / network.cpp
blob1d5563c3d93014767d185a8e2e74c298bae42237
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 */
8 /** @file network.cpp Base functions for networking support. */
10 #include "../stdafx.h"
12 #include "../strings_func.h"
13 #include "../command_func.h"
14 #include "../date_func.h"
15 #include "network_admin.h"
16 #include "network_client.h"
17 #include "network_server.h"
18 #include "network_content.h"
19 #include "network_udp.h"
20 #include "network_gamelist.h"
21 #include "network_base.h"
22 #include "core/udp.h"
23 #include "core/host.h"
24 #include "network_gui.h"
25 #include "../console_func.h"
26 #include "../3rdparty/md5/md5.h"
27 #include "../core/random_func.hpp"
28 #include "../window_func.h"
29 #include "../company_func.h"
30 #include "../company_base.h"
31 #include "../landscape_type.h"
32 #include "../rev.h"
33 #include "../core/pool_func.hpp"
34 #include "../gfx_func.h"
35 #include "../error.h"
37 #include "../safeguards.h"
39 #ifdef DEBUG_DUMP_COMMANDS
40 #include "../fileio_func.h"
41 /** When running the server till the wait point, run as fast as we can! */
42 bool _ddc_fastforward = true;
43 #endif /* DEBUG_DUMP_COMMANDS */
45 /** Make sure both pools have the same size. */
46 static_assert(NetworkClientInfoPool::MAX_SIZE == NetworkClientSocketPool::MAX_SIZE);
48 /** The pool with client information. */
49 NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo");
50 INSTANTIATE_POOL_METHODS(NetworkClientInfo)
52 bool _networking; ///< are we in networking mode?
53 bool _network_server; ///< network-server is active
54 bool _network_available; ///< is network mode available?
55 bool _network_dedicated; ///< are we a dedicated server?
56 bool _is_network_server; ///< Does this client wants to be a network-server?
57 NetworkServerGameInfo _network_game_info; ///< Information about our game.
58 NetworkCompanyState *_network_company_states = nullptr; ///< Statistics about some companies.
59 ClientID _network_own_client_id; ///< Our client identifier.
60 ClientID _redirect_console_to_client; ///< If not invalid, redirect the console output to a client.
61 bool _network_need_advertise; ///< Whether we need to advertise.
62 uint8 _network_reconnect; ///< Reconnect timeout
63 StringList _network_bind_list; ///< The addresses to bind on.
64 StringList _network_host_list; ///< The servers we know.
65 StringList _network_ban_list; ///< The banned clients.
66 uint32 _frame_counter_server; ///< The frame_counter of the server, if in network-mode
67 uint32 _frame_counter_max; ///< To where we may go with our clients
68 uint32 _frame_counter; ///< The current frame.
69 uint32 _last_sync_frame; ///< Used in the server to store the last time a sync packet was sent to clients.
70 NetworkAddressList _broadcast_list; ///< List of broadcast addresses.
71 uint32 _sync_seed_1; ///< Seed to compare during sync checks.
72 #ifdef NETWORK_SEND_DOUBLE_SEED
73 uint32 _sync_seed_2; ///< Second part of the seed.
74 #endif
75 uint32 _sync_frame; ///< The frame to perform the sync check.
76 bool _network_first_time; ///< Whether we have finished joining or not.
77 bool _network_udp_server; ///< Is the UDP server started?
78 uint16 _network_udp_broadcast; ///< Timeout for the UDP broadcasts.
79 uint8 _network_advertise_retries; ///< The number of advertisement retries we did.
80 CompanyMask _network_company_passworded; ///< Bitmask of the password status of all companies.
82 /* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
83 static_assert((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
84 static_assert((int)NETWORK_COMPANY_NAME_LENGTH == MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH);
86 extern NetworkUDPSocketHandler *_udp_client_socket; ///< udp client socket
87 extern NetworkUDPSocketHandler *_udp_server_socket; ///< udp server socket
88 extern NetworkUDPSocketHandler *_udp_master_socket; ///< udp master socket
90 /** The amount of clients connected */
91 byte _network_clients_connected = 0;
93 /* Some externs / forwards */
94 extern void StateGameLoop();
96 /**
97 * Return whether there is any client connected or trying to connect at all.
98 * @return whether we have any client activity
100 bool HasClients()
102 return !NetworkClientSocket::Iterate().empty();
106 * Basically a client is leaving us right now.
108 NetworkClientInfo::~NetworkClientInfo()
110 /* Delete the chat window, if you were chatting with this client. */
111 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_CLIENT, this->client_id);
115 * Return the CI given it's client-identifier
116 * @param client_id the ClientID to search for
117 * @return return a pointer to the corresponding NetworkClientInfo struct or nullptr when not found
119 /* static */ NetworkClientInfo *NetworkClientInfo::GetByClientID(ClientID client_id)
121 for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
122 if (ci->client_id == client_id) return ci;
125 return nullptr;
129 * Return the client state given it's client-identifier
130 * @param client_id the ClientID to search for
131 * @return return a pointer to the corresponding NetworkClientSocket struct or nullptr when not found
133 /* static */ ServerNetworkGameSocketHandler *ServerNetworkGameSocketHandler::GetByClientID(ClientID client_id)
135 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
136 if (cs->client_id == client_id) return cs;
139 return nullptr;
142 byte NetworkSpectatorCount()
144 byte count = 0;
146 for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
147 if (ci->client_playas == COMPANY_SPECTATOR) count++;
150 /* Don't count a dedicated server as spectator */
151 if (_network_dedicated) count--;
153 return count;
157 * Change the company password of a given company.
158 * @param company_id ID of the company the password should be changed for.
159 * @param password The unhashed password we like to set ('*' or '' resets the password)
160 * @return The password.
162 const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
164 if (strcmp(password, "*") == 0) password = "";
166 if (_network_server) {
167 NetworkServerSetCompanyPassword(company_id, password, false);
168 } else {
169 NetworkClientSetCompanyPassword(password);
172 return password;
176 * Hash the given password using server ID and game seed.
177 * @param password Password to hash.
178 * @param password_server_id Server ID.
179 * @param password_game_seed Game seed.
180 * @return The hashed password.
182 const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
184 if (StrEmpty(password)) return password;
186 char salted_password[NETWORK_SERVER_ID_LENGTH];
188 memset(salted_password, 0, sizeof(salted_password));
189 seprintf(salted_password, lastof(salted_password), "%s", password);
190 /* Add the game seed and the server's ID as the salt. */
191 for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
192 salted_password[i] ^= password_server_id[i] ^ (password_game_seed >> (i % 32));
195 Md5 checksum;
196 uint8 digest[16];
197 static char hashed_password[NETWORK_SERVER_ID_LENGTH];
199 /* Generate the MD5 hash */
200 checksum.Append(salted_password, sizeof(salted_password) - 1);
201 checksum.Finish(digest);
203 for (int di = 0; di < 16; di++) seprintf(hashed_password + di * 2, lastof(hashed_password), "%02x", digest[di]);
205 return hashed_password;
209 * Check if the company we want to join requires a password.
210 * @param company_id id of the company we want to check the 'passworded' flag for.
211 * @return true if the company requires a password.
213 bool NetworkCompanyIsPassworded(CompanyID company_id)
215 return HasBit(_network_company_passworded, company_id);
218 /* This puts a text-message to the console, or in the future, the chat-box,
219 * (to keep it all a bit more general)
220 * If 'self_send' is true, this is the client who is sending the message */
221 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const char *name, const char *str, int64 data)
223 StringID strid;
224 switch (action) {
225 case NETWORK_ACTION_SERVER_MESSAGE:
226 /* Ignore invalid messages */
227 strid = STR_NETWORK_SERVER_MESSAGE;
228 colour = CC_DEFAULT;
229 break;
230 case NETWORK_ACTION_COMPANY_SPECTATOR:
231 colour = CC_DEFAULT;
232 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
233 break;
234 case NETWORK_ACTION_COMPANY_JOIN:
235 colour = CC_DEFAULT;
236 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
237 break;
238 case NETWORK_ACTION_COMPANY_NEW:
239 colour = CC_DEFAULT;
240 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
241 break;
242 case NETWORK_ACTION_JOIN:
243 /* Show the Client ID for the server but not for the client. */
244 strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID : STR_NETWORK_MESSAGE_CLIENT_JOINED;
245 break;
246 case NETWORK_ACTION_LEAVE: strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
247 case NETWORK_ACTION_NAME_CHANGE: strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
248 case NETWORK_ACTION_GIVE_MONEY: strid = STR_NETWORK_MESSAGE_GIVE_MONEY; break;
249 case NETWORK_ACTION_CHAT_COMPANY: strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
250 case NETWORK_ACTION_CHAT_CLIENT: strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT; break;
251 case NETWORK_ACTION_KICKED: strid = STR_NETWORK_MESSAGE_KICKED; break;
252 default: strid = STR_NETWORK_CHAT_ALL; break;
255 char message[1024];
256 SetDParamStr(0, name);
257 SetDParamStr(1, str);
258 SetDParam(2, data);
260 /* All of these strings start with "***". These characters are interpreted as both left-to-right and
261 * right-to-left characters depending on the context. As the next text might be an user's name, the
262 * user name's characters will influence the direction of the "***" instead of the language setting
263 * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
264 char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
265 GetString(msg_ptr, strid, lastof(message));
267 DEBUG(desync, 1, "msg: %08x; %02x; %s", _date, _date_fract, message);
268 IConsolePrintF(colour, "%s", message);
269 NetworkAddChatMessage((TextColour)colour, _settings_client.gui.network_chat_timeout, "%s", message);
272 /* Calculate the frame-lag of a client */
273 uint NetworkCalculateLag(const NetworkClientSocket *cs)
275 int lag = cs->last_frame_server - cs->last_frame;
276 /* This client has missed his ACK packet after 1 DAY_TICKS..
277 * so we increase his lag for every frame that passes!
278 * The packet can be out by a max of _net_frame_freq */
279 if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
280 lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
282 return lag;
286 /* There was a non-recoverable error, drop back to the main menu with a nice
287 * error */
288 void NetworkError(StringID error_string)
290 _switch_mode = SM_MENU;
291 ShowErrorMessage(error_string, INVALID_STRING_ID, WL_CRITICAL);
295 * Retrieve the string id of an internal error number
296 * @param err NetworkErrorCode
297 * @return the StringID
299 StringID GetNetworkErrorMsg(NetworkErrorCode err)
301 /* List of possible network errors, used by
302 * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
303 static const StringID network_error_strings[] = {
304 STR_NETWORK_ERROR_CLIENT_GENERAL,
305 STR_NETWORK_ERROR_CLIENT_DESYNC,
306 STR_NETWORK_ERROR_CLIENT_SAVEGAME,
307 STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST,
308 STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR,
309 STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH,
310 STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED,
311 STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED,
312 STR_NETWORK_ERROR_CLIENT_WRONG_REVISION,
313 STR_NETWORK_ERROR_CLIENT_NAME_IN_USE,
314 STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD,
315 STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH,
316 STR_NETWORK_ERROR_CLIENT_KICKED,
317 STR_NETWORK_ERROR_CLIENT_CHEATER,
318 STR_NETWORK_ERROR_CLIENT_SERVER_FULL,
319 STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS,
320 STR_NETWORK_ERROR_CLIENT_TIMEOUT_PASSWORD,
321 STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER,
322 STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP,
323 STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN,
325 static_assert(lengthof(network_error_strings) == NETWORK_ERROR_END);
327 if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
329 return network_error_strings[err];
333 * Handle the pause mode change so we send the right messages to the chat.
334 * @param prev_mode The previous pause mode.
335 * @param changed_mode The pause mode that got changed.
337 void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
339 if (!_networking) return;
341 switch (changed_mode) {
342 case PM_PAUSED_NORMAL:
343 case PM_PAUSED_JOIN:
344 case PM_PAUSED_GAME_SCRIPT:
345 case PM_PAUSED_ACTIVE_CLIENTS:
346 case PM_PAUSED_LINK_GRAPH: {
347 bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
348 bool paused = (_pause_mode != PM_UNPAUSED);
349 if (!paused && !changed) return;
351 StringID str;
352 if (!changed) {
353 int i = -1;
355 if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL);
356 if ((_pause_mode & PM_PAUSED_JOIN) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS);
357 if ((_pause_mode & PM_PAUSED_GAME_SCRIPT) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT);
358 if ((_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS);
359 if ((_pause_mode & PM_PAUSED_LINK_GRAPH) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH);
360 str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
361 } else {
362 switch (changed_mode) {
363 case PM_PAUSED_NORMAL: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
364 case PM_PAUSED_JOIN: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
365 case PM_PAUSED_GAME_SCRIPT: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT); break;
366 case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
367 case PM_PAUSED_LINK_GRAPH: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH); break;
368 default: NOT_REACHED();
370 str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
373 char buffer[DRAW_STRING_BUFFER];
374 GetString(buffer, str, lastof(buffer));
375 NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, nullptr, buffer);
376 break;
379 default:
380 return;
386 * Helper function for the pause checkers. If pause is true and the
387 * current pause mode isn't set the game will be paused, if it it false
388 * and the pause mode is set the game will be unpaused. In the other
389 * cases nothing happens to the pause state.
390 * @param pause whether we'd like to pause
391 * @param pm the mode which we would like to pause with
393 static void CheckPauseHelper(bool pause, PauseMode pm)
395 if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
397 DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
401 * Counts the number of active clients connected.
402 * It has to be in STATUS_ACTIVE and not a spectator
403 * @return number of active clients
405 static uint NetworkCountActiveClients()
407 uint count = 0;
409 for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
410 if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
411 if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
412 count++;
415 return count;
419 * Check if the minimum number of active clients has been reached and pause or unpause the game as appropriate
421 static void CheckMinActiveClients()
423 if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
424 !_network_dedicated ||
425 (_settings_client.network.min_active_clients == 0 && (_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) == PM_UNPAUSED)) {
426 return;
428 CheckPauseHelper(NetworkCountActiveClients() < _settings_client.network.min_active_clients, PM_PAUSED_ACTIVE_CLIENTS);
432 * Checks whether there is a joining client
433 * @return true iff one client is joining (but not authorizing)
435 static bool NetworkHasJoiningClient()
437 for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
438 if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
441 return false;
445 * Check whether we should pause on join
447 static void CheckPauseOnJoin()
449 if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
450 (!_settings_client.network.pause_on_join && (_pause_mode & PM_PAUSED_JOIN) == PM_UNPAUSED)) {
451 return;
453 CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
457 * Converts a string to ip/port/company
458 * Format: IP:port#company
460 * connection_string will be re-terminated to separate out the hostname, and company and port will
461 * be set to the company and port strings given by the user, inside the memory area originally
462 * occupied by connection_string.
464 void ParseConnectionString(const char **company, const char **port, char *connection_string)
466 bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
467 char *p;
468 for (p = connection_string; *p != '\0'; p++) {
469 switch (*p) {
470 case '[':
471 ipv6 = true;
472 break;
474 case ']':
475 ipv6 = false;
476 break;
478 case '#':
479 *company = p + 1;
480 *p = '\0';
481 break;
483 case ':':
484 if (ipv6) break;
485 *port = p + 1;
486 *p = '\0';
487 break;
493 * Handle the accepting of a connection to the server.
494 * @param s The socket of the new connection.
495 * @param address The address of the peer.
497 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
499 /* Register the login */
500 _network_clients_connected++;
502 SetWindowDirty(WC_CLIENT_LIST, 0);
503 ServerNetworkGameSocketHandler *cs = new ServerNetworkGameSocketHandler(s);
504 cs->client_address = address; // Save the IP of the client
508 * Resets the pools used for network clients, and the admin pool if needed.
509 * @param close_admins Whether the admin pool has to be cleared as well.
511 static void InitializeNetworkPools(bool close_admins = true)
513 PoolBase::Clean(PT_NCLIENT | (close_admins ? PT_NADMIN : PT_NONE));
517 * Close current connections.
518 * @param close_admins Whether the admin connections have to be closed as well.
520 void NetworkClose(bool close_admins)
522 if (_network_server) {
523 if (close_admins) {
524 for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::Iterate()) {
525 as->CloseConnection(true);
529 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
530 cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
532 ServerNetworkGameSocketHandler::CloseListeners();
533 ServerNetworkAdminSocketHandler::CloseListeners();
534 } else if (MyClient::my_client != nullptr) {
535 MyClient::SendQuit();
536 MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
539 TCPConnecter::KillAll();
541 _networking = false;
542 _network_server = false;
544 NetworkFreeLocalCommandQueue();
546 free(_network_company_states);
547 _network_company_states = nullptr;
549 InitializeNetworkPools(close_admins);
552 /* Initializes the network (cleans sockets and stuff) */
553 static void NetworkInitialize(bool close_admins = true)
555 InitializeNetworkPools(close_admins);
556 NetworkUDPInitialize();
558 _sync_frame = 0;
559 _network_first_time = true;
561 _network_reconnect = 0;
564 /** Non blocking connection create to query servers */
565 class TCPQueryConnecter : TCPConnecter {
566 public:
567 TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
569 void OnFailure() override
571 NetworkDisconnect();
574 void OnConnect(SOCKET s) override
576 _networking = true;
577 new ClientNetworkGameSocketHandler(s);
578 MyClient::SendCompanyInformationQuery();
582 /* Query a server to fetch his game-info
583 * If game_info is true, only the gameinfo is fetched,
584 * else only the client_info is fetched */
585 void NetworkTCPQueryServer(NetworkAddress address)
587 if (!_network_available) return;
589 NetworkDisconnect();
590 NetworkInitialize();
592 new TCPQueryConnecter(address);
595 /* Validates an address entered as a string and adds the server to
596 * the list. If you use this function, the games will be marked
597 * as manually added. */
598 void NetworkAddServer(const char *b)
600 if (*b != '\0') {
601 const char *port = nullptr;
602 const char *company = nullptr;
603 char host[NETWORK_HOSTNAME_LENGTH];
604 uint16 rport;
606 strecpy(host, b, lastof(host));
608 strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
609 rport = NETWORK_DEFAULT_PORT;
611 ParseConnectionString(&company, &port, host);
612 if (port != nullptr) rport = atoi(port);
614 NetworkUDPQueryServer(NetworkAddress(host, rport), true);
619 * Get the addresses to bind to.
620 * @param addresses the list to write to.
621 * @param port the port to bind to.
623 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
625 for (const auto &iter : _network_bind_list) {
626 addresses->emplace_back(iter.c_str(), port);
629 /* No address, so bind to everything. */
630 if (addresses->size() == 0) {
631 addresses->emplace_back("", port);
635 /* Generates the list of manually added hosts from NetworkGameList and
636 * dumps them into the array _network_host_list. This array is needed
637 * by the function that generates the config file. */
638 void NetworkRebuildHostList()
640 _network_host_list.clear();
642 for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
643 if (item->manually) _network_host_list.emplace_back(item->address.GetAddressAsString(false));
647 /** Non blocking connection create to actually connect to servers */
648 class TCPClientConnecter : TCPConnecter {
649 public:
650 TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
652 void OnFailure() override
654 NetworkError(STR_NETWORK_ERROR_NOCONNECTION);
657 void OnConnect(SOCKET s) override
659 _networking = true;
660 new ClientNetworkGameSocketHandler(s);
661 IConsoleCmdExec("exec scripts/on_client.scr 0");
662 NetworkClient_Connected();
667 /* Used by clients, to connect to a server */
668 void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
670 if (!_network_available) return;
672 if (address.GetPort() == 0) return;
674 strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
675 _settings_client.network.last_port = address.GetPort();
676 _network_join_as = join_as;
677 _network_join_server_password = join_server_password;
678 _network_join_company_password = join_company_password;
680 NetworkDisconnect();
681 NetworkInitialize();
683 _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
684 ShowJoinStatusWindow();
686 new TCPClientConnecter(address);
689 static void NetworkInitGameInfo()
691 if (StrEmpty(_settings_client.network.server_name)) {
692 seprintf(_settings_client.network.server_name, lastof(_settings_client.network.server_name), "Unnamed Server");
695 /* The server is a client too */
696 _network_game_info.clients_on = _network_dedicated ? 0 : 1;
698 /* There should be always space for the server. */
699 assert(NetworkClientInfo::CanAllocateItem());
700 NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
701 ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
703 strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
706 bool NetworkServerStart()
708 if (!_network_available) return false;
710 /* Call the pre-scripts */
711 IConsoleCmdExec("exec scripts/pre_server.scr 0");
712 if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
714 NetworkDisconnect(false, false);
715 NetworkInitialize(false);
716 DEBUG(net, 1, "starting listeners for clients");
717 if (!ServerNetworkGameSocketHandler::Listen(_settings_client.network.server_port)) return false;
719 /* Only listen for admins when the password isn't empty. */
720 if (!StrEmpty(_settings_client.network.admin_password)) {
721 DEBUG(net, 1, "starting listeners for admins");
722 if (!ServerNetworkAdminSocketHandler::Listen(_settings_client.network.server_admin_port)) return false;
725 /* Try to start UDP-server */
726 DEBUG(net, 1, "starting listeners for incoming server queries");
727 _network_udp_server = _udp_server_socket->Listen();
729 _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
730 _network_server = true;
731 _networking = true;
732 _frame_counter = 0;
733 _frame_counter_server = 0;
734 _frame_counter_max = 0;
735 _last_sync_frame = 0;
736 _network_own_client_id = CLIENT_ID_SERVER;
738 _network_clients_connected = 0;
739 _network_company_passworded = 0;
741 NetworkInitGameInfo();
743 /* execute server initialization script */
744 IConsoleCmdExec("exec scripts/on_server.scr 0");
745 /* if the server is dedicated ... add some other script */
746 if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
748 /* Try to register us to the master server */
749 _network_need_advertise = true;
750 NetworkUDPAdvertise();
752 /* welcome possibly still connected admins - this can only happen on a dedicated server. */
753 if (_network_dedicated) ServerNetworkAdminSocketHandler::WelcomeAll();
755 return true;
758 /* The server is rebooting...
759 * The only difference with NetworkDisconnect, is the packets that is sent */
760 void NetworkReboot()
762 if (_network_server) {
763 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
764 cs->SendNewGame();
765 cs->SendPackets();
768 for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
769 as->SendNewGame();
770 as->SendPackets();
774 /* For non-dedicated servers we have to kick the admins as we are not
775 * certain that we will end up in a new network game. */
776 NetworkClose(!_network_dedicated);
780 * We want to disconnect from the host/clients.
781 * @param blocking whether to wait till everything has been closed.
782 * @param close_admins Whether the admin sockets need to be closed as well.
784 void NetworkDisconnect(bool blocking, bool close_admins)
786 if (_network_server) {
787 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
788 cs->SendShutdown();
789 cs->SendPackets();
792 if (close_admins) {
793 for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
794 as->SendShutdown();
795 as->SendPackets();
800 if (_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(blocking);
802 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
804 NetworkClose(close_admins);
806 /* Reinitialize the UDP stack, i.e. close all existing connections. */
807 NetworkUDPInitialize();
811 * Receives something from the network.
812 * @return true if everything went fine, false when the connection got closed.
814 static bool NetworkReceive()
816 if (_network_server) {
817 ServerNetworkAdminSocketHandler::Receive();
818 return ServerNetworkGameSocketHandler::Receive();
819 } else {
820 return ClientNetworkGameSocketHandler::Receive();
824 /* This sends all buffered commands (if possible) */
825 static void NetworkSend()
827 if (_network_server) {
828 ServerNetworkAdminSocketHandler::Send();
829 ServerNetworkGameSocketHandler::Send();
830 } else {
831 ClientNetworkGameSocketHandler::Send();
836 * We have to do some (simple) background stuff that runs normally,
837 * even when we are not in multiplayer. For example stuff needed
838 * for finding servers or downloading content.
840 void NetworkBackgroundLoop()
842 _network_content_client.SendReceive();
843 TCPConnecter::CheckCallbacks();
844 NetworkHTTPSocketHandler::HTTPReceive();
846 NetworkBackgroundUDPLoop();
849 /* The main loop called from ttd.c
850 * Here we also have to do StateGameLoop if needed! */
851 void NetworkGameLoop()
853 if (!_networking) return;
855 if (!NetworkReceive()) return;
857 if (_network_server) {
858 /* Log the sync state to check for in-syncedness of replays. */
859 if (_date_fract == 0) {
860 /* We don't want to log multiple times if paused. */
861 static Date last_log;
862 if (last_log != _date) {
863 DEBUG(desync, 1, "sync: %08x; %02x; %08x; %08x", _date, _date_fract, _random.state[0], _random.state[1]);
864 last_log = _date;
868 #ifdef DEBUG_DUMP_COMMANDS
869 /* Loading of the debug commands from -ddesync>=1 */
870 static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
871 static Date next_date = 0;
872 static uint32 next_date_fract;
873 static CommandPacket *cp = nullptr;
874 static bool check_sync_state = false;
875 static uint32 sync_state[2];
876 if (f == nullptr && next_date == 0) {
877 DEBUG(net, 0, "Cannot open commands.log");
878 next_date = 1;
881 while (f != nullptr && !feof(f)) {
882 if (_date == next_date && _date_fract == next_date_fract) {
883 if (cp != nullptr) {
884 NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text, cp->company);
885 DEBUG(net, 0, "injecting: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, cp->tile, cp->p1, cp->p2, cp->cmd, cp->text, GetCommandName(cp->cmd));
886 free(cp);
887 cp = nullptr;
889 if (check_sync_state) {
890 if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
891 DEBUG(net, 0, "sync check: %08x; %02x; match", _date, _date_fract);
892 } else {
893 DEBUG(net, 0, "sync check: %08x; %02x; mismatch expected {%08x, %08x}, got {%08x, %08x}",
894 _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
895 NOT_REACHED();
897 check_sync_state = false;
901 if (cp != nullptr || check_sync_state) break;
903 char buff[4096];
904 if (fgets(buff, lengthof(buff), f) == nullptr) break;
906 char *p = buff;
907 /* Ignore the "[date time] " part of the message */
908 if (*p == '[') {
909 p = strchr(p, ']');
910 if (p == nullptr) break;
911 p += 2;
914 if (strncmp(p, "cmd: ", 5) == 0
915 #ifdef DEBUG_FAILED_DUMP_COMMANDS
916 || strncmp(p, "cmdf: ", 6) == 0
917 #endif
919 p += 5;
920 if (*p == ' ') p++;
921 cp = CallocT<CommandPacket>(1);
922 int company;
923 static_assert(sizeof(cp->text) == 128);
924 int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %x; \"%127[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text);
925 /* There are 8 pieces of data to read, however the last is a
926 * string that might or might not exist. Ignore it if that
927 * string misses because in 99% of the time it's not used. */
928 assert(ret == 8 || ret == 7);
929 cp->company = (CompanyID)company;
930 } else if (strncmp(p, "join: ", 6) == 0) {
931 /* Manually insert a pause when joining; this way the client can join at the exact right time. */
932 int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
933 assert(ret == 2);
934 DEBUG(net, 0, "injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
935 cp = CallocT<CommandPacket>(1);
936 cp->company = COMPANY_SPECTATOR;
937 cp->cmd = CMD_PAUSE;
938 cp->p1 = PM_PAUSED_NORMAL;
939 cp->p2 = 1;
940 _ddc_fastforward = false;
941 } else if (strncmp(p, "sync: ", 6) == 0) {
942 int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
943 assert(ret == 4);
944 check_sync_state = true;
945 } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
946 strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
947 /* A message that is not very important to the log playback, but part of the log. */
948 #ifndef DEBUG_FAILED_DUMP_COMMANDS
949 } else if (strncmp(p, "cmdf: ", 6) == 0) {
950 DEBUG(net, 0, "Skipping replay of failed command: %s", p + 6);
951 #endif
952 } else {
953 /* Can't parse a line; what's wrong here? */
954 DEBUG(net, 0, "trying to parse: %s", p);
955 NOT_REACHED();
958 if (f != nullptr && feof(f)) {
959 DEBUG(net, 0, "End of commands.log");
960 fclose(f);
961 f = nullptr;
963 #endif /* DEBUG_DUMP_COMMANDS */
964 if (_frame_counter >= _frame_counter_max) {
965 /* Only check for active clients just before we're going to send out
966 * the commands so we don't send multiple pause/unpause commands when
967 * the frame_freq is more than 1 tick. Same with distributing commands. */
968 CheckPauseOnJoin();
969 CheckMinActiveClients();
970 NetworkDistributeCommands();
973 bool send_frame = false;
975 /* We first increase the _frame_counter */
976 _frame_counter++;
977 /* Update max-frame-counter */
978 if (_frame_counter > _frame_counter_max) {
979 _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
980 send_frame = true;
983 NetworkExecuteLocalCommandQueue();
985 /* Then we make the frame */
986 StateGameLoop();
988 _sync_seed_1 = _random.state[0];
989 #ifdef NETWORK_SEND_DOUBLE_SEED
990 _sync_seed_2 = _random.state[1];
991 #endif
993 NetworkServer_Tick(send_frame);
994 } else {
995 /* Client */
997 /* Make sure we are at the frame were the server is (quick-frames) */
998 if (_frame_counter_server > _frame_counter) {
999 /* Run a number of frames; when things go bad, get out. */
1000 while (_frame_counter_server > _frame_counter) {
1001 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1003 } else {
1004 /* Else, keep on going till _frame_counter_max */
1005 if (_frame_counter_max > _frame_counter) {
1006 /* Run one frame; if things went bad, get out. */
1007 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1012 NetworkSend();
1015 static void NetworkGenerateServerId()
1017 Md5 checksum;
1018 uint8 digest[16];
1019 char hex_output[16 * 2 + 1];
1020 char coding_string[NETWORK_NAME_LENGTH];
1021 int di;
1023 seprintf(coding_string, lastof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
1025 /* Generate the MD5 hash */
1026 checksum.Append((const uint8*)coding_string, strlen(coding_string));
1027 checksum.Finish(digest);
1029 for (di = 0; di < 16; ++di) {
1030 seprintf(hex_output + di * 2, lastof(hex_output), "%02x", digest[di]);
1033 /* _settings_client.network.network_id is our id */
1034 seprintf(_settings_client.network.network_id, lastof(_settings_client.network.network_id), "%s", hex_output);
1037 void NetworkStartDebugLog(NetworkAddress address)
1039 extern SOCKET _debug_socket; // Comes from debug.c
1041 DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
1043 SOCKET s = address.Connect();
1044 if (s == INVALID_SOCKET) {
1045 DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
1046 return;
1049 _debug_socket = s;
1051 DEBUG(net, 0, "DEBUG() is now redirected");
1054 /** This tries to launch the network for a given OS */
1055 void NetworkStartUp()
1057 DEBUG(net, 3, "[core] starting network...");
1059 /* Network is available */
1060 _network_available = NetworkCoreInitialize();
1061 _network_dedicated = false;
1062 _network_need_advertise = true;
1063 _network_advertise_retries = 0;
1065 /* Generate an server id when there is none yet */
1066 if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
1068 memset(&_network_game_info, 0, sizeof(_network_game_info));
1070 NetworkInitialize();
1071 DEBUG(net, 3, "[core] network online, multiplayer available");
1072 NetworkFindBroadcastIPs(&_broadcast_list);
1075 /** This shuts the network down */
1076 void NetworkShutDown()
1078 NetworkDisconnect(true);
1079 NetworkUDPClose();
1081 DEBUG(net, 3, "[core] shutting down network");
1083 _network_available = false;
1085 NetworkCoreShutdown();
1089 * How many hex digits of the git hash to include in network revision string.
1090 * Determined as 10 hex digits + 2 characters for -g/-u/-m prefix.
1092 static const uint GITHASH_SUFFIX_LEN = 12;
1095 * Get the network version string used by this build.
1096 * The returned string is guaranteed to be at most NETWORK_REVISON_LENGTH bytes.
1098 const char * GetNetworkRevisionString()
1100 /* This will be allocated on heap and never free'd, but only once so not a "real" leak. */
1101 static char *network_revision = nullptr;
1103 if (!network_revision) {
1104 /* Start by taking a chance on the full revision string. */
1105 network_revision = stredup(_openttd_revision);
1106 /* Ensure it's not longer than the packet buffer length. */
1107 if (strlen(network_revision) >= NETWORK_REVISION_LENGTH) network_revision[NETWORK_REVISION_LENGTH - 1] = '\0';
1109 /* Tag names are not mangled further. */
1110 if (_openttd_revision_tagged) {
1111 DEBUG(net, 1, "Network revision name is '%s'", network_revision);
1112 return network_revision;
1115 /* Prepare a prefix of the git hash.
1116 * Size is length + 1 for terminator, +2 for -g prefix. */
1117 assert(_openttd_revision_modified < 3);
1118 char githash_suffix[GITHASH_SUFFIX_LEN + 1] = "-";
1119 githash_suffix[1] = "gum"[_openttd_revision_modified];
1120 for (uint i = 2; i < GITHASH_SUFFIX_LEN; i++) {
1121 githash_suffix[i] = _openttd_revision_hash[i-2];
1124 /* Where did the hash start in the original string?
1125 * Overwrite from that position, unless that would go past end of packet buffer length. */
1126 ptrdiff_t hashofs = strrchr(_openttd_revision, '-') - _openttd_revision;
1127 if (hashofs + strlen(githash_suffix) + 1 > NETWORK_REVISION_LENGTH) hashofs = strlen(network_revision) - strlen(githash_suffix);
1128 /* Replace the git hash in revision string. */
1129 strecpy(network_revision + hashofs, githash_suffix, network_revision + NETWORK_REVISION_LENGTH);
1130 assert(strlen(network_revision) < NETWORK_REVISION_LENGTH); // strlen does not include terminator, constant does, hence strictly less than
1131 DEBUG(net, 1, "Network revision name is '%s'", network_revision);
1134 return network_revision;
1137 static const char *ExtractNetworkRevisionHash(const char *revstr)
1139 return strrchr(revstr, '-');
1143 * Checks whether the given version string is compatible with our version.
1144 * First tries to match the full string, if that fails, attempts to compare just git hashes.
1145 * @param other the version string to compare to
1147 bool IsNetworkCompatibleVersion(const char *other)
1149 if (strncmp(GetNetworkRevisionString(), other, NETWORK_REVISION_LENGTH - 1) == 0) return true;
1151 /* If this version is tagged, then the revision string must be a complete match,
1152 * since there is no git hash suffix in it.
1153 * This is needed to avoid situations like "1.9.0-beta1" comparing equal to "2.0.0-beta1". */
1154 if (_openttd_revision_tagged) return false;
1156 const char *hash1 = ExtractNetworkRevisionHash(GetNetworkRevisionString());
1157 const char *hash2 = ExtractNetworkRevisionHash(other);
1158 return hash1 && hash2 && (strncmp(hash1, hash2, GITHASH_SUFFIX_LEN) == 0);
1161 #ifdef __EMSCRIPTEN__
1162 extern "C" {
1164 void CDECL em_openttd_add_server(const char *host, int port)
1166 NetworkUDPQueryServer(NetworkAddress(host, port), true);
1170 #endif