summaryrefslogtreecommitdiffstats
path: root/tdecore/network/ksrvresolverworker.cpp
blob: f3166b98227c7b213947094b91497c5b9d7fcdd6 (plain)
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
/*  -*- C++ -*-
 *  Copyright (C) 2005 Thiago Macieira <[email protected]>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include "ksrvresolverworker_p.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>

#include <tqapplication.h>
#include <tqevent.h>

using namespace KNetwork;
using namespace KNetwork::Internal;

namespace 
{
  struct KSrvStartEvent: public TQCustomEvent
  {
    inline KSrvStartEvent() : TQCustomEvent(TQEvent::User) { }
  };
}

static void sortPriorityClass(KSrvResolverWorker::PriorityClass&)
{
  // do nothing
}

bool KSrvResolverWorker::preprocess()
{
  // check if the resolver flags mention SRV-based lookup
  if ((flags() & (KResolver::NoSrv | KResolver::UseSrv)) != KResolver::UseSrv)
    return false;

  TQString node = nodeName();
  if (node.find('%') != -1)
    node.truncate(node.find('%'));

  if (node.isEmpty() || node == TQString::fromLatin1("*") ||
      node == TQString::fromLatin1("localhost"))
    return false;		// empty == localhost

  encodedName = KResolver::domainToAscii(node);
  if (encodedName.isNull())
    return false;

  // we only work with Internet-based families
  if ((familyMask() & KResolver::InternetFamily) == 0)
    return false;

  // SRV-based resolution only works if the service isn't numeric
  bool ok;
  serviceName().toUInt(&ok);
  if (ok)
    return false;		// it is numeric

  // check the protocol for something we know
  TQCString protoname;
  int sockettype = socketType();
  if (!protocolName().isEmpty())
    protoname = protocolName();
  else if (protocol() != 0)
    {
      TQStrList names = KResolver::protocolName(protocol());
      names.setAutoDelete(true);
      if (names.isEmpty())
	return false;

      protoname = "_";
      protoname += names.at(0);
    }
  else if (sockettype == SOCK_STREAM || sockettype == 0)
    protoname = "_tcp";
  else if (sockettype == SOCK_DGRAM)
    protoname = "_udp";
  else
    return false;		// unknown protocol and socket type

  encodedName.prepend(".");
  encodedName.prepend(protoname);
  encodedName.prepend(".");
  encodedName.prepend(serviceName().latin1());
  encodedName.prepend("_");

  // looks like something we could process
  return true;
}

bool KSrvResolverWorker::run()
{
  sem = new TQSemaphore(1);
  // zero out
  sem->tryAccess(sem->available());

  TQApplication::postEvent(this, new KSrvStartEvent);

  // block
  (*sem)++;
  delete sem;
  sem = 0L;

  if (rawResults.isEmpty())
    {
      // normal lookup
      KResolver *r = new KResolver(nodeName(), serviceName());
      r->setFlags(flags() | KResolver::NoSrv);
      r->setFamily(familyMask());
      r->setSocketType(socketType());
      r->setProtocol(protocol(), protocolName());

      enqueue(r);

      Entry e;
      PriorityClass cl;
      e.resolver = r;
      cl.entries.append(e);
      myResults[0] = cl;

      return true;
    }
  else if (rawResults.count() == 1 && rawResults.first().name == ".")
    {
      // no name
      setError(KResolver::NoName);
      finished();
      return true;
    }
  else
    {
      // now process the results
      TQValueList<TQDns::Server>::ConstIterator it = rawResults.begin();
      while (it != rawResults.end())
	{
	  const TQDns::Server& srv = *it;
	  PriorityClass& r = myResults[srv.priority];
	  r.totalWeight += srv.weight;

	  Entry e;
	  e.name = srv.name;
	  e.port = srv.port;
	  e.weight = srv.weight;
	  e.resolver = 0L;
	  r.entries.append(e);

	  ++it;
	}
      rawResults.clear();	// free memory

      Results::Iterator mapit;
      for (mapit = myResults.begin(); mapit != myResults.end(); ++mapit)
	{
	  // sort the priority
	  sortPriorityClass(*mapit);

	  TQValueList<Entry>& entries = (*mapit).entries;

	  // start the resolvers
	  for (TQValueList<Entry>::Iterator it = entries.begin();
	       it != entries.end(); ++it)
	    {
	      Entry &e = *it;

	      KResolver* r = new KResolver(e.name, TQString::number(e.port));
	      r->setFlags(flags() | KResolver::NoSrv);
	      r->setFamily(familyMask());
	      r->setSocketType(socketType());
	      r->setProtocol(protocol(), protocolName());

	      enqueue(r);
	      e.resolver = r;
	    }
	}

      return true;
    }
}

bool KSrvResolverWorker::postprocess()
{
  setError(KResolver::NoName);
  if (myResults.isEmpty())
    return false;

  Results::Iterator mapit, mapend;
  for (mapit = myResults.begin(), mapend = myResults.end();
       mapit != mapend; ++mapit)
    {
      TQValueList<Entry>::Iterator it = (*mapit).entries.begin(),
	end = (*mapit).entries.end();
      for ( ; it != end; ++it)
	{
	  Entry &e = *it;
	  KResolverResults r = e.resolver->results();
	  if (r.isEmpty() && results.isEmpty())
	    setError(r.error(), r.systemError());
	  else
	    {
	      setError(KResolver::NoError);
	      results += r;
	    }
	}
    }

  finished();
  return true;
}

void KSrvResolverWorker::customEvent(TQCustomEvent*)
{
  dns = new TQDns(TQString::fromLatin1(encodedName), TQDns::Srv);
  TQObject::connect(dns, TQT_SIGNAL(resultsReady()), this, TQT_SLOT(dnsResultsReady()));
}

void KSrvResolverWorker::dnsResultsReady()
{
  (*sem)--;
  rawResults = dns->servers();
  dns->deleteLater();
  dns = 0L;
}

namespace KNetwork
{
  namespace Internal
  {

    void initSrvWorker() KDE_NO_EXPORT;
    void initSrvWorker()
    {
      if (getenv("KDE_NO_SRV") != NULL)
	return;
      
      KResolverWorkerFactoryBase::registerNewWorker(new KResolverWorkerFactory<KSrvResolverWorker>);
    }
    
  }
}

#include "ksrvresolverworker_p.moc"