Always use IPv6 for the server if a "dual stack" is available. This means a server on vista (as well as most linux systems) will support IPv6 by default. IPv4 requests will be mapped by the OS.
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
#include <net/socket_helper.h>
|
#include <net/socket_helper.h>
|
||||||
#include <net/serverexception.h>
|
#include <net/serverexception.h>
|
||||||
#include <net/socket_msg.h>
|
#include <net/socket_msg.h>
|
||||||
|
#include <net/socket_startup.h>
|
||||||
|
|
||||||
#define ACCEPT_TIMEOUT_MSEC 50
|
#define ACCEPT_TIMEOUT_MSEC 50
|
||||||
#define NET_SERVER_LISTEN_BACKLOG 5
|
#define NET_SERVER_LISTEN_BACKLOG 5
|
||||||
@@ -50,7 +51,9 @@ ServerAcceptThread::Init(unsigned serverPort, bool ipv6, bool sctp, const std::s
|
|||||||
ServerContext &context = GetContext();
|
ServerContext &context = GetContext();
|
||||||
|
|
||||||
context.SetProtocol(sctp ? SOCKET_IPPROTO_SCTP : 0);
|
context.SetProtocol(sctp ? SOCKET_IPPROTO_SCTP : 0);
|
||||||
context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET);
|
// If a "dual stack" is available, the pokerth server always uses ipv6.
|
||||||
|
// In this case, ipv4 requests will be mapped to ipv6.
|
||||||
|
context.SetAddrFamily(socket_has_dual_stack() ? AF_INET6 : (ipv6 ? AF_INET6 : AF_INET));
|
||||||
context.SetServerPort(serverPort);
|
context.SetServerPort(serverPort);
|
||||||
|
|
||||||
GetLobbyThread().Init(pwd);
|
GetLobbyThread().Init(pwd);
|
||||||
@@ -105,11 +108,14 @@ ServerAcceptThread::Listen()
|
|||||||
if (IOCTLSOCKET(context.GetSocket(), FIONBIO, &mode) == SOCKET_ERROR)
|
if (IOCTLSOCKET(context.GetSocket(), FIONBIO, &mode) == SOCKET_ERROR)
|
||||||
throw ServerException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
throw ServerException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||||
|
|
||||||
// The following two calls are optional. If they fail, we don't care.
|
// The following three calls are optional. If they fail, we don't care.
|
||||||
int reuse = 1;
|
int reuse = 1;
|
||||||
setsockopt(context.GetSocket(), SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
|
setsockopt(context.GetSocket(), SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
|
||||||
int nodelay = 1;
|
int nodelay = 1;
|
||||||
setsockopt(context.GetSocket(), SOL_SOCKET, TCP_NODELAY, (char *)&nodelay, sizeof(nodelay));
|
setsockopt(context.GetSocket(), SOL_SOCKET, TCP_NODELAY, (char *)&nodelay, sizeof(nodelay));
|
||||||
|
// Enable dual-stack socket on Windows Vista.
|
||||||
|
int ipv6only = 0;
|
||||||
|
setsockopt(context.GetSocket(), IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
|
||||||
|
|
||||||
context.GetServerSockaddr()->ss_family = context.GetAddrFamily();
|
context.GetServerSockaddr()->ss_family = context.GetAddrFamily();
|
||||||
|
|
||||||
|
|||||||
@@ -47,3 +47,19 @@ socket_has_ipv6()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
socket_has_dual_stack()
|
||||||
|
{
|
||||||
|
bool retVal = false;
|
||||||
|
SOCKET test = socket(AF_INET6, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
if (test != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
int ipv6only = 0;
|
||||||
|
if (setsockopt(test, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)) == 0)
|
||||||
|
retVal = true;
|
||||||
|
CLOSESOCKET(test);
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,10 @@
|
|||||||
#define SOCKET_ERR_NOTCONN WSAENOTCONN
|
#define SOCKET_ERR_NOTCONN WSAENOTCONN
|
||||||
#define SOCKET_ERR_NOTSOCK WSAENOTSOCK
|
#define SOCKET_ERR_NOTSOCK WSAENOTSOCK
|
||||||
|
|
||||||
|
#ifndef IPV6_V6ONLY
|
||||||
|
#define IPV6_V6ONLY 27
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __GNUC__ /* mingw provides stdint.h */
|
#ifdef __GNUC__ /* mingw provides stdint.h */
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
typedef uint16_t u_int16_t;
|
typedef uint16_t u_int16_t;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ void socket_cleanup();
|
|||||||
|
|
||||||
bool socket_has_sctp();
|
bool socket_has_sctp();
|
||||||
bool socket_has_ipv6();
|
bool socket_has_ipv6();
|
||||||
|
bool socket_has_dual_stack();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user