]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dns.cpp
Left figure from output
[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 "helperfuncs.h"
40 #include "socketengine.h"
41
42 extern SocketEngine* SE;
43
44 extern int statsAccept,statsRefused,statsUnknown,statsCollisions,statsDns,statsDnsGood,statsDnsBad,statsConnects,statsSent,statsRecv;
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         log(DEBUG,"DNS: dns_close on fd %d",fd);
159         if (fd == lastcreate) {
160                 wantclose = 1;
161                 return;
162         }
163         shutdown(fd,2);
164         close(fd);
165         return;
166 }
167
168 void DNS::dns_init() { /* on first call only: populates servers4 struct with up to DNS_MAX nameserver IP addresses from /etc/resolv.conf */
169         FILE *f;
170         int i;
171         in_addr addr4;
172         char buf[1024];
173         if (initdone == 1)
174                 return;
175         i4 = 0;
176
177         initdone = 1;
178         srand((unsigned int) TIME);
179         memset(servers4,'\0',sizeof(in_addr) * DNS_MAX);
180         f = fopen(DNS_CONFIG_FBCK,"r");
181         if (f == NULL)
182                 return;
183         while (fgets(buf,1024,f) != NULL) {
184                 if (strncmp(buf,"nameserver",10) == 0) {
185                         i = 10;
186                         while (buf[i] == ' ' || buf[i] == '\t')
187                                 i++;
188                         if (i4 < DNS_MAX) {
189                                 if (dns_aton4_s(&buf[i],&addr4) != NULL)
190                                         memcpy(&servers4[i4++],&addr4,sizeof(in_addr));
191                         }
192                 }
193         }
194         fclose(f);
195 }
196
197 void DNS::dns_init_2(const char* dnsserver)
198 {
199         in_addr addr4;
200         i4 = 0;
201         srand((unsigned int) TIME);
202         memset(servers4,'\0',sizeof(in_addr) * DNS_MAX);
203         if (dns_aton4_s(dnsserver,&addr4) != NULL)
204             memcpy(&servers4[i4++],&addr4,sizeof(in_addr));
205 }
206
207
208 static int dns_send_requests(const s_header *h, const s_connection *s, const int l)
209 {
210         int i;
211         sockaddr_in addr4;
212         unsigned char payload[sizeof(s_header)];
213
214         dns_empty_header(payload,h,l);
215
216
217         i = 0;
218
219         /* otherwise send via standard ipv4 boringness */
220         memset(&addr4,0,sizeof(addr4));
221         memcpy(&addr4.sin_addr,&servers4[i],sizeof(addr4.sin_addr));
222         addr4.sin_family = AF_INET;
223         addr4.sin_port = htons(DNS_PORT);
224         if (sendto(s->fd, payload, l + 12, 0, (sockaddr *) &addr4, sizeof(addr4)) == -1)
225         {
226                 return -1;
227         }
228
229         return 0;
230 }
231
232 static s_connection *dns_add_query(s_header *h) { /* build DNS query, add to list */
233         s_connection * s;
234
235         s = new s_connection;
236
237         /* set header flags */
238         h->id[0] = s->id[0] = rand() % 255; /* verified by dns_getresult_s() */
239         h->id[1] = s->id[1] = rand() % 255;
240         h->flags1 = 0 | FLAGS1_MASK_RD;
241         h->flags2 = 0;
242         h->qdcount = 1;
243         h->ancount = 0;
244         h->nscount = 0;
245         h->arcount = 0;
246
247         /* turn off want_list by default */
248         s->want_list = 0;
249
250         /* try to create ipv6 or ipv4 socket */
251                 s->fd = socket(PF_INET, SOCK_DGRAM, 0);
252                 if (s->fd != -1) {
253                         if (fcntl(s->fd, F_SETFL, O_NONBLOCK) != 0) {
254                                 shutdown(s->fd,2);
255                                 close(s->fd);
256                                 s->fd = -1;
257                         }
258                 }
259                 if (s->fd != -1) {
260                         sockaddr_in addr;
261                         memset(&addr,0,sizeof(addr));
262                         addr.sin_family = AF_INET;
263                         addr.sin_port = 0;
264                         addr.sin_addr.s_addr = INADDR_ANY;
265                         if (bind(s->fd,(sockaddr *)&addr,sizeof(addr)) != 0) {
266                                 shutdown(s->fd,2);
267                                 close(s->fd);
268                                 s->fd = -1;
269                         }
270                 }
271                 if (s->fd == -1) {
272                         delete s;
273                         return NULL;
274                 }
275         /* create new connection object, add to linked list */
276         s->next = connection_head;
277         connection_head = s;
278
279         if (wantclose == 1) {
280                 shutdown(lastcreate,2);
281                 close(lastcreate);
282                 wantclose = 0;
283         }
284         lastcreate = s->fd;
285         return s;
286 }
287
288 static int dns_build_query_payload(const char * const name, const unsigned short rr, const unsigned short _class, unsigned char * const payload) { 
289         short payloadpos;
290         const char * tempchr, * tempchr2;
291         unsigned short l;
292
293         payloadpos = 0;
294         tempchr2 = name;
295
296         /* split name up into labels, create query */
297         while ((tempchr = strchr(tempchr2,'.')) != NULL) {
298                 l = tempchr - tempchr2;
299                 if (payloadpos + l + 1 > 507)
300                         return -1;
301                 payload[payloadpos++] = l;
302                 memcpy(&payload[payloadpos],tempchr2,l);
303                 payloadpos += l;
304                 tempchr2 = &tempchr[1];
305         }
306         l = strlen(tempchr2);
307         if (l) {
308                 if (payloadpos + l + 2 > 507)
309                         return -1;
310                 payload[payloadpos++] = l;
311                 memcpy(&payload[payloadpos],tempchr2,l);
312                 payloadpos += l;
313                 payload[payloadpos++] = '\0';
314         }
315         if (payloadpos > 508)
316                 return -1;
317         l = htons(rr);
318         memcpy(&payload[payloadpos],&l,2);
319         l = htons(_class);
320         memcpy(&payload[payloadpos + 2],&l,2);
321         return payloadpos + 4;
322 }
323
324 in_addr* DNS::dns_aton4(const char * const ipstring) { /* ascii to numeric: convert string to static 4part IP addr struct */
325         static in_addr ip;
326         return dns_aton4_s(ipstring,&ip);
327 }
328
329 in_addr* DNS::dns_aton4_r(const char *ipstring) { /* ascii to numeric (reentrant): convert string to new 4part IP addr struct */
330         in_addr* ip;
331         ip = new in_addr;
332         if(dns_aton4_s(ipstring,ip) == NULL) {
333                 delete ip;
334                 return NULL;
335         }
336         return ip;
337 }
338
339 in_addr* DNS::dns_aton4_s(const char *ipstring, in_addr *ip) { /* ascii to numeric (buffered): convert string to given 4part IP addr struct */
340         inet_aton(ipstring,ip);
341         return ip;
342 }
343
344 int DNS::dns_getip4(const char *name) { /* build, add and send A query; retrieve result with dns_getresult() */
345         s_header h;
346         s_connection *s;
347         int l;
348
349         dns_init();
350         
351
352         l = dns_build_query_payload(name,DNS_QRY_A,1,(unsigned char *)&h.payload);
353         if (l == -1)
354                 return -1;
355         s = dns_add_query(&h);
356         if (s == NULL)
357                 return -1;
358         s->_class = 1;
359         s->type = DNS_QRY_A;
360         if (dns_send_requests(&h,s,l) == -1)
361                 return -1;
362
363         return s->fd;
364 }
365
366 int DNS::dns_getip4list(const char *name) { /* build, add and send A query; retrieve result with dns_getresult() */
367         s_header h;
368         s_connection *s;
369         int l;
370
371         dns_init();
372         
373
374         l = dns_build_query_payload(name,DNS_QRY_A,1,(unsigned char *)&h.payload);
375         if (l == -1)
376                 return -1;
377         s = dns_add_query(&h);
378         if (s == NULL)
379                 return -1;
380         s->_class = 1;
381         s->type = DNS_QRY_A;
382         s->want_list = 1;
383         if (dns_send_requests(&h,s,l) == -1)
384                 return -1;
385
386         return s->fd;
387 }
388
389 int DNS::dns_getname4(const in_addr *ip) { /* build, add and send PTR query; retrieve result with dns_getresult() */
390         char query[512];
391         s_header h;
392         s_connection * s;
393         unsigned char *c;
394         int l;
395
396         c = (unsigned char *)&ip->s_addr;
397
398         sprintf(query,"%d.%d.%d.%d.in-addr.arpa",c[3],c[2],c[1],c[0]);
399
400         l = dns_build_query_payload(query,DNS_QRY_PTR,1,(unsigned char *)&h.payload);
401         if (l == -1)
402                 return -1;
403         s = dns_add_query(&h);
404         if (s == NULL)
405                 return -1;
406         s->_class = 1;
407         s->type = DNS_QRY_PTR;
408         if (dns_send_requests(&h,s,l) == -1)
409                 return -1;
410
411         return s->fd;
412 }
413
414 char* DNS::dns_ntoa4(const in_addr * const ip) { /* numeric to ascii: convert 4part IP addr struct to static string */
415         static char r[256];
416         return dns_ntoa4_s(ip,r);
417 }
418
419 char* DNS::dns_ntoa4_s(const in_addr *ip, char *r) { /* numeric to ascii (buffered): convert 4part IP addr struct to given string */
420         unsigned char *m;
421         m = (unsigned char *)&ip->s_addr;
422         sprintf(r,"%d.%d.%d.%d",m[0],m[1],m[2],m[3]);
423         return r;
424 }
425
426 char* DNS::dns_getresult(const int cfd) { /* retrieve result of DNS query */
427         log(DEBUG,"DNS: dns_getresult with cfd=%d",cfd);
428         return dns_getresult_s(cfd,this->localbuf);
429 }
430
431 char* DNS::dns_getresult_s(const int cfd, char *res) { /* retrieve result of DNS query (buffered) */
432         s_header h;
433         s_connection *c, *prev;
434         int l,i,q,curanswer,o;
435         s_rr_middle rr;
436         unsigned char buffer[sizeof(s_header)];
437         unsigned short p;
438
439         if (res)
440         {
441                 res[0] = 0;
442         }
443
444         prev = NULL;
445         c = connection_head;
446         while (c != NULL) { /* find query in list of open queries */
447                 if (c->fd == cfd)
448                         break;
449                 prev = c;
450                 c = c->next;
451         }
452         if (c == NULL) {
453                 log(DEBUG,"DNS: got a response for a query we didnt send with fd=%d",cfd);
454                 return NULL; /* query not found */
455         }
456         /* query found-- pull from list: */
457         if (prev != NULL)
458                 prev->next = c->next;
459         else
460                 connection_head = c->next;
461
462         l = recv(c->fd,buffer,sizeof(s_header),0);
463         dns_close(c->fd);
464         if (l < 12) {
465                 delete c;
466                 return NULL;
467         }
468         dns_fill_header(&h,buffer,l - 12);
469         if (c->id[0] != h.id[0] || c->id[1] != h.id[1]) {
470                 log(DEBUG,"DNS: id mismatch on query");
471                 delete c;
472                 return NULL; /* ID mismatch */
473         }
474         if ((h.flags1 & FLAGS1_MASK_QR) == 0) {
475                 log(DEBUG,"DNS: didnt get a query result");
476                 delete c;
477                 return NULL;
478         }
479         if ((h.flags1 & FLAGS1_MASK_OPCODE) != 0) {
480                 log(DEBUG,"DNS: got an OPCODE and didnt want one");
481                 delete c;
482                 return NULL;
483         }
484         if ((h.flags2 & FLAGS2_MASK_RCODE) != 0) {
485                 log(DEBUG,"DNS lookup failed due to SERVFAIL");
486                 delete c;
487                 return NULL;
488         }
489         if (h.ancount < 1)  { /* no sense going on if we don't have any answers */
490                 log(DEBUG,"DNS: no answers!");
491                 delete c;
492                 return NULL;
493         }
494         /* skip queries */
495         i = 0;
496         q = 0;
497         l -= 12;
498         while ((unsigned)q < h.qdcount && i < l) {
499                 if (h.payload[i] > 63) { /* pointer */
500                         i += 6; /* skip pointer, _class and type */
501                         q++;
502                 } else { /* label */
503                         if (h.payload[i] == 0) {
504                                 q++;
505                                 i += 5; /* skip nil, _class and type */
506                         } else
507                                 i += h.payload[i] + 1; /* skip length and label */
508                 }
509         }
510         /* &h.payload[i] should now be the start of the first response */
511         curanswer = 0;
512         while ((unsigned)curanswer < h.ancount) {
513                 q = 0;
514                 while (q == 0 && i < l) {
515                         if (h.payload[i] > 63) { /* pointer */
516                                 i += 2; /* skip pointer */
517                                 q = 1;
518                         } else { /* label */
519                                 if (h.payload[i] == 0) {
520                                         i++;
521                                         q = 1;
522                                 } else
523                                         i += h.payload[i] + 1; /* skip length and label */
524                         }
525                 }
526                 if (l - i < 10) {
527                         delete c;
528                         return NULL;
529                 }
530                 dns_fill_rr(&rr,&h.payload[i]);
531                 i += 10;
532                 if (rr.type != c->type) {
533                         curanswer++;
534                         i += rr.rdlength;
535                         continue;
536                 }
537                 if (rr._class != c->_class) {
538                         curanswer++;
539                         i += rr.rdlength;
540                         continue;
541                 }
542                 break;
543         }
544         if ((unsigned)curanswer == h.ancount)
545                 return NULL;
546         if ((unsigned)i + rr.rdlength > (unsigned)l)
547                 return NULL;
548         if (rr.rdlength > 1023)
549                 return NULL;
550
551         switch (rr.type) {
552                 case DNS_QRY_PTR:
553                         log(DEBUG,"DNS: got a result of type DNS_QRY_PTR");
554                         o = 0;
555                         q = 0;
556                         while (q == 0 && i < l && o + 256 < 1023) {
557                                 if (h.payload[i] > 63) { /* pointer */
558                                         memcpy(&p,&h.payload[i],2);
559                                         i = ntohs(p) - DNS_POINTER_VALUE - 12;
560                                 } else { /* label */
561                                         if (h.payload[i] == 0)
562                                                 q = 1;
563                                         else {
564                                                 res[o] = '\0';
565                                                 if (o != 0)
566                                                         res[o++] = '.';
567                                                 memcpy(&res[o],&h.payload[i + 1],h.payload[i]);
568                                                 o += h.payload[i];
569                                                 i += h.payload[i] + 1;
570                                         }
571                                 }
572                         }
573                         res[o] = '\0';
574                         break;
575                 case DNS_QRY_A:
576                         log(DEBUG,"DNS: got a result of type DNS_QRY_A");
577                         if (c->want_list) {
578                                 dns_ip4list *alist = (dns_ip4list *) res; /* we have to trust that this is aligned */
579                                 while ((char *)alist - (char *)res < 700) {
580                                         if (rr.type != DNS_QRY_A)
581                                                 break;
582                                         if (rr._class != 1)
583                                                 break;
584                                         if (rr.rdlength != 4) {
585                                                 delete c;
586                                                 return NULL;
587                                         }
588                                         memcpy(&alist->ip,&h.payload[i],4);
589                                         if ((unsigned)++curanswer >= h.ancount)
590                                                 break;
591                                         i += rr.rdlength;
592                                         {
593                                                 /* skip next name */
594                                                 q = 0;
595                                                 while (q == 0 && i < l) {
596                                                         if (h.payload[i] > 63) { /* pointer */
597                                                                 i += 2; /* skip pointer */
598                                                                 q = 1;
599                                                         } else { /* label */
600                                                                 if (h.payload[i] == 0) {
601                                                                         i++;
602                                                                         q = 1;
603                                                                 } else
604                                                                         i += h.payload[i] + 1; /* skip length and label */
605                                                         }
606                                                 }
607                                         }
608                                         if (l - i < 10) {
609                                                 delete c;
610                                                 return NULL;
611                                         }
612                                         dns_fill_rr(&rr,&h.payload[i]);
613                                         i += 10;
614                                         alist->next = (dns_ip4list *) dns_align(((char *) alist) + sizeof(dns_ip4list));
615                                         alist = alist->next;
616                                         alist->next = NULL;
617                                 }
618                                 alist->next = NULL;
619                                 break;
620                         }
621                         goto defaultcase;
622                         break;
623                 default:
624                 defaultcase:
625                         log(DEBUG,"DNS: doing something with result 'default'");
626                         memcpy(res,&h.payload[i],rr.rdlength);
627                         res[rr.rdlength] = '\0';
628                         break;
629         }
630         delete c;
631         return res;
632 }
633
634 DNS::DNS()
635 {
636         dns_init();
637         log(DEBUG,"Create blank DNS");
638 }
639
640 DNS::DNS(std::string dnsserver)
641 {
642         dns_init_2(dnsserver.c_str());
643         log(DEBUG,"Create DNS");
644 }
645
646 void DNS::SetNS(std::string dnsserver)
647 {
648         dns_init_2(dnsserver.c_str());
649         log(DEBUG,"Set NS");
650 }
651
652 DNS::~DNS()
653 {
654 }
655
656 bool DNS::ReverseLookup(std::string ip)
657 {
658         statsDns++;
659         binip = dns_aton4(ip.c_str());
660         if (binip == NULL) {
661                 return false;
662         }
663
664         this->myfd = dns_getname4(binip);
665         if (this->myfd == -1)
666         {
667                 return false;
668         }
669         log(DEBUG,"DNS: ReverseLookup, fd=%d",this->myfd);
670 #ifndef THREADED_DNS
671         SE->AddFd(this->myfd,true,X_ESTAB_DNS);
672 #endif
673         return true;
674 }
675
676 bool DNS::ForwardLookup(std::string host)
677 {
678         statsDns++;
679         this->myfd = dns_getip4(host.c_str());
680         if (this->myfd == -1)
681         {
682                 return false;
683         }
684         log(DEBUG,"DNS: ForwardLookup, fd=%d",this->myfd);
685 #ifndef THREADED_DNS
686         SE->AddFd(this->myfd,true,X_ESTAB_DNS);
687 #endif
688         return true;
689 }
690
691 bool DNS::HasResult(int fd)
692 {
693         return (fd == this->myfd);
694 }
695
696 /* Only the multithreaded dns uses this poll() based
697  * check now. As its in another thread we dont have
698  * to worry about its performance that much.
699  */
700 bool DNS::HasResult()
701 {
702         log(DEBUG,"DNS: HasResult, fd=%d",this->myfd);
703         pollfd polls;
704         polls.fd = this->myfd;
705         polls.events = POLLIN;
706         int ret = poll(&polls,1,1);
707         log(DEBUG,"DNS: Hasresult returning %d",ret);
708         return (ret > 0);
709 }
710
711 int DNS::GetFD()
712 {
713         return this->myfd;
714 }
715
716 std::string DNS::GetResult()
717 {
718         log(DEBUG,"DNS: GetResult()");
719         result = dns_getresult(this->myfd);
720 #ifndef THREADED_DNS
721         SE->DelFd(this->myfd);
722 #endif
723         if (result) {
724                 statsDnsGood++;
725                 dns_close(this->myfd);
726                 return result;
727         } else {
728                 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 #ifndef THREADED_DNS
745                 SE->DelFd(this->myfd);
746 #endif
747                 dns_close(this->myfd);
748         }
749         if (result)
750         {
751                 statsDnsGood++;
752                 unsigned char a = (unsigned)result[0];
753                 unsigned char b = (unsigned)result[1];
754                 unsigned char c = (unsigned)result[2];
755                 unsigned char d = (unsigned)result[3];
756                 snprintf(r,1024,"%u.%u.%u.%u",a,b,c,d);
757                 return r;
758         }
759         else
760         {
761                 statsDnsBad++;
762                 log(DEBUG,"DANGER WILL ROBINSON! NXDOMAIN for forward lookup, but we got a reverse lookup!");
763                 return "";
764         }
765 }
766