~ubuntu-branches/ubuntu/oneiric/psi/oneiric

« back to all changes in this revision

Viewing changes to iris/src/irisnet/noncore/legacy/srvresolver.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * srvresolver.cpp - class to simplify SRV lookups
 
3
 * Copyright (C) 2003  Justin Karneges
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "srvresolver.h"
 
22
 
 
23
#ifndef NO_NDNS
 
24
# include "ndns.h"
 
25
#endif
 
26
 
 
27
// CS_NAMESPACE_BEGIN
 
28
 
 
29
static void sortSRVList(QList<Q3Dns::Server> &list)
 
30
{
 
31
        QList<Q3Dns::Server> tmp = list;
 
32
        list.clear();
 
33
 
 
34
        while(!tmp.isEmpty()) {
 
35
                QList<Q3Dns::Server>::Iterator p = tmp.end();
 
36
                for(QList<Q3Dns::Server>::Iterator it = tmp.begin(); it != tmp.end(); ++it) {
 
37
                        if(p == tmp.end())
 
38
                                p = it;
 
39
                        else {
 
40
                                int a = (*it).priority;
 
41
                                int b = (*p).priority;
 
42
                                int j = (*it).weight;
 
43
                                int k = (*p).weight;
 
44
                                if(a < b || (a == b && j < k))
 
45
                                        p = it;
 
46
                        }
 
47
                }
 
48
                list.append(*p);
 
49
                tmp.erase(p);
 
50
        }
 
51
}
 
52
 
 
53
class SrvResolver::Private
 
54
{
 
55
public:
 
56
        Private() {}
 
57
 
 
58
        XMPP::NameResolver nndns;
 
59
        XMPP::NameRecord::Type nntype;
 
60
        bool nndns_busy;
 
61
 
 
62
#ifndef NO_NDNS
 
63
        NDns ndns;
 
64
#endif
 
65
 
 
66
        bool failed;
 
67
        QHostAddress resultAddress;
 
68
        quint16 resultPort;
 
69
 
 
70
        bool srvonly;
 
71
        QString srv;
 
72
        QList<Q3Dns::Server> servers;
 
73
        bool aaaa;
 
74
 
 
75
        QTimer t;
 
76
};
 
77
 
 
78
SrvResolver::SrvResolver(QObject *parent)
 
79
:QObject(parent)
 
80
{
 
81
        d = new Private;
 
82
        d->nndns_busy = false;
 
83
 
 
84
        connect(&d->nndns, SIGNAL(resultsReady(const QList<XMPP::NameRecord> &)), SLOT(nndns_resultsReady(const QList<XMPP::NameRecord> &)));
 
85
        connect(&d->nndns, SIGNAL(error(XMPP::NameResolver::Error)), SLOT(nndns_error(XMPP::NameResolver::Error)));
 
86
 
 
87
#ifndef NO_NDNS
 
88
        connect(&d->ndns, SIGNAL(resultsReady()), SLOT(ndns_done()));
 
89
#endif
 
90
        connect(&d->t, SIGNAL(timeout()), SLOT(t_timeout()));
 
91
        stop();
 
92
}
 
93
 
 
94
SrvResolver::~SrvResolver()
 
95
{
 
96
        stop();
 
97
        delete d;
 
98
}
 
99
 
 
100
void SrvResolver::resolve(const QString &server, const QString &type, const QString &proto)
 
101
{
 
102
        stop();
 
103
 
 
104
        d->failed = false;
 
105
        d->srvonly = false;
 
106
        d->srv = QString("_") + type + "._" + proto + '.' + server;
 
107
        d->t.setSingleShot(true);
 
108
        d->t.start(15000);
 
109
        d->nndns_busy = true;
 
110
        d->nntype = XMPP::NameRecord::Srv;
 
111
        d->nndns.start(d->srv.toLatin1(), d->nntype);
 
112
}
 
113
 
 
114
void SrvResolver::resolveSrvOnly(const QString &server, const QString &type, const QString &proto)
 
115
{
 
116
        stop();
 
117
 
 
118
        d->failed = false;
 
119
        d->srvonly = true;
 
120
        d->srv = QString("_") + type + "._" + proto + '.' + server;
 
121
        d->t.setSingleShot(true);
 
122
        d->t.start(15000);
 
123
        d->nndns_busy = true;
 
124
        d->nntype = XMPP::NameRecord::Srv;
 
125
        d->nndns.start(d->srv.toLatin1(), d->nntype);
 
126
}
 
127
 
 
128
void SrvResolver::next()
 
129
{
 
130
        if(d->servers.isEmpty())
 
131
                return;
 
132
 
 
133
        tryNext();
 
134
}
 
135
 
 
136
void SrvResolver::stop()
 
137
{
 
138
        if(d->t.isActive())
 
139
                d->t.stop();
 
140
        if(d->nndns_busy) {
 
141
                d->nndns.stop();
 
142
                d->nndns_busy = false;
 
143
        }
 
144
#ifndef NO_NDNS
 
145
        if(d->ndns.isBusy())
 
146
                d->ndns.stop();
 
147
#endif
 
148
        d->resultAddress = QHostAddress();
 
149
        d->resultPort = 0;
 
150
        d->servers.clear();
 
151
        d->srv = "";
 
152
        d->failed = true;
 
153
}
 
154
 
 
155
bool SrvResolver::isBusy() const
 
156
{
 
157
#ifndef NO_NDNS
 
158
        if(d->nndns_busy || d->ndns.isBusy())
 
159
#else
 
160
        if(d->nndns_busy)
 
161
#endif
 
162
                return true;
 
163
        else
 
164
                return false;
 
165
}
 
166
 
 
167
QList<Q3Dns::Server> SrvResolver::servers() const
 
168
{
 
169
        return d->servers;
 
170
}
 
171
 
 
172
bool SrvResolver::failed() const
 
173
{
 
174
        return d->failed;
 
175
}
 
176
 
 
177
QHostAddress SrvResolver::resultAddress() const
 
178
{
 
179
        return d->resultAddress;
 
180
}
 
181
 
 
182
quint16 SrvResolver::resultPort() const
 
183
{
 
184
        return d->resultPort;
 
185
}
 
186
 
 
187
void SrvResolver::tryNext()
 
188
{
 
189
#ifndef NO_NDNS
 
190
        d->ndns.resolve(d->servers.first().name);
 
191
#else
 
192
        d->nndns_busy = true;
 
193
        d->nntype = d->aaaa ? XMPP::NameRecord::Aaaa : XMPP::NameRecord::A;
 
194
        d->nndns.start(d->servers.first().name.toLatin1(), d->nntype);
 
195
#endif
 
196
}
 
197
 
 
198
void SrvResolver::nndns_resultsReady(const QList<XMPP::NameRecord> &results)
 
199
{
 
200
        if(!d->nndns_busy)
 
201
                return;
 
202
 
 
203
        d->t.stop();
 
204
 
 
205
        if(d->nntype == XMPP::NameRecord::Srv) {
 
206
                // grab the server list and destroy the qdns object
 
207
                QList<Q3Dns::Server> list;
 
208
                for(int n = 0; n < results.count(); ++n)
 
209
                {
 
210
                        list += Q3Dns::Server(QString::fromLatin1(results[n].name()), results[n].priority(), results[n].weight(), results[n].port());
 
211
                }
 
212
 
 
213
                d->nndns_busy = false;
 
214
                d->nndns.stop();
 
215
 
 
216
                if(list.isEmpty()) {
 
217
                        stop();
 
218
                        resultsReady();
 
219
                        return;
 
220
                }
 
221
                sortSRVList(list);
 
222
                d->servers = list;
 
223
 
 
224
                if(d->srvonly)
 
225
                        resultsReady();
 
226
                else {
 
227
                        // kick it off
 
228
                        d->aaaa = true;
 
229
                        tryNext();
 
230
                }
 
231
        }
 
232
        else {
 
233
                // grab the address list and destroy the qdns object
 
234
                QList<QHostAddress> list;
 
235
                if(d->nntype == XMPP::NameRecord::A || d->nntype == XMPP::NameRecord::Aaaa)
 
236
                {
 
237
                        for(int n = 0; n < results.count(); ++n)
 
238
                        {
 
239
                                list += results[n].address();
 
240
                        }
 
241
                }
 
242
                d->nndns_busy = false;
 
243
                d->nndns.stop();
 
244
 
 
245
                if(!list.isEmpty()) {
 
246
                        int port = d->servers.first().port;
 
247
                        d->servers.removeFirst();
 
248
                        d->aaaa = true;
 
249
 
 
250
                        d->resultAddress = list.first();
 
251
                        d->resultPort = port;
 
252
                        resultsReady();
 
253
                }
 
254
                else {
 
255
                        if(!d->aaaa)
 
256
                                d->servers.removeFirst();
 
257
                        d->aaaa = !d->aaaa;
 
258
 
 
259
                        // failed?  bail if last one
 
260
                        if(d->servers.isEmpty()) {
 
261
                                stop();
 
262
                                resultsReady();
 
263
                                return;
 
264
                        }
 
265
 
 
266
                        // otherwise try the next
 
267
                        tryNext();
 
268
                }
 
269
        }
 
270
}
 
271
 
 
272
void SrvResolver::nndns_error(XMPP::NameResolver::Error)
 
273
{
 
274
        nndns_resultsReady(QList<XMPP::NameRecord>());
 
275
}
 
276
 
 
277
void SrvResolver::ndns_done()
 
278
{
 
279
#ifndef NO_NDNS
 
280
        QHostAddress r = d->ndns.result();
 
281
        int port = d->servers.first().port;
 
282
        d->servers.removeFirst();
 
283
 
 
284
        if(!r.isNull()) {
 
285
                d->resultAddress = d->ndns.result();
 
286
                d->resultPort = port;
 
287
                resultsReady();
 
288
        }
 
289
        else {
 
290
                // failed?  bail if last one
 
291
                if(d->servers.isEmpty()) {
 
292
                        stop();
 
293
                        resultsReady();
 
294
                        return;
 
295
                }
 
296
 
 
297
                // otherwise try the next
 
298
                tryNext();
 
299
        }
 
300
#endif
 
301
}
 
302
 
 
303
void SrvResolver::t_timeout()
 
304
{
 
305
        stop();
 
306
        resultsReady();
 
307
}
 
308
 
 
309
// CS_NAMESPACE_END