# network.cpp

Go to the documentation of this file.

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