xref: /illumos-gate/usr/src/lib/libnsl/rpc/clnt_generic.c (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
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 	extern int __rpc_minfd;
311 
312 
313 	/*
314 	 * handle const of netclass
315 	 */
316 	if (netclass == NULL)
317 		nettype = NULL;
318 	else {
319 		size_t len = strlen(netclass);
320 		if (len >= sizeof (nettype_array)) {
321 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
322 			return (NULL);
323 		}
324 		(void) strcpy(nettype, netclass);
325 	}
326 
327 	if (tmout == NULL) {
328 		to.tv_sec = 10;
329 		to.tv_usec = 0;
330 	} else
331 		to = *tmout;
332 
333 	if ((handle = __rpc_setconf(nettype)) == NULL) {
334 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
335 		return (NULL);
336 	}
337 
338 	/*
339 	 * Sinct host, and service are const
340 	 */
341 	if (host == NULL || (hostname = strdup(host)) == NULL) {
342 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
343 		rpc_createerr.cf_error.re_errno = (host ? errno : EINVAL);
344 		rpc_createerr.cf_error.re_terrno = 0;
345 		return (NULL);
346 	}
347 
348 	if (service == NULL)
349 		serv = NULL;
350 	else if ((serv = strdup(service ? service : "")) == NULL) {
351 		free(hostname);
352 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
353 		rpc_createerr.cf_error.re_errno = errno;
354 		rpc_createerr.cf_error.re_terrno = 0;
355 		return (NULL);
356 	}
357 
358 	hs.h_host = hostname;
359 	hs.h_serv = port ? NULL : serv;
360 
361 	/*
362 	 * Check to see if a rendezvous over doors should be attempted.
363 	 */
364 	if (__rpc_try_doors(nettype, &try_others)) {
365 		/*
366 		 * Make sure this is the local host.
367 		 */
368 		if (__rpc_is_local_host(hostname)) {
369 			if ((clnt = clnt_door_create(prog, vers, 0)) != NULL)
370 				goto done;
371 			else if (rpc_createerr.cf_stat == RPC_SYSTEMERROR)
372 				goto done;
373 		} else {
374 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
375 		}
376 	}
377 	if (!try_others)
378 		goto done;
379 
380 	rpc_createerr.cf_stat = RPC_SUCCESS;
381 	while (clnt == NULL) {
382 		tbind = NULL;
383 		if ((nconf = __rpc_getconf(handle)) == NULL) {
384 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
385 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
386 			break;
387 		}
388 
389 		if (port) {
390 			if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
391 			    strcmp(nconf->nc_protofmly, NC_INET6) != 0)
392 				continue;
393 		}
394 
395 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
396 			rpc_createerr.cf_stat = RPC_TLIERROR;
397 			rpc_createerr.cf_error.re_errno = errno;
398 			rpc_createerr.cf_error.re_terrno = t_errno;
399 			continue;
400 		}
401 
402 		if (fd < __rpc_minfd)
403 			fd = __rpc_raise_fd(fd);
404 
405 		__rpc_set_mac_options(fd, nconf, prog);
406 
407 		/* LINTED pointer cast */
408 		if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
409 		    == NULL) {
410 			(void) t_close(fd);
411 			rpc_createerr.cf_stat = RPC_TLIERROR;
412 			rpc_createerr.cf_error.re_errno = errno;
413 			rpc_createerr.cf_error.re_terrno = t_errno;
414 			continue;
415 		}
416 
417 		if (netdir_getbyname(nconf, &hs, &raddrs) != ND_OK) {
418 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
419 				rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
420 			if (tbind)
421 				(void) t_free((char *)tbind, T_BIND);
422 			(void) t_close(fd);
423 			continue;
424 		}
425 		(void) memcpy(tbind->addr.buf, raddrs->n_addrs->buf,
426 		    raddrs->n_addrs->len);
427 		tbind->addr.len = raddrs->n_addrs->len;
428 		netdir_free((void *)raddrs, ND_ADDRLIST);
429 
430 		if (port) {
431 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
432 				/* LINTED pointer alignment */
433 				((struct sockaddr_in *)
434 				tbind->addr.buf)->sin_port = htons(port);
435 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
436 				/* LINTED pointer alignment */
437 				((struct sockaddr_in6 *)
438 				tbind->addr.buf)->sin6_port = htons(port);
439 		}
440 
441 		clnt = _clnt_tli_create_timed(fd, nconf, &tbind->addr,
442 					    prog, vers, 0, 0, &to);
443 
444 		if (clnt == NULL) {
445 			if (tbind)
446 				(void) t_free((char *)tbind, T_BIND);
447 			(void) t_close(fd);
448 			continue;
449 		}
450 
451 		(void) CLNT_CONTROL(clnt, CLSET_FD_CLOSE, NULL);
452 
453 		/*
454 		 * Check if we can reach the server with this clnt handle
455 		 * Other clnt_create calls do a ping by contacting the
456 		 * remote rpcbind, here will just try to execute the service's
457 		 * NULL proc.
458 		 */
459 
460 		rpc_createerr.cf_stat = clnt_call(clnt, NULLPROC,
461 						xdr_void, 0, xdr_void, 0, to);
462 
463 		rpc_createerr.cf_error.re_errno = rpc_callerr.re_status;
464 		rpc_createerr.cf_error.re_terrno = 0;
465 
466 		if (rpc_createerr.cf_stat != RPC_SUCCESS) {
467 			clnt_destroy(clnt);
468 			clnt = NULL;
469 			if (tbind)
470 				(void) t_free((char *)tbind, T_BIND);
471 			continue;
472 		} else
473 			break;
474 	}
475 
476 	__rpc_endconf(handle);
477 	if (tbind)
478 		(void) t_free((char *)tbind, T_BIND);
479 
480 done:
481 	if (hostname)
482 		free(hostname);
483 	if (serv)
484 		free(serv);
485 
486 	return (clnt);
487 }
488 
489 /*
490  * Generic client creation: takes (servers name, program-number, netconf) and
491  * returns client handle. Default options are set, which the user can
492  * change using the rpc equivalent of ioctl()'s : clnt_control()
493  * It finds out the server address from rpcbind and calls clnt_tli_create().
494  *
495  * It calls clnt_tp_create_timed() with the default timeout.
496  */
497 CLIENT *
498 clnt_tp_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers,
499     const struct netconfig *nconf)
500 {
501 	return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
502 }
503 
504 /*
505  * This has the same definition as clnt_tp_create(), except it
506  * takes an additional parameter - a pointer to a timeval structure.
507  * A NULL value for the timeout pointer indicates that the default
508  * value for the timeout should be used.
509  */
510 CLIENT *
511 clnt_tp_create_timed(const char *hostname, const rpcprog_t prog,
512     const rpcvers_t vers, const struct netconfig *nconf,
513     const struct timeval *tp)
514 {
515 	struct netbuf *svcaddr;			/* servers address */
516 	CLIENT *cl = NULL;			/* client handle */
517 	extern struct netbuf *__rpcb_findaddr_timed(rpcprog_t, rpcvers_t,
518 	    struct netconfig *, char *, CLIENT **, struct timeval *);
519 
520 	if (nconf == NULL) {
521 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
522 		return (NULL);
523 	}
524 
525 	/*
526 	 * Get the address of the server
527 	 */
528 	if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
529 			(struct netconfig *)nconf, (char *)hostname,
530 			&cl, (struct timeval *)tp)) == NULL) {
531 		/* appropriate error number is set by rpcbind libraries */
532 		return (NULL);
533 	}
534 	if (cl == NULL) {
535 		cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
536 					prog, vers, 0, 0, tp);
537 	} else {
538 		/* Reuse the CLIENT handle and change the appropriate fields */
539 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
540 			if (cl->cl_netid == NULL) {
541 				cl->cl_netid = strdup(nconf->nc_netid);
542 				if (cl->cl_netid == NULL) {
543 					netdir_free((char *)svcaddr, ND_ADDR);
544 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
545 					syslog(LOG_ERR,
546 						"clnt_tp_create_timed: "
547 						"strdup failed.");
548 					return (NULL);
549 				}
550 			}
551 			if (cl->cl_tp == NULL) {
552 				cl->cl_tp = strdup(nconf->nc_device);
553 				if (cl->cl_tp == NULL) {
554 					netdir_free((char *)svcaddr, ND_ADDR);
555 					if (cl->cl_netid)
556 						free(cl->cl_netid);
557 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
558 					syslog(LOG_ERR,
559 						"clnt_tp_create_timed: "
560 						"strdup failed.");
561 					return (NULL);
562 				}
563 			}
564 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
565 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
566 		} else {
567 			CLNT_DESTROY(cl);
568 			cl = _clnt_tli_create_timed(RPC_ANYFD, nconf, svcaddr,
569 					prog, vers, 0, 0, tp);
570 		}
571 	}
572 	netdir_free((char *)svcaddr, ND_ADDR);
573 	return (cl);
574 }
575 
576 /*
577  * Generic client creation:  returns client handle.
578  * Default options are set, which the user can
579  * change using the rpc equivalent of ioctl()'s : clnt_control().
580  * If fd is RPC_ANYFD, it will be opened using nconf.
581  * It will be bound if not so.
582  * If sizes are 0; appropriate defaults will be chosen.
583  */
584 CLIENT *
585 clnt_tli_create(const int fd, const struct netconfig *nconf,
586     struct netbuf *svcaddr, const rpcprog_t prog, const rpcvers_t vers,
587     const uint_t sendsz, const uint_t recvsz)
588 {
589 	return (_clnt_tli_create_timed(fd, nconf, svcaddr, prog, vers, sendsz,
590 		recvsz, NULL));
591 }
592 
593 /*
594  * This has the same definition as clnt_tli_create(), except it
595  * takes an additional parameter - a pointer to a timeval structure.
596  *
597  * Not a public interface. This is for clnt_create_timed,
598  * clnt_create_vers_times, clnt_tp_create_timed to pass down  the
599  * timeout value to COTS creation routine.
600  * (for bug 4049792: clnt_create_timed does not time out)
601  */
602 CLIENT *
603 _clnt_tli_create_timed(int fd, const struct netconfig *nconf,
604 	struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers, uint_t sendsz,
605 	uint_t recvsz, const struct timeval *tp)
606 {
607 	CLIENT *cl;			/* client handle */
608 	struct t_info tinfo;		/* transport info */
609 	bool_t madefd;			/* whether fd opened here */
610 	t_scalar_t servtype;
611 	int retval;
612 	extern int __rpc_minfd;
613 
614 	if (fd == RPC_ANYFD) {
615 		if (nconf == NULL) {
616 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
617 			return (NULL);
618 		}
619 
620 		fd = t_open(nconf->nc_device, O_RDWR, NULL);
621 		if (fd == -1)
622 			goto err;
623 		if (fd < __rpc_minfd)
624 			fd = __rpc_raise_fd(fd);
625 		madefd = TRUE;
626 		__rpc_set_mac_options(fd, nconf, prog);
627 		if (t_bind(fd, NULL, NULL) == -1)
628 			goto err;
629 		switch (nconf->nc_semantics) {
630 		case NC_TPI_CLTS:
631 			servtype = T_CLTS;
632 			break;
633 		case NC_TPI_COTS:
634 			servtype = T_COTS;
635 			break;
636 		case NC_TPI_COTS_ORD:
637 			servtype = T_COTS_ORD;
638 			break;
639 		default:
640 			if (t_getinfo(fd, &tinfo) == -1)
641 				goto err;
642 			servtype = tinfo.servtype;
643 			break;
644 		}
645 	} else {
646 		int state;		/* Current state of provider */
647 
648 		/*
649 		 * Sync the opened fd.
650 		 * Check whether bound or not, else bind it
651 		 */
652 		if (((state = t_sync(fd)) == -1) ||
653 		    ((state == T_UNBND) && (t_bind(fd, NULL, NULL) == -1)) ||
654 		    (t_getinfo(fd, &tinfo) == -1))
655 			goto err;
656 		servtype = tinfo.servtype;
657 		madefd = FALSE;
658 	}
659 
660 	switch (servtype) {
661 	case T_COTS:
662 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
663 				recvsz, tp);
664 		break;
665 	case T_COTS_ORD:
666 		if (nconf && ((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
667 		    (strcmp(nconf->nc_protofmly, NC_INET6) == 0))) {
668 			retval =  __td_setnodelay(fd);
669 			if (retval == -1)
670 				goto err;
671 		}
672 		cl = _clnt_vc_create_timed(fd, svcaddr, prog, vers, sendsz,
673 			recvsz, tp);
674 		break;
675 	case T_CLTS:
676 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
677 		break;
678 	default:
679 		goto err;
680 	}
681 
682 	if (cl == NULL)
683 		goto err1; /* borrow errors from clnt_dg/vc creates */
684 	if (nconf) {
685 		cl->cl_netid = strdup(nconf->nc_netid);
686 		if (cl->cl_netid == NULL) {
687 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
688 			rpc_createerr.cf_error.re_errno = errno;
689 			rpc_createerr.cf_error.re_terrno = 0;
690 			syslog(LOG_ERR,
691 				"clnt_tli_create: strdup failed");
692 			goto err1;
693 		}
694 		cl->cl_tp = strdup(nconf->nc_device);
695 		if (cl->cl_tp == NULL) {
696 			if (cl->cl_netid)
697 				free(cl->cl_netid);
698 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
699 			rpc_createerr.cf_error.re_errno = errno;
700 			rpc_createerr.cf_error.re_terrno = 0;
701 			syslog(LOG_ERR,
702 				"clnt_tli_create: strdup failed");
703 			goto err1;
704 		}
705 	} else {
706 		struct netconfig *nc;
707 
708 		if ((nc = __rpcfd_to_nconf(fd, servtype)) != NULL) {
709 			if (nc->nc_netid) {
710 				cl->cl_netid = strdup(nc->nc_netid);
711 				if (cl->cl_netid == NULL) {
712 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
713 					rpc_createerr.cf_error.re_errno = errno;
714 					rpc_createerr.cf_error.re_terrno = 0;
715 					syslog(LOG_ERR,
716 						"clnt_tli_create: "
717 						"strdup failed");
718 					goto err1;
719 				}
720 			}
721 			if (nc->nc_device) {
722 				cl->cl_tp = strdup(nc->nc_device);
723 				if (cl->cl_tp == NULL) {
724 					if (cl->cl_netid)
725 						free(cl->cl_netid);
726 					rpc_createerr.cf_stat = RPC_SYSTEMERROR;
727 					rpc_createerr.cf_error.re_errno = errno;
728 					rpc_createerr.cf_error.re_terrno = 0;
729 					syslog(LOG_ERR,
730 						"clnt_tli_create: "
731 						"strdup failed");
732 					goto err1;
733 				}
734 			}
735 			freenetconfigent(nc);
736 		}
737 		if (cl->cl_netid == NULL)
738 			cl->cl_netid = "";
739 		if (cl->cl_tp == NULL)
740 			cl->cl_tp = "";
741 	}
742 	if (madefd) {
743 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
744 /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
745 	};
746 
747 	return (cl);
748 
749 err:
750 	rpc_createerr.cf_stat = RPC_TLIERROR;
751 	rpc_createerr.cf_error.re_errno = errno;
752 	rpc_createerr.cf_error.re_terrno = t_errno;
753 err1:	if (madefd)
754 		(void) t_close(fd);
755 	return (NULL);
756 }
757 
758 /*
759  *  To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
760  *  we try to not use them.  The __rpc_raise_fd() routine will dup
761  *  a descriptor to a higher value.  If we fail to do it, we continue
762  *  to use the old one (and hope for the best).
763  */
764 int __rpc_minfd = 3;
765 
766 int
767 __rpc_raise_fd(int fd)
768 {
769 	int nfd;
770 
771 	if (fd >= __rpc_minfd)
772 		return (fd);
773 
774 	if ((nfd = fcntl(fd, F_DUPFD, __rpc_minfd)) == -1)
775 		return (fd);
776 
777 	if (t_sync(nfd) == -1) {
778 		(void) close(nfd);
779 		return (fd);
780 	}
781 
782 	if (t_close(fd) == -1) {
783 		/* this is okay, we will syslog an error, then use the new fd */
784 		(void) syslog(LOG_ERR,
785 			"could not t_close() fd %d; mem & fd leak", fd);
786 	}
787 
788 	return (nfd);
789 }
790