res_query.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * Copyright (c) 1988, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  31. *
  32. * Permission to use, copy, modify, and distribute this software for any
  33. * purpose with or without fee is hereby granted, provided that the above
  34. * copyright notice and this permission notice appear in all copies, and that
  35. * the name of Digital Equipment Corporation not be used in advertising or
  36. * publicity pertaining to distribution of the document or software without
  37. * specific, written prior permission.
  38. *
  39. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  40. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  41. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  42. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  43. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  44. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  45. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  46. * SOFTWARE.
  47. */
  48. /*
  49. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  50. *
  51. * Permission to use, copy, modify, and distribute this software for any
  52. * purpose with or without fee is hereby granted, provided that the above
  53. * copyright notice and this permission notice appear in all copies.
  54. *
  55. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  56. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  57. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  58. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  59. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  60. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  61. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  62. * SOFTWARE.
  63. */
  64. #include <assert.h>
  65. #include <sys/types.h>
  66. #include <sys/param.h>
  67. #include <netinet/in.h>
  68. #include <arpa/inet.h>
  69. #include <arpa/nameser.h>
  70. #include <ctype.h>
  71. #include <errno.h>
  72. #include <netdb.h>
  73. #include <resolv.h>
  74. #include <resolv/resolv-internal.h>
  75. #include <resolv/resolv_context.h>
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. #include <shlib-compat.h>
  80. #if PACKETSZ > 65536
  81. #define MAXPACKET PACKETSZ
  82. #else
  83. #define MAXPACKET 65536
  84. #endif
  85. #define QUERYSIZE (HFIXEDSZ + QFIXEDSZ + MAXCDNAME + 1)
  86. static int
  87. __res_context_querydomain (struct resolv_context *,
  88. const char *name, const char *domain,
  89. int class, int type, unsigned char *answer, int anslen,
  90. unsigned char **answerp, unsigned char **answerp2, int *nanswerp2,
  91. int *resplen2, int *answerp2_malloced);
  92. /* Formulate a normal query, send, and await answer. Returned answer
  93. is placed in supplied buffer ANSWER. Perform preliminary check of
  94. answer, returning success only if no error is indicated and the
  95. answer count is nonzero. Return the size of the response on
  96. success, -1 on error. Error number is left in h_errno.
  97. Caller must parse answer and determine whether it answers the
  98. question. */
  99. int
  100. __res_context_query (struct resolv_context *ctx, const char *name,
  101. int class, int type,
  102. unsigned char *answer, int anslen,
  103. unsigned char **answerp, unsigned char **answerp2,
  104. int *nanswerp2, int *resplen2, int *answerp2_malloced)
  105. {
  106. struct __res_state *statp = ctx->resp;
  107. HEADER *hp = (HEADER *) answer;
  108. HEADER *hp2;
  109. int n, use_malloc = 0;
  110. size_t bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * QUERYSIZE;
  111. u_char *buf = alloca (bufsize);
  112. u_char *query1 = buf;
  113. int nquery1 = -1;
  114. u_char *query2 = NULL;
  115. int nquery2 = 0;
  116. again:
  117. hp->rcode = NOERROR; /* default */
  118. if (type == T_QUERY_A_AND_AAAA)
  119. {
  120. n = __res_context_mkquery (ctx, QUERY, name, class, T_A, NULL,
  121. query1, bufsize);
  122. if (n > 0)
  123. {
  124. if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0)
  125. {
  126. /* Use RESOLV_EDNS_BUFFER_SIZE because the receive
  127. buffer can be reallocated. */
  128. n = __res_nopt (ctx, n, query1, bufsize,
  129. RESOLV_EDNS_BUFFER_SIZE);
  130. if (n < 0)
  131. goto unspec_nomem;
  132. }
  133. nquery1 = n;
  134. /* Align the buffer. */
  135. int npad = ((nquery1 + __alignof__ (HEADER) - 1)
  136. & ~(__alignof__ (HEADER) - 1)) - nquery1;
  137. if (n > bufsize - npad)
  138. {
  139. n = -1;
  140. goto unspec_nomem;
  141. }
  142. int nused = n + npad;
  143. query2 = buf + nused;
  144. n = __res_context_mkquery (ctx, QUERY, name, class, T_AAAA,
  145. NULL, query2, bufsize - nused);
  146. if (n > 0
  147. && (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0)
  148. /* Use RESOLV_EDNS_BUFFER_SIZE because the receive
  149. buffer can be reallocated. */
  150. n = __res_nopt (ctx, n, query2, bufsize,
  151. RESOLV_EDNS_BUFFER_SIZE);
  152. nquery2 = n;
  153. }
  154. unspec_nomem:;
  155. }
  156. else
  157. {
  158. n = __res_context_mkquery (ctx, QUERY, name, class, type, NULL,
  159. query1, bufsize);
  160. if (n > 0
  161. && (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0)
  162. {
  163. /* Use RESOLV_EDNS_BUFFER_SIZE if the receive buffer
  164. can be reallocated. */
  165. size_t advertise;
  166. if (answerp == NULL)
  167. advertise = anslen;
  168. else
  169. advertise = RESOLV_EDNS_BUFFER_SIZE;
  170. n = __res_nopt (ctx, n, query1, bufsize, advertise);
  171. }
  172. nquery1 = n;
  173. }
  174. if (__glibc_unlikely (n <= 0) && !use_malloc) {
  175. /* Retry just in case res_nmkquery failed because of too
  176. short buffer. Shouldn't happen. */
  177. bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * MAXPACKET;
  178. buf = malloc (bufsize);
  179. if (buf != NULL) {
  180. query1 = buf;
  181. use_malloc = 1;
  182. goto again;
  183. }
  184. }
  185. if (__glibc_unlikely (n <= 0)) {
  186. RES_SET_H_ERRNO(statp, NO_RECOVERY);
  187. if (use_malloc)
  188. free (buf);
  189. return (n);
  190. }
  191. assert (answerp == NULL || (void *) *answerp == (void *) answer);
  192. n = __res_context_send (ctx, query1, nquery1, query2, nquery2, answer,
  193. anslen, answerp, answerp2, nanswerp2, resplen2,
  194. answerp2_malloced);
  195. if (use_malloc)
  196. free (buf);
  197. if (n < 0) {
  198. RES_SET_H_ERRNO(statp, TRY_AGAIN);
  199. return (n);
  200. }
  201. if (answerp != NULL)
  202. /* __res_context_send might have reallocated the buffer. */
  203. hp = (HEADER *) *answerp;
  204. /* We simplify the following tests by assigning HP to HP2 or
  205. vice versa. It is easy to verify that this is the same as
  206. ignoring all tests of HP or HP2. */
  207. if (answerp2 == NULL || *resplen2 < (int) sizeof (HEADER))
  208. {
  209. hp2 = hp;
  210. }
  211. else
  212. {
  213. hp2 = (HEADER *) *answerp2;
  214. if (n < (int) sizeof (HEADER))
  215. {
  216. hp = hp2;
  217. }
  218. }
  219. /* Make sure both hp and hp2 are defined */
  220. assert((hp != NULL) && (hp2 != NULL));
  221. if ((hp->rcode != NOERROR || ntohs(hp->ancount) == 0)
  222. && (hp2->rcode != NOERROR || ntohs(hp2->ancount) == 0)) {
  223. switch (hp->rcode == NOERROR ? hp2->rcode : hp->rcode) {
  224. case NXDOMAIN:
  225. if ((hp->rcode == NOERROR && ntohs (hp->ancount) != 0)
  226. || (hp2->rcode == NOERROR
  227. && ntohs (hp2->ancount) != 0))
  228. goto success;
  229. RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
  230. break;
  231. case SERVFAIL:
  232. RES_SET_H_ERRNO(statp, TRY_AGAIN);
  233. break;
  234. case NOERROR:
  235. if (ntohs (hp->ancount) != 0
  236. || ntohs (hp2->ancount) != 0)
  237. goto success;
  238. RES_SET_H_ERRNO(statp, NO_DATA);
  239. break;
  240. case FORMERR:
  241. case NOTIMP:
  242. /* Servers must not reply to AAAA queries with
  243. NOTIMP etc but some of them do. */
  244. if ((hp->rcode == NOERROR && ntohs (hp->ancount) != 0)
  245. || (hp2->rcode == NOERROR
  246. && ntohs (hp2->ancount) != 0))
  247. goto success;
  248. /* FALLTHROUGH */
  249. case REFUSED:
  250. default:
  251. RES_SET_H_ERRNO(statp, NO_RECOVERY);
  252. break;
  253. }
  254. return (-1);
  255. }
  256. success:
  257. return (n);
  258. }
  259. libresolv_hidden_def (__res_context_query)
  260. /* Common part of res_nquery and res_query. */
  261. static int
  262. context_query_common (struct resolv_context *ctx,
  263. const char *name, int class, int type,
  264. unsigned char *answer, int anslen)
  265. {
  266. if (ctx == NULL)
  267. {
  268. RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
  269. return -1;
  270. }
  271. int result = __res_context_query (ctx, name, class, type, answer, anslen,
  272. NULL, NULL, NULL, NULL, NULL);
  273. __resolv_context_put (ctx);
  274. return result;
  275. }
  276. int
  277. res_nquery(res_state statp,
  278. const char *name, /* domain name */
  279. int class, int type, /* class and type of query */
  280. u_char *answer, /* buffer to put answer */
  281. int anslen) /* size of answer buffer */
  282. {
  283. return context_query_common
  284. (__resolv_context_get_override (statp), name, class, type, answer, anslen);
  285. }
  286. int
  287. res_query (const char *name, int class, int type,
  288. unsigned char *answer, int anslen)
  289. {
  290. return context_query_common
  291. (__resolv_context_get (), name, class, type, answer, anslen);
  292. }
  293. /* Formulate a normal query, send, and retrieve answer in supplied
  294. buffer. Return the size of the response on success, -1 on error.
  295. If enabled, implement search rules until answer or unrecoverable
  296. failure is detected. Error code, if any, is left in h_errno. */
  297. int
  298. __res_context_search (struct resolv_context *ctx,
  299. const char *name, int class, int type,
  300. unsigned char *answer, int anslen,
  301. unsigned char **answerp, unsigned char **answerp2,
  302. int *nanswerp2, int *resplen2, int *answerp2_malloced)
  303. {
  304. struct __res_state *statp = ctx->resp;
  305. const char *cp;
  306. HEADER *hp = (HEADER *) answer;
  307. char tmp[NS_MAXDNAME];
  308. u_int dots;
  309. int trailing_dot, ret, saved_herrno;
  310. int got_nodata = 0, got_servfail = 0, root_on_list = 0;
  311. int tried_as_is = 0;
  312. int searched = 0;
  313. __set_errno (0);
  314. RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
  315. dots = 0;
  316. for (cp = name; *cp != '\0'; cp++)
  317. dots += (*cp == '.');
  318. trailing_dot = 0;
  319. if (cp > name && *--cp == '.')
  320. trailing_dot++;
  321. /* If there aren't any dots, it could be a user-level alias. */
  322. if (!dots && (cp = __res_context_hostalias
  323. (ctx, name, tmp, sizeof tmp))!= NULL)
  324. return __res_context_query (ctx, cp, class, type, answer,
  325. anslen, answerp, answerp2,
  326. nanswerp2, resplen2, answerp2_malloced);
  327. /*
  328. * If there are enough dots in the name, let's just give it a
  329. * try 'as is'. The threshold can be set with the "ndots" option.
  330. * Also, query 'as is', if there is a trailing dot in the name.
  331. */
  332. saved_herrno = -1;
  333. if (dots >= statp->ndots || trailing_dot) {
  334. ret = __res_context_querydomain (ctx, name, NULL, class, type,
  335. answer, anslen, answerp,
  336. answerp2, nanswerp2, resplen2,
  337. answerp2_malloced);
  338. if (ret > 0 || trailing_dot
  339. /* If the second response is valid then we use that. */
  340. || (ret == 0 && resplen2 != NULL && *resplen2 > 0))
  341. return (ret);
  342. saved_herrno = h_errno;
  343. tried_as_is++;
  344. if (answerp && *answerp != answer) {
  345. answer = *answerp;
  346. anslen = MAXPACKET;
  347. }
  348. if (answerp2 && *answerp2_malloced)
  349. {
  350. free (*answerp2);
  351. *answerp2 = NULL;
  352. *nanswerp2 = 0;
  353. *answerp2_malloced = 0;
  354. }
  355. }
  356. /*
  357. * We do at least one level of search if
  358. * - there is no dot and RES_DEFNAME is set, or
  359. * - there is at least one dot, there is no trailing dot,
  360. * and RES_DNSRCH is set.
  361. */
  362. if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
  363. (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
  364. int done = 0;
  365. for (size_t domain_index = 0; !done; ++domain_index) {
  366. const char *dname = __resolv_context_search_list
  367. (ctx, domain_index);
  368. if (dname == NULL)
  369. break;
  370. searched = 1;
  371. /* __res_context_querydoman concatenates name
  372. with dname with a "." in between. If we
  373. pass it in dname the "." we got from the
  374. configured default search path, we'll end
  375. up with "name..", which won't resolve.
  376. OTOH, passing it "" will result in "name.",
  377. which has the intended effect for both
  378. possible representations of the root
  379. domain. */
  380. if (dname[0] == '.')
  381. dname++;
  382. if (dname[0] == '\0')
  383. root_on_list++;
  384. ret = __res_context_querydomain
  385. (ctx, name, dname, class, type,
  386. answer, anslen, answerp, answerp2, nanswerp2,
  387. resplen2, answerp2_malloced);
  388. if (ret > 0 || (ret == 0 && resplen2 != NULL
  389. && *resplen2 > 0))
  390. return (ret);
  391. if (answerp && *answerp != answer) {
  392. answer = *answerp;
  393. anslen = MAXPACKET;
  394. }
  395. if (answerp2 && *answerp2_malloced)
  396. {
  397. free (*answerp2);
  398. *answerp2 = NULL;
  399. *nanswerp2 = 0;
  400. *answerp2_malloced = 0;
  401. }
  402. /*
  403. * If no server present, give up.
  404. * If name isn't found in this domain,
  405. * keep trying higher domains in the search list
  406. * (if that's enabled).
  407. * On a NO_DATA error, keep trying, otherwise
  408. * a wildcard entry of another type could keep us
  409. * from finding this entry higher in the domain.
  410. * If we get some other error (negative answer or
  411. * server failure), then stop searching up,
  412. * but try the input name below in case it's
  413. * fully-qualified.
  414. */
  415. if (errno == ECONNREFUSED) {
  416. RES_SET_H_ERRNO(statp, TRY_AGAIN);
  417. return (-1);
  418. }
  419. switch (statp->res_h_errno) {
  420. case NO_DATA:
  421. got_nodata++;
  422. /* FALLTHROUGH */
  423. case HOST_NOT_FOUND:
  424. /* keep trying */
  425. break;
  426. case TRY_AGAIN:
  427. if (hp->rcode == SERVFAIL) {
  428. /* try next search element, if any */
  429. got_servfail++;
  430. break;
  431. }
  432. /* FALLTHROUGH */
  433. default:
  434. /* anything else implies that we're done */
  435. done++;
  436. }
  437. /* if we got here for some reason other than DNSRCH,
  438. * we only wanted one iteration of the loop, so stop.
  439. */
  440. if ((statp->options & RES_DNSRCH) == 0)
  441. done++;
  442. }
  443. }
  444. /*
  445. * If the query has not already been tried as is then try it
  446. * unless RES_NOTLDQUERY is set and there were no dots.
  447. */
  448. if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0)
  449. && !(tried_as_is || root_on_list)) {
  450. ret = __res_context_querydomain
  451. (ctx, name, NULL, class, type,
  452. answer, anslen, answerp, answerp2, nanswerp2,
  453. resplen2, answerp2_malloced);
  454. if (ret > 0 || (ret == 0 && resplen2 != NULL
  455. && *resplen2 > 0))
  456. return (ret);
  457. }
  458. /* if we got here, we didn't satisfy the search.
  459. * if we did an initial full query, return that query's H_ERRNO
  460. * (note that we wouldn't be here if that query had succeeded).
  461. * else if we ever got a nodata, send that back as the reason.
  462. * else send back meaningless H_ERRNO, that being the one from
  463. * the last DNSRCH we did.
  464. */
  465. if (answerp2 && *answerp2_malloced)
  466. {
  467. free (*answerp2);
  468. *answerp2 = NULL;
  469. *nanswerp2 = 0;
  470. *answerp2_malloced = 0;
  471. }
  472. if (saved_herrno != -1)
  473. RES_SET_H_ERRNO(statp, saved_herrno);
  474. else if (got_nodata)
  475. RES_SET_H_ERRNO(statp, NO_DATA);
  476. else if (got_servfail)
  477. RES_SET_H_ERRNO(statp, TRY_AGAIN);
  478. return (-1);
  479. }
  480. libresolv_hidden_def (__res_context_search)
  481. /* Common part of res_nsearch and res_search. */
  482. static int
  483. context_search_common (struct resolv_context *ctx,
  484. const char *name, int class, int type,
  485. unsigned char *answer, int anslen)
  486. {
  487. if (ctx == NULL)
  488. {
  489. RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
  490. return -1;
  491. }
  492. int result = __res_context_search (ctx, name, class, type, answer, anslen,
  493. NULL, NULL, NULL, NULL, NULL);
  494. __resolv_context_put (ctx);
  495. return result;
  496. }
  497. int
  498. res_nsearch(res_state statp,
  499. const char *name, /* domain name */
  500. int class, int type, /* class and type of query */
  501. u_char *answer, /* buffer to put answer */
  502. int anslen) /* size of answer */
  503. {
  504. return context_search_common
  505. (__resolv_context_get_override (statp), name, class, type, answer, anslen);
  506. }
  507. int
  508. res_search (const char *name, int class, int type,
  509. unsigned char *answer, int anslen)
  510. {
  511. return context_search_common
  512. (__resolv_context_get (), name, class, type, answer, anslen);
  513. }
  514. /* Perform a call on res_query on the concatenation of name and
  515. domain. */
  516. static int
  517. __res_context_querydomain (struct resolv_context *ctx,
  518. const char *name, const char *domain,
  519. int class, int type,
  520. unsigned char *answer, int anslen,
  521. unsigned char **answerp, unsigned char **answerp2,
  522. int *nanswerp2, int *resplen2,
  523. int *answerp2_malloced)
  524. {
  525. struct __res_state *statp = ctx->resp;
  526. char nbuf[MAXDNAME];
  527. const char *longname = nbuf;
  528. size_t n, d;
  529. if (domain == NULL) {
  530. n = strlen(name);
  531. /* Decrement N prior to checking it against MAXDNAME
  532. so that we detect a wrap to SIZE_MAX and return
  533. a reasonable error. */
  534. n--;
  535. if (n >= MAXDNAME - 1) {
  536. RES_SET_H_ERRNO(statp, NO_RECOVERY);
  537. return (-1);
  538. }
  539. longname = name;
  540. } else {
  541. n = strlen(name);
  542. d = strlen(domain);
  543. if (n + d + 1 >= MAXDNAME) {
  544. RES_SET_H_ERRNO(statp, NO_RECOVERY);
  545. return (-1);
  546. }
  547. sprintf(nbuf, "%s.%s", name, domain);
  548. }
  549. return __res_context_query (ctx, longname, class, type, answer,
  550. anslen, answerp, answerp2, nanswerp2,
  551. resplen2, answerp2_malloced);
  552. }
  553. /* Common part of res_nquerydomain and res_querydomain. */
  554. static int
  555. context_querydomain_common (struct resolv_context *ctx,
  556. const char *name, const char *domain,
  557. int class, int type,
  558. unsigned char *answer, int anslen)
  559. {
  560. if (ctx == NULL)
  561. {
  562. RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
  563. return -1;
  564. }
  565. int result = __res_context_querydomain (ctx, name, domain, class, type,
  566. answer, anslen,
  567. NULL, NULL, NULL, NULL, NULL);
  568. __resolv_context_put (ctx);
  569. return result;
  570. }
  571. int
  572. res_nquerydomain(res_state statp,
  573. const char *name,
  574. const char *domain,
  575. int class, int type, /* class and type of query */
  576. u_char *answer, /* buffer to put answer */
  577. int anslen) /* size of answer */
  578. {
  579. return context_querydomain_common
  580. (__resolv_context_get_override (statp),
  581. name, domain, class, type, answer, anslen);
  582. }
  583. int
  584. res_querydomain (const char *name, const char *domain, int class, int type,
  585. unsigned char *answer, int anslen)
  586. {
  587. return context_querydomain_common
  588. (__resolv_context_get (), name, domain, class, type, answer, anslen);
  589. }
  590. const char *
  591. __res_context_hostalias (struct resolv_context *ctx,
  592. const char *name, char *dst, size_t siz)
  593. {
  594. char *file, *cp1, *cp2;
  595. char buf[BUFSIZ];
  596. FILE *fp;
  597. if (ctx->resp->options & RES_NOALIASES)
  598. return (NULL);
  599. file = getenv("HOSTALIASES");
  600. if (file == NULL || (fp = fopen(file, "rce")) == NULL)
  601. return (NULL);
  602. setbuf(fp, NULL);
  603. buf[sizeof(buf) - 1] = '\0';
  604. while (fgets(buf, sizeof(buf), fp)) {
  605. for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
  606. ;
  607. if (!*cp1)
  608. break;
  609. *cp1 = '\0';
  610. if (ns_samename(buf, name) == 1) {
  611. while (isspace(*++cp1))
  612. ;
  613. if (!*cp1)
  614. break;
  615. for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
  616. ;
  617. *cp2 = '\0';
  618. strncpy(dst, cp1, siz - 1);
  619. dst[siz - 1] = '\0';
  620. fclose(fp);
  621. return (dst);
  622. }
  623. }
  624. fclose(fp);
  625. return (NULL);
  626. }
  627. libresolv_hidden_def (__res_context_hostalias)
  628. /* Common part of res_hostalias and hostalias. */
  629. static const char *
  630. context_hostalias_common (struct resolv_context *ctx,
  631. const char *name, char *dst, size_t siz)
  632. {
  633. if (ctx == NULL)
  634. {
  635. RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
  636. return NULL;
  637. }
  638. const char *result = __res_context_hostalias (ctx, name, dst, siz);
  639. __resolv_context_put (ctx);
  640. return result;
  641. }
  642. const char *
  643. res_hostalias (res_state statp, const char *name, char *dst, size_t siz)
  644. {
  645. return context_hostalias_common
  646. (__resolv_context_get_override (statp), name, dst, siz);
  647. }
  648. const char *
  649. hostalias (const char *name)
  650. {
  651. static char abuf[MAXDNAME];
  652. return context_hostalias_common
  653. (__resolv_context_get (), name, abuf, sizeof (abuf));
  654. }
  655. #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
  656. # undef res_query
  657. # undef res_querydomain
  658. # undef res_search
  659. weak_alias (__res_query, res_query);
  660. weak_alias (__res_querydomain, res_querydomain);
  661. weak_alias (__res_search, res_search);
  662. #endif