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