ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/res.c
Revision: 8362
Committed: Sun Mar 4 14:26:48 2018 UTC (8 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 19680 byte(s)
Error occurred while calculating annotation data.
Log Message:
- res.c:restart_resolver(): Added sanity test. ResolverFileDescriptor might be NULL at this point

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2018 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file res.c
23 * \brief ircd resolver functions
24 * \version $Id$
25 */
26
27 /*
28 * A rewrite of Darren Reed's original res.c As there is nothing
29 * left of Darren's 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 * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
37 * added callbacks and reference counting of returned hostents.
38 * --Bleep (Thomas Helvey <tomh@inxpress.net>)
39 *
40 * This was all needlessly complicated for irc. Simplified. No more hostent
41 * All we really care about is the IP -> hostname mappings. That's all.
42 *
43 * Apr 28, 2003 --cryogen and Dianora
44 */
45
46 #include "stdinc.h"
47 #include "list.h"
48 #include "event.h"
49 #include "irc_string.h"
50 #include "ircd.h"
51 #include "rng_mt.h"
52 #include "fdlist.h"
53 #include "s_bsd.h"
54 #include "misc.h"
55 #include "mempool.h"
56 #include "res.h"
57 #include "reslib.h"
58
59 #if (CHAR_BIT != 8)
60 #error this code needs to be able to address individual octets
61 #endif
62
63 static void res_readreply(fde_t *, void *);
64
65 #define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */
66 #define AR_TTL 600 /**< TTL in seconds for dns cache entries */
67
68 /*
69 * RFC 1104/1105 wasn't very helpful about what these fields
70 * should be named, so for now, we'll just name them this way.
71 * We probably should look at what named calls them or something.
72 */
73 #define TYPE_SIZE (size_t)2
74 #define CLASS_SIZE (size_t)2
75 #define TTL_SIZE (size_t)4
76 #define RDLENGTH_SIZE (size_t)2
77 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
78
79 struct reslist
80 {
81 dlink_node node; /**< Doubly linked list node. */
82 unsigned int id; /**< Request ID (from request header). */
83 char type; /**< Current request type. */
84 char retries; /**< Retry counter */
85 unsigned int sends; /**< Number of sends (>1 means resent). */
86 uintmax_t sentat; /**< Timestamp we last sent this request. */
87 uintmax_t timeout; /**< When this request times out. */
88 struct irc_ssaddr addr; /**< Address for this request. */
89 char name[RFC1035_MAX_DOMAIN_LENGTH + 1]; /**< Hostname for this request. */
90 size_t namelength; /**< Actual hostname length. */
91 dns_callback_fnc callback; /**< Callback function on completion. */
92 void *callback_ctx; /**< Context pointer for callback. */
93 };
94
95 static fde_t *ResolverFileDescriptor;
96 static dlink_list request_list;
97 static mp_pool_t *dns_pool;
98
99
100 /*
101 * rem_request - remove a request from the list.
102 * This must also free any memory that has been allocated for
103 * temporary storage of DNS results.
104 */
105 static void
106 rem_request(struct reslist *request)
107 {
108 dlinkDelete(&request->node, &request_list);
109 mp_pool_release(request);
110 }
111
112 /*
113 * make_request - Create a DNS request record for the server.
114 */
115 static struct reslist *
116 make_request(dns_callback_fnc callback, void *ctx)
117 {
118 struct reslist *request = mp_pool_get(dns_pool);
119
120 request->sentat = CurrentTime;
121 request->retries = 2;
122 request->timeout = 4; /* Start at 4 and exponential inc. */
123 request->callback = callback;
124 request->callback_ctx = ctx;
125
126 dlinkAdd(request, &request->node, &request_list);
127 return request;
128 }
129
130 /*
131 * int
132 * res_ourserver(inp)
133 * looks up "inp" in irc_nsaddr_list[]
134 * returns:
135 * 0 : not found
136 * >0 : found
137 * author:
138 * paul vixie, 29may94
139 * revised for ircd, cryogen(stu) may03
140 */
141 static int
142 res_ourserver(const struct irc_ssaddr *inp)
143 {
144 const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
145 const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
146
147 for (unsigned int i = 0; i < irc_nscount; ++i)
148 {
149 const struct irc_ssaddr *srv = &irc_nsaddr_list[i];
150 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)srv;
151 const struct sockaddr_in *v4 = (const struct sockaddr_in *)srv;
152
153 /*
154 * Could probably just memcmp(srv, inp, srv.ss_len) here
155 * but we'll air on the side of caution - stu
156 */
157 switch (srv->ss.ss_family)
158 {
159 case AF_INET6:
160 if (srv->ss.ss_family == inp->ss.ss_family)
161 if (v6->sin6_port == v6in->sin6_port)
162 if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
163 sizeof(struct in6_addr)))
164 return 1;
165 break;
166 case AF_INET:
167 if (srv->ss.ss_family == inp->ss.ss_family)
168 if (v4->sin_port == v4in->sin_port)
169 if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)
170 return 1;
171 break;
172 default:
173 break;
174 }
175 }
176
177 return 0;
178 }
179
180 /*
181 * start_resolver - do everything we need to read the resolv.conf file
182 * and initialize the resolver file descriptor if needed
183 */
184 static void
185 start_resolver(void)
186 {
187 irc_res_init();
188
189 if (!ResolverFileDescriptor)
190 {
191 int fd = comm_socket(irc_nsaddr_list[0].ss.ss_family, SOCK_DGRAM, 0);
192 if (fd == -1)
193 return;
194
195 ResolverFileDescriptor = fd_open(fd, 1, "UDP resolver socket");
196
197 /* At the moment, the resolver FD data is global .. */
198 comm_setselect(ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0);
199 }
200 }
201
202 /*
203 * restart_resolver - reread resolv.conf, reopen socket
204 */
205 void
206 restart_resolver(void)
207 {
208 if (ResolverFileDescriptor)
209 {
210 fd_close(ResolverFileDescriptor);
211 ResolverFileDescriptor = NULL;
212 }
213
214 start_resolver();
215 }
216
217 /*
218 * delete_resolver_queries - cleanup outstanding queries
219 * for which there no longer exist clients or conf lines.
220 */
221 void
222 delete_resolver_queries(const void *vptr)
223 {
224 dlink_node *node, *node_next;
225
226 DLINK_FOREACH_SAFE(node, node_next, request_list.head)
227 {
228 struct reslist *request = node->data;
229
230 if (request->callback_ctx == vptr)
231 rem_request(request);
232 }
233 }
234
235 /*
236 * send_res_msg - sends msg to all nameservers found in the "_res" structure.
237 * This should reflect /etc/resolv.conf. We will get responses
238 * which arent needed but is easier than checking to see if nameserver
239 * isn't present. Returns number of messages successfully sent to
240 * nameservers or -1 if no successful sends.
241 */
242 static void
243 send_res_msg(const unsigned char *msg, int len, unsigned int rcount)
244 {
245 unsigned int max_queries = IRCD_MIN(irc_nscount, rcount);
246
247 /* RES_PRIMARY option is not implemented
248 * if (res.options & RES_PRIMARY || 0 == max_queries)
249 */
250 if (max_queries == 0)
251 max_queries = 1;
252
253 for (unsigned int i = 0; i < max_queries; ++i)
254 sendto(ResolverFileDescriptor->fd, msg, len, 0,
255 (struct sockaddr *)&irc_nsaddr_list[i], irc_nsaddr_list[i].ss_len);
256 }
257
258 /*
259 * find_id - find a dns request id (id is determined by dn_mkquery)
260 */
261 static struct reslist *
262 find_id(unsigned int id)
263 {
264 dlink_node *node;
265
266 DLINK_FOREACH(node, request_list.head)
267 {
268 struct reslist *request = node->data;
269
270 if (request->id == id)
271 return request;
272 }
273
274 return NULL;
275 }
276
277 /*
278 * query_name - generate a query based on class, type and name.
279 */
280 static void
281 query_name(const char *name, int query_class, int type, struct reslist *request)
282 {
283 unsigned char buf[MAXPACKET];
284 int request_len = 0;
285
286 memset(buf, 0, sizeof(buf));
287
288 if ((request_len = irc_res_mkquery(name, query_class, type, buf, sizeof(buf))) > 0)
289 {
290 HEADER *header = (HEADER *)buf;
291
292 /*
293 * Generate an unique id.
294 * NOTE: we don't have to worry about converting this to and from
295 * network byte order, the nameserver does not interpret this value
296 * and returns it unchanged.
297 */
298 do
299 header->id = (header->id + genrand_int32()) & 0xFFFF;
300 while (find_id(header->id));
301
302 request->id = header->id;
303 ++request->sends;
304
305 send_res_msg(buf, request_len, request->sends);
306 }
307 }
308
309 /*
310 * do_query_name - nameserver lookup name
311 */
312 static void
313 do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
314 struct reslist *request, int type)
315 {
316 char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1];
317
318 strlcpy(host_name, name, sizeof(host_name));
319
320 if (!request)
321 {
322 request = make_request(callback, ctx);
323 request->type = type;
324 request->namelength = strlcpy(request->name, host_name, sizeof(request->name));
325 }
326
327 request->type = type;
328 query_name(host_name, C_IN, type, request);
329 }
330
331 /*
332 * do_query_number - Use this to do reverse IP# lookups.
333 */
334 static void
335 do_query_number(dns_callback_fnc callback, void *ctx,
336 const struct irc_ssaddr *addr,
337 struct reslist *request)
338 {
339 char ipbuf[128] = "";
340
341 if (addr->ss.ss_family == AF_INET)
342 {
343 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
344 const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr;
345
346 snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
347 (unsigned int)(cp[3]), (unsigned int)(cp[2]),
348 (unsigned int)(cp[1]), (unsigned int)(cp[0]));
349 }
350 else if (addr->ss.ss_family == AF_INET6)
351 {
352 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
353 const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
354
355 snprintf(ipbuf, sizeof(ipbuf),
356 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
357 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
358 (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
359 (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
360 (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
361 (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
362 (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
363 (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
364 (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
365 (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
366 (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
367 (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
368 (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
369 (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
370 (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
371 (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
372 (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
373 (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
374 }
375
376 if (!request)
377 {
378 request = make_request(callback, ctx);
379 request->type = T_PTR;
380 memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
381 }
382
383 query_name(ipbuf, C_IN, T_PTR, request);
384 }
385
386 /*
387 * gethost_byname_type - get host address from name
388 *
389 */
390 void
391 gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
392 {
393 assert(name);
394 do_query_name(callback, ctx, name, NULL, type);
395 }
396
397 /*
398 * gethost_byaddr - get host name from address
399 */
400 void
401 gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
402 {
403 do_query_number(callback, ctx, addr, NULL);
404 }
405
406 static void
407 resend_query(struct reslist *request)
408 {
409 switch (request->type)
410 {
411 case T_PTR:
412 do_query_number(NULL, NULL, &request->addr, request);
413 break;
414 case T_A:
415 case T_AAAA:
416 do_query_name(NULL, NULL, request->name, request, request->type);
417 break;
418 default:
419 break;
420 }
421 }
422
423 /*
424 * proc_answer - process name server reply
425 */
426 static int
427 proc_answer(struct reslist *request, HEADER *header, unsigned char *buf, unsigned char *eob)
428 {
429 char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */
430 unsigned char *current = buf + sizeof(HEADER); /* current position in buf */
431 unsigned int type = 0; /* answer type */
432 unsigned int rd_length = 0;
433 int n; /* temp count */
434 struct sockaddr_in *v4; /* conversion */
435 struct sockaddr_in6 *v6;
436
437 for (; header->qdcount > 0; --header->qdcount)
438 {
439 if ((n = irc_dn_skipname(current, eob)) < 0)
440 break;
441
442 current += (size_t)n + QFIXEDSZ;
443 }
444
445 /*
446 * Process each answer sent to us blech.
447 */
448 while (header->ancount > 0 && current < eob)
449 {
450 --header->ancount;
451
452 n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
453
454 if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
455 return 0;
456
457 hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0';
458
459 /*
460 * With Address arithmetic you have to be very anal
461 * this code was not working on alpha due to that
462 * (spotted by rodder/jailbird/dianora)
463 */
464 current += (size_t)n;
465
466 if (!((current + ANSWER_FIXED_SIZE) < eob))
467 break;
468
469 type = irc_ns_get16(current);
470 current += TYPE_SIZE;
471 current += CLASS_SIZE;
472 current += TTL_SIZE;
473 rd_length = irc_ns_get16(current);
474 current += RDLENGTH_SIZE;
475
476 /*
477 * Wait to set request->type until we verify this structure
478 */
479 switch (type)
480 {
481 case T_A:
482 if (request->type != T_A)
483 return 0;
484
485 /*
486 * Check for invalid rd_length or too many addresses
487 */
488 if (rd_length != sizeof(struct in_addr))
489 return 0;
490
491 request->addr.ss_len = sizeof(struct sockaddr_in);
492 v4 = (struct sockaddr_in *)&request->addr;
493 v4->sin_family = AF_INET;
494 memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
495 return 1;
496 break;
497 case T_AAAA:
498 if (request->type != T_AAAA)
499 return 0;
500
501 if (rd_length != sizeof(struct in6_addr))
502 return 0;
503
504 request->addr.ss_len = sizeof(struct sockaddr_in6);
505 v6 = (struct sockaddr_in6 *)&request->addr;
506 v6->sin6_family = AF_INET6;
507 memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
508 return 1;
509 break;
510 case T_PTR:
511 if (request->type != T_PTR)
512 return 0;
513
514 n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
515 if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
516 return 0;
517
518 request->namelength = strlcpy(request->name, hostbuf, sizeof(request->name));
519 return 1;
520 break;
521 case T_CNAME:
522 current += rd_length;
523 break;
524 default:
525 return 0;
526 break;
527 }
528 }
529
530 return 0;
531 }
532
533 /*
534 * res_readreply - read a dns reply from the nameserver and process it.
535 */
536 static void
537 res_readreply(fde_t *F, void *data)
538 {
539 unsigned char buf[sizeof(HEADER) + MAXPACKET];
540 struct reslist *request = NULL;
541 ssize_t rc = 0;
542 socklen_t len = sizeof(struct irc_ssaddr);
543 struct irc_ssaddr lsin;
544
545 while ((rc = recvfrom(F->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1)
546 {
547 if (rc <= (ssize_t)sizeof(HEADER))
548 continue;
549
550 /*
551 * Check against possibly fake replies
552 */
553 if (!res_ourserver(&lsin))
554 continue;
555
556 /*
557 * Convert DNS reply reader from Network byte order to CPU byte order.
558 */
559 HEADER *header = (HEADER *)buf;
560 header->ancount = ntohs(header->ancount);
561 header->qdcount = ntohs(header->qdcount);
562 header->nscount = ntohs(header->nscount);
563 header->arcount = ntohs(header->arcount);
564
565 /*
566 * Response for an id which we have already received an answer for
567 * just ignore this response.
568 */
569 if ((request = find_id(header->id)) == NULL)
570 continue;
571
572 if (header->rcode != NO_ERRORS || header->ancount == 0)
573 {
574 /*
575 * If a bad error was returned, stop here and don't send
576 * any more (no retries granted).
577 */
578 (*request->callback)(request->callback_ctx, NULL, NULL, 0);
579 rem_request(request);
580 continue;
581 }
582
583 /*
584 * If this fails there was an error decoding the received packet.
585 * We only give it one shot. If it fails, just leave the client
586 * unresolved.
587 */
588 if (!proc_answer(request, header, buf, buf + rc))
589 {
590 (*request->callback)(request->callback_ctx, NULL, NULL, 0);
591 rem_request(request);
592 continue;
593 }
594
595 if (request->type == T_PTR)
596 {
597 if (request->namelength == 0)
598 {
599 /*
600 * Got a PTR response with no name, something bogus is happening
601 * don't bother trying again, the client address doesn't resolve
602 */
603 (*request->callback)(request->callback_ctx, NULL, NULL, 0);
604 rem_request(request);
605 continue;
606 }
607
608 /*
609 * Lookup the 'authoritative' name that we were given for the ip#.
610 */
611 if (request->addr.ss.ss_family == AF_INET6)
612 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
613 else
614 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
615
616 rem_request(request);
617 }
618 else
619 {
620 /*
621 * Got a name and address response, client resolved
622 */
623 (*request->callback)(request->callback_ctx, &request->addr, request->name, request->namelength);
624 rem_request(request);
625 }
626
627 continue;
628 }
629
630 comm_setselect(F, COMM_SELECT_READ, res_readreply, NULL, 0);
631 }
632
633 /*
634 * timeout_query_list - Remove queries from the list which have been
635 * there too long without being resolved.
636 */
637 static uintmax_t
638 timeout_query_list(void)
639 {
640 dlink_node *node, *node_next;
641 uintmax_t next_time = 0;
642
643 DLINK_FOREACH_SAFE(node, node_next, request_list.head)
644 {
645 struct reslist *request = node->data;
646 uintmax_t timeout = request->sentat + request->timeout;
647
648 if (CurrentTime >= timeout)
649 {
650 if (--request->retries <= 0)
651 {
652 (*request->callback)(request->callback_ctx, NULL, NULL, 0);
653 rem_request(request);
654 continue;
655 }
656 else
657 {
658 request->sentat = CurrentTime;
659 request->timeout += request->timeout;
660 resend_query(request);
661 }
662 }
663
664 if (next_time == 0 || timeout < next_time)
665 next_time = timeout;
666 }
667
668 return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL);
669 }
670
671 /*
672 * timeout_resolver - check request list
673 */
674 static void
675 timeout_resolver(void *unused)
676 {
677 timeout_query_list();
678 }
679
680 /*
681 * resolver_init - initialize resolver and resolver library
682 */
683 void
684 resolver_init(void)
685 {
686 static struct event event_timeout_resolver =
687 {
688 .name = "timeout_resolver",
689 .handler = timeout_resolver,
690 .when = 1
691 };
692
693 dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
694
695 start_resolver();
696 event_add(&event_timeout_resolver, NULL);
697 }

Properties

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