10 #include "../serverapp/serverapp.h"
11 #include "../../shared/settings.h"
12 #include "../../shared/network.h"
13 #include "../../shared/util.h"
14 #include <QCryptographicHash>
15 #include <QSslCertificate>
18 #define UDPBUFSIZ 9000
19 #define SPAM_CUTOFF 50
20 #define SPAM_MODERATE_INTERVAL 6787
21 #define SPAM_MODERATE_AT_ONCE 100
33 :
QObject(parent), _socket(this)
52 static quint16
hash(
const QHostAddress& host)
54 static quint16 seed1 = 0, seed2 = 0;
57 seed1 = quint16(
slxrand() & 0xffff);
58 seed2 = quint16(
slxrand() & 0xffff);
61 if (host.protocol() == QAbstractSocket::IPv4Protocol) {
63 quint32 addr = host.toIPv4Address();
65 memcpy(data, &addr, len);
66 }
else if (host.protocol() == QAbstractSocket::IPv6Protocol) {
70 memcpy(data, host.toIPv6Address().c, len);
84 for (quint8 i = 0; i < len; ++i) {
85 result = quint16(((result << 1) + data[i]) ^ mod);
86 mod = quint16(mod + seed2 + data[i]);
121 static int certFails = 0;
125 while (
_socket.hasPendingDatagrams()) {
131 qDebug() <<
"SD: Potential (D)DoS from " << addr.toString();
139 qDebug() <<
"Corrupted service discovery message from" << addr.toString();
148 if (salt1.size() < 16 || salt2.size() < 16)
152 qDebug() <<
"SD: Client" << addr.toString() <<
"did not supply IP in list:" << iplist;
157 !(
serverApp->getCurrentRoom()->clientPositions.contains(addr.toString()))) {
159 qDebug() <<
"SD: Mismatch, neither session name match, nor client for current room" <<
serverApp->getCurrentRoom()->tutorIP;
160 qDebug() <<
"SD: Allowed clients from room:" <<
serverApp->getCurrentRoom()->clientPositions.keys();
164 qDebug(
"Got matching discovery request...");
167 QSslCertificate cert;
169 if (++certFails > 5) {
174 QByteArray certhash(cert.digest(QCryptographicHash::Sha1));
bool writeMessage(QAbstractSocket *socket)
#define SERVICE_DISCOVERY_PORT
static quint16 hash(const QHostAddress &host)
hash
QByteArray genSha1(const QByteArray *a, const QByteArray *b, const QByteArray *c, const QByteArray *d, const QByteArray *e)
bool getPrivateKeyAndCert(const QString &name, QSslKey &key, QSslCertificate &cert)
DiscoveryListener(QObject *parent)
DiscoveryListener::DiscoveryListener.
#define SD_PACKET_TABLE_SIZE
void timerEvent(QTimerEvent *event) override
Decrease packet counters per source IP in our "spam protection" table.
bool isAddressInList(const QString &list, const QString &address)
#define SPAM_MODERATE_INTERVAL
int readMessage(QAbstractSocket *socket)
static const QByteArray CLIENT_PORT_ARRAY(QString::number(CLIENT_PORT).toUtf8())
quint8 _packetCounter[SD_PACKET_TABLE_SIZE]
void onReadyRead()
Incoming UDP packet on service discovery port - handle.
QString interfaceAddressesToString()
Returns list of all addresses assigned to the interfaces of this machine.
#define SPAM_MODERATE_AT_ONCE
QByteArray getFieldBytes(const QByteArray &key) const
~DiscoveryListener() override
DiscoveryListener::~DiscoveryListener.
void setField(const QByteArray &key, const QByteArray &value)