]>
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); | |
ed62c46b AC |
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 | ||
ed62c46b AC |
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 | */ | |
c99ae190 AC |
120 | static int |
121 | res_ourserver(const struct rb_sockaddr_storage *inp) | |
ed62c46b AC |
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 | ||
c99ae190 | 131 | for(ns = 0; ns < irc_nscount; ns++) |
ed62c46b AC |
132 | { |
133 | const struct rb_sockaddr_storage *srv = &irc_nsaddr_list[ns]; | |
ed62c46b AC |
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 | |
c99ae190 | 140 | * but we'll air on the side of caution - stu |
ed62c46b | 141 | */ |
c99ae190 | 142 | switch (GET_SS_FAMILY(srv)) |
ed62c46b AC |
143 | { |
144 | #ifdef RB_IPV6 | |
c99ae190 AC |
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; | |
ed62c46b | 154 | #endif |
c99ae190 AC |
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; | |
ed62c46b AC |
164 | } |
165 | } | |
166 | ||
c99ae190 | 167 | return 0; |
ed62c46b AC |
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 | { | |
c99ae190 | 228 | if ((res_fd = rb_socket(GET_SS_FAMILY(&irc_nsaddr_list[0]), SOCK_DGRAM, 0, |
ed62c46b AC |
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 | |
fa2d5b83 EM |
325 | * |
326 | * This is a cubic backoff btw, if anyone didn't pick up on it. --Elizafox | |
ed62c46b AC |
327 | */ |
328 | static int retryfreq(int timeouts) | |
329 | { | |
330 | switch (timeouts) | |
331 | { | |
fa2d5b83 EM |
332 | case 1: |
333 | return 3; | |
334 | case 2: | |
335 | return 9; | |
336 | case 3: | |
337 | return 27; | |
338 | case 4: | |
339 | return 81; | |
340 | default: | |
341 | return 243; | |
ed62c46b AC |
342 | } |
343 | } | |
344 | ||
345 | /* | |
346 | * send_res_msg - sends msg to a nameserver. | |
347 | * This should reflect /etc/resolv.conf. | |
348 | * Returns number of nameserver successfully sent to | |
349 | * or -1 if no successful sends. | |
350 | */ | |
351 | static int send_res_msg(const char *msg, int len, int rcount) | |
352 | { | |
353 | int i; | |
354 | int ns; | |
355 | static int retrycnt; | |
356 | ||
357 | retrycnt++; | |
358 | /* First try a nameserver that seems to work. | |
359 | * Every once in a while, try a possibly broken one to check | |
360 | * if it is working again. | |
361 | */ | |
362 | for (i = 0; i < irc_nscount; i++) | |
363 | { | |
364 | ns = (i + rcount - 1) % irc_nscount; | |
365 | if (ns_failure_count[ns] && retrycnt % retryfreq(ns_failure_count[ns])) | |
366 | continue; | |
367 | if (sendto(rb_get_fd(res_fd), msg, len, 0, | |
368 | (struct sockaddr *)&(irc_nsaddr_list[ns]), | |
369 | GET_SS_LEN(&irc_nsaddr_list[ns])) == len) | |
370 | return ns; | |
371 | } | |
372 | ||
373 | /* No known working nameservers, try some broken one. */ | |
374 | for (i = 0; i < irc_nscount; i++) | |
375 | { | |
376 | ns = (i + rcount - 1) % irc_nscount; | |
377 | if (!ns_failure_count[ns]) | |
378 | continue; | |
379 | if (sendto(rb_get_fd(res_fd), msg, len, 0, | |
380 | (struct sockaddr *)&(irc_nsaddr_list[ns]), | |
381 | GET_SS_LEN(&irc_nsaddr_list[ns])) == len) | |
382 | return ns; | |
383 | } | |
384 | ||
385 | return -1; | |
386 | } | |
387 | ||
388 | /* | |
389 | * find_id - find a dns request id (id is determined by dn_mkquery) | |
390 | */ | |
391 | static struct reslist *find_id(int id) | |
392 | { | |
393 | rb_dlink_node *ptr; | |
394 | struct reslist *request; | |
395 | ||
396 | RB_DLINK_FOREACH(ptr, request_list.head) | |
397 | { | |
398 | request = ptr->data; | |
399 | ||
400 | if (request->id == id) | |
401 | return (request); | |
402 | } | |
403 | ||
404 | return (NULL); | |
405 | } | |
406 | ||
407 | static uint16_t | |
408 | generate_random_id(void) | |
409 | { | |
410 | uint16_t id; | |
411 | ||
412 | do | |
413 | { | |
414 | rb_get_random(&id, sizeof(id)); | |
415 | if(id == 0xffff) | |
416 | continue; | |
417 | } | |
418 | while(find_id(id)); | |
419 | return id; | |
420 | } | |
421 | ||
ed62c46b AC |
422 | /* |
423 | * gethost_byname_type - get host address from name, adding domain if needed | |
424 | */ | |
425 | void gethost_byname_type(const char *name, struct DNSQuery *query, int type) | |
426 | { | |
427 | char fqdn[IRCD_RES_HOSTLEN + 1]; | |
428 | assert(name != 0); | |
429 | ||
430 | rb_strlcpy(fqdn, name, sizeof fqdn); | |
431 | add_local_domain(fqdn, IRCD_RES_HOSTLEN); | |
432 | gethost_byname_type_fqdn(fqdn, query, type); | |
433 | } | |
434 | ||
435 | /* | |
436 | * gethost_byname_type_fqdn - get host address from fqdn | |
437 | */ | |
438 | static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query, | |
439 | int type) | |
440 | { | |
441 | assert(name != 0); | |
442 | do_query_name(query, name, NULL, type); | |
443 | } | |
444 | ||
445 | /* | |
446 | * gethost_byaddr - get host name from address | |
447 | */ | |
448 | void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *query) | |
449 | { | |
450 | do_query_number(query, addr, NULL); | |
451 | } | |
452 | ||
453 | /* | |
454 | * do_query_name - nameserver lookup name | |
455 | */ | |
456 | static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, | |
457 | int type) | |
458 | { | |
459 | if (request == NULL) | |
460 | { | |
461 | request = make_request(query); | |
462 | request->name = rb_strdup(name); | |
463 | } | |
464 | ||
465 | rb_strlcpy(request->queryname, name, sizeof(request->queryname)); | |
466 | request->type = type; | |
467 | query_name(request); | |
468 | } | |
469 | ||
8ed8e5ca EM |
470 | /* Build an rDNS style query - if suffix is NULL, use the appropriate .arpa zone */ |
471 | void build_rdns(char *buf, size_t size, const struct rb_sockaddr_storage *addr, const char *suffix) | |
ed62c46b AC |
472 | { |
473 | const unsigned char *cp; | |
474 | ||
c99ae190 | 475 | if (GET_SS_FAMILY(addr) == AF_INET) |
ed62c46b AC |
476 | { |
477 | const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; | |
478 | cp = (const unsigned char *)&v4->sin_addr.s_addr; | |
479 | ||
8ed8e5ca EM |
480 | (void) snprintf(buf, size, "%u.%u.%u.%u.%s", |
481 | (unsigned int)(cp[3]), | |
482 | (unsigned int)(cp[2]), | |
483 | (unsigned int)(cp[1]), | |
484 | (unsigned int)(cp[0]), | |
485 | suffix == NULL ? "in-addr.arpa" : suffix); | |
ed62c46b AC |
486 | } |
487 | #ifdef RB_IPV6 | |
c99ae190 | 488 | else if (GET_SS_FAMILY(addr) == AF_INET6) |
ed62c46b AC |
489 | { |
490 | const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; | |
491 | cp = (const unsigned char *)&v6->sin6_addr.s6_addr; | |
492 | ||
8ed8e5ca EM |
493 | (void) snprintf(buf, size, |
494 | "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s", | |
495 | (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), | |
496 | (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), | |
497 | (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), | |
498 | (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), | |
499 | (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), | |
500 | (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), | |
501 | (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), | |
502 | (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), | |
503 | (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), | |
504 | (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), | |
505 | (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), | |
506 | (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), | |
507 | (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), | |
508 | (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), | |
509 | (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), | |
510 | (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4), | |
511 | suffix == NULL ? "ip6.arpa" : suffix); | |
ed62c46b AC |
512 | } |
513 | #endif | |
8ed8e5ca EM |
514 | } |
515 | ||
516 | /* | |
517 | * do_query_number - Use this to do reverse IP# lookups. | |
518 | */ | |
519 | static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *addr, | |
520 | struct reslist *request) | |
521 | { | |
8ed8e5ca EM |
522 | if (request == NULL) |
523 | { | |
524 | request = make_request(query); | |
525 | memcpy(&request->addr, addr, sizeof(struct rb_sockaddr_storage)); | |
526 | request->name = (char *)rb_malloc(IRCD_RES_HOSTLEN + 1); | |
527 | } | |
528 | ||
529 | build_rdns(request->queryname, IRCD_RES_HOSTLEN + 1, addr, NULL); | |
ed62c46b AC |
530 | |
531 | request->type = T_PTR; | |
532 | query_name(request); | |
533 | } | |
534 | ||
535 | /* | |
536 | * query_name - generate a query based on class, type and name. | |
537 | */ | |
538 | static void query_name(struct reslist *request) | |
539 | { | |
540 | char buf[MAXPACKET]; | |
541 | int request_len = 0; | |
542 | int ns; | |
543 | ||
544 | memset(buf, 0, sizeof(buf)); | |
545 | ||
546 | if ((request_len = | |
547 | irc_res_mkquery(request->queryname, C_IN, request->type, (unsigned char *)buf, sizeof(buf))) > 0) | |
548 | { | |
549 | HEADER *header = (HEADER *)(void *)buf; | |
550 | header->id = request->id; | |
551 | ++request->sends; | |
552 | ||
553 | ns = send_res_msg(buf, request_len, request->sends); | |
554 | if (ns != -1) | |
555 | request->lastns = ns; | |
556 | } | |
557 | } | |
558 | ||
559 | static void resend_query(struct reslist *request) | |
560 | { | |
561 | if (--request->retries <= 0) | |
562 | { | |
563 | (*request->query->callback) (request->query->ptr, NULL); | |
564 | rem_request(request); | |
565 | return; | |
566 | } | |
567 | ||
568 | switch (request->type) | |
569 | { | |
fa2d5b83 EM |
570 | case T_PTR: |
571 | do_query_number(NULL, &request->addr, request); | |
572 | break; | |
573 | case T_A: | |
ed62c46b | 574 | #ifdef RB_IPV6 |
fa2d5b83 | 575 | case T_AAAA: |
ed62c46b | 576 | #endif |
fa2d5b83 EM |
577 | do_query_name(NULL, request->name, request, request->type); |
578 | break; | |
579 | default: | |
580 | break; | |
ed62c46b AC |
581 | } |
582 | } | |
583 | ||
584 | /* | |
585 | * check_question - check if the reply really belongs to the | |
586 | * name we queried (to guard against late replies from previous | |
587 | * queries with the same id). | |
588 | */ | |
589 | static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob) | |
590 | { | |
591 | char hostbuf[IRCD_RES_HOSTLEN + 1]; /* working buffer */ | |
592 | unsigned char *current; /* current position in buf */ | |
593 | int n; /* temp count */ | |
594 | ||
595 | current = (unsigned char *)buf + sizeof(HEADER); | |
596 | if (header->qdcount != 1) | |
597 | return 0; | |
598 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
599 | sizeof(hostbuf)); | |
600 | if (n <= 0) | |
601 | return 0; | |
f956cb0f | 602 | if (rb_strcasecmp(hostbuf, request->queryname)) |
ed62c46b AC |
603 | return 0; |
604 | return 1; | |
605 | } | |
606 | ||
607 | /* | |
608 | * proc_answer - process name server reply | |
609 | */ | |
610 | static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob) | |
611 | { | |
612 | char hostbuf[IRCD_RES_HOSTLEN + 100]; /* working buffer */ | |
613 | unsigned char *current; /* current position in buf */ | |
614 | int type; /* answer type */ | |
615 | int n; /* temp count */ | |
616 | int rd_length; | |
617 | struct sockaddr_in *v4; /* conversion */ | |
618 | #ifdef RB_IPV6 | |
619 | struct sockaddr_in6 *v6; | |
620 | #endif | |
621 | current = (unsigned char *)buf + sizeof(HEADER); | |
622 | ||
623 | for (; header->qdcount > 0; --header->qdcount) | |
624 | { | |
625 | if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0) | |
626 | return 0; | |
627 | ||
628 | current += (size_t) n + QFIXEDSZ; | |
629 | } | |
630 | ||
631 | /* | |
632 | * process each answer sent to us blech. | |
633 | */ | |
634 | while (header->ancount > 0 && (char *)current < eob) | |
635 | { | |
636 | header->ancount--; | |
637 | ||
638 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
639 | sizeof(hostbuf)); | |
640 | ||
641 | if (n < 0) | |
642 | { | |
643 | /* | |
644 | * broken message | |
645 | */ | |
646 | return (0); | |
647 | } | |
648 | else if (n == 0) | |
649 | { | |
650 | /* | |
651 | * no more answers left | |
652 | */ | |
653 | return (0); | |
654 | } | |
655 | ||
656 | hostbuf[IRCD_RES_HOSTLEN] = '\0'; | |
657 | ||
658 | /* With Address arithmetic you have to be very anal | |
659 | * this code was not working on alpha due to that | |
660 | * (spotted by rodder/jailbird/dianora) | |
661 | */ | |
662 | current += (size_t) n; | |
663 | ||
664 | if (!(((char *)current + ANSWER_FIXED_SIZE) < eob)) | |
665 | break; | |
666 | ||
667 | type = irc_ns_get16(current); | |
668 | current += TYPE_SIZE; | |
669 | ||
670 | (void) irc_ns_get16(current); | |
671 | current += CLASS_SIZE; | |
672 | ||
673 | request->ttl = irc_ns_get32(current); | |
674 | current += TTL_SIZE; | |
675 | ||
676 | rd_length = irc_ns_get16(current); | |
677 | current += RDLENGTH_SIZE; | |
678 | ||
679 | /* | |
680 | * Wait to set request->type until we verify this structure | |
681 | */ | |
682 | switch (type) | |
683 | { | |
fa2d5b83 EM |
684 | case T_A: |
685 | if (request->type != T_A) | |
686 | return (0); | |
687 | ||
688 | /* | |
689 | * check for invalid rd_length or too many addresses | |
690 | */ | |
691 | if (rd_length != sizeof(struct in_addr)) | |
692 | return (0); | |
693 | v4 = (struct sockaddr_in *)&request->addr; | |
694 | SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in)); | |
695 | v4->sin_family = AF_INET; | |
696 | memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); | |
697 | return (1); | |
698 | break; | |
ed62c46b | 699 | #ifdef RB_IPV6 |
fa2d5b83 EM |
700 | case T_AAAA: |
701 | if (request->type != T_AAAA) | |
702 | return (0); | |
703 | if (rd_length != sizeof(struct in6_addr)) | |
704 | return (0); | |
705 | SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in6)); | |
706 | v6 = (struct sockaddr_in6 *)&request->addr; | |
707 | v6->sin6_family = AF_INET6; | |
708 | memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); | |
709 | return (1); | |
710 | break; | |
ed62c46b | 711 | #endif |
fa2d5b83 EM |
712 | case T_PTR: |
713 | if (request->type != T_PTR) | |
714 | return (0); | |
715 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, | |
716 | hostbuf, sizeof(hostbuf)); | |
717 | if (n < 0) | |
718 | return (0); /* broken message */ | |
719 | else if (n == 0) | |
720 | return (0); /* no more answers left */ | |
721 | ||
722 | rb_strlcpy(request->name, hostbuf, IRCD_RES_HOSTLEN + 1); | |
723 | ||
724 | return (1); | |
725 | break; | |
726 | case T_CNAME: | |
727 | /* real answer will follow */ | |
728 | current += rd_length; | |
729 | break; | |
730 | default: | |
731 | break; | |
ed62c46b AC |
732 | } |
733 | } | |
734 | ||
735 | return (1); | |
736 | } | |
737 | ||
738 | /* | |
739 | * res_read_single_reply - read a dns reply from the nameserver and process it. | |
740 | * Return value: 1 if a packet was read, 0 otherwise | |
741 | */ | |
742 | static int res_read_single_reply(rb_fde_t *F, void *data) | |
743 | { | |
744 | char buf[sizeof(HEADER) + MAXPACKET] | |
745 | /* Sparc and alpha need 16bit-alignment for accessing header->id | |
746 | * (which is uint16_t). Because of the header = (HEADER*) buf; | |
747 | * lateron, this is neeeded. --FaUl | |
748 | */ | |
749 | #if defined(__sparc__) || defined(__alpha__) | |
750 | __attribute__ ((aligned(16))) | |
751 | #endif | |
752 | ; | |
753 | HEADER *header; | |
754 | struct reslist *request = NULL; | |
755 | struct DNSReply *reply = NULL; | |
756 | int rc; | |
757 | int answer_count; | |
758 | socklen_t len = sizeof(struct rb_sockaddr_storage); | |
759 | struct rb_sockaddr_storage lsin; | |
760 | int ns; | |
761 | ||
762 | rc = recvfrom(rb_get_fd(F), buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); | |
763 | ||
764 | /* No packet */ | |
765 | if (rc == 0 || rc == -1) | |
766 | return 0; | |
767 | ||
768 | /* Too small */ | |
769 | if (rc <= (int)(sizeof(HEADER))) | |
770 | return 1; | |
771 | ||
772 | /* | |
773 | * convert DNS reply reader from Network byte order to CPU byte order. | |
774 | */ | |
775 | header = (HEADER *)(void *)buf; | |
776 | header->ancount = ntohs(header->ancount); | |
777 | header->qdcount = ntohs(header->qdcount); | |
778 | header->nscount = ntohs(header->nscount); | |
779 | header->arcount = ntohs(header->arcount); | |
780 | ||
781 | /* | |
782 | * response for an id which we have already received an answer for | |
783 | * just ignore this response. | |
784 | */ | |
785 | if (0 == (request = find_id(header->id))) | |
786 | return 1; | |
787 | ||
788 | /* | |
789 | * check against possibly fake replies | |
790 | */ | |
791 | ns = res_ourserver(&lsin); | |
792 | if (ns == -1) | |
793 | return 1; | |
794 | ||
795 | if (ns != request->lastns) | |
796 | { | |
797 | /* | |
798 | * We'll accept the late reply, but penalize it a little more to make | |
799 | * sure a laggy server doesn't end up favored. | |
800 | */ | |
801 | ns_failure_count[ns] += 3; | |
802 | } | |
803 | ||
804 | ||
805 | if (!check_question(request, header, buf, buf + rc)) | |
806 | return 1; | |
807 | ||
808 | if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) | |
809 | { | |
810 | /* | |
811 | * RFC 2136 states that in the event of a server returning SERVFAIL | |
812 | * or NOTIMP, the request should be resent to the next server. | |
813 | * Additionally, if the server refuses our query, resend it as well. | |
814 | * -- mr_flea | |
815 | */ | |
816 | if (SERVFAIL == header->rcode || NOTIMP == header->rcode || | |
817 | REFUSED == header->rcode) | |
818 | { | |
819 | ns_failure_count[ns]++; | |
820 | resend_query(request); | |
821 | } | |
822 | else | |
823 | { | |
824 | /* | |
825 | * Either a fatal error was returned or no answer. Cancel the | |
826 | * request. | |
827 | */ | |
828 | if (NXDOMAIN == header->rcode) | |
829 | { | |
830 | /* If the rcode is NXDOMAIN, treat it as a good response. */ | |
831 | ns_failure_count[ns] /= 4; | |
832 | } | |
833 | (*request->query->callback) (request->query->ptr, NULL); | |
834 | rem_request(request); | |
835 | } | |
836 | return 1; | |
837 | } | |
838 | /* | |
839 | * If this fails there was an error decoding the received packet. | |
840 | * -- jilles | |
841 | */ | |
842 | answer_count = proc_answer(request, header, buf, buf + rc); | |
843 | ||
844 | if (answer_count) | |
845 | { | |
846 | if (request->type == T_PTR) | |
847 | { | |
848 | if (request->name == NULL) | |
849 | { | |
850 | /* | |
851 | * Got a PTR response with no name, something strange is | |
852 | * happening. Try another DNS server. | |
853 | */ | |
854 | ns_failure_count[ns]++; | |
855 | resend_query(request); | |
856 | return 1; | |
857 | } | |
858 | ||
859 | /* | |
860 | * Lookup the 'authoritative' name that we were given for the | |
861 | * ip#. | |
862 | */ | |
863 | #ifdef RB_IPV6 | |
9783438e | 864 | if (GET_SS_FAMILY(&request->addr) == AF_INET6) |
ed62c46b AC |
865 | gethost_byname_type_fqdn(request->name, request->query, T_AAAA); |
866 | else | |
867 | #endif | |
868 | gethost_byname_type_fqdn(request->name, request->query, T_A); | |
869 | rem_request(request); | |
870 | } | |
871 | else | |
872 | { | |
873 | /* | |
874 | * got a name and address response, client resolved | |
875 | */ | |
876 | reply = make_dnsreply(request); | |
877 | (*request->query->callback) (request->query->ptr, reply); | |
878 | rb_free(reply); | |
879 | rem_request(request); | |
880 | } | |
881 | ||
882 | ns_failure_count[ns] /= 4; | |
883 | } | |
884 | else | |
885 | { | |
886 | /* Invalid or corrupt reply - try another resolver. */ | |
887 | ns_failure_count[ns]++; | |
888 | resend_query(request); | |
889 | } | |
890 | return 1; | |
891 | } | |
892 | ||
893 | static void | |
894 | res_readreply(rb_fde_t *F, void *data) | |
895 | { | |
896 | while (res_read_single_reply(F, data)) | |
897 | ; | |
898 | rb_setselect(F, RB_SELECT_READ, res_readreply, NULL); | |
899 | } | |
900 | ||
901 | static struct DNSReply * | |
902 | make_dnsreply(struct reslist *request) | |
903 | { | |
904 | struct DNSReply *cp; | |
905 | lrb_assert(request != 0); | |
906 | ||
907 | cp = (struct DNSReply *)rb_malloc(sizeof(struct DNSReply)); | |
908 | ||
909 | cp->h_name = request->name; | |
910 | memcpy(&cp->addr, &request->addr, sizeof(cp->addr)); | |
911 | return (cp); | |
912 | } |