tron@2186: /* $Id$ */ tron@2186: Darkvater@4826: #ifdef ENABLE_NETWORK Darkvater@4826: truelight@543: #include "stdafx.h" tron@1299: #include "debug.h" tron@1317: #include "string.h" truelight@543: #include "network_data.h" rubidium@4261: #include "date.h" tron@850: #include "map.h" truelight@543: #include "network_gamelist.h" truelight@764: #include "network_udp.h" tron@2153: #include "variables.h" rubidium@5339: #include "newgrf_config.h" truelight@543: truelight@543: // truelight@543: // This file handles all the LAN-stuff truelight@543: // Stuff like: truelight@543: // - UDP search over the network truelight@543: // truelight@543: truelight@543: typedef enum { truelight@638: PACKET_UDP_CLIENT_FIND_SERVER, truelight@543: PACKET_UDP_SERVER_RESPONSE, truelight@638: PACKET_UDP_CLIENT_DETAIL_INFO, rubidium@4344: PACKET_UDP_SERVER_DETAIL_INFO, // Is not used in OpenTTD itself, only for external querying rubidium@4344: PACKET_UDP_SERVER_REGISTER, // Packet to register itself to the master server rubidium@4344: PACKET_UDP_MASTER_ACK_REGISTER, // Packet indicating registration has succedeed rubidium@4344: PACKET_UDP_CLIENT_GET_LIST, // Request for serverlist from master server truelight@764: PACKET_UDP_MASTER_RESPONSE_LIST, // Response from master server with server ip's + port's rubidium@4344: PACKET_UDP_SERVER_UNREGISTER, // Request to be removed from the server-list rubidium@5339: PACKET_UDP_CLIENT_GET_NEWGRFS, // Requests the name for a list of GRFs (GRF_ID and MD5) rubidium@5339: PACKET_UDP_SERVER_NEWGRFS, // Sends the list of NewGRF's requested. truelight@543: PACKET_UDP_END truelight@543: } PacketUDPType; truelight@543: truelight@764: enum { peter1138@2861: ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes) rubidium@4344: ADVERTISE_RETRY_INTERVAL = 300, // readvertise when no response after this many ticks (9 seconds) rubidium@4344: ADVERTISE_RETRY_TIMES = 3 // give up readvertising after this much failed retries truelight@764: }; truelight@543: truelight@543: #define DEF_UDP_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(Packet *p, struct sockaddr_in *client_addr) tron@2817: static void NetworkSendUDP_Packet(SOCKET udp, Packet* p, struct sockaddr_in* recv); truelight@543: tron@2789: static NetworkClientState _udp_cs; truelight@903: rubidium@5339: /** rubidium@5339: * Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet rubidium@5339: * @param p the packet to write the data to rubidium@5339: * @param c the configuration to write the GRF ID and MD5 checksum from rubidium@5339: */ rubidium@5339: static void NetworkSend_GRFIdentifier(Packet *p, const GRFConfig *c) rubidium@5339: { rubidium@5339: uint j; rubidium@5339: NetworkSend_uint32(p, c->grfid); rubidium@5339: for (j = 0; j < sizeof(c->md5sum); j++) { rubidium@5339: NetworkSend_uint8 (p, c->md5sum[j]); rubidium@5339: } rubidium@5339: } rubidium@5339: rubidium@5339: /** rubidium@5339: * Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet rubidium@5339: * @param p the packet to read the data from rubidium@5339: * @param c the configuration to write the GRF ID and MD5 checksum to rubidium@5339: */ rubidium@5339: static void NetworkRecv_GRFIdentifier(Packet *p, GRFConfig *c) rubidium@5339: { rubidium@5339: uint j; rubidium@5339: c->grfid = NetworkRecv_uint32(&_udp_cs, p); rubidium@5339: for (j = 0; j < sizeof(c->md5sum); j++) { rubidium@5339: c->md5sum[j] = NetworkRecv_uint8(&_udp_cs, p); rubidium@5339: } rubidium@5339: } rubidium@5339: truelight@638: DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER) truelight@543: { truelight@543: Packet *packet; truelight@543: // Just a fail-safe.. should never happen truelight@543: if (!_network_udp_server) truelight@543: return; truelight@543: truelight@543: packet = NetworkSend_Init(PACKET_UDP_SERVER_RESPONSE); truelight@543: truelight@543: // Update some game_info truelight@543: _network_game_info.game_date = _date; tron@850: _network_game_info.map_width = MapSizeX(); tron@850: _network_game_info.map_height = MapSizeY(); truelight@543: _network_game_info.map_set = _opt.landscape; truelight@543: truelight@543: NetworkSend_uint8 (packet, NETWORK_GAME_INFO_VERSION); Darkvater@2879: rubidium@5339: /* NETWORK_GAME_INFO_VERSION = 4 */ rubidium@5339: { rubidium@5339: /* Only send the GRF Identification (GRF_ID and MD5 checksum) of rubidium@5339: * the GRFs that are needed, i.e. the ones that the server has rubidium@5339: * selected in the NewGRF GUI and not the ones that are used due rubidium@5339: * to the fact that they are in [newgrf-static] in openttd.cfg */ rubidium@5339: const GRFConfig *c; rubidium@5339: uint i = 0; rubidium@5339: rubidium@5339: /* Count number of GRFs to send information about */ rubidium@5339: for (c = _grfconfig; c != NULL; c = c->next) { rubidium@5339: if (!HASBIT(c->flags, GCF_STATIC)) i++; rubidium@5339: } rubidium@5339: NetworkSend_uint8 (packet, i); // Send number of GRFs rubidium@5339: rubidium@5339: /* Send actual GRF Identifications */ rubidium@5339: for (c = _grfconfig; c != NULL; c = c->next) { rubidium@5339: if (!HASBIT(c->flags, GCF_STATIC)) NetworkSend_GRFIdentifier(packet, c); rubidium@5339: } rubidium@5339: } rubidium@5339: rubidium@4326: /* NETWORK_GAME_INFO_VERSION = 3 */ rubidium@4326: NetworkSend_uint32(packet, _network_game_info.game_date); rubidium@4326: NetworkSend_uint32(packet, _network_game_info.start_date); rubidium@4326: Darkvater@2879: /* NETWORK_GAME_INFO_VERSION = 2 */ Darkvater@2879: NetworkSend_uint8 (packet, _network_game_info.companies_max); Darkvater@2944: NetworkSend_uint8 (packet, ActivePlayerCount()); Darkvater@2879: NetworkSend_uint8 (packet, _network_game_info.spectators_max); Darkvater@2879: Darkvater@2879: /* NETWORK_GAME_INFO_VERSION = 1 */ truelight@543: NetworkSend_string(packet, _network_game_info.server_name); truelight@543: NetworkSend_string(packet, _network_game_info.server_revision); truelight@543: NetworkSend_uint8 (packet, _network_game_info.server_lang); truelight@543: NetworkSend_uint8 (packet, _network_game_info.use_password); truelight@543: NetworkSend_uint8 (packet, _network_game_info.clients_max); truelight@543: NetworkSend_uint8 (packet, _network_game_info.clients_on); Darkvater@2944: NetworkSend_uint8 (packet, NetworkSpectatorCount()); truelight@543: NetworkSend_string(packet, _network_game_info.map_name); truelight@543: NetworkSend_uint16(packet, _network_game_info.map_width); truelight@543: NetworkSend_uint16(packet, _network_game_info.map_height); truelight@543: NetworkSend_uint8 (packet, _network_game_info.map_set); truelight@543: NetworkSend_uint8 (packet, _network_game_info.dedicated); truelight@543: truelight@543: // Let the client know that we are here truelight@764: NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr); truelight@543: truelight@543: free(packet); truelight@543: Darkvater@5568: DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr)); truelight@543: } truelight@543: truelight@543: DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE) truelight@543: { Darkvater@2881: extern const char _openttd_revision[]; truelight@543: NetworkGameList *item; truelight@543: byte game_info_version; truelight@543: truelight@543: // Just a fail-safe.. should never happen truelight@543: if (_network_udp_server) truelight@543: return; truelight@543: truelight@903: game_info_version = NetworkRecv_uint8(&_udp_cs, p); truelight@543: Darkvater@4880: if (_udp_cs.has_quit) return; truelight@764: Darkvater@5568: DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port)); truelight@764: truelight@543: // Find next item truelight@543: item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port)); truelight@543: rubidium@5339: item->info.compatible = true; Darkvater@2881: /* Please observer the order. In the order in which packets are sent Darkvater@2879: * they are to be received */ Darkvater@2879: switch (game_info_version) { rubidium@5339: case 4: { rubidium@5339: GRFConfig *c, **dst = &item->info.grfconfig; rubidium@5339: const GRFConfig *f; rubidium@5339: uint i; rubidium@5339: uint num_grfs = NetworkRecv_uint8(&_udp_cs, p); rubidium@5339: rubidium@5339: for (i = 0; i < num_grfs; i++) { rubidium@5339: c = calloc(1, sizeof(*c)); rubidium@5339: NetworkRecv_GRFIdentifier(p, c); rubidium@5339: rubidium@5339: /* Find the matching GRF file */ rubidium@5339: f = FindGRFConfig(c->grfid, c->md5sum); rubidium@5339: if (f == NULL) { rubidium@5339: /* Don't know the GRF, so mark game incompatible and the (possibly) rubidium@5339: * already resolved name for this GRF (another server has sent the rubidium@5339: * name of the GRF already */ rubidium@5339: item->info.compatible = false; rubidium@5339: c->name = FindUnknownGRFName(c->grfid, c->md5sum, true); rubidium@5339: SETBIT(c->flags, GCF_NOT_FOUND); rubidium@5339: } else { rubidium@5339: c->filename = f->filename; rubidium@5339: c->name = f->name; rubidium@5339: c->info = f->info; rubidium@5339: } rubidium@5339: SETBIT(c->flags, GCF_COPY); rubidium@5339: rubidium@5339: /* Append GRFConfig to the list */ rubidium@5339: *dst = c; rubidium@5339: dst = &c->next; rubidium@5339: } rubidium@5339: } /* Fallthrough */ rubidium@4326: case 3: rubidium@4326: item->info.game_date = NetworkRecv_uint32(&_udp_cs, p); rubidium@4326: item->info.start_date = NetworkRecv_uint32(&_udp_cs, p); rubidium@4326: /* Fallthrough */ Darkvater@2879: case 2: Darkvater@2879: item->info.companies_max = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: item->info.companies_on = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: item->info.spectators_max = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: /* Fallthrough */ Darkvater@2879: case 1: Darkvater@2879: NetworkRecv_string(&_udp_cs, p, item->info.server_name, sizeof(item->info.server_name)); Darkvater@2879: NetworkRecv_string(&_udp_cs, p, item->info.server_revision, sizeof(item->info.server_revision)); Darkvater@2879: item->info.server_lang = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: item->info.use_password = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: item->info.clients_max = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: item->info.clients_on = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: item->info.spectators_on = NetworkRecv_uint8(&_udp_cs, p); rubidium@4326: if (game_info_version < 3) { // 16 bits dates got scrapped and are read earlier rubidium@4326: item->info.game_date = NetworkRecv_uint16(&_udp_cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; rubidium@4326: item->info.start_date = NetworkRecv_uint16(&_udp_cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; rubidium@4326: } Darkvater@2879: NetworkRecv_string(&_udp_cs, p, item->info.map_name, sizeof(item->info.map_name)); Darkvater@2879: item->info.map_width = NetworkRecv_uint16(&_udp_cs, p); Darkvater@2879: item->info.map_height = NetworkRecv_uint16(&_udp_cs, p); Darkvater@2879: item->info.map_set = NetworkRecv_uint8(&_udp_cs, p); Darkvater@2879: item->info.dedicated = NetworkRecv_uint8(&_udp_cs, p); truelight@543: Darkvater@2879: if (item->info.server_lang >= NETWORK_NUM_LANGUAGES) item->info.server_lang = 0; Darkvater@2879: if (item->info.map_set >= NUM_LANDSCAPE ) item->info.map_set = 0; Darkvater@2775: Darkvater@2879: if (item->info.hostname[0] == '\0') Darkvater@2879: snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr)); Darkvater@2881: Darkvater@2881: /* Check if we are allowed on this server based on the revision-match */ rubidium@5339: item->info.version_compatible = tron@4077: strcmp(item->info.server_revision, _openttd_revision) == 0 || tron@4077: strcmp(item->info.server_revision, NOREV_STRING) == 0; rubidium@5339: item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs Darkvater@2879: break; truelight@543: } truelight@543: rubidium@5339: { rubidium@5339: /* Checks whether there needs to be a request for names of GRFs and makes rubidium@5339: * the request if necessary. GRFs that need to be requested are the GRFs rubidium@5339: * that do not exist on the clients system and we do not have the name rubidium@5339: * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER. rubidium@5339: * The in_request array and in_request_count are used so there is no need rubidium@5339: * to do a second loop over the GRF list, which can be relatively expensive rubidium@5339: * due to the string comparisons. */ rubidium@5339: const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT]; rubidium@5339: const GRFConfig *c; rubidium@5339: uint in_request_count = 0; rubidium@5339: struct sockaddr_in out_addr; rubidium@5339: rubidium@5339: for (c = item->info.grfconfig; c != NULL; c = c->next) { rubidium@5339: if (!HASBIT(c->flags, GCF_NOT_FOUND) || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue; rubidium@5339: in_request[in_request_count] = c; rubidium@5339: in_request_count++; rubidium@5339: } rubidium@5339: rubidium@5339: if (in_request_count > 0) { rubidium@5339: /* There are 'unknown' GRFs, now send a request for them */ rubidium@5339: uint i; rubidium@5339: Packet *packet = NetworkSend_Init(PACKET_UDP_CLIENT_GET_NEWGRFS); rubidium@5339: rubidium@5339: NetworkSend_uint8 (packet, in_request_count); rubidium@5339: for (i = 0; i < in_request_count; i++) { rubidium@5339: NetworkSend_GRFIdentifier(packet, in_request[i]); rubidium@5339: } rubidium@5339: rubidium@5339: out_addr.sin_family = AF_INET; rubidium@5339: out_addr.sin_port = htons(item->port); rubidium@5339: out_addr.sin_addr.s_addr = item->ip; rubidium@5339: NetworkSendUDP_Packet(_udp_client_socket, packet, &out_addr); rubidium@5339: free(packet); rubidium@5339: } rubidium@5339: } rubidium@5339: truelight@543: item->online = true; truelight@543: truelight@543: UpdateNetworkGameWindow(false); truelight@543: } truelight@543: truelight@638: DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO) truelight@638: { truelight@716: NetworkClientState *cs; truelight@638: NetworkClientInfo *ci; truelight@638: Packet *packet; truelight@638: Player *player; truelight@638: byte current = 0; truelight@638: int i; truelight@638: truelight@638: // Just a fail-safe.. should never happen tron@4077: if (!_network_udp_server) return; truelight@638: truelight@638: packet = NetworkSend_Init(PACKET_UDP_SERVER_DETAIL_INFO); truelight@638: truelight@638: /* Send the amount of active companies */ truelight@638: NetworkSend_uint8 (packet, NETWORK_COMPANY_INFO_VERSION); Darkvater@2944: NetworkSend_uint8 (packet, ActivePlayerCount()); truelight@638: truelight@638: /* Fetch the latest version of everything */ truelight@638: NetworkPopulateCompanyInfo(); truelight@638: truelight@638: /* Go through all the players */ truelight@638: FOR_ALL_PLAYERS(player) { truelight@638: /* Skip non-active players */ tron@4077: if (!player->is_active) continue; truelight@638: truelight@638: current++; truelight@638: truelight@638: /* Send the information */ tron@4077: NetworkSend_uint8(packet, current); truelight@638: truelight@638: NetworkSend_string(packet, _network_player_info[player->index].company_name); rubidium@4326: NetworkSend_uint32(packet, _network_player_info[player->index].inaugurated_year); truelight@638: NetworkSend_uint64(packet, _network_player_info[player->index].company_value); truelight@638: NetworkSend_uint64(packet, _network_player_info[player->index].money); truelight@638: NetworkSend_uint64(packet, _network_player_info[player->index].income); truelight@638: NetworkSend_uint16(packet, _network_player_info[player->index].performance); truelight@638: tron@4077: /* Send 1 if there is a passord for the company else send 0 */ truelight@1011: if (_network_player_info[player->index].password[0] != '\0') { tron@4077: NetworkSend_uint8(packet, 1); truelight@1011: } else { tron@4077: NetworkSend_uint8(packet, 0); tron@1019: } tron@1019: truelight@638: for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) truelight@638: NetworkSend_uint16(packet, _network_player_info[player->index].num_vehicle[i]); truelight@638: truelight@638: for (i = 0; i < NETWORK_STATION_TYPES; i++) truelight@638: NetworkSend_uint16(packet, _network_player_info[player->index].num_station[i]); truelight@638: truelight@638: /* Find the clients that are connected to this player */ truelight@638: FOR_ALL_CLIENTS(cs) { truelight@638: ci = DEREF_CLIENT_INFO(cs); Darkvater@4878: if (ci->client_playas == player->index) { truelight@638: /* The uint8 == 1 indicates that a client is following */ truelight@638: NetworkSend_uint8(packet, 1); truelight@638: NetworkSend_string(packet, ci->client_name); truelight@638: NetworkSend_string(packet, ci->unique_id); rubidium@4326: NetworkSend_uint32(packet, ci->join_date); truelight@638: } truelight@638: } truelight@638: /* Also check for the server itself */ truelight@638: ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); Darkvater@4878: if (ci->client_playas == player->index) { truelight@638: /* The uint8 == 1 indicates that a client is following */ truelight@638: NetworkSend_uint8(packet, 1); truelight@638: NetworkSend_string(packet, ci->client_name); truelight@638: NetworkSend_string(packet, ci->unique_id); rubidium@4326: NetworkSend_uint32(packet, ci->join_date); truelight@638: } truelight@638: truelight@638: /* Indicates end of client list */ truelight@638: NetworkSend_uint8(packet, 0); truelight@638: } truelight@638: truelight@638: /* And check if we have any spectators */ truelight@638: FOR_ALL_CLIENTS(cs) { truelight@638: ci = DEREF_CLIENT_INFO(cs); Darkvater@4878: if (!IsValidPlayer(ci->client_playas)) { truelight@638: /* The uint8 == 1 indicates that a client is following */ truelight@638: NetworkSend_uint8(packet, 1); truelight@638: NetworkSend_string(packet, ci->client_name); truelight@638: NetworkSend_string(packet, ci->unique_id); rubidium@4326: NetworkSend_uint32(packet, ci->join_date); truelight@638: } truelight@638: } Darkvater@4880: truelight@638: /* Also check for the server itself */ truelight@638: ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); Darkvater@4878: if (!IsValidPlayer(ci->client_playas)) { truelight@638: /* The uint8 == 1 indicates that a client is following */ truelight@638: NetworkSend_uint8(packet, 1); truelight@638: NetworkSend_string(packet, ci->client_name); truelight@638: NetworkSend_string(packet, ci->unique_id); rubidium@4326: NetworkSend_uint32(packet, ci->join_date); truelight@638: } truelight@638: truelight@638: /* Indicates end of client list */ truelight@638: NetworkSend_uint8(packet, 0); truelight@638: truelight@764: NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr); truelight@638: truelight@638: free(packet); truelight@638: } truelight@638: tron@4077: DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST) tron@4077: { truelight@764: int i; truelight@764: struct in_addr ip; truelight@764: uint16 port; truelight@764: uint8 ver; truelight@764: truelight@764: /* packet begins with the protocol version (uint8) truelight@764: * then an uint16 which indicates how many truelight@764: * ip:port pairs are in this packet, after that truelight@764: * an uint32 (ip) and an uint16 (port) for each pair truelight@764: */ truelight@764: truelight@903: ver = NetworkRecv_uint8(&_udp_cs, p); truelight@903: Darkvater@4880: if (_udp_cs.has_quit) return; truelight@764: truelight@764: if (ver == 1) { truelight@903: for (i = NetworkRecv_uint16(&_udp_cs, p); i != 0 ; i--) { truelight@903: ip.s_addr = TO_LE32(NetworkRecv_uint32(&_udp_cs, p)); truelight@903: port = NetworkRecv_uint16(&_udp_cs, p); truelight@764: NetworkUDPQueryServer(inet_ntoa(ip), port); truelight@764: } truelight@764: } truelight@764: } truelight@764: tron@4077: DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER) tron@4077: { truelight@764: _network_advertise_retries = 0; Darkvater@5568: DEBUG(net, 2, "[udp] advertising on master server successfull"); truelight@765: Darkvater@5568: /* We are advertised, but we don't want to! */ Darkvater@5568: if (!_network_advertise) NetworkUDPRemoveAdvertise(); truelight@764: } truelight@764: rubidium@5339: /** rubidium@5339: * A client has requested the names of some NewGRFs. rubidium@5339: * rubidium@5339: * Replying this can be tricky as we have a limit of SEND_MTU bytes rubidium@5339: * in the reply packet and we can send up to 100 bytes per NewGRF rubidium@5339: * (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name). rubidium@5339: * As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it rubidium@5339: * could be that a packet overflows. To stop this we only reply rubidium@5339: * with the first N NewGRFs so that if the first N + 1 NewGRFs rubidium@5339: * would be sent, the packet overflows. rubidium@5339: * in_reply and in_reply_count are used to keep a list of GRFs to rubidium@5339: * send in the reply. rubidium@5339: */ rubidium@5339: DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS) rubidium@5339: { rubidium@5339: uint8 num_grfs; rubidium@5339: uint i; rubidium@5339: rubidium@5339: const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT]; rubidium@5339: Packet *packet; rubidium@5339: uint8 in_reply_count = 0; rubidium@5339: uint packet_len = 0; rubidium@5339: rubidium@5339: /* Just a fail-safe.. should never happen */ rubidium@5339: if (_udp_cs.has_quit) return; rubidium@5339: Darkvater@5568: DEBUG(net, 6, "[udp] newgrf data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); rubidium@5339: rubidium@5339: num_grfs = NetworkRecv_uint8 (&_udp_cs, p); rubidium@5339: if (num_grfs > NETWORK_MAX_GRF_COUNT) return; rubidium@5339: rubidium@5339: for (i = 0; i < num_grfs; i++) { rubidium@5339: GRFConfig c; rubidium@5339: const GRFConfig *f; rubidium@5339: rubidium@5339: NetworkRecv_GRFIdentifier(p, &c); rubidium@5339: rubidium@5339: /* Find the matching GRF file */ rubidium@5339: f = FindGRFConfig(c.grfid, c.md5sum); rubidium@5339: if (f == NULL) continue; // The GRF is unknown to this server rubidium@5339: rubidium@5339: /* If the reply might exceed the size of the packet, only reply rubidium@5341: * the current list and do not send the other data. rubidium@5341: * The name could be an empty string, if so take the filename. */ rubidium@5341: packet_len += sizeof(c.grfid) + sizeof(c.md5sum) + rubidium@5341: min(strlen((f->name != NULL && strlen(f->name) > 0) ? f->name : f->filename) + 1, NETWORK_GRF_NAME_LENGTH); rubidium@5339: if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply rubidium@5339: break; rubidium@5339: } rubidium@5339: in_reply[in_reply_count] = f; rubidium@5339: in_reply_count++; rubidium@5339: } rubidium@5339: rubidium@5339: if (in_reply_count == 0) return; rubidium@5339: rubidium@5339: packet = NetworkSend_Init(PACKET_UDP_SERVER_NEWGRFS); rubidium@5339: NetworkSend_uint8 (packet, in_reply_count); rubidium@5339: for (i = 0; i < in_reply_count; i++) { rubidium@5339: char name[NETWORK_GRF_NAME_LENGTH]; rubidium@5341: rubidium@5341: /* The name could be an empty string, if so take the filename */ rubidium@5341: ttd_strlcpy(name, (in_reply[i]->name != NULL && strlen(in_reply[i]->name) > 0) ? rubidium@5341: in_reply[i]->name : in_reply[i]->filename, sizeof(name)); rubidium@5341: NetworkSend_GRFIdentifier(packet, in_reply[i]); rubidium@5339: NetworkSend_string(packet, name); rubidium@5339: } rubidium@5339: rubidium@5339: NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr); rubidium@5339: free(packet); rubidium@5339: } rubidium@5339: rubidium@5339: /** The return of the client's request of the names of some NewGRFs */ rubidium@5339: DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS) rubidium@5339: { rubidium@5339: uint8 num_grfs; rubidium@5339: uint i; rubidium@5339: rubidium@5339: /* Just a fail-safe.. should never happen */ rubidium@5339: if (_udp_cs.has_quit) return; rubidium@5339: Darkvater@5568: DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port)); rubidium@5339: rubidium@5339: num_grfs = NetworkRecv_uint8 (&_udp_cs, p); rubidium@5339: if (num_grfs > NETWORK_MAX_GRF_COUNT) return; rubidium@5339: rubidium@5339: for (i = 0; i < num_grfs; i++) { rubidium@5339: char *unknown_name; rubidium@5339: char name[NETWORK_GRF_NAME_LENGTH]; rubidium@5339: GRFConfig c; rubidium@5339: rubidium@5339: NetworkRecv_GRFIdentifier(p, &c); rubidium@5339: NetworkRecv_string(&_udp_cs, p, name, sizeof(name)); rubidium@5339: rubidium@5341: /* An empty name is not possible under normal circumstances rubidium@5341: * and causes problems when showing the NewGRF list. */ rubidium@5341: if (strlen(name) == 0) continue; rubidium@5341: rubidium@5339: /* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple. rubidium@5339: * If it exists and not resolved yet, then name of the fake GRF is rubidium@5339: * overwritten with the name from the reply. */ rubidium@5339: unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false); rubidium@5339: if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) { rubidium@5339: ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH); rubidium@5339: } rubidium@5339: } rubidium@5339: } rubidium@5339: truelight@543: truelight@543: // The layout for the receive-functions by UDP truelight@543: typedef void NetworkUDPPacket(Packet *p, struct sockaddr_in *client_addr); truelight@543: truelight@543: static NetworkUDPPacket* const _network_udp_packet[] = { truelight@638: RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER), truelight@543: RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE), truelight@638: RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO), truelight@638: NULL, truelight@668: NULL, truelight@764: RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER), truelight@764: NULL, truelight@765: RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST), rubidium@5339: NULL, rubidium@5339: RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS), rubidium@5339: RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS), truelight@543: }; truelight@543: truelight@764: truelight@543: // If this fails, check the array above with network_data.h truelight@543: assert_compile(lengthof(_network_udp_packet) == PACKET_UDP_END); truelight@543: truelight@543: tron@2817: static void NetworkHandleUDPPacket(Packet* p, struct sockaddr_in* client_addr) truelight@543: { truelight@543: byte type; truelight@543: truelight@903: /* Fake a client, so we can see when there is an illegal packet */ truelight@903: _udp_cs.socket = INVALID_SOCKET; Darkvater@4880: _udp_cs.has_quit = false; truelight@543: truelight@903: type = NetworkRecv_uint8(&_udp_cs, p); truelight@903: Darkvater@4880: if (type < PACKET_UDP_END && _network_udp_packet[type] != NULL && !_udp_cs.has_quit) { truelight@543: _network_udp_packet[type](p, client_addr); truelight@3547: } else { Darkvater@4880: if (!_udp_cs.has_quit) { Darkvater@5568: DEBUG(net, 0, "[udp] received invalid packet type %d", type); truelight@3547: } else { Darkvater@5568: DEBUG(net, 0, "[udp] received illegal packet"); truelight@3547: } truelight@543: } truelight@543: } truelight@543: truelight@543: truelight@543: // Send a packet over UDP tron@2817: static void NetworkSendUDP_Packet(SOCKET udp, Packet* p, struct sockaddr_in* recv) truelight@543: { truelight@543: int res; truelight@543: truelight@543: // Put the length in the buffer truelight@543: p->buffer[0] = p->size & 0xFF; truelight@543: p->buffer[1] = p->size >> 8; truelight@543: truelight@543: // Send the buffer truelight@543: res = sendto(udp, p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv)); truelight@543: Darkvater@5568: // Check for any errors, but ignore it otherwise Darkvater@5568: if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR()); truelight@543: } truelight@543: truelight@543: // Start UDP listener truelight@764: bool NetworkUDPListen(SOCKET *udp, uint32 host, uint16 port, bool broadcast) truelight@543: { truelight@543: struct sockaddr_in sin; truelight@543: truelight@764: // Make sure socket is closed truelight@764: closesocket(*udp); truelight@543: truelight@764: *udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); truelight@764: if (*udp == INVALID_SOCKET) { Darkvater@5568: DEBUG(net, 0, "[udp] failed to start UDP listener"); truelight@543: return false; truelight@543: } truelight@543: truelight@543: // set nonblocking mode for socket truelight@543: { truelight@543: unsigned long blocking = 1; bjarni@2497: #ifndef BEOS_NET_SERVER truelight@764: ioctlsocket(*udp, FIONBIO, &blocking); bjarni@2497: #else truelight@2524: setsockopt(*udp, SOL_SOCKET, SO_NONBLOCK, &blocking, NULL); bjarni@2497: #endif truelight@543: } truelight@543: truelight@543: sin.sin_family = AF_INET; truelight@543: // Listen on all IPs truelight@543: sin.sin_addr.s_addr = host; truelight@543: sin.sin_port = htons(port); truelight@543: truelight@764: if (bind(*udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) { Darkvater@5568: DEBUG(net, 0, "[udp] bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port); truelight@543: return false; truelight@543: } truelight@543: truelight@764: if (broadcast) { truelight@764: /* Enable broadcast */ truelight@543: unsigned long val = 1; bjarni@2497: #ifndef BEOS_NET_SERVER // will work around this, some day; maybe. truelight@764: setsockopt(*udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); bjarni@2497: #endif truelight@543: } truelight@543: Darkvater@5568: DEBUG(net, 1, "[udp] listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port); truelight@543: truelight@543: return true; truelight@543: } truelight@543: truelight@543: // Close UDP connection truelight@543: void NetworkUDPClose(void) truelight@543: { Darkvater@5568: DEBUG(net, 1, "[udp] closed listeners"); truelight@543: truelight@543: if (_network_udp_server) { truelight@985: if (_udp_server_socket != INVALID_SOCKET) { truelight@985: closesocket(_udp_server_socket); truelight@985: _udp_server_socket = INVALID_SOCKET; truelight@985: } truelight@764: truelight@985: if (_udp_master_socket != INVALID_SOCKET) { truelight@985: closesocket(_udp_master_socket); truelight@985: _udp_master_socket = INVALID_SOCKET; truelight@985: } truelight@764: truelight@543: _network_udp_server = false; truelight@543: _network_udp_broadcast = 0; truelight@543: } else { truelight@985: if (_udp_client_socket != INVALID_SOCKET) { truelight@985: closesocket(_udp_client_socket); truelight@985: _udp_client_socket = INVALID_SOCKET; truelight@985: } truelight@543: _network_udp_broadcast = 0; truelight@543: } truelight@543: } truelight@543: truelight@543: // Receive something on UDP level truelight@764: void NetworkUDPReceive(SOCKET udp) truelight@543: { truelight@543: struct sockaddr_in client_addr; tron@1343: socklen_t client_len; truelight@543: int nbytes; truelight@543: static Packet *p = NULL; truelight@543: int packet_len; truelight@543: truelight@543: // If p is NULL, malloc him.. this prevents unneeded mallocs tron@4077: if (p == NULL) p = malloc(sizeof(*p)); truelight@543: truelight@543: packet_len = sizeof(p->buffer); truelight@543: client_len = sizeof(client_addr); truelight@543: truelight@543: // Try to receive anything truelight@543: nbytes = recvfrom(udp, p->buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len); truelight@543: truelight@543: // We got some bytes.. just asume we receive the whole packet truelight@543: if (nbytes > 0) { truelight@543: // Get the size of the buffer truelight@543: p->size = (uint16)p->buffer[0]; truelight@543: p->size += (uint16)p->buffer[1] << 8; truelight@543: // Put the position on the right place truelight@543: p->pos = 2; truelight@543: p->next = NULL; truelight@543: truelight@543: // Handle the packet truelight@543: NetworkHandleUDPPacket(p, &client_addr); truelight@543: truelight@543: // Free the packet truelight@543: free(p); truelight@543: p = NULL; truelight@543: } truelight@543: } truelight@543: truelight@543: // Broadcast to all ips tron@2817: static void NetworkUDPBroadCast(SOCKET udp) truelight@543: { tron@4077: Packet* p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER); tron@4077: uint i; truelight@543: tron@4077: for (i = 0; _broadcast_list[i] != 0; i++) { tron@4077: struct sockaddr_in out_addr; truelight@543: truelight@543: out_addr.sin_family = AF_INET; truelight@543: out_addr.sin_port = htons(_network_server_port); tron@4034: out_addr.sin_addr.s_addr = _broadcast_list[i]; tron@4034: Darkvater@5568: DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr)); truelight@543: truelight@764: NetworkSendUDP_Packet(udp, p, &out_addr); truelight@543: } tron@4035: tron@4035: free(p); truelight@764: } truelight@764: truelight@764: truelight@764: // Request the the server-list from the master server truelight@764: void NetworkUDPQueryMasterServer(void) truelight@764: { truelight@764: struct sockaddr_in out_addr; truelight@764: Packet *p; truelight@764: truelight@764: if (_udp_client_socket == INVALID_SOCKET) truelight@764: if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) truelight@764: return; truelight@764: truelight@764: p = NetworkSend_Init(PACKET_UDP_CLIENT_GET_LIST); truelight@764: truelight@764: out_addr.sin_family = AF_INET; truelight@764: out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); truelight@764: out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); truelight@764: truelight@764: // packet only contains protocol version truelight@764: NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); truelight@764: truelight@764: NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr); truelight@764: Darkvater@5568: DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr),ntohs(out_addr.sin_port)); truelight@543: truelight@543: free(p); truelight@543: } truelight@543: truelight@543: // Find all servers truelight@543: void NetworkUDPSearchGame(void) truelight@543: { truelight@543: // We are still searching.. Darkvater@5568: if (_network_udp_broadcast > 0) return; truelight@543: truelight@543: // No UDP-socket yet.. truelight@543: if (_udp_client_socket == INVALID_SOCKET) truelight@764: if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) truelight@543: return; truelight@543: Darkvater@5568: DEBUG(net, 0, "[udp] searching server"); truelight@543: truelight@764: NetworkUDPBroadCast(_udp_client_socket); truelight@543: _network_udp_broadcast = 300; // Stay searching for 300 ticks truelight@543: } truelight@543: tron@1329: NetworkGameList *NetworkUDPQueryServer(const char* host, unsigned short port) truelight@543: { truelight@543: struct sockaddr_in out_addr; truelight@543: Packet *p; truelight@543: NetworkGameList *item; truelight@543: truelight@543: // No UDP-socket yet.. truelight@543: if (_udp_client_socket == INVALID_SOCKET) truelight@764: if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true)) dominik@738: return NULL; truelight@543: truelight@543: out_addr.sin_family = AF_INET; truelight@543: out_addr.sin_port = htons(port); truelight@543: out_addr.sin_addr.s_addr = NetworkResolveHost(host); truelight@543: truelight@543: // Clear item in gamelist truelight@543: item = NetworkGameListAddItem(inet_addr(inet_ntoa(out_addr.sin_addr)), ntohs(out_addr.sin_port)); truelight@543: memset(&item->info, 0, sizeof(item->info)); tron@3550: ttd_strlcpy(item->info.server_name, host, lengthof(item->info.server_name)); tron@3550: ttd_strlcpy(item->info.hostname, host, lengthof(item->info.hostname)); truelight@543: item->online = false; truelight@543: truelight@543: // Init the packet truelight@638: p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER); truelight@543: truelight@764: NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr); truelight@543: truelight@543: free(p); truelight@543: truelight@543: UpdateNetworkGameWindow(false); dominik@738: return item; truelight@543: } truelight@543: truelight@765: /* Remove our advertise from the master-server */ truelight@765: void NetworkUDPRemoveAdvertise(void) truelight@765: { truelight@765: struct sockaddr_in out_addr; truelight@765: Packet *p; truelight@765: truelight@765: /* Check if we are advertising */ Darkvater@5568: if (!_networking || !_network_server || !_network_udp_server) return; truelight@765: truelight@765: /* check for socket */ truelight@765: if (_udp_master_socket == INVALID_SOCKET) truelight@805: if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false)) truelight@765: return; truelight@765: Darkvater@5568: DEBUG(net, 1, "[udp] removing advertise from master server"); truelight@765: truelight@765: /* Find somewhere to send */ truelight@765: out_addr.sin_family = AF_INET; truelight@765: out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); truelight@765: out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); truelight@765: truelight@765: /* Send the packet */ truelight@765: p = NetworkSend_Init(PACKET_UDP_SERVER_UNREGISTER); truelight@765: /* Packet is: Version, server_port */ truelight@765: NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); truelight@765: NetworkSend_uint16(p, _network_server_port); truelight@765: NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr); truelight@765: truelight@765: free(p); truelight@765: } truelight@765: truelight@668: /* Register us to the master server truelight@668: This function checks if it needs to send an advertise */ truelight@716: void NetworkUDPAdvertise(void) truelight@668: { truelight@668: struct sockaddr_in out_addr; truelight@668: Packet *p; truelight@668: truelight@668: /* Check if we should send an advertise */ truelight@668: if (!_networking || !_network_server || !_network_udp_server || !_network_advertise) truelight@668: return; truelight@668: truelight@764: /* check for socket */ truelight@764: if (_udp_master_socket == INVALID_SOCKET) truelight@805: if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false)) truelight@764: return; truelight@764: peter1138@2861: if (_network_need_advertise) { peter1138@2861: _network_need_advertise = false; peter1138@2861: _network_advertise_retries = ADVERTISE_RETRY_TIMES; peter1138@2861: } else { peter1138@2861: /* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */ peter1138@2861: if (_network_advertise_retries == 0) { peter1138@2861: if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter) peter1138@2861: return; peter1138@2861: _network_advertise_retries = ADVERTISE_RETRY_TIMES; peter1138@2861: } peter1138@2861: peter1138@2861: if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter) truelight@764: return; truelight@764: } truelight@764: truelight@764: _network_advertise_retries--; peter1138@2861: _network_last_advertise_frame = _frame_counter; truelight@668: truelight@668: /* Find somewhere to send */ truelight@668: out_addr.sin_family = AF_INET; truelight@668: out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); truelight@668: out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); truelight@668: Darkvater@5568: DEBUG(net, 1, "[udp] advertising to master server"); truelight@668: truelight@668: /* Send the packet */ truelight@668: p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER); truelight@668: /* Packet is: WELCOME_MESSAGE, Version, server_port */ truelight@668: NetworkSend_string(p, NETWORK_MASTER_SERVER_WELCOME_MESSAGE); truelight@668: NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); truelight@668: NetworkSend_uint16(p, _network_server_port); truelight@764: NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr); truelight@764: truelight@668: free(p); truelight@668: } truelight@668: truelight@543: void NetworkUDPInitialize(void) truelight@543: { truelight@543: _udp_client_socket = INVALID_SOCKET; truelight@543: _udp_server_socket = INVALID_SOCKET; truelight@764: _udp_master_socket = INVALID_SOCKET; truelight@543: truelight@543: _network_udp_server = false; truelight@543: _network_udp_broadcast = 0; truelight@543: } truelight@543: truelight@543: #endif /* ENABLE_NETWORK */