1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2013 Martin Gräßlin <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
// own
#include "client_machine.h"
#include "utils.h"
// KF5
#include <NETWM>
// Qt
#include <QtConcurrentRun>
#include <QFutureWatcher>
// system
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
namespace KWin {
static QByteArray getHostName()
{
#ifdef HOST_NAME_MAX
char hostnamebuf[HOST_NAME_MAX];
#else
char hostnamebuf[256];
#endif
if (gethostname(hostnamebuf, sizeof hostnamebuf) >= 0) {
hostnamebuf[sizeof(hostnamebuf)-1] = 0;
return QByteArray(hostnamebuf);
}
return QByteArray();
}
GetAddrInfo::GetAddrInfo(const QByteArray &hostName, QObject *parent)
: QObject(parent)
, m_resolving(false)
, m_resolved(false)
, m_ownResolved(false)
, m_hostName(hostName)
, m_addressHints(new addrinfo)
, m_address(NULL)
, m_ownAddress(NULL)
, m_watcher(new QFutureWatcher<int>(this))
, m_ownAddressWatcher(new QFutureWatcher<int>(this))
{
// watcher will be deleted together with the GetAddrInfo once the future
// got canceled or finished
connect(m_watcher, SIGNAL(canceled()), SLOT(deleteLater()));
connect(m_watcher, SIGNAL(finished()), SLOT(slotResolved()));
connect(m_ownAddressWatcher, SIGNAL(canceled()), SLOT(deleteLater()));
connect(m_ownAddressWatcher, SIGNAL(finished()), SLOT(slotOwnAddressResolved()));
}
GetAddrInfo::~GetAddrInfo()
{
if (m_watcher && m_watcher->isRunning()) {
m_watcher->cancel();
m_watcher->waitForFinished();
}
if (m_ownAddressWatcher && m_ownAddressWatcher->isRunning()) {
m_ownAddressWatcher->cancel();
m_ownAddressWatcher->waitForFinished();
}
if (m_address) {
freeaddrinfo(m_address);
}
if (m_ownAddress) {
freeaddrinfo(m_ownAddress);
}
delete m_addressHints;
}
void GetAddrInfo::resolve()
{
if (m_resolving) {
return;
}
m_resolving = true;
memset(m_addressHints, 0, sizeof(*m_addressHints));
m_addressHints->ai_family = PF_UNSPEC;
m_addressHints->ai_socktype = SOCK_STREAM;
m_addressHints->ai_flags |= AI_CANONNAME;
m_watcher->setFuture(QtConcurrent::run(getaddrinfo, m_hostName.constData(), nullptr, m_addressHints, &m_address));
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
return getaddrinfo(getHostName().constData(), nullptr, m_addressHints, &m_ownAddress);
}));
}
void GetAddrInfo::slotResolved()
{
if (resolved(m_watcher)) {
m_resolved = true;
compare();
}
}
void GetAddrInfo::slotOwnAddressResolved()
{
if (resolved(m_ownAddressWatcher)) {
m_ownResolved = true;
compare();
}
}
bool GetAddrInfo::resolved(QFutureWatcher< int >* watcher)
{
if (!watcher->isFinished()) {
return false;
}
if (watcher->result() != 0) {
qCDebug(KWIN_CORE) << "getaddrinfo failed with error:" << gai_strerror(watcher->result());
// 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) {
if (ownAddress->ai_canonname && QByteArray(ownAddress->ai_canonname).toLower() == m_hostName) {
localFound = true;
break;
}
ownAddress = ownAddress->ai_next;
}
if (localFound) {
emit local();
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;
}
QByteArray name = NETWinInfo(connection(), window, rootWindow(), NET::Properties(), NET::WM2ClientMachine).clientMachine();
if (name.isEmpty() && clientLeader && clientLeader != window) {
name = NETWinInfo(connection(), clientLeader, rootWindow(), NET::Properties(), NET::WM2ClientMachine).clientMachine();
}
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;
}
QByteArray host = getHostName();
if (!host.isEmpty()) {
host = host.toLower();
const QByteArray lowerHostName(m_hostName.toLower());
if (host == lowerHostName) {
setLocal();
return;
}
if (char *dot = strchr(host.data(), '.')) {
*dot = '\0';
if (host == lowerHostName) {
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);
connect(info, SIGNAL(local()), SLOT(setLocal()));
connect(info, SIGNAL(destroyed(QObject*)), SLOT(resolveFinished()));
info->resolve();
}
}
}
void ClientMachine::setLocal()
{
m_localhost = true;
emit localhostChanged();
}
void ClientMachine::resolveFinished()
{
m_resolving = false;
}
} // namespace
|