| 1 | /* |
| 2 | * ++Copyright++ 1985, 1988, 1993 |
| 3 | * - |
| 4 | * Copyright (c) 1985, 1988, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 4. Neither the name of the University nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | * - |
| 31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
| 32 | * |
| 33 | * Permission to use, copy, modify, and distribute this software for any |
| 34 | * purpose with or without fee is hereby granted, provided that the above |
| 35 | * copyright notice and this permission notice appear in all copies, and that |
| 36 | * the name of Digital Equipment Corporation not be used in advertising or |
| 37 | * publicity pertaining to distribution of the document or software without |
| 38 | * specific, written prior permission. |
| 39 | * |
| 40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
| 41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
| 42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
| 43 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 44 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 45 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 46 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 47 | * SOFTWARE. |
| 48 | * - |
| 49 | * --Copyright-- |
| 50 | */ |
| 51 | |
| 52 | /* XXX This file is not used by any of the resolver functions implemented by |
| 53 | glibc (i.e. get*info and gethostby*). It cannot be removed however because |
| 54 | it exports symbols in the libresolv ABI. The file is not maintained any |
| 55 | more, nor are these functions. */ |
| 56 | |
| 57 | #include <shlib-compat.h> |
| 58 | #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_25) |
| 59 | |
| 60 | # include <sys/types.h> |
| 61 | # include <sys/param.h> |
| 62 | # include <sys/socket.h> |
| 63 | # include <netinet/in.h> |
| 64 | # include <arpa/inet.h> |
| 65 | # include <arpa/nameser.h> |
| 66 | |
| 67 | # include <stdio.h> |
| 68 | # include <netdb.h> |
| 69 | # include <resolv/resolv-internal.h> |
| 70 | # include <ctype.h> |
| 71 | # include <errno.h> |
| 72 | # include <stdlib.h> |
| 73 | # include <string.h> |
| 74 | |
| 75 | # define MAXALIASES 35 |
| 76 | # define MAXADDRS 35 |
| 77 | |
| 78 | static char *h_addr_ptrs[MAXADDRS + 1]; |
| 79 | |
| 80 | static struct hostent host; |
| 81 | static char *host_aliases[MAXALIASES]; |
| 82 | static char hostbuf[8*1024]; |
| 83 | static u_char host_addr[16]; /* IPv4 or IPv6 */ |
| 84 | static FILE *hostf = NULL; |
| 85 | static int stayopen = 0; |
| 86 | |
| 87 | static void map_v4v6_address (const char *src, char *dst) __THROW; |
| 88 | static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW; |
| 89 | |
| 90 | extern void addrsort (char **, int) __THROW; |
| 91 | |
| 92 | # if PACKETSZ > 65536 |
| 93 | # define MAXPACKET PACKETSZ |
| 94 | # else |
| 95 | # define MAXPACKET 65536 |
| 96 | # endif |
| 97 | |
| 98 | /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */ |
| 99 | # ifdef MAXHOSTNAMELEN |
| 100 | # undef MAXHOSTNAMELEN |
| 101 | # endif |
| 102 | # define MAXHOSTNAMELEN 256 |
| 103 | |
| 104 | typedef union { |
| 105 | HEADER hdr; |
| 106 | u_char buf[MAXPACKET]; |
| 107 | } querybuf; |
| 108 | |
| 109 | typedef union { |
| 110 | int32_t al; |
| 111 | char ac; |
| 112 | } align; |
| 113 | |
| 114 | # ifndef h_errno |
| 115 | extern int h_errno; |
| 116 | # endif |
| 117 | |
| 118 | # ifdef DEBUG |
| 119 | static void |
| 120 | Dprintf (char *msg, int num) |
| 121 | { |
| 122 | if (_res.options & RES_DEBUG) { |
| 123 | int save = errno; |
| 124 | |
| 125 | printf(msg, num); |
| 126 | __set_errno (save); |
| 127 | } |
| 128 | } |
| 129 | # else |
| 130 | # define Dprintf(msg, num) /*nada*/ |
| 131 | # endif |
| 132 | |
| 133 | # define BOUNDED_INCR(x) \ |
| 134 | do { \ |
| 135 | cp += x; \ |
| 136 | if (cp > eom) { \ |
| 137 | __set_h_errno (NO_RECOVERY); \ |
| 138 | return (NULL); \ |
| 139 | } \ |
| 140 | } while (0) |
| 141 | |
| 142 | # define BOUNDS_CHECK(ptr, count) \ |
| 143 | do { \ |
| 144 | if ((ptr) + (count) > eom) { \ |
| 145 | __set_h_errno (NO_RECOVERY); \ |
| 146 | return (NULL); \ |
| 147 | } \ |
| 148 | } while (0) |
| 149 | |
| 150 | |
| 151 | static struct hostent * |
| 152 | getanswer (const querybuf *answer, int anslen, const char *qname, int qtype) |
| 153 | { |
| 154 | const HEADER *hp; |
| 155 | const u_char *cp; |
| 156 | int n; |
| 157 | const u_char *eom, *erdata; |
| 158 | char *bp, **ap, **hap; |
| 159 | int type, class, buflen, ancount, qdcount; |
| 160 | int haveanswer, had_error; |
| 161 | int toobig = 0; |
| 162 | char tbuf[MAXDNAME]; |
| 163 | const char *tname; |
| 164 | int (*name_ok) (const char *); |
| 165 | |
| 166 | tname = qname; |
| 167 | host.h_name = NULL; |
| 168 | eom = answer->buf + anslen; |
| 169 | switch (qtype) { |
| 170 | case T_A: |
| 171 | case T_AAAA: |
| 172 | name_ok = res_hnok; |
| 173 | break; |
| 174 | case T_PTR: |
| 175 | name_ok = res_dnok; |
| 176 | break; |
| 177 | default: |
| 178 | return (NULL); /* XXX should be abort(); */ |
| 179 | } |
| 180 | /* |
| 181 | * find first satisfactory answer |
| 182 | */ |
| 183 | hp = &answer->hdr; |
| 184 | ancount = ntohs(hp->ancount); |
| 185 | qdcount = ntohs(hp->qdcount); |
| 186 | bp = hostbuf; |
| 187 | buflen = sizeof hostbuf; |
| 188 | cp = answer->buf; |
| 189 | BOUNDED_INCR(HFIXEDSZ); |
| 190 | if (qdcount != 1) { |
| 191 | __set_h_errno (NO_RECOVERY); |
| 192 | return (NULL); |
| 193 | } |
| 194 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
| 195 | if ((n < 0) || !(*name_ok)(bp)) { |
| 196 | __set_h_errno (NO_RECOVERY); |
| 197 | return (NULL); |
| 198 | } |
| 199 | BOUNDED_INCR(n + QFIXEDSZ); |
| 200 | if (qtype == T_A || qtype == T_AAAA) { |
| 201 | /* res_send() has already verified that the query name is the |
| 202 | * same as the one we sent; this just gets the expanded name |
| 203 | * (i.e., with the succeeding search-domain tacked on). |
| 204 | */ |
| 205 | n = strlen(bp) + 1; /* for the \0 */ |
| 206 | if (n >= MAXHOSTNAMELEN) { |
| 207 | __set_h_errno (NO_RECOVERY); |
| 208 | return (NULL); |
| 209 | } |
| 210 | host.h_name = bp; |
| 211 | bp += n; |
| 212 | buflen -= n; |
| 213 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 214 | qname = host.h_name; |
| 215 | } |
| 216 | ap = host_aliases; |
| 217 | *ap = NULL; |
| 218 | host.h_aliases = host_aliases; |
| 219 | hap = h_addr_ptrs; |
| 220 | *hap = NULL; |
| 221 | host.h_addr_list = h_addr_ptrs; |
| 222 | haveanswer = 0; |
| 223 | had_error = 0; |
| 224 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 225 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
| 226 | if ((n < 0) || !(*name_ok)(bp)) { |
| 227 | had_error++; |
| 228 | continue; |
| 229 | } |
| 230 | cp += n; /* name */ |
| 231 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
| 232 | type = ns_get16(cp); |
| 233 | cp += INT16SZ; /* type */ |
| 234 | class = ns_get16(cp); |
| 235 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
| 236 | n = ns_get16(cp); |
| 237 | cp += INT16SZ; /* len */ |
| 238 | BOUNDS_CHECK(cp, n); |
| 239 | erdata = cp + n; |
| 240 | if (class != C_IN) { |
| 241 | /* XXX - debug? syslog? */ |
| 242 | cp += n; |
| 243 | continue; /* XXX - had_error++ ? */ |
| 244 | } |
| 245 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
| 246 | if (ap >= &host_aliases[MAXALIASES-1]) |
| 247 | continue; |
| 248 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 249 | if ((n < 0) || !(*name_ok)(tbuf)) { |
| 250 | had_error++; |
| 251 | continue; |
| 252 | } |
| 253 | cp += n; |
| 254 | if (cp != erdata) { |
| 255 | __set_h_errno (NO_RECOVERY); |
| 256 | return (NULL); |
| 257 | } |
| 258 | /* Store alias. */ |
| 259 | *ap++ = bp; |
| 260 | n = strlen(bp) + 1; /* for the \0 */ |
| 261 | if (n >= MAXHOSTNAMELEN) { |
| 262 | had_error++; |
| 263 | continue; |
| 264 | } |
| 265 | bp += n; |
| 266 | buflen -= n; |
| 267 | /* Get canonical name. */ |
| 268 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 269 | if (n > buflen || n >= MAXHOSTNAMELEN) { |
| 270 | had_error++; |
| 271 | continue; |
| 272 | } |
| 273 | strcpy(bp, tbuf); |
| 274 | host.h_name = bp; |
| 275 | bp += n; |
| 276 | buflen -= n; |
| 277 | continue; |
| 278 | } |
| 279 | if (qtype == T_PTR && type == T_CNAME) { |
| 280 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 281 | if (n < 0 || !res_dnok(tbuf)) { |
| 282 | had_error++; |
| 283 | continue; |
| 284 | } |
| 285 | cp += n; |
| 286 | if (cp != erdata) { |
| 287 | __set_h_errno (NO_RECOVERY); |
| 288 | return (NULL); |
| 289 | } |
| 290 | /* Get canonical name. */ |
| 291 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 292 | if (n > buflen || n >= MAXHOSTNAMELEN) { |
| 293 | had_error++; |
| 294 | continue; |
| 295 | } |
| 296 | strcpy(bp, tbuf); |
| 297 | tname = bp; |
| 298 | bp += n; |
| 299 | buflen -= n; |
| 300 | continue; |
| 301 | } |
| 302 | if (type != qtype) { |
| 303 | /* Log a low priority message if we get an unexpected |
| 304 | * record, but skip it if we are using DNSSEC since it |
| 305 | * uses many different types in responses that do not |
| 306 | * match QTYPE. |
| 307 | */ |
| 308 | cp += n; |
| 309 | continue; /* XXX - had_error++ ? */ |
| 310 | } |
| 311 | switch (type) { |
| 312 | case T_PTR: |
| 313 | if (strcasecmp(tname, bp) != 0) { |
| 314 | cp += n; |
| 315 | continue; /* XXX - had_error++ ? */ |
| 316 | } |
| 317 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
| 318 | if ((n < 0) || !res_hnok(bp)) { |
| 319 | had_error++; |
| 320 | break; |
| 321 | } |
| 322 | cp += n; |
| 323 | if (cp != erdata) { |
| 324 | __set_h_errno (NO_RECOVERY); |
| 325 | return (NULL); |
| 326 | } |
| 327 | if (!haveanswer) |
| 328 | host.h_name = bp; |
| 329 | else if (ap < &host_aliases[MAXALIASES-1]) |
| 330 | *ap++ = bp; |
| 331 | else |
| 332 | n = -1; |
| 333 | if (n != -1) { |
| 334 | n = strlen(bp) + 1; /* for the \0 */ |
| 335 | if (n >= MAXHOSTNAMELEN) { |
| 336 | had_error++; |
| 337 | break; |
| 338 | } |
| 339 | bp += n; |
| 340 | buflen -= n; |
| 341 | } |
| 342 | break; |
| 343 | case T_A: |
| 344 | case T_AAAA: |
| 345 | if (strcasecmp(host.h_name, bp) != 0) { |
| 346 | cp += n; |
| 347 | continue; /* XXX - had_error++ ? */ |
| 348 | } |
| 349 | if (n != host.h_length) { |
| 350 | cp += n; |
| 351 | continue; |
| 352 | } |
| 353 | if (!haveanswer) { |
| 354 | int nn; |
| 355 | |
| 356 | host.h_name = bp; |
| 357 | nn = strlen(bp) + 1; /* for the \0 */ |
| 358 | bp += nn; |
| 359 | buflen -= nn; |
| 360 | } |
| 361 | |
| 362 | /* XXX: when incrementing bp, we have to decrement |
| 363 | * buflen by the same amount --okir */ |
| 364 | buflen -= sizeof(align) - ((u_long)bp % sizeof(align)); |
| 365 | |
| 366 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); |
| 367 | |
| 368 | if (bp + n >= &hostbuf[sizeof hostbuf]) { |
| 369 | Dprintf("size (%d) too big\n" , n); |
| 370 | had_error++; |
| 371 | continue; |
| 372 | } |
| 373 | if (hap >= &h_addr_ptrs[MAXADDRS-1]) { |
| 374 | if (!toobig++) { |
| 375 | Dprintf("Too many addresses (%d)\n" , |
| 376 | MAXADDRS); |
| 377 | } |
| 378 | cp += n; |
| 379 | continue; |
| 380 | } |
| 381 | memmove(*hap++ = bp, cp, n); |
| 382 | bp += n; |
| 383 | buflen -= n; |
| 384 | cp += n; |
| 385 | if (cp != erdata) { |
| 386 | __set_h_errno (NO_RECOVERY); |
| 387 | return (NULL); |
| 388 | } |
| 389 | break; |
| 390 | default: |
| 391 | abort(); |
| 392 | } |
| 393 | if (!had_error) |
| 394 | haveanswer++; |
| 395 | } |
| 396 | if (haveanswer) { |
| 397 | *ap = NULL; |
| 398 | *hap = NULL; |
| 399 | /* |
| 400 | * Note: we sort even if host can take only one address |
| 401 | * in its return structures - should give it the "best" |
| 402 | * address in that case, not some random one |
| 403 | */ |
| 404 | if (_res.nsort && haveanswer > 1 && qtype == T_A) |
| 405 | addrsort(h_addr_ptrs, haveanswer); |
| 406 | if (!host.h_name) { |
| 407 | n = strlen(qname) + 1; /* for the \0 */ |
| 408 | if (n > buflen || n >= MAXHOSTNAMELEN) |
| 409 | goto no_recovery; |
| 410 | strcpy(bp, qname); |
| 411 | host.h_name = bp; |
| 412 | bp += n; |
| 413 | buflen -= n; |
| 414 | } |
| 415 | if (res_use_inet6 ()) |
| 416 | map_v4v6_hostent(&host, &bp, &buflen); |
| 417 | __set_h_errno (NETDB_SUCCESS); |
| 418 | return (&host); |
| 419 | } |
| 420 | no_recovery: |
| 421 | __set_h_errno (NO_RECOVERY); |
| 422 | return (NULL); |
| 423 | } |
| 424 | |
| 425 | extern struct hostent *res_gethostbyname2(const char *name, int af); |
| 426 | libresolv_hidden_proto (res_gethostbyname2) |
| 427 | |
| 428 | struct hostent * |
| 429 | res_gethostbyname (const char *name) |
| 430 | { |
| 431 | struct hostent *hp; |
| 432 | |
| 433 | if (__res_maybe_init (&_res, 0) == -1) { |
| 434 | __set_h_errno (NETDB_INTERNAL); |
| 435 | return (NULL); |
| 436 | } |
| 437 | if (res_use_inet6 ()) { |
| 438 | hp = res_gethostbyname2(name, AF_INET6); |
| 439 | if (hp) |
| 440 | return (hp); |
| 441 | } |
| 442 | return (res_gethostbyname2(name, AF_INET)); |
| 443 | } |
| 444 | compat_symbol (libresolv, res_gethostbyname, res_gethostbyname, GLIBC_2_0); |
| 445 | |
| 446 | struct hostent * |
| 447 | res_gethostbyname2 (const char *name, int af) |
| 448 | { |
| 449 | union |
| 450 | { |
| 451 | querybuf *buf; |
| 452 | u_char *ptr; |
| 453 | } buf; |
| 454 | querybuf *origbuf; |
| 455 | const char *cp; |
| 456 | char *bp; |
| 457 | int n, size, type, len; |
| 458 | struct hostent *ret; |
| 459 | |
| 460 | if (__res_maybe_init (&_res, 0) == -1) { |
| 461 | __set_h_errno (NETDB_INTERNAL); |
| 462 | return (NULL); |
| 463 | } |
| 464 | |
| 465 | switch (af) { |
| 466 | case AF_INET: |
| 467 | size = INADDRSZ; |
| 468 | type = T_A; |
| 469 | break; |
| 470 | case AF_INET6: |
| 471 | size = IN6ADDRSZ; |
| 472 | type = T_AAAA; |
| 473 | break; |
| 474 | default: |
| 475 | __set_h_errno (NETDB_INTERNAL); |
| 476 | __set_errno (EAFNOSUPPORT); |
| 477 | return (NULL); |
| 478 | } |
| 479 | |
| 480 | host.h_addrtype = af; |
| 481 | host.h_length = size; |
| 482 | |
| 483 | /* |
| 484 | * if there aren't any dots, it could be a user-level alias. |
| 485 | * this is also done in res_query() since we are not the only |
| 486 | * function that looks up host names. |
| 487 | */ |
| 488 | if (!strchr(name, '.') && (cp = __hostalias(name))) |
| 489 | name = cp; |
| 490 | |
| 491 | /* |
| 492 | * disallow names consisting only of digits/dots, unless |
| 493 | * they end in a dot. |
| 494 | */ |
| 495 | if (isdigit(name[0])) |
| 496 | for (cp = name;; ++cp) { |
| 497 | if (!*cp) { |
| 498 | if (*--cp == '.') |
| 499 | break; |
| 500 | /* |
| 501 | * All-numeric, no dot at the end. |
| 502 | * Fake up a hostent as if we'd actually |
| 503 | * done a lookup. |
| 504 | */ |
| 505 | if (inet_pton(af, name, host_addr) <= 0) { |
| 506 | __set_h_errno (HOST_NOT_FOUND); |
| 507 | return (NULL); |
| 508 | } |
| 509 | strncpy(hostbuf, name, MAXDNAME); |
| 510 | hostbuf[MAXDNAME] = '\0'; |
| 511 | bp = hostbuf + MAXDNAME; |
| 512 | len = sizeof hostbuf - MAXDNAME; |
| 513 | host.h_name = hostbuf; |
| 514 | host.h_aliases = host_aliases; |
| 515 | host_aliases[0] = NULL; |
| 516 | h_addr_ptrs[0] = (char *)host_addr; |
| 517 | h_addr_ptrs[1] = NULL; |
| 518 | host.h_addr_list = h_addr_ptrs; |
| 519 | if (res_use_inet6 ()) |
| 520 | map_v4v6_hostent(&host, &bp, &len); |
| 521 | __set_h_errno (NETDB_SUCCESS); |
| 522 | return (&host); |
| 523 | } |
| 524 | if (!isdigit(*cp) && *cp != '.') |
| 525 | break; |
| 526 | } |
| 527 | if ((isxdigit(name[0]) && strchr(name, ':') != NULL) || |
| 528 | name[0] == ':') |
| 529 | for (cp = name;; ++cp) { |
| 530 | if (!*cp) { |
| 531 | if (*--cp == '.') |
| 532 | break; |
| 533 | /* |
| 534 | * All-IPv6-legal, no dot at the end. |
| 535 | * Fake up a hostent as if we'd actually |
| 536 | * done a lookup. |
| 537 | */ |
| 538 | if (inet_pton(af, name, host_addr) <= 0) { |
| 539 | __set_h_errno (HOST_NOT_FOUND); |
| 540 | return (NULL); |
| 541 | } |
| 542 | strncpy(hostbuf, name, MAXDNAME); |
| 543 | hostbuf[MAXDNAME] = '\0'; |
| 544 | bp = hostbuf + MAXDNAME; |
| 545 | len = sizeof hostbuf - MAXDNAME; |
| 546 | host.h_name = hostbuf; |
| 547 | host.h_aliases = host_aliases; |
| 548 | host_aliases[0] = NULL; |
| 549 | h_addr_ptrs[0] = (char *)host_addr; |
| 550 | h_addr_ptrs[1] = NULL; |
| 551 | host.h_addr_list = h_addr_ptrs; |
| 552 | __set_h_errno (NETDB_SUCCESS); |
| 553 | return (&host); |
| 554 | } |
| 555 | if (!isxdigit(*cp) && *cp != ':' && *cp != '.') |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | buf.buf = origbuf = (querybuf *) alloca (1024); |
| 560 | |
| 561 | if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024, |
| 562 | &buf.ptr, NULL, NULL, NULL, NULL)) < 0) { |
| 563 | if (buf.buf != origbuf) |
| 564 | free (buf.buf); |
| 565 | Dprintf("res_nsearch failed (%d)\n" , n); |
| 566 | if (errno == ECONNREFUSED) |
| 567 | return (_gethtbyname2(name, af)); |
| 568 | return (NULL); |
| 569 | } |
| 570 | ret = getanswer(buf.buf, n, name, type); |
| 571 | if (buf.buf != origbuf) |
| 572 | free (buf.buf); |
| 573 | return ret; |
| 574 | } |
| 575 | libresolv_hidden_def (res_gethostbyname2) |
| 576 | compat_symbol (libresolv, res_gethostbyname2, res_gethostbyname2, GLIBC_2_0); |
| 577 | |
| 578 | struct hostent * |
| 579 | res_gethostbyaddr (const void *addr, socklen_t len, int af) |
| 580 | { |
| 581 | const u_char *uaddr = (const u_char *)addr; |
| 582 | static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; |
| 583 | static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; |
| 584 | int n; |
| 585 | socklen_t size; |
| 586 | union |
| 587 | { |
| 588 | querybuf *buf; |
| 589 | u_char *ptr; |
| 590 | } buf; |
| 591 | querybuf *orig_buf; |
| 592 | struct hostent *hp; |
| 593 | char qbuf[MAXDNAME+1], *qp = NULL; |
| 594 | |
| 595 | if (__res_maybe_init (&_res, 0) == -1) { |
| 596 | __set_h_errno (NETDB_INTERNAL); |
| 597 | return (NULL); |
| 598 | } |
| 599 | if (af == AF_INET6 && len == IN6ADDRSZ && |
| 600 | (!memcmp(uaddr, mapped, sizeof mapped) || |
| 601 | !memcmp(uaddr, tunnelled, sizeof tunnelled))) { |
| 602 | /* Unmap. */ |
| 603 | addr += sizeof mapped; |
| 604 | uaddr += sizeof mapped; |
| 605 | af = AF_INET; |
| 606 | len = INADDRSZ; |
| 607 | } |
| 608 | switch (af) { |
| 609 | case AF_INET: |
| 610 | size = INADDRSZ; |
| 611 | break; |
| 612 | case AF_INET6: |
| 613 | size = IN6ADDRSZ; |
| 614 | break; |
| 615 | default: |
| 616 | __set_errno (EAFNOSUPPORT); |
| 617 | __set_h_errno (NETDB_INTERNAL); |
| 618 | return (NULL); |
| 619 | } |
| 620 | if (size != len) { |
| 621 | __set_errno (EINVAL); |
| 622 | __set_h_errno (NETDB_INTERNAL); |
| 623 | return (NULL); |
| 624 | } |
| 625 | switch (af) { |
| 626 | case AF_INET: |
| 627 | (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa" , |
| 628 | (uaddr[3] & 0xff), |
| 629 | (uaddr[2] & 0xff), |
| 630 | (uaddr[1] & 0xff), |
| 631 | (uaddr[0] & 0xff)); |
| 632 | break; |
| 633 | case AF_INET6: |
| 634 | qp = qbuf; |
| 635 | for (n = IN6ADDRSZ - 1; n >= 0; n--) { |
| 636 | qp += sprintf(qp, "%x.%x." , |
| 637 | uaddr[n] & 0xf, |
| 638 | (uaddr[n] >> 4) & 0xf); |
| 639 | } |
| 640 | strcpy(qp, "ip6.arpa" ); |
| 641 | break; |
| 642 | default: |
| 643 | abort(); |
| 644 | } |
| 645 | |
| 646 | buf.buf = orig_buf = (querybuf *) alloca (1024); |
| 647 | |
| 648 | n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024, |
| 649 | &buf.ptr, NULL, NULL, NULL, NULL); |
| 650 | if (n < 0) { |
| 651 | if (buf.buf != orig_buf) |
| 652 | free (buf.buf); |
| 653 | Dprintf("res_nquery failed (%d)\n" , n); |
| 654 | if (errno == ECONNREFUSED) |
| 655 | return (_gethtbyaddr(addr, len, af)); |
| 656 | return (NULL); |
| 657 | } |
| 658 | hp = getanswer(buf.buf, n, qbuf, T_PTR); |
| 659 | if (buf.buf != orig_buf) |
| 660 | free (buf.buf); |
| 661 | if (!hp) |
| 662 | return (NULL); /* h_errno was set by getanswer() */ |
| 663 | hp->h_addrtype = af; |
| 664 | hp->h_length = len; |
| 665 | memmove(host_addr, addr, len); |
| 666 | h_addr_ptrs[0] = (char *)host_addr; |
| 667 | h_addr_ptrs[1] = NULL; |
| 668 | if (af == AF_INET && res_use_inet6 ()) { |
| 669 | map_v4v6_address((char*)host_addr, (char*)host_addr); |
| 670 | hp->h_addrtype = AF_INET6; |
| 671 | hp->h_length = IN6ADDRSZ; |
| 672 | } |
| 673 | __set_h_errno (NETDB_SUCCESS); |
| 674 | return (hp); |
| 675 | } |
| 676 | compat_symbol (libresolv, res_gethostbyaddr, res_gethostbyaddr, GLIBC_2_0); |
| 677 | |
| 678 | void |
| 679 | _sethtent (int f) |
| 680 | { |
| 681 | if (!hostf) |
| 682 | hostf = fopen(_PATH_HOSTS, "rce" ); |
| 683 | else |
| 684 | rewind(hostf); |
| 685 | stayopen = f; |
| 686 | } |
| 687 | libresolv_hidden_def (_sethtent) |
| 688 | compat_symbol (libresolv, _sethtent, _sethtent, GLIBC_2_0); |
| 689 | |
| 690 | static void |
| 691 | _endhtent (void) |
| 692 | { |
| 693 | if (hostf && !stayopen) { |
| 694 | (void) fclose(hostf); |
| 695 | hostf = NULL; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | struct hostent * |
| 700 | _gethtent (void) |
| 701 | { |
| 702 | char *p; |
| 703 | char *cp, **q; |
| 704 | int af, len; |
| 705 | |
| 706 | if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) { |
| 707 | __set_h_errno (NETDB_INTERNAL); |
| 708 | return (NULL); |
| 709 | } |
| 710 | again: |
| 711 | if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { |
| 712 | __set_h_errno (HOST_NOT_FOUND); |
| 713 | return (NULL); |
| 714 | } |
| 715 | if (*p == '#') |
| 716 | goto again; |
| 717 | if (!(cp = strpbrk(p, "#\n" ))) |
| 718 | goto again; |
| 719 | *cp = '\0'; |
| 720 | if (!(cp = strpbrk(p, " \t" ))) |
| 721 | goto again; |
| 722 | *cp++ = '\0'; |
| 723 | if (inet_pton(AF_INET6, p, host_addr) > 0) { |
| 724 | af = AF_INET6; |
| 725 | len = IN6ADDRSZ; |
| 726 | } else if (inet_pton(AF_INET, p, host_addr) > 0) { |
| 727 | if (res_use_inet6 ()) { |
| 728 | map_v4v6_address((char*)host_addr, (char*)host_addr); |
| 729 | af = AF_INET6; |
| 730 | len = IN6ADDRSZ; |
| 731 | } else { |
| 732 | af = AF_INET; |
| 733 | len = INADDRSZ; |
| 734 | } |
| 735 | } else { |
| 736 | goto again; |
| 737 | } |
| 738 | h_addr_ptrs[0] = (char *)host_addr; |
| 739 | h_addr_ptrs[1] = NULL; |
| 740 | host.h_addr_list = h_addr_ptrs; |
| 741 | host.h_length = len; |
| 742 | host.h_addrtype = af; |
| 743 | while (*cp == ' ' || *cp == '\t') |
| 744 | cp++; |
| 745 | host.h_name = cp; |
| 746 | q = host.h_aliases = host_aliases; |
| 747 | if ((cp = strpbrk(cp, " \t" ))) |
| 748 | *cp++ = '\0'; |
| 749 | while (cp && *cp) { |
| 750 | if (*cp == ' ' || *cp == '\t') { |
| 751 | cp++; |
| 752 | continue; |
| 753 | } |
| 754 | if (q < &host_aliases[MAXALIASES - 1]) |
| 755 | *q++ = cp; |
| 756 | if ((cp = strpbrk(cp, " \t" ))) |
| 757 | *cp++ = '\0'; |
| 758 | } |
| 759 | *q = NULL; |
| 760 | __set_h_errno (NETDB_SUCCESS); |
| 761 | return (&host); |
| 762 | } |
| 763 | libresolv_hidden_def (_gethtent) |
| 764 | compat_symbol (libresolv, _gethtent, _gethtent, GLIBC_2_0); |
| 765 | |
| 766 | struct hostent * |
| 767 | _gethtbyname (const char *name) |
| 768 | { |
| 769 | struct hostent *hp; |
| 770 | |
| 771 | if (res_use_inet6 ()) { |
| 772 | hp = _gethtbyname2(name, AF_INET6); |
| 773 | if (hp) |
| 774 | return (hp); |
| 775 | } |
| 776 | return (_gethtbyname2(name, AF_INET)); |
| 777 | } |
| 778 | compat_symbol (libresolv, _gethtbyname, _gethtbyname, GLIBC_2_0); |
| 779 | |
| 780 | struct hostent * |
| 781 | _gethtbyname2 (const char *name, int af) |
| 782 | { |
| 783 | struct hostent *p; |
| 784 | char **cp; |
| 785 | |
| 786 | _sethtent(0); |
| 787 | while ((p = _gethtent())) { |
| 788 | if (p->h_addrtype != af) |
| 789 | continue; |
| 790 | if (strcasecmp(p->h_name, name) == 0) |
| 791 | break; |
| 792 | for (cp = p->h_aliases; *cp != 0; cp++) |
| 793 | if (strcasecmp(*cp, name) == 0) |
| 794 | goto found; |
| 795 | } |
| 796 | found: |
| 797 | _endhtent(); |
| 798 | return (p); |
| 799 | } |
| 800 | libresolv_hidden_def (_gethtbyname2) |
| 801 | compat_symbol (libresolv, _gethtbyname2, _gethtbyname2, GLIBC_2_0); |
| 802 | |
| 803 | struct hostent * |
| 804 | _gethtbyaddr (const char *addr, size_t len, int af) |
| 805 | { |
| 806 | struct hostent *p; |
| 807 | |
| 808 | _sethtent(0); |
| 809 | while ((p = _gethtent())) |
| 810 | if (p->h_addrtype == af && !memcmp(p->h_addr, addr, len)) |
| 811 | break; |
| 812 | _endhtent(); |
| 813 | return (p); |
| 814 | } |
| 815 | libresolv_hidden_def (_gethtbyaddr) |
| 816 | compat_symbol (libresolv, _gethtbyaddr, _gethtbyaddr, GLIBC_2_0); |
| 817 | |
| 818 | static void |
| 819 | map_v4v6_address (const char *src, char *dst) |
| 820 | { |
| 821 | u_char *p = (u_char *)dst; |
| 822 | char tmp[INADDRSZ]; |
| 823 | int i; |
| 824 | |
| 825 | /* Stash a temporary copy so our caller can update in place. */ |
| 826 | memcpy(tmp, src, INADDRSZ); |
| 827 | /* Mark this ipv6 addr as a mapped ipv4. */ |
| 828 | for (i = 0; i < 10; i++) |
| 829 | *p++ = 0x00; |
| 830 | *p++ = 0xff; |
| 831 | *p++ = 0xff; |
| 832 | /* Retrieve the saved copy and we're done. */ |
| 833 | memcpy((void*)p, tmp, INADDRSZ); |
| 834 | } |
| 835 | |
| 836 | static void |
| 837 | map_v4v6_hostent (struct hostent *hp, char **bpp, int *lenp) |
| 838 | { |
| 839 | char **ap; |
| 840 | |
| 841 | if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) |
| 842 | return; |
| 843 | hp->h_addrtype = AF_INET6; |
| 844 | hp->h_length = IN6ADDRSZ; |
| 845 | for (ap = hp->h_addr_list; *ap; ap++) { |
| 846 | int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); |
| 847 | |
| 848 | if (*lenp < (i + IN6ADDRSZ)) { |
| 849 | /* Out of memory. Truncate address list here. XXX */ |
| 850 | *ap = NULL; |
| 851 | return; |
| 852 | } |
| 853 | *bpp += i; |
| 854 | *lenp -= i; |
| 855 | map_v4v6_address(*ap, *bpp); |
| 856 | *ap = *bpp; |
| 857 | *bpp += IN6ADDRSZ; |
| 858 | *lenp -= IN6ADDRSZ; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | extern void |
| 863 | addrsort (char **ap, int num) |
| 864 | { |
| 865 | int i, j; |
| 866 | char **p; |
| 867 | short aval[MAXADDRS]; |
| 868 | int needsort = 0; |
| 869 | |
| 870 | p = ap; |
| 871 | for (i = 0; i < num; i++, p++) { |
| 872 | for (j = 0 ; (unsigned)j < _res.nsort; j++) |
| 873 | if (_res.sort_list[j].addr.s_addr == |
| 874 | (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) |
| 875 | break; |
| 876 | aval[i] = j; |
| 877 | if (needsort == 0 && i > 0 && j < aval[i-1]) |
| 878 | needsort = i; |
| 879 | } |
| 880 | if (!needsort) |
| 881 | return; |
| 882 | |
| 883 | while (needsort < num) { |
| 884 | for (j = needsort - 1; j >= 0; j--) { |
| 885 | if (aval[j] > aval[j+1]) { |
| 886 | char *hp; |
| 887 | |
| 888 | i = aval[j]; |
| 889 | aval[j] = aval[j+1]; |
| 890 | aval[j+1] = i; |
| 891 | |
| 892 | hp = ap[j]; |
| 893 | ap[j] = ap[j+1]; |
| 894 | ap[j+1] = hp; |
| 895 | |
| 896 | } else |
| 897 | break; |
| 898 | } |
| 899 | needsort++; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | #endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25) */ |
| 904 | |