1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* Copyright (C) 2008-2010 The QXmpp developers
*
* Author:
* Jeremy Lainé
*
* Source:
* http://code.google.com/p/qxmpp
*
* This file is a part of QXmpp library.
*
* 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.
*
*/
#include <QDebug>
#include "QXmppSrvInfo.h"
#if defined(Q_OS_WIN)
#include <windows.h>
#include <windns.h>
#elif defined(Q_OS_SYMBIAN)
#include <dns_qry.h>
#else
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/nameser.h>
#include <arpa/nameser_compat.h>
#include <resolv.h>
#endif
/// Constructs an empty service record object.
///
QXmppSrvRecord::QXmppSrvRecord()
: m_port(0),
m_priority(0),
m_weight(0)
{
}
/// Returns host name for this service record.
///
QString QXmppSrvRecord::hostName() const
{
return host_name;
}
/// Sets the host name for this service record.
///
/// \param hostName
void QXmppSrvRecord::setHostName(const QString &hostName)
{
host_name = hostName;
}
/// Returns the port for this service record.
///
quint16 QXmppSrvRecord::port() const
{
return m_port;
}
/// Sets the port for this service record.
///
/// \param port
void QXmppSrvRecord::setPort(quint16 port)
{
m_port = port;
}
/// Returns the priority for this service record.
///
quint16 QXmppSrvRecord::priority() const
{
return m_priority;
}
/// Sets the priority for this service record.
///
/// \param priority
void QXmppSrvRecord::setPriority(quint16 priority)
{
m_priority = priority;
}
/// Returns the weight for this service record.
///
quint16 QXmppSrvRecord::weight() const
{
return m_weight;
}
/// Sets the weight for this service record.
///
/// \param priority
void QXmppSrvRecord::setWeight(quint16 weight)
{
m_weight = weight;
}
/// If the lookup failed, this function returns a human readable description of the error.
///
QString QXmppSrvInfo::errorString() const
{
return m_errorString;
}
/// Returns the list of records associated with this service.
///
QList<QXmppSrvRecord> QXmppSrvInfo::records() const
{
return m_records;
}
/// Perform a DNS lookup for an SRV entry.
///
/// Returns a QXmppSrvInfo object containing the found records.
///
/// \param dname
QXmppSrvInfo QXmppSrvInfo::fromName(const QString &dname)
{
QXmppSrvInfo result;
#if defined(Q_OS_WIN)
PDNS_RECORD records, ptr;
/* perform DNS query */
if (DnsQuery_UTF8(dname.toUtf8(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &records, NULL) != ERROR_SUCCESS)
{
result.m_errorString = QLatin1String("DnsQuery_UTF8 failed");
return result;
}
/* extract results */
for (ptr = records; ptr != NULL; ptr = ptr->pNext)
{
if ((ptr->wType == DNS_TYPE_SRV) && !strcmp((char*)ptr->pName, dname.toUtf8()))
{
QXmppSrvRecord record;
record.setHostName(QString::fromUtf8((char*)ptr->Data.Srv.pNameTarget));
record.setPort(ptr->Data.Srv.wPort);
record.setPriority(ptr->Data.Srv.wPriority);
record.setWeight(ptr->Data.Srv.wWeight);
result.m_records.append(record);
}
}
DnsRecordListFree(records, DnsFreeRecordList);
#elif defined(Q_OS_SYMBIAN)
RHostResolver dnsResolver;
RSocketServ dnsSocket;
/* initialise resolver */
TInt err = dnsSocket.Connect();
err = dnsResolver.Open(dnsSocket, KAfInet, KProtocolInetUdp);
if (err != KErrNone)
{
result.m_errorString = QLatin1String("RHostResolver::Open failed");
return result;
}
/* perform DNS query */
TDnsQueryBuf dnsQuery;
TDnsRespSRVBuf dnsResponse;
dnsQuery().SetClass(KDnsRRClassIN);
QByteArray utf8name = dname.toUtf8();
TPtrC8 queryPtr(reinterpret_cast<const TUint8*>(utf8name.constData()),utf8name.size());
dnsQuery().SetData(queryPtr);
dnsQuery().SetType(KDnsRRTypeSRV);
err = dnsResolver.Query(dnsQuery, dnsResponse);
if (err != KErrNone)
{
result.m_errorString = QLatin1String("RHostResolver::Query failed");
return result;
}
/* extract results */
while (err == KErrNone)
{
QXmppSrvRecord record;
record.setHostName(QString::fromUtf8((const char*)dnsResponse().Target().Ptr(),
dnsResponse().Target().Length()));
record.setPort(dnsResponse().Port());
record.setPriority(dnsResponse().Priority());
record.setWeight(dnsResponse().Weight());
result.m_records.append(record);
err = dnsResolver.QueryGetNext(dnsResponse);
}
#else
unsigned char response[PACKETSZ];
int responseLength, answerCount, answerIndex;
/* explicitly call res_init in case config changed */
res_init();
/* perform DNS query */
memset(response, 0, sizeof(response));
responseLength = res_query(dname.toAscii(), C_IN, T_SRV, response, sizeof(response));
if (responseLength < int(sizeof(HEADER)))
{
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)))
{
result.m_errorString = QLatin1String("res_query returned an error");
return result;
}
/* skip the query */
char host[PACKETSZ], answer[PACKETSZ];
unsigned char *p = response + sizeof(HEADER);
int status = dn_expand(response, response + responseLength, p, host, sizeof(host));
if (status < 0)
{
result.m_errorString = QLatin1String("dn_expand failed");
return result;
}
p += status + 4;
/* parse answers */
answerIndex = 0;
while ((p < response + responseLength) && (answerIndex < answerCount))
{
int type, klass, ttl, size;
status = dn_expand(response, response + responseLength, p, host, sizeof(host));
if (status < 0)
{
result.m_errorString = QLatin1String("dn_expand failed");
return result;
}
p += status;
type = (p[0] << 8) | p[1];
p += 2;
klass = (p[0] << 8) | p[1];
p += 2;
ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
p += 4;
size = (p[0] << 8) | p[1];
p += 2;
if (type == T_SRV)
{
quint16 priority = (p[0] << 8) | p[1];
quint16 weight = (p[2] << 8) | p[3];
quint16 port = (p[4] << 8) | p[5];
status = dn_expand(response, response + responseLength, p + 6, answer, sizeof(answer));
if (status < 0)
{
result.m_errorString = QLatin1String("dn_expand failed");
return result;
}
QXmppSrvRecord record;
record.setHostName(answer);
record.setPort(port);
record.setPriority(priority);
record.setWeight(weight);
result.m_records.append(record);
} else {
qWarning("Unexpected DNS answer type");
}
p += size;
answerIndex++;
}
#endif
return result;
}
|