1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
|
#ifndef lint
static char *rcsid = "$Id: resolver.c,v 1.9.2.3 2002/01/25 08:03:10 matoi Exp $";
#endif
/*
* Copyright (c) 2001 Japan Network Information Center. All rights reserved.
*
* By using this file, you agree to the terms and conditions set forth bellow.
*
* LICENSE TERMS AND CONDITIONS
*
* The following License Terms and Conditions apply, unless a different
* license is obtained from Japan Network Information Center ("JPNIC"),
* a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
* Chiyoda-ku, Tokyo 101-0047, Japan.
*
* 1. Use, Modification and Redistribution (including distribution of any
* modified or derived work) in source and/or binary forms is permitted
* under this License Terms and Conditions.
*
* 2. Redistribution of source code must retain the copyright notices as they
* appear in each source code file, this License Terms and Conditions.
*
* 3. Redistribution in binary form must reproduce the Copyright Notice,
* this License Terms and Conditions, in the documentation and/or other
* materials provided with the distribution. For the purposes of binary
* distribution the "Copyright Notice" refers to the following language:
* "Copyright (c) Japan Network Information Center. All rights reserved."
*
* 4. Neither the name of JPNIC may be used to endorse or promote products
* derived from this Software without specific prior written approval of
* JPNIC.
*
* 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* 6. Indemnification by Licensee
* Any person or entities using and/or redistributing this Software under
* this License Terms and Conditions shall defend indemnify and hold
* harmless JPNIC from and against any and all judgements damages,
* expenses, settlement liabilities, cost and other liabilities of any
* kind as a result of use and redistribution of this Software or any
* claim, suite, action, litigation or proceeding by any third party
* arising out of or relates to this License Terms and Conditions.
*
* 7. Governing Law, Jurisdiction and Venue
* This License Terms and Conditions shall be governed by and and
* construed in accordance with the law of Japan. Any person or entities
* using and/or redistributing this Software under this License Terms and
* Conditions hereby agrees and consent to the personal and exclusive
* jurisdiction and venue of Tokyo District Court of Japan.
*/
#include <config.h>
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <mdn/api.h>
#include <mdn/log.h>
#include <mdn/logmacro.h>
#include <mdn/debug.h>
#ifdef FOR_RUNMDN
/*
* This file is specially compiled for runmdn.
* Runmdn replaces existing resolver functions dynamically with ones
* with MDN processing (encoding conversion and normalization).
* So entry names must be same as the system's one.
*/
#include "stub.h"
#define ENTRY(name) name
#define REAL(name) mdn_stub_ ## name
#else
/*
* For normal use. All the entry names are prefixed with "mdn_resolver_".
* <mdn/resolver.h> has bunch of #defines to substitute the standard
* name resolver functions with ones provided here.
*/
#include <mdn/resolver.h>
#undef gethostbyname
#undef gethostbyname2
#undef gethostbyaddr
#undef gethostbyname_r
#undef gethostbyname2_r
#undef gethostbyaddr_r
#undef getipnodebyname
#undef getipnodebyaddr
#undef getaddrinfo
#undef getnameinfo
#define ENTRY(name) mdn_resolver_ ## name
#define REAL(name) name
#endif
#define MDN_NAME_SIZE 512
#define MDN_HOSTBUF_SIZE 2048
typedef union {
char *dummy_for_alignment;
char data[MDN_HOSTBUF_SIZE];
} hostbuf_t;
typedef struct obj_lock {
void *key;
struct obj_lock *next;
} obj_lock_t;
#define OBJLOCKHASH_SIZE 127
static obj_lock_t *obj_lock_hash[OBJLOCKHASH_SIZE];
/*
* This variable is to prevent MDN processing occuring more than once for
* a single name resolution. This will happen if some resolver function
* is implemented using another function (e.g. gethostbyname() implemented
* using gethostbyname2()).
* No, using the static variable is not a correct thing to do for a multi-
* threading environment, but I don't think of a better solution..
*/
static int mdn_isprocessing = 0;
static int obj_hash(void *key);
static int obj_islocked(void *key);
static void obj_lock(void *key);
static void obj_unlock(void *key);
static struct hostent *copy_decode_hostent_static(struct hostent *hp,
struct hostent *newhp,
char *buf, size_t buflen,
int *errp);
static char *decode_name_dynamic(const char *name);
static struct hostent *copy_decode_hostent_dynamic(struct hostent *hp,
int *errp);
static void free_copied_hostent(struct hostent *hp);
#ifdef HAVE_GETADDRINFO
static struct addrinfo *copy_decode_addrinfo_dynamic(struct addrinfo *aip);
#endif
#ifdef HAVE_FREEADDRINFO
static void free_copied_addrinfo(struct addrinfo *aip);
#endif
/*
* Object locking facility.
*/
static int
obj_hash(void *key) {
/*
* Hash function for obj_*.
* 'key' is supposed to be an address.
*/
unsigned long v = (unsigned long)key;
return ((v >> 3) % OBJLOCKHASH_SIZE);
}
static int
obj_islocked(void *key)
{
/*
* Check if the object specified by 'key' is locked.
* Return 1 if so, 0 otherwise.
*/
int h = obj_hash(key);
obj_lock_t *olp = obj_lock_hash[h];
while (olp != NULL) {
if (olp->key == key)
return (1);
olp = olp->next;
}
return (0);
}
static void
obj_lock(void *key)
{
/*
* Lock an object specified by 'key'.
*/
int h = obj_hash(key);
obj_lock_t *olp;
olp = malloc(sizeof(obj_lock_t));
if (olp != NULL) {
olp->key = key;
olp->next = obj_lock_hash[h];
obj_lock_hash[h] = olp;
}
}
static void
obj_unlock(void *key)
{
/*
* Unlock an object specified by 'key'.
*/
int h = obj_hash(key);
obj_lock_t *olp, *olp0;
olp = obj_lock_hash[h];
olp0 = NULL;
while (olp != NULL) {
if (olp->key == key) {
if (olp0 == NULL)
obj_lock_hash[h] = olp->next;
else
olp0->next = olp->next;
free(olp);
return;
}
olp0 = olp;
olp = olp->next;
}
}
static struct hostent *
copy_decode_hostent_static(struct hostent *hp, struct hostent *newhp,
char *buf, size_t buflen, int *errp)
{
/*
* Copy "struct hostent" data referenced by 'hp' to 'newhp'.
* It's a deep-copy, meaning all the data referenced by 'hp' are
* also copied. They are copied into 'buf', whose length is 'buflen'.
* The domain names ('hp->h_name' and 'hp->h_aliases') are
* decoded from ACE to the local encoding before they are copied.
* If 'buf' is too small to hold all the data, NULL will be
* returned and '*errp' is set to NO_RECOVERY.
*/
int naliases = 0;
int naddrs = 0;
if (hp == NULL)
return (NULL);
*newhp = *hp;
if (hp->h_aliases != NULL) {
/*
* Allocate aliase table in 'buf'.
*/
size_t sz;
while (hp->h_aliases[naliases] != NULL)
naliases++;
newhp->h_aliases = (char **)buf;
sz = sizeof(char *) * (naliases + 1);
if (buflen < sz)
goto overflow;
buf += sz;
buflen -= sz;
}
if (hp->h_addr_list != NULL) {
/*
* Allocate address table in 'buf'.
*/
size_t sz;
int i;
while (hp->h_addr_list[naddrs] != NULL)
naddrs++;
newhp->h_addr_list = (char **)buf;
sz = sizeof(char *) * (naddrs + 1);
if (buflen < sz)
goto overflow;
buf += sz;
buflen -= sz;
/*
* Copy the addresses.
*/
sz = hp->h_length * naddrs;
if (buflen < sz)
goto overflow;
for (i = 0; i < naddrs; i++) {
newhp->h_addr_list[i] = buf;
memcpy(buf, hp->h_addr_list[i], hp->h_length);
buf += hp->h_length;
}
newhp->h_addr_list[naddrs] = NULL;
buf += sz;
buflen -= sz;
}
if (hp->h_name != NULL) {
/*
* Decode the name in h_name.
*/
mdn_result_t r;
size_t slen;
mdn_enable(1);
r = mdn_decodename(MDN_DECODE_APP, hp->h_name,
buf, buflen);
switch (r) {
case mdn_success:
newhp->h_name = buf;
break;
default:
/* Copy hp->h_name verbatim. */
if (strlen(hp->h_name) + 1 <= buflen) {
newhp->h_name = buf;
strcpy(buf, hp->h_name);
break;
}
/* falllthrough */
case mdn_buffer_overflow:
goto overflow;
}
slen = strlen(buf) + 1;
buf += slen;
buflen -= slen;
}
if (hp->h_aliases != NULL) {
/*
* Decode the names in h_aliases.
*/
char **aliases = hp->h_aliases;
char **newaliases = newhp->h_aliases;
int i;
for (i = 0; i < naliases; i++) {
mdn_result_t r;
size_t slen;
mdn_enable(1);
r = mdn_decodename(MDN_DECODE_APP, aliases[i],
buf, buflen);
switch (r) {
case mdn_success:
newaliases[i] = buf;
break;
default:
/* Copy hp->h_name verbatim. */
if (strlen(aliases[i]) + 1 <= buflen) {
newaliases[i] = buf;
strcpy(buf, aliases[i]);
break;
}
/* falllthrough */
case mdn_buffer_overflow:
goto overflow;
}
slen = strlen(buf) + 1;
buf += slen;
buflen -= slen;
}
newaliases[naliases] = NULL;
}
return (newhp);
overflow:
*errp = NO_RECOVERY;
return (NULL);
}
static char *
decode_name_dynamic(const char *name) {
mdn_result_t r;
char buf[MDN_NAME_SIZE];
char *s;
mdn_enable(1);
r = mdn_decodename(MDN_DECODE_APP, name, buf, sizeof(buf));
if (r == mdn_success) {
name = buf;
}
s = malloc(strlen(name) + 1);
if (s == NULL)
return (NULL);
else
return (strcpy(s, name));
}
static struct hostent *
copy_decode_hostent_dynamic(struct hostent *hp, int *errp) {
/*
* Make a deep-copy of the data referenced by 'hp', and return
* a pointer to the copied data.
* All the data are dynamically allocated using malloc().
* The domain names ('hp->h_name' and 'hp->h_aliases') are
* decoded from ACE to the local encoding before they are copied.
* If malloc() fails, NULL will be returned and '*errp' is set to
* NO_RECOVERY.
*/
struct hostent *newhp;
char **pp;
size_t alloc_size;
int naliases = 0;
int naddrs = 0;
int i;
if (hp == NULL)
return (NULL);
if (hp->h_aliases != NULL) {
while (hp->h_aliases[naliases] != NULL)
naliases++;
}
if (hp->h_addr_list != NULL) {
while (hp->h_addr_list[naddrs] != NULL)
naddrs++;
}
alloc_size = sizeof(struct hostent) +
sizeof(char *) * (naliases + 1) +
sizeof(char *) * (naddrs + 1) +
hp->h_length * naddrs;
if ((newhp = malloc(alloc_size)) == NULL) {
return (hp);
}
memset(newhp, 0, alloc_size);
pp = (char **)(newhp + 1);
if (hp->h_name != NULL) {
newhp->h_name = decode_name_dynamic(hp->h_name);
if (newhp->h_name == NULL)
goto alloc_fail;
}
newhp->h_addrtype = hp->h_addrtype;
newhp->h_length = hp->h_length;
if (hp->h_aliases != NULL) {
newhp->h_aliases = pp;
for (i = 0; i < naliases; i++) {
newhp->h_aliases[i] =
decode_name_dynamic(hp->h_aliases[i]);
if (newhp->h_aliases[i] == NULL)
goto alloc_fail;
}
newhp->h_aliases[naliases] = NULL;
pp += naliases + 1;
}
if (hp->h_addr_list != NULL) {
char *p;
newhp->h_addr_list = pp;
pp += naddrs + 1;
p = (char *)pp;
for (i = 0; i < naddrs; i++) {
newhp->h_addr_list[i] = p;
memcpy(p, hp->h_addr_list[i], hp->h_length);
p += hp->h_length;
}
newhp->h_addr_list[naddrs] = NULL;
}
return (newhp);
alloc_fail:
free_copied_hostent(hp);
*errp = NO_RECOVERY;
return (NULL);
}
static void
free_copied_hostent(struct hostent *hp) {
/*
* Free all the memory allocated by copy_decode_hostent_dynamic().
*/
if (hp->h_name != NULL)
free(hp->h_name);
if (hp->h_aliases != NULL) {
char **pp = hp->h_aliases;
while (*pp != NULL)
free(*pp++);
}
free(hp);
}
#ifdef HAVE_GETNAMEINFO
static struct addrinfo *
copy_decode_addrinfo_dynamic(struct addrinfo *aip) {
struct addrinfo *newaip;
if (aip == NULL)
return (NULL);
newaip = malloc(sizeof(struct addrinfo) + aip->ai_addrlen);
if (newaip == NULL)
return (NULL);
*newaip = *aip;
newaip->ai_addr = (struct sockaddr *)(newaip + 1);
memcpy(newaip->ai_addr, aip->ai_addr, aip->ai_addrlen);
if (newaip->ai_canonname != NULL)
newaip->ai_canonname = decode_name_dynamic(aip->ai_canonname);
newaip->ai_next = copy_decode_addrinfo_dynamic(aip->ai_next);
return (newaip);
}
#endif
#ifdef HAVE_FREEADDRINFO
static void
free_copied_addrinfo(struct addrinfo *aip) {
while (aip != NULL) {
struct addrinfo *next = aip->ai_next;
if (aip->ai_canonname != NULL)
free(aip->ai_canonname);
free(aip);
aip = next;
}
}
#endif
#ifdef HAVE_GETHOSTBYNAME
struct hostent *
ENTRY(gethostbyname)(const char *name) {
static hostbuf_t buf;
static struct hostent he;
mdn_result_t r;
struct hostent *hp;
if (mdn_isprocessing)
return (REAL(gethostbyname)(name));
TRACE(("gethostbyname(name=%s)\n", mdn_debug_xstring(name, 60)));
mdn_isprocessing = 1;
mdn_enable(1);
r = mdn_encodename(MDN_ENCODE_APP, name, buf.data, sizeof(buf));
if (r == mdn_success)
name = buf.data;
hp = copy_decode_hostent_static(REAL(gethostbyname)(name),
&he, buf.data, sizeof(buf),
&h_errno);
mdn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_GETHOSTBYNAME2
struct hostent *
ENTRY(gethostbyname2)(const char *name, int af) {
static hostbuf_t buf;
static struct hostent he;
mdn_result_t r;
struct hostent *hp;
if (mdn_isprocessing)
return (REAL(gethostbyname2)(name, af));
TRACE(("gethostbyname2(name=%s)\n", mdn_debug_xstring(name, 60), af));
mdn_isprocessing = 1;
mdn_enable(1);
r = mdn_encodename(MDN_ENCODE_APP, name, buf.data, sizeof(buf));
if (r == mdn_success)
name = buf.data;
hp = copy_decode_hostent_static(REAL(gethostbyname2)(name, af),
&he, buf.data, sizeof(buf),
&h_errno);
mdn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_GETHOSTBYADDR
struct hostent *
ENTRY(gethostbyaddr)(GHBA_ADDR_T addr, GHBA_ADDRLEN_T len, int type) {
static hostbuf_t buf;
static struct hostent he;
struct hostent *hp;
if (mdn_isprocessing)
return (REAL(gethostbyaddr)(addr, len, type));
TRACE(("gethostbyaddr()\n"));
mdn_isprocessing = 1;
hp = copy_decode_hostent_static(REAL(gethostbyaddr)(addr, len, type),
&he, buf.data, sizeof(buf),
&h_errno);
mdn_isprocessing = 0;
return (hp);
}
#endif
#ifdef GETHOST_R_GLIBC_FLAVOR
#ifdef HAVE_GETHOSTBYNAME_R
int
ENTRY(gethostbyname_r)(const char *name, struct hostent *result,
char *buffer, size_t buflen,
struct hostent **rp, int *errp)
{
char namebuf[MDN_NAME_SIZE];
char *data;
size_t datalen;
mdn_result_t r;
struct hostent he;
hostbuf_t buf;
int n;
if (mdn_isprocessing)
return (REAL(gethostbyname_r)(name, result, buffer,
buflen, rp, errp));
TRACE(("gethostbyname_r(name=%s,buflen=%d)\n",
mdn_debug_xstring(name, 60), buflen));
if (buflen <= sizeof(buf)) {
data = buf.data;
datalen = sizeof(buf);
} else {
data = malloc(buflen);
datalen = buflen;
if (data == NULL) {
*errp = NO_RECOVERY;
return (ENOMEM);
}
}
mdn_isprocessing = 1;
mdn_enable(1);
r = mdn_encodename(MDN_ENCODE_APP, name, namebuf, sizeof(namebuf));
if (r == mdn_success)
name = namebuf;
*errp = 0;
n = REAL(gethostbyname_r)(name, &he, data, datalen, rp, errp);
if (n == 0 && *rp != NULL)
*rp = copy_decode_hostent_static(*rp, result, buffer, buflen,
errp);
mdn_isprocessing = 0;
if (data != buf.data)
free(data);
if (*errp != 0)
n = EINVAL; /* XXX */
return (n);
}
#endif
#ifdef HAVE_GETHOSTBYNAME2_R
int
ENTRY(gethostbyname2_r)(const char *name, int af, struct hostent *result,
char *buffer, size_t buflen,
struct hostent **rp, int *errp)
{
char namebuf[MDN_NAME_SIZE];
char *data;
size_t datalen;
mdn_result_t r;
struct hostent he;
hostbuf_t buf;
int n;
if (mdn_isprocessing)
return (REAL(gethostbyname2_r)(name, af, result, buffer,
buflen, rp, errp));
TRACE(("gethostbyname2_r(name=%s,buflen=%d)\n",
mdn_debug_xstring(name, 60), buflen));
if (buflen <= sizeof(buf)) {
data = buf.data;
datalen = sizeof(buf);
} else {
data = malloc(buflen);
datalen = buflen;
if (data == NULL) {
*errp = NO_RECOVERY;
return (ENOMEM);
}
}
mdn_isprocessing = 1;
mdn_enable(1);
r = mdn_encodename(MDN_ENCODE_APP, name, namebuf, sizeof(namebuf));
if (r == mdn_success)
name = namebuf;
n = REAL(gethostbyname2_r)(name, af, &he, data, datalen, rp, errp);
if (n == 0 && *rp != NULL)
*rp = copy_decode_hostent_static(*rp, result, buffer, buflen,
errp);
mdn_isprocessing = 0;
if (data != buf.data)
free(data);
if (*errp != 0)
n = EINVAL; /* XXX */
return (n);
}
#endif
#ifdef HAVE_GETHOSTBYADDR_R
int
ENTRY(gethostbyaddr_r)(GHBA_ADDR_T addr, GHBA_ADDRLEN_T len, int type,
struct hostent *result,
char *buffer, size_t buflen,
struct hostent **rp, int *errp)
{
char *data;
size_t datalen;
struct hostent he;
hostbuf_t buf;
int n;
if (mdn_isprocessing) {
return (REAL(gethostbyaddr_r)(addr, len, type, result,
buffer, buflen, rp, errp));
}
TRACE(("gethostbyaddr_r(buflen=%d)\n", buflen));
if (buflen <= sizeof(buf)) {
data = buf.data;
datalen = sizeof(buf);
} else {
data = malloc(buflen);
datalen = buflen;
if (data == NULL) {
*errp = NO_RECOVERY;
return (ENOMEM);
}
}
mdn_isprocessing = 1;
n = REAL(gethostbyaddr_r)(addr, len, type, &he,
data, datalen, rp, errp);
if (n == 0 && *rp != NULL)
*rp = copy_decode_hostent_static(*rp, result, buffer, buflen,
errp);
mdn_isprocessing = 0;
if (data != buf.data)
free(data);
if (*errp != 0)
n = EINVAL; /* XXX */
return (0);
}
#endif
#else /* GETHOST_R_GLIBC_FLAVOR */
#ifdef HAVE_GETHOSTBYNAME_R
struct hostent *
ENTRY(gethostbyname_r)(const char *name, struct hostent *result,
char *buffer, int buflen, int *errp)
{
char namebuf[MDN_NAME_SIZE];
char *data;
size_t datalen;
mdn_result_t r;
struct hostent *hp, he;
hostbuf_t buf;
if (mdn_isprocessing)
return (REAL(gethostbyname_r)(name, result, buffer,
buflen, errp));
TRACE(("gethostbyname_r(name=%s,buflen=%d)\n",
mdn_debug_xstring(name, 60), buflen));
if (buflen <= sizeof(buf)) {
data = buf.data;
datalen = sizeof(buf);
} else {
data = malloc(buflen);
datalen = buflen;
if (data == NULL) {
*errp = NO_RECOVERY;
return (NULL);
}
}
mdn_isprocessing = 1;
mdn_enable(1);
r = mdn_encodename(MDN_ENCODE_APP, name, namebuf, sizeof(namebuf));
if (r == mdn_success)
name = namebuf;
hp = REAL(gethostbyname_r)(name, &he, data, datalen, errp);
if (hp != NULL)
hp = copy_decode_hostent_static(hp, result, buffer, buflen,
errp);
mdn_isprocessing = 0;
if (data != buf.data)
free(data);
return (hp);
}
#endif
#ifdef HAVE_GETHOSTBYADDR_R
struct hostent *
ENTRY(gethostbyaddr_r)(GHBA_ADDR_T addr, GHBA_ADDRLEN_T len, int type,
struct hostent *result,
char *buffer, int buflen, int *errp)
{
char *data;
size_t datalen;
struct hostent *hp, he;
hostbuf_t buf;
if (mdn_isprocessing) {
return (REAL(gethostbyaddr_r)(addr, len, type, result,
buffer, buflen, errp));
}
TRACE(("gethostbyaddr_r(buflen=%d)\n", buflen));
if (buflen <= sizeof(buf)) {
data = buf.data;
datalen = sizeof(buf);
} else {
data = malloc(buflen);
datalen = buflen;
if (data == NULL) {
*errp = NO_RECOVERY;
return (NULL);
}
}
mdn_isprocessing = 1;
hp = REAL(gethostbyaddr_r)(addr, len, type, &he, data, datalen, errp);
if (hp != NULL)
hp = copy_decode_hostent_static(hp, result, buffer, buflen,
errp);
mdn_isprocessing = 0;
if (data != buf.data)
free(data);
return (hp);
}
#endif
#endif /* GETHOST_R_GLIBC_FLAVOR */
#ifdef HAVE_GETIPNODEBYNAME
struct hostent *
ENTRY(getipnodebyname)(const char *name, int af, int flags, int *errp) {
char namebuf[MDN_NAME_SIZE];
mdn_result_t r;
struct hostent *hp;
if (mdn_isprocessing)
return (REAL(getipnodebyname)(name, af, flags, errp));
TRACE(("getipnodebyname(name=%s)\n", mdn_debug_xstring(name, 60), af));
mdn_isprocessing = 1;
mdn_enable(1);
r = mdn_encodename(MDN_ENCODE_APP, name, namebuf, sizeof(namebuf));
if (r == mdn_success)
name = namebuf;
hp = REAL(getipnodebyname)(name, af, flags, errp);
if (hp != NULL) {
struct hostent *newhp = copy_decode_hostent_dynamic(hp, errp);
if (newhp != hp) {
REAL(freehostent)(hp);
obj_lock(newhp);
hp = newhp;
}
}
mdn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_GETIPNODEBYADDR
struct hostent *
ENTRY(getipnodebyaddr)(const void *src, size_t len, int af, int *errp) {
struct hostent *hp;
if (mdn_isprocessing)
return (REAL(getipnodebyaddr)(src, len, af, errp));
TRACE(("getipnodebyaddr()\n"));
mdn_isprocessing = 1;
hp = REAL(getipnodebyaddr)(src, len, af, errp);
if (hp != NULL) {
struct hostent *newhp = copy_decode_hostent_dynamic(hp, errp);
if (newhp != hp) {
REAL(freehostent)(hp);
obj_lock(newhp);
hp = newhp;
}
}
mdn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_FREEHOSTENT
void
ENTRY(freehostent)(struct hostent *hp) {
TRACE(("freehostent(hp=%p)\n", (void *)hp));
if (obj_islocked(hp)) {
/*
* We allocated the data.
*/
obj_unlock(hp);
free_copied_hostent(hp);
} else {
/*
* It was allocated the original getipnodeby*().
*/
REAL(freehostent)(hp);
}
}
#endif
#ifdef HAVE_GETADDRINFO
int
ENTRY(getaddrinfo)(const char *nodename, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
char namebuf[MDN_NAME_SIZE];
mdn_result_t r;
struct addrinfo *aip;
int err;
if (nodename == NULL || mdn_isprocessing)
return (REAL(getaddrinfo)(nodename, servname, hints, res));
TRACE(("getaddrinfo(nodename=%s)\n", mdn_debug_xstring(nodename, 60)));
mdn_isprocessing = 1;
mdn_enable(1);
r = mdn_encodename(MDN_ENCODE_APP, nodename,
namebuf, sizeof(namebuf));
if (r == mdn_success)
nodename = namebuf;
err = REAL(getaddrinfo)(nodename, servname, hints, &aip);
if (err == 0 && aip != NULL) {
*res = copy_decode_addrinfo_dynamic(aip);
if (*res == NULL)
err = EAI_FAIL;
else
obj_lock(*res);
if (aip != NULL)
REAL(freeaddrinfo)(aip);
}
mdn_isprocessing = 0;
return (err);
}
#endif
#ifdef HAVE_FREEADDRINFO
void
ENTRY(freeaddrinfo)(struct addrinfo *aip) {
TRACE(("freeaddrinfo(aip=%p)\n", (void *)aip));
if (obj_islocked(aip)) {
/*
* We allocated the data.
*/
obj_unlock(aip);
free_copied_addrinfo(aip);
} else {
/*
* It was allocated the original getaddrinfo().
*/
REAL(freeaddrinfo)(aip);
}
}
#endif
#ifdef HAVE_GETNAMEINFO
int
ENTRY(getnameinfo)(const struct sockaddr *sa, GNI_SALEN_T salen,
char *host, GNI_HOSTLEN_T hostlen, char *serv,
GNI_SERVLEN_T servlen, GNI_FLAGS_T flags)
{
char name[MDN_NAME_SIZE];
size_t namelen = sizeof(name);
int code;
mdn_result_t r;
if (host == NULL || hostlen == 0 || mdn_isprocessing) {
return (REAL(getnameinfo)(sa, salen, host, hostlen,
serv, servlen, flags));
}
TRACE(("getnameinfo(hostlen=%u)\n", hostlen));
mdn_isprocessing = 1;
code = REAL(getnameinfo)(sa, salen, name, namelen,
serv, servlen, flags);
if (code == 0 && name[0] != '\0') {
mdn_enable(1);
r = mdn_decodename(MDN_DECODE_APP, name, host, hostlen);
switch (r) {
case mdn_success:
code = 0;
break;
case mdn_buffer_overflow:
case mdn_nomemory:
code = EAI_MEMORY;
break;
default:
code = EAI_FAIL;
break;
}
}
mdn_isprocessing = 0;
return (code);
}
#endif
|