89 serverConfiguration = QSslConfiguration::defaultDtlsConfiguration();
126 void DtlsServer::readyRead()
130 if (bytesToRead <= 0) {
139 &peerAddress, &peerPort);
140 if (bytesRead <= 0) {
145 dgram.resize(bytesRead);
148 if (peerAddress.
isNull() || !peerPort) {
153 const auto client = std::find_if(knownClients.begin(), knownClients.end(),
154 [&](
const std::unique_ptr<QDtls> &
connection){
155 return connection->peerAddress() == peerAddress
156 && connection->peerPort() == peerPort;
161 if (client == knownClients.end())
162 return handleNewConnection(peerAddress, peerPort, dgram);
166 if ((*client)->isConnectionEncrypted()) {
167 decryptDatagram(client->get(), dgram);
169 knownClients.erase(client);
175 doHandshake(client->get(), dgram);
191 void DtlsServer::handleNewConnection(
const QHostAddress &peerAddress,
203 newConnection->setDtlsConfiguration(serverConfiguration);
204 newConnection->setPeer(peerAddress, peerPort);
206 this, &DtlsServer::pskRequired);
207 knownClients.push_back(std::move(newConnection));
208 doHandshake(knownClients.back().get(),
clientHello);
251 connection->writeDatagramEncrypted(&serverSocket,
tr(
"to %1: ACK").
arg(peerInfo).toLatin1());
261 void DtlsServer::shutdown()
263 for (
const auto &
connection : qExchange(knownClients, {}))
266 serverSocket.
close();
void infoMessage(const QString &message)
bool isListening() const
[2]
void datagramReceived(const QString &peerInfo, const QByteArray &cipherText, const QByteArray &plainText)
void errorMessage(const QString &message)
void warningMessage(const QString &message)
bool listen(const QHostAddress &address, quint16 port)
[2]
QHostAddress localAddress() const
virtual bool bind(const QHostAddress &address, quint16 port=0, BindMode mode=DefaultForPlatform)
quint16 localPort() const
The QByteArray class provides an array of bytes.
qsizetype size() const noexcept
QString dtlsErrorString() const
bool verifyClient(QUdpSocket *socket, const QByteArray &dgram, const QHostAddress &address, quint16 port)
QDtlsError dtlsError() const
This class provides encryption for UDP sockets.
bool doHandshake(QUdpSocket *socket, const QByteArray &dgram={})
QString dtlsErrorString() const
HandshakeState handshakeState() const
QHostAddress peerAddress() const
void pskRequired(QSslPreSharedKeyAuthenticator *authenticator)
The QHostAddress class provides an IP address.\inmodule QtNetwork.
QString errorString() const
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
void setPreSharedKeyIdentityHint(const QByteArray &hint)
The QSslPreSharedKeyAuthenticator class provides authentication data for pre shared keys (PSK) cipher...
Q_NETWORK_EXPORT QByteArray identity() const
Q_NETWORK_EXPORT void setPreSharedKey(const QByteArray &preSharedKey)
The QString class provides a Unicode character string.
static QString fromLatin1(QByteArrayView ba)
qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *host=nullptr, quint16 *port=nullptr)
qint64 pendingDatagramSize() const
backing_store_ptr info
[4]
QString peer_info(const QHostAddress &address, quint16 port)
QString connection_info(QDtls *connection)
constexpr Initialization Uninitialized
#define QByteArrayLiteral(str)
DBusConnection * connection
@ RemoteClosedConnectionError
GLuint GLuint64EXT address
#define QStringLiteral(str)
QByteArray clientHello(serverSocket.pendingDatagramSize(), Qt::Uninitialized)