| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 1997-2016 ircd-hybrid development team |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file res.c |
| 23 |
* \brief ircd resolver functions |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
/* |
| 28 |
* A rewrite of Darren Reed's original res.c As there is nothing |
| 29 |
* left of Darren's original code, this is now licensed by the hybrid group. |
| 30 |
* (Well, some of the function names are the same, and bits of the structs..) |
| 31 |
* You can use it where it is useful, free even. Buy us a beer and stuff. |
| 32 |
* |
| 33 |
* The authors takes no responsibility for any damage or loss |
| 34 |
* of property which results from the use of this software. |
| 35 |
* |
| 36 |
* July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, |
| 37 |
* added callbacks and reference counting of returned hostents. |
| 38 |
* --Bleep (Thomas Helvey <tomh@inxpress.net>) |
| 39 |
* |
| 40 |
* This was all needlessly complicated for irc. Simplified. No more hostent |
| 41 |
* All we really care about is the IP -> hostname mappings. That's all. |
| 42 |
* |
| 43 |
* Apr 28, 2003 --cryogen and Dianora |
| 44 |
*/ |
| 45 |
|
| 46 |
#include "stdinc.h" |
| 47 |
#include "list.h" |
| 48 |
#include "event.h" |
| 49 |
#include "irc_string.h" |
| 50 |
#include "ircd.h" |
| 51 |
#include "rng_mt.h" |
| 52 |
#include "fdlist.h" |
| 53 |
#include "s_bsd.h" |
| 54 |
#include "misc.h" |
| 55 |
#include "mempool.h" |
| 56 |
#include "res.h" |
| 57 |
#include "reslib.h" |
| 58 |
|
| 59 |
#if (CHAR_BIT != 8) |
| 60 |
#error this code needs to be able to address individual octets |
| 61 |
#endif |
| 62 |
|
| 63 |
static void res_readreply(fde_t *, void *); |
| 64 |
|
| 65 |
#define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */ |
| 66 |
#define AR_TTL 600 /**< TTL in seconds for dns cache entries */ |
| 67 |
|
| 68 |
/* |
| 69 |
* RFC 1104/1105 wasn't very helpful about what these fields |
| 70 |
* should be named, so for now, we'll just name them this way. |
| 71 |
* We probably should look at what named calls them or something. |
| 72 |
*/ |
| 73 |
#define TYPE_SIZE (size_t)2 |
| 74 |
#define CLASS_SIZE (size_t)2 |
| 75 |
#define TTL_SIZE (size_t)4 |
| 76 |
#define RDLENGTH_SIZE (size_t)2 |
| 77 |
#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) |
| 78 |
|
| 79 |
struct reslist |
| 80 |
{ |
| 81 |
dlink_node node; /**< Doubly linked list node. */ |
| 82 |
unsigned int id; /**< Request ID (from request header). */ |
| 83 |
char type; /**< Current request type. */ |
| 84 |
char retries; /**< Retry counter */ |
| 85 |
unsigned int sends; /**< Number of sends (>1 means resent). */ |
| 86 |
uintmax_t sentat; /**< Timestamp we last sent this request. */ |
| 87 |
uintmax_t timeout; /**< When this request times out. */ |
| 88 |
struct irc_ssaddr addr; /**< Address for this request. */ |
| 89 |
char name[RFC1035_MAX_DOMAIN_LENGTH + 1]; /**< Hostname for this request. */ |
| 90 |
size_t namelength; /**< Actual hostname length. */ |
| 91 |
dns_callback_fnc callback; /**< Callback function on completion. */ |
| 92 |
void *callback_ctx; /**< Context pointer for callback. */ |
| 93 |
}; |
| 94 |
|
| 95 |
static fde_t ResolverFileDescriptor; |
| 96 |
static dlink_list request_list; |
| 97 |
static mp_pool_t *dns_pool; |
| 98 |
|
| 99 |
|
| 100 |
/* |
| 101 |
* rem_request - remove a request from the list. |
| 102 |
* This must also free any memory that has been allocated for |
| 103 |
* temporary storage of DNS results. |
| 104 |
*/ |
| 105 |
static void |
| 106 |
rem_request(struct reslist *request) |
| 107 |
{ |
| 108 |
dlinkDelete(&request->node, &request_list); |
| 109 |
mp_pool_release(request); |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* make_request - Create a DNS request record for the server. |
| 114 |
*/ |
| 115 |
static struct reslist * |
| 116 |
make_request(dns_callback_fnc callback, void *ctx) |
| 117 |
{ |
| 118 |
struct reslist *request = mp_pool_get(dns_pool); |
| 119 |
|
| 120 |
request->sentat = CurrentTime; |
| 121 |
request->retries = 2; |
| 122 |
request->timeout = 4; /* Start at 4 and exponential inc. */ |
| 123 |
request->callback = callback; |
| 124 |
request->callback_ctx = ctx; |
| 125 |
|
| 126 |
dlinkAdd(request, &request->node, &request_list); |
| 127 |
return request; |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
* int |
| 132 |
* res_ourserver(inp) |
| 133 |
* looks up "inp" in irc_nsaddr_list[] |
| 134 |
* returns: |
| 135 |
* 0 : not found |
| 136 |
* >0 : found |
| 137 |
* author: |
| 138 |
* paul vixie, 29may94 |
| 139 |
* revised for ircd, cryogen(stu) may03 |
| 140 |
*/ |
| 141 |
static int |
| 142 |
res_ourserver(const struct irc_ssaddr *inp) |
| 143 |
{ |
| 144 |
const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; |
| 145 |
const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; |
| 146 |
|
| 147 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
| 148 |
{ |
| 149 |
const struct irc_ssaddr *srv = &irc_nsaddr_list[i]; |
| 150 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)srv; |
| 151 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)srv; |
| 152 |
|
| 153 |
/* |
| 154 |
* Could probably just memcmp(srv, inp, srv.ss_len) here |
| 155 |
* but we'll air on the side of caution - stu |
| 156 |
*/ |
| 157 |
switch (srv->ss.ss_family) |
| 158 |
{ |
| 159 |
case AF_INET6: |
| 160 |
if (srv->ss.ss_family == inp->ss.ss_family) |
| 161 |
if (v6->sin6_port == v6in->sin6_port) |
| 162 |
if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, |
| 163 |
sizeof(struct in6_addr))) |
| 164 |
return 1; |
| 165 |
break; |
| 166 |
case AF_INET: |
| 167 |
if (srv->ss.ss_family == inp->ss.ss_family) |
| 168 |
if (v4->sin_port == v4in->sin_port) |
| 169 |
if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr) |
| 170 |
return 1; |
| 171 |
break; |
| 172 |
default: |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* start_resolver - do everything we need to read the resolv.conf file |
| 182 |
* and initialize the resolver file descriptor if needed |
| 183 |
*/ |
| 184 |
static void |
| 185 |
start_resolver(void) |
| 186 |
{ |
| 187 |
irc_res_init(); |
| 188 |
|
| 189 |
if (!ResolverFileDescriptor.flags.open) |
| 190 |
{ |
| 191 |
if (comm_open(&ResolverFileDescriptor, irc_nsaddr_list[0].ss.ss_family, |
| 192 |
SOCK_DGRAM, 0, "UDP resolver socket") == -1) |
| 193 |
return; |
| 194 |
|
| 195 |
/* At the moment, the resolver FD data is global .. */ |
| 196 |
comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/* |
| 201 |
* restart_resolver - reread resolv.conf, reopen socket |
| 202 |
*/ |
| 203 |
void |
| 204 |
restart_resolver(void) |
| 205 |
{ |
| 206 |
fd_close(&ResolverFileDescriptor); |
| 207 |
start_resolver(); |
| 208 |
} |
| 209 |
|
| 210 |
/* |
| 211 |
* delete_resolver_queries - cleanup outstanding queries |
| 212 |
* for which there no longer exist clients or conf lines. |
| 213 |
*/ |
| 214 |
void |
| 215 |
delete_resolver_queries(const void *vptr) |
| 216 |
{ |
| 217 |
dlink_node *node = NULL, *node_next = NULL; |
| 218 |
|
| 219 |
DLINK_FOREACH_SAFE(node, node_next, request_list.head) |
| 220 |
{ |
| 221 |
struct reslist *request = node->data; |
| 222 |
|
| 223 |
if (request->callback_ctx == vptr) |
| 224 |
rem_request(request); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
| 230 |
* This should reflect /etc/resolv.conf. We will get responses |
| 231 |
* which arent needed but is easier than checking to see if nameserver |
| 232 |
* isn't present. Returns number of messages successfully sent to |
| 233 |
* nameservers or -1 if no successful sends. |
| 234 |
*/ |
| 235 |
static void |
| 236 |
send_res_msg(const unsigned char *msg, int len, unsigned int rcount) |
| 237 |
{ |
| 238 |
unsigned int max_queries = IRCD_MIN(irc_nscount, rcount); |
| 239 |
|
| 240 |
/* RES_PRIMARY option is not implemented |
| 241 |
* if (res.options & RES_PRIMARY || 0 == max_queries) |
| 242 |
*/ |
| 243 |
if (max_queries == 0) |
| 244 |
max_queries = 1; |
| 245 |
|
| 246 |
for (unsigned int i = 0; i < max_queries; ++i) |
| 247 |
sendto(ResolverFileDescriptor.fd, msg, len, 0, |
| 248 |
(struct sockaddr *)&irc_nsaddr_list[i], irc_nsaddr_list[i].ss_len); |
| 249 |
} |
| 250 |
|
| 251 |
/* |
| 252 |
* find_id - find a dns request id (id is determined by dn_mkquery) |
| 253 |
*/ |
| 254 |
static struct reslist * |
| 255 |
find_id(unsigned int id) |
| 256 |
{ |
| 257 |
dlink_node *node = NULL; |
| 258 |
|
| 259 |
DLINK_FOREACH(node, request_list.head) |
| 260 |
{ |
| 261 |
struct reslist *request = node->data; |
| 262 |
|
| 263 |
if (request->id == id) |
| 264 |
return request; |
| 265 |
} |
| 266 |
|
| 267 |
return NULL; |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* query_name - generate a query based on class, type and name. |
| 272 |
*/ |
| 273 |
static void |
| 274 |
query_name(const char *name, int query_class, int type, struct reslist *request) |
| 275 |
{ |
| 276 |
unsigned char buf[MAXPACKET]; |
| 277 |
int request_len = 0; |
| 278 |
|
| 279 |
memset(buf, 0, sizeof(buf)); |
| 280 |
|
| 281 |
if ((request_len = irc_res_mkquery(name, query_class, type, buf, sizeof(buf))) > 0) |
| 282 |
{ |
| 283 |
HEADER *header = (HEADER *)buf; |
| 284 |
|
| 285 |
/* |
| 286 |
* Generate an unique id. |
| 287 |
* NOTE: we don't have to worry about converting this to and from |
| 288 |
* network byte order, the nameserver does not interpret this value |
| 289 |
* and returns it unchanged. |
| 290 |
*/ |
| 291 |
do |
| 292 |
header->id = (header->id + genrand_int32()) & 0xFFFF; |
| 293 |
while (find_id(header->id)); |
| 294 |
|
| 295 |
request->id = header->id; |
| 296 |
++request->sends; |
| 297 |
|
| 298 |
send_res_msg(buf, request_len, request->sends); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/* |
| 303 |
* do_query_name - nameserver lookup name |
| 304 |
*/ |
| 305 |
static void |
| 306 |
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
| 307 |
struct reslist *request, int type) |
| 308 |
{ |
| 309 |
char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1]; |
| 310 |
|
| 311 |
strlcpy(host_name, name, sizeof(host_name)); |
| 312 |
|
| 313 |
if (!request) |
| 314 |
{ |
| 315 |
request = make_request(callback, ctx); |
| 316 |
request->type = type; |
| 317 |
request->namelength = strlcpy(request->name, host_name, sizeof(request->name)); |
| 318 |
} |
| 319 |
|
| 320 |
request->type = type; |
| 321 |
query_name(host_name, C_IN, type, request); |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* do_query_number - Use this to do reverse IP# lookups. |
| 326 |
*/ |
| 327 |
static void |
| 328 |
do_query_number(dns_callback_fnc callback, void *ctx, |
| 329 |
const struct irc_ssaddr *addr, |
| 330 |
struct reslist *request) |
| 331 |
{ |
| 332 |
char ipbuf[128] = ""; |
| 333 |
|
| 334 |
if (addr->ss.ss_family == AF_INET) |
| 335 |
{ |
| 336 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
| 337 |
const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr; |
| 338 |
|
| 339 |
snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.", |
| 340 |
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
| 341 |
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
| 342 |
} |
| 343 |
else if (addr->ss.ss_family == AF_INET6) |
| 344 |
{ |
| 345 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
| 346 |
const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
| 347 |
|
| 348 |
snprintf(ipbuf, sizeof(ipbuf), |
| 349 |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
| 350 |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
| 351 |
(unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), |
| 352 |
(unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), |
| 353 |
(unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), |
| 354 |
(unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), |
| 355 |
(unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), |
| 356 |
(unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), |
| 357 |
(unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), |
| 358 |
(unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), |
| 359 |
(unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), |
| 360 |
(unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), |
| 361 |
(unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), |
| 362 |
(unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), |
| 363 |
(unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), |
| 364 |
(unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), |
| 365 |
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
| 366 |
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
| 367 |
} |
| 368 |
|
| 369 |
if (!request) |
| 370 |
{ |
| 371 |
request = make_request(callback, ctx); |
| 372 |
request->type = T_PTR; |
| 373 |
memcpy(&request->addr, addr, sizeof(struct irc_ssaddr)); |
| 374 |
} |
| 375 |
|
| 376 |
query_name(ipbuf, C_IN, T_PTR, request); |
| 377 |
} |
| 378 |
|
| 379 |
/* |
| 380 |
* gethost_byname_type - get host address from name |
| 381 |
* |
| 382 |
*/ |
| 383 |
void |
| 384 |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
| 385 |
{ |
| 386 |
assert(name); |
| 387 |
do_query_name(callback, ctx, name, NULL, type); |
| 388 |
} |
| 389 |
|
| 390 |
/* |
| 391 |
* gethost_byaddr - get host name from address |
| 392 |
*/ |
| 393 |
void |
| 394 |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
| 395 |
{ |
| 396 |
do_query_number(callback, ctx, addr, NULL); |
| 397 |
} |
| 398 |
|
| 399 |
static void |
| 400 |
resend_query(struct reslist *request) |
| 401 |
{ |
| 402 |
switch (request->type) |
| 403 |
{ |
| 404 |
case T_PTR: |
| 405 |
do_query_number(NULL, NULL, &request->addr, request); |
| 406 |
break; |
| 407 |
case T_A: |
| 408 |
case T_AAAA: |
| 409 |
do_query_name(NULL, NULL, request->name, request, request->type); |
| 410 |
break; |
| 411 |
default: |
| 412 |
break; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
/* |
| 417 |
* proc_answer - process name server reply |
| 418 |
*/ |
| 419 |
static int |
| 420 |
proc_answer(struct reslist *request, HEADER *header, unsigned char *buf, unsigned char *eob) |
| 421 |
{ |
| 422 |
char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */ |
| 423 |
unsigned char *current = buf + sizeof(HEADER); /* current position in buf */ |
| 424 |
unsigned int type = 0; /* answer type */ |
| 425 |
unsigned int rd_length = 0; |
| 426 |
int n; /* temp count */ |
| 427 |
struct sockaddr_in *v4; /* conversion */ |
| 428 |
struct sockaddr_in6 *v6; |
| 429 |
|
| 430 |
for (; header->qdcount > 0; --header->qdcount) |
| 431 |
{ |
| 432 |
if ((n = irc_dn_skipname(current, eob)) < 0) |
| 433 |
break; |
| 434 |
|
| 435 |
current += (size_t)n + QFIXEDSZ; |
| 436 |
} |
| 437 |
|
| 438 |
/* |
| 439 |
* Process each answer sent to us blech. |
| 440 |
*/ |
| 441 |
while (header->ancount > 0 && current < eob) |
| 442 |
{ |
| 443 |
--header->ancount; |
| 444 |
|
| 445 |
n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
| 446 |
|
| 447 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 448 |
return 0; |
| 449 |
|
| 450 |
hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0'; |
| 451 |
|
| 452 |
/* |
| 453 |
* With Address arithmetic you have to be very anal |
| 454 |
* this code was not working on alpha due to that |
| 455 |
* (spotted by rodder/jailbird/dianora) |
| 456 |
*/ |
| 457 |
current += (size_t)n; |
| 458 |
|
| 459 |
if (!((current + ANSWER_FIXED_SIZE) < eob)) |
| 460 |
break; |
| 461 |
|
| 462 |
type = irc_ns_get16(current); |
| 463 |
current += TYPE_SIZE; |
| 464 |
current += CLASS_SIZE; |
| 465 |
current += TTL_SIZE; |
| 466 |
rd_length = irc_ns_get16(current); |
| 467 |
current += RDLENGTH_SIZE; |
| 468 |
|
| 469 |
/* |
| 470 |
* Wait to set request->type until we verify this structure |
| 471 |
*/ |
| 472 |
switch (type) |
| 473 |
{ |
| 474 |
case T_A: |
| 475 |
if (request->type != T_A) |
| 476 |
return 0; |
| 477 |
|
| 478 |
/* |
| 479 |
* Check for invalid rd_length or too many addresses |
| 480 |
*/ |
| 481 |
if (rd_length != sizeof(struct in_addr)) |
| 482 |
return 0; |
| 483 |
|
| 484 |
request->addr.ss_len = sizeof(struct sockaddr_in); |
| 485 |
v4 = (struct sockaddr_in *)&request->addr; |
| 486 |
v4->sin_family = AF_INET; |
| 487 |
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
| 488 |
return 1; |
| 489 |
break; |
| 490 |
case T_AAAA: |
| 491 |
if (request->type != T_AAAA) |
| 492 |
return 0; |
| 493 |
|
| 494 |
if (rd_length != sizeof(struct in6_addr)) |
| 495 |
return 0; |
| 496 |
|
| 497 |
request->addr.ss_len = sizeof(struct sockaddr_in6); |
| 498 |
v6 = (struct sockaddr_in6 *)&request->addr; |
| 499 |
v6->sin6_family = AF_INET6; |
| 500 |
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
| 501 |
return 1; |
| 502 |
break; |
| 503 |
case T_PTR: |
| 504 |
if (request->type != T_PTR) |
| 505 |
return 0; |
| 506 |
|
| 507 |
n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
| 508 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 509 |
return 0; |
| 510 |
|
| 511 |
request->namelength = strlcpy(request->name, hostbuf, sizeof(request->name)); |
| 512 |
return 1; |
| 513 |
break; |
| 514 |
case T_CNAME: |
| 515 |
current += rd_length; |
| 516 |
break; |
| 517 |
default: |
| 518 |
return 0; |
| 519 |
break; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
/* |
| 527 |
* res_readreply - read a dns reply from the nameserver and process it. |
| 528 |
*/ |
| 529 |
static void |
| 530 |
res_readreply(fde_t *fd, void *data) |
| 531 |
{ |
| 532 |
unsigned char buf[sizeof(HEADER) + MAXPACKET]; |
| 533 |
struct reslist *request = NULL; |
| 534 |
ssize_t rc = 0; |
| 535 |
socklen_t len = sizeof(struct irc_ssaddr); |
| 536 |
struct irc_ssaddr lsin; |
| 537 |
|
| 538 |
while ((rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1) |
| 539 |
{ |
| 540 |
if (rc <= (ssize_t)sizeof(HEADER)) |
| 541 |
continue; |
| 542 |
|
| 543 |
/* |
| 544 |
* Check against possibly fake replies |
| 545 |
*/ |
| 546 |
if (!res_ourserver(&lsin)) |
| 547 |
continue; |
| 548 |
|
| 549 |
/* |
| 550 |
* Convert DNS reply reader from Network byte order to CPU byte order. |
| 551 |
*/ |
| 552 |
HEADER *header = (HEADER *)buf; |
| 553 |
header->ancount = ntohs(header->ancount); |
| 554 |
header->qdcount = ntohs(header->qdcount); |
| 555 |
header->nscount = ntohs(header->nscount); |
| 556 |
header->arcount = ntohs(header->arcount); |
| 557 |
|
| 558 |
/* |
| 559 |
* Response for an id which we have already received an answer for |
| 560 |
* just ignore this response. |
| 561 |
*/ |
| 562 |
if ((request = find_id(header->id)) == NULL) |
| 563 |
continue; |
| 564 |
|
| 565 |
if (header->rcode != NO_ERRORS || header->ancount == 0) |
| 566 |
{ |
| 567 |
/* |
| 568 |
* If a bad error was returned, stop here and don't send |
| 569 |
* any more (no retries granted). |
| 570 |
*/ |
| 571 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 572 |
rem_request(request); |
| 573 |
continue; |
| 574 |
} |
| 575 |
|
| 576 |
/* |
| 577 |
* If this fails there was an error decoding the received packet. |
| 578 |
* We only give it one shot. If it fails, just leave the client |
| 579 |
* unresolved. |
| 580 |
*/ |
| 581 |
if (!proc_answer(request, header, buf, buf + rc)) |
| 582 |
{ |
| 583 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 584 |
rem_request(request); |
| 585 |
continue; |
| 586 |
} |
| 587 |
|
| 588 |
if (request->type == T_PTR) |
| 589 |
{ |
| 590 |
if (request->namelength == 0) |
| 591 |
{ |
| 592 |
/* |
| 593 |
* Got a PTR response with no name, something bogus is happening |
| 594 |
* don't bother trying again, the client address doesn't resolve |
| 595 |
*/ |
| 596 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 597 |
rem_request(request); |
| 598 |
continue; |
| 599 |
} |
| 600 |
|
| 601 |
/* |
| 602 |
* Lookup the 'authoritative' name that we were given for the ip#. |
| 603 |
*/ |
| 604 |
if (request->addr.ss.ss_family == AF_INET6) |
| 605 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
| 606 |
else |
| 607 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
| 608 |
|
| 609 |
rem_request(request); |
| 610 |
} |
| 611 |
else |
| 612 |
{ |
| 613 |
/* |
| 614 |
* Got a name and address response, client resolved |
| 615 |
*/ |
| 616 |
(*request->callback)(request->callback_ctx, &request->addr, request->name, request->namelength); |
| 617 |
rem_request(request); |
| 618 |
} |
| 619 |
|
| 620 |
continue; |
| 621 |
} |
| 622 |
|
| 623 |
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
| 624 |
} |
| 625 |
|
| 626 |
/* |
| 627 |
* timeout_query_list - Remove queries from the list which have been |
| 628 |
* there too long without being resolved. |
| 629 |
*/ |
| 630 |
static uintmax_t |
| 631 |
timeout_query_list(void) |
| 632 |
{ |
| 633 |
dlink_node *node = NULL, *node_next = NULL; |
| 634 |
struct reslist *request = NULL; |
| 635 |
uintmax_t next_time = 0; |
| 636 |
uintmax_t timeout = 0; |
| 637 |
|
| 638 |
DLINK_FOREACH_SAFE(node, node_next, request_list.head) |
| 639 |
{ |
| 640 |
request = node->data; |
| 641 |
timeout = request->sentat + request->timeout; |
| 642 |
|
| 643 |
if (CurrentTime >= timeout) |
| 644 |
{ |
| 645 |
if (--request->retries <= 0) |
| 646 |
{ |
| 647 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 648 |
rem_request(request); |
| 649 |
continue; |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
request->sentat = CurrentTime; |
| 654 |
request->timeout += request->timeout; |
| 655 |
resend_query(request); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
if (next_time == 0 || timeout < next_time) |
| 660 |
next_time = timeout; |
| 661 |
} |
| 662 |
|
| 663 |
return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL); |
| 664 |
} |
| 665 |
|
| 666 |
/* |
| 667 |
* timeout_resolver - check request list |
| 668 |
*/ |
| 669 |
static void |
| 670 |
timeout_resolver(void *unused) |
| 671 |
{ |
| 672 |
timeout_query_list(); |
| 673 |
} |
| 674 |
|
| 675 |
/* |
| 676 |
* resolver_init - initialize resolver and resolver library |
| 677 |
*/ |
| 678 |
void |
| 679 |
resolver_init(void) |
| 680 |
{ |
| 681 |
static struct event event_timeout_resolver = |
| 682 |
{ |
| 683 |
.name = "timeout_resolver", |
| 684 |
.handler = timeout_resolver, |
| 685 |
.when = 1 |
| 686 |
}; |
| 687 |
|
| 688 |
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
| 689 |
|
| 690 |
start_resolver(); |
| 691 |
event_add(&event_timeout_resolver, NULL); |
| 692 |
} |