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