diff options
Diffstat (limited to 'examples/network/secureudpserver')
-rw-r--r-- | examples/network/secureudpserver/mainwindow.cpp | 2 | ||||
-rw-r--r-- | examples/network/secureudpserver/mainwindow.h | 3 | ||||
-rw-r--r-- | examples/network/secureudpserver/server.cpp | 28 | ||||
-rw-r--r-- | examples/network/secureudpserver/server.h | 6 |
4 files changed, 30 insertions, 9 deletions
diff --git a/examples/network/secureudpserver/mainwindow.cpp b/examples/network/secureudpserver/mainwindow.cpp index d751ec931d..ef1974c311 100644 --- a/examples/network/secureudpserver/mainwindow.cpp +++ b/examples/network/secureudpserver/mainwindow.cpp @@ -104,6 +104,7 @@ void MainWindow::updateUi() : ui->startButton->setText(tr("Start listening")); } +//! [0] const QString colorizer(QStringLiteral("<font color=\"%1\">%2</font><br>")); void MainWindow::addErrorMessage(const QString &message) @@ -134,3 +135,4 @@ void MainWindow::addClientMessage(const QString &peerInfo, const QByteArray &dat QString::fromUtf8(plainText)); ui->messages->insertHtml(colorizer.arg(messageColor, html)); } +//! [0] diff --git a/examples/network/secureudpserver/mainwindow.h b/examples/network/secureudpserver/mainwindow.h index 0c914f5021..b39d984d50 100644 --- a/examples/network/secureudpserver/mainwindow.h +++ b/examples/network/secureudpserver/mainwindow.h @@ -69,12 +69,10 @@ class MainWindow : public QMainWindow Q_OBJECT public: - MainWindow(); ~MainWindow(); private slots: - void addErrorMessage(const QString &message); void addWarningMessage(const QString &message); void addInfoMessage(const QString &message); @@ -85,7 +83,6 @@ private slots: void on_quitButton_clicked(); private: - void updateUi(); Ui::MainWindow *ui = nullptr; diff --git a/examples/network/secureudpserver/server.cpp b/examples/network/secureudpserver/server.cpp index 763024e4f4..6870123163 100644 --- a/examples/network/secureudpserver/server.cpp +++ b/examples/network/secureudpserver/server.cpp @@ -87,6 +87,7 @@ QString connection_info(QSharedPointer<QDtls> connection) } // unnamed namespace +//! [1] DtlsServer::DtlsServer() { connect(&serverSocket, &QAbstractSocket::readyRead, this, &DtlsServer::readyRead); @@ -94,12 +95,14 @@ DtlsServer::DtlsServer() serverConfiguration.setPreSharedKeyIdentityHint("Qt DTLS example server"); serverConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone); } +//! [1] DtlsServer::~DtlsServer() { shutdown(); } +//! [2] bool DtlsServer::listen(const QHostAddress &address, quint16 port) { if (address != serverSocket.localAddress() || port != serverSocket.localPort()) { @@ -113,6 +116,7 @@ bool DtlsServer::listen(const QHostAddress &address, quint16 port) return listening; } +//! [2] bool DtlsServer::isListening() const { @@ -126,6 +130,7 @@ void DtlsServer::close() void DtlsServer::readyRead() { + //! [3] const qint64 bytesToRead = serverSocket.pendingDatagramSize(); if (bytesToRead <= 0) { emit warningMessage(tr("A spurious read notification")); @@ -143,7 +148,8 @@ void DtlsServer::readyRead() } dgram.resize(bytesRead); - + //! [3] + //! [4] if (peerAddress.isNull() || !peerPort) { emit warningMessage(tr("Failed to extract peer info (address, port)")); return; @@ -154,20 +160,28 @@ void DtlsServer::readyRead() return connection->peerAddress() == peerAddress && connection->peerPort() == peerPort; }); + //! [4] + //! [5] if (client == knownClients.end()) return handleNewConnection(peerAddress, peerPort, dgram); + //! [5] + //! [6] if ((*client)->isConnectionEncrypted()) { decryptDatagram(*client, dgram); if ((*client)->dtlsError() == QDtlsError::RemoteClosedConnectionError) knownClients.erase(client); return; } + //! [6] + //! [7] doHandshake(*client, dgram); + //! [7] } +//! [13] void DtlsServer::pskRequired(QSslPreSharedKeyAuthenticator *auth) { Q_ASSERT(auth); @@ -176,7 +190,9 @@ void DtlsServer::pskRequired(QSslPreSharedKeyAuthenticator *auth) .arg(QString::fromLatin1(auth->identity()))); auth->setPreSharedKey(QByteArrayLiteral("\x1a\x2b\x3c\x4d\x5e\x6f")); } +//! [13] +//! [8] void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, quint16 peerPort, const QByteArray &clientHello) { @@ -186,7 +202,8 @@ void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, const QString peerInfo = peer_info(peerAddress, peerPort); if (cookieSender.verifyClient(&serverSocket, clientHello, peerAddress, peerPort)) { emit infoMessage(peerInfo + tr(": verified, starting a handshake")); - + //! [8] + //! [9] DtlsConnection newConnection(new QDtls(QSslSocket::SslServerMode)); newConnection->setDtlsConfiguration(serverConfiguration); newConnection->setPeer(peerAddress, peerPort); @@ -194,6 +211,7 @@ void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, this, &DtlsServer::pskRequired); knownClients.push_back(newConnection); doHandshake(newConnection, clientHello); + //! [9] } else if (cookieSender.dtlsError() != QDtlsError::NoError) { emit errorMessage(tr("DTLS error: ") + cookieSender.dtlsErrorString()); } else { @@ -201,6 +219,7 @@ void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, } } +//! [11] void DtlsServer::doHandshake(DtlsConnection newConnection, const QByteArray &clientHello) { const bool result = newConnection->doHandshake(&serverSocket, clientHello); @@ -223,7 +242,9 @@ void DtlsServer::doHandshake(DtlsConnection newConnection, const QByteArray &cli Q_UNREACHABLE(); } } +//! [11] +//! [12] void DtlsServer::decryptDatagram(DtlsConnection connection, const QByteArray &clientMessage) { Q_ASSERT(connection->isConnectionEncrypted()); @@ -239,7 +260,9 @@ void DtlsServer::decryptDatagram(DtlsConnection connection, const QByteArray &cl emit errorMessage(peerInfo + ": " + connection->dtlsErrorString()); } } +//! [12] +//! [14] void DtlsServer::shutdown() { for (DtlsConnection &connection : knownClients) @@ -248,5 +271,6 @@ void DtlsServer::shutdown() knownClients.clear(); serverSocket.close(); } +//! [14] QT_END_NAMESPACE diff --git a/examples/network/secureudpserver/server.h b/examples/network/secureudpserver/server.h index 33444f7407..b720368e7b 100644 --- a/examples/network/secureudpserver/server.h +++ b/examples/network/secureudpserver/server.h @@ -57,12 +57,12 @@ QT_BEGIN_NAMESPACE +//! [0] class DtlsServer : public QObject { Q_OBJECT public: - DtlsServer(); ~DtlsServer(); @@ -71,7 +71,6 @@ public: void close(); signals: - void errorMessage(const QString &message); void warningMessage(const QString &message); void infoMessage(const QString &message); @@ -80,12 +79,10 @@ signals: const QByteArray &plainText); private slots: - void readyRead(); void pskRequired(QSslPreSharedKeyAuthenticator *auth); private: - void handleNewConnection(const QHostAddress &peerAddress, quint16 peerPort, const QByteArray &clientHello); @@ -103,6 +100,7 @@ private: Q_DISABLE_COPY(DtlsServer) }; +//! [0] QT_END_NAMESPACE |