| 25 |
|
*/ |
| 26 |
|
|
| 27 |
|
/* |
| 28 |
< |
* A rewrite of Darren Reeds original res.c As there is nothing |
| 29 |
< |
* left of Darrens original code, this is now licensed by the hybrid group. |
| 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 |
– |
* $Id$ |
| 37 |
– |
* |
| 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. Thats all. |
| 41 |
> |
* All we really care about is the IP -> hostname mappings. That's all. |
| 42 |
|
* |
| 43 |
|
* Apr 28, 2003 --cryogen and Dianora |
| 44 |
|
*/ |
| 67 |
|
|
| 68 |
|
static PF res_readreply; |
| 69 |
|
|
| 70 |
< |
#define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */ |
| 71 |
< |
#define AR_TTL 600 /* TTL in seconds for dns cache entries */ |
| 70 |
> |
#define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */ |
| 71 |
> |
#define AR_TTL 600 /**< TTL in seconds for dns cache entries */ |
| 72 |
|
|
| 73 |
< |
/* RFC 1104/1105 wasn't very helpful about what these fields |
| 73 |
> |
/* |
| 74 |
> |
* RFC 1104/1105 wasn't very helpful about what these fields |
| 75 |
|
* should be named, so for now, we'll just name them this way. |
| 76 |
< |
* we probably should look at what named calls them or something. |
| 76 |
> |
* We probably should look at what named calls them or something. |
| 77 |
|
*/ |
| 78 |
|
#define TYPE_SIZE (size_t)2 |
| 79 |
|
#define CLASS_SIZE (size_t)2 |
| 83 |
|
|
| 84 |
|
typedef enum |
| 85 |
|
{ |
| 86 |
< |
REQ_IDLE, /* We're doing not much at all */ |
| 87 |
< |
REQ_PTR, /* Looking up a PTR */ |
| 88 |
< |
REQ_A, /* Looking up an A, possibly because AAAA failed */ |
| 86 |
> |
REQ_IDLE, /**< We're doing not much at all */ |
| 87 |
> |
REQ_PTR, /**< Looking up a PTR */ |
| 88 |
> |
REQ_A, /**< Looking up an A, possibly because AAAA failed */ |
| 89 |
|
#ifdef IPV6 |
| 90 |
< |
REQ_AAAA, /* Looking up an AAAA */ |
| 90 |
> |
REQ_AAAA, /**< Looking up an AAAA */ |
| 91 |
|
#endif |
| 92 |
< |
REQ_CNAME /* We got a CNAME in response, we better get a real answer next */ |
| 92 |
> |
REQ_CNAME /**< We got a CNAME in response, we better get a real answer next */ |
| 93 |
|
} request_state; |
| 94 |
|
|
| 95 |
|
struct reslist |
| 96 |
|
{ |
| 97 |
< |
dlink_node node; |
| 98 |
< |
int id; |
| 99 |
< |
int sent; /* number of requests sent */ |
| 100 |
< |
request_state state; /* State the resolver machine is in */ |
| 101 |
< |
time_t ttl; |
| 102 |
< |
char type; |
| 103 |
< |
char retries; /* retry counter */ |
| 104 |
< |
unsigned int sends; /* number of sends (>1 means resent) */ |
| 105 |
< |
char resend; /* send flag. 0 == dont resend */ |
| 106 |
< |
time_t sentat; |
| 107 |
< |
time_t timeout; |
| 108 |
< |
struct irc_ssaddr addr; |
| 109 |
< |
char *name; |
| 110 |
< |
dns_callback_fnc callback; |
| 112 |
< |
void *callback_ctx; |
| 97 |
> |
dlink_node node; /**< Doubly linked list node. */ |
| 98 |
> |
int id; /**< Request ID (from request header). */ |
| 99 |
> |
int sent; /**< Number of requests sent */ |
| 100 |
> |
request_state state; /**< State the resolver machine is in */ |
| 101 |
> |
char type; /**< Current request type. */ |
| 102 |
> |
char retries; /**< Retry counter */ |
| 103 |
> |
unsigned int sends; /**< Number of sends (>1 means resent). */ |
| 104 |
> |
char resend; /**< Send flag; 0 == don't resend. */ |
| 105 |
> |
time_t sentat; /**< Timestamp we last sent this request. */ |
| 106 |
> |
time_t timeout; /**< When this request times out. */ |
| 107 |
> |
struct irc_ssaddr addr; /**< Address for this request. */ |
| 108 |
> |
char *name; /**< Hostname for this request. */ |
| 109 |
> |
dns_callback_fnc callback; /**< Callback function on completion. */ |
| 110 |
> |
void *callback_ctx; /**< Context pointer for callback. */ |
| 111 |
|
}; |
| 112 |
|
|
| 113 |
|
static fde_t ResolverFileDescriptor; |
| 114 |
|
static dlink_list request_list; |
| 115 |
|
static mp_pool_t *dns_pool; |
| 116 |
|
|
| 119 |
– |
static void rem_request(struct reslist *); |
| 120 |
– |
static struct reslist *make_request(dns_callback_fnc, void *); |
| 121 |
– |
static void do_query_name(dns_callback_fnc, void *, |
| 122 |
– |
const char *, struct reslist *, int); |
| 123 |
– |
static void do_query_number(dns_callback_fnc, void *, |
| 124 |
– |
const struct irc_ssaddr *, |
| 125 |
– |
struct reslist *); |
| 126 |
– |
static void query_name(const char *, int, int, struct reslist *); |
| 127 |
– |
static int send_res_msg(const char *, int, unsigned int); |
| 128 |
– |
static void resend_query(struct reslist *); |
| 129 |
– |
static int proc_answer(struct reslist *, HEADER *, char *, char *); |
| 130 |
– |
static struct reslist *find_id(int); |
| 117 |
|
|
| 118 |
+ |
/* |
| 119 |
+ |
* rem_request - remove a request from the list. |
| 120 |
+ |
* This must also free any memory that has been allocated for |
| 121 |
+ |
* temporary storage of DNS results. |
| 122 |
+ |
*/ |
| 123 |
+ |
static void |
| 124 |
+ |
rem_request(struct reslist *request) |
| 125 |
+ |
{ |
| 126 |
+ |
dlinkDelete(&request->node, &request_list); |
| 127 |
+ |
|
| 128 |
+ |
MyFree(request->name); |
| 129 |
+ |
mp_pool_release(request); |
| 130 |
+ |
} |
| 131 |
+ |
|
| 132 |
+ |
/* |
| 133 |
+ |
* make_request - Create a DNS request record for the server. |
| 134 |
+ |
*/ |
| 135 |
+ |
static struct reslist * |
| 136 |
+ |
make_request(dns_callback_fnc callback, void *ctx) |
| 137 |
+ |
{ |
| 138 |
+ |
struct reslist *request = mp_pool_get(dns_pool); |
| 139 |
+ |
|
| 140 |
+ |
request->sentat = CurrentTime; |
| 141 |
+ |
request->retries = 2; |
| 142 |
+ |
request->resend = 1; |
| 143 |
+ |
request->timeout = 4; /* Start at 4 and exponential inc. */ |
| 144 |
+ |
request->state = REQ_IDLE; |
| 145 |
+ |
request->callback = callback; |
| 146 |
+ |
request->callback_ctx = ctx; |
| 147 |
+ |
|
| 148 |
+ |
dlinkAdd(request, &request->node, &request_list); |
| 149 |
+ |
return request; |
| 150 |
+ |
} |
| 151 |
|
|
| 152 |
|
/* |
| 153 |
|
* int |
| 178 |
|
#endif |
| 179 |
|
v4 = (const struct sockaddr_in *)srv; |
| 180 |
|
|
| 181 |
< |
/* could probably just memcmp(srv, inp, srv.ss_len) here |
| 181 |
> |
/* |
| 182 |
> |
* Could probably just memcmp(srv, inp, srv.ss_len) here |
| 183 |
|
* but we'll air on the side of caution - stu |
| 164 |
– |
* |
| 184 |
|
*/ |
| 185 |
|
switch (srv->ss.ss_family) |
| 186 |
|
{ |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
/* |
| 192 |
– |
* timeout_query_list - Remove queries from the list which have been |
| 193 |
– |
* there too long without being resolved. |
| 194 |
– |
*/ |
| 195 |
– |
static time_t |
| 196 |
– |
timeout_query_list(void) |
| 197 |
– |
{ |
| 198 |
– |
dlink_node *ptr = NULL, *ptr_next = NULL; |
| 199 |
– |
struct reslist *request = NULL; |
| 200 |
– |
time_t next_time = 0; |
| 201 |
– |
time_t timeout = 0; |
| 202 |
– |
|
| 203 |
– |
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
| 204 |
– |
{ |
| 205 |
– |
request = ptr->data; |
| 206 |
– |
timeout = request->sentat + request->timeout; |
| 207 |
– |
|
| 208 |
– |
if (CurrentTime >= timeout) |
| 209 |
– |
{ |
| 210 |
– |
if (--request->retries <= 0) |
| 211 |
– |
{ |
| 212 |
– |
(*request->callback)(request->callback_ctx, NULL, NULL); |
| 213 |
– |
rem_request(request); |
| 214 |
– |
continue; |
| 215 |
– |
} |
| 216 |
– |
else |
| 217 |
– |
{ |
| 218 |
– |
request->sentat = CurrentTime; |
| 219 |
– |
request->timeout += request->timeout; |
| 220 |
– |
resend_query(request); |
| 221 |
– |
} |
| 222 |
– |
} |
| 223 |
– |
|
| 224 |
– |
if ((next_time == 0) || timeout < next_time) |
| 225 |
– |
next_time = timeout; |
| 226 |
– |
} |
| 227 |
– |
|
| 228 |
– |
return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL); |
| 229 |
– |
} |
| 230 |
– |
|
| 231 |
– |
/* |
| 232 |
– |
* timeout_resolver - check request list |
| 233 |
– |
*/ |
| 234 |
– |
static void |
| 235 |
– |
timeout_resolver(void *notused) |
| 236 |
– |
{ |
| 237 |
– |
timeout_query_list(); |
| 238 |
– |
} |
| 239 |
– |
|
| 240 |
– |
/* |
| 211 |
|
* start_resolver - do everything we need to read the resolv.conf file |
| 212 |
|
* and initialize the resolver file descriptor if needed |
| 213 |
|
*/ |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
/* |
| 261 |
– |
* init_resolver - initialize resolver and resolver library |
| 262 |
– |
*/ |
| 263 |
– |
void |
| 264 |
– |
init_resolver(void) |
| 265 |
– |
{ |
| 266 |
– |
static struct event event_timeout_resolver = |
| 267 |
– |
{ |
| 268 |
– |
.name = "timeout_resolver", |
| 269 |
– |
.handler = timeout_resolver, |
| 270 |
– |
.when = 1 |
| 271 |
– |
}; |
| 272 |
– |
|
| 273 |
– |
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
| 274 |
– |
memset(&ResolverFileDescriptor, 0, sizeof(fde_t)); |
| 275 |
– |
start_resolver(); |
| 276 |
– |
|
| 277 |
– |
event_add(&event_timeout_resolver, NULL); |
| 278 |
– |
} |
| 279 |
– |
|
| 280 |
– |
/* |
| 231 |
|
* restart_resolver - reread resolv.conf, reopen socket |
| 232 |
|
*/ |
| 233 |
|
void |
| 238 |
|
} |
| 239 |
|
|
| 240 |
|
/* |
| 291 |
– |
* rem_request - remove a request from the list. |
| 292 |
– |
* This must also free any memory that has been allocated for |
| 293 |
– |
* temporary storage of DNS results. |
| 294 |
– |
*/ |
| 295 |
– |
static void |
| 296 |
– |
rem_request(struct reslist *request) |
| 297 |
– |
{ |
| 298 |
– |
dlinkDelete(&request->node, &request_list); |
| 299 |
– |
|
| 300 |
– |
MyFree(request->name); |
| 301 |
– |
mp_pool_release(request); |
| 302 |
– |
} |
| 303 |
– |
|
| 304 |
– |
/* |
| 305 |
– |
* make_request - Create a DNS request record for the server. |
| 306 |
– |
*/ |
| 307 |
– |
static struct reslist * |
| 308 |
– |
make_request(dns_callback_fnc callback, void *ctx) |
| 309 |
– |
{ |
| 310 |
– |
struct reslist *request = mp_pool_get(dns_pool); |
| 311 |
– |
|
| 312 |
– |
request->sentat = CurrentTime; |
| 313 |
– |
request->retries = 2; |
| 314 |
– |
request->resend = 1; |
| 315 |
– |
request->timeout = 4; /* Start at 4 and exponential inc. */ |
| 316 |
– |
request->state = REQ_IDLE; |
| 317 |
– |
request->callback = callback; |
| 318 |
– |
request->callback_ctx = ctx; |
| 319 |
– |
|
| 320 |
– |
dlinkAdd(request, &request->node, &request_list); |
| 321 |
– |
return request; |
| 322 |
– |
} |
| 323 |
– |
|
| 324 |
– |
/* |
| 241 |
|
* delete_resolver_queries - cleanup outstanding queries |
| 242 |
|
* for which there no longer exist clients or conf lines. |
| 243 |
|
*/ |
| 259 |
|
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
| 260 |
|
* This should reflect /etc/resolv.conf. We will get responses |
| 261 |
|
* which arent needed but is easier than checking to see if nameserver |
| 262 |
< |
* isnt present. Returns number of messages successfully sent to |
| 262 |
> |
* isn't present. Returns number of messages successfully sent to |
| 263 |
|
* nameservers or -1 if no successful sends. |
| 264 |
|
*/ |
| 265 |
|
static int |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
/* |
| 308 |
< |
* gethost_byname_type - get host address from name |
| 393 |
< |
* |
| 308 |
> |
* query_name - generate a query based on class, type and name. |
| 309 |
|
*/ |
| 310 |
< |
void |
| 311 |
< |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
| 310 |
> |
static void |
| 311 |
> |
query_name(const char *name, int query_class, int type, |
| 312 |
> |
struct reslist *request) |
| 313 |
|
{ |
| 314 |
< |
assert(name); |
| 315 |
< |
do_query_name(callback, ctx, name, NULL, type); |
| 400 |
< |
} |
| 314 |
> |
char buf[MAXPACKET]; |
| 315 |
> |
int request_len = 0; |
| 316 |
|
|
| 317 |
< |
/* |
| 403 |
< |
* gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported |
| 404 |
< |
*/ |
| 405 |
< |
void |
| 406 |
< |
gethost_byname(dns_callback_fnc callback, void *ctx, const char *name) |
| 407 |
< |
{ |
| 408 |
< |
#ifdef IPV6 |
| 409 |
< |
gethost_byname_type(callback, ctx, name, T_AAAA); |
| 410 |
< |
#else |
| 411 |
< |
gethost_byname_type(callback, ctx, name, T_A); |
| 412 |
< |
#endif |
| 413 |
< |
} |
| 317 |
> |
memset(buf, 0, sizeof(buf)); |
| 318 |
|
|
| 319 |
< |
/* |
| 320 |
< |
* gethost_byaddr - get host name from address |
| 321 |
< |
*/ |
| 322 |
< |
void |
| 323 |
< |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
| 324 |
< |
{ |
| 325 |
< |
do_query_number(callback, ctx, addr, NULL); |
| 319 |
> |
if ((request_len = irc_res_mkquery(name, query_class, type, |
| 320 |
> |
(unsigned char *)buf, sizeof(buf))) > 0) |
| 321 |
> |
{ |
| 322 |
> |
HEADER *header = (HEADER *)buf; |
| 323 |
> |
|
| 324 |
> |
/* |
| 325 |
> |
* Generate an unique id. |
| 326 |
> |
* NOTE: we don't have to worry about converting this to and from |
| 327 |
> |
* network byte order, the nameserver does not interpret this value |
| 328 |
> |
* and returns it unchanged. |
| 329 |
> |
*/ |
| 330 |
> |
do |
| 331 |
> |
header->id = (header->id + genrand_int32()) & 0xFFFF; |
| 332 |
> |
while (find_id(header->id)); |
| 333 |
> |
|
| 334 |
> |
request->id = header->id; |
| 335 |
> |
++request->sends; |
| 336 |
> |
|
| 337 |
> |
request->sent += send_res_msg(buf, request_len, request->sends); |
| 338 |
> |
} |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
/* |
| 425 |
|
} |
| 426 |
|
|
| 427 |
|
/* |
| 428 |
< |
* query_name - generate a query based on class, type and name. |
| 428 |
> |
* gethost_byname_type - get host address from name |
| 429 |
> |
* |
| 430 |
|
*/ |
| 431 |
< |
static void |
| 432 |
< |
query_name(const char *name, int query_class, int type, |
| 515 |
< |
struct reslist *request) |
| 431 |
> |
void |
| 432 |
> |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
| 433 |
|
{ |
| 434 |
< |
char buf[MAXPACKET]; |
| 435 |
< |
int request_len = 0; |
| 436 |
< |
|
| 520 |
< |
memset(buf, 0, sizeof(buf)); |
| 521 |
< |
|
| 522 |
< |
if ((request_len = irc_res_mkquery(name, query_class, type, |
| 523 |
< |
(unsigned char *)buf, sizeof(buf))) > 0) |
| 524 |
< |
{ |
| 525 |
< |
HEADER *header = (HEADER *)buf; |
| 526 |
< |
|
| 527 |
< |
/* |
| 528 |
< |
* generate an unique id |
| 529 |
< |
* NOTE: we don't have to worry about converting this to and from |
| 530 |
< |
* network byte order, the nameserver does not interpret this value |
| 531 |
< |
* and returns it unchanged |
| 532 |
< |
*/ |
| 533 |
< |
do |
| 534 |
< |
header->id = (header->id + genrand_int32()) & 0xffff; |
| 535 |
< |
while (find_id(header->id)); |
| 434 |
> |
assert(name); |
| 435 |
> |
do_query_name(callback, ctx, name, NULL, type); |
| 436 |
> |
} |
| 437 |
|
|
| 438 |
< |
request->id = header->id; |
| 439 |
< |
++request->sends; |
| 438 |
> |
/* |
| 439 |
> |
* gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported |
| 440 |
> |
*/ |
| 441 |
> |
void |
| 442 |
> |
gethost_byname(dns_callback_fnc callback, void *ctx, const char *name) |
| 443 |
> |
{ |
| 444 |
> |
#ifdef IPV6 |
| 445 |
> |
gethost_byname_type(callback, ctx, name, T_AAAA); |
| 446 |
> |
#else |
| 447 |
> |
gethost_byname_type(callback, ctx, name, T_A); |
| 448 |
> |
#endif |
| 449 |
> |
} |
| 450 |
|
|
| 451 |
< |
request->sent += send_res_msg(buf, request_len, request->sends); |
| 452 |
< |
} |
| 451 |
> |
/* |
| 452 |
> |
* gethost_byaddr - get host name from address |
| 453 |
> |
*/ |
| 454 |
> |
void |
| 455 |
> |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
| 456 |
> |
{ |
| 457 |
> |
do_query_number(callback, ctx, addr, NULL); |
| 458 |
|
} |
| 459 |
|
|
| 460 |
|
static void |
| 472 |
|
do_query_name(NULL, NULL, request->name, request, request->type); |
| 473 |
|
break; |
| 474 |
|
#ifdef IPV6 |
| 475 |
< |
case T_AAAA: |
| 560 |
< |
/* didnt work, try A */ |
| 475 |
> |
case T_AAAA: /* Didn't work, try A */ |
| 476 |
|
if (request->state == REQ_AAAA) |
| 477 |
|
do_query_name(NULL, NULL, request->name, request, T_A); |
| 478 |
|
#endif |
| 489 |
|
{ |
| 490 |
|
char hostbuf[HOSTLEN + 100]; /* working buffer */ |
| 491 |
|
unsigned char *current; /* current position in buf */ |
| 577 |
– |
int query_class; /* answer class */ |
| 492 |
|
int type; /* answer type */ |
| 493 |
|
int n; /* temp count */ |
| 494 |
|
int rd_length; |
| 507 |
|
} |
| 508 |
|
|
| 509 |
|
/* |
| 510 |
< |
* process each answer sent to us blech. |
| 510 |
> |
* Process each answer sent to us blech. |
| 511 |
|
*/ |
| 512 |
|
while (header->ancount > 0 && (char *)current < eob) |
| 513 |
|
{ |
| 516 |
|
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, |
| 517 |
|
hostbuf, sizeof(hostbuf)); |
| 518 |
|
|
| 519 |
< |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 519 |
> |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 520 |
|
return 0; |
| 521 |
|
|
| 522 |
|
hostbuf[HOSTLEN] = '\0'; |
| 523 |
|
|
| 524 |
< |
/* With Address arithmetic you have to be very anal |
| 524 |
> |
/* |
| 525 |
> |
* With Address arithmetic you have to be very anal |
| 526 |
|
* this code was not working on alpha due to that |
| 527 |
|
* (spotted by rodder/jailbird/dianora) |
| 528 |
|
*/ |
| 533 |
|
|
| 534 |
|
type = irc_ns_get16(current); |
| 535 |
|
current += TYPE_SIZE; |
| 621 |
– |
|
| 622 |
– |
query_class = irc_ns_get16(current); |
| 536 |
|
current += CLASS_SIZE; |
| 624 |
– |
|
| 625 |
– |
request->ttl = irc_ns_get32(current); |
| 537 |
|
current += TTL_SIZE; |
| 627 |
– |
|
| 538 |
|
rd_length = irc_ns_get16(current); |
| 539 |
|
current += RDLENGTH_SIZE; |
| 540 |
|
|
| 580 |
|
|
| 581 |
|
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, |
| 582 |
|
current, hostbuf, sizeof(hostbuf)); |
| 583 |
< |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 583 |
> |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 584 |
|
return 0; |
| 585 |
|
|
| 586 |
|
strlcpy(request->name, hostbuf, HOSTLEN + 1); |
| 587 |
|
return 1; |
| 588 |
|
break; |
| 589 |
< |
case T_CNAME: /* first check we already havent started looking |
| 680 |
< |
into a cname */ |
| 589 |
> |
case T_CNAME: /* First check we already haven't started looking into a cname */ |
| 590 |
|
if (request->type != T_PTR) |
| 591 |
|
return 0; |
| 592 |
|
|
| 623 |
|
static void |
| 624 |
|
res_readreply(fde_t *fd, void *data) |
| 625 |
|
{ |
| 626 |
< |
char buf[sizeof(HEADER) + MAXPACKET] |
| 718 |
< |
/* Sparc and alpha need 16bit-alignment for accessing header->id |
| 719 |
< |
* (which is uint16_t). Because of the header = (HEADER*) buf; |
| 720 |
< |
* lateron, this is neeeded. --FaUl |
| 721 |
< |
*/ |
| 722 |
< |
#if defined(__sparc__) || defined(__alpha__) |
| 723 |
< |
__attribute__((aligned (16))) |
| 724 |
< |
#endif |
| 725 |
< |
; |
| 626 |
> |
char buf[sizeof(HEADER) + MAXPACKET]; |
| 627 |
|
HEADER *header; |
| 628 |
|
struct reslist *request = NULL; |
| 629 |
< |
int rc; |
| 629 |
> |
ssize_t rc = 0; |
| 630 |
|
socklen_t len = sizeof(struct irc_ssaddr); |
| 631 |
|
struct irc_ssaddr lsin; |
| 632 |
|
|
| 633 |
|
rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); |
| 634 |
|
|
| 635 |
< |
/* Re-schedule a read *after* recvfrom, or we'll be registering |
| 635 |
> |
/* |
| 636 |
> |
* Re-schedule a read *after* recvfrom, or we'll be registering |
| 637 |
|
* interest where it'll instantly be ready for read :-) -- adrian |
| 638 |
|
*/ |
| 639 |
|
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
| 640 |
|
|
| 641 |
< |
/* Better to cast the sizeof instead of rc */ |
| 642 |
< |
if (rc <= (int)(sizeof(HEADER))) |
| 641 |
> |
if (rc <= (ssize_t)sizeof(HEADER)) |
| 642 |
> |
return; |
| 643 |
> |
|
| 644 |
> |
/* |
| 645 |
> |
* Check against possibly fake replies |
| 646 |
> |
*/ |
| 647 |
> |
if (!res_ourserver(&lsin)) |
| 648 |
|
return; |
| 649 |
|
|
| 650 |
|
/* |
| 657 |
|
header->arcount = ntohs(header->arcount); |
| 658 |
|
|
| 659 |
|
/* |
| 753 |
– |
* Check against possibly fake replies |
| 754 |
– |
*/ |
| 755 |
– |
if (!res_ourserver(&lsin)) |
| 756 |
– |
return; |
| 757 |
– |
|
| 758 |
– |
/* |
| 660 |
|
* Response for an id which we have already received an answer for |
| 661 |
|
* just ignore this response. |
| 662 |
|
*/ |
| 663 |
< |
if (!(request = find_id(header->id))) |
| 663 |
> |
if ((request = find_id(header->id)) == NULL) |
| 664 |
|
return; |
| 665 |
|
|
| 666 |
|
if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) |
| 678 |
|
else |
| 679 |
|
{ |
| 680 |
|
/* |
| 681 |
< |
* If we havent already tried this, and we're looking up AAAA, try A |
| 781 |
< |
* now |
| 681 |
> |
* If we havent already tried this, and we're looking up AAAA, try A now. |
| 682 |
|
*/ |
| 683 |
|
if (request->state == REQ_AAAA && request->type == T_AAAA) |
| 684 |
|
{ |
| 756 |
|
sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr); |
| 757 |
|
} |
| 758 |
|
} |
| 759 |
+ |
|
| 760 |
+ |
/* |
| 761 |
+ |
* timeout_query_list - Remove queries from the list which have been |
| 762 |
+ |
* there too long without being resolved. |
| 763 |
+ |
*/ |
| 764 |
+ |
static time_t |
| 765 |
+ |
timeout_query_list(void) |
| 766 |
+ |
{ |
| 767 |
+ |
dlink_node *ptr = NULL, *ptr_next = NULL; |
| 768 |
+ |
struct reslist *request = NULL; |
| 769 |
+ |
time_t next_time = 0; |
| 770 |
+ |
time_t timeout = 0; |
| 771 |
+ |
|
| 772 |
+ |
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
| 773 |
+ |
{ |
| 774 |
+ |
request = ptr->data; |
| 775 |
+ |
timeout = request->sentat + request->timeout; |
| 776 |
+ |
|
| 777 |
+ |
if (CurrentTime >= timeout) |
| 778 |
+ |
{ |
| 779 |
+ |
if (--request->retries <= 0) |
| 780 |
+ |
{ |
| 781 |
+ |
(*request->callback)(request->callback_ctx, NULL, NULL); |
| 782 |
+ |
rem_request(request); |
| 783 |
+ |
continue; |
| 784 |
+ |
} |
| 785 |
+ |
else |
| 786 |
+ |
{ |
| 787 |
+ |
request->sentat = CurrentTime; |
| 788 |
+ |
request->timeout += request->timeout; |
| 789 |
+ |
resend_query(request); |
| 790 |
+ |
} |
| 791 |
+ |
} |
| 792 |
+ |
|
| 793 |
+ |
if (next_time == 0 || timeout < next_time) |
| 794 |
+ |
next_time = timeout; |
| 795 |
+ |
} |
| 796 |
+ |
|
| 797 |
+ |
return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL); |
| 798 |
+ |
} |
| 799 |
+ |
|
| 800 |
+ |
/* |
| 801 |
+ |
* timeout_resolver - check request list |
| 802 |
+ |
*/ |
| 803 |
+ |
static void |
| 804 |
+ |
timeout_resolver(void *notused) |
| 805 |
+ |
{ |
| 806 |
+ |
timeout_query_list(); |
| 807 |
+ |
} |
| 808 |
+ |
|
| 809 |
+ |
/* |
| 810 |
+ |
* init_resolver - initialize resolver and resolver library |
| 811 |
+ |
*/ |
| 812 |
+ |
void |
| 813 |
+ |
init_resolver(void) |
| 814 |
+ |
{ |
| 815 |
+ |
static struct event event_timeout_resolver = |
| 816 |
+ |
{ |
| 817 |
+ |
.name = "timeout_resolver", |
| 818 |
+ |
.handler = timeout_resolver, |
| 819 |
+ |
.when = 1 |
| 820 |
+ |
}; |
| 821 |
+ |
|
| 822 |
+ |
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
| 823 |
+ |
memset(&ResolverFileDescriptor, 0, sizeof(fde_t)); |
| 824 |
+ |
start_resolver(); |
| 825 |
+ |
|
| 826 |
+ |
event_add(&event_timeout_resolver, NULL); |
| 827 |
+ |
} |