void QSslServer::incomingConnection(qintptr socket) { QSslSocket *pSslSocket = new QSslSocket(); if (Q_LIKELY(pSslSocket)) { pSslSocket->setSslConfiguration(m_sslConfiguration); if (Q_LIKELY(pSslSocket->setSocketDescriptor(socket))) { typedef void (QSslSocket::* sslErrorsSignal)(const QList<QSslError> &); #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 2) connect(pSslSocket, &QSslSocket::peerVerifyError, this, &QSslServer::peerVerifyError); connect(pSslSocket, &QSslSocket::encrypted, this, &QSslServer::newEncryptedConnection); #else connect(pSslSocket,SIGNAL(peerVerifyError(QSslError)), this, SIGNAL(peerVerifyError(QSslError))); connect(pSslSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SIGNAL(sslErrors(QList<QSslError>))); connect(pSslSocket, SIGNAL(encrypted()), this, SIGNAL(newEncryptedConnection())); #endif addPendingConnection(pSslSocket); pSslSocket->startServerEncryption(); } else { delete pSslSocket; } } }
Transfer::Transfer(QSslConfiguration *configuration, TransferModel::Direction direction) : mDirection(direction) { // Create a socket of the appropriate type if (configuration) { QSslSocket *socket = new QSslSocket(this); socket->setSslConfiguration(*configuration); connect(socket, &QSslSocket::encrypted, this, &Transfer::initTransfer); connect(socket, &QSslSocket::encryptedBytesWritten, this, &Transfer::onBytesWritten); connect(socket, static_cast<void(QSslSocket::*)(const QList<QSslError> &)>(&QSslSocket::sslErrors), this, &Transfer::onSslErrors); mSocket = socket; } else { mSocket = new QTcpSocket(this); connect(mSocket, &QTcpSocket::bytesWritten, this, &Transfer::onBytesWritten); } connect(mSocket, &QTcpSocket::connected, this, &Transfer::onConnected); connect(mSocket, &QTcpSocket::readyRead, this, &Transfer::onReadyRead); connect(mSocket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error), this, &Transfer::onError); // Set all of the transfer members to proper initial values reset(); }
/*! Called when a new connection is established. Converts \a socket to a QSslSocket. \internal */ void QSslServer::incomingConnection(qintptr socket) { QSslSocket *pSslSocket = new QSslSocket(); if (Q_LIKELY(pSslSocket)) { pSslSocket->setSslConfiguration(m_sslConfiguration); if (Q_LIKELY(pSslSocket->setSocketDescriptor(socket))) { connect(pSslSocket, &QSslSocket::peerVerifyError, this, &QSslServer::peerVerifyError); typedef void (QSslSocket::* sslErrorsSignal)(const QList<QSslError> &); connect(pSslSocket, static_cast<sslErrorsSignal>(&QSslSocket::sslErrors), this, &QSslServer::sslErrors); connect(pSslSocket, &QSslSocket::encrypted, this, &QSslServer::newEncryptedConnection); addPendingConnection(pSslSocket); pSslSocket->startServerEncryption(); } else { delete pSslSocket; } } }
/*! \internal */ void QWebSocketPrivate::open(const QUrl &url, bool mask) { //just delete the old socket for the moment; //later, we can add more 'intelligent' handling by looking at the URL //m_pSocket.reset(); Q_Q(QWebSocket); if (!url.isValid() || url.toString().contains(QStringLiteral("\r\n"))) { setErrorString(QWebSocket::tr("Invalid URL.")); Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError); return; } QTcpSocket *pTcpSocket = m_pSocket.take(); if (pTcpSocket) { releaseConnections(pTcpSocket); pTcpSocket->deleteLater(); } //if (m_url != url) if (Q_LIKELY(!m_pSocket)) { m_dataProcessor.clear(); m_isClosingHandshakeReceived = false; m_isClosingHandshakeSent = false; setRequestUrl(url); QString resourceName = url.path(); if (resourceName.contains(QStringLiteral("\r\n"))) { setRequestUrl(QUrl()); //clear requestUrl setErrorString(QWebSocket::tr("Invalid resource name.")); Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError); return; } if (!url.query().isEmpty()) { if (!resourceName.endsWith(QChar::fromLatin1('?'))) { resourceName.append(QChar::fromLatin1('?')); } resourceName.append(url.query()); } if (resourceName.isEmpty()) resourceName = QStringLiteral("/"); setResourceName(resourceName); enableMasking(mask); #ifndef QT_NO_SSL if (url.scheme() == QStringLiteral("wss")) { if (!QSslSocket::supportsSsl()) { const QString message = QWebSocket::tr("SSL Sockets are not supported on this platform."); setErrorString(message); Q_EMIT q->error(QAbstractSocket::UnsupportedSocketOperationError); } else { QSslSocket *sslSocket = new QSslSocket; m_pSocket.reset(sslSocket); if (Q_LIKELY(m_pSocket)) { m_pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1); m_pSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1); m_pSocket->setReadBufferSize(m_readBufferSize); m_pSocket->setPauseMode(m_pauseMode); makeConnections(m_pSocket.data()); QObject::connect(sslSocket, &QSslSocket::encryptedBytesWritten, q, &QWebSocket::bytesWritten); typedef void (QSslSocket:: *sslErrorSignalType)(const QList<QSslError> &); QObject::connect(sslSocket, static_cast<sslErrorSignalType>(&QSslSocket::sslErrors), q, &QWebSocket::sslErrors); setSocketState(QAbstractSocket::ConnectingState); sslSocket->setSslConfiguration(m_configuration.m_sslConfiguration); if (Q_UNLIKELY(m_configuration.m_ignoreSslErrors)) sslSocket->ignoreSslErrors(); else sslSocket->ignoreSslErrors(m_configuration.m_ignoredSslErrors); #ifndef QT_NO_NETWORKPROXY sslSocket->setProxy(m_configuration.m_proxy); #endif sslSocket->connectToHostEncrypted(url.host(), url.port(443)); } else { const QString message = QWebSocket::tr("Out of memory."); setErrorString(message); Q_EMIT q->error(QAbstractSocket::SocketResourceError); } } } else #endif if (url.scheme() == QStringLiteral("ws")) { m_pSocket.reset(new QTcpSocket); if (Q_LIKELY(m_pSocket)) { m_pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1); m_pSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1); m_pSocket->setReadBufferSize(m_readBufferSize); m_pSocket->setPauseMode(m_pauseMode); makeConnections(m_pSocket.data()); QObject::connect(m_pSocket.data(), &QAbstractSocket::bytesWritten, q, &QWebSocket::bytesWritten); setSocketState(QAbstractSocket::ConnectingState); #ifndef QT_NO_NETWORKPROXY m_pSocket->setProxy(m_configuration.m_proxy); #endif m_pSocket->connectToHost(url.host(), url.port(80)); } else { const QString message = QWebSocket::tr("Out of memory."); setErrorString(message); Q_EMIT q->error(QAbstractSocket::SocketResourceError); } } else { const QString message = QWebSocket::tr("Unsupported WebSocket scheme: %1").arg(url.scheme()); setErrorString(message); Q_EMIT q->error(QAbstractSocket::UnsupportedSocketOperationError); } } }
void QHttpNetworkConnectionChannel::init() { #ifndef QT_NO_SSL if (connection->d_func()->encrypt) socket = new QSslSocket; else socket = new QTcpSocket; #else socket = new QTcpSocket; #endif #ifndef QT_NO_BEARERMANAGEMENT //push session down to socket if (networkSession) socket->setProperty("_q_networksession", QVariant::fromValue(networkSession)); #endif #ifndef QT_NO_NETWORKPROXY // Set by QNAM anyway, but let's be safe here socket->setProxy(QNetworkProxy::NoProxy); #endif // After some back and forth in all the last years, this is now a DirectConnection because otherwise // the state inside the *Socket classes gets messed up, also in conjunction with the socket notifiers // which behave slightly differently on Windows vs Linux QObject::connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(_q_bytesWritten(qint64)), Qt::DirectConnection); QObject::connect(socket, SIGNAL(connected()), this, SLOT(_q_connected()), Qt::DirectConnection); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(_q_readyRead()), Qt::DirectConnection); // The disconnected() and error() signals may already come // while calling connectToHost(). // In case of a cached hostname or an IP this // will then emit a signal to the user of QNetworkReply // but cannot be caught because the user did not have a chance yet // to connect to QNetworkReply's signals. qRegisterMetaType<QAbstractSocket::SocketError>(); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(_q_disconnected()), Qt::DirectConnection); QObject::connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(_q_error(QAbstractSocket::SocketError)), Qt::DirectConnection); #ifndef QT_NO_NETWORKPROXY QObject::connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), this, SLOT(_q_proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), Qt::DirectConnection); #endif #ifndef QT_NO_SSL QSslSocket *sslSocket = qobject_cast<QSslSocket*>(socket); if (sslSocket) { // won't be a sslSocket if encrypt is false QObject::connect(sslSocket, SIGNAL(encrypted()), this, SLOT(_q_encrypted()), Qt::DirectConnection); QObject::connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(_q_sslErrors(QList<QSslError>)), Qt::DirectConnection); QObject::connect(sslSocket, SIGNAL(preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator*)), this, SLOT(_q_preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator*)), Qt::DirectConnection); QObject::connect(sslSocket, SIGNAL(encryptedBytesWritten(qint64)), this, SLOT(_q_encryptedBytesWritten(qint64)), Qt::DirectConnection); if (ignoreAllSslErrors) sslSocket->ignoreSslErrors(); if (!ignoreSslErrorsList.isEmpty()) sslSocket->ignoreSslErrors(ignoreSslErrorsList); if (!sslConfiguration.isNull()) sslSocket->setSslConfiguration(sslConfiguration); } else {
void QHttpNetworkConnectionChannel::init() { #ifndef QT_NO_SSL if (connection->d_func()->encrypt) socket = new QSslSocket; else socket = new QTcpSocket; #else socket = new QTcpSocket; #endif #ifndef QT_NO_BEARERMANAGEMENT //push session down to socket if (networkSession) socket->setProperty("_q_networksession", QVariant::fromValue(networkSession)); #endif #ifndef QT_NO_NETWORKPROXY // Set by QNAM anyway, but let's be safe here socket->setProxy(QNetworkProxy::NoProxy); #endif QObject::connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(_q_bytesWritten(qint64)), Qt::DirectConnection); QObject::connect(socket, SIGNAL(connected()), this, SLOT(_q_connected()), Qt::DirectConnection); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(_q_readyRead()), Qt::DirectConnection); // The disconnected() and error() signals may already come // while calling connectToHost(). // In case of a cached hostname or an IP this // will then emit a signal to the user of QNetworkReply // but cannot be caught because the user did not have a chance yet // to connect to QNetworkReply's signals. qRegisterMetaType<QAbstractSocket::SocketError>(); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(_q_disconnected()), Qt::QueuedConnection); QObject::connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(_q_error(QAbstractSocket::SocketError)), Qt::QueuedConnection); #ifndef QT_NO_NETWORKPROXY QObject::connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), this, SLOT(_q_proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), Qt::DirectConnection); #endif #ifndef QT_NO_SSL QSslSocket *sslSocket = qobject_cast<QSslSocket*>(socket); if (sslSocket) { // won't be a sslSocket if encrypt is false QObject::connect(sslSocket, SIGNAL(encrypted()), this, SLOT(_q_encrypted()), Qt::DirectConnection); QObject::connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(_q_sslErrors(QList<QSslError>)), Qt::DirectConnection); QObject::connect(sslSocket, SIGNAL(encryptedBytesWritten(qint64)), this, SLOT(_q_encryptedBytesWritten(qint64)), Qt::DirectConnection); if (ignoreAllSslErrors) sslSocket->ignoreSslErrors(); if (!ignoreSslErrorsList.isEmpty()) sslSocket->ignoreSslErrors(ignoreSslErrorsList); if (!sslConfiguration.isNull()) sslSocket->setSslConfiguration(sslConfiguration); } #endif #ifndef QT_NO_NETWORKPROXY if (proxy.type() != QNetworkProxy::NoProxy) socket->setProxy(proxy); #endif isInitialized = true; }
void WebSocketWorker::SetupSocket() { if (m_connectionType == kSSLServer) { #ifndef QT_NO_OPENSSL QSslSocket *pSslSocket = new QSslSocket(); if (pSslSocket->setSocketDescriptor(m_socketFD) && gCoreContext->CheckSubnet(pSslSocket)) { pSslSocket->setSslConfiguration(m_sslConfig); pSslSocket->startServerEncryption(); if (pSslSocket->waitForEncrypted(5000)) { LOG(VB_HTTP, LOG_INFO, "SSL Handshake occurred, connection encrypted"); LOG(VB_HTTP, LOG_INFO, QString("Using %1 cipher").arg(pSslSocket->sessionCipher().name())); } else { LOG(VB_HTTP, LOG_WARNING, "SSL Handshake FAILED, connection terminated"); delete pSslSocket; pSslSocket = nullptr; } } else { delete pSslSocket; pSslSocket = nullptr; } if (pSslSocket) m_socket = dynamic_cast<QTcpSocket *>(pSslSocket); else return; #else return; #endif } else // Plain old unencrypted socket { m_socket = new QTcpSocket(); m_socket->setSocketDescriptor(m_socketFD); if (!gCoreContext->CheckSubnet(m_socket)) { delete m_socket; m_socket = nullptr; return; } } m_socket->setSocketOption(QAbstractSocket::KeepAliveOption, QVariant(1)); connect(m_socket, SIGNAL(readyRead()), SLOT(doRead())); connect(m_socket, SIGNAL(disconnected()), SLOT(CloseConnection())); // Setup heartbeat m_heartBeat->setInterval(20000); // 20 second m_heartBeat->setSingleShot(false); connect(m_heartBeat, SIGNAL(timeout()), SLOT(SendHeartBeat())); }