ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/res.c
Revision: 3235
Committed: Sat Mar 29 19:34:16 2014 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/irc_res.c
File size: 23321 byte(s)
Log Message:
- Style corrections all over the place

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file irc_res.c
23 * \brief ircd resolver functions
24 * \version $Id$
25 */
26
27 /*
28 * A rewrite of Darren Reeds original res.c As there is nothing
29 * left of Darrens original code, this is now licensed by the hybrid group.
30 * (Well, some of the function names are the same, and bits of the structs..)
31 * You can use it where it is useful, free even. Buy us a beer and stuff.
32 *
33 * The authors takes no responsibility for any damage or loss
34 * of property which results from the use of this software.
35 *
36 * $Id$
37 *
38 * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
39 * added callbacks and reference counting of returned hostents.
40 * --Bleep (Thomas Helvey <tomh@inxpress.net>)
41 *
42 * This was all needlessly complicated for irc. Simplified. No more hostent
43 * All we really care about is the IP -> hostname mappings. Thats all.
44 *
45 * Apr 28, 2003 --cryogen and Dianora
46 */
47
48 #include "stdinc.h"
49 #include "list.h"
50 #include "client.h"
51 #include "event.h"
52 #include "irc_string.h"
53 #include "ircd.h"
54 #include "numeric.h"
55 #include "rng_mt.h"
56 #include "fdlist.h"
57 #include "s_bsd.h"
58 #include "log.h"
59 #include "s_misc.h"
60 #include "send.h"
61 #include "memory.h"
62 #include "mempool.h"
63 #include "irc_res.h"
64 #include "irc_reslib.h"
65
66 #if (CHAR_BIT != 8)
67 #error this code needs to be able to address individual octets
68 #endif
69
70 static PF res_readreply;
71
72 #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */
73 #define RES_MAXALIASES 35 /* maximum aliases allowed */
74 #define RES_MAXADDRS 35 /* maximum addresses allowed */
75 #define AR_TTL 600 /* TTL in seconds for dns cache entries */
76
77 /* RFC 1104/1105 wasn't very helpful about what these fields
78 * should be named, so for now, we'll just name them this way.
79 * we probably should look at what named calls them or something.
80 */
81 #define TYPE_SIZE (size_t)2
82 #define CLASS_SIZE (size_t)2
83 #define TTL_SIZE (size_t)4
84 #define RDLENGTH_SIZE (size_t)2
85 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
86
87 typedef enum
88 {
89 REQ_IDLE, /* We're doing not much at all */
90 REQ_PTR, /* Looking up a PTR */
91 REQ_A, /* Looking up an A, possibly because AAAA failed */
92 #ifdef IPV6
93 REQ_AAAA, /* Looking up an AAAA */
94 #endif
95 REQ_CNAME /* We got a CNAME in response, we better get a real answer next */
96 } request_state;
97
98 struct reslist
99 {
100 dlink_node node;
101 int id;
102 int sent; /* number of requests sent */
103 request_state state; /* State the resolver machine is in */
104 time_t ttl;
105 char type;
106 char retries; /* retry counter */
107 char sends; /* number of sends (>1 means resent) */
108 char resend; /* send flag. 0 == dont resend */
109 time_t sentat;
110 time_t timeout;
111 struct irc_ssaddr addr;
112 char *name;
113 dns_callback_fnc callback;
114 void *callback_ctx;
115 };
116
117 static fde_t ResolverFileDescriptor;
118 static dlink_list request_list;
119 static mp_pool_t *dns_pool;
120
121 static void rem_request(struct reslist *);
122 static struct reslist *make_request(dns_callback_fnc, void *);
123 static void do_query_name(dns_callback_fnc, void *,
124 const char *, struct reslist *, int);
125 static void do_query_number(dns_callback_fnc, void *,
126 const struct irc_ssaddr *,
127 struct reslist *);
128 static void query_name(const char *, int, int, struct reslist *);
129 static int send_res_msg(const char *, int, int);
130 static void resend_query(struct reslist *);
131 static int proc_answer(struct reslist *, HEADER *, char *, char *);
132 static struct reslist *find_id(int);
133
134
135 /*
136 * int
137 * res_ourserver(inp)
138 * looks up "inp" in irc_nsaddr_list[]
139 * returns:
140 * 0 : not found
141 * >0 : found
142 * author:
143 * paul vixie, 29may94
144 * revised for ircd, cryogen(stu) may03
145 */
146 static int
147 res_ourserver(const struct irc_ssaddr *inp)
148 {
149 #ifdef IPV6
150 const struct sockaddr_in6 *v6;
151 const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
152 #endif
153 const struct sockaddr_in *v4;
154 const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
155
156 for (int i = 0; i < irc_nscount; ++i)
157 {
158 const struct irc_ssaddr *srv = &irc_nsaddr_list[i];
159 #ifdef IPV6
160 v6 = (const struct sockaddr_in6 *)srv;
161 #endif
162 v4 = (const struct sockaddr_in *)srv;
163
164 /* could probably just memcmp(srv, inp, srv.ss_len) here
165 * but we'll air on the side of caution - stu
166 *
167 */
168 switch (srv->ss.ss_family)
169 {
170 #ifdef IPV6
171 case AF_INET6:
172 if (srv->ss.ss_family == inp->ss.ss_family)
173 if (v6->sin6_port == v6in->sin6_port)
174 if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
175 sizeof(struct in6_addr)))
176 return 1;
177 break;
178 #endif
179 case AF_INET:
180 if (srv->ss.ss_family == inp->ss.ss_family)
181 if (v4->sin_port == v4in->sin_port)
182 if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)
183 return 1;
184 break;
185 default:
186 break;
187 }
188 }
189
190 return 0;
191 }
192
193 /*
194 * timeout_query_list - Remove queries from the list which have been
195 * there too long without being resolved.
196 */
197 static time_t
198 timeout_query_list(time_t now)
199 {
200 dlink_node *ptr = NULL, *ptr_next = NULL;
201 struct reslist *request;
202 time_t next_time = 0;
203 time_t timeout = 0;
204
205 DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
206 {
207 request = ptr->data;
208 timeout = request->sentat + request->timeout;
209
210 if (now >= timeout)
211 {
212 if (--request->retries <= 0)
213 {
214 (*request->callback)(request->callback_ctx, NULL, NULL);
215 rem_request(request);
216 continue;
217 }
218 else
219 {
220 request->sentat = now;
221 request->timeout += request->timeout;
222 resend_query(request);
223 }
224 }
225
226 if ((next_time == 0) || timeout < next_time)
227 next_time = timeout;
228 }
229
230 return (next_time > now) ? next_time : (now + AR_TTL);
231 }
232
233 /*
234 * timeout_resolver - check request list
235 */
236 static void
237 timeout_resolver(void *notused)
238 {
239 timeout_query_list(CurrentTime);
240 }
241
242 /*
243 * start_resolver - do everything we need to read the resolv.conf file
244 * and initialize the resolver file descriptor if needed
245 */
246 static void
247 start_resolver(void)
248 {
249 irc_res_init();
250
251 if (!ResolverFileDescriptor.flags.open)
252 {
253 if (comm_open(&ResolverFileDescriptor, irc_nsaddr_list[0].ss.ss_family,
254 SOCK_DGRAM, 0, "Resolver socket") == -1)
255 return;
256
257 /* At the moment, the resolver FD data is global .. */
258 comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ,
259 res_readreply, NULL, 0);
260 eventAdd("timeout_resolver", timeout_resolver, NULL, 1);
261 }
262 }
263
264 /*
265 * init_resolver - initialize resolver and resolver library
266 */
267 void
268 init_resolver(void)
269 {
270 dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
271 memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
272 start_resolver();
273 }
274
275 /*
276 * restart_resolver - reread resolv.conf, reopen socket
277 */
278 void
279 restart_resolver(void)
280 {
281 fd_close(&ResolverFileDescriptor);
282 eventDelete(timeout_resolver, NULL); /* -ddosen */
283 start_resolver();
284 }
285
286 /*
287 * rem_request - remove a request from the list.
288 * This must also free any memory that has been allocated for
289 * temporary storage of DNS results.
290 */
291 static void
292 rem_request(struct reslist *request)
293 {
294 dlinkDelete(&request->node, &request_list);
295
296 MyFree(request->name);
297 mp_pool_release(request);
298 }
299
300 /*
301 * make_request - Create a DNS request record for the server.
302 */
303 static struct reslist *
304 make_request(dns_callback_fnc callback, void *ctx)
305 {
306 struct reslist *request = mp_pool_get(dns_pool);
307
308 memset(request, 0, sizeof(*request));
309 request->sentat = CurrentTime;
310 request->retries = 3;
311 request->resend = 1;
312 request->timeout = 4; /* start at 4 and exponential inc. */
313 request->state = REQ_IDLE;
314 request->callback = callback;
315 request->callback_ctx = ctx;
316
317 dlinkAdd(request, &request->node, &request_list);
318 return request;
319 }
320
321 /*
322 * delete_resolver_queries - cleanup outstanding queries
323 * for which there no longer exist clients or conf lines.
324 */
325 void
326 delete_resolver_queries(const void *vptr)
327 {
328 dlink_node *ptr = NULL, *ptr_next = NULL;
329
330 DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
331 {
332 struct reslist *request = ptr->data;
333
334 if (request->callback_ctx == vptr)
335 rem_request(request);
336 }
337 }
338
339 /*
340 * send_res_msg - sends msg to all nameservers found in the "_res" structure.
341 * This should reflect /etc/resolv.conf. We will get responses
342 * which arent needed but is easier than checking to see if nameserver
343 * isnt present. Returns number of messages successfully sent to
344 * nameservers or -1 if no successful sends.
345 */
346 static int
347 send_res_msg(const char *msg, int len, int rcount)
348 {
349 int sent = 0;
350 int max_queries = IRCD_MIN(irc_nscount, rcount);
351
352 /* RES_PRIMARY option is not implemented
353 * if (res.options & RES_PRIMARY || 0 == max_queries)
354 */
355 if (max_queries == 0)
356 max_queries = 1;
357
358 for (int i = 0; i < max_queries; ++i)
359 {
360 if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
361 (struct sockaddr*)&(irc_nsaddr_list[i]),
362 irc_nsaddr_list[i].ss_len) == len)
363 ++sent;
364 }
365
366 return sent;
367 }
368
369 /*
370 * find_id - find a dns request id (id is determined by dn_mkquery)
371 */
372 static struct reslist *
373 find_id(int id)
374 {
375 dlink_node *ptr = NULL;
376
377 DLINK_FOREACH(ptr, request_list.head)
378 {
379 struct reslist *request = ptr->data;
380
381 if (request->id == id)
382 return request;
383 }
384
385 return NULL;
386 }
387
388 /*
389 * gethost_byname_type - get host address from name
390 *
391 */
392 void
393 gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
394 {
395 assert(name);
396 do_query_name(callback, ctx, name, NULL, type);
397 }
398
399 /*
400 * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
401 */
402 void
403 gethost_byname(dns_callback_fnc callback, void *ctx, const char *name)
404 {
405 #ifdef IPV6
406 gethost_byname_type(callback, ctx, name, T_AAAA);
407 #else
408 gethost_byname_type(callback, ctx, name, T_A);
409 #endif
410 }
411
412 /*
413 * gethost_byaddr - get host name from address
414 */
415 void
416 gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
417 {
418 do_query_number(callback, ctx, addr, NULL);
419 }
420
421 /*
422 * do_query_name - nameserver lookup name
423 */
424 static void
425 do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
426 struct reslist *request, int type)
427 {
428 char host_name[HOSTLEN + 1];
429
430 strlcpy(host_name, name, sizeof(host_name));
431
432 if (request == NULL)
433 {
434 request = make_request(callback, ctx);
435 request->name = MyMalloc(strlen(host_name) + 1);
436 request->type = type;
437 strcpy(request->name, host_name);
438 #ifdef IPV6
439 if (type != T_A)
440 request->state = REQ_AAAA;
441 else
442 #endif
443 request->state = REQ_A;
444 }
445
446 request->type = type;
447 query_name(host_name, C_IN, type, request);
448 }
449
450 /*
451 * do_query_number - Use this to do reverse IP# lookups.
452 */
453 static void
454 do_query_number(dns_callback_fnc callback, void *ctx,
455 const struct irc_ssaddr *addr,
456 struct reslist *request)
457 {
458 char ipbuf[128];
459 const unsigned char *cp;
460
461 if (addr->ss.ss_family == AF_INET)
462 {
463 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
464 cp = (const unsigned char *)&v4->sin_addr.s_addr;
465
466 snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
467 (unsigned int)(cp[3]), (unsigned int)(cp[2]),
468 (unsigned int)(cp[1]), (unsigned int)(cp[0]));
469 }
470 #ifdef IPV6
471 else if (addr->ss.ss_family == AF_INET6)
472 {
473 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
474 cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
475
476 snprintf(ipbuf, sizeof(ipbuf),
477 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
478 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
479 (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
480 (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
481 (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
482 (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
483 (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
484 (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
485 (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
486 (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
487 (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
488 (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
489 (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
490 (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
491 (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
492 (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
493 (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
494 (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
495 }
496 #endif
497 if (request == NULL)
498 {
499 request = make_request(callback, ctx);
500 request->type = T_PTR;
501 memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
502 request->name = MyMalloc(HOSTLEN + 1);
503 }
504
505 query_name(ipbuf, C_IN, T_PTR, request);
506 }
507
508 /*
509 * query_name - generate a query based on class, type and name.
510 */
511 static void
512 query_name(const char *name, int query_class, int type,
513 struct reslist *request)
514 {
515 char buf[MAXPACKET];
516 int request_len = 0;
517
518 memset(buf, 0, sizeof(buf));
519
520 if ((request_len = irc_res_mkquery(name, query_class, type,
521 (unsigned char *)buf, sizeof(buf))) > 0)
522 {
523 HEADER *header = (HEADER *)buf;
524
525 /*
526 * generate an unique id
527 * NOTE: we don't have to worry about converting this to and from
528 * network byte order, the nameserver does not interpret this value
529 * and returns it unchanged
530 */
531 do
532 header->id = (header->id + genrand_int32()) & 0xffff;
533 while (find_id(header->id));
534
535 request->id = header->id;
536 ++request->sends;
537
538 request->sent += send_res_msg(buf, request_len, request->sends);
539 }
540 }
541
542 static void
543 resend_query(struct reslist *request)
544 {
545 if (request->resend == 0)
546 return;
547
548 switch (request->type)
549 {
550 case T_PTR:
551 do_query_number(NULL, NULL, &request->addr, request);
552 break;
553 case T_A:
554 do_query_name(NULL, NULL, request->name, request, request->type);
555 break;
556 #ifdef IPV6
557 case T_AAAA:
558 /* didnt work, try A */
559 if (request->state == REQ_AAAA)
560 do_query_name(NULL, NULL, request->name, request, T_A);
561 #endif
562 default:
563 break;
564 }
565 }
566
567 /*
568 * proc_answer - process name server reply
569 */
570 static int
571 proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob)
572 {
573 char hostbuf[HOSTLEN + 100]; /* working buffer */
574 unsigned char *current; /* current position in buf */
575 int query_class; /* answer class */
576 int type; /* answer type */
577 int n; /* temp count */
578 int rd_length;
579 struct sockaddr_in *v4; /* conversion */
580 #ifdef IPV6
581 struct sockaddr_in6 *v6;
582 #endif
583 current = (unsigned char *)buf + sizeof(HEADER);
584
585 for (; header->qdcount > 0; --header->qdcount)
586 {
587 if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
588 break;
589
590 current += (size_t)n + QFIXEDSZ;
591 }
592
593 /*
594 * process each answer sent to us blech.
595 */
596 while (header->ancount > 0 && (char *)current < eob)
597 {
598 header->ancount--;
599
600 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
601 hostbuf, sizeof(hostbuf));
602
603 if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
604 return 0;
605
606 hostbuf[HOSTLEN] = '\0';
607
608 /* With Address arithmetic you have to be very anal
609 * this code was not working on alpha due to that
610 * (spotted by rodder/jailbird/dianora)
611 */
612 current += (size_t) n;
613
614 if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
615 break;
616
617 type = irc_ns_get16(current);
618 current += TYPE_SIZE;
619
620 query_class = irc_ns_get16(current);
621 current += CLASS_SIZE;
622
623 request->ttl = irc_ns_get32(current);
624 current += TTL_SIZE;
625
626 rd_length = irc_ns_get16(current);
627 current += RDLENGTH_SIZE;
628
629 /*
630 * Wait to set request->type until we verify this structure
631 */
632 switch (type)
633 {
634 case T_A:
635 if (request->type != T_A)
636 return 0;
637
638 /*
639 * check for invalid rd_length or too many addresses
640 */
641 if (rd_length != sizeof(struct in_addr))
642 return 0;
643
644 v4 = (struct sockaddr_in *)&request->addr;
645 request->addr.ss_len = sizeof(struct sockaddr_in);
646 v4->sin_family = AF_INET;
647 memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
648 return 1;
649 break;
650 #ifdef IPV6
651 case T_AAAA:
652 if (request->type != T_AAAA)
653 return 0;
654
655 if (rd_length != sizeof(struct in6_addr))
656 return 0;
657
658 request->addr.ss_len = sizeof(struct sockaddr_in6);
659 v6 = (struct sockaddr_in6 *)&request->addr;
660 v6->sin6_family = AF_INET6;
661 memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
662 return 1;
663 break;
664 #endif
665 case T_PTR:
666 if (request->type != T_PTR)
667 return 0;
668
669 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
670 current, hostbuf, sizeof(hostbuf));
671 if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
672 return 0;
673
674 strlcpy(request->name, hostbuf, HOSTLEN + 1);
675 return 1;
676 break;
677 case T_CNAME: /* first check we already havent started looking
678 into a cname */
679 if (request->type != T_PTR)
680 return 0;
681
682 if (request->state == REQ_CNAME)
683 {
684 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
685 current, hostbuf, sizeof(hostbuf));
686
687 if (n < 0)
688 return 0;
689 return 1;
690 }
691
692 request->state = REQ_CNAME;
693 current += rd_length;
694 break;
695
696 default:
697 /* XXX I'd rather just throw away the entire bogus thing
698 * but its possible its just a broken nameserver with still
699 * valid answers. But lets do some rudimentary logging for now...
700 */
701 ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type);
702 break;
703 }
704 }
705
706 return 1;
707 }
708
709 /*
710 * res_readreply - read a dns reply from the nameserver and process it.
711 */
712 static void
713 res_readreply(fde_t *fd, void *data)
714 {
715 char buf[sizeof(HEADER) + MAXPACKET]
716 /* Sparc and alpha need 16bit-alignment for accessing header->id
717 * (which is uint16_t). Because of the header = (HEADER*) buf;
718 * lateron, this is neeeded. --FaUl
719 */
720 #if defined(__sparc__) || defined(__alpha__)
721 __attribute__((aligned (16)))
722 #endif
723 ;
724 HEADER *header;
725 struct reslist *request = NULL;
726 int rc;
727 socklen_t len = sizeof(struct irc_ssaddr);
728 struct irc_ssaddr lsin;
729
730 rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
731
732 /* Re-schedule a read *after* recvfrom, or we'll be registering
733 * interest where it'll instantly be ready for read :-) -- adrian
734 */
735 comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
736
737 /* Better to cast the sizeof instead of rc */
738 if (rc <= (int)(sizeof(HEADER)))
739 return;
740
741 /*
742 * convert DNS reply reader from Network byte order to CPU byte order.
743 */
744 header = (HEADER *)buf;
745 header->ancount = ntohs(header->ancount);
746 header->qdcount = ntohs(header->qdcount);
747 header->nscount = ntohs(header->nscount);
748 header->arcount = ntohs(header->arcount);
749
750 /*
751 * check against possibly fake replies
752 */
753 if (!res_ourserver(&lsin))
754 return;
755
756 /*
757 * response for an id which we have already received an answer for
758 * just ignore this response.
759 */
760 if (!(request = find_id(header->id)))
761 return;
762
763 if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
764 {
765 if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
766 {
767 /*
768 * If a bad error was returned, stop here and don't
769 * send any more (no retries granted).
770 */
771 (*request->callback)(request->callback_ctx, NULL, NULL);
772 rem_request(request);
773 }
774 #ifdef IPV6
775 else
776 {
777 /*
778 * If we havent already tried this, and we're looking up AAAA, try A
779 * now
780 */
781 if (request->state == REQ_AAAA && request->type == T_AAAA)
782 {
783 request->timeout += 4;
784 resend_query(request);
785 }
786 }
787 #endif
788
789 return;
790 }
791
792 /*
793 * If this fails there was an error decoding the received packet,
794 * try it again and hope it works the next time.
795 */
796 if (proc_answer(request, header, buf, buf + rc))
797 {
798 if (request->type == T_PTR)
799 {
800 if (request->name == NULL)
801 {
802 /*
803 * got a PTR response with no name, something bogus is happening
804 * don't bother trying again, the client address doesn't resolve
805 */
806 (*request->callback)(request->callback_ctx, NULL, NULL);
807 rem_request(request);
808 return;
809 }
810
811 /*
812 * Lookup the 'authoritative' name that we were given for the
813 * ip#.
814 *
815 */
816 #ifdef IPV6
817 if (request->addr.ss.ss_family == AF_INET6)
818 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
819 else
820 #endif
821 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
822 rem_request(request);
823 }
824 else
825 {
826 /*
827 * got a name and address response, client resolved
828 */
829 (*request->callback)(request->callback_ctx, &request->addr, request->name);
830 rem_request(request);
831 }
832 }
833 else if (!request->sent)
834 {
835 /* XXX - we got a response for a query we didn't send with a valid id?
836 * this should never happen, bail here and leave the client unresolved
837 */
838 assert(0);
839
840 /* XXX don't leak it */
841 rem_request(request);
842 }
843 }
844
845 void
846 report_dns_servers(struct Client *source_p)
847 {
848 char ipaddr[HOSTIPLEN + 1] = "";
849
850 for (int i = 0; i < irc_nscount; ++i)
851 {
852 getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
853 irc_nsaddr_list[i].ss_len, ipaddr,
854 sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
855 sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr);
856 }
857 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision