| 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 */ |
| 89 |
< |
#ifdef IPV6 |
| 90 |
< |
REQ_AAAA, /* Looking up an AAAA */ |
| 92 |
< |
#endif |
| 93 |
< |
REQ_CNAME /* We got a CNAME in response, we better get a real answer next */ |
| 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 |
> |
REQ_AAAA, /**< Looking up an AAAA */ |
| 90 |
> |
REQ_CNAME /**< We got a CNAME in response, we better get a real answer next */ |
| 91 |
|
} request_state; |
| 92 |
|
|
| 93 |
|
struct reslist |
| 94 |
|
{ |
| 95 |
< |
dlink_node node; |
| 96 |
< |
int id; |
| 97 |
< |
int sent; /* number of requests sent */ |
| 98 |
< |
request_state state; /* State the resolver machine is in */ |
| 99 |
< |
time_t ttl; |
| 100 |
< |
char type; |
| 101 |
< |
char retries; /* retry counter */ |
| 102 |
< |
unsigned int sends; /* number of sends (>1 means resent) */ |
| 103 |
< |
char resend; /* send flag. 0 == don't resend */ |
| 104 |
< |
time_t sentat; |
| 105 |
< |
time_t timeout; |
| 106 |
< |
struct irc_ssaddr addr; |
| 107 |
< |
char *name; |
| 108 |
< |
dns_callback_fnc callback; |
| 109 |
< |
void *callback_ctx; |
| 95 |
> |
dlink_node node; /**< Doubly linked list node. */ |
| 96 |
> |
int id; /**< Request ID (from request header). */ |
| 97 |
> |
int sent; /**< Number of requests sent */ |
| 98 |
> |
request_state state; /**< State the resolver machine is in */ |
| 99 |
> |
char type; /**< Current request type. */ |
| 100 |
> |
char retries; /**< Retry counter */ |
| 101 |
> |
unsigned int sends; /**< Number of sends (>1 means resent). */ |
| 102 |
> |
char resend; /**< Send flag; 0 == don't resend. */ |
| 103 |
> |
time_t sentat; /**< Timestamp we last sent this request. */ |
| 104 |
> |
time_t timeout; /**< When this request times out. */ |
| 105 |
> |
struct irc_ssaddr addr; /**< Address for this request. */ |
| 106 |
> |
char name[RFC1035_MAX_DOMAIN_LENGTH]; /**< Hostname for this request. */ |
| 107 |
> |
size_t namelength; /**< Actual hostname length. */ |
| 108 |
> |
dns_callback_fnc callback; /**< Callback function on completion. */ |
| 109 |
> |
void *callback_ctx; /**< Context pointer for callback. */ |
| 110 |
|
}; |
| 111 |
|
|
| 112 |
|
static fde_t ResolverFileDescriptor; |
| 113 |
|
static dlink_list request_list; |
| 114 |
|
static mp_pool_t *dns_pool; |
| 115 |
|
|
| 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); |
| 116 |
|
|
| 117 |
+ |
/* |
| 118 |
+ |
* rem_request - remove a request from the list. |
| 119 |
+ |
* This must also free any memory that has been allocated for |
| 120 |
+ |
* temporary storage of DNS results. |
| 121 |
+ |
*/ |
| 122 |
+ |
static void |
| 123 |
+ |
rem_request(struct reslist *request) |
| 124 |
+ |
{ |
| 125 |
+ |
dlinkDelete(&request->node, &request_list); |
| 126 |
+ |
mp_pool_release(request); |
| 127 |
+ |
} |
| 128 |
+ |
|
| 129 |
+ |
/* |
| 130 |
+ |
* make_request - Create a DNS request record for the server. |
| 131 |
+ |
*/ |
| 132 |
+ |
static struct reslist * |
| 133 |
+ |
make_request(dns_callback_fnc callback, void *ctx) |
| 134 |
+ |
{ |
| 135 |
+ |
struct reslist *request = mp_pool_get(dns_pool); |
| 136 |
+ |
|
| 137 |
+ |
request->sentat = CurrentTime; |
| 138 |
+ |
request->retries = 2; |
| 139 |
+ |
request->resend = 1; |
| 140 |
+ |
request->timeout = 4; /* Start at 4 and exponential inc. */ |
| 141 |
+ |
request->state = REQ_IDLE; |
| 142 |
+ |
request->callback = callback; |
| 143 |
+ |
request->callback_ctx = ctx; |
| 144 |
+ |
|
| 145 |
+ |
dlinkAdd(request, &request->node, &request_list); |
| 146 |
+ |
return request; |
| 147 |
+ |
} |
| 148 |
|
|
| 149 |
|
/* |
| 150 |
|
* int |
| 160 |
|
static int |
| 161 |
|
res_ourserver(const struct irc_ssaddr *inp) |
| 162 |
|
{ |
| 147 |
– |
#ifdef IPV6 |
| 163 |
|
const struct sockaddr_in6 *v6; |
| 164 |
|
const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; |
| 150 |
– |
#endif |
| 165 |
|
const struct sockaddr_in *v4; |
| 166 |
|
const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; |
| 167 |
|
|
| 168 |
|
for (unsigned int i = 0; i < irc_nscount; ++i) |
| 169 |
|
{ |
| 170 |
|
const struct irc_ssaddr *srv = &irc_nsaddr_list[i]; |
| 157 |
– |
#ifdef IPV6 |
| 171 |
|
v6 = (const struct sockaddr_in6 *)srv; |
| 159 |
– |
#endif |
| 172 |
|
v4 = (const struct sockaddr_in *)srv; |
| 173 |
|
|
| 174 |
< |
/* could probably just memcmp(srv, inp, srv.ss_len) here |
| 174 |
> |
/* |
| 175 |
> |
* Could probably just memcmp(srv, inp, srv.ss_len) here |
| 176 |
|
* but we'll air on the side of caution - stu |
| 164 |
– |
* |
| 177 |
|
*/ |
| 178 |
|
switch (srv->ss.ss_family) |
| 179 |
|
{ |
| 168 |
– |
#ifdef IPV6 |
| 180 |
|
case AF_INET6: |
| 181 |
|
if (srv->ss.ss_family == inp->ss.ss_family) |
| 182 |
|
if (v6->sin6_port == v6in->sin6_port) |
| 184 |
|
sizeof(struct in6_addr))) |
| 185 |
|
return 1; |
| 186 |
|
break; |
| 176 |
– |
#endif |
| 187 |
|
case AF_INET: |
| 188 |
|
if (srv->ss.ss_family == inp->ss.ss_family) |
| 189 |
|
if (v4->sin_port == v4in->sin_port) |
| 199 |
|
} |
| 200 |
|
|
| 201 |
|
/* |
| 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 |
– |
/* |
| 202 |
|
* start_resolver - do everything we need to read the resolv.conf file |
| 203 |
|
* and initialize the resolver file descriptor if needed |
| 204 |
|
*/ |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
/* |
| 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 |
– |
/* |
| 222 |
|
* restart_resolver - reread resolv.conf, reopen socket |
| 223 |
|
*/ |
| 224 |
|
void |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
/* |
| 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 |
– |
/* |
| 232 |
|
* delete_resolver_queries - cleanup outstanding queries |
| 233 |
|
* for which there no longer exist clients or conf lines. |
| 234 |
|
*/ |
| 296 |
|
} |
| 297 |
|
|
| 298 |
|
/* |
| 299 |
< |
* gethost_byname_type - get host address from name |
| 393 |
< |
* |
| 299 |
> |
* query_name - generate a query based on class, type and name. |
| 300 |
|
*/ |
| 301 |
< |
void |
| 302 |
< |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
| 301 |
> |
static void |
| 302 |
> |
query_name(const char *name, int query_class, int type, |
| 303 |
> |
struct reslist *request) |
| 304 |
|
{ |
| 305 |
< |
assert(name); |
| 306 |
< |
do_query_name(callback, ctx, name, NULL, type); |
| 400 |
< |
} |
| 305 |
> |
char buf[MAXPACKET]; |
| 306 |
> |
int request_len = 0; |
| 307 |
|
|
| 308 |
< |
/* |
| 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 |
< |
} |
| 308 |
> |
memset(buf, 0, sizeof(buf)); |
| 309 |
|
|
| 310 |
< |
/* |
| 311 |
< |
* gethost_byaddr - get host name from address |
| 312 |
< |
*/ |
| 313 |
< |
void |
| 314 |
< |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
| 315 |
< |
{ |
| 316 |
< |
do_query_number(callback, ctx, addr, NULL); |
| 310 |
> |
if ((request_len = irc_res_mkquery(name, query_class, type, |
| 311 |
> |
(unsigned char *)buf, sizeof(buf))) > 0) |
| 312 |
> |
{ |
| 313 |
> |
HEADER *header = (HEADER *)buf; |
| 314 |
> |
|
| 315 |
> |
/* |
| 316 |
> |
* Generate an unique id. |
| 317 |
> |
* NOTE: we don't have to worry about converting this to and from |
| 318 |
> |
* network byte order, the nameserver does not interpret this value |
| 319 |
> |
* and returns it unchanged. |
| 320 |
> |
*/ |
| 321 |
> |
do |
| 322 |
> |
header->id = (header->id + genrand_int32()) & 0xFFFF; |
| 323 |
> |
while (find_id(header->id)); |
| 324 |
> |
|
| 325 |
> |
request->id = header->id; |
| 326 |
> |
++request->sends; |
| 327 |
> |
|
| 328 |
> |
request->sent += send_res_msg(buf, request_len, request->sends); |
| 329 |
> |
} |
| 330 |
|
} |
| 331 |
|
|
| 332 |
|
/* |
| 336 |
|
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
| 337 |
|
struct reslist *request, int type) |
| 338 |
|
{ |
| 339 |
< |
char host_name[HOSTLEN + 1]; |
| 339 |
> |
char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1]; |
| 340 |
|
|
| 341 |
|
strlcpy(host_name, name, sizeof(host_name)); |
| 342 |
|
|
| 343 |
|
if (request == NULL) |
| 344 |
|
{ |
| 345 |
< |
request = make_request(callback, ctx); |
| 346 |
< |
request->name = MyCalloc(strlen(host_name) + 1); |
| 347 |
< |
request->type = type; |
| 348 |
< |
strcpy(request->name, host_name); |
| 441 |
< |
#ifdef IPV6 |
| 345 |
> |
request = make_request(callback, ctx); |
| 346 |
> |
request->type = type; |
| 347 |
> |
request->namelength = strlcpy(request->name, host_name, sizeof(request->name)); |
| 348 |
> |
|
| 349 |
|
if (type != T_A) |
| 350 |
|
request->state = REQ_AAAA; |
| 351 |
|
else |
| 352 |
< |
#endif |
| 446 |
< |
request->state = REQ_A; |
| 352 |
> |
request->state = REQ_A; |
| 353 |
|
} |
| 354 |
|
|
| 355 |
|
request->type = type; |
| 375 |
|
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
| 376 |
|
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
| 377 |
|
} |
| 472 |
– |
#ifdef IPV6 |
| 378 |
|
else if (addr->ss.ss_family == AF_INET6) |
| 379 |
|
{ |
| 380 |
|
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
| 400 |
|
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
| 401 |
|
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
| 402 |
|
} |
| 403 |
< |
#endif |
| 403 |
> |
|
| 404 |
|
if (request == NULL) |
| 405 |
|
{ |
| 406 |
|
request = make_request(callback, ctx); |
| 407 |
|
request->type = T_PTR; |
| 408 |
|
memcpy(&request->addr, addr, sizeof(struct irc_ssaddr)); |
| 504 |
– |
request->name = MyCalloc(HOSTLEN + 1); |
| 409 |
|
} |
| 410 |
|
|
| 411 |
|
query_name(ipbuf, C_IN, T_PTR, request); |
| 412 |
|
} |
| 413 |
|
|
| 414 |
|
/* |
| 415 |
< |
* query_name - generate a query based on class, type and name. |
| 415 |
> |
* gethost_byname_type - get host address from name |
| 416 |
> |
* |
| 417 |
|
*/ |
| 418 |
< |
static void |
| 419 |
< |
query_name(const char *name, int query_class, int type, |
| 515 |
< |
struct reslist *request) |
| 418 |
> |
void |
| 419 |
> |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
| 420 |
|
{ |
| 421 |
< |
char buf[MAXPACKET]; |
| 422 |
< |
int request_len = 0; |
| 423 |
< |
|
| 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)); |
| 421 |
> |
assert(name); |
| 422 |
> |
do_query_name(callback, ctx, name, NULL, type); |
| 423 |
> |
} |
| 424 |
|
|
| 425 |
< |
request->id = header->id; |
| 426 |
< |
++request->sends; |
| 425 |
> |
/* |
| 426 |
> |
* gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported |
| 427 |
> |
*/ |
| 428 |
> |
void |
| 429 |
> |
gethost_byname(dns_callback_fnc callback, void *ctx, const char *name) |
| 430 |
> |
{ |
| 431 |
> |
gethost_byname_type(callback, ctx, name, T_AAAA); |
| 432 |
> |
} |
| 433 |
|
|
| 434 |
< |
request->sent += send_res_msg(buf, request_len, request->sends); |
| 435 |
< |
} |
| 434 |
> |
/* |
| 435 |
> |
* gethost_byaddr - get host name from address |
| 436 |
> |
*/ |
| 437 |
> |
void |
| 438 |
> |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
| 439 |
> |
{ |
| 440 |
> |
do_query_number(callback, ctx, addr, NULL); |
| 441 |
|
} |
| 442 |
|
|
| 443 |
|
static void |
| 454 |
|
case T_A: |
| 455 |
|
do_query_name(NULL, NULL, request->name, request, request->type); |
| 456 |
|
break; |
| 558 |
– |
#ifdef IPV6 |
| 457 |
|
case T_AAAA: /* Didn't work, try A */ |
| 458 |
|
if (request->state == REQ_AAAA) |
| 459 |
|
do_query_name(NULL, NULL, request->name, request, T_A); |
| 562 |
– |
#endif |
| 460 |
|
default: |
| 461 |
|
break; |
| 462 |
|
} |
| 468 |
|
static int |
| 469 |
|
proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob) |
| 470 |
|
{ |
| 471 |
< |
char hostbuf[HOSTLEN + 100]; /* working buffer */ |
| 471 |
> |
char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */ |
| 472 |
|
unsigned char *current; /* current position in buf */ |
| 576 |
– |
int query_class; /* answer class */ |
| 473 |
|
int type; /* answer type */ |
| 474 |
|
int n; /* temp count */ |
| 475 |
|
int rd_length; |
| 476 |
|
struct sockaddr_in *v4; /* conversion */ |
| 581 |
– |
#ifdef IPV6 |
| 477 |
|
struct sockaddr_in6 *v6; |
| 478 |
< |
#endif |
| 478 |
> |
|
| 479 |
|
current = (unsigned char *)buf + sizeof(HEADER); |
| 480 |
|
|
| 481 |
|
for (; header->qdcount > 0; --header->qdcount) |
| 499 |
|
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 500 |
|
return 0; |
| 501 |
|
|
| 502 |
< |
hostbuf[HOSTLEN] = '\0'; |
| 502 |
> |
hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0'; |
| 503 |
|
|
| 504 |
|
/* |
| 505 |
|
* With Address arithmetic you have to be very anal |
| 513 |
|
|
| 514 |
|
type = irc_ns_get16(current); |
| 515 |
|
current += TYPE_SIZE; |
| 621 |
– |
|
| 622 |
– |
query_class = irc_ns_get16(current); |
| 516 |
|
current += CLASS_SIZE; |
| 624 |
– |
|
| 625 |
– |
request->ttl = irc_ns_get32(current); |
| 517 |
|
current += TTL_SIZE; |
| 627 |
– |
|
| 518 |
|
rd_length = irc_ns_get16(current); |
| 519 |
|
current += RDLENGTH_SIZE; |
| 520 |
|
|
| 539 |
|
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
| 540 |
|
return 1; |
| 541 |
|
break; |
| 652 |
– |
#ifdef IPV6 |
| 542 |
|
case T_AAAA: |
| 543 |
|
if (request->type != T_AAAA) |
| 544 |
|
return 0; |
| 552 |
|
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
| 553 |
|
return 1; |
| 554 |
|
break; |
| 666 |
– |
#endif |
| 555 |
|
case T_PTR: |
| 556 |
|
if (request->type != T_PTR) |
| 557 |
|
return 0; |
| 561 |
|
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
| 562 |
|
return 0; |
| 563 |
|
|
| 564 |
< |
strlcpy(request->name, hostbuf, HOSTLEN + 1); |
| 564 |
> |
request->namelength = strlcpy(request->name, hostbuf, sizeof(request->name)); |
| 565 |
|
return 1; |
| 566 |
|
break; |
| 567 |
|
case T_CNAME: /* First check we already haven't started looking into a cname */ |
| 604 |
|
char buf[sizeof(HEADER) + MAXPACKET]; |
| 605 |
|
HEADER *header; |
| 606 |
|
struct reslist *request = NULL; |
| 607 |
< |
int rc; |
| 607 |
> |
ssize_t rc = 0; |
| 608 |
|
socklen_t len = sizeof(struct irc_ssaddr); |
| 609 |
|
struct irc_ssaddr lsin; |
| 610 |
|
|
| 611 |
< |
rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); |
| 612 |
< |
|
| 613 |
< |
/* Re-schedule a read *after* recvfrom, or we'll be registering |
| 614 |
< |
* interest where it'll instantly be ready for read :-) -- adrian |
| 727 |
< |
*/ |
| 728 |
< |
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
| 729 |
< |
|
| 730 |
< |
/* Better to cast the sizeof instead of rc */ |
| 731 |
< |
if (rc <= (int)(sizeof(HEADER))) |
| 732 |
< |
return; |
| 611 |
> |
while ((rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1) |
| 612 |
> |
{ |
| 613 |
> |
if (rc <= (ssize_t)sizeof(HEADER)) |
| 614 |
> |
continue; |
| 615 |
|
|
| 616 |
< |
/* |
| 617 |
< |
* Check against possibly fake replies |
| 618 |
< |
*/ |
| 619 |
< |
if (!res_ourserver(&lsin)) |
| 620 |
< |
return; |
| 616 |
> |
/* |
| 617 |
> |
* Check against possibly fake replies |
| 618 |
> |
*/ |
| 619 |
> |
if (!res_ourserver(&lsin)) |
| 620 |
> |
continue; |
| 621 |
|
|
| 622 |
< |
/* |
| 623 |
< |
* Convert DNS reply reader from Network byte order to CPU byte order. |
| 624 |
< |
*/ |
| 625 |
< |
header = (HEADER *)buf; |
| 626 |
< |
header->ancount = ntohs(header->ancount); |
| 627 |
< |
header->qdcount = ntohs(header->qdcount); |
| 628 |
< |
header->nscount = ntohs(header->nscount); |
| 629 |
< |
header->arcount = ntohs(header->arcount); |
| 622 |
> |
/* |
| 623 |
> |
* Convert DNS reply reader from Network byte order to CPU byte order. |
| 624 |
> |
*/ |
| 625 |
> |
header = (HEADER *)buf; |
| 626 |
> |
header->ancount = ntohs(header->ancount); |
| 627 |
> |
header->qdcount = ntohs(header->qdcount); |
| 628 |
> |
header->nscount = ntohs(header->nscount); |
| 629 |
> |
header->arcount = ntohs(header->arcount); |
| 630 |
|
|
| 631 |
< |
/* |
| 632 |
< |
* Response for an id which we have already received an answer for |
| 633 |
< |
* just ignore this response. |
| 634 |
< |
*/ |
| 635 |
< |
if (!(request = find_id(header->id))) |
| 636 |
< |
return; |
| 631 |
> |
/* |
| 632 |
> |
* Response for an id which we have already received an answer for |
| 633 |
> |
* just ignore this response. |
| 634 |
> |
*/ |
| 635 |
> |
if ((request = find_id(header->id)) == NULL) |
| 636 |
> |
continue; |
| 637 |
|
|
| 638 |
< |
if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) |
| 757 |
< |
{ |
| 758 |
< |
if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN) |
| 638 |
> |
if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) |
| 639 |
|
{ |
| 640 |
< |
/* |
| 761 |
< |
* If a bad error was returned, stop here and don't |
| 762 |
< |
* send any more (no retries granted). |
| 763 |
< |
*/ |
| 764 |
< |
(*request->callback)(request->callback_ctx, NULL, NULL); |
| 765 |
< |
rem_request(request); |
| 766 |
< |
} |
| 767 |
< |
#ifdef IPV6 |
| 768 |
< |
else |
| 769 |
< |
{ |
| 770 |
< |
/* |
| 771 |
< |
* If we havent already tried this, and we're looking up AAAA, try A |
| 772 |
< |
* now |
| 773 |
< |
*/ |
| 774 |
< |
if (request->state == REQ_AAAA && request->type == T_AAAA) |
| 640 |
> |
if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN) |
| 641 |
|
{ |
| 642 |
< |
request->timeout += 4; |
| 643 |
< |
resend_query(request); |
| 642 |
> |
/* |
| 643 |
> |
* If a bad error was returned, stop here and don't |
| 644 |
> |
* send any more (no retries granted). |
| 645 |
> |
*/ |
| 646 |
> |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 647 |
> |
rem_request(request); |
| 648 |
> |
} |
| 649 |
> |
else |
| 650 |
> |
{ |
| 651 |
> |
/* |
| 652 |
> |
* If we havent already tried this, and we're looking up AAAA, try A now. |
| 653 |
> |
*/ |
| 654 |
> |
if (request->state == REQ_AAAA && request->type == T_AAAA) |
| 655 |
> |
{ |
| 656 |
> |
request->timeout += 4; |
| 657 |
> |
resend_query(request); |
| 658 |
> |
} |
| 659 |
|
} |
| 660 |
+ |
|
| 661 |
+ |
continue; |
| 662 |
|
} |
| 780 |
– |
#endif |
| 663 |
|
|
| 664 |
< |
return; |
| 665 |
< |
} |
| 664 |
> |
/* |
| 665 |
> |
* If this fails there was an error decoding the received packet. |
| 666 |
> |
* We only give it one shot. If it fails, just leave the client |
| 667 |
> |
* unresolved. |
| 668 |
> |
*/ |
| 669 |
> |
if (!proc_answer(request, header, buf, buf + rc)) |
| 670 |
> |
{ |
| 671 |
> |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 672 |
> |
rem_request(request); |
| 673 |
> |
continue; |
| 674 |
> |
} |
| 675 |
|
|
| 785 |
– |
/* |
| 786 |
– |
* If this fails there was an error decoding the received packet, |
| 787 |
– |
* try it again and hope it works the next time. |
| 788 |
– |
*/ |
| 789 |
– |
if (proc_answer(request, header, buf, buf + rc)) |
| 790 |
– |
{ |
| 676 |
|
if (request->type == T_PTR) |
| 677 |
|
{ |
| 678 |
< |
if (request->name == NULL) |
| 678 |
> |
if (request->namelength == 0) |
| 679 |
|
{ |
| 680 |
|
/* |
| 681 |
|
* Got a PTR response with no name, something bogus is happening |
| 682 |
|
* don't bother trying again, the client address doesn't resolve |
| 683 |
|
*/ |
| 684 |
< |
(*request->callback)(request->callback_ctx, NULL, NULL); |
| 684 |
> |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 685 |
|
rem_request(request); |
| 686 |
< |
return; |
| 686 |
> |
continue; |
| 687 |
|
} |
| 688 |
|
|
| 689 |
|
/* |
| 690 |
|
* Lookup the 'authoritative' name that we were given for the ip#. |
| 691 |
|
*/ |
| 807 |
– |
#ifdef IPV6 |
| 692 |
|
if (request->addr.ss.ss_family == AF_INET6) |
| 693 |
|
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
| 694 |
|
else |
| 695 |
< |
#endif |
| 812 |
< |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
| 695 |
> |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
| 696 |
|
rem_request(request); |
| 697 |
|
} |
| 698 |
|
else |
| 700 |
|
/* |
| 701 |
|
* Got a name and address response, client resolved |
| 702 |
|
*/ |
| 703 |
< |
(*request->callback)(request->callback_ctx, &request->addr, request->name); |
| 703 |
> |
(*request->callback)(request->callback_ctx, &request->addr, request->name, request->namelength); |
| 704 |
|
rem_request(request); |
| 705 |
|
} |
| 823 |
– |
} |
| 824 |
– |
else if (!request->sent) |
| 825 |
– |
{ |
| 826 |
– |
/* XXX - we got a response for a query we didn't send with a valid id? |
| 827 |
– |
* this should never happen, bail here and leave the client unresolved |
| 828 |
– |
*/ |
| 829 |
– |
assert(0); |
| 706 |
|
|
| 707 |
< |
(*request->callback)(request->callback_ctx, NULL, NULL); |
| 832 |
< |
/* XXX don't leak it */ |
| 833 |
< |
rem_request(request); |
| 707 |
> |
continue; |
| 708 |
|
} |
| 709 |
+ |
|
| 710 |
+ |
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
| 711 |
|
} |
| 712 |
|
|
| 713 |
|
void |
| 723 |
|
sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr); |
| 724 |
|
} |
| 725 |
|
} |
| 726 |
+ |
|
| 727 |
+ |
/* |
| 728 |
+ |
* timeout_query_list - Remove queries from the list which have been |
| 729 |
+ |
* there too long without being resolved. |
| 730 |
+ |
*/ |
| 731 |
+ |
static time_t |
| 732 |
+ |
timeout_query_list(void) |
| 733 |
+ |
{ |
| 734 |
+ |
dlink_node *ptr = NULL, *ptr_next = NULL; |
| 735 |
+ |
struct reslist *request = NULL; |
| 736 |
+ |
time_t next_time = 0; |
| 737 |
+ |
time_t timeout = 0; |
| 738 |
+ |
|
| 739 |
+ |
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
| 740 |
+ |
{ |
| 741 |
+ |
request = ptr->data; |
| 742 |
+ |
timeout = request->sentat + request->timeout; |
| 743 |
+ |
|
| 744 |
+ |
if (CurrentTime >= timeout) |
| 745 |
+ |
{ |
| 746 |
+ |
if (--request->retries <= 0) |
| 747 |
+ |
{ |
| 748 |
+ |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
| 749 |
+ |
rem_request(request); |
| 750 |
+ |
continue; |
| 751 |
+ |
} |
| 752 |
+ |
else |
| 753 |
+ |
{ |
| 754 |
+ |
request->sentat = CurrentTime; |
| 755 |
+ |
request->timeout += request->timeout; |
| 756 |
+ |
resend_query(request); |
| 757 |
+ |
} |
| 758 |
+ |
} |
| 759 |
+ |
|
| 760 |
+ |
if (next_time == 0 || timeout < next_time) |
| 761 |
+ |
next_time = timeout; |
| 762 |
+ |
} |
| 763 |
+ |
|
| 764 |
+ |
return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL); |
| 765 |
+ |
} |
| 766 |
+ |
|
| 767 |
+ |
/* |
| 768 |
+ |
* timeout_resolver - check request list |
| 769 |
+ |
*/ |
| 770 |
+ |
static void |
| 771 |
+ |
timeout_resolver(void *notused) |
| 772 |
+ |
{ |
| 773 |
+ |
timeout_query_list(); |
| 774 |
+ |
} |
| 775 |
+ |
|
| 776 |
+ |
/* |
| 777 |
+ |
* init_resolver - initialize resolver and resolver library |
| 778 |
+ |
*/ |
| 779 |
+ |
void |
| 780 |
+ |
init_resolver(void) |
| 781 |
+ |
{ |
| 782 |
+ |
static struct event event_timeout_resolver = |
| 783 |
+ |
{ |
| 784 |
+ |
.name = "timeout_resolver", |
| 785 |
+ |
.handler = timeout_resolver, |
| 786 |
+ |
.when = 1 |
| 787 |
+ |
}; |
| 788 |
+ |
|
| 789 |
+ |
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
| 790 |
+ |
memset(&ResolverFileDescriptor, 0, sizeof(fde_t)); |
| 791 |
+ |
start_resolver(); |
| 792 |
+ |
|
| 793 |
+ |
event_add(&event_timeout_resolver, NULL); |
| 794 |
+ |
} |