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