Allow setting a zero address as long as there is an IPv6 address

This commit is contained in:
Benau 2020-01-25 11:15:34 +08:00
parent 6d2e060bf2
commit d2f2c6cf1f
4 changed files with 44 additions and 20 deletions

View File

@ -255,13 +255,14 @@ void ConnectToServer::asynchronousUpdate()
registerWithSTKServer(); registerWithSTKServer();
} }
// Set to DONE will stop STKHost is not connected // Set to DONE will stop STKHost is not connected
m_state = STKHost::get()->getPublicAddress().isUnset() ? m_state = STKHost::get()->getVaildPublicAddress().empty() ?
DONE : GOT_SERVER_ADDRESS; DONE : GOT_SERVER_ADDRESS;
break; break;
} }
case GOT_SERVER_ADDRESS: case GOT_SERVER_ADDRESS:
{ {
if (!STKHost::get()->isClientServer() && if (!STKHost::get()->getPublicAddress().isUnset() &&
!STKHost::get()->isClientServer() &&
m_server_address.getIP() == m_server_address.getIP() ==
STKHost::get()->getPublicAddress().getIP()) STKHost::get()->getPublicAddress().getIP())
{ {
@ -314,8 +315,7 @@ void ConnectToServer::update(int ticks)
{ {
// lobby room protocol if we're connected only // lobby room protocol if we're connected only
if (STKHost::get()->getPeerCount() > 0 && if (STKHost::get()->getPeerCount() > 0 &&
STKHost::get()->getServerPeerForClient()->isConnected() && STKHost::get()->getServerPeerForClient()->isConnected())
!m_server_address.isUnset())
{ {
// Let main thread create ClientLobby for better // Let main thread create ClientLobby for better
// synchronization with GUI // synchronization with GUI
@ -493,7 +493,7 @@ void ConnectToServer::registerWithSTKServer()
NetworkConfig::get()->setServerDetails(request, "join-server-key"); NetworkConfig::get()->setServerDetails(request, "join-server-key");
request->addParameter("server-id", m_server->getServerId()); request->addParameter("server-id", m_server->getServerId());
request->addParameter("address", addr.getIP()); request->addParameter("address", addr.getIP());
request->addParameter("address_ipv6", request->addParameter("address-ipv6",
STKHost::get()->getPublicIPV6Address()); STKHost::get()->getPublicIPV6Address());
request->addParameter("port", addr.getPort()); request->addParameter("port", addr.getPort());
@ -502,9 +502,7 @@ void ConnectToServer::registerWithSTKServer()
request->addParameter("aes-iv", Crypto::getClientIV()); request->addParameter("aes-iv", Crypto::getClientIV());
Log::info("ConnectToServer", "Registering addr %s", Log::info("ConnectToServer", "Registering addr %s",
STKHost::get()->getPublicIPV6Address().empty() ? STKHost::get()->getVaildPublicAddress().c_str());
addr.toString().c_str() :
STKHost::get()->getPublicIPV6Address().c_str());
// This can be done blocking: till we are registered with the // This can be done blocking: till we are registered with the
// stk server, there is no need to to react to any other // stk server, there is no need to to react to any other

View File

@ -1372,7 +1372,8 @@ void ServerLobby::asynchronousUpdate()
{ {
STKHost::get()->setPublicAddress(true/*ipv4*/); STKHost::get()->setPublicAddress(true/*ipv4*/);
} }
if (STKHost::get()->getPublicAddress().isUnset()) if (STKHost::get()->getPublicAddress().isUnset() &&
STKHost::get()->getPublicIPV6Address().empty())
{ {
m_state = ERROR_LEAVE; m_state = ERROR_LEAVE;
} }
@ -2242,14 +2243,18 @@ bool ServerLobby::registerServer(bool now)
request->addParameter("password", (unsigned)(!pw.empty())); request->addParameter("password", (unsigned)(!pw.empty()));
request->addParameter("version", (unsigned)ServerConfig::m_server_version); request->addParameter("version", (unsigned)ServerConfig::m_server_version);
Log::info("ServerLobby", "Public server address %s", bool ipv6_only = m_server_address.isUnset();
m_server_address.toString().c_str()); if (!ipv6_only)
{
Log::info("ServerLobby", "Public server address %s",
m_server_address.toString().c_str());
}
if (!STKHost::get()->getPublicIPV6Address().empty()) if (!STKHost::get()->getPublicIPV6Address().empty())
{ {
request->addParameter("address_ipv6", request->addParameter("address_ipv6",
STKHost::get()->getPublicIPV6Address()); STKHost::get()->getPublicIPV6Address());
Log::info("ServerLobby", "Public IPv6 server address %s", Log::info("ServerLobby", "Public IPv6 server address %s",
STKHost::get()->getPublicIPV6Address().c_str()); STKHost::get()->getVaildPublicAddress().c_str());
} }
if (now) if (now)
{ {
@ -2278,8 +2283,18 @@ void ServerLobby::unregisterServer(bool now)
request->addParameter("address", m_server_address.getIP()); request->addParameter("address", m_server_address.getIP());
request->addParameter("port", m_server_address.getPort()); request->addParameter("port", m_server_address.getPort());
Log::info("ServerLobby", "Unregister server address %s", bool ipv6_only = m_server_address.isUnset();
m_server_address.toString().c_str()); if (!ipv6_only)
{
Log::info("ServerLobby", "Unregister server address %s",
m_server_address.toString().c_str());
}
else
{
Log::info("ServerLobby", "Unregister server address %s",
STKHost::get()->getVaildPublicAddress().c_str());
}
// No need to check for result as server will be auto-cleared anyway // No need to check for result as server will be auto-cleared anyway
// when no polling is done // when no polling is done
if (now) if (now)

View File

@ -693,12 +693,6 @@ void STKHost::getIPFromStun(int socket, const std::string& stun_address,
*/ */
void STKHost::setPublicAddress(bool ipv4) void STKHost::setPublicAddress(bool ipv4)
{ {
if (isIPv6Socket() && !ipv4)
{
// Set an unused IPv4 first for possible IPv6 only network
m_public_address = TransportAddress("169.254.0.0:65535");
}
auto& stun_map = ipv4 ? UserConfigParams::m_stun_servers_v4 : auto& stun_map = ipv4 ? UserConfigParams::m_stun_servers_v4 :
UserConfigParams::m_stun_servers; UserConfigParams::m_stun_servers;
std::vector<std::pair<std::string, uint32_t> > untried_server; std::vector<std::pair<std::string, uint32_t> > untried_server;
@ -1682,3 +1676,18 @@ bool STKHost::hasServerAI() const
{ {
return NetworkConfig::get()->isServer() && m_separate_process != NULL; return NetworkConfig::get()->isServer() && m_separate_process != NULL;
} // hasServerAI } // hasServerAI
// ----------------------------------------------------------------------------
/** Return an valid public IPv4 or IPv6 address with port, empty if both are
* unset, IPv6 will come first if both exists. */
std::string STKHost::getVaildPublicAddress() const
{
if (!m_public_ipv6_address.empty() && m_public_address.getPort() != 0)
{
return std::string("[") + m_public_ipv6_address + "]:" +
StringUtils::toString(m_public_address.getPort());
}
if (!m_public_address.isUnset())
return m_public_address.toString();
return "";
} // getVaildPublicAddress

View File

@ -206,6 +206,8 @@ public:
const std::string& getPublicIPV6Address() const const std::string& getPublicIPV6Address() const
{ return m_public_ipv6_address; } { return m_public_ipv6_address; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
std::string getVaildPublicAddress() const;
// ------------------------------------------------------------------------
const TransportAddress& getStunAddress() const { return m_stun_address; } const TransportAddress& getStunAddress() const { return m_stun_address; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
uint16_t getPrivatePort() const { return m_private_port; } uint16_t getPrivatePort() const { return m_private_port; }