xref: /titanic_41/usr/src/lib/libnsl/rpc/clnt_generic.c (revision e11c3f44f531fdff80941ce57c065d2ae861cefc)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
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 "mt.h"
38 #include "rpc_mt.h"
39 #include <stdio.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <rpc/rpc.h>
44 #include <rpc/nettype.h>
45 #include <netdir.h>
46 #include <string.h>
47 #include <syslog.h>
48 
49 extern int __td_setnodelay(int);
50 extern bool_t __rpc_is_local_host(const char *);
51 extern bool_t __rpc_try_doors(const char *, bool_t *);
52 extern CLIENT *_clnt_vc_create_timed(int, struct netbuf *, rpcprog_t,
53 			rpcvers_t, uint_t, uint_t, const struct timeval *);
54 
55 CLIENT *_clnt_tli_create_timed(int, const struct netconfig *, struct netbuf *,
56 		rpcprog_t, rpcvers_t, uint_t, uint_t, const struct timeval *);
57 
58 #ifndef NETIDLEN
59 #define	NETIDLEN 32
60 #endif
61 
62 /*
63  * Generic client creation with version checking the value of
64  * vers_out is set to the highest server supported value
65  * vers_low <= vers_out <= vers_high  AND an error results
66  * if this can not be done.
67  *
68  * It calls clnt_create_vers_timed() with a NULL value for the timeout
69  * pointer, which indicates that the default timeout should be used.
70  */
71 CLIENT *
72 clnt_create_vers(const char *hostname, const rpcprog_t prog,
73 	rpcvers_t *vers_out, const rpcvers_t vers_low,
74 	const rpcvers_t vers_high, const char *nettype)
75 {
76 	return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
77 				vers_high, nettype, NULL));
78 }
79 
80 /*
81  * This routine has the same definition as clnt_create_vers(),
82  * except it takes an additional timeout parameter - a pointer to
83  * a timeval structure.  A NULL value for the pointer indicates
84  * that the default timeout value should be used.
85  */
86 CLIENT *
87 clnt_create_vers_timed(const char *hostname, const rpcprog_t prog,
88     rpcvers_t *vers_out, const rpcvers_t vers_low, const rpcvers_t vers_high,
89     const char *nettype, const struct timeval *tp)
90 {
91 	CLIENT *clnt;
92 	struct timeval to;
93 	enum clnt_stat rpc_stat;
94 	struct rpc_err rpcerr;
95 	rpcvers_t v_low, v_high;
96 
97 	clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
98 	if (clnt == NULL)
99 		return (NULL);
100 	if (tp == NULL) {
101 		to.tv_sec = 10;
102 		to.tv_usec = 0;
103 	} else
104 		to = *tp;
105 
106 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
107 			NULL, (xdrproc_t)xdr_void, NULL, to);
108 	if (rpc_stat == RPC_SUCCESS) {
109 		*vers_out = vers_high;
110 		return (clnt);
111 	}
112 	v_low = vers_low;
113 	v_high = vers_high;
114 	while (rpc_stat == RPC_PROGVERSMISMATCH && v_high > v_low) {
115 		unsigned int minvers, maxvers;
116 
117 		clnt_geterr(clnt, &rpcerr);
118 		minvers = rpcerr.re_vers.low;
119 		maxvers = rpcerr.re_vers.high;
120 		if (maxvers < v_high)
121 			v_high = maxvers;
122 		else
123 			v_high--;
124 		if (minvers > v_low)
125 			v_low = minvers;
126 		if (v_low > v_high) {
127 			goto error;
128 		}
129 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)&v_high);
130 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
131 				NULL, (xdrproc_t)xdr_void,
132 				NULL, to);
133 		if (rpc_stat == RPC_SUCCESS) {
134 			*vers_out = v_high;
135 			return (clnt);
136 		}
137 	}
138 	clnt_geterr(clnt, &rpcerr);
139 
140 error:
141 	rpc_createerr.cf_stat = rpc_stat;
142 	rpc_createerr.cf_error = rpcerr;
143 	clnt_destroy(clnt);
144 	return (NULL);
145 }
146 
147 /*
148  * Top level client creation routine.
149  * Generic client creation: takes (servers name, program-number, nettype) and
150  * returns client handle. Default options are set, which the user can
151  * change using the rpc equivalent of ioctl()'s.
152  *
153  * It tries for all the netids in that particular class of netid until
154  * it succeeds.
155  * XXX The error message in the case of failure will be the one
156  * pertaining to the last create error.
157  *
158  * It calls clnt_create_timed() with the default timeout.
159  */
160 CLIENT *
161 clnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
162     const char *nettype)
163 {
164 	return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
165 }
166 
167 /*
168  * This the routine has the same definition as clnt_create(),
169  * except it takes an additional timeout parameter - a pointer to
170  * a timeval structure.  A NULL value for the pointer indicates
171  * that the default timeout value should be used.
172  *
173  * This function calls clnt_tp_create_timed().
174  */
175 CLIENT *
176 clnt_create_timed(const char *hostname, const rpcprog_t prog,
177     const rpcvers_t vers, const char *netclass, const struct timeval *tp)
178 {
179 	struct netconfig *nconf;
180 	CLIENT *clnt = NULL;
181 	void *handle;
182 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
183 	struct rpc_err	save_cf_error;
184 	char nettype_array[NETIDLEN];
185 	char *nettype = &nettype_array[0];
186 	bool_t try_others;
187 
188 	if (netclass == NULL)
189 		nettype = NULL;
190 	else {
191 		size_t len = strlen(netclass);
192 		if (len >= sizeof (nettype_array)) {
193 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
194 			return (NULL);
195 		}
196 		(void) strcpy(nettype, netclass);
197 	}
198 
199 	/*
200 	 * Check to see if a rendezvous over doors should be attempted.
201 	 */
202 	if (__rpc_try_doors(nettype, &try_others)) {
203 		/*
204 		 * Make sure this is the local host.
205 		 */
206 		if (__rpc_is_local_host(hostname)) {
207 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
208 				return (clnt);
209 			else {
210 				if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
211 					return (NULL);
212 				save_cf_stat = rpc_createerr.cf_stat;
213 				save_cf_error = rpc_createerr.cf_error;
214 			}
215 		} else {
216 			save_cf_stat = rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
217 		}
218 	}
219 	if (!try_others)
220 		return (NULL);
221 
222 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
223 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
224 		return (NULL);
225 	}
226 	rpc_createerr.cf_stat = RPC_SUCCESS;
227 	while (clnt == NULL) {
228 		if ((nconf = __rpc_getconf(handle)) == NULL) {
229 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
230 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
231 			break;
232 		}
233 		clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
234 		if (clnt)
235 			break;
236 		else {
237 			/*
238 			 *	Since we didn't get a name-to-address
239 			 *	translation failure here, we remember
240 			 *	this particular error.  The object of
241 			 *	this is to enable us to return to the
242 			 *	caller a more-specific error than the
243 			 *	unhelpful ``Name to address translation
244 			 *	failed'' which might well occur if we
245 			 *	merely returned the last error (because
246 			 *	the local loopbacks are typically the
247 			 *	last ones in /etc/netconfig and the most
248 			 *	likely to be unable to translate a host
249 			 *	name).  We also check for a more
250 			 *	meaningful error than ``unknown host
251 			 *	name'' for the same reasons.
252 			 */
253 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
254 				syslog(LOG_ERR, "clnt_create_timed: "
255 					"RPC_SYSTEMERROR.");
256 				break;
257 			}
258 
259 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
260 			    rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
261 				save_cf_stat = rpc_createerr.cf_stat;
262 				save_cf_error = rpc_createerr.cf_error;
263 			}
264 		}
265 	}
266 
267 	/*
268 	 *	Attempt to return an error more specific than ``Name to address
269 	 *	translation failed'' or ``unknown host name''
270 	 */
271 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
272 				rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
273 					(save_cf_stat != RPC_SUCCESS)) {
274 		rpc_createerr.cf_stat = save_cf_stat;
275 		rpc_createerr.cf_error = save_cf_error;
276 	}
277 	__rpc_endconf(handle);
278 	return (clnt);
279 }
280 
281 /*
282  * Create a client handle for a well known service or a specific port on
283  * host. This routine bypasses rpcbind and can be use to construct a client
284  * handle to services that are not registered with rpcbind or where the remote
285  * rpcbind is not available, e.g., the remote rpcbind port is blocked by a
286  * firewall. We construct a client handle and then ping the service's NULL
287  * proc to see that the service is really available. If the caller supplies
288  * a non zero port number, the service name is ignored and the port will be
289  * used. A non-zero port number limits the protocol family to inet or inet6.
290  */
291 
292 CLIENT *
293 clnt_create_service_timed(const char *host, const char *service,
294 			const rpcprog_t prog, const rpcvers_t vers,
295 			const ushort_t port, const char *netclass,
296 			const struct timeval *tmout)
297 {
298 	int fd;
299 	void *handle;
300 	CLIENT *clnt = NULL;
301 	struct netconfig *nconf;
302 	struct nd_hostserv hs;
303 	struct nd_addrlist *raddrs;
304 	struct t_bind *tbind = NULL;
305 	struct timeval to;
306 	char nettype_array[NETIDLEN];
307 	char *nettype = &nettype_array[0];
308 	char *hostname, *serv;
309 	bool_t try_others;
310 
311 	/*
312 	 * handle const of netclass
313 	 */
314 	if (netclass == NULL)
315 		nettype = NULL;
316 	else {
317 		size_t len = strlen(netclass);
318 		if (len >= sizeof (nettype_array)) {
319 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
320 			return (NULL);
321 		}
322 		(void) strcpy(nettype, netclass);
323 	}
324 
325 	if (tmout == NULL) {
326 		to.tv_sec = 10;
327 		to.tv_usec = 0;
328 	} else
329 		to = *tmout;
330 
331 	if ((handle = __rpc_setconf(nettype)) == NULL) {
332 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
333 		return (NULL);
334 	}
335 
336 	/*
337 	 * Sinct host, and service are const
338 	 */
339 	if (host == NULL || (hostname = strdup(host)) == NULL) {
340 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
341 		rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL);
342 		rpc_createerr.cf_error.re_terrno = 0;
343 		return (NULL);
344 	}
345 
346 	if (service == NULL)
347 		serv = NULL;
348 	else if ((serv = strdup(service ? service : "")) == NULL) {
349 		free(hostname);
350 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
351 		rpc_createerr.cf_error.re_errno = errno;
352 		rpc_createerr.cf_error.re_terrno = 0;
353 		return (NULL);
354 	}
355 
356 	hs.h_host = hostname;
357 	hs.h_serv = port ? NULL : serv;
358 
359 	/*
360 	 * Check to see if a rendezvous over doors should be attempted.
361 	 */
362 	if (__rpc_try_doors(nettype, &try_others)) {
363 		/*
364 		 * Make sure this is the local host.
365 		 */
366 		if (__rpc_is_local_host(hostname)) {
367 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
368 				goto done;
369 			else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
370 				goto done;
371 		} else {
372 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
373 		}
374 	}
375 	if (!try_others)
376 		goto done;
377 
378 	rpc_createerr.cf_stat = RPC_SUCCESS;
379 	while (clnt == NULL) {
380 		tbind = NULL;
381 		if ((nconf = __rpc_getconf(handle)) == NULL) {
382 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
383 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
384 			break;
385 		}
386 
387 		if (port) {
388 			if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
389 			    strcmp(nconf->nc_protofmly, NC_INET6) != 0)
390 				continue;
391 		}
392 
393 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
394 			rpc_createerr.cf_stat = RPC_TLIERROR;
395 			rpc_createerr.cf_error.re_errno = errno;
396 			rpc_createerr.cf_error.re_terrno = t_errno;
397 			continue;
398 		}
399 
400 		RPC_RAISEFD(fd);
401 
402 		__rpc_set_mac_options(fd, nconf, prog);
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 
610 	if (fd == RPC_ANYFD) {
611 		if (nconf == NULL) {
612 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
613 			return (NULL);
614 		}
615 
616 		fd = t_open(nconf->nc_device, O_RDWR, NULL);
617 		if (fd == -1)
618 			goto err;
619 		RPC_RAISEFD(fd);
620 		madefd = TRUE;
621 		__rpc_set_mac_options(fd, nconf, prog);
622 		if (t_bind(fd, NULL, NULL) == -1)
623 			goto err;
624 		switch (nconf->nc_semantics) {
625 		case NC_TPI_CLTS:
626 			servtype = T_CLTS;
627 			break;
628 		case NC_TPI_COTS:
629 			servtype = T_COTS;
630 			break;
631 		case NC_TPI_COTS_ORD:
632 			servtype = T_COTS_ORD;
633 			break;
634 		default:
635 			if (t_getinfo(fd, &tinfo) == -1)
636 				goto err;
637 			servtype = tinfo.servtype;
638 			break;
639 		}
640 	} else {
641 		int state;		/* Current state of provider */
642 
643 		/*
644 		 * Sync the opened fd.
645 		 * Check whether bound or not, else bind it
646 		 */
647 		if (((state = t_sync(fd)) == -1) ||
648 		    ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) ||
649 		    (t_getinfo(fd, &tinfo) == -1))
650 			goto err;
651 		servtype = tinfo.servtype;
652 		madefd = FALSE;
653 	}
654 
655 	switch (servtype) {
656 	case T_COTS:
657 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
658 				recvsz, tp);
659 		break;
660 	case T_COTS_ORD:
661 		if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
662 		    (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) {
663 			retval =  __td_setnodelay(fd);
664 			if (retval == -1)
665 				goto err;
666 		}
667 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
668 			recvsz, tp);
669 		break;
670 	case T_CLTS:
671 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
672 		break;
673 	default:
674 		goto err;
675 	}
676 
677 	if (cl == NULL)
678 		goto err1; /* borrow errors from clnt_dg/vc creates */
679 	if (nconf) {
680 		cl->cl_netid = strdup(nconf->nc_netid);
681 		if (cl->cl_netid == NULL) {
682 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
683 			rpc_createerr.cf_error.re_errno = errno;
684 			rpc_createerr.cf_error.re_terrno = 0;
685 			syslog(LOG_ERR,
686 				"clnt_tli_create: strdup failed");
687 			goto err1;
688 		}
689 		cl->cl_tp = strdup(nconf->nc_device);
690 		if (cl->cl_tp == NULL) {
691 			if (cl->cl_netid)
692 				free(cl->cl_netid);
693 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
694 			rpc_createerr.cf_error.re_errno = errno;
695 			rpc_createerr.cf_error.re_terrno = 0;
696 			syslog(LOG_ERR,
697 				"clnt_tli_create: strdup failed");
698 			goto err1;
699 		}
700 	} else {
701 		struct netconfig *nc;
702 
703 		if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) {
704 			if (nc->nc_netid) {
705 				cl->cl_netid = strdup(nc->nc_netid);
706 				if (cl->cl_netid == NULL) {
707 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
708 					rpc_createerr.cf_error.re_errno = errno;
709 					rpc_createerr.cf_error.re_terrno = 0;
710 					syslog(LOG_ERR,
711 						"clnt_tli_create: "
712 						"strdup failed");
713 					goto err1;
714 				}
715 			}
716 			if (nc->nc_device) {
717 				cl->cl_tp = strdup(nc->nc_device);
718 				if (cl->cl_tp == NULL) {
719 					if (cl->cl_netid)
720 						free(cl->cl_netid);
721 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
722 					rpc_createerr.cf_error.re_errno = errno;
723 					rpc_createerr.cf_error.re_terrno = 0;
724 					syslog(LOG_ERR,
725 						"clnt_tli_create: "
726 						"strdup failed");
727 					goto err1;
728 				}
729 			}
730 			freenetconfigent(nc);
731 		}
732 		if (cl->cl_netid == NULL)
733 			cl->cl_netid = "";
734 		if (cl->cl_tp == NULL)
735 			cl->cl_tp = "";
736 	}
737 	if (madefd) {
738 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
739 /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
740 	};
741 
742 	return (cl);
743 
744 err:
745 	rpc_createerr.cf_stat = RPC_TLIERROR;
746 	rpc_createerr.cf_error.re_errno = errno;
747 	rpc_createerr.cf_error.re_terrno = t_errno;
748 err1:	if (madefd)
749 		(void) t_close(fd);
750 	return (NULL);
751 }
752 
753 /*
754  *  To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
755  *  we try to not use them.  The __rpc_raise_fd() routine will dup
756  *  a descriptor to a higher value.  If we fail to do it, we continue
757  *  to use the old one (and hope for the best).
758  */
759 int
760 __rpc_raise_fd(int fd)
761 {
762 	int nfd;
763 
764 	if ((nfd = fcntl(fd, F_DUPFD, RPC_MINFD)) == -1)
765 		return (fd);
766 
767 	if (t_sync(nfd) == -1) {
768 		(void) close(nfd);
769 		return (fd);
770 	}
771 
772 	if (t_close(fd) == -1) {
773 		/* this is okay, we will syslog an error, then use the new fd */
774 		(void) syslog(LOG_ERR,
775 			"could not t_close() fd %d; mem & fd leak", fd);
776 	}
777 
778 	return (nfd);
779 }
780