/[svn]/ircd-hybrid/branches/8.2.x/src/res.c
ViewVC logotype

Contents of /ircd-hybrid/branches/8.2.x/src/res.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8751 - (show annotations)
Tue Jan 1 11:06:50 2019 UTC (19 months, 1 week ago) by michael
File MIME type: text/x-chdr
File size: 19651 byte(s)
- Update copyright years

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28