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