From a84b42e6194be0f40bd8f961effef9947c07c832 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Fri, 30 Mar 2012 21:21:40 +0200 Subject: Fix QTcpServer::listen() returning true when the port is in use on OS X. Way back in the mists of time, someone added SO_REUSEPORT to socket binding, which was great, because otherwise it meant that multiple UDP sockets couldn't share the same port on OS X (as platforms with SO_REUSEPORT apparently don't support rebinding with SO_REUSEADDR). However: SO_REUSEPORT also means that *any* bind on a port will succeed, which is most definitely not wanted in the case of TCP sockets, so check the socket type before performing the actual bind. Also test that multiple listens don't take effect. Change-Id: I2f8d450bcfb8a7f3abd8918a4e789a850281dd13 Done-with: Thiago Macieira Done-with: Shane Kearns Task-number: QTBUG-6305 Reviewed-by: Thiago Macieira Reviewed-by: Shane Kearns --- src/network/socket/qnativesocketengine_unix.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp index 295d3961a8..7cd8e1bff3 100644 --- a/src/network/socket/qnativesocketengine_unix.cpp +++ b/src/network/socket/qnativesocketengine_unix.cpp @@ -321,7 +321,13 @@ bool QNativeSocketEnginePrivate::setOption(QNativeSocketEngine::SocketOption opt } case QNativeSocketEngine::AddressReusable: #if defined(SO_REUSEPORT) - n = SO_REUSEPORT; + // on OS X, SO_REUSEADDR isn't sufficient to allow multiple binds to the + // same port (which is useful for multicast UDP). SO_REUSEPORT is, but + // we most definitely do not want to use this for TCP. See QTBUG-6305. + if (socketType == QAbstractSocket::UdpSocket) + n = SO_REUSEPORT; + else + n = SO_REUSEADDR; #else n = SO_REUSEADDR; #endif -- cgit v1.2.3