xref: /titanic_50/usr/src/lib/libnsl/rpc/clnt_generic.c (revision 4f85d229295a756a4e6f1759b47df7b97412db7d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include "rpc_mt.h"
38 #include <stdio.h>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <rpc/rpc.h>
43 #include <rpc/nettype.h>
44 #include <netdir.h>
45 #include <string.h>
46 #include <syslog.h>
47 
48 extern int __td_setnodelay(int);
49 extern bool_t __rpc_is_local_host(const char *);
50 extern bool_t __rpc_try_doors(const char *, bool_t *);
51 extern CLIENT *_clnt_vc_create_timed(int, struct netbuf *, rpcprog_t,
52 			rpcvers_t, uint_t, uint_t, const struct timeval *);
53 
54 CLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *,
55 		rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *);
56 
57 #ifndef NETIDLEN
58 #define	NETIDLEN 32
59 #endif
60 
61 /*
62  * Generic client creation with version checking the value of
63  * vers_out is set to the highest server supported value
64  * vers_low <= vers_out <= vers_high  AND an error results
65  * if this can not be done.
66  *
67  * It calls clnt_create_vers_timed() with a NULL value for the timeout
68  * pointer, which indicates that the default timeout should be used.
69  */
70 CLIENT *
71 clnt_create_vers(const char *hostname, const rpcprog_t prog,
72 	rpcvers_t *vers_out, const rpcvers_t vers_low,
73 	const rpcvers_t vers_high, const char *nettype)
74 {
75 	return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
76 				vers_high, nettype, NULL));
77 }
78 
79 /*
80  * This routine has the same definition as clnt_create_vers(),
81  * except it takes an additional timeout parameter - a pointer to
82  * a timeval structure.  A NULL value for the pointer indicates
83  * that the default timeout value should be used.
84  */
85 CLIENT *
86 clnt_create_vers_timed(const char *hostname, const rpcprog_t prog,
87     rpcvers_t *vers_out, const rpcvers_t vers_low, const rpcvers_t vers_high,
88     const char *nettype, const struct timeval *tp)
89 {
90 	CLIENT *clnt;
91 	struct timeval to;
92 	enum clnt_stat rpc_stat;
93 	struct rpc_err rpcerr;
94 	rpcvers_t v_low, v_high;
95 
96 	clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
97 	if (clnt == NULL)
98 		return (NULL);
99 	if (tp == NULL) {
100 		to.tv_sec = 10;
101 		to.tv_usec = 0;
102 	} else
103 		to = *tp;
104 
105 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
106 			NULL, (xdrproc_t)xdr_void, NULL, to);
107 	if (rpc_stat == RPC_SUCCESS) {
108 		*vers_out = vers_high;
109 		return (clnt);
110 	}
111 	v_low = vers_low;
112 	v_high = vers_high;
113 	while (rpc_stat == RPC_PROGVERSMISMATCH && v_high > v_low) {
114 		unsigned int minvers, maxvers;
115 
116 		clnt_geterr(clnt, &rpcerr);
117 		minvers = rpcerr.re_vers.low;
118 		maxvers = rpcerr.re_vers.high;
119 		if (maxvers < v_high)
120 			v_high = maxvers;
121 		else
122 			v_high--;
123 		if (minvers > v_low)
124 			v_low = minvers;
125 		if (v_low > v_high) {
126 			goto error;
127 		}
128 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)&v_high);
129 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
130 				NULL, (xdrproc_t)xdr_void,
131 				NULL, to);
132 		if (rpc_stat == RPC_SUCCESS) {
133 			*vers_out = v_high;
134 			return (clnt);
135 		}
136 	}
137 	clnt_geterr(clnt, &rpcerr);
138 
139 error:
140 	rpc_createerr.cf_stat = rpc_stat;
141 	rpc_createerr.cf_error = rpcerr;
142 	clnt_destroy(clnt);
143 	return (NULL);
144 }
145 
146 /*
147  * Top level client creation routine.
148  * Generic client creation: takes (servers name, program-number, nettype) and
149  * returns client handle. Default options are set, which the user can
150  * change using the rpc equivalent of ioctl()'s.
151  *
152  * It tries for all the netids in that particular class of netid until
153  * it succeeds.
154  * XXX The error message in the case of failure will be the one
155  * pertaining to the last create error.
156  *
157  * It calls clnt_create_timed() with the default timeout.
158  */
159 CLIENT *
160 clnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
161     const char *nettype)
162 {
163 	return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
164 }
165 
166 /*
167  * This the routine has the same definition as clnt_create(),
168  * except it takes an additional timeout parameter - a pointer to
169  * a timeval structure.  A NULL value for the pointer indicates
170  * that the default timeout value should be used.
171  *
172  * This function calls clnt_tp_create_timed().
173  */
174 CLIENT *
175 clnt_create_timed(const char *hostname, const rpcprog_t prog,
176     const rpcvers_t vers, const char *netclass, const struct timeval *tp)
177 {
178 	struct netconfig *nconf;
179 	CLIENT *clnt = NULL;
180 	void *handle;
181 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
182 	struct rpc_err	save_cf_error;
183 	char nettype_array[NETIDLEN];
184 	char *nettype = &nettype_array[0];
185 	bool_t try_others;
186 
187 	if (netclass == NULL)
188 		nettype = NULL;
189 	else {
190 		size_t len = strlen(netclass);
191 		if (len >= sizeof (nettype_array)) {
192 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
193 			return (NULL);
194 		}
195 		(void) strcpy(nettype, netclass);
196 	}
197 
198 	/*
199 	 * Check to see if a rendezvous over doors should be attempted.
200 	 */
201 	if (__rpc_try_doors(nettype, &try_others)) {
202 		/*
203 		 * Make sure this is the local host.
204 		 */
205 		if (__rpc_is_local_host(hostname)) {
206 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
207 				return (clnt);
208 			else {
209 				if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
210 					return (NULL);
211 				save_cf_stat = rpc_createerr.cf_stat;
212 				save_cf_error = rpc_createerr.cf_error;
213 			}
214 		} else {
215 			save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
216 		}
217 	}
218 	if (!try_others)
219 		return (NULL);
220 
221 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
222 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
223 		return (NULL);
224 	}
225 	rpc_createerr.cf_stat = RPC_SUCCESS;
226 	while (clnt == NULL) {
227 		if ((nconf = __rpc_getconf(handle)) == NULL) {
228 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
229 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
230 			break;
231 		}
232 		clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
233 		if (clnt)
234 			break;
235 		else {
236 			/*
237 			 *	Since we didn't get a name-to-address
238 			 *	translation failure here, we remember
239 			 *	this particular error.  The object of
240 			 *	this is to enable us to return to the
241 			 *	caller a more-specific error than the
242 			 *	unhelpful ``Name to address translation
243 			 *	failed'' which might well occur if we
244 			 *	merely returned the last error (because
245 			 *	the local loopbacks are typically the
246 			 *	last ones in /etc/netconfig and the most
247 			 *	likely to be unable to translate a host
248 			 *	name).  We also check for a more
249 			 *	meaningful error than ``unknown host
250 			 *	name'' for the same reasons.
251 			 */
252 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
253 				syslog(LOG_ERR, "clnt_create_timed: "
254 					"RPC_SYSTEMERROR.");
255 				break;
256 			}
257 
258 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
259 			    rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
260 				save_cf_stat = rpc_createerr.cf_stat;
261 				save_cf_error = rpc_createerr.cf_error;
262 			}
263 		}
264 	}
265 
266 	/*
267 	 *	Attempt to return an error more specific than ``Name to address
268 	 *	translation failed'' or ``unknown host name''
269 	 */
270 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
271 				rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
272 					(save_cf_stat != RPC_SUCCESS)) {
273 		rpc_createerr.cf_stat = save_cf_stat;
274 		rpc_createerr.cf_error = save_cf_error;
275 	}
276 	__rpc_endconf(handle);
277 	return (clnt);
278 }
279 
280 /*
281  * Create a client handle for a well known service or a specific port on
282  * host. This routine bypasses rpcbind and can be use to construct a client
283  * handle to services that are not registered with rpcbind or where the remote
284  * rpcbind is not available, e.g., the remote rpcbind port is blocked by a
285  * firewall. We construct a client handle and then ping the service's NULL
286  * proc to see that the service is really available. If the caller supplies
287  * a non zero port number, the service name is ignored and the port will be
288  * used. A non-zero port number limits the protocol family to inet or inet6.
289  */
290 
291 CLIENT *
292 clnt_create_service_timed(const char *host, const char *service,
293 			const rpcprog_t prog, const rpcvers_t vers,
294 			const ushort_t port, const char *netclass,
295 			const struct timeval *tmout)
296 {
297 	int fd;
298 	void *handle;
299 	CLIENT *clnt = NULL;
300 	struct netconfig *nconf;
301 	struct nd_hostserv hs;
302 	struct nd_addrlist *raddrs;
303 	struct t_bind *tbind = NULL;
304 	struct timeval to;
305 	char nettype_array[NETIDLEN];
306 	char *nettype = &nettype_array[0];
307 	char *hostname, *serv;
308 	bool_t try_others;
309 	extern int __rpc_minfd;
310 
311 
312 	/*
313 	 * handle const of netclass
314 	 */
315 	if (netclass == NULL)
316 		nettype = NULL;
317 	else {
318 		size_t len = strlen(netclass);
319 		if (len >= sizeof (nettype_array)) {
320 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
321 			return (NULL);
322 		}
323 		(void) strcpy(nettype, netclass);
324 	}
325 
326 	if (tmout == NULL) {
327 		to.tv_sec = 10;
328 		to.tv_usec = 0;
329 	} else
330 		to = *tmout;
331 
332 	if ((handle = __rpc_setconf(nettype)) == NULL) {
333 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
334 		return (NULL);
335 	}
336 
337 	/*
338 	 * Sinct host, and service are const
339 	 */
340 	if (host == NULL || (hostname = strdup(host)) == NULL) {
341 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
342 		rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL);
343 		rpc_createerr.cf_error.re_terrno = 0;
344 		return (NULL);
345 	}
346 
347 	if (service == NULL)
348 		serv = NULL;
349 	else if ((serv = strdup(service ? service : "")) == NULL) {
350 		free(hostname);
351 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
352 		rpc_createerr.cf_error.re_errno = errno;
353 		rpc_createerr.cf_error.re_terrno = 0;
354 		return (NULL);
355 	}
356 
357 	hs.h_host = hostname;
358 	hs.h_serv = port ? NULL : serv;
359 
360 	/*
361 	 * Check to see if a rendezvous over doors should be attempted.
362 	 */
363 	if (__rpc_try_doors(nettype, &try_others)) {
364 		/*
365 		 * Make sure this is the local host.
366 		 */
367 		if (__rpc_is_local_host(hostname)) {
368 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
369 				goto done;
370 			else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
371 				goto done;
372 		} else {
373 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
374 		}
375 	}
376 	if (!try_others)
377 		goto done;
378 
379 	rpc_createerr.cf_stat = RPC_SUCCESS;
380 	while (clnt == NULL) {
381 		tbind = NULL;
382 		if ((nconf = __rpc_getconf(handle)) == NULL) {
383 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
384 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
385 			break;
386 		}
387 
388 		if (port) {
389 			if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
390 			    strcmp(nconf->nc_protofmly, NC_INET6) != 0)
391 				continue;
392 		}
393 
394 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
395 			rpc_createerr.cf_stat = RPC_TLIERROR;
396 			rpc_createerr.cf_error.re_errno = errno;
397 			rpc_createerr.cf_error.re_terrno = t_errno;
398 			continue;
399 		}
400 
401 		if (fd < __rpc_minfd)
402 			fd = __rpc_raise_fd(fd);
403 
404 		/* LINTED pointer cast */
405 		if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
406 		    == NULL) {
407 			(void) t_close(fd);
408 			rpc_createerr.cf_stat = RPC_TLIERROR;
409 			rpc_createerr.cf_error.re_errno = errno;
410 			rpc_createerr.cf_error.re_terrno = t_errno;
411 			continue;
412 		}
413 
414 		if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) {
415 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
416 				rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
417 			if (tbind)
418 				(void) t_free((char *)tbind, T_BIND);
419 			(void) t_close(fd);
420 			continue;
421 		}
422 		(void) memcpy(tbind->addr.buf, raddrs->n_addrs->buf,
423 		    raddrs->n_addrs->len);
424 		tbind->addr.len = raddrs->n_addrs->len;
425 		netdir_free((void *)raddrs, ND_ADDRLIST);
426 
427 		if (port) {
428 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
429 				/* LINTED pointer alignment */
430 				((struct sockaddr_in *)
431 				tbind->addr.buf)->sin_port = htons(port);
432 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
433 				/* LINTED pointer alignment */
434 				((struct sockaddr_in6 *)
435 				tbind->addr.buf)->sin6_port = htons(port);
436 		}
437 
438 		clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr,
439 					    prog, vers, 0, 0, &to);
440 
441 		if (clnt == NULL) {
442 			if (tbind)
443 				(void) t_free((char *)tbind, T_BIND);
444 			(void) t_close(fd);
445 			continue;
446 		}
447 
448 		(void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, NULL);
449 
450 		/*
451 		 * Check if we can reach the server with this clnt handle
452 		 * Other clnt_create calls do a ping by contacting the
453 		 * remote rpcbind, here will just try to execute the service's
454 		 * NULL proc.
455 		 */
456 
457 		rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC,
458 						xdr_void, 0, xdr_void, 0, to);
459 
460 		rpc_createerr.cf_error.re_errno = rpc_callerr.re_status;
461 		rpc_createerr.cf_error.re_terrno = 0;
462 
463 		if (rpc_createerr.cf_stat != RPC_SUCCESS) {
464 			clnt_destroy(clnt);
465 			clnt = NULL;
466 			if (tbind)
467 				(void) t_free((char *)tbind, T_BIND);
468 			continue;
469 		} else
470 			break;
471 	}
472 
473 	__rpc_endconf(handle);
474 	if (tbind)
475 		(void) t_free((char *)tbind, T_BIND);
476 
477 done:
478 	if (hostname)
479 		free(hostname);
480 	if (serv)
481 		free(serv);
482 
483 	return (clnt);
484 }
485 
486 /*
487  * Generic client creation: takes (servers name, program-number, netconf) and
488  * returns client handle. Default options are set, which the user can
489  * change using the rpc equivalent of ioctl()'s : clnt_control()
490  * It finds out the server address from rpcbind and calls clnt_tli_create().
491  *
492  * It calls clnt_tp_create_timed() with the default timeout.
493  */
494 CLIENT *
495 clnt_tp_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
496     const struct netconfig *nconf)
497 {
498 	return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
499 }
500 
501 /*
502  * This has the same definition as clnt_tp_create(), except it
503  * takes an additional parameter - a pointer to a timeval structure.
504  * A NULL value for the timeout pointer indicates that the default
505  * value for the timeout should be used.
506  */
507 CLIENT *
508 clnt_tp_create_timed(const char *hostname, const rpcprog_t prog,
509     const rpcvers_t vers, const struct netconfig *nconf,
510     const struct timeval *tp)
511 {
512 	struct netbuf *svcaddr;			/* servers address */
513 	CLIENT *cl = NULL;			/* client handle */
514 	extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t,
515 	    struct netconfig *, char *, CLIENT **, struct timeval *);
516 
517 	if (nconf == NULL) {
518 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
519 		return (NULL);
520 	}
521 
522 	/*
523 	 * Get the address of the server
524 	 */
525 	if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
526 			(struct netconfig *)nconf, (char *)hostname,
527 			&cl, (struct timeval *)tp)) == NULL) {
528 		/* appropriate error number is set by rpcbind libraries */
529 		return (NULL);
530 	}
531 	if (cl == NULL) {
532 		cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
533 					prog, vers, 0, 0, tp);
534 	} else {
535 		/* Reuse the CLIENT handle and change the appropriate fields */
536 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
537 			if (cl->cl_netid == NULL) {
538 				cl->cl_netid = strdup(nconf->nc_netid);
539 				if (cl->cl_netid == NULL) {
540 					netdir_free((char *)svcaddr, ND_ADDR);
541 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
542 					syslog(LOG_ERR,
543 						"clnt_tp_create_timed: "
544 						"strdup failed.");
545 					return (NULL);
546 				}
547 			}
548 			if (cl->cl_tp == NULL) {
549 				cl->cl_tp = strdup(nconf->nc_device);
550 				if (cl->cl_tp == NULL) {
551 					netdir_free((char *)svcaddr, ND_ADDR);
552 					if (cl->cl_netid)
553 						free(cl->cl_netid);
554 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
555 					syslog(LOG_ERR,
556 						"clnt_tp_create_timed: "
557 						"strdup failed.");
558 					return (NULL);
559 				}
560 			}
561 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
562 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
563 		} else {
564 			CLNT_DESTROY(cl);
565 			cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
566 					prog, vers, 0, 0, tp);
567 		}
568 	}
569 	netdir_free((char *)svcaddr, ND_ADDR);
570 	return (cl);
571 }
572 
573 /*
574  * Generic client creation:  returns client handle.
575  * Default options are set, which the user can
576  * change using the rpc equivalent of ioctl()'s : clnt_control().
577  * If fd is RPC_ANYFD, it will be opened using nconf.
578  * It will be bound if not so.
579  * If sizes are 0; appropriate defaults will be chosen.
580  */
581 CLIENT *
582 clnt_tli_create(const int fd, const struct netconfig *nconf,
583     struct netbuf *svcaddr, const rpcprog_t prog, const rpcvers_t vers,
584     const uint_t sendsz, const uint_t recvsz)
585 {
586 	return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz,
587 		recvsz, NULL));
588 }
589 
590 /*
591  * This has the same definition as clnt_tli_create(), except it
592  * takes an additional parameter - a pointer to a timeval structure.
593  *
594  * Not a public interface. This is for clnt_create_timed,
595  * clnt_create_vers_times, clnt_tp_create_timed to pass down  the
596  * timeout value to COTS creation routine.
597  * (for bug 4049792: clnt_create_timed does not time out)
598  */
599 CLIENT *
600 _clnt_tli_create_timed(int fd, const struct netconfig *nconf,
601 	struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz,
602 	uint_t recvsz, const struct timeval *tp)
603 {
604 	CLIENT *cl;			/* client handle */
605 	struct t_info tinfo;		/* transport info */
606 	bool_t madefd;			/* whether fd opened here */
607 	t_scalar_t servtype;
608 	int retval;
609 	extern int __rpc_minfd;
610 
611 	if (fd == RPC_ANYFD) {
612 		if (nconf == NULL) {
613 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
614 			return (NULL);
615 		}
616 
617 		fd = t_open(nconf->nc_device, O_RDWR, NULL);
618 		if (fd == -1)
619 			goto err;
620 		if (fd < __rpc_minfd)
621 			fd = __rpc_raise_fd(fd);
622 		madefd = TRUE;
623 		if (t_bind(fd, NULL, NULL) == -1)
624 			goto err;
625 		switch (nconf->nc_semantics) {
626 		case NC_TPI_CLTS:
627 			servtype = T_CLTS;
628 			break;
629 		case NC_TPI_COTS:
630 			servtype = T_COTS;
631 			break;
632 		case NC_TPI_COTS_ORD:
633 			servtype = T_COTS_ORD;
634 			break;
635 		default:
636 			if (t_getinfo(fd, &tinfo) == -1)
637 				goto err;
638 			servtype = tinfo.servtype;
639 			break;
640 		}
641 	} else {
642 		int state;		/* Current state of provider */
643 
644 		/*
645 		 * Sync the opened fd.
646 		 * Check whether bound or not, else bind it
647 		 */
648 		if (((state = t_sync(fd)) == -1) ||
649 		    ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) ||
650 		    (t_getinfo(fd, &tinfo) == -1))
651 			goto err;
652 		servtype = tinfo.servtype;
653 		madefd = FALSE;
654 	}
655 
656 	switch (servtype) {
657 	case T_COTS:
658 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
659 				recvsz, tp);
660 		break;
661 	case T_COTS_ORD:
662 		if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
663 		    (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) {
664 			retval =  __td_setnodelay(fd);
665 			if (retval == -1)
666 				goto err;
667 		}
668 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
669 			recvsz, tp);
670 		break;
671 	case T_CLTS:
672 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
673 		break;
674 	default:
675 		goto err;
676 	}
677 
678 	if (cl == NULL)
679 		goto err1; /* borrow errors from clnt_dg/vc creates */
680 	if (nconf) {
681 		cl->cl_netid = strdup(nconf->nc_netid);
682 		if (cl->cl_netid == NULL) {
683 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
684 			rpc_createerr.cf_error.re_errno = errno;
685 			rpc_createerr.cf_error.re_terrno = 0;
686 			syslog(LOG_ERR,
687 				"clnt_tli_create: strdup failed");
688 			goto err1;
689 		}
690 		cl->cl_tp = strdup(nconf->nc_device);
691 		if (cl->cl_tp == NULL) {
692 			if (cl->cl_netid)
693 				free(cl->cl_netid);
694 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
695 			rpc_createerr.cf_error.re_errno = errno;
696 			rpc_createerr.cf_error.re_terrno = 0;
697 			syslog(LOG_ERR,
698 				"clnt_tli_create: strdup failed");
699 			goto err1;
700 		}
701 	} else {
702 		struct netconfig *nc;
703 
704 		if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) {
705 			if (nc->nc_netid) {
706 				cl->cl_netid = strdup(nc->nc_netid);
707 				if (cl->cl_netid == NULL) {
708 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
709 					rpc_createerr.cf_error.re_errno = errno;
710 					rpc_createerr.cf_error.re_terrno = 0;
711 					syslog(LOG_ERR,
712 						"clnt_tli_create: "
713 						"strdup failed");
714 					goto err1;
715 				}
716 			}
717 			if (nc->nc_device) {
718 				cl->cl_tp = strdup(nc->nc_device);
719 				if (cl->cl_tp == NULL) {
720 					if (cl->cl_netid)
721 						free(cl->cl_netid);
722 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
723 					rpc_createerr.cf_error.re_errno = errno;
724 					rpc_createerr.cf_error.re_terrno = 0;
725 					syslog(LOG_ERR,
726 						"clnt_tli_create: "
727 						"strdup failed");
728 					goto err1;
729 				}
730 			}
731 			freenetconfigent(nc);
732 		}
733 		if (cl->cl_netid == NULL)
734 			cl->cl_netid = "";
735 		if (cl->cl_tp == NULL)
736 			cl->cl_tp = "";
737 	}
738 	if (madefd) {
739 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
740 /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
741 	};
742 
743 	return (cl);
744 
745 err:
746 	rpc_createerr.cf_stat = RPC_TLIERROR;
747 	rpc_createerr.cf_error.re_errno = errno;
748 	rpc_createerr.cf_error.re_terrno = t_errno;
749 err1:	if (madefd)
750 		(void) t_close(fd);
751 	return (NULL);
752 }
753 
754 /*
755  *  To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
756  *  we try to not use them.  The __rpc_raise_fd() routine will dup
757  *  a descriptor to a higher value.  If we fail to do it, we continue
758  *  to use the old one (and hope for the best).
759  */
760 int __rpc_minfd = 3;
761 
762 int
763 __rpc_raise_fd(int fd)
764 {
765 	int nfd;
766 
767 	if (fd >= __rpc_minfd)
768 		return (fd);
769 
770 	if ((nfd = _fcntl(fd, F_DUPFD, __rpc_minfd)) == -1)
771 		return (fd);
772 
773 	if (t_sync(nfd) == -1) {
774 		(void) close(nfd);
775 		return (fd);
776 	}
777 
778 	if (t_close(fd) == -1) {
779 		/* this is okay, we will syslog an error, then use the new fd */
780 		(void) syslog(LOG_ERR,
781 			"could not t_close() fd %d; mem & fd leak", fd);
782 	}
783 
784 	return (nfd);
785 }
786