Since broadcasting to 255.255.255.255 does not work in all cases,

add code to create a set of potential broadcast addresses and
use them all. Duplicated will be filtered out (since likely a server
will replace more than all).
This commit is contained in:
hiker 2018-04-23 23:30:48 +10:00
parent bc8539ae10
commit 59b796a7f5
15 changed files with 213 additions and 55 deletions

View File

@ -20,6 +20,7 @@
#include "config/user_config.hpp"
#include "io/file_manager.hpp"
#include "network/network_config.hpp"
#include "network/network_string.hpp"
#include "network/transport_address.hpp"
#include "utils/time.hpp"
@ -27,12 +28,16 @@
#include <string.h>
#if defined(WIN32)
# include "ws2tcpip.h"
# include <iphlpapi.h>
# define inet_ntop InetNtop
#else
# include <arpa/inet.h>
# include <errno.h>
# include <ifaddrs.h>
# include <sys/socket.h>
#endif
#include <pthread.h>
#include <signal.h>
@ -225,3 +230,135 @@ void Network::closeLog()
m_log_file.unlock();
}
} // closeLog
// ----------------------------------------------------------------------------
/** Sets a list of default broadcast addresses which is used in case no valid
* broadcast address is found. This list includes default private network
* addresses.
*/
void Network::setDefaultBroadcastAddresses()
{
// Add some common LAN addresses
m_broadcast_address.emplace_back(std::string("192.168.255.255"));
m_broadcast_address.emplace_back(std::string("192.168.255.255"));
m_broadcast_address.emplace_back(std::string("192.168.255.255"));
m_broadcast_address.emplace_back(std::string("192.168.0.255") );
m_broadcast_address.emplace_back(std::string("172.31.255.255") );
m_broadcast_address.emplace_back(std::string("172.16.255.255") );
m_broadcast_address.emplace_back(std::string("172.16.0.255") );
m_broadcast_address.emplace_back(std::string("10.255.255.255") );
m_broadcast_address.emplace_back(std::string("10.0.255.255") );
m_broadcast_address.emplace_back(std::string("10.0.0.255") );
m_broadcast_address.emplace_back(std::string("255.255.255.255"));
m_broadcast_address.emplace_back(std::string("127.0.0.255") );
m_broadcast_address.emplace_back(std::string("127.0.0.1") );
} // setDefaultBroadcastAddresses
// ----------------------------------------------------------------------------
/** This masks various possible broadcast addresses. For example, in a /16
* network it would first use *.*.255.255, then *.*.*.255. Also if the
* length of the mask is not a multiple of 8, the original value will
* be used, before multiple of 8 are create: /22 (*.3f.ff.ff), then
* /16 (*.*.ff.ff), /8 (*.*.*.ff). While this is usually an overkill,
* it can help in the case that the router does not forward a broadcast
* as expected (this problem often happens with 255.255.255.255, which is
* why this broadcast address creation code was added).
* \param a The transport address for which the broadcast addresses need
* to be created.
* \param len Number of bits to be or'ed.
*/
void Network::addAllBroadcastAddresses(const TransportAddress &a, int len)
{
// Try different broadcast addresses - by masking on
// byte boundaries
while (len > 0)
{
unsigned int mask = (1 << len) - 1;
TransportAddress bcast(a.getIP() | mask,
NetworkConfig::get()->getServerDiscoveryPort());
Log::info("Broadcast", "address %s length %d mask %x --> %s",
a.toString().c_str(),
len, mask,
bcast.toString().c_str());
m_broadcast_address.push_back(bcast);
if (len % 8 != 0)
len -= (len % 8);
else
len = len - 8;
} // while len > 0
} // addAllBroadcastAddresses
// ----------------------------------------------------------------------------
/** Returns a list of all possible broadcast addresses on this machine.
* It queries all adapters for active IPV4 interfaces, determines their
* netmask to create the broadcast addresses. It will also add 'smaller'
* broadcast addesses, e.g. in a /16 network, it will add *.*.255.255 and
* *.*.*.255, since it was sometimes observed that routers would not let
* all broadcast addresses through. Duplicated answers (from the same server
* to different addersses) will be filtered out in ServersManager.
*/
const std::vector<TransportAddress>& Network::getBroadcastAddresses()
{
if (m_broadcast_address.size() > 0) return m_broadcast_address;
#ifdef WIN32
IP_ADAPTER_ADDRESSES *addresses;
int count = 100, return_code;
int iteration = 0;
do
{
addresses = new IP_ADAPTER_ADDRESSES[count];
ULONG buf_len = sizeof(IP_ADAPTER_ADDRESSES)*count;
long flags = 0;
return_code = GetAdaptersAddresses(AF_INET, flags, NULL, addresses,
&buf_len);
iteration++;
} while (return_code == ERROR_BUFFER_OVERFLOW && iteration<10);
if (return_code == ERROR_BUFFER_OVERFLOW)
{
Log::warn("NetworkConfig", "Can not get broadcast addresses.");
setDefaultBroadcastAddresses();
return m_broadcast_address;
}
for (IP_ADAPTER_ADDRESSES *p = addresses; p; p = p->Next)
{
// Check all operational IP4 adapters
if (p->OperStatus == IfOperStatusUp &&
p->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET)
{
const sockaddr_in *sa = (sockaddr_in*)p->FirstUnicastAddress->Address.lpSockaddr;
// Use sa->sin_addr.S_un.S_addr and htonl?
TransportAddress ta(sa->sin_addr.S_un.S_un_b.s_b1,
sa->sin_addr.S_un.S_un_b.s_b2,
sa->sin_addr.S_un.S_un_b.s_b3,
sa->sin_addr.S_un.S_un_b.s_b4);
int len = 32 - p->FirstUnicastAddress->OnLinkPrefixLength;
addAllBroadcastAddresses(ta, len);
}
}
#else
struct ifaddrs *addresses, *p;
getifaddrs(&addresses);
for (p = addresses; p; p = p->ifa_next)
{
if (p->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *sa = (struct sockaddr_in *) p->ifa_addr;
TransportAddress ta(sa->sin_addr, get);
char *addr = inet_ntoa(sa->sin_addr);
int ip = ((sockaddr_in*)(p->ifa_netmask))->sin_addr.s_addr;
ip = ~ip;
printf("Interface: %s\tAddress: %s\tmask: %x -> %x\n", p->ifa_name,
addr, p->ifa_netmask, htonl(ip));
}
}
#endif
return m_broadcast_address;
} // getBroadcastAddresses

View File

@ -32,6 +32,7 @@
#include <enet/enet.h>
#include <stdio.h>
#include <vector>
class BareNetworkString;
class NetworkString;
@ -50,6 +51,12 @@ private:
/** Where to log packets. If NULL for FILE* logging is disabled. */
static Synchronised<FILE*> m_log_file;
/** List of broadcast addresses to use. */
std::vector<TransportAddress> m_broadcast_address;
void setDefaultBroadcastAddresses();
void addAllBroadcastAddresses(const TransportAddress &a, int len);
public:
Network(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
@ -68,6 +75,8 @@ public:
TransportAddress* sender, int max_tries = -1);
void broadcastPacket(NetworkString *data,
bool reliable = true);
const std::vector<TransportAddress>& getBroadcastAddresses();
// ------------------------------------------------------------------------
/** Returns a pointer to the ENet host object. */
ENetHost* getENetHost() { return m_host; }

View File

@ -19,6 +19,7 @@
#include "network/network_config.hpp"
#include "config/stk_config.hpp"
#include "config/user_config.hpp"
#include "network/transport_address.hpp"
#include "online/xml_request.hpp"
#include "states_screens/main_menu_screen.hpp"
#include "states_screens/networking_lobby.hpp"
@ -78,7 +79,7 @@ void NetworkConfig::setIsServer(bool b)
// ----------------------------------------------------------------------------
void NetworkConfig::setServerMode(RaceManager::MinorRaceModeType minor,
RaceManager::MajorRaceModeType major)
RaceManager::MajorRaceModeType major)
{
if (major == RaceManager::MAJOR_MODE_GRAND_PRIX)
{

View File

@ -24,6 +24,7 @@
#include "network/transport_address.hpp"
#include "race/race_manager.hpp"
#include "utils/no_copy.hpp"
#include "irrString.h"
#include <tuple>
@ -42,7 +43,7 @@ namespace GUIEngine
class InputDevice;
class PlayerProfile;
class NetworkConfig
class NetworkConfig : public NoCopy
{
private:
/** The singleton instance. */

View File

@ -97,7 +97,7 @@ void ClientLobby::clearPlayers()
*/
void ClientLobby::setAddress(const TransportAddress &address)
{
m_server_address.copy(address);
m_server_address = address;
} // setAddress
//-----------------------------------------------------------------------------

View File

@ -46,7 +46,7 @@ ConnectToPeer::ConnectToPeer(uint32_t peer_id) : Protocol(PROTOCOL_CONNECTION)
ConnectToPeer::ConnectToPeer(const TransportAddress &address)
: Protocol(PROTOCOL_CONNECTION)
{
m_peer_address.copy(address);
m_peer_address = address;
// We don't need to find the peer address, so we can start
// with the state when we found the peer address.
m_state = WAIT_FOR_CONNECTION;
@ -84,7 +84,7 @@ void ConnectToPeer::asynchronousUpdate()
assert(get_peer_address);
if (get_peer_address->getAddress().isUnset())
return;
m_peer_address.copy(get_peer_address->getAddress());
m_peer_address = get_peer_address->getAddress();
m_current_protocol = nullptr;
if (m_peer_address.isUnset())
{
@ -117,7 +117,7 @@ void ConnectToPeer::asynchronousUpdate()
// The wan remote should already start its ping message to us now
// so we can send packet directly to it.
TransportAddress broadcast_address;
broadcast_address.copy(m_peer_address);
broadcast_address = m_peer_address;
BareNetworkString aloha(std::string("aloha_stk"));
STKHost::get()->sendRawPacket(aloha, broadcast_address);

View File

@ -46,8 +46,8 @@ ConnectToServer::ConnectToServer(std::shared_ptr<Server> server)
{
if (server)
{
m_server = server;
m_server_address.copy(m_server->getAddress());
m_server = server;
m_server_address = m_server->getAddress();
}
setHandleConnections(true);
} // ConnectToServer(server, host)
@ -105,7 +105,7 @@ void ConnectToServer::asynchronousUpdate()
return a->getCurrentPlayers() < b->getCurrentPlayers();
});
m_server = servers[0];
m_server_address.copy(m_server->getAddress());
m_server_address = m_server->getAddress();
}
else
{
@ -435,7 +435,7 @@ void ConnectToServer::waitingAloha(bool is_wan)
if (sender.isPublicAddressLocalhost())
sender.setIP(0x7f000001); // 127.0.0.1
}
m_server_address.copy(sender);
m_server_address = sender;
m_state = CONNECTING;
// Reset timer for next usage
m_timer = 0.0;

View File

@ -264,7 +264,7 @@ void ServerLobby::asynchronousUpdate()
}
else
{
m_server_address.copy(STKHost::get()->getPublicAddress());
m_server_address = STKHost::get()->getPublicAddress();
STKHost::get()->startListening();
m_state = REGISTER_SELF_ADDRESS;
}

View File

@ -145,7 +145,7 @@ void RewindManager::addEvent(EventRewinder *event_rewinder,
* \param buffer Pointer to the event data.
*/
void RewindManager::addNetworkEvent(EventRewinder *event_rewinder,
BareNetworkString *buffer, int ticks)
BareNetworkString *buffer, int ticks)
{
m_rewind_queue.addNetworkEvent(event_rewinder, buffer, ticks);
} // addNetworkEvent

View File

@ -116,11 +116,11 @@ Server::Server(unsigned server_id, const core::stringw &name, int max_players,
m_server_owner = 0;
m_current_players = current_players;
m_max_players = max_players;
m_address.copy(address);
m_address = address;
// In case of LAN server, public and private port are the same.
m_private_port = m_address.getPort();
m_difficulty = (RaceManager::Difficulty)difficulty;
m_server_mode = server_mode;
m_difficulty = (RaceManager::Difficulty)difficulty;
m_server_mode = server_mode;
m_password_protected = password_protected;
m_distance = 0.0f;
} // server(server_id, ...)

View File

@ -31,7 +31,6 @@
#include "utils/time.hpp"
#include <assert.h>
#include <irrString.h>
#include <string>
#define SERVER_REFRESH_INTERVAL 5.0f
@ -136,11 +135,14 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
addr.host = STKHost::HOST_ANY;
addr.port = STKHost::PORT_ANY;
Network *broadcast = new Network(1, 1, 0, 0, &addr);
BareNetworkString s(std::string("stk-server"));
TransportAddress broadcast_address(-1,
NetworkConfig::get()->getServerDiscoveryPort());
broadcast->sendRawPacket(s, broadcast_address);
const std::vector<TransportAddress> &all_bcast =
broadcast->getBroadcastAddresses();
for (auto &bcast_addr : all_bcast)
{
Log::info("Server Discovery", "Broadcasting to %s",
bcast_addr.toString().c_str());
broadcast->sendRawPacket(std::string("stk-server"), bcast_addr);
}
Log::info("ServersManager", "Sent broadcast message.");
@ -153,7 +155,12 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
const auto& servers = ServersManager::get()->getServers();
int cur_server_id = (int)servers.size();
assert(cur_server_id == 0);
std::vector<std::shared_ptr<Server> > servers_now;
// Use a map with the server name as key to automatically remove
// duplicated answers from a server (since we potentially do
// multiple broadcasts). We can not use the sender ip address,
// because e.g. a local client would answer as 127.0.0.1 and
// 192.168.**.
std::map<irr::core::stringw, std::shared_ptr<Server> > servers_now;
while (StkTime::getRealTime() - start_time < DURATION)
{
TransportAddress sender;
@ -178,9 +185,10 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
uint8_t mode = s.getUInt8();
sender.setPort(port);
uint8_t password = s.getUInt8();
servers_now.emplace_back(std::make_shared<Server>
servers_now.emplace(name, std::make_shared<Server>
(cur_server_id++, name, max_players, players,
difficulty, mode, sender, password == 1));
difficulty, mode, sender, password == 1) );
//all_servers.[name] = servers_now.back();
} // if received_data
} // while still waiting
m_success = true;
@ -199,6 +207,20 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
} // getLANRefreshRequest
// ----------------------------------------------------------------------------
/** Takes a mapping of server name to server data (to avoid having the same
* server listed more than once since the client will be doing multiple
* broadcasts to find a server), and converts this into a list of servers.
* \param servers Mapping of server name to Server object.
*/
void ServersManager::setLanServers(const std::map<irr::core::stringw,
std::shared_ptr<Server> >& servers)
{
m_servers.clear();
for (auto i : servers) m_servers.emplace_back(i.second);
m_list_updated = true;
}
// ----------------------------------------------------------------------------
/** Factory function to create either a LAN or a WAN update-of-server
* requests. The current list of servers is also cleared.

View File

@ -19,7 +19,10 @@
#ifndef HEADER_SERVERS_MANAGER_HPP
#define HEADER_SERVERS_MANAGER_HPP
#include <irrString.h>
#include <atomic>
#include <map>
#include <memory>
#include <string>
#include <vector>
@ -48,16 +51,12 @@ private:
// ------------------------------------------------------------------------
void setWanServers(bool success, const XMLNode* input);
// ------------------------------------------------------------------------
void setLanServers(std::vector<std::shared_ptr<Server> >& servers)
{
m_servers = std::move(servers);
m_list_updated = true;
}
// ------------------------------------------------------------------------
Online::XMLRequest* getWANRefreshRequest() const;
// ------------------------------------------------------------------------
Online::XMLRequest* getLANRefreshRequest() const;
// ------------------------------------------------------------------------
void setLanServers(const std::map<irr::core::stringw,
std::shared_ptr<Server> >& servers);
public:
// ------------------------------------------------------------------------
// Singleton

View File

@ -577,12 +577,12 @@ void STKHost::setPublicAddress()
// alternate NAT traversal methods.
if (!xor_addr.isUnset())
{
m_public_address.copy(xor_addr);
m_public_address = xor_addr;
}
else
{
Log::warn("STKHost", "Only non xor-mapped address returned.");
m_public_address.copy(non_xor_addr);
m_public_address = non_xor_addr;
}
// Succeed, save ping
UserConfigParams::m_stun_list[server_name] =

View File

@ -22,7 +22,6 @@
#ifndef HEADER_TRANSPORT_ADDRESS_HPP
#define HEADER_TRANSPORT_ADDRESS_HPP
#include "utils/no_copy.hpp"
#include "utils/string_utils.hpp"
#include "utils/types.hpp"
@ -35,7 +34,7 @@
* \brief Describes a transport-layer address.
* For IP networks, a transport address is the couple ip:port.
*/
class TransportAddress : public NoCopy
class TransportAddress
{
private:
uint32_t m_ip; //!< The IPv4 address
@ -49,6 +48,14 @@ public:
m_port = port;
} // TransportAddress
// ------------------------------------------------------------------------
TransportAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4,
uint16_t port=0)
{
m_ip = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
m_port = port;
} // TransportAddress(uint8_t,...)
// ------------------------------------------------------------------------
/** Construct an transport address from an ENetAddress. */
TransportAddress(const ENetAddress &a)
@ -92,15 +99,6 @@ public:
~TransportAddress() {}
// ------------------------------------------------------------------------
static void unitTesting();
private:
friend class NetworkConfig;
/** The copy constructor is private, so that the friend class
* NetworkConfig can access it to create a copy (getMyAddress), but
* no other class can. */
TransportAddress(const TransportAddress &other)
{
copy(other);
} // TransportAddress(const TransportAddress&)
public:
// ------------------------------------------------------------------------
bool isPublicAddressLocalhost() const;
@ -108,15 +106,6 @@ public:
bool isLAN() const;
// ------------------------------------------------------------------------
bool isUnset() const { return m_ip == 0 || m_port == 0; }
// ------------------------------------------------------------------------
/** A copy function (to replace the copy constructor which is disabled
* using NoCopy): it copies the data from the argument into this object.*/
void copy(const TransportAddress &other)
{
m_ip = other.m_ip;
m_port = other.m_port;
} // copy
// ------------------------------------------------------------------------
/** Resets ip and port to 0. */
void clear()

View File

@ -271,8 +271,8 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name,
return false;
}
m_entered_server_address.copy(
STKHost::get()->getServerPeerForClient()->getAddress());
m_entered_server_address =
STKHost::get()->getServerPeerForClient()->getAddress();
auto cl = LobbyProtocol::create<ClientLobby>();
cl->setAddress(m_entered_server_address);
cl->requestStart();