Files @ r23483:3733e6b8ff17
Branch filter:

Location: cpp/openttd-patchpack/source/src/network/core/address.h - annotation

Patric Stout
Remove: ENABLE_NETWORK switch

This switch has been a pain for years. Often disabling broke
compilation, as no developer compiles OpenTTD without, neither do
any of our official binaries.

Additionaly, it has grown so hugely in our codebase, that it
clearly shows that the current solution was a poor one. 350+
instances of "#ifdef ENABLE_NETWORK" were in the code, of which
only ~30 in the networking code itself. The rest were all around
the code to do the right thing, from GUI to NewGRF.

A more proper solution would be to stub all the functions, and
make sure the rest of the code can simply assume network is
available. This was also partially done, and most variables were
correct if networking was disabled. Despite that, often the #ifdefs
were still used.

With the recent removal of DOS, there is also no platform anymore
which we support where networking isn't working out-of-the-box.

All in all, it is time to remove the ENABLE_NETWORK switch. No
replacement is planned, but if you feel we really need this option,
we welcome any Pull Request which implements this in a way that
doesn't crawl through the code like this diff shows we used to.
r10828:d8d68d002525
r10828:d8d68d002525
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r10828:d8d68d002525
r10828:d8d68d002525
r16851:b5784600e35e
r16851:b5784600e35e
r10828:d8d68d002525
r10828:d8d68d002525
r11590:2bff4ec7f0ca
r11590:2bff4ec7f0ca
r11592:12c71fbe8d50
r11591:dcfe7b4a6015
r11591:dcfe7b4a6015
r17622:d9f6f6845b5d
r17622:d9f6f6845b5d
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r11590:2bff4ec7f0ca
r11649:7c14ee641f09
r11590:2bff4ec7f0ca
r17886:18d13f57e663
r10828:d8d68d002525
r11553:665b4b3ba421
r11553:665b4b3ba421
r11553:665b4b3ba421
r11553:665b4b3ba421
r11553:665b4b3ba421
r11553:665b4b3ba421
r11553:665b4b3ba421
r11592:12c71fbe8d50
r10828:d8d68d002525
r10828:d8d68d002525
r17622:d9f6f6845b5d
r17622:d9f6f6845b5d
r17622:d9f6f6845b5d
r11545:27d3f758c0c0
r11649:7c14ee641f09
r11551:57af1dbb6426
r17886:18d13f57e663
r17886:18d13f57e663
r10828:d8d68d002525
r11590:2bff4ec7f0ca
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r17622:d9f6f6845b5d
r17622:d9f6f6845b5d
r17622:d9f6f6845b5d
r11555:622725a26a6d
r11649:7c14ee641f09
r17886:18d13f57e663
r17886:18d13f57e663
r11555:622725a26a6d
r11590:2bff4ec7f0ca
r11555:622725a26a6d
r11555:622725a26a6d
r11555:622725a26a6d
r11555:622725a26a6d
r11555:622725a26a6d
r10828:d8d68d002525
r17622:d9f6f6845b5d
r10828:d8d68d002525
r11567:a288e946756b
r10828:d8d68d002525
r11615:3725a6566d9c
r17886:18d13f57e663
r17886:18d13f57e663
r10828:d8d68d002525
r11597:39dbd2f0af04
r11597:39dbd2f0af04
r11597:39dbd2f0af04
r11590:2bff4ec7f0ca
r11599:60558ee2245d
r11597:39dbd2f0af04
r11597:39dbd2f0af04
r11545:27d3f758c0c0
r11567:a288e946756b
r11546:a4422f41f18b
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r11590:2bff4ec7f0ca
r10828:d8d68d002525
r11590:2bff4ec7f0ca
r10828:d8d68d002525
r10828:d8d68d002525
r11545:27d3f758c0c0
r11609:9bf9c36c3172
r11602:593b9791e12a
r11545:27d3f758c0c0
r11544:5b7269ee2600
r11544:5b7269ee2600
r11551:57af1dbb6426
r11551:57af1dbb6426
r11551:57af1dbb6426
r11649:7c14ee641f09
r11551:57af1dbb6426
r11551:57af1dbb6426
r11551:57af1dbb6426
r11551:57af1dbb6426
r11551:57af1dbb6426
r11551:57af1dbb6426
r11545:27d3f758c0c0
r11546:a4422f41f18b
r11546:a4422f41f18b
r11546:a4422f41f18b
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r10828:d8d68d002525
r17886:18d13f57e663
r10828:d8d68d002525
r11546:a4422f41f18b
r11597:39dbd2f0af04
r11572:9d570d041dd0
r11572:9d570d041dd0
r11572:9d570d041dd0
r11546:a4422f41f18b
r11546:a4422f41f18b
r11574:e29f5f02e7d5
r11546:a4422f41f18b
r11677:627f7f0cc1a2
r11546:a4422f41f18b
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11579:997fbf83d583
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11546:a4422f41f18b
r11547:d8613f4c59f9
r11550:baebd05afdd5
r11564:f44f0aa8c0c3
r11564:f44f0aa8c0c3
r11590:2bff4ec7f0ca
r11564:f44f0aa8c0c3
r11677:627f7f0cc1a2
r11564:f44f0aa8c0c3
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11590:2bff4ec7f0ca
r11590:2bff4ec7f0ca
r11677:627f7f0cc1a2
r11590:2bff4ec7f0ca
r11590:2bff4ec7f0ca
r11590:2bff4ec7f0ca
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11739:0fd5c23f2ad1
r11590:2bff4ec7f0ca
r11590:2bff4ec7f0ca
r11590:2bff4ec7f0ca
r11590:2bff4ec7f0ca
r11574:e29f5f02e7d5
r11677:627f7f0cc1a2
r11574:e29f5f02e7d5
r11574:e29f5f02e7d5
r11564:f44f0aa8c0c3
r11564:f44f0aa8c0c3
r11550:baebd05afdd5
r11622:2661de6c808c
r11843:07fb5d356fc9
r11843:07fb5d356fc9
r11843:07fb5d356fc9
r10828:d8d68d002525
r10828:d8d68d002525
r16851:b5784600e35e
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file core/address.h Wrapper for network addresses. */

#ifndef NETWORK_CORE_ADDRESS_H
#define NETWORK_CORE_ADDRESS_H

#include "os_abstraction.h"
#include "config.h"
#include "../../string_func.h"
#include "../../core/smallmap_type.hpp"

class NetworkAddress;
typedef SmallVector<NetworkAddress, 4> NetworkAddressList; ///< Type for a list of addresses.
typedef SmallMap<NetworkAddress, SOCKET, 4> SocketList;    ///< Type for a mapping between address and socket.

/**
 * Wrapper for (un)resolved network addresses; there's no reason to transform
 * a numeric IP to a string and then back again to pass it to functions. It
 * furthermore allows easier delaying of the hostname lookup.
 */
class NetworkAddress {
private:
	char hostname[NETWORK_HOSTNAME_LENGTH]; ///< The hostname
	int address_length;                     ///< The length of the resolved address
	sockaddr_storage address;               ///< The resolved address
	bool resolved;                          ///< Whether the address has been (tried to be) resolved

	/**
	 * Helper function to resolve something to a socket.
	 * @param runp information about the socket to try not
	 * @return the opened socket or INVALID_SOCKET
	 */
	typedef SOCKET (*LoopProc)(addrinfo *runp);

	SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
public:
	/**
	 * Create a network address based on a resolved IP and port.
	 * @param address The IP address with port.
	 * @param address_length The length of the address.
	 */
	NetworkAddress(struct sockaddr_storage &address, int address_length) :
		address_length(address_length),
		address(address),
		resolved(address_length != 0)
	{
		*this->hostname = '\0';
	}

	/**
	 * Create a network address based on a resolved IP and port.
	 * @param address The IP address with port.
	 * @param address_length The length of the address.
	 */
	NetworkAddress(sockaddr *address, int address_length) :
		address_length(address_length),
		resolved(address_length != 0)
	{
		*this->hostname = '\0';
		memset(&this->address, 0, sizeof(this->address));
		memcpy(&this->address, address, address_length);
	}

	/**
	 * Create a network address based on a unresolved host and port
	 * @param hostname the unresolved hostname
	 * @param port the port
	 * @param family the address family
	 */
	NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
		address_length(0),
		resolved(false)
	{
		/* Also handle IPv6 bracket enclosed hostnames */
		if (StrEmpty(hostname)) hostname = "";
		if (*hostname == '[') hostname++;
		strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
		char *tmp = strrchr(this->hostname, ']');
		if (tmp != NULL) *tmp = '\0';

		memset(&this->address, 0, sizeof(this->address));
		this->address.ss_family = family;
		this->SetPort(port);
	}

	/**
	 * Make a clone of another address
	 * @param address the address to clone
	 */
	NetworkAddress(const NetworkAddress &address)
	{
		memcpy(this, &address, sizeof(*this));
	}

	const char *GetHostname();
	void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
	const char *GetAddressAsString(bool with_family = true);
	const sockaddr_storage *GetAddress();

	/**
	 * Get the (valid) length of the address.
	 * @return the length
	 */
	int GetAddressLength()
	{
		/* Resolve it if we didn't do it already */
		if (!this->IsResolved()) this->GetAddress();
		return this->address_length;
	}

	uint16 GetPort() const;
	void SetPort(uint16 port);

	/**
	 * Check whether the IP address has been resolved already
	 * @return true iff the port has been resolved
	 */
	bool IsResolved() const
	{
		return this->resolved;
	}

	bool IsFamily(int family);
	bool IsInNetmask(char *netmask);

	/**
	 * Compare the address of this class with the address of another.
	 * @param address the other address.
	 * @return < 0 if address is less, 0 if equal and > 0 if address is more
	 */
	int CompareTo(NetworkAddress &address)
	{
		int r = this->GetAddressLength() - address.GetAddressLength();
		if (r == 0) r = this->address.ss_family - address.address.ss_family;
		if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
		if (r == 0) r = this->GetPort() - address.GetPort();
		return r;
	}

	/**
	 * Compare the address of this class with the address of another.
	 * @param address the other address.
	 * @return true if both match.
	 */
	bool operator == (NetworkAddress &address)
	{
		return this->CompareTo(address) == 0;
	}

	/**
	 * Compare the address of this class with the address of another.
	 * @param address the other address.
	 * @return true if both match.
	 */
	bool operator == (NetworkAddress &address) const
	{
		return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
	}
	/**
	 * Compare the address of this class with the address of another.
	 * @param address the other address.
	 * @return true if both do not match.
	 */
	bool operator != (NetworkAddress address) const
	{
		return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
	}

	/**
	 * Compare the address of this class with the address of another.
	 * @param address the other address.
	 */
	bool operator < (NetworkAddress &address)
	{
		return this->CompareTo(address) < 0;
	}

	SOCKET Connect();
	void Listen(int socktype, SocketList *sockets);

	static const char *SocketTypeAsString(int socktype);
	static const char *AddressFamilyAsString(int family);
};

#endif /* NETWORK_CORE_ADDRESS_H */