File indexing completed on 2025-01-05 04:37:19

0001 /*
0002     SPDX-FileCopyrightText: 2011 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "addressresolver.h"
0008 
0009 namespace net
0010 {
0011 AddressResolver::AddressResolver(const QString &host, bt::Uint16 port, QObject *parent, const char *slot)
0012     : QObject(parent)
0013     , lookup_id(-1)
0014     , succesfull(false)
0015 {
0016     result.setPort(port);
0017     lookup_id = QHostInfo::lookupHost(host, this, SLOT(hostResolved(QHostInfo)));
0018     ongoing = true;
0019     connect(this, SIGNAL(resolved(net::AddressResolver *)), parent, slot);
0020 }
0021 
0022 AddressResolver::~AddressResolver()
0023 {
0024     if (ongoing)
0025         QHostInfo::abortHostLookup(lookup_id);
0026 }
0027 
0028 void AddressResolver::hostResolved(const QHostInfo &res)
0029 {
0030     ongoing = false;
0031     succesfull = res.error() == QHostInfo::NoError && res.addresses().count() > 0;
0032     if (!succesfull) {
0033         resolved(this);
0034     } else {
0035         result = net::Address(res.addresses().first(), result.port());
0036         resolved(this);
0037     }
0038 
0039     deleteLater();
0040 }
0041 
0042 void AddressResolver::resolve(const QString &host, Uint16 port, QObject *parent, const char *slot)
0043 {
0044     new AddressResolver(host, port, parent, slot);
0045 }
0046 
0047 Address AddressResolver::resolve(const QString &host, Uint16 port)
0048 {
0049     QHostInfo info = QHostInfo::fromName(host);
0050     if (info.error() == QHostInfo::NoError && info.addresses().size() > 0)
0051         return net::Address(info.addresses().first(), port);
0052     else
0053         return net::Address();
0054 }
0055 
0056 }
0057 
0058 #include "moc_addressresolver.cpp"