1 /*
2  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #include <port_before.h>
7 #include <thread.h>
8 #include <errno.h>
9 #include <netdb.h>
10 #include <malloc.h>
11 #include <string.h>
12 #include <resolv_mt.h>
13 #include <irs.h>
14 #include <port_after.h>
15 
16 /*
17  * much of the original version of sunw_mtxtxres.c was incorporated into
18  * ISC libbind as resolv/mtctxres.c. The following bits have not yet made
19  * it into ISC libbind.
20  */
21 
22 /*
23  * There used to be a private, MT-safe resolver interface that used TSD
24  * to store per-thread _res, h_errno, etc. We continue to provide the
25  * access functions __res_get_res() and __res_get_h_errno() so that binaries
26  * that used the private interface will continue to work.
27  */
28 
29 #ifdef	_res
30 #undef	_res
31 #endif
32 
33 extern struct __res_state	*__res_state(void);
34 
35 struct __res_state *
__res_get_res(void)36 __res_get_res(void) {
37 	return (__res_state());
38 }
39 
40 
41 #ifdef	h_errno
42 #undef	h_errno
43 #endif
44 
45 extern int			*__h_errno(void);
46 
47 int *
__res_get_h_errno(void)48 __res_get_h_errno(void) {
49 	return (__h_errno());
50 }
51 
52 
53 #ifdef SUNW_HOSTS_FALLBACK
54 
55 /*
56  * When the name service switch calls libresolv, it doesn't want fallback
57  * to /etc/hosts, so we provide a method to turn it off.
58  */
59 
60 void
__res_set_no_hosts_fallback(void)61 __res_set_no_hosts_fallback(void) {
62 	___mtctxres()->no_hosts_fallback_private = 1;
63 }
64 
65 void
__res_unset_no_hosts_fallback(void)66 __res_unset_no_hosts_fallback(void) {
67 	___mtctxres()->no_hosts_fallback_private = 0;
68 }
69 
70 int
__res_no_hosts_fallback(void)71 __res_no_hosts_fallback(void) {
72 	return (___mtctxres()->no_hosts_fallback_private);
73 }
74 
75 #endif  /* SUNW_HOSTS_FALLBACK */
76 
77 #ifdef	SUNW_OVERRIDE_RETRY
78 
79 /*
80  * The NS switch wants to be able to override the number of retries.
81  */
82 
83 int
__res_override_retry(int retry)84 __res_override_retry(int retry) {
85 	___mtctxres()->retry_private = retry;
86 	/*
87 	 * This function doesn't really need a return value; saving the
88 	 * old retry setting, and restoring it, is handled by __res_retry()
89 	 * and __res_retry_reset() below. However, the nss_dns library
90 	 * must have a private version of this function to be used when
91 	 * running with an old libresolv. That private nss_dns function
92 	 * needs a return value, and a function pointer is used to select
93 	 * the right function at runtime. Thus, __res_override_retry
94 	 * must have a function prototype consistent with the private
95 	 * nss_dns function, i.e., one that returns an int.
96 	 *
97 	 * Given that we do have a return value, that value must be zero.
98 	 * That's because retry_private == 0 is used to indicate that
99 	 * no override retry value is in effect, and the way we expect
100 	 * nss_dns to call us is:
101 	 *
102 	 *	int oldretry = __res_override_retry(N);
103 	 *	<whatever>
104 	 *	(void)__res_override_retry(old_retry);
105 	 */
106 	return (0);
107 }
108 
109 int
__res_retry(int retry)110 __res_retry(int retry) {
111 	mtctxres_t	*mt = ___mtctxres();
112 
113 	mt->retry_save = retry;
114 	return ((mt->retry_private != 0) ? mt->retry_private : retry);
115 }
116 
117 int
__res_retry_reset(void)118 __res_retry_reset(void) {
119 	mtctxres_t	*mt = ___mtctxres();
120 
121 	return (mt->retry_save);
122 }
123 
124 #endif	/* SUNW_OVERRIDE_RETRY */
125