Increase the number of road and tram subtypes to 32.
[openttd-joker.git] / src / network / network.cpp
blob03dab641dacf2307103f087696a2037fddf3d7d3
1 /* $Id: network.cpp 25997 2013-11-14 19:31:10Z rubidium $ */
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 */
10 /** @file network.cpp Base functions for networking support. */
12 #include "../stdafx.h"
14 #ifdef ENABLE_NETWORK
16 #include "../strings_func.h"
17 #include "../command_func.h"
18 #include "../date_func.h"
19 #include "network_admin.h"
20 #include "network_client.h"
21 #include "network_server.h"
22 #include "network_content.h"
23 #include "network_udp.h"
24 #include "network_gamelist.h"
25 #include "network_base.h"
26 #include "core/udp.h"
27 #include "core/host.h"
28 #include "network_gui.h"
29 #include "../console_func.h"
30 #include "../3rdparty/md5/md5.h"
31 #include "../core/random_func.hpp"
32 #include "../window_func.h"
33 #include "../company_func.h"
34 #include "../company_base.h"
35 #include "../landscape_type.h"
36 #include "../rev.h"
37 #include "../core/pool_func.hpp"
38 #include "../gfx_func.h"
39 #include "../error.h"
41 #include "../safeguards.h"
43 #ifdef DEBUG_DUMP_COMMANDS
44 #include "../fileio_func.h"
45 /** When running the server till the wait point, run as fast as we can! */
46 bool _ddc_fastforward = true;
47 #endif /* DEBUG_DUMP_COMMANDS */
49 /** Make sure both pools have the same size. */
50 assert_compile(NetworkClientInfoPool::MAX_SIZE == NetworkClientSocketPool::MAX_SIZE);
52 /** The pool with client information. */
53 NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo");
54 INSTANTIATE_POOL_METHODS(NetworkClientInfo)
56 bool _networking; ///< are we in networking mode?
57 bool _network_server; ///< network-server is active
58 bool _network_available; ///< is network mode available?
59 bool _network_dedicated; ///< are we a dedicated server?
60 bool _is_network_server; ///< Does this client wants to be a network-server?
61 NetworkServerGameInfo _network_game_info; ///< Information about our game.
62 NetworkCompanyState *_network_company_states = nullptr; ///< Statistics about some companies.
63 ClientID _network_own_client_id; ///< Our client identifier.
64 ClientID _redirect_console_to_client; ///< If not invalid, redirect the console output to a client.
65 bool _network_need_advertise; ///< Whether we need to advertise.
66 uint8 _network_reconnect; ///< Reconnect timeout
67 StringList _network_bind_list; ///< The addresses to bind on.
68 StringList _network_host_list; ///< The servers we know.
69 StringList _network_ban_list; ///< The banned clients.
70 uint32 _frame_counter_server; ///< The frame_counter of the server, if in network-mode
71 uint32 _frame_counter_max; ///< To where we may go with our clients
72 uint32 _frame_counter; ///< The current frame.
73 uint32 _last_sync_frame; ///< Used in the server to store the last time a sync packet was sent to clients.
74 NetworkAddressList _broadcast_list; ///< List of broadcast addresses.
75 uint32 _sync_seed_1; ///< Seed to compare during sync checks.
76 #ifdef NETWORK_SEND_DOUBLE_SEED
77 uint32 _sync_seed_2; ///< Second part of the seed.
78 #endif
79 uint32 _sync_frame; ///< The frame to perform the sync check.
80 bool _network_first_time; ///< Whether we have finished joining or not.
81 bool _network_udp_server; ///< Is the UDP server started?
82 uint16 _network_udp_broadcast; ///< Timeout for the UDP broadcasts.
83 uint8 _network_advertise_retries; ///< The number of advertisement retries we did.
84 CompanyMask _network_company_passworded; ///< Bitmask of the password status of all companies.
86 /* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
87 assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
88 assert_compile((int)NETWORK_COMPANY_NAME_LENGTH == MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH);
90 extern NetworkUDPSocketHandler *_udp_client_socket; ///< udp client socket
91 extern NetworkUDPSocketHandler *_udp_server_socket; ///< udp server socket
92 extern NetworkUDPSocketHandler *_udp_master_socket; ///< udp master socket
94 /** The amount of clients connected */
95 byte _network_clients_connected = 0;
97 /* Some externs / forwards */
98 extern void StateGameLoop();
101 * Return whether there is any client connected or trying to connect at all.
102 * @return whether we have any client activity
104 bool HasClients()
106 NetworkClientSocket *cs;
107 FOR_ALL_CLIENT_SOCKETS(cs) return true;
109 return false;
113 * Basically a client is leaving us right now.
115 NetworkClientInfo::~NetworkClientInfo()
117 /* Delete the chat window, if you were chatting with this client. */
118 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_CLIENT, this->client_id);
122 * Return the CI given it's client-identifier
123 * @param client_id the ClientID to search for
124 * @return return a pointer to the corresponding NetworkClientInfo struct or nullptr when not found
126 /* static */ NetworkClientInfo *NetworkClientInfo::GetByClientID(ClientID client_id)
128 NetworkClientInfo *ci;
130 FOR_ALL_CLIENT_INFOS(ci) {
131 if (ci->client_id == client_id) return ci;
134 return nullptr;
138 * Return the client state given it's client-identifier
139 * @param client_id the ClientID to search for
140 * @return return a pointer to the corresponding NetworkClientSocket struct or nullptr when not found
142 /* static */ ServerNetworkGameSocketHandler *ServerNetworkGameSocketHandler::GetByClientID(ClientID client_id)
144 NetworkClientSocket *cs;
146 FOR_ALL_CLIENT_SOCKETS(cs) {
147 if (cs->client_id == client_id) return cs;
150 return nullptr;
153 byte NetworkSpectatorCount()
155 const NetworkClientInfo *ci;
156 byte count = 0;
158 FOR_ALL_CLIENT_INFOS(ci) {
159 if (ci->client_playas == COMPANY_SPECTATOR) count++;
162 /* Don't count a dedicated server as spectator */
163 if (_network_dedicated) count--;
165 return count;
169 * Change the company password of a given company.
170 * @param company_id ID of the company the password should be changed for.
171 * @param password The unhashed password we like to set ('*' or '' resets the password)
172 * @return The password.
174 const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
176 if (strcmp(password, "*") == 0) password = "";
178 if (_network_server) {
179 NetworkServerSetCompanyPassword(company_id, password, false);
180 } else {
181 NetworkClientSetCompanyPassword(password);
184 return password;
188 * Hash the given password using server ID and game seed.
189 * @param password Password to hash.
190 * @param password_server_id Server ID.
191 * @param password_game_seed Game seed.
192 * @return The hashed password.
194 const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
196 if (StrEmpty(password)) return password;
198 char salted_password[NETWORK_SERVER_ID_LENGTH];
200 memset(salted_password, 0, sizeof(salted_password));
201 seprintf(salted_password, lastof(salted_password), "%s", password);
202 /* Add the game seed and the server's ID as the salt. */
203 for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
204 salted_password[i] ^= password_server_id[i] ^ (password_game_seed >> (i % 32));
207 Md5 checksum;
208 uint8 digest[16];
209 static char hashed_password[NETWORK_SERVER_ID_LENGTH];
211 /* Generate the MD5 hash */
212 checksum.Append(salted_password, sizeof(salted_password) - 1);
213 checksum.Finish(digest);
215 for (int di = 0; di < 16; di++) seprintf(hashed_password + di * 2, lastof(hashed_password), "%02x", digest[di]);
217 return hashed_password;
221 * Check if the company we want to join requires a password.
222 * @param company_id id of the company we want to check the 'passworded' flag for.
223 * @return true if the company requires a password.
225 bool NetworkCompanyIsPassworded(CompanyID company_id)
227 return HasBit(_network_company_passworded, company_id);
230 /* This puts a text-message to the console, or in the future, the chat-box,
231 * (to keep it all a bit more general)
232 * If 'self_send' is true, this is the client who is sending the message */
233 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const char *name, const char *str, NetworkTextMessageData data)
235 StringID strid;
236 switch (action) {
237 case NETWORK_ACTION_SERVER_MESSAGE:
238 /* Ignore invalid messages */
239 strid = STR_NETWORK_SERVER_MESSAGE;
240 colour = CC_DEFAULT;
241 break;
242 case NETWORK_ACTION_COMPANY_SPECTATOR:
243 colour = CC_DEFAULT;
244 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
245 break;
246 case NETWORK_ACTION_COMPANY_JOIN:
247 colour = CC_DEFAULT;
248 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
249 break;
250 case NETWORK_ACTION_COMPANY_NEW:
251 colour = CC_DEFAULT;
252 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
253 break;
254 case NETWORK_ACTION_JOIN:
255 /* Show the Client ID for the server but not for the client. */
256 strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID : STR_NETWORK_MESSAGE_CLIENT_JOINED;
257 break;
258 case NETWORK_ACTION_LEAVE: strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
259 case NETWORK_ACTION_NAME_CHANGE: strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
260 case NETWORK_ACTION_GIVE_MONEY: strid = self_send ? STR_NETWORK_MESSAGE_GAVE_MONEY_AWAY : STR_NETWORK_MESSAGE_GIVE_MONEY; break;
261 case NETWORK_ACTION_CHAT_COMPANY: strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
262 case NETWORK_ACTION_CHAT_CLIENT: strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT; break;
263 default: strid = STR_NETWORK_CHAT_ALL; break;
266 char message[1024];
267 SetDParamStr(0, name);
268 SetDParamStr(1, str);
269 SetDParam(2, data.data);
271 /* All of these strings start with "***". These characters are interpreted as both left-to-right and
272 * right-to-left characters depending on the context. As the next text might be an user's name, the
273 * user name's characters will influence the direction of the "***" instead of the language setting
274 * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
275 char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
276 GetString(msg_ptr, strid, lastof(message));
278 DEBUG(desync, 1, "msg: %08x; %02x; %s", _date, _date_fract, message);
279 IConsolePrintF(colour, "%s", message);
280 NetworkAddChatMessage((TextColour)colour, _settings_client.gui.network_chat_timeout, "%s", message);
283 /* Calculate the frame-lag of a client */
284 uint NetworkCalculateLag(const NetworkClientSocket *cs)
286 int lag = cs->last_frame_server - cs->last_frame;
287 /* This client has missed his ACK packet after 1 DAY_TICKS..
288 * so we increase his lag for every frame that passes!
289 * The packet can be out by a max of _net_frame_freq */
290 if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
291 lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
293 return lag;
297 /* There was a non-recoverable error, drop back to the main menu with a nice
298 * error */
299 void NetworkError(StringID error_string)
301 _switch_mode = SM_MENU;
302 ShowErrorMessage(error_string, INVALID_STRING_ID, WL_CRITICAL);
306 * Retrieve the string id of an internal error number
307 * @param err NetworkErrorCode
308 * @return the StringID
310 StringID GetNetworkErrorMsg(NetworkErrorCode err)
312 /* List of possible network errors, used by
313 * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
314 static const StringID network_error_strings[] = {
315 STR_NETWORK_ERROR_CLIENT_GENERAL,
316 STR_NETWORK_ERROR_CLIENT_DESYNC,
317 STR_NETWORK_ERROR_CLIENT_SAVEGAME,
318 STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST,
319 STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR,
320 STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH,
321 STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED,
322 STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED,
323 STR_NETWORK_ERROR_CLIENT_WRONG_REVISION,
324 STR_NETWORK_ERROR_CLIENT_NAME_IN_USE,
325 STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD,
326 STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH,
327 STR_NETWORK_ERROR_CLIENT_KICKED,
328 STR_NETWORK_ERROR_CLIENT_CHEATER,
329 STR_NETWORK_ERROR_CLIENT_SERVER_FULL,
330 STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS,
331 STR_NETWORK_ERROR_CLIENT_TIMEOUT_PASSWORD,
332 STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER,
333 STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP,
334 STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN,
336 assert_compile(lengthof(network_error_strings) == NETWORK_ERROR_END);
338 if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
340 return network_error_strings[err];
344 * Handle the pause mode change so we send the right messages to the chat.
345 * @param prev_mode The previous pause mode.
346 * @param changed_mode The pause mode that got changed.
348 void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
350 if (!_networking) return;
352 switch (changed_mode) {
353 case PM_PAUSED_NORMAL:
354 case PM_PAUSED_JOIN:
355 case PM_PAUSED_GAME_SCRIPT:
356 case PM_PAUSED_ACTIVE_CLIENTS:
357 case PM_PAUSED_LINK_GRAPH: {
358 bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
359 bool paused = (_pause_mode != PM_UNPAUSED);
360 if (!paused && !changed) return;
362 StringID str;
363 if (!changed) {
364 int i = -1;
366 if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL);
367 if ((_pause_mode & PM_PAUSED_JOIN) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS);
368 if ((_pause_mode & PM_PAUSED_GAME_SCRIPT) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT);
369 if ((_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS);
370 if ((_pause_mode & PM_PAUSED_LINK_GRAPH) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH);
371 str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
372 } else {
373 switch (changed_mode) {
374 case PM_PAUSED_NORMAL: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
375 case PM_PAUSED_JOIN: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
376 case PM_PAUSED_GAME_SCRIPT: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT); break;
377 case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
378 case PM_PAUSED_LINK_GRAPH: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH); break;
379 default: NOT_REACHED();
381 str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
384 char buffer[DRAW_STRING_BUFFER];
385 GetString(buffer, str, lastof(buffer));
386 NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, nullptr, buffer);
387 break;
390 default:
391 return;
397 * Helper function for the pause checkers. If pause is true and the
398 * current pause mode isn't set the game will be paused, if it it false
399 * and the pause mode is set the game will be unpaused. In the other
400 * cases nothing happens to the pause state.
401 * @param pause whether we'd like to pause
402 * @param pm the mode which we would like to pause with
404 static void CheckPauseHelper(bool pause, PauseMode pm)
406 if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
408 DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
412 * Counts the number of active clients connected.
413 * It has to be in STATUS_ACTIVE and not a spectator
414 * @return number of active clients
416 static uint NetworkCountActiveClients()
418 const NetworkClientSocket *cs;
419 uint count = 0;
421 FOR_ALL_CLIENT_SOCKETS(cs) {
422 if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
423 if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
424 count++;
427 return count;
431 * Check if the minimum number of active clients has been reached and pause or unpause the game as appropriate
433 static void CheckMinActiveClients()
435 if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
436 !_network_dedicated ||
437 (_settings_client.network.min_active_clients == 0 && (_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) == PM_UNPAUSED)) {
438 return;
440 CheckPauseHelper(NetworkCountActiveClients() < _settings_client.network.min_active_clients, PM_PAUSED_ACTIVE_CLIENTS);
444 * Checks whether there is a joining client
445 * @return true iff one client is joining (but not authorizing)
447 static bool NetworkHasJoiningClient()
449 const NetworkClientSocket *cs;
450 FOR_ALL_CLIENT_SOCKETS(cs) {
451 if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
454 return false;
458 * Check whether we should pause on join
460 static void CheckPauseOnJoin()
462 if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
463 (!_settings_client.network.pause_on_join && (_pause_mode & PM_PAUSED_JOIN) == PM_UNPAUSED)) {
464 return;
466 CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
470 * Converts a string to ip/port/company
471 * Format: IP:port#company
473 * connection_string will be re-terminated to separate out the hostname, and company and port will
474 * be set to the company and port strings given by the user, inside the memory area originally
475 * occupied by connection_string.
477 void ParseConnectionString(const char **company, const char **port, char *connection_string)
479 bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
480 char *p;
481 for (p = connection_string; *p != '\0'; p++) {
482 switch (*p) {
483 case '[':
484 ipv6 = true;
485 break;
487 case ']':
488 ipv6 = false;
489 break;
491 case '#':
492 *company = p + 1;
493 *p = '\0';
494 break;
496 case ':':
497 if (ipv6) break;
498 *port = p + 1;
499 *p = '\0';
500 break;
506 * Handle the accepting of a connection to the server.
507 * @param s The socket of the new connection.
508 * @param address The address of the peer.
510 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
512 /* Register the login */
513 _network_clients_connected++;
515 SetWindowDirty(WC_CLIENT_LIST, 0);
516 ServerNetworkGameSocketHandler *cs = new ServerNetworkGameSocketHandler(s);
517 cs->client_address = address; // Save the IP of the client
521 * Resets the pools used for network clients, and the admin pool if needed.
522 * @param close_admins Whether the admin pool has to be cleared as well.
524 static void InitializeNetworkPools(bool close_admins = true)
526 PoolBase::Clean(PT_NCLIENT | (close_admins ? PT_NADMIN : PT_NONE));
530 * Close current connections.
531 * @param close_admins Whether the admin connections have to be closed as well.
533 void NetworkClose(bool close_admins)
535 if (_network_server) {
536 if (close_admins) {
537 ServerNetworkAdminSocketHandler *as;
538 FOR_ALL_ADMIN_SOCKETS(as) {
539 as->CloseConnection(true);
543 NetworkClientSocket *cs;
544 FOR_ALL_CLIENT_SOCKETS(cs) {
545 cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
547 ServerNetworkGameSocketHandler::CloseListeners();
548 ServerNetworkAdminSocketHandler::CloseListeners();
549 } else if (MyClient::my_client != nullptr) {
550 MyClient::SendQuit();
551 MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
554 TCPConnecter::KillAll();
556 _networking = false;
557 _network_server = false;
559 NetworkFreeLocalCommandQueue();
561 free(_network_company_states);
562 _network_company_states = nullptr;
564 InitializeNetworkPools(close_admins);
567 /* Initializes the network (cleans sockets and stuff) */
568 static void NetworkInitialize(bool close_admins = true)
570 InitializeNetworkPools(close_admins);
571 NetworkUDPInitialize();
573 _sync_frame = 0;
574 _network_first_time = true;
576 _network_reconnect = 0;
579 /** Non blocking connection create to query servers */
580 class TCPQueryConnecter : TCPConnecter {
581 public:
582 TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
584 virtual void OnFailure()
586 NetworkDisconnect();
589 virtual void OnConnect(SOCKET s)
591 _networking = true;
592 new ClientNetworkGameSocketHandler(s);
593 MyClient::SendCompanyInformationQuery();
597 /* Query a server to fetch his game-info
598 * If game_info is true, only the gameinfo is fetched,
599 * else only the client_info is fetched */
600 void NetworkTCPQueryServer(NetworkAddress address)
602 if (!_network_available) return;
604 NetworkDisconnect();
605 NetworkInitialize();
607 new TCPQueryConnecter(address);
610 /* Validates an address entered as a string and adds the server to
611 * the list. If you use this function, the games will be marked
612 * as manually added. */
613 void NetworkAddServer(const char *b)
615 if (*b != '\0') {
616 const char *port = nullptr;
617 const char *company = nullptr;
618 char host[NETWORK_HOSTNAME_LENGTH];
619 uint16 rport;
621 strecpy(host, b, lastof(host));
623 strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
624 rport = NETWORK_DEFAULT_PORT;
626 ParseConnectionString(&company, &port, host);
627 if (port != nullptr) rport = atoi(port);
629 NetworkUDPQueryServer(NetworkAddress(host, rport), true);
634 * Get the addresses to bind to.
635 * @param addresses the list to write to.
636 * @param port the port to bind to.
638 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
640 for (char **iter = _network_bind_list.Begin(); iter != _network_bind_list.End(); iter++) {
641 *addresses->Append() = NetworkAddress(*iter, port);
644 /* No address, so bind to everything. */
645 if (addresses->Length() == 0) {
646 *addresses->Append() = NetworkAddress("", port);
650 /* Generates the list of manually added hosts from NetworkGameList and
651 * dumps them into the array _network_host_list. This array is needed
652 * by the function that generates the config file. */
653 void NetworkRebuildHostList()
655 _network_host_list.Clear();
657 for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
658 if (item->manually) *_network_host_list.Append() = stredup(item->address.GetAddressAsString(false));
662 /** Non blocking connection create to actually connect to servers */
663 class TCPClientConnecter : TCPConnecter {
664 public:
665 TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
667 virtual void OnFailure()
669 NetworkError(STR_NETWORK_ERROR_NOCONNECTION);
672 virtual void OnConnect(SOCKET s)
674 _networking = true;
675 new ClientNetworkGameSocketHandler(s);
676 IConsoleCmdExec("exec scripts/on_client.scr 0");
677 NetworkClient_Connected();
682 /* Used by clients, to connect to a server */
683 void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
685 if (!_network_available) return;
687 if (address.GetPort() == 0) return;
689 strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
690 _settings_client.network.last_port = address.GetPort();
691 _network_join_as = join_as;
692 _network_join_server_password = join_server_password;
693 _network_join_company_password = join_company_password;
695 NetworkDisconnect();
696 NetworkInitialize();
698 _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
699 ShowJoinStatusWindow();
701 new TCPClientConnecter(address);
704 static void NetworkInitGameInfo()
706 if (StrEmpty(_settings_client.network.server_name)) {
707 seprintf(_settings_client.network.server_name, lastof(_settings_client.network.server_name), "Unnamed Server");
710 /* The server is a client too */
711 _network_game_info.clients_on = _network_dedicated ? 0 : 1;
713 /* There should be always space for the server. */
714 assert(NetworkClientInfo::CanAllocateItem());
715 NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
716 ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
718 strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
721 bool NetworkServerStart()
723 if (!_network_available) return false;
725 /* Call the pre-scripts */
726 IConsoleCmdExec("exec scripts/pre_server.scr 0");
727 if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
729 NetworkDisconnect(false, false);
730 NetworkInitialize(false);
731 DEBUG(net, 1, "starting listeners for clients");
732 if (!ServerNetworkGameSocketHandler::Listen(_settings_client.network.server_port)) return false;
734 /* Only listen for admins when the password isn't empty. */
735 if (!StrEmpty(_settings_client.network.admin_password)) {
736 DEBUG(net, 1, "starting listeners for admins");
737 if (!ServerNetworkAdminSocketHandler::Listen(_settings_client.network.server_admin_port)) return false;
740 /* Try to start UDP-server */
741 DEBUG(net, 1, "starting listeners for incoming server queries");
742 _network_udp_server = _udp_server_socket->Listen();
744 _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
745 _network_server = true;
746 _networking = true;
747 _frame_counter = 0;
748 _frame_counter_server = 0;
749 _frame_counter_max = 0;
750 _last_sync_frame = 0;
751 _network_own_client_id = CLIENT_ID_SERVER;
753 _network_clients_connected = 0;
754 _network_company_passworded = 0;
756 NetworkInitGameInfo();
758 /* execute server initialization script */
759 IConsoleCmdExec("exec scripts/on_server.scr 0");
760 /* if the server is dedicated ... add some other script */
761 if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
763 /* Try to register us to the master server */
764 _network_need_advertise = true;
765 NetworkUDPAdvertise();
767 /* welcome possibly still connected admins - this can only happen on a dedicated server. */
768 if (_network_dedicated) ServerNetworkAdminSocketHandler::WelcomeAll();
770 return true;
773 /* The server is rebooting...
774 * The only difference with NetworkDisconnect, is the packets that is sent */
775 void NetworkReboot()
777 if (_network_server) {
778 NetworkClientSocket *cs;
779 FOR_ALL_CLIENT_SOCKETS(cs) {
780 cs->SendNewGame();
781 cs->SendPackets();
784 ServerNetworkAdminSocketHandler *as;
785 FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
786 as->SendNewGame();
787 as->SendPackets();
791 /* For non-dedicated servers we have to kick the admins as we are not
792 * certain that we will end up in a new network game. */
793 NetworkClose(!_network_dedicated);
797 * We want to disconnect from the host/clients.
798 * @param blocking whether to wait till everything has been closed.
799 * @param close_admins Whether the admin sockets need to be closed as well.
801 void NetworkDisconnect(bool blocking, bool close_admins)
803 if (_network_server) {
804 NetworkClientSocket *cs;
805 FOR_ALL_CLIENT_SOCKETS(cs) {
806 cs->SendShutdown();
807 cs->SendPackets();
810 if (close_admins) {
811 ServerNetworkAdminSocketHandler *as;
812 FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
813 as->SendShutdown();
814 as->SendPackets();
819 if (_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(blocking);
821 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
823 NetworkClose(close_admins);
825 /* Reinitialize the UDP stack, i.e. close all existing connections. */
826 NetworkUDPInitialize();
830 * Receives something from the network.
831 * @return true if everything went fine, false when the connection got closed.
833 static bool NetworkReceive()
835 if (_network_server) {
836 ServerNetworkAdminSocketHandler::Receive();
837 return ServerNetworkGameSocketHandler::Receive();
838 } else {
839 return ClientNetworkGameSocketHandler::Receive();
843 /* This sends all buffered commands (if possible) */
844 static void NetworkSend()
846 if (_network_server) {
847 ServerNetworkAdminSocketHandler::Send();
848 ServerNetworkGameSocketHandler::Send();
849 } else {
850 ClientNetworkGameSocketHandler::Send();
855 * We have to do some (simple) background stuff that runs normally,
856 * even when we are not in multiplayer. For example stuff needed
857 * for finding servers or downloading content.
859 void NetworkBackgroundLoop()
861 _network_content_client.SendReceive();
862 TCPConnecter::CheckCallbacks();
863 NetworkHTTPSocketHandler::HTTPReceive();
865 NetworkBackgroundUDPLoop();
868 /* The main loop called from ttd.c
869 * Here we also have to do StateGameLoop if needed! */
870 void NetworkGameLoop()
872 if (!_networking) return;
874 if (!NetworkReceive()) return;
876 if (_network_server) {
877 /* Log the sync state to check for in-syncedness of replays. */
878 if (_date_fract == 0) {
879 /* We don't want to log multiple times if paused. */
880 static Date last_log;
881 if (last_log != _date) {
882 DEBUG(desync, 1, "sync: %08x; %02x; %08x; %08x", _date, _date_fract, _random.state[0], _random.state[1]);
883 last_log = _date;
887 #ifdef DEBUG_DUMP_COMMANDS
888 /* Loading of the debug commands from -ddesync>=1 */
889 static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
890 static Date next_date = 0;
891 static uint32 next_date_fract;
892 static CommandPacket *cp = nullptr;
893 static bool check_sync_state = false;
894 static uint32 sync_state[2];
895 if (f == nullptr && next_date == 0) {
896 DEBUG(net, 0, "Cannot open commands.log");
897 next_date = 1;
900 while (f != nullptr && !feof(f)) {
901 if (_date == next_date && _date_fract == next_date_fract) {
902 if (cp != nullptr) {
903 NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text, cp->company);
904 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));
905 free(cp);
906 cp = nullptr;
908 if (check_sync_state) {
909 if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
910 DEBUG(net, 0, "sync check: %08x; %02x; match", _date, _date_fract);
911 } else {
912 DEBUG(net, 0, "sync check: %08x; %02x; mismatch expected {%08x, %08x}, got {%08x, %08x}",
913 _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
914 NOT_REACHED();
916 check_sync_state = false;
920 if (cp != nullptr || check_sync_state) break;
922 char buff[4096];
923 if (fgets(buff, lengthof(buff), f) == nullptr) break;
925 char *p = buff;
926 /* Ignore the "[date time] " part of the message */
927 if (*p == '[') {
928 p = strchr(p, ']');
929 if (p == nullptr) break;
930 p += 2;
933 if (strncmp(p, "cmd: ", 5) == 0
934 #ifdef DEBUG_FAILED_DUMP_COMMANDS
935 || strncmp(p, "cmdf: ", 6) == 0
936 #endif
938 p += 5;
939 if (*p == ' ') p++;
940 cp = CallocT<CommandPacket>(1);
941 int company;
942 int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %x; \"%[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text);
943 /* There are 8 pieces of data to read, however the last is a
944 * string that might or might not exist. Ignore it if that
945 * string misses because in 99% of the time it's not used. */
946 assert(ret == 8 || ret == 7);
947 cp->company = (CompanyID)company;
948 } else if (strncmp(p, "join: ", 6) == 0) {
949 /* Manually insert a pause when joining; this way the client can join at the exact right time. */
950 int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
951 assert(ret == 2);
952 DEBUG(net, 0, "injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
953 cp = CallocT<CommandPacket>(1);
954 cp->company = COMPANY_SPECTATOR;
955 cp->cmd = CMD_PAUSE;
956 cp->p1 = PM_PAUSED_NORMAL;
957 cp->p2 = 1;
958 _ddc_fastforward = false;
959 } else if (strncmp(p, "sync: ", 6) == 0) {
960 int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
961 assert(ret == 4);
962 check_sync_state = true;
963 } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
964 strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
965 /* A message that is not very important to the log playback, but part of the log. */
966 #ifndef DEBUG_FAILED_DUMP_COMMANDS
967 } else if (strncmp(p, "cmdf: ", 6) == 0) {
968 DEBUG(net, 0, "Skipping replay of failed command: %s", p + 6);
969 #endif
970 } else {
971 /* Can't parse a line; what's wrong here? */
972 DEBUG(net, 0, "trying to parse: %s", p);
973 NOT_REACHED();
976 if (f != nullptr && feof(f)) {
977 DEBUG(net, 0, "End of commands.log");
978 fclose(f);
979 f = nullptr;
981 #endif /* DEBUG_DUMP_COMMANDS */
982 if (_frame_counter >= _frame_counter_max) {
983 /* Only check for active clients just before we're going to send out
984 * the commands so we don't send multiple pause/unpause commands when
985 * the frame_freq is more than 1 tick. Same with distributing commands. */
986 CheckPauseOnJoin();
987 CheckMinActiveClients();
988 NetworkDistributeCommands();
991 bool send_frame = false;
993 /* We first increase the _frame_counter */
994 _frame_counter++;
995 /* Update max-frame-counter */
996 if (_frame_counter > _frame_counter_max) {
997 _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
998 send_frame = true;
1001 NetworkExecuteLocalCommandQueue();
1003 /* Then we make the frame */
1004 StateGameLoop();
1006 _sync_seed_1 = _random.state[0];
1007 #ifdef NETWORK_SEND_DOUBLE_SEED
1008 _sync_seed_2 = _random.state[1];
1009 #endif
1011 NetworkServer_Tick(send_frame);
1012 } else {
1013 /* Client */
1015 /* Make sure we are at the frame were the server is (quick-frames) */
1016 if (_frame_counter_server > _frame_counter) {
1017 /* Run a number of frames; when things go bad, get out. */
1018 while (_frame_counter_server > _frame_counter) {
1019 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1021 } else {
1022 /* Else, keep on going till _frame_counter_max */
1023 if (_frame_counter_max > _frame_counter) {
1024 /* Run one frame; if things went bad, get out. */
1025 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1030 NetworkSend();
1033 static void NetworkGenerateServerId()
1035 Md5 checksum;
1036 uint8 digest[16];
1037 char hex_output[16 * 2 + 1];
1038 char coding_string[NETWORK_NAME_LENGTH];
1039 int di;
1041 seprintf(coding_string, lastof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
1043 /* Generate the MD5 hash */
1044 checksum.Append((const uint8*)coding_string, strlen(coding_string));
1045 checksum.Finish(digest);
1047 for (di = 0; di < 16; ++di) {
1048 seprintf(hex_output + di * 2, lastof(hex_output), "%02x", digest[di]);
1051 /* _settings_client.network.network_id is our id */
1052 seprintf(_settings_client.network.network_id, lastof(_settings_client.network.network_id), "%s", hex_output);
1055 void NetworkStartDebugLog(NetworkAddress address)
1057 extern SOCKET _debug_socket; // Comes from debug.c
1059 DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
1061 SOCKET s = address.Connect();
1062 if (s == INVALID_SOCKET) {
1063 DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
1064 return;
1067 _debug_socket = s;
1069 DEBUG(net, 0, "DEBUG() is now redirected");
1072 /** This tries to launch the network for a given OS */
1073 void NetworkStartUp()
1075 DEBUG(net, 3, "[core] starting network...");
1077 /* Network is available */
1078 _network_available = NetworkCoreInitialize();
1079 _network_dedicated = false;
1080 _network_need_advertise = true;
1081 _network_advertise_retries = 0;
1083 /* Generate an server id when there is none yet */
1084 if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
1086 memset(&_network_game_info, 0, sizeof(_network_game_info));
1088 NetworkInitialize();
1089 DEBUG(net, 3, "[core] network online, multiplayer available");
1090 NetworkFindBroadcastIPs(&_broadcast_list);
1093 /** This shuts the network down */
1094 void NetworkShutDown()
1096 NetworkDisconnect(true);
1097 NetworkUDPClose();
1099 DEBUG(net, 3, "[core] shutting down network");
1101 _network_available = false;
1103 NetworkCoreShutdown();
1107 * Checks whether the given version string is compatible with our version.
1108 * @param other the version string to compare to
1110 bool IsNetworkCompatibleVersion(const char *other)
1112 return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
1115 #endif /* ENABLE_NETWORK */