network.cpp

Go to the documentation of this file.
00001 /* $Id: network.cpp 22701 2011-07-30 17:45:37Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 
00014 #ifdef ENABLE_NETWORK
00015 
00016 #include "../strings_func.h"
00017 #include "../command_func.h"
00018 #include "../date_func.h"
00019 #include "network_admin.h"
00020 #include "network_client.h"
00021 #include "network_server.h"
00022 #include "network_content.h"
00023 #include "network_udp.h"
00024 #include "network_gamelist.h"
00025 #include "network_base.h"
00026 #include "core/udp.h"
00027 #include "core/host.h"
00028 #include "network_gui.h"
00029 #include "../console_func.h"
00030 #include "../3rdparty/md5/md5.h"
00031 #include "../core/random_func.hpp"
00032 #include "../window_func.h"
00033 #include "../company_func.h"
00034 #include "../company_base.h"
00035 #include "../landscape_type.h"
00036 #include "../rev.h"
00037 #include "../core/pool_func.hpp"
00038 #include "../gfx_func.h"
00039 #include "table/strings.h"
00040 
00041 #ifdef DEBUG_DUMP_COMMANDS
00042 #include "../fileio_func.h"
00044 bool _ddc_fastforward = true;
00045 #endif /* DEBUG_DUMP_COMMANDS */
00046 
00047 assert_compile(NetworkClientInfoPool::MAX_SIZE == NetworkClientSocketPool::MAX_SIZE);
00048 
00049 NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo");
00050 INSTANTIATE_POOL_METHODS(NetworkClientInfo)
00051 
00052 bool _networking;         
00053 bool _network_server;     
00054 bool _network_available;  
00055 bool _network_dedicated;  
00056 bool _is_network_server;  
00057 NetworkServerGameInfo _network_game_info;
00058 NetworkCompanyState *_network_company_states = NULL;
00059 ClientID _network_own_client_id;
00060 ClientID _redirect_console_to_client;
00061 bool _network_need_advertise;
00062 uint32 _network_last_advertise_frame;
00063 uint8 _network_reconnect;
00064 StringList _network_bind_list;
00065 StringList _network_host_list;
00066 StringList _network_ban_list;
00067 uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
00068 uint32 _frame_counter_max; // To where we may go with our clients
00069 uint32 _frame_counter;
00070 uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
00071 NetworkAddressList _broadcast_list;
00072 uint32 _sync_seed_1;
00073 #ifdef NETWORK_SEND_DOUBLE_SEED
00074 uint32 _sync_seed_2;
00075 #endif
00076 uint32 _sync_frame;
00077 bool _network_first_time;
00078 bool _network_udp_server;
00079 uint16 _network_udp_broadcast;
00080 uint8 _network_advertise_retries;
00081 CompanyMask _network_company_passworded; 
00082 
00083 /* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
00084 assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
00085 assert_compile((int)NETWORK_COMPANY_NAME_LENGTH == MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH);
00086 
00087 extern NetworkUDPSocketHandler *_udp_client_socket; 
00088 extern NetworkUDPSocketHandler *_udp_server_socket; 
00089 extern NetworkUDPSocketHandler *_udp_master_socket; 
00090 
00091 /* The amount of clients connected */
00092 byte _network_clients_connected = 0;
00093 
00094 /* Some externs / forwards */
00095 extern void StateGameLoop();
00096 
00100 NetworkClientInfo::~NetworkClientInfo()
00101 {
00102   /* Delete the chat window, if you were chatting with this client. */
00103   InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_CLIENT, this->client_id);
00104 }
00105 
00111 /* static */ NetworkClientInfo *NetworkClientInfo::GetByClientID(ClientID client_id)
00112 {
00113   NetworkClientInfo *ci;
00114 
00115   FOR_ALL_CLIENT_INFOS(ci) {
00116     if (ci->client_id == client_id) return ci;
00117   }
00118 
00119   return NULL;
00120 }
00121 
00127 /* static */ NetworkClientSocket *NetworkClientSocket::GetByClientID(ClientID client_id)
00128 {
00129   NetworkClientSocket *cs;
00130 
00131   FOR_ALL_CLIENT_SOCKETS(cs) {
00132     if (cs->client_id == client_id) return cs;
00133   }
00134 
00135   return NULL;
00136 }
00137 
00138 byte NetworkSpectatorCount()
00139 {
00140   const NetworkClientInfo *ci;
00141   byte count = 0;
00142 
00143   FOR_ALL_CLIENT_INFOS(ci) {
00144     if (ci->client_playas == COMPANY_SPECTATOR) count++;
00145   }
00146 
00147   /* Don't count a dedicated server as spectator */
00148   if (_network_dedicated) count--;
00149 
00150   return count;
00151 }
00152 
00159 const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
00160 {
00161   if (strcmp(password, "*") == 0) password = "";
00162 
00163   if (_network_server) {
00164     NetworkServerSetCompanyPassword(company_id, password, already_hashed);
00165   } else {
00166     NetworkClientSetCompanyPassword(password);
00167   }
00168 
00169   return password;
00170 }
00171 
00179 const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
00180 {
00181   if (StrEmpty(password)) return password;
00182 
00183   char salted_password[NETWORK_SERVER_ID_LENGTH];
00184 
00185   memset(salted_password, 0, sizeof(salted_password));
00186   snprintf(salted_password, sizeof(salted_password), "%s", password);
00187   /* Add the game seed and the server's ID as the salt. */
00188   for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
00189     salted_password[i] ^= password_server_id[i] ^ (password_game_seed >> (i % 32));
00190   }
00191 
00192   Md5 checksum;
00193   uint8 digest[16];
00194   static char hashed_password[NETWORK_SERVER_ID_LENGTH];
00195 
00196   /* Generate the MD5 hash */
00197   checksum.Append(salted_password, sizeof(salted_password) - 1);
00198   checksum.Finish(digest);
00199 
00200   for (int di = 0; di < 16; di++) sprintf(hashed_password + di * 2, "%02x", digest[di]);
00201   hashed_password[lengthof(hashed_password) - 1] = '\0';
00202 
00203   return hashed_password;
00204 }
00205 
00211 bool NetworkCompanyIsPassworded(CompanyID company_id)
00212 {
00213   return HasBit(_network_company_passworded, company_id);
00214 }
00215 
00216 /* This puts a text-message to the console, or in the future, the chat-box,
00217  *  (to keep it all a bit more general)
00218  * If 'self_send' is true, this is the client who is sending the message */
00219 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const char *name, const char *str, int64 data)
00220 {
00221   StringID strid;
00222   switch (action) {
00223     case NETWORK_ACTION_SERVER_MESSAGE:
00224       /* Ignore invalid messages */
00225       strid = STR_NETWORK_SERVER_MESSAGE;
00226       colour = CC_DEFAULT;
00227       break;
00228     case NETWORK_ACTION_COMPANY_SPECTATOR:
00229       colour = CC_DEFAULT;
00230       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
00231       break;
00232     case NETWORK_ACTION_COMPANY_JOIN:
00233       colour = CC_DEFAULT;
00234       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
00235       break;
00236     case NETWORK_ACTION_COMPANY_NEW:
00237       colour = CC_DEFAULT;
00238       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
00239       break;
00240     case NETWORK_ACTION_JOIN:
00241       /* Show the Client ID for the server but not for the client. */
00242       strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID :  STR_NETWORK_MESSAGE_CLIENT_JOINED;
00243       break;
00244     case NETWORK_ACTION_LEAVE:          strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
00245     case NETWORK_ACTION_NAME_CHANGE:    strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
00246     case NETWORK_ACTION_GIVE_MONEY:     strid = self_send ? STR_NETWORK_MESSAGE_GAVE_MONEY_AWAY : STR_NETWORK_MESSAGE_GIVE_MONEY;   break;
00247     case NETWORK_ACTION_CHAT_COMPANY:   strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
00248     case NETWORK_ACTION_CHAT_CLIENT:    strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT  : STR_NETWORK_CHAT_CLIENT;  break;
00249     default:                            strid = STR_NETWORK_CHAT_ALL; break;
00250   }
00251 
00252   char message[1024];
00253   SetDParamStr(0, name);
00254   SetDParamStr(1, str);
00255   SetDParam(2, data);
00256 
00257   /* All of these strings start with "***". These characters are interpreted as both left-to-right and
00258    * right-to-left characters depending on the context. As the next text might be an user's name, the
00259    * user name's characters will influence the direction of the "***" instead of the language setting
00260    * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
00261   char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
00262   GetString(msg_ptr, strid, lastof(message));
00263 
00264   DEBUG(desync, 1, "msg: %08x; %02x; %s", _date, _date_fract, message);
00265   IConsolePrintF(colour, "%s", message);
00266   NetworkAddChatMessage((TextColour)colour, _settings_client.gui.network_chat_timeout, "%s", message);
00267 }
00268 
00269 /* Calculate the frame-lag of a client */
00270 uint NetworkCalculateLag(const NetworkClientSocket *cs)
00271 {
00272   int lag = cs->last_frame_server - cs->last_frame;
00273   /* This client has missed his ACK packet after 1 DAY_TICKS..
00274    *  so we increase his lag for every frame that passes!
00275    * The packet can be out by a max of _net_frame_freq */
00276   if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
00277     lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
00278   }
00279   return lag;
00280 }
00281 
00282 
00283 /* There was a non-recoverable error, drop back to the main menu with a nice
00284  *  error */
00285 void NetworkError(StringID error_string)
00286 {
00287   _switch_mode = SM_MENU;
00288   extern StringID _switch_mode_errorstr;
00289   _switch_mode_errorstr = error_string;
00290 }
00291 
00297 StringID GetNetworkErrorMsg(NetworkErrorCode err)
00298 {
00299   /* List of possible network errors, used by
00300    * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
00301   static const StringID network_error_strings[] = {
00302     STR_NETWORK_ERROR_CLIENT_GENERAL,
00303     STR_NETWORK_ERROR_CLIENT_DESYNC,
00304     STR_NETWORK_ERROR_CLIENT_SAVEGAME,
00305     STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST,
00306     STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR,
00307     STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH,
00308     STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED,
00309     STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED,
00310     STR_NETWORK_ERROR_CLIENT_WRONG_REVISION,
00311     STR_NETWORK_ERROR_CLIENT_NAME_IN_USE,
00312     STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD,
00313     STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH,
00314     STR_NETWORK_ERROR_CLIENT_KICKED,
00315     STR_NETWORK_ERROR_CLIENT_CHEATER,
00316     STR_NETWORK_ERROR_CLIENT_SERVER_FULL,
00317     STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS
00318   };
00319 
00320   if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
00321 
00322   return network_error_strings[err];
00323 }
00324 
00330 void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
00331 {
00332   if (!_networking) return;
00333 
00334   switch (changed_mode) {
00335     case PM_PAUSED_NORMAL:
00336     case PM_PAUSED_JOIN:
00337     case PM_PAUSED_ACTIVE_CLIENTS: {
00338       bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
00339       bool paused = (_pause_mode != PM_UNPAUSED);
00340       if (!paused && !changed) return;
00341 
00342       StringID str;
00343       if (!changed) {
00344         int i = -1;
00345         if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED)         SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL);
00346         if ((_pause_mode & PM_PAUSED_JOIN) != PM_UNPAUSED)           SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS);
00347         if ((_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS);
00348         str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
00349       } else {
00350         switch (changed_mode) {
00351           case PM_PAUSED_NORMAL:         SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
00352           case PM_PAUSED_JOIN:           SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
00353           case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
00354           default: NOT_REACHED();
00355         }
00356         str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
00357       }
00358 
00359       char buffer[DRAW_STRING_BUFFER];
00360       GetString(buffer, str, lastof(buffer));
00361       NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, NULL, buffer);
00362       break;
00363     }
00364 
00365     default:
00366       return;
00367   }
00368 }
00369 
00370 
00379 static void CheckPauseHelper(bool pause, PauseMode pm)
00380 {
00381   if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
00382 
00383   DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
00384 }
00385 
00391 static uint NetworkCountActiveClients()
00392 {
00393   const NetworkClientSocket *cs;
00394   uint count = 0;
00395 
00396   FOR_ALL_CLIENT_SOCKETS(cs) {
00397     if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
00398     if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
00399     count++;
00400   }
00401 
00402   return count;
00403 }
00404 
00408 static void CheckMinActiveClients()
00409 {
00410   if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
00411       !_network_dedicated ||
00412       (_settings_client.network.min_active_clients == 0 && (_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) == PM_UNPAUSED)) {
00413     return;
00414   }
00415   CheckPauseHelper(NetworkCountActiveClients() < _settings_client.network.min_active_clients, PM_PAUSED_ACTIVE_CLIENTS);
00416 }
00417 
00422 static bool NetworkHasJoiningClient()
00423 {
00424   const NetworkClientSocket *cs;
00425   FOR_ALL_CLIENT_SOCKETS(cs) {
00426     if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
00427   }
00428 
00429   return false;
00430 }
00431 
00435 static void CheckPauseOnJoin()
00436 {
00437   if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
00438       (!_settings_client.network.pause_on_join && (_pause_mode & PM_PAUSED_JOIN) == PM_UNPAUSED)) {
00439     return;
00440   }
00441   CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
00442 }
00443 
00452 void ParseConnectionString(const char **company, const char **port, char *connection_string)
00453 {
00454   bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
00455   char *p;
00456   for (p = connection_string; *p != '\0'; p++) {
00457     switch (*p) {
00458       case '[':
00459         ipv6 = true;
00460         break;
00461 
00462       case ']':
00463         ipv6 = false;
00464         break;
00465 
00466       case '#':
00467         *company = p + 1;
00468         *p = '\0';
00469         break;
00470 
00471       case ':':
00472         if (ipv6) break;
00473         *port = p + 1;
00474         *p = '\0';
00475         break;
00476     }
00477   }
00478 }
00479 
00480 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
00481 {
00482   /* Register the login */
00483   _network_clients_connected++;
00484 
00485   SetWindowDirty(WC_CLIENT_LIST, 0);
00486   ServerNetworkGameSocketHandler *cs = new ServerNetworkGameSocketHandler(s);
00487   cs->client_address = address; // Save the IP of the client
00488 }
00489 
00494 static void InitializeNetworkPools(bool close_admins = true)
00495 {
00496   _networkclientsocket_pool.CleanPool();
00497   _networkclientinfo_pool.CleanPool();
00498   if (close_admins) _networkadminsocket_pool.CleanPool();
00499 }
00500 
00505 void NetworkClose(bool close_admins)
00506 {
00507   if (_network_server) {
00508     if (close_admins) {
00509       ServerNetworkAdminSocketHandler *as;
00510       FOR_ALL_ADMIN_SOCKETS(as) {
00511         as->CloseConnection(true);
00512       }
00513     }
00514 
00515     NetworkClientSocket *cs;
00516     FOR_ALL_CLIENT_SOCKETS(cs) {
00517       cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
00518     }
00519     ServerNetworkGameSocketHandler::CloseListeners();
00520     ServerNetworkAdminSocketHandler::CloseListeners();
00521   } else if (MyClient::my_client != NULL) {
00522     MyClient::SendQuit();
00523     MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
00524   }
00525 
00526   TCPConnecter::KillAll();
00527 
00528   _networking = false;
00529   _network_server = false;
00530 
00531   NetworkFreeLocalCommandQueue();
00532 
00533   free(_network_company_states);
00534   _network_company_states = NULL;
00535 
00536   InitializeNetworkPools(close_admins);
00537 }
00538 
00539 /* Inits the network (cleans sockets and stuff) */
00540 static void NetworkInitialize(bool close_admins = true)
00541 {
00542   InitializeNetworkPools(close_admins);
00543   NetworkUDPInitialize();
00544 
00545   _sync_frame = 0;
00546   _network_first_time = true;
00547 
00548   _network_reconnect = 0;
00549 }
00550 
00552 class TCPQueryConnecter : TCPConnecter {
00553 public:
00554   TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00555 
00556   virtual void OnFailure()
00557   {
00558     NetworkDisconnect();
00559   }
00560 
00561   virtual void OnConnect(SOCKET s)
00562   {
00563     _networking = true;
00564     new ClientNetworkGameSocketHandler(s);
00565     MyClient::SendCompanyInformationQuery();
00566   }
00567 };
00568 
00569 /* Query a server to fetch his game-info
00570  *  If game_info is true, only the gameinfo is fetched,
00571  *   else only the client_info is fetched */
00572 void NetworkTCPQueryServer(NetworkAddress address)
00573 {
00574   if (!_network_available) return;
00575 
00576   NetworkDisconnect();
00577   NetworkInitialize();
00578 
00579   new TCPQueryConnecter(address);
00580 }
00581 
00582 /* Validates an address entered as a string and adds the server to
00583  * the list. If you use this function, the games will be marked
00584  * as manually added. */
00585 void NetworkAddServer(const char *b)
00586 {
00587   if (*b != '\0') {
00588     const char *port = NULL;
00589     const char *company = NULL;
00590     char host[NETWORK_HOSTNAME_LENGTH];
00591     uint16 rport;
00592 
00593     strecpy(host, b, lastof(host));
00594 
00595     strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
00596     rport = NETWORK_DEFAULT_PORT;
00597 
00598     ParseConnectionString(&company, &port, host);
00599     if (port != NULL) rport = atoi(port);
00600 
00601     NetworkUDPQueryServer(NetworkAddress(host, rport), true);
00602   }
00603 }
00604 
00610 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
00611 {
00612   for (char **iter = _network_bind_list.Begin(); iter != _network_bind_list.End(); iter++) {
00613     *addresses->Append() = NetworkAddress(*iter, port);
00614   }
00615 
00616   /* No address, so bind to everything. */
00617   if (addresses->Length() == 0) {
00618     *addresses->Append() = NetworkAddress("", port);
00619   }
00620 }
00621 
00622 /* Generates the list of manually added hosts from NetworkGameList and
00623  * dumps them into the array _network_host_list. This array is needed
00624  * by the function that generates the config file. */
00625 void NetworkRebuildHostList()
00626 {
00627   _network_host_list.Clear();
00628 
00629   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00630     if (item->manually) *_network_host_list.Append() = strdup(item->address.GetAddressAsString(false));
00631   }
00632 }
00633 
00635 class TCPClientConnecter : TCPConnecter {
00636 public:
00637   TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00638 
00639   virtual void OnFailure()
00640   {
00641     NetworkError(STR_NETWORK_ERROR_NOCONNECTION);
00642   }
00643 
00644   virtual void OnConnect(SOCKET s)
00645   {
00646     _networking = true;
00647     new ClientNetworkGameSocketHandler(s);
00648     IConsoleCmdExec("exec scripts/on_client.scr 0");
00649     NetworkClient_Connected();
00650   }
00651 };
00652 
00653 
00654 /* Used by clients, to connect to a server */
00655 void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
00656 {
00657   if (!_network_available) return;
00658 
00659   if (address.GetPort() == 0) return;
00660 
00661   strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
00662   _settings_client.network.last_port = address.GetPort();
00663   _network_join_as = join_as;
00664   _network_join_server_password = join_server_password;
00665   _network_join_company_password = join_company_password;
00666 
00667   NetworkDisconnect();
00668   NetworkInitialize();
00669 
00670   _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
00671   ShowJoinStatusWindow();
00672 
00673   new TCPClientConnecter(address);
00674 }
00675 
00676 static void NetworkInitGameInfo()
00677 {
00678   if (StrEmpty(_settings_client.network.server_name)) {
00679     snprintf(_settings_client.network.server_name, sizeof(_settings_client.network.server_name), "Unnamed Server");
00680   }
00681 
00682   /* The server is a client too */
00683   _network_game_info.clients_on = _network_dedicated ? 0 : 1;
00684 
00685   /* There should be always space for the server. */
00686   assert(NetworkClientInfo::CanAllocateItem());
00687   NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
00688   ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : _local_company;
00689 
00690   strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
00691 }
00692 
00693 bool NetworkServerStart()
00694 {
00695   if (!_network_available) return false;
00696 
00697   /* Call the pre-scripts */
00698   IConsoleCmdExec("exec scripts/pre_server.scr 0");
00699   if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
00700 
00701   NetworkDisconnect(false, false);
00702   NetworkInitialize(false);
00703   if (!ServerNetworkGameSocketHandler::Listen(_settings_client.network.server_port)) return false;
00704 
00705   /* Only listen for admins when the password isn't empty. */
00706   if (!StrEmpty(_settings_client.network.admin_password) && !ServerNetworkAdminSocketHandler::Listen(_settings_client.network.server_admin_port)) return false;
00707 
00708   /* Try to start UDP-server */
00709   _network_udp_server = _udp_server_socket->Listen();
00710 
00711   _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
00712   _network_server = true;
00713   _networking = true;
00714   _frame_counter = 0;
00715   _frame_counter_server = 0;
00716   _frame_counter_max = 0;
00717   _last_sync_frame = 0;
00718   _network_own_client_id = CLIENT_ID_SERVER;
00719 
00720   _network_clients_connected = 0;
00721   _network_company_passworded = 0;
00722 
00723   NetworkInitGameInfo();
00724 
00725   /* execute server initialization script */
00726   IConsoleCmdExec("exec scripts/on_server.scr 0");
00727   /* if the server is dedicated ... add some other script */
00728   if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
00729 
00730   /* Try to register us to the master server */
00731   _network_last_advertise_frame = 0;
00732   _network_need_advertise = true;
00733   NetworkUDPAdvertise();
00734 
00735   /* welcome possibly still connected admins - this can only happen on a dedicated server. */
00736   if (_network_dedicated) ServerNetworkAdminSocketHandler::WelcomeAll();
00737 
00738   return true;
00739 }
00740 
00741 /* The server is rebooting...
00742  * The only difference with NetworkDisconnect, is the packets that is sent */
00743 void NetworkReboot()
00744 {
00745   if (_network_server) {
00746     NetworkClientSocket *cs;
00747     FOR_ALL_CLIENT_SOCKETS(cs) {
00748       cs->SendNewGame();
00749       cs->SendPackets();
00750     }
00751 
00752     ServerNetworkAdminSocketHandler *as;
00753     FOR_ALL_ADMIN_SOCKETS(as) {
00754       as->SendNewGame();
00755       as->SendPackets();
00756     }
00757   }
00758 
00759   /* For non-dedicated servers we have to kick the admins as we are not
00760    * certain that we will end up in a new network game. */
00761   NetworkClose(!_network_dedicated);
00762 }
00763 
00769 void NetworkDisconnect(bool blocking, bool close_admins)
00770 {
00771   if (_network_server) {
00772     NetworkClientSocket *cs;
00773     FOR_ALL_CLIENT_SOCKETS(cs) {
00774       cs->SendShutdown();
00775       cs->SendPackets();
00776     }
00777 
00778     if (close_admins) {
00779       ServerNetworkAdminSocketHandler *as;
00780       FOR_ALL_ADMIN_SOCKETS(as) {
00781         as->SendShutdown();
00782         as->SendPackets();
00783       }
00784     }
00785   }
00786 
00787   if (_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(blocking);
00788 
00789   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00790 
00791   NetworkClose(close_admins);
00792 
00793   /* Reinitialize the UDP stack, i.e. close all existing connections. */
00794   NetworkUDPInitialize();
00795 }
00796 
00801 static bool NetworkReceive()
00802 {
00803   if (_network_server) {
00804     ServerNetworkAdminSocketHandler::Receive();
00805     return ServerNetworkGameSocketHandler::Receive();
00806   } else {
00807     return ClientNetworkGameSocketHandler::Receive();
00808   }
00809 }
00810 
00811 /* This sends all buffered commands (if possible) */
00812 static void NetworkSend()
00813 {
00814   if (_network_server) {
00815     ServerNetworkAdminSocketHandler::Send();
00816     ServerNetworkGameSocketHandler::Send();
00817   } else {
00818     ClientNetworkGameSocketHandler::Send();
00819   }
00820 }
00821 
00822 /* We have to do some UDP checking */
00823 void NetworkUDPGameLoop()
00824 {
00825   _network_content_client.SendReceive();
00826   TCPConnecter::CheckCallbacks();
00827   NetworkHTTPSocketHandler::HTTPReceive();
00828 
00829   if (_network_udp_server) {
00830     _udp_server_socket->ReceivePackets();
00831     _udp_master_socket->ReceivePackets();
00832   } else {
00833     _udp_client_socket->ReceivePackets();
00834     if (_network_udp_broadcast > 0) _network_udp_broadcast--;
00835   }
00836 }
00837 
00838 /* The main loop called from ttd.c
00839  *  Here we also have to do StateGameLoop if needed! */
00840 void NetworkGameLoop()
00841 {
00842   if (!_networking) return;
00843 
00844   if (!NetworkReceive()) return;
00845 
00846   if (_network_server) {
00847     /* Log the sync state to check for in-syncedness of replays. */
00848     if (_date_fract == 0) {
00849       /* We don't want to log multiple times if paused. */
00850       static Date last_log;
00851       if (last_log != _date) {
00852         DEBUG(desync, 1, "sync: %08x; %02x; %08x; %08x", _date, _date_fract, _random.state[0], _random.state[1]);
00853         last_log = _date;
00854       }
00855     }
00856 
00857 #ifdef DEBUG_DUMP_COMMANDS
00858     /* Loading of the debug commands from -ddesync>=1 */
00859     static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
00860     static Date next_date = 0;
00861     static uint32 next_date_fract;
00862     static CommandPacket *cp = NULL;
00863     static bool check_sync_state = false;
00864     static uint32 sync_state[2];
00865     if (f == NULL && next_date == 0) {
00866       DEBUG(net, 0, "Cannot open commands.log");
00867       next_date = 1;
00868     }
00869 
00870     while (f != NULL && !feof(f)) {
00871       if (_date == next_date && _date_fract == next_date_fract) {
00872         if (cp != NULL) {
00873           NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, NULL, cp->text, cp->company);
00874           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));
00875           free(cp);
00876           cp = NULL;
00877         }
00878         if (check_sync_state) {
00879           if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
00880             DEBUG(net, 0, "sync check: %08x; %02x; match", _date, _date_fract);
00881           } else {
00882             DEBUG(net, 0, "sync check: %08x; %02x; mismatch expected {%08x, %08x}, got {%08x, %08x}",
00883                   _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
00884             NOT_REACHED();
00885           }
00886           check_sync_state = false;
00887         }
00888       }
00889 
00890       if (cp != NULL || check_sync_state) break;
00891 
00892       char buff[4096];
00893       if (fgets(buff, lengthof(buff), f) == NULL) break;
00894 
00895       char *p = buff;
00896       /* Ignore the "[date time] " part of the message */
00897       if (*p == '[') {
00898         p = strchr(p, ']');
00899         if (p == NULL) break;
00900         p += 2;
00901       }
00902 
00903       if (strncmp(p, "cmd: ", 5) == 0) {
00904         cp = CallocT<CommandPacket>(1);
00905         int company;
00906         int ret = sscanf(p + 5, "%x; %x; %x; %x; %x; %x; %x; \"%[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text);
00907         /* There are 8 pieces of data to read, however the last is a
00908          * string that might or might not exist. Ignore it if that
00909          * string misses because in 99% of the time it's not used. */
00910         assert(ret == 8 || ret == 7);
00911         cp->company = (CompanyID)company;
00912       } else if (strncmp(p, "join: ", 6) == 0) {
00913         /* Manually insert a pause when joining; this way the client can join at the exact right time. */
00914         int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
00915         assert(ret == 2);
00916         DEBUG(net, 0, "injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
00917         cp = CallocT<CommandPacket>(1);
00918         cp->company = COMPANY_SPECTATOR;
00919         cp->cmd = CMD_PAUSE;
00920         cp->p1 = PM_PAUSED_NORMAL;
00921         cp->p2 = 1;
00922         _ddc_fastforward = false;
00923       } else if (strncmp(p, "sync: ", 6) == 0) {
00924         int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
00925         assert(ret == 4);
00926         check_sync_state = true;
00927       } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
00928             strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
00929         /* A message that is not very important to the log playback, but part of the log. */
00930       } else {
00931         /* Can't parse a line; what's wrong here? */
00932         DEBUG(net, 0, "trying to parse: %s", p);
00933         NOT_REACHED();
00934       }
00935     }
00936     if (f != NULL && feof(f)) {
00937       DEBUG(net, 0, "End of commands.log");
00938       fclose(f);
00939       f = NULL;
00940     }
00941 #endif /* DEBUG_DUMP_COMMANDS */
00942     if (_frame_counter >= _frame_counter_max) {
00943       /* Only check for active clients just before we're going to send out
00944        * the commands so we don't send multiple pause/unpause commands when
00945        * the frame_freq is more than 1 tick. Same with distributing commands. */
00946       CheckPauseOnJoin();
00947       CheckMinActiveClients();
00948       NetworkDistributeCommands();
00949     }
00950 
00951     bool send_frame = false;
00952 
00953     /* We first increase the _frame_counter */
00954     _frame_counter++;
00955     /* Update max-frame-counter */
00956     if (_frame_counter > _frame_counter_max) {
00957       _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
00958       send_frame = true;
00959     }
00960 
00961     NetworkExecuteLocalCommandQueue();
00962 
00963     /* Then we make the frame */
00964     StateGameLoop();
00965 
00966     _sync_seed_1 = _random.state[0];
00967 #ifdef NETWORK_SEND_DOUBLE_SEED
00968     _sync_seed_2 = _random.state[1];
00969 #endif
00970 
00971     NetworkServer_Tick(send_frame);
00972   } else {
00973     /* Client */
00974 
00975     /* Make sure we are at the frame were the server is (quick-frames) */
00976     if (_frame_counter_server > _frame_counter) {
00977       /* Run a number of frames; when things go bad, get out. */
00978       while (_frame_counter_server > _frame_counter) {
00979         if (!ClientNetworkGameSocketHandler::GameLoop()) return;
00980       }
00981     } else {
00982       /* Else, keep on going till _frame_counter_max */
00983       if (_frame_counter_max > _frame_counter) {
00984         /* Run one frame; if things went bad, get out. */
00985         if (!ClientNetworkGameSocketHandler::GameLoop()) return;
00986       }
00987     }
00988   }
00989 
00990   NetworkSend();
00991 }
00992 
00993 static void NetworkGenerateServerId()
00994 {
00995   Md5 checksum;
00996   uint8 digest[16];
00997   char hex_output[16 * 2 + 1];
00998   char coding_string[NETWORK_NAME_LENGTH];
00999   int di;
01000 
01001   snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
01002 
01003   /* Generate the MD5 hash */
01004   checksum.Append((const uint8*)coding_string, strlen(coding_string));
01005   checksum.Finish(digest);
01006 
01007   for (di = 0; di < 16; ++di) {
01008     sprintf(hex_output + di * 2, "%02x", digest[di]);
01009   }
01010 
01011   /* _settings_client.network.network_id is our id */
01012   snprintf(_settings_client.network.network_id, sizeof(_settings_client.network.network_id), "%s", hex_output);
01013 }
01014 
01015 void NetworkStartDebugLog(NetworkAddress address)
01016 {
01017   extern SOCKET _debug_socket;  // Comes from debug.c
01018 
01019   DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
01020 
01021   SOCKET s = address.Connect();
01022   if (s == INVALID_SOCKET) {
01023     DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
01024     return;
01025   }
01026 
01027   _debug_socket = s;
01028 
01029   DEBUG(net, 0, "DEBUG() is now redirected");
01030 }
01031 
01033 void NetworkStartUp()
01034 {
01035   DEBUG(net, 3, "[core] starting network...");
01036 
01037   /* Network is available */
01038   _network_available = NetworkCoreInitialize();
01039   _network_dedicated = false;
01040   _network_last_advertise_frame = 0;
01041   _network_need_advertise = true;
01042   _network_advertise_retries = 0;
01043 
01044   /* Generate an server id when there is none yet */
01045   if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
01046 
01047   memset(&_network_game_info, 0, sizeof(_network_game_info));
01048 
01049   NetworkInitialize();
01050   DEBUG(net, 3, "[core] network online, multiplayer available");
01051   NetworkFindBroadcastIPs(&_broadcast_list);
01052 }
01053 
01055 void NetworkShutDown()
01056 {
01057   NetworkDisconnect(true);
01058   NetworkUDPClose();
01059 
01060   DEBUG(net, 3, "[core] shutting down network");
01061 
01062   _network_available = false;
01063 
01064   NetworkCoreShutdown();
01065 }
01066 
01071 bool IsNetworkCompatibleVersion(const char *other)
01072 {
01073   return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
01074 }
01075 
01076 #endif /* ENABLE_NETWORK */