1 |
/* |
2 |
* A rewrite of Darren Reeds original res.c As there is nothing |
3 |
* left of Darrens original code, this is now licensed by the hybrid group. |
4 |
* (Well, some of the function names are the same, and bits of the structs..) |
5 |
* You can use it where it is useful, free even. Buy us a beer and stuff. |
6 |
* |
7 |
* The authors takes no responsibility for any damage or loss |
8 |
* of property which results from the use of this software. |
9 |
* |
10 |
* $Id$ |
11 |
* |
12 |
* July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, |
13 |
* added callbacks and reference counting of returned hostents. |
14 |
* --Bleep (Thomas Helvey <tomh@inxpress.net>) |
15 |
* |
16 |
* This was all needlessly complicated for irc. Simplified. No more hostent |
17 |
* All we really care about is the IP -> hostname mappings. Thats all. |
18 |
* |
19 |
* Apr 28, 2003 --cryogen and Dianora |
20 |
*/ |
21 |
|
22 |
#include "stdinc.h" |
23 |
#include "list.h" |
24 |
#include "client.h" |
25 |
#include "ioengine.h" |
26 |
#include "irc_string.h" |
27 |
#include "ircd.h" |
28 |
#include "numeric.h" |
29 |
#include "rng_mt.h" |
30 |
#include "s_bsd.h" |
31 |
#include "log.h" |
32 |
#include "s_misc.h" |
33 |
#include "send.h" |
34 |
#include "memory.h" |
35 |
#include "mempool.h" |
36 |
#include "irc_res.h" |
37 |
#include "irc_reslib.h" |
38 |
|
39 |
#if (CHAR_BIT != 8) |
40 |
#error this code needs to be able to address individual octets |
41 |
#endif |
42 |
|
43 |
|
44 |
/** IPv4 resolver UDP socket. */ |
45 |
static struct Socket res_socket_v4; |
46 |
/** IPv6 resolver UDP socket. */ |
47 |
static struct Socket res_socket_v6; |
48 |
/** Next DNS lookup timeout. */ |
49 |
static struct Timer res_timeout; |
50 |
|
51 |
|
52 |
#define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */ |
53 |
#define RES_MAXALIASES 35 /* maximum aliases allowed */ |
54 |
#define RES_MAXADDRS 35 /* maximum addresses allowed */ |
55 |
#define AR_TTL 600 /* TTL in seconds for dns cache entries */ |
56 |
|
57 |
/* RFC 1104/1105 wasn't very helpful about what these fields |
58 |
* should be named, so for now, we'll just name them this way. |
59 |
* we probably should look at what named calls them or something. |
60 |
*/ |
61 |
#define TYPE_SIZE (size_t)2 |
62 |
#define CLASS_SIZE (size_t)2 |
63 |
#define TTL_SIZE (size_t)4 |
64 |
#define RDLENGTH_SIZE (size_t)2 |
65 |
#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) |
66 |
|
67 |
typedef enum |
68 |
{ |
69 |
REQ_IDLE, /* We're doing not much at all */ |
70 |
REQ_PTR, /* Looking up a PTR */ |
71 |
REQ_A, /* Looking up an A, possibly because AAAA failed */ |
72 |
REQ_AAAA, /* Looking up an AAAA */ |
73 |
REQ_CNAME /* We got a CNAME in response, we better get a real answer next */ |
74 |
} request_state; |
75 |
|
76 |
struct reslist |
77 |
{ |
78 |
dlink_node node; |
79 |
int id; |
80 |
int sent; /* number of requests sent */ |
81 |
request_state state; /* State the resolver machine is in */ |
82 |
time_t ttl; |
83 |
char type; |
84 |
char retries; /* retry counter */ |
85 |
char sends; /* number of sends (>1 means resent) */ |
86 |
char resend; /* send flag. 0 == dont resend */ |
87 |
time_t sentat; |
88 |
time_t timeout; |
89 |
struct irc_ssaddr addr; |
90 |
char *name; |
91 |
dns_callback_fnc callback; |
92 |
void *callback_ctx; |
93 |
}; |
94 |
|
95 |
static dlink_list request_list = { NULL, NULL, 0 }; |
96 |
static mp_pool_t *dns_pool = NULL; |
97 |
|
98 |
static void timeout_resolver(struct Event *); |
99 |
static void rem_request(struct reslist *); |
100 |
static struct reslist *make_request(dns_callback_fnc, void *); |
101 |
static void do_query_name(dns_callback_fnc, void *, |
102 |
const char *, struct reslist *, int); |
103 |
static void do_query_number(dns_callback_fnc, void *, |
104 |
const struct irc_ssaddr *, |
105 |
struct reslist *); |
106 |
static void query_name(const char *, int, int, struct reslist *); |
107 |
static int send_res_msg(const char *, int, int); |
108 |
static void resend_query(struct reslist *); |
109 |
static void res_readreply(struct Event *); |
110 |
static int proc_answer(struct reslist *, HEADER *, char *, char *); |
111 |
static struct reslist *find_id(int); |
112 |
|
113 |
|
114 |
/* |
115 |
* int |
116 |
* res_ourserver(inp) |
117 |
* looks up "inp" in irc_nsaddr_list[] |
118 |
* returns: |
119 |
* 0 : not found |
120 |
* >0 : found |
121 |
* author: |
122 |
* paul vixie, 29may94 |
123 |
* revised for ircd, cryogen(stu) may03 |
124 |
*/ |
125 |
static int |
126 |
res_ourserver(const struct irc_ssaddr *inp) |
127 |
{ |
128 |
const struct sockaddr_in6 *v6; |
129 |
const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; |
130 |
const struct sockaddr_in *v4; |
131 |
const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; |
132 |
int ns; |
133 |
|
134 |
for (ns = 0; ns < irc_nscount; ++ns) |
135 |
{ |
136 |
const struct irc_ssaddr *srv = &irc_nsaddr_list[ns]; |
137 |
|
138 |
v6 = (const struct sockaddr_in6 *)srv; |
139 |
v4 = (const struct sockaddr_in *)srv; |
140 |
|
141 |
/* could probably just memcmp(srv, inp, srv.ss_len) here |
142 |
* but we'll air on the side of caution - stu |
143 |
* |
144 |
*/ |
145 |
switch (srv->ss.ss_family) |
146 |
{ |
147 |
case AF_INET6: |
148 |
if (srv->ss.ss_family == inp->ss.ss_family) |
149 |
if (v6->sin6_port == v6in->sin6_port) |
150 |
if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, |
151 |
sizeof(struct in6_addr))) |
152 |
return 1; |
153 |
break; |
154 |
case AF_INET: |
155 |
if (srv->ss.ss_family == inp->ss.ss_family) |
156 |
if (v4->sin_port == v4in->sin_port) |
157 |
if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr) |
158 |
return 1; |
159 |
break; |
160 |
default: |
161 |
break; |
162 |
} |
163 |
} |
164 |
|
165 |
return 0; |
166 |
} |
167 |
|
168 |
/** Make sure that a timeout event will happen by the given time. |
169 |
* @param[in] when Latest time for timeout to run. |
170 |
*/ |
171 |
static void |
172 |
check_resolver_timeout(time_t when) |
173 |
{ |
174 |
if (when > CurrentTime + AR_TTL) |
175 |
when = CurrentTime + AR_TTL; |
176 |
/* TODO after 2.10.12: Rewrite the timer API because there should be |
177 |
* no need for clients to know this kind of implementation detail. */ |
178 |
if (when > t_expire(&res_timeout)) |
179 |
/* do nothing */; |
180 |
else if (t_onqueue(&res_timeout) && !(res_timeout.t_header.gh_flags & GEN_MARKED)) |
181 |
timer_chg(&res_timeout, TT_ABSOLUTE, when); |
182 |
else |
183 |
timer_add(&res_timeout, timeout_resolver, NULL, TT_ABSOLUTE, when); |
184 |
} |
185 |
|
186 |
/* |
187 |
* timeout_query_list - Remove queries from the list which have been |
188 |
* there too long without being resolved. |
189 |
*/ |
190 |
static void |
191 |
timeout_resolver(struct Event *ev) |
192 |
{ |
193 |
dlink_node *ptr; |
194 |
dlink_node *next_ptr; |
195 |
struct reslist *request; |
196 |
time_t next_time = 0; |
197 |
time_t timeout = 0; |
198 |
|
199 |
DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head) |
200 |
{ |
201 |
request = ptr->data; |
202 |
timeout = request->sentat + request->timeout; |
203 |
|
204 |
if (CurrentTime >= timeout) |
205 |
{ |
206 |
if (--request->retries <= 0) |
207 |
{ |
208 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
209 |
rem_request(request); |
210 |
continue; |
211 |
} |
212 |
else |
213 |
{ |
214 |
request->sentat = CurrentTime; |
215 |
request->timeout += request->timeout; |
216 |
resend_query(request); |
217 |
} |
218 |
} |
219 |
|
220 |
if ((next_time == 0) || timeout < next_time) |
221 |
next_time = timeout; |
222 |
} |
223 |
|
224 |
if (next_time <= CurrentTime) |
225 |
next_time = CurrentTime + AR_TTL; |
226 |
check_resolver_timeout(next_time); |
227 |
} |
228 |
|
229 |
/* |
230 |
* start_resolver - do everything we need to read the resolv.conf file |
231 |
* and initialize the resolver file descriptor if needed |
232 |
*/ |
233 |
static void |
234 |
start_resolver(void) |
235 |
{ |
236 |
int need_v4 = 0; |
237 |
int need_v6 = 0; |
238 |
int ns = 0; |
239 |
|
240 |
irc_res_init(); |
241 |
|
242 |
/* Check which address family (or families) our nameservers use. */ |
243 |
for (; ns < irc_nscount; ++ns) |
244 |
{ |
245 |
if (irc_nsaddr_list[ns].ss.ss_family == AF_INET) |
246 |
need_v4 = 1; |
247 |
else |
248 |
need_v6 = 1; |
249 |
} |
250 |
|
251 |
/* If we need an IPv4 socket, and don't have one, open it. */ |
252 |
if (need_v4 && !s_active(&res_socket_v4)) |
253 |
{ |
254 |
int fd = os_socket(SOCK_DGRAM, AF_INET); |
255 |
if (fd >= 0) |
256 |
socket_add(&res_socket_v4, res_readreply, NULL, |
257 |
SS_DATAGRAM, SOCK_EVENT_READABLE, fd); |
258 |
} |
259 |
|
260 |
/* If we need an IPv6 socket, and don't have one, open it. */ |
261 |
if (need_v6 && !s_active(&res_socket_v6)) |
262 |
{ |
263 |
int fd = os_socket(SOCK_DGRAM, AF_INET6); |
264 |
if (fd >= 0) |
265 |
socket_add(&res_socket_v6, res_readreply, NULL, |
266 |
SS_DATAGRAM, SOCK_EVENT_READABLE, fd); |
267 |
} |
268 |
|
269 |
if (s_active(&res_socket_v4) || s_active(&res_socket_v6)) |
270 |
timer_init(&res_timeout); |
271 |
} |
272 |
|
273 |
/* |
274 |
* init_resolver - initialize resolver and resolver library |
275 |
*/ |
276 |
void |
277 |
init_resolver(void) |
278 |
{ |
279 |
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
280 |
start_resolver(); |
281 |
} |
282 |
|
283 |
/* |
284 |
* restart_resolver - reread resolv.conf, reopen socket |
285 |
*/ |
286 |
void |
287 |
restart_resolver(void) |
288 |
{ |
289 |
start_resolver(); |
290 |
} |
291 |
|
292 |
/* |
293 |
* rem_request - remove a request from the list. |
294 |
* This must also free any memory that has been allocated for |
295 |
* temporary storage of DNS results. |
296 |
*/ |
297 |
static void |
298 |
rem_request(struct reslist *request) |
299 |
{ |
300 |
dlinkDelete(&request->node, &request_list); |
301 |
|
302 |
MyFree(request->name); |
303 |
mp_pool_release(request); |
304 |
} |
305 |
|
306 |
/* |
307 |
* make_request - Create a DNS request record for the server. |
308 |
*/ |
309 |
static struct reslist * |
310 |
make_request(dns_callback_fnc callback, void *ctx) |
311 |
{ |
312 |
struct reslist *request = mp_pool_get(dns_pool); |
313 |
|
314 |
memset(request, 0, sizeof(*request)); |
315 |
request->sentat = CurrentTime; |
316 |
request->retries = 3; |
317 |
request->resend = 1; |
318 |
request->timeout = 4; /* start at 4 and exponential inc. */ |
319 |
request->state = REQ_IDLE; |
320 |
request->callback = callback; |
321 |
request->callback_ctx = ctx; |
322 |
|
323 |
dlinkAdd(request, &request->node, &request_list); |
324 |
return request; |
325 |
} |
326 |
|
327 |
/* |
328 |
* delete_resolver_queries - cleanup outstanding queries |
329 |
* for which there no longer exist clients or conf lines. |
330 |
*/ |
331 |
void |
332 |
delete_resolver_queries(const void *vptr) |
333 |
{ |
334 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
335 |
|
336 |
DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head) |
337 |
{ |
338 |
struct reslist *request = ptr->data; |
339 |
|
340 |
if (request->callback_ctx == vptr) |
341 |
rem_request(request); |
342 |
} |
343 |
} |
344 |
|
345 |
/* |
346 |
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
347 |
* This should reflect /etc/resolv.conf. We will get responses |
348 |
* which arent needed but is easier than checking to see if nameserver |
349 |
* isnt present. Returns number of messages successfully sent to |
350 |
* nameservers or -1 if no successful sends. |
351 |
*/ |
352 |
static int |
353 |
send_res_msg(const char *msg, int len, int rcount) |
354 |
{ |
355 |
int i; |
356 |
int sent = 0; |
357 |
int max_queries = IRCD_MIN(irc_nscount, rcount); |
358 |
|
359 |
/* |
360 |
* RES_PRIMARY option is not implemented |
361 |
* if (res.options & RES_PRIMARY || 0 == max_queries) |
362 |
*/ |
363 |
if (max_queries == 0) |
364 |
max_queries = 1; |
365 |
|
366 |
for (i = 0; i < max_queries; i++) |
367 |
{ |
368 |
int fd = (irc_nsaddr_list[i].ss.ss_family == AF_INET) ? s_fd(&res_socket_v4) : |
369 |
s_fd(&res_socket_v6); |
370 |
|
371 |
if (os_sendto_nonb(fd, msg, len, NULL, 0, &irc_nsaddr_list[i]) == IO_SUCCESS) |
372 |
++sent; |
373 |
} |
374 |
|
375 |
return sent; |
376 |
} |
377 |
|
378 |
/* |
379 |
* find_id - find a dns request id (id is determined by dn_mkquery) |
380 |
*/ |
381 |
static struct reslist * |
382 |
find_id(int id) |
383 |
{ |
384 |
dlink_node *ptr = NULL; |
385 |
|
386 |
DLINK_FOREACH(ptr, request_list.head) |
387 |
{ |
388 |
struct reslist *request = ptr->data; |
389 |
|
390 |
if (request->id == id) |
391 |
return request; |
392 |
} |
393 |
|
394 |
return NULL; |
395 |
} |
396 |
|
397 |
/* |
398 |
* gethost_byname_type - get host address from name |
399 |
* |
400 |
*/ |
401 |
void |
402 |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
403 |
{ |
404 |
assert(name != NULL); |
405 |
do_query_name(callback, ctx, name, NULL, type); |
406 |
} |
407 |
|
408 |
/* |
409 |
* gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported |
410 |
*/ |
411 |
void |
412 |
gethost_byname(dns_callback_fnc callback, void *ctx, const char *name) |
413 |
{ |
414 |
gethost_byname_type(callback, ctx, name, T_AAAA); |
415 |
} |
416 |
|
417 |
/* |
418 |
* gethost_byaddr - get host name from address |
419 |
*/ |
420 |
void |
421 |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
422 |
{ |
423 |
do_query_number(callback, ctx, addr, NULL); |
424 |
} |
425 |
|
426 |
/* |
427 |
* do_query_name - nameserver lookup name |
428 |
*/ |
429 |
static void |
430 |
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
431 |
struct reslist *request, int type) |
432 |
{ |
433 |
char host_name[HOSTLEN + 1]; |
434 |
|
435 |
strlcpy(host_name, name, sizeof(host_name)); |
436 |
|
437 |
if (request == NULL) |
438 |
{ |
439 |
request = make_request(callback, ctx); |
440 |
request->name = MyMalloc(strlen(host_name) + 1); |
441 |
request->type = type; |
442 |
strcpy(request->name, host_name); |
443 |
|
444 |
if (type != T_A) |
445 |
request->state = REQ_AAAA; |
446 |
else |
447 |
request->state = REQ_A; |
448 |
} |
449 |
|
450 |
request->type = type; |
451 |
query_name(host_name, C_IN, type, request); |
452 |
} |
453 |
|
454 |
/* |
455 |
* do_query_number - Use this to do reverse IP# lookups. |
456 |
*/ |
457 |
static void |
458 |
do_query_number(dns_callback_fnc callback, void *ctx, |
459 |
const struct irc_ssaddr *addr, |
460 |
struct reslist *request) |
461 |
{ |
462 |
char ipbuf[128]; |
463 |
const unsigned char *cp; |
464 |
|
465 |
if (addr->ss.ss_family == AF_INET) |
466 |
{ |
467 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
468 |
cp = (const unsigned char *)&v4->sin_addr.s_addr; |
469 |
|
470 |
snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.", |
471 |
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
472 |
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
473 |
} |
474 |
else if (addr->ss.ss_family == AF_INET6) |
475 |
{ |
476 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
477 |
cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
478 |
|
479 |
snprintf(ipbuf, sizeof(ipbuf), |
480 |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
481 |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
482 |
(unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), |
483 |
(unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), |
484 |
(unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), |
485 |
(unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), |
486 |
(unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), |
487 |
(unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), |
488 |
(unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), |
489 |
(unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), |
490 |
(unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), |
491 |
(unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), |
492 |
(unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), |
493 |
(unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), |
494 |
(unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), |
495 |
(unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), |
496 |
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
497 |
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
498 |
} |
499 |
|
500 |
if (request == NULL) |
501 |
{ |
502 |
request = make_request(callback, ctx); |
503 |
request->type = T_PTR; |
504 |
memcpy(&request->addr, addr, sizeof(struct irc_ssaddr)); |
505 |
request->name = MyMalloc(HOSTLEN + 1); |
506 |
} |
507 |
|
508 |
query_name(ipbuf, C_IN, T_PTR, request); |
509 |
} |
510 |
|
511 |
/* |
512 |
* query_name - generate a query based on class, type and name. |
513 |
*/ |
514 |
static void |
515 |
query_name(const char *name, int query_class, int type, |
516 |
struct reslist *request) |
517 |
{ |
518 |
char buf[MAXPACKET]; |
519 |
int request_len = 0; |
520 |
|
521 |
memset(buf, 0, sizeof(buf)); |
522 |
|
523 |
if ((request_len = irc_res_mkquery(name, query_class, type, |
524 |
(unsigned char *)buf, sizeof(buf))) > 0) |
525 |
{ |
526 |
HEADER *header = (HEADER *)buf; |
527 |
|
528 |
/* |
529 |
* generate an unique id |
530 |
* NOTE: we don't have to worry about converting this to and from |
531 |
* network byte order, the nameserver does not interpret this value |
532 |
* and returns it unchanged |
533 |
*/ |
534 |
do |
535 |
header->id = (header->id + genrand_int32()) & 0xffff; |
536 |
while (find_id(header->id)); |
537 |
|
538 |
request->id = header->id; |
539 |
++request->sends; |
540 |
|
541 |
request->sent += send_res_msg(buf, request_len, request->sends); |
542 |
} |
543 |
} |
544 |
|
545 |
static void |
546 |
resend_query(struct reslist *request) |
547 |
{ |
548 |
if (request->resend == 0) |
549 |
return; |
550 |
|
551 |
switch (request->type) |
552 |
{ |
553 |
case T_PTR: |
554 |
do_query_number(NULL, NULL, &request->addr, request); |
555 |
break; |
556 |
case T_A: |
557 |
do_query_name(NULL, NULL, request->name, request, request->type); |
558 |
break; |
559 |
case T_AAAA: |
560 |
/* didnt work, try A */ |
561 |
if (request->state == REQ_AAAA) |
562 |
do_query_name(NULL, NULL, request->name, request, T_A); |
563 |
default: |
564 |
break; |
565 |
} |
566 |
} |
567 |
|
568 |
/* |
569 |
* proc_answer - process name server reply |
570 |
*/ |
571 |
static int |
572 |
proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob) |
573 |
{ |
574 |
char hostbuf[HOSTLEN + 100]; /* working buffer */ |
575 |
unsigned char *current; /* current position in buf */ |
576 |
int query_class; /* answer class */ |
577 |
int type; /* answer type */ |
578 |
int n; /* temp count */ |
579 |
int rd_length; |
580 |
struct sockaddr_in *v4; /* conversion */ |
581 |
struct sockaddr_in6 *v6; |
582 |
|
583 |
current = (unsigned char *)buf + sizeof(HEADER); |
584 |
|
585 |
for (; header->qdcount > 0; --header->qdcount) |
586 |
{ |
587 |
if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0) |
588 |
break; |
589 |
|
590 |
current += (size_t)n + QFIXEDSZ; |
591 |
} |
592 |
|
593 |
/* |
594 |
* process each answer sent to us blech. |
595 |
*/ |
596 |
while (header->ancount > 0 && (char *)current < eob) |
597 |
{ |
598 |
header->ancount--; |
599 |
|
600 |
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, |
601 |
hostbuf, sizeof(hostbuf)); |
602 |
|
603 |
if (n < 0 /* broken message */ || n == 0 /* no more answers left */) |
604 |
return 0; |
605 |
|
606 |
hostbuf[HOSTLEN] = '\0'; |
607 |
|
608 |
/* With Address arithmetic you have to be very anal |
609 |
* this code was not working on alpha due to that |
610 |
* (spotted by rodder/jailbird/dianora) |
611 |
*/ |
612 |
current += (size_t) n; |
613 |
|
614 |
if (!(((char *)current + ANSWER_FIXED_SIZE) < eob)) |
615 |
break; |
616 |
|
617 |
type = irc_ns_get16(current); |
618 |
current += TYPE_SIZE; |
619 |
|
620 |
query_class = irc_ns_get16(current); |
621 |
current += CLASS_SIZE; |
622 |
|
623 |
request->ttl = irc_ns_get32(current); |
624 |
current += TTL_SIZE; |
625 |
|
626 |
rd_length = irc_ns_get16(current); |
627 |
current += RDLENGTH_SIZE; |
628 |
|
629 |
/* |
630 |
* Wait to set request->type until we verify this structure |
631 |
*/ |
632 |
switch (type) |
633 |
{ |
634 |
case T_A: |
635 |
if (request->type != T_A) |
636 |
return 0; |
637 |
|
638 |
/* |
639 |
* check for invalid rd_length or too many addresses |
640 |
*/ |
641 |
if (rd_length != sizeof(struct in_addr)) |
642 |
return 0; |
643 |
|
644 |
v4 = (struct sockaddr_in *)&request->addr; |
645 |
request->addr.ss_len = sizeof(struct sockaddr_in); |
646 |
v4->sin_family = AF_INET; |
647 |
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
648 |
return 1; |
649 |
break; |
650 |
|
651 |
case T_AAAA: |
652 |
if (request->type != T_AAAA) |
653 |
return 0; |
654 |
|
655 |
if (rd_length != sizeof(struct in6_addr)) |
656 |
return 0; |
657 |
|
658 |
request->addr.ss_len = sizeof(struct sockaddr_in6); |
659 |
v6 = (struct sockaddr_in6 *)&request->addr; |
660 |
v6->sin6_family = AF_INET6; |
661 |
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
662 |
return 1; |
663 |
break; |
664 |
|
665 |
case T_PTR: |
666 |
if (request->type != T_PTR) |
667 |
return 0; |
668 |
|
669 |
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, |
670 |
current, hostbuf, sizeof(hostbuf)); |
671 |
if (n < 0 /* broken message */ || n == 0 /* no more answers left */) |
672 |
return 0; |
673 |
|
674 |
strlcpy(request->name, hostbuf, HOSTLEN + 1); |
675 |
return 1; |
676 |
break; |
677 |
case T_CNAME: /* first check we already havent started looking |
678 |
into a cname */ |
679 |
if (request->type != T_PTR) |
680 |
return 0; |
681 |
|
682 |
if (request->state == REQ_CNAME) |
683 |
{ |
684 |
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, |
685 |
current, hostbuf, sizeof(hostbuf)); |
686 |
|
687 |
if (n < 0) |
688 |
return 0; |
689 |
return 1; |
690 |
} |
691 |
|
692 |
request->state = REQ_CNAME; |
693 |
current += rd_length; |
694 |
break; |
695 |
|
696 |
default: |
697 |
/* XXX I'd rather just throw away the entire bogus thing |
698 |
* but its possible its just a broken nameserver with still |
699 |
* valid answers. But lets do some rudimentary logging for now... |
700 |
*/ |
701 |
ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type); |
702 |
break; |
703 |
} |
704 |
} |
705 |
|
706 |
return 1; |
707 |
} |
708 |
|
709 |
/* |
710 |
* res_readreply - read a dns reply from the nameserver and process it. |
711 |
*/ |
712 |
static void |
713 |
res_readreply(struct Event *ev) |
714 |
{ |
715 |
char buf[sizeof(HEADER) + MAXPACKET]; |
716 |
HEADER *header; |
717 |
struct reslist *request = NULL; |
718 |
struct irc_ssaddr lsin; |
719 |
struct Socket *sock = ev_socket(ev); |
720 |
unsigned int rc = 0; |
721 |
|
722 |
assert((ev_socket(ev) == &res_socket_v4) || (ev_socket(ev) == &res_socket_v6)); |
723 |
|
724 |
if (IO_SUCCESS != os_recvfrom_nonb(s_fd(sock), buf, sizeof(buf), &rc, &lsin) || |
725 |
(rc <= sizeof(HEADER))) |
726 |
return; |
727 |
|
728 |
/* |
729 |
* check against possibly fake replies |
730 |
*/ |
731 |
if (!res_ourserver(&lsin)) |
732 |
return; |
733 |
|
734 |
/* |
735 |
* convert DNS reply reader from Network byte order to CPU byte order. |
736 |
*/ |
737 |
header = (HEADER *)buf; |
738 |
header->ancount = ntohs(header->ancount); |
739 |
header->qdcount = ntohs(header->qdcount); |
740 |
header->nscount = ntohs(header->nscount); |
741 |
header->arcount = ntohs(header->arcount); |
742 |
|
743 |
/* |
744 |
* response for an id which we have already received an answer for |
745 |
* just ignore this response. |
746 |
*/ |
747 |
if (!(request = find_id(header->id))) |
748 |
return; |
749 |
|
750 |
if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) |
751 |
{ |
752 |
if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN) |
753 |
{ |
754 |
/* |
755 |
* If a bad error was returned, stop here and don't |
756 |
* send any more (no retries granted). |
757 |
*/ |
758 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
759 |
rem_request(request); |
760 |
} |
761 |
else |
762 |
{ |
763 |
/* |
764 |
* If we havent already tried this, and we're looking up AAAA, try A |
765 |
* now |
766 |
*/ |
767 |
if (request->state == REQ_AAAA && request->type == T_AAAA) |
768 |
{ |
769 |
request->timeout += 4; |
770 |
resend_query(request); |
771 |
} |
772 |
} |
773 |
|
774 |
return; |
775 |
} |
776 |
|
777 |
/* |
778 |
* If this fails there was an error decoding the received packet, |
779 |
* try it again and hope it works the next time. |
780 |
*/ |
781 |
if (proc_answer(request, header, buf, buf + rc)) |
782 |
{ |
783 |
if (request->type == T_PTR) |
784 |
{ |
785 |
if (request->name == NULL) |
786 |
{ |
787 |
/* |
788 |
* got a PTR response with no name, something bogus is happening |
789 |
* don't bother trying again, the client address doesn't resolve |
790 |
*/ |
791 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
792 |
rem_request(request); |
793 |
return; |
794 |
} |
795 |
|
796 |
/* |
797 |
* Lookup the 'authoritative' name that we were given for the |
798 |
* ip#. |
799 |
* |
800 |
*/ |
801 |
if (request->addr.ss.ss_family == AF_INET6) |
802 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
803 |
else |
804 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
805 |
rem_request(request); |
806 |
} |
807 |
else |
808 |
{ |
809 |
/* |
810 |
* got a name and address response, client resolved |
811 |
*/ |
812 |
(*request->callback)(request->callback_ctx, &request->addr, request->name); |
813 |
rem_request(request); |
814 |
} |
815 |
} |
816 |
else if (!request->sent) |
817 |
{ |
818 |
/* XXX - we got a response for a query we didn't send with a valid id? |
819 |
* this should never happen, bail here and leave the client unresolved |
820 |
*/ |
821 |
assert(0); |
822 |
|
823 |
/* XXX don't leak it */ |
824 |
rem_request(request); |
825 |
} |
826 |
} |
827 |
|
828 |
void |
829 |
report_dns_servers(struct Client *source_p) |
830 |
{ |
831 |
int i; |
832 |
char ipaddr[HOSTIPLEN + 1]; |
833 |
|
834 |
for (i = 0; i < irc_nscount; i++) |
835 |
{ |
836 |
getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]), |
837 |
irc_nsaddr_list[i].ss_len, ipaddr, |
838 |
sizeof(ipaddr), NULL, 0, NI_NUMERICHOST); |
839 |
sendto_one(source_p, form_str(RPL_STATSALINE), |
840 |
me.name, source_p->name, ipaddr); |
841 |
} |
842 |
} |