]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dns.cpp
Attempt to cut out unneccessary printf() va_args voodoo (ugleh)
[user/henk/code/inspircd.git] / src / dns.cpp
1 /*
2 dns.cpp - based on the firedns library Copyright (C) 2002 Ian Gulliver
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of version 2 of the GNU General Public License as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 */
17
18 #define _DNS_C
19
20 using namespace std;
21
22 #include <string>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/time.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <poll.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include "dns.h"
39 #include "inspircd.h"
40 #include "helperfuncs.h"
41 #include "socketengine.h"
42
43 extern InspIRCd* ServerInstance;
44 extern ServerConfig* Config;
45
46 #define max(a,b) (a > b ? a : b)
47 #define DNS_MAX              8                    /* max number of nameservers used */
48 #define DNS_CONFIG_FBCK     "/etc/resolv.conf"    /* fallback config file */
49 #define DNS_PORT            53                    /* DNS well known port */
50 #define DNS_QRY_A            1                    /* name to IP address */
51 #define DNS_QRY_AAAA        28                    /* name to IP6 address */
52 #define DNS_QRY_PTR         12                    /* IP address to name */
53 #define DNS_QRY_MX          15                    /* name to MX */
54 #define DNS_QRY_TXT         16                    /* name to TXT */
55 #define DNS_QRY_CNAME       5
56
57 #define DNS_ALIGN (sizeof(void *) > sizeof(long) ? sizeof(void *) : sizeof(long))
58 #define DNS_TRIES 3
59 #define RESULTSIZE 1024
60 #define min(a,b) (a < b ? a : b)
61
62 static struct in_addr servers4[DNS_MAX]; /* up to DNS_MAX nameservers; populated by dns_init() */
63 static int i4; /* actual count of nameservers; set by dns_init() */
64
65 static int initdone = 0; /* to ensure dns_init() only runs once (on the first call) */
66 static int wantclose = 0;
67 static int lastcreate = -1;
68
69 struct s_connection { /* open DNS query */
70         struct s_connection *next; /* next in list */
71         unsigned char id[2];
72         unsigned int _class;
73         unsigned int type;
74         int want_list;
75         int fd; /* file descriptor returned from sockets */
76 };
77
78 struct s_rr_middle {
79         unsigned int type;
80         unsigned int _class;
81         unsigned long ttl;
82         unsigned int rdlength;
83 };
84
85 #define DNS_POINTER_VALUE 0xc000
86
87 static s_connection *connection_head = NULL; /* linked list of open DNS queries; populated by dns_add_query(), decimated by dns_getresult_s() */
88
89 struct s_header { /* DNS query header */
90         unsigned char id[2];
91         unsigned int flags1;
92 #define FLAGS1_MASK_QR 0x80
93 #define FLAGS1_MASK_OPCODE 0x78 /* bitshift right 3 */
94 #define FLAGS1_MASK_AA 0x04
95 #define FLAGS1_MASK_TC 0x02
96 #define FLAGS1_MASK_RD 0x01
97         unsigned int flags2;
98 #define FLAGS2_MASK_RA 0x80
99 #define FLAGS2_MASK_Z  0x70
100 #define FLAGS2_MASK_RCODE 0x0f
101         unsigned int qdcount;
102         unsigned int ancount;
103         unsigned int nscount;
104         unsigned int arcount;
105         unsigned char payload[512]; /* DNS question, populated by dns_build_query_payload() */
106 };
107
108 extern time_t TIME;
109
110 void *dns_align(void *inp) {
111         char *p = (char*)inp;
112         int offby = ((char *)p - (char *)0) % DNS_ALIGN;
113         if (offby != 0)
114                 return p + (DNS_ALIGN - offby);
115         else
116                 return p;
117 }
118
119 /*
120  * These little hacks are here to avoid alignment and type sizing issues completely by doing manual copies
121  */
122 void dns_fill_rr(s_rr_middle* rr, const unsigned char *input) {
123         rr->type = input[0] * 256 + input[1];
124         rr->_class = input[2] * 256 + input[3];
125         rr->ttl = input[4] * 16777216 + input[5] * 65536 + input[6] * 256 + input[7];
126         rr->rdlength = input[8] * 256 + input[9];
127 }
128
129 void dns_fill_header(s_header *header, const unsigned char *input, const int l) {
130         header->id[0] = input[0];
131         header->id[1] = input[1];
132         header->flags1 = input[2];
133         header->flags2 = input[3];
134         header->qdcount = input[4] * 256 + input[5];
135         header->ancount = input[6] * 256 + input[7];
136         header->nscount = input[8] * 256 + input[9];
137         header->arcount = input[10] * 256 + input[11];
138         memcpy(header->payload,&input[12],l);
139 }
140
141 void dns_empty_header(unsigned char *output, const s_header *header, const int l) {
142         output[0] = header->id[0];
143         output[1] = header->id[1];
144         output[2] = header->flags1;
145         output[3] = header->flags2;
146         output[4] = header->qdcount / 256;
147         output[5] = header->qdcount % 256;
148         output[6] = header->ancount / 256;
149         output[7] = header->ancount % 256;
150         output[8] = header->nscount / 256;
151         output[9] = header->nscount % 256;
152         output[10] = header->arcount / 256;
153         output[11] = header->arcount % 256;
154         memcpy(&output[12],header->payload,l);
155 }
156
157 void dns_close(int fd) { /* close query */
158 #ifndef THREADED_DNS
159         ServerInstance->SE->DelFd(fd);
160 #endif
161         log(DEBUG,"DNS: dns_close on fd %d",fd);
162         if (fd == lastcreate) {
163                 wantclose = 1;
164                 return;
165         }
166         shutdown(fd,2);
167         close(fd);
168         return;
169 }
170
171 void DNS::dns_init() { /* on first call only: populates servers4 struct with up to DNS_MAX nameserver IP addresses from /etc/resolv.conf */
172         FILE *f;
173         int i;
174         in_addr addr4;
175         char buf[1024];
176         if (initdone == 1)
177                 return;
178         i4 = 0;
179
180         initdone = 1;
181         srand((unsigned int) TIME);
182         memset(servers4,'\0',sizeof(in_addr) * DNS_MAX);
183         f = fopen(DNS_CONFIG_FBCK,"r");
184         if (f == NULL)
185                 return;
186         while (fgets(buf,1024,f) != NULL) {
187                 if (strncmp(buf,"nameserver",10) == 0) {
188                         i = 10;
189                         while (buf[i] == ' ' || buf[i] == '\t')
190                                 i++;
191                         if (i4 < DNS_MAX) {
192                                 if (dns_aton4_s(&buf[i],&addr4) != NULL)
193                                         memcpy(&servers4[i4++],&addr4,sizeof(in_addr));
194                         }
195                 }
196         }
197         fclose(f);
198 }
199
200 void DNS::dns_init_2(const char* dnsserver)
201 {
202         in_addr addr4;
203         i4 = 0;
204         srand((unsigned int) TIME);
205         memset(servers4,'\0',sizeof(in_addr) * DNS_MAX);
206         if (dns_aton4_s(dnsserver,&addr4) != NULL)
207             memcpy(&servers4[i4++],&addr4,sizeof(in_addr));
208 }
209
210
211 static int dns_send_requests(const s_header *h, const s_connection *s, const int l)
212 {
213         int i;
214         sockaddr_in addr4;
215         unsigned char payload[sizeof(s_header)];
216
217         dns_empty_header(payload,h,l);
218
219
220         i = 0;
221
222         /* otherwise send via standard ipv4 boringness */
223         memset(&addr4,0,sizeof(addr4));
224         memcpy(&addr4.sin_addr,&servers4[i],sizeof(addr4.sin_addr));
225         addr4.sin_family = AF_INET;
226         addr4.sin_port = htons(DNS_PORT);
227         if (sendto(s->fd, payload, l + 12, 0, (sockaddr *) &addr4, sizeof(addr4)) == -1)
228         {
229                 return -1;
230         }
231
232         return 0;
233 }
234
235 static s_connection *dns_add_query(s_header *h) { /* build DNS query, add to list */
236         s_connection * s;
237
238         s = new s_connection;
239
240         /* set header flags */
241         h->id[0] = s->id[0] = rand() % 255; /* verified by dns_getresult_s() */
242         h->id[1] = s->id[1] = rand() % 255;
243         h->flags1 = 0 | FLAGS1_MASK_RD;
244         h->flags2 = 0;
245         h->qdcount = 1;
246         h->ancount = 0;
247         h->nscount = 0;
248         h->arcount = 0;
249
250         /* turn off want_list by default */
251         s->want_list = 0;
252
253         /* try to create ipv6 or ipv4 socket */
254                 s->fd = socket(PF_INET, SOCK_DGRAM, 0);
255                 if (s->fd != -1) {
256                         if (fcntl(s->fd, F_SETFL, O_NONBLOCK) != 0) {
257                                 shutdown(s->fd,2);
258                                 close(s->fd);
259                                 s->fd = -1;
260                         }
261                 }
262                 if (s->fd != -1) {
263                         sockaddr_in addr;
264                         memset(&addr,0,sizeof(addr));
265                         addr.sin_family = AF_INET;
266                         addr.sin_port = 0;
267                         addr.sin_addr.s_addr = INADDR_ANY;
268                         if (bind(s->fd,(sockaddr *)&addr,sizeof(addr)) != 0) {
269                                 shutdown(s->fd,2);
270                                 close(s->fd);
271                                 s->fd = -1;
272                         }
273                 }
274                 if (s->fd == -1) {
275                         delete s;
276                         return NULL;
277                 }
278         /* create new connection object, add to linked list */
279         s->next = connection_head;
280         connection_head = s;
281
282         if (wantclose == 1) {
283                 shutdown(lastcreate,2);
284                 close(lastcreate);
285                 wantclose = 0;
286         }
287         lastcreate = s->fd;
288         return s;
289 }
290
291 static int dns_build_query_payload(const char * const name, const unsigned short rr, const unsigned short _class, unsigned char * const payload) { 
292         short payloadpos;
293         const char * tempchr, * tempchr2;
294         unsigned short l;
295
296         payloadpos = 0;
297         tempchr2 = name;
298
299         /* split name up into labels, create query */
300         while ((tempchr = strchr(tempchr2,'.')) != NULL) {
301                 l = tempchr - tempchr2;
302                 if (payloadpos + l + 1 > 507)
303                         return -1;
304                 payload[payloadpos++] = l;
305                 memcpy(&payload[payloadpos],tempchr2,l);
306                 payloadpos += l;
307                 tempchr2 = &tempchr[1];
308         }
309         l = strlen(tempchr2);
310         if (l) {
311                 if (payloadpos + l + 2 > 507)
312                         return -1;
313                 payload[payloadpos++] = l;
314                 memcpy(&payload[payloadpos],tempchr2,l);
315                 payloadpos += l;
316                 payload[payloadpos++] = '\0';
317         }
318         if (payloadpos > 508)
319                 return -1;
320         l = htons(rr);
321         memcpy(&payload[payloadpos],&l,2);
322         l = htons(_class);
323         memcpy(&payload[payloadpos + 2],&l,2);
324         return payloadpos + 4;
325 }
326
327 in_addr* DNS::dns_aton4(const char * const ipstring) { /* ascii to numeric: convert string to static 4part IP addr struct */
328         static in_addr ip;
329         return dns_aton4_s(ipstring,&ip);
330 }
331
332 in_addr* DNS::dns_aton4_r(const char *ipstring) { /* ascii to numeric (reentrant): convert string to new 4part IP addr struct */
333         in_addr* ip;
334         ip = new in_addr;
335         if(dns_aton4_s(ipstring,ip) == NULL) {
336                 delete ip;
337                 return NULL;
338         }
339         return ip;
340 }
341
342 in_addr* DNS::dns_aton4_s(const char *ipstring, in_addr *ip) { /* ascii to numeric (buffered): convert string to given 4part IP addr struct */
343         inet_aton(ipstring,ip);
344         return ip;
345 }
346
347 int DNS::dns_getip4(const char *name) { /* build, add and send A query; retrieve result with dns_getresult() */
348         s_header h;
349         s_connection *s;
350         int l;
351
352         dns_init();
353         
354
355         l = dns_build_query_payload(name,DNS_QRY_A,1,(unsigned char *)&h.payload);
356         if (l == -1)
357                 return -1;
358         s = dns_add_query(&h);
359         if (s == NULL)
360                 return -1;
361         s->_class = 1;
362         s->type = DNS_QRY_A;
363         if (dns_send_requests(&h,s,l) == -1)
364                 return -1;
365
366         return s->fd;
367 }
368
369 int DNS::dns_getip4list(const char *name) { /* build, add and send A query; retrieve result with dns_getresult() */
370         s_header h;
371         s_connection *s;
372         int l;
373
374         dns_init();
375         
376
377         l = dns_build_query_payload(name,DNS_QRY_A,1,(unsigned char *)&h.payload);
378         if (l == -1)
379                 return -1;
380         s = dns_add_query(&h);
381         if (s == NULL)
382                 return -1;
383         s->_class = 1;
384         s->type = DNS_QRY_A;
385         s->want_list = 1;
386         if (dns_send_requests(&h,s,l) == -1)
387                 return -1;
388
389         return s->fd;
390 }
391
392 int DNS::dns_getname4(const in_addr *ip) { /* build, add and send PTR query; retrieve result with dns_getresult() */
393         char query[512];
394         s_header h;
395         s_connection * s;
396         unsigned char *c;
397         int l;
398
399         c = (unsigned char *)&ip->s_addr;
400
401         sprintf(query,"%d.%d.%d.%d.in-addr.arpa",c[3],c[2],c[1],c[0]);
402
403         l = dns_build_query_payload(query,DNS_QRY_PTR,1,(unsigned char *)&h.payload);
404         if (l == -1)
405                 return -1;
406         s = dns_add_query(&h);
407         if (s == NULL)
408                 return -1;
409         s->_class = 1;
410         s->type = DNS_QRY_PTR;
411         if (dns_send_requests(&h,s,l) == -1)
412                 return -1;
413
414         return s->fd;
415 }
416
417 char* DNS::dns_ntoa4(const in_addr * const ip) { /* numeric to ascii: convert 4part IP addr struct to static string */
418         static char r[256];
419         return dns_ntoa4_s(ip,r);
420 }
421
422 char* DNS::dns_ntoa4_s(const in_addr *ip, char *r) { /* numeric to ascii (buffered): convert 4part IP addr struct to given string */
423         unsigned char *m;
424         m = (unsigned char *)&ip->s_addr;
425         sprintf(r,"%d.%d.%d.%d",m[0],m[1],m[2],m[3]);
426         return r;
427 }
428
429 char* DNS::dns_getresult(const int cfd) { /* retrieve result of DNS query */
430         log(DEBUG,"DNS: dns_getresult with cfd=%d",cfd);
431         return dns_getresult_s(cfd,this->localbuf);
432 }
433
434 char* DNS::dns_getresult_s(const int cfd, char *res) { /* retrieve result of DNS query (buffered) */
435         s_header h;
436         s_connection *c, *prev;
437         int l,i,q,curanswer,o;
438         s_rr_middle rr;
439         unsigned char buffer[sizeof(s_header)];
440         unsigned short p;
441
442         if (res)
443         {
444                 res[0] = 0;
445         }
446
447         prev = NULL;
448         c = connection_head;
449         while (c != NULL) { /* find query in list of open queries */
450                 if (c->fd == cfd)
451                         break;
452                 prev = c;
453                 c = c->next;
454         }
455         if (c == NULL) {
456                 log(DEBUG,"DNS: got a response for a query we didnt send with fd=%d",cfd);
457                 return NULL; /* query not found */
458         }
459         /* query found-- pull from list: */
460         if (prev != NULL)
461                 prev->next = c->next;
462         else
463                 connection_head = c->next;
464
465         l = recv(c->fd,buffer,sizeof(s_header),0);
466         dns_close(c->fd);
467         if (l < 12) {
468                 delete c;
469                 return NULL;
470         }
471         dns_fill_header(&h,buffer,l - 12);
472         if (c->id[0] != h.id[0] || c->id[1] != h.id[1]) {
473                 log(DEBUG,"DNS: id mismatch on query");
474                 delete c;
475                 return NULL; /* ID mismatch */
476         }
477         if ((h.flags1 & FLAGS1_MASK_QR) == 0) {
478                 log(DEBUG,"DNS: didnt get a query result");
479                 delete c;
480                 return NULL;
481         }
482         if ((h.flags1 & FLAGS1_MASK_OPCODE) != 0) {
483                 log(DEBUG,"DNS: got an OPCODE and didnt want one");
484                 delete c;
485                 return NULL;
486         }
487         if ((h.flags2 & FLAGS2_MASK_RCODE) != 0) {
488                 log(DEBUG,"DNS lookup failed due to SERVFAIL");
489                 delete c;
490                 return NULL;
491         }
492         if (h.ancount < 1)  { /* no sense going on if we don't have any answers */
493                 log(DEBUG,"DNS: no answers!");
494                 delete c;
495                 return NULL;
496         }
497         /* skip queries */
498         i = 0;
499         q = 0;
500         l -= 12;
501         while ((unsigned)q < h.qdcount && i < l) {
502                 if (h.payload[i] > 63) { /* pointer */
503                         i += 6; /* skip pointer, _class and type */
504                         q++;
505                 } else { /* label */
506                         if (h.payload[i] == 0) {
507                                 q++;
508                                 i += 5; /* skip nil, _class and type */
509                         } else
510                                 i += h.payload[i] + 1; /* skip length and label */
511                 }
512         }
513         /* &h.payload[i] should now be the start of the first response */
514         curanswer = 0;
515         while ((unsigned)curanswer < h.ancount) {
516                 q = 0;
517                 while (q == 0 && i < l) {
518                         if (h.payload[i] > 63) { /* pointer */
519                                 i += 2; /* skip pointer */
520                                 q = 1;
521                         } else { /* label */
522                                 if (h.payload[i] == 0) {
523                                         i++;
524                                         q = 1;
525                                 } else
526                                         i += h.payload[i] + 1; /* skip length and label */
527                         }
528                 }
529                 if (l - i < 10) {
530                         delete c;
531                         return NULL;
532                 }
533                 dns_fill_rr(&rr,&h.payload[i]);
534                 i += 10;
535                 if (rr.type != c->type) {
536                         curanswer++;
537                         i += rr.rdlength;
538                         continue;
539                 }
540                 if (rr._class != c->_class) {
541                         curanswer++;
542                         i += rr.rdlength;
543                         continue;
544                 }
545                 break;
546         }
547         if ((unsigned)curanswer == h.ancount)
548                 return NULL;
549         if ((unsigned)i + rr.rdlength > (unsigned)l)
550                 return NULL;
551         if (rr.rdlength > 1023)
552                 return NULL;
553
554         switch (rr.type) {
555                 case DNS_QRY_PTR:
556                         log(DEBUG,"DNS: got a result of type DNS_QRY_PTR");
557                         o = 0;
558                         q = 0;
559                         while (q == 0 && i < l && o + 256 < 1023) {
560                                 if (h.payload[i] > 63) { /* pointer */
561                                         memcpy(&p,&h.payload[i],2);
562                                         i = ntohs(p) - DNS_POINTER_VALUE - 12;
563                                 } else { /* label */
564                                         if (h.payload[i] == 0)
565                                                 q = 1;
566                                         else {
567                                                 res[o] = '\0';
568                                                 if (o != 0)
569                                                         res[o++] = '.';
570                                                 memcpy(&res[o],&h.payload[i + 1],h.payload[i]);
571                                                 o += h.payload[i];
572                                                 i += h.payload[i] + 1;
573                                         }
574                                 }
575                         }
576                         res[o] = '\0';
577                         break;
578                 case DNS_QRY_A:
579                         log(DEBUG,"DNS: got a result of type DNS_QRY_A");
580                         if (c->want_list) {
581                                 dns_ip4list *alist = (dns_ip4list *) res; /* we have to trust that this is aligned */
582                                 while ((char *)alist - (char *)res < 700) {
583                                         if (rr.type != DNS_QRY_A)
584                                                 break;
585                                         if (rr._class != 1)
586                                                 break;
587                                         if (rr.rdlength != 4) {
588                                                 delete c;
589                                                 return NULL;
590                                         }
591                                         memcpy(&alist->ip,&h.payload[i],4);
592                                         if ((unsigned)++curanswer >= h.ancount)
593                                                 break;
594                                         i += rr.rdlength;
595                                         {
596                                                 /* skip next name */
597                                                 q = 0;
598                                                 while (q == 0 && i < l) {
599                                                         if (h.payload[i] > 63) { /* pointer */
600                                                                 i += 2; /* skip pointer */
601                                                                 q = 1;
602                                                         } else { /* label */
603                                                                 if (h.payload[i] == 0) {
604                                                                         i++;
605                                                                         q = 1;
606                                                                 } else
607                                                                         i += h.payload[i] + 1; /* skip length and label */
608                                                         }
609                                                 }
610                                         }
611                                         if (l - i < 10) {
612                                                 delete c;
613                                                 return NULL;
614                                         }
615                                         dns_fill_rr(&rr,&h.payload[i]);
616                                         i += 10;
617                                         alist->next = (dns_ip4list *) dns_align(((char *) alist) + sizeof(dns_ip4list));
618                                         alist = alist->next;
619                                         alist->next = NULL;
620                                 }
621                                 alist->next = NULL;
622                                 break;
623                         }
624                         goto defaultcase;
625                         break;
626                 default:
627                 defaultcase:
628                         log(DEBUG,"DNS: doing something with result 'default'");
629                         memcpy(res,&h.payload[i],rr.rdlength);
630                         res[rr.rdlength] = '\0';
631                         break;
632         }
633         delete c;
634         return res;
635 }
636
637 DNS::DNS()
638 {
639         dns_init();
640         log(DEBUG,"Create blank DNS");
641 }
642
643 DNS::DNS(std::string dnsserver)
644 {
645         dns_init_2(dnsserver.c_str());
646         log(DEBUG,"Create DNS");
647 }
648
649 void DNS::SetNS(std::string dnsserver)
650 {
651         dns_init_2(dnsserver.c_str());
652         log(DEBUG,"Set NS");
653 }
654
655 DNS::~DNS()
656 {
657 }
658
659 bool DNS::ReverseLookup(std::string ip)
660 {
661         ServerInstance->stats->statsDns++;
662         binip = dns_aton4(ip.c_str());
663         if (binip == NULL) {
664                 return false;
665         }
666
667         this->myfd = dns_getname4(binip);
668         if (this->myfd == -1)
669         {
670                 return false;
671         }
672         log(DEBUG,"DNS: ReverseLookup, fd=%d",this->myfd);
673 #ifndef THREADED_DNS
674         ServerInstance->SE->AddFd(this->myfd,true,X_ESTAB_DNS);
675 #endif
676         return true;
677 }
678
679 bool DNS::ForwardLookup(std::string host)
680 {
681         ServerInstance->stats->statsDns++;
682         this->myfd = dns_getip4(host.c_str());
683         if (this->myfd == -1)
684         {
685                 return false;
686         }
687         log(DEBUG,"DNS: ForwardLookup, fd=%d",this->myfd);
688 #ifndef THREADED_DNS
689         ServerInstance->SE->AddFd(this->myfd,true,X_ESTAB_DNS);
690 #endif
691         return true;
692 }
693
694 bool DNS::HasResult(int fd)
695 {
696         return (fd == this->myfd);
697 }
698
699 /* Only the multithreaded dns uses this poll() based
700  * check now. As its in another thread we dont have
701  * to worry about its performance that much.
702  */
703 bool DNS::HasResult()
704 {
705         log(DEBUG,"DNS: HasResult, fd=%d",this->myfd);
706         pollfd polls;
707         polls.fd = this->myfd;
708         polls.events = POLLIN;
709         int ret = poll(&polls,1,1);
710         log(DEBUG,"DNS: Hasresult returning %d",ret);
711         return (ret > 0);
712 }
713
714 int DNS::GetFD()
715 {
716         return this->myfd;
717 }
718
719 std::string DNS::GetResult()
720 {
721         log(DEBUG,"DNS: GetResult()");
722         result = dns_getresult(this->myfd);
723         if (result) {
724                 ServerInstance->stats->statsDnsGood++;
725                 dns_close(this->myfd);
726                 return result;
727         } else {
728                 ServerInstance->stats->statsDnsBad++;
729                 if (this->myfd != -1)
730                 {
731                         dns_close(this->myfd);
732                 }
733                 return "";
734         }
735 }
736
737 std::string DNS::GetResultIP()
738 {
739         char r[1024];
740         log(DEBUG,"DNS: GetResultIP()");
741         result = dns_getresult(this->myfd);
742         if (this->myfd != -1)
743         {
744                 dns_close(this->myfd);
745         }
746         if (result)
747         {
748                 ServerInstance->stats->statsDnsGood++;
749                 unsigned char a = (unsigned)result[0];
750                 unsigned char b = (unsigned)result[1];
751                 unsigned char c = (unsigned)result[2];
752                 unsigned char d = (unsigned)result[3];
753                 snprintf(r,1024,"%u.%u.%u.%u",a,b,c,d);
754                 return r;
755         }
756         else
757         {
758                 ServerInstance->stats->statsDnsBad++;
759                 log(DEBUG,"DANGER WILL ROBINSON! NXDOMAIN for forward lookup, but we got a reverse lookup!");
760                 return "";
761         }
762 }
763
764
765
766 #ifdef THREADED_DNS
767 void* dns_task(void* arg)
768 {
769         userrec* u = (userrec*)arg;
770         log(DEBUG,"DNS thread for user %s",u->nick);
771         DNS dns1;
772         DNS dns2;
773         std::string host;
774         std::string ip;
775         if (dns1.ReverseLookup(u->ip))
776         {
777                 while (!dns1.HasResult())
778                 {
779                         usleep(100);
780                 }
781                 host = dns1.GetResult();
782                 if (host != "")
783                 {
784                         if (dns2.ForwardLookup(host))
785                         {
786                                 while (!dns2.HasResult())
787                                 {
788                                         usleep(100);
789                                 }
790                                 ip = dns2.GetResultIP();
791                                 if (ip == std::string(u->ip))
792                                 {
793                                         if (host.length() < 160)
794                                         {
795                                                 strcpy(u->host,host.c_str());
796                                                 strcpy(u->dhost,host.c_str());
797                                         }
798                                 }
799                         }
800                 }
801         }
802         u->dns_done = true;
803         return NULL;
804 }
805 #endif