summaryrefslogtreecommitdiffstats
path: root/src/server/net/discoverylistener.cpp
diff options
context:
space:
mode:
authorChristian Klinger2016-09-29 15:40:18 +0200
committerChristian Klinger2016-09-29 15:40:18 +0200
commit3d82bfc698165bb20f3c38d42f2b77ab855c1edf (patch)
tree2c170bcdd5c89259bb0b530fa11fdfe701c99850 /src/server/net/discoverylistener.cpp
parentadded an information dialog that displays ip and hostname. (diff)
downloadpvs2-3d82bfc698165bb20f3c38d42f2b77ab855c1edf.tar.gz
pvs2-3d82bfc698165bb20f3c38d42f2b77ab855c1edf.tar.xz
pvs2-3d82bfc698165bb20f3c38d42f2b77ab855c1edf.zip
astyle.
Diffstat (limited to 'src/server/net/discoverylistener.cpp')
-rw-r--r--src/server/net/discoverylistener.cpp30
1 files changed, 10 insertions, 20 deletions
diff --git a/src/server/net/discoverylistener.cpp b/src/server/net/discoverylistener.cpp
index 892ca53..8eb1091 100644
--- a/src/server/net/discoverylistener.cpp
+++ b/src/server/net/discoverylistener.cpp
@@ -55,21 +55,18 @@ DiscoveryListener::~DiscoveryListener()
static quint16 hash(const QHostAddress& host)
{
static quint16 seed1 = 0, seed2 = 0;
- while (seed1 == 0) // Make sure the algorithm uses different seeds each time the program is
- { // run to prevent hash collision attacks
+ while (seed1 == 0) { // Make sure the algorithm uses different seeds each time the program is
+ // run to prevent hash collision attacks
seed1 = (quint16)(qrand() & 0xffff);
seed2 = (quint16)(qrand() & 0xffff);
}
quint8 data[16], len;
- if (host.protocol() == QAbstractSocket::IPv4Protocol)
- {
+ if (host.protocol() == QAbstractSocket::IPv4Protocol) {
// IPv4
quint32 addr = host.toIPv4Address();
len = 4;
memcpy(data, &addr, len);
- }
- else if (QAbstractSocket::IPv6Protocol)
- {
+ } else if (QAbstractSocket::IPv6Protocol) {
// IPv6
len = 16;
// Fast version (might break with future qt versions)
@@ -79,9 +76,7 @@ static quint16 hash(const QHostAddress& host)
for (int i = 0; i < len; ++i)
data[i] = addr[i];
*/
- }
- else
- {
+ } else {
// Durr?
len = 2;
data[0] = (quint16)qrand();
@@ -89,8 +84,7 @@ static quint16 hash(const QHostAddress& host)
}
quint16 result = 0;
quint16 mod = seed1;
- for (quint8 i = 0; i < len; ++i)
- {
+ for (quint8 i = 0; i < len; ++i) {
result = ((result << 1) + data[i]) ^ mod; // because of the shift this algo is not suitable for len(input) > 8
mod += seed2 + data[i];
}
@@ -107,8 +101,7 @@ static quint16 hash(const QHostAddress& host)
*/
void DiscoveryListener::timerEvent(QTimerEvent* event)
{
- for (int i = 0; i < SPAM_MODERATE_AT_ONCE; ++i)
- {
+ for (int i = 0; i < SPAM_MODERATE_AT_ONCE; ++i) {
if (++_counterResetPos >= SD_PACKET_TABLE_SIZE)
_counterResetPos = 0;
if (_packetCounter[_counterResetPos] > 10)
@@ -130,14 +123,12 @@ void DiscoveryListener::onReadyRead()
char data[UDPBUFSIZ];
QHostAddress addr;
quint16 port;
- while (_socket.hasPendingDatagrams())
- {
+ while (_socket.hasPendingDatagrams()) {
const qint64 size = _socket.readDatagram(data, UDPBUFSIZ, &addr, &port);
if (size <= 0)
continue;
const quint16 bucket = hash(addr) % SD_PACKET_TABLE_SIZE;
- if (_packetCounter[bucket] > SPAM_CUTOFF)
- {
+ if (_packetCounter[bucket] > SPAM_CUTOFF) {
qDebug() << "SD: Potential (D)DoS from " << _socket.peerAddress().toString();
// emit some signal and pop up a big warning that someone is flooding/ddosing the PVS SD
// ... on the other hand, will the user understand? ;)
@@ -160,8 +151,7 @@ void DiscoveryListener::onReadyRead()
continue;
// If so, check if the submitted hash seems valid
if (genSha1(&Global::sessionNameArray(), &salt1, &iplist) != hash &&
- !(Global::getCurrentRoom()->clientPositions.contains(addr.toString())))
- {
+ !(Global::getCurrentRoom()->clientPositions.contains(addr.toString()))) {
// did not match local session name and client is not in same room.
continue;
}