Clean up direct connection code with full IPv6 support
This commit is contained in:
parent
1e9dab821c
commit
23ad6f09a6
43
src/main.cpp
43
src/main.cpp
@ -616,11 +616,8 @@ void cmdLineHelp()
|
||||
" --lan-server=name Start a LAN server (not a playing client).\n"
|
||||
" --server-password= Sets a password for a server (both client and server).\n"
|
||||
" --connect-now=ip Connect to a server with IP or domain known now\n"
|
||||
" (in format x.x.x.x:xxx(port)), the port should be its\n"
|
||||
" public port.\n"
|
||||
" --connect-now6=ip Connect to a server with IPv6 known now\n"
|
||||
" (in format [x:x:x:x:x:x:x:x]:xxx(port)), the port should be its\n"
|
||||
" public port.\n"
|
||||
" (in format x.x.x.x:xxx(optional port)), the port should be its\n"
|
||||
" public port, you can use [::] to replace x.x.x.x for IPv6 address.\n"
|
||||
" --server-id=n Server id in stk addons for --connect-now.\n"
|
||||
" --network-ai=n Numbers of AI for connecting to linear race server, used\n"
|
||||
" together with --connect-now.\n"
|
||||
@ -1363,11 +1360,9 @@ int handleCmdLine(bool has_server_config, bool has_parent_process)
|
||||
ServerConfig::m_server_configurable = false;
|
||||
}
|
||||
|
||||
std::string ipv4;
|
||||
std::string ipv6;
|
||||
bool has_ipv4 = CommandLine::has("--connect-now", &ipv4);
|
||||
bool has_ipv6 = CommandLine::has("--connect-now6", &ipv6);
|
||||
if (has_ipv4 || has_ipv6)
|
||||
std::string addr;
|
||||
bool has_addr = CommandLine::has("--connect-now", &addr);
|
||||
if (has_addr)
|
||||
{
|
||||
NetworkConfig::get()->setIsServer(false);
|
||||
if (CommandLine::has("--network-ai", &n))
|
||||
@ -1389,24 +1384,22 @@ int handleCmdLine(bool has_server_config, bool has_parent_process)
|
||||
input_manager->getDeviceManager()->getLatestUsedDevice(),
|
||||
PlayerManager::getCurrentPlayer(), HANDICAP_NONE);
|
||||
}
|
||||
std::string fixed_ipv6 = StringUtils::findAndReplace(ipv6, "[", " ");
|
||||
fixed_ipv6 = StringUtils::findAndReplace(fixed_ipv6, "]", " ");
|
||||
auto split_ipv6 = StringUtils::split(fixed_ipv6, ' ');
|
||||
std::string ipv6_port;
|
||||
if (split_ipv6.size() == 3)
|
||||
SocketAddress server_addr(addr);
|
||||
if (server_addr.getIP() == 0 && !server_addr.isIPv6())
|
||||
{
|
||||
ipv4 = "0.0.0.1" + split_ipv6[2];
|
||||
fixed_ipv6 = split_ipv6[1];
|
||||
Log::error("Main", "Invalid server address: %s", addr.c_str());
|
||||
cleanSuperTuxKart();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
fixed_ipv6.clear();
|
||||
TransportAddress server_addr = TransportAddress::fromDomain(ipv4);
|
||||
SocketAddress ipv4_addr = server_addr;
|
||||
if (server_addr.isIPv6())
|
||||
ipv4_addr.setIP(0);
|
||||
auto server = std::make_shared<Server>(0,
|
||||
StringUtils::utf8ToWide(server_addr.toString()), 0, 0, 0, 0,
|
||||
server_addr, !server_password.empty(), false);
|
||||
if (!fixed_ipv6.empty())
|
||||
StringUtils::utf8ToWide(addr), 0, 0, 0, 0, ipv4_addr,
|
||||
!server_password.empty(), false);
|
||||
if (server_addr.isIPv6())
|
||||
{
|
||||
server->setIPV6Address(fixed_ipv6);
|
||||
server->setIPV6Address(server_addr);
|
||||
server->setIPV6Connection(true);
|
||||
}
|
||||
NetworkConfig::get()->doneAddingNetworkPlayers();
|
||||
@ -2587,8 +2580,6 @@ void runUnitTests()
|
||||
GraphicsRestrictions::unitTesting();
|
||||
Log::info("UnitTest", "NetworkString");
|
||||
NetworkString::unitTesting();
|
||||
Log::info("UnitTest", "TransportAddress");
|
||||
TransportAddress::unitTesting();
|
||||
Log::info("UnitTest", "SocketAddress");
|
||||
SocketAddress::unitTesting();
|
||||
Log::info("UnitTest", "StringUtils::versionToInt");
|
||||
|
@ -117,10 +117,9 @@ Network::~Network()
|
||||
} // ~Network
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
ENetPeer *Network::connectTo(const TransportAddress &address)
|
||||
ENetPeer* Network::connectTo(const ENetAddress &address)
|
||||
{
|
||||
const ENetAddress enet_address = address.toEnetAddress();
|
||||
return enet_host_connect(m_host, &enet_address, EVENT_CHANNEL_COUNT, 0);
|
||||
return enet_host_connect(m_host, &address, EVENT_CHANNEL_COUNT, 0);
|
||||
} // connectTo
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
@ -66,7 +66,7 @@ public:
|
||||
static void openLog();
|
||||
static void logPacket(const BareNetworkString &ns, bool incoming);
|
||||
static void closeLog();
|
||||
ENetPeer *connectTo(const TransportAddress &address);
|
||||
ENetPeer *connectTo(const ENetAddress &address);
|
||||
void sendRawPacket(const BareNetworkString &buffer,
|
||||
const TransportAddress& dst);
|
||||
int receiveRawPacket(char *buffer, int buf_len,
|
||||
|
@ -87,7 +87,7 @@ engine.
|
||||
|
||||
*/
|
||||
|
||||
ClientLobby::ClientLobby(const TransportAddress& a, std::shared_ptr<Server> s)
|
||||
ClientLobby::ClientLobby(const ENetAddress& a, std::shared_ptr<Server> s)
|
||||
: LobbyProtocol()
|
||||
{
|
||||
m_auto_started = false;
|
||||
|
@ -21,9 +21,10 @@
|
||||
|
||||
#include "input/input.hpp"
|
||||
#include "network/protocols/lobby_protocol.hpp"
|
||||
#include "network/transport_address.hpp"
|
||||
#include "utils/cpp2011.hpp"
|
||||
|
||||
#include <enet/enet.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@ -71,7 +72,7 @@ private:
|
||||
void handleBadConnection();
|
||||
void becomingServerOwner();
|
||||
|
||||
TransportAddress m_server_address;
|
||||
ENetAddress m_server_address;
|
||||
|
||||
std::shared_ptr<Server> m_server;
|
||||
|
||||
@ -141,7 +142,7 @@ private:
|
||||
bool* is_spectator = NULL) const;
|
||||
void getKartsTracksNetworkString(BareNetworkString* ns);
|
||||
public:
|
||||
ClientLobby(const TransportAddress& a, std::shared_ptr<Server> s);
|
||||
ClientLobby(const ENetAddress& a, std::shared_ptr<Server> s);
|
||||
virtual ~ClientLobby();
|
||||
void doneWithResults();
|
||||
bool receivedServerResult() { return m_received_server_result; }
|
||||
|
@ -49,7 +49,7 @@
|
||||
#include <algorithm>
|
||||
// ============================================================================
|
||||
std::weak_ptr<Online::Request> ConnectToServer::m_previous_unjoin;
|
||||
TransportAddress ConnectToServer::m_server_address;
|
||||
ENetAddress ConnectToServer::m_server_address;
|
||||
int ConnectToServer::m_retry_count = 0;
|
||||
bool ConnectToServer::m_done_intecept = false;
|
||||
// ----------------------------------------------------------------------------
|
||||
@ -60,11 +60,10 @@ ConnectToServer::ConnectToServer(std::shared_ptr<Server> server)
|
||||
: Protocol(PROTOCOL_CONNECTION)
|
||||
{
|
||||
m_quick_play_err_msg = _("No quick play server available.");
|
||||
m_server_address.host = 0;
|
||||
m_server_address.port = 0;
|
||||
if (server)
|
||||
{
|
||||
m_server = server;
|
||||
m_server_address = m_server->getAddress();
|
||||
}
|
||||
m_server = server;
|
||||
} // ConnectToServer(server, host)
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@ -152,7 +151,7 @@ void ConnectToServer::getClientServerInfo()
|
||||
else
|
||||
{
|
||||
assert(port != 0);
|
||||
m_server_address.setPort(port);
|
||||
m_server->setAddress(SocketAddress("127.0.0.1", port));
|
||||
m_server->setPrivatePort(port);
|
||||
if (server_ipv6_socket)
|
||||
{
|
||||
@ -220,7 +219,6 @@ void ConnectToServer::asynchronousUpdate()
|
||||
a->getCurrentPlayers() != a->getMaxPlayers();
|
||||
});
|
||||
m_server = servers[0];
|
||||
m_server_address = m_server->getAddress();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -268,9 +266,20 @@ void ConnectToServer::asynchronousUpdate()
|
||||
}
|
||||
case GOT_SERVER_ADDRESS:
|
||||
{
|
||||
// Detect port from possible connect-now or enter server address
|
||||
// dialog
|
||||
if ((m_server->useIPV6Connection() &&
|
||||
m_server->getIPV6Address()->getPort() == 0) ||
|
||||
(!m_server->useIPV6Connection() &&
|
||||
m_server->getAddress().getPort() == 0))
|
||||
{
|
||||
if (!detectPort())
|
||||
return;
|
||||
}
|
||||
|
||||
if (!STKHost::get()->getPublicAddress().isUnset() &&
|
||||
!STKHost::get()->isClientServer() &&
|
||||
m_server_address.getIP() ==
|
||||
m_server->getAddress().getIP() ==
|
||||
STKHost::get()->getPublicAddress().getIP())
|
||||
{
|
||||
Log::info("ConnectToServer", "Server is in the same lan");
|
||||
@ -366,23 +375,24 @@ int ConnectToServer::interceptCallback(ENetHost* host, ENetEvent* event)
|
||||
host->receivedData[8] == '-' && host->receivedData[9] == 's' &&
|
||||
host->receivedData[10] == 't' && host->receivedData[11] == 'k')
|
||||
{
|
||||
TransportAddress server_addr = host->receivedAddress;
|
||||
if (server_addr != m_server_address)
|
||||
if (host->receivedAddress.host != m_server_address.host ||
|
||||
host->receivedAddress.port != m_server_address.port)
|
||||
{
|
||||
std::string new_address = server_addr.toString();
|
||||
std::string new_address =
|
||||
enetAddressToString(host->receivedAddress);
|
||||
if (isIPv6Socket())
|
||||
{
|
||||
// It was already mapped to real IPv6 before the intercept
|
||||
// callback
|
||||
ENetAddress en_addr = server_addr.toEnetAddress();
|
||||
new_address = "[";
|
||||
new_address += getIPV6ReadableFromMappedAddress
|
||||
(&en_addr) + "]:" + StringUtils::toString(en_addr.port);
|
||||
(&host->receivedAddress) + "]:" +
|
||||
StringUtils::toString(host->receivedAddress.port);
|
||||
}
|
||||
Log::info("ConnectToServer", "Using new server address %s",
|
||||
new_address.c_str());
|
||||
m_retry_count = 15;
|
||||
m_server_address = server_addr;
|
||||
m_server_address = host->receivedAddress;
|
||||
m_done_intecept = true;
|
||||
return 1;
|
||||
}
|
||||
@ -422,10 +432,10 @@ bool ConnectToServer::tryConnect(int timeout, int retry, bool another_port,
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
// From IPv4
|
||||
std::string addr_string =
|
||||
m_server_address.toString(false/*show_port*/);
|
||||
m_server->getAddress().toString(false/*show_port*/);
|
||||
addr_string = NetworkConfig::get()->getNAT64Prefix() + addr_string;
|
||||
std::string port =
|
||||
StringUtils::toString(m_server_address.getPort());
|
||||
StringUtils::toString(m_server->getAddress().getPort());
|
||||
if (getaddrinfo_compat(addr_string.c_str(), port.c_str(),
|
||||
&hints, &res) != 0 || res == NULL)
|
||||
return false;
|
||||
@ -436,8 +446,7 @@ bool ConnectToServer::tryConnect(int timeout, int retry, bool another_port,
|
||||
{
|
||||
struct sockaddr_in6* ipv6_sock =
|
||||
(struct sockaddr_in6*)addr->ai_addr;
|
||||
ENetAddress en_addr = m_server_address.toEnetAddress();
|
||||
addMappedAddress(&en_addr, ipv6_sock);
|
||||
addMappedAddress(&m_server_address, ipv6_sock);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -448,21 +457,26 @@ bool ConnectToServer::tryConnect(int timeout, int retry, bool another_port,
|
||||
SocketAddress* sa = m_server->getIPV6Address();
|
||||
if (!sa)
|
||||
return false;
|
||||
ENetAddress en_addr = m_server_address.toEnetAddress();
|
||||
struct sockaddr_in6* in6 = (struct sockaddr_in6*)sa->getSockaddr();
|
||||
addMappedAddress(&en_addr, in6);
|
||||
addMappedAddress(&m_server_address, in6);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// IPv4 address
|
||||
m_server_address = toENetAddress(m_server->getAddress().getIP(),
|
||||
m_server->getAddress().getPort());
|
||||
}
|
||||
|
||||
while (--m_retry_count >= 0 && !ProtocolManager::lock()->isExiting())
|
||||
{
|
||||
std::string connecting_address = m_server_address.toString();
|
||||
std::string connecting_address = enetAddressToString(m_server_address);
|
||||
if (ipv6)
|
||||
{
|
||||
ENetAddress en_addr = m_server_address.toEnetAddress();
|
||||
connecting_address = "[";
|
||||
connecting_address += getIPV6ReadableFromMappedAddress
|
||||
(&en_addr) + "]:" + StringUtils::toString(en_addr.port);
|
||||
(&m_server_address) + "]:" +
|
||||
StringUtils::toString(m_server->getIPV6Address()->getPort());
|
||||
}
|
||||
ENetPeer* p = nw->connectTo(m_server_address);
|
||||
if (!p)
|
||||
@ -540,3 +554,77 @@ void ConnectToServer::registerWithSTKServer()
|
||||
m_state = DONE;
|
||||
}
|
||||
} // registerWithSTKServer
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
std::string ConnectToServer::enetAddressToString(const ENetAddress& addr)
|
||||
{
|
||||
uint32_t ip = htonl(addr.host);
|
||||
std::string s =
|
||||
StringUtils::insertValues("%d.%d.%d.%d",
|
||||
((ip >> 24) & 0xff), ((ip >> 16) & 0xff),
|
||||
((ip >> 8) & 0xff), ((ip >> 0) & 0xff));
|
||||
s += StringUtils::insertValues(":%d", addr.port);
|
||||
return s;
|
||||
} // enetAddressToString
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
ENetAddress ConnectToServer::toENetAddress(uint32_t ip, uint16_t port)
|
||||
{
|
||||
// because ENet wants little endian
|
||||
ENetAddress a;
|
||||
a.host = ((ip & 0xff000000) >> 24) +
|
||||
((ip & 0x00ff0000) >> 8) + ((ip & 0x0000ff00) << 8) +
|
||||
((ip & 0x000000ff) << 24);
|
||||
a.port = port;
|
||||
return a;
|
||||
} // toENetAddress
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
bool ConnectToServer::detectPort()
|
||||
{
|
||||
ENetAddress ea;
|
||||
ea.host = STKHost::HOST_ANY;
|
||||
ea.port = STKHost::PORT_ANY;
|
||||
Network* nw = new Network(/*peer_count*/1,
|
||||
/*channel_limit*/EVENT_CHANNEL_COUNT,
|
||||
/*max_in_bandwidth*/0, /*max_out_bandwidth*/0, &ea,
|
||||
true/*change_port_if_bound*/);
|
||||
BareNetworkString s(std::string("stk-server-port"));
|
||||
SocketAddress address;
|
||||
if (m_server->useIPV6Connection())
|
||||
address = *m_server->getIPV6Address();
|
||||
else
|
||||
address = m_server->getAddress();
|
||||
address.setPort(stk_config->m_server_discovery_port);
|
||||
nw->sendRawPacket(s, address);
|
||||
SocketAddress sender;
|
||||
const int LEN = 2048;
|
||||
char buffer[LEN];
|
||||
int len = nw->receiveRawPacket(buffer, LEN, &sender, 2000);
|
||||
if (len != 2)
|
||||
{
|
||||
const core::stringw& n = m_server->getName();
|
||||
//I18N: Show the failed detect port server name
|
||||
core::stringw e = _("Failed to detect port number for server %s.", n);
|
||||
delete nw;
|
||||
STKHost::get()->setErrorMessage(e);
|
||||
STKHost::get()->requestShutdown();
|
||||
m_state = EXITING;
|
||||
return false;
|
||||
}
|
||||
BareNetworkString server_port(buffer, len);
|
||||
uint16_t port = server_port.getUInt16();
|
||||
sender.setPort(port);
|
||||
// We replace the server address with sender with the detected port
|
||||
// completely, so we can use input like ff02::1 and then get the correct
|
||||
// local link server address
|
||||
if (m_server->useIPV6Connection())
|
||||
m_server->setIPV6Address(sender);
|
||||
else
|
||||
m_server->setAddress(sender);
|
||||
Log::info("ConnectToServer",
|
||||
"Detected new address %s for server address: %s.",
|
||||
sender.toString().c_str(), address.toString(false).c_str());
|
||||
delete nw;
|
||||
return true;
|
||||
} // detectPort
|
||||
|
@ -20,13 +20,13 @@
|
||||
#define CONNECT_TO_SERVER_HPP
|
||||
|
||||
#include "network/protocol.hpp"
|
||||
#include "network/transport_address.hpp"
|
||||
#include "utils/cpp2011.hpp"
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "irrString.h"
|
||||
#include <enet/enet.h>
|
||||
|
||||
namespace Online
|
||||
{
|
||||
@ -56,10 +56,11 @@ private:
|
||||
void registerWithSTKServer();
|
||||
bool tryConnect(int timeout, int retry, bool another_port = false,
|
||||
bool ipv6 = false);
|
||||
static TransportAddress m_server_address;
|
||||
static ENetAddress m_server_address;
|
||||
static int interceptCallback(ENetHost* host, ENetEvent* event);
|
||||
static int m_retry_count;
|
||||
static bool m_done_intecept;
|
||||
bool detectPort();
|
||||
public:
|
||||
static std::weak_ptr<Online::Request> m_previous_unjoin;
|
||||
ConnectToServer(std::shared_ptr<Server> server);
|
||||
@ -67,7 +68,8 @@ public:
|
||||
virtual void setup() OVERRIDE;
|
||||
virtual void asynchronousUpdate() OVERRIDE;
|
||||
virtual void update(int ticks) OVERRIDE;
|
||||
|
||||
static std::string enetAddressToString(const ENetAddress& addr);
|
||||
static ENetAddress toENetAddress(uint32_t ip, uint16_t port);
|
||||
}; // class ConnectToServer
|
||||
|
||||
#endif // CONNECT_TO_SERVER_HPP
|
||||
|
@ -36,6 +36,7 @@
|
||||
Server::Server(const XMLNode& server_info) : m_supports_encrytion(true)
|
||||
{
|
||||
const XMLNode& xml = *server_info.getNode("server-info");
|
||||
m_address.reset(new SocketAddress());
|
||||
|
||||
m_ipv6_connection = false;
|
||||
m_name = "";
|
||||
@ -61,10 +62,10 @@ Server::Server(const XMLNode& server_info) : m_supports_encrytion(true)
|
||||
xml.get("current_track", &m_current_track);
|
||||
uint32_t ip;
|
||||
xml.get("ip", &ip);
|
||||
m_address.setIP(ip);
|
||||
m_address->setIP(ip);
|
||||
uint16_t port;
|
||||
xml.get("port", &port);
|
||||
m_address.setPort(port);
|
||||
m_address->setPort(port);
|
||||
std::string ipv6_address;
|
||||
xml.get("ipv6", &ipv6_address);
|
||||
if (!ipv6_address.empty())
|
||||
@ -172,7 +173,7 @@ Server::Server(const XMLNode& server_info) : m_supports_encrytion(true)
|
||||
*/
|
||||
Server::Server(unsigned server_id, const core::stringw &name, int max_players,
|
||||
int current_players, unsigned difficulty, unsigned server_mode,
|
||||
const TransportAddress &address, bool password_protected,
|
||||
const SocketAddress &address, bool password_protected,
|
||||
bool game_started, const std::string& current_track)
|
||||
: m_supports_encrytion(false)
|
||||
{
|
||||
@ -183,9 +184,10 @@ 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 = address;
|
||||
m_address.reset(new SocketAddress(address));
|
||||
|
||||
// In case of LAN server, public and private port are the same.
|
||||
m_private_port = m_address.getPort();
|
||||
m_private_port = m_address->getPort();
|
||||
m_difficulty = (RaceManager::Difficulty)difficulty;
|
||||
m_server_mode = server_mode;
|
||||
m_password_protected = password_protected;
|
||||
@ -223,3 +225,9 @@ void Server::setIPV6Address(const SocketAddress& addr)
|
||||
{
|
||||
m_ipv6_address.reset(new SocketAddress(addr));
|
||||
} // setIPV6Address
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void Server::setAddress(const SocketAddress& addr)
|
||||
{
|
||||
m_address.reset(new SocketAddress(addr));
|
||||
} // setAddress
|
||||
|
@ -24,7 +24,6 @@
|
||||
* Represents a server that is joinable
|
||||
*/
|
||||
|
||||
#include "network/transport_address.hpp"
|
||||
#include "race/race_manager.hpp"
|
||||
#include "utils/types.hpp"
|
||||
|
||||
@ -72,7 +71,7 @@ protected:
|
||||
int m_current_players;
|
||||
|
||||
/** The public ip address and port of this server. */
|
||||
TransportAddress m_address;
|
||||
std::unique_ptr<SocketAddress> m_address;
|
||||
|
||||
/** This is the private port of the server. This is used if a WAN game
|
||||
* is started, but one client is discovered on the same LAN, so a direct
|
||||
@ -114,12 +113,14 @@ public:
|
||||
Server(const XMLNode& server_info);
|
||||
Server(unsigned server_id, const irr::core::stringw &name,
|
||||
int max_players, int current_players, unsigned difficulty,
|
||||
unsigned server_mode, const TransportAddress &address,
|
||||
unsigned server_mode, const SocketAddress &address,
|
||||
bool password_protected, bool game_started,
|
||||
const std::string& current_track = "");
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns ip address and port of this server. */
|
||||
const TransportAddress& getAddress() const { return m_address; }
|
||||
/** Returns IPv4 address and port of this server. */
|
||||
const SocketAddress& getAddress() const { return *m_address.get(); }
|
||||
// ------------------------------------------------------------------------
|
||||
void setAddress(const SocketAddress& addr);
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the lower case name of the server. */
|
||||
const std::string& getLowerCaseName() const { return m_lower_case_name; }
|
||||
|
@ -227,7 +227,7 @@ std::shared_ptr<Online::XMLRequest> ServersManager::getLANRefreshRequest() const
|
||||
}
|
||||
auto server = std::make_shared<Server>(cur_server_id++,
|
||||
name, max_players, players, difficulty, mode,
|
||||
TransportAddress(sender.getIP(), sender.getPort()),
|
||||
SocketAddress(sender.getIP(), sender.getPort()),
|
||||
password == 1, game_started == 1, current_track);
|
||||
if (sender.isIPv6())
|
||||
{
|
||||
|
@ -1267,12 +1267,13 @@ std::shared_ptr<STKPeer> STKHost::getServerPeerForClient() const
|
||||
/** \brief Tells if a peer is known and connected.
|
||||
* \return True if the peer is known and connected, false elseway.
|
||||
*/
|
||||
bool STKHost::isConnectedTo(const TransportAddress& peer)
|
||||
bool STKHost::isConnectedTo(const ENetAddress& peer)
|
||||
{
|
||||
ENetHost *host = m_network->getENetHost();
|
||||
for (unsigned int i = 0; i < host->peerCount; i++)
|
||||
{
|
||||
if (peer == host->peers[i].address &&
|
||||
if (peer.host == host->peers[i].address.host &&
|
||||
peer.port == host->peers[i].address.port &&
|
||||
host->peers[i].state == ENET_PEER_STATE_CONNECTED)
|
||||
{
|
||||
return true;
|
||||
|
@ -273,7 +273,7 @@ public:
|
||||
// ------------------------------------------------------------------------
|
||||
bool peerExists(const SocketAddress& peer_address);
|
||||
// ------------------------------------------------------------------------
|
||||
bool isConnectedTo(const TransportAddress& peer_address);
|
||||
bool isConnectedTo(const ENetAddress& peer_address);
|
||||
// ------------------------------------------------------------------------
|
||||
std::shared_ptr<STKPeer> getServerPeerForClient() const;
|
||||
// ------------------------------------------------------------------------
|
||||
|
@ -364,8 +364,7 @@ void addMappedAddress(const ENetAddress* ea, const struct sockaddr_in6* in6)
|
||||
#else
|
||||
|
||||
#include "network/stk_ipv6.hpp"
|
||||
#include "network/network_config.hpp"
|
||||
#include "network/transport_address.hpp"
|
||||
#include "network/protocols/connect_to_server.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "utils/log.hpp"
|
||||
#include "utils/time.hpp"
|
||||
@ -480,7 +479,6 @@ void getMappedFromIPV6(const struct sockaddr_in6* in6, ENetAddress* ea)
|
||||
{
|
||||
ea->host = ((in_addr*)(in6->sin6_addr.s6_addr + 12))->s_addr;
|
||||
ea->port = ntohs(in6->sin6_port);
|
||||
TransportAddress addr(*ea);
|
||||
addMappedAddress(ea, in6);
|
||||
}
|
||||
else
|
||||
@ -488,10 +486,11 @@ void getMappedFromIPV6(const struct sockaddr_in6* in6, ENetAddress* ea)
|
||||
// Create a fake IPv4 address of 0.x.x.x if it's a real IPv6 connection
|
||||
if (g_mapped_ipv6_used >= 16777215)
|
||||
g_mapped_ipv6_used = 0;
|
||||
TransportAddress addr(++g_mapped_ipv6_used, ntohs(in6->sin6_port));
|
||||
*ea = addr.toEnetAddress();
|
||||
*ea = ConnectToServer::toENetAddress(++g_mapped_ipv6_used,
|
||||
ntohs(in6->sin6_port));
|
||||
Log::debug("IPv6", "Fake IPv4 address %s mapped to %s",
|
||||
addr.toString().c_str(), getIPV6ReadableFromIn6(in6).c_str());
|
||||
ConnectToServer::enetAddressToString(*ea).c_str(),
|
||||
getIPV6ReadableFromIn6(in6).c_str());
|
||||
addMappedAddress(ea, in6);
|
||||
}
|
||||
} // getMappedFromIPV6
|
||||
@ -506,10 +505,9 @@ void removeDisconnectedMappedAddress()
|
||||
{
|
||||
if (it->m_last_activity + 20000 < StkTime::getMonoTimeMs())
|
||||
{
|
||||
TransportAddress addr(it->m_addr);
|
||||
Log::debug("IPv6", "Removing expired %s, IPv4 address %s.",
|
||||
getIPV6ReadableFromIn6(&it->m_in6).c_str(),
|
||||
addr.toString().c_str());
|
||||
ConnectToServer::enetAddressToString(it->m_addr).c_str());
|
||||
it = g_mapped_ips.erase(it);
|
||||
Log::debug("IPv6", "Mapped address size now: %d.",
|
||||
g_mapped_ips.size());
|
||||
|
@ -295,7 +295,7 @@ void CreateServerScreen::createServer()
|
||||
ServerConfig::m_private_server_password = password;
|
||||
password = std::string(" --server-password=") + password;
|
||||
|
||||
TransportAddress server_address(0x7f000001,
|
||||
SocketAddress server_address(0x7f000001,
|
||||
stk_config->m_server_discovery_port);
|
||||
|
||||
auto server = std::make_shared<Server>(0/*server_id*/, name,
|
||||
|
@ -28,7 +28,6 @@
|
||||
#include "guiengine/widgets/text_box_widget.hpp"
|
||||
#include "input/device_manager.hpp"
|
||||
#include "network/event.hpp"
|
||||
#include "network/network.hpp"
|
||||
#include "network/network_config.hpp"
|
||||
#include "network/server.hpp"
|
||||
#include "network/server_config.hpp"
|
||||
@ -243,60 +242,35 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name,
|
||||
[this] (GUIEngine::LabelWidget* lw,
|
||||
GUIEngine::TextBoxWidget* tb)->bool
|
||||
{
|
||||
TransportAddress server_addr =
|
||||
TransportAddress::fromDomain(
|
||||
StringUtils::wideToUtf8(tb->getText()));
|
||||
if (server_addr.getIP() == 0)
|
||||
core::stringw addr_w = tb->getText();
|
||||
std::string addr_u = StringUtils::wideToUtf8(addr_w);
|
||||
SocketAddress server_addr(addr_u);
|
||||
if (server_addr.getIP() == 0 && !server_addr.isIPv6())
|
||||
{
|
||||
m_entered_server_address = L"";
|
||||
core::stringw err = _("Invalid server address: %s.",
|
||||
tb->getText());
|
||||
addr_w);
|
||||
lw->setText(err, true);
|
||||
return false;
|
||||
}
|
||||
if (server_addr.getPort() == 0)
|
||||
SocketAddress ipv4_addr = server_addr;
|
||||
if (server_addr.isIPv6())
|
||||
ipv4_addr.setIP(0);
|
||||
auto server = std::make_shared<Server>(0, addr_w, 0, 0, 0, 0,
|
||||
ipv4_addr, false, false);
|
||||
if (server_addr.isIPv6())
|
||||
{
|
||||
ENetAddress ea;
|
||||
ea.host = STKHost::HOST_ANY;
|
||||
ea.port = STKHost::PORT_ANY;
|
||||
Network* nw = new Network(/*peer_count*/1,
|
||||
/*channel_limit*/EVENT_CHANNEL_COUNT,
|
||||
/*max_in_bandwidth*/0, /*max_out_bandwidth*/0, &ea,
|
||||
true/*change_port_if_bound*/);
|
||||
BareNetworkString s(std::string("stk-server-port"));
|
||||
TransportAddress address(server_addr.getIP(),
|
||||
stk_config->m_server_discovery_port);
|
||||
nw->sendRawPacket(s, address);
|
||||
TransportAddress sender;
|
||||
const int LEN = 2048;
|
||||
char buffer[LEN];
|
||||
int len = nw->receiveRawPacket(buffer, LEN, &sender, 2000);
|
||||
if (len != 2)
|
||||
{
|
||||
//I18N: In enter server ip address dialog
|
||||
core::stringw err = _("Failed to detect port number.");
|
||||
lw->setText(err, true);
|
||||
delete nw;
|
||||
return false;
|
||||
}
|
||||
BareNetworkString server_port(buffer, len);
|
||||
uint16_t port = server_port.getUInt16();
|
||||
server_addr.setPort(port);
|
||||
Log::info("OnlineScreen",
|
||||
"Detected port %d for server address: %s.",
|
||||
port, server_addr.toString(false).c_str());
|
||||
delete nw;
|
||||
server->setIPV6Address(server_addr);
|
||||
server->setIPV6Connection(true);
|
||||
}
|
||||
auto server = std::make_shared<Server>(0,
|
||||
StringUtils::utf8ToWide(server_addr.toString()), 0, 0, 0, 0,
|
||||
server_addr, false, false);
|
||||
m_entered_server = server;
|
||||
m_entered_server_address = server_addr;
|
||||
m_entered_server_address =
|
||||
StringUtils::utf8ToWide(server_addr.toString());
|
||||
return true;
|
||||
});
|
||||
if (!m_entered_server_address.isUnset())
|
||||
if (!m_entered_server_address.empty())
|
||||
{
|
||||
gtfd->getTextField()->setText(StringUtils::utf8ToWide(
|
||||
m_entered_server_address.toString()));
|
||||
gtfd->getTextField()->setText(m_entered_server_address);
|
||||
}
|
||||
}
|
||||
} // eventCallback
|
||||
|
@ -19,11 +19,11 @@
|
||||
#define HEADER_ONLINE_SCREEN_HPP
|
||||
|
||||
#include "guiengine/screen.hpp"
|
||||
#include "network/transport_address.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Server;
|
||||
class SocketAddress;
|
||||
|
||||
namespace GUIEngine { class CheckBoxWidget; class ListWidget;
|
||||
class ButtonWidget; class IconButtonWidget; }
|
||||
@ -51,7 +51,7 @@ private:
|
||||
|
||||
std::shared_ptr<Server> m_entered_server;
|
||||
|
||||
TransportAddress m_entered_server_address;
|
||||
core::stringw m_entered_server_address;
|
||||
|
||||
OnlineScreen();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user