1 /* $OpenBSD: asr.c,v 1.58 2018/02/06 13:00:48 eric Exp $ */ 2 /* 3 * Copyright (c) 2010-2012 Eric Faurot <eric@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <sys/stat.h> 21 #include <netinet/in.h> 22 #include <arpa/inet.h> 23 #include <arpa/nameser.h> 24 #include <netdb.h> 25 26 #include <asr.h> 27 #include <errno.h> 28 #include <fcntl.h> 29 #include <resolv.h> 30 #include <poll.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <unistd.h> 35 #include <limits.h> 36 37 #include "asr_private.h" 38 39 #include "thread_private.h" 40 41 #define DEFAULT_CONF "lookup file\n" 42 #define DEFAULT_LOOKUP "lookup bind file" 43 44 #define RELOAD_DELAY 15 /* seconds */ 45 46 static void asr_check_reload(struct asr *); 47 static struct asr_ctx *asr_ctx_create(void); 48 static void asr_ctx_ref(struct asr_ctx *); 49 static void asr_ctx_free(struct asr_ctx *); 50 static int asr_ctx_add_searchdomain(struct asr_ctx *, const char *); 51 static int asr_ctx_from_file(struct asr_ctx *, const char *); 52 static int asr_ctx_from_string(struct asr_ctx *, const char *); 53 static int asr_ctx_parse(struct asr_ctx *, const char *); 54 static int asr_parse_nameserver(struct sockaddr *, const char *); 55 static int asr_ndots(const char *); 56 static void pass0(char **, int, struct asr_ctx *); 57 static int strsplit(char *, char **, int); 58 static void asr_ctx_envopts(struct asr_ctx *); 59 static void *__THREAD_NAME(_asr); 60 61 static struct asr *_asr = NULL; 62 63 /* Allocate and configure an async "resolver". */ 64 static void * 65 _asr_resolver(void) 66 { 67 static int init = 0; 68 struct asr *asr; 69 70 if (init == 0) { 71 #ifdef DEBUG 72 if (getenv("ASR_DEBUG")) 73 _asr_debug = stderr; 74 #endif 75 init = 1; 76 } 77 78 if ((asr = calloc(1, sizeof(*asr))) == NULL) 79 goto fail; 80 81 asr_check_reload(asr); 82 if (asr->a_ctx == NULL) { 83 if ((asr->a_ctx = asr_ctx_create()) == NULL) 84 goto fail; 85 if (asr_ctx_from_string(asr->a_ctx, DEFAULT_CONF) == -1) 86 goto fail; 87 asr_ctx_envopts(asr->a_ctx); 88 } 89 90 #ifdef DEBUG 91 _asr_dump_config(_asr_debug, asr); 92 #endif 93 return (asr); 94 95 fail: 96 if (asr) { 97 if (asr->a_ctx) 98 asr_ctx_free(asr->a_ctx); 99 free(asr); 100 } 101 102 return (NULL); 103 } 104 105 /* 106 * Free the "asr" async resolver (or the thread-local resolver if NULL). 107 * Drop the reference to the current context. 108 */ 109 void 110 _asr_resolver_done(void *arg) 111 { 112 struct asr *asr = arg; 113 struct asr **priv; 114 115 if (asr == NULL) { 116 priv = _THREAD_PRIVATE(_asr, _asr, &_asr); 117 if (*priv == NULL) 118 return; 119 asr = *priv; 120 *priv = NULL; 121 } 122 123 _asr_ctx_unref(asr->a_ctx); 124 free(asr); 125 } 126 127 /* 128 * Cancel an async query. 129 */ 130 void 131 asr_abort(struct asr_query *as) 132 { 133 _asr_async_free(as); 134 } 135 136 /* 137 * Resume the "as" async query resolution. Return one of ASYNC_COND, 138 * or ASYNC_DONE and put query-specific return values in the user-allocated 139 * memory at "ar". 140 */ 141 int 142 asr_run(struct asr_query *as, struct asr_result *ar) 143 { 144 int r, saved_errno = errno; 145 146 DPRINT("asr: asr_run(%p, %p) %s ctx=[%p]\n", as, ar, 147 _asr_querystr(as->as_type), as->as_ctx); 148 r = as->as_run(as, ar); 149 150 DPRINT("asr: asr_run(%p, %p) -> %s", as, ar, _asr_transitionstr(r)); 151 #ifdef DEBUG 152 if (r == ASYNC_COND) 153 #endif 154 DPRINT(" fd=%i timeout=%i", ar->ar_fd, ar->ar_timeout); 155 DPRINT("\n"); 156 if (r == ASYNC_DONE) 157 _asr_async_free(as); 158 159 errno = saved_errno; 160 161 return (r); 162 } 163 DEF_WEAK(asr_run); 164 165 /* 166 * Same as above, but run in a loop that handles the fd conditions result. 167 */ 168 169 static int 170 poll_intrsafe(struct pollfd *fds, nfds_t nfds, int timeout) 171 { 172 struct timespec pollstart, pollend, elapsed; 173 int r; 174 175 if (clock_gettime(CLOCK_MONOTONIC, &pollstart)) 176 return -1; 177 178 while ((r = poll(fds, 1, timeout)) == -1 && errno == EINTR) { 179 if (clock_gettime(CLOCK_MONOTONIC, &pollend)) 180 return -1; 181 timespecsub(&pollend, &pollstart, &elapsed); 182 timeout -= elapsed.tv_sec * 1000 + elapsed.tv_nsec / 1000000; 183 if (timeout < 1) 184 return 0; 185 } 186 187 return r; 188 } 189 190 int 191 asr_run_sync(struct asr_query *as, struct asr_result *ar) 192 { 193 struct pollfd fds[1]; 194 int r, saved_errno = errno; 195 196 while ((r = asr_run(as, ar)) == ASYNC_COND) { 197 fds[0].fd = ar->ar_fd; 198 fds[0].events = (ar->ar_cond == ASR_WANT_READ) ? POLLIN:POLLOUT; 199 200 if (poll_intrsafe(fds, 1, ar->ar_timeout) == -1) { 201 memset(ar, 0, sizeof(*ar)); 202 ar->ar_errno = errno; 203 ar->ar_h_errno = NETDB_INTERNAL; 204 ar->ar_gai_errno = EAI_SYSTEM; 205 ar->ar_rrset_errno = NETDB_INTERNAL; 206 _asr_async_free(as); 207 errno = saved_errno; 208 return ASYNC_DONE; 209 } 210 211 /* 212 * Otherwise, just ignore the error and let asr_run() 213 * catch the failure. 214 */ 215 } 216 217 errno = saved_errno; 218 219 return (r); 220 } 221 DEF_WEAK(asr_run_sync); 222 223 /* 224 * Create a new async request of the given "type" on the async context "ac". 225 * Take a reference on it so it does not gets deleted while the async query 226 * is running. 227 */ 228 struct asr_query * 229 _asr_async_new(struct asr_ctx *ac, int type) 230 { 231 struct asr_query *as; 232 233 DPRINT("asr: asr_async_new(ctx=%p) type=%i refcount=%i\n", ac, type, 234 ac ? ac->ac_refcount : 0); 235 if (ac == NULL || (as = calloc(1, sizeof(*as))) == NULL) 236 return (NULL); 237 238 ac->ac_refcount += 1; 239 as->as_ctx = ac; 240 as->as_fd = -1; 241 as->as_type = type; 242 as->as_state = ASR_STATE_INIT; 243 244 return (as); 245 } 246 247 /* 248 * Free an async query and unref the associated context. 249 */ 250 void 251 _asr_async_free(struct asr_query *as) 252 { 253 DPRINT("asr: asr_async_free(%p)\n", as); 254 255 if (as->as_subq) 256 _asr_async_free(as->as_subq); 257 258 switch (as->as_type) { 259 case ASR_SEND: 260 if (as->as_fd != -1) 261 close(as->as_fd); 262 if (as->as.dns.obuf && !(as->as_flags & ASYNC_EXTOBUF)) 263 free(as->as.dns.obuf); 264 if (as->as.dns.ibuf) 265 free(as->as.dns.ibuf); 266 if (as->as.dns.dname) 267 free(as->as.dns.dname); 268 break; 269 270 case ASR_SEARCH: 271 if (as->as.search.name) 272 free(as->as.search.name); 273 break; 274 275 case ASR_GETRRSETBYNAME: 276 if (as->as.rrset.name) 277 free(as->as.rrset.name); 278 break; 279 280 case ASR_GETHOSTBYNAME: 281 case ASR_GETHOSTBYADDR: 282 if (as->as.hostnamadr.name) 283 free(as->as.hostnamadr.name); 284 break; 285 286 case ASR_GETNETBYNAME: 287 case ASR_GETNETBYADDR: 288 if (as->as.netnamadr.name) 289 free(as->as.netnamadr.name); 290 break; 291 292 case ASR_GETADDRINFO: 293 if (as->as.ai.aifirst) 294 freeaddrinfo(as->as.ai.aifirst); 295 if (as->as.ai.hostname) 296 free(as->as.ai.hostname); 297 if (as->as.ai.servname) 298 free(as->as.ai.servname); 299 if (as->as.ai.fqdn) 300 free(as->as.ai.fqdn); 301 break; 302 303 case ASR_GETNAMEINFO: 304 break; 305 } 306 307 _asr_ctx_unref(as->as_ctx); 308 free(as); 309 } 310 311 /* 312 * Get a context from the given resolver. This takes a new reference to 313 * the returned context, which *must* be explicitely dropped when done 314 * using this context. 315 */ 316 struct asr_ctx * 317 _asr_use_resolver(void *arg) 318 { 319 struct asr *asr = arg; 320 struct asr **priv; 321 322 if (asr == NULL) { 323 DPRINT("using thread-local resolver\n"); 324 priv = _THREAD_PRIVATE(_asr, _asr, &_asr); 325 if (*priv == NULL) { 326 DPRINT("setting up thread-local resolver\n"); 327 *priv = _asr_resolver(); 328 } 329 asr = *priv; 330 } 331 if (asr != NULL) { 332 asr_check_reload(asr); 333 asr_ctx_ref(asr->a_ctx); 334 return (asr->a_ctx); 335 } 336 return (NULL); 337 } 338 339 static void 340 asr_ctx_ref(struct asr_ctx *ac) 341 { 342 DPRINT("asr: asr_ctx_ref(ctx=%p) refcount=%i\n", ac, ac->ac_refcount); 343 ac->ac_refcount += 1; 344 } 345 346 /* 347 * Drop a reference to an async context, freeing it if the reference 348 * count drops to 0. 349 */ 350 void 351 _asr_ctx_unref(struct asr_ctx *ac) 352 { 353 DPRINT("asr: asr_ctx_unref(ctx=%p) refcount=%i\n", ac, 354 ac ? ac->ac_refcount : 0); 355 if (ac == NULL) 356 return; 357 if (--ac->ac_refcount) 358 return; 359 360 asr_ctx_free(ac); 361 } 362 363 static void 364 asr_ctx_free(struct asr_ctx *ac) 365 { 366 int i; 367 368 if (ac->ac_domain) 369 free(ac->ac_domain); 370 for (i = 0; i < ASR_MAXNS; i++) 371 free(ac->ac_ns[i]); 372 for (i = 0; i < ASR_MAXDOM; i++) 373 free(ac->ac_dom[i]); 374 375 free(ac); 376 } 377 378 /* 379 * Reload the configuration file if it has changed on disk. 380 */ 381 static void 382 asr_check_reload(struct asr *asr) 383 { 384 struct asr_ctx *ac; 385 struct stat st; 386 struct timespec ts; 387 pid_t pid; 388 389 pid = getpid(); 390 if (pid != asr->a_pid) { 391 asr->a_pid = pid; 392 asr->a_rtime = 0; 393 } 394 395 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) 396 return; 397 398 if ((ts.tv_sec - asr->a_rtime) < RELOAD_DELAY && asr->a_rtime != 0) 399 return; 400 asr->a_rtime = ts.tv_sec; 401 402 DPRINT("asr: checking for update of \"%s\"\n", _PATH_RESCONF); 403 if (stat(_PATH_RESCONF, &st) == -1 || 404 asr->a_mtime == st.st_mtime || 405 (ac = asr_ctx_create()) == NULL) 406 return; 407 asr->a_mtime = st.st_mtime; 408 409 DPRINT("asr: reloading config file\n"); 410 if (asr_ctx_from_file(ac, _PATH_RESCONF) == -1) { 411 asr_ctx_free(ac); 412 return; 413 } 414 415 asr_ctx_envopts(ac); 416 if (asr->a_ctx) 417 _asr_ctx_unref(asr->a_ctx); 418 asr->a_ctx = ac; 419 } 420 421 /* 422 * Construct a fully-qualified domain name for the given name and domain. 423 * If "name" ends with a '.' it is considered as a FQDN by itself. 424 * Otherwise, the domain, which must be a FQDN, is appended to "name" (it 425 * may have a leading dot which would be ignored). If the domain is null, 426 * then "." is used. Return the length of the constructed FQDN or (0) on 427 * error. 428 */ 429 size_t 430 _asr_make_fqdn(const char *name, const char *domain, char *buf, size_t buflen) 431 { 432 size_t len; 433 434 if (domain == NULL) 435 domain = "."; 436 else if ((len = strlen(domain)) == 0) 437 return (0); 438 else if (domain[len -1] != '.') 439 return (0); 440 441 len = strlen(name); 442 if (len == 0) { 443 if (strlcpy(buf, domain, buflen) >= buflen) 444 return (0); 445 } else if (name[len - 1] != '.') { 446 if (domain[0] == '.') 447 domain += 1; 448 if (strlcpy(buf, name, buflen) >= buflen || 449 strlcat(buf, ".", buflen) >= buflen || 450 strlcat(buf, domain, buflen) >= buflen) 451 return (0); 452 } else { 453 if (strlcpy(buf, name, buflen) >= buflen) 454 return (0); 455 } 456 457 return (strlen(buf)); 458 } 459 460 /* 461 * Count the dots in a string. 462 */ 463 static int 464 asr_ndots(const char *s) 465 { 466 int n; 467 468 for (n = 0; *s; s++) 469 if (*s == '.') 470 n += 1; 471 472 return (n); 473 } 474 475 /* 476 * Allocate a new empty context. 477 */ 478 static struct asr_ctx * 479 asr_ctx_create(void) 480 { 481 struct asr_ctx *ac; 482 483 if ((ac = calloc(1, sizeof(*ac))) == NULL) 484 return (NULL); 485 486 ac->ac_options = RES_RECURSE | RES_DEFNAMES | RES_DNSRCH; 487 ac->ac_refcount = 1; 488 ac->ac_ndots = 1; 489 ac->ac_family[0] = AF_INET; 490 ac->ac_family[1] = AF_INET6; 491 ac->ac_family[2] = -1; 492 493 ac->ac_nscount = 0; 494 ac->ac_nstimeout = 5; 495 ac->ac_nsretries = 4; 496 497 return (ac); 498 } 499 500 struct asr_ctx * 501 _asr_no_resolver(void) 502 { 503 return asr_ctx_create(); 504 } 505 506 /* 507 * Add a search domain to the async context. 508 */ 509 static int 510 asr_ctx_add_searchdomain(struct asr_ctx *ac, const char *domain) 511 { 512 char buf[MAXDNAME]; 513 514 if (ac->ac_domcount == ASR_MAXDOM) 515 return (-1); 516 517 if (_asr_make_fqdn(domain, NULL, buf, sizeof(buf)) == 0) 518 return (-1); 519 520 if ((ac->ac_dom[ac->ac_domcount] = strdup(buf)) == NULL) 521 return (0); 522 523 ac->ac_domcount += 1; 524 525 return (1); 526 } 527 528 static int 529 strsplit(char *line, char **tokens, int ntokens) 530 { 531 int ntok; 532 char *cp, **tp; 533 534 for (cp = line, tp = tokens, ntok = 0; 535 ntok < ntokens && (*tp = strsep(&cp, " \t")) != NULL; ) 536 if (**tp != '\0') { 537 tp++; 538 ntok++; 539 } 540 541 return (ntok); 542 } 543 544 /* 545 * Pass on a split config line. 546 */ 547 static void 548 pass0(char **tok, int n, struct asr_ctx *ac) 549 { 550 int i, j, d; 551 const char *e; 552 struct sockaddr_storage ss; 553 554 if (!strcmp(tok[0], "nameserver")) { 555 if (ac->ac_nscount == ASR_MAXNS) 556 return; 557 if (n != 2) 558 return; 559 if (asr_parse_nameserver((struct sockaddr *)&ss, tok[1])) 560 return; 561 if ((ac->ac_ns[ac->ac_nscount] = calloc(1, ss.ss_len)) == NULL) 562 return; 563 memmove(ac->ac_ns[ac->ac_nscount], &ss, ss.ss_len); 564 ac->ac_nscount += 1; 565 566 } else if (!strcmp(tok[0], "domain")) { 567 if (n != 2) 568 return; 569 if (ac->ac_domain) 570 return; 571 ac->ac_domain = strdup(tok[1]); 572 573 } else if (!strcmp(tok[0], "lookup")) { 574 /* ensure that each lookup is only given once */ 575 for (i = 1; i < n; i++) 576 for (j = i + 1; j < n; j++) 577 if (!strcmp(tok[i], tok[j])) 578 return; 579 ac->ac_dbcount = 0; 580 for (i = 1; i < n && ac->ac_dbcount < ASR_MAXDB; i++) { 581 if (!strcmp(tok[i], "yp")) { 582 /* silently deprecated */ 583 } else if (!strcmp(tok[i], "bind")) 584 ac->ac_db[ac->ac_dbcount++] = ASR_DB_DNS; 585 else if (!strcmp(tok[i], "file")) 586 ac->ac_db[ac->ac_dbcount++] = ASR_DB_FILE; 587 } 588 } else if (!strcmp(tok[0], "search")) { 589 /* resolv.conf says the last line wins */ 590 for (i = 0; i < ASR_MAXDOM; i++) { 591 free(ac->ac_dom[i]); 592 ac->ac_dom[i] = NULL; 593 } 594 ac->ac_domcount = 0; 595 for (i = 1; i < n; i++) 596 asr_ctx_add_searchdomain(ac, tok[i]); 597 598 } else if (!strcmp(tok[0], "family")) { 599 if (n == 1 || n > 3) 600 return; 601 for (i = 1; i < n; i++) 602 if (strcmp(tok[i], "inet4") && strcmp(tok[i], "inet6")) 603 return; 604 for (i = 1; i < n; i++) 605 ac->ac_family[i - 1] = strcmp(tok[i], "inet4") ? \ 606 AF_INET6 : AF_INET; 607 ac->ac_family[i - 1] = -1; 608 609 } else if (!strcmp(tok[0], "options")) { 610 for (i = 1; i < n; i++) { 611 if (!strcmp(tok[i], "tcp")) 612 ac->ac_options |= RES_USEVC; 613 else if (!strcmp(tok[i], "edns0")) 614 ac->ac_options |= RES_USE_EDNS0; 615 else if ((!strncmp(tok[i], "ndots:", 6))) { 616 e = NULL; 617 d = strtonum(tok[i] + 6, 1, 16, &e); 618 if (e == NULL) 619 ac->ac_ndots = d; 620 } 621 } 622 } 623 } 624 625 /* 626 * Setup an async context with the config specified in the string "str". 627 */ 628 static int 629 asr_ctx_from_string(struct asr_ctx *ac, const char *str) 630 { 631 char buf[512], *ch; 632 633 asr_ctx_parse(ac, str); 634 635 if (ac->ac_dbcount == 0) { 636 /* No lookup directive */ 637 asr_ctx_parse(ac, DEFAULT_LOOKUP); 638 } 639 640 if (ac->ac_nscount == 0) 641 asr_ctx_parse(ac, "nameserver 127.0.0.1"); 642 643 if (ac->ac_domain == NULL) 644 if (gethostname(buf, sizeof buf) == 0) { 645 ch = strchr(buf, '.'); 646 if (ch) 647 ac->ac_domain = strdup(ch + 1); 648 else /* Assume root. see resolv.conf(5) */ 649 ac->ac_domain = strdup(""); 650 } 651 652 /* If no search domain was specified, use the local subdomains */ 653 if (ac->ac_domcount == 0) 654 for (ch = ac->ac_domain; ch; ) { 655 asr_ctx_add_searchdomain(ac, ch); 656 ch = strchr(ch, '.'); 657 if (ch && asr_ndots(++ch) == 0) 658 break; 659 } 660 661 return (0); 662 } 663 664 /* 665 * Setup the "ac" async context from the file at location "path". 666 */ 667 static int 668 asr_ctx_from_file(struct asr_ctx *ac, const char *path) 669 { 670 FILE *cf; 671 char buf[4096]; 672 ssize_t r; 673 674 cf = fopen(path, "re"); 675 if (cf == NULL) 676 return (-1); 677 678 r = fread(buf, 1, sizeof buf - 1, cf); 679 if (feof(cf) == 0) { 680 DPRINT("asr: config file too long: \"%s\"\n", path); 681 r = -1; 682 } 683 fclose(cf); 684 if (r == -1) 685 return (-1); 686 buf[r] = '\0'; 687 688 return asr_ctx_from_string(ac, buf); 689 } 690 691 /* 692 * Parse lines in the configuration string. For each one, split it into 693 * tokens and pass them to "pass0" for processing. 694 */ 695 static int 696 asr_ctx_parse(struct asr_ctx *ac, const char *str) 697 { 698 size_t len; 699 const char *line; 700 char buf[1024]; 701 char *tok[10]; 702 int ntok; 703 704 line = str; 705 while (*line) { 706 len = strcspn(line, "\n\0"); 707 if (len < sizeof buf) { 708 memmove(buf, line, len); 709 buf[len] = '\0'; 710 } else 711 buf[0] = '\0'; 712 line += len; 713 if (*line == '\n') 714 line++; 715 buf[strcspn(buf, ";#")] = '\0'; 716 if ((ntok = strsplit(buf, tok, 10)) == 0) 717 continue; 718 719 pass0(tok, ntok, ac); 720 } 721 722 return (0); 723 } 724 725 /* 726 * Check for environment variables altering the configuration as described 727 * in resolv.conf(5). Although not documented there, this feature is disabled 728 * for setuid/setgid programs. 729 */ 730 static void 731 asr_ctx_envopts(struct asr_ctx *ac) 732 { 733 char buf[4096], *e; 734 size_t s; 735 736 if (issetugid()) { 737 ac->ac_options |= RES_NOALIASES; 738 return; 739 } 740 741 if ((e = getenv("RES_OPTIONS")) != NULL) { 742 strlcpy(buf, "options ", sizeof buf); 743 strlcat(buf, e, sizeof buf); 744 s = strlcat(buf, "\n", sizeof buf); 745 if (s < sizeof buf) 746 asr_ctx_parse(ac, buf); 747 } 748 749 if ((e = getenv("LOCALDOMAIN")) != NULL) { 750 strlcpy(buf, "search ", sizeof buf); 751 strlcat(buf, e, sizeof buf); 752 s = strlcat(buf, "\n", sizeof buf); 753 if (s < sizeof buf) 754 asr_ctx_parse(ac, buf); 755 } 756 } 757 758 /* 759 * Parse a resolv.conf(5) nameserver string into a sockaddr. 760 */ 761 static int 762 asr_parse_nameserver(struct sockaddr *sa, const char *s) 763 { 764 in_port_t portno = 53; 765 766 if (_asr_sockaddr_from_str(sa, PF_UNSPEC, s) == -1) 767 return (-1); 768 769 if (sa->sa_family == PF_INET) 770 ((struct sockaddr_in *)sa)->sin_port = htons(portno); 771 else if (sa->sa_family == PF_INET6) 772 ((struct sockaddr_in6 *)sa)->sin6_port = htons(portno); 773 774 return (0); 775 } 776 777 /* 778 * Turn a (uncompressed) DNS domain name into a regular nul-terminated string 779 * where labels are separated by dots. The result is put into the "buf" buffer, 780 * truncated if it exceeds "max" chars. The function returns "buf". 781 */ 782 char * 783 _asr_strdname(const char *_dname, char *buf, size_t max) 784 { 785 const unsigned char *dname = _dname; 786 char *res; 787 size_t left, n, count; 788 789 if (_dname[0] == 0) { 790 strlcpy(buf, ".", max); 791 return buf; 792 } 793 794 res = buf; 795 left = max - 1; 796 for (n = 0; dname[0] && left; n += dname[0]) { 797 count = (dname[0] < (left - 1)) ? dname[0] : (left - 1); 798 memmove(buf, dname + 1, count); 799 dname += dname[0] + 1; 800 left -= count; 801 buf += count; 802 if (left) { 803 left -= 1; 804 *buf++ = '.'; 805 } 806 } 807 buf[0] = 0; 808 809 return (res); 810 } 811 812 /* 813 * Read and split the next line from the given namedb file. 814 * Return -1 on error, or put the result in the "tokens" array of 815 * size "ntoken" and returns the number of token on the line. 816 */ 817 int 818 _asr_parse_namedb_line(FILE *file, char **tokens, int ntoken, char *lbuf, size_t sz) 819 { 820 size_t len; 821 char *buf; 822 int ntok; 823 824 again: 825 if ((buf = fgetln(file, &len)) == NULL) 826 return (-1); 827 828 if (len >= sz) 829 goto again; 830 831 if (buf[len - 1] == '\n') 832 len--; 833 else { 834 memcpy(lbuf, buf, len); 835 buf = lbuf; 836 } 837 838 buf[len] = '\0'; 839 buf[strcspn(buf, "#")] = '\0'; 840 if ((ntok = strsplit(buf, tokens, ntoken)) == 0) 841 goto again; 842 843 return (ntok); 844 } 845 846 /* 847 * Update the async context so that it uses the next configured DB. 848 * Return 0 on success, or -1 if no more DBs is available. 849 */ 850 int 851 _asr_iter_db(struct asr_query *as) 852 { 853 if (as->as_db_idx >= as->as_ctx->ac_dbcount) { 854 DPRINT("asr_iter_db: done\n"); 855 return (-1); 856 } 857 858 as->as_db_idx += 1; 859 DPRINT("asr_iter_db: %i\n", as->as_db_idx); 860 861 return (0); 862 } 863