• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
  • network
ksrvresolverworker.cpp
1 /*
2  * Copyright (C) 2005 Thiago Macieira <thiago@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include <config.h>
21 
22 #include "ksrvresolverworker_p.h"
23 
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <stdlib.h>
27 
28 #include <tqapplication.h>
29 #include <tqevent.h>
30 
31 using namespace KNetwork;
32 using namespace KNetwork::Internal;
33 
34 namespace
35 {
36  struct KSrvStartEvent: public TQCustomEvent
37  {
38  inline KSrvStartEvent() : TQCustomEvent(TQEvent::User) { }
39  };
40 }
41 
42 static void sortPriorityClass(KSrvResolverWorker::PriorityClass&)
43 {
44  // do nothing
45 }
46 
47 bool KSrvResolverWorker::preprocess()
48 {
49  // check if the resolver flags mention SRV-based lookup
50  if ((flags() & (KResolver::NoSrv | KResolver::UseSrv)) != KResolver::UseSrv)
51  return false;
52 
53  TQString node = nodeName();
54  if (node.find('%') != -1)
55  node.truncate(node.find('%'));
56 
57  if (node.isEmpty() || node == TQString::fromLatin1("*") ||
58  node == TQString::fromLatin1("localhost"))
59  return false; // empty == localhost
60 
61  encodedName = KResolver::domainToAscii(node);
62  if (encodedName.isNull())
63  return false;
64 
65  // we only work with Internet-based families
66  if ((familyMask() & KResolver::InternetFamily) == 0)
67  return false;
68 
69  // SRV-based resolution only works if the service isn't numeric
70  bool ok;
71  serviceName().toUInt(&ok);
72  if (ok)
73  return false; // it is numeric
74 
75  // check the protocol for something we know
76  TQCString protoname;
77  int sockettype = socketType();
78  if (!protocolName().isEmpty())
79  protoname = protocolName();
80  else if (protocol() != 0)
81  {
82  TQStrList names = KResolver::protocolName(protocol());
83  names.setAutoDelete(true);
84  if (names.isEmpty())
85  return false;
86 
87  protoname = "_";
88  protoname += names.at(0);
89  }
90  else if (sockettype == SOCK_STREAM || sockettype == 0)
91  protoname = "_tcp";
92  else if (sockettype == SOCK_DGRAM)
93  protoname = "_udp";
94  else
95  return false; // unknown protocol and socket type
96 
97  encodedName.prepend(".");
98  encodedName.prepend(protoname);
99  encodedName.prepend(".");
100  encodedName.prepend(serviceName().latin1());
101  encodedName.prepend("_");
102 
103  // looks like something we could process
104  return true;
105 }
106 
107 bool KSrvResolverWorker::run()
108 {
109  sem = new TQSemaphore(1);
110  // zero out
111  sem->tryAccess(sem->available());
112 
113  TQApplication::postEvent(this, new KSrvStartEvent);
114 
115  // block
116  (*sem)++;
117  delete sem;
118  sem = 0L;
119 
120  if (rawResults.isEmpty())
121  {
122  // normal lookup
123  KResolver *r = new KResolver(nodeName(), serviceName());
124  r->setFlags(flags() | KResolver::NoSrv);
125  r->setFamily(familyMask());
126  r->setSocketType(socketType());
127  r->setProtocol(protocol(), protocolName());
128 
129  enqueue(r);
130 
131  Entry e;
132  PriorityClass cl;
133  e.resolver = r;
134  cl.entries.append(e);
135  myResults[0] = cl;
136 
137  return true;
138  }
139  else if (rawResults.count() == 1 && rawResults.first().name == ".")
140  {
141  // no name
142  setError(KResolver::NoName);
143  finished();
144  return true;
145  }
146  else
147  {
148  // now process the results
149  TQValueList<TQDns::Server>::ConstIterator it = rawResults.begin();
150  while (it != rawResults.end())
151  {
152  const TQDns::Server& srv = *it;
153  PriorityClass& r = myResults[srv.priority];
154  r.totalWeight += srv.weight;
155 
156  Entry e;
157  e.name = srv.name;
158  e.port = srv.port;
159  e.weight = srv.weight;
160  e.resolver = 0L;
161  r.entries.append(e);
162 
163  ++it;
164  }
165  rawResults.clear(); // free memory
166 
167  Results::Iterator mapit;
168  for (mapit = myResults.begin(); mapit != myResults.end(); ++mapit)
169  {
170  // sort the priority
171  sortPriorityClass(*mapit);
172 
173  TQValueList<Entry>& entries = (*mapit).entries;
174 
175  // start the resolvers
176  for (TQValueList<Entry>::Iterator it = entries.begin();
177  it != entries.end(); ++it)
178  {
179  Entry &e = *it;
180 
181  KResolver* r = new KResolver(e.name, TQString::number(e.port));
182  r->setFlags(flags() | KResolver::NoSrv);
183  r->setFamily(familyMask());
184  r->setSocketType(socketType());
185  r->setProtocol(protocol(), protocolName());
186 
187  enqueue(r);
188  e.resolver = r;
189  }
190  }
191 
192  return true;
193  }
194 }
195 
196 bool KSrvResolverWorker::postprocess()
197 {
198  setError(KResolver::NoName);
199  if (myResults.isEmpty())
200  return false;
201 
202  Results::Iterator mapit, mapend;
203  for (mapit = myResults.begin(), mapend = myResults.end();
204  mapit != mapend; ++mapit)
205  {
206  TQValueList<Entry>::Iterator it = (*mapit).entries.begin(),
207  end = (*mapit).entries.end();
208  for ( ; it != end; ++it)
209  {
210  Entry &e = *it;
211  KResolverResults r = e.resolver->results();
212  if (r.isEmpty() && results.isEmpty())
213  setError(r.error(), r.systemError());
214  else
215  {
216  setError(KResolver::NoError);
217  results += r;
218  }
219  }
220  }
221 
222  finished();
223  return true;
224 }
225 
226 void KSrvResolverWorker::customEvent(TQCustomEvent*)
227 {
228  dns = new TQDns(TQString::fromLatin1(encodedName), TQDns::Srv);
229  TQObject::connect(dns, TQT_SIGNAL(resultsReady()), this, TQT_SLOT(dnsResultsReady()));
230 }
231 
232 void KSrvResolverWorker::dnsResultsReady()
233 {
234  (*sem)--;
235  rawResults = dns->servers();
236  dns->deleteLater();
237  dns = 0L;
238 }
239 
240 namespace KNetwork
241 {
242  namespace Internal
243  {
244 
245  void initSrvWorker() KDE_NO_EXPORT;
246  void initSrvWorker()
247  {
248  if (getenv("TDE_NO_SRV") != NULL)
249  return;
250 
251  KResolverWorkerFactoryBase::registerNewWorker(new KResolverWorkerFactory<KSrvResolverWorker>);
252  }
253 
254  }
255 }
256 
257 #include "ksrvresolverworker_p.moc"
KNetwork::Internal
Definition: kresolver.h:47
KNetwork::KResolver::setFamily
void setFamily(int families)
Sets the allowed socket families.
Definition: kresolver.cpp:401
KNetwork::KResolver
Name and service resolution class.
Definition: kresolver.h:295
KNetwork::KResolver::setProtocol
void setProtocol(int protonum, const char *name=0L)
Sets the protocol we want.
Definition: kresolver.cpp:421
KNetwork::KResolverResults
Name and service resolution results.
Definition: kresolver.h:197
KNetwork::KResolver::results
KResolverResults results() const
Retrieves the results of this resolution.
Definition: kresolver.cpp:520
KNetwork
A namespace to store all networking-related (socket) classes.
Definition: kbufferedsocket.h:36
TDEStdAccel::end
const TDEShortcut & end()
Goto end of the document.
Definition: tdestdaccel.cpp:289
KNetwork::KResolverResults::systemError
int systemError() const
Retrieves the system error code, if any.
Definition: kresolver.cpp:253
KNetwork::KResolver::setSocketType
void setSocketType(int type)
Sets the socket type we want.
Definition: kresolver.cpp:411
KNetwork::KResolver::domainToAscii
static TQCString domainToAscii(const TQString &unicodeDomain)
Returns the domain name in an ASCII Compatible Encoding form, suitable for DNS lookups.
Definition: kresolver.cpp:1035
KNetwork::KResolver::setFlags
int setFlags(int flags)
Sets the flags.
Definition: kresolver.cpp:389
KNetwork::KResolverResults::error
int error() const
Retrieves the error code associated with this resolution.
Definition: kresolver.cpp:247
KNetwork::KResolver::protocolName
static TQStrList protocolName(int protonum)
Resolves a protocol number to its names.
Definition: kresolver.cpp:614

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.