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