xref: /freebsd/lib/libc/rpc/clnt_vc.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*	$NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009, Sun Microsystems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * - Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15  *   contributors may be used to endorse or promote products derived
16  *   from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
34 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
35 #endif
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 /*
40  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
43  *
44  * TCP based RPC supports 'batched calls'.
45  * A sequence of calls may be batched-up in a send buffer.  The rpc call
46  * return immediately to the client even though the call was not necessarily
47  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
48  * the rpc timeout value is zero (see clnt.h, rpc).
49  *
50  * Clients should NOT casually batch calls that in fact return results; that is,
51  * the server side should be aware that a call is batched and not produce any
52  * return message.  Batched calls that produce many result messages can
53  * deadlock (netlock) the client and the server....
54  *
55  * Now go hang yourself.
56  */
57 
58 #include "namespace.h"
59 #include "reentrant.h"
60 #include <sys/types.h>
61 #include <sys/poll.h>
62 #include <sys/syslog.h>
63 #include <sys/socket.h>
64 #include <sys/un.h>
65 #include <sys/uio.h>
66 
67 #include <arpa/inet.h>
68 #include <assert.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <netdb.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76 #include <signal.h>
77 
78 #include <rpc/rpc.h>
79 #include <rpc/rpcsec_gss.h>
80 #include "un-namespace.h"
81 #include "rpc_com.h"
82 #include "mt_misc.h"
83 
84 #define MCALL_MSG_SIZE 24
85 
86 struct cmessage {
87         struct cmsghdr cmsg;
88         struct cmsgcred cmcred;
89 };
90 
91 static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
92     xdrproc_t, void *, struct timeval);
93 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
94 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
95 static void clnt_vc_abort(CLIENT *);
96 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
97 static void clnt_vc_destroy(CLIENT *);
98 static struct clnt_ops *clnt_vc_ops(void);
99 static bool_t time_not_ok(struct timeval *);
100 static int read_vc(void *, void *, int);
101 static int write_vc(void *, void *, int);
102 static int __msgwrite(int, void *, size_t);
103 static int __msgread(int, void *, size_t);
104 
105 struct ct_data {
106 	int		ct_fd;		/* connection's fd */
107 	bool_t		ct_closeit;	/* close it on destroy */
108 	struct timeval	ct_wait;	/* wait interval in milliseconds */
109 	bool_t          ct_waitset;	/* wait set by clnt_control? */
110 	struct netbuf	ct_addr;	/* remote addr */
111 	struct rpc_err	ct_error;
112 	union {
113 		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
114 		u_int32_t ct_mcalli;
115 	} ct_u;
116 	u_int		ct_mpos;	/* pos after marshal */
117 	XDR		ct_xdrs;	/* XDR stream */
118 };
119 
120 /*
121  *      This machinery implements per-fd locks for MT-safety.  It is not
122  *      sufficient to do per-CLIENT handle locks for MT-safety because a
123  *      user may create more than one CLIENT handle with the same fd behind
124  *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
125  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
126  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
127  *      CLIENT handle created for that fd.
128  *      The current implementation holds locks across the entire RPC and reply.
129  *      Yes, this is silly, and as soon as this code is proven to work, this
130  *      should be the first thing fixed.  One step at a time.
131  */
132 static int      *vc_fd_locks;
133 static cond_t   *vc_cv;
134 #define release_fd_lock(fd, mask) {	\
135 	mutex_lock(&clnt_fd_lock);	\
136 	vc_fd_locks[fd] = 0;		\
137 	mutex_unlock(&clnt_fd_lock);	\
138 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);	\
139 	cond_signal(&vc_cv[fd]);	\
140 }
141 
142 static const char clnt_vc_errstr[] = "%s : %s";
143 static const char clnt_vc_str[] = "clnt_vc_create";
144 static const char __no_mem_str[] = "out of memory";
145 
146 /*
147  * Create a client handle for a connection.
148  * Default options are set, which the user can change using clnt_control()'s.
149  * The rpc/vc package does buffering similar to stdio, so the client
150  * must pick send and receive buffer sizes, 0 => use the default.
151  * NB: fd is copied into a private area.
152  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
153  * set this something more useful.
154  *
155  * fd should be an open socket
156  *
157  * fd - open file descriptor
158  * raddr - servers address
159  * prog  - program number
160  * vers  - version number
161  * sendsz - buffer send size
162  * recvsz - buffer recv size
163  */
164 CLIENT *
165 clnt_vc_create(int fd, const struct netbuf *raddr, const rpcprog_t prog,
166     const rpcvers_t vers, u_int sendsz, u_int recvsz)
167 {
168 	CLIENT *cl;			/* client handle */
169 	struct ct_data *ct = NULL;	/* client handle */
170 	struct timeval now;
171 	struct rpc_msg call_msg;
172 	static u_int32_t disrupt;
173 	sigset_t mask;
174 	sigset_t newmask;
175 	struct sockaddr_storage ss;
176 	socklen_t slen;
177 	struct __rpc_sockinfo si;
178 
179 	if (disrupt == 0)
180 		disrupt = (u_int32_t)(long)raddr;
181 
182 	cl = (CLIENT *)mem_alloc(sizeof (*cl));
183 	ct = (struct ct_data *)mem_alloc(sizeof (*ct));
184 	if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) {
185 		(void) syslog(LOG_ERR, clnt_vc_errstr,
186 		    clnt_vc_str, __no_mem_str);
187 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
188 		rpc_createerr.cf_error.re_errno = errno;
189 		goto err;
190 	}
191 	ct->ct_addr.buf = NULL;
192 	sigfillset(&newmask);
193 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
194 	mutex_lock(&clnt_fd_lock);
195 	if (vc_fd_locks == (int *) NULL) {
196 		int cv_allocsz, fd_allocsz;
197 		int dtbsize = __rpc_dtbsize();
198 
199 		fd_allocsz = dtbsize * sizeof (int);
200 		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
201 		if (vc_fd_locks == (int *) NULL) {
202 			mutex_unlock(&clnt_fd_lock);
203 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
204 			goto err;
205 		} else
206 			memset(vc_fd_locks, '\0', fd_allocsz);
207 
208 		assert(vc_cv == (cond_t *) NULL);
209 		cv_allocsz = dtbsize * sizeof (cond_t);
210 		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
211 		if (vc_cv == (cond_t *) NULL) {
212 			mem_free(vc_fd_locks, fd_allocsz);
213 			vc_fd_locks = (int *) NULL;
214 			mutex_unlock(&clnt_fd_lock);
215 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
216 			goto err;
217 		} else {
218 			int i;
219 
220 			for (i = 0; i < dtbsize; i++)
221 				cond_init(&vc_cv[i], 0, (void *) 0);
222 		}
223 	} else
224 		assert(vc_cv != (cond_t *) NULL);
225 
226 	/*
227 	 * XXX - fvdl connecting while holding a mutex?
228 	 */
229 	slen = sizeof ss;
230 	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
231 		if (errno != ENOTCONN) {
232 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
233 			rpc_createerr.cf_error.re_errno = errno;
234 			mutex_unlock(&clnt_fd_lock);
235 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
236 			goto err;
237 		}
238 		if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
239 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
240 			rpc_createerr.cf_error.re_errno = errno;
241 			mutex_unlock(&clnt_fd_lock);
242 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
243 			goto err;
244 		}
245 	}
246 	mutex_unlock(&clnt_fd_lock);
247 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
248 	if (!__rpc_fd2sockinfo(fd, &si))
249 		goto err;
250 
251 	ct->ct_closeit = FALSE;
252 
253 	/*
254 	 * Set up private data struct
255 	 */
256 	ct->ct_fd = fd;
257 	ct->ct_wait.tv_usec = 0;
258 	ct->ct_waitset = FALSE;
259 	ct->ct_addr.buf = malloc(raddr->maxlen);
260 	if (ct->ct_addr.buf == NULL)
261 		goto err;
262 	memcpy(ct->ct_addr.buf, raddr->buf, raddr->len);
263 	ct->ct_addr.len = raddr->len;
264 	ct->ct_addr.maxlen = raddr->maxlen;
265 
266 	/*
267 	 * Initialize call message
268 	 */
269 	(void)gettimeofday(&now, NULL);
270 	call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
271 	call_msg.rm_direction = CALL;
272 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
273 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
274 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
275 
276 	/*
277 	 * pre-serialize the static part of the call msg and stash it away
278 	 */
279 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
280 	    XDR_ENCODE);
281 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
282 		if (ct->ct_closeit) {
283 			(void)_close(fd);
284 		}
285 		goto err;
286 	}
287 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
288 	XDR_DESTROY(&(ct->ct_xdrs));
289 	assert(ct->ct_mpos + sizeof(uint32_t) <= MCALL_MSG_SIZE);
290 
291 	/*
292 	 * Create a client handle which uses xdrrec for serialization
293 	 * and authnone for authentication.
294 	 */
295 	cl->cl_ops = clnt_vc_ops();
296 	cl->cl_private = ct;
297 	cl->cl_auth = authnone_create();
298 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
299 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
300 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
301 	    cl->cl_private, read_vc, write_vc);
302 	return (cl);
303 
304 err:
305 	if (ct) {
306 		if (ct->ct_addr.len)
307 			mem_free(ct->ct_addr.buf, ct->ct_addr.len);
308 		mem_free(ct, sizeof (struct ct_data));
309 	}
310 	if (cl)
311 		mem_free(cl, sizeof (CLIENT));
312 	return ((CLIENT *)NULL);
313 }
314 
315 static enum clnt_stat
316 clnt_vc_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xdr_args, void *args_ptr,
317     xdrproc_t xdr_results, void *results_ptr, struct timeval timeout)
318 {
319 	struct ct_data *ct = (struct ct_data *) cl->cl_private;
320 	XDR *xdrs = &(ct->ct_xdrs);
321 	struct rpc_msg reply_msg;
322 	u_int32_t x_id;
323 	u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli;    /* yuk */
324 	bool_t shipnow;
325 	int refreshes = 2;
326 	sigset_t mask, newmask;
327 	int rpc_lock_value;
328 	bool_t reply_stat;
329 
330 	assert(cl != NULL);
331 
332 	sigfillset(&newmask);
333 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
334 	mutex_lock(&clnt_fd_lock);
335 	while (vc_fd_locks[ct->ct_fd])
336 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
337 	if (__isthreaded)
338                 rpc_lock_value = 1;
339         else
340                 rpc_lock_value = 0;
341 	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
342 	mutex_unlock(&clnt_fd_lock);
343 	if (!ct->ct_waitset) {
344 		/* If time is not within limits, we ignore it. */
345 		if (time_not_ok(&timeout) == FALSE)
346 			ct->ct_wait = timeout;
347 	}
348 
349 	shipnow =
350 	    (xdr_results == NULL && timeout.tv_sec == 0
351 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
352 
353 call_again:
354 	xdrs->x_op = XDR_ENCODE;
355 	ct->ct_error.re_status = RPC_SUCCESS;
356 	x_id = ntohl(--(*msg_x_id));
357 
358 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
359 		if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
360 		    (! XDR_PUTINT32(xdrs, &proc)) ||
361 		    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
362 		    (! (*xdr_args)(xdrs, args_ptr))) {
363 			if (ct->ct_error.re_status == RPC_SUCCESS)
364 				ct->ct_error.re_status = RPC_CANTENCODEARGS;
365 			(void)xdrrec_endofrecord(xdrs, TRUE);
366 			release_fd_lock(ct->ct_fd, mask);
367 			return (ct->ct_error.re_status);
368 		}
369 	} else {
370 		*(uint32_t *) &ct->ct_u.ct_mcallc[ct->ct_mpos] = htonl(proc);
371 		if (! __rpc_gss_wrap(cl->cl_auth, ct->ct_u.ct_mcallc,
372 			ct->ct_mpos + sizeof(uint32_t),
373 			xdrs, xdr_args, args_ptr)) {
374 			if (ct->ct_error.re_status == RPC_SUCCESS)
375 				ct->ct_error.re_status = RPC_CANTENCODEARGS;
376 			(void)xdrrec_endofrecord(xdrs, TRUE);
377 			release_fd_lock(ct->ct_fd, mask);
378 			return (ct->ct_error.re_status);
379 		}
380 	}
381 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
382 		release_fd_lock(ct->ct_fd, mask);
383 		return (ct->ct_error.re_status = RPC_CANTSEND);
384 	}
385 	if (! shipnow) {
386 		release_fd_lock(ct->ct_fd, mask);
387 		return (RPC_SUCCESS);
388 	}
389 	/*
390 	 * Hack to provide rpc-based message passing
391 	 */
392 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
393 		release_fd_lock(ct->ct_fd, mask);
394 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
395 	}
396 
397 
398 	/*
399 	 * Keep receiving until we get a valid transaction id
400 	 */
401 	xdrs->x_op = XDR_DECODE;
402 	while (TRUE) {
403 		reply_msg.acpted_rply.ar_verf = _null_auth;
404 		reply_msg.acpted_rply.ar_results.where = NULL;
405 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
406 		if (! xdrrec_skiprecord(xdrs)) {
407 			release_fd_lock(ct->ct_fd, mask);
408 			return (ct->ct_error.re_status);
409 		}
410 		/* now decode and validate the response header */
411 		if (! xdr_replymsg(xdrs, &reply_msg)) {
412 			if (ct->ct_error.re_status == RPC_SUCCESS)
413 				continue;
414 			release_fd_lock(ct->ct_fd, mask);
415 			return (ct->ct_error.re_status);
416 		}
417 		if (reply_msg.rm_xid == x_id)
418 			break;
419 	}
420 
421 	/*
422 	 * process header
423 	 */
424 	_seterr_reply(&reply_msg, &(ct->ct_error));
425 	if (ct->ct_error.re_status == RPC_SUCCESS) {
426 		if (! AUTH_VALIDATE(cl->cl_auth,
427 		    &reply_msg.acpted_rply.ar_verf)) {
428 			ct->ct_error.re_status = RPC_AUTHERROR;
429 			ct->ct_error.re_why = AUTH_INVALIDRESP;
430 		} else {
431 			if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
432 				reply_stat = (*xdr_results)(xdrs, results_ptr);
433 			} else {
434 				reply_stat = __rpc_gss_unwrap(cl->cl_auth,
435 				    xdrs, xdr_results, results_ptr);
436 			}
437 			if (! reply_stat) {
438 				if (ct->ct_error.re_status == RPC_SUCCESS)
439 					ct->ct_error.re_status =
440 						RPC_CANTDECODERES;
441 			}
442 		}
443 		/* free verifier ... */
444 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
445 			xdrs->x_op = XDR_FREE;
446 			(void)xdr_opaque_auth(xdrs,
447 			    &(reply_msg.acpted_rply.ar_verf));
448 		}
449 	}  /* end successful completion */
450 	else {
451 		/* maybe our credentials need to be refreshed ... */
452 		if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg))
453 			goto call_again;
454 	}  /* end of unsuccessful completion */
455 	release_fd_lock(ct->ct_fd, mask);
456 	return (ct->ct_error.re_status);
457 }
458 
459 static void
460 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
461 {
462 	struct ct_data *ct;
463 
464 	assert(cl != NULL);
465 	assert(errp != NULL);
466 
467 	ct = (struct ct_data *) cl->cl_private;
468 	*errp = ct->ct_error;
469 }
470 
471 static bool_t
472 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
473 {
474 	struct ct_data *ct;
475 	XDR *xdrs;
476 	bool_t dummy;
477 	sigset_t mask;
478 	sigset_t newmask;
479 
480 	assert(cl != NULL);
481 
482 	ct = (struct ct_data *)cl->cl_private;
483 	xdrs = &(ct->ct_xdrs);
484 
485 	sigfillset(&newmask);
486 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
487 	mutex_lock(&clnt_fd_lock);
488 	while (vc_fd_locks[ct->ct_fd])
489 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
490 	xdrs->x_op = XDR_FREE;
491 	dummy = (*xdr_res)(xdrs, res_ptr);
492 	mutex_unlock(&clnt_fd_lock);
493 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
494 	cond_signal(&vc_cv[ct->ct_fd]);
495 
496 	return dummy;
497 }
498 
499 /*ARGSUSED*/
500 static void
501 clnt_vc_abort(CLIENT *cl)
502 {
503 }
504 
505 static bool_t
506 clnt_vc_control(CLIENT *cl, u_int request, void *info)
507 {
508 	struct ct_data *ct;
509 	void *infop = info;
510 	sigset_t mask;
511 	sigset_t newmask;
512 	int rpc_lock_value;
513 
514 	assert(cl != NULL);
515 
516 	ct = (struct ct_data *)cl->cl_private;
517 
518 	sigfillset(&newmask);
519 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
520 	mutex_lock(&clnt_fd_lock);
521 	while (vc_fd_locks[ct->ct_fd])
522 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
523 	if (__isthreaded)
524                 rpc_lock_value = 1;
525         else
526                 rpc_lock_value = 0;
527 	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
528 	mutex_unlock(&clnt_fd_lock);
529 
530 	switch (request) {
531 	case CLSET_FD_CLOSE:
532 		ct->ct_closeit = TRUE;
533 		release_fd_lock(ct->ct_fd, mask);
534 		return (TRUE);
535 	case CLSET_FD_NCLOSE:
536 		ct->ct_closeit = FALSE;
537 		release_fd_lock(ct->ct_fd, mask);
538 		return (TRUE);
539 	default:
540 		break;
541 	}
542 
543 	/* for other requests which use info */
544 	if (info == NULL) {
545 		release_fd_lock(ct->ct_fd, mask);
546 		return (FALSE);
547 	}
548 	switch (request) {
549 	case CLSET_TIMEOUT:
550 		if (time_not_ok((struct timeval *)info)) {
551 			release_fd_lock(ct->ct_fd, mask);
552 			return (FALSE);
553 		}
554 		ct->ct_wait = *(struct timeval *)infop;
555 		ct->ct_waitset = TRUE;
556 		break;
557 	case CLGET_TIMEOUT:
558 		*(struct timeval *)infop = ct->ct_wait;
559 		break;
560 	case CLGET_SERVER_ADDR:
561 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
562 		break;
563 	case CLGET_FD:
564 		*(int *)info = ct->ct_fd;
565 		break;
566 	case CLGET_SVC_ADDR:
567 		/* The caller should not free this memory area */
568 		*(struct netbuf *)info = ct->ct_addr;
569 		break;
570 	case CLSET_SVC_ADDR:		/* set to new address */
571 		release_fd_lock(ct->ct_fd, mask);
572 		return (FALSE);
573 	case CLGET_XID:
574 		/*
575 		 * use the knowledge that xid is the
576 		 * first element in the call structure
577 		 * This will get the xid of the PREVIOUS call
578 		 */
579 		*(u_int32_t *)info =
580 		    ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
581 		break;
582 	case CLSET_XID:
583 		/* This will set the xid of the NEXT call */
584 		*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
585 		    htonl(*((u_int32_t *)info) + 1);
586 		/* increment by 1 as clnt_vc_call() decrements once */
587 		break;
588 	case CLGET_VERS:
589 		/*
590 		 * This RELIES on the information that, in the call body,
591 		 * the version number field is the fifth field from the
592 		 * begining of the RPC header. MUST be changed if the
593 		 * call_struct is changed
594 		 */
595 		*(u_int32_t *)info =
596 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
597 		    4 * BYTES_PER_XDR_UNIT));
598 		break;
599 
600 	case CLSET_VERS:
601 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
602 		    4 * BYTES_PER_XDR_UNIT) =
603 		    htonl(*(u_int32_t *)info);
604 		break;
605 
606 	case CLGET_PROG:
607 		/*
608 		 * This RELIES on the information that, in the call body,
609 		 * the program number field is the fourth field from the
610 		 * begining of the RPC header. MUST be changed if the
611 		 * call_struct is changed
612 		 */
613 		*(u_int32_t *)info =
614 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
615 		    3 * BYTES_PER_XDR_UNIT));
616 		break;
617 
618 	case CLSET_PROG:
619 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
620 		    3 * BYTES_PER_XDR_UNIT) =
621 		    htonl(*(u_int32_t *)info);
622 		break;
623 
624 	default:
625 		release_fd_lock(ct->ct_fd, mask);
626 		return (FALSE);
627 	}
628 	release_fd_lock(ct->ct_fd, mask);
629 	return (TRUE);
630 }
631 
632 
633 static void
634 clnt_vc_destroy(CLIENT *cl)
635 {
636 	struct ct_data *ct = (struct ct_data *) cl->cl_private;
637 	int ct_fd = ct->ct_fd;
638 	sigset_t mask;
639 	sigset_t newmask;
640 
641 	assert(cl != NULL);
642 
643 	ct = (struct ct_data *) cl->cl_private;
644 
645 	sigfillset(&newmask);
646 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
647 	mutex_lock(&clnt_fd_lock);
648 	while (vc_fd_locks[ct_fd])
649 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
650 	if (ct->ct_closeit && ct->ct_fd != -1) {
651 		(void)_close(ct->ct_fd);
652 	}
653 	XDR_DESTROY(&(ct->ct_xdrs));
654 	free(ct->ct_addr.buf);
655 	mem_free(ct, sizeof(struct ct_data));
656 	if (cl->cl_netid && cl->cl_netid[0])
657 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
658 	if (cl->cl_tp && cl->cl_tp[0])
659 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
660 	mem_free(cl, sizeof(CLIENT));
661 	mutex_unlock(&clnt_fd_lock);
662 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
663 	cond_signal(&vc_cv[ct_fd]);
664 }
665 
666 /*
667  * Interface between xdr serializer and tcp connection.
668  * Behaves like the system calls, read & write, but keeps some error state
669  * around for the rpc level.
670  */
671 static int
672 read_vc(void *ctp, void *buf, int len)
673 {
674 	struct sockaddr sa;
675 	socklen_t sal;
676 	struct ct_data *ct = (struct ct_data *)ctp;
677 	struct pollfd fd;
678 	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
679 	    (ct->ct_wait.tv_usec / 1000));
680 
681 	if (len == 0)
682 		return (0);
683 	fd.fd = ct->ct_fd;
684 	fd.events = POLLIN;
685 	for (;;) {
686 		switch (_poll(&fd, 1, milliseconds)) {
687 		case 0:
688 			ct->ct_error.re_status = RPC_TIMEDOUT;
689 			return (-1);
690 
691 		case -1:
692 			if (errno == EINTR)
693 				continue;
694 			ct->ct_error.re_status = RPC_CANTRECV;
695 			ct->ct_error.re_errno = errno;
696 			return (-1);
697 		}
698 		break;
699 	}
700 
701 	sal = sizeof(sa);
702 	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
703 	    (sa.sa_family == AF_LOCAL)) {
704 		len = __msgread(ct->ct_fd, buf, (size_t)len);
705 	} else {
706 		len = _read(ct->ct_fd, buf, (size_t)len);
707 	}
708 
709 	switch (len) {
710 	case 0:
711 		/* premature eof */
712 		ct->ct_error.re_errno = ECONNRESET;
713 		ct->ct_error.re_status = RPC_CANTRECV;
714 		len = -1;  /* it's really an error */
715 		break;
716 
717 	case -1:
718 		ct->ct_error.re_errno = errno;
719 		ct->ct_error.re_status = RPC_CANTRECV;
720 		break;
721 	}
722 	return (len);
723 }
724 
725 static int
726 write_vc(void *ctp, void *buf, int len)
727 {
728 	struct sockaddr sa;
729 	socklen_t sal;
730 	struct ct_data *ct = (struct ct_data *)ctp;
731 	int i, cnt;
732 
733 	sal = sizeof(sa);
734 	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
735 	    (sa.sa_family == AF_LOCAL)) {
736 		for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
737 			if ((i = __msgwrite(ct->ct_fd, buf,
738 			     (size_t)cnt)) == -1) {
739 				ct->ct_error.re_errno = errno;
740 				ct->ct_error.re_status = RPC_CANTSEND;
741 				return (-1);
742 			}
743 		}
744 	} else {
745 		for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
746 			if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
747 				ct->ct_error.re_errno = errno;
748 				ct->ct_error.re_status = RPC_CANTSEND;
749 				return (-1);
750 			}
751 		}
752 	}
753 	return (len);
754 }
755 
756 static struct clnt_ops *
757 clnt_vc_ops(void)
758 {
759 	static struct clnt_ops ops;
760 	sigset_t mask, newmask;
761 
762 	/* VARIABLES PROTECTED BY ops_lock: ops */
763 
764 	sigfillset(&newmask);
765 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
766 	mutex_lock(&ops_lock);
767 	if (ops.cl_call == NULL) {
768 		ops.cl_call = clnt_vc_call;
769 		ops.cl_abort = clnt_vc_abort;
770 		ops.cl_geterr = clnt_vc_geterr;
771 		ops.cl_freeres = clnt_vc_freeres;
772 		ops.cl_destroy = clnt_vc_destroy;
773 		ops.cl_control = clnt_vc_control;
774 	}
775 	mutex_unlock(&ops_lock);
776 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
777 	return (&ops);
778 }
779 
780 /*
781  * Make sure that the time is not garbage.   -1 value is disallowed.
782  * Note this is different from time_not_ok in clnt_dg.c
783  */
784 static bool_t
785 time_not_ok(struct timeval *t)
786 {
787 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
788 		t->tv_usec <= -1 || t->tv_usec > 1000000);
789 }
790 
791 static int
792 __msgread(int sock, void *buf, size_t cnt)
793 {
794 	struct iovec iov[1];
795 	struct msghdr msg;
796 	union {
797 		struct cmsghdr cmsg;
798 		char control[CMSG_SPACE(sizeof(struct cmsgcred))];
799 	} cm;
800 
801 	bzero((char *)&cm, sizeof(cm));
802 	iov[0].iov_base = buf;
803 	iov[0].iov_len = cnt;
804 
805 	msg.msg_iov = iov;
806 	msg.msg_iovlen = 1;
807 	msg.msg_name = NULL;
808 	msg.msg_namelen = 0;
809 	msg.msg_control = (caddr_t)&cm;
810 	msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
811 	msg.msg_flags = 0;
812 
813 	return(_recvmsg(sock, &msg, 0));
814 }
815 
816 static int
817 __msgwrite(int sock, void *buf, size_t cnt)
818 {
819 	struct iovec iov[1];
820 	struct msghdr msg;
821 	union {
822 		struct cmsghdr cmsg;
823 		char control[CMSG_SPACE(sizeof(struct cmsgcred))];
824 	} cm;
825 
826 	bzero((char *)&cm, sizeof(cm));
827 	iov[0].iov_base = buf;
828 	iov[0].iov_len = cnt;
829 
830 	cm.cmsg.cmsg_type = SCM_CREDS;
831 	cm.cmsg.cmsg_level = SOL_SOCKET;
832 	cm.cmsg.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
833 
834 	msg.msg_iov = iov;
835 	msg.msg_iovlen = 1;
836 	msg.msg_name = NULL;
837 	msg.msg_namelen = 0;
838 	msg.msg_control = (caddr_t)&cm;
839 	msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
840 	msg.msg_flags = 0;
841 
842 	return(_sendmsg(sock, &msg, 0));
843 }
844