Allow connect to server with domain in IPv4, fixed #3969

This commit is contained in:
Benau 2019-12-14 09:52:04 +08:00
parent 5273dac899
commit 49651cca4f
4 changed files with 61 additions and 4 deletions

View File

@ -612,7 +612,7 @@ void cmdLineHelp()
" --public-server Allow direct connection to the server (without stk server)\n"
" --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 known now\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"
@ -1397,7 +1397,7 @@ int handleCmdLine(bool has_server_config, bool has_parent_process)
}
else
fixed_ipv6.clear();
TransportAddress server_addr(ipv4);
TransportAddress server_addr = TransportAddress::fromDomain(ipv4);
auto server = std::make_shared<Server>(0,
StringUtils::utf8ToWide(server_addr.toString()), 0, 0, 0, 0,
server_addr, !server_password.empty(), false);

View File

@ -17,6 +17,7 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/transport_address.hpp"
#include "network/stk_ipv6.hpp"
#include "utils/log.hpp"
#include "utils/string_utils.hpp"
@ -29,6 +30,59 @@
# include <errno.h>
#endif
#if defined(WIN32)
# include "ws2tcpip.h"
# define inet_ntop InetNtop
#else
# include <arpa/inet.h>
# include <errno.h>
# include <sys/socket.h>
#endif
#ifdef __MINGW32__
# undef _WIN32_WINNT
# define _WIN32_WINNT 0x501
#endif
#ifdef WIN32
# include <winsock2.h>
# include <ws2tcpip.h>
#else
# include <netdb.h>
#endif
#include <sys/types.h>
// ----------------------------------------------------------------------------
/* Return TransportAddress (with optionally port) from string, it can also be
* used with an ipv4 string too. */
TransportAddress TransportAddress::fromDomain(const std::string& str)
{
TransportAddress result;
auto split = StringUtils::split(str, ':');
if (split.empty())
return result;
struct addrinfo hints;
struct addrinfo* res = NULL;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
int status = getaddrinfo_compat(split[0].c_str(),
split.size() > 1 ? split[1].c_str() : NULL, &hints, &res);
if (status != 0 || res == NULL)
{
Log::error("TransportAddress", "Error in getaddrinfo for fromDomain"
" %s: %s", split[0].c_str(), gai_strerror(status));
return result;
}
struct sockaddr_in* ipv4_addr = (struct sockaddr_in*)(res->ai_addr);
result.setIP(ntohl(ipv4_addr->sin_addr.s_addr));
result.setPort(ntohs(ipv4_addr->sin_port));
freeaddrinfo(res);
return result;
} // fromDomain
// ----------------------------------------------------------------------------
/** Construct an IO address from a string in the format x.x.x.x with a
* port number. */

View File

@ -72,6 +72,8 @@ public:
// ------------------------------------------------------------------------
static void unitTesting();
public:
// ------------------------------------------------------------------------
static TransportAddress fromDomain(const std::string& str);
// ------------------------------------------------------------------------
bool isPublicAddressLocalhost() const;
// ------------------------------------------------------------------------

View File

@ -235,14 +235,15 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name,
return;
}
core::stringw instruction =
_("Enter the server address with IP optionally followed by : and"
_("Enter the server address optionally followed by : and"
" then port.");
auto gtfd = new GeneralTextFieldDialog(instruction.c_str(),
[] (const irr::core::stringw& text) {},
[this] (GUIEngine::LabelWidget* lw,
GUIEngine::TextBoxWidget* tb)->bool
{
TransportAddress server_addr(
TransportAddress server_addr =
TransportAddress::fromDomain(
StringUtils::wideToUtf8(tb->getText()));
if (server_addr.getIP() == 0)
{