Home | History | Annotate | Line # | Download | only in rpc
clnt_vc.c revision 1.7.2.4
      1 /*	$NetBSD: clnt_vc.c,v 1.7.2.4 2002/11/11 22:22:39 nathanw Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
     36 static char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
     37 static char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
     38 #else
     39 __RCSID("$NetBSD: clnt_vc.c,v 1.7.2.4 2002/11/11 22:22:39 nathanw Exp $");
     40 #endif
     41 #endif
     42 
     43 /*
     44  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
     45  *
     46  * Copyright (C) 1984, Sun Microsystems, Inc.
     47  *
     48  * TCP based RPC supports 'batched calls'.
     49  * A sequence of calls may be batched-up in a send buffer.  The rpc call
     50  * return immediately to the client even though the call was not necessarily
     51  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
     52  * the rpc timeout value is zero (see clnt.h, rpc).
     53  *
     54  * Clients should NOT casually batch calls that in fact return results; that is,
     55  * the server side should be aware that a call is batched and not produce any
     56  * return message.  Batched calls that produce many result messages can
     57  * deadlock (netlock) the client and the server....
     58  *
     59  * Now go hang yourself.
     60  */
     61 
     62 #include "namespace.h"
     63 #include "reentrant.h"
     64 #include <sys/types.h>
     65 #include <sys/poll.h>
     66 #include <sys/socket.h>
     67 
     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 
     80 #include "rpc_internal.h"
     81 
     82 #ifdef __weak_alias
     83 __weak_alias(clnt_vc_create,_clnt_vc_create)
     84 #endif
     85 
     86 #define MCALL_MSG_SIZE 24
     87 
     88 static enum clnt_stat clnt_vc_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t,
     89     xdrproc_t, caddr_t, struct timeval));
     90 static void clnt_vc_geterr __P((CLIENT *, struct rpc_err *));
     91 static bool_t clnt_vc_freeres __P((CLIENT *, xdrproc_t, caddr_t));
     92 static void clnt_vc_abort __P((CLIENT *));
     93 static bool_t clnt_vc_control __P((CLIENT *, u_int, char *));
     94 static void clnt_vc_destroy __P((CLIENT *));
     95 static struct clnt_ops *clnt_vc_ops __P((void));
     96 static bool_t time_not_ok __P((struct timeval *));
     97 static int read_vc __P((caddr_t, caddr_t, int));
     98 static int write_vc __P((caddr_t, caddr_t, int));
     99 
    100 struct ct_data {
    101 	int		ct_fd;
    102 	bool_t		ct_closeit;
    103 	struct timeval	ct_wait;
    104 	bool_t          ct_waitset;       /* wait set by clnt_control? */
    105 	struct netbuf	ct_addr;
    106 	struct rpc_err	ct_error;
    107 	union {
    108 		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
    109 		u_int32_t ct_mcalli;
    110 	} ct_u;
    111 	u_int		ct_mpos;			/* pos after marshal */
    112 	XDR		ct_xdrs;
    113 };
    114 
    115 /*
    116  *      This machinery implements per-fd locks for MT-safety.  It is not
    117  *      sufficient to do per-CLIENT handle locks for MT-safety because a
    118  *      user may create more than one CLIENT handle with the same fd behind
    119  *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
    120  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
    121  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
    122  *      CLIENT handle created for that fd.
    123  *      The current implementation holds locks across the entire RPC and reply.
    124  *      Yes, this is silly, and as soon as this code is proven to work, this
    125  *      should be the first thing fixed.  One step at a time.
    126  */
    127 #ifdef _REENTRANT
    128 static int      *vc_fd_locks;
    129 extern int __isthreaded;
    130 #define __rpc_lock_value __isthreaded;
    131 extern mutex_t  clnt_fd_lock;
    132 static cond_t   *vc_cv;
    133 #define release_fd_lock(fd, mask) {             \
    134 	mutex_lock(&clnt_fd_lock);      \
    135 	vc_fd_locks[fd] = 0;            \
    136 	mutex_unlock(&clnt_fd_lock);    \
    137 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);        \
    138 	cond_signal(&vc_cv[fd]);        \
    139 }
    140 #else
    141 #define release_fd_lock(fd,mask)
    142 #define __rpc_lock_value 0
    143 #endif
    144 
    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 CLIENT *
    158 clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
    159 	int fd;
    160 	const struct netbuf *raddr;
    161 	rpcprog_t prog;
    162 	rpcvers_t vers;
    163 	u_int sendsz;
    164 	u_int recvsz;
    165 {
    166 	CLIENT *h;
    167 	struct ct_data *ct = NULL;
    168 	struct timeval now;
    169 	struct rpc_msg call_msg;
    170 	static u_int32_t disrupt;
    171 #ifdef _REENTRANT
    172 	sigset_t mask;
    173 #endif
    174 	sigset_t newmask;
    175 	struct sockaddr_storage ss;
    176 	socklen_t slen;
    177 	struct __rpc_sockinfo si;
    178 
    179 	_DIAGASSERT(raddr != NULL);
    180 
    181 	if (disrupt == 0)
    182 		disrupt = (u_int32_t)(long)raddr;
    183 
    184 	h  = mem_alloc(sizeof(*h));
    185 	if (h == NULL) {
    186 		warnx("clnt_vc_create: out of memory");
    187 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    188 		rpc_createerr.cf_error.re_errno = errno;
    189 		goto fooy;
    190 	}
    191 	ct = mem_alloc(sizeof(*ct));
    192 	if (ct == NULL) {
    193 		warnx("clnt_vc_create: out of memory");
    194 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    195 		rpc_createerr.cf_error.re_errno = errno;
    196 		goto fooy;
    197 	}
    198 
    199 	sigfillset(&newmask);
    200 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    201 #ifdef _REENTRANT
    202 	mutex_lock(&clnt_fd_lock);
    203 	if (vc_fd_locks == (int *) NULL) {
    204 		size_t cv_allocsz, fd_allocsz;
    205 		int dtbsize = __rpc_dtbsize();
    206 
    207 		fd_allocsz = dtbsize * sizeof (int);
    208 		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
    209 		if (vc_fd_locks == (int *) NULL) {
    210 			mutex_unlock(&clnt_fd_lock);
    211 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    212 			goto fooy;
    213 		} else
    214 			memset(vc_fd_locks, '\0', fd_allocsz);
    215 
    216 		assert(vc_cv == (cond_t *) NULL);
    217 		cv_allocsz = dtbsize * sizeof (cond_t);
    218 		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
    219 		if (vc_cv == (cond_t *) NULL) {
    220 			mem_free(vc_fd_locks, fd_allocsz);
    221 			vc_fd_locks = (int *) NULL;
    222 			mutex_unlock(&clnt_fd_lock);
    223 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    224 			goto fooy;
    225 		} else {
    226 			int i;
    227 
    228 			for (i = 0; i < dtbsize; i++)
    229 				cond_init(&vc_cv[i], 0, (void *) 0);
    230 		}
    231 	} else
    232 		assert(vc_cv != (cond_t *) NULL);
    233 #endif
    234 
    235 	/*
    236 	 * XXX - fvdl connecting while holding a mutex?
    237 	 */
    238 	slen = sizeof ss;
    239 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    240 		if (errno != ENOTCONN) {
    241 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    242 			rpc_createerr.cf_error.re_errno = errno;
    243 			mutex_unlock(&clnt_fd_lock);
    244 			goto fooy;
    245 		}
    246 		if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
    247 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    248 			rpc_createerr.cf_error.re_errno = errno;
    249 			mutex_unlock(&clnt_fd_lock);
    250 			goto fooy;
    251 		}
    252 	}
    253 	mutex_unlock(&clnt_fd_lock);
    254 	if (!__rpc_fd2sockinfo(fd, &si))
    255 		goto fooy;
    256 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    257 
    258 	ct->ct_closeit = FALSE;
    259 
    260 	/*
    261 	 * Set up private data struct
    262 	 */
    263 	ct->ct_fd = fd;
    264 	ct->ct_wait.tv_usec = 0;
    265 	ct->ct_waitset = FALSE;
    266 	ct->ct_addr.buf = malloc(raddr->maxlen);
    267 	if (ct->ct_addr.buf == NULL)
    268 		goto fooy;
    269 	memcpy(ct->ct_addr.buf, &raddr->buf, raddr->len);
    270 	ct->ct_addr.len = raddr->maxlen;
    271 	ct->ct_addr.maxlen = raddr->maxlen;
    272 
    273 	/*
    274 	 * Initialize call message
    275 	 */
    276 	(void)gettimeofday(&now, NULL);
    277 	call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
    278 	call_msg.rm_direction = CALL;
    279 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    280 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
    281 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
    282 
    283 	/*
    284 	 * pre-serialize the static part of the call msg and stash it away
    285 	 */
    286 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
    287 	    XDR_ENCODE);
    288 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
    289 		if (ct->ct_closeit) {
    290 			(void)close(fd);
    291 		}
    292 		goto fooy;
    293 	}
    294 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
    295 	XDR_DESTROY(&(ct->ct_xdrs));
    296 
    297 	/*
    298 	 * Create a client handle which uses xdrrec for serialization
    299 	 * and authnone for authentication.
    300 	 */
    301 	h->cl_ops = clnt_vc_ops();
    302 	h->cl_private = ct;
    303 	h->cl_auth = authnone_create();
    304 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
    305 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
    306 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
    307 	    h->cl_private, read_vc, write_vc);
    308 	return (h);
    309 
    310 fooy:
    311 	/*
    312 	 * Something goofed, free stuff and barf
    313 	 */
    314 	if (ct)
    315 		mem_free(ct, sizeof(struct ct_data));
    316 	if (h)
    317 		mem_free(h, sizeof(CLIENT));
    318 	return (NULL);
    319 }
    320 
    321 static enum clnt_stat
    322 clnt_vc_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
    323 	CLIENT *h;
    324 	rpcproc_t proc;
    325 	xdrproc_t xdr_args;
    326 	caddr_t args_ptr;
    327 	xdrproc_t xdr_results;
    328 	caddr_t results_ptr;
    329 	struct timeval timeout;
    330 {
    331 	struct ct_data *ct;
    332 	XDR *xdrs;
    333 	struct rpc_msg reply_msg;
    334 	u_int32_t x_id;
    335 	u_int32_t *msg_x_id;
    336 	bool_t shipnow;
    337 	int refreshes = 2;
    338 #ifdef _REENTRANT
    339 	sigset_t mask, newmask;
    340 #endif
    341 
    342 	_DIAGASSERT(h != NULL);
    343 
    344 	ct = (struct ct_data *) h->cl_private;
    345 
    346 #ifdef _REENTRANT
    347 	sigfillset(&newmask);
    348 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    349 	mutex_lock(&clnt_fd_lock);
    350 	while (vc_fd_locks[ct->ct_fd])
    351 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
    352 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
    353 	mutex_unlock(&clnt_fd_lock);
    354 #endif
    355 
    356 	xdrs = &(ct->ct_xdrs);
    357 	msg_x_id = &ct->ct_u.ct_mcalli;
    358 
    359 	if (!ct->ct_waitset) {
    360 		if (time_not_ok(&timeout) == FALSE)
    361 		ct->ct_wait = timeout;
    362 	}
    363 
    364 	shipnow =
    365 	    (xdr_results == NULL && timeout.tv_sec == 0
    366 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
    367 
    368 call_again:
    369 	xdrs->x_op = XDR_ENCODE;
    370 	ct->ct_error.re_status = RPC_SUCCESS;
    371 	x_id = ntohl(--(*msg_x_id));
    372 	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
    373 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
    374 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
    375 	    (! (*xdr_args)(xdrs, args_ptr))) {
    376 		if (ct->ct_error.re_status == RPC_SUCCESS)
    377 			ct->ct_error.re_status = RPC_CANTENCODEARGS;
    378 		(void)xdrrec_endofrecord(xdrs, TRUE);
    379 		release_fd_lock(ct->ct_fd, mask);
    380 		return (ct->ct_error.re_status);
    381 	}
    382 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
    383 		release_fd_lock(ct->ct_fd, mask);
    384 		return (ct->ct_error.re_status = RPC_CANTSEND);
    385 	}
    386 	if (! shipnow) {
    387 		release_fd_lock(ct->ct_fd, mask);
    388 		return (RPC_SUCCESS);
    389 	}
    390 	/*
    391 	 * Hack to provide rpc-based message passing
    392 	 */
    393 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
    394 		release_fd_lock(ct->ct_fd, mask);
    395 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
    396 	}
    397 
    398 
    399 	/*
    400 	 * Keep receiving until we get a valid transaction id
    401 	 */
    402 	xdrs->x_op = XDR_DECODE;
    403 	for (;;) {
    404 		reply_msg.acpted_rply.ar_verf = _null_auth;
    405 		reply_msg.acpted_rply.ar_results.where = NULL;
    406 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
    407 		if (! xdrrec_skiprecord(xdrs)) {
    408 			release_fd_lock(ct->ct_fd, mask);
    409 			return (ct->ct_error.re_status);
    410 		}
    411 		/* now decode and validate the response header */
    412 		if (! xdr_replymsg(xdrs, &reply_msg)) {
    413 			if (ct->ct_error.re_status == RPC_SUCCESS)
    414 				continue;
    415 			release_fd_lock(ct->ct_fd, mask);
    416 			return (ct->ct_error.re_status);
    417 		}
    418 		if (reply_msg.rm_xid == x_id)
    419 			break;
    420 	}
    421 
    422 	/*
    423 	 * process header
    424 	 */
    425 	_seterr_reply(&reply_msg, &(ct->ct_error));
    426 	if (ct->ct_error.re_status == RPC_SUCCESS) {
    427 		if (! AUTH_VALIDATE(h->cl_auth,
    428 		    &reply_msg.acpted_rply.ar_verf)) {
    429 			ct->ct_error.re_status = RPC_AUTHERROR;
    430 			ct->ct_error.re_why = AUTH_INVALIDRESP;
    431 		} else if (! (*xdr_results)(xdrs, results_ptr)) {
    432 			if (ct->ct_error.re_status == RPC_SUCCESS)
    433 				ct->ct_error.re_status = RPC_CANTDECODERES;
    434 		}
    435 		/* free verifier ... */
    436 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
    437 			xdrs->x_op = XDR_FREE;
    438 			(void)xdr_opaque_auth(xdrs,
    439 			    &(reply_msg.acpted_rply.ar_verf));
    440 		}
    441 	}  /* end successful completion */
    442 	else {
    443 		/* maybe our credentials need to be refreshed ... */
    444 		if (refreshes-- && AUTH_REFRESH(h->cl_auth))
    445 			goto call_again;
    446 	}  /* end of unsuccessful completion */
    447 	release_fd_lock(ct->ct_fd, mask);
    448 	return (ct->ct_error.re_status);
    449 }
    450 
    451 static void
    452 clnt_vc_geterr(h, errp)
    453 	CLIENT *h;
    454 	struct rpc_err *errp;
    455 {
    456 	struct ct_data *ct;
    457 
    458 	_DIAGASSERT(h != NULL);
    459 	_DIAGASSERT(errp != NULL);
    460 
    461 	ct = (struct ct_data *) h->cl_private;
    462 	*errp = ct->ct_error;
    463 }
    464 
    465 static bool_t
    466 clnt_vc_freeres(cl, xdr_res, res_ptr)
    467 	CLIENT *cl;
    468 	xdrproc_t xdr_res;
    469 	caddr_t res_ptr;
    470 {
    471 	struct ct_data *ct;
    472 	XDR *xdrs;
    473 	bool_t dummy;
    474 #ifdef _REENTRANT
    475 	sigset_t mask;
    476 #endif
    477 	sigset_t newmask;
    478 
    479 	_DIAGASSERT(cl != NULL);
    480 
    481 	ct = (struct ct_data *)cl->cl_private;
    482 	xdrs = &(ct->ct_xdrs);
    483 
    484 	sigfillset(&newmask);
    485 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    486 	mutex_lock(&clnt_fd_lock);
    487 #ifdef _REENTRANT
    488 	while (vc_fd_locks[ct->ct_fd])
    489 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
    490 #endif
    491 
    492 	xdrs->x_op = XDR_FREE;
    493 	dummy = (*xdr_res)(xdrs, res_ptr);
    494 	mutex_unlock(&clnt_fd_lock);
    495 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    496 	cond_signal(&vc_cv[ct->ct_fd]);
    497 
    498 	return dummy;
    499 }
    500 
    501 /*ARGSUSED*/
    502 static void
    503 clnt_vc_abort(cl)
    504 	CLIENT *cl;
    505 {
    506 }
    507 
    508 static bool_t
    509 clnt_vc_control(cl, request, info)
    510 	CLIENT *cl;
    511 	u_int request;
    512 	char *info;
    513 {
    514 	struct ct_data *ct;
    515 	void *infop = info;
    516 #ifdef _REENTRANT
    517 	sigset_t mask;
    518 #endif
    519 	sigset_t newmask;
    520 
    521 	_DIAGASSERT(cl != NULL);
    522 
    523 	ct = (struct ct_data *)cl->cl_private;
    524 
    525 	sigfillset(&newmask);
    526 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    527 	mutex_lock(&clnt_fd_lock);
    528 #ifdef _REENTRANT
    529 	while (vc_fd_locks[ct->ct_fd])
    530 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
    531 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
    532 #endif
    533 	mutex_unlock(&clnt_fd_lock);
    534 
    535 	switch (request) {
    536 	case CLSET_FD_CLOSE:
    537 		ct->ct_closeit = TRUE;
    538 		release_fd_lock(ct->ct_fd, mask);
    539 		return (TRUE);
    540 	case CLSET_FD_NCLOSE:
    541 		ct->ct_closeit = FALSE;
    542 		release_fd_lock(ct->ct_fd, mask);
    543 		return (TRUE);
    544 	default:
    545 		break;
    546 	}
    547 
    548 	/* for other requests which use info */
    549 	if (info == NULL) {
    550 		release_fd_lock(ct->ct_fd, mask);
    551 		return (FALSE);
    552 	}
    553 	switch (request) {
    554 	case CLSET_TIMEOUT:
    555 		if (time_not_ok((struct timeval *)(void *)info)) {
    556 			release_fd_lock(ct->ct_fd, mask);
    557 			return (FALSE);
    558 		}
    559 		ct->ct_wait = *(struct timeval *)infop;
    560 		ct->ct_waitset = TRUE;
    561 		break;
    562 	case CLGET_TIMEOUT:
    563 		*(struct timeval *)infop = ct->ct_wait;
    564 		break;
    565 	case CLGET_SERVER_ADDR:
    566 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
    567 		break;
    568 	case CLGET_FD:
    569 		*(int *)(void *)info = ct->ct_fd;
    570 		break;
    571 	case CLGET_SVC_ADDR:
    572 		/* The caller should not free this memory area */
    573 		*(struct netbuf *)(void *)info = ct->ct_addr;
    574 		break;
    575 	case CLSET_SVC_ADDR:		/* set to new address */
    576 		release_fd_lock(ct->ct_fd, mask);
    577 		return (FALSE);
    578 	case CLGET_XID:
    579 		/*
    580 		 * use the knowledge that xid is the
    581 		 * first element in the call structure
    582 		 * This will get the xid of the PREVIOUS call
    583 		 */
    584 		*(u_int32_t *)(void *)info =
    585 		    ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
    586 		break;
    587 	case CLSET_XID:
    588 		/* This will set the xid of the NEXT call */
    589 		*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
    590 		    htonl(*((u_int32_t *)(void *)info) + 1);
    591 		/* increment by 1 as clnt_vc_call() decrements once */
    592 		break;
    593 	case CLGET_VERS:
    594 		/*
    595 		 * This RELIES on the information that, in the call body,
    596 		 * the version number field is the fifth field from the
    597 		 * begining of the RPC header. MUST be changed if the
    598 		 * call_struct is changed
    599 		 */
    600 		*(u_int32_t *)(void *)info =
    601 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    602 		    4 * BYTES_PER_XDR_UNIT));
    603 		break;
    604 
    605 	case CLSET_VERS:
    606 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    607 		    4 * BYTES_PER_XDR_UNIT) =
    608 		    htonl(*(u_int32_t *)(void *)info);
    609 		break;
    610 
    611 	case CLGET_PROG:
    612 		/*
    613 		 * This RELIES on the information that, in the call body,
    614 		 * the program number field is the fourth field from the
    615 		 * begining of the RPC header. MUST be changed if the
    616 		 * call_struct is changed
    617 		 */
    618 		*(u_int32_t *)(void *)info =
    619 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    620 		    3 * BYTES_PER_XDR_UNIT));
    621 		break;
    622 
    623 	case CLSET_PROG:
    624 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    625 		    3 * BYTES_PER_XDR_UNIT) =
    626 		    htonl(*(u_int32_t *)(void *)info);
    627 		break;
    628 
    629 	default:
    630 		release_fd_lock(ct->ct_fd, mask);
    631 		return (FALSE);
    632 	}
    633 	release_fd_lock(ct->ct_fd, mask);
    634 	return (TRUE);
    635 }
    636 
    637 
    638 static void
    639 clnt_vc_destroy(cl)
    640 	CLIENT *cl;
    641 {
    642 	struct ct_data *ct;
    643 #ifdef _REENTRANT
    644 	int ct_fd;
    645 	sigset_t mask;
    646 #endif
    647 	sigset_t newmask;
    648 
    649 	_DIAGASSERT(cl != NULL);
    650 
    651 	ct = (struct ct_data *) cl->cl_private;
    652 	ct_fd = ct->ct_fd;
    653 
    654 	sigfillset(&newmask);
    655 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    656 	mutex_lock(&clnt_fd_lock);
    657 #ifdef _REENTRANT
    658 	while (vc_fd_locks[ct_fd])
    659 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
    660 #endif
    661 	if (ct->ct_closeit && ct->ct_fd != -1) {
    662 		(void)close(ct->ct_fd);
    663 	}
    664 	XDR_DESTROY(&(ct->ct_xdrs));
    665 	if (ct->ct_addr.buf)
    666 		free(ct->ct_addr.buf);
    667 	mem_free(ct, sizeof(struct ct_data));
    668 	mem_free(cl, sizeof(CLIENT));
    669 	mutex_unlock(&clnt_fd_lock);
    670 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    671 
    672 	cond_signal(&vc_cv[ct_fd]);
    673 }
    674 
    675 /*
    676  * Interface between xdr serializer and tcp connection.
    677  * Behaves like the system calls, read & write, but keeps some error state
    678  * around for the rpc level.
    679  */
    680 static int
    681 read_vc(ctp, buf, len)
    682 	caddr_t ctp;
    683 	caddr_t buf;
    684 	int len;
    685 {
    686 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
    687 	struct pollfd fd;
    688 	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
    689 	    (ct->ct_wait.tv_usec / 1000));
    690 
    691 	if (len == 0)
    692 		return (0);
    693 	fd.fd = ct->ct_fd;
    694 	fd.events = POLLIN;
    695 	for (;;) {
    696 		switch (poll(&fd, 1, milliseconds)) {
    697 		case 0:
    698 			ct->ct_error.re_status = RPC_TIMEDOUT;
    699 			return (-1);
    700 
    701 		case -1:
    702 			if (errno == EINTR)
    703 				continue;
    704 			ct->ct_error.re_status = RPC_CANTRECV;
    705 			ct->ct_error.re_errno = errno;
    706 			return (-1);
    707 		}
    708 		break;
    709 	}
    710 	switch (len = read(ct->ct_fd, buf, (size_t)len)) {
    711 
    712 	case 0:
    713 		/* premature eof */
    714 		ct->ct_error.re_errno = ECONNRESET;
    715 		ct->ct_error.re_status = RPC_CANTRECV;
    716 		len = -1;  /* it's really an error */
    717 		break;
    718 
    719 	case -1:
    720 		ct->ct_error.re_errno = errno;
    721 		ct->ct_error.re_status = RPC_CANTRECV;
    722 		break;
    723 	}
    724 	return (len);
    725 }
    726 
    727 static int
    728 write_vc(ctp, buf, len)
    729 	caddr_t ctp;
    730 	caddr_t buf;
    731 	int len;
    732 {
    733 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
    734 	int i, cnt;
    735 
    736 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
    737 		if ((i = write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
    738 			ct->ct_error.re_errno = errno;
    739 			ct->ct_error.re_status = RPC_CANTSEND;
    740 			return (-1);
    741 		}
    742 	}
    743 	return (len);
    744 }
    745 
    746 static struct clnt_ops *
    747 clnt_vc_ops()
    748 {
    749 	static struct clnt_ops ops;
    750 #ifdef _REENTRANT
    751 	extern mutex_t  ops_lock;
    752 	sigset_t mask;
    753 #endif
    754 	sigset_t newmask;
    755 
    756 	/* VARIABLES PROTECTED BY ops_lock: ops */
    757 
    758 	sigfillset(&newmask);
    759 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    760 	mutex_lock(&ops_lock);
    761 	if (ops.cl_call == NULL) {
    762 		ops.cl_call = clnt_vc_call;
    763 		ops.cl_abort = clnt_vc_abort;
    764 		ops.cl_geterr = clnt_vc_geterr;
    765 		ops.cl_freeres = clnt_vc_freeres;
    766 		ops.cl_destroy = clnt_vc_destroy;
    767 		ops.cl_control = clnt_vc_control;
    768 	}
    769 	mutex_unlock(&ops_lock);
    770 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    771 	return (&ops);
    772 }
    773 
    774 /*
    775  * Make sure that the time is not garbage.   -1 value is disallowed.
    776  * Note this is different from time_not_ok in clnt_dg.c
    777  */
    778 static bool_t
    779 time_not_ok(t)
    780 	struct timeval *t;
    781 {
    782 
    783 	_DIAGASSERT(t != NULL);
    784 
    785 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
    786 		t->tv_usec <= -1 || t->tv_usec > 1000000);
    787 }
    788