initial commit

This commit is contained in:
mikhail "synzr" 2025-12-25 01:37:49 +05:00
commit 9d20827c46
2469 changed files with 470994 additions and 0 deletions

View file

@ -0,0 +1,393 @@
/*
* ndns.cpp - native DNS resolution
* Copyright (C) 2001, 2002 Justin Karneges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
//! \class NDns ndns.h
//! \brief Simple DNS resolution using native system calls
//!
//! This class is to be used when Qt's QDns is not good enough. Because QDns
//! does not use threads, it cannot make a system call asyncronously. Thus,
//! QDns tries to imitate the behavior of each platform's native behavior, and
//! generally falls short.
//!
//! NDns uses a thread to make the system call happen in the background. This
//! gives your program native DNS behavior, at the cost of requiring threads
//! to build.
//!
//! \code
//! #include "ndns.h"
//!
//! ...
//!
//! NDns dns;
//! dns.resolve("psi.affinix.com");
//!
//! // The class will emit the resultsReady() signal when the resolution
//! // is finished. You may then retrieve the results:
//!
//! QHostAddress ip_address = dns.result();
//!
//! // or if you want to get the IP address as a string:
//!
//! QString ip_address = dns.resultString();
//! \endcode
#include "ndns.h"
#include <QCoreApplication>
#include <q3socketdevice.h>
#include <q3ptrlist.h>
#include <qeventloop.h>
//Added by qt3to4:
#include <QCustomEvent>
#include <QEvent>
#include <Q3CString>
#include <QPointer>
#ifdef Q_OS_UNIX
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#ifdef Q_OS_WIN32
#include <windows.h>
#endif
#include "psilogger.h"
// CS_NAMESPACE_BEGIN
//! \if _hide_doc_
class NDnsWorker : public QThread
{
public:
NDnsWorker(QObject *, const Q3CString &);
~NDnsWorker();
bool success;
bool cancelled;
QHostAddress addr;
protected:
void run();
private:
Q3CString host;
};
//! \endif
//----------------------------------------------------------------------------
// NDnsManager
//----------------------------------------------------------------------------
#ifndef HAVE_GETHOSTBYNAME_R
#ifndef Q_WS_WIN
static QMutex *workerMutex = 0;
static QMutex *workerCancelled = 0;
#endif
#endif
static NDnsManager *manager_instance = 0;
bool winsock_init = false;
class NDnsManager::Item
{
public:
NDns *ndns;
NDnsWorker *worker;
};
class NDnsManager::Private
{
public:
Item *find(const NDns *n)
{
Q3PtrListIterator<Item> it(list);
for(Item *i; (i = it.current()); ++it) {
if(i->ndns == n)
return i;
}
return 0;
}
Item *find(const NDnsWorker *w)
{
Q3PtrListIterator<Item> it(list);
for(Item *i; (i = it.current()); ++it) {
if(i->worker == w)
return i;
}
return 0;
}
Q3PtrList<Item> list;
};
NDnsManager::NDnsManager()
: QObject(QCoreApplication::instance())
{
#ifndef HAVE_GETHOSTBYNAME_R
#ifndef Q_WS_WIN
workerMutex = new QMutex;
workerCancelled = new QMutex;
#endif
#endif
#ifdef Q_OS_WIN32
if(!winsock_init) {
winsock_init = true;
Q3SocketDevice *sd = new Q3SocketDevice;
delete sd;
}
#endif
d = new Private;
d->list.setAutoDelete(true);
connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), SLOT(app_aboutToQuit()));
}
NDnsManager::~NDnsManager()
{
delete d;
#ifndef HAVE_GETHOSTBYNAME_R
#ifndef Q_WS_WIN
delete workerMutex;
workerMutex = 0;
delete workerCancelled;
workerCancelled = 0;
#endif
#endif
}
void NDnsManager::resolve(NDns *self, const QString &name)
{
Item *i = new Item;
i->ndns = self;
i->worker = new NDnsWorker(this, name.utf8());
connect(i->worker, SIGNAL(finished()), SLOT(workerFinished()));
d->list.append(i);
i->worker->start();
}
void NDnsManager::stop(NDns *self)
{
Item *i = d->find(self);
if(!i)
return;
// disassociate
i->ndns = 0;
#ifndef HAVE_GETHOSTBYNAME_R
#ifndef Q_WS_WIN
// cancel
workerCancelled->lock();
i->worker->cancelled = true;
workerCancelled->unlock();
#endif
#endif
d->list.removeRef(i);
}
bool NDnsManager::isBusy(const NDns *self) const
{
Item *i = d->find(self);
return (i ? true: false);
}
void NDnsManager::workerFinished()
{
NDnsWorker* worker = dynamic_cast<NDnsWorker*>(sender());
Q_ASSERT(worker);
if (!worker)
return;
worker->wait(); // ensure that the thread is terminated
Item *i = d->find(worker);
if(i) {
QHostAddress addr = i->worker->addr;
QPointer<NDns> ndns = i->ndns;
d->list.removeRef(i);
// nuke manager if no longer needed (code that follows MUST BE SAFE!)
tryDestroy();
// requestor still around?
if(ndns) {
ndns->finished(addr);
}
}
worker->deleteLater();
}
void NDnsManager::tryDestroy()
{
// mblsha: NDnsManager is now singleton
#if 0
if(d->list.isEmpty()) {
manager_instance = 0;
deleteLater();
}
#endif
}
void NDnsManager::app_aboutToQuit()
{
// mblsha: NDnsManager is now singleton
#if 0
while(man) {
QCoreApplication::instance()->processEvents(QEventLoop::WaitForMoreEvents);
}
#endif
}
//----------------------------------------------------------------------------
// NDns
//----------------------------------------------------------------------------
//! \fn void NDns::resultsReady()
//! This signal is emitted when the DNS resolution succeeds or fails.
//!
//! Constructs an NDns object with parent \a parent.
NDns::NDns(QObject *parent)
:QObject(parent)
{
}
//!
//! Destroys the object and frees allocated resources.
NDns::~NDns()
{
stop();
PsiLogger::instance()->log(QString("%1 NDns::~NDns()").arg(LOG_THIS));
}
//!
//! Resolves hostname \a host (eg. psi.affinix.com)
void NDns::resolve(const QString &host)
{
PsiLogger::instance()->log(QString("%1 NDns::resolve(%2)").arg(LOG_THIS).arg(host));
stop();
if(!manager_instance)
manager_instance = new NDnsManager;
manager_instance->resolve(this, host);
}
//!
//! Cancels the lookup action.
//! \note This will not stop the underlying system call, which must finish before the next lookup will proceed.
void NDns::stop()
{
PsiLogger::instance()->log(QString("%1 NDns::stop()").arg(LOG_THIS));
if(manager_instance)
manager_instance->stop(this);
}
//!
//! Returns the IP address as QHostAddress. This will be a Null QHostAddress if the lookup failed.
//! \sa resultsReady()
QHostAddress NDns::result() const
{
return addr;
}
//!
//! Returns the IP address as a string. This will be an empty string if the lookup failed.
//! \sa resultsReady()
QString NDns::resultString() const
{
if (addr.isNull())
return QString();
else
return addr.toString();
}
//!
//! Returns TRUE if busy resolving a hostname.
bool NDns::isBusy() const
{
if(!manager_instance)
return false;
return manager_instance->isBusy(this);
}
void NDns::finished(const QHostAddress &a)
{
PsiLogger::instance()->log(QString("%1 NDns::finished(%2)").arg(LOG_THIS).arg(a.toString()));
addr = a;
resultsReady();
}
//----------------------------------------------------------------------------
// NDnsWorker
//----------------------------------------------------------------------------
NDnsWorker::NDnsWorker(QObject *_par, const Q3CString &_host)
: QThread(_par)
{
success = cancelled = false;
host = _host.copy(); // do we need this to avoid sharing across threads?
}
NDnsWorker::~NDnsWorker()
{
}
void NDnsWorker::run()
{
hostent *h = 0;
#ifdef HAVE_GETHOSTBYNAME_R
hostent buf;
char char_buf[1024];
int err;
gethostbyname_r(host.data(), &buf, char_buf, sizeof(char_buf), &h, &err);
#else
#ifndef Q_WS_WIN
// lock for gethostbyname
QMutexLocker locker(workerMutex);
// check for cancel
workerCancelled->lock();
bool cancel = cancelled;
workerCancelled->unlock();
if(!cancel)
#endif
h = gethostbyname(host.data());
#endif
// FIXME: not ipv6 clean, currently.
if(!h || h->h_addrtype != AF_INET) {
success = false;
return;
}
in_addr a = *((struct in_addr *)h->h_addr_list[0]);
addr.setAddress(ntohl(a.s_addr));
success = true;
}
// CS_NAMESPACE_END

View file

@ -0,0 +1,90 @@
/*
* ndns.h - native DNS resolution
* Copyright (C) 2001, 2002 Justin Karneges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef CS_NDNS_H
#define CS_NDNS_H
#include <qobject.h>
#include <q3cstring.h>
#include <qthread.h>
#include <qmutex.h>
#include <qhostaddress.h>
//Added by qt3to4:
#include <QEvent>
// CS_NAMESPACE_BEGIN
class NDnsWorker;
class NDnsManager;
class NDns : public QObject
{
Q_OBJECT
public:
NDns(QObject *parent=0);
~NDns();
void resolve(const QString &);
void stop();
bool isBusy() const;
QHostAddress result() const;
QString resultString() const;
signals:
void resultsReady();
private:
QHostAddress addr;
friend class NDnsManager;
void finished(const QHostAddress &);
};
class NDnsManager : public QObject
{
Q_OBJECT
public:
~NDnsManager();
class Item;
//! \if _hide_doc_
protected slots:
void workerFinished();
//! \endif
private slots:
void app_aboutToQuit();
private:
class Private;
Private *d;
friend class NDns;
NDnsManager();
void resolve(NDns *self, const QString &name);
void stop(NDns *self);
bool isBusy(const NDns *self) const;
void tryDestroy();
};
// CS_NAMESPACE_END
#endif

View file

@ -0,0 +1,111 @@
#include "safedelete.h"
#include <qtimer.h>
//----------------------------------------------------------------------------
// SafeDelete
//----------------------------------------------------------------------------
SafeDelete::SafeDelete()
{
lock = 0;
}
SafeDelete::~SafeDelete()
{
if(lock)
lock->dying();
}
void SafeDelete::deleteLater(QObject *o)
{
if(!lock)
deleteSingle(o);
else
list.append(o);
}
void SafeDelete::unlock()
{
lock = 0;
deleteAll();
}
void SafeDelete::deleteAll()
{
if(list.isEmpty())
return;
QObjectList::Iterator it = list.begin();
for(QObjectList::Iterator it = list.begin(); it != list.end(); ++it)
deleteSingle(*it);
list.clear();
}
void SafeDelete::deleteSingle(QObject *o)
{
o->deleteLater();
}
//----------------------------------------------------------------------------
// SafeDeleteLock
//----------------------------------------------------------------------------
SafeDeleteLock::SafeDeleteLock(SafeDelete *sd)
{
own = false;
if(!sd->lock) {
_sd = sd;
_sd->lock = this;
}
else
_sd = 0;
}
SafeDeleteLock::~SafeDeleteLock()
{
if(_sd) {
_sd->unlock();
if(own)
delete _sd;
}
}
void SafeDeleteLock::dying()
{
_sd = new SafeDelete(*_sd);
own = true;
}
//----------------------------------------------------------------------------
// SafeDeleteLater
//----------------------------------------------------------------------------
SafeDeleteLater *SafeDeleteLater::self = 0;
SafeDeleteLater *SafeDeleteLater::ensureExists()
{
if(!self)
new SafeDeleteLater();
return self;
}
SafeDeleteLater::SafeDeleteLater()
{
self = this;
QTimer::singleShot(0, this, SLOT(explode()));
}
SafeDeleteLater::~SafeDeleteLater()
{
while (!list.isEmpty())
delete list.takeFirst();
self = 0;
}
void SafeDeleteLater::deleteItLater(QObject *o)
{
list.append(o);
}
void SafeDeleteLater::explode()
{
delete this;
}

View file

@ -0,0 +1,60 @@
#ifndef SAFEDELETE_H
#define SAFEDELETE_H
#include <qobject.h>
#include <qobject.h>
class SafeDelete;
class SafeDeleteLock
{
public:
SafeDeleteLock(SafeDelete *sd);
~SafeDeleteLock();
private:
SafeDelete *_sd;
bool own;
friend class SafeDelete;
void dying();
};
class SafeDelete
{
public:
SafeDelete();
~SafeDelete();
void deleteLater(QObject *o);
// same as QObject::deleteLater()
static void deleteSingle(QObject *o);
private:
QObjectList list;
void deleteAll();
friend class SafeDeleteLock;
SafeDeleteLock *lock;
void unlock();
};
class SafeDeleteLater : public QObject
{
Q_OBJECT
public:
static SafeDeleteLater *ensureExists();
void deleteItLater(QObject *o);
private slots:
void explode();
private:
SafeDeleteLater();
~SafeDeleteLater();
QObjectList list;
friend class SafeDelete;
static SafeDeleteLater *self;
};
#endif

View file

@ -0,0 +1,111 @@
/*
* servsock.cpp - simple wrapper to QServerSocket
* Copyright (C) 2003 Justin Karneges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "servsock.h"
// CS_NAMESPACE_BEGIN
//----------------------------------------------------------------------------
// ServSock
//----------------------------------------------------------------------------
class ServSock::Private
{
public:
Private() {}
ServSockSignal *serv;
};
ServSock::ServSock(QObject *parent)
:QObject(parent)
{
d = new Private;
d->serv = 0;
}
ServSock::~ServSock()
{
stop();
delete d;
}
bool ServSock::isActive() const
{
return (d->serv ? true: false);
}
bool ServSock::listen(quint16 port)
{
stop();
d->serv = new ServSockSignal(this);
if(!d->serv->listen(QHostAddress::Any, port)) {
delete d->serv;
d->serv = 0;
return false;
}
connect(d->serv, SIGNAL(connectionReady(int)), SLOT(sss_connectionReady(int)));
return true;
}
void ServSock::stop()
{
delete d->serv;
d->serv = 0;
}
int ServSock::port() const
{
if(d->serv)
return d->serv->serverPort();
else
return -1;
}
QHostAddress ServSock::address() const
{
if(d->serv)
return d->serv->serverAddress();
else
return QHostAddress();
}
void ServSock::sss_connectionReady(int s)
{
connectionReady(s);
}
//----------------------------------------------------------------------------
// ServSockSignal
//----------------------------------------------------------------------------
ServSockSignal::ServSockSignal(QObject *parent)
:QTcpServer(parent)
{
setMaxPendingConnections(16);
}
void ServSockSignal::incomingConnection(int socketDescriptor)
{
connectionReady(socketDescriptor);
}
// CS_NAMESPACE_END

View file

@ -0,0 +1,68 @@
/*
* servsock.h - simple wrapper to QServerSocket
* Copyright (C) 2003 Justin Karneges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef CS_SERVSOCK_H
#define CS_SERVSOCK_H
#include <QTcpServer>
// CS_NAMESPACE_BEGIN
class ServSock : public QObject
{
Q_OBJECT
public:
ServSock(QObject *parent=0);
~ServSock();
bool isActive() const;
bool listen(quint16 port);
void stop();
int port() const;
QHostAddress address() const;
signals:
void connectionReady(int);
private slots:
void sss_connectionReady(int);
private:
class Private;
Private *d;
};
class ServSockSignal : public QTcpServer
{
Q_OBJECT
public:
ServSockSignal(QObject *parent = 0);
signals:
void connectionReady(int);
protected:
// reimplemented
void incomingConnection(int socketDescriptor);
};
// CS_NAMESPACE_END
#endif

View file

@ -0,0 +1,313 @@
/*
* srvresolver.cpp - class to simplify SRV lookups
* Copyright (C) 2003 Justin Karneges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "srvresolver.h"
#include <q3cstring.h>
#include <qtimer.h>
#include <q3dns.h>
//Added by qt3to4:
#include <QList>
#include <QtAlgorithms>
#include "safedelete.h"
#include "psilogger.h"
#ifndef NO_NDNS
#include "ndns.h"
#endif
// CS_NAMESPACE_BEGIN
bool serverLessThan(const Q3Dns::Server &s1, const Q3Dns::Server &s2)
{
int a = s1.priority;
int b = s2.priority;
int j = s1.weight;
int k = s2.weight;
return a < b || (a == b && j < k);
}
static void sortSRVList(QList<Q3Dns::Server> &list)
{
qStableSort(list.begin(), list.end(), serverLessThan);
}
class SrvResolver::Private
{
public:
Private() {}
Q3Dns *qdns;
#ifndef NO_NDNS
NDns ndns;
#endif
bool failed;
QHostAddress resultAddress;
Q_UINT16 resultPort;
bool srvonly;
QString srv;
QList<Q3Dns::Server> servers;
bool aaaa;
QTimer t;
SafeDelete sd;
};
SrvResolver::SrvResolver(QObject *parent)
:QObject(parent)
{
d = new Private;
d->qdns = 0;
#ifndef NO_NDNS
connect(&d->ndns, SIGNAL(resultsReady()), SLOT(ndns_done()));
#endif
connect(&d->t, SIGNAL(timeout()), SLOT(t_timeout()));
stop();
}
SrvResolver::~SrvResolver()
{
stop();
delete d;
}
void SrvResolver::resolve(const QString &server, const QString &type, const QString &proto, bool srvOnly)
{
PsiLogger::instance()->log(QString("SrvResolver::resolve(%1, %2, %3, %4)").arg(server).arg(type).arg(proto).arg(srvOnly));
stop();
d->failed = false;
d->srvonly = srvOnly;
d->srv = QString("_") + type + "._" + proto + '.' + server;
d->t.start(15000, true);
d->qdns = new Q3Dns;
connect(d->qdns, SIGNAL(resultsReady()), SLOT(qdns_done()));
d->qdns->setRecordType(Q3Dns::Srv);
d->qdns->setLabel(d->srv);
}
void SrvResolver::resolve(const QString &server, const QString &type, const QString &proto)
{
resolve(server, type, proto, false);
}
void SrvResolver::resolveSrvOnly(const QString &server, const QString &type, const QString &proto)
{
resolve(server, type, proto, true);
}
void SrvResolver::next()
{
if(d->servers.isEmpty())
return;
tryNext();
}
void SrvResolver::stop()
{
if(d->t.isActive())
d->t.stop();
if(d->qdns) {
d->qdns->disconnect(this);
d->sd.deleteLater(d->qdns);
d->qdns = 0;
}
#ifndef NO_NDNS
if(d->ndns.isBusy())
d->ndns.stop();
#endif
d->resultAddress = QHostAddress();
d->resultPort = 0;
d->servers.clear();
d->srv = "";
d->failed = true;
}
bool SrvResolver::isBusy() const
{
#ifndef NO_NDNS
if(d->qdns || d->ndns.isBusy())
#else
if(d->qdns)
#endif
return true;
else
return false;
}
QList<Q3Dns::Server> SrvResolver::servers() const
{
return d->servers;
}
bool SrvResolver::failed() const
{
return d->failed;
}
QHostAddress SrvResolver::resultAddress() const
{
return d->resultAddress;
}
Q_UINT16 SrvResolver::resultPort() const
{
return d->resultPort;
}
void SrvResolver::tryNext()
{
#ifndef NO_NDNS
PsiLogger::instance()->log(QString("SrvResolver(%1)::tryNext() d->ndns.resolve(%2)").arg(d->srv).arg(d->servers.first().name));
d->ndns.resolve(d->servers.first().name);
#else
d->qdns = new Q3Dns;
connect(d->qdns, SIGNAL(resultsReady()), SLOT(ndns_done()));
if(d->aaaa)
d->qdns->setRecordType(Q3Dns::Aaaa); // IPv6
else
d->qdns->setRecordType(Q3Dns::A); // IPv4
d->qdns->setLabel(d->servers.first().name);
#endif
}
void SrvResolver::qdns_done()
{
PsiLogger::instance()->log(QString("SrvResolver(%1)::qdns_done() d->qdns = %2; d->qdns->isWorking() = %3; d->qdns->servers().count() = %4").arg(d->srv).arg((long)d->qdns).arg(d->qdns ? d->qdns->isWorking() : 0).arg(d->qdns ? d->qdns->servers().count() : 0));
if(!d->qdns)
return;
// apparently we sometimes get this signal even though the results aren't ready
if(d->qdns->isWorking())
return;
d->t.stop();
SafeDeleteLock s(&d->sd);
// grab the server list and destroy the qdns object
QList<Q3Dns::Server> list;
if(d->qdns->recordType() == Q3Dns::Srv)
list = d->qdns->servers();
d->qdns->disconnect(this);
d->sd.deleteLater(d->qdns);
d->qdns = 0;
if(list.isEmpty()) {
stop();
resultsReady();
return;
}
sortSRVList(list);
d->servers = list;
if(d->srvonly)
resultsReady();
else {
// kick it off
d->aaaa = true;
tryNext();
}
}
void SrvResolver::ndns_done()
{
#ifndef NO_NDNS
SafeDeleteLock s(&d->sd);
QHostAddress r = d->ndns.result();
int port = d->servers.first().port;
d->servers.remove(d->servers.begin());
PsiLogger::instance()->log(QString("SrvResolver(%1)::ndns_done() r.isNull = %2, r = %3, port = %4").arg(d->srv).arg(r.isNull()).arg(r.toString()).arg(port));
if(!r.isNull()) {
d->resultAddress = r;
d->resultPort = port;
resultsReady();
}
else {
// failed? bail if last one
if(d->servers.isEmpty()) {
stop();
resultsReady();
return;
}
// otherwise try the next
tryNext();
}
#else
if(!d->qdns)
return;
// apparently we sometimes get this signal even though the results aren't ready
if(d->qdns->isWorking())
return;
SafeDeleteLock s(&d->sd);
// grab the address list and destroy the qdns object
QList<QHostAddress> list;
if(d->qdns->recordType() == Q3Dns::A || d->qdns->recordType() == Q3Dns::Aaaa)
list = d->qdns->addresses();
d->qdns->disconnect(this);
d->sd.deleteLater(d->qdns);
d->qdns = 0;
if(!list.isEmpty()) {
int port = d->servers.first().port;
d->servers.remove(d->servers.begin());
d->aaaa = true;
d->resultAddress = list.first();
d->resultPort = port;
resultsReady();
}
else {
if(!d->aaaa)
d->servers.remove(d->servers.begin());
d->aaaa = !d->aaaa;
// failed? bail if last one
if(d->servers.isEmpty()) {
stop();
resultsReady();
return;
}
// otherwise try the next
tryNext();
}
#endif
}
void SrvResolver::t_timeout()
{
SafeDeleteLock s(&d->sd);
stop();
resultsReady();
}
// CS_NAMESPACE_END

View file

@ -0,0 +1,66 @@
/*
* srvresolver.h - class to simplify SRV lookups
* Copyright (C) 2003 Justin Karneges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef CS_SRVRESOLVER_H
#define CS_SRVRESOLVER_H
#include <QList>
#include <q3dns.h>
// CS_NAMESPACE_BEGIN
class SrvResolver : public QObject
{
Q_OBJECT
public:
SrvResolver(QObject *parent=0);
~SrvResolver();
void resolve(const QString &server, const QString &type, const QString &proto);
void resolveSrvOnly(const QString &server, const QString &type, const QString &proto);
void next();
void stop();
bool isBusy() const;
QList<Q3Dns::Server> servers() const;
bool failed() const;
QHostAddress resultAddress() const;
Q_UINT16 resultPort() const;
signals:
void resultsReady();
private slots:
void qdns_done();
void ndns_done();
void t_timeout();
private:
class Private;
Private *d;
void tryNext();
void resolve(const QString &server, const QString &type, const QString &proto, bool srvOnly);
};
// CS_NAMESPACE_END
#endif