ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/irc_res.c
Revision: 1325
Committed: Sat Mar 31 10:29:02 2012 UTC (14 years, 6 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/irc_res.c
File size: 22679 byte(s)
Log Message:
- Get rid of fileio.c. Replace some ircsprintf() with snprintf() while on it

File Contents

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

Properties

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