QAbstractSocket: Consolidate error reporting

Introduce the methods setError(), setErrorAndEmit() to consistently
set the internal error state.

Change-Id: I4ff951d100cf5e9f9a7e27135bb52188cde99853
Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Kai Koehne 2015-07-10 16:10:44 +02:00
parent b1738d6329
commit a8bc96e9e5
8 changed files with 133 additions and 188 deletions

View File

@ -630,8 +630,8 @@ bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtoc
resetSocketLayer();
socketEngine = QAbstractSocketEngine::createSocketEngine(q->socketType(), proxyInUse, q);
if (!socketEngine) {
socketError = QAbstractSocket::UnsupportedSocketOperationError;
q->setErrorString(QAbstractSocket::tr("Operation on socket is not supported"));
setError(QAbstractSocket::UnsupportedSocketOperationError,
QAbstractSocket::tr("Operation on socket is not supported"));
return false;
}
#ifndef QT_NO_BEARERMANAGEMENT
@ -644,8 +644,7 @@ bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtoc
typeStr.toLatin1().constData(), protocolStr.toLatin1().constData(),
socketEngine->errorString().toLatin1().constData());
#endif
socketError = socketEngine->error();
q->setErrorString(socketEngine->errorString());
setError(socketEngine->error(), socketEngine->errorString());
return false;
}
@ -890,12 +889,10 @@ bool QAbstractSocketPrivate::flush()
// Attempt to write it all in one chunk.
qint64 written = socketEngine->write(ptr, nextSize);
if (written < 0) {
socketError = socketEngine->error();
q->setErrorString(socketEngine->errorString());
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug() << "QAbstractSocketPrivate::flush() write error, aborting." << socketEngine->errorString();
#endif
emit q->error(socketError);
setErrorAndEmit(socketEngine->error(), socketEngine->errorString());
// an unexpected error so close the socket.
q->abort();
return false;
@ -997,8 +994,7 @@ void QAbstractSocketPrivate::startConnectingByName(const QString &host)
}
// failed to connect
socketError = socketEngine->error();
q->setErrorString(socketEngine->errorString());
setError(socketEngine->error(), socketEngine->errorString());
}
state = QAbstractSocket::UnconnectedState;
@ -1057,8 +1053,7 @@ void QAbstractSocketPrivate::_q_startConnecting(const QHostInfo &hostInfo)
qDebug("QAbstractSocketPrivate::_q_startConnecting(), host not found");
#endif
state = QAbstractSocket::UnconnectedState;
socketError = QAbstractSocket::HostNotFoundError;
q->setErrorString(QAbstractSocket::tr("Host not found"));
setError(QAbstractSocket::HostNotFoundError, QAbstractSocket::tr("Host not found"));
emit q->stateChanged(state);
emit q->error(QAbstractSocket::HostNotFoundError);
return;
@ -1107,11 +1102,10 @@ void QAbstractSocketPrivate::_q_connectToNextAddress()
|| socketEngine->error() == QAbstractSocket::UnsupportedSocketOperationError
#endif
) && socketEngine->state() == QAbstractSocket::ConnectingState) {
socketError = QAbstractSocket::ConnectionRefusedError;
q->setErrorString(QAbstractSocket::tr("Connection refused"));
setError(QAbstractSocket::ConnectionRefusedError,
QAbstractSocket::tr("Connection refused"));
} else {
socketError = socketEngine->error();
q->setErrorString(socketEngine->errorString());
setError(socketEngine->error(), socketEngine->errorString());
}
} else {
// socketError = QAbstractSocket::ConnectionRefusedError;
@ -1235,8 +1229,8 @@ void QAbstractSocketPrivate::_q_abortConnectionAttempt()
if (addresses.isEmpty()) {
state = QAbstractSocket::UnconnectedState;
socketError = QAbstractSocket::SocketTimeoutError;
q->setErrorString(QAbstractSocket::tr("Connection timed out"));
setError(QAbstractSocket::SocketTimeoutError,
QAbstractSocket::tr("Connection timed out"));
emit q->stateChanged(state);
emit q->error(socketError);
} else {
@ -1260,7 +1254,6 @@ void QAbstractSocketPrivate::_q_forceDisconnect()
*/
bool QAbstractSocketPrivate::readFromSocket()
{
Q_Q(QAbstractSocket);
// Find how many bytes we can read from the socket layer.
qint64 bytesToRead = socketEngine->bytesAvailable();
if (bytesToRead == 0) {
@ -1296,9 +1289,7 @@ bool QAbstractSocketPrivate::readFromSocket()
#endif
if (!socketEngine->isValid()) {
socketError = socketEngine->error();
q->setErrorString(socketEngine->errorString());
emit q->error(socketError);
setErrorAndEmit(socketEngine->error(), socketEngine->errorString());
#if defined(QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::readFromSocket() read failed: %s",
q->errorString().toLatin1().constData());
@ -1368,6 +1359,31 @@ QAbstractSocketEngine* QAbstractSocketPrivate::getSocketEngine(QAbstractSocket *
return socket->d_func()->socketEngine;
}
/*!
\internal
Sets the socket error state to \c errorCode and \a errorString.
*/
void QAbstractSocketPrivate::setError(QAbstractSocket::SocketError errorCode,
const QString &errStr)
{
socketError = errorCode;
errorString = errStr;
}
/*!
\internal
Sets the socket error state to \c errorCode and \a errorString,
and emits the QAbstractSocket::error() signal.
*/
void QAbstractSocketPrivate::setErrorAndEmit(QAbstractSocket::SocketError errorCode,
const QString &errorString)
{
Q_Q(QAbstractSocket);
setError(errorCode, errorString);
emit q->error(errorCode);
}
/*! \internal
@ -1532,9 +1548,7 @@ bool QAbstractSocketPrivate::bind(const QHostAddress &address, quint16 port, QAb
cachedSocketDescriptor = socketEngine->socketDescriptor();
if (!result) {
socketError = socketEngine->error();
q->setErrorString(socketEngine->errorString());
emit q->error(socketError);
setErrorAndEmit(socketEngine->error(), socketEngine->errorString());
return false;
}
@ -1611,9 +1625,7 @@ void QAbstractSocket::connectToHost(const QString &hostName, quint16 port,
if (d->state == ConnectedState || d->state == ConnectingState
|| d->state == ClosingState || d->state == HostLookupState) {
qWarning("QAbstractSocket::connectToHost() called when already looking up or connecting/connected to \"%s\"", qPrintable(hostName));
d->socketError = QAbstractSocket::OperationError;
setErrorString(QAbstractSocket::tr("Trying to connect while connection is in progress"));
emit error(d->socketError);
d->setErrorAndEmit(OperationError, tr("Trying to connect while connection is in progress"));
return;
}
@ -1642,9 +1654,8 @@ void QAbstractSocket::connectToHost(const QString &hostName, quint16 port,
d->resolveProxy(hostName, port);
if (d->proxyInUse.type() == QNetworkProxy::DefaultProxy) {
// failed to setup the proxy
d->socketError = QAbstractSocket::UnsupportedSocketOperationError;
setErrorString(QAbstractSocket::tr("Operation on socket is not supported"));
emit error(d->socketError);
d->setErrorAndEmit(UnsupportedSocketOperationError,
tr("Operation on socket is not supported"));
return;
}
#endif
@ -1864,8 +1875,7 @@ bool QAbstractSocket::setSocketDescriptor(qintptr socketDescriptor, SocketState
d->buffer.clear();
d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this);
if (!d->socketEngine) {
d->socketError = UnsupportedSocketOperationError;
setErrorString(tr("Operation on socket is not supported"));
d->setError(UnsupportedSocketOperationError, tr("Operation on socket is not supported"));
return false;
}
#ifndef QT_NO_BEARERMANAGEMENT
@ -1874,8 +1884,7 @@ bool QAbstractSocket::setSocketDescriptor(qintptr socketDescriptor, SocketState
#endif
bool result = d->socketEngine->initialize(socketDescriptor, socketState);
if (!result) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
return false;
}
@ -2085,8 +2094,7 @@ bool QAbstractSocket::waitForConnected(int msecs)
}
if ((timedOut && state() != ConnectedState) || state() == ConnectingState) {
d->socketError = SocketTimeoutError;
setErrorString(tr("Socket operation timed out"));
d->setError(SocketTimeoutError, tr("Socket operation timed out"));
d->state = UnconnectedState;
emit stateChanged(d->state);
d->resetSocketLayer();
@ -2148,13 +2156,11 @@ bool QAbstractSocket::waitForReadyRead(int msecs)
bool readyToWrite = false;
if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, !d->writeBuffer.isEmpty(),
qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::waitForReadyRead(%i) failed (%i, %s)",
msecs, d->socketError, errorString().toLatin1().constData());
msecs, d->socketEngine->error(), d->socketEngine->errorString().toLatin1().constData());
#endif
emit error(d->socketError);
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
if (d->socketError != SocketTimeoutError)
close();
return false;
@ -2220,13 +2226,11 @@ bool QAbstractSocket::waitForBytesWritten(int msecs)
bool readyToWrite = false;
if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, !d->writeBuffer.isEmpty(),
qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::waitForBytesWritten(%i) failed (%i, %s)",
msecs, d->socketError, errorString().toLatin1().constData());
msecs, d->socketEngine->error(), d->socketEngine->errorString().toLatin1().constData());
#endif
emit error(d->socketError);
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
if (d->socketError != SocketTimeoutError)
close();
return false;
@ -2302,13 +2306,11 @@ bool QAbstractSocket::waitForDisconnected(int msecs)
if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, state() == ConnectedState,
!d->writeBuffer.isEmpty(),
qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::waitForReadyRead(%i) failed (%i, %s)",
msecs, d->socketError, errorString().toLatin1().constData());
msecs, d->socketEngine->error(), d->socketEngine->errorString().toLatin1().constData());
#endif
emit error(d->socketError);
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
if (d->socketError != SocketTimeoutError)
close();
return false;
@ -2439,8 +2441,7 @@ qint64 QAbstractSocket::readData(char *data, qint64 maxSize)
// -2 from the engine means no bytes available (EAGAIN) so read more later
return 0;
} else if (readBytes < 0) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
d->resetSocketLayer();
d->state = QAbstractSocket::UnconnectedState;
} else if (!d->socketEngine->isReadNotificationEnabled()) {
@ -2470,8 +2471,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
{
Q_D(QAbstractSocket);
if (d->state == QAbstractSocket::UnconnectedState) {
d->socketError = QAbstractSocket::UnknownSocketError;
setErrorString(tr("Socket is not connected"));
d->setError(UnknownSocketError, tr("Socket is not connected"));
return -1;
}
@ -2479,8 +2479,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
// This code is for the new Unbuffered QTcpSocket use case
qint64 written = d->socketEngine->write(data, size);
if (written < 0) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
return written;
} else if (written < size) {
// Buffer what was not written yet
@ -2494,8 +2493,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
// This is for a QUdpSocket that was connect()ed
qint64 written = d->socketEngine->write(data, size);
if (written < 0) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
} else if (!d->writeBuffer.isEmpty()) {
d->socketEngine->setWriteNotificationEnabled(true);
}

View File

@ -136,6 +136,9 @@ public:
void setupSocketNotifiers();
bool readFromSocket();
void setError(QAbstractSocket::SocketError errorCode, const QString &errorString);
void setErrorAndEmit(QAbstractSocket::SocketError errorCode, const QString &errorString);
qint64 readBufferMaxSize;
QRingBuffer writeBuffer;

View File

@ -339,9 +339,7 @@ qint64 QUdpSocket::writeDatagram(const char *data, qint64 size, const QHostAddre
if (sent >= 0) {
emit bytesWritten(sent);
} else {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
emit error(d->socketError);
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
}
return sent;
}
@ -394,11 +392,8 @@ qint64 QUdpSocket::readDatagram(char *data, qint64 maxSize, QHostAddress *addres
}
d_func()->socketEngine->setReadNotificationEnabled(true);
if (readBytes < 0) {
d->socketError = d->socketEngine->error();
setErrorString(d->socketEngine->errorString());
emit error(d->socketError);
}
if (readBytes < 0)
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
return readBytes;
}
#endif // QT_NO_UDPSOCKET

View File

@ -499,8 +499,7 @@ bool QSslSocket::setSocketDescriptor(qintptr socketDescriptor, SocketState state
d->createPlainSocket(openMode);
bool retVal = d->plainSocket->setSocketDescriptor(socketDescriptor, state, openMode);
d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
setSocketError(d->plainSocket->error());
setErrorString(d->plainSocket->errorString());
d->setError(d->plainSocket->error(), d->plainSocket->errorString());
setSocketState(state);
setOpenMode(openMode);
setLocalPort(d->plainSocket->localPort());
@ -1532,8 +1531,7 @@ bool QSslSocket::waitForConnected(int msecs)
bool retVal = d->plainSocket->waitForConnected(msecs);
if (!retVal) {
setSocketState(d->plainSocket->state());
setSocketError(d->plainSocket->error());
setErrorString(d->plainSocket->errorString());
d->setError(d->plainSocket->error(), d->plainSocket->errorString());
}
return retVal;
}
@ -1688,8 +1686,7 @@ bool QSslSocket::waitForDisconnected(int msecs)
bool retVal = d->plainSocket->waitForDisconnected(qt_subtract_from_timeout(msecs, stopWatch.elapsed()));
if (!retVal) {
setSocketState(d->plainSocket->state());
setSocketError(d->plainSocket->error());
setErrorString(d->plainSocket->errorString());
d->setError(d->plainSocket->error(), d->plainSocket->errorString());
}
return retVal;
}
@ -2402,8 +2399,9 @@ void QSslSocketPrivate::_q_stateChangedSlot(QAbstractSocket::SocketState state)
*/
void QSslSocketPrivate::_q_errorSlot(QAbstractSocket::SocketError error)
{
Q_Q(QSslSocket);
Q_UNUSED(error)
#ifdef QSSLSOCKET_DEBUG
Q_Q(QSslSocket);
qCDebug(lcSsl) << "QSslSocket::_q_errorSlot(" << error << ')';
qCDebug(lcSsl) << "\tstate =" << q->state();
qCDebug(lcSsl) << "\terrorString =" << q->errorString();
@ -2416,9 +2414,7 @@ void QSslSocketPrivate::_q_errorSlot(QAbstractSocket::SocketError error)
readBufferMaxSize = tmpReadBufferMaxSize;
}
q->setSocketError(plainSocket->error());
q->setErrorString(plainSocket->errorString());
emit q->error(error);
setErrorAndEmit(plainSocket->error(), plainSocket->errorString());
}
/*!
@ -2483,7 +2479,6 @@ void QSslSocketPrivate::_q_flushReadBuffer()
*/
void QSslSocketPrivate::_q_resumeImplementation()
{
Q_Q(QSslSocket);
if (plainSocket)
plainSocket->resume();
paused = false;
@ -2491,9 +2486,7 @@ void QSslSocketPrivate::_q_resumeImplementation()
if (verifyErrorsHaveBeenIgnored()) {
continueHandshake();
} else {
q->setErrorString(sslErrors.first().errorString());
q->setSocketError(QAbstractSocket::SslHandshakeFailedError);
emit q->error(QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError, sslErrors.first().errorString());
plainSocket->disconnectFromHost();
return;
}

View File

@ -318,7 +318,7 @@ void QSslSocketBackendPrivate::startClientEncryption()
// Error description/code were set, 'error' emitted
// by initSslContext, but OpenSSL socket also sets error
// emits a signal twice, so ...
setError("Unable to init SSL Context", QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError, "Unable to init SSL Context");
return;
}
@ -331,7 +331,7 @@ void QSslSocketBackendPrivate::startServerEncryption()
// Error description/code were set, 'error' emitted
// by initSslContext, but OpenSSL socket also sets error
// emits a signal twice, so ...
setError("Unable to init SSL Context", QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError, "Unable to init SSL Context");
return;
}
@ -360,8 +360,8 @@ void QSslSocketBackendPrivate::transmit()
qCDebug(lcSsl) << plainSocket << "SSLWrite returned" << err;
#endif
if (err != noErr && err != errSSLWouldBlock) {
setError(QStringLiteral("SSLWrite failed: %1").arg(err),
QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QStringLiteral("SSLWrite failed: %1").arg(err));
break;
}
@ -395,12 +395,12 @@ void QSslSocketBackendPrivate::transmit()
#endif
if (err == errSSLClosedGraceful) {
shutdown = true; // the other side shut down, make sure we do not send shutdown ourselves
setError(QSslSocket::tr("The TLS/SSL connection has been closed"),
QAbstractSocket::RemoteHostClosedError);
setErrorAndEmit(QAbstractSocket::RemoteHostClosedError,
QSslSocket::tr("The TLS/SSL connection has been closed"));
break;
} else if (err != noErr && err != errSSLWouldBlock) {
setError(QStringLiteral("SSLRead failed: %1").arg(err),
QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QStringLiteral("SSLRead failed: %1").arg(err));
break;
}
@ -658,7 +658,7 @@ bool QSslSocketBackendPrivate::initSslContext()
context = SSLCreateContext(Q_NULLPTR, side, kSSLStreamType);
if (!context) {
setError("SSLCreateContext failed", QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError, "SSLCreateContext failed");
return false;
}
@ -666,8 +666,8 @@ bool QSslSocketBackendPrivate::initSslContext()
reinterpret_cast<SSLWriteFunc>(&_q_SSLWrite));
if (err != noErr) {
destroySslContext();
setError(QStringLiteral("SSLSetIOFuncs failed: %1").arg(err),
QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QStringLiteral("SSLSetIOFuncs failed: %1").arg(err));
return false;
}
@ -679,14 +679,14 @@ bool QSslSocketBackendPrivate::initSslContext()
QAbstractSocket::SocketError errorCode = QAbstractSocket::UnknownSocketError;
if (!setSessionCertificate(errorDescription, errorCode)) {
destroySslContext();
setError(errorDescription, errorCode);
setErrorAndEmit(errorCode, errorDescription);
return false;
}
}
if (!setSessionProtocol()) {
destroySslContext();
setError("Failed to set protocol version", QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError, "Failed to set protocol version");
return false;
}
@ -698,8 +698,8 @@ bool QSslSocketBackendPrivate::initSslContext()
const OSStatus err = SSLSetEnableCertVerify(context, false);
if (err != noErr) {
destroySslContext();
setError(QStringLiteral("SSLSetEnableCertVerify failed: %1").arg(err),
QSslSocket::SslInternalError);
setErrorAndEmit(QSslSocket::SslInternalError,
QStringLiteral("SSLSetEnableCertVerify failed: %1").arg(err));
return false;
}
}
@ -720,8 +720,8 @@ bool QSslSocketBackendPrivate::initSslContext()
if (err != noErr) {
destroySslContext();
setError(QStringLiteral("SSLSetSessionOption failed: %1").arg(err),
QSslSocket::SslInternalError);
setErrorAndEmit(QSslSocket::SslInternalError,
QStringLiteral("SSLSetSessionOption failed: %1").arg(err));
return false;
}
//
@ -737,8 +737,8 @@ bool QSslSocketBackendPrivate::initSslContext()
if (err != noErr) {
destroySslContext();
setError(QStringLiteral("failed to set SSL context option in server mode: %1").arg(err),
QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QStringLiteral("failed to set SSL context option in server mode: %1").arg(err));
return false;
}
}
@ -981,8 +981,8 @@ bool QSslSocketBackendPrivate::verifyPeerTrust()
// !trust - SSLCopyPeerTrust can return noErr but null trust.
if (err != noErr || !trust) {
if (!canIgnoreVerify) {
setError(QStringLiteral("Failed to obtain peer trust: %1").arg(err),
QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError,
QStringLiteral("Failed to obtain peer trust: %1").arg(err));
plainSocket->disconnectFromHost();
return false;
} else {
@ -1005,8 +1005,8 @@ bool QSslSocketBackendPrivate::verifyPeerTrust()
if (err != noErr) {
// We can not ignore this, it's not even about trust verification
// probably ...
setError(QStringLiteral("SecTrustEvaluate failed: %1").arg(err),
QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError,
QStringLiteral("SecTrustEvaluate failed: %1").arg(err));
plainSocket->disconnectFromHost();
return false;
}
@ -1124,8 +1124,8 @@ bool QSslSocketBackendPrivate::checkSslErrors()
pauseSocketNotifiers(q);
paused = true;
} else {
setError(sslErrors.first().errorString(),
QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError,
sslErrors.first().errorString());
plainSocket->disconnectFromHost();
}
return false;
@ -1162,7 +1162,7 @@ bool QSslSocketBackendPrivate::startHandshake()
// setSessionCertificate does not fail if we have no certificate.
// Failure means a real error (invalid certificate, no private key, etc).
if (!setSessionCertificate(errorDescription, errorCode)) {
setError(errorDescription, errorCode);
setErrorAndEmit(errorCode, errorDescription);
return false;
} else {
// We try to resume a handshake, even if have no
@ -1177,8 +1177,8 @@ bool QSslSocketBackendPrivate::startHandshake()
return startHandshake();
}
setError(QStringLiteral("SSLHandshake failed: %1").arg(err),
QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError,
QStringLiteral("SSLHandshake failed: %1").arg(err));
plainSocket->disconnectFromHost();
return false;
}
@ -1192,8 +1192,8 @@ bool QSslSocketBackendPrivate::startHandshake()
// check protocol version ourselves, as Secure Transport does not enforce
// the requested min / max versions.
if (!verifySessionProtocol()) {
setError("Protocol version mismatch",
QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError,
"Protocol version mismatch");
plainSocket->disconnectFromHost();
return false;
}
@ -1206,16 +1206,6 @@ bool QSslSocketBackendPrivate::startHandshake()
}
}
void QSslSocketBackendPrivate::setError(const QString &errorString,
QAbstractSocket::SocketError errorCode)
{
Q_Q(QSslSocket);
q->setErrorString(errorString);
q->setSocketError(errorCode);
emit q->error(errorCode);
}
/*
PKCS12 helpers.
*/

View File

@ -101,13 +101,6 @@ private:
bool checkSslErrors();
bool startHandshake();
// Aux. function, sets:
//1) socket error code,
//2) error string (description)
//3) emits a signal.
void setError(const QString &errorString,
QAbstractSocket::SocketError errorCode);
mutable QCFType<SSLContextRef> context;
Q_DISABLE_COPY(QSslSocketBackendPrivate);

View File

@ -364,9 +364,7 @@ bool QSslSocketBackendPrivate::initSslContext()
}
if (sslContextPointer->error() != QSslError::NoError) {
q->setErrorString(sslContextPointer->errorString());
q->setSocketError(QAbstractSocket::SslInvalidUserDataError);
emit q->error(QAbstractSocket::SslInvalidUserDataError);
setErrorAndEmit(QAbstractSocket::SslInvalidUserDataError, sslContextPointer->errorString());
sslContextPointer.clear(); // deletes the QSslContext
return false;
}
@ -374,9 +372,8 @@ bool QSslSocketBackendPrivate::initSslContext()
// Create and initialize SSL session
if (!(ssl = sslContextPointer->createSsl())) {
// ### Bad error code
q->setErrorString(QSslSocket::tr("Error creating SSL session, %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Error creating SSL session, %1").arg(getErrorsFromOpenSsl()));
return false;
}
@ -405,9 +402,8 @@ bool QSslSocketBackendPrivate::initSslContext()
readBio = q_BIO_new(q_BIO_s_mem());
writeBio = q_BIO_new(q_BIO_s_mem());
if (!readBio || !writeBio) {
q->setErrorString(QSslSocket::tr("Error creating SSL session: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Error creating SSL session: %1").arg(getErrorsFromOpenSsl()));
return false;
}
@ -808,11 +804,9 @@ QList<QSslCertificate> QSslSocketPrivate::systemCaCertificates()
void QSslSocketBackendPrivate::startClientEncryption()
{
Q_Q(QSslSocket);
if (!initSslContext()) {
q->setErrorString(QSslSocket::tr("Unable to init SSL Context: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Unable to init SSL Context: %1").arg(getErrorsFromOpenSsl()));
return;
}
@ -824,11 +818,9 @@ void QSslSocketBackendPrivate::startClientEncryption()
void QSslSocketBackendPrivate::startServerEncryption()
{
Q_Q(QSslSocket);
if (!initSslContext()) {
q->setErrorString(QSslSocket::tr("Unable to init SSL Context: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Unable to init SSL Context: %1").arg(getErrorsFromOpenSsl()));
return;
}
@ -874,9 +866,9 @@ void QSslSocketBackendPrivate::transmit()
break;
} else {
// ### Better error handling.
q->setErrorString(QSslSocket::tr("Unable to write data: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Unable to write data: %1").arg(
getErrorsFromOpenSsl()));
return;
}
}
@ -918,9 +910,7 @@ void QSslSocketBackendPrivate::transmit()
#endif
if (actualWritten < 0) {
//plain socket write fails if it was in the pending close state.
q->setErrorString(plainSocket->errorString());
q->setSocketError(plainSocket->error());
emit q->error(plainSocket->error());
setErrorAndEmit(plainSocket->error(), plainSocket->errorString());
return;
}
transmitting = true;
@ -946,9 +936,9 @@ void QSslSocketBackendPrivate::transmit()
plainSocket->read(data.data(), writtenToBio);
} else {
// ### Better error handling.
q->setErrorString(QSslSocket::tr("Unable to decrypt data: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Unable to decrypt data: %1").arg(
getErrorsFromOpenSsl()));
return;
}
@ -1022,17 +1012,15 @@ void QSslSocketBackendPrivate::transmit()
qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: remote disconnect";
#endif
shutdown = true; // the other side shut down, make sure we do not send shutdown ourselves
q->setErrorString(QSslSocket::tr("The TLS/SSL connection has been closed"));
q->setSocketError(QAbstractSocket::RemoteHostClosedError);
emit q->error(QAbstractSocket::RemoteHostClosedError);
setErrorAndEmit(QAbstractSocket::RemoteHostClosedError,
QSslSocket::tr("The TLS/SSL connection has been closed"));
return;
case SSL_ERROR_SYSCALL: // some IO error
case SSL_ERROR_SSL: // error in the SSL library
// we do not know exactly what the error is, nor whether we can recover from it,
// so just return to prevent an endless loop in the outer "while" statement
q->setErrorString(QSslSocket::tr("Error while reading: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Error while reading: %1").arg(getErrorsFromOpenSsl()));
return;
default:
// SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT: can only happen with a
@ -1040,9 +1028,8 @@ void QSslSocketBackendPrivate::transmit()
// SSL_ERROR_WANT_X509_LOOKUP: can only happen with a
// SSL_CTX_set_client_cert_cb(), which we do not call.
// So this default case should never be triggered.
q->setErrorString(QSslSocket::tr("Error while reading: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Error while reading: %1").arg(getErrorsFromOpenSsl()));
break;
}
} while (ssl && readBytes > 0);
@ -1134,12 +1121,12 @@ bool QSslSocketBackendPrivate::startHandshake()
// The handshake is not yet complete.
break;
default:
q->setErrorString(QSslSocket::tr("Error during SSL handshake: %1").arg(getErrorsFromOpenSsl()));
q->setSocketError(QAbstractSocket::SslHandshakeFailedError);
QString errorString
= QSslSocket::tr("Error during SSL handshake: %1").arg(getErrorsFromOpenSsl());
#ifdef QSSLSOCKET_DEBUG
qCDebug(lcSsl) << "QSslSocketBackendPrivate::startHandshake: error!" << q->errorString();
qCDebug(lcSsl) << "QSslSocketBackendPrivate::startHandshake: error!" << errorString;
#endif
emit q->error(QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError, errorString);
q->abort();
}
return false;
@ -1290,9 +1277,7 @@ bool QSslSocketBackendPrivate::checkSslErrors()
pauseSocketNotifiers(q);
paused = true;
} else {
q->setErrorString(sslErrors.first().errorString());
q->setSocketError(QAbstractSocket::SslHandshakeFailedError);
emit q->error(QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError, sslErrors.first().errorString());
plainSocket->disconnectFromHost();
}
return false;

View File

@ -251,9 +251,8 @@ void QSslSocketBackendPrivate::startClientEncryption()
case QSsl::TlsV1_2OrLater:
// TlsV1_0OrLater, TlsV1_1OrLater and TlsV1_2OrLater are disabled on WinRT
// because there is no good way to map them to the native API.
q->setErrorString(QStringLiteral("unsupported protocol"));
q->setSocketError(QAbstractSocket::SslInvalidUserDataError);
emit q->error(QAbstractSocket::SslInvalidUserDataError);
setErrorAndEmit(QAbstractSocket::SslInvalidUserDataError,
QStringLiteral("unsupported protocol"));
return;
default:
protectionLevel = SocketProtectionLevel_Tls12; // default to highest
@ -347,13 +346,10 @@ QSsl::SslProtocol QSslSocketBackendPrivate::sessionProtocol() const
void QSslSocketBackendPrivate::continueHandshake()
{
Q_Q(QSslSocket);
IStreamSocket *socket = reinterpret_cast<IStreamSocket *>(plainSocket->socketDescriptor());
if (qintptr(socket) == -1) {
q->setErrorString(QStringLiteral("At attempt was made to continue the handshake on an invalid socket."));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QStringLiteral("At attempt was made to continue the handshake on an invalid socket."));
return;
}
@ -372,9 +368,7 @@ void QSslSocketBackendPrivate::continueHandshake()
Q_ASSERT_SUCCEEDED(hr);
}
if (FAILED(hr)) {
q->setErrorString(qt_error_string(hr));
q->setSocketError(QAbstractSocket::SslInvalidUserDataError);
emit q->error(QAbstractSocket::SslInvalidUserDataError);
setErrorAndEmit(QAbstractSocket::SslInvalidUserDataError, qt_error_string(hr));
return;
}
@ -440,10 +434,8 @@ void QSslSocketBackendPrivate::continueHandshake()
ComPtr<IAsyncAction> op;
hr = socket->UpgradeToSslAsync(protectionLevel, hostName.Get(), &op);
if (FAILED(hr)) {
q->setErrorString(QSslSocket::tr("Error creating SSL session: %1")
.arg(qt_error_string(hr)));
q->setSocketError(QAbstractSocket::SslInternalError);
emit q->error(QAbstractSocket::SslInternalError);
setErrorAndEmit(QAbstractSocket::SslInternalError,
QSslSocket::tr("Error creating SSL session: %1").arg(qt_error_string(hr)));
return;
}
@ -467,9 +459,7 @@ HRESULT QSslSocketBackendPrivate::onSslUpgrade(IAsyncAction *action, AsyncStatus
QSet<QSslError> errors;
switch (hr) {
case SEC_E_INVALID_TOKEN: // Occurs when the server doesn't support the requested protocol
q->setErrorString(qt_error_string(hr));
q->setSocketError(QAbstractSocket::SslHandshakeFailedError);
emit q->error(QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError, qt_error_string(hr));
q->disconnectFromHost();
return S_OK;
default:
@ -628,9 +618,7 @@ HRESULT QSslSocketBackendPrivate::onSslUpgrade(IAsyncAction *action, AsyncStatus
if (!sslErrors.isEmpty()) {
emit q->sslErrors(sslErrors);
q->setErrorString(sslErrors.first().errorString());
q->setSocketError(QAbstractSocket::SslHandshakeFailedError);
emit q->error(QAbstractSocket::SslHandshakeFailedError);
setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError, sslErrors.first().errorString());
// Disconnect if there are any non-ignorable errors
foreach (const QSslError &error, sslErrors) {