src/network/network_udp.c
author rubidium
Tue, 09 Jan 2007 14:48:21 +0000
changeset 5572 a98ffa55b19c
parent 5552 527df7300907
permissions -rw-r--r--
(svn r8000) -Codechange: drop UDP packets when their internal size does not match the received size. If that is the case, the packet was not received in one piece (or got somehow mangled with another packet), which will cause us to drop the packet later on because we are (for example) trying to read beyond the end of the packet.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     2
4826
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4344
diff changeset
     3
#ifdef ENABLE_NETWORK
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4344
diff changeset
     4
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
     5
#include "../stdafx.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
     6
#include "../debug.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
     7
#include "../string.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     8
#include "network_data.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
     9
#include "../date.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    10
#include "../map.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    11
#include "network_gamelist.h"
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    12
#include "network_udp.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    13
#include "../variables.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    14
#include "../newgrf_config.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    15
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    16
#include "core/udp.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    17
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    18
/**
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    19
 * @file network_udp.c This file handles the UDP related communication.
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    20
 *
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    21
 * This is the GameServer <-> MasterServer and GameServer <-> GameClient
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    22
 * communication before the game is being joined.
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    23
 */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    24
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    25
enum {
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
    26
	ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes)
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    27
	ADVERTISE_RETRY_INTERVAL  =   300, // readvertise when no response after this many ticks (9 seconds)
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    28
	ADVERTISE_RETRY_TIMES     =     3  // give up readvertising after this much failed retries
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    29
};
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    30
5528
45a70016d39a (svn r7836) -Codechange: some constness for network/core.
rubidium
parents: 5521
diff changeset
    31
#define DEF_UDP_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(Packet *p, const struct sockaddr_in *client_addr)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    32
2789
12a6b123f12c (svn r3337) Remove an unused variable and add 2 times static
tron
parents: 2775
diff changeset
    33
static NetworkClientState _udp_cs;
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
    34
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
    35
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    36
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    37
	Packet *packet;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    38
	// Just a fail-safe.. should never happen
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    39
	if (!_network_udp_server)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    40
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    41
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
	packet = NetworkSend_Init(PACKET_UDP_SERVER_RESPONSE);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    43
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    44
	// Update some game_info
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    45
	_network_game_info.game_date     = _date;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    46
	_network_game_info.map_width     = MapSizeX();
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    47
	_network_game_info.map_height    = MapSizeY();
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    48
	_network_game_info.map_set       = _opt.landscape;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    49
	_network_game_info.companies_on  = ActivePlayerCount();
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    50
	_network_game_info.spectators_on = NetworkSpectatorCount();
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    51
	_network_game_info.grfconfig     = _grfconfig;
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    52
5517
f2f1b3e99f29 (svn r7818) -Fix (r7751): wrote some data to the wrong network packet.
rubidium
parents: 5475
diff changeset
    53
	NetworkSend_NetworkGameInfo(packet, &_network_game_info);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    54
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    55
	// Let the client know that we are here
5552
527df7300907 (svn r7932) -Fix (r7931): committed a few files too much :( (the ones I used to test the new masterserver)
rubidium
parents: 5551
diff changeset
    56
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    57
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    58
	free(packet);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    59
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
    60
	DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    61
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    62
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    63
void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    64
{
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    65
	/* Find the matching GRF file */
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    66
	const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum);
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    67
	if (f == NULL) {
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    68
		/* Don't know the GRF, so mark game incompatible and the (possibly)
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    69
		 * already resolved name for this GRF (another server has sent the
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    70
		 * name of the GRF already */
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    71
		config->name     = FindUnknownGRFName(config->grfid, config->md5sum, true);
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    72
		SETBIT(config->flags, GCF_NOT_FOUND);
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    73
	} else {
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    74
		config->filename = f->filename;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    75
		config->name     = f->name;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    76
		config->info     = f->info;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    77
	}
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    78
	SETBIT(config->flags, GCF_COPY);
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    79
}
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    80
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    81
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    82
{
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
    83
	extern const char _openttd_revision[];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    84
	NetworkGameList *item;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    85
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    86
	// Just a fail-safe.. should never happen
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    87
	if (_network_udp_server || _udp_cs.has_quit) return;
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    88
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
    89
	DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    90
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    91
	// Find next item
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    92
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    93
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    94
	NetworkRecv_NetworkGameInfo(&_udp_cs, p, &item->info);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    95
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    96
	item->info.compatible = true;
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    97
	{
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    98
		/* Checks whether there needs to be a request for names of GRFs and makes
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    99
		 * the request if necessary. GRFs that need to be requested are the GRFs
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   100
		 * that do not exist on the clients system and we do not have the name
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   101
		 * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   102
		 * The in_request array and in_request_count are used so there is no need
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   103
		 * to do a second loop over the GRF list, which can be relatively expensive
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   104
		 * due to the string comparisons. */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   105
		const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   106
		const GRFConfig *c;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   107
		uint in_request_count = 0;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   108
		struct sockaddr_in out_addr;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   109
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   110
		for (c = item->info.grfconfig; c != NULL; c = c->next) {
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   111
			if (HASBIT(c->flags, GCF_NOT_FOUND)) item->info.compatible = false;
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   112
			if (!HASBIT(c->flags, GCF_NOT_FOUND) || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   113
			in_request[in_request_count] = c;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   114
			in_request_count++;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   115
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   116
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   117
		if (in_request_count > 0) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   118
			/* There are 'unknown' GRFs, now send a request for them */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   119
			uint i;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   120
			Packet *packet = NetworkSend_Init(PACKET_UDP_CLIENT_GET_NEWGRFS);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   121
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   122
			NetworkSend_uint8 (packet, in_request_count);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   123
			for (i = 0; i < in_request_count; i++) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   124
				NetworkSend_GRFIdentifier(packet, in_request[i]);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   125
			}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   126
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   127
			out_addr.sin_family      = AF_INET;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   128
			out_addr.sin_port        = htons(item->port);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   129
			out_addr.sin_addr.s_addr = item->ip;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   130
			NetworkSendUDP_Packet(_udp_client_socket, packet, &out_addr);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   131
			free(packet);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   132
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   133
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   134
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   135
	if (item->info.server_lang >= NETWORK_NUM_LANGUAGES) item->info.server_lang = 0;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   136
	if (item->info.map_set >= NUM_LANDSCAPE ) item->info.map_set = 0;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   137
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   138
	if (item->info.hostname[0] == '\0')
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   139
		snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr));
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   140
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   141
	/* Check if we are allowed on this server based on the revision-match */
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   142
	item->info.version_compatible =
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   143
		strcmp(item->info.server_revision, _openttd_revision) == 0 ||
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   144
		strcmp(item->info.server_revision, NOREV_STRING) == 0;
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   145
	item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   146
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   147
	item->online = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   148
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   149
	UpdateNetworkGameWindow(false);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   150
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   151
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   152
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO)
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   153
{
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 668
diff changeset
   154
	NetworkClientState *cs;
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   155
	NetworkClientInfo *ci;
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   156
	Packet *packet;
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   157
	Player *player;
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   158
	byte current = 0;
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   159
	int i;
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   160
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   161
	// Just a fail-safe.. should never happen
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   162
	if (!_network_udp_server) return;
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   163
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   164
	packet = NetworkSend_Init(PACKET_UDP_SERVER_DETAIL_INFO);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   165
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   166
	/* Send the amount of active companies */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   167
	NetworkSend_uint8 (packet, NETWORK_COMPANY_INFO_VERSION);
2944
2360b2da2bb0 (svn r3500) - Workaround the inaccurate count of spectators/companies that can happen in certain border-cases. For now just dynamically get this value when requested so it is always right. To do properly all player/client creation/destruction needs a hook for networking.
Darkvater
parents: 2881
diff changeset
   168
	NetworkSend_uint8 (packet, ActivePlayerCount());
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   169
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   170
	/* Fetch the latest version of everything */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   171
	NetworkPopulateCompanyInfo();
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   172
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   173
	/* Go through all the players */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   174
	FOR_ALL_PLAYERS(player) {
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   175
		/* Skip non-active players */
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   176
		if (!player->is_active) continue;
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   177
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   178
		current++;
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   179
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   180
		/* Send the information */
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   181
		NetworkSend_uint8(packet, current);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   182
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   183
		NetworkSend_string(packet, _network_player_info[player->index].company_name);
4326
2e2c9d21ed96 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   184
		NetworkSend_uint32(packet, _network_player_info[player->index].inaugurated_year);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   185
		NetworkSend_uint64(packet, _network_player_info[player->index].company_value);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   186
		NetworkSend_uint64(packet, _network_player_info[player->index].money);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   187
		NetworkSend_uint64(packet, _network_player_info[player->index].income);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   188
		NetworkSend_uint16(packet, _network_player_info[player->index].performance);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   189
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   190
		/* Send 1 if there is a passord for the company else send 0 */
1011
933efc472c62 (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 985
diff changeset
   191
		if (_network_player_info[player->index].password[0] != '\0') {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   192
			NetworkSend_uint8(packet, 1);
1011
933efc472c62 (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 985
diff changeset
   193
		} else {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   194
			NetworkSend_uint8(packet, 0);
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1011
diff changeset
   195
		}
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1011
diff changeset
   196
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   197
		for (i = 0; i < NETWORK_VEHICLE_TYPES; i++)
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   198
			NetworkSend_uint16(packet, _network_player_info[player->index].num_vehicle[i]);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   199
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   200
		for (i = 0; i < NETWORK_STATION_TYPES; i++)
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   201
			NetworkSend_uint16(packet, _network_player_info[player->index].num_station[i]);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   202
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   203
		/* Find the clients that are connected to this player */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   204
		FOR_ALL_CLIENTS(cs) {
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   205
			ci = DEREF_CLIENT_INFO(cs);
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   206
			if (ci->client_playas == player->index) {
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   207
				/* The uint8 == 1 indicates that a client is following */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   208
				NetworkSend_uint8(packet, 1);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   209
				NetworkSend_string(packet, ci->client_name);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   210
				NetworkSend_string(packet, ci->unique_id);
4326
2e2c9d21ed96 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   211
				NetworkSend_uint32(packet, ci->join_date);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   212
			}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   213
		}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   214
		/* Also check for the server itself */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   215
		ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   216
		if (ci->client_playas == player->index) {
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   217
			/* The uint8 == 1 indicates that a client is following */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   218
			NetworkSend_uint8(packet, 1);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   219
			NetworkSend_string(packet, ci->client_name);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   220
			NetworkSend_string(packet, ci->unique_id);
4326
2e2c9d21ed96 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   221
			NetworkSend_uint32(packet, ci->join_date);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   222
		}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   223
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   224
		/* Indicates end of client list */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   225
		NetworkSend_uint8(packet, 0);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   226
	}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   227
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   228
	/* And check if we have any spectators */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   229
	FOR_ALL_CLIENTS(cs) {
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   230
		ci = DEREF_CLIENT_INFO(cs);
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   231
		if (!IsValidPlayer(ci->client_playas)) {
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   232
			/* The uint8 == 1 indicates that a client is following */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   233
			NetworkSend_uint8(packet, 1);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   234
			NetworkSend_string(packet, ci->client_name);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   235
			NetworkSend_string(packet, ci->unique_id);
4326
2e2c9d21ed96 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   236
			NetworkSend_uint32(packet, ci->join_date);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   237
		}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   238
	}
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   239
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   240
	/* Also check for the server itself */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   241
	ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   242
	if (!IsValidPlayer(ci->client_playas)) {
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   243
		/* The uint8 == 1 indicates that a client is following */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   244
		NetworkSend_uint8(packet, 1);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   245
		NetworkSend_string(packet, ci->client_name);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   246
		NetworkSend_string(packet, ci->unique_id);
4326
2e2c9d21ed96 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   247
		NetworkSend_uint32(packet, ci->join_date);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   248
	}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   249
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   250
	/* Indicates end of client list */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   251
	NetworkSend_uint8(packet, 0);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   252
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   253
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   254
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   255
	free(packet);
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   256
}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   257
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   258
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST)
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   259
{
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   260
	int i;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   261
	struct in_addr ip;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   262
	uint16 port;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   263
	uint8 ver;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   264
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   265
	/* packet begins with the protocol version (uint8)
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   266
	 * then an uint16 which indicates how many
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   267
	 * ip:port pairs are in this packet, after that
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   268
	 * an uint32 (ip) and an uint16 (port) for each pair
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   269
	 */
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   270
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   271
	ver = NetworkRecv_uint8(&_udp_cs, p);
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   272
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   273
	if (_udp_cs.has_quit) return;
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   274
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   275
	if (ver == 1) {
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   276
		for (i = NetworkRecv_uint16(&_udp_cs, p); i != 0 ; i--) {
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   277
			ip.s_addr = TO_LE32(NetworkRecv_uint32(&_udp_cs, p));
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   278
			port = NetworkRecv_uint16(&_udp_cs, p);
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   279
			NetworkUDPQueryServer(inet_ntoa(ip), port);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   280
		}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   281
	}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   282
}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   283
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   284
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER)
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   285
{
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   286
	_network_advertise_retries = 0;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   287
	DEBUG(net, 2, "[udp] advertising on master server successfull");
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   288
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   289
	/* We are advertised, but we don't want to! */
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   290
	if (!_network_advertise) NetworkUDPRemoveAdvertise();
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   291
}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   292
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   293
/**
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   294
 * A client has requested the names of some NewGRFs.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   295
 *
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   296
 * Replying this can be tricky as we have a limit of SEND_MTU bytes
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   297
 * in the reply packet and we can send up to 100 bytes per NewGRF
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   298
 * (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name).
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   299
 * As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   300
 * could be that a packet overflows. To stop this we only reply
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   301
 * with the first N NewGRFs so that if the first N + 1 NewGRFs
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   302
 * would be sent, the packet overflows.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   303
 * in_reply and in_reply_count are used to keep a list of GRFs to
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   304
 * send in the reply.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   305
 */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   306
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS)
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   307
{
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   308
	uint8 num_grfs;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   309
	uint i;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   310
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   311
	const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   312
	Packet *packet;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   313
	uint8 in_reply_count = 0;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   314
	uint packet_len = 0;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   315
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   316
	/* Just a fail-safe.. should never happen */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   317
	if (_udp_cs.has_quit) return;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   318
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   319
	DEBUG(net, 6, "[udp] newgrf data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   320
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   321
	num_grfs = NetworkRecv_uint8 (&_udp_cs, p);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   322
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   323
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   324
	for (i = 0; i < num_grfs; i++) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   325
		GRFConfig c;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   326
		const GRFConfig *f;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   327
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   328
		NetworkRecv_GRFIdentifier(&_udp_cs, p, &c);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   329
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   330
		/* Find the matching GRF file */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   331
		f = FindGRFConfig(c.grfid, c.md5sum);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   332
		if (f == NULL) continue; // The GRF is unknown to this server
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   333
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   334
		/* If the reply might exceed the size of the packet, only reply
5341
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   335
		 * the current list and do not send the other data.
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   336
		 * The name could be an empty string, if so take the filename. */
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   337
		packet_len += sizeof(c.grfid) + sizeof(c.md5sum) +
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   338
				min(strlen((f->name != NULL && strlen(f->name) > 0) ? f->name : f->filename) + 1, NETWORK_GRF_NAME_LENGTH);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   339
		if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   340
			break;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   341
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   342
		in_reply[in_reply_count] = f;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   343
		in_reply_count++;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   344
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   345
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   346
	if (in_reply_count == 0) return;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   347
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   348
	packet = NetworkSend_Init(PACKET_UDP_SERVER_NEWGRFS);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   349
	NetworkSend_uint8 (packet, in_reply_count);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   350
	for (i = 0; i < in_reply_count; i++) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   351
		char name[NETWORK_GRF_NAME_LENGTH];
5341
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   352
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   353
		/* The name could be an empty string, if so take the filename */
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   354
		ttd_strlcpy(name, (in_reply[i]->name != NULL && strlen(in_reply[i]->name) > 0) ?
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   355
				in_reply[i]->name : in_reply[i]->filename, sizeof(name));
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   356
	 	NetworkSend_GRFIdentifier(packet, in_reply[i]);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   357
		NetworkSend_string(packet, name);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   358
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   359
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   360
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   361
	free(packet);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   362
}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   363
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   364
/** The return of the client's request of the names of some NewGRFs */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   365
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS)
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   366
{
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   367
	uint8 num_grfs;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   368
	uint i;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   369
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   370
	/* Just a fail-safe.. should never happen */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   371
	if (_udp_cs.has_quit) return;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   372
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   373
	DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   374
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   375
	num_grfs = NetworkRecv_uint8 (&_udp_cs, p);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   376
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   377
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   378
	for (i = 0; i < num_grfs; i++) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   379
		char *unknown_name;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   380
		char name[NETWORK_GRF_NAME_LENGTH];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   381
		GRFConfig c;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   382
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
   383
		NetworkRecv_GRFIdentifier(&_udp_cs, p, &c);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   384
		NetworkRecv_string(&_udp_cs, p, name, sizeof(name));
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   385
5341
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   386
		/* An empty name is not possible under normal circumstances
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   387
		 * and causes problems when showing the NewGRF list. */
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   388
		if (strlen(name) == 0) continue;
02fb6f89fc8a (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   389
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   390
		/* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   391
		 * If it exists and not resolved yet, then name of the fake GRF is
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   392
		 * overwritten with the name from the reply. */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   393
		unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   394
		if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   395
			ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   396
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   397
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   398
}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   399
5519
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   400
/**
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   401
 * Every type of UDP packet should only be received by a single socket;
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   402
 * The socket communicating with the masterserver should receive the
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   403
 * game information of some 'random' host.
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   404
 */
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   405
typedef struct NetworkUDPPacketAndSocket {
5528
45a70016d39a (svn r7836) -Codechange: some constness for network/core.
rubidium
parents: 5521
diff changeset
   406
	void (*callback)(Packet *p, const struct sockaddr_in *client_addr);
5519
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   407
	SOCKET *incoming_socket;
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   408
} NetworkUPDPacketAndSocket;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   409
5519
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   410
static const NetworkUPDPacketAndSocket _network_udp_packet[PACKET_UDP_END] = {
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   411
	{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER),   &_udp_server_socket },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   412
	{ RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE),      &_udp_client_socket },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   413
	{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO),   &_udp_server_socket },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   414
	{ NULL,                                             NULL                },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   415
	{ NULL,                                             NULL                },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   416
	{ RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER),  &_udp_master_socket },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   417
	{ NULL,                                             NULL                },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   418
	{ RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST), &_udp_client_socket },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   419
	{ NULL,                                             NULL                },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   420
	{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS),   &_udp_server_socket },
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   421
	{ RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS),       &_udp_client_socket },
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   422
};
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   423
5528
45a70016d39a (svn r7836) -Codechange: some constness for network/core.
rubidium
parents: 5521
diff changeset
   424
void NetworkHandleUDPPacket(const SOCKET udp, Packet *p, const struct sockaddr_in *client_addr)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   425
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   426
	byte type;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   427
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   428
	/* Fake a client, so we can see when there is an illegal packet */
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   429
	_udp_cs.socket = INVALID_SOCKET;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   430
	_udp_cs.has_quit = false;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   431
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   432
	type = NetworkRecv_uint8(&_udp_cs, p);
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   433
5519
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   434
	if (type < PACKET_UDP_END && *_network_udp_packet[type].incoming_socket == udp && !_udp_cs.has_quit) {
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   435
		_network_udp_packet[type].callback(p, client_addr);
3547
fea14ff86f9e (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   436
	} else {
5519
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   437
		if (*_network_udp_packet[type].incoming_socket != udp) {
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   438
			DEBUG(net, 0, "[udp] received packet on wrong port from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   439
		} else if (!_udp_cs.has_quit) {
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   440
			DEBUG(net, 0, "[udp] received invalid packet type %d from %s:%d", type,  inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
3547
fea14ff86f9e (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   441
		} else {
5519
4b801fb78e52 (svn r7821) -Fix: be more strict about the socket from where packets arrive. Do not accept requests about the game server on the master/client socket, do not accept master server acks on the client/server socket, etc.
rubidium
parents: 5517
diff changeset
   442
			DEBUG(net, 0, "[udp] received illegal packet from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
3547
fea14ff86f9e (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   443
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   444
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   445
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   446
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   447
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   448
// Close UDP connection
5521
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   449
void NetworkUDPStop(void)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   450
{
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   451
	DEBUG(net, 1, "[udp] closed listeners");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   452
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   453
	if (_network_udp_server) {
5521
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   454
		NetworkUDPClose(&_udp_server_socket);
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   455
		NetworkUDPClose(&_udp_master_socket);
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   456
	} else {
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   457
		NetworkUDPClose(&_udp_client_socket);
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   458
	}
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   459
5521
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   460
	_network_udp_server = false;
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   461
	_network_udp_broadcast = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   462
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   463
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   464
// Broadcast to all ips
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
   465
static void NetworkUDPBroadCast(SOCKET udp)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   466
{
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   467
	Packet* p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   468
	uint i;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   469
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   470
	for (i = 0; _broadcast_list[i] != 0; i++) {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   471
		struct sockaddr_in out_addr;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   472
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   473
		out_addr.sin_family = AF_INET;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   474
		out_addr.sin_port = htons(_network_server_port);
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4026
diff changeset
   475
		out_addr.sin_addr.s_addr = _broadcast_list[i];
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4026
diff changeset
   476
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   477
		DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   478
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   479
		NetworkSendUDP_Packet(udp, p, &out_addr);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   480
	}
4035
e3280e6deef7 (svn r5294) -Fix: Plug a memory leak
tron
parents: 4034
diff changeset
   481
e3280e6deef7 (svn r5294) -Fix: Plug a memory leak
tron
parents: 4034
diff changeset
   482
	free(p);
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   483
}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   484
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   485
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   486
// Request the the server-list from the master server
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   487
void NetworkUDPQueryMasterServer(void)
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   488
{
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   489
	struct sockaddr_in out_addr;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   490
	Packet *p;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   491
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   492
	if (_udp_client_socket == INVALID_SOCKET)
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   493
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   494
			return;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   495
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   496
	p = NetworkSend_Init(PACKET_UDP_CLIENT_GET_LIST);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   497
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   498
	out_addr.sin_family = AF_INET;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   499
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   500
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   501
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   502
	// packet only contains protocol version
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   503
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   504
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   505
	NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   506
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   507
	DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr),ntohs(out_addr.sin_port));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   508
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   509
	free(p);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   510
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   511
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   512
// Find all servers
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   513
void NetworkUDPSearchGame(void)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   514
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   515
	// We are still searching..
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   516
	if (_network_udp_broadcast > 0) return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   517
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   518
	// No UDP-socket yet..
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   519
	if (_udp_client_socket == INVALID_SOCKET)
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   520
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   521
			return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   522
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   523
	DEBUG(net, 0, "[udp] searching server");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   524
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   525
	NetworkUDPBroadCast(_udp_client_socket);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   526
	_network_udp_broadcast = 300; // Stay searching for 300 ticks
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   527
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   528
1329
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   529
NetworkGameList *NetworkUDPQueryServer(const char* host, unsigned short port)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   530
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   531
	struct sockaddr_in out_addr;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   532
	Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   533
	NetworkGameList *item;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   534
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   535
	// No UDP-socket yet..
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   536
	if (_udp_client_socket == INVALID_SOCKET)
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   537
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
   538
			return NULL;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   539
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   540
	out_addr.sin_family = AF_INET;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   541
	out_addr.sin_port = htons(port);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   542
	out_addr.sin_addr.s_addr = NetworkResolveHost(host);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   544
	// Clear item in gamelist
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   545
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(out_addr.sin_addr)), ntohs(out_addr.sin_port));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   546
	memset(&item->info, 0, sizeof(item->info));
3550
1c07423d9bf2 (svn r4416) Remove a pointless buffer copy and use strlcpy() instead of snprintf("%s")
tron
parents: 3547
diff changeset
   547
	ttd_strlcpy(item->info.server_name, host, lengthof(item->info.server_name));
1c07423d9bf2 (svn r4416) Remove a pointless buffer copy and use strlcpy() instead of snprintf("%s")
tron
parents: 3547
diff changeset
   548
	ttd_strlcpy(item->info.hostname, host, lengthof(item->info.hostname));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   549
	item->online = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   550
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   551
	// Init the packet
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   552
	p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   553
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   554
	NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   555
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   556
	free(p);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   557
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   558
	UpdateNetworkGameWindow(false);
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
   559
	return item;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   560
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   561
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   562
/* Remove our advertise from the master-server */
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   563
void NetworkUDPRemoveAdvertise(void)
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   564
{
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   565
	struct sockaddr_in out_addr;
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   566
	Packet *p;
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   567
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   568
	/* Check if we are advertising */
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   569
	if (!_networking || !_network_server || !_network_udp_server) return;
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   570
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   571
	/* check for socket */
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   572
	if (_udp_master_socket == INVALID_SOCKET)
805
175b92d3c196 (svn r1276) -Fix: [Network] Bug in bind system. Advertising failed on systems with
truelight
parents: 765
diff changeset
   573
		if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false))
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   574
			return;
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   575
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   576
	DEBUG(net, 1, "[udp] removing advertise from master server");
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   577
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   578
	/* Find somewhere to send */
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   579
	out_addr.sin_family = AF_INET;
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   580
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   581
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   582
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   583
	/* Send the packet */
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   584
	p = NetworkSend_Init(PACKET_UDP_SERVER_UNREGISTER);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   585
	/* Packet is: Version, server_port */
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   586
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   587
	NetworkSend_uint16(p, _network_server_port);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   588
	NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   589
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   590
	free(p);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   591
}
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   592
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   593
/* Register us to the master server
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   594
     This function checks if it needs to send an advertise */
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 668
diff changeset
   595
void NetworkUDPAdvertise(void)
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   596
{
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   597
	struct sockaddr_in out_addr;
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   598
	Packet *p;
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   599
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   600
	/* Check if we should send an advertise */
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   601
	if (!_networking || !_network_server || !_network_udp_server || !_network_advertise)
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   602
		return;
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   603
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   604
	/* check for socket */
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   605
	if (_udp_master_socket == INVALID_SOCKET)
805
175b92d3c196 (svn r1276) -Fix: [Network] Bug in bind system. Advertising failed on systems with
truelight
parents: 765
diff changeset
   606
		if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false))
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   607
			return;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   608
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   609
	if (_network_need_advertise) {
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   610
		_network_need_advertise = false;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   611
		_network_advertise_retries = ADVERTISE_RETRY_TIMES;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   612
	} else {
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   613
		/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   614
		if (_network_advertise_retries == 0) {
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   615
			if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter)
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   616
				return;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   617
			_network_advertise_retries = ADVERTISE_RETRY_TIMES;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   618
		}
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   619
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   620
		if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter)
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   621
			return;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   622
	}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   623
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   624
	_network_advertise_retries--;
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   625
	_network_last_advertise_frame = _frame_counter;
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   626
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   627
	/* Find somewhere to send */
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   628
	out_addr.sin_family = AF_INET;
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   629
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   630
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   631
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   632
	DEBUG(net, 1, "[udp] advertising to master server");
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   633
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   634
	/* Send the packet */
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   635
	p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   636
	/* Packet is: WELCOME_MESSAGE, Version, server_port */
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   637
	NetworkSend_string(p, NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   638
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   639
	NetworkSend_uint16(p, _network_server_port);
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   640
	NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   641
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   642
	free(p);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   643
}
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   644
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   645
void NetworkUDPInitialize(void)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   646
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   647
	_udp_client_socket = INVALID_SOCKET;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   648
	_udp_server_socket = INVALID_SOCKET;
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   649
	_udp_master_socket = INVALID_SOCKET;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   650
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   651
	_network_udp_server = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   652
	_network_udp_broadcast = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   653
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   654
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   655
#endif /* ENABLE_NETWORK */