Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_network_ssl_qdtlscookie.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5class DtlsServer : public QObject
6{
7public:
9 // ...
10
11private:
12 void readyRead();
13 // ...
14
15 QUdpSocket serverSocket;
16 QDtlsClientVerifier verifier;
17 // ...
18};
19
20bool DtlsServer::listen(const QHostAddress &serverAddress, quint16 serverPort)
21{
22 if (serverSocket.bind(serverAddress, serverPort))
23 connect(&serverSocket, &QUdpSocket::readyRead, this, &DtlsServer::readyRead);
24 return serverSocket.state() == QAbstractSocket::BoundState;
25}
26
27void DtlsServer::readyRead()
28{
31 quint16 port = {};
32 serverSocket.readDatagram(dgram.data(), dgram.size(), &address, &port);
33 if (verifiedClients.contains({address, port}) {
34 // This client was verified previously, we either continue the
35 // handshake or decrypt the incoming message.
36 } else if (verifier.verifyClient(&serverSocket, dgram, address, port)) {
37 // Apparently we have a real DTLS client who wants to send us
38 // encrypted datagrams. Remember this client as verified
39 // and proceed with a handshake.
40 } else {
41 // No matching cookie was found in the incoming datagram,
42 // verifyClient() has sent a ClientVerify message.
43 // We'll hear from the client again soon, if they're real.
44 }
45}
47
49void DtlsServer::updateServerSecret()
50{
51 const QByteArray newSecret(generateCryptoStrongSecret());
52 if (newSecret.size()) {
53 usedCookies.append(newSecret);
55 }
56}
58
60if (!verifier.verifyClient(&socket, message, address, port)) {
61 switch (verifyClient.dtlsError()) {
63 // Not verified yet, but no errors found and we have to wait for the next
64 // message from this client.
65 return;
67 // This error is fatal, nothing we can do about it.
68 // Probably, quit the server after reporting the error.
69 return;
71 // There is some problem in QUdpSocket, handle it (see QUdpSocket::error())
72 return;
74 default:
75 Q_UNREACHABLE();
76 }
77}
bool listen(const QHostAddress &address, quint16 port)
virtual bool bind(const QHostAddress &address, quint16 port=0, BindMode mode=DefaultForPlatform)
SocketState state() const
Returns the state of the socket.
\inmodule QtCore
Definition qbytearray.h:57
This class implements server-side DTLS cookie generation and verification.
Definition qdtls.h:44
bool verifyClient(QUdpSocket *socket, const QByteArray &dgram, const QHostAddress &address, quint16 port)
socket must be a valid pointer, dgram must be a non-empty datagram, address cannot be null,...
Definition qdtls.cpp:431
bool setCookieGeneratorParameters(const GeneratorParameters &params)
Sets the secret and the cryptographic hash algorithm from params.
Definition qdtls.cpp:387
The QHostAddress class provides an IP address.
void readyRead()
This signal is emitted once every time new data is available for reading from the device's current re...
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\reentrant
Definition qudpsocket.h:21
qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *host=nullptr, quint16 *port=nullptr)
Receives a datagram no larger than maxSize bytes and stores it in data.
qint64 pendingDatagramSize() const
Returns the size of the first pending UDP datagram.
constexpr Initialization Uninitialized
@ TlsInitializationError
@ UnderlyingSocketError
@ InvalidInputParameters
EGLOutputPortEXT port
GLuint GLsizei const GLchar * message
GLuint GLuint64EXT address
unsigned short quint16
Definition qtypes.h:48
if(qFloatDistance(a, b)<(1<< 7))
[0]
QTcpSocket * socket
[1]
socketLayer listen()