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 
35e8031f0aSraf #include "mt.h"
367c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
427c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
437c478bd9Sstevel@tonic-gate #include <netdir.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <syslog.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern int __td_setnodelay(int);
487c478bd9Sstevel@tonic-gate extern bool_t __rpc_is_local_host(const char *);
4961961e0fSrobinson extern bool_t __rpc_try_doors(const char *, bool_t *);
5061961e0fSrobinson extern CLIENT *_clnt_vc_create_timed(int, struct netbuf *, rpcprog_t,
517c478bd9Sstevel@tonic-gate 			rpcvers_t, uint_t, uint_t, const struct timeval *);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate CLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *,
547c478bd9Sstevel@tonic-gate 		rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #ifndef NETIDLEN
577c478bd9Sstevel@tonic-gate #define	NETIDLEN 32
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Generic client creation with version checking the value of
627c478bd9Sstevel@tonic-gate  * vers_out is set to the highest server supported value
637c478bd9Sstevel@tonic-gate  * vers_low <= vers_out <= vers_high  AND an error results
647c478bd9Sstevel@tonic-gate  * if this can not be done.
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  * It calls clnt_create_vers_timed() with a NULL value for the timeout
677c478bd9Sstevel@tonic-gate  * pointer, which indicates that the default timeout should be used.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate CLIENT *
clnt_create_vers(const char * hostname,const rpcprog_t prog,rpcvers_t * vers_out,const rpcvers_t vers_low,const rpcvers_t vers_high,const char * nettype)7061961e0fSrobinson clnt_create_vers(const char *hostname, const rpcprog_t prog,
71*77c0a660SToomas Soome     rpcvers_t *vers_out, const rpcvers_t vers_low,
72*77c0a660SToomas Soome     const rpcvers_t vers_high, const char *nettype)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
75*77c0a660SToomas Soome 	    vers_high, nettype, NULL));
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * This routine has the same definition as clnt_create_vers(),
807c478bd9Sstevel@tonic-gate  * except it takes an additional timeout parameter - a pointer to
817c478bd9Sstevel@tonic-gate  * a timeval structure.  A NULL value for the pointer indicates
827c478bd9Sstevel@tonic-gate  * that the default timeout value should be used.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate CLIENT *
clnt_create_vers_timed(const char * hostname,const rpcprog_t prog,rpcvers_t * vers_out,const rpcvers_t vers_low,const rpcvers_t vers_high,const char * nettype,const struct timeval * tp)8561961e0fSrobinson clnt_create_vers_timed(const char *hostname, const rpcprog_t prog,
8661961e0fSrobinson     rpcvers_t *vers_out, const rpcvers_t vers_low, const rpcvers_t vers_high,
877c478bd9Sstevel@tonic-gate     const char *nettype, const struct timeval *tp)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	CLIENT *clnt;
907c478bd9Sstevel@tonic-gate 	struct timeval to;
917c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
927c478bd9Sstevel@tonic-gate 	struct rpc_err rpcerr;
9361961e0fSrobinson 	rpcvers_t v_low, v_high;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
9661961e0fSrobinson 	if (clnt == NULL)
977c478bd9Sstevel@tonic-gate 		return (NULL);
987c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
997c478bd9Sstevel@tonic-gate 		to.tv_sec = 10;
1007c478bd9Sstevel@tonic-gate 		to.tv_usec = 0;
1017c478bd9Sstevel@tonic-gate 	} else
1027c478bd9Sstevel@tonic-gate 		to = *tp;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
105*77c0a660SToomas Soome 	    NULL, (xdrproc_t)xdr_void, NULL, to);
1067c478bd9Sstevel@tonic-gate 	if (rpc_stat == RPC_SUCCESS) {
1077c478bd9Sstevel@tonic-gate 		*vers_out = vers_high;
1087c478bd9Sstevel@tonic-gate 		return (clnt);
1097c478bd9Sstevel@tonic-gate 	}
11061961e0fSrobinson 	v_low = vers_low;
11161961e0fSrobinson 	v_high = vers_high;
11261961e0fSrobinson 	while (rpc_stat == RPC_PROGVERSMISMATCH && v_high > v_low) {
1137c478bd9Sstevel@tonic-gate 		unsigned int minvers, maxvers;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		clnt_geterr(clnt, &rpcerr);
1167c478bd9Sstevel@tonic-gate 		minvers = rpcerr.re_vers.low;
1177c478bd9Sstevel@tonic-gate 		maxvers = rpcerr.re_vers.high;
11861961e0fSrobinson 		if (maxvers < v_high)
11961961e0fSrobinson 			v_high = maxvers;
1207c478bd9Sstevel@tonic-gate 		else
12161961e0fSrobinson 			v_high--;
12261961e0fSrobinson 		if (minvers > v_low)
12361961e0fSrobinson 			v_low = minvers;
12461961e0fSrobinson 		if (v_low > v_high) {
1257c478bd9Sstevel@tonic-gate 			goto error;
1267c478bd9Sstevel@tonic-gate 		}
12761961e0fSrobinson 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)&v_high);
1287c478bd9Sstevel@tonic-gate 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
129*77c0a660SToomas Soome 		    NULL, (xdrproc_t)xdr_void, NULL, to);
1307c478bd9Sstevel@tonic-gate 		if (rpc_stat == RPC_SUCCESS) {
13161961e0fSrobinson 			*vers_out = v_high;
1327c478bd9Sstevel@tonic-gate 			return (clnt);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	clnt_geterr(clnt, &rpcerr);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate error:
1387c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = rpc_stat;
1397c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_error = rpcerr;
1407c478bd9Sstevel@tonic-gate 	clnt_destroy(clnt);
1417c478bd9Sstevel@tonic-gate 	return (NULL);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Top level client creation routine.
1467c478bd9Sstevel@tonic-gate  * Generic client creation: takes (servers name, program-number, nettype) and
1477c478bd9Sstevel@tonic-gate  * returns client handle. Default options are set, which the user can
1487c478bd9Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s.
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * It tries for all the netids in that particular class of netid until
1517c478bd9Sstevel@tonic-gate  * it succeeds.
1527c478bd9Sstevel@tonic-gate  * XXX The error message in the case of failure will be the one
1537c478bd9Sstevel@tonic-gate  * pertaining to the last create error.
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * It calls clnt_create_timed() with the default timeout.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate CLIENT *
clnt_create(const char * hostname,const rpcprog_t prog,const rpcvers_t vers,const char * nettype)15861961e0fSrobinson clnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
1597c478bd9Sstevel@tonic-gate     const char *nettype)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * This the routine has the same definition as clnt_create(),
1667c478bd9Sstevel@tonic-gate  * except it takes an additional timeout parameter - a pointer to
1677c478bd9Sstevel@tonic-gate  * a timeval structure.  A NULL value for the pointer indicates
1687c478bd9Sstevel@tonic-gate  * that the default timeout value should be used.
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * This function calls clnt_tp_create_timed().
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate CLIENT *
clnt_create_timed(const char * hostname,const rpcprog_t prog,const rpcvers_t vers,const char * netclass,const struct timeval * tp)17361961e0fSrobinson clnt_create_timed(const char *hostname, const rpcprog_t prog,
17461961e0fSrobinson     const rpcvers_t vers, const char *netclass, const struct timeval *tp)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1777c478bd9Sstevel@tonic-gate 	CLIENT *clnt = NULL;
1787c478bd9Sstevel@tonic-gate 	void *handle;
1797c478bd9Sstevel@tonic-gate 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
1807c478bd9Sstevel@tonic-gate 	struct rpc_err	save_cf_error;
1817c478bd9Sstevel@tonic-gate 	char nettype_array[NETIDLEN];
1827c478bd9Sstevel@tonic-gate 	char *nettype = &nettype_array[0];
1837c478bd9Sstevel@tonic-gate 	bool_t try_others;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (netclass == NULL)
1867c478bd9Sstevel@tonic-gate 		nettype = NULL;
1877c478bd9Sstevel@tonic-gate 	else {
1887c478bd9Sstevel@tonic-gate 		size_t len = strlen(netclass);
1897c478bd9Sstevel@tonic-gate 		if (len >= sizeof (nettype_array)) {
1907c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
19161961e0fSrobinson 			return (NULL);
1927c478bd9Sstevel@tonic-gate 		}
19361961e0fSrobinson 		(void) strcpy(nettype, netclass);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Check to see if a rendezvous over doors should be attempted.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	if (__rpc_try_doors(nettype, &try_others)) {
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * Make sure this is the local host.
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		if (__rpc_is_local_host(hostname)) {
2047c478bd9Sstevel@tonic-gate 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
2057c478bd9Sstevel@tonic-gate 				return (clnt);
2067c478bd9Sstevel@tonic-gate 			else {
2077c478bd9Sstevel@tonic-gate 				if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
20861961e0fSrobinson 					return (NULL);
2097c478bd9Sstevel@tonic-gate 				save_cf_stat = rpc_createerr.cf_stat;
2107c478bd9Sstevel@tonic-gate 				save_cf_error = rpc_createerr.cf_error;
2117c478bd9Sstevel@tonic-gate 			}
2127c478bd9Sstevel@tonic-gate 		} else {
2137c478bd9Sstevel@tonic-gate 			save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	if (!try_others)
2177c478bd9Sstevel@tonic-gate 		return (NULL);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
2207c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
22161961e0fSrobinson 		return (NULL);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
22461961e0fSrobinson 	while (clnt == NULL) {
2257c478bd9Sstevel@tonic-gate 		if ((nconf = __rpc_getconf(handle)) == NULL) {
2267c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
2277c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 		clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
2317c478bd9Sstevel@tonic-gate 		if (clnt)
2327c478bd9Sstevel@tonic-gate 			break;
2337c478bd9Sstevel@tonic-gate 		else {
2347c478bd9Sstevel@tonic-gate 			/*
2357c478bd9Sstevel@tonic-gate 			 *	Since we didn't get a name-to-address
2367c478bd9Sstevel@tonic-gate 			 *	translation failure here, we remember
2377c478bd9Sstevel@tonic-gate 			 *	this particular error.  The object of
2387c478bd9Sstevel@tonic-gate 			 *	this is to enable us to return to the
2397c478bd9Sstevel@tonic-gate 			 *	caller a more-specific error than the
2407c478bd9Sstevel@tonic-gate 			 *	unhelpful ``Name to address translation
2417c478bd9Sstevel@tonic-gate 			 *	failed'' which might well occur if we
2427c478bd9Sstevel@tonic-gate 			 *	merely returned the last error (because
2437c478bd9Sstevel@tonic-gate 			 *	the local loopbacks are typically the
2447c478bd9Sstevel@tonic-gate 			 *	last ones in /etc/netconfig and the most
2457c478bd9Sstevel@tonic-gate 			 *	likely to be unable to translate a host
2467c478bd9Sstevel@tonic-gate 			 *	name).  We also check for a more
2477c478bd9Sstevel@tonic-gate 			 *	meaningful error than ``unknown host
2487c478bd9Sstevel@tonic-gate 			 *	name'' for the same reasons.
2497c478bd9Sstevel@tonic-gate 			 */
2507c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
2517c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "clnt_create_timed: "
252*77c0a660SToomas Soome 				    "RPC_SYSTEMERROR.");
2537c478bd9Sstevel@tonic-gate 				break;
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
2577c478bd9Sstevel@tonic-gate 			    rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
2587c478bd9Sstevel@tonic-gate 				save_cf_stat = rpc_createerr.cf_stat;
2597c478bd9Sstevel@tonic-gate 				save_cf_error = rpc_createerr.cf_error;
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 *	Attempt to return an error more specific than ``Name to address
2667c478bd9Sstevel@tonic-gate 	 *	translation failed'' or ``unknown host name''
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
269*77c0a660SToomas Soome 	    rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
270*77c0a660SToomas Soome 	    (save_cf_stat != RPC_SUCCESS)) {
2717c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = save_cf_stat;
2727c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error = save_cf_error;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	__rpc_endconf(handle);
2757c478bd9Sstevel@tonic-gate 	return (clnt);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * Create a client handle for a well known service or a specific port on
2807c478bd9Sstevel@tonic-gate  * host. This routine bypasses rpcbind and can be use to construct a client
2817c478bd9Sstevel@tonic-gate  * handle to services that are not registered with rpcbind or where the remote
2827c478bd9Sstevel@tonic-gate  * rpcbind is not available, e.g., the remote rpcbind port is blocked by a
2837c478bd9Sstevel@tonic-gate  * firewall. We construct a client handle and then ping the service's NULL
2847c478bd9Sstevel@tonic-gate  * proc to see that the service is really available. If the caller supplies
2857c478bd9Sstevel@tonic-gate  * a non zero port number, the service name is ignored and the port will be
2867c478bd9Sstevel@tonic-gate  * used. A non-zero port number limits the protocol family to inet or inet6.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate CLIENT *
clnt_create_service_timed(const char * host,const char * service,const rpcprog_t prog,const rpcvers_t vers,const ushort_t port,const char * netclass,const struct timeval * tmout)2907c478bd9Sstevel@tonic-gate clnt_create_service_timed(const char *host, const char *service,
291*77c0a660SToomas Soome     const rpcprog_t prog, const rpcvers_t vers,
292*77c0a660SToomas Soome     const ushort_t port, const char *netclass,
293*77c0a660SToomas Soome     const struct timeval *tmout)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	int fd;
2967c478bd9Sstevel@tonic-gate 	void *handle;
2977c478bd9Sstevel@tonic-gate 	CLIENT *clnt = NULL;
2987c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
2997c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
3007c478bd9Sstevel@tonic-gate 	struct nd_addrlist *raddrs;
3017c478bd9Sstevel@tonic-gate 	struct t_bind *tbind = NULL;
3027c478bd9Sstevel@tonic-gate 	struct timeval to;
3037c478bd9Sstevel@tonic-gate 	char nettype_array[NETIDLEN];
3047c478bd9Sstevel@tonic-gate 	char *nettype = &nettype_array[0];
3057c478bd9Sstevel@tonic-gate 	char *hostname, *serv;
3067c478bd9Sstevel@tonic-gate 	bool_t try_others;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * handle const of netclass
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (netclass == NULL)
3127c478bd9Sstevel@tonic-gate 		nettype = NULL;
3137c478bd9Sstevel@tonic-gate 	else {
3147c478bd9Sstevel@tonic-gate 		size_t len = strlen(netclass);
3157c478bd9Sstevel@tonic-gate 		if (len >= sizeof (nettype_array)) {
3167c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
31761961e0fSrobinson 			return (NULL);
3187c478bd9Sstevel@tonic-gate 		}
31961961e0fSrobinson 		(void) strcpy(nettype, netclass);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (tmout == NULL) {
3237c478bd9Sstevel@tonic-gate 		to.tv_sec = 10;
3247c478bd9Sstevel@tonic-gate 		to.tv_usec = 0;
3257c478bd9Sstevel@tonic-gate 	} else
3267c478bd9Sstevel@tonic-gate 		to = *tmout;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if ((handle = __rpc_setconf(nettype)) == NULL) {
3297c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
33061961e0fSrobinson 		return (NULL);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * Sinct host, and service are const
3357c478bd9Sstevel@tonic-gate 	 */
3367c478bd9Sstevel@tonic-gate 	if (host == NULL || (hostname = strdup(host)) == NULL) {
3377c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3387c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL);
3397c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
34061961e0fSrobinson 		return (NULL);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (service == NULL)
3447c478bd9Sstevel@tonic-gate 		serv = NULL;
3457c478bd9Sstevel@tonic-gate 	else if ((serv = strdup(service ? service : "")) == NULL) {
3467c478bd9Sstevel@tonic-gate 		free(hostname);
3477c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3487c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
3497c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
35061961e0fSrobinson 		return (NULL);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	hs.h_host = hostname;
3547c478bd9Sstevel@tonic-gate 	hs.h_serv = port ? NULL : serv;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * Check to see if a rendezvous over doors should be attempted.
3587c478bd9Sstevel@tonic-gate 	 */
3597c478bd9Sstevel@tonic-gate 	if (__rpc_try_doors(nettype, &try_others)) {
3607c478bd9Sstevel@tonic-gate 		/*
3617c478bd9Sstevel@tonic-gate 		 * Make sure this is the local host.
3627c478bd9Sstevel@tonic-gate 		 */
3637c478bd9Sstevel@tonic-gate 		if (__rpc_is_local_host(hostname)) {
3647c478bd9Sstevel@tonic-gate 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
3657c478bd9Sstevel@tonic-gate 				goto done;
3667c478bd9Sstevel@tonic-gate 			else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
3677c478bd9Sstevel@tonic-gate 				goto done;
3687c478bd9Sstevel@tonic-gate 		} else {
3697c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	if (!try_others)
3737c478bd9Sstevel@tonic-gate 		goto done;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
37661961e0fSrobinson 	while (clnt == NULL) {
3777c478bd9Sstevel@tonic-gate 		tbind = NULL;
3787c478bd9Sstevel@tonic-gate 		if ((nconf = __rpc_getconf(handle)) == NULL) {
3797c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
3807c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		if (port) {
3857c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
3867c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6) != 0)
3877c478bd9Sstevel@tonic-gate 				continue;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
3917c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
3927c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
3937c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = t_errno;
3947c478bd9Sstevel@tonic-gate 			continue;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
397004388ebScasper 		RPC_RAISEFD(fd);
3987c478bd9Sstevel@tonic-gate 
39945916cd2Sjpk 		__rpc_set_mac_options(fd, nconf, prog);
40045916cd2Sjpk 
4017c478bd9Sstevel@tonic-gate 		if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
4027c478bd9Sstevel@tonic-gate 		    == NULL) {
40361961e0fSrobinson 			(void) t_close(fd);
4047c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
4057c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
4067c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = t_errno;
4077c478bd9Sstevel@tonic-gate 			continue;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) {
4117c478bd9Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
4127c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
4137c478bd9Sstevel@tonic-gate 			if (tbind)
41461961e0fSrobinson 				(void) t_free((char *)tbind, T_BIND);
41561961e0fSrobinson 			(void) t_close(fd);
4167c478bd9Sstevel@tonic-gate 			continue;
4177c478bd9Sstevel@tonic-gate 		}
41861961e0fSrobinson 		(void) memcpy(tbind->addr.buf, raddrs->n_addrs->buf,
4197c478bd9Sstevel@tonic-gate 		    raddrs->n_addrs->len);
4207c478bd9Sstevel@tonic-gate 		tbind->addr.len = raddrs->n_addrs->len;
4217c478bd9Sstevel@tonic-gate 		netdir_free((void *)raddrs, ND_ADDRLIST);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		if (port) {
424*77c0a660SToomas Soome 			if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
4257c478bd9Sstevel@tonic-gate 				((struct sockaddr_in *)
426*77c0a660SToomas Soome 				    tbind->addr.buf)->sin_port = htons(port);
427*77c0a660SToomas Soome 			} else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
4287c478bd9Sstevel@tonic-gate 				((struct sockaddr_in6 *)
429*77c0a660SToomas Soome 				    tbind->addr.buf)->sin6_port = htons(port);
430*77c0a660SToomas Soome 			}
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr,
434*77c0a660SToomas Soome 		    prog, vers, 0, 0, &to);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		if (clnt == NULL) {
4377c478bd9Sstevel@tonic-gate 			if (tbind)
43861961e0fSrobinson 				(void) t_free((char *)tbind, T_BIND);
43961961e0fSrobinson 			(void) t_close(fd);
4407c478bd9Sstevel@tonic-gate 			continue;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 
44361961e0fSrobinson 		(void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, NULL);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		/*
4467c478bd9Sstevel@tonic-gate 		 * Check if we can reach the server with this clnt handle
4477c478bd9Sstevel@tonic-gate 		 * Other clnt_create calls do a ping by contacting the
4487c478bd9Sstevel@tonic-gate 		 * remote rpcbind, here will just try to execute the service's
4497c478bd9Sstevel@tonic-gate 		 * NULL proc.
4507c478bd9Sstevel@tonic-gate 		 */
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC,
453*77c0a660SToomas Soome 		    xdr_void, 0, xdr_void, 0, to);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = rpc_callerr.re_status;
4567c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat != RPC_SUCCESS) {
4597c478bd9Sstevel@tonic-gate 			clnt_destroy(clnt);
4607c478bd9Sstevel@tonic-gate 			clnt = NULL;
4617c478bd9Sstevel@tonic-gate 			if (tbind)
46261961e0fSrobinson 				(void) t_free((char *)tbind, T_BIND);
4637c478bd9Sstevel@tonic-gate 			continue;
464*77c0a660SToomas Soome 		} else {
4657c478bd9Sstevel@tonic-gate 			break;
466*77c0a660SToomas Soome 		}
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	__rpc_endconf(handle);
4707c478bd9Sstevel@tonic-gate 	if (tbind)
47161961e0fSrobinson 		(void) t_free((char *)tbind, T_BIND);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate done:
4747c478bd9Sstevel@tonic-gate 	if (hostname)
4757c478bd9Sstevel@tonic-gate 		free(hostname);
4767c478bd9Sstevel@tonic-gate 	if (serv)
4777c478bd9Sstevel@tonic-gate 		free(serv);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	return (clnt);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * Generic client creation: takes (servers name, program-number, netconf) and
4847c478bd9Sstevel@tonic-gate  * returns client handle. Default options are set, which the user can
4857c478bd9Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s : clnt_control()
4867c478bd9Sstevel@tonic-gate  * It finds out the server address from rpcbind and calls clnt_tli_create().
4877c478bd9Sstevel@tonic-gate  *
4887c478bd9Sstevel@tonic-gate  * It calls clnt_tp_create_timed() with the default timeout.
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate CLIENT *
clnt_tp_create(const char * hostname,const rpcprog_t prog,const rpcvers_t vers,const struct netconfig * nconf)49161961e0fSrobinson clnt_tp_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
4927c478bd9Sstevel@tonic-gate     const struct netconfig *nconf)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * This has the same definition as clnt_tp_create(), except it
4997c478bd9Sstevel@tonic-gate  * takes an additional parameter - a pointer to a timeval structure.
5007c478bd9Sstevel@tonic-gate  * A NULL value for the timeout pointer indicates that the default
5017c478bd9Sstevel@tonic-gate  * value for the timeout should be used.
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate CLIENT *
clnt_tp_create_timed(const char * hostname,const rpcprog_t prog,const rpcvers_t vers,const struct netconfig * nconf,const struct timeval * tp)50461961e0fSrobinson clnt_tp_create_timed(const char *hostname, const rpcprog_t prog,
50561961e0fSrobinson     const rpcvers_t vers, const struct netconfig *nconf,
50661961e0fSrobinson     const struct timeval *tp)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	struct netbuf *svcaddr;			/* servers address */
5097c478bd9Sstevel@tonic-gate 	CLIENT *cl = NULL;			/* client handle */
5107c478bd9Sstevel@tonic-gate 	extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t,
5117c478bd9Sstevel@tonic-gate 	    struct netconfig *, char *, CLIENT **, struct timeval *);
5127c478bd9Sstevel@tonic-gate 
51361961e0fSrobinson 	if (nconf == NULL) {
5147c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
51561961e0fSrobinson 		return (NULL);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * Get the address of the server
5207c478bd9Sstevel@tonic-gate 	 */
521*77c0a660SToomas Soome 	svcaddr = __rpcb_findaddr_timed(prog, vers,
522*77c0a660SToomas Soome 	    (struct netconfig *)nconf, (char *)hostname,
523*77c0a660SToomas Soome 	    &cl, (struct timeval *)tp);
524*77c0a660SToomas Soome 	if (svcaddr == NULL) {
5257c478bd9Sstevel@tonic-gate 		/* appropriate error number is set by rpcbind libraries */
52661961e0fSrobinson 		return (NULL);
5277c478bd9Sstevel@tonic-gate 	}
52861961e0fSrobinson 	if (cl == NULL) {
5297c478bd9Sstevel@tonic-gate 		cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
530*77c0a660SToomas Soome 		    prog, vers, 0, 0, tp);
5317c478bd9Sstevel@tonic-gate 	} else {
5327c478bd9Sstevel@tonic-gate 		/* Reuse the CLIENT handle and change the appropriate fields */
5337c478bd9Sstevel@tonic-gate 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
5347c478bd9Sstevel@tonic-gate 			if (cl->cl_netid == NULL) {
5357c478bd9Sstevel@tonic-gate 				cl->cl_netid = strdup(nconf->nc_netid);
5367c478bd9Sstevel@tonic-gate 				if (cl->cl_netid == NULL) {
5377c478bd9Sstevel@tonic-gate 					netdir_free((char *)svcaddr, ND_ADDR);
5387c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
5397c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
540*77c0a660SToomas Soome 					    "clnt_tp_create_timed: "
541*77c0a660SToomas Soome 					    "strdup failed.");
54261961e0fSrobinson 					return (NULL);
5437c478bd9Sstevel@tonic-gate 				}
5447c478bd9Sstevel@tonic-gate 			}
5457c478bd9Sstevel@tonic-gate 			if (cl->cl_tp == NULL) {
5467c478bd9Sstevel@tonic-gate 				cl->cl_tp = strdup(nconf->nc_device);
5477c478bd9Sstevel@tonic-gate 				if (cl->cl_tp == NULL) {
5487c478bd9Sstevel@tonic-gate 					netdir_free((char *)svcaddr, ND_ADDR);
5497c478bd9Sstevel@tonic-gate 					if (cl->cl_netid)
5507c478bd9Sstevel@tonic-gate 						free(cl->cl_netid);
5517c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
5527c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
553*77c0a660SToomas Soome 					    "clnt_tp_create_timed: "
554*77c0a660SToomas Soome 					    "strdup failed.");
55561961e0fSrobinson 					return (NULL);
5567c478bd9Sstevel@tonic-gate 				}
5577c478bd9Sstevel@tonic-gate 			}
5587c478bd9Sstevel@tonic-gate 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
5597c478bd9Sstevel@tonic-gate 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
5607c478bd9Sstevel@tonic-gate 		} else {
5617c478bd9Sstevel@tonic-gate 			CLNT_DESTROY(cl);
5627c478bd9Sstevel@tonic-gate 			cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
563*77c0a660SToomas Soome 			    prog, vers, 0, 0, tp);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	netdir_free((char *)svcaddr, ND_ADDR);
5677c478bd9Sstevel@tonic-gate 	return (cl);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Generic client creation:  returns client handle.
5727c478bd9Sstevel@tonic-gate  * Default options are set, which the user can
5737c478bd9Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s : clnt_control().
5747c478bd9Sstevel@tonic-gate  * If fd is RPC_ANYFD, it will be opened using nconf.
5757c478bd9Sstevel@tonic-gate  * It will be bound if not so.
5767c478bd9Sstevel@tonic-gate  * If sizes are 0; appropriate defaults will be chosen.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate CLIENT *
clnt_tli_create(const int fd,const struct netconfig * nconf,struct netbuf * svcaddr,const rpcprog_t prog,const rpcvers_t vers,const uint_t sendsz,const uint_t recvsz)57961961e0fSrobinson clnt_tli_create(const int fd, const struct netconfig *nconf,
58061961e0fSrobinson     struct netbuf *svcaddr, const rpcprog_t prog, const rpcvers_t vers,
58161961e0fSrobinson     const uint_t sendsz, const uint_t recvsz)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz,
584*77c0a660SToomas Soome 	    recvsz, NULL));
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * This has the same definition as clnt_tli_create(), except it
5897c478bd9Sstevel@tonic-gate  * takes an additional parameter - a pointer to a timeval structure.
5907c478bd9Sstevel@tonic-gate  *
5917c478bd9Sstevel@tonic-gate  * Not a public interface. This is for clnt_create_timed,
5927c478bd9Sstevel@tonic-gate  * clnt_create_vers_times, clnt_tp_create_timed to pass down  the
5937c478bd9Sstevel@tonic-gate  * timeout value to COTS creation routine.
5947c478bd9Sstevel@tonic-gate  * (for bug 4049792: clnt_create_timed does not time out)
5957c478bd9Sstevel@tonic-gate  */
5967c478bd9Sstevel@tonic-gate CLIENT *
_clnt_tli_create_timed(int fd,const struct netconfig * nconf,struct netbuf * svcaddr,rpcprog_t prog,rpcvers_t vers,uint_t sendsz,uint_t recvsz,const struct timeval * tp)5977c478bd9Sstevel@tonic-gate _clnt_tli_create_timed(int fd, const struct netconfig *nconf,
598*77c0a660SToomas Soome     struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz,
599*77c0a660SToomas Soome     uint_t recvsz, const struct timeval *tp)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	CLIENT *cl;			/* client handle */
6027c478bd9Sstevel@tonic-gate 	struct t_info tinfo;		/* transport info */
603*77c0a660SToomas Soome 	bool_t madefd = FALSE;		/* whether fd opened here */
6047c478bd9Sstevel@tonic-gate 	t_scalar_t servtype;
6057c478bd9Sstevel@tonic-gate 	int retval;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (fd == RPC_ANYFD) {
60861961e0fSrobinson 		if (nconf == NULL) {
6097c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
61061961e0fSrobinson 			return (NULL);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		fd = t_open(nconf->nc_device, O_RDWR, NULL);
6147c478bd9Sstevel@tonic-gate 		if (fd == -1)
6157c478bd9Sstevel@tonic-gate 			goto err;
616004388ebScasper 		RPC_RAISEFD(fd);
6177c478bd9Sstevel@tonic-gate 		madefd = TRUE;
61845916cd2Sjpk 		__rpc_set_mac_options(fd, nconf, prog);
61961961e0fSrobinson 		if (t_bind(fd, NULL, NULL) == -1)
62061961e0fSrobinson 			goto err;
6217c478bd9Sstevel@tonic-gate 		switch (nconf->nc_semantics) {
6227c478bd9Sstevel@tonic-gate 		case NC_TPI_CLTS:
6237c478bd9Sstevel@tonic-gate 			servtype = T_CLTS;
6247c478bd9Sstevel@tonic-gate 			break;
6257c478bd9Sstevel@tonic-gate 		case NC_TPI_COTS:
6267c478bd9Sstevel@tonic-gate 			servtype = T_COTS;
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		case NC_TPI_COTS_ORD:
6297c478bd9Sstevel@tonic-gate 			servtype = T_COTS_ORD;
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 		default:
6327c478bd9Sstevel@tonic-gate 			if (t_getinfo(fd, &tinfo) == -1)
6337c478bd9Sstevel@tonic-gate 				goto err;
6347c478bd9Sstevel@tonic-gate 			servtype = tinfo.servtype;
6357c478bd9Sstevel@tonic-gate 			break;
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 	} else {
6387c478bd9Sstevel@tonic-gate 		int state;		/* Current state of provider */
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * Sync the opened fd.
6427c478bd9Sstevel@tonic-gate 		 * Check whether bound or not, else bind it
6437c478bd9Sstevel@tonic-gate 		 */
6447c478bd9Sstevel@tonic-gate 		if (((state = t_sync(fd)) == -1) ||
64561961e0fSrobinson 		    ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) ||
6467c478bd9Sstevel@tonic-gate 		    (t_getinfo(fd, &tinfo) == -1))
6477c478bd9Sstevel@tonic-gate 			goto err;
6487c478bd9Sstevel@tonic-gate 		servtype = tinfo.servtype;
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	switch (servtype) {
6527c478bd9Sstevel@tonic-gate 	case T_COTS:
6537c478bd9Sstevel@tonic-gate 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
654*77c0a660SToomas Soome 		    recvsz, tp);
6557c478bd9Sstevel@tonic-gate 		break;
6567c478bd9Sstevel@tonic-gate 	case T_COTS_ORD:
6577c478bd9Sstevel@tonic-gate 		if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
6587c478bd9Sstevel@tonic-gate 		    (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) {
6597c478bd9Sstevel@tonic-gate 			retval =  __td_setnodelay(fd);
6607c478bd9Sstevel@tonic-gate 			if (retval == -1)
6617c478bd9Sstevel@tonic-gate 				goto err;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
664*77c0a660SToomas Soome 		    recvsz, tp);
6657c478bd9Sstevel@tonic-gate 		break;
6667c478bd9Sstevel@tonic-gate 	case T_CLTS:
6677c478bd9Sstevel@tonic-gate 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
6687c478bd9Sstevel@tonic-gate 		break;
6697c478bd9Sstevel@tonic-gate 	default:
6707c478bd9Sstevel@tonic-gate 		goto err;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
67361961e0fSrobinson 	if (cl == NULL)
6747c478bd9Sstevel@tonic-gate 		goto err1; /* borrow errors from clnt_dg/vc creates */
6757c478bd9Sstevel@tonic-gate 	if (nconf) {
6767c478bd9Sstevel@tonic-gate 		cl->cl_netid = strdup(nconf->nc_netid);
6777c478bd9Sstevel@tonic-gate 		if (cl->cl_netid == NULL) {
6787c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6797c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
6807c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = 0;
6817c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
682*77c0a660SToomas Soome 			    "clnt_tli_create: strdup failed");
6837c478bd9Sstevel@tonic-gate 			goto err1;
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 		cl->cl_tp = strdup(nconf->nc_device);
6867c478bd9Sstevel@tonic-gate 		if (cl->cl_tp == NULL) {
6877c478bd9Sstevel@tonic-gate 			if (cl->cl_netid)
6887c478bd9Sstevel@tonic-gate 				free(cl->cl_netid);
6897c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6907c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
6917c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = 0;
6927c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
693*77c0a660SToomas Soome 			    "clnt_tli_create: strdup failed");
6947c478bd9Sstevel@tonic-gate 			goto err1;
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	} else {
6977c478bd9Sstevel@tonic-gate 		struct netconfig *nc;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) {
7007c478bd9Sstevel@tonic-gate 			if (nc->nc_netid) {
7017c478bd9Sstevel@tonic-gate 				cl->cl_netid = strdup(nc->nc_netid);
7027c478bd9Sstevel@tonic-gate 				if (cl->cl_netid == NULL) {
7037c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7047c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_errno = errno;
7057c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_terrno = 0;
7067c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
707*77c0a660SToomas Soome 					    "clnt_tli_create: "
708*77c0a660SToomas Soome 					    "strdup failed");
7097c478bd9Sstevel@tonic-gate 					goto err1;
7107c478bd9Sstevel@tonic-gate 				}
7117c478bd9Sstevel@tonic-gate 			}
7127c478bd9Sstevel@tonic-gate 			if (nc->nc_device) {
7137c478bd9Sstevel@tonic-gate 				cl->cl_tp = strdup(nc->nc_device);
7147c478bd9Sstevel@tonic-gate 				if (cl->cl_tp == NULL) {
7157c478bd9Sstevel@tonic-gate 					if (cl->cl_netid)
7167c478bd9Sstevel@tonic-gate 						free(cl->cl_netid);
7177c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7187c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_errno = errno;
7197c478bd9Sstevel@tonic-gate 					rpc_createerr.cf_error.re_terrno = 0;
7207c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
721*77c0a660SToomas Soome 					    "clnt_tli_create: "
722*77c0a660SToomas Soome 					    "strdup failed");
7237c478bd9Sstevel@tonic-gate 					goto err1;
7247c478bd9Sstevel@tonic-gate 				}
7257c478bd9Sstevel@tonic-gate 			}
7267c478bd9Sstevel@tonic-gate 			freenetconfigent(nc);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		if (cl->cl_netid == NULL)
7297c478bd9Sstevel@tonic-gate 			cl->cl_netid = "";
7307c478bd9Sstevel@tonic-gate 		if (cl->cl_tp == NULL)
7317c478bd9Sstevel@tonic-gate 			cl->cl_tp = "";
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	if (madefd) {
73461961e0fSrobinson 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
73561961e0fSrobinson /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
7367c478bd9Sstevel@tonic-gate 	};
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	return (cl);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate err:
7417c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_TLIERROR;
7427c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_error.re_errno = errno;
7437c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_error.re_terrno = t_errno;
7447c478bd9Sstevel@tonic-gate err1:	if (madefd)
7457c478bd9Sstevel@tonic-gate 		(void) t_close(fd);
74661961e0fSrobinson 	return (NULL);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate  *  To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
7517c478bd9Sstevel@tonic-gate  *  we try to not use them.  The __rpc_raise_fd() routine will dup
7527c478bd9Sstevel@tonic-gate  *  a descriptor to a higher value.  If we fail to do it, we continue
7537c478bd9Sstevel@tonic-gate  *  to use the old one (and hope for the best).
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate int
__rpc_raise_fd(int fd)7567c478bd9Sstevel@tonic-gate __rpc_raise_fd(int fd)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	int nfd;
7597c478bd9Sstevel@tonic-gate 
760004388ebScasper 	if ((nfd = fcntl(fd, F_DUPFD, RPC_MINFD)) == -1)
7617c478bd9Sstevel@tonic-gate 		return (fd);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	if (t_sync(nfd) == -1) {
76461961e0fSrobinson 		(void) close(nfd);
7657c478bd9Sstevel@tonic-gate 		return (fd);
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	if (t_close(fd) == -1) {
7697c478bd9Sstevel@tonic-gate 		/* this is okay, we will syslog an error, then use the new fd */
7707c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
771*77c0a660SToomas Soome 		    "could not t_close() fd %d; mem & fd leak", fd);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	return (nfd);
7757c478bd9Sstevel@tonic-gate }
776