Improved hostname resolution code

This commit is contained in:
Christophe Dumez 2010-10-20 17:19:18 +00:00
commit 105de3161a
2 changed files with 16 additions and 7 deletions

View file

@ -313,7 +313,8 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso
if(host.isNull()) { if(host.isNull()) {
resolver->resolve(peer.ip); resolver->resolve(peer.ip);
} else { } else {
peerItems.value(peer_ip)->setData(host); qDebug("Got peer IP from cache");
handleResolved(peer_ip, host);
} }
} }
} }
@ -394,6 +395,8 @@ void PeerListWidget::updatePeer(QString ip, peer_info peer) {
void PeerListWidget::handleResolved(QString ip, QString hostname) { void PeerListWidget::handleResolved(QString ip, QString hostname) {
QStandardItem *item = peerItems.value(ip, 0); QStandardItem *item = peerItems.value(ip, 0);
if(item) { if(item) {
qDebug("Resolved %s -> %s", qPrintable(ip), qPrintable(hostname));
item->setData(hostname); item->setData(hostname);
//listModel->setData(listModel->index(item->row(), IP), hostname);
} }
} }

View file

@ -83,10 +83,16 @@ signals:
protected: protected:
void run() { void run() {
try { try {
libtorrent::asio::ip::tcp::resolver::iterator it = resolver.resolve(ip); boost::system::error_code ec;
if(stopped) return; libtorrent::asio::ip::tcp::resolver::iterator it = resolver.resolve(ip, ec);
libtorrent::asio::ip::tcp::endpoint endpoint = *it; if(ec || stopped) return;
emit ip_resolved(misc::toQString(endpoint.address().to_string()), misc::toQString((*it).host_name())); const std::string ip_str = ip.address().to_string(ec);
if(ec) return;
const QString host_name = misc::toQString(it->host_name());
const QString ip_qstr = misc::toQString(ip_str);
if(host_name != ip_qstr) {
emit ip_resolved(ip_qstr, host_name);
}
} catch(std::exception/* &e*/) { } catch(std::exception/* &e*/) {
/*std::cerr << "Hostname resolution failed, reason: " << e.what() << std::endl;*/ /*std::cerr << "Hostname resolution failed, reason: " << e.what() << std::endl;*/
std::cerr << "Hostname resolution error." << std::endl; std::cerr << "Hostname resolution error." << std::endl;