2009-11-14 13:33:55 +00:00
|
|
|
/*
|
2015-04-13 19:02:48 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2018-04-14 22:53:45 +03:00
|
|
|
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
2009-11-14 13:33:55 +00:00
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2018-04-14 22:53:45 +03:00
|
|
|
#include "reverseresolution.h"
|
2009-11-14 13:33:55 +00:00
|
|
|
|
2018-04-14 22:53:45 +03:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QHostInfo>
|
|
|
|
#include <QString>
|
2015-04-13 19:02:48 +03:00
|
|
|
|
2010-10-10 21:03:45 +00:00
|
|
|
const int CACHE_SIZE = 500;
|
2009-11-14 13:33:55 +00:00
|
|
|
|
2015-04-13 19:02:48 +03:00
|
|
|
using namespace Net;
|
|
|
|
|
2019-07-14 17:22:17 +08:00
|
|
|
namespace
|
2015-04-13 19:02:48 +03:00
|
|
|
{
|
2019-07-14 17:22:17 +08:00
|
|
|
bool isUsefulHostName(const QString &hostname, const QString &ip)
|
|
|
|
{
|
|
|
|
return (!hostname.isEmpty() && (hostname != ip));
|
|
|
|
}
|
2015-04-13 19:02:48 +03:00
|
|
|
}
|
2009-11-14 13:33:55 +00:00
|
|
|
|
2015-04-13 19:02:48 +03:00
|
|
|
ReverseResolution::ReverseResolution(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2011-01-22 18:55:54 +00:00
|
|
|
m_cache.setMaxCost(CACHE_SIZE);
|
2015-04-13 19:02:48 +03:00
|
|
|
}
|
2009-11-14 13:33:55 +00:00
|
|
|
|
2015-04-13 19:02:48 +03:00
|
|
|
ReverseResolution::~ReverseResolution()
|
|
|
|
{
|
2009-11-15 10:17:21 +00:00
|
|
|
qDebug("Deleting host name resolver...");
|
2015-04-13 19:02:48 +03:00
|
|
|
}
|
2009-11-15 10:17:21 +00:00
|
|
|
|
2015-04-13 19:02:48 +03:00
|
|
|
void ReverseResolution::resolve(const QString &ip)
|
|
|
|
{
|
2013-03-28 21:47:45 +02:00
|
|
|
if (m_cache.contains(ip)) {
|
2015-04-13 19:02:48 +03:00
|
|
|
const QString &hostname = *m_cache.object(ip);
|
|
|
|
qDebug() << "Resolved host name using cache: " << ip << " -> " << hostname;
|
|
|
|
if (isUsefulHostName(hostname, ip))
|
|
|
|
emit ipResolved(ip, hostname);
|
2010-10-10 21:03:45 +00:00
|
|
|
}
|
2015-04-13 19:02:48 +03:00
|
|
|
else {
|
|
|
|
// Actually resolve the ip
|
2018-05-10 20:42:19 +03:00
|
|
|
m_lookups.insert(QHostInfo::lookupHost(ip, this, &ReverseResolution::hostResolved), ip);
|
2015-04-13 19:02:48 +03:00
|
|
|
}
|
|
|
|
}
|
2011-01-22 18:55:54 +00:00
|
|
|
|
2015-04-13 19:02:48 +03:00
|
|
|
void ReverseResolution::hostResolved(const QHostInfo &host)
|
|
|
|
{
|
2018-07-21 15:07:11 +08:00
|
|
|
const QString ip = m_lookups.take(host.lookupId());
|
2013-03-28 21:47:45 +02:00
|
|
|
Q_ASSERT(!ip.isNull());
|
|
|
|
|
|
|
|
if (host.error() != QHostInfo::NoError) {
|
2015-04-13 19:02:48 +03:00
|
|
|
qDebug() << "DNS Reverse resolution error: " << host.errorString();
|
|
|
|
return;
|
2011-01-22 18:55:54 +00:00
|
|
|
}
|
2013-03-28 21:47:45 +02:00
|
|
|
|
2018-07-21 15:07:11 +08:00
|
|
|
const QString hostname = host.hostName();
|
2013-03-28 21:47:45 +02:00
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << ip << QString("->") << hostname;
|
|
|
|
m_cache.insert(ip, new QString(hostname));
|
|
|
|
if (isUsefulHostName(hostname, ip))
|
2015-04-13 19:02:48 +03:00
|
|
|
emit ipResolved(ip, hostname);
|
|
|
|
}
|