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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
|
/* -*- C++ -*-
* Copyright (C) 2003,2004 Thiago Macieira <thiago.macieira@kdemail.net>
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#include <tqthread.h>
#include <tqmutex.h>
#include <tqstrlist.h>
#include <tqfile.h>
#include "kdebug.h"
#include "kglobal.h"
#include "kstandarddirs.h"
#include "kapplication.h"
#include "kresolver.h"
#include "ksocketaddress.h"
#include "kresolverstandardworkers_p.h"
struct hostent;
struct addrinfo;
using namespace KNetwork;
using namespace KNetwork::Internal;
static bool hasIPv6()
{
#ifndef AF_INET6
return false;
#else
if (getenv("KDE_NO_IPV6") != 0L)
return false;
int fd = ::socket(AF_INET6, SOCK_STREAM, 0);
if (fd == -1)
return false;
::close(fd);
return true;
#endif
}
// blacklist management
static TQMutex blacklistMutex; // KDE4: change to a QReadWriteLock
TQStringList KBlacklistWorker::blacklist;
void KBlacklistWorker::init()
{
// HACK!
// FIXME KDE4: How do I detect there is an instance, without triggering
// its creation or an assertion fault?
if (!KGlobal::_instance)
return;
static bool beenhere = false;
if (beenhere)
return;
beenhere = true;
loadBlacklist();
}
void KBlacklistWorker::loadBlacklist()
{
TQMutexLocker locker(&blacklistMutex);
TQStringList filelist = KGlobal::dirs()->findAllResources("config", "ipv6blacklist");
TQStringList::ConstIterator it = filelist.constBegin(),
end = filelist.constEnd();
for ( ; it != end; ++it)
{
// for each file, each line is a domainname to be blacklisted
TQFile f(*it);
if (!f.open(IO_ReadOnly))
continue;
TQTextStream stream(&f);
stream.setEncoding(TQTextStream::Latin1);
for (TQString line = stream.readLine(); !line.isNull();
line = stream.readLine())
{
if (line.isEmpty())
continue;
// make sure there are no surrounding whitespaces
// and that it starts with .
line = line.stripWhiteSpace();
if (line[0] != '.')
line.prepend('.');
blacklist.append(line.lower());
}
}
}
// checks the blacklist to see if the domain is listed
// it matches the domain ending part
bool KBlacklistWorker::isBlacklisted(const TQString& host)
{
KBlacklistWorker::init();
// empty hostnames cannot be blacklisted
if (host.isEmpty())
return false;
// KDE4: QLatin1String
TQString ascii = TQString::tqfromLatin1(KResolver::domainToAscii(host));
TQMutexLocker locker(&blacklistMutex);
// now tqfind out if this hostname is present
TQStringList::ConstIterator it = blacklist.constBegin(),
end = blacklist.constEnd();
for ( ; it != end; ++it)
if (ascii.endsWith(*it))
return true;
// no match:
return false;
}
bool KBlacklistWorker::preprocess()
{
if (isBlacklisted(nodeName()))
{
results.setError(KResolver::NoName);
finished();
return true;
}
return false;
}
bool KBlacklistWorker::run()
{
results.setError(KResolver::NoName);
finished();
return false; // resolution failure
}
namespace
{
/*
* Note on the use of the system resolver functions:
*
* In all cases, we prefer to use the new getaddrinfo(3) call. That means
* it will always be used if it is found.
*
* If it's not found, we have the option to use gethostbyname2_r,
* gethostbyname_r, gethostbyname2 and gethostbyname. If gethostbyname2_r
* is defined, we will use it.
*
* If it's not defined, we have to choose between the non-reentrant
* gethostbyname2 and the reentrant but IPv4-only gethostbyname_r:
* we will choose gethostbyname2 if AF_INET6 is defined.
*
* Lastly, gethostbyname will be used if nothing else is present.
*/
#ifndef HAVE_GETADDRINFO
# if defined(HAVE_GETHOSTBYNAME2_R)
# define USE_GETHOSTBYNAME2_R
# elif defined(HAVE_GETHOSTBYNAME_R) && (!defined(AF_INET6) || !defined(HAVE_GETHOSTBYNAME2))
# define USE_GETHOSTBYNAME_R
# elif defined(HAVE_GETHOSTBYNAME2)
# define USE_GETHOSTBYNAME2)
# else
# define USE_GETHOSTBYNAME
# endif
class GetHostByNameThread: public KResolverWorkerBase
{
public:
TQCString m_hostname; // might be different!
TQ_UINT16 m_port;
int m_scopeid;
int m_af;
KResolverResults& results;
GetHostByNameThread(const char * hostname, TQ_UINT16 port,
int scopeid, int af, KResolverResults* res) :
m_hostname(hostname), m_port(port), m_scopeid(scopeid), m_af(af),
results(*res)
{ }
~GetHostByNameThread()
{ }
virtual bool preprocess()
{ return true; }
virtual bool run();
void processResults(hostent* he, int my_h_errno);
};
bool GetHostByNameThread::run()
{
hostent *resultptr;
hostent my_results;
unsigned buflen = 1024;
int res;
int my_h_errno;
char *buf = 0L;
// qDebug("ResolveThread::run(): started threaded gethostbyname for %s (af = %d)",
// m_hostname.data(), m_af);
ResolverLocker resLock( this );
do
{
res = 0;
my_h_errno = HOST_NOT_FOUND;
// check blacklist
if (m_af != AF_INET &&
KBlacklistWorker::isBlacklisted(TQString::tqfromLatin1(m_hostname)))
break;
# ifdef USE_GETHOSTBYNAME2_R
buf = new char[buflen];
res = gethostbyname2_r(m_hostname, m_af, &my_results, buf, buflen,
&resultptr, &my_h_errno);
# elif defined(USE_GETHOSTBYNAME_R)
if (m_af == AF_INET)
{
buf = new char[buflen];
res = gethostbyname_r(m_hostname, &my_results, buf, buflen,
&resultptr, &my_h_errno);
}
else
resultptr = 0; // signal error
# elif defined(USE_GETHOSTBYNAME2)
// must lock mutex
resultptr = gethostbyname2(m_hostname, m_af);
my_h_errno = h_errno;
# else
if (m_af == AF_INET)
{
// must lock mutex
resultptr = gethostbyname(m_hostname);
my_h_errno = h_errno;
}
else
resultptr = 0;
# endif
if (resultptr != 0L)
my_h_errno = 0;
// qDebug("GetHostByNameThread::run(): gethostbyname for %s (af = %d) returned: %d",
// m_hostname.data(), m_af, my_h_errno);
if (res == ERANGE)
{
// Enlarge the buffer
buflen += 1024;
delete [] buf;
buf = new char[buflen];
}
if ((res == ERANGE || my_h_errno != 0) && checkResolver())
{
// resolver needs updating, so we might as well do it now
resLock.openClose();
}
}
while (res == ERANGE);
processResults(resultptr, my_h_errno);
delete [] buf;
finished();
return results.error() == KResolver::NoError;
}
void GetHostByNameThread::processResults(hostent *he, int herrno)
{
if (herrno)
{
qDebug("KStandardWorker::processResults: got error %d", herrno);
switch (herrno)
{
case HOST_NOT_FOUND:
results.setError(KResolver::NoName);
return;
case TRY_AGAIN:
results.setError(KResolver::TryAgain);
return;
case NO_RECOVERY:
results.setError(KResolver::NonRecoverable);
return;
case NO_ADDRESS:
results.setError(KResolver::NoName);
return;
default:
results.setError(KResolver::UnknownError);
return;
}
}
else if (he == 0L)
{
results.setError(KResolver::NoName);
return; // this was an error
}
// clear any errors
setError(KResolver::NoError);
results.setError(KResolver::NoError);
// we process results in the reverse order
// that is, we prepend each result to the list of results
int proto = protocol();
int socktype = socketType();
if (socktype == 0)
socktype = SOCK_STREAM; // default
TQString canon = KResolver::domainToUnicode(TQString::tqfromLatin1(he->h_name));
KInetSocketAddress sa;
sa.setPort(m_port);
if (he->h_addrtype != AF_INET)
sa.setScopeId(m_scopeid); // this will also change the socket into IPv6
for (int i = 0; he->h_addr_list[i]; i++)
{
sa.setHost(KIpAddress(he->h_addr_list[i], he->h_addrtype == AF_INET ? 4 : 6));
results.prepend(KResolverEntry(sa, socktype, proto, canon, m_hostname));
// qDebug("KStandardWorker::processResults: adding %s", sa.toString().latin1());
}
// qDebug("KStandardWorker::processResults: added %d entries", i);
}
#else // HAVE_GETADDRINFO
class GetAddrInfoThread: public KResolverWorkerBase
{
public:
TQCString m_node;
TQCString m_serv;
int m_af;
int m_flags;
KResolverResults& results;
GetAddrInfoThread(const char* node, const char* serv, int af, int flags,
KResolverResults* res) :
m_node(node), m_serv(serv), m_af(af), m_flags(flags), results(*res)
{ }
~GetAddrInfoThread()
{ }
virtual bool preprocess()
{ return true; }
virtual bool run();
void processResults(addrinfo* ai, int ret_code, KResolverResults& rr);
};
bool GetAddrInfoThread::run()
{
// check blacklist
if ((m_af != AF_INET && m_af != AF_UNSPEC) &&
KBlacklistWorker::isBlacklisted(TQString::tqfromLatin1(m_node)))
{
results.setError(KResolver::NoName);
finished();
return false; // failed
}
do
{
ResolverLocker resLock( this );
// process hints
addrinfo hint;
memset(&hint, 0, sizeof(hint));
hint.ai_family = m_af;
hint.ai_socktype = socketType();
hint.ai_protocol = protocol();
if (hint.ai_socktype == 0)
hint.ai_socktype = SOCK_STREAM; // default
if (m_flags & KResolver::Passive)
hint.ai_flags |= AI_PASSIVE;
if (m_flags & KResolver::CanonName)
hint.ai_flags |= AI_CANONNAME;
# ifdef AI_NUMERICHOST
if (m_flags & KResolver::NoResolve)
hint.ai_flags |= AI_NUMERICHOST;
# endif
# ifdef AI_ADDRCONFIG
hint.ai_flags |= AI_ADDRCONFIG;
# endif
// now we do the blocking processing
if (m_node.isEmpty())
m_node = "*";
addrinfo *result;
int res = getaddrinfo(m_node, m_serv, &hint, &result);
// kdDebug(179) << k_funcinfo << "getaddrinfo(\""
// << m_node << "\", \"" << m_serv << "\", af="
// << m_af << ") returned " << res << endl;
if (res != 0)
{
if (checkResolver())
{
// resolver requires reinitialisation
resLock.openClose();
continue;
}
switch (res)
{
case EAI_BADFLAGS:
results.setError(KResolver::BadFlags);
break;
#ifdef EAI_NODATA
// In some systems, EAI_NODATA was #define'd to EAI_NONAME which would break this case.
#if EAI_NODATA != EAI_NONAME
case EAI_NODATA: // it was removed in RFC 3493
#endif
#endif
case EAI_NONAME:
results.setError(KResolver::NoName);
break;
case EAI_AGAIN:
results.setError(KResolver::TryAgain);
break;
case EAI_FAIL:
results.setError(KResolver::NonRecoverable);
break;
case EAI_FAMILY:
results.setError(KResolver::UnsupportedFamily);
break;
case EAI_SOCKTYPE:
results.setError(KResolver::UnsupportedSocketType);
break;
case EAI_SERVICE:
results.setError(KResolver::UnsupportedService);
break;
case EAI_MEMORY:
results.setError(KResolver::Memory);
break;
case EAI_SYSTEM:
results.setError(KResolver::SystemError, errno);
break;
default:
results.setError(KResolver::UnknownError, errno);
break;
}
finished();
return false; // failed
}
// if we are here, lookup succeeded
TQString canon;
const char *previous_canon = 0L;
for (addrinfo* p = result; p; p = p->ai_next)
{
// cache the last canon name to avoid doing the ToUnicode processing unnecessarily
if ((previous_canon && !p->ai_canonname) ||
(!previous_canon && p->ai_canonname) ||
(p->ai_canonname != previous_canon &&
strcmp(p->ai_canonname, previous_canon) != 0))
{
canon = KResolver::domainToUnicode(TQString::fromAscii(p->ai_canonname));
previous_canon = p->ai_canonname;
}
results.append(KResolverEntry(p->ai_addr, p->ai_addrlen, p->ai_socktype,
p->ai_protocol, canon, m_node));
}
freeaddrinfo(result);
results.setError(KResolver::NoError);
finished();
return results.error() == KResolver::NoError;
}
while (true);
}
#endif // HAVE_GETADDRINFO
} // namespace
bool KStandardWorker::sanityCheck()
{
// check that the requested values are sensible
if (!nodeName().isEmpty())
{
TQString node = nodeName();
if (node.tqfind('%') != -1)
node.truncate(node.tqfind('%'));
if (node.isEmpty() || node == TQString::tqfromLatin1("*") ||
node == TQString::tqfromLatin1("localhost"))
m_encodedName.truncate(0);
else
{
m_encodedName = KResolver::domainToAscii(node);
if (m_encodedName.isNull())
{
qDebug("could not encode hostname '%s' (UTF-8)", node.utf8().data());
setError(KResolver::NoName);
return false; // invalid hostname!
}
// qDebug("Using encoded hostname '%s' for '%s' (UTF-8)", m_encodedName.data(),
// node.utf8().data());
}
}
else
m_encodedName.truncate(0); // just to be sure, but it should be clear already
if (protocol() == -1)
{
setError(KResolver::NonRecoverable);
return false; // user passed invalid protocol name
}
return true; // it's sane
}
bool KStandardWorker::resolveScopeId()
{
// we must test the original name, not the encoded one
scopeid = 0;
int pos = nodeName().tqfindRev('%');
if (pos == -1)
return true;
TQString scopename = nodeName().mid(pos + 1);
bool ok;
scopeid = scopename.toInt(&ok);
if (!ok)
{
// it's not a number
// therefore, it's an interface name
#ifdef HAVE_IF_NAMETOINDEX
scopeid = if_nametoindex(scopename.latin1());
#else
scopeid = 0;
#endif
}
return true;
}
bool KStandardWorker::resolveService()
{
// tqfind the service first
bool ok;
port = serviceName().toUInt(&ok);
if (!ok)
{
// service name does not contain a port number
// must be a name
if (serviceName().isEmpty() || serviceName().compare(TQString::tqfromLatin1("*")) == 0)
port = 0;
else
{
// it's a name. We need the protocol name in order to lookup.
TQCString protoname = protocolName();
if (protoname.isEmpty() && protocol())
{
protoname = KResolver::protocolName(protocol()).first();
// if it's still empty...
if (protoname.isEmpty())
{
// lookup failed!
setError(KResolver::NoName);
return false;
}
}
else
protoname = "tcp";
// it's not, so we can do a port lookup
int result = KResolver::servicePort(serviceName().latin1(), protoname);
if (result == -1)
{
// lookup failed!
setError(KResolver::NoName);
return false;
}
// it worked, we have a port number
port = (TQ_UINT16)result;
}
}
// we found a port
return true;
}
KResolver::ErrorCodes KStandardWorker::addUnix()
{
// before trying to add, see if the user wants Unix sockets
if ((familyMask() & KResolver::UnixFamily) == 0)
// no, Unix sockets are not wanted
return KResolver::UnsupportedFamily;
// now check if the requested data are good for a Unix socket
if (!m_encodedName.isEmpty())
return KResolver::AddrFamily; // non local hostname
if (protocol() || protocolName())
return KResolver::BadFlags; // cannot have Unix sockets with protocols
TQString pathname = serviceName();
if (pathname.isEmpty())
return KResolver::NoName;; // no path?
if (pathname[0] != '/')
// non absolute pathname
// put it in /tmp
pathname.prepend("/tmp/");
// qDebug("QNoResolveWorker::addUnix(): adding Unix socket for %s", pathname.local8Bit().data());
KUnixSocketAddress sa(pathname);
int socktype = socketType();
if (socktype == 0)
socktype = SOCK_STREAM; // default
results.append(KResolverEntry(sa, socktype, 0));
setError(KResolver::NoError);
return KResolver::NoError;
}
bool KStandardWorker::resolveNumerically()
{
// if the NoResolve flag is active, our result from this point forward
// will always be true, even if the resolution failed.
// that indicates that our result is authoritative.
bool wantV4 = familyMask() & KResolver::IPv4Family,
wantV6 = familyMask() & KResolver::IPv6Family;
if (!wantV6 && !wantV4)
// no Internet address is wanted!
return (flags() & KResolver::NoResolve);
// now try to tqfind results
if (!resolveScopeId() || !resolveService())
return (flags() & KResolver::NoResolve);
// we have scope IDs and port numbers
// now try to resolve the hostname numerically
KInetSocketAddress sa;
setError(KResolver::NoError);
sa.setHost(KIpAddress(TQString::tqfromLatin1(m_encodedName)));
// if it failed, the length was reset to 0
bool ok = sa.length() != 0;
sa.setPort(port);
if (sa.ipVersion() == 6)
sa.setScopeId(scopeid);
int proto = protocol();
int socktype = socketType();
if (socktype == 0)
socktype = SOCK_STREAM;
if (ok)
{
// the given hostname was successfully converted to an IP address
// check if the user wanted this kind of address
if ((sa.ipVersion() == 4 && wantV4) ||
(sa.ipVersion() == 6 && wantV6))
results.append(KResolverEntry(sa, socktype, proto));
else
{
// Note: the address *IS* a numeric IP
// but it's not of the kind the user asked for
//
// that means that it cannot be a Unix socket (because it's an IP)
// and that means that no resolution will tell us otherwise
//
// This is a failed resolution
setError(KResolver::AddrFamily);
return true;
}
}
else if (m_encodedName.isEmpty())
{
// user wanted localhost
if (flags() & KResolver::Passive)
{
if (wantV6)
{
sa.setHost(KIpAddress::anyhostV6);
results.append(KResolverEntry(sa, socktype, proto));
}
if (wantV4)
{
sa.setHost(KIpAddress::anyhostV4);
results.append(KResolverEntry(sa, socktype, proto));
}
}
else
{
if (wantV6)
{
sa.setHost(KIpAddress::localhostV6);
results.append(KResolverEntry(sa, socktype, proto));
}
if (wantV4)
{
sa.setHost(KIpAddress::localhostV4);
results.append(KResolverEntry(sa, socktype, proto));
}
}
ok = true;
}
else
{
// probably bad flags, since the address is not convertible without
// resolution
setError(KResolver::BadFlags);
ok = false;
}
return ok || (flags() & KResolver::NoResolve);
}
bool KStandardWorker::preprocess()
{
// check sanity
if (!sanityCheck())
return false;
// this worker class can only handle known families
if (familyMask() & KResolver::UnknownFamily)
{
setError(KResolver::UnsupportedFamily);
return false; // we don't know about this
}
// check the socket types
if (socketType() != SOCK_STREAM && socketType() != SOCK_DGRAM && socketType() != 0)
{
setError(KResolver::UnsupportedSocketType);
return false;
}
// check if we can resolve all numerically
// resolveNumerically always returns true if the NoResolve flag is set
if (resolveNumerically() || m_encodedName.isEmpty())
{
// indeed, we have resolved numerically
setError(addUnix());
if (results.count())
setError(KResolver::NoError);
finished();
return true;
}
// check if the user wants something we know about
#ifdef AF_INET6
# define tqmask (KResolver::IPv6Family | KResolver::IPv4Family | KResolver::UnixFamily)
#else
# define tqmask (KResolver::IPv4Family | KResolver::UnixFamily)
#endif
if ((familyMask() & tqmask) == 0)
// errr... nothing we know about
return false;
#undef tqmask
return true; // it's ok
}
bool KStandardWorker::run()
{
#ifndef HAVE_GETADDRINFO
// check the scope id first
// since most of the resolutions won't have a scope id, this should be fast
// and we won't have wasted time on services if this fails
if (!resolveScopeId())
return false;
// resolve the service now, before entering the blocking operation
if (!resolveService())
return false;
#endif
// good
// now we need the hostname
setError(KResolver::NoName);
// these are the family types that we know of
struct
{
KResolver::SocketFamilies tqmask;
int af;
} families[] = { { KResolver::IPv4Family, AF_INET }
#ifdef AF_INET6
, { KResolver::IPv6Family, AF_INET6 }
#endif
};
int familyCount = sizeof(families)/sizeof(families[0]);
bool skipIPv6 = !hasIPv6();
resultList.setAutoDelete(true);
for (int i = 0; i < familyCount; i++)
if (familyMask() & families[i].tqmask)
{
#ifdef AF_INET6
if (skipIPv6 && families[i].af == AF_INET6)
continue;
#endif
KResolverWorkerBase *worker;
KResolverResults *res = new KResolverResults;
resultList.append(res);
#ifdef HAVE_GETADDRINFO
worker = new GetAddrInfoThread(m_encodedName,
serviceName().latin1(),
families[i].af, flags(), res);
#else
worker = new GetHostByNameThread(m_encodedName, port, scopeid,
families[i].af, res);
#endif
enqueue(worker);
}
// not finished
return true;
}
bool KStandardWorker::postprocess()
{
if (results.count())
return true; // no need
// now copy over what we need from the underlying results
// start backwards because IPv6 was launched later (if at all)
if (resultList.isEmpty())
{
results.setError(KResolver::NoName);
return true;
}
KResolverResults *rr = resultList.last();
while (rr)
{
if (!rr->isEmpty())
{
results.setError(KResolver::NoError);
KResolverResults::Iterator it = rr->begin();
for ( ; it != rr->end(); ++it)
results.append(*it);
}
else if (results.isEmpty())
// this generated an error
// copy the error code over
setError(rr->error(), rr->systemError());
rr = resultList.prev();
}
resultList.clear();
return true;
}
#ifdef HAVE_GETADDRINFO
KGetAddrinfoWorker::~KGetAddrinfoWorker()
{
}
bool KGetAddrinfoWorker::preprocess()
{
// getaddrinfo(3) can always handle any kind of request that makes sense
if (!sanityCheck())
return false;
if (flags() & KResolver::NoResolve)
// oops, numeric resolution?
return run();
return true;
}
bool KGetAddrinfoWorker::run()
{
// make an AF_UNSPEC getaddrinfo(3) call
GetAddrInfoThread worker(m_encodedName, serviceName().latin1(),
AF_UNSPEC, flags(), &results);
if (!worker.run())
{
if (wantThis(AF_UNIX))
{
if (addUnix() == KResolver::NoError)
setError(KResolver::NoError);
}
else
setError(worker.results.error(), worker.results.systemError());
return false;
}
// The worker has finished working
// now copy over only what we may want
// keep track of any Unix-domain sockets
bool seen_unix = false;
KResolverResults::Iterator it = results.begin();
for ( ; it != results.end(); )
{
if ((*it).family() == AF_UNIX)
seen_unix = true;
if (!wantThis((*it).family()))
it = results.remove(it);
else
++it;
}
if (!seen_unix)
addUnix();
finished();
return true;
}
bool KGetAddrinfoWorker::wantThis(int family)
{
// tells us if the user wants a socket of this family
#ifdef AF_INET6
if (family == AF_INET6 && familyMask() & KResolver::IPv6Family)
return true;
#endif
if (family == AF_INET && familyMask() & KResolver::IPv4Family)
return true;
if (family == AF_UNIX && familyMask() & KResolver::UnixFamily)
return true;
// it's not a family we know about...
if (familyMask() & KResolver::UnknownFamily)
return true;
return false;
}
#endif
void KNetwork::Internal::initStandardWorkers()
{
//KResolverWorkerFactoryBase::registerNewWorker(new KResolverWorkerFactory<KBlacklistWorker>);
KResolverWorkerFactoryBase::registerNewWorker(new KResolverWorkerFactory<KStandardWorker>);
#ifdef HAVE_GETADDRINFO
KResolverWorkerFactoryBase::registerNewWorker(new KResolverWorkerFactory<KGetAddrinfoWorker>);
#endif
}
|