]>
Commit | Line | Data |
---|---|---|
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 | * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, | |
11 | * added callbacks and reference counting of returned hostents. | |
12 | * --Bleep (Thomas Helvey <tomh@inxpress.net>) | |
13 | * | |
14 | * This was all needlessly complicated for irc. Simplified. No more hostent | |
15 | * All we really care about is the IP -> hostname mappings. Thats all. | |
16 | * | |
17 | * Apr 28, 2003 --cryogen and Dianora | |
18 | * | |
19 | * DNS server flooding lessened, AAAA-or-A lookup removed, ip6.int support | |
20 | * removed, various robustness fixes | |
21 | * | |
22 | * 2006 --jilles and nenolod | |
23 | * | |
24 | * Resend queries to other servers if the DNS server replies with an error or | |
25 | * an invalid response. Also, avoid servers that return errors or invalid | |
26 | * responses. | |
27 | * | |
28 | * October 2012 --mr_flea | |
29 | * | |
30 | * ircd-ratbox changes for random IDs merged back in. | |
31 | * | |
32 | * January 2016 --kaniini | |
33 | */ | |
34 | ||
35 | #include <rb_lib.h> | |
36 | #include "setup.h" | |
37 | #include "res.h" | |
38 | #include "reslib.h" | |
39 | ||
40 | #if (CHAR_BIT != 8) | |
41 | #error this code needs to be able to address individual octets | |
42 | #endif | |
43 | ||
44 | static PF res_readreply; | |
45 | ||
46 | #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */ | |
47 | #define RES_MAXALIASES 35 /* maximum aliases allowed */ | |
48 | #define RES_MAXADDRS 35 /* maximum addresses allowed */ | |
49 | #define AR_TTL 600 /* TTL in seconds for dns cache entries */ | |
50 | ||
51 | /* RFC 1104/1105 wasn't very helpful about what these fields | |
52 | * should be named, so for now, we'll just name them this way. | |
53 | * we probably should look at what named calls them or something. | |
54 | */ | |
55 | #define TYPE_SIZE (size_t)2 | |
56 | #define CLASS_SIZE (size_t)2 | |
57 | #define TTL_SIZE (size_t)4 | |
58 | #define RDLENGTH_SIZE (size_t)2 | |
59 | #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) | |
60 | ||
61 | #ifdef RB_IPV6 | |
62 | struct in6_addr ipv6_addr; | |
63 | #endif | |
64 | struct in_addr ipv4_addr; | |
65 | ||
66 | struct reslist | |
67 | { | |
68 | rb_dlink_node node; | |
69 | int id; | |
70 | time_t ttl; | |
71 | char type; | |
72 | char queryname[IRCD_RES_HOSTLEN + 1]; /* name currently being queried */ | |
73 | char retries; /* retry counter */ | |
74 | char sends; /* number of sends (>1 means resent) */ | |
75 | time_t sentat; | |
76 | time_t timeout; | |
77 | int lastns; /* index of last server sent to */ | |
78 | struct rb_sockaddr_storage addr; | |
79 | char *name; | |
80 | struct DNSQuery *query; /* query callback for this request */ | |
81 | }; | |
82 | ||
83 | static rb_fde_t *res_fd; | |
84 | static rb_dlink_list request_list = { NULL, NULL, 0 }; | |
85 | static int ns_failure_count[IRCD_MAXNS]; /* timeouts and invalid/failed replies */ | |
86 | ||
87 | static void rem_request(struct reslist *request); | |
88 | static struct reslist *make_request(struct DNSQuery *query); | |
89 | static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query, | |
90 | int type); | |
91 | static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int); | |
92 | static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *, | |
93 | struct reslist *request); | |
94 | static void query_name(struct reslist *request); | |
95 | static int send_res_msg(const char *buf, int len, int count); | |
96 | static void resend_query(struct reslist *request); | |
97 | static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob); | |
98 | static int proc_answer(struct reslist *request, HEADER * header, char *, char *); | |
99 | static struct reslist *find_id(int id); | |
100 | static struct DNSReply *make_dnsreply(struct reslist *request); | |
101 | static uint16_t generate_random_id(void); | |
102 | ||
103 | #ifdef RES_MIN | |
104 | #undef RES_MIN | |
105 | #endif | |
106 | ||
107 | #define RES_MIN(a, b) ((a) < (b) ? (a) : (b)) | |
108 | ||
109 | /* | |
110 | * int | |
111 | * res_ourserver(inp) | |
112 | * looks up "inp" in irc_nsaddr_list[] | |
113 | * returns: | |
114 | * server ID or -1 for not found | |
115 | * author: | |
116 | * paul vixie, 29may94 | |
117 | * revised for ircd, cryogen(stu) may03 | |
118 | * slightly modified for charybdis, mr_flea oct12 | |
119 | */ | |
120 | static int | |
121 | res_ourserver(const struct rb_sockaddr_storage *inp) | |
122 | { | |
123 | #ifdef RB_IPV6 | |
124 | const struct sockaddr_in6 *v6; | |
125 | const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; | |
126 | #endif | |
127 | const struct sockaddr_in *v4; | |
128 | const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; | |
129 | int ns; | |
130 | ||
131 | for(ns = 0; ns < irc_nscount; ns++) | |
132 | { | |
133 | const struct rb_sockaddr_storage *srv = &irc_nsaddr_list[ns]; | |
134 | #ifdef RB_IPV6 | |
135 | v6 = (const struct sockaddr_in6 *)srv; | |
136 | #endif | |
137 | v4 = (const struct sockaddr_in *)srv; | |
138 | ||
139 | /* could probably just memcmp(srv, inp, srv.ss_len) here | |
140 | * but we'll air on the side of caution - stu | |
141 | */ | |
142 | switch (GET_SS_FAMILY(srv)) | |
143 | { | |
144 | #ifdef RB_IPV6 | |
145 | case AF_INET6: | |
146 | if(GET_SS_FAMILY(srv) == GET_SS_FAMILY(inp)) | |
147 | if(v6->sin6_port == v6in->sin6_port) | |
148 | if((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, | |
149 | sizeof(struct in6_addr)) == 0) || | |
150 | (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any, | |
151 | sizeof(struct in6_addr)) == 0)) | |
152 | return 1; | |
153 | break; | |
154 | #endif | |
155 | case AF_INET: | |
156 | if(GET_SS_FAMILY(srv) == GET_SS_FAMILY(inp)) | |
157 | if(v4->sin_port == v4in->sin_port) | |
158 | if((v4->sin_addr.s_addr == INADDR_ANY) | |
159 | || (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)) | |
160 | return 1; | |
161 | break; | |
162 | default: | |
163 | break; | |
164 | } | |
165 | } | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | /* | |
171 | * timeout_query_list - Remove queries from the list which have been | |
172 | * there too long without being resolved. | |
173 | */ | |
174 | static time_t timeout_query_list(time_t now) | |
175 | { | |
176 | rb_dlink_node *ptr; | |
177 | rb_dlink_node *next_ptr; | |
178 | struct reslist *request; | |
179 | time_t next_time = 0; | |
180 | time_t timeout = 0; | |
181 | ||
182 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head) | |
183 | { | |
184 | request = ptr->data; | |
185 | timeout = request->sentat + request->timeout; | |
186 | ||
187 | if (now >= timeout) | |
188 | { | |
189 | ns_failure_count[request->lastns]++; | |
190 | request->sentat = now; | |
191 | request->timeout += request->timeout; | |
192 | resend_query(request); | |
193 | } | |
194 | ||
195 | if ((next_time == 0) || timeout < next_time) | |
196 | { | |
197 | next_time = timeout; | |
198 | } | |
199 | } | |
200 | ||
201 | return (next_time > now) ? next_time : (now + AR_TTL); | |
202 | } | |
203 | ||
204 | /* | |
205 | * timeout_resolver - check request list | |
206 | */ | |
207 | static void timeout_resolver(void *notused) | |
208 | { | |
209 | timeout_query_list(rb_current_time()); | |
210 | } | |
211 | ||
212 | static struct ev_entry *timeout_resolver_ev = NULL; | |
213 | ||
214 | /* | |
215 | * start_resolver - do everything we need to read the resolv.conf file | |
216 | * and initialize the resolver file descriptor if needed | |
217 | */ | |
218 | static void start_resolver(void) | |
219 | { | |
220 | int i; | |
221 | ||
222 | irc_res_init(); | |
223 | for (i = 0; i < irc_nscount; i++) | |
224 | ns_failure_count[i] = 0; | |
225 | ||
226 | if (res_fd == NULL) | |
227 | { | |
228 | if ((res_fd = rb_socket(GET_SS_FAMILY(&irc_nsaddr_list[0]), SOCK_DGRAM, 0, | |
229 | "UDP resolver socket")) == NULL) | |
230 | return; | |
231 | ||
232 | /* At the moment, the resolver FD data is global .. */ | |
233 | rb_setselect(res_fd, RB_SELECT_READ, res_readreply, NULL); | |
234 | timeout_resolver_ev = rb_event_add("timeout_resolver", timeout_resolver, NULL, 1); | |
235 | } | |
236 | } | |
237 | ||
238 | /* | |
239 | * init_resolver - initialize resolver and resolver library | |
240 | */ | |
241 | void init_resolver(void) | |
242 | { | |
243 | #ifdef HAVE_SRAND48 | |
244 | srand48(rb_current_time()); | |
245 | #endif | |
246 | start_resolver(); | |
247 | } | |
248 | ||
249 | /* | |
250 | * restart_resolver - reread resolv.conf, reopen socket | |
251 | */ | |
252 | void restart_resolver(void) | |
253 | { | |
254 | rb_close(res_fd); | |
255 | res_fd = NULL; | |
256 | rb_event_delete(timeout_resolver_ev); /* -ddosen */ | |
257 | start_resolver(); | |
258 | } | |
259 | ||
260 | /* | |
261 | * add_local_domain - Add the domain to hostname, if it is missing | |
262 | * (as suggested by eps@TOASTER.SFSU.EDU) | |
263 | */ | |
264 | void add_local_domain(char *hname, size_t size) | |
265 | { | |
266 | /* try to fix up unqualified names */ | |
267 | if (strchr(hname, '.') == NULL) | |
268 | { | |
269 | if (irc_domain[0]) | |
270 | { | |
271 | size_t len = strlen(hname); | |
272 | ||
273 | if ((strlen(irc_domain) + len + 2) < size) | |
274 | { | |
275 | hname[len++] = '.'; | |
276 | strcpy(hname + len, irc_domain); | |
277 | } | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | /* | |
283 | * rem_request - remove a request from the list. | |
284 | * This must also free any memory that has been allocated for | |
285 | * temporary storage of DNS results. | |
286 | */ | |
287 | static void rem_request(struct reslist *request) | |
288 | { | |
289 | rb_dlinkDelete(&request->node, &request_list); | |
290 | rb_free(request->name); | |
291 | rb_free(request); | |
292 | } | |
293 | ||
294 | /* | |
295 | * make_request - Create a DNS request record for the server. | |
296 | */ | |
297 | static struct reslist *make_request(struct DNSQuery *query) | |
298 | { | |
299 | struct reslist *request = rb_malloc(sizeof(struct reslist)); | |
300 | ||
301 | request->sentat = rb_current_time(); | |
302 | request->retries = 3; | |
303 | request->timeout = 4; /* start at 4 and exponential inc. */ | |
304 | request->query = query; | |
305 | ||
306 | /* | |
307 | * generate a unique id | |
308 | * NOTE: we don't have to worry about converting this to and from | |
309 | * network byte order, the nameserver does not interpret this value | |
310 | * and returns it unchanged | |
311 | * | |
312 | * we generate an id per request now (instead of per send) to allow | |
313 | * late replies to be used. | |
314 | */ | |
315 | request->id = generate_random_id(); | |
316 | ||
317 | rb_dlinkAdd(request, &request->node, &request_list); | |
318 | ||
319 | return request; | |
320 | } | |
321 | ||
322 | /* | |
323 | * retryfreq - determine how many queries to wait before resending | |
324 | * if there have been that many consecutive timeouts | |
325 | */ | |
326 | static int retryfreq(int timeouts) | |
327 | { | |
328 | switch (timeouts) | |
329 | { | |
330 | case 1: | |
331 | return 3; | |
332 | case 2: | |
333 | return 9; | |
334 | case 3: | |
335 | return 27; | |
336 | case 4: | |
337 | return 81; | |
338 | default: | |
339 | return 243; | |
340 | } | |
341 | } | |
342 | ||
343 | /* | |
344 | * send_res_msg - sends msg to a nameserver. | |
345 | * This should reflect /etc/resolv.conf. | |
346 | * Returns number of nameserver successfully sent to | |
347 | * or -1 if no successful sends. | |
348 | */ | |
349 | static int send_res_msg(const char *msg, int len, int rcount) | |
350 | { | |
351 | int i; | |
352 | int ns; | |
353 | static int retrycnt; | |
354 | ||
355 | retrycnt++; | |
356 | /* First try a nameserver that seems to work. | |
357 | * Every once in a while, try a possibly broken one to check | |
358 | * if it is working again. | |
359 | */ | |
360 | for (i = 0; i < irc_nscount; i++) | |
361 | { | |
362 | ns = (i + rcount - 1) % irc_nscount; | |
363 | if (ns_failure_count[ns] && retrycnt % retryfreq(ns_failure_count[ns])) | |
364 | continue; | |
365 | if (sendto(rb_get_fd(res_fd), msg, len, 0, | |
366 | (struct sockaddr *)&(irc_nsaddr_list[ns]), | |
367 | GET_SS_LEN(&irc_nsaddr_list[ns])) == len) | |
368 | return ns; | |
369 | } | |
370 | ||
371 | /* No known working nameservers, try some broken one. */ | |
372 | for (i = 0; i < irc_nscount; i++) | |
373 | { | |
374 | ns = (i + rcount - 1) % irc_nscount; | |
375 | if (!ns_failure_count[ns]) | |
376 | continue; | |
377 | if (sendto(rb_get_fd(res_fd), msg, len, 0, | |
378 | (struct sockaddr *)&(irc_nsaddr_list[ns]), | |
379 | GET_SS_LEN(&irc_nsaddr_list[ns])) == len) | |
380 | return ns; | |
381 | } | |
382 | ||
383 | return -1; | |
384 | } | |
385 | ||
386 | /* | |
387 | * find_id - find a dns request id (id is determined by dn_mkquery) | |
388 | */ | |
389 | static struct reslist *find_id(int id) | |
390 | { | |
391 | rb_dlink_node *ptr; | |
392 | struct reslist *request; | |
393 | ||
394 | RB_DLINK_FOREACH(ptr, request_list.head) | |
395 | { | |
396 | request = ptr->data; | |
397 | ||
398 | if (request->id == id) | |
399 | return (request); | |
400 | } | |
401 | ||
402 | return (NULL); | |
403 | } | |
404 | ||
405 | static uint16_t | |
406 | generate_random_id(void) | |
407 | { | |
408 | uint16_t id; | |
409 | ||
410 | do | |
411 | { | |
412 | rb_get_random(&id, sizeof(id)); | |
413 | if(id == 0xffff) | |
414 | continue; | |
415 | } | |
416 | while(find_id(id)); | |
417 | return id; | |
418 | } | |
419 | ||
420 | /* | |
421 | * gethost_byname_type - get host address from name, adding domain if needed | |
422 | */ | |
423 | void gethost_byname_type(const char *name, struct DNSQuery *query, int type) | |
424 | { | |
425 | char fqdn[IRCD_RES_HOSTLEN + 1]; | |
426 | assert(name != 0); | |
427 | ||
428 | rb_strlcpy(fqdn, name, sizeof fqdn); | |
429 | add_local_domain(fqdn, IRCD_RES_HOSTLEN); | |
430 | gethost_byname_type_fqdn(fqdn, query, type); | |
431 | } | |
432 | ||
433 | /* | |
434 | * gethost_byname_type_fqdn - get host address from fqdn | |
435 | */ | |
436 | static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query, | |
437 | int type) | |
438 | { | |
439 | assert(name != 0); | |
440 | do_query_name(query, name, NULL, type); | |
441 | } | |
442 | ||
443 | /* | |
444 | * gethost_byaddr - get host name from address | |
445 | */ | |
446 | void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *query) | |
447 | { | |
448 | do_query_number(query, addr, NULL); | |
449 | } | |
450 | ||
451 | /* | |
452 | * do_query_name - nameserver lookup name | |
453 | */ | |
454 | static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, | |
455 | int type) | |
456 | { | |
457 | if (request == NULL) | |
458 | { | |
459 | request = make_request(query); | |
460 | request->name = rb_strdup(name); | |
461 | } | |
462 | ||
463 | rb_strlcpy(request->queryname, name, sizeof(request->queryname)); | |
464 | request->type = type; | |
465 | query_name(request); | |
466 | } | |
467 | ||
468 | /* | |
469 | * do_query_number - Use this to do reverse IP# lookups. | |
470 | */ | |
471 | static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *addr, | |
472 | struct reslist *request) | |
473 | { | |
474 | const unsigned char *cp; | |
475 | ||
476 | if (request == NULL) | |
477 | { | |
478 | request = make_request(query); | |
479 | memcpy(&request->addr, addr, sizeof(struct rb_sockaddr_storage)); | |
480 | request->name = (char *)rb_malloc(IRCD_RES_HOSTLEN + 1); | |
481 | } | |
482 | ||
483 | if (GET_SS_FAMILY(addr) == AF_INET) | |
484 | { | |
485 | const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; | |
486 | cp = (const unsigned char *)&v4->sin_addr.s_addr; | |
487 | ||
488 | sprintf(request->queryname, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp[3]), | |
489 | (unsigned int)(cp[2]), (unsigned int)(cp[1]), (unsigned int)(cp[0])); | |
490 | } | |
491 | #ifdef RB_IPV6 | |
492 | else if (GET_SS_FAMILY(addr) == AF_INET6) | |
493 | { | |
494 | const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; | |
495 | cp = (const unsigned char *)&v6->sin6_addr.s6_addr; | |
496 | ||
497 | (void)sprintf(request->queryname, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." | |
498 | "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa", | |
499 | (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), | |
500 | (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), | |
501 | (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), | |
502 | (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), | |
503 | (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), | |
504 | (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), | |
505 | (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), | |
506 | (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), | |
507 | (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), | |
508 | (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), | |
509 | (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), | |
510 | (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), | |
511 | (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), | |
512 | (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), | |
513 | (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), | |
514 | (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); | |
515 | } | |
516 | #endif | |
517 | ||
518 | request->type = T_PTR; | |
519 | query_name(request); | |
520 | } | |
521 | ||
522 | /* | |
523 | * query_name - generate a query based on class, type and name. | |
524 | */ | |
525 | static void query_name(struct reslist *request) | |
526 | { | |
527 | char buf[MAXPACKET]; | |
528 | int request_len = 0; | |
529 | int ns; | |
530 | ||
531 | memset(buf, 0, sizeof(buf)); | |
532 | ||
533 | if ((request_len = | |
534 | irc_res_mkquery(request->queryname, C_IN, request->type, (unsigned char *)buf, sizeof(buf))) > 0) | |
535 | { | |
536 | HEADER *header = (HEADER *)(void *)buf; | |
537 | header->id = request->id; | |
538 | ++request->sends; | |
539 | ||
540 | ns = send_res_msg(buf, request_len, request->sends); | |
541 | if (ns != -1) | |
542 | request->lastns = ns; | |
543 | } | |
544 | } | |
545 | ||
546 | static void resend_query(struct reslist *request) | |
547 | { | |
548 | if (--request->retries <= 0) | |
549 | { | |
550 | (*request->query->callback) (request->query->ptr, NULL); | |
551 | rem_request(request); | |
552 | return; | |
553 | } | |
554 | ||
555 | switch (request->type) | |
556 | { | |
557 | case T_PTR: | |
558 | do_query_number(NULL, &request->addr, request); | |
559 | break; | |
560 | case T_A: | |
561 | #ifdef RB_IPV6 | |
562 | case T_AAAA: | |
563 | #endif | |
564 | do_query_name(NULL, request->name, request, request->type); | |
565 | break; | |
566 | default: | |
567 | break; | |
568 | } | |
569 | } | |
570 | ||
571 | /* | |
572 | * check_question - check if the reply really belongs to the | |
573 | * name we queried (to guard against late replies from previous | |
574 | * queries with the same id). | |
575 | */ | |
576 | static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob) | |
577 | { | |
578 | char hostbuf[IRCD_RES_HOSTLEN + 1]; /* working buffer */ | |
579 | unsigned char *current; /* current position in buf */ | |
580 | int n; /* temp count */ | |
581 | ||
582 | current = (unsigned char *)buf + sizeof(HEADER); | |
583 | if (header->qdcount != 1) | |
584 | return 0; | |
585 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
586 | sizeof(hostbuf)); | |
587 | if (n <= 0) | |
588 | return 0; | |
589 | if (strcasecmp(hostbuf, request->queryname)) | |
590 | return 0; | |
591 | return 1; | |
592 | } | |
593 | ||
594 | /* | |
595 | * proc_answer - process name server reply | |
596 | */ | |
597 | static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob) | |
598 | { | |
599 | char hostbuf[IRCD_RES_HOSTLEN + 100]; /* working buffer */ | |
600 | unsigned char *current; /* current position in buf */ | |
601 | int type; /* answer type */ | |
602 | int n; /* temp count */ | |
603 | int rd_length; | |
604 | struct sockaddr_in *v4; /* conversion */ | |
605 | #ifdef RB_IPV6 | |
606 | struct sockaddr_in6 *v6; | |
607 | #endif | |
608 | current = (unsigned char *)buf + sizeof(HEADER); | |
609 | ||
610 | for (; header->qdcount > 0; --header->qdcount) | |
611 | { | |
612 | if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0) | |
613 | return 0; | |
614 | ||
615 | current += (size_t) n + QFIXEDSZ; | |
616 | } | |
617 | ||
618 | /* | |
619 | * process each answer sent to us blech. | |
620 | */ | |
621 | while (header->ancount > 0 && (char *)current < eob) | |
622 | { | |
623 | header->ancount--; | |
624 | ||
625 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
626 | sizeof(hostbuf)); | |
627 | ||
628 | if (n < 0) | |
629 | { | |
630 | /* | |
631 | * broken message | |
632 | */ | |
633 | return (0); | |
634 | } | |
635 | else if (n == 0) | |
636 | { | |
637 | /* | |
638 | * no more answers left | |
639 | */ | |
640 | return (0); | |
641 | } | |
642 | ||
643 | hostbuf[IRCD_RES_HOSTLEN] = '\0'; | |
644 | ||
645 | /* With Address arithmetic you have to be very anal | |
646 | * this code was not working on alpha due to that | |
647 | * (spotted by rodder/jailbird/dianora) | |
648 | */ | |
649 | current += (size_t) n; | |
650 | ||
651 | if (!(((char *)current + ANSWER_FIXED_SIZE) < eob)) | |
652 | break; | |
653 | ||
654 | type = irc_ns_get16(current); | |
655 | current += TYPE_SIZE; | |
656 | ||
657 | (void) irc_ns_get16(current); | |
658 | current += CLASS_SIZE; | |
659 | ||
660 | request->ttl = irc_ns_get32(current); | |
661 | current += TTL_SIZE; | |
662 | ||
663 | rd_length = irc_ns_get16(current); | |
664 | current += RDLENGTH_SIZE; | |
665 | ||
666 | /* | |
667 | * Wait to set request->type until we verify this structure | |
668 | */ | |
669 | switch (type) | |
670 | { | |
671 | case T_A: | |
672 | if (request->type != T_A) | |
673 | return (0); | |
674 | ||
675 | /* | |
676 | * check for invalid rd_length or too many addresses | |
677 | */ | |
678 | if (rd_length != sizeof(struct in_addr)) | |
679 | return (0); | |
680 | v4 = (struct sockaddr_in *)&request->addr; | |
681 | SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in)); | |
682 | v4->sin_family = AF_INET; | |
683 | memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); | |
684 | return (1); | |
685 | break; | |
686 | #ifdef RB_IPV6 | |
687 | case T_AAAA: | |
688 | if (request->type != T_AAAA) | |
689 | return (0); | |
690 | if (rd_length != sizeof(struct in6_addr)) | |
691 | return (0); | |
692 | SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in6)); | |
693 | v6 = (struct sockaddr_in6 *)&request->addr; | |
694 | v6->sin6_family = AF_INET6; | |
695 | memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); | |
696 | return (1); | |
697 | break; | |
698 | #endif | |
699 | case T_PTR: | |
700 | if (request->type != T_PTR) | |
701 | return (0); | |
702 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, | |
703 | hostbuf, sizeof(hostbuf)); | |
704 | if (n < 0) | |
705 | return (0); /* broken message */ | |
706 | else if (n == 0) | |
707 | return (0); /* no more answers left */ | |
708 | ||
709 | rb_strlcpy(request->name, hostbuf, IRCD_RES_HOSTLEN + 1); | |
710 | ||
711 | return (1); | |
712 | break; | |
713 | case T_CNAME: | |
714 | /* real answer will follow */ | |
715 | current += rd_length; | |
716 | break; | |
717 | ||
718 | default: | |
719 | break; | |
720 | } | |
721 | } | |
722 | ||
723 | return (1); | |
724 | } | |
725 | ||
726 | /* | |
727 | * res_read_single_reply - read a dns reply from the nameserver and process it. | |
728 | * Return value: 1 if a packet was read, 0 otherwise | |
729 | */ | |
730 | static int res_read_single_reply(rb_fde_t *F, void *data) | |
731 | { | |
732 | char buf[sizeof(HEADER) + MAXPACKET] | |
733 | /* Sparc and alpha need 16bit-alignment for accessing header->id | |
734 | * (which is uint16_t). Because of the header = (HEADER*) buf; | |
735 | * lateron, this is neeeded. --FaUl | |
736 | */ | |
737 | #if defined(__sparc__) || defined(__alpha__) | |
738 | __attribute__ ((aligned(16))) | |
739 | #endif | |
740 | ; | |
741 | HEADER *header; | |
742 | struct reslist *request = NULL; | |
743 | struct DNSReply *reply = NULL; | |
744 | int rc; | |
745 | int answer_count; | |
746 | socklen_t len = sizeof(struct rb_sockaddr_storage); | |
747 | struct rb_sockaddr_storage lsin; | |
748 | int ns; | |
749 | ||
750 | rc = recvfrom(rb_get_fd(F), buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); | |
751 | ||
752 | /* No packet */ | |
753 | if (rc == 0 || rc == -1) | |
754 | return 0; | |
755 | ||
756 | /* Too small */ | |
757 | if (rc <= (int)(sizeof(HEADER))) | |
758 | return 1; | |
759 | ||
760 | /* | |
761 | * convert DNS reply reader from Network byte order to CPU byte order. | |
762 | */ | |
763 | header = (HEADER *)(void *)buf; | |
764 | header->ancount = ntohs(header->ancount); | |
765 | header->qdcount = ntohs(header->qdcount); | |
766 | header->nscount = ntohs(header->nscount); | |
767 | header->arcount = ntohs(header->arcount); | |
768 | ||
769 | /* | |
770 | * response for an id which we have already received an answer for | |
771 | * just ignore this response. | |
772 | */ | |
773 | if (0 == (request = find_id(header->id))) | |
774 | return 1; | |
775 | ||
776 | /* | |
777 | * check against possibly fake replies | |
778 | */ | |
779 | ns = res_ourserver(&lsin); | |
780 | if (ns == -1) | |
781 | return 1; | |
782 | ||
783 | if (ns != request->lastns) | |
784 | { | |
785 | /* | |
786 | * We'll accept the late reply, but penalize it a little more to make | |
787 | * sure a laggy server doesn't end up favored. | |
788 | */ | |
789 | ns_failure_count[ns] += 3; | |
790 | } | |
791 | ||
792 | ||
793 | if (!check_question(request, header, buf, buf + rc)) | |
794 | return 1; | |
795 | ||
796 | if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) | |
797 | { | |
798 | /* | |
799 | * RFC 2136 states that in the event of a server returning SERVFAIL | |
800 | * or NOTIMP, the request should be resent to the next server. | |
801 | * Additionally, if the server refuses our query, resend it as well. | |
802 | * -- mr_flea | |
803 | */ | |
804 | if (SERVFAIL == header->rcode || NOTIMP == header->rcode || | |
805 | REFUSED == header->rcode) | |
806 | { | |
807 | ns_failure_count[ns]++; | |
808 | resend_query(request); | |
809 | } | |
810 | else | |
811 | { | |
812 | /* | |
813 | * Either a fatal error was returned or no answer. Cancel the | |
814 | * request. | |
815 | */ | |
816 | if (NXDOMAIN == header->rcode) | |
817 | { | |
818 | /* If the rcode is NXDOMAIN, treat it as a good response. */ | |
819 | ns_failure_count[ns] /= 4; | |
820 | } | |
821 | (*request->query->callback) (request->query->ptr, NULL); | |
822 | rem_request(request); | |
823 | } | |
824 | return 1; | |
825 | } | |
826 | /* | |
827 | * If this fails there was an error decoding the received packet. | |
828 | * -- jilles | |
829 | */ | |
830 | answer_count = proc_answer(request, header, buf, buf + rc); | |
831 | ||
832 | if (answer_count) | |
833 | { | |
834 | if (request->type == T_PTR) | |
835 | { | |
836 | if (request->name == NULL) | |
837 | { | |
838 | /* | |
839 | * Got a PTR response with no name, something strange is | |
840 | * happening. Try another DNS server. | |
841 | */ | |
842 | ns_failure_count[ns]++; | |
843 | resend_query(request); | |
844 | return 1; | |
845 | } | |
846 | ||
847 | /* | |
848 | * Lookup the 'authoritative' name that we were given for the | |
849 | * ip#. | |
850 | */ | |
851 | #ifdef RB_IPV6 | |
852 | if (GET_SS_FAMILY(&request->addr) == AF_INET6) | |
853 | gethost_byname_type_fqdn(request->name, request->query, T_AAAA); | |
854 | else | |
855 | #endif | |
856 | gethost_byname_type_fqdn(request->name, request->query, T_A); | |
857 | rem_request(request); | |
858 | } | |
859 | else | |
860 | { | |
861 | /* | |
862 | * got a name and address response, client resolved | |
863 | */ | |
864 | reply = make_dnsreply(request); | |
865 | (*request->query->callback) (request->query->ptr, reply); | |
866 | rb_free(reply); | |
867 | rem_request(request); | |
868 | } | |
869 | ||
870 | ns_failure_count[ns] /= 4; | |
871 | } | |
872 | else | |
873 | { | |
874 | /* Invalid or corrupt reply - try another resolver. */ | |
875 | ns_failure_count[ns]++; | |
876 | resend_query(request); | |
877 | } | |
878 | return 1; | |
879 | } | |
880 | ||
881 | static void | |
882 | res_readreply(rb_fde_t *F, void *data) | |
883 | { | |
884 | while (res_read_single_reply(F, data)) | |
885 | ; | |
886 | rb_setselect(F, RB_SELECT_READ, res_readreply, NULL); | |
887 | } | |
888 | ||
889 | static struct DNSReply * | |
890 | make_dnsreply(struct reslist *request) | |
891 | { | |
892 | struct DNSReply *cp; | |
893 | lrb_assert(request != 0); | |
894 | ||
895 | cp = (struct DNSReply *)rb_malloc(sizeof(struct DNSReply)); | |
896 | ||
897 | cp->h_name = request->name; | |
898 | memcpy(&cp->addr, &request->addr, sizeof(cp->addr)); | |
899 | return (cp); | |
900 | } |