diff options
author | Timur Pocheptsov <Timur.Pocheptsov@digia.com> | 2015-06-10 17:37:28 +0200 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@theqtcompany.com> | 2015-11-16 04:19:38 +0000 |
commit | 52e335e191130c5beb457e8d07851fc98ceb909a (patch) | |
tree | 74fc3bdc0bc0436a6881cc384903aa2742c08358 /src/network | |
parent | bc6af06b7db10bf797eb523e7cf78ca4c153b687 (diff) |
Fix for a native socket engine on Windows
nativeConnect calls WSAConnect, tests its return value and then in case of SOCKET_ERROR
checks WSAGetLastError and calls getsockopt to extract SO_ERROR. While MSDN
says that getsockopt should clear the error, it's not true and this error can later affect in some
weird way the next WSAConnect attempt/or nativeConnect's logic.
Found in tst_qtcpsocket::bindThenResolveHost failing in this scenario: QHostInfo
has a list of invalid/valid addresses,
and as a result test doing strange things in waitForConnected and failing (and sometimes
passing).
Task-number: QTBUG-49255
Change-Id: I71f771647f305d8ca327d25ac7382a2edab96d17
Reviewed-by: Richard J. Moore <rich@kde.org>
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/socket/qnativesocketengine_win.cpp | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/network/socket/qnativesocketengine_win.cpp b/src/network/socket/qnativesocketengine_win.cpp index a7fb5f5502..1379ed93ba 100644 --- a/src/network/socket/qnativesocketengine_win.cpp +++ b/src/network/socket/qnativesocketengine_win.cpp @@ -651,6 +651,13 @@ bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &address, quin int tries = 0; do { if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) { + if (value != NOERROR) { + // MSDN says getsockopt with SO_ERROR clears the error, but it's not actually cleared + // and this can affect all subsequent WSAConnect attempts, so clear it now. + const int val = NO_ERROR; + ::setsockopt(socketDescriptor, SOL_SOCKET, SO_ERROR, reinterpret_cast<const char*>(&val), sizeof val); + } + if (value == WSAECONNREFUSED) { setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString); socketState = QAbstractSocket::UnconnectedState; |