ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.3.x/src/res.c
Revision: 6838
Committed: Tue Nov 24 17:28:38 2015 UTC (10 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 19879 byte(s)
Log Message:
- res.c: remove unused header includes

File Contents

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

Properties

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