| 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 |
* July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, |
| 11 |
* added callbacks and reference counting of returned hostents. |
| 12 |
* --Bleep (Thomas Helvey <[email protected]>) |
| 13 |
* |
| 14 |
* This was all needlessly complicated for irc. Simplified. No more hostent |
| 15 |
* All we really care about is the IP -> hostname mappings. Thats all. |
| 16 |
* |
| 17 |
* Apr 28, 2003 --cryogen and Dianora |
| 18 |
* |
| 19 |
* DNS server flooding lessened, AAAA-or-A lookup removed, ip6.int support |
| 20 |
* removed, various robustness fixes |
| 21 |
* |
| 22 |
* 2006 --jilles and nenolod |
| 23 |
* |
| 24 |
* Clean up various crap, remove global state. Reindent because two space indent |
| 25 |
* is hideous. Also remove ancient assumptions that don't make sense anymore (e.g., |
| 26 |
* libmowgli targets C99, which specifies an 8-bit char). Pack all this stuff into |
| 27 |
* its own namespace. Also gutted a lot of needless/one-use/few-line functions. |
| 28 |
* Jesus, what were they thinking... |
| 29 |
* |
| 30 |
* 2012 --Elizacat |
| 31 |
*/ |
| 32 |
|
| 33 |
#include "mowgli.h" |
| 34 |
|
| 35 |
#define MOWGLI_DNS_MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */ |
| 36 |
#define MOWGLI_DNS_RES_MAXALIASES 35 /* maximum aliases allowed */ |
| 37 |
#define MOWGLI_DNS_RES_MAXADDRS 35 /* maximum addresses allowed */ |
| 38 |
#define MOWGLI_DNS_AR_TTL 600 /* TTL in seconds for dns cache entries */ |
| 39 |
|
| 40 |
/* RFC 1104/1105 wasn't very helpful about what these fields should be named, so for now, we'll |
| 41 |
just name them this way. we probably should look at what named calls them or something. */ |
| 42 |
#define MOWGLI_DNS_TYPE_SIZE ((size_t) 2) |
| 43 |
#define MOWGLI_DNS_CLASS_SIZE ((size_t) 2) |
| 44 |
#define MOWGLI_DNS_TTL_SIZE ((size_t) 4) |
| 45 |
#define MOWGLI_DNS_RDLENGTH_SIZE ((size_t) 2) |
| 46 |
#define MOWGLI_DNS_ANSWER_FIXED_SIZE (MOWGLI_DNS_TYPE_SIZE + MOWGLI_DNS_CLASS_SIZE + MOWGLI_DNS_TTL_SIZE + MOWGLI_DNS_RDLENGTH_SIZE) |
| 47 |
|
| 48 |
#define MOWGLI_DNS_MAXLINE 128 |
| 49 |
|
| 50 |
typedef struct |
| 51 |
{ |
| 52 |
mowgli_node_t node; |
| 53 |
int id; |
| 54 |
time_t ttl; |
| 55 |
char type; |
| 56 |
char queryname[MOWGLI_DNS_RES_HOSTLEN + 1]; /* name currently being queried */ |
| 57 |
char retries; /* retry counter */ |
| 58 |
char sends; /* number of sends (>1 means resent) */ |
| 59 |
time_t sentat; |
| 60 |
time_t timeout; |
| 61 |
unsigned int lastns; /* index of last server sent to */ |
| 62 |
struct sockaddr_storage addr; |
| 63 |
|
| 64 |
char *name; |
| 65 |
mowgli_dns_query_t *query; /* query callback for this request */ |
| 66 |
} mowgli_dns_reslist_t; |
| 67 |
|
| 68 |
static mowgli_heap_t *reslist_heap = NULL; |
| 69 |
|
| 70 |
#ifndef _WIN32 |
| 71 |
static int parse_resvconf(mowgli_dns_t *dns); |
| 72 |
|
| 73 |
#else |
| 74 |
static void parse_windows_resolvers(mowgli_dns_t *dns); |
| 75 |
|
| 76 |
#endif |
| 77 |
|
| 78 |
static void timeout_resolver(void *arg); |
| 79 |
static void add_nameserver(mowgli_dns_t *dns, const char *arg); |
| 80 |
static int res_ourserver(mowgli_dns_t *dns, const struct sockaddr_storage *inp); |
| 81 |
static void rem_request(mowgli_dns_t *dns, mowgli_dns_reslist_t *request); |
| 82 |
static mowgli_dns_reslist_t *make_request(mowgli_dns_t *dns, mowgli_dns_query_t *query); |
| 83 |
static void do_query_name(mowgli_dns_t *dns, mowgli_dns_query_t *query, const char *name, mowgli_dns_reslist_t *request, int); |
| 84 |
static void do_query_number(mowgli_dns_t *dns, mowgli_dns_query_t *query, const struct sockaddr_storage *, mowgli_dns_reslist_t *request); |
| 85 |
static void query_name(mowgli_dns_t *dns, mowgli_dns_reslist_t *request); |
| 86 |
static int send_res_msg(mowgli_dns_t *dns, const char *buf, int len, int count); |
| 87 |
static void resend_query(mowgli_dns_t *dns, mowgli_dns_reslist_t *request); |
| 88 |
static int check_question(mowgli_dns_reslist_t *request, mowgli_dns_resheader_t *header, char *buf, char *eob); |
| 89 |
static int proc_answer(mowgli_dns_reslist_t *request, mowgli_dns_resheader_t *header, char *, char *); |
| 90 |
static mowgli_dns_reslist_t *find_id(mowgli_dns_t *dns, int id); |
| 91 |
static mowgli_dns_reply_t *make_dnsreply(mowgli_dns_reslist_t *request); |
| 92 |
static void res_readreply(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata); |
| 93 |
|
| 94 |
/* |
| 95 |
* mowgli_dns_evloop_init - do everything we need to read the resolv.conf file |
| 96 |
* and initialize the resolver file descriptor if needed |
| 97 |
*/ |
| 98 |
int |
| 99 |
mowgli_dns_evloop_init(mowgli_dns_t *dns, mowgli_eventloop_t *eventloop) |
| 100 |
{ |
| 101 |
int i; |
| 102 |
mowgli_dns_evloop_t *state; |
| 103 |
|
| 104 |
if (dns->dns_state == NULL) |
| 105 |
dns->dns_state = mowgli_alloc(sizeof(mowgli_dns_evloop_t)); |
| 106 |
|
| 107 |
dns->dns_type = MOWGLI_DNS_TYPE_ASYNC; |
| 108 |
|
| 109 |
if (!reslist_heap) |
| 110 |
reslist_heap = mowgli_heap_create(sizeof(mowgli_dns_reslist_t), 512, BH_LAZY); |
| 111 |
|
| 112 |
state = dns->dns_state; |
| 113 |
|
| 114 |
state->rand = mowgli_random_create(); |
| 115 |
|
| 116 |
state->nscount = 0; |
| 117 |
|
| 118 |
#ifndef _WIN32 |
| 119 |
parse_resvconf(dns); |
| 120 |
#else |
| 121 |
parse_windows_resolvers(dns); |
| 122 |
#endif |
| 123 |
|
| 124 |
if (state->nscount == 0) |
| 125 |
{ |
| 126 |
mowgli_log("couldn't get resolv.conf entries, falling back to localhost resolver"); |
| 127 |
add_nameserver(dns, "127.0.0.1"); |
| 128 |
} |
| 129 |
|
| 130 |
for (i = 0; i < state->nscount; i++) |
| 131 |
state->timeout_count[i] = 0; |
| 132 |
|
| 133 |
if (state->vio == NULL) |
| 134 |
{ |
| 135 |
state->vio = mowgli_vio_create(dns); |
| 136 |
|
| 137 |
if (mowgli_vio_socket(state->vio, state->nsaddr_list[0].addr.ss_family, SOCK_DGRAM, 0) != 0) |
| 138 |
{ |
| 139 |
mowgli_log("start_resolver(): unable to open UDP resolver socket: %s", |
| 140 |
state->vio->error.string); |
| 141 |
return -1; |
| 142 |
} |
| 143 |
|
| 144 |
state->eventloop = eventloop; |
| 145 |
mowgli_vio_eventloop_attach(state->vio, state->eventloop, NULL); |
| 146 |
mowgli_pollable_setselect(state->eventloop, state->vio->io.e, MOWGLI_EVENTLOOP_IO_READ, res_readreply); |
| 147 |
mowgli_pollable_set_cloexec(state->vio->io.e, true); |
| 148 |
state->timeout_resolver_timer = mowgli_timer_add(state->eventloop, "timeout_resolver", timeout_resolver, dns, 1); |
| 149 |
} |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
int |
| 155 |
mowgli_dns_evloop_set_resolvconf(mowgli_dns_t *dns, const char *respath) |
| 156 |
{ |
| 157 |
#ifndef _WIN32 |
| 158 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 159 |
|
| 160 |
return_val_if_fail(dns, -1); |
| 161 |
|
| 162 |
state->resolvconf = respath; |
| 163 |
|
| 164 |
if (!state->dns_init) |
| 165 |
return mowgli_dns_evloop_restart(dns); |
| 166 |
|
| 167 |
return 0; |
| 168 |
#else |
| 169 |
mowgli_log("Unimplemented on Windows. :("); |
| 170 |
#endif |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
* mowgli_dns_evloop_restart - reread resolv.conf, reopen socket |
| 175 |
*/ |
| 176 |
int |
| 177 |
mowgli_dns_evloop_restart(mowgli_dns_t *dns) |
| 178 |
{ |
| 179 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 180 |
|
| 181 |
mowgli_dns_evloop_destroy(dns); |
| 182 |
return mowgli_dns_evloop_init(dns, state->eventloop); |
| 183 |
} |
| 184 |
|
| 185 |
/* mowgli_dns_evloop_destroy - finish us off */ |
| 186 |
void |
| 187 |
mowgli_dns_evloop_destroy(mowgli_dns_t *dns) |
| 188 |
{ |
| 189 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 190 |
|
| 191 |
mowgli_vio_destroy(state->vio); |
| 192 |
|
| 193 |
mowgli_timer_destroy(state->eventloop, state->timeout_resolver_timer); |
| 194 |
|
| 195 |
mowgli_object_unref(state->rand); |
| 196 |
mowgli_free(state); |
| 197 |
dns->dns_state = NULL; |
| 198 |
} |
| 199 |
|
| 200 |
#ifndef _WIN32 |
| 201 |
|
| 202 |
/* parse_resvconf() |
| 203 |
* inputs - NONE |
| 204 |
* output - -1 if failure, 0 if success |
| 205 |
* side effects - fills in state->nsaddr_list |
| 206 |
*/ |
| 207 |
static int |
| 208 |
parse_resvconf(mowgli_dns_t *dns) |
| 209 |
{ |
| 210 |
char *p; |
| 211 |
char *opt; |
| 212 |
char *arg; |
| 213 |
const char *respath; |
| 214 |
char input[MOWGLI_DNS_MAXLINE]; |
| 215 |
FILE *file; |
| 216 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 217 |
|
| 218 |
if (state->resolvconf) |
| 219 |
respath = state->resolvconf; |
| 220 |
else |
| 221 |
respath = "/etc/resolv.conf"; |
| 222 |
|
| 223 |
if ((file = fopen(respath, "r")) == NULL) |
| 224 |
{ |
| 225 |
mowgli_log("Failed to open %s: %s", respath, strerror(errno)); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
while (fgets(input, sizeof(input), file) != NULL) |
| 230 |
{ |
| 231 |
/* blow away any newline */ |
| 232 |
if ((p = strpbrk(input, "\r\n")) != NULL) |
| 233 |
*p = '\0'; |
| 234 |
|
| 235 |
p = input; |
| 236 |
|
| 237 |
/* skip until something thats not a space is seen */ |
| 238 |
while (isspace((unsigned char)*p)) |
| 239 |
{ |
| 240 |
p++; |
| 241 |
} |
| 242 |
|
| 243 |
/* if at this point, have a '\0' then continue */ |
| 244 |
if (*p == '\0') |
| 245 |
continue; |
| 246 |
|
| 247 |
/* Ignore comment lines immediately */ |
| 248 |
if ((*p == '#') || (*p == ';')) |
| 249 |
continue; |
| 250 |
|
| 251 |
/* skip until a space is found */ |
| 252 |
opt = p; |
| 253 |
|
| 254 |
while (!isspace((unsigned char)*p) && *p != '\0') |
| 255 |
{ |
| 256 |
p++; |
| 257 |
} |
| 258 |
|
| 259 |
if (*p == '\0') |
| 260 |
continue; /* no arguments?.. ignore this line */ |
| 261 |
|
| 262 |
/* blow away the space character */ |
| 263 |
*p++ = '\0'; |
| 264 |
|
| 265 |
/* skip these spaces that are before the argument */ |
| 266 |
while (isspace((unsigned char)*p)) |
| 267 |
{ |
| 268 |
p++; |
| 269 |
} |
| 270 |
|
| 271 |
/* Now arg should be right where p is pointing */ |
| 272 |
arg = p; |
| 273 |
|
| 274 |
if ((p = strpbrk(arg, " \t")) != NULL) |
| 275 |
*p = '\0'; /* take the first word */ |
| 276 |
|
| 277 |
if (strcmp(opt, "domain") == 0) |
| 278 |
mowgli_strlcpy(state->domain, arg, sizeof(state->domain)); |
| 279 |
else if (strcmp(opt, "nameserver") == 0) |
| 280 |
add_nameserver(dns, arg); |
| 281 |
} |
| 282 |
|
| 283 |
fclose(file); |
| 284 |
|
| 285 |
state->dns_init = true; |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
#else |
| 291 |
|
| 292 |
extern int mowgli_dns_get_windows_nameservers(char *ret_buf, size_t ret_size); |
| 293 |
|
| 294 |
static void |
| 295 |
parse_windows_resolvers(mowgli_dns_t *dns) |
| 296 |
{ |
| 297 |
char ns_buf[4096]; |
| 298 |
char *server; |
| 299 |
|
| 300 |
mowgli_dns_get_windows_nameservers(ns_buf, sizeof ns_buf); |
| 301 |
|
| 302 |
for (server = strtok(ns_buf, ","); server != NULL; server = strtok(NULL, ",")) |
| 303 |
add_nameserver(dns, server); |
| 304 |
} |
| 305 |
|
| 306 |
#endif |
| 307 |
|
| 308 |
/* add_nameserver() |
| 309 |
* input - either an IPV4 address in dotted quad or an IPV6 address in : format |
| 310 |
* output - NONE |
| 311 |
* side effects - entry in state->nsaddr_list is filled in as needed */ |
| 312 |
static void |
| 313 |
add_nameserver(mowgli_dns_t *dns, const char *arg) |
| 314 |
{ |
| 315 |
struct addrinfo hints, *res; |
| 316 |
|
| 317 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 318 |
|
| 319 |
#ifdef DEBUG |
| 320 |
mowgli_log("add_nameserver(): %s", arg); |
| 321 |
#endif |
| 322 |
|
| 323 |
/* Done max number of nameservers? */ |
| 324 |
if (state->nscount >= MOWGLI_DNS_MAXNS) |
| 325 |
{ |
| 326 |
mowgli_log("Too many nameservers, ignoring %s", arg); |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
memset(&hints, 0, sizeof(hints)); |
| 331 |
hints.ai_family = PF_UNSPEC; |
| 332 |
hints.ai_socktype = SOCK_DGRAM; |
| 333 |
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
| 334 |
|
| 335 |
if (getaddrinfo(arg, "domain", &hints, &res)) |
| 336 |
return; |
| 337 |
|
| 338 |
if (res == NULL) |
| 339 |
return; |
| 340 |
|
| 341 |
memcpy(&state->nsaddr_list[state->nscount].addr, res->ai_addr, res->ai_addrlen); |
| 342 |
state->nsaddr_list[state->nscount].addrlen = res->ai_addrlen; |
| 343 |
state->nscount++; |
| 344 |
freeaddrinfo(res); |
| 345 |
} |
| 346 |
|
| 347 |
/* |
| 348 |
* int |
| 349 |
* res_ourserver(dns, inp) |
| 350 |
* looks up "inp" in state->nsaddr_list[] |
| 351 |
* returns: |
| 352 |
* 0 : not found |
| 353 |
* >0 : found |
| 354 |
* author: |
| 355 |
* paul vixie, 29may94 |
| 356 |
* revised for ircd, cryogen(stu) may03 |
| 357 |
* rewritten by Elizacat 25mar12 |
| 358 |
*/ |
| 359 |
static int |
| 360 |
res_ourserver(mowgli_dns_t *dns, const struct sockaddr_storage *inp) |
| 361 |
{ |
| 362 |
int ns; |
| 363 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 364 |
|
| 365 |
for (ns = 0; ns < state->nscount; ns++) |
| 366 |
{ |
| 367 |
const struct sockaddr_storage *srv = &state->nsaddr_list[ns].addr; |
| 368 |
|
| 369 |
/* could probably just memcmp(srv, inp, srv.ss_len) here but we'll air on the side of |
| 370 |
caution - stu */ |
| 371 |
switch (srv->ss_family) |
| 372 |
{ |
| 373 |
case AF_INET6: |
| 374 |
{ |
| 375 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *) srv; |
| 376 |
const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *) inp; |
| 377 |
|
| 378 |
if ((srv->ss_family == inp->ss_family) && (v6->sin6_port == v6in->sin6_port)) |
| 379 |
if ((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, |
| 380 |
sizeof(struct in6_addr)) == 0) || |
| 381 |
(memcmp(&v6->sin6_addr.s6_addr, &in6addr_any, |
| 382 |
sizeof(struct in6_addr)) == 0)) |
| 383 |
{ |
| 384 |
state->timeout_count[ns] = 0; |
| 385 |
return 1; |
| 386 |
} |
| 387 |
|
| 388 |
break; |
| 389 |
} |
| 390 |
case AF_INET: |
| 391 |
{ |
| 392 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *) srv; |
| 393 |
const struct sockaddr_in *v4in = (const struct sockaddr_in *) inp; |
| 394 |
|
| 395 |
if ((srv->ss_family == inp->ss_family) && (v4->sin_port == v4in->sin_port)) |
| 396 |
if ((v4->sin_addr.s_addr == INADDR_ANY) |
| 397 |
|| (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)) |
| 398 |
{ |
| 399 |
state->timeout_count[ns] = 0; |
| 400 |
return 1; |
| 401 |
} |
| 402 |
|
| 403 |
break; |
| 404 |
} |
| 405 |
default: |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
* timeout_query_list - Remove queries from the list which have been |
| 415 |
* there too long without being resolved. |
| 416 |
*/ |
| 417 |
static time_t |
| 418 |
timeout_query_list(mowgli_dns_t *dns, time_t now) |
| 419 |
{ |
| 420 |
mowgli_node_t *ptr; |
| 421 |
mowgli_node_t *next_ptr; |
| 422 |
mowgli_dns_reslist_t *request; |
| 423 |
time_t next_time = 0; |
| 424 |
time_t timeout = 0; |
| 425 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 426 |
|
| 427 |
MOWGLI_ITER_FOREACH_SAFE(ptr, next_ptr, state->request_list.head) |
| 428 |
{ |
| 429 |
request = ptr->data; |
| 430 |
timeout = request->sentat + request->timeout; |
| 431 |
|
| 432 |
if (now >= timeout) |
| 433 |
{ |
| 434 |
if (--request->retries <= 0) |
| 435 |
{ |
| 436 |
(*request->query->callback)(NULL, MOWGLI_DNS_RES_TIMEOUT, request->query->ptr); |
| 437 |
rem_request(dns, request); |
| 438 |
continue; |
| 439 |
} |
| 440 |
else |
| 441 |
{ |
| 442 |
state->timeout_count[request->lastns]++; |
| 443 |
request->sentat = now; |
| 444 |
request->timeout += request->timeout; |
| 445 |
resend_query(dns, request); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
if ((next_time == 0) || (timeout < next_time)) |
| 450 |
next_time = timeout; |
| 451 |
} |
| 452 |
|
| 453 |
return (next_time > now) ? next_time : (now + MOWGLI_DNS_AR_TTL); |
| 454 |
} |
| 455 |
|
| 456 |
/* |
| 457 |
* timeout_resolver - check request list |
| 458 |
*/ |
| 459 |
static void |
| 460 |
timeout_resolver(void *arg) |
| 461 |
{ |
| 462 |
mowgli_dns_t *dns = arg; |
| 463 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 464 |
time_t now, next; |
| 465 |
|
| 466 |
now = mowgli_eventloop_get_time(state->eventloop); |
| 467 |
next = timeout_query_list(dns, now); |
| 468 |
|
| 469 |
/* Reschedule */ |
| 470 |
mowgli_timer_destroy(state->eventloop, state->timeout_resolver_timer); |
| 471 |
mowgli_timer_add(state->eventloop, "timeout_resolver", timeout_resolver, dns, next - now); |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* mowgli_dns_evloop_add_local_domain - Add the domain to hostname, if it is missing |
| 476 |
* (as suggested by [email protected]) |
| 477 |
*/ |
| 478 |
void |
| 479 |
mowgli_dns_evloop_add_local_domain(mowgli_dns_t *dns, char *hname, size_t size) |
| 480 |
{ |
| 481 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 482 |
|
| 483 |
/* try to fix up unqualified names */ |
| 484 |
if (strchr(hname, '.') == NULL) |
| 485 |
if (state->domain[0]) |
| 486 |
{ |
| 487 |
size_t len = strlen(hname); |
| 488 |
|
| 489 |
if ((strlen(state->domain) + len + 2) < size) |
| 490 |
{ |
| 491 |
hname[len++] = '.'; |
| 492 |
strcpy(hname + len, state->domain); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
/* |
| 498 |
* rem_request - remove a request from the list. |
| 499 |
* This must also free any memory that has been allocated for |
| 500 |
* temporary storage of DNS results. |
| 501 |
*/ |
| 502 |
static void |
| 503 |
rem_request(mowgli_dns_t *dns, mowgli_dns_reslist_t *request) |
| 504 |
{ |
| 505 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 506 |
|
| 507 |
return_if_fail(request != NULL); |
| 508 |
|
| 509 |
mowgli_node_delete(&request->node, &state->request_list); |
| 510 |
mowgli_free(request->name); |
| 511 |
mowgli_heap_free(reslist_heap, request); |
| 512 |
} |
| 513 |
|
| 514 |
/* |
| 515 |
* make_request - Create a DNS request record for the server. |
| 516 |
*/ |
| 517 |
static mowgli_dns_reslist_t * |
| 518 |
make_request(mowgli_dns_t *dns, mowgli_dns_query_t *query) |
| 519 |
{ |
| 520 |
mowgli_dns_reslist_t *request = mowgli_heap_alloc(reslist_heap); |
| 521 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 522 |
|
| 523 |
request->sentat = mowgli_eventloop_get_time(state->eventloop); |
| 524 |
request->retries = 3; |
| 525 |
request->timeout = 4; /* start at 4 and exponential inc. */ |
| 526 |
request->query = query; |
| 527 |
|
| 528 |
mowgli_node_add(request, &request->node, &state->request_list); |
| 529 |
|
| 530 |
return request; |
| 531 |
} |
| 532 |
|
| 533 |
/* |
| 534 |
* mowgli_dns_evloop_delete_queries - cleanup outstanding queries |
| 535 |
* for which there no longer exist clients or conf lines. |
| 536 |
*/ |
| 537 |
void |
| 538 |
mowgli_dns_evloop_delete_queries(mowgli_dns_t *dns, const mowgli_dns_query_t *query) |
| 539 |
{ |
| 540 |
mowgli_node_t *ptr; |
| 541 |
mowgli_node_t *next_ptr; |
| 542 |
mowgli_dns_reslist_t *request; |
| 543 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 544 |
|
| 545 |
MOWGLI_ITER_FOREACH_SAFE(ptr, next_ptr, state->request_list.head) |
| 546 |
{ |
| 547 |
if ((request = ptr->data) != NULL) |
| 548 |
if (query == request->query) |
| 549 |
rem_request(dns, request); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
/* |
| 554 |
* retryfreq - determine how many queries to wait before resending |
| 555 |
* if there have been that many consecutive timeouts |
| 556 |
*/ |
| 557 |
static inline int |
| 558 |
retryfreq(int timeouts) |
| 559 |
{ |
| 560 |
int i; |
| 561 |
int counter = 1; |
| 562 |
const int max_retries = 5; |
| 563 |
|
| 564 |
for (i = 0; i < (timeouts < max_retries ? timeouts : max_retries); i++) |
| 565 |
counter *= 3; |
| 566 |
|
| 567 |
return counter; |
| 568 |
} |
| 569 |
|
| 570 |
/* |
| 571 |
* send_res_msg - sends msg to a nameserver. |
| 572 |
* This should reflect /etc/resolv.conf. |
| 573 |
* Returns number of nameserver successfully sent to |
| 574 |
* or -1 if no successful sends. |
| 575 |
*/ |
| 576 |
static int |
| 577 |
send_res_msg(mowgli_dns_t *dns, const char *rmsg, int len, int rcount) |
| 578 |
{ |
| 579 |
int i; |
| 580 |
int ns; |
| 581 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 582 |
|
| 583 |
state->retrycnt++; |
| 584 |
|
| 585 |
/* First try a nameserver that seems to work. Every once in a while, try a possibly broken one |
| 586 |
* to check if it is working again. */ |
| 587 |
for (i = 0; i < state->nscount; i++) |
| 588 |
{ |
| 589 |
ns = (i + rcount - 1) % state->nscount; |
| 590 |
|
| 591 |
if (state->timeout_count[ns] && state->retrycnt % retryfreq(state->timeout_count[ns])) |
| 592 |
continue; |
| 593 |
|
| 594 |
if (mowgli_vio_sendto(state->vio, rmsg, len, &state->nsaddr_list[ns]) == len) |
| 595 |
return ns; |
| 596 |
} |
| 597 |
|
| 598 |
/* No known working nameservers, try some broken one. */ |
| 599 |
for (i = 0; i < state->nscount; i++) |
| 600 |
{ |
| 601 |
ns = (i + rcount - 1) % state->nscount; |
| 602 |
|
| 603 |
if (!state->timeout_count[ns]) |
| 604 |
continue; |
| 605 |
|
| 606 |
if (mowgli_vio_sendto(state->vio, rmsg, len, &state->nsaddr_list[ns]) == len) |
| 607 |
return ns; |
| 608 |
} |
| 609 |
|
| 610 |
return -1; |
| 611 |
} |
| 612 |
|
| 613 |
/* |
| 614 |
* find_id - find a dns request id (id is determined by dn_mkquery) |
| 615 |
*/ |
| 616 |
static mowgli_dns_reslist_t * |
| 617 |
find_id(mowgli_dns_t *dns, int id) |
| 618 |
{ |
| 619 |
mowgli_node_t *ptr; |
| 620 |
mowgli_dns_reslist_t *request; |
| 621 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 622 |
|
| 623 |
MOWGLI_ITER_FOREACH(ptr, state->request_list.head) |
| 624 |
{ |
| 625 |
request = ptr->data; |
| 626 |
|
| 627 |
if (request->id == id) |
| 628 |
return request; |
| 629 |
} |
| 630 |
|
| 631 |
return NULL; |
| 632 |
} |
| 633 |
|
| 634 |
/* |
| 635 |
* mowgli_dns_evloop_gethost_byname - get host address from name |
| 636 |
* |
| 637 |
*/ |
| 638 |
void |
| 639 |
mowgli_dns_evloop_gethost_byname(mowgli_dns_t *dns, const char *name, mowgli_dns_query_t *query, int type) |
| 640 |
{ |
| 641 |
return_if_fail(name != NULL); |
| 642 |
|
| 643 |
do_query_name(dns, query, name, NULL, type); |
| 644 |
} |
| 645 |
|
| 646 |
/* |
| 647 |
* mowgli_dns_evloop_gethost_byaddr - get host name from address |
| 648 |
*/ |
| 649 |
void |
| 650 |
mowgli_dns_evloop_gethost_byaddr(mowgli_dns_t *dns, const struct sockaddr_storage *addr, mowgli_dns_query_t *query) |
| 651 |
{ |
| 652 |
return_if_fail(addr != NULL); |
| 653 |
|
| 654 |
do_query_number(dns, query, addr, NULL); |
| 655 |
} |
| 656 |
|
| 657 |
/* |
| 658 |
* do_query_name - nameserver lookup name |
| 659 |
*/ |
| 660 |
static void |
| 661 |
do_query_name(mowgli_dns_t *dns, mowgli_dns_query_t *query, const char *name, mowgli_dns_reslist_t *request, int type) |
| 662 |
{ |
| 663 |
char host_name[MOWGLI_DNS_RES_HOSTLEN + 1]; |
| 664 |
|
| 665 |
mowgli_strlcpy(host_name, name, MOWGLI_DNS_RES_HOSTLEN + 1); |
| 666 |
mowgli_dns_evloop_add_local_domain(dns, host_name, MOWGLI_DNS_RES_HOSTLEN); |
| 667 |
|
| 668 |
if (request == NULL) |
| 669 |
{ |
| 670 |
request = make_request(dns, query); |
| 671 |
request->name = mowgli_strdup(host_name); |
| 672 |
} |
| 673 |
|
| 674 |
mowgli_strlcpy(request->queryname, host_name, sizeof(request->queryname)); |
| 675 |
request->type = type; |
| 676 |
query_name(dns, request); |
| 677 |
} |
| 678 |
|
| 679 |
/* |
| 680 |
* do_query_number - Use this to do reverse IP# lookups. |
| 681 |
*/ |
| 682 |
static void |
| 683 |
do_query_number(mowgli_dns_t *dns, mowgli_dns_query_t *query, const struct sockaddr_storage *addr, mowgli_dns_reslist_t *request) |
| 684 |
{ |
| 685 |
const unsigned char *cp; |
| 686 |
const size_t size = addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
| 687 |
|
| 688 |
if (request == NULL) |
| 689 |
{ |
| 690 |
request = make_request(dns, query); |
| 691 |
memcpy(&request->addr, addr, size); |
| 692 |
request->name = mowgli_alloc(MOWGLI_DNS_RES_HOSTLEN + 1); |
| 693 |
} |
| 694 |
|
| 695 |
if (addr->ss_family == AF_INET) |
| 696 |
{ |
| 697 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *) addr; |
| 698 |
cp = (const unsigned char *) &v4->sin_addr.s_addr; |
| 699 |
|
| 700 |
sprintf(request->queryname, "%u.%u.%u.%u.in-addr.arpa", (unsigned int) (cp[3]), |
| 701 |
(unsigned int) (cp[2]), (unsigned int) (cp[1]), (unsigned int) (cp[0])); |
| 702 |
} |
| 703 |
else if (addr->ss_family == AF_INET6) |
| 704 |
{ |
| 705 |
int i; |
| 706 |
char *rqptr = request->queryname; |
| 707 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *) addr; |
| 708 |
cp = (const unsigned char *) &v6->sin6_addr.s6_addr; |
| 709 |
|
| 710 |
for (i = 15; i >= 0; i--, rqptr += 4) |
| 711 |
sprintf(rqptr, "%1x.%1x.", |
| 712 |
(unsigned int) (cp[i] & 0xf), |
| 713 |
(unsigned int) (cp[i] >> 4)); |
| 714 |
|
| 715 |
strcpy(rqptr, "ip6.arpa"); |
| 716 |
} |
| 717 |
else |
| 718 |
{ |
| 719 |
mowgli_log("do_query_number() called with invalid sockaddr_storage %d", addr->ss_family); |
| 720 |
return; |
| 721 |
} |
| 722 |
|
| 723 |
request->type = MOWGLI_DNS_T_PTR; |
| 724 |
query_name(dns, request); |
| 725 |
} |
| 726 |
|
| 727 |
/* |
| 728 |
* query_name - generate a query based on class, type and name. |
| 729 |
*/ |
| 730 |
static void |
| 731 |
query_name(mowgli_dns_t *dns, mowgli_dns_reslist_t *request) |
| 732 |
{ |
| 733 |
char buf[MOWGLI_DNS_MAXPACKET]; |
| 734 |
int request_len = 0; |
| 735 |
int ns; |
| 736 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 737 |
|
| 738 |
memset(buf, 0, sizeof(buf)); |
| 739 |
|
| 740 |
if ((request_len = |
| 741 |
mowgli_dns_res_mkquery(request->queryname, MOWGLI_DNS_C_IN, request->type, (unsigned char *) buf, |
| 742 |
sizeof(buf))) > 0) |
| 743 |
{ |
| 744 |
mowgli_dns_resheader_t *header = (mowgli_dns_resheader_t *) buf; |
| 745 |
|
| 746 |
/* |
| 747 |
* generate an unique id |
| 748 |
* NOTE: we don't have to worry about converting this to and from |
| 749 |
* network byte order, the nameserver does not interpret this value |
| 750 |
* and returns it unchanged |
| 751 |
*/ |
| 752 |
do |
| 753 |
header->id = (header->id + mowgli_random_int(state->rand)) & 0xffff; |
| 754 |
|
| 755 |
while (find_id(dns, header->id)); |
| 756 |
|
| 757 |
request->id = header->id; |
| 758 |
++request->sends; |
| 759 |
|
| 760 |
ns = send_res_msg(dns, buf, request_len, request->sends); |
| 761 |
|
| 762 |
if (ns != -1) |
| 763 |
request->lastns = ns; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
static void |
| 768 |
resend_query(mowgli_dns_t *dns, mowgli_dns_reslist_t *request) |
| 769 |
{ |
| 770 |
switch (request->type) |
| 771 |
{ |
| 772 |
case MOWGLI_DNS_T_PTR: |
| 773 |
do_query_number(dns, NULL, &request->addr, request); |
| 774 |
break; |
| 775 |
case MOWGLI_DNS_T_A: |
| 776 |
case MOWGLI_DNS_T_AAAA: |
| 777 |
do_query_name(dns, NULL, request->name, request, request->type); |
| 778 |
break; |
| 779 |
default: |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
/* |
| 785 |
* check_question - check if the reply really belongs to the |
| 786 |
* name we queried (to guard against late replies from previous |
| 787 |
* queries with the same id). |
| 788 |
*/ |
| 789 |
static int |
| 790 |
check_question(mowgli_dns_reslist_t *request, mowgli_dns_resheader_t *header, char *buf, char *eob) |
| 791 |
{ |
| 792 |
char hostbuf[MOWGLI_DNS_RES_HOSTLEN + 1]; /* working buffer */ |
| 793 |
unsigned char *current; /* current position in buf */ |
| 794 |
int n; /* temp count */ |
| 795 |
|
| 796 |
current = (unsigned char *) buf + sizeof(mowgli_dns_resheader_t); |
| 797 |
|
| 798 |
if (header->qdcount != 1) |
| 799 |
return 0; |
| 800 |
|
| 801 |
n = mowgli_dns_dn_expand((unsigned char *) buf, (unsigned char *) eob, current, hostbuf, sizeof(hostbuf)); |
| 802 |
|
| 803 |
if (n <= 0) |
| 804 |
return 0; |
| 805 |
|
| 806 |
if (strcasecmp(hostbuf, request->queryname)) |
| 807 |
return 0; |
| 808 |
|
| 809 |
return 1; |
| 810 |
} |
| 811 |
|
| 812 |
/* |
| 813 |
* proc_answer - process name server reply |
| 814 |
*/ |
| 815 |
static int |
| 816 |
proc_answer(mowgli_dns_reslist_t *request, mowgli_dns_resheader_t *header, char *buf, char *eob) |
| 817 |
{ |
| 818 |
char hostbuf[MOWGLI_DNS_RES_HOSTLEN + 100]; /* working buffer */ |
| 819 |
unsigned char *current; /* current position in buf */ |
| 820 |
int query_class;/* answer class */ |
| 821 |
int type; /* answer type */ |
| 822 |
int n; /* temp count */ |
| 823 |
int rd_length; |
| 824 |
|
| 825 |
current = (unsigned char *) buf + sizeof(mowgli_dns_resheader_t); |
| 826 |
|
| 827 |
for (; header->qdcount > 0; --header->qdcount) |
| 828 |
{ |
| 829 |
if ((n = mowgli_dns_dn_skipname(current, (unsigned char *) eob)) < 0) |
| 830 |
return 0; |
| 831 |
|
| 832 |
current += (size_t) n + MOWGLI_DNS_QFIXEDSIZE; |
| 833 |
} |
| 834 |
|
| 835 |
/* process each answer sent to us. */ |
| 836 |
while (header->ancount > 0 && (char *) current < eob) |
| 837 |
{ |
| 838 |
header->ancount--; |
| 839 |
|
| 840 |
n = mowgli_dns_dn_expand((unsigned char *) buf, (unsigned char *) eob, current, hostbuf, |
| 841 |
sizeof(hostbuf)); |
| 842 |
|
| 843 |
/* Broken message (< 0) or no more answers left (== 0) */ |
| 844 |
if (n <= 0) |
| 845 |
return 0; |
| 846 |
|
| 847 |
hostbuf[MOWGLI_DNS_RES_HOSTLEN] = '\0'; |
| 848 |
|
| 849 |
/* With Address arithmetic you have to be very anal -- this code was not working on alpha due |
| 850 |
* to that (spotted by rodder/jailbird/dianora) */ |
| 851 |
current += (size_t) n; |
| 852 |
|
| 853 |
if (!(((char *) current + MOWGLI_DNS_ANSWER_FIXED_SIZE) < eob)) |
| 854 |
break; |
| 855 |
|
| 856 |
type = mowgli_dns_ns_get16(current); |
| 857 |
current += MOWGLI_DNS_TYPE_SIZE; |
| 858 |
|
| 859 |
query_class = mowgli_dns_ns_get16(current); |
| 860 |
current += MOWGLI_DNS_CLASS_SIZE; |
| 861 |
|
| 862 |
/* We may use this later at some point so... eliminate bogus GCC warning */ |
| 863 |
(void) query_class; |
| 864 |
|
| 865 |
request->ttl = mowgli_dns_ns_get32(current); |
| 866 |
current += MOWGLI_DNS_TTL_SIZE; |
| 867 |
|
| 868 |
rd_length = mowgli_dns_ns_get16(current); |
| 869 |
current += MOWGLI_DNS_RDLENGTH_SIZE; |
| 870 |
|
| 871 |
/* Wait to set request->type until we verify this structure */ |
| 872 |
switch (type) |
| 873 |
{ |
| 874 |
case MOWGLI_DNS_T_A: |
| 875 |
{ |
| 876 |
struct sockaddr_in *v4; |
| 877 |
|
| 878 |
if (request->type != MOWGLI_DNS_T_A) |
| 879 |
return 0; |
| 880 |
|
| 881 |
/* check for invalid rd_length or too many addresses */ |
| 882 |
if (rd_length != sizeof(struct in_addr)) |
| 883 |
return 0; |
| 884 |
|
| 885 |
v4 = (struct sockaddr_in *) &request->addr; |
| 886 |
v4->sin_family = AF_INET; |
| 887 |
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
| 888 |
|
| 889 |
return 1; |
| 890 |
} |
| 891 |
case MOWGLI_DNS_T_AAAA: |
| 892 |
{ |
| 893 |
struct sockaddr_in6 *v6; |
| 894 |
|
| 895 |
if (request->type != MOWGLI_DNS_T_AAAA) |
| 896 |
return 0; |
| 897 |
|
| 898 |
if (rd_length != sizeof(struct in6_addr)) |
| 899 |
return 0; |
| 900 |
|
| 901 |
v6 = (struct sockaddr_in6 *) &request->addr; |
| 902 |
v6->sin6_family = AF_INET6; |
| 903 |
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
| 904 |
|
| 905 |
return 1; |
| 906 |
} |
| 907 |
case MOWGLI_DNS_T_PTR: |
| 908 |
|
| 909 |
if (request->type != MOWGLI_DNS_T_PTR) |
| 910 |
return 0; |
| 911 |
|
| 912 |
n = mowgli_dns_dn_expand((unsigned char *) buf, (unsigned char *) eob, current, |
| 913 |
hostbuf, sizeof(hostbuf)); |
| 914 |
|
| 915 |
/* Broken message or no more answers left */ |
| 916 |
if (n <= 0) |
| 917 |
return 0; |
| 918 |
|
| 919 |
mowgli_strlcpy(request->name, hostbuf, MOWGLI_DNS_RES_HOSTLEN + 1); |
| 920 |
|
| 921 |
return 1; |
| 922 |
case MOWGLI_DNS_T_CNAME: |
| 923 |
|
| 924 |
/* real answer will follow */ |
| 925 |
current += rd_length; |
| 926 |
break; |
| 927 |
default: |
| 928 |
|
| 929 |
/* XXX I'd rather just throw away the entire bogus thing but its possible its just a |
| 930 |
* broken nameserver with still valid answers. But lets do some rudimentary logging for |
| 931 |
* now... */ |
| 932 |
mowgli_log("proc_answer(): bogus type %d", type); |
| 933 |
break; |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
return 1; |
| 938 |
} |
| 939 |
|
| 940 |
/* |
| 941 |
* res_read_single_reply - read a dns reply from the nameserver and process it. |
| 942 |
* Return value: 1 if a packet was read, 0 otherwise |
| 943 |
*/ |
| 944 |
static int |
| 945 |
res_read_single_reply(mowgli_dns_t *dns) |
| 946 |
{ |
| 947 |
char buf[sizeof(mowgli_dns_resheader_t) + MOWGLI_DNS_MAXPACKET] |
| 948 |
|
| 949 |
/* Sparc and alpha need 16bit-alignment for accessing header->id (which is uint16_t). |
| 950 |
Because of the header = (mowgli_dns_resheader_t*) buf; later on, this is neeeded. --FaUl */ |
| 951 |
#if defined(__sparc__) || defined(__alpha__) |
| 952 |
__attribute__((aligned(16))) |
| 953 |
#endif |
| 954 |
; |
| 955 |
mowgli_dns_resheader_t *header; |
| 956 |
mowgli_dns_reslist_t *request = NULL; |
| 957 |
mowgli_dns_reply_t *reply = NULL; |
| 958 |
int rc; |
| 959 |
int answer_count; |
| 960 |
mowgli_vio_sockaddr_t lsin = { .addrlen = sizeof(lsin.addr) }; |
| 961 |
mowgli_dns_evloop_t *state = dns->dns_state; |
| 962 |
|
| 963 |
rc = mowgli_vio_recvfrom(state->vio, buf, sizeof(buf), &lsin); |
| 964 |
|
| 965 |
/* No packet */ |
| 966 |
if ((rc == 0) || (rc == -1)) |
| 967 |
return 0; |
| 968 |
|
| 969 |
/* Too small */ |
| 970 |
if (rc <= (int) (sizeof(mowgli_dns_resheader_t))) |
| 971 |
return 1; |
| 972 |
|
| 973 |
/* |
| 974 |
* convert DNS reply reader from Network byte order to CPU byte order. |
| 975 |
*/ |
| 976 |
header = (mowgli_dns_resheader_t *) buf; |
| 977 |
header->ancount = ntohs(header->ancount); |
| 978 |
header->qdcount = ntohs(header->qdcount); |
| 979 |
header->nscount = ntohs(header->nscount); |
| 980 |
header->arcount = ntohs(header->arcount); |
| 981 |
|
| 982 |
/* response for an id which we have already received an answer for |
| 983 |
* just ignore this response. */ |
| 984 |
if ((request = find_id(dns, header->id)) == 0) |
| 985 |
return 1; |
| 986 |
|
| 987 |
/* check against possibly fake replies */ |
| 988 |
if (!res_ourserver(dns, &lsin.addr)) |
| 989 |
return 1; |
| 990 |
|
| 991 |
if (!check_question(request, header, buf, buf + rc)) |
| 992 |
return 1; |
| 993 |
|
| 994 |
if ((header->rcode != MOWGLI_DNS_NO_ERRORS) || (header->ancount == 0)) |
| 995 |
{ |
| 996 |
if (header->rcode == MOWGLI_DNS_NXDOMAIN) |
| 997 |
{ |
| 998 |
(*request->query->callback)(NULL, MOWGLI_DNS_RES_NXDOMAIN, request->query->ptr); |
| 999 |
rem_request(dns, request); |
| 1000 |
} |
| 1001 |
else |
| 1002 |
{ |
| 1003 |
/* |
| 1004 |
* If a bad error was returned, we stop here and dont send |
| 1005 |
* send any more (no retries granted). |
| 1006 |
*/ |
| 1007 |
(*request->query->callback)(NULL, MOWGLI_DNS_RES_INVALID, request->query->ptr); |
| 1008 |
rem_request(dns, request); |
| 1009 |
} |
| 1010 |
|
| 1011 |
return 1; |
| 1012 |
} |
| 1013 |
|
| 1014 |
/* If this fails there was an error decoding the received packet, |
| 1015 |
* give up. -- jilles |
| 1016 |
*/ |
| 1017 |
answer_count = proc_answer(request, header, buf, buf + rc); |
| 1018 |
|
| 1019 |
if (answer_count) |
| 1020 |
{ |
| 1021 |
if (request->type == MOWGLI_DNS_T_PTR) |
| 1022 |
{ |
| 1023 |
if (request->name == NULL) |
| 1024 |
{ |
| 1025 |
/* got a PTR response with no name, something bogus is happening |
| 1026 |
* don't bother trying again, the client address doesn't resolve |
| 1027 |
*/ |
| 1028 |
(*request->query->callback)(reply, MOWGLI_DNS_RES_INVALID, request->query->ptr); |
| 1029 |
rem_request(dns, request); |
| 1030 |
return 1; |
| 1031 |
} |
| 1032 |
|
| 1033 |
/* Lookup the 'authoritative' name that we were given for the |
| 1034 |
* ip#. */ |
| 1035 |
if (request->addr.ss_family == AF_INET6) |
| 1036 |
mowgli_dns_evloop_gethost_byname(dns, request->name, request->query, MOWGLI_DNS_T_AAAA); |
| 1037 |
else |
| 1038 |
mowgli_dns_evloop_gethost_byname(dns, request->name, request->query, MOWGLI_DNS_T_A); |
| 1039 |
|
| 1040 |
rem_request(dns, request); |
| 1041 |
} |
| 1042 |
else |
| 1043 |
{ |
| 1044 |
/* got a name and address response, client resolved */ |
| 1045 |
reply = make_dnsreply(request); |
| 1046 |
(*request->query->callback)(reply, MOWGLI_DNS_RES_SUCCESS, request->query->ptr); |
| 1047 |
mowgli_free(reply); |
| 1048 |
rem_request(dns, request); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
else |
| 1052 |
{ |
| 1053 |
/* couldn't decode, give up -- jilles */ |
| 1054 |
(*request->query->callback)(NULL, MOWGLI_DNS_RES_INVALID, request->query->ptr); |
| 1055 |
rem_request(dns, request); |
| 1056 |
} |
| 1057 |
|
| 1058 |
return 1; |
| 1059 |
} |
| 1060 |
|
| 1061 |
static mowgli_dns_reply_t * |
| 1062 |
make_dnsreply(mowgli_dns_reslist_t *request) |
| 1063 |
{ |
| 1064 |
mowgli_dns_reply_t *cp; |
| 1065 |
|
| 1066 |
return_val_if_fail(request != 0, NULL); |
| 1067 |
|
| 1068 |
cp = mowgli_alloc(sizeof *cp); |
| 1069 |
|
| 1070 |
cp->h_name = request->name; |
| 1071 |
memcpy(&cp->addr.addr, &request->addr, sizeof(request->addr)); |
| 1072 |
return cp; |
| 1073 |
} |
| 1074 |
|
| 1075 |
static void |
| 1076 |
res_readreply(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) |
| 1077 |
{ |
| 1078 |
mowgli_dns_t *dns = userdata; |
| 1079 |
|
| 1080 |
while (res_read_single_reply(dns)) |
| 1081 |
{ } |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* DNS ops for this resolver */ |
| 1085 |
const mowgli_dns_ops_t mowgli_dns_evloop_resolver = |
| 1086 |
{ |
| 1087 |
.mowgli_dns_init_func_t = mowgli_dns_evloop_init, |
| 1088 |
.mowgli_dns_fini_func_t = mowgli_dns_evloop_destroy, |
| 1089 |
.mowgli_dns_restart_func_t = mowgli_dns_evloop_restart, |
| 1090 |
.mowgli_dns_delete_query_func_t = mowgli_dns_evloop_delete_queries, |
| 1091 |
.mowgli_dns_gethost_byname_func_t = mowgli_dns_evloop_gethost_byname, |
| 1092 |
.mowgli_dns_gethost_byaddr_func_t = mowgli_dns_evloop_gethost_byaddr, |
| 1093 |
}; |