aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2010-08-29 15:28:28 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2010-08-29 15:28:28 +0000
commitc4ccfbf5795d7cdc37fb32ebdd145ae0e68b5b77 (patch)
treed8cec84d7b4ef0eb50ea7e5679a736fbdd0f3cb9 /src
parentf88c369a352750426ef3eeb67130777e8af80176 (diff)
downloadqxmpp-c4ccfbf5795d7cdc37fb32ebdd145ae0e68b5b77.tar.gz
improve DNS SRV lookup API
Diffstat (limited to 'src')
-rw-r--r--src/QXmppServiceInfo.cpp83
-rw-r--r--src/QXmppServiceInfo.h24
2 files changed, 68 insertions, 39 deletions
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 <resolv.h>
#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<QXmppServiceRecord> 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<QXmppServiceInfo> &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, QList<QXmppServiceInf
{
if ((ptr->wType == 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, QList<QXmppServiceInf
responseLength = res_query(dname.toAscii(), C_IN, T_SRV, response, sizeof(response));
if (responseLength < int(sizeof(HEADER)))
{
- qWarning() << "SRV lookup for" << dname << "failed";
- herror("SRV lookup error");
- return false;
+ result.m_errorString = QString("res_query failed: %1").arg(hstrerror(h_errno));
+ return result;
}
/* check the response header */
HEADER *header = (HEADER*)response;
if (header->rcode != 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<QXmppServiceInf
int status = dn_expand(response, response + responseLength, p, host, sizeof(host));
if (status < 0)
{
- qWarning("dn_expand failed");
- return false;
+ result.m_errorString = QLatin1String("dn_expand failed");
+ return result;
}
p += status + 4;
@@ -155,8 +170,8 @@ bool QXmppServiceInfo::lookupService(const QString &dname, QList<QXmppServiceInf
status = dn_expand(response, response + responseLength, p, host, sizeof(host));
if (status < 0)
{
- qWarning("dn_expand failed");
- return false;
+ result.m_errorString = QLatin1String("dn_expand failed");
+ return result;
}
p += status;
@@ -175,13 +190,13 @@ bool QXmppServiceInfo::lookupService(const QString &dname, QList<QXmppServiceInf
status = dn_expand(response, response + responseLength, p + 6, answer, sizeof(answer));
if (status < 0)
{
- qWarning("dn_expand failed");
- return false;
+ result.m_errorString = QLatin1String("dn_expand failed");
+ return result;
}
- QXmppServiceInfo info;
- info.setHostName(answer);
- info.setPort(port);
- results.append(info);
+ QXmppServiceRecord record;
+ record.setHostName(answer);
+ record.setPort(port);
+ result.m_records.append(record);
} else {
qWarning("Unexpected DNS answer type");
}
@@ -189,5 +204,5 @@ bool QXmppServiceInfo::lookupService(const QString &dname, QList<QXmppServiceInf
answerIndex++;
}
#endif
- return !results.isEmpty();
+ return result;
}
diff --git a/src/QXmppServiceInfo.h b/src/QXmppServiceInfo.h
index 6dd44476..51e71282 100644
--- a/src/QXmppServiceInfo.h
+++ b/src/QXmppServiceInfo.h
@@ -27,13 +27,13 @@
#include <QList>
#include <QString>
-/// \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<QXmppServiceInfo> &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<QXmppServiceRecord> records() const;
+
+ static QXmppServiceInfo fromName(const QString &dname);
+
+private:
+ QString m_errorString;
+ QList<QXmppServiceRecord> m_records;
+};
+
#endif