Changeset - r5619:40c6c3e3d5f7
[Not reviewed]
master
0 8 0
rubidium - 18 years ago 2007-01-12 14:30:01
rubidium@openttd.org
(svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
8 files changed with 392 insertions and 324 deletions:
0 comments (0 inline, 0 general)
src/network/core/udp.cpp
Show inline comments
 
@@ -10,115 +10,123 @@
 
#include "udp.h"
 

	
 
/**
 
 * @file udp.c Basic functions to receive and send UDP packets.
 
 */
 

	
 
/** Initialize the sockets with an INVALID_SOCKET */
 
NetworkUDPSocketHandler::NetworkUDPSocketHandler()
 
{
 
	this->udp = INVALID_SOCKET;
 
}
 

	
 
/**
 
 * Start listening on the given host and port.
 
 * @param udp       the place where the (references to the) UDP are stored
 
 * @param host      the host (ip) to listen on
 
 * @param port      the port to listen on
 
 * @param broadcast whether to allow broadcast sending/receiving
 
 * @return true if the listening succeeded
 
 */
 
bool NetworkUDPListen(SOCKET *udp, const uint32 host, const uint16 port, const bool broadcast)
 
bool NetworkUDPSocketHandler::Listen(const uint32 host, const uint16 port, const bool broadcast)
 
{
 
	struct sockaddr_in sin;
 

	
 
	/* Make sure socket is closed */
 
	NetworkUDPClose(udp);
 
	this->Close();
 

	
 
	*udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 
	if (*udp == INVALID_SOCKET) {
 
	this->udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 
	if (this->udp == INVALID_SOCKET) {
 
		DEBUG(net, 0, "[udp] failed to start UDP listener");
 
		return false;
 
	}
 

	
 
	/* set nonblocking mode for socket */
 
	{
 
		unsigned long blocking = 1;
 
#ifndef BEOS_NET_SERVER
 
		ioctlsocket(*udp, FIONBIO, &blocking);
 
		ioctlsocket(this->udp, FIONBIO, &blocking);
 
#else
 
		setsockopt(*udp, SOL_SOCKET, SO_NONBLOCK, &blocking, NULL);
 
		setsockopt(this->udp, SOL_SOCKET, SO_NONBLOCK, &blocking, NULL);
 
#endif
 
	}
 

	
 
	sin.sin_family = AF_INET;
 
	/* Listen on all IPs */
 
	sin.sin_addr.s_addr = host;
 
	sin.sin_port = htons(port);
 

	
 
	if (bind(*udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
 
	if (bind(this->udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
 
		DEBUG(net, 0, "[udp] bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port);
 
		return false;
 
	}
 

	
 
	if (broadcast) {
 
		/* Enable broadcast */
 
		unsigned long val = 1;
 
#ifndef BEOS_NET_SERVER // will work around this, some day; maybe.
 
		setsockopt(*udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val));
 
		setsockopt(this->udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val));
 
#endif
 
	}
 

	
 
	DEBUG(net, 1, "[udp] listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port);
 

	
 
	return true;
 
}
 

	
 
/**
 
 * Close the given UDP socket
 
 * @param udp the socket to close
 
 */
 
void NetworkUDPClose(SOCKET *udp)
 
void NetworkUDPSocketHandler::Close()
 
{
 
	if (*udp == INVALID_SOCKET) return;
 
	if (this->udp == INVALID_SOCKET) return;
 

	
 
	closesocket(*udp);
 
	*udp = INVALID_SOCKET;
 
	closesocket(this->udp);
 
	this->udp = INVALID_SOCKET;
 
}
 

	
 

	
 
/**
 
 * Send a packet over UDP
 
 * @param udp  the socket to send over
 
 * @param p    the packet to send
 
 * @param recv the receiver (target) of the packet
 
 */
 
void NetworkSendUDP_Packet(const SOCKET udp, Packet *p, const struct sockaddr_in *recv)
 
void NetworkUDPSocketHandler::SendPacket(Packet *p, const struct sockaddr_in *recv)
 
{
 
	int res;
 

	
 
	NetworkSend_FillPacketSize(p);
 

	
 
	/* Send the buffer */
 
	res = sendto(udp, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
 
	res = sendto(this->udp, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
 

	
 
	/* Check for any errors, but ignore it otherwise */
 
	if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
 
}
 

	
 
/**
 
 * Receive a packet at UDP level
 
 * @param udp the socket to receive the packet on
 
 */
 
void NetworkUDPReceive(const SOCKET udp)
 
void NetworkUDPSocketHandler::ReceivePackets()
 
{
 
	struct sockaddr_in client_addr;
 
	socklen_t client_len;
 
	int nbytes;
 
	Packet p;
 
	int packet_len;
 

	
 
	if (this->udp == INVALID_SOCKET) return;
 

	
 
	packet_len = sizeof(p.buffer);
 
	client_len = sizeof(client_addr);
 

	
 
	/* Try to receive anything */
 
	nbytes = recvfrom(udp, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
 
	nbytes = recvfrom(this->udp, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
 

	
 
	/* We got some bytes for the base header of the packet. */
 
	if (nbytes > 2) {
 
		NetworkRecv_ReadPacketSize(&p);
 

	
 
		/* If the size does not match the packet must be corrupted.
 
@@ -132,53 +140,52 @@ void NetworkUDPReceive(const SOCKET udp)
 

	
 
		/* Put the position on the right place */
 
		p.pos = 2;
 
		p.next = NULL;
 

	
 
		/* Handle the packet */
 
		NetworkHandleUDPPacket(udp, &p, &client_addr);
 
		this->HandleUDPPacket(&p, &client_addr);
 
	}
 
}
 

	
 

	
 
/**
 
 * Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet
 
 * @param p the packet to write the data to
 
 * @param c the configuration to write the GRF ID and MD5 checksum from
 
 */
 
void NetworkSend_GRFIdentifier(Packet *p, const GRFConfig *c)
 
void NetworkUDPSocketHandler::Send_GRFIdentifier(Packet *p, const GRFConfig *c)
 
{
 
	uint j;
 
	NetworkSend_uint32(p, c->grfid);
 
	for (j = 0; j < sizeof(c->md5sum); j++) {
 
		NetworkSend_uint8 (p, c->md5sum[j]);
 
	}
 
}
 

	
 
/**
 
 * Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet
 
 * @param cs the client state (for closing connect on out-of-bounds reading etc)
 
 * @param p  the packet to read the data from
 
 * @param c  the configuration to write the GRF ID and MD5 checksum to
 
 */
 
void NetworkRecv_GRFIdentifier(NetworkClientState *cs, Packet *p, GRFConfig *c)
 
void NetworkUDPSocketHandler::Recv_GRFIdentifier(Packet *p, GRFConfig *c)
 
{
 
	uint j;
 
	c->grfid = NetworkRecv_uint32(cs, p);
 
	c->grfid = NetworkRecv_uint32(&this->cs, p);
 
	for (j = 0; j < sizeof(c->md5sum); j++) {
 
		c->md5sum[j] = NetworkRecv_uint8(cs, p);
 
		c->md5sum[j] = NetworkRecv_uint8(&this->cs, p);
 
	}
 
}
 

	
 

	
 
/**
 
 * Serializes the NetworkGameInfo struct to the packet
 
 * @param p    the packet to write the data to
 
 * @param info the NetworkGameInfo struct to serialize
 
 */
 
void NetworkSend_NetworkGameInfo(Packet *p, const NetworkGameInfo *info)
 
void NetworkUDPSocketHandler::Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info)
 
{
 
	NetworkSend_uint8 (p, NETWORK_GAME_INFO_VERSION);
 

	
 
	/*
 
	 *              Please observe the order.
 
	 * The parts must be read in the same order as they are sent!
 
@@ -201,13 +208,13 @@ void NetworkSend_NetworkGameInfo(Packet 
 
			if (!HASBIT(c->flags, GCF_STATIC)) count++;
 
		}
 
		NetworkSend_uint8 (p, count); // Send number of GRFs
 

	
 
		/* Send actual GRF Identifications */
 
		for (c = info->grfconfig; c != NULL; c = c->next) {
 
			if (!HASBIT(c->flags, GCF_STATIC)) NetworkSend_GRFIdentifier(p, c);
 
			if (!HASBIT(c->flags, GCF_STATIC)) this->Send_GRFIdentifier(p, c);
 
		}
 
	}
 

	
 
	/* NETWORK_GAME_INFO_VERSION = 3 */
 
	NetworkSend_uint32(p, info->game_date);
 
	NetworkSend_uint32(p, info->start_date);
 
@@ -231,19 +238,18 @@ void NetworkSend_NetworkGameInfo(Packet 
 
	NetworkSend_uint8 (p, info->map_set);
 
	NetworkSend_uint8 (p, info->dedicated);
 
}
 

	
 
/**
 
 * Deserializes the NetworkGameInfo struct from the packet
 
 * @param cs   the client state (for closing connect on out-of-bounds reading etc)
 
 * @param p    the packet to read the data from
 
 * @param info the NetworkGameInfo to deserialize into
 
 */
 
void NetworkRecv_NetworkGameInfo(NetworkClientState *cs, Packet *p, NetworkGameInfo *info)
 
void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *info)
 
{
 
	info->game_info_version = NetworkRecv_uint8(cs, p);
 
	info->game_info_version = NetworkRecv_uint8(&this->cs, p);
 

	
 
	/*
 
	 *              Please observe the order.
 
	 * The parts must be read in the same order as they are sent!
 
	 */
 

	
 
@@ -251,48 +257,114 @@ void NetworkRecv_NetworkGameInfo(Network
 
	 * to the NetworkGameInfo wire-protocol! */
 

	
 
	switch (info->game_info_version) {
 
		case 4: {
 
			GRFConfig **dst = &info->grfconfig;
 
			uint i;
 
			uint num_grfs = NetworkRecv_uint8(cs, p);
 
			uint num_grfs = NetworkRecv_uint8(&this->cs, p);
 

	
 
			for (i = 0; i < num_grfs; i++) {
 
				GRFConfig *c = CallocT<GRFConfig>(1);
 
				NetworkRecv_GRFIdentifier(cs, p, c);
 
				HandleIncomingNetworkGameInfoGRFConfig(c);
 
				this->Recv_GRFIdentifier(p, c);
 
				this->HandleIncomingNetworkGameInfoGRFConfig(c);
 

	
 
				/* Append GRFConfig to the list */
 
				*dst = c;
 
				dst = &c->next;
 
			}
 
		} /* Fallthrough */
 
		case 3:
 
			info->game_date      = NetworkRecv_uint32(cs, p);
 
			info->start_date     = NetworkRecv_uint32(cs, p);
 
			info->game_date      = NetworkRecv_uint32(&this->cs, p);
 
			info->start_date     = NetworkRecv_uint32(&this->cs, p);
 
			/* Fallthrough */
 
		case 2:
 
			info->companies_max  = NetworkRecv_uint8 (cs, p);
 
			info->companies_on   = NetworkRecv_uint8 (cs, p);
 
			info->spectators_max = NetworkRecv_uint8 (cs, p);
 
			info->companies_max  = NetworkRecv_uint8 (&this->cs, p);
 
			info->companies_on   = NetworkRecv_uint8 (&this->cs, p);
 
			info->spectators_max = NetworkRecv_uint8 (&this->cs, p);
 
			/* Fallthrough */
 
		case 1:
 
			NetworkRecv_string(cs, p, info->server_name,     sizeof(info->server_name));
 
			NetworkRecv_string(cs, p, info->server_revision, sizeof(info->server_revision));
 
			info->server_lang    = NetworkRecv_uint8 (cs, p);
 
			info->use_password   = NetworkRecv_uint8 (cs, p);
 
			info->clients_max    = NetworkRecv_uint8 (cs, p);
 
			info->clients_on     = NetworkRecv_uint8 (cs, p);
 
			info->spectators_on  = NetworkRecv_uint8 (cs, p);
 
			NetworkRecv_string(&this->cs, p, info->server_name,     sizeof(info->server_name));
 
			NetworkRecv_string(&this->cs, p, info->server_revision, sizeof(info->server_revision));
 
			info->server_lang    = NetworkRecv_uint8 (&this->cs, p);
 
			info->use_password   = NetworkRecv_uint8 (&this->cs, p);
 
			info->clients_max    = NetworkRecv_uint8 (&this->cs, p);
 
			info->clients_on     = NetworkRecv_uint8 (&this->cs, p);
 
			info->spectators_on  = NetworkRecv_uint8 (&this->cs, p);
 
			if (info->game_info_version < 3) { // 16 bits dates got scrapped and are read earlier
 
				info->game_date    = NetworkRecv_uint16(cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
 
				info->start_date   = NetworkRecv_uint16(cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
 
				info->game_date    = NetworkRecv_uint16(&this->cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
 
				info->start_date   = NetworkRecv_uint16(&this->cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
 
			}
 
			NetworkRecv_string(cs, p, info->map_name, sizeof(info->map_name));
 
			info->map_width      = NetworkRecv_uint16(cs, p);
 
			info->map_height     = NetworkRecv_uint16(cs, p);
 
			info->map_set        = NetworkRecv_uint8 (cs, p);
 
			info->dedicated      = (NetworkRecv_uint8 (cs, p) != 0);
 
			NetworkRecv_string(&this->cs, p, info->map_name, sizeof(info->map_name));
 
			info->map_width      = NetworkRecv_uint16(&this->cs, p);
 
			info->map_height     = NetworkRecv_uint16(&this->cs, p);
 
			info->map_set        = NetworkRecv_uint8 (&this->cs, p);
 
			info->dedicated      = (NetworkRecv_uint8(&this->cs, p) != 0);
 
	}
 
}
 

	
 
/* Defines a simple (switch) case for each network packet */
 
#define UDP_COMMAND(type) case type: this->NetworkPacketReceive_ ## type ## _command(p, client_addr); break;
 

	
 
/**
 
 * Handle an incoming packets by sending it to the correct function.
 
 * @param p the received packet
 
 * @param client_addr the sender of the packet
 
 */
 
void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_in *client_addr)
 
{
 
	PacketUDPType type;
 

	
 
	/* Fake a client, so we can see when there is an illegal packet */
 
	this->cs.socket = INVALID_SOCKET;
 
	this->cs.has_quit = false;
 

	
 
	type = (PacketUDPType)NetworkRecv_uint8(&this->cs, p);
 

	
 
	switch (this->cs.has_quit ? PACKET_UDP_END : type) {
 
		UDP_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
 
		UDP_COMMAND(PACKET_UDP_SERVER_RESPONSE);
 
		UDP_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
 
		UDP_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
 
		UDP_COMMAND(PACKET_UDP_SERVER_REGISTER);
 
		UDP_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
 
		UDP_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
 
		UDP_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
 
		UDP_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
 
		UDP_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
 
		UDP_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
 

	
 
		default:
 
			if (!this->cs.has_quit) {
 
				DEBUG(net, 0, "[udp] received invalid packet type %d from %s:%d", type,  inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
 
			} else {
 
				DEBUG(net, 0, "[udp] received illegal packet from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
 
			}
 
			break;
 
	}
 
}
 

	
 
/*
 
 * Create stub implementations for all receive commands that only
 
 * show a warning that the given command is not available for the
 
 * socket where the packet came from.
 
 */
 

	
 
#define DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(type) \
 
void NetworkUDPSocketHandler::NetworkPacketReceive_## type ##_command(\
 
		Packet *p, const struct sockaddr_in *client_addr) { \
 
	DEBUG(net, 0, "[udp] received packet on wrong port from %s:%d", \
 
			inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); \
 
}
 

	
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_REGISTER);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
 
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
 

	
 
#endif /* ENABLE_NETWORK */
src/network/core/udp.h
Show inline comments
 
@@ -6,12 +6,14 @@
 
#ifdef ENABLE_NETWORK
 

	
 
#include "os_abstraction.h"
 
#include "game.h"
 
#include "packet.h"
 
#include "../../newgrf_config.h"
 
#include "../../debug.h"
 
#include "../network_data.h"
 

	
 
/**
 
 * @file udp.h Basic functions to receive and send UDP packets.
 
 *
 
 *
 
 * *** Requesting game information from a server ***
 
@@ -68,34 +70,14 @@
 
 *   1+       2       height of the map in tiles
 
 *   1+       1       type of map:
 
 *                    (0 = temperate, 1 = arctic, 2 = desert, 3 = toyland)
 
 *   1+       1       whether the server is dedicated (0 = no, 1 = yes)
 
 */
 

	
 
///** Sending/receiving of UDP packets **////
 

	
 
bool NetworkUDPListen(SOCKET *udp, const uint32 host, const uint16 port, const bool broadcast);
 
void NetworkUDPClose(SOCKET *udp);
 

	
 
void NetworkSendUDP_Packet(const SOCKET udp, Packet *p, const struct sockaddr_in *recv);
 
void NetworkUDPReceive(const SOCKET udp);
 

	
 
/**
 
 * Function that is called for every received UDP packet.
 
 * @param udp         the socket the packet is received on
 
 * @param packet      the received packet
 
 * @param client_addr the address of the sender of the packet
 
 */
 
void NetworkHandleUDPPacket(const SOCKET udp, Packet *p, const struct sockaddr_in *client_addr);
 

	
 

	
 
///** Sending/receiving of (large) chuncks of UDP packets **////
 

	
 

	
 
/** Enum with all types of UDP packets. The order MUST not be changed **/
 
enum {
 
enum PacketUDPType {
 
	PACKET_UDP_CLIENT_FIND_SERVER,   ///< Queries a game server for game information
 
	PACKET_UDP_SERVER_RESPONSE,      ///< Reply of the game server with game information
 
	PACKET_UDP_CLIENT_DETAIL_INFO,   ///< Queries a game server about details of the game, such as companies
 
	PACKET_UDP_SERVER_DETAIL_INFO,   ///< Reply of the game server about details of the game, such as companies
 
	PACKET_UDP_SERVER_REGISTER,      ///< Packet to register itself to the master server
 
	PACKET_UDP_MASTER_ACK_REGISTER,  ///< Packet indicating registration has succedeed
 
@@ -104,24 +86,58 @@ enum {
 
	PACKET_UDP_SERVER_UNREGISTER,    ///< Request to be removed from the server-list
 
	PACKET_UDP_CLIENT_GET_NEWGRFS,   ///< Requests the name for a list of GRFs (GRF_ID and MD5)
 
	PACKET_UDP_SERVER_NEWGRFS,       ///< Sends the list of NewGRF's requested.
 
	PACKET_UDP_END                   ///< Must ALWAYS be on the end of this list!! (period)
 
};
 

	
 
void NetworkSend_GRFIdentifier(Packet *p, const GRFConfig *c);
 
void NetworkSend_NetworkGameInfo(Packet *p, const NetworkGameInfo *info);
 
#define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, const struct sockaddr_in *)
 

	
 
void NetworkRecv_GRFIdentifier(NetworkClientState *cs, Packet *p, GRFConfig *c);
 
void NetworkRecv_NetworkGameInfo(NetworkClientState *cs, Packet *p, NetworkGameInfo *info);
 
class NetworkUDPSocketHandler {
 
private:
 
	SOCKET udp;
 
protected:
 
	NetworkClientState cs;
 
	/* Declare all possible packets here. If it can be received by the
 
	 * a specific handler, it has to be implemented. */
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_REGISTER);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
 

	
 
	void HandleUDPPacket(Packet *p, const struct sockaddr_in *client_addr);
 

	
 
/**
 
 * Function that is called for every GRFConfig that is read when receiving
 
 * a NetworkGameInfo. Only grfid and md5sum are set, the rest is zero. This
 
 * function must set all appropriate fields. This GRF is later appended to
 
 * the grfconfig list of the NetworkGameInfo.
 
 * @param config the GRF to handle
 
 */
 
void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config);
 
	/**
 
	 * Function that is called for every GRFConfig that is read when receiving
 
	 * a NetworkGameInfo. Only grfid and md5sum are set, the rest is zero. This
 
	 * function must set all appropriate fields. This GRF is later appended to
 
	 * the grfconfig list of the NetworkGameInfo.
 
	 * @param config the GRF to handle
 
	 */
 
	virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); }
 
public:
 
	NetworkUDPSocketHandler();
 
	virtual ~NetworkUDPSocketHandler() { this->Close(); }
 

	
 
	bool IsListening() { return this->udp != INVALID_SOCKET; }
 
	bool Listen(uint32 host, uint16 port, bool broadcast);
 
	void Close();
 

	
 
	void SendPacket(Packet *p, const struct sockaddr_in *recv);
 
	void ReceivePackets();
 

	
 
	void Send_GRFIdentifier(Packet *p, const GRFConfig *c);
 
	void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info);
 

	
 
	void Recv_GRFIdentifier(Packet *p, GRFConfig *c);
 
	void Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *info);
 
};
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
#endif /* NETWORK_CORE_UDP_H */
src/network/network.cpp
Show inline comments
 
@@ -38,15 +38,15 @@
 
#include <stdarg.h> /* va_list */
 
#include "../md5.h"
 

	
 
// global variables (declared in network_data.h)
 
CommandPacket *_local_command_queue;
 

	
 
SOCKET _udp_client_socket; // udp client socket
 
SOCKET _udp_server_socket; // udp server socket
 
SOCKET _udp_master_socket; // udp master socket
 
extern NetworkUDPSocketHandler *_udp_client_socket; ///< udp client socket
 
extern NetworkUDPSocketHandler *_udp_server_socket; ///< udp server socket
 
extern NetworkUDPSocketHandler *_udp_master_socket; ///< udp master socket
 

	
 
// Here we keep track of the clients
 
//  (and the client uses [0] for his own communication)
 
NetworkClientState _clients[MAX_CLIENTS];
 

	
 

	
 
@@ -839,13 +839,13 @@ static void NetworkClose(void)
 

	
 
	if (_network_server) {
 
		// We are a server, also close the listensocket
 
		closesocket(_listensocket);
 
		_listensocket = INVALID_SOCKET;
 
		DEBUG(net, 1, "Closed listener");
 
		NetworkUDPStop();
 
		NetworkUDPCloseAll();
 
	}
 
}
 

	
 
// Inits the network (cleans sockets and stuff)
 
static void NetworkInitialize(void)
 
{
 
@@ -955,13 +955,13 @@ bool NetworkClientConnectGame(const char
 
	if (port == 0) return false;
 

	
 
	ttd_strlcpy(_network_last_host, host, sizeof(_network_last_host));
 
	_network_last_port = port;
 

	
 
	NetworkDisconnect();
 
	NetworkUDPStop();
 
	NetworkUDPCloseAll();
 
	NetworkInitialize();
 

	
 
	// Try to connect
 
	_networking = NetworkConnect(host, port);
 

	
 
	// We are connected
 
@@ -1031,13 +1031,13 @@ bool NetworkServerStart(void)
 

	
 
	NetworkInitialize();
 
	if (!NetworkListen()) return false;
 

	
 
	// Try to start UDP-server
 
	_network_udp_server = true;
 
	_network_udp_server = NetworkUDPListen(&_udp_server_socket, _network_server_bind_ip, _network_server_port, false);
 
	_network_udp_server = _udp_server_socket->Listen(_network_server_bind_ip, _network_server_port, false);
 

	
 
	_network_server = true;
 
	_networking = true;
 
	_frame_counter = 0;
 
	_frame_counter_server = 0;
 
	_frame_counter_max = 0;
 
@@ -1267,18 +1267,16 @@ static bool NetworkDoClientLoop(void)
 
}
 

	
 
// We have to do some UDP checking
 
void NetworkUDPGameLoop(void)
 
{
 
	if (_network_udp_server) {
 
		NetworkUDPReceive(_udp_server_socket);
 
		if (_udp_master_socket != INVALID_SOCKET) {
 
			NetworkUDPReceive(_udp_master_socket);
 
		}
 
	} else if (_udp_client_socket != INVALID_SOCKET) {
 
		NetworkUDPReceive(_udp_client_socket);
 
		_udp_server_socket->ReceivePackets();
 
		_udp_master_socket->ReceivePackets();
 
	} else {
 
		_udp_client_socket->ReceivePackets();
 
		if (_network_udp_broadcast > 0) _network_udp_broadcast--;
 
	}
 
}
 

	
 
// The main loop called from ttd.c
 
//  Here we also have to do StateGameLoop if needed!
 
@@ -1387,13 +1385,13 @@ void NetworkStartUp(void)
 
}
 

	
 
/** This shuts the network down */
 
void NetworkShutDown(void)
 
{
 
	NetworkDisconnect();
 
	NetworkUDPStop();
 
	NetworkUDPShutdown();
 

	
 
	DEBUG(net, 3, "[core] shutting down network");
 

	
 
	_network_available = false;
 

	
 
	NetworkCoreShutdown();
src/network/network.h
Show inline comments
 
@@ -176,13 +176,13 @@ void NetworkRebuildHostList(void);
 
bool NetworkChangeCompanyPassword(byte argc, char *argv[]);
 
void NetworkPopulateCompanyInfo(void);
 
void UpdateNetworkGameWindow(bool unselect);
 
void CheckMinPlayers(void);
 

	
 
void NetworkStartUp(void);
 
void NetworkUDPStop(void);
 
void NetworkUDPCloseAll();
 
void NetworkShutDown(void);
 
void NetworkGameLoop(void);
 
void NetworkUDPGameLoop(void);
 
bool NetworkServerStart(void);
 
bool NetworkClientConnectGame(const char *host, uint16 port);
 
void NetworkReboot(void);
src/network/network_data.h
Show inline comments
 
@@ -120,16 +120,12 @@ typedef enum {
 
	DESTTYPE_CLIENT,    ///< Send message/notice to only a certain player (Private)
 
} DestType;
 

	
 
// following externs are instantiated at network.cpp
 
extern CommandPacket *_local_command_queue;
 

	
 
extern SOCKET _udp_client_socket; // udp client socket
 
extern SOCKET _udp_server_socket; // udp server socket
 
extern SOCKET _udp_master_socket; // udp master socket
 

	
 
// Here we keep track of the clients
 
//  (and the client uses [0] for his own communication)
 
extern NetworkClientState _clients[MAX_CLIENTS];
 

	
 
#define DEREF_CLIENT(i) (&_clients[i])
 
// This returns the NetworkClientInfo from a NetworkClientState
src/network/network_udp.cpp
Show inline comments
 
@@ -25,17 +25,48 @@
 
enum {
 
	ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes)
 
	ADVERTISE_RETRY_INTERVAL  =   300, // readvertise when no response after this many ticks (9 seconds)
 
	ADVERTISE_RETRY_TIMES     =     3  // give up readvertising after this much failed retries
 
};
 

	
 
#define DEF_UDP_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(Packet *p, const struct sockaddr_in *client_addr)
 
NetworkUDPSocketHandler *_udp_client_socket; ///< udp client socket
 
NetworkUDPSocketHandler *_udp_server_socket; ///< udp server socket
 
NetworkUDPSocketHandler *_udp_master_socket; ///< udp master socket
 

	
 
#define DEF_UDP_RECEIVE_COMMAND(cls, type) void cls ##NetworkUDPSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p, const struct sockaddr_in *client_addr)
 

	
 
///*** Communication with the masterserver ***/
 

	
 
class MasterNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
 
protected:
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
 
public:
 
	virtual ~MasterNetworkUDPSocketHandler() {}
 
};
 

	
 
static NetworkClientState _udp_cs;
 
DEF_UDP_RECEIVE_COMMAND(Master, PACKET_UDP_MASTER_ACK_REGISTER)
 
{
 
	_network_advertise_retries = 0;
 
	DEBUG(net, 2, "[udp] advertising on master server successfull");
 

	
 
	/* We are advertised, but we don't want to! */
 
	if (!_network_advertise) NetworkUDPRemoveAdvertise();
 
}
 

	
 
///*** Communication with clients (we are server) ***/
 

	
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER)
 
class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
 
protected:
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
 
public:
 
	virtual ~ServerNetworkUDPSocketHandler() {}
 
};
 

	
 
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_FIND_SERVER)
 
{
 
	Packet *packet;
 
	// Just a fail-safe.. should never happen
 
	if (!_network_udp_server)
 
		return;
 

	
 
@@ -47,112 +78,23 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIEN
 
	_network_game_info.map_height    = MapSizeY();
 
	_network_game_info.map_set       = _opt.landscape;
 
	_network_game_info.companies_on  = ActivePlayerCount();
 
	_network_game_info.spectators_on = NetworkSpectatorCount();
 
	_network_game_info.grfconfig     = _grfconfig;
 

	
 
	NetworkSend_NetworkGameInfo(packet, &_network_game_info);
 
	this->Send_NetworkGameInfo(packet, &_network_game_info);
 

	
 
	// Let the client know that we are here
 
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
 
	this->SendPacket(packet, client_addr);
 

	
 
	free(packet);
 

	
 
	DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr));
 
}
 

	
 
void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
 
{
 
	/* Find the matching GRF file */
 
	const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum);
 
	if (f == NULL) {
 
		/* Don't know the GRF, so mark game incompatible and the (possibly)
 
		 * already resolved name for this GRF (another server has sent the
 
		 * name of the GRF already */
 
		config->name     = FindUnknownGRFName(config->grfid, config->md5sum, true);
 
		SETBIT(config->flags, GCF_NOT_FOUND);
 
	} else {
 
		config->filename = f->filename;
 
		config->name     = f->name;
 
		config->info     = f->info;
 
	}
 
	SETBIT(config->flags, GCF_COPY);
 
}
 

	
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE)
 
{
 
	extern const char _openttd_revision[];
 
	NetworkGameList *item;
 

	
 
	// Just a fail-safe.. should never happen
 
	if (_network_udp_server || _udp_cs.has_quit) return;
 

	
 
	DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
 

	
 
	// Find next item
 
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port));
 

	
 
	NetworkRecv_NetworkGameInfo(&_udp_cs, p, &item->info);
 

	
 
	item->info.compatible = true;
 
	{
 
		/* Checks whether there needs to be a request for names of GRFs and makes
 
		 * the request if necessary. GRFs that need to be requested are the GRFs
 
		 * that do not exist on the clients system and we do not have the name
 
		 * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
 
		 * The in_request array and in_request_count are used so there is no need
 
		 * to do a second loop over the GRF list, which can be relatively expensive
 
		 * due to the string comparisons. */
 
		const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
 
		const GRFConfig *c;
 
		uint in_request_count = 0;
 
		struct sockaddr_in out_addr;
 

	
 
		for (c = item->info.grfconfig; c != NULL; c = c->next) {
 
			if (HASBIT(c->flags, GCF_NOT_FOUND)) item->info.compatible = false;
 
			if (!HASBIT(c->flags, GCF_NOT_FOUND) || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
 
			in_request[in_request_count] = c;
 
			in_request_count++;
 
		}
 

	
 
		if (in_request_count > 0) {
 
			/* There are 'unknown' GRFs, now send a request for them */
 
			uint i;
 
			Packet *packet = NetworkSend_Init(PACKET_UDP_CLIENT_GET_NEWGRFS);
 

	
 
			NetworkSend_uint8 (packet, in_request_count);
 
			for (i = 0; i < in_request_count; i++) {
 
				NetworkSend_GRFIdentifier(packet, in_request[i]);
 
			}
 

	
 
			out_addr.sin_family      = AF_INET;
 
			out_addr.sin_port        = htons(item->port);
 
			out_addr.sin_addr.s_addr = item->ip;
 
			NetworkSendUDP_Packet(_udp_client_socket, packet, &out_addr);
 
			free(packet);
 
		}
 
	}
 

	
 
	if (item->info.server_lang >= NETWORK_NUM_LANGUAGES) item->info.server_lang = 0;
 
	if (item->info.map_set >= NUM_LANDSCAPE ) item->info.map_set = 0;
 

	
 
	if (item->info.hostname[0] == '\0')
 
		snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr));
 

	
 
	/* Check if we are allowed on this server based on the revision-match */
 
	item->info.version_compatible =
 
		strcmp(item->info.server_revision, _openttd_revision) == 0 ||
 
		strcmp(item->info.server_revision, NOREV_STRING) == 0;
 
	item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
 

	
 
	item->online = true;
 

	
 
	UpdateNetworkGameWindow(false);
 
}
 

	
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO)
 
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO)
 
{
 
	NetworkClientState *cs;
 
	NetworkClientInfo *ci;
 
	Packet *packet;
 
	Player *player;
 
	byte current = 0;
 
@@ -247,52 +189,16 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIEN
 
		NetworkSend_uint32(packet, ci->join_date);
 
	}
 

	
 
	/* Indicates end of client list */
 
	NetworkSend_uint8(packet, 0);
 

	
 
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
 

	
 
	this->SendPacket(packet, client_addr);
 
	free(packet);
 
}
 

	
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST)
 
{
 
	int i;
 
	struct in_addr ip;
 
	uint16 port;
 
	uint8 ver;
 

	
 
	/* packet begins with the protocol version (uint8)
 
	 * then an uint16 which indicates how many
 
	 * ip:port pairs are in this packet, after that
 
	 * an uint32 (ip) and an uint16 (port) for each pair
 
	 */
 

	
 
	ver = NetworkRecv_uint8(&_udp_cs, p);
 

	
 
	if (_udp_cs.has_quit) return;
 

	
 
	if (ver == 1) {
 
		for (i = NetworkRecv_uint16(&_udp_cs, p); i != 0 ; i--) {
 
			ip.s_addr = TO_LE32(NetworkRecv_uint32(&_udp_cs, p));
 
			port = NetworkRecv_uint16(&_udp_cs, p);
 
			NetworkUDPQueryServer(inet_ntoa(ip), port);
 
		}
 
	}
 
}
 

	
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER)
 
{
 
	_network_advertise_retries = 0;
 
	DEBUG(net, 2, "[udp] advertising on master server successfull");
 

	
 
	/* We are advertised, but we don't want to! */
 
	if (!_network_advertise) NetworkUDPRemoveAdvertise();
 
}
 

	
 
/**
 
 * A client has requested the names of some NewGRFs.
 
 *
 
 * Replying this can be tricky as we have a limit of SEND_MTU bytes
 
 * in the reply packet and we can send up to 100 bytes per NewGRF
 
 * (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name).
 
@@ -300,35 +206,32 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTE
 
 * could be that a packet overflows. To stop this we only reply
 
 * with the first N NewGRFs so that if the first N + 1 NewGRFs
 
 * would be sent, the packet overflows.
 
 * in_reply and in_reply_count are used to keep a list of GRFs to
 
 * send in the reply.
 
 */
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS)
 
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_GET_NEWGRFS)
 
{
 
	uint8 num_grfs;
 
	uint i;
 

	
 
	const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT];
 
	Packet *packet;
 
	uint8 in_reply_count = 0;
 
	uint packet_len = 0;
 

	
 
	/* Just a fail-safe.. should never happen */
 
	if (_udp_cs.has_quit) return;
 

	
 
	DEBUG(net, 6, "[udp] newgrf data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
 

	
 
	num_grfs = NetworkRecv_uint8 (&_udp_cs, p);
 
	num_grfs = NetworkRecv_uint8 (&this->cs, p);
 
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
 

	
 
	for (i = 0; i < num_grfs; i++) {
 
		GRFConfig c;
 
		const GRFConfig *f;
 

	
 
		NetworkRecv_GRFIdentifier(&_udp_cs, p, &c);
 
		this->Recv_GRFIdentifier(p, &c);
 

	
 
		/* Find the matching GRF file */
 
		f = FindGRFConfig(c.grfid, c.md5sum);
 
		if (f == NULL) continue; // The GRF is unknown to this server
 

	
 
		/* If the reply might exceed the size of the packet, only reply
 
@@ -350,41 +253,147 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIEN
 
	for (i = 0; i < in_reply_count; i++) {
 
		char name[NETWORK_GRF_NAME_LENGTH];
 

	
 
		/* The name could be an empty string, if so take the filename */
 
		ttd_strlcpy(name, (in_reply[i]->name != NULL && strlen(in_reply[i]->name) > 0) ?
 
				in_reply[i]->name : in_reply[i]->filename, sizeof(name));
 
	 	NetworkSend_GRFIdentifier(packet, in_reply[i]);
 
	 	this->Send_GRFIdentifier(packet, in_reply[i]);
 
		NetworkSend_string(packet, name);
 
	}
 

	
 
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
 
	this->SendPacket(packet, client_addr);
 
	free(packet);
 
}
 

	
 
///*** Communication with servers (we are client) ***/
 

	
 
class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
 
protected:
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
 
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
 
	virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config);
 
public:
 
	virtual ~ClientNetworkUDPSocketHandler() {}
 
};
 

	
 
DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
 
{
 
	extern const char _openttd_revision[];
 
	NetworkGameList *item;
 

	
 
	// Just a fail-safe.. should never happen
 
	if (_network_udp_server) return;
 

	
 
	DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
 

	
 
	// Find next item
 
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port));
 

	
 
	this->Recv_NetworkGameInfo(p, &item->info);
 

	
 
	item->info.compatible = true;
 
	{
 
		/* Checks whether there needs to be a request for names of GRFs and makes
 
		 * the request if necessary. GRFs that need to be requested are the GRFs
 
		 * that do not exist on the clients system and we do not have the name
 
		 * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
 
		 * The in_request array and in_request_count are used so there is no need
 
		 * to do a second loop over the GRF list, which can be relatively expensive
 
		 * due to the string comparisons. */
 
		const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
 
		const GRFConfig *c;
 
		uint in_request_count = 0;
 
		struct sockaddr_in out_addr;
 

	
 
		for (c = item->info.grfconfig; c != NULL; c = c->next) {
 
			if (HASBIT(c->flags, GCF_NOT_FOUND)) item->info.compatible = false;
 
			if (!HASBIT(c->flags, GCF_NOT_FOUND) || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
 
			in_request[in_request_count] = c;
 
			in_request_count++;
 
		}
 

	
 
		if (in_request_count > 0) {
 
			/* There are 'unknown' GRFs, now send a request for them */
 
			uint i;
 
			Packet *packet = NetworkSend_Init(PACKET_UDP_CLIENT_GET_NEWGRFS);
 

	
 
			NetworkSend_uint8 (packet, in_request_count);
 
			for (i = 0; i < in_request_count; i++) {
 
				this->Send_GRFIdentifier(packet, in_request[i]);
 
			}
 

	
 
			out_addr.sin_family      = AF_INET;
 
			out_addr.sin_port        = htons(item->port);
 
			out_addr.sin_addr.s_addr = item->ip;
 
			this->SendPacket(packet, &out_addr);
 
			free(packet);
 
		}
 
	}
 

	
 
	if (item->info.server_lang >= NETWORK_NUM_LANGUAGES) item->info.server_lang = 0;
 
	if (item->info.map_set >= NUM_LANDSCAPE ) item->info.map_set = 0;
 

	
 
	if (item->info.hostname[0] == '\0')
 
		snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr));
 

	
 
	/* Check if we are allowed on this server based on the revision-match */
 
	item->info.version_compatible =
 
		strcmp(item->info.server_revision, _openttd_revision) == 0 ||
 
		strcmp(item->info.server_revision, NOREV_STRING) == 0;
 
	item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
 

	
 
	item->online = true;
 

	
 
	UpdateNetworkGameWindow(false);
 
}
 

	
 
DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_MASTER_RESPONSE_LIST)
 
{
 
	int i;
 
	struct in_addr ip;
 
	uint16 port;
 
	uint8 ver;
 

	
 
	/* packet begins with the protocol version (uint8)
 
	 * then an uint16 which indicates how many
 
	 * ip:port pairs are in this packet, after that
 
	 * an uint32 (ip) and an uint16 (port) for each pair
 
	 */
 

	
 
	ver = NetworkRecv_uint8(&this->cs, p);
 

	
 
	if (this->cs.has_quit) return;
 

	
 
	if (ver == 1) {
 
		for (i = NetworkRecv_uint16(&this->cs, p); i != 0 ; i--) {
 
			ip.s_addr = TO_LE32(NetworkRecv_uint32(&this->cs, p));
 
			port = NetworkRecv_uint16(&this->cs, p);
 
			NetworkUDPQueryServer(inet_ntoa(ip), port);
 
		}
 
	}
 
}
 

	
 
/** The return of the client's request of the names of some NewGRFs */
 
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS)
 
DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_NEWGRFS)
 
{
 
	uint8 num_grfs;
 
	uint i;
 

	
 
	/* Just a fail-safe.. should never happen */
 
	if (_udp_cs.has_quit) return;
 
	DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
 

	
 
	DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
 

	
 
	num_grfs = NetworkRecv_uint8 (&_udp_cs, p);
 
	num_grfs = NetworkRecv_uint8 (&this->cs, p);
 
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
 

	
 
	for (i = 0; i < num_grfs; i++) {
 
		char *unknown_name;
 
		char name[NETWORK_GRF_NAME_LENGTH];
 
		GRFConfig c;
 

	
 
		NetworkRecv_GRFIdentifier(&_udp_cs, p, &c);
 
		NetworkRecv_string(&_udp_cs, p, name, sizeof(name));
 
		this->Recv_GRFIdentifier(p, &c);
 
		NetworkRecv_string(&this->cs, p, name, sizeof(name));
 

	
 
		/* An empty name is not possible under normal circumstances
 
		 * and causes problems when showing the NewGRF list. */
 
		if (strlen(name) == 0) continue;
 

	
 
		/* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple.
 
@@ -394,78 +403,45 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVE
 
		if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
 
			ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Every type of UDP packet should only be received by a single socket;
 
 * The socket communicating with the masterserver should receive the
 
 * game information of some 'random' host.
 
 */
 
typedef struct NetworkUDPPacketAndSocket {
 
	void (*callback)(Packet *p, const struct sockaddr_in *client_addr);
 
	SOCKET *incoming_socket;
 
} NetworkUPDPacketAndSocket;
 

	
 
static const NetworkUPDPacketAndSocket _network_udp_packet[PACKET_UDP_END] = {
 
	{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER),   &_udp_server_socket },
 
	{ RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE),      &_udp_client_socket },
 
	{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO),   &_udp_server_socket },
 
	{ NULL,                                             NULL                },
 
	{ NULL,                                             NULL                },
 
	{ RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER),  &_udp_master_socket },
 
	{ NULL,                                             NULL                },
 
	{ RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST), &_udp_client_socket },
 
	{ NULL,                                             NULL                },
 
	{ RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS),   &_udp_server_socket },
 
	{ RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS),       &_udp_client_socket },
 
};
 

	
 
void NetworkHandleUDPPacket(const SOCKET udp, Packet *p, const struct sockaddr_in *client_addr)
 
void ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
 
{
 
	byte type;
 

	
 
	/* Fake a client, so we can see when there is an illegal packet */
 
	_udp_cs.socket = INVALID_SOCKET;
 
	_udp_cs.has_quit = false;
 

	
 
	type = NetworkRecv_uint8(&_udp_cs, p);
 

	
 
	if (type < PACKET_UDP_END && *_network_udp_packet[type].incoming_socket == udp && !_udp_cs.has_quit) {
 
		_network_udp_packet[type].callback(p, client_addr);
 
	/* Find the matching GRF file */
 
	const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum);
 
	if (f == NULL) {
 
		/* Don't know the GRF, so mark game incompatible and the (possibly)
 
		 * already resolved name for this GRF (another server has sent the
 
		 * name of the GRF already */
 
		config->name     = FindUnknownGRFName(config->grfid, config->md5sum, true);
 
		SETBIT(config->flags, GCF_NOT_FOUND);
 
	} else {
 
		if (*_network_udp_packet[type].incoming_socket != udp) {
 
			DEBUG(net, 0, "[udp] received packet on wrong port from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
 
		} else if (!_udp_cs.has_quit) {
 
			DEBUG(net, 0, "[udp] received invalid packet type %d from %s:%d", type,  inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
 
		} else {
 
			DEBUG(net, 0, "[udp] received illegal packet from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
 
		}
 
		config->filename = f->filename;
 
		config->name     = f->name;
 
		config->info     = f->info;
 
	}
 
	SETBIT(config->flags, GCF_COPY);
 
}
 

	
 

	
 
// Close UDP connection
 
void NetworkUDPStop(void)
 
void NetworkUDPCloseAll(void)
 
{
 
	DEBUG(net, 1, "[udp] closed listeners");
 

	
 
	if (_network_udp_server) {
 
		NetworkUDPClose(&_udp_server_socket);
 
		NetworkUDPClose(&_udp_master_socket);
 
	} else {
 
		NetworkUDPClose(&_udp_client_socket);
 
	}
 
	_udp_server_socket->Close();
 
	_udp_master_socket->Close();
 
	_udp_client_socket->Close();
 

	
 
	_network_udp_server = false;
 
	_network_udp_broadcast = 0;
 
}
 

	
 
// Broadcast to all ips
 
static void NetworkUDPBroadCast(SOCKET udp)
 
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
 
{
 
	Packet* p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
 
	uint i;
 

	
 
	for (i = 0; _broadcast_list[i] != 0; i++) {
 
		struct sockaddr_in out_addr;
 
@@ -473,39 +449,39 @@ static void NetworkUDPBroadCast(SOCKET u
 
		out_addr.sin_family = AF_INET;
 
		out_addr.sin_port = htons(_network_server_port);
 
		out_addr.sin_addr.s_addr = _broadcast_list[i];
 

	
 
		DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr));
 

	
 
		NetworkSendUDP_Packet(udp, p, &out_addr);
 
		socket->SendPacket(p, &out_addr);
 
	}
 

	
 
	free(p);
 
}
 

	
 

	
 
// Request the the server-list from the master server
 
void NetworkUDPQueryMasterServer(void)
 
{
 
	struct sockaddr_in out_addr;
 
	Packet *p;
 

	
 
	if (_udp_client_socket == INVALID_SOCKET)
 
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
 
			return;
 
	if (!_udp_client_socket->IsListening()) {
 
		if (!_udp_client_socket->Listen(0, 0, true)) return;
 
	}
 

	
 
	p = NetworkSend_Init(PACKET_UDP_CLIENT_GET_LIST);
 

	
 
	out_addr.sin_family = AF_INET;
 
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
 
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
 

	
 
	// packet only contains protocol version
 
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
 

	
 
	NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
 
	_udp_client_socket->SendPacket(p, &out_addr);
 

	
 
	DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr),ntohs(out_addr.sin_port));
 

	
 
	free(p);
 
}
 

	
 
@@ -513,15 +489,15 @@ void NetworkUDPQueryMasterServer(void)
 
void NetworkUDPSearchGame(void)
 
{
 
	// We are still searching..
 
	if (_network_udp_broadcast > 0) return;
 

	
 
	// No UDP-socket yet..
 
	if (_udp_client_socket == INVALID_SOCKET)
 
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
 
			return;
 
	if (!_udp_client_socket->IsListening()) {
 
		if (!_udp_client_socket->Listen(0, 0, true)) return;
 
	}
 

	
 
	DEBUG(net, 0, "[udp] searching server");
 

	
 
	NetworkUDPBroadCast(_udp_client_socket);
 
	_network_udp_broadcast = 300; // Stay searching for 300 ticks
 
}
 
@@ -530,15 +506,15 @@ NetworkGameList *NetworkUDPQueryServer(c
 
{
 
	struct sockaddr_in out_addr;
 
	Packet *p;
 
	NetworkGameList *item;
 

	
 
	// No UDP-socket yet..
 
	if (_udp_client_socket == INVALID_SOCKET)
 
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
 
			return NULL;
 
	if (!_udp_client_socket->IsListening()) {
 
		if (!_udp_client_socket->Listen(0, 0, true)) return NULL;
 
	}
 

	
 
	out_addr.sin_family = AF_INET;
 
	out_addr.sin_port = htons(port);
 
	out_addr.sin_addr.s_addr = NetworkResolveHost(host);
 

	
 
	// Clear item in gamelist
 
@@ -548,13 +524,13 @@ NetworkGameList *NetworkUDPQueryServer(c
 
	ttd_strlcpy(item->info.hostname, host, lengthof(item->info.hostname));
 
	item->online = false;
 

	
 
	// Init the packet
 
	p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
 

	
 
	NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
 
	_udp_client_socket->SendPacket(p, &out_addr);
 

	
 
	free(p);
 

	
 
	UpdateNetworkGameWindow(false);
 
	return item;
 
}
 
@@ -566,15 +542,15 @@ void NetworkUDPRemoveAdvertise(void)
 
	Packet *p;
 

	
 
	/* Check if we are advertising */
 
	if (!_networking || !_network_server || !_network_udp_server) return;
 

	
 
	/* check for socket */
 
	if (_udp_master_socket == INVALID_SOCKET)
 
		if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false))
 
			return;
 
	if (!_udp_master_socket->IsListening()) {
 
		if (!_udp_master_socket->Listen(0, 0, false)) return;
 
	}
 

	
 
	DEBUG(net, 1, "[udp] removing advertise from master server");
 

	
 
	/* Find somewhere to send */
 
	out_addr.sin_family = AF_INET;
 
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
 
@@ -582,13 +558,13 @@ void NetworkUDPRemoveAdvertise(void)
 

	
 
	/* Send the packet */
 
	p = NetworkSend_Init(PACKET_UDP_SERVER_UNREGISTER);
 
	/* Packet is: Version, server_port */
 
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
 
	NetworkSend_uint16(p, _network_server_port);
 
	NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr);
 
	_udp_master_socket->SendPacket(p, &out_addr);
 

	
 
	free(p);
 
}
 

	
 
/* Register us to the master server
 
     This function checks if it needs to send an advertise */
 
@@ -599,15 +575,15 @@ void NetworkUDPAdvertise(void)
 

	
 
	/* Check if we should send an advertise */
 
	if (!_networking || !_network_server || !_network_udp_server || !_network_advertise)
 
		return;
 

	
 
	/* check for socket */
 
	if (_udp_master_socket == INVALID_SOCKET)
 
		if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false))
 
			return;
 
	if (!_udp_master_socket->IsListening()) {
 
		if (!_udp_master_socket->Listen(0, 0, false)) return;
 
	}
 

	
 
	if (_network_need_advertise) {
 
		_network_need_advertise = false;
 
		_network_advertise_retries = ADVERTISE_RETRY_TIMES;
 
	} else {
 
		/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
 
@@ -634,22 +610,31 @@ void NetworkUDPAdvertise(void)
 
	/* Send the packet */
 
	p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER);
 
	/* Packet is: WELCOME_MESSAGE, Version, server_port */
 
	NetworkSend_string(p, NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
 
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
 
	NetworkSend_uint16(p, _network_server_port);
 
	NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr);
 
	_udp_master_socket->SendPacket(p, &out_addr);
 

	
 
	free(p);
 
}
 

	
 
void NetworkUDPInitialize(void)
 
{
 
	_udp_client_socket = INVALID_SOCKET;
 
	_udp_server_socket = INVALID_SOCKET;
 
	_udp_master_socket = INVALID_SOCKET;
 
	_udp_client_socket = new ClientNetworkUDPSocketHandler();
 
	_udp_server_socket = new ServerNetworkUDPSocketHandler();
 
	_udp_master_socket = new MasterNetworkUDPSocketHandler();
 

	
 
	_network_udp_server = false;
 
	_network_udp_broadcast = 0;
 
}
 

	
 
void NetworkUDPShutdown(void)
 
{
 
	NetworkUDPCloseAll();
 

	
 
	delete _udp_client_socket;
 
	delete _udp_server_socket;
 
	delete _udp_master_socket;
 
}
 

	
 
#endif /* ENABLE_NETWORK */
src/network/network_udp.h
Show inline comments
 
@@ -8,10 +8,11 @@
 
void NetworkUDPInitialize(void);
 
void NetworkUDPSearchGame(void);
 
void NetworkUDPQueryMasterServer(void);
 
NetworkGameList *NetworkUDPQueryServer(const char* host, unsigned short port);
 
void NetworkUDPAdvertise(void);
 
void NetworkUDPRemoveAdvertise(void);
 
void NetworkUDPShutdown(void);
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
#endif /* NETWORK_UDP_H */
src/openttd.cpp
Show inline comments
 
@@ -745,16 +745,16 @@ void SwitchMode(int new_mode)
 
	// If we are saving something, the network stays in his current state
 
	if (new_mode != SM_SAVE) {
 
		// If the network is active, make it not-active
 
		if (_networking) {
 
			if (_network_server && (new_mode == SM_LOAD || new_mode == SM_NEWGAME)) {
 
				NetworkReboot();
 
				NetworkUDPStop();
 
				NetworkUDPCloseAll();
 
			} else {
 
				NetworkDisconnect();
 
				NetworkUDPStop();
 
				NetworkUDPCloseAll();
 
			}
 
		}
 

	
 
		// If we are a server, we restart the server
 
		if (_is_network_server) {
 
			// But not if we are going to the menu
0 comments (0 inline, 0 general)