OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

network.cpp
Go to the documentation of this file.
1 /* $Id: network.cpp 26788 2014-09-07 14:21:16Z frosch $ */
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
14 #ifdef ENABLE_NETWORK
15
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"
40
41 #include "../safeguards.h"
42
43 #ifdef DEBUG_DUMP_COMMANDS
44 #include "../fileio_func.h"
46 bool _ddc_fastforward = true;
47 #endif /* DEBUG_DUMP_COMMANDS */
48
50 assert_compile(NetworkClientInfoPool::MAX_SIZE == NetworkClientSocketPool::MAX_SIZE);
51
53 NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo");
54 INSTANTIATE_POOL_METHODS(NetworkClientInfo)
55
56 bool _networking;
57 bool _network_server;
58 bool _network_available;
59 bool _network_dedicated;
60 bool _is_network_server;
61 NetworkServerGameInfo _network_game_info;
62 NetworkCompanyState *_network_company_states = NULL;
63 ClientID _network_own_client_id;
64 ClientID _redirect_console_to_client;
65 bool _network_need_advertise;
66 uint32 _network_last_advertise_frame;
67 uint8 _network_reconnect;
68 StringList _network_bind_list;
69 StringList _network_host_list;
70 StringList _network_ban_list;
71 uint32 _frame_counter_server;
72 uint32 _frame_counter_max;
73 uint32 _frame_counter;
74 uint32 _last_sync_frame;
75 NetworkAddressList _broadcast_list;
76 uint32 _sync_seed_1;
77 #ifdef NETWORK_SEND_DOUBLE_SEED
78 uint32 _sync_seed_2;
79 #endif
80 uint32 _sync_frame;
81 bool _network_first_time;
82 bool _network_udp_server;
83 uint16 _network_udp_broadcast;
84 uint8 _network_advertise_retries;
85 CompanyMask _network_company_passworded;
86
87 /* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
88 assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
89 assert_compile((int)NETWORK_COMPANY_NAME_LENGTH == MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH);
90
91 extern NetworkUDPSocketHandler *_udp_client_socket;
92 extern NetworkUDPSocketHandler *_udp_server_socket;
93 extern NetworkUDPSocketHandler *_udp_master_socket;
94
96 byte _network_clients_connected = 0;
97
98 /* Some externs / forwards */
99 extern void StateGameLoop();
100
105 bool HasClients()
106 {
107 NetworkClientSocket *cs;
108 FOR_ALL_CLIENT_SOCKETS(cs) return true;
109
110 return false;
111 }
112
116 NetworkClientInfo::~NetworkClientInfo()
117 {
118 /* Delete the chat window, if you were chatting with this client. */
119 InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_CLIENT, this->client_id);
120 }
121
127 /* static */ NetworkClientInfo *NetworkClientInfo::GetByClientID(ClientID client_id)
128 {
129 NetworkClientInfo *ci;
130
131 FOR_ALL_CLIENT_INFOS(ci) {
132 if (ci->client_id == client_id) return ci;
133 }
134
135 return NULL;
136 }
137
143 /* static */ ServerNetworkGameSocketHandler *ServerNetworkGameSocketHandler::GetByClientID(ClientID client_id)
144 {
145 NetworkClientSocket *cs;
146
147 FOR_ALL_CLIENT_SOCKETS(cs) {
148 if (cs->client_id == client_id) return cs;
149 }
150
151 return NULL;
152 }
153
154 byte NetworkSpectatorCount()
155 {
156 const NetworkClientInfo *ci;
157 byte count = 0;
158
159 FOR_ALL_CLIENT_INFOS(ci) {
160 if (ci->client_playas == COMPANY_SPECTATOR) count++;
161 }
162
163 /* Don't count a dedicated server as spectator */
164 if (_network_dedicated) count--;
165
166 return count;
167 }
168
175 const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
176 {
177 if (strcmp(password, "*") == 0) password = "";
178
179 if (_network_server) {
180 NetworkServerSetCompanyPassword(company_id, password, false);
181 } else {
182 NetworkClientSetCompanyPassword(password);
183 }
184
185 return password;
186 }
187
195 const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
196 {
197 if (StrEmpty(password)) return password;
198
199 char salted_password[NETWORK_SERVER_ID_LENGTH];
200
201 memset(salted_password, 0, sizeof(salted_password));
202 seprintf(salted_password, lastof(salted_password), "%s", password);
203 /* Add the game seed and the server's ID as the salt. */
204 for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
205 salted_password[i] ^= password_server_id[i] ^ (password_game_seed >> (i % 32));
206 }
207
208 Md5 checksum;
209 uint8 digest[16];
210 static char hashed_password[NETWORK_SERVER_ID_LENGTH];
211
212 /* Generate the MD5 hash */
213 checksum.Append(salted_password, sizeof(salted_password) - 1);
214 checksum.Finish(digest);
215
216 for (int di = 0; di < 16; di++) seprintf(hashed_password + di * 2, lastof(hashed_password), "%02x", digest[di]);
217
218 return hashed_password;
219 }
220
226 bool NetworkCompanyIsPassworded(CompanyID company_id)
227 {
228 return HasBit(_network_company_passworded, company_id);
229 }
230
231 /* This puts a text-message to the console, or in the future, the chat-box,
232 * (to keep it all a bit more general)
233 * If 'self_send' is true, this is the client who is sending the message */
234 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const char *name, const char *str, int64 data)
235 {
236 StringID strid;
237 switch (action) {
238 case NETWORK_ACTION_SERVER_MESSAGE:
239 /* Ignore invalid messages */
240 strid = STR_NETWORK_SERVER_MESSAGE;
241 colour = CC_DEFAULT;
242 break;
243 case NETWORK_ACTION_COMPANY_SPECTATOR:
244 colour = CC_DEFAULT;
245 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
246 break;
247 case NETWORK_ACTION_COMPANY_JOIN:
248 colour = CC_DEFAULT;
249 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
250 break;
251 case NETWORK_ACTION_COMPANY_NEW:
252 colour = CC_DEFAULT;
253 strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
254 break;
255 case NETWORK_ACTION_JOIN:
256 /* Show the Client ID for the server but not for the client. */
257 strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID : STR_NETWORK_MESSAGE_CLIENT_JOINED;
258 break;
259 case NETWORK_ACTION_LEAVE: strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
260 case NETWORK_ACTION_NAME_CHANGE: strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
261 case NETWORK_ACTION_GIVE_MONEY: strid = self_send ? STR_NETWORK_MESSAGE_GAVE_MONEY_AWAY : STR_NETWORK_MESSAGE_GIVE_MONEY; break;
262 case NETWORK_ACTION_CHAT_COMPANY: strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
263 case NETWORK_ACTION_CHAT_CLIENT: strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT; break;
264 default: strid = STR_NETWORK_CHAT_ALL; break;
265 }
266
267 char message[1024];
268 SetDParamStr(0, name);
269 SetDParamStr(1, str);
270 SetDParam(2, data);
271
272 /* All of these strings start with "***". These characters are interpreted as both left-to-right and
273 * right-to-left characters depending on the context. As the next text might be an user's name, the
274 * user name's characters will influence the direction of the "***" instead of the language setting
275 * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
276 char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
277 GetString(msg_ptr, strid, lastof(message));
278
279 DEBUG(desync, 1, "msg: %08x; %02x; %s", _date, _date_fract, message);
280 IConsolePrintF(colour, "%s", message);
281 NetworkAddChatMessage((TextColour)colour, _settings_client.gui.network_chat_timeout, "%s", message);
282 }
283
284 /* Calculate the frame-lag of a client */
285 uint NetworkCalculateLag(const NetworkClientSocket *cs)
286 {
287 int lag = cs->last_frame_server - cs->last_frame;
288 /* This client has missed his ACK packet after 1 DAY_TICKS..
289 * so we increase his lag for every frame that passes!
290 * The packet can be out by a max of _net_frame_freq */
291 if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
292 lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
293 }
294 return lag;
295 }
296
297
298 /* There was a non-recoverable error, drop back to the main menu with a nice
299 * error */
300 void NetworkError(StringID error_string)
301 {
302 _switch_mode = SM_MENU;
303 ShowErrorMessage(error_string, INVALID_STRING_ID, WL_CRITICAL);
304 }
305
311 StringID GetNetworkErrorMsg(NetworkErrorCode err)
312 {
313 /* List of possible network errors, used by
314 * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
315 static const StringID network_error_strings[] = {
316 STR_NETWORK_ERROR_CLIENT_GENERAL,
317 STR_NETWORK_ERROR_CLIENT_DESYNC,
318 STR_NETWORK_ERROR_CLIENT_SAVEGAME,
319 STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST,
320 STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR,
321 STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH,
322 STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED,
323 STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED,
324 STR_NETWORK_ERROR_CLIENT_WRONG_REVISION,
325 STR_NETWORK_ERROR_CLIENT_NAME_IN_USE,
326 STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD,
327 STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH,
328 STR_NETWORK_ERROR_CLIENT_KICKED,
329 STR_NETWORK_ERROR_CLIENT_CHEATER,
330 STR_NETWORK_ERROR_CLIENT_SERVER_FULL,
331 STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS,
332 STR_NETWORK_ERROR_CLIENT_TIMEOUT_PASSWORD,
333 STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER,
334 STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP,
335 STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN,
336 };
337 assert_compile(lengthof(network_error_strings) == NETWORK_ERROR_END);
338
339 if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
340
341 return network_error_strings[err];
342 }
343
349 void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
350 {
351 if (!_networking) return;
352
353 switch (changed_mode) {
354 case PM_PAUSED_NORMAL:
355 case PM_PAUSED_JOIN:
356 case PM_PAUSED_GAME_SCRIPT:
357 case PM_PAUSED_ACTIVE_CLIENTS: {
358 bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
359 bool paused = (_pause_mode != PM_UNPAUSED);
360 if (!paused && !changed) return;
361
362 StringID str;
363 if (!changed) {
364 int i = -1;
365
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 str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
371 } else {
372 switch (changed_mode) {
373 case PM_PAUSED_NORMAL: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
374 case PM_PAUSED_JOIN: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
375 case PM_PAUSED_GAME_SCRIPT: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT); break;
376 case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
377 default: NOT_REACHED();
378 }
379 str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
380 }
381
382 char buffer[DRAW_STRING_BUFFER];
383 GetString(buffer, str, lastof(buffer));
384 NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, NULL, buffer);
385 break;
386 }
387
388 default:
389 return;
390 }
391 }
392
393
402 static void CheckPauseHelper(bool pause, PauseMode pm)
403 {
404 if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
405
406 DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
407 }
408
414 static uint NetworkCountActiveClients()
415 {
416 const NetworkClientSocket *cs;
417 uint count = 0;
418
419 FOR_ALL_CLIENT_SOCKETS(cs) {
420 if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
421 if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
422 count++;
423 }
424
425 return count;
426 }
427
431 static void CheckMinActiveClients()
432 {
433 if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
434 !_network_dedicated ||
435 (_settings_client.network.min_active_clients == 0 && (_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) == PM_UNPAUSED)) {
436 return;
437 }
438 CheckPauseHelper(NetworkCountActiveClients() < _settings_client.network.min_active_clients, PM_PAUSED_ACTIVE_CLIENTS);
439 }
440
445 static bool NetworkHasJoiningClient()
446 {
447 const NetworkClientSocket *cs;
448 FOR_ALL_CLIENT_SOCKETS(cs) {
449 if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
450 }
451
452 return false;
453 }
454
458 static void CheckPauseOnJoin()
459 {
460 if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
461 (!_settings_client.network.pause_on_join && (_pause_mode & PM_PAUSED_JOIN) == PM_UNPAUSED)) {
462 return;
463 }
464 CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
465 }
466
475 void ParseConnectionString(const char **company, const char **port, char *connection_string)
476 {
477 bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
478 char *p;
479 for (p = connection_string; *p != '\0'; p++) {
480 switch (*p) {
481 case '[':
482 ipv6 = true;
483 break;
484
485 case ']':
486 ipv6 = false;
487 break;
488
489 case '#':
490 *company = p + 1;
491 *p = '\0';
492 break;
493
494 case ':':
495 if (ipv6) break;
496 *port = p + 1;
497 *p = '\0';
498 break;
499 }
500 }
501 }
502
508 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
509 {
510 /* Register the login */
511 _network_clients_connected++;
512
513 SetWindowDirty(WC_CLIENT_LIST, 0);
514 ServerNetworkGameSocketHandler *cs = new ServerNetworkGameSocketHandler(s);
515 cs->client_address = address; // Save the IP of the client
516 }
517
522 static void InitializeNetworkPools(bool close_admins = true)
523 {
524 PoolBase::Clean(PT_NCLIENT | (close_admins ? PT_NADMIN : PT_NONE));
525 }
526
531 void NetworkClose(bool close_admins)
532 {
533 if (_network_server) {
534 if (close_admins) {
535 ServerNetworkAdminSocketHandler *as;
536 FOR_ALL_ADMIN_SOCKETS(as) {
537 as->CloseConnection(true);
538 }
539 }
540
541 NetworkClientSocket *cs;
542 FOR_ALL_CLIENT_SOCKETS(cs) {
543 cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
544 }
545 ServerNetworkGameSocketHandler::CloseListeners();
546 ServerNetworkAdminSocketHandler::CloseListeners();
547 } else if (MyClient::my_client != NULL) {
548 MyClient::SendQuit();
549 MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
550 }
551
552 TCPConnecter::KillAll();
553
554 _networking = false;
555 _network_server = false;
556
557 NetworkFreeLocalCommandQueue();
558
559 free(_network_company_states);
560 _network_company_states = NULL;
561
562 InitializeNetworkPools(close_admins);
563 }
564
565 /* Initializes the network (cleans sockets and stuff) */
566 static void NetworkInitialize(bool close_admins = true)
567 {
568 InitializeNetworkPools(close_admins);
569 NetworkUDPInitialize();
570
571 _sync_frame = 0;
572 _network_first_time = true;
573
574 _network_reconnect = 0;
575 }
576
578 class TCPQueryConnecter : TCPConnecter {
579 public:
580 TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
581
582 virtual void OnFailure()
583 {
584 NetworkDisconnect();
585 }
586
587 virtual void OnConnect(SOCKET s)
588 {
589 _networking = true;
590 new ClientNetworkGameSocketHandler(s);
591 MyClient::SendCompanyInformationQuery();
592 }
593 };
594
595 /* Query a server to fetch his game-info
596 * If game_info is true, only the gameinfo is fetched,
597 * else only the client_info is fetched */
598 void NetworkTCPQueryServer(NetworkAddress address)
599 {
600 if (!_network_available) return;
601
602 NetworkDisconnect();
603 NetworkInitialize();
604
605 new TCPQueryConnecter(address);
606 }
607
608 /* Validates an address entered as a string and adds the server to
609 * the list. If you use this function, the games will be marked
610 * as manually added. */
611 void NetworkAddServer(const char *b)
612 {
613 if (*b != '\0') {
614 const char *port = NULL;
615 const char *company = NULL;
616 char host[NETWORK_HOSTNAME_LENGTH];
617 uint16 rport;
618
619 strecpy(host, b, lastof(host));
620
621 strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
622 rport = NETWORK_DEFAULT_PORT;
623
624 ParseConnectionString(&company, &port, host);
625 if (port != NULL) rport = atoi(port);
626
627 NetworkUDPQueryServer(NetworkAddress(host, rport), true);
628 }
629 }
630
636 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
637 {
638 for (char **iter = _network_bind_list.Begin(); iter != _network_bind_list.End(); iter++) {
639 *addresses->Append() = NetworkAddress(*iter, port);
640 }
641
642 /* No address, so bind to everything. */
643 if (addresses->Length() == 0) {
644 *addresses->Append() = NetworkAddress("", port);
645 }
646 }
647
648 /* Generates the list of manually added hosts from NetworkGameList and
649 * dumps them into the array _network_host_list. This array is needed
650 * by the function that generates the config file. */
651 void NetworkRebuildHostList()
652 {
653 _network_host_list.Clear();
654
655 for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
656 if (item->manually) *_network_host_list.Append() = stredup(item->address.GetAddressAsString(false));
657 }
658 }
659
661 class TCPClientConnecter : TCPConnecter {
662 public:
663 TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
664
665 virtual void OnFailure()
666 {
667 NetworkError(STR_NETWORK_ERROR_NOCONNECTION);
668 }
669
670 virtual void OnConnect(SOCKET s)
671 {
672 _networking = true;
673 new ClientNetworkGameSocketHandler(s);
674 IConsoleCmdExec("exec scripts/on_client.scr 0");
675 NetworkClient_Connected();
676 }
677 };
678
679
680 /* Used by clients, to connect to a server */
681 void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
682 {
683 if (!_network_available) return;
684
685 if (address.GetPort() == 0) return;
686
687 strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
688 _settings_client.network.last_port = address.GetPort();
689 _network_join_as = join_as;
690 _network_join_server_password = join_server_password;
691 _network_join_company_password = join_company_password;
692
693 NetworkDisconnect();
694 NetworkInitialize();
695
696 _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
697 ShowJoinStatusWindow();
698
699 new TCPClientConnecter(address);
700 }
701
702 static void NetworkInitGameInfo()
703 {
704 if (StrEmpty(_settings_client.network.server_name)) {
705 seprintf(_settings_client.network.server_name, lastof(_settings_client.network.server_name), "Unnamed Server");
706 }
707
708 /* The server is a client too */
709 _network_game_info.clients_on = _network_dedicated ? 0 : 1;
710
711 /* There should be always space for the server. */
712 assert(NetworkClientInfo::CanAllocateItem());
713 NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
714 ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
715
716 strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
717 }
718
719 bool NetworkServerStart()
720 {
721 if (!_network_available) return false;
722
723 /* Call the pre-scripts */
724 IConsoleCmdExec("exec scripts/pre_server.scr 0");
725 if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
726
727 NetworkDisconnect(false, false);
728 NetworkInitialize(false);
729 DEBUG(net, 1, "starting listeners for clients");
730 if (!ServerNetworkGameSocketHandler::Listen(_settings_client.network.server_port)) return false;
731
732 /* Only listen for admins when the password isn't empty. */
733 if (!StrEmpty(_settings_client.network.admin_password)) {
734 DEBUG(net, 1, "starting listeners for admins");
735 if (!ServerNetworkAdminSocketHandler::Listen(_settings_client.network.server_admin_port)) return false;
736 }
737
738 /* Try to start UDP-server */
739 DEBUG(net, 1, "starting listeners for incoming server queries");
740 _network_udp_server = _udp_server_socket->Listen();
741
742 _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
743 _network_server = true;
744 _networking = true;
745 _frame_counter = 0;
746 _frame_counter_server = 0;
747 _frame_counter_max = 0;
748 _last_sync_frame = 0;
749 _network_own_client_id = CLIENT_ID_SERVER;
750
751 _network_clients_connected = 0;
752 _network_company_passworded = 0;
753
754 NetworkInitGameInfo();
755
756 /* execute server initialization script */
757 IConsoleCmdExec("exec scripts/on_server.scr 0");
758 /* if the server is dedicated ... add some other script */
759 if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
760
761 /* Try to register us to the master server */
762 _network_last_advertise_frame = 0;
763 _network_need_advertise = true;
764 NetworkUDPAdvertise();
765
766 /* welcome possibly still connected admins - this can only happen on a dedicated server. */
767 if (_network_dedicated) ServerNetworkAdminSocketHandler::WelcomeAll();
768
769 return true;
770 }
771
772 /* The server is rebooting...
773 * The only difference with NetworkDisconnect, is the packets that is sent */
774 void NetworkReboot()
775 {
776 if (_network_server) {
777 NetworkClientSocket *cs;
778 FOR_ALL_CLIENT_SOCKETS(cs) {
779 cs->SendNewGame();
780 cs->SendPackets();
781 }
782
783 ServerNetworkAdminSocketHandler *as;
784 FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
785 as->SendNewGame();
786 as->SendPackets();
787 }
788 }
789
790 /* For non-dedicated servers we have to kick the admins as we are not
791 * certain that we will end up in a new network game. */
792 NetworkClose(!_network_dedicated);
793 }
794
800 void NetworkDisconnect(bool blocking, bool close_admins)
801 {
802 if (_network_server) {
803 NetworkClientSocket *cs;
804 FOR_ALL_CLIENT_SOCKETS(cs) {
805 cs->SendShutdown();
806 cs->SendPackets();
807 }
808
809 if (close_admins) {
810 ServerNetworkAdminSocketHandler *as;
811 FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) {
812 as->SendShutdown();
813 as->SendPackets();
814 }
815 }
816 }
817
818 if (_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(blocking);
819
820 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
821
822 NetworkClose(close_admins);
823
824 /* Reinitialize the UDP stack, i.e. close all existing connections. */
825 NetworkUDPInitialize();
826 }
827
832 static bool NetworkReceive()
833 {
834 if (_network_server) {
835 ServerNetworkAdminSocketHandler::Receive();
836 return ServerNetworkGameSocketHandler::Receive();
837 } else {
838 return ClientNetworkGameSocketHandler::Receive();
839 }
840 }
841
842 /* This sends all buffered commands (if possible) */
843 static void NetworkSend()
844 {
845 if (_network_server) {
846 ServerNetworkAdminSocketHandler::Send();
847 ServerNetworkGameSocketHandler::Send();
848 } else {
849 ClientNetworkGameSocketHandler::Send();
850 }
851 }
852
858 void NetworkBackgroundLoop()
859 {
860 _network_content_client.SendReceive();
861 TCPConnecter::CheckCallbacks();
862 NetworkHTTPSocketHandler::HTTPReceive();
863
864 NetworkBackgroundUDPLoop();
865 }
866
867 /* The main loop called from ttd.c
868 * Here we also have to do StateGameLoop if needed! */
869 void NetworkGameLoop()
870 {
871 if (!_networking) return;
872
873 if (!NetworkReceive()) return;
874
875 if (_network_server) {
876 /* Log the sync state to check for in-syncedness of replays. */
877 if (_date_fract == 0) {
878 /* We don't want to log multiple times if paused. */
879 static Date last_log;
880 if (last_log != _date) {
881 DEBUG(desync, 1, "sync: %08x; %02x; %08x; %08x", _date, _date_fract, _random.state[0], _random.state[1]);
882 last_log = _date;
883 }
884 }
885
886 #ifdef DEBUG_DUMP_COMMANDS
887 /* Loading of the debug commands from -ddesync>=1 */
888 static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
889 static Date next_date = 0;
890 static uint32 next_date_fract;
891 static CommandPacket *cp = NULL;
892 static bool check_sync_state = false;
893 static uint32 sync_state[2];
894 if (f == NULL && next_date == 0) {
895 DEBUG(net, 0, "Cannot open commands.log");
896 next_date = 1;
897 }
898
899 while (f != NULL && !feof(f)) {
900 if (_date == next_date && _date_fract == next_date_fract) {
901 if (cp != NULL) {
902 NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, NULL, cp->text, cp->company);
903 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));
904 free(cp);
905 cp = NULL;
906 }
907 if (check_sync_state) {
908 if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
909 DEBUG(net, 0, "sync check: %08x; %02x; match", _date, _date_fract);
910 } else {
911 DEBUG(net, 0, "sync check: %08x; %02x; mismatch expected {%08x, %08x}, got {%08x, %08x}",
912 _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
913 NOT_REACHED();
914 }
915 check_sync_state = false;
916 }
917 }
918
919 if (cp != NULL || check_sync_state) break;
920
921 char buff[4096];
922 if (fgets(buff, lengthof(buff), f) == NULL) break;
923
924 char *p = buff;
925 /* Ignore the "[date time] " part of the message */
926 if (*p == '[') {
927 p = strchr(p, ']');
928 if (p == NULL) break;
929 p += 2;
930 }
931
932 if (strncmp(p, "cmd: ", 5) == 0
933 #ifdef DEBUG_FAILED_DUMP_COMMANDS
934 || strncmp(p, "cmdf: ", 6) == 0
935 #endif
936 ) {
937 p += 5;
938 if (*p == ' ') p++;
939 cp = CallocT<CommandPacket>(1);
940 int company;
941 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);
942 /* There are 8 pieces of data to read, however the last is a
943 * string that might or might not exist. Ignore it if that
944 * string misses because in 99% of the time it's not used. */
945 assert(ret == 8 || ret == 7);
946 cp->company = (CompanyID)company;
947 } else if (strncmp(p, "join: ", 6) == 0) {
948 /* Manually insert a pause when joining; this way the client can join at the exact right time. */
949 int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
950 assert(ret == 2);
951 DEBUG(net, 0, "injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
952 cp = CallocT<CommandPacket>(1);
953 cp->company = COMPANY_SPECTATOR;
954 cp->cmd = CMD_PAUSE;
955 cp->p1 = PM_PAUSED_NORMAL;
956 cp->p2 = 1;
957 _ddc_fastforward = false;
958 } else if (strncmp(p, "sync: ", 6) == 0) {
959 int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
960 assert(ret == 4);
961 check_sync_state = true;
962 } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
963 strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
964 /* A message that is not very important to the log playback, but part of the log. */
965 #ifndef DEBUG_FAILED_DUMP_COMMANDS
966 } else if (strncmp(p, "cmdf: ", 6) == 0) {
967 DEBUG(net, 0, "Skipping replay of failed command: %s", p + 6);
968 #endif
969 } else {
970 /* Can't parse a line; what's wrong here? */
971 DEBUG(net, 0, "trying to parse: %s", p);
972 NOT_REACHED();
973 }
974 }
975 if (f != NULL && feof(f)) {
976 DEBUG(net, 0, "End of commands.log");
977 fclose(f);
978 f = NULL;
979 }
980 #endif /* DEBUG_DUMP_COMMANDS */
981 if (_frame_counter >= _frame_counter_max) {
982 /* Only check for active clients just before we're going to send out
983 * the commands so we don't send multiple pause/unpause commands when
984 * the frame_freq is more than 1 tick. Same with distributing commands. */
985 CheckPauseOnJoin();
986 CheckMinActiveClients();
987 NetworkDistributeCommands();
988 }
989
990 bool send_frame = false;
991
992 /* We first increase the _frame_counter */
993 _frame_counter++;
994 /* Update max-frame-counter */
995 if (_frame_counter > _frame_counter_max) {
996 _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
997 send_frame = true;
998 }
999
1000 NetworkExecuteLocalCommandQueue();
1001
1002 /* Then we make the frame */
1003 StateGameLoop();
1004
1005 _sync_seed_1 = _random.state[0];
1006 #ifdef NETWORK_SEND_DOUBLE_SEED
1007 _sync_seed_2 = _random.state[1];
1008 #endif
1009
1010 NetworkServer_Tick(send_frame);
1011 } else {
1012 /* Client */
1013
1014 /* Make sure we are at the frame were the server is (quick-frames) */
1015 if (_frame_counter_server > _frame_counter) {
1016 /* Run a number of frames; when things go bad, get out. */
1017 while (_frame_counter_server > _frame_counter) {
1018 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1019 }
1020 } else {
1021 /* Else, keep on going till _frame_counter_max */
1022 if (_frame_counter_max > _frame_counter) {
1023 /* Run one frame; if things went bad, get out. */
1024 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1025 }
1026 }
1027 }
1028
1029 NetworkSend();
1030 }
1031
1032 static void NetworkGenerateServerId()
1033 {
1034 Md5 checksum;
1035 uint8 digest[16];
1036 char hex_output[16 * 2 + 1];
1037 char coding_string[NETWORK_NAME_LENGTH];
1038 int di;
1039
1040 seprintf(coding_string, lastof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
1041
1042 /* Generate the MD5 hash */
1043 checksum.Append((const uint8*)coding_string, strlen(coding_string));
1044 checksum.Finish(digest);
1045
1046 for (di = 0; di < 16; ++di) {
1047 seprintf(hex_output + di * 2, lastof(hex_output), "%02x", digest[di]);
1048 }
1049
1050 /* _settings_client.network.network_id is our id */
1051 seprintf(_settings_client.network.network_id, lastof(_settings_client.network.network_id), "%s", hex_output);
1052 }
1053
1054 void NetworkStartDebugLog(NetworkAddress address)
1055 {
1056 extern SOCKET _debug_socket; // Comes from debug.c
1057
1058 DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
1059
1060 SOCKET s = address.Connect();
1061 if (s == INVALID_SOCKET) {
1062 DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
1063 return;
1064 }
1065
1066 _debug_socket = s;
1067
1068 DEBUG(net, 0, "DEBUG() is now redirected");
1069 }
1070
1072 void NetworkStartUp()
1073 {
1074 DEBUG(net, 3, "[core] starting network...");
1075
1076 /* Network is available */
1077 _network_available = NetworkCoreInitialize();
1078 _network_dedicated = false;
1079 _network_last_advertise_frame = 0;
1080 _network_need_advertise = true;
1081 _network_advertise_retries = 0;
1082
1083 /* Generate an server id when there is none yet */
1084 if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
1085
1086 memset(&_network_game_info, 0, sizeof(_network_game_info));
1087
1088 NetworkInitialize();
1089 DEBUG(net, 3, "[core] network online, multiplayer available");
1090 NetworkFindBroadcastIPs(&_broadcast_list);
1091 }
1092
1094 void NetworkShutDown()
1095 {
1096 NetworkDisconnect(true);
1097 NetworkUDPClose();
1098
1099 DEBUG(net, 3, "[core] shutting down network");
1100
1101 _network_available = false;
1102
1103 NetworkCoreShutdown();
1104 }
1105
1110 bool IsNetworkCompatibleVersion(const char *other)
1111 {
1112 return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
1113 }
1114
1115 #endif /* ENABLE_NETWORK */