~ubuntu-branches/ubuntu/breezy/psi/breezy

« back to all changes in this revision

Viewing changes to cutestuff/network/srvresolver.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

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
#include<qcstring.h>
 
24
#include<qtimer.h>
 
25
#include<qdns.h>
 
26
#include"safedelete.h"
 
27
 
 
28
#ifndef NO_NDNS
 
29
#include"ndns.h"
 
30
#endif
 
31
 
 
32
// CS_NAMESPACE_BEGIN
 
33
 
 
34
static void sortSRVList(QValueList<QDns::Server> &list)
 
35
{
 
36
        QValueList<QDns::Server> tmp = list;
 
37
        list.clear();
 
38
 
 
39
        while(!tmp.isEmpty()) {
 
40
                QValueList<QDns::Server>::Iterator p = tmp.end();
 
41
                for(QValueList<QDns::Server>::Iterator it = tmp.begin(); it != tmp.end(); ++it) {
 
42
                        if(p == tmp.end())
 
43
                                p = it;
 
44
                        else {
 
45
                                int a = (*it).priority;
 
46
                                int b = (*p).priority;
 
47
                                int j = (*it).weight;
 
48
                                int k = (*p).weight;
 
49
                                if(a < b || (a == b && j < k))
 
50
                                        p = it;
 
51
                        }
 
52
                }
 
53
                list.append(*p);
 
54
                tmp.remove(p);
 
55
        }
 
56
}
 
57
 
 
58
class SrvResolver::Private
 
59
{
 
60
public:
 
61
        Private() {}
 
62
 
 
63
        QDns *qdns;
 
64
#ifndef NO_NDNS
 
65
        NDns ndns;
 
66
#endif
 
67
 
 
68
        bool failed;
 
69
        QHostAddress resultAddress;
 
70
        Q_UINT16 resultPort;
 
71
 
 
72
        bool srvonly;
 
73
        QString srv;
 
74
        QValueList<QDns::Server> servers;
 
75
        bool aaaa;
 
76
 
 
77
        QTimer t;
 
78
        SafeDelete sd;
 
79
};
 
80
 
 
81
SrvResolver::SrvResolver(QObject *parent)
 
82
:QObject(parent)
 
83
{
 
84
        d = new Private;
 
85
        d->qdns = 0;
 
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.start(15000, true);
 
108
        d->qdns = new QDns;
 
109
        connect(d->qdns, SIGNAL(resultsReady()), SLOT(qdns_done()));
 
110
        d->qdns->setRecordType(QDns::Srv);
 
111
        d->qdns->setLabel(d->srv);
 
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.start(15000, true);
 
122
        d->qdns = new QDns;
 
123
        connect(d->qdns, SIGNAL(resultsReady()), SLOT(qdns_done()));
 
124
        d->qdns->setRecordType(QDns::Srv);
 
125
        d->qdns->setLabel(d->srv);
 
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->qdns) {
 
141
                d->qdns->disconnect(this);
 
142
                d->sd.deleteLater(d->qdns);
 
143
                d->qdns = 0;
 
144
        }
 
145
#ifndef NO_NDNS
 
146
        if(d->ndns.isBusy())
 
147
                d->ndns.stop();
 
148
#endif
 
149
        d->resultAddress = QHostAddress();
 
150
        d->resultPort = 0;
 
151
        d->servers.clear();
 
152
        d->srv = "";
 
153
        d->failed = true;
 
154
}
 
155
 
 
156
bool SrvResolver::isBusy() const
 
157
{
 
158
#ifndef NO_NDNS
 
159
        if(d->qdns || d->ndns.isBusy())
 
160
#else
 
161
        if(d->qdns)
 
162
#endif
 
163
                return true;
 
164
        else
 
165
                return false;
 
166
}
 
167
 
 
168
QValueList<QDns::Server> SrvResolver::servers() const
 
169
{
 
170
        return d->servers;
 
171
}
 
172
 
 
173
bool SrvResolver::failed() const
 
174
{
 
175
        return d->failed;
 
176
}
 
177
 
 
178
QHostAddress SrvResolver::resultAddress() const
 
179
{
 
180
        return d->resultAddress;
 
181
}
 
182
 
 
183
Q_UINT16 SrvResolver::resultPort() const
 
184
{
 
185
        return d->resultPort;
 
186
}
 
187
 
 
188
void SrvResolver::tryNext()
 
189
{
 
190
#ifndef NO_NDNS
 
191
        d->ndns.resolve(d->servers.first().name);
 
192
#else
 
193
        d->qdns = new QDns;
 
194
        connect(d->qdns, SIGNAL(resultsReady()), SLOT(ndns_done()));
 
195
        if(d->aaaa)
 
196
                d->qdns->setRecordType(QDns::Aaaa); // IPv6
 
197
        else
 
198
                d->qdns->setRecordType(QDns::A); // IPv4
 
199
        d->qdns->setLabel(d->servers.first().name);
 
200
#endif
 
201
}
 
202
 
 
203
void SrvResolver::qdns_done()
 
204
{
 
205
        if(!d->qdns)
 
206
                return;
 
207
 
 
208
        // apparently we sometimes get this signal even though the results aren't ready
 
209
        if(d->qdns->isWorking())
 
210
                return;
 
211
        d->t.stop();
 
212
 
 
213
        SafeDeleteLock s(&d->sd);
 
214
 
 
215
        // grab the server list and destroy the qdns object
 
216
        QValueList<QDns::Server> list;
 
217
        if(d->qdns->recordType() == QDns::Srv)
 
218
                list = d->qdns->servers();
 
219
        d->qdns->disconnect(this);
 
220
        d->sd.deleteLater(d->qdns);
 
221
        d->qdns = 0;
 
222
 
 
223
        if(list.isEmpty()) {
 
224
                stop();
 
225
                resultsReady();
 
226
                return;
 
227
        }
 
228
        sortSRVList(list);
 
229
        d->servers = list;
 
230
 
 
231
        if(d->srvonly)
 
232
                resultsReady();
 
233
        else {
 
234
                // kick it off
 
235
                d->aaaa = true;
 
236
                tryNext();
 
237
        }
 
238
}
 
239
 
 
240
void SrvResolver::ndns_done()
 
241
{
 
242
#ifndef NO_NDNS
 
243
        SafeDeleteLock s(&d->sd);
 
244
 
 
245
        uint r = d->ndns.result();
 
246
        int port = d->servers.first().port;
 
247
        d->servers.remove(d->servers.begin());
 
248
 
 
249
        if(r) {
 
250
                d->resultAddress = QHostAddress(d->ndns.result());
 
251
                d->resultPort = port;
 
252
                resultsReady();
 
253
        }
 
254
        else {
 
255
                // failed?  bail if last one
 
256
                if(d->servers.isEmpty()) {
 
257
                        stop();
 
258
                        resultsReady();
 
259
                        return;
 
260
                }
 
261
 
 
262
                // otherwise try the next
 
263
                tryNext();
 
264
        }
 
265
#else
 
266
        if(!d->qdns)
 
267
                return;
 
268
 
 
269
        // apparently we sometimes get this signal even though the results aren't ready
 
270
        if(d->qdns->isWorking())
 
271
                return;
 
272
 
 
273
        SafeDeleteLock s(&d->sd);
 
274
 
 
275
        // grab the address list and destroy the qdns object
 
276
        QValueList<QHostAddress> list;
 
277
        if(d->qdns->recordType() == QDns::A || d->qdns->recordType() == QDns::Aaaa)
 
278
                list = d->qdns->addresses();
 
279
        d->qdns->disconnect(this);
 
280
        d->sd.deleteLater(d->qdns);
 
281
        d->qdns = 0;
 
282
 
 
283
        if(!list.isEmpty()) {
 
284
                int port = d->servers.first().port;
 
285
                d->servers.remove(d->servers.begin());
 
286
                d->aaaa = true;
 
287
 
 
288
                d->resultAddress = list.first();
 
289
                d->resultPort = port;
 
290
                resultsReady();
 
291
        }
 
292
        else {
 
293
                if(!d->aaaa)
 
294
                        d->servers.remove(d->servers.begin());
 
295
                d->aaaa = !d->aaaa;
 
296
 
 
297
                // failed?  bail if last one
 
298
                if(d->servers.isEmpty()) {
 
299
                        stop();
 
300
                        resultsReady();
 
301
                        return;
 
302
                }
 
303
 
 
304
                // otherwise try the next
 
305
                tryNext();
 
306
        }
 
307
#endif
 
308
}
 
309
 
 
310
void SrvResolver::t_timeout()
 
311
{
 
312
        SafeDeleteLock s(&d->sd);
 
313
 
 
314
        stop();
 
315
        resultsReady();
 
316
}
 
317
 
 
318
// CS_NAMESPACE_END