xref: /onnv-gate/usr/src/lib/libnsl/nss/gethostbyname_r.c (revision 2830:5228d1267a01)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21132Srobinson 
220Sstevel@tonic-gate /*
231219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * gethostbyname_r() is defined in this file.  It is implemented on top of
310Sstevel@tonic-gate  *   _get_hostserv_inetnetdir_byname() which is also used to implement
320Sstevel@tonic-gate  *   netdir_getbyname() for inet family transports.  In turn the common code
330Sstevel@tonic-gate  *   uses the name service switch policy for "hosts" and "services" unless
340Sstevel@tonic-gate  *   the administrator chooses to bypass the name service switch by
350Sstevel@tonic-gate  *   specifying third-party supplied nametoaddr libs for inet transports
360Sstevel@tonic-gate  *   in /etc/netconfig.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * gethostbyaddr_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
390Sstevel@tonic-gate  *   and netdir_getbyaddr();
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * The common code lives in netdir_inet.c.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * gethostent_r(), sethostent() and endhostent() are *not* implemented on top
440Sstevel@tonic-gate  *   of the common interface;  they go straight to the switch and are
450Sstevel@tonic-gate  *   defined in gethostent_r.c.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * There is absolutely no data sharing, not even the stayopen flag or
480Sstevel@tonic-gate  *   enumeration state, between gethostbyYY_r() and gethostent_r();
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
511219Sraf #include "mt.h"
520Sstevel@tonic-gate #include <netdb.h>
530Sstevel@tonic-gate #include <netdir.h>
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate #include <nss_netdir.h>
560Sstevel@tonic-gate #include <string.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip();
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * h_errno POLICY: The frontends expect the name service
620Sstevel@tonic-gate  * backends to modify the h_errno in "arg"; _switch_gethostbyYY_r()
630Sstevel@tonic-gate  * will copy that over onto user's h_errnop pointer. This h_errno is
640Sstevel@tonic-gate  * never used for "switching" -- status from nss_search serves
650Sstevel@tonic-gate  * the purpose. There is no explicit zeroing in the case of success.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate extern struct hostent *
690Sstevel@tonic-gate _switch_gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
700Sstevel@tonic-gate 	int buflen, int *h_errnop);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate extern struct hostent *
730Sstevel@tonic-gate _switch_gethostbyaddr_r(const char *addr, int length, int type,
740Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #ifdef PIC
770Sstevel@tonic-gate struct hostent *
_uncached_gethostbyname_r(const char * nam,struct hostent * result,char * buffer,int buflen,int * h_errnop)780Sstevel@tonic-gate _uncached_gethostbyname_r(const char *nam, struct hostent *result,
790Sstevel@tonic-gate 	char *buffer, int buflen, int *h_errnop)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	return (_switch_gethostbyname_r(nam, result,
820Sstevel@tonic-gate 	buffer, buflen, h_errnop));
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate struct hostent *
_uncached_gethostbyaddr_r(const char * addr,int length,int type,struct hostent * result,char * buffer,int buflen,int * h_errnop)860Sstevel@tonic-gate _uncached_gethostbyaddr_r(const char *addr, int length, int type,
870Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	return (_switch_gethostbyaddr_r(addr, length, type,
900Sstevel@tonic-gate 					result, buffer, buflen, h_errnop));
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate 
950Sstevel@tonic-gate extern struct hostent *
960Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
970Sstevel@tonic-gate 	int buflen, int *h_errnop);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate extern struct hostent *
1000Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
1010Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate struct hostent *
gethostbyname_r(const char * nam,struct hostent * result,char * buffer,int buflen,int * h_errnop)1040Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
1050Sstevel@tonic-gate 	int buflen, int *h_errnop)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	struct netconfig *nconf;
1080Sstevel@tonic-gate 	struct	nss_netdirbyname_in nssin;
1090Sstevel@tonic-gate 	union	nss_netdirbyname_out nssout;
1100Sstevel@tonic-gate 	int neterr, dummy;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (h_errnop == NULL)
1130Sstevel@tonic-gate 		h_errnop = &dummy;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (strlen(nam) == 0) {
1160Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
117132Srobinson 		return (NULL);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
1210Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
1220Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
123132Srobinson 		return (NULL);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
1270Sstevel@tonic-gate 	nssin.arg.nss.host.name = nam;
1280Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
1290Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	nssout.nss.host.hent = result;
1320Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of the long-named func
1360Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
1370Sstevel@tonic-gate 	 */
1380Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
141132Srobinson 	if (neterr != ND_OK)
142132Srobinson 		return (NULL);
1430Sstevel@tonic-gate 	return (nssout.nss.host.hent);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate struct hostent *
gethostbyaddr_r(const char * addr,int length,int type,struct hostent * result,char * buffer,int buflen,int * h_errnop)1470Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
1480Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	struct netconfig *nconf;
1510Sstevel@tonic-gate 	struct	nss_netdirbyaddr_in nssin;
1520Sstevel@tonic-gate 	union	nss_netdirbyaddr_out nssout;
153*2830Sdjl 	int neterr, dummy;
154*2830Sdjl 
155*2830Sdjl 	if (h_errnop == NULL)
156*2830Sdjl 		h_errnop = &dummy;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (type != AF_INET) {
1590Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
160132Srobinson 		return (NULL);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
1640Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
1650Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
166132Srobinson 		return (NULL);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
1700Sstevel@tonic-gate 	nssin.arg.nss.host.addr = addr;
1710Sstevel@tonic-gate 	nssin.arg.nss.host.len = length;
1720Sstevel@tonic-gate 	nssin.arg.nss.host.type = type;
1730Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
1740Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	nssout.nss.host.hent = result;
1770Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of this long-named func
1810Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
186132Srobinson 	if (neterr != ND_OK)
187132Srobinson 		return (NULL);
1880Sstevel@tonic-gate 	return (nssout.nss.host.hent);
1890Sstevel@tonic-gate }
190