Rework socket IPV6_V6ONLY handling

This commit is contained in:
sfan5 2025-01-31 17:10:00 +01:00
parent eb797c502a
commit a11d526110

View file

@ -87,25 +87,16 @@ bool UDPSocket::init(bool ipv6, bool noExceptions)
m_handle = socket(m_addr_family, SOCK_DGRAM, IPPROTO_UDP);
if (m_handle < 0) {
if (noExceptions) {
auto msg = std::string("Failed to create socket: ") +
SOCKET_ERR_STR(LAST_SOCKET_ERR());
verbosestream << msg << std::endl;
if (noExceptions)
return false;
}
throw SocketException(std::string("Failed to create socket: error ") +
SOCKET_ERR_STR(LAST_SOCKET_ERR()));
throw SocketException(msg);
}
setTimeoutMs(0);
if (m_addr_family == AF_INET6) {
// Allow our socket to accept both IPv4 and IPv6 connections
// required on Windows:
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb513665(v=vs.85).aspx
int value = 0;
setsockopt(m_handle, IPPROTO_IPV6, IPV6_V6ONLY,
reinterpret_cast<char *>(&value), sizeof(value));
}
return true;
}
@ -129,6 +120,19 @@ void UDPSocket::Bind(Address addr)
throw SocketException(errmsg);
}
if (m_addr_family == AF_INET6) {
// Allow our socket to accept both IPv4 and IPv6 connections
// required on Windows:
// <https://msdn.microsoft.com/en-us/library/windows/desktop/bb513665(v=vs.85).aspx>
int value = 0;
if (setsockopt(m_handle, IPPROTO_IPV6, IPV6_V6ONLY,
reinterpret_cast<char *>(&value), sizeof(value)) != 0) {
auto errmsg = SOCKET_ERR_STR(LAST_SOCKET_ERR());
errorstream << "Failed to disable V6ONLY: " << errmsg << std::endl;
throw SocketException(errmsg);
}
}
int ret = 0;
if (m_addr_family == AF_INET6) {