📄 qsslsocket.cpp
字号:
The most common way to implement an SSL server is to create a subclass of QTcpServer and reimplement QTcpServer::incomingConnection(). The returned socket descriptor is then passed to QSslSocket::setSocketDescriptor(). \sa connectToHostEncrypted(), startClientEncryption()*/void QSslSocket::startServerEncryption(){ Q_D(QSslSocket); if (d->mode != UnencryptedMode) { qWarning("QSslSocket::startClientEncryption: cannot start handshake on non-plain connection"); return; }#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::startServerEncryption()";#endif d->mode = SslServerMode; emit modeChanged(d->mode); d->startServerEncryption();}/*! This slot tells QSslSocket to ignore errors during QSslSocket's handshake phase and continue connecting. If you want to continue with the connection even if errors occur during the handshake phase, then you must call this slot, either from a slot connected to sslErrors(), or before the handshake phase. If you don't call this slot, either in response to errors or before the handshake, the connection will be dropped after the sslErrors() signal has been emitted. If there are no errors during the SSL handshake phase (i.e., the identity of the peer is established with no problems), QSslSocket will not emit the sslErrors() signal, and it is unnecessary to call this function. Ignoring errors that occur during an SSL handshake should be done with caution. A fundamental characteristic of secure connections is that they should be established with an error free handshake. \sa sslErrors()*/void QSslSocket::ignoreSslErrors(){ Q_D(QSslSocket); d->ignoreSslErrors = true;}/*! \internal*/void QSslSocket::connectToHostImplementation(const QString &hostName, quint16 port, OpenMode openMode){ Q_D(QSslSocket); d->init();#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::connectToHostImplementation(" << hostName << "," << port << "," << openMode << ")";#endif if (!d->plainSocket) {#ifdef QSSLSOCKET_DEBUG qDebug() << "\tcreating internal plain socket";#endif d->createPlainSocket(openMode);#ifndef QT_NO_NETWORKPROXY d->plainSocket->setProxy(proxy());#endif } setOpenMode(openMode); d->plainSocket->connectToHost(hostName, port, openMode); d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();}/*! \internal*/void QSslSocket::disconnectFromHostImplementation(){ Q_D(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::disconnectFromHostImplementation()";#endif if (!d->plainSocket) return; if (d->mode == UnencryptedMode) { d->plainSocket->disconnectFromHost(); } else { d->disconnectFromHost(); }}/*! \reimp*/qint64 QSslSocket::readData(char *data, qint64 maxlen){ Q_D(QSslSocket); qint64 readBytes = 0; if (d->mode == UnencryptedMode && !d->autoStartHandshake) { readBytes = d->plainSocket->read(data, maxlen); } else { do { const char *readPtr = d->readBuffer.readPointer(); int bytesToRead = qMin<int>(maxlen - readBytes, d->readBuffer.nextDataBlockSize()); ::memcpy(data + readBytes, readPtr, bytesToRead); readBytes += bytesToRead; d->readBuffer.free(bytesToRead); } while (!d->readBuffer.isEmpty() && readBytes < maxlen); }#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::readData(" << (void *)data << "," << maxlen << ") ==" << readBytes;#endif return readBytes;}/*! \reimp*/qint64 QSslSocket::writeData(const char *data, qint64 len){ Q_D(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::writeData(" << (void *)data << "," << len << ")";#endif if (d->mode == UnencryptedMode && !d->autoStartHandshake) return d->plainSocket->write(data, len); char *writePtr = d->writeBuffer.reserve(len); ::memcpy(writePtr, data, len); if (d->connectionEncrypted) d->transmit(); return len;}/*! \internal*/QSslSocketPrivate::QSslSocketPrivate() : protocol(QSsl::SslV3), plainSocket(0){}/*! \internal*/QSslSocketPrivate::~QSslSocketPrivate(){}/*! \internal*/void QSslSocketPrivate::init(){ mode = QSslSocket::UnencryptedMode; autoStartHandshake = false; connectionEncrypted = false; ignoreSslErrors = false; useLocalCaCertificatesOnly = false; readBuffer.clear(); writeBuffer.clear(); peerCertificate.clear();}/*! \internal*/QList<QSslCipher> QSslSocketPrivate::defaultCiphers(){ QMutexLocker locker(&globalData()->mutex); return globalData()->ciphers;}/*! \internal*/QList<QSslCipher> QSslSocketPrivate::supportedCiphers(){ QSslSocketPrivate::ensureInitialized(); QMutexLocker locker(&globalData()->mutex); return globalData()->supportedCiphers;}/*! \internal*/void QSslSocketPrivate::setDefaultCiphers(const QList<QSslCipher> &ciphers){ QMutexLocker locker(&globalData()->mutex); globalData()->ciphers = ciphers;}/*! \internal*/void QSslSocketPrivate::setDefaultSupportedCiphers(const QList<QSslCipher> &ciphers){ QMutexLocker locker(&globalData()->mutex); globalData()->supportedCiphers = ciphers;}/*! \internal*/QList<QSslCertificate> QSslSocketPrivate::defaultCaCertificates(){ QSslSocketPrivate::ensureInitialized(); QMutexLocker locker(&globalData()->mutex); return globalData()->caCertificates;}/*! \internal*/void QSslSocketPrivate::setDefaultCaCertificates(const QList<QSslCertificate> &certs){ QSslSocketPrivate::ensureInitialized(); QMutexLocker locker(&globalData()->mutex); globalData()->caCertificates = certs;}/*! \internal*/bool QSslSocketPrivate::addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat format, QRegExp::PatternSyntax syntax){ QSslSocketPrivate::ensureInitialized(); QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax); if (certs.isEmpty()) return false; QMutexLocker locker(&globalData()->mutex); globalData()->caCertificates += certs; return true;}/*! \internal*/void QSslSocketPrivate::addDefaultCaCertificate(const QSslCertificate &cert){ QSslSocketPrivate::ensureInitialized(); QMutexLocker locker(&globalData()->mutex); globalData()->caCertificates += cert;}/*! \internal*/void QSslSocketPrivate::addDefaultCaCertificates(const QList<QSslCertificate> &certs){ QSslSocketPrivate::ensureInitialized(); QMutexLocker locker(&globalData()->mutex); globalData()->caCertificates += certs;}/*! \internal*/void QSslSocketPrivate::createPlainSocket(QIODevice::OpenMode openMode){ Q_Q(QSslSocket); q->setOpenMode(openMode); // <- from QIODevice q->setSocketState(QAbstractSocket::UnconnectedState); q->setSocketError(QAbstractSocket::UnknownSocketError); q->setLocalPort(0); q->setLocalAddress(QHostAddress()); q->setPeerPort(0); q->setPeerAddress(QHostAddress()); q->setPeerName(QString()); plainSocket = new QTcpSocket(q); q->connect(plainSocket, SIGNAL(connected()), q, SLOT(_q_connectedSlot())); q->connect(plainSocket, SIGNAL(hostFound()), q, SLOT(_q_hostFoundSlot())); q->connect(plainSocket, SIGNAL(disconnected()), q, SLOT(_q_disconnectedSlot())); q->connect(plainSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), q, SLOT(_q_stateChangedSlot(QAbstractSocket::SocketState))); q->connect(plainSocket, SIGNAL(error(QAbstractSocket::SocketError)), q, SLOT(_q_errorSlot(QAbstractSocket::SocketError))); q->connect(plainSocket, SIGNAL(readyRead()), q, SLOT(_q_readyReadSlot())); q->connect(plainSocket, SIGNAL(bytesWritten(qint64)), q, SLOT(_q_bytesWrittenSlot(qint64))); q->connect(plainSocket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), q, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); readBuffer.clear(); writeBuffer.clear(); connectionEncrypted = false; peerCertificate.clear(); peerCertificateChain.clear(); mode = QSslSocket::UnencryptedMode;}/*! \internal*/void QSslSocketPrivate::_q_connectedSlot(){ Q_Q(QSslSocket); q->setLocalPort(plainSocket->localPort()); q->setLocalAddress(plainSocket->localAddress()); q->setPeerPort(plainSocket->peerPort()); q->setPeerAddress(plainSocket->peerAddress()); q->setPeerName(plainSocket->peerName()); cachedSocketDescriptor = plainSocket->socketDescriptor();#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::_q_connectedSlot()"; qDebug() << "\tstate =" << q->state(); qDebug() << "\tpeer =" << q->peerName() << q->peerAddress() << q->peerPort(); qDebug() << "\tlocal =" << QHostInfo::fromName(q->localAddress().toString()).hostName() << q->localAddress() << q->localPort();#endif emit q->connected(); if (autoStartHandshake) q->startClientEncryption();}/*! \internal*/void QSslSocketPrivate::_q_hostFoundSlot(){ Q_Q(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::_q_hostFoundSlot()"; qDebug() << "\tstate =" << q->state();#endif emit q->hostFound();}/*! \internal*/void QSslSocketPrivate::_q_disconnectedSlot(){ Q_Q(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::_q_disconnectedSlot()"; qDebug() << "\tstate =" << q->state();#endif disconnected(); emit q->disconnected();}/*! \internal*/void QSslSocketPrivate::_q_stateChangedSlot(QAbstractSocket::SocketState state){ Q_Q(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::_q_stateChangedSlot(" << state << ")";#endif q->setSocketState(state); emit q->stateChanged(state);}/*! \internal*/void QSslSocketPrivate::_q_errorSlot(QAbstractSocket::SocketError error){ Q_Q(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::_q_errorSlot(" << error << ")"; qDebug() << "\tstate =" << q->state(); qDebug() << "\terrorString =" << q->errorString();#endif q->setSocketError(plainSocket->error()); q->setErrorString(plainSocket->errorString()); emit q->error(error);}/*! \internal*/void QSslSocketPrivate::_q_readyReadSlot(){ Q_Q(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::_q_readyReadSlot() -" << plainSocket->bytesAvailable() << "bytes available";#endif if (mode == QSslSocket::UnencryptedMode) { emit q->readyRead(); return; } transmit();}/*! \internal*/void QSslSocketPrivate::_q_bytesWrittenSlot(qint64 written){ Q_Q(QSslSocket);#ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::_q_bytesWrittenSlot(" << written << ")";#endif emit q->bytesWritten(written);}// For private slots#define d d_ptr#include "moc_qsslsocket.cpp"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -