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
|
//=============================================================================
//
// File : kvi_dns.cpp
// Creation date : Sat Jul 21 2000 17:19:31 by Szymon Stefanek
//
// This file is part of the KVirc irc client distribution
// Copyright (C) 2000-2007 Szymon Stefanek (pragma at kvirc dot net)
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your opinion) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#define __KVILIB__
#include "kvi_dns.h"
#include "kvi_error.h"
#include "kvi_netutils.h"
#include <errno.h>
#ifdef COMPILE_ON_WINDOWS
#include <winsock2.h>
#ifdef COMPILE_IPV6_SUPPORT
#ifdef WIN2K
#include <ws2ip6.h>
#else
#include <ws2tcpip.h>
//#include <tpipv6.h>
#endif
#endif
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#endif
// this is for FreeBSD
#ifndef EAI_ADDRFAMILY
#define EAI_ADDRFAMILY EAI_FAMILY
#endif
#ifndef EAI_NODATA
#define EAI_NODATA 0
#endif
KviDnsResult::KviDnsResult()
{
m_iError = KviError_success;
m_pHostnameList = new KviPointerList<TQString>;
m_pHostnameList->setAutoDelete(true);
m_pIpAddressList = new KviPointerList<TQString>;
m_pIpAddressList->setAutoDelete(true);
}
KviDnsResult::~KviDnsResult()
{
delete m_pHostnameList;
delete m_pIpAddressList;
}
void KviDnsResult::appendHostname(const TQString &host)
{
m_pHostnameList->append(new TQString(host));
}
void KviDnsResult::appendAddress(const TQString &addr)
{
m_pIpAddressList->append(new TQString(addr));
}
KviDnsThread::KviDnsThread(KviDns * pDns)
{
m_pParentDns = pDns;
}
KviDnsThread::~KviDnsThread()
{
}
int KviDnsThread::translateDnsError(int iErr)
{
#if defined(COMPILE_IPV6_SUPPORT) || !defined(COMPILE_ON_WINDOWS)
switch(iErr)
{
case EAI_FAMILY: return KviError_unsupportedAddressFamily; break;
#if !defined(COMPILE_ON_WINDOWS) && defined(EAI_ADDRFAMILY) && (EAI_ADDRFAMILY != EAI_FAMILY)
case EAI_ADDRFAMILY: return KviError_unsupportedAddressFamily; break;
#endif
// NOT FreeBSD ARE WE?
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
// YARR
case EAI_NODATA: return KviError_validNameButNoIpAddress; break;
#endif
case EAI_FAIL: return KviError_unrecoverableNameserverError; break;
case EAI_AGAIN: return KviError_dnsTemporaneousFault; break;
// this should never happen
case EAI_BADFLAGS: return KviError_dnsInternalErrorBadFlags; break;
case EAI_MEMORY: return KviError_dnsInternalErrorOutOfMemory; break;
// got this when experimenting with protocols
case EAI_SERVICE: return KviError_dnsInternalErrorServiceNotSupported; break;
#ifndef COMPILE_ON_WINDOWS
case EAI_NONAME: return KviError_dnsNoName; break;
#endif
// got this when experimenting with protocols
case EAI_SOCKTYPE: return KviError_dnsInternalErrorUnsupportedSocketType; break;
#ifndef COMPILE_ON_WINDOWS
case EAI_SYSTEM: return -errno;
#endif
}
#endif
return KviError_dnsQueryFailed;
}
void KviDnsThread::postDnsError(KviDnsResult * dns,int iErr)
{
dns->setError(iErr);
KviThreadDataEvent<KviDnsResult> * e = new KviThreadDataEvent<KviDnsResult>(KVI_DNS_THREAD_EVENT_DATA);
e->setData(dns);
postEvent(m_pParentDns,e);
}
void KviDnsThread::run()
{
KviDnsResult * dns = new KviDnsResult();
dns->setQuery(m_szQuery);
if(m_szQuery.isEmpty())
{
postDnsError(dns,KviError_noHostToResolve);
return;
}
#ifndef COMPILE_IPV6_SUPPORT
if(m_queryType != KviDns::IpV4)
{
if(m_queryType == KviDns::IpV6)
{
postDnsError(dns,KviError_noIpV6Support);
return;
}
m_queryType = KviDns::IpV4;
}
#endif
#if defined(COMPILE_ON_WINDOWS) && !defined(COMPILE_IPV6_SUPPORT)
if(m_queryType == KviDns::IpV6)
{
postDnsError(dns,KviError_noIpV6Support);
return;
}
// gethostbyaddr and gethostbyname are thread-safe on Windoze
struct in_addr inAddr;
struct hostent *pHostEntry = 0;
// DIE DIE!....I hope that this stuff will disappear sooner or later :)
if(KviNetUtils::stringIpToBinaryIp(m_szQuery,&inAddr))
{
pHostEntry = gethostbyaddr((const char *)&inAddr,sizeof(inAddr),AF_INET);
} else {
pHostEntry = gethostbyname(m_szQuery);
}
if(!pHostEntry)
{
switch(h_errno)
{
case HOST_NOT_FOUND: dns->setError(KviError_hostNotFound); break;
case NO_ADDRESS: dns->setError(KviError_validNameButNoIpAddress); break;
case NO_RECOVERY: dns->setError(KviError_unrecoverableNameserverError); break;
case TRY_AGAIN: dns->setError(KviError_dnsTemporaneousFault); break;
default: dns->setError(KviError_dnsQueryFailed); break;
}
} else {
dns->appendHostname(pHostEntry->h_name);
TQString szIp;
KviNetUtils::binaryIpToStringIp(* ((struct in_addr*)(pHostEntry->h_addr)),szIp);
dns->appendAddress(szIp);
int idx = 1;
while(pHostEntry->h_addr_list[idx])
{
TQString tmp;
KviNetUtils::binaryIpToStringIp(* ((struct in_addr*)(pHostEntry->h_addr_list[idx])),tmp);
if(tmp.hasData())dns->appendAddress(tmp);
++idx;
}
if(pHostEntry->h_aliases[0])
{
dns->appendHostname(TQString::fromUtf8(pHostEntry->h_aliases[0]));
if(pHostEntry->h_aliases[1])dns->appendHostname(TQString::fromUtf8(pHostEntry->h_aliases[1]));
}
}
#else //!COMPILE_ON_WINDOWS || COMPILE_IPV6_SUPPORT
int retVal;
//#ifdef HAVE_GETNAMEINFO
struct sockaddr_in ipv4Addr;
#ifdef COMPILE_IPV6_SUPPORT
struct sockaddr_in6 ipv6Addr;
bool bIsIpV6Ip = false;
#endif
bool bIsIpV4Ip = KviNetUtils::stringIpToBinaryIp(m_szQuery,(struct in_addr *)&(ipv4Addr.sin_addr));
#ifdef COMPILE_IPV6_SUPPORT
if(!bIsIpV4Ip)bIsIpV6Ip = KviNetUtils::stringIpToBinaryIp_V6(m_szQuery,(struct in6_addr *)&(ipv6Addr.sin6_addr));
#endif
//#ifdef HAVE_GETNAMEINFO
#ifdef COMPILE_IPV6_SUPPORT
if(bIsIpV4Ip || bIsIpV6Ip)
{
#else
if(bIsIpV4Ip)
{
#endif
// use getnameinfo...
char retname[1025]; // should be enough....
#ifdef COMPILE_IPV6_SUPPORT
if(bIsIpV4Ip)
{
#endif
ipv4Addr.sin_family = AF_INET;
ipv4Addr.sin_port = 0;
// NI_NAMEREQD as last param ?
retVal = getnameinfo((struct sockaddr *)&ipv4Addr,sizeof(ipv4Addr),retname,1025,0,0,NI_NAMEREQD);
#ifdef COMPILE_IPV6_SUPPORT
} else {
ipv6Addr.sin6_family = AF_INET6;
ipv6Addr.sin6_port = 0;
retVal = getnameinfo((struct sockaddr *)&ipv6Addr,sizeof(ipv6Addr),retname,1025,0,0,NI_NAMEREQD);
}
#endif
if(retVal != 0)dns->setError(translateDnsError(retVal));
else {
dns->appendHostname(retname);
dns->appendAddress(m_szQuery);
}
} else {
//#endif //HAVE_GETNAMEINFO
//#ifdef COMPILE_IPV6_SUPPORT
// struct in6_addr in6Addr;
//#endif
struct addrinfo * pRet = 0;
struct addrinfo * pNext;
struct addrinfo hints;
hints.ai_flags = 0; //AI_CANONNAME; <-- for IPV6 it makes cannoname to point to the IP address!
#ifdef COMPILE_IPV6_SUPPORT
hints.ai_family = (m_queryType == KviDns::IpV6) ? PF_INET6 : ((m_queryType == KviDns::IpV4) ? PF_INET : PF_UNSPEC);
#else
hints.ai_family = PF_INET;
#endif
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_canonname = 0;
hints.ai_addr = 0;
hints.ai_next = 0;
retVal = getaddrinfo(KviTQString::toUtf8(m_szQuery).data(),0,&hints,&pRet);
if(retVal != 0)dns->setError(translateDnsError(retVal));
else {
dns->appendHostname(pRet->ai_canonname ? TQString::fromUtf8(pRet->ai_canonname) : m_szQuery);
TQString szIp;
#ifdef COMPILE_IPV6_SUPPORT
if(pRet->ai_family == PF_INET6)KviNetUtils::binaryIpToStringIp_V6(((sockaddr_in6 *)(pRet->ai_addr))->sin6_addr,szIp);
else {
#endif
KviNetUtils::binaryIpToStringIp(((sockaddr_in *)(pRet->ai_addr))->sin_addr,szIp);
#ifdef COMPILE_IPV6_SUPPORT
}
#endif
dns->appendAddress(szIp);
pNext = pRet->ai_next;
while(pNext)
{
TQString tmp;
#ifdef COMPILE_IPV6_SUPPORT
if(pNext->ai_family == PF_INET6)KviNetUtils::binaryIpToStringIp_V6(((sockaddr_in6 *)(pNext->ai_addr))->sin6_addr,tmp);
else {
#endif
KviNetUtils::binaryIpToStringIp(((sockaddr_in *)(pNext->ai_addr))->sin_addr,tmp);
#ifdef COMPILE_IPV6_SUPPORT
}
#endif
if(!tmp.isEmpty())dns->appendAddress(tmp);
if(pNext->ai_canonname)
{
// FIXME: only of not equal to other names ?
dns->appendHostname(TQString::fromUtf8(pNext->ai_canonname));
}
pNext = pNext->ai_next;
}
}
if(pRet)freeaddrinfo(pRet);
//#ifdef HAVE_GETNAMEINFO
}
//#endif //HAVE_GETNAMEINFO
#endif // !COMPILE_ON_WINDOWS
KviThreadDataEvent<KviDnsResult> * e = new KviThreadDataEvent<KviDnsResult>(KVI_DNS_THREAD_EVENT_DATA);
e->setData(dns);
postEvent(m_pParentDns,e);
}
KviDns::KviDns()
: TQObject()
{
m_pSlaveThread = new KviDnsThread(this);
m_pDnsResult = new KviDnsResult();
m_pAuxData = 0;
m_state = Idle;
}
KviDns::~KviDns()
{
if(m_pSlaveThread)delete m_pSlaveThread; // will eventually terminate it (but it will also block us!!!)
KviThreadManager::killPendingEvents(this);
if(m_pDnsResult)delete m_pDnsResult;
if(m_pAuxData)tqDebug("You're leaking memory man! m_pAuxData is non 0!");
}
bool KviDns::isRunning() const
{
return (m_state == Busy);
};
bool KviDns::lookup(const TQString &query,QueryType type)
{
if(m_state == Busy)return false;
m_pSlaveThread->setQuery(KviTQString::trimmed(query),type);
bool bStarted = m_pSlaveThread->start();
m_state = bStarted ? Busy : Failure;
return bStarted;
}
int KviDns::error()
{
if(!m_pDnsResult)return KviError_dnsQueryFailed;
return m_pDnsResult->error();
}
KviDnsResult * KviDns::result()
{
if(!m_pDnsResult)m_pDnsResult = new KviDnsResult();
return m_pDnsResult;
}
KviPointerList<TQString> * KviDns::hostnameList()
{
return result()->hostnameList();
}
KviPointerList<TQString> * KviDns::ipAddressList()
{
return result()->ipAddressList();
}
int KviDns::hostnameCount()
{
return result()->hostnameList()->count();
}
int KviDns::ipAddressCount()
{
return result()->ipAddressList()->count();
}
const TQString & KviDns::firstHostname()
{
TQString * pStr = result()->hostnameList()->first();
if(pStr)return *pStr;
return KviTQString::empty;
}
const TQString & KviDns::firstIpAddress()
{
TQString * pStr = result()->ipAddressList()->first();
if(pStr)return *pStr;
return KviTQString::empty;
}
const TQString & KviDns::query()
{
return result()->query();
}
bool KviDns::event(TQEvent *e)
{
if(e->type() == KVI_THREAD_EVENT)
{
if(((KviThreadEvent *)e)->id() == KVI_DNS_THREAD_EVENT_DATA)
{
if(m_pDnsResult)delete m_pDnsResult;
m_pDnsResult = ((KviThreadDataEvent<KviDnsResult> *)e)->getData();
m_state = (m_pDnsResult->error() == KviError_success) ? Success : Failure;
emit lookupDone(this);
return true;
} // else ops... unknown thread event ?
}
return TQObject::event(e);
}
|