xref: /titanic_51/usr/src/lib/libnsl/rpc/clnt_generic.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26e8031f0aSraf 
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
37e8031f0aSraf #include "mt.h"
387c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
447c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
457c478bd9Sstevel@tonic-gate #include <netdir.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <syslog.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate extern int __td_setnodelay(int);
507c478bd9Sstevel@tonic-gate extern bool_t __rpc_is_local_host(const char *);
5161961e0fSrobinson extern bool_t __rpc_try_doors(const char *, bool_t *);
5261961e0fSrobinson extern CLIENT *_clnt_vc_create_timed(int, struct netbuf *, rpcprog_t,
537c478bd9Sstevel@tonic-gate 			rpcvers_t, uint_t, uint_t, const struct timeval *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate CLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *,
567c478bd9Sstevel@tonic-gate 		rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifndef NETIDLEN
597c478bd9Sstevel@tonic-gate #define	NETIDLEN 32
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Generic client creation with version checking the value of
647c478bd9Sstevel@tonic-gate  * vers_out is set to the highest server supported value
657c478bd9Sstevel@tonic-gate  * vers_low <= vers_out <= vers_high  AND an error results
667c478bd9Sstevel@tonic-gate  * if this can not be done.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * It calls clnt_create_vers_timed() with a NULL value for the timeout
697c478bd9Sstevel@tonic-gate  * pointer, which indicates that the default timeout should be used.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate CLIENT *
7261961e0fSrobinson clnt_create_vers(const char *hostname, const rpcprog_t prog,
7361961e0fSrobinson 	rpcvers_t *vers_out, const rpcvers_t vers_low,
7461961e0fSrobinson 	const rpcvers_t vers_high, const char *nettype)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
777c478bd9Sstevel@tonic-gate 				vers_high, nettype, NULL));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * This routine has the same definition as clnt_create_vers(),
827c478bd9Sstevel@tonic-gate  * except it takes an additional timeout parameter - a pointer to
837c478bd9Sstevel@tonic-gate  * a timeval structure.  A NULL value for the pointer indicates
847c478bd9Sstevel@tonic-gate  * that the default timeout value should be used.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate CLIENT *
8761961e0fSrobinson clnt_create_vers_timed(const char *hostname, const rpcprog_t prog,
8861961e0fSrobinson     rpcvers_t *vers_out, const rpcvers_t vers_low, const rpcvers_t vers_high,
897c478bd9Sstevel@tonic-gate     const char *nettype, const struct timeval *tp)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	CLIENT *clnt;
927c478bd9Sstevel@tonic-gate 	struct timeval to;
937c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
947c478bd9Sstevel@tonic-gate 	struct rpc_err rpcerr;
9561961e0fSrobinson 	rpcvers_t v_low, v_high;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
9861961e0fSrobinson 	if (clnt == NULL)
997c478bd9Sstevel@tonic-gate 		return (NULL);
1007c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
1017c478bd9Sstevel@tonic-gate 		to.tv_sec = 10;
1027c478bd9Sstevel@tonic-gate 		to.tv_usec = 0;
1037c478bd9Sstevel@tonic-gate 	} else
1047c478bd9Sstevel@tonic-gate 		to = *tp;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
10761961e0fSrobinson 			NULL, (xdrproc_t)xdr_void, NULL, to);
1087c478bd9Sstevel@tonic-gate 	if (rpc_stat == RPC_SUCCESS) {
1097c478bd9Sstevel@tonic-gate 		*vers_out = vers_high;
1107c478bd9Sstevel@tonic-gate 		return (clnt);
1117c478bd9Sstevel@tonic-gate 	}
11261961e0fSrobinson 	v_low = vers_low;
11361961e0fSrobinson 	v_high = vers_high;
11461961e0fSrobinson 	while (rpc_stat == RPC_PROGVERSMISMATCH && v_high > v_low) {
1157c478bd9Sstevel@tonic-gate 		unsigned int minvers, maxvers;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		clnt_geterr(clnt, &rpcerr);
1187c478bd9Sstevel@tonic-gate 		minvers = rpcerr.re_vers.low;
1197c478bd9Sstevel@tonic-gate 		maxvers = rpcerr.re_vers.high;
12061961e0fSrobinson 		if (maxvers < v_high)
12161961e0fSrobinson 			v_high = maxvers;
1227c478bd9Sstevel@tonic-gate 		else
12361961e0fSrobinson 			v_high--;
12461961e0fSrobinson 		if (minvers > v_low)
12561961e0fSrobinson 			v_low = minvers;
12661961e0fSrobinson 		if (v_low > v_high) {
1277c478bd9Sstevel@tonic-gate 			goto error;
1287c478bd9Sstevel@tonic-gate 		}
12961961e0fSrobinson 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)&v_high);
1307c478bd9Sstevel@tonic-gate 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
13161961e0fSrobinson 				NULL, (xdrproc_t)xdr_void,
13261961e0fSrobinson 				NULL, to);
1337c478bd9Sstevel@tonic-gate 		if (rpc_stat == RPC_SUCCESS) {
13461961e0fSrobinson 			*vers_out = v_high;
1357c478bd9Sstevel@tonic-gate 			return (clnt);
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	clnt_geterr(clnt, &rpcerr);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate error:
1417c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = rpc_stat;
1427c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_error = rpcerr;
1437c478bd9Sstevel@tonic-gate 	clnt_destroy(clnt);
1447c478bd9Sstevel@tonic-gate 	return (NULL);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Top level client creation routine.
1497c478bd9Sstevel@tonic-gate  * Generic client creation: takes (servers name, program-number, nettype) and
1507c478bd9Sstevel@tonic-gate  * returns client handle. Default options are set, which the user can
1517c478bd9Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s.
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * It tries for all the netids in that particular class of netid until
1547c478bd9Sstevel@tonic-gate  * it succeeds.
1557c478bd9Sstevel@tonic-gate  * XXX The error message in the case of failure will be the one
1567c478bd9Sstevel@tonic-gate  * pertaining to the last create error.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * It calls clnt_create_timed() with the default timeout.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate CLIENT *
16161961e0fSrobinson clnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
1627c478bd9Sstevel@tonic-gate     const char *nettype)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * This the routine has the same definition as clnt_create(),
1697c478bd9Sstevel@tonic-gate  * except it takes an additional timeout parameter - a pointer to
1707c478bd9Sstevel@tonic-gate  * a timeval structure.  A NULL value for the pointer indicates
1717c478bd9Sstevel@tonic-gate  * that the default timeout value should be used.
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * This function calls clnt_tp_create_timed().
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate CLIENT *
17661961e0fSrobinson clnt_create_timed(const char *hostname, const rpcprog_t prog,
17761961e0fSrobinson     const rpcvers_t vers, const char *netclass, const struct timeval *tp)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1807c478bd9Sstevel@tonic-gate 	CLIENT *clnt = NULL;
1817c478bd9Sstevel@tonic-gate 	void *handle;
1827c478bd9Sstevel@tonic-gate 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
1837c478bd9Sstevel@tonic-gate 	struct rpc_err	save_cf_error;
1847c478bd9Sstevel@tonic-gate 	char nettype_array[NETIDLEN];
1857c478bd9Sstevel@tonic-gate 	char *nettype = &nettype_array[0];
1867c478bd9Sstevel@tonic-gate 	bool_t try_others;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (netclass == NULL)
1897c478bd9Sstevel@tonic-gate 		nettype = NULL;
1907c478bd9Sstevel@tonic-gate 	else {
1917c478bd9Sstevel@tonic-gate 		size_t len = strlen(netclass);
1927c478bd9Sstevel@tonic-gate 		if (len >= sizeof (nettype_array)) {
1937c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
19461961e0fSrobinson 			return (NULL);
1957c478bd9Sstevel@tonic-gate 		}
19661961e0fSrobinson 		(void) strcpy(nettype, netclass);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * Check to see if a rendezvous over doors should be attempted.
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	if (__rpc_try_doors(nettype, &try_others)) {
2037c478bd9Sstevel@tonic-gate 		/*
2047c478bd9Sstevel@tonic-gate 		 * Make sure this is the local host.
2057c478bd9Sstevel@tonic-gate 		 */
2067c478bd9Sstevel@tonic-gate 		if (__rpc_is_local_host(hostname)) {
2077c478bd9Sstevel@tonic-gate 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
2087c478bd9Sstevel@tonic-gate 				return (clnt);
2097c478bd9Sstevel@tonic-gate 			else {
2107c478bd9Sstevel@tonic-gate 				if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
21161961e0fSrobinson 					return (NULL);
2127c478bd9Sstevel@tonic-gate 				save_cf_stat = rpc_createerr.cf_stat;
2137c478bd9Sstevel@tonic-gate 				save_cf_error = rpc_createerr.cf_error;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 		} else {
2167c478bd9Sstevel@tonic-gate 			save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	if (!try_others)
2207c478bd9Sstevel@tonic-gate 		return (NULL);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
2237c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
22461961e0fSrobinson 		return (NULL);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
22761961e0fSrobinson 	while (clnt == NULL) {
2287c478bd9Sstevel@tonic-gate 		if ((nconf = __rpc_getconf(handle)) == NULL) {
2297c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
2307c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 		clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
2347c478bd9Sstevel@tonic-gate 		if (clnt)
2357c478bd9Sstevel@tonic-gate 			break;
2367c478bd9Sstevel@tonic-gate 		else {
2377c478bd9Sstevel@tonic-gate 			/*
2387c478bd9Sstevel@tonic-gate 			 *	Since we didn't get a name-to-address
2397c478bd9Sstevel@tonic-gate 			 *	translation failure here, we remember
2407c478bd9Sstevel@tonic-gate 			 *	this particular error.  The object of
2417c478bd9Sstevel@tonic-gate 			 *	this is to enable us to return to the
2427c478bd9Sstevel@tonic-gate 			 *	caller a more-specific error than the
2437c478bd9Sstevel@tonic-gate 			 *	unhelpful ``Name to address translation
2447c478bd9Sstevel@tonic-gate 			 *	failed'' which might well occur if we
2457c478bd9Sstevel@tonic-gate 			 *	merely returned the last error (because
2467c478bd9Sstevel@tonic-gate 			 *	the local loopbacks are typically the
2477c478bd9Sstevel@tonic-gate 			 *	last ones in /etc/netconfig and the most
2487c478bd9Sstevel@tonic-gate 			 *	likely to be unable to translate a host
2497c478bd9Sstevel@tonic-gate 			 *	name).  We also check for a more
2507c478bd9Sstevel@tonic-gate 			 *	meaningful error than ``unknown host
2517c478bd9Sstevel@tonic-gate 			 *	name'' for the same reasons.
2527c478bd9Sstevel@tonic-gate 			 */
2537c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
2547c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "clnt_create_timed: "
2557c478bd9Sstevel@tonic-gate 					"RPC_SYSTEMERROR.");
2567c478bd9Sstevel@tonic-gate 				break;
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
2607c478bd9Sstevel@tonic-gate 			    rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
2617c478bd9Sstevel@tonic-gate 				save_cf_stat = rpc_createerr.cf_stat;
2627c478bd9Sstevel@tonic-gate 				save_cf_error = rpc_createerr.cf_error;
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 *	Attempt to return an error more specific than ``Name to address
2697c478bd9Sstevel@tonic-gate 	 *	translation failed'' or ``unknown host name''
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
2727c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
2737c478bd9Sstevel@tonic-gate 					(save_cf_stat != RPC_SUCCESS)) {
2747c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = save_cf_stat;
2757c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error = save_cf_error;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	__rpc_endconf(handle);
2787c478bd9Sstevel@tonic-gate 	return (clnt);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate  * Create a client handle for a well known service or a specific port on
2837c478bd9Sstevel@tonic-gate  * host. This routine bypasses rpcbind and can be use to construct a client
2847c478bd9Sstevel@tonic-gate  * handle to services that are not registered with rpcbind or where the remote
2857c478bd9Sstevel@tonic-gate  * rpcbind is not available, e.g., the remote rpcbind port is blocked by a
2867c478bd9Sstevel@tonic-gate  * firewall. We construct a client handle and then ping the service's NULL
2877c478bd9Sstevel@tonic-gate  * proc to see that the service is really available. If the caller supplies
2887c478bd9Sstevel@tonic-gate  * a non zero port number, the service name is ignored and the port will be
2897c478bd9Sstevel@tonic-gate  * used. A non-zero port number limits the protocol family to inet or inet6.
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate CLIENT *
2937c478bd9Sstevel@tonic-gate clnt_create_service_timed(const char *host, const char *service,
2947c478bd9Sstevel@tonic-gate 			const rpcprog_t prog, const rpcvers_t vers,
2957c478bd9Sstevel@tonic-gate 			const ushort_t port, const char *netclass,
2967c478bd9Sstevel@tonic-gate 			const struct timeval *tmout)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int fd;
2997c478bd9Sstevel@tonic-gate 	void *handle;
3007c478bd9Sstevel@tonic-gate 	CLIENT *clnt = NULL;
3017c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
3027c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
3037c478bd9Sstevel@tonic-gate 	struct nd_addrlist *raddrs;
3047c478bd9Sstevel@tonic-gate 	struct t_bind *tbind = NULL;
3057c478bd9Sstevel@tonic-gate 	struct timeval to;
3067c478bd9Sstevel@tonic-gate 	char nettype_array[NETIDLEN];
3077c478bd9Sstevel@tonic-gate 	char *nettype = &nettype_array[0];
3087c478bd9Sstevel@tonic-gate 	char *hostname, *serv;
3097c478bd9Sstevel@tonic-gate 	bool_t try_others;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * handle const of netclass
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	if (netclass == NULL)
3157c478bd9Sstevel@tonic-gate 		nettype = NULL;
3167c478bd9Sstevel@tonic-gate 	else {
3177c478bd9Sstevel@tonic-gate 		size_t len = strlen(netclass);
3187c478bd9Sstevel@tonic-gate 		if (len >= sizeof (nettype_array)) {
3197c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
32061961e0fSrobinson 			return (NULL);
3217c478bd9Sstevel@tonic-gate 		}
32261961e0fSrobinson 		(void) strcpy(nettype, netclass);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (tmout == NULL) {
3267c478bd9Sstevel@tonic-gate 		to.tv_sec = 10;
3277c478bd9Sstevel@tonic-gate 		to.tv_usec = 0;
3287c478bd9Sstevel@tonic-gate 	} else
3297c478bd9Sstevel@tonic-gate 		to = *tmout;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	if ((handle = __rpc_setconf(nettype)) == NULL) {
3327c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
33361961e0fSrobinson 		return (NULL);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * Sinct host, and service are const
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	if (host == NULL || (hostname = strdup(host)) == NULL) {
3407c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3417c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL);
3427c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
34361961e0fSrobinson 		return (NULL);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (service == NULL)
3477c478bd9Sstevel@tonic-gate 		serv = NULL;
3487c478bd9Sstevel@tonic-gate 	else if ((serv = strdup(service ? service : "")) == NULL) {
3497c478bd9Sstevel@tonic-gate 		free(hostname);
3507c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3517c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
3527c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
35361961e0fSrobinson 		return (NULL);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	hs.h_host = hostname;
3577c478bd9Sstevel@tonic-gate 	hs.h_serv = port ? NULL : serv;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * Check to see if a rendezvous over doors should be attempted.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	if (__rpc_try_doors(nettype, &try_others)) {
3637c478bd9Sstevel@tonic-gate 		/*
3647c478bd9Sstevel@tonic-gate 		 * Make sure this is the local host.
3657c478bd9Sstevel@tonic-gate 		 */
3667c478bd9Sstevel@tonic-gate 		if (__rpc_is_local_host(hostname)) {
3677c478bd9Sstevel@tonic-gate 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
3687c478bd9Sstevel@tonic-gate 				goto done;
3697c478bd9Sstevel@tonic-gate 			else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
3707c478bd9Sstevel@tonic-gate 				goto done;
3717c478bd9Sstevel@tonic-gate 		} else {
3727c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	if (!try_others)
3767c478bd9Sstevel@tonic-gate 		goto done;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
37961961e0fSrobinson 	while (clnt == NULL) {
3807c478bd9Sstevel@tonic-gate 		tbind = NULL;
3817c478bd9Sstevel@tonic-gate 		if ((nconf = __rpc_getconf(handle)) == NULL) {
3827c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
3837c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		if (port) {
3887c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
3897c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6) != 0)
3907c478bd9Sstevel@tonic-gate 				continue;
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
3947c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
3957c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
3967c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = t_errno;
3977c478bd9Sstevel@tonic-gate 			continue;
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 
400*004388ebScasper 		RPC_RAISEFD(fd);
4017c478bd9Sstevel@tonic-gate 
40245916cd2Sjpk 		__rpc_set_mac_options(fd, nconf, prog);
40345916cd2Sjpk 
40461961e0fSrobinson 		/* LINTED pointer cast */
4057c478bd9Sstevel@tonic-gate 		if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
4067c478bd9Sstevel@tonic-gate 		    == NULL) {
40761961e0fSrobinson 			(void) t_close(fd);
4087c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
4097c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
4107c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = t_errno;
4117c478bd9Sstevel@tonic-gate 			continue;
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) {
4157c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
4167c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
4177c478bd9Sstevel@tonic-gate 			if (tbind)
41861961e0fSrobinson 				(void) t_free((char *)tbind, T_BIND);
41961961e0fSrobinson 			(void) t_close(fd);
4207c478bd9Sstevel@tonic-gate 			continue;
4217c478bd9Sstevel@tonic-gate 		}
42261961e0fSrobinson 		(void) memcpy(tbind->addr.buf, raddrs->n_addrs->buf,
4237c478bd9Sstevel@tonic-gate 		    raddrs->n_addrs->len);
4247c478bd9Sstevel@tonic-gate 		tbind->addr.len = raddrs->n_addrs->len;
4257c478bd9Sstevel@tonic-gate 		netdir_free((void *)raddrs, ND_ADDRLIST);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		if (port) {
4287c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
42961961e0fSrobinson 				/* LINTED pointer alignment */
4307c478bd9Sstevel@tonic-gate 				((struct sockaddr_in *)
4317c478bd9Sstevel@tonic-gate 				tbind->addr.buf)->sin_port = htons(port);
4327c478bd9Sstevel@tonic-gate 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
43361961e0fSrobinson 				/* LINTED pointer alignment */
4347c478bd9Sstevel@tonic-gate 				((struct sockaddr_in6 *)
4357c478bd9Sstevel@tonic-gate 				tbind->addr.buf)->sin6_port = htons(port);
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr,
4397c478bd9Sstevel@tonic-gate 					    prog, vers, 0, 0, &to);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		if (clnt == NULL) {
4427c478bd9Sstevel@tonic-gate 			if (tbind)
44361961e0fSrobinson 				(void) t_free((char *)tbind, T_BIND);
44461961e0fSrobinson 			(void) t_close(fd);
4457c478bd9Sstevel@tonic-gate 			continue;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 
44861961e0fSrobinson 		(void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, NULL);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		/*
4517c478bd9Sstevel@tonic-gate 		 * Check if we can reach the server with this clnt handle
4527c478bd9Sstevel@tonic-gate 		 * Other clnt_create calls do a ping by contacting the
4537c478bd9Sstevel@tonic-gate 		 * remote rpcbind, here will just try to execute the service's
4547c478bd9Sstevel@tonic-gate 		 * NULL proc.
4557c478bd9Sstevel@tonic-gate 		 */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC,
4587c478bd9Sstevel@tonic-gate 						xdr_void, 0, xdr_void, 0, to);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = rpc_callerr.re_status;
4617c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat != RPC_SUCCESS) {
4647c478bd9Sstevel@tonic-gate 			clnt_destroy(clnt);
4657c478bd9Sstevel@tonic-gate 			clnt = NULL;
4667c478bd9Sstevel@tonic-gate 			if (tbind)
46761961e0fSrobinson 				(void) t_free((char *)tbind, T_BIND);
4687c478bd9Sstevel@tonic-gate 			continue;
4697c478bd9Sstevel@tonic-gate 		} else
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	__rpc_endconf(handle);
4747c478bd9Sstevel@tonic-gate 	if (tbind)
47561961e0fSrobinson 		(void) t_free((char *)tbind, T_BIND);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate done:
4787c478bd9Sstevel@tonic-gate 	if (hostname)
4797c478bd9Sstevel@tonic-gate 		free(hostname);
4807c478bd9Sstevel@tonic-gate 	if (serv)
4817c478bd9Sstevel@tonic-gate 		free(serv);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (clnt);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * Generic client creation: takes (servers name, program-number, netconf) and
4887c478bd9Sstevel@tonic-gate  * returns client handle. Default options are set, which the user can
4897c478bd9Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s : clnt_control()
4907c478bd9Sstevel@tonic-gate  * It finds out the server address from rpcbind and calls clnt_tli_create().
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  * It calls clnt_tp_create_timed() with the default timeout.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate CLIENT *
49561961e0fSrobinson clnt_tp_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
4967c478bd9Sstevel@tonic-gate     const struct netconfig *nconf)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * This has the same definition as clnt_tp_create(), except it
5037c478bd9Sstevel@tonic-gate  * takes an additional parameter - a pointer to a timeval structure.
5047c478bd9Sstevel@tonic-gate  * A NULL value for the timeout pointer indicates that the default
5057c478bd9Sstevel@tonic-gate  * value for the timeout should be used.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate CLIENT *
50861961e0fSrobinson clnt_tp_create_timed(const char *hostname, const rpcprog_t prog,
50961961e0fSrobinson     const rpcvers_t vers, const struct netconfig *nconf,
51061961e0fSrobinson     const struct timeval *tp)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	struct netbuf *svcaddr;			/* servers address */
5137c478bd9Sstevel@tonic-gate 	CLIENT *cl = NULL;			/* client handle */
5147c478bd9Sstevel@tonic-gate 	extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t,
5157c478bd9Sstevel@tonic-gate 	    struct netconfig *, char *, CLIENT **, struct timeval *);
5167c478bd9Sstevel@tonic-gate 
51761961e0fSrobinson 	if (nconf == NULL) {
5187c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
51961961e0fSrobinson 		return (NULL);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * Get the address of the server
5247c478bd9Sstevel@tonic-gate 	 */
5257c478bd9Sstevel@tonic-gate 	if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
5267c478bd9Sstevel@tonic-gate 			(struct netconfig *)nconf, (char *)hostname,
5277c478bd9Sstevel@tonic-gate 			&cl, (struct timeval *)tp)) == NULL) {
5287c478bd9Sstevel@tonic-gate 		/* appropriate error number is set by rpcbind libraries */
52961961e0fSrobinson 		return (NULL);
5307c478bd9Sstevel@tonic-gate 	}
53161961e0fSrobinson 	if (cl == NULL) {
5327c478bd9Sstevel@tonic-gate 		cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
5337c478bd9Sstevel@tonic-gate 					prog, vers, 0, 0, tp);
5347c478bd9Sstevel@tonic-gate 	} else {
5357c478bd9Sstevel@tonic-gate 		/* Reuse the CLIENT handle and change the appropriate fields */
5367c478bd9Sstevel@tonic-gate 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
5377c478bd9Sstevel@tonic-gate 			if (cl->cl_netid == NULL) {
5387c478bd9Sstevel@tonic-gate 				cl->cl_netid = strdup(nconf->nc_netid);
5397c478bd9Sstevel@tonic-gate 				if (cl->cl_netid == NULL) {
5407c478bd9Sstevel@tonic-gate 					netdir_free((char *)svcaddr, ND_ADDR);
5417c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
5427c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
5437c478bd9Sstevel@tonic-gate 						"clnt_tp_create_timed: "
5447c478bd9Sstevel@tonic-gate 						"strdup failed.");
54561961e0fSrobinson 					return (NULL);
5467c478bd9Sstevel@tonic-gate 				}
5477c478bd9Sstevel@tonic-gate 			}
5487c478bd9Sstevel@tonic-gate 			if (cl->cl_tp == NULL) {
5497c478bd9Sstevel@tonic-gate 				cl->cl_tp = strdup(nconf->nc_device);
5507c478bd9Sstevel@tonic-gate 				if (cl->cl_tp == NULL) {
5517c478bd9Sstevel@tonic-gate 					netdir_free((char *)svcaddr, ND_ADDR);
5527c478bd9Sstevel@tonic-gate 					if (cl->cl_netid)
5537c478bd9Sstevel@tonic-gate 						free(cl->cl_netid);
5547c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
5557c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
5567c478bd9Sstevel@tonic-gate 						"clnt_tp_create_timed: "
5577c478bd9Sstevel@tonic-gate 						"strdup failed.");
55861961e0fSrobinson 					return (NULL);
5597c478bd9Sstevel@tonic-gate 				}
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
5627c478bd9Sstevel@tonic-gate 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
5637c478bd9Sstevel@tonic-gate 		} else {
5647c478bd9Sstevel@tonic-gate 			CLNT_DESTROY(cl);
5657c478bd9Sstevel@tonic-gate 			cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
5667c478bd9Sstevel@tonic-gate 					prog, vers, 0, 0, tp);
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 	netdir_free((char *)svcaddr, ND_ADDR);
5707c478bd9Sstevel@tonic-gate 	return (cl);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate /*
5747c478bd9Sstevel@tonic-gate  * Generic client creation:  returns client handle.
5757c478bd9Sstevel@tonic-gate  * Default options are set, which the user can
5767c478bd9Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s : clnt_control().
5777c478bd9Sstevel@tonic-gate  * If fd is RPC_ANYFD, it will be opened using nconf.
5787c478bd9Sstevel@tonic-gate  * It will be bound if not so.
5797c478bd9Sstevel@tonic-gate  * If sizes are 0; appropriate defaults will be chosen.
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate CLIENT *
58261961e0fSrobinson clnt_tli_create(const int fd, const struct netconfig *nconf,
58361961e0fSrobinson     struct netbuf *svcaddr, const rpcprog_t prog, const rpcvers_t vers,
58461961e0fSrobinson     const uint_t sendsz, const uint_t recvsz)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz,
5877c478bd9Sstevel@tonic-gate 		recvsz, NULL));
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate  * This has the same definition as clnt_tli_create(), except it
5927c478bd9Sstevel@tonic-gate  * takes an additional parameter - a pointer to a timeval structure.
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * Not a public interface. This is for clnt_create_timed,
5957c478bd9Sstevel@tonic-gate  * clnt_create_vers_times, clnt_tp_create_timed to pass down  the
5967c478bd9Sstevel@tonic-gate  * timeout value to COTS creation routine.
5977c478bd9Sstevel@tonic-gate  * (for bug 4049792: clnt_create_timed does not time out)
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate CLIENT *
6007c478bd9Sstevel@tonic-gate _clnt_tli_create_timed(int fd, const struct netconfig *nconf,
6017c478bd9Sstevel@tonic-gate 	struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz,
6027c478bd9Sstevel@tonic-gate 	uint_t recvsz, const struct timeval *tp)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	CLIENT *cl;			/* client handle */
6057c478bd9Sstevel@tonic-gate 	struct t_info tinfo;		/* transport info */
6067c478bd9Sstevel@tonic-gate 	bool_t madefd;			/* whether fd opened here */
6077c478bd9Sstevel@tonic-gate 	t_scalar_t servtype;
6087c478bd9Sstevel@tonic-gate 	int retval;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (fd == RPC_ANYFD) {
61161961e0fSrobinson 		if (nconf == NULL) {
6127c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
61361961e0fSrobinson 			return (NULL);
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		fd = t_open(nconf->nc_device, O_RDWR, NULL);
6177c478bd9Sstevel@tonic-gate 		if (fd == -1)
6187c478bd9Sstevel@tonic-gate 			goto err;
619*004388ebScasper 		RPC_RAISEFD(fd);
6207c478bd9Sstevel@tonic-gate 		madefd = TRUE;
62145916cd2Sjpk 		__rpc_set_mac_options(fd, nconf, prog);
62261961e0fSrobinson 		if (t_bind(fd, NULL, NULL) == -1)
6237c478bd9Sstevel@tonic-gate 			goto err;
6247c478bd9Sstevel@tonic-gate 		switch (nconf->nc_semantics) {
6257c478bd9Sstevel@tonic-gate 		case NC_TPI_CLTS:
6267c478bd9Sstevel@tonic-gate 			servtype = T_CLTS;
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		case NC_TPI_COTS:
6297c478bd9Sstevel@tonic-gate 			servtype = T_COTS;
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 		case NC_TPI_COTS_ORD:
6327c478bd9Sstevel@tonic-gate 			servtype = T_COTS_ORD;
6337c478bd9Sstevel@tonic-gate 			break;
6347c478bd9Sstevel@tonic-gate 		default:
6357c478bd9Sstevel@tonic-gate 			if (t_getinfo(fd, &tinfo) == -1)
6367c478bd9Sstevel@tonic-gate 				goto err;
6377c478bd9Sstevel@tonic-gate 			servtype = tinfo.servtype;
6387c478bd9Sstevel@tonic-gate 			break;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 	} else {
6417c478bd9Sstevel@tonic-gate 		int state;		/* Current state of provider */
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		/*
6447c478bd9Sstevel@tonic-gate 		 * Sync the opened fd.
6457c478bd9Sstevel@tonic-gate 		 * Check whether bound or not, else bind it
6467c478bd9Sstevel@tonic-gate 		 */
6477c478bd9Sstevel@tonic-gate 		if (((state = t_sync(fd)) == -1) ||
64861961e0fSrobinson 		    ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) ||
6497c478bd9Sstevel@tonic-gate 		    (t_getinfo(fd, &tinfo) == -1))
6507c478bd9Sstevel@tonic-gate 			goto err;
6517c478bd9Sstevel@tonic-gate 		servtype = tinfo.servtype;
6527c478bd9Sstevel@tonic-gate 		madefd = FALSE;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	switch (servtype) {
6567c478bd9Sstevel@tonic-gate 	case T_COTS:
6577c478bd9Sstevel@tonic-gate 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
6587c478bd9Sstevel@tonic-gate 				recvsz, tp);
6597c478bd9Sstevel@tonic-gate 		break;
6607c478bd9Sstevel@tonic-gate 	case T_COTS_ORD:
6617c478bd9Sstevel@tonic-gate 		if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
6627c478bd9Sstevel@tonic-gate 		    (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) {
6637c478bd9Sstevel@tonic-gate 			retval =  __td_setnodelay(fd);
6647c478bd9Sstevel@tonic-gate 			if (retval == -1)
6657c478bd9Sstevel@tonic-gate 				goto err;
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
6687c478bd9Sstevel@tonic-gate 			recvsz, tp);
6697c478bd9Sstevel@tonic-gate 		break;
6707c478bd9Sstevel@tonic-gate 	case T_CLTS:
6717c478bd9Sstevel@tonic-gate 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
6727c478bd9Sstevel@tonic-gate 		break;
6737c478bd9Sstevel@tonic-gate 	default:
6747c478bd9Sstevel@tonic-gate 		goto err;
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
67761961e0fSrobinson 	if (cl == NULL)
6787c478bd9Sstevel@tonic-gate 		goto err1; /* borrow errors from clnt_dg/vc creates */
6797c478bd9Sstevel@tonic-gate 	if (nconf) {
6807c478bd9Sstevel@tonic-gate 		cl->cl_netid = strdup(nconf->nc_netid);
6817c478bd9Sstevel@tonic-gate 		if (cl->cl_netid == NULL) {
6827c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6837c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
6847c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = 0;
6857c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
6867c478bd9Sstevel@tonic-gate 				"clnt_tli_create: strdup failed");
6877c478bd9Sstevel@tonic-gate 			goto err1;
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 		cl->cl_tp = strdup(nconf->nc_device);
6907c478bd9Sstevel@tonic-gate 		if (cl->cl_tp == NULL) {
6917c478bd9Sstevel@tonic-gate 			if (cl->cl_netid)
6927c478bd9Sstevel@tonic-gate 				free(cl->cl_netid);
6937c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6947c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
6957c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = 0;
6967c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
6977c478bd9Sstevel@tonic-gate 				"clnt_tli_create: strdup failed");
6987c478bd9Sstevel@tonic-gate 			goto err1;
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 	} else {
7017c478bd9Sstevel@tonic-gate 		struct netconfig *nc;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) {
7047c478bd9Sstevel@tonic-gate 			if (nc->nc_netid) {
7057c478bd9Sstevel@tonic-gate 				cl->cl_netid = strdup(nc->nc_netid);
7067c478bd9Sstevel@tonic-gate 				if (cl->cl_netid == NULL) {
7077c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7087c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_errno = errno;
7097c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_terrno = 0;
7107c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
7117c478bd9Sstevel@tonic-gate 						"clnt_tli_create: "
7127c478bd9Sstevel@tonic-gate 						"strdup failed");
7137c478bd9Sstevel@tonic-gate 					goto err1;
7147c478bd9Sstevel@tonic-gate 				}
7157c478bd9Sstevel@tonic-gate 			}
7167c478bd9Sstevel@tonic-gate 			if (nc->nc_device) {
7177c478bd9Sstevel@tonic-gate 				cl->cl_tp = strdup(nc->nc_device);
7187c478bd9Sstevel@tonic-gate 				if (cl->cl_tp == NULL) {
7197c478bd9Sstevel@tonic-gate 					if (cl->cl_netid)
7207c478bd9Sstevel@tonic-gate 						free(cl->cl_netid);
7217c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7227c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_errno = errno;
7237c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_terrno = 0;
7247c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
7257c478bd9Sstevel@tonic-gate 						"clnt_tli_create: "
7267c478bd9Sstevel@tonic-gate 						"strdup failed");
7277c478bd9Sstevel@tonic-gate 					goto err1;
7287c478bd9Sstevel@tonic-gate 				}
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 			freenetconfigent(nc);
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 		if (cl->cl_netid == NULL)
7337c478bd9Sstevel@tonic-gate 			cl->cl_netid = "";
7347c478bd9Sstevel@tonic-gate 		if (cl->cl_tp == NULL)
7357c478bd9Sstevel@tonic-gate 			cl->cl_tp = "";
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	if (madefd) {
73861961e0fSrobinson 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
73961961e0fSrobinson /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
7407c478bd9Sstevel@tonic-gate 	};
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	return (cl);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate err:
7457c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_TLIERROR;
7467c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_error.re_errno = errno;
7477c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_error.re_terrno = t_errno;
7487c478bd9Sstevel@tonic-gate err1:	if (madefd)
7497c478bd9Sstevel@tonic-gate 		(void) t_close(fd);
75061961e0fSrobinson 	return (NULL);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate /*
7547c478bd9Sstevel@tonic-gate  *  To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
7557c478bd9Sstevel@tonic-gate  *  we try to not use them.  The __rpc_raise_fd() routine will dup
7567c478bd9Sstevel@tonic-gate  *  a descriptor to a higher value.  If we fail to do it, we continue
7577c478bd9Sstevel@tonic-gate  *  to use the old one (and hope for the best).
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate int
7607c478bd9Sstevel@tonic-gate __rpc_raise_fd(int fd)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	int nfd;
7637c478bd9Sstevel@tonic-gate 
764*004388ebScasper 	if ((nfd = fcntl(fd, F_DUPFD, RPC_MINFD)) == -1)
7657c478bd9Sstevel@tonic-gate 		return (fd);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (t_sync(nfd) == -1) {
76861961e0fSrobinson 		(void) close(nfd);
7697c478bd9Sstevel@tonic-gate 		return (fd);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (t_close(fd) == -1) {
7737c478bd9Sstevel@tonic-gate 		/* this is okay, we will syslog an error, then use the new fd */
7747c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
7757c478bd9Sstevel@tonic-gate 			"could not t_close() fd %d; mem & fd leak", fd);
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	return (nfd);
7797c478bd9Sstevel@tonic-gate }
780