2009-11-14 16:33:55 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef REVERSERESOLUTION_H
|
|
|
|
#define REVERSERESOLUTION_H
|
|
|
|
|
|
|
|
#include <QList>
|
2010-10-11 01:03:45 +04:00
|
|
|
#include <QCache>
|
2011-01-22 21:55:54 +03:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QHostInfo>
|
2009-11-14 16:33:55 +03:00
|
|
|
#include "misc.h"
|
|
|
|
|
2009-11-29 01:22:35 +03:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
#if BOOST_VERSION < 103500
|
|
|
|
#include <libtorrent/asio/ip/tcp.hpp>
|
|
|
|
#else
|
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
|
|
#endif
|
|
|
|
|
2010-10-11 01:03:45 +04:00
|
|
|
const int CACHE_SIZE = 500;
|
2009-11-14 16:33:55 +03:00
|
|
|
|
2011-01-22 21:55:54 +03:00
|
|
|
class ReverseResolution: public QObject {
|
2009-11-14 16:33:55 +03:00
|
|
|
Q_OBJECT
|
2010-10-11 01:03:45 +04:00
|
|
|
Q_DISABLE_COPY(ReverseResolution)
|
2009-11-14 16:33:55 +03:00
|
|
|
|
|
|
|
public:
|
2011-01-22 21:55:54 +03:00
|
|
|
explicit ReverseResolution(QObject* parent): QObject(parent) {
|
|
|
|
m_cache.setMaxCost(CACHE_SIZE);
|
2009-11-14 16:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~ReverseResolution() {
|
2009-11-15 13:17:21 +03:00
|
|
|
qDebug("Deleting host name resolver...");
|
|
|
|
}
|
|
|
|
|
2013-03-28 23:47:45 +04:00
|
|
|
void resolve(const QString &ip) {
|
|
|
|
if (m_cache.contains(ip)) {
|
|
|
|
const QString& hostname = *m_cache.object(ip);
|
|
|
|
qDebug() << "Resolved host name using cache: " << ip << " -> " << hostname;
|
|
|
|
if (isUsefulHostName(hostname, ip))
|
|
|
|
emit ip_resolved(ip, hostname);
|
2010-10-11 01:03:45 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-01-22 21:55:54 +03:00
|
|
|
// Actually resolve the ip
|
2013-03-28 23:47:45 +04:00
|
|
|
m_lookups.insert(QHostInfo::lookupHost(ip, this, SLOT(hostResolved(QHostInfo))), ip);
|
2009-11-14 16:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
2011-01-22 21:55:54 +03:00
|
|
|
void ip_resolved(const QString &ip, const QString &hostname);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void hostResolved(const QHostInfo& host) {
|
2013-03-28 23:47:45 +04:00
|
|
|
const QString& ip = m_lookups.take(host.lookupId());
|
|
|
|
Q_ASSERT(!ip.isNull());
|
|
|
|
|
|
|
|
if (host.error() != QHostInfo::NoError) {
|
|
|
|
qDebug() << "DNS Reverse resolution error: " << host.errorString();
|
|
|
|
return;
|
2011-01-22 21:55:54 +03:00
|
|
|
}
|
2013-03-28 23:47:45 +04:00
|
|
|
|
|
|
|
const QString& hostname = host.hostName();
|
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << ip << QString("->") << hostname;
|
|
|
|
m_cache.insert(ip, new QString(hostname));
|
|
|
|
if (isUsefulHostName(hostname, ip))
|
|
|
|
emit ip_resolved(ip, hostname);
|
2009-11-14 16:33:55 +03:00
|
|
|
}
|
2010-10-11 01:03:45 +04:00
|
|
|
|
|
|
|
private:
|
2013-03-28 23:47:45 +04:00
|
|
|
static inline bool isUsefulHostName(const QString& hostname, const QString& ip) {
|
|
|
|
return (!hostname.isEmpty() && hostname != ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int /* LookupID */, QString /* IP */> m_lookups;
|
|
|
|
QCache<QString /* IP */, QString /* HostName */> m_cache;
|
2009-11-14 16:33:55 +03:00
|
|
|
};
|
|
|
|
|
2010-10-11 01:03:45 +04:00
|
|
|
|
2009-11-14 16:33:55 +03:00
|
|
|
#endif // REVERSERESOLUTION_H
|