Refactor Server::supportsDualStack().

The current implementation tries too hard to avoid duplication
of very few lines of code.

Instead, use two separate code paths, one for Windows, one for
Unix-like sytems.

This makes it more readable, and allows us to handle system-specific
quirks much easier. For example, SOCKET is unsigned on Windows, and
comparing it to -1 technically works (at least for two's complement
machines), but causes a compiler warning for a signed vs. unsigned
comparison.
This commit is contained in:
Mikkel Krautz 2017-02-02 00:34:11 +01:00
parent 82d385f6bd
commit 4d6a813e80

View File

@ -50,6 +50,13 @@ bool SslServer::hasDualStackSupport() {
bool result = false;
#ifdef Q_OS_UNIX
int s = ::socket(AF_INET6, SOCK_STREAM, 0);
if (s != -1) {
const int ipv6only = 0;
if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char*>(&ipv6only), sizeof(ipv6only)) == 0) {
result = true;
}
::close(s);
}
#else
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
@ -59,17 +66,11 @@ bool SslServer::hasDualStackSupport() {
}
SOCKET s = ::WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
#endif
if (s != -1) { // Equals INVALID_SOCKET
if (s != INVALID_SOCKET) {
const int ipv6only = 0;
if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char*>(&ipv6only), sizeof(ipv6only)) == 0) {
result = true;
}
#ifdef Q_OS_UNIX
::close(s);
}
#else
closesocket(s);
}
WSACleanup();