xref: /titanic_41/usr/src/lib/libnsl/nss/gethostbyname_r.c (revision 7a286c471efbab8562f7655a82931904703fffe0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * gethostbyname_r() is defined in this file.  It is implemented on top of
31  *   _get_hostserv_inetnetdir_byname() which is also used to implement
32  *   netdir_getbyname() for inet family transports.  In turn the common code
33  *   uses the name service switch policy for "hosts" and "services" unless
34  *   the administrator chooses to bypass the name service switch by
35  *   specifying third-party supplied nametoaddr libs for inet transports
36  *   in /etc/netconfig.
37  *
38  * gethostbyaddr_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
39  *   and netdir_getbyaddr();
40  *
41  * The common code lives in netdir_inet.c.
42  *
43  * gethostent_r(), sethostent() and endhostent() are *not* implemented on top
44  *   of the common interface;  they go straight to the switch and are
45  *   defined in gethostent_r.c.
46  *
47  * There is absolutely no data sharing, not even the stayopen flag or
48  *   enumeration state, between gethostbyYY_r() and gethostent_r();
49  */
50 
51 #include "mt.h"
52 #include <netdb.h>
53 #include <netdir.h>
54 #include <sys/types.h>
55 #include <nss_netdir.h>
56 #include <string.h>
57 
58 extern struct netconfig *__rpc_getconfip();
59 
60 /*
61  * h_errno POLICY: The frontends expect the name service
62  * backends to modify the h_errno in "arg"; _switch_gethostbyYY_r()
63  * will copy that over onto user's h_errnop pointer. This h_errno is
64  * never used for "switching" -- status from nss_search serves
65  * the purpose. There is no explicit zeroing in the case of success.
66  */
67 
68 extern struct hostent *
69 _switch_gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
70 	int buflen, int *h_errnop);
71 
72 extern struct hostent *
73 _switch_gethostbyaddr_r(const char *addr, int length, int type,
74 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
75 
76 #ifdef PIC
77 struct hostent *
78 _uncached_gethostbyname_r(const char *nam, struct hostent *result,
79 	char *buffer, int buflen, int *h_errnop)
80 {
81 	return (_switch_gethostbyname_r(nam, result,
82 	buffer, buflen, h_errnop));
83 }
84 
85 struct hostent *
86 _uncached_gethostbyaddr_r(const char *addr, int length, int type,
87 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
88 {
89 	return (_switch_gethostbyaddr_r(addr, length, type,
90 					result, buffer, buflen, h_errnop));
91 }
92 
93 #endif
94 
95 extern struct hostent *
96 gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
97 	int buflen, int *h_errnop);
98 
99 extern struct hostent *
100 gethostbyaddr_r(const char *addr, int length, int type,
101 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
102 
103 struct hostent *
104 gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
105 	int buflen, int *h_errnop)
106 {
107 	struct netconfig *nconf;
108 	struct	nss_netdirbyname_in nssin;
109 	union	nss_netdirbyname_out nssout;
110 	int neterr, dummy;
111 
112 	if (h_errnop == NULL)
113 		h_errnop = &dummy;
114 
115 	if (strlen(nam) == 0) {
116 		*h_errnop = HOST_NOT_FOUND;
117 		return (NULL);
118 	}
119 
120 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
121 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
122 		*h_errnop = NO_RECOVERY;
123 		return (NULL);
124 	}
125 
126 	nssin.op_t = NSS_HOST;
127 	nssin.arg.nss.host.name = nam;
128 	nssin.arg.nss.host.buf = buffer;
129 	nssin.arg.nss.host.buflen = buflen;
130 
131 	nssout.nss.host.hent = result;
132 	nssout.nss.host.herrno_p = h_errnop;
133 
134 	/*
135 	 * We pass in nconf and let the implementation of the long-named func
136 	 * decide whether to use the switch based on nc_nlookups.
137 	 */
138 	neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
139 
140 	(void) freenetconfigent(nconf);
141 	if (neterr != ND_OK)
142 		return (NULL);
143 	return (nssout.nss.host.hent);
144 }
145 
146 struct hostent *
147 gethostbyaddr_r(const char *addr, int length, int type,
148 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
149 {
150 	struct netconfig *nconf;
151 	struct	nss_netdirbyaddr_in nssin;
152 	union	nss_netdirbyaddr_out nssout;
153 	int neterr, dummy;
154 
155 	if (h_errnop == NULL)
156 		h_errnop = &dummy;
157 
158 	if (type != AF_INET) {
159 		*h_errnop = HOST_NOT_FOUND;
160 		return (NULL);
161 	}
162 
163 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
164 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
165 		*h_errnop = NO_RECOVERY;
166 		return (NULL);
167 	}
168 
169 	nssin.op_t = NSS_HOST;
170 	nssin.arg.nss.host.addr = addr;
171 	nssin.arg.nss.host.len = length;
172 	nssin.arg.nss.host.type = type;
173 	nssin.arg.nss.host.buf = buffer;
174 	nssin.arg.nss.host.buflen = buflen;
175 
176 	nssout.nss.host.hent = result;
177 	nssout.nss.host.herrno_p = h_errnop;
178 
179 	/*
180 	 * We pass in nconf and let the implementation of this long-named func
181 	 * decide whether to use the switch based on nc_nlookups.
182 	 */
183 	neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
184 
185 	(void) freenetconfigent(nconf);
186 	if (neterr != ND_OK)
187 		return (NULL);
188 	return (nssout.nss.host.hent);
189 }
190