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