xref: /illumos-gate/usr/src/lib/libnsl/nss/gethostbyname_r.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*61961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * lib/libnsl/nss/gethostbyname_r.c
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * gethostbyname_r() is defined in this file.  It is implemented on top of
347c478bd9Sstevel@tonic-gate  *   _get_hostserv_inetnetdir_byname() which is also used to implement
357c478bd9Sstevel@tonic-gate  *   netdir_getbyname() for inet family transports.  In turn the common code
367c478bd9Sstevel@tonic-gate  *   uses the name service switch policy for "hosts" and "services" unless
377c478bd9Sstevel@tonic-gate  *   the administrator chooses to bypass the name service switch by
387c478bd9Sstevel@tonic-gate  *   specifying third-party supplied nametoaddr libs for inet transports
397c478bd9Sstevel@tonic-gate  *   in /etc/netconfig.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * gethostbyaddr_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
427c478bd9Sstevel@tonic-gate  *   and netdir_getbyaddr();
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The common code lives in netdir_inet.c.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * gethostent_r(), sethostent() and endhostent() are *not* implemented on top
477c478bd9Sstevel@tonic-gate  *   of the common interface;  they go straight to the switch and are
487c478bd9Sstevel@tonic-gate  *   defined in gethostent_r.c.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * There is absolutely no data sharing, not even the stayopen flag or
517c478bd9Sstevel@tonic-gate  *   enumeration state, between gethostbyYY_r() and gethostent_r();
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <netdb.h>
557c478bd9Sstevel@tonic-gate #include <netdir.h>
567c478bd9Sstevel@tonic-gate #include <sys/types.h>
577c478bd9Sstevel@tonic-gate #include <nss_netdir.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip();
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * h_errno POLICY: The frontends expect the name service
647c478bd9Sstevel@tonic-gate  * backends to modify the h_errno in "arg"; _switch_gethostbyYY_r()
657c478bd9Sstevel@tonic-gate  * will copy that over onto user's h_errnop pointer. This h_errno is
667c478bd9Sstevel@tonic-gate  * never used for "switching" -- status from nss_search serves
677c478bd9Sstevel@tonic-gate  * the purpose. There is no explicit zeroing in the case of success.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate extern struct hostent *
717c478bd9Sstevel@tonic-gate _switch_gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
727c478bd9Sstevel@tonic-gate 	int buflen, int *h_errnop);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate extern struct hostent *
757c478bd9Sstevel@tonic-gate _switch_gethostbyaddr_r(const char *addr, int length, int type,
767c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #ifdef PIC
797c478bd9Sstevel@tonic-gate struct hostent *
807c478bd9Sstevel@tonic-gate _uncached_gethostbyname_r(const char *nam, struct hostent *result,
817c478bd9Sstevel@tonic-gate 	char *buffer, int buflen, int *h_errnop)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	return (_switch_gethostbyname_r(nam, result,
847c478bd9Sstevel@tonic-gate 	buffer, buflen, h_errnop));
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate struct hostent *
887c478bd9Sstevel@tonic-gate _uncached_gethostbyaddr_r(const char *addr, int length, int type,
897c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	return (_switch_gethostbyaddr_r(addr, length, type,
927c478bd9Sstevel@tonic-gate 					result, buffer, buflen, h_errnop));
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate extern struct hostent *
987c478bd9Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
997c478bd9Sstevel@tonic-gate 	int buflen, int *h_errnop);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate extern struct hostent *
1027c478bd9Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
1037c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate struct hostent *
1067c478bd9Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
1077c478bd9Sstevel@tonic-gate 	int buflen, int *h_errnop)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1107c478bd9Sstevel@tonic-gate 	struct	nss_netdirbyname_in nssin;
1117c478bd9Sstevel@tonic-gate 	union	nss_netdirbyname_out nssout;
1127c478bd9Sstevel@tonic-gate 	int neterr, dummy;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (h_errnop == NULL)
1157c478bd9Sstevel@tonic-gate 		h_errnop = &dummy;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (strlen(nam) == 0) {
1187c478bd9Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
119*61961e0fSrobinson 		return (NULL);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
1237c478bd9Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
1247c478bd9Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
125*61961e0fSrobinson 		return (NULL);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
1297c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.name = nam;
1307c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
1317c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	nssout.nss.host.hent = result;
1347c478bd9Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of the long-named func
1387c478bd9Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
1397c478bd9Sstevel@tonic-gate 	 */
1407c478bd9Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
143*61961e0fSrobinson 	if (neterr != ND_OK)
144*61961e0fSrobinson 		return (NULL);
1457c478bd9Sstevel@tonic-gate 	return (nssout.nss.host.hent);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate struct hostent *
1497c478bd9Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
1507c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1537c478bd9Sstevel@tonic-gate 	struct	nss_netdirbyaddr_in nssin;
1547c478bd9Sstevel@tonic-gate 	union	nss_netdirbyaddr_out nssout;
1557c478bd9Sstevel@tonic-gate 	int neterr;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (type != AF_INET) {
1587c478bd9Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
159*61961e0fSrobinson 		return (NULL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
1637c478bd9Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
1647c478bd9Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
165*61961e0fSrobinson 		return (NULL);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
1697c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.addr = addr;
1707c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.len = length;
1717c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.type = type;
1727c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
1737c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	nssout.nss.host.hent = result;
1767c478bd9Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of this long-named func
1807c478bd9Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
185*61961e0fSrobinson 	if (neterr != ND_OK)
186*61961e0fSrobinson 		return (NULL);
1877c478bd9Sstevel@tonic-gate 	return (nssout.nss.host.hent);
1887c478bd9Sstevel@tonic-gate }
189