compat-gethnamaddr.c 22 KB

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