# network.cpp

Go to the documentation of this file.

    00001 /* $Id: network.cpp 15425 2009-02-09 02:09:47Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "../company_type.h"
    00007
    00008 #ifdef ENABLE_NETWORK
    00009
    00010 #include "../openttd.h"
    00011 #include "../strings_func.h"
    00012 #include "../command_func.h"
    00013 #include "../variables.h"
    00014 #include "../date_func.h"
    00015 #include "network_internal.h"
    00016 #include "network_client.h"
    00017 #include "network_server.h"
    00018 #include "network_content.h"
    00019 #include "network_udp.h"
    00020 #include "network_gamelist.h"
    00021 #include "core/udp.h"
    00022 #include "core/host.h"
    00023 #include "network_gui.h"
    00024 #include "../console_func.h"
    00025 #include "../md5.h"
    00026 #include "../core/random_func.hpp"
    00027 #include "../window_func.h"
    00028 #include "../string_func.h"
    00029 #include "../company_func.h"
    00030 #include "../company_base.h"
    00031 #include "../settings_type.h"
    00032 #include "../landscape_type.h"
    00033 #include "../rev.h"
    00034 #include "../core/alloc_func.hpp"
    00035 #ifdef DEBUG_DUMP_COMMANDS
    00036   #include "../fileio_func.h"
    00037 #endif /* DEBUG_DUMP_COMMANDS */
    00038 #include "table/strings.h"
    00039 #include "../oldpool_func.h"
    00040
    00041 DECLARE_POSTFIX_INCREMENT(ClientID);
    00042
    00043 typedef ClientIndex NetworkClientInfoID;
    00044 DEFINE_OLD_POOL_GENERIC(NetworkClientInfo, NetworkClientInfo);
    00045
    00046 bool _network_server;
    00047 bool _network_available;
    00048 bool _network_dedicated;
    00049 bool _is_network_server;
    00050 NetworkServerGameInfo _network_game_info;
    00051 NetworkCompanyState *_network_company_states = NULL;
    00052 ClientID _network_own_client_id;
    00053 ClientID _redirect_console_to_client;
    00054 bool _network_need_advertise;
    00055 uint32 _network_last_advertise_frame;
    00056 uint8 _network_reconnect;
    00057 char *_network_host_list[10];
    00058 char *_network_ban_list[25];
    00059 uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
    00060 uint32 _frame_counter_max; // To where we may go with our clients
    00061 uint32 _frame_counter;
    00062 uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
    00063 uint32 _broadcast_list[MAX_INTERFACES + 1];
    00064 uint32 _network_server_bind_ip;
    00065 uint32 _sync_seed_1, _sync_seed_2;
    00066 uint32 _sync_frame;
    00067 bool _network_first_time;
    00068 bool _network_udp_server;
    00069 uint16 _network_udp_broadcast;
    00070 uint8 _network_advertise_retries;
    00071 CompanyMask _network_company_passworded;
    00072
    00073 /* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
    00074 assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
    00075 assert_compile((int)NETWORK_COMPANY_NAME_LENGTH == MAX_LENGTH_COMPANY_NAME_BYTES);
    00076
    00077 extern NetworkUDPSocketHandler *_udp_client_socket;
    00078 extern NetworkUDPSocketHandler *_udp_server_socket;
    00079 extern NetworkUDPSocketHandler *_udp_master_socket;
    00080
    00081 // The listen socket for the server
    00082 static SOCKET _listensocket;
    00083
    00084 // The amount of clients connected
    00085 static byte _network_clients_connected = 0;
    00086 // The identifier counter for new clients (is never decreased)
    00087 static ClientID _network_client_id = CLIENT_ID_FIRST;
    00088
    00089 /* Some externs / forwards */
    00090 extern void StateGameLoop();
    00091
    00097 NetworkClientInfo *NetworkFindClientInfoFromIndex(ClientIndex index)
    00098 {
    00099   return IsValidNetworkClientInfoIndex(index) ? GetNetworkClientInfo(index) : NULL;
    00100 }
    00101
    00107 NetworkClientInfo *NetworkFindClientInfoFromClientID(ClientID client_id)
    00108 {
    00109   NetworkClientInfo *ci;
    00110
    00111   FOR_ALL_CLIENT_INFOS(ci) {
    00112     if (ci->client_id == client_id) return ci;
    00113   }
    00114
    00115   return NULL;
    00116 }
    00117
    00123 NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip)
    00124 {
    00125   NetworkClientInfo *ci;
    00126   uint32 ip_number = inet_addr(ip);
    00127
    00128   FOR_ALL_CLIENT_INFOS(ci) {
    00129     if (ci->client_ip == ip_number) return ci;
    00130   }
    00131
    00132   return NULL;
    00133 }
    00134
    00140 NetworkClientSocket *NetworkFindClientStateFromClientID(ClientID client_id)
    00141 {
    00142   NetworkClientSocket *cs;
    00143
    00144   FOR_ALL_CLIENT_SOCKETS(cs) {
    00145     if (cs->client_id == client_id) return cs;
    00146   }
    00147
    00148   return NULL;
    00149 }
    00150
    00151 // NetworkGetClientName is a server-safe function to get the name of the client
    00152 //  if the user did not send it yet, Client #<no> is used.
    00153 void NetworkGetClientName(char *client_name, size_t size, const NetworkClientSocket *cs)
    00154 {
    00155   const NetworkClientInfo *ci = cs->GetInfo();
    00156
    00157   if (StrEmpty(ci->client_name)) {
    00158     snprintf(client_name, size, "Client #%4d", cs->client_id);
    00159   } else {
    00160     ttd_strlcpy(client_name, ci->client_name, size);
    00161   }
    00162 }
    00163
    00164 byte NetworkSpectatorCount()
    00165 {
    00166   const NetworkClientInfo *ci;
    00167   byte count = 0;
    00168
    00169   FOR_ALL_CLIENT_INFOS(ci) {
    00170     if (ci->client_playas == COMPANY_SPECTATOR) count++;
    00171   }
    00172
    00173   /* Don't count a dedicated server as spectator */
    00174   if (_network_dedicated) count--;
    00175
    00176   return count;
    00177 }
    00178
    00184 bool NetworkCompanyIsPassworded(CompanyID company_id)
    00185 {
    00186   return HasBit(_network_company_passworded, company_id);
    00187 }
    00188
    00189 // This puts a text-message to the console, or in the future, the chat-box,
    00190 //  (to keep it all a bit more general)
    00191 // If 'self_send' is true, this is the client who is sending the message
    00192 void NetworkTextMessage(NetworkAction action, ConsoleColour colour, bool self_send, const char *name, const char *str, int64 data)
    00193 {
    00194   const int duration = 10; // Game days the messages stay visible
    00195
    00196   StringID strid;
    00197   switch (action) {
    00198     case NETWORK_ACTION_SERVER_MESSAGE:
    00199       /* Ignore invalid messages */
    00200       if (data >= NETWORK_SERVER_MESSAGE_END) return;
    00201
    00202       strid = STR_NETWORK_SERVER_MESSAGE;
    00203       colour = CC_DEFAULT;
    00204       data = STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED_PLAYERS + data;
    00205       break;
    00206     case NETWORK_ACTION_COMPANY_SPECTATOR:
    00207       colour = CC_DEFAULT;
    00208       strid = STR_NETWORK_CLIENT_COMPANY_SPECTATE;
    00209       break;
    00210     case NETWORK_ACTION_COMPANY_JOIN:
    00211       colour = CC_DEFAULT;
    00212       strid = STR_NETWORK_CLIENT_COMPANY_JOIN;
    00213       break;
    00214     case NETWORK_ACTION_COMPANY_NEW:
    00215       colour = CC_DEFAULT;
    00216       strid = STR_NETWORK_CLIENT_COMPANY_NEW;
    00217       break;
    00218     case NETWORK_ACTION_JOIN:           strid = STR_NETWORK_CLIENT_JOINED; break;
    00219     case NETWORK_ACTION_LEAVE:          strid = STR_NETWORK_CLIENT_LEFT; break;
    00220     case NETWORK_ACTION_NAME_CHANGE:    strid = STR_NETWORK_NAME_CHANGE; break;
    00221     case NETWORK_ACTION_GIVE_MONEY:     strid = self_send ? STR_NETWORK_GAVE_MONEY_AWAY : STR_NETWORK_GIVE_MONEY;   break;
    00222     case NETWORK_ACTION_CHAT_COMPANY:   strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
    00223     case NETWORK_ACTION_CHAT_CLIENT:    strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT  : STR_NETWORK_CHAT_CLIENT;  break;
    00224     default:                            strid = STR_NETWORK_CHAT_ALL; break;
    00225   }
    00226
    00227   char message[1024];
    00228   SetDParamStr(0, name);
    00229   SetDParamStr(1, str);
    00230   SetDParam(2, data);
    00231   GetString(message, strid, lastof(message));
    00232
    00233   DEBUG(desync, 1, "msg: %d; %d; %s\n", _date, _date_fract, message);
    00234   IConsolePrintF(colour, "%s", message);
    00235   NetworkAddChatMessage((TextColour)colour, duration, "%s", message);
    00236 }
    00237
    00238 // Calculate the frame-lag of a client
    00239 uint NetworkCalculateLag(const NetworkClientSocket *cs)
    00240 {
    00241   int lag = cs->last_frame_server - cs->last_frame;
    00242   // This client has missed his ACK packet after 1 DAY_TICKS..
    00243   //  so we increase his lag for every frame that passes!
    00244   // The packet can be out by a max of _net_frame_freq
    00245   if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter)
    00246     lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
    00247
    00248   return lag;
    00249 }
    00250
    00251
    00252 // There was a non-recoverable error, drop back to the main menu with a nice
    00253 //  error
    00254 static void NetworkError(StringID error_string)
    00255 {
    00256   _switch_mode = SM_MENU;
    00257   extern StringID _switch_mode_errorstr;
    00258   _switch_mode_errorstr = error_string;
    00259 }
    00260
    00261 static void ServerStartError(const char *error)
    00262 {
    00263   DEBUG(net, 0, "[server] could not start network: %s",error);
    00264   NetworkError(STR_NETWORK_ERR_SERVER_START);
    00265 }
    00266
    00267 static void NetworkClientError(NetworkRecvStatus res, NetworkClientSocket *cs)
    00268 {
    00269   // First, send a CLIENT_ERROR to the server, so he knows we are
    00270   //  disconnection (and why!)
    00271   NetworkErrorCode errorno;
    00272
    00273   // We just want to close the connection..
    00274   if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
    00275     cs->has_quit = true;
    00276     NetworkCloseClient(cs);
    00277     _networking = false;
    00278
    00279     DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00280     return;
    00281   }
    00282
    00283   switch (res) {
    00284     case NETWORK_RECV_STATUS_DESYNC:          errorno = NETWORK_ERROR_DESYNC; break;
    00285     case NETWORK_RECV_STATUS_SAVEGAME:        errorno = NETWORK_ERROR_SAVEGAME_FAILED; break;
    00286     case NETWORK_RECV_STATUS_NEWGRF_MISMATCH: errorno = NETWORK_ERROR_NEWGRF_MISMATCH; break;
    00287     default:                                  errorno = NETWORK_ERROR_GENERAL; break;
    00288   }
    00289   // This means we fucked up and the server closed the connection
    00290   if (res != NETWORK_RECV_STATUS_SERVER_ERROR && res != NETWORK_RECV_STATUS_SERVER_FULL &&
    00291       res != NETWORK_RECV_STATUS_SERVER_BANNED) {
    00292     SEND_COMMAND(PACKET_CLIENT_ERROR)(errorno);
    00293   }
    00294
    00295   _switch_mode = SM_MENU;
    00296   NetworkCloseClient(cs);
    00297   _networking = false;
    00298 }
    00299
    00305 StringID GetNetworkErrorMsg(NetworkErrorCode err)
    00306 {
    00307   /* List of possible network errors, used by
    00308    * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
    00309   static const StringID network_error_strings[] = {
    00310     STR_NETWORK_ERR_CLIENT_GENERAL,
    00311     STR_NETWORK_ERR_CLIENT_DESYNC,
    00312     STR_NETWORK_ERR_CLIENT_SAVEGAME,
    00313     STR_NETWORK_ERR_CLIENT_CONNECTION_LOST,
    00314     STR_NETWORK_ERR_CLIENT_PROTOCOL_ERROR,
    00315     STR_NETWORK_ERR_CLIENT_NEWGRF_MISMATCH,
    00316     STR_NETWORK_ERR_CLIENT_NOT_AUTHORIZED,
    00317     STR_NETWORK_ERR_CLIENT_NOT_EXPECTED,
    00318     STR_NETWORK_ERR_CLIENT_WRONG_REVISION,
    00319     STR_NETWORK_ERR_CLIENT_NAME_IN_USE,
    00320     STR_NETWORK_ERR_CLIENT_WRONG_PASSWORD,
    00321     STR_NETWORK_ERR_CLIENT_COMPANY_MISMATCH,
    00322     STR_NETWORK_ERR_CLIENT_KICKED,
    00323     STR_NETWORK_ERR_CLIENT_CHEATER,
    00324     STR_NETWORK_ERR_CLIENT_SERVER_FULL
    00325   };
    00326
    00327   if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
    00328
    00329   return network_error_strings[err];
    00330 }
    00331
    00332 /* Count the number of active clients connected */
    00333 static uint NetworkCountActiveClients()
    00334 {
    00335   const NetworkClientInfo *ci;
    00336   uint count = 0;
    00337
    00338   FOR_ALL_CLIENT_INFOS(ci) {
    00339     if (IsValidCompanyID(ci->client_playas)) count++;
    00340   }
    00341
    00342   return count;
    00343 }
    00344
    00345 static bool _min_active_clients_paused = false;
    00346
    00347 /* Check if the minimum number of active clients has been reached and pause or unpause the game as appropriate */
    00348 void CheckMinActiveClients()
    00349 {
    00350   if (!_network_dedicated) return;
    00351
    00352   if (NetworkCountActiveClients() < _settings_client.network.min_active_clients) {
    00353     if (_min_active_clients_paused) return;
    00354
    00355     _min_active_clients_paused = true;
    00356     DoCommandP(0, 1, 0, CMD_PAUSE);
    00357     NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "", CLIENT_ID_SERVER, NETWORK_SERVER_MESSAGE_GAME_PAUSED_PLAYERS);
    00358   } else {
    00359     if (!_min_active_clients_paused) return;
    00360
    00361     _min_active_clients_paused = false;
    00362     DoCommandP(0, 0, 0, CMD_PAUSE);
    00363     NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "", CLIENT_ID_SERVER, NETWORK_SERVER_MESSAGE_GAME_UNPAUSED_PLAYERS);
    00364   }
    00365 }
    00366
    00373 void ParseConnectionString(const char **company, const char **port, char *connection_string)
    00374 {
    00375   char *p;
    00376   for (p = connection_string; *p != '\0'; p++) {
    00377     switch (*p) {
    00378       case '#':
    00379         *company = p + 1;
    00380         *p = '\0';
    00381         break;
    00382       case ':':
    00383         *port = p + 1;
    00384         *p = '\0';
    00385         break;
    00386     }
    00387   }
    00388 }
    00389
    00390 // Creates a new client from a socket
    00391 //   Used both by the server and the client
    00392 static NetworkClientSocket *NetworkAllocClient(SOCKET s)
    00393 {
    00394   if (_network_server) {
    00395     // Can we handle a new client?
    00396     if (_network_clients_connected >= MAX_CLIENTS) return NULL;
    00397     if (_network_game_info.clients_on >= _settings_client.network.max_clients) return NULL;
    00398
    00399     // Register the login
    00400     _network_clients_connected++;
    00401   }
    00402
    00403   NetworkClientSocket *cs = new NetworkClientSocket(INVALID_CLIENT_ID);
    00404   cs->sock = s;
    00405   cs->last_frame = _frame_counter;
    00406   cs->last_frame_server = _frame_counter;
    00407
    00408   if (_network_server) {
    00409     cs->client_id = _network_client_id++;
    00410     NetworkClientInfo *ci = new NetworkClientInfo(cs->client_id);
    00411     cs->SetInfo(ci);
    00412     ci->client_playas = COMPANY_INACTIVE_CLIENT;
    00413     ci->join_date = _date;
    00414
    00415     InvalidateWindow(WC_CLIENT_LIST, 0);
    00416   }
    00417
    00418   return cs;
    00419 }
    00420
    00421 // Close a connection
    00422 void NetworkCloseClient(NetworkClientSocket *cs)
    00423 {
    00424   assert(cs->sock != INVALID_SOCKET);
    00425
    00426   DEBUG(net, 1, "Closed client connection %d", cs->client_id);
    00427
    00428   if (!cs->has_quit && _network_server && cs->status > STATUS_INACTIVE) {
    00429     // We did not receive a leave message from this client...
    00430     char client_name[NETWORK_CLIENT_NAME_LENGTH];
    00431     NetworkClientSocket *new_cs;
    00432
    00433     NetworkGetClientName(client_name, sizeof(client_name), cs);
    00434
    00435     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, STR_NETWORK_ERR_CLIENT_CONNECTION_LOST);
    00436
    00437     // Inform other clients of this... strange leaving ;)
    00438     FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00439       if (new_cs->status > STATUS_AUTH && cs != new_cs) {
    00440         SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->client_id, NETWORK_ERROR_CONNECTION_LOST);
    00441       }
    00442     }
    00443   }
    00444
    00445   /* When the client was PRE_ACTIVE, the server was in pause mode, so unpause */
    00446   if (cs->status == STATUS_PRE_ACTIVE && _settings_client.network.pause_on_join) {
    00447     DoCommandP(0, 0, 0, CMD_PAUSE);
    00448     NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "", CLIENT_ID_SERVER, NETWORK_SERVER_MESSAGE_GAME_UNPAUSED_CONNECT_FAIL);
    00449   }
    00450
    00451   if (_network_server) {
    00452     // We just lost one client :(
    00453     if (cs->status >= STATUS_AUTH) _network_game_info.clients_on--;
    00454     _network_clients_connected--;
    00455
    00456     InvalidateWindow(WC_CLIENT_LIST, 0);
    00457   }
    00458
    00459   delete cs->GetInfo();
    00460   delete cs;
    00461
    00462   CheckMinActiveClients();
    00463 }
    00464
    00465 // For the server, to accept new clients
    00466 static void NetworkAcceptClients()
    00467 {
    00468   struct sockaddr_in sin;
    00469   NetworkClientSocket *cs;
    00470   uint i;
    00471   bool banned;
    00472
    00473   // Should never ever happen.. is it possible??
    00474   assert(_listensocket != INVALID_SOCKET);
    00475
    00476   for (;;) {
    00477     socklen_t sin_len = sizeof(sin);
    00478     SOCKET s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len);
    00479     if (s == INVALID_SOCKET) return;
    00480
    00481     SetNonBlocking(s); // XXX error handling?
    00482
    00483     DEBUG(net, 1, "Client connected from %s on frame %d", inet_ntoa(sin.sin_addr), _frame_counter);
    00484
    00485     SetNoDelay(s); // XXX error handling?
    00486
    00487     /* Check if the client is banned */
    00488     banned = false;
    00489     for (i = 0; i < lengthof(_network_ban_list); i++) {
    00490       if (_network_ban_list[i] == NULL) continue;
    00491
    00492       /* Check for CIDR separator */
    00493       char *chr_cidr = strchr(_network_ban_list[i], '/');
    00494       if (chr_cidr != NULL) {
    00495         int cidr = atoi(chr_cidr + 1);
    00496
    00497         /* Invalid CIDR, treat as single host */
    00498         if (cidr <= 0 || cidr > 32) cidr = 32;
    00499
    00500         /* Remove and then replace the / so that inet_addr() works on the IP portion */
    00501         *chr_cidr = '\0';
    00502         uint32 ban_ip = inet_addr(_network_ban_list[i]);
    00503         *chr_cidr = '/';
    00504
    00505         /* Convert CIDR to mask in network format */
    00506         uint32 mask = htonl(-(1 << (32 - cidr)));
    00507         if ((sin.sin_addr.s_addr & mask) == (ban_ip & mask)) banned = true;
    00508       } else {
    00509         /* No CIDR used, so just perform a simple IP test */
    00510         if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) banned = true;
    00511       }
    00512
    00513       if (banned) {
    00514         Packet p(PACKET_SERVER_BANNED);
    00515         p.PrepareToSend();
    00516
    00517         DEBUG(net, 1, "Banned ip tried to join (%s), refused", _network_ban_list[i]);
    00518
    00519         send(s, (const char*)p.buffer, p.size, 0);
    00520         closesocket(s);
    00521         break;
    00522       }
    00523     }
    00524     /* If this client is banned, continue with next client */
    00525     if (banned) continue;
    00526
    00527     cs = NetworkAllocClient(s);
    00528     if (cs == NULL) {
    00529       // no more clients allowed?
    00530       // Send to the client that we are full!
    00531       Packet p(PACKET_SERVER_FULL);
    00532       p.PrepareToSend();
    00533
    00534       send(s, (const char*)p.buffer, p.size, 0);
    00535       closesocket(s);
    00536
    00537       continue;
    00538     }
    00539
    00540     // a new client has connected. We set him at inactive for now
    00541     //  maybe he is only requesting server-info. Till he has sent a PACKET_CLIENT_MAP_OK
    00542     //  the client stays inactive
    00543     cs->status = STATUS_INACTIVE;
    00544
    00545     cs->GetInfo()->client_ip = sin.sin_addr.s_addr; // Save the IP of the client
    00546   }
    00547 }
    00548
    00549 // Set up the listen socket for the server
    00550 static bool NetworkListen()
    00551 {
    00552   SOCKET ls;
    00553   struct sockaddr_in sin;
    00554
    00555   DEBUG(net, 1, "Listening on %s:%d", _settings_client.network.server_bind_ip, _settings_client.network.server_port);
    00556
    00557   ls = socket(AF_INET, SOCK_STREAM, 0);
    00558   if (ls == INVALID_SOCKET) {
    00559     ServerStartError("socket() on listen socket failed");
    00560     return false;
    00561   }
    00562
    00563   { // reuse the socket
    00564     int reuse = 1;
    00565     // The (const char*) cast is needed for windows!!
    00566     if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) {
    00567       ServerStartError("setsockopt() on listen socket failed");
    00568       return false;
    00569     }
    00570   }
    00571
    00572   if (!SetNonBlocking(ls)) DEBUG(net, 0, "Setting non-blocking mode failed"); // XXX should this be an error?
    00573
    00574   sin.sin_family = AF_INET;
    00575   sin.sin_addr.s_addr = _network_server_bind_ip;
    00576   sin.sin_port = htons(_settings_client.network.server_port);
    00577
    00578   if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
    00579     ServerStartError("bind() failed");
    00580     return false;
    00581   }
    00582
    00583   if (listen(ls, 1) != 0) {
    00584     ServerStartError("listen() failed");
    00585     return false;
    00586   }
    00587
    00588   _listensocket = ls;
    00589
    00590   return true;
    00591 }
    00592
    00593 // Close all current connections
    00594 static void NetworkClose()
    00595 {
    00596   /* The pool is already empty, so we already closed the connections */
    00597   if (GetNetworkClientSocketPoolSize() == 0) return;
    00598
    00599   NetworkClientSocket *cs;
    00600
    00601   FOR_ALL_CLIENT_SOCKETS(cs) {
    00602     if (!_network_server) {
    00603       SEND_COMMAND(PACKET_CLIENT_QUIT)();
    00604       cs->Send_Packets();
    00605     }
    00606     NetworkCloseClient(cs);
    00607   }
    00608
    00609   if (_network_server) {
    00610     /* We are a server, also close the listensocket */
    00611     closesocket(_listensocket);
    00612     _listensocket = INVALID_SOCKET;
    00613     DEBUG(net, 1, "Closed listener");
    00614   }
    00615   NetworkUDPCloseAll();
    00616
    00617   TCPConnecter::KillAll();
    00618
    00619   _networking = false;
    00620   _network_server = false;
    00621
    00622   free(_network_company_states);
    00623   _network_company_states = NULL;
    00624
    00625   _NetworkClientSocket_pool.CleanPool();
    00626   _NetworkClientInfo_pool.CleanPool();
    00627 }
    00628
    00629 // Inits the network (cleans sockets and stuff)
    00630 static void NetworkInitialize()
    00631 {
    00632   _NetworkClientSocket_pool.CleanPool();
    00633   _NetworkClientSocket_pool.AddBlockToPool();
    00634   _NetworkClientInfo_pool.CleanPool();
    00635   _NetworkClientInfo_pool.AddBlockToPool();
    00636
    00637   _sync_frame = 0;
    00638   _network_first_time = true;
    00639
    00640   _network_reconnect = 0;
    00641 }
    00642
    00644 class TCPQueryConnecter : TCPConnecter {
    00645 public:
    00646   TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
    00647
    00648   virtual void OnFailure()
    00649   {
    00650     NetworkDisconnect();
    00651   }
    00652
    00653   virtual void OnConnect(SOCKET s)
    00654   {
    00655     _networking = true;
    00656     NetworkAllocClient(s);
    00657     SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)();
    00658   }
    00659 };
    00660
    00661 // Query a server to fetch his game-info
    00662 //  If game_info is true, only the gameinfo is fetched,
    00663 //   else only the client_info is fetched
    00664 void NetworkTCPQueryServer(NetworkAddress address)
    00665 {
    00666   if (!_network_available) return;
    00667
    00668   NetworkDisconnect();
    00669   NetworkInitialize();
    00670
    00671   new TCPQueryConnecter(address);
    00672 }
    00673
    00674 /* Validates an address entered as a string and adds the server to
    00675  * the list. If you use this function, the games will be marked
    00676  * as manually added. */
    00677 void NetworkAddServer(const char *b)
    00678 {
    00679   if (*b != '\0') {
    00680     const char *port = NULL;
    00681     const char *company = NULL;
    00682     char host[NETWORK_HOSTNAME_LENGTH];
    00683     uint16 rport;
    00684
    00685     strecpy(host, b, lastof(host));
    00686
    00687     strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
    00688     rport = NETWORK_DEFAULT_PORT;
    00689
    00690     ParseConnectionString(&company, &port, host);
    00691     if (port != NULL) rport = atoi(port);
    00692
    00693     NetworkUDPQueryServer(NetworkAddress(host, rport), true);
    00694   }
    00695 }
    00696
    00697 /* Generates the list of manually added hosts from NetworkGameList and
    00698  * dumps them into the array _network_host_list. This array is needed
    00699  * by the function that generates the config file. */
    00700 void NetworkRebuildHostList()
    00701 {
    00702   uint i = 0;
    00703   const NetworkGameList *item = _network_game_list;
    00704   while (item != NULL && i != lengthof(_network_host_list)) {
    00705     if (item->manually) {
    00706       free(_network_host_list[i]);
    00707       _network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->port);
    00708     }
    00709     item = item->next;
    00710   }
    00711
    00712   for (; i < lengthof(_network_host_list); i++) {
    00713     free(_network_host_list[i]);
    00714     _network_host_list[i] = NULL;
    00715   }
    00716 }
    00717
    00719 class TCPClientConnecter : TCPConnecter {
    00720 public:
    00721   TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
    00722
    00723   virtual void OnFailure()
    00724   {
    00725     NetworkError(STR_NETWORK_ERR_NOCONNECTION);
    00726   }
    00727
    00728   virtual void OnConnect(SOCKET s)
    00729   {
    00730     _networking = true;
    00731     NetworkAllocClient(s);
    00732     IConsoleCmdExec("exec scripts/on_client.scr 0");
    00733     NetworkClient_Connected();
    00734   }
    00735 };
    00736
    00737
    00738 // Used by clients, to connect to a server
    00739 void NetworkClientConnectGame(NetworkAddress address)
    00740 {
    00741   if (!_network_available) return;
    00742
    00743   if (address.GetPort() == 0) return;
    00744
    00745   strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
    00746   _settings_client.network.last_port = address.GetPort();
    00747
    00748   NetworkDisconnect();
    00749   NetworkInitialize();
    00750
    00751   _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
    00752   ShowJoinStatusWindow();
    00753
    00754   new TCPClientConnecter(address);
    00755 }
    00756
    00757 static void NetworkInitGameInfo()
    00758 {
    00759   if (StrEmpty(_settings_client.network.server_name)) {
    00760     snprintf(_settings_client.network.server_name, sizeof(_settings_client.network.server_name), "Unnamed Server");
    00761   }
    00762
    00763   /* The server is a client too */
    00764   _network_game_info.clients_on = _network_dedicated ? 0 : 1;
    00765   _network_game_info.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
    00766
    00767   NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
    00768   ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : _local_company;
    00769
    00770   strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
    00771   strecpy(ci->unique_id, _settings_client.network.network_id, lastof(ci->unique_id));
    00772 }
    00773
    00774 bool NetworkServerStart()
    00775 {
    00776   if (!_network_available) return false;
    00777
    00778   /* Call the pre-scripts */
    00779   IConsoleCmdExec("exec scripts/pre_server.scr 0");
    00780   if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
    00781
    00782   NetworkInitialize();
    00783   if (!NetworkListen()) return false;
    00784
    00785   // Try to start UDP-server
    00786   _network_udp_server = true;
    00787   _network_udp_server = _udp_server_socket->Listen(_network_server_bind_ip, _settings_client.network.server_port, false);
    00788
    00789   _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
    00790   _network_server = true;
    00791   _networking = true;
    00792   _frame_counter = 0;
    00793   _frame_counter_server = 0;
    00794   _frame_counter_max = 0;
    00795   _last_sync_frame = 0;
    00796   _network_own_client_id = CLIENT_ID_SERVER;
    00797
    00798   /* Non-dedicated server will always be company #1 */
    00799   if (!_network_dedicated) _network_playas = COMPANY_FIRST;
    00800
    00801   _network_clients_connected = 0;
    00802
    00803   NetworkInitGameInfo();
    00804
    00805   // execute server initialization script
    00806   IConsoleCmdExec("exec scripts/on_server.scr 0");
    00807   // if the server is dedicated ... add some other script
    00808   if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
    00809
    00810   _min_active_clients_paused = false;
    00811   CheckMinActiveClients();
    00812
    00813   /* Try to register us to the master server */
    00814   _network_last_advertise_frame = 0;
    00815   _network_need_advertise = true;
    00816   NetworkUDPAdvertise();
    00817   return true;
    00818 }
    00819
    00820 // The server is rebooting...
    00821 // The only difference with NetworkDisconnect, is the packets that is sent
    00822 void NetworkReboot()
    00823 {
    00824   if (_network_server) {
    00825     NetworkClientSocket *cs;
    00826     FOR_ALL_CLIENT_SOCKETS(cs) {
    00827       SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs);
    00828       cs->Send_Packets();
    00829     }
    00830   }
    00831
    00832   NetworkClose();
    00833 }
    00834
    00835 // We want to disconnect from the host/clients
    00836 void NetworkDisconnect()
    00837 {
    00838   if (_network_server) {
    00839     NetworkClientSocket *cs;
    00840     FOR_ALL_CLIENT_SOCKETS(cs) {
    00841       SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs);
    00842       cs->Send_Packets();
    00843     }
    00844   }
    00845
    00846   if (_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise();
    00847
    00848   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00849
    00850   NetworkClose();
    00851 }
    00852
    00853 // Receives something from the network
    00854 static bool NetworkReceive()
    00855 {
    00856   NetworkClientSocket *cs;
    00857   int n;
    00858   fd_set read_fd, write_fd;
    00859   struct timeval tv;
    00860
    00861   FD_ZERO(&read_fd);
    00862   FD_ZERO(&write_fd);
    00863
    00864   FOR_ALL_CLIENT_SOCKETS(cs) {
    00865     FD_SET(cs->sock, &read_fd);
    00866     FD_SET(cs->sock, &write_fd);
    00867   }
    00868
    00869   // take care of listener port
    00870   if (_network_server) FD_SET(_listensocket, &read_fd);
    00871
    00872   tv.tv_sec = tv.tv_usec = 0; // don't block at all.
    00873 #if !defined(__MORPHOS__) && !defined(__AMIGA__)
    00874   n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv);
    00875 #else
    00876   n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL);
    00877 #endif
    00878   if (n == -1 && !_network_server) NetworkError(STR_NETWORK_ERR_LOSTCONNECTION);
    00879
    00880   // accept clients..
    00881   if (_network_server && FD_ISSET(_listensocket, &read_fd)) NetworkAcceptClients();
    00882
    00883   // read stuff from clients
    00884   FOR_ALL_CLIENT_SOCKETS(cs) {
    00885     cs->writable = !!FD_ISSET(cs->sock, &write_fd);
    00886     if (FD_ISSET(cs->sock, &read_fd)) {
    00887       if (_network_server) {
    00888         NetworkServer_ReadPackets(cs);
    00889       } else {
    00890         NetworkRecvStatus res;
    00891
    00892         // The client already was quiting!
    00893         if (cs->has_quit) return false;
    00894
    00895         res = NetworkClient_ReadPackets(cs);
    00896         if (res != NETWORK_RECV_STATUS_OKAY) {
    00897           // The client made an error of which we can not recover
    00898           //   close the client and drop back to main menu
    00899           NetworkClientError(res, cs);
    00900           return false;
    00901         }
    00902       }
    00903     }
    00904   }
    00905   return true;
    00906 }
    00907
    00908 // This sends all buffered commands (if possible)
    00909 static void NetworkSend()
    00910 {
    00911   NetworkClientSocket *cs;
    00912   FOR_ALL_CLIENT_SOCKETS(cs) {
    00913     if (cs->writable) {
    00914       cs->Send_Packets();
    00915
    00916       if (cs->status == STATUS_MAP) {
    00917         // This client is in the middle of a map-send, call the function for that
    00918         SEND_COMMAND(PACKET_SERVER_MAP)(cs);
    00919       }
    00920     }
    00921   }
    00922 }
    00923
    00924 static bool NetworkDoClientLoop()
    00925 {
    00926   _frame_counter++;
    00927
    00928   NetworkExecuteLocalCommandQueue();
    00929
    00930   StateGameLoop();
    00931
    00932   // Check if we are in sync!
    00933   if (_sync_frame != 0) {
    00934     if (_sync_frame == _frame_counter) {
    00935 #ifdef NETWORK_SEND_DOUBLE_SEED
    00936       if (_sync_seed_1 != _random.state[0] || _sync_seed_2 != _random.state[1]) {
    00937 #else
    00938       if (_sync_seed_1 != _random.state[0]) {
    00939 #endif
    00940         NetworkError(STR_NETWORK_ERR_DESYNC);
    00941         DEBUG(desync, 1, "sync_err: %d; %d\n", _date, _date_fract);
    00942         DEBUG(net, 0, "Sync error detected!");
    00943         NetworkClientError(NETWORK_RECV_STATUS_DESYNC, GetNetworkClientSocket(0));
    00944         return false;
    00945       }
    00946
    00947       // If this is the first time we have a sync-frame, we
    00948       //   need to let the server know that we are ready and at the same
    00949       //   frame as he is.. so we can start playing!
    00950       if (_network_first_time) {
    00951         _network_first_time = false;
    00952         SEND_COMMAND(PACKET_CLIENT_ACK)();
    00953       }
    00954
    00955       _sync_frame = 0;
    00956     } else if (_sync_frame < _frame_counter) {
    00957       DEBUG(net, 1, "Missed frame for sync-test (%d / %d)", _sync_frame, _frame_counter);
    00958       _sync_frame = 0;
    00959     }
    00960   }
    00961
    00962   return true;
    00963 }
    00964
    00965 // We have to do some UDP checking
    00966 void NetworkUDPGameLoop()
    00967 {
    00968   _network_content_client.SendReceive();
    00969   TCPConnecter::CheckCallbacks();
    00970
    00971   if (_network_udp_server) {
    00972     _udp_server_socket->ReceivePackets();
    00973     _udp_master_socket->ReceivePackets();
    00974   } else {
    00975     _udp_client_socket->ReceivePackets();
    00976     if (_network_udp_broadcast > 0) _network_udp_broadcast--;
    00977     NetworkGameListRequery();
    00978   }
    00979 }
    00980
    00981 // The main loop called from ttd.c
    00982 //  Here we also have to do StateGameLoop if needed!
    00983 void NetworkGameLoop()
    00984 {
    00985   if (!_networking) return;
    00986
    00987   if (!NetworkReceive()) return;
    00988
    00989   if (_network_server) {
    00990 #ifdef DEBUG_DUMP_COMMANDS
    00991     static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
    00992     static Date next_date = 0;
    00993     static uint32 next_date_fract;
    00994     static CommandPacket *cp = NULL;
    00995     if (f == NULL && next_date == 0) {
    00996       printf("Cannot open commands.log\n");
    00997       next_date = 1;
    00998     }
    00999
    01000     while (f != NULL && !feof(f)) {
    01001       if (cp != NULL && _date == next_date && _date_fract == next_date_fract) {
    01002         _current_company = cp->company;
    01003         DoCommandP(cp->tile, cp->p1, cp->p2, cp->cmd, NULL, cp->text);
    01004         free(cp);
    01005         cp = NULL;
    01006       }
    01007
    01008       if (cp != NULL) break;
    01009
    01010       char buff[4096];
    01011       if (fgets(buff, lengthof(buff), f) == NULL) break;
    01012       if (strncmp(buff, "cmd: ", 8) != 0) continue;
    01013       cp = MallocT<CommandPacket>(1);
    01014       int company;
    01015       sscanf(&buff[8], "%d; %d; %d; %d; %d; %d; %d; %s", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text);
    01016       cp->company = (CompanyID)company;
    01017     }
    01018 #endif /* DEBUG_DUMP_COMMANDS */
    01019
    01020     bool send_frame = false;
    01021
    01022     // We first increase the _frame_counter
    01023     _frame_counter++;
    01024     // Update max-frame-counter
    01025     if (_frame_counter > _frame_counter_max) {
    01026       _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
    01027       send_frame = true;
    01028     }
    01029
    01030     NetworkExecuteLocalCommandQueue();
    01031
    01032     // Then we make the frame
    01033     StateGameLoop();
    01034
    01035     _sync_seed_1 = _random.state[0];
    01036 #ifdef NETWORK_SEND_DOUBLE_SEED
    01037     _sync_seed_2 = _random.state[1];
    01038 #endif
    01039
    01040     NetworkServer_Tick(send_frame);
    01041   } else {
    01042     // Client
    01043
    01044     // Make sure we are at the frame were the server is (quick-frames)
    01045     if (_frame_counter_server > _frame_counter) {
    01046       while (_frame_counter_server > _frame_counter) {
    01047         if (!NetworkDoClientLoop()) break;
    01048       }
    01049     } else {
    01050       // Else, keep on going till _frame_counter_max
    01051       if (_frame_counter_max > _frame_counter) NetworkDoClientLoop();
    01052     }
    01053   }
    01054
    01055   NetworkSend();
    01056 }
    01057
    01058 static void NetworkGenerateUniqueId()
    01059 {
    01060   Md5 checksum;
    01061   uint8 digest[16];
    01062   char hex_output[16 * 2 + 1];
    01063   char coding_string[NETWORK_NAME_LENGTH];
    01064   int di;
    01065
    01066   snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Unique ID");
    01067
    01068   /* Generate the MD5 hash */
    01069   checksum.Append((const uint8*)coding_string, strlen(coding_string));
    01070   checksum.Finish(digest);
    01071
    01072   for (di = 0; di < 16; ++di)
    01073     sprintf(hex_output + di * 2, "%02x", digest[di]);
    01074
    01075   /* _network_unique_id is our id */
    01076   snprintf(_settings_client.network.network_id, sizeof(_settings_client.network.network_id), "%s", hex_output);
    01077 }
    01078
    01079 void NetworkStartDebugLog(NetworkAddress address)
    01080 {
    01081   extern SOCKET _debug_socket;  // Comes from debug.c
    01082   SOCKET s;
    01083   struct sockaddr_in sin;
    01084
    01085   DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
    01086
    01087   s = socket(AF_INET, SOCK_STREAM, 0);
    01088   if (s == INVALID_SOCKET) {
    01089     DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
    01090     return;
    01091   }
    01092
    01093   if (!SetNoDelay(s)) DEBUG(net, 1, "Setting TCP_NODELAY failed");
    01094
    01095   sin.sin_family = AF_INET;
    01096   sin.sin_addr.s_addr = address.GetIP();
    01097   sin.sin_port = htons(address.GetPort());
    01098
    01099   if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
    01100     DEBUG(net, 0, "Failed to redirection DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
    01101     return;
    01102   }
    01103
    01104   if (!SetNonBlocking(s)) DEBUG(net, 0, "Setting non-blocking mode failed");
    01105   _debug_socket = s;
    01106
    01107   DEBUG(net, 0, "DEBUG() is now redirected");
    01108 }
    01109
    01111 void NetworkStartUp()
    01112 {
    01113   DEBUG(net, 3, "[core] starting network...");
    01114
    01115   /* Network is available */
    01116   _network_available = NetworkCoreInitialize();;
    01117   _network_dedicated = false;
    01118   _network_last_advertise_frame = 0;
    01119   _network_need_advertise = true;
    01120   _network_advertise_retries = 0;
    01121
    01122   /* Load the ip from the openttd.cfg */
    01123   _network_server_bind_ip = inet_addr(_settings_client.network.server_bind_ip);
    01124   /* And put the data back in it in case it was an invalid ip */
    01125   snprintf(_settings_client.network.server_bind_ip, sizeof(_settings_client.network.server_bind_ip), "%s", inet_ntoa(*(struct in_addr *)&_network_server_bind_ip));
    01126
    01127   /* Generate an unique id when there is none yet */
    01128   if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateUniqueId();
    01129
    01130   memset(&_network_game_info, 0, sizeof(_network_game_info));
    01131
    01132   NetworkUDPInitialize();
    01133   NetworkInitialize();
    01134   DEBUG(net, 3, "[core] network online, multiplayer available");
    01135   NetworkFindBroadcastIPs(_broadcast_list, MAX_INTERFACES);
    01136 }
    01137
    01139 void NetworkShutDown()
    01140 {
    01141   NetworkDisconnect();
    01142   NetworkUDPShutdown();
    01143
    01144   DEBUG(net, 3, "[core] shutting down network");
    01145
    01146   _network_available = false;
    01147
    01148   NetworkCoreShutdown();
    01149 }
    01150
    01155 bool IsNetworkCompatibleVersion(const char *other)
    01156 {
    01157   return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
    01158 }
    01159
    01160 #endif /* ENABLE_NETWORK */
    01161
    01162 /* NOTE: this variable needs to be always available */
    01163 CompanyID _network_playas;