From c4ccfbf5795d7cdc37fb32ebdd145ae0e68b5b77 Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Sun, 29 Aug 2010 15:28:28 +0000 Subject: improve DNS SRV lookup API --- src/QXmppServiceInfo.cpp | 83 ++++++++++++++++++++++++++++-------------------- src/QXmppServiceInfo.h | 24 +++++++++++--- 2 files changed, 68 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/QXmppServiceInfo.cpp b/src/QXmppServiceInfo.cpp index cae8ff64..7f627bf8 100644 --- a/src/QXmppServiceInfo.cpp +++ b/src/QXmppServiceInfo.cpp @@ -37,65 +37,81 @@ #include #endif -/// Constructs an empty service info object. +/// Constructs an empty service record object. /// -QXmppServiceInfo::QXmppServiceInfo() +QXmppServiceRecord::QXmppServiceRecord() : host_port(0) { } -/// Returns host name for this service. +/// Returns host name for this service record. /// -QString QXmppServiceInfo::hostName() const +QString QXmppServiceRecord::hostName() const { return host_name; } -/// Sets the host name for this service. +/// Sets the host name for this service record. /// /// \param hostName -void QXmppServiceInfo::setHostName(const QString &hostName) +void QXmppServiceRecord::setHostName(const QString &hostName) { host_name = hostName; } -/// Returns the port for this service. +/// Returns the port for this service record. /// -quint16 QXmppServiceInfo::port() const +quint16 QXmppServiceRecord::port() const { return host_port; } -/// Sets the port for this service. +/// Sets the port for this service record. /// /// \param port -void QXmppServiceInfo::setPort(quint16 port) +void QXmppServiceRecord::setPort(quint16 port) { host_port = port; } +/// If the lookup failed, this function returns a human readable description of the error. +/// + +QString QXmppServiceInfo::errorString() const +{ + return m_errorString; +} + +/// Returns the list of records associated with this service. +/// +QList QXmppServiceInfo::records() const +{ + return m_records; +} + /// Perform a DNS lookup for an SRV entry. /// /// Returns true if the lookup was succesful, false if it failed. /// /// \param dname -/// \param results -bool QXmppServiceInfo::lookupService(const QString &dname, QList &results) +QXmppServiceInfo QXmppServiceInfo::fromName(const QString &dname) { + QXmppServiceInfo result; + #ifdef Q_OS_WIN PDNS_RECORD records, ptr; /* perform DNS query */ if (DnsQuery_UTF8(dname.toAscii(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &records, NULL) != ERROR_SUCCESS) { - qWarning() << "SRV lookup for" << dname << "failed"; - return false; + result.m_errorString = QLatin1String("DnsQuery_UTF8 failed"); + return result; } /* extract results */ @@ -103,10 +119,10 @@ bool QXmppServiceInfo::lookupService(const QString &dname, QListwType == DNS_TYPE_SRV) && !strcmp(ptr->pName, dname.toAscii())) { - QXmppServiceInfo info; - info.setHostName(ptr->Data.Srv.pNameTarget); - info.setPort(ptr->Data.Srv.wPort); - results.append(info); + QXmppServiceRecord record; + record.setHostName(ptr->Data.Srv.pNameTarget); + record.setPort(ptr->Data.Srv.wPort); + result.m_records.append(record); } } @@ -123,17 +139,16 @@ bool QXmppServiceInfo::lookupService(const QString &dname, QListrcode != NOERROR || !(answerCount = ntohs(header->ancount))) { - qWarning() << "SRV lookup for" << dname << "returned an error"; - return false; + result.m_errorString = QLatin1String("res_query returned an error"); + return result; } /* skip the query */ @@ -142,8 +157,8 @@ bool QXmppServiceInfo::lookupService(const QString &dname, QList #include -/// \brief The QXmppServiceInfo class is used to perform DNS SRV queries. +/// \brief The QXmppServiceRecord class represents a DNS SRV record. /// -class QXmppServiceInfo +class QXmppServiceRecord { public: - QXmppServiceInfo(); + QXmppServiceRecord(); QString hostName() const; void setHostName(const QString &hostName); @@ -41,11 +41,25 @@ public: quint16 port() const; void setPort(quint16 port); - static bool lookupService(const QString &dname, QList &results); - private: QString host_name; quint16 host_port; }; +/// \brief The QXmppServiceInfo class provides static methods for DNS SRV lookups. +/// + +class QXmppServiceInfo +{ +public: + QString errorString() const; + QList records() const; + + static QXmppServiceInfo fromName(const QString &dname); + +private: + QString m_errorString; + QList m_records; +}; + #endif -- cgit v1.2.3