281void QSslServer::incomingConnection(qintptr socket)
283 QSslSocket *pSslSocket =
new QSslSocket(
this);
285 pSslSocket->setSslConfiguration(sslConfiguration());
287 if (Q_LIKELY(pSslSocket->setSocketDescriptor(socket))) {
288 connect(pSslSocket, &QSslSocket::peerVerifyError,
this,
289 [
this, pSslSocket](
const QSslError &error) {
290 Q_EMIT peerVerifyError(pSslSocket, error);
292 connect(pSslSocket, &QSslSocket::sslErrors,
this,
293 [
this, pSslSocket](
const QList<QSslError> &errors) {
294 Q_EMIT sslErrors(pSslSocket, errors);
296 connect(pSslSocket, &QAbstractSocket::errorOccurred,
this,
297 [
this, pSslSocket](QAbstractSocket::SocketError error) {
298 Q_EMIT errorOccurred(pSslSocket, error);
299 if (!pSslSocket->isEncrypted())
300 pSslSocket->deleteLater();
302 connect(pSslSocket, &QSslSocket::encrypted,
this, [
this, pSslSocket]() {
304 d->removeSocketData(quintptr(pSslSocket));
305 pSslSocket->disconnect(
this);
306 addPendingConnection(pSslSocket);
308 connect(pSslSocket, &QSslSocket::preSharedKeyAuthenticationRequired,
this,
309 [
this, pSslSocket](QSslPreSharedKeyAuthenticator *authenticator) {
310 Q_EMIT preSharedKeyAuthenticationRequired(pSslSocket, authenticator);
312 connect(pSslSocket, &QSslSocket::alertSent,
this,
313 [
this, pSslSocket](QSsl::AlertLevel level, QSsl::AlertType type,
314 const QString &description) {
315 Q_EMIT alertSent(pSslSocket, level, type, description);
317 connect(pSslSocket, &QSslSocket::alertReceived,
this,
318 [
this, pSslSocket](QSsl::AlertLevel level, QSsl::AlertType type,
319 const QString &description) {
320 Q_EMIT alertReceived(pSslSocket, level, type, description);
322 connect(pSslSocket, &QSslSocket::handshakeInterruptedOnError,
this,
323 [
this, pSslSocket](
const QSslError &error) {
324 Q_EMIT handshakeInterruptedOnError(pSslSocket, error);
327 d_func()->initializeHandshakeProcess(pSslSocket);
331void QSslServerPrivate::initializeHandshakeProcess(QSslSocket *socket)
334 QMetaObject::Connection readyRead = QObject::connect(
335 socket, &QSslSocket::readyRead, q, [
this]() { checkClientHelloAndContinue(); });
337 QMetaObject::Connection destroyed =
338 QObject::connect(socket, &QSslSocket::destroyed, q, [
this](QObject *obj) {
341 removeSocketData(quintptr(obj));
343 auto it = socketData.emplace(quintptr(socket), readyRead, destroyed, std::make_shared<QTimer>());
344 it->timeoutTimer->setSingleShot(
true);
345 it->timeoutTimer->callOnTimeout(q, [
this, socket]() { handleHandshakeTimedOut(socket); });
346 it->timeoutTimer->setInterval(handshakeTimeout);
347 it->timeoutTimer->start();
367void QSslServerPrivate::checkClientHelloAndContinue()
370 QSslSocket *socket = qobject_cast<QSslSocket *>(q->sender());
371 if (Q_UNLIKELY(!socket) || socket->bytesAvailable() <= 0)
375 if (socket->peek(&byte, 1) != 1) {
376 socket->deleteLater();
380 auto it = socketData.find(quintptr(socket));
381 const bool foundData = it != socketData.end();
382 if (foundData && it->readyReadConnection)
383 QObject::disconnect(std::exchange(it->readyReadConnection, {}));
385 constexpr char CLIENT_HELLO = 0x16;
386 if (byte != CLIENT_HELLO) {
387 socket->disconnectFromHost();
388 socket->deleteLater();
394 it->timeoutTimer->start();
396 socket->startServerEncryption();
397 Q_EMIT q->startedEncryptionHandshake(socket);
400void QSslServerPrivate::handleHandshakeTimedOut(QSslSocket *socket)
403 removeSocketData(quintptr(socket));
404 socket->disconnectFromHost();
405 Q_EMIT q->errorOccurred(socket, QAbstractSocket::SocketTimeoutError);
406 socket->deleteLater();
407 if (!socketEngine->isReadNotificationEnabled() && totalPendingConnections() < maxConnections)
408 q->resumeAccepting();