DTLS client - improve the example
- Remove redundant pingTimer.start() call (timer is not a single shot timer and already started before this point in code). - Use connected UDP sockets to show that it can work with QDtls. - Replace (translated) string concatenations with formatted strings and 'arg' where needed. Change-Id: I8dba54f43464a718062cd897d24f89b75b99f2a4 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
d27dd47ffa
commit
ced0bd8068
@ -65,6 +65,7 @@ DtlsAssociation::DtlsAssociation(const QHostAddress &address, quint16 port,
|
||||
connect(&crypto, &QDtls::handshakeTimeout, this, &DtlsAssociation::handshakeTimeout);
|
||||
connect(&crypto, &QDtls::pskRequired, this, &DtlsAssociation::pskRequired);
|
||||
|
||||
socket.connectToHost(address.toString(), port);
|
||||
connect(&socket, &QUdpSocket::readyRead, this, &DtlsAssociation::readyRead);
|
||||
|
||||
pingTimer.setInterval(5000);
|
||||
@ -79,10 +80,22 @@ DtlsAssociation::~DtlsAssociation()
|
||||
|
||||
void DtlsAssociation::startHandshake()
|
||||
{
|
||||
if (socket.state() != QAbstractSocket::ConnectedState) {
|
||||
emit infoMessage(tr("%1: connecting UDP socket first ...").arg(name));
|
||||
connect(&socket, &QAbstractSocket::connected, this, &DtlsAssociation::udpSocketConnected);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!crypto.doHandshake(&socket, {}))
|
||||
emit errorMessage(name + tr(": failed to start a handshake - ") + crypto.dtlsErrorString());
|
||||
emit errorMessage(tr("%1: failed to start a handshake - %2").arg(name, crypto.dtlsErrorString()));
|
||||
else
|
||||
emit infoMessage(name + tr(": starting a handshake"));
|
||||
emit infoMessage(tr("%1: starting a handshake").arg(name));
|
||||
}
|
||||
|
||||
void DtlsAssociation::udpSocketConnected()
|
||||
{
|
||||
emit infoMessage(tr("%1: UDP socket is now in ConnectedState, continue with handshake ...").arg(name));
|
||||
startHandshake();
|
||||
}
|
||||
|
||||
void DtlsAssociation::readyRead()
|
||||
@ -90,7 +103,7 @@ void DtlsAssociation::readyRead()
|
||||
QByteArray dgram(socket.pendingDatagramSize(), '\0');
|
||||
const qint64 bytesRead = socket.readDatagram(dgram.data(), dgram.size());
|
||||
if (bytesRead <= 0) {
|
||||
emit warningMessage(name + tr(": spurious read notification?"));
|
||||
emit warningMessage(tr("%1: spurious read notification?").arg(name));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -99,45 +112,44 @@ void DtlsAssociation::readyRead()
|
||||
const QByteArray plainText = crypto.decryptDatagram(&socket, dgram);
|
||||
if (plainText.size()) {
|
||||
emit serverResponse(name, dgram, plainText);
|
||||
pingTimer.start();
|
||||
return;
|
||||
}
|
||||
|
||||
if (crypto.dtlsError() == QDtlsError::RemoteClosedConnectionError) {
|
||||
emit errorMessage(name + tr(": shutdown alert received"));
|
||||
emit errorMessage(tr("%1: shutdown alert received").arg(name));
|
||||
socket.close();
|
||||
pingTimer.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
emit warningMessage(name + tr(": zero-length datagram received?"));
|
||||
emit warningMessage(tr("%1: zero-length datagram received?").arg(name));
|
||||
} else {
|
||||
if (!crypto.doHandshake(&socket, dgram)) {
|
||||
emit errorMessage(name + tr(": handshake error - ") + crypto.dtlsErrorString());
|
||||
emit errorMessage(tr("%1: handshake error - %2").arg(name, crypto.dtlsErrorString()));
|
||||
return;
|
||||
}
|
||||
if (crypto.connectionEncrypted()) {
|
||||
emit infoMessage(name + tr(": encrypted connection established!"));
|
||||
emit infoMessage(tr("%1: encrypted connection established!").arg(name));
|
||||
pingTimer.start();
|
||||
pingTimeout();
|
||||
} else {
|
||||
emit infoMessage(name + tr(": continuing with handshake ..."));
|
||||
emit infoMessage(tr("%1: continuing with handshake ...").arg(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DtlsAssociation::handshakeTimeout()
|
||||
{
|
||||
emit warningMessage(name + tr(": handshake timeout, trying to re-transmit"));
|
||||
emit warningMessage(tr("%1: handshake timeout, trying to re-transmit").arg(name));
|
||||
if (!crypto.handleTimeout(&socket))
|
||||
emit errorMessage(name + tr(": failed to re-transmit - ") + crypto.dtlsErrorString());
|
||||
emit errorMessage(tr("%1: failed to re-transmit - %2").arg(name, crypto.dtlsErrorString()));
|
||||
}
|
||||
|
||||
void DtlsAssociation::pskRequired(QSslPreSharedKeyAuthenticator *auth)
|
||||
{
|
||||
Q_ASSERT(auth);
|
||||
|
||||
emit infoMessage(name + tr(": providing pre-shared key ..."));
|
||||
emit infoMessage(tr("%1: providing pre-shared key ...").arg(name));
|
||||
auth->setIdentity(name.toLatin1());
|
||||
auth->setPreSharedKey(QByteArrayLiteral("\x1a\x2b\x3c\x4d\x5e\x6f"));
|
||||
}
|
||||
@ -147,7 +159,7 @@ void DtlsAssociation::pingTimeout()
|
||||
static const QString message = QStringLiteral("I am %1, please, accept our ping %2");
|
||||
const qint64 written = crypto.writeDatagramEncrypted(&socket, message.arg(name).arg(ping).toLatin1());
|
||||
if (written <= 0) {
|
||||
emit errorMessage(name + tr(": failed to send a ping - ") + crypto.dtlsErrorString());
|
||||
emit errorMessage(tr("%1: failed to send a ping - %2").arg(name, crypto.dtlsErrorString()));
|
||||
pingTimer.stop();
|
||||
return;
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ signals:
|
||||
|
||||
private slots:
|
||||
|
||||
void udpSocketConnected();
|
||||
void readyRead();
|
||||
void handshakeTimeout();
|
||||
void pskRequired(QSslPreSharedKeyAuthenticator *auth);
|
||||
|
Loading…
Reference in New Issue
Block a user