2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2013-01-07 07:07:27 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
|
2013-01-07 07:07:27 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2013-01-07 07:07:27 +00:00
|
|
|
// own
|
|
|
|
#include "client_machine.h"
|
2023-11-17 08:59:08 +00:00
|
|
|
#include "effect/xcb.h"
|
2022-02-08 22:22:38 +00:00
|
|
|
#include "main.h"
|
2022-01-18 08:35:52 +00:00
|
|
|
#include "utils/common.h"
|
2014-04-10 11:15:16 +00:00
|
|
|
// KF5
|
2015-02-17 12:59:04 +00:00
|
|
|
#include <NETWM>
|
2013-01-07 07:07:27 +00:00
|
|
|
// Qt
|
|
|
|
#include <QFutureWatcher>
|
2022-03-23 10:13:38 +00:00
|
|
|
#include <QtConcurrentRun>
|
2013-01-07 07:07:27 +00:00
|
|
|
// system
|
|
|
|
#include <netdb.h>
|
2022-03-23 10:13:38 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2013-01-07 07:07:27 +00:00
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
2013-01-07 07:07:27 +00:00
|
|
|
|
2022-10-21 15:24:38 +00:00
|
|
|
static QString getHostName()
|
2013-01-07 07:07:27 +00:00
|
|
|
{
|
|
|
|
#ifdef HOST_NAME_MAX
|
|
|
|
char hostnamebuf[HOST_NAME_MAX];
|
|
|
|
#else
|
|
|
|
char hostnamebuf[256];
|
|
|
|
#endif
|
|
|
|
if (gethostname(hostnamebuf, sizeof hostnamebuf) >= 0) {
|
2022-03-23 10:13:38 +00:00
|
|
|
hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
|
2022-10-21 15:24:38 +00:00
|
|
|
return QString::fromLocal8Bit(hostnamebuf);
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
2022-10-21 15:24:38 +00:00
|
|
|
return QString();
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 15:24:38 +00:00
|
|
|
GetAddrInfo::GetAddrInfo(const QString &hostName, QObject *parent)
|
2013-01-07 07:07:27 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, m_resolving(false)
|
|
|
|
, m_resolved(false)
|
|
|
|
, m_ownResolved(false)
|
|
|
|
, m_hostName(hostName)
|
2022-07-05 11:43:25 +00:00
|
|
|
, m_addressHints(std::make_unique<addrinfo>())
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
, m_address(nullptr)
|
|
|
|
, m_ownAddress(nullptr)
|
2022-07-05 11:43:25 +00:00
|
|
|
, m_watcher(std::make_unique<QFutureWatcher<int>>())
|
|
|
|
, m_ownAddressWatcher(std::make_unique<QFutureWatcher<int>>())
|
2013-01-07 07:07:27 +00:00
|
|
|
{
|
|
|
|
// watcher will be deleted together with the GetAddrInfo once the future
|
|
|
|
// got canceled or finished
|
2022-07-05 11:43:25 +00:00
|
|
|
connect(m_watcher.get(), &QFutureWatcher<int>::canceled, this, &GetAddrInfo::deleteLater);
|
|
|
|
connect(m_watcher.get(), &QFutureWatcher<int>::finished, this, &GetAddrInfo::slotResolved);
|
|
|
|
connect(m_ownAddressWatcher.get(), &QFutureWatcher<int>::canceled, this, &GetAddrInfo::deleteLater);
|
|
|
|
connect(m_ownAddressWatcher.get(), &QFutureWatcher<int>::finished, this, &GetAddrInfo::slotOwnAddressResolved);
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GetAddrInfo::~GetAddrInfo()
|
|
|
|
{
|
|
|
|
if (m_watcher && m_watcher->isRunning()) {
|
|
|
|
m_watcher->cancel();
|
2014-12-01 22:14:57 +00:00
|
|
|
m_watcher->waitForFinished();
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
if (m_ownAddressWatcher && m_ownAddressWatcher->isRunning()) {
|
|
|
|
m_ownAddressWatcher->cancel();
|
2014-12-01 22:14:57 +00:00
|
|
|
m_ownAddressWatcher->waitForFinished();
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
if (m_address) {
|
|
|
|
freeaddrinfo(m_address);
|
|
|
|
}
|
|
|
|
if (m_ownAddress) {
|
|
|
|
freeaddrinfo(m_ownAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAddrInfo::resolve()
|
|
|
|
{
|
|
|
|
if (m_resolving) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_resolving = true;
|
2022-07-05 11:43:25 +00:00
|
|
|
*m_addressHints = {};
|
2013-01-07 07:07:27 +00:00
|
|
|
m_addressHints->ai_family = PF_UNSPEC;
|
|
|
|
m_addressHints->ai_socktype = SOCK_STREAM;
|
|
|
|
m_addressHints->ai_flags |= AI_CANONNAME;
|
|
|
|
|
2022-10-21 15:24:38 +00:00
|
|
|
m_watcher->setFuture(QtConcurrent::run([this]() {
|
|
|
|
return getaddrinfo(m_hostName.toLocal8Bit().constData(), nullptr, m_addressHints.get(), &m_address);
|
|
|
|
}));
|
2015-09-30 07:51:31 +00:00
|
|
|
m_ownAddressWatcher->setFuture(QtConcurrent::run([this] {
|
|
|
|
// needs to be performed in a lambda as getHostName() returns a temporary value which would
|
|
|
|
// get destroyed in the main thread before the getaddrinfo thread is able to read it
|
2022-10-21 15:24:38 +00:00
|
|
|
return getaddrinfo(getHostName().toLocal8Bit().constData(), nullptr, m_addressHints.get(), &m_ownAddress);
|
2015-09-30 07:51:31 +00:00
|
|
|
}));
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetAddrInfo::slotResolved()
|
|
|
|
{
|
2022-07-05 11:43:25 +00:00
|
|
|
if (resolved(m_watcher.get())) {
|
2013-01-07 07:07:27 +00:00
|
|
|
m_resolved = true;
|
|
|
|
compare();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAddrInfo::slotOwnAddressResolved()
|
|
|
|
{
|
2022-07-05 11:43:25 +00:00
|
|
|
if (resolved(m_ownAddressWatcher.get())) {
|
2013-01-07 07:07:27 +00:00
|
|
|
m_ownResolved = true;
|
|
|
|
compare();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
bool GetAddrInfo::resolved(QFutureWatcher<int> *watcher)
|
2013-01-07 07:07:27 +00:00
|
|
|
{
|
|
|
|
if (!watcher->isFinished()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (watcher->result() != 0) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "getaddrinfo failed with error:" << gai_strerror(watcher->result());
|
2013-01-07 07:07:27 +00:00
|
|
|
// call failed;
|
|
|
|
deleteLater();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAddrInfo::compare()
|
|
|
|
{
|
|
|
|
if (!m_resolved || !m_ownResolved) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addrinfo *address = m_address;
|
|
|
|
while (address) {
|
|
|
|
if (address->ai_canonname && m_hostName == QByteArray(address->ai_canonname).toLower()) {
|
|
|
|
addrinfo *ownAddress = m_ownAddress;
|
|
|
|
bool localFound = false;
|
|
|
|
while (ownAddress) {
|
2022-03-23 10:13:38 +00:00
|
|
|
if (ownAddress->ai_canonname && QByteArray(ownAddress->ai_canonname).toLower() == m_hostName) {
|
2013-01-07 07:07:27 +00:00
|
|
|
localFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ownAddress = ownAddress->ai_next;
|
|
|
|
}
|
|
|
|
if (localFound) {
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT local();
|
2013-01-07 07:07:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
address = address->ai_next;
|
|
|
|
}
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientMachine::ClientMachine(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_localhost(false)
|
|
|
|
, m_resolved(false)
|
|
|
|
, m_resolving(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientMachine::~ClientMachine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientMachine::resolve(xcb_window_t window, xcb_window_t clientLeader)
|
|
|
|
{
|
|
|
|
if (m_resolved) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-21 15:24:38 +00:00
|
|
|
QString name = NETWinInfo(connection(), window, rootWindow(), NET::Properties(), NET::WM2ClientMachine).clientMachine();
|
2013-01-07 07:07:27 +00:00
|
|
|
if (name.isEmpty() && clientLeader && clientLeader != window) {
|
2015-02-17 12:59:04 +00:00
|
|
|
name = NETWinInfo(connection(), clientLeader, rootWindow(), NET::Properties(), NET::WM2ClientMachine).clientMachine();
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = localhost();
|
|
|
|
}
|
|
|
|
if (name == localhost()) {
|
|
|
|
setLocal();
|
|
|
|
}
|
|
|
|
m_hostName = name;
|
|
|
|
checkForLocalhost();
|
|
|
|
m_resolved = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientMachine::checkForLocalhost()
|
|
|
|
{
|
|
|
|
if (isLocal()) {
|
|
|
|
// nothing to do
|
|
|
|
return;
|
|
|
|
}
|
2022-10-21 15:24:38 +00:00
|
|
|
QString host = getHostName();
|
2013-01-07 07:07:27 +00:00
|
|
|
|
|
|
|
if (!host.isEmpty()) {
|
|
|
|
host = host.toLower();
|
2022-10-21 15:24:38 +00:00
|
|
|
const QString lowerHostName(m_hostName.toLower());
|
2013-01-07 07:07:27 +00:00
|
|
|
if (host == lowerHostName) {
|
|
|
|
setLocal();
|
|
|
|
return;
|
|
|
|
}
|
2022-10-21 15:24:38 +00:00
|
|
|
if (int index = host.indexOf('.'); index != -1) {
|
|
|
|
if (QStringView(host).left(index) == lowerHostName) {
|
2013-01-07 07:07:27 +00:00
|
|
|
setLocal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_resolving = true;
|
|
|
|
// check using information from get addr info
|
|
|
|
// GetAddrInfo gets automatically destroyed once it finished or not
|
|
|
|
GetAddrInfo *info = new GetAddrInfo(lowerHostName, this);
|
2020-09-23 18:39:59 +00:00
|
|
|
connect(info, &GetAddrInfo::local, this, &ClientMachine::setLocal);
|
|
|
|
connect(info, &GetAddrInfo::destroyed, this, &ClientMachine::resolveFinished);
|
2013-01-07 07:07:27 +00:00
|
|
|
info->resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientMachine::setLocal()
|
|
|
|
{
|
|
|
|
m_localhost = true;
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT localhostChanged();
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientMachine::resolveFinished()
|
|
|
|
{
|
|
|
|
m_resolving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2023-07-05 06:30:14 +00:00
|
|
|
|
|
|
|
#include "moc_client_machine.cpp"
|