1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-15 17:20:06 +00:00
qBittorrent/src/base/net/reverseresolution.cpp

89 lines
2.9 KiB
C++
Raw Normal View History

/*
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>
*
* 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"
2018-04-14 22:53:45 +03:00
#include <QDebug>
#include <QHostInfo>
#include <QString>
2015-04-13 19:02:48 +03:00
const int CACHE_SIZE = 500;
2015-04-13 19:02:48 +03:00
using namespace Net;
namespace
2015-04-13 19:02:48 +03:00
{
bool isUsefulHostName(const QString &hostname, const QString &ip)
{
return (!hostname.isEmpty() && (hostname != ip));
}
2015-04-13 19:02:48 +03: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
}
2015-04-13 19:02:48 +03:00
ReverseResolution::~ReverseResolution()
{
qDebug("Deleting host name resolver...");
2015-04-13 19:02:48 +03:00
}
2015-04-13 19:02:48 +03:00
void ReverseResolution::resolve(const QString &ip)
{
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);
}
2015-04-13 19:02:48 +03:00
else {
// Actually resolve the ip
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)
{
const QString ip = m_lookups.take(host.lookupId());
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
}
const QString hostname = host.hostName();
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);
}