2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// Copyright (C) 2018 Intel Corporation.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
#include "connection.h"
|
|
|
|
#include "peermanager.h"
|
|
|
|
|
2023-05-16 10:58:47 +00:00
|
|
|
#include <QNetworkInterface>
|
2023-06-14 16:48:19 +00:00
|
|
|
#include <QUuid>
|
2023-05-16 10:58:47 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
static const qint32 BroadcastInterval = 2000;
|
|
|
|
static const unsigned broadcastPort = 45000;
|
|
|
|
|
|
|
|
PeerManager::PeerManager(Client *client)
|
2023-05-16 11:17:18 +00:00
|
|
|
: QObject(client), client(client)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2018-01-18 01:12:27 +00:00
|
|
|
static const char *envVariables[] = {
|
|
|
|
"USERNAME", "USER", "USERDOMAIN", "HOSTNAME", "DOMAINNAME"
|
|
|
|
};
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-01-18 01:12:27 +00:00
|
|
|
for (const char *varname : envVariables) {
|
|
|
|
username = qEnvironmentVariable(varname);
|
|
|
|
if (!username.isNull())
|
2016-12-15 04:14:16 +00:00
|
|
|
break;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (username.isEmpty())
|
|
|
|
username = "unknown";
|
|
|
|
|
2023-06-14 16:48:19 +00:00
|
|
|
// We generate a unique per-process identifier so we can avoid multiple
|
|
|
|
// connections to/from the same remote peer as well as ignore our own
|
|
|
|
// broadcasts.
|
|
|
|
localUniqueId = QUuid::createUuid().toByteArray();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
updateAddresses();
|
|
|
|
|
|
|
|
broadcastSocket.bind(QHostAddress::Any, broadcastPort, QUdpSocket::ShareAddress
|
|
|
|
| QUdpSocket::ReuseAddressHint);
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(&broadcastSocket, &QUdpSocket::readyRead,
|
|
|
|
this, &PeerManager::readBroadcastDatagram);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
broadcastTimer.setInterval(BroadcastInterval);
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(&broadcastTimer, &QTimer::timeout,
|
|
|
|
this, &PeerManager::sendBroadcastDatagram);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerManager::setServerPort(int port)
|
|
|
|
{
|
|
|
|
serverPort = port;
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:12:27 +00:00
|
|
|
QString PeerManager::userName() const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:48:19 +00:00
|
|
|
QByteArray PeerManager::uniqueId() const
|
|
|
|
{
|
|
|
|
return localUniqueId;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void PeerManager::startBroadcasting()
|
|
|
|
{
|
|
|
|
broadcastTimer.start();
|
|
|
|
}
|
|
|
|
|
2018-12-31 16:41:11 +00:00
|
|
|
bool PeerManager::isLocalHostAddress(const QHostAddress &address) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2023-05-23 11:10:41 +00:00
|
|
|
return ipAddresses.contains(address);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerManager::sendBroadcastDatagram()
|
|
|
|
{
|
2018-01-18 01:12:27 +00:00
|
|
|
QByteArray datagram;
|
|
|
|
{
|
|
|
|
QCborStreamWriter writer(&datagram);
|
|
|
|
writer.startArray(2);
|
2023-06-14 16:48:19 +00:00
|
|
|
writer.append(localUniqueId);
|
2018-01-18 01:12:27 +00:00
|
|
|
writer.append(serverPort);
|
|
|
|
writer.endArray();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
bool validBroadcastAddresses = true;
|
2022-10-06 09:08:21 +00:00
|
|
|
for (const QHostAddress &address : std::as_const(broadcastAddresses)) {
|
2023-05-23 11:20:44 +00:00
|
|
|
if (broadcastSocket.writeDatagram(datagram, address, broadcastPort) == -1)
|
2011-04-27 10:05:43 +00:00
|
|
|
validBroadcastAddresses = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validBroadcastAddresses)
|
|
|
|
updateAddresses();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerManager::readBroadcastDatagram()
|
|
|
|
{
|
|
|
|
while (broadcastSocket.hasPendingDatagrams()) {
|
|
|
|
QHostAddress senderIp;
|
|
|
|
quint16 senderPort;
|
|
|
|
QByteArray datagram;
|
|
|
|
datagram.resize(broadcastSocket.pendingDatagramSize());
|
|
|
|
if (broadcastSocket.readDatagram(datagram.data(), datagram.size(),
|
|
|
|
&senderIp, &senderPort) == -1)
|
|
|
|
continue;
|
|
|
|
|
2018-01-18 01:12:27 +00:00
|
|
|
int senderServerPort;
|
2023-06-14 16:48:19 +00:00
|
|
|
QByteArray peerUniqueId;
|
2018-01-18 01:12:27 +00:00
|
|
|
{
|
|
|
|
// decode the datagram
|
|
|
|
QCborStreamReader reader(datagram);
|
|
|
|
if (reader.lastError() != QCborError::NoError || !reader.isArray())
|
|
|
|
continue;
|
|
|
|
if (!reader.isLengthKnown() || reader.length() != 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
reader.enterContainer();
|
2023-06-14 16:48:19 +00:00
|
|
|
if (reader.lastError() != QCborError::NoError || !reader.isByteArray())
|
2018-01-18 01:12:27 +00:00
|
|
|
continue;
|
2023-06-14 16:48:19 +00:00
|
|
|
auto r = reader.readByteArray();
|
|
|
|
while (r.status == QCborStreamReader::Ok) {
|
|
|
|
peerUniqueId = r.data;
|
|
|
|
r = reader.readByteArray();
|
2018-01-18 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reader.lastError() != QCborError::NoError || !reader.isUnsignedInteger())
|
|
|
|
continue;
|
|
|
|
senderServerPort = reader.toInteger();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-06-14 16:48:19 +00:00
|
|
|
if (peerUniqueId == localUniqueId)
|
2011-04-27 10:05:43 +00:00
|
|
|
continue;
|
|
|
|
|
2023-06-14 16:48:19 +00:00
|
|
|
if (!client->hasConnection(peerUniqueId)) {
|
2011-04-27 10:05:43 +00:00
|
|
|
Connection *connection = new Connection(this);
|
|
|
|
emit newConnection(connection);
|
|
|
|
connection->connectToHost(senderIp, senderServerPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerManager::updateAddresses()
|
|
|
|
{
|
|
|
|
broadcastAddresses.clear();
|
|
|
|
ipAddresses.clear();
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
|
|
|
|
for (const QNetworkInterface &interface : interfaces) {
|
|
|
|
const QList<QNetworkAddressEntry> entries = interface.addressEntries();
|
|
|
|
for (const QNetworkAddressEntry &entry : entries) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QHostAddress broadcastAddress = entry.broadcast();
|
|
|
|
if (broadcastAddress != QHostAddress::Null && entry.ip() != QHostAddress::LocalHost) {
|
|
|
|
broadcastAddresses << broadcastAddress;
|
|
|
|
ipAddresses << entry.ip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|