Allow IPV6 only in iOS to connect to game server not behind a firewall

This commit is contained in:
Benau 2019-07-27 15:48:21 +08:00
parent 0b1da5a1d0
commit c9656d4410
4 changed files with 185 additions and 39 deletions

View File

@ -60,6 +60,9 @@ static enet_uint32 timeBase = 0;
#ifdef IOS_STK #ifdef IOS_STK
extern void iOSInitialize(void); extern void iOSInitialize(void);
extern int isIPV6Only(void);
extern void getSynthesizedAddress(const ENetAddress* ea, struct sockaddr_in6* in6);
extern void getIPV4FromSynthesized(const struct sockaddr_in6* in6, ENetAddress* ea);
#endif #endif
int int
@ -195,6 +198,21 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng
int int
enet_socket_bind (ENetSocket socket, const ENetAddress * address) enet_socket_bind (ENetSocket socket, const ENetAddress * address)
{
#ifdef IOS_STK
// In STK we only bind port and listen to any address
if (isIPV6Only())
{
struct sockaddr_in6 sin;
memset (&sin, 0, sizeof (struct sockaddr_in6));
sin.sin6_family = AF_INET6;
sin.sin6_addr = in6addr_any;
sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
return bind (socket, (struct sockaddr *) & sin,
sizeof (struct sockaddr_in6));
}
else
#endif
{ {
struct sockaddr_in sin; struct sockaddr_in sin;
@ -217,6 +235,7 @@ enet_socket_bind (ENetSocket socket, const ENetAddress * address)
(struct sockaddr *) & sin, (struct sockaddr *) & sin,
sizeof (struct sockaddr_in)); sizeof (struct sockaddr_in));
} }
}
int int
enet_socket_get_address (ENetSocket socket, ENetAddress * address) enet_socket_get_address (ENetSocket socket, ENetAddress * address)
@ -242,7 +261,12 @@ enet_socket_listen (ENetSocket socket, int backlog)
ENetSocket ENetSocket
enet_socket_create (ENetSocketType type) enet_socket_create (ENetSocketType type)
{ {
return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); #ifdef IOS_STK
int af_family = isIPV6Only() == 1 ? PF_INET6 : PF_INET;
#else
int af_family = PF_INET;
#endif
return socket (af_family, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
} }
int int
@ -384,11 +408,24 @@ enet_socket_send (ENetSocket socket,
{ {
struct msghdr msgHdr; struct msghdr msgHdr;
struct sockaddr_in sin; struct sockaddr_in sin;
#ifdef IOS_STK
struct sockaddr_in6 sin6;
#endif
int sentLength; int sentLength;
memset (& msgHdr, 0, sizeof (struct msghdr)); memset (& msgHdr, 0, sizeof (struct msghdr));
if (address != NULL) if (address != NULL)
{
#ifdef IOS_STK
if (isIPV6Only())
{
getSynthesizedAddress(address, &sin6);
msgHdr.msg_name = & sin6;
msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
}
else
#endif
{ {
memset (& sin, 0, sizeof (struct sockaddr_in)); memset (& sin, 0, sizeof (struct sockaddr_in));
@ -399,6 +436,7 @@ enet_socket_send (ENetSocket socket,
msgHdr.msg_name = & sin; msgHdr.msg_name = & sin;
msgHdr.msg_namelen = sizeof (struct sockaddr_in); msgHdr.msg_namelen = sizeof (struct sockaddr_in);
} }
}
msgHdr.msg_iov = (struct iovec *) buffers; msgHdr.msg_iov = (struct iovec *) buffers;
msgHdr.msg_iovlen = bufferCount; msgHdr.msg_iovlen = bufferCount;
@ -424,15 +462,28 @@ enet_socket_receive (ENetSocket socket,
{ {
struct msghdr msgHdr; struct msghdr msgHdr;
struct sockaddr_in sin; struct sockaddr_in sin;
#ifdef IOS_STK
struct sockaddr_in6 sin6;
#endif
int recvLength; int recvLength;
memset (& msgHdr, 0, sizeof (struct msghdr)); memset (& msgHdr, 0, sizeof (struct msghdr));
if (address != NULL) if (address != NULL)
{
#ifdef IOS_STK
if (isIPV6Only())
{
msgHdr.msg_name = & sin6;
msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
}
else
#endif
{ {
msgHdr.msg_name = & sin; msgHdr.msg_name = & sin;
msgHdr.msg_namelen = sizeof (struct sockaddr_in); msgHdr.msg_namelen = sizeof (struct sockaddr_in);
} }
}
msgHdr.msg_iov = (struct iovec *) buffers; msgHdr.msg_iov = (struct iovec *) buffers;
msgHdr.msg_iovlen = bufferCount; msgHdr.msg_iovlen = bufferCount;
@ -453,10 +504,19 @@ enet_socket_receive (ENetSocket socket,
#endif #endif
if (address != NULL) if (address != NULL)
{
#ifdef IOS_STK
if (isIPV6Only())
{
getIPV4FromSynthesized(&sin6, address);
}
else
#endif
{ {
address -> host = (enet_uint32) sin.sin_addr.s_addr; address -> host = (enet_uint32) sin.sin_addr.s_addr;
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
} }
}
return recvLength; return recvLength;
} }

View File

@ -20,13 +20,10 @@
#ifdef IOS_STK #ifdef IOS_STK
#include "network/ios_ipv6.hpp" #include "network/ios_ipv6.hpp"
#include "config/stk_config.hpp" #include "network/transport_address.hpp"
#include "config/user_config.hpp"
#include "utils/string_utils.hpp" #include "utils/string_utils.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include <enet/enet.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <err.h> #include <err.h>
#include <netdb.h> #include <netdb.h>
@ -48,8 +45,9 @@
#define NAME_SVR_LEN (10) #define NAME_SVR_LEN (10)
bool g_ipv6_only; int g_ipv6_only;
ENetAddress g_connected_address; struct sockaddr_in6 g_connected_ipv6;
ENetAddress g_connected_ipv4;
namespace Mars namespace Mars
{ {
@ -273,7 +271,7 @@ namespace Mars
} }
} }
bool isIPV6Only() int isIPV6Only()
{ {
return g_ipv6_only; return g_ipv6_only;
} }
@ -281,13 +279,86 @@ bool isIPV6Only()
void iOSInitialize() void iOSInitialize()
{ {
// Clear previous setting, in case user changed wifi or mobile data // Clear previous setting, in case user changed wifi or mobile data
g_ipv6_only = false; g_ipv6_only = 0;
g_connected_address.host = 0; g_connected_ipv4.host = 0;
g_connected_address.port = 0; g_connected_ipv4.port = 0;
memset(&g_connected_ipv6, 0, sizeof(struct sockaddr_in6));
using namespace Mars; using namespace Mars;
int ipstack = local_ipstack_detect(); int ipstack = local_ipstack_detect();
g_ipv6_only = ipstack == ELocalIPStack_IPv6; if (ipstack == ELocalIPStack_IPv6)
g_ipv6_only = 1;
} // iOSInitialize } // iOSInitialize
void getSynthesizedAddress(const ENetAddress* ea, struct sockaddr_in6* in6)
{
if (ea->host != g_connected_ipv4.host &&
ea->port != g_connected_ipv4.port)
{
g_connected_ipv4.host = ea->host;
g_connected_ipv4.port = ea->port;
memset(&g_connected_ipv6, 0, sizeof(struct sockaddr_in6));
g_connected_ipv6.sin6_family = AF_INET6;
TransportAddress addr(*ea);
// The ability to synthesize IPv6 addresses was added to getaddrinfo in iOS 9.2
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
// Resolve the stun server name so we can send it a STUN request
const std::string& ipv4 = addr.toString(false/*show_port*/);
int status = getaddrinfo(ipv4.c_str(), StringUtils::toString(ea->port).c_str(),
&hints, &res);
if (status != 0)
{
Log::error("STKHost", "Error in getaddrinfo for synthesizing ipv6"
" %s: %s", ipv4.c_str(), gai_strerror(status));
return;
}
assert(res != NULL);
for (const struct addrinfo* addr = res; addr != NULL; addr = addr->ai_next)
{
if (addr->ai_family == AF_INET6)
{
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)addr->ai_addr;
g_connected_ipv6.sin6_addr = ipv6->sin6_addr;
// Workaround in iOS 9 where port is not written (fixed in iOS 10)
if (ipv6->sin6_port == 0)
ipv6->sin6_port = htons(ea->port);
g_connected_ipv6.sin6_port = ipv6->sin6_port;
break;
}
}
freeaddrinfo(res);
}
memcpy(in6, &g_connected_ipv6, sizeof(struct sockaddr_in6));
}
bool sameIPV6(const struct in6_addr* a, const struct in6_addr* b)
{
for (unsigned i = 0; i < sizeof(struct in6_addr); i++)
{
if (a->s6_addr[i] != b->s6_addr[i])
return false;
}
return true;
}
void getIPV4FromSynthesized(const struct sockaddr_in6* in6, ENetAddress* ea)
{
if (sameIPV6(&(in6->sin6_addr), &(g_connected_ipv6.sin6_addr)) &&
in6->sin6_port == g_connected_ipv6.sin6_port)
{
ea->host = g_connected_ipv4.host;
ea->port = g_connected_ipv4.port;
}
else
{
ea->host = 0;
ea->port = 0;
}
}
#endif #endif

View File

@ -15,11 +15,15 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <enet/enet.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
bool isIPV6Only(); int isIPV6Only();
void iOSInitialize(); void iOSInitialize();
void getSynthesizedAddress(const ENetAddress* ea, struct sockaddr_in6* in6);
void getIPV4FromSynthesized(const struct sockaddr_in6* in6, ENetAddress* ea);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -23,6 +23,7 @@
#include "io/file_manager.hpp" #include "io/file_manager.hpp"
#include "network/event.hpp" #include "network/event.hpp"
#include "network/game_setup.hpp" #include "network/game_setup.hpp"
#include "network/ios_ipv6.hpp"
#include "network/network_config.hpp" #include "network/network_config.hpp"
#include "network/network_console.hpp" #include "network/network_console.hpp"
#include "network/network_player_profile.hpp" #include "network/network_player_profile.hpp"
@ -369,6 +370,16 @@ void STKHost::shutdown()
*/ */
void STKHost::setPublicAddress() void STKHost::setPublicAddress()
{ {
#ifdef IOS_STK
if (isIPV6Only())
{
// IPV6 only in iOS doesn't support connection to firewalled server,
// so no need to test STUN
Log::info("STKHost", "IPV6 only environment detected.");
m_public_address = TransportAddress("169.254.0.0:65535");
return;
}
#endif
std::vector<std::pair<std::string, uint32_t> > untried_server; std::vector<std::pair<std::string, uint32_t> > untried_server;
for (auto& p : UserConfigParams::m_stun_servers) for (auto& p : UserConfigParams::m_stun_servers)
untried_server.push_back(p); untried_server.push_back(p);