xref: /onnv-gate/usr/src/lib/libnsl/rpc/clnt_generic.c (revision 1914:8a8c5f225b1b)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * 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  */
261219Sraf 
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
371219Sraf #include "mt.h"
380Sstevel@tonic-gate #include "rpc_mt.h"
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <rpc/rpc.h>
440Sstevel@tonic-gate #include <rpc/nettype.h>
450Sstevel@tonic-gate #include <netdir.h>
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate extern int __td_setnodelay(int);
500Sstevel@tonic-gate extern bool_t __rpc_is_local_host(const char *);
51132Srobinson extern bool_t __rpc_try_doors(const char *, bool_t *);
52132Srobinson extern CLIENT *_clnt_vc_create_timed(int, struct netbuf *, rpcprog_t,
530Sstevel@tonic-gate 			rpcvers_t, uint_t, uint_t, const struct timeval *);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate CLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *,
560Sstevel@tonic-gate 		rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #ifndef NETIDLEN
590Sstevel@tonic-gate #define	NETIDLEN 32
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Generic client creation with version checking the value of
640Sstevel@tonic-gate  * vers_out is set to the highest server supported value
650Sstevel@tonic-gate  * vers_low <= vers_out <= vers_high  AND an error results
660Sstevel@tonic-gate  * if this can not be done.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * It calls clnt_create_vers_timed() with a NULL value for the timeout
690Sstevel@tonic-gate  * pointer, which indicates that the default timeout should be used.
700Sstevel@tonic-gate  */
710Sstevel@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)72132Srobinson clnt_create_vers(const char *hostname, const rpcprog_t prog,
73132Srobinson 	rpcvers_t *vers_out, const rpcvers_t vers_low,
74132Srobinson 	const rpcvers_t vers_high, const char *nettype)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
770Sstevel@tonic-gate 				vers_high, nettype, NULL));
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * This routine has the same definition as clnt_create_vers(),
820Sstevel@tonic-gate  * except it takes an additional timeout parameter - a pointer to
830Sstevel@tonic-gate  * a timeval structure.  A NULL value for the pointer indicates
840Sstevel@tonic-gate  * that the default timeout value should be used.
850Sstevel@tonic-gate  */
860Sstevel@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)87132Srobinson clnt_create_vers_timed(const char *hostname, const rpcprog_t prog,
88132Srobinson     rpcvers_t *vers_out, const rpcvers_t vers_low, const rpcvers_t vers_high,
890Sstevel@tonic-gate     const char *nettype, const struct timeval *tp)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	CLIENT *clnt;
920Sstevel@tonic-gate 	struct timeval to;
930Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
940Sstevel@tonic-gate 	struct rpc_err rpcerr;
95132Srobinson 	rpcvers_t v_low, v_high;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
98132Srobinson 	if (clnt == NULL)
990Sstevel@tonic-gate 		return (NULL);
1000Sstevel@tonic-gate 	if (tp == NULL) {
1010Sstevel@tonic-gate 		to.tv_sec = 10;
1020Sstevel@tonic-gate 		to.tv_usec = 0;
1030Sstevel@tonic-gate 	} else
1040Sstevel@tonic-gate 		to = *tp;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
107132Srobinson 			NULL, (xdrproc_t)xdr_void, NULL, to);
1080Sstevel@tonic-gate 	if (rpc_stat == RPC_SUCCESS) {
1090Sstevel@tonic-gate 		*vers_out = vers_high;
1100Sstevel@tonic-gate 		return (clnt);
1110Sstevel@tonic-gate 	}
112132Srobinson 	v_low = vers_low;
113132Srobinson 	v_high = vers_high;
114132Srobinson 	while (rpc_stat == RPC_PROGVERSMISMATCH && v_high > v_low) {
1150Sstevel@tonic-gate 		unsigned int minvers, maxvers;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		clnt_geterr(clnt, &rpcerr);
1180Sstevel@tonic-gate 		minvers = rpcerr.re_vers.low;
1190Sstevel@tonic-gate 		maxvers = rpcerr.re_vers.high;
120132Srobinson 		if (maxvers < v_high)
121132Srobinson 			v_high = maxvers;
1220Sstevel@tonic-gate 		else
123132Srobinson 			v_high--;
124132Srobinson 		if (minvers > v_low)
125132Srobinson 			v_low = minvers;
126132Srobinson 		if (v_low > v_high) {
1270Sstevel@tonic-gate 			goto error;
1280Sstevel@tonic-gate 		}
129132Srobinson 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)&v_high);
1300Sstevel@tonic-gate 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
131132Srobinson 				NULL, (xdrproc_t)xdr_void,
132132Srobinson 				NULL, to);
1330Sstevel@tonic-gate 		if (rpc_stat == RPC_SUCCESS) {
134132Srobinson 			*vers_out = v_high;
1350Sstevel@tonic-gate 			return (clnt);
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	clnt_geterr(clnt, &rpcerr);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate error:
1410Sstevel@tonic-gate 	rpc_createerr.cf_stat = rpc_stat;
1420Sstevel@tonic-gate 	rpc_createerr.cf_error = rpcerr;
1430Sstevel@tonic-gate 	clnt_destroy(clnt);
1440Sstevel@tonic-gate 	return (NULL);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * Top level client creation routine.
1490Sstevel@tonic-gate  * Generic client creation: takes (servers name, program-number, nettype) and
1500Sstevel@tonic-gate  * returns client handle. Default options are set, which the user can
1510Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s.
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * It tries for all the netids in that particular class of netid until
1540Sstevel@tonic-gate  * it succeeds.
1550Sstevel@tonic-gate  * XXX The error message in the case of failure will be the one
1560Sstevel@tonic-gate  * pertaining to the last create error.
1570Sstevel@tonic-gate  *
1580Sstevel@tonic-gate  * It calls clnt_create_timed() with the default timeout.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate CLIENT *
clnt_create(const char * hostname,const rpcprog_t prog,const rpcvers_t vers,const char * nettype)161132Srobinson clnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
1620Sstevel@tonic-gate     const char *nettype)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * This the routine has the same definition as clnt_create(),
1690Sstevel@tonic-gate  * except it takes an additional timeout parameter - a pointer to
1700Sstevel@tonic-gate  * a timeval structure.  A NULL value for the pointer indicates
1710Sstevel@tonic-gate  * that the default timeout value should be used.
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  * This function calls clnt_tp_create_timed().
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate CLIENT *
clnt_create_timed(const char * hostname,const rpcprog_t prog,const rpcvers_t vers,const char * netclass,const struct timeval * tp)176132Srobinson clnt_create_timed(const char *hostname, const rpcprog_t prog,
177132Srobinson     const rpcvers_t vers, const char *netclass, const struct timeval *tp)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	struct netconfig *nconf;
1800Sstevel@tonic-gate 	CLIENT *clnt = NULL;
1810Sstevel@tonic-gate 	void *handle;
1820Sstevel@tonic-gate 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
1830Sstevel@tonic-gate 	struct rpc_err	save_cf_error;
1840Sstevel@tonic-gate 	char nettype_array[NETIDLEN];
1850Sstevel@tonic-gate 	char *nettype = &nettype_array[0];
1860Sstevel@tonic-gate 	bool_t try_others;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (netclass == NULL)
1890Sstevel@tonic-gate 		nettype = NULL;
1900Sstevel@tonic-gate 	else {
1910Sstevel@tonic-gate 		size_t len = strlen(netclass);
1920Sstevel@tonic-gate 		if (len >= sizeof (nettype_array)) {
1930Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
194132Srobinson 			return (NULL);
1950Sstevel@tonic-gate 		}
196132Srobinson 		(void) strcpy(nettype, netclass);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * Check to see if a rendezvous over doors should be attempted.
2010Sstevel@tonic-gate 	 */
2020Sstevel@tonic-gate 	if (__rpc_try_doors(nettype, &try_others)) {
2030Sstevel@tonic-gate 		/*
2040Sstevel@tonic-gate 		 * Make sure this is the local host.
2050Sstevel@tonic-gate 		 */
2060Sstevel@tonic-gate 		if (__rpc_is_local_host(hostname)) {
2070Sstevel@tonic-gate 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
2080Sstevel@tonic-gate 				return (clnt);
2090Sstevel@tonic-gate 			else {
2100Sstevel@tonic-gate 				if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
211132Srobinson 					return (NULL);
2120Sstevel@tonic-gate 				save_cf_stat = rpc_createerr.cf_stat;
2130Sstevel@tonic-gate 				save_cf_error = rpc_createerr.cf_error;
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 		} else {
2160Sstevel@tonic-gate 			save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	if (!try_others)
2200Sstevel@tonic-gate 		return (NULL);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
2230Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
224132Srobinson 		return (NULL);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
227132Srobinson 	while (clnt == NULL) {
2280Sstevel@tonic-gate 		if ((nconf = __rpc_getconf(handle)) == NULL) {
2290Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
2300Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2310Sstevel@tonic-gate 			break;
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 		clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
2340Sstevel@tonic-gate 		if (clnt)
2350Sstevel@tonic-gate 			break;
2360Sstevel@tonic-gate 		else {
2370Sstevel@tonic-gate 			/*
2380Sstevel@tonic-gate 			 *	Since we didn't get a name-to-address
2390Sstevel@tonic-gate 			 *	translation failure here, we remember
2400Sstevel@tonic-gate 			 *	this particular error.  The object of
2410Sstevel@tonic-gate 			 *	this is to enable us to return to the
2420Sstevel@tonic-gate 			 *	caller a more-specific error than the
2430Sstevel@tonic-gate 			 *	unhelpful ``Name to address translation
2440Sstevel@tonic-gate 			 *	failed'' which might well occur if we
2450Sstevel@tonic-gate 			 *	merely returned the last error (because
2460Sstevel@tonic-gate 			 *	the local loopbacks are typically the
2470Sstevel@tonic-gate 			 *	last ones in /etc/netconfig and the most
2480Sstevel@tonic-gate 			 *	likely to be unable to translate a host
2490Sstevel@tonic-gate 			 *	name).  We also check for a more
2500Sstevel@tonic-gate 			 *	meaningful error than ``unknown host
2510Sstevel@tonic-gate 			 *	name'' for the same reasons.
2520Sstevel@tonic-gate 			 */
2530Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
2540Sstevel@tonic-gate 				syslog(LOG_ERR, "clnt_create_timed: "
2550Sstevel@tonic-gate 					"RPC_SYSTEMERROR.");
2560Sstevel@tonic-gate 				break;
2570Sstevel@tonic-gate 			}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
2600Sstevel@tonic-gate 			    rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
2610Sstevel@tonic-gate 				save_cf_stat = rpc_createerr.cf_stat;
2620Sstevel@tonic-gate 				save_cf_error = rpc_createerr.cf_error;
2630Sstevel@tonic-gate 			}
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/*
2680Sstevel@tonic-gate 	 *	Attempt to return an error more specific than ``Name to address
2690Sstevel@tonic-gate 	 *	translation failed'' or ``unknown host name''
2700Sstevel@tonic-gate 	 */
2710Sstevel@tonic-gate 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
2720Sstevel@tonic-gate 				rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
2730Sstevel@tonic-gate 					(save_cf_stat != RPC_SUCCESS)) {
2740Sstevel@tonic-gate 		rpc_createerr.cf_stat = save_cf_stat;
2750Sstevel@tonic-gate 		rpc_createerr.cf_error = save_cf_error;
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 	__rpc_endconf(handle);
2780Sstevel@tonic-gate 	return (clnt);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate  * Create a client handle for a well known service or a specific port on
2830Sstevel@tonic-gate  * host. This routine bypasses rpcbind and can be use to construct a client
2840Sstevel@tonic-gate  * handle to services that are not registered with rpcbind or where the remote
2850Sstevel@tonic-gate  * rpcbind is not available, e.g., the remote rpcbind port is blocked by a
2860Sstevel@tonic-gate  * firewall. We construct a client handle and then ping the service's NULL
2870Sstevel@tonic-gate  * proc to see that the service is really available. If the caller supplies
2880Sstevel@tonic-gate  * a non zero port number, the service name is ignored and the port will be
2890Sstevel@tonic-gate  * used. A non-zero port number limits the protocol family to inet or inet6.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate 
2920Sstevel@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)2930Sstevel@tonic-gate clnt_create_service_timed(const char *host, const char *service,
2940Sstevel@tonic-gate 			const rpcprog_t prog, const rpcvers_t vers,
2950Sstevel@tonic-gate 			const ushort_t port, const char *netclass,
2960Sstevel@tonic-gate 			const struct timeval *tmout)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	int fd;
2990Sstevel@tonic-gate 	void *handle;
3000Sstevel@tonic-gate 	CLIENT *clnt = NULL;
3010Sstevel@tonic-gate 	struct netconfig *nconf;
3020Sstevel@tonic-gate 	struct nd_hostserv hs;
3030Sstevel@tonic-gate 	struct nd_addrlist *raddrs;
3040Sstevel@tonic-gate 	struct t_bind *tbind = NULL;
3050Sstevel@tonic-gate 	struct timeval to;
3060Sstevel@tonic-gate 	char nettype_array[NETIDLEN];
3070Sstevel@tonic-gate 	char *nettype = &nettype_array[0];
3080Sstevel@tonic-gate 	char *hostname, *serv;
3090Sstevel@tonic-gate 	bool_t try_others;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
3120Sstevel@tonic-gate 	 * handle const of netclass
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	if (netclass == NULL)
3150Sstevel@tonic-gate 		nettype = NULL;
3160Sstevel@tonic-gate 	else {
3170Sstevel@tonic-gate 		size_t len = strlen(netclass);
3180Sstevel@tonic-gate 		if (len >= sizeof (nettype_array)) {
3190Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
320132Srobinson 			return (NULL);
3210Sstevel@tonic-gate 		}
322132Srobinson 		(void) strcpy(nettype, netclass);
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	if (tmout == NULL) {
3260Sstevel@tonic-gate 		to.tv_sec = 10;
3270Sstevel@tonic-gate 		to.tv_usec = 0;
3280Sstevel@tonic-gate 	} else
3290Sstevel@tonic-gate 		to = *tmout;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if ((handle = __rpc_setconf(nettype)) == NULL) {
3320Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
333132Srobinson 		return (NULL);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * Sinct host, and service are const
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	if (host == NULL || (hostname = strdup(host)) == NULL) {
3400Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3410Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL);
3420Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
343132Srobinson 		return (NULL);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (service == NULL)
3470Sstevel@tonic-gate 		serv = NULL;
3480Sstevel@tonic-gate 	else if ((serv = strdup(service ? service : "")) == NULL) {
3490Sstevel@tonic-gate 		free(hostname);
3500Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3510Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
3520Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
353132Srobinson 		return (NULL);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	hs.h_host = hostname;
3570Sstevel@tonic-gate 	hs.h_serv = port ? NULL : serv;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * Check to see if a rendezvous over doors should be attempted.
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	if (__rpc_try_doors(nettype, &try_others)) {
3630Sstevel@tonic-gate 		/*
3640Sstevel@tonic-gate 		 * Make sure this is the local host.
3650Sstevel@tonic-gate 		 */
3660Sstevel@tonic-gate 		if (__rpc_is_local_host(hostname)) {
3670Sstevel@tonic-gate 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
3680Sstevel@tonic-gate 				goto done;
3690Sstevel@tonic-gate 			else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
3700Sstevel@tonic-gate 				goto done;
3710Sstevel@tonic-gate 		} else {
3720Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 	if (!try_others)
3760Sstevel@tonic-gate 		goto done;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
379132Srobinson 	while (clnt == NULL) {
3800Sstevel@tonic-gate 		tbind = NULL;
3810Sstevel@tonic-gate 		if ((nconf = __rpc_getconf(handle)) == NULL) {
3820Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
3830Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3840Sstevel@tonic-gate 			break;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		if (port) {
3880Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
3890Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6) != 0)
3900Sstevel@tonic-gate 				continue;
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
3940Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
3950Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
3960Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = t_errno;
3970Sstevel@tonic-gate 			continue;
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 
400*1914Scasper 		RPC_RAISEFD(fd);
4010Sstevel@tonic-gate 
4021676Sjpk 		__rpc_set_mac_options(fd, nconf, prog);
4031676Sjpk 
404132Srobinson 		/* LINTED pointer cast */
4050Sstevel@tonic-gate 		if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
4060Sstevel@tonic-gate 		    == NULL) {
407132Srobinson 			(void) t_close(fd);
4080Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
4090Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
4100Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = t_errno;
4110Sstevel@tonic-gate 			continue;
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) {
4150Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
4160Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
4170Sstevel@tonic-gate 			if (tbind)
418132Srobinson 				(void) t_free((char *)tbind, T_BIND);
419132Srobinson 			(void) t_close(fd);
4200Sstevel@tonic-gate 			continue;
4210Sstevel@tonic-gate 		}
422132Srobinson 		(void) memcpy(tbind->addr.buf, raddrs->n_addrs->buf,
4230Sstevel@tonic-gate 		    raddrs->n_addrs->len);
4240Sstevel@tonic-gate 		tbind->addr.len = raddrs->n_addrs->len;
4250Sstevel@tonic-gate 		netdir_free((void *)raddrs, ND_ADDRLIST);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		if (port) {
4280Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
429132Srobinson 				/* LINTED pointer alignment */
4300Sstevel@tonic-gate 				((struct sockaddr_in *)
4310Sstevel@tonic-gate 				tbind->addr.buf)->sin_port = htons(port);
4320Sstevel@tonic-gate 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
433132Srobinson 				/* LINTED pointer alignment */
4340Sstevel@tonic-gate 				((struct sockaddr_in6 *)
4350Sstevel@tonic-gate 				tbind->addr.buf)->sin6_port = htons(port);
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 		clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr,
4390Sstevel@tonic-gate 					    prog, vers, 0, 0, &to);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		if (clnt == NULL) {
4420Sstevel@tonic-gate 			if (tbind)
443132Srobinson 				(void) t_free((char *)tbind, T_BIND);
444132Srobinson 			(void) t_close(fd);
4450Sstevel@tonic-gate 			continue;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 
448132Srobinson 		(void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, NULL);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		/*
4510Sstevel@tonic-gate 		 * Check if we can reach the server with this clnt handle
4520Sstevel@tonic-gate 		 * Other clnt_create calls do a ping by contacting the
4530Sstevel@tonic-gate 		 * remote rpcbind, here will just try to execute the service's
4540Sstevel@tonic-gate 		 * NULL proc.
4550Sstevel@tonic-gate 		 */
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC,
4580Sstevel@tonic-gate 						xdr_void, 0, xdr_void, 0, to);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = rpc_callerr.re_status;
4610Sstevel@tonic-gate 		rpc_createerr.cf_error.re_terrno = 0;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		if (rpc_createerr.cf_stat != RPC_SUCCESS) {
4640Sstevel@tonic-gate 			clnt_destroy(clnt);
4650Sstevel@tonic-gate 			clnt = NULL;
4660Sstevel@tonic-gate 			if (tbind)
467132Srobinson 				(void) t_free((char *)tbind, T_BIND);
4680Sstevel@tonic-gate 			continue;
4690Sstevel@tonic-gate 		} else
4700Sstevel@tonic-gate 			break;
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	__rpc_endconf(handle);
4740Sstevel@tonic-gate 	if (tbind)
475132Srobinson 		(void) t_free((char *)tbind, T_BIND);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate done:
4780Sstevel@tonic-gate 	if (hostname)
4790Sstevel@tonic-gate 		free(hostname);
4800Sstevel@tonic-gate 	if (serv)
4810Sstevel@tonic-gate 		free(serv);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	return (clnt);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate  * Generic client creation: takes (servers name, program-number, netconf) and
4880Sstevel@tonic-gate  * returns client handle. Default options are set, which the user can
4890Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s : clnt_control()
4900Sstevel@tonic-gate  * It finds out the server address from rpcbind and calls clnt_tli_create().
4910Sstevel@tonic-gate  *
4920Sstevel@tonic-gate  * It calls clnt_tp_create_timed() with the default timeout.
4930Sstevel@tonic-gate  */
4940Sstevel@tonic-gate CLIENT *
clnt_tp_create(const char * hostname,const rpcprog_t prog,const rpcvers_t vers,const struct netconfig * nconf)495132Srobinson clnt_tp_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
4960Sstevel@tonic-gate     const struct netconfig *nconf)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * This has the same definition as clnt_tp_create(), except it
5030Sstevel@tonic-gate  * takes an additional parameter - a pointer to a timeval structure.
5040Sstevel@tonic-gate  * A NULL value for the timeout pointer indicates that the default
5050Sstevel@tonic-gate  * value for the timeout should be used.
5060Sstevel@tonic-gate  */
5070Sstevel@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)508132Srobinson clnt_tp_create_timed(const char *hostname, const rpcprog_t prog,
509132Srobinson     const rpcvers_t vers, const struct netconfig *nconf,
510132Srobinson     const struct timeval *tp)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	struct netbuf *svcaddr;			/* servers address */
5130Sstevel@tonic-gate 	CLIENT *cl = NULL;			/* client handle */
5140Sstevel@tonic-gate 	extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t,
5150Sstevel@tonic-gate 	    struct netconfig *, char *, CLIENT **, struct timeval *);
5160Sstevel@tonic-gate 
517132Srobinson 	if (nconf == NULL) {
5180Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
519132Srobinson 		return (NULL);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * Get the address of the server
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
5260Sstevel@tonic-gate 			(struct netconfig *)nconf, (char *)hostname,
5270Sstevel@tonic-gate 			&cl, (struct timeval *)tp)) == NULL) {
5280Sstevel@tonic-gate 		/* appropriate error number is set by rpcbind libraries */
529132Srobinson 		return (NULL);
5300Sstevel@tonic-gate 	}
531132Srobinson 	if (cl == NULL) {
5320Sstevel@tonic-gate 		cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
5330Sstevel@tonic-gate 					prog, vers, 0, 0, tp);
5340Sstevel@tonic-gate 	} else {
5350Sstevel@tonic-gate 		/* Reuse the CLIENT handle and change the appropriate fields */
5360Sstevel@tonic-gate 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
5370Sstevel@tonic-gate 			if (cl->cl_netid == NULL) {
5380Sstevel@tonic-gate 				cl->cl_netid = strdup(nconf->nc_netid);
5390Sstevel@tonic-gate 				if (cl->cl_netid == NULL) {
5400Sstevel@tonic-gate 					netdir_free((char *)svcaddr, ND_ADDR);
5410Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
5420Sstevel@tonic-gate 					syslog(LOG_ERR,
5430Sstevel@tonic-gate 						"clnt_tp_create_timed: "
5440Sstevel@tonic-gate 						"strdup failed.");
545132Srobinson 					return (NULL);
5460Sstevel@tonic-gate 				}
5470Sstevel@tonic-gate 			}
5480Sstevel@tonic-gate 			if (cl->cl_tp == NULL) {
5490Sstevel@tonic-gate 				cl->cl_tp = strdup(nconf->nc_device);
5500Sstevel@tonic-gate 				if (cl->cl_tp == NULL) {
5510Sstevel@tonic-gate 					netdir_free((char *)svcaddr, ND_ADDR);
5520Sstevel@tonic-gate 					if (cl->cl_netid)
5530Sstevel@tonic-gate 						free(cl->cl_netid);
5540Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
5550Sstevel@tonic-gate 					syslog(LOG_ERR,
5560Sstevel@tonic-gate 						"clnt_tp_create_timed: "
5570Sstevel@tonic-gate 						"strdup failed.");
558132Srobinson 					return (NULL);
5590Sstevel@tonic-gate 				}
5600Sstevel@tonic-gate 			}
5610Sstevel@tonic-gate 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
5620Sstevel@tonic-gate 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
5630Sstevel@tonic-gate 		} else {
5640Sstevel@tonic-gate 			CLNT_DESTROY(cl);
5650Sstevel@tonic-gate 			cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
5660Sstevel@tonic-gate 					prog, vers, 0, 0, tp);
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 	netdir_free((char *)svcaddr, ND_ADDR);
5700Sstevel@tonic-gate 	return (cl);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate  * Generic client creation:  returns client handle.
5750Sstevel@tonic-gate  * Default options are set, which the user can
5760Sstevel@tonic-gate  * change using the rpc equivalent of ioctl()'s : clnt_control().
5770Sstevel@tonic-gate  * If fd is RPC_ANYFD, it will be opened using nconf.
5780Sstevel@tonic-gate  * It will be bound if not so.
5790Sstevel@tonic-gate  * If sizes are 0; appropriate defaults will be chosen.
5800Sstevel@tonic-gate  */
5810Sstevel@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)582132Srobinson clnt_tli_create(const int fd, const struct netconfig *nconf,
583132Srobinson     struct netbuf *svcaddr, const rpcprog_t prog, const rpcvers_t vers,
584132Srobinson     const uint_t sendsz, const uint_t recvsz)
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate 	return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz,
5870Sstevel@tonic-gate 		recvsz, NULL));
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate  * This has the same definition as clnt_tli_create(), except it
5920Sstevel@tonic-gate  * takes an additional parameter - a pointer to a timeval structure.
5930Sstevel@tonic-gate  *
5940Sstevel@tonic-gate  * Not a public interface. This is for clnt_create_timed,
5950Sstevel@tonic-gate  * clnt_create_vers_times, clnt_tp_create_timed to pass down  the
5960Sstevel@tonic-gate  * timeout value to COTS creation routine.
5970Sstevel@tonic-gate  * (for bug 4049792: clnt_create_timed does not time out)
5980Sstevel@tonic-gate  */
5990Sstevel@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)6000Sstevel@tonic-gate _clnt_tli_create_timed(int fd, const struct netconfig *nconf,
6010Sstevel@tonic-gate 	struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz,
6020Sstevel@tonic-gate 	uint_t recvsz, const struct timeval *tp)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	CLIENT *cl;			/* client handle */
6050Sstevel@tonic-gate 	struct t_info tinfo;		/* transport info */
6060Sstevel@tonic-gate 	bool_t madefd;			/* whether fd opened here */
6070Sstevel@tonic-gate 	t_scalar_t servtype;
6080Sstevel@tonic-gate 	int retval;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (fd == RPC_ANYFD) {
611132Srobinson 		if (nconf == NULL) {
6120Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
613132Srobinson 			return (NULL);
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		fd = t_open(nconf->nc_device, O_RDWR, NULL);
6170Sstevel@tonic-gate 		if (fd == -1)
6180Sstevel@tonic-gate 			goto err;
619*1914Scasper 		RPC_RAISEFD(fd);
6200Sstevel@tonic-gate 		madefd = TRUE;
6211676Sjpk 		__rpc_set_mac_options(fd, nconf, prog);
622132Srobinson 		if (t_bind(fd, NULL, NULL) == -1)
623132Srobinson 			goto err;
6240Sstevel@tonic-gate 		switch (nconf->nc_semantics) {
6250Sstevel@tonic-gate 		case NC_TPI_CLTS:
6260Sstevel@tonic-gate 			servtype = T_CLTS;
6270Sstevel@tonic-gate 			break;
6280Sstevel@tonic-gate 		case NC_TPI_COTS:
6290Sstevel@tonic-gate 			servtype = T_COTS;
6300Sstevel@tonic-gate 			break;
6310Sstevel@tonic-gate 		case NC_TPI_COTS_ORD:
6320Sstevel@tonic-gate 			servtype = T_COTS_ORD;
6330Sstevel@tonic-gate 			break;
6340Sstevel@tonic-gate 		default:
6350Sstevel@tonic-gate 			if (t_getinfo(fd, &tinfo) == -1)
6360Sstevel@tonic-gate 				goto err;
6370Sstevel@tonic-gate 			servtype = tinfo.servtype;
6380Sstevel@tonic-gate 			break;
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 	} else {
6410Sstevel@tonic-gate 		int state;		/* Current state of provider */
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 		/*
6440Sstevel@tonic-gate 		 * Sync the opened fd.
6450Sstevel@tonic-gate 		 * Check whether bound or not, else bind it
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		if (((state = t_sync(fd)) == -1) ||
648132Srobinson 		    ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) ||
6490Sstevel@tonic-gate 		    (t_getinfo(fd, &tinfo) == -1))
6500Sstevel@tonic-gate 			goto err;
6510Sstevel@tonic-gate 		servtype = tinfo.servtype;
6520Sstevel@tonic-gate 		madefd = FALSE;
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	switch (servtype) {
6560Sstevel@tonic-gate 	case T_COTS:
6570Sstevel@tonic-gate 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
6580Sstevel@tonic-gate 				recvsz, tp);
6590Sstevel@tonic-gate 		break;
6600Sstevel@tonic-gate 	case T_COTS_ORD:
6610Sstevel@tonic-gate 		if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
6620Sstevel@tonic-gate 		    (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) {
6630Sstevel@tonic-gate 			retval =  __td_setnodelay(fd);
6640Sstevel@tonic-gate 			if (retval == -1)
6650Sstevel@tonic-gate 				goto err;
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
6680Sstevel@tonic-gate 			recvsz, tp);
6690Sstevel@tonic-gate 		break;
6700Sstevel@tonic-gate 	case T_CLTS:
6710Sstevel@tonic-gate 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
6720Sstevel@tonic-gate 		break;
6730Sstevel@tonic-gate 	default:
6740Sstevel@tonic-gate 		goto err;
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 
677132Srobinson 	if (cl == NULL)
6780Sstevel@tonic-gate 		goto err1; /* borrow errors from clnt_dg/vc creates */
6790Sstevel@tonic-gate 	if (nconf) {
6800Sstevel@tonic-gate 		cl->cl_netid = strdup(nconf->nc_netid);
6810Sstevel@tonic-gate 		if (cl->cl_netid == NULL) {
6820Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6830Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
6840Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = 0;
6850Sstevel@tonic-gate 			syslog(LOG_ERR,
6860Sstevel@tonic-gate 				"clnt_tli_create: strdup failed");
6870Sstevel@tonic-gate 			goto err1;
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 		cl->cl_tp = strdup(nconf->nc_device);
6900Sstevel@tonic-gate 		if (cl->cl_tp == NULL) {
6910Sstevel@tonic-gate 			if (cl->cl_netid)
6920Sstevel@tonic-gate 				free(cl->cl_netid);
6930Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
6940Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
6950Sstevel@tonic-gate 			rpc_createerr.cf_error.re_terrno = 0;
6960Sstevel@tonic-gate 			syslog(LOG_ERR,
6970Sstevel@tonic-gate 				"clnt_tli_create: strdup failed");
6980Sstevel@tonic-gate 			goto err1;
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 	} else {
7010Sstevel@tonic-gate 		struct netconfig *nc;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) {
7040Sstevel@tonic-gate 			if (nc->nc_netid) {
7050Sstevel@tonic-gate 				cl->cl_netid = strdup(nc->nc_netid);
7060Sstevel@tonic-gate 				if (cl->cl_netid == NULL) {
7070Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7080Sstevel@tonic-gate 					rpc_createerr.cf_error.re_errno = errno;
7090Sstevel@tonic-gate 					rpc_createerr.cf_error.re_terrno = 0;
7100Sstevel@tonic-gate 					syslog(LOG_ERR,
7110Sstevel@tonic-gate 						"clnt_tli_create: "
7120Sstevel@tonic-gate 						"strdup failed");
7130Sstevel@tonic-gate 					goto err1;
7140Sstevel@tonic-gate 				}
7150Sstevel@tonic-gate 			}
7160Sstevel@tonic-gate 			if (nc->nc_device) {
7170Sstevel@tonic-gate 				cl->cl_tp = strdup(nc->nc_device);
7180Sstevel@tonic-gate 				if (cl->cl_tp == NULL) {
7190Sstevel@tonic-gate 					if (cl->cl_netid)
7200Sstevel@tonic-gate 						free(cl->cl_netid);
7210Sstevel@tonic-gate 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7220Sstevel@tonic-gate 					rpc_createerr.cf_error.re_errno = errno;
7230Sstevel@tonic-gate 					rpc_createerr.cf_error.re_terrno = 0;
7240Sstevel@tonic-gate 					syslog(LOG_ERR,
7250Sstevel@tonic-gate 						"clnt_tli_create: "
7260Sstevel@tonic-gate 						"strdup failed");
7270Sstevel@tonic-gate 					goto err1;
7280Sstevel@tonic-gate 				}
7290Sstevel@tonic-gate 			}
7300Sstevel@tonic-gate 			freenetconfigent(nc);
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		if (cl->cl_netid == NULL)
7330Sstevel@tonic-gate 			cl->cl_netid = "";
7340Sstevel@tonic-gate 		if (cl->cl_tp == NULL)
7350Sstevel@tonic-gate 			cl->cl_tp = "";
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 	if (madefd) {
738132Srobinson 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
739132Srobinson /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
7400Sstevel@tonic-gate 	};
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	return (cl);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate err:
7450Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_TLIERROR;
7460Sstevel@tonic-gate 	rpc_createerr.cf_error.re_errno = errno;
7470Sstevel@tonic-gate 	rpc_createerr.cf_error.re_terrno = t_errno;
7480Sstevel@tonic-gate err1:	if (madefd)
7490Sstevel@tonic-gate 		(void) t_close(fd);
750132Srobinson 	return (NULL);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate  *  To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
7550Sstevel@tonic-gate  *  we try to not use them.  The __rpc_raise_fd() routine will dup
7560Sstevel@tonic-gate  *  a descriptor to a higher value.  If we fail to do it, we continue
7570Sstevel@tonic-gate  *  to use the old one (and hope for the best).
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate int
__rpc_raise_fd(int fd)7600Sstevel@tonic-gate __rpc_raise_fd(int fd)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	int nfd;
7630Sstevel@tonic-gate 
764*1914Scasper 	if ((nfd = fcntl(fd, F_DUPFD, RPC_MINFD)) == -1)
7650Sstevel@tonic-gate 		return (fd);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (t_sync(nfd) == -1) {
768132Srobinson 		(void) close(nfd);
7690Sstevel@tonic-gate 		return (fd);
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (t_close(fd) == -1) {
7730Sstevel@tonic-gate 		/* this is okay, we will syslog an error, then use the new fd */
7740Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
7750Sstevel@tonic-gate 			"could not t_close() fd %d; mem & fd leak", fd);
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	return (nfd);
7790Sstevel@tonic-gate }
780