Home | History | Annotate | Line # | Download | only in rpc
clnt_vc.c revision 1.4
      1 /*	$NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl 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.4 2000/07/14 08:40:42 fvdl 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_com.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 __REENT
    128 static int      *vc_fd_locks;
    129 extern int __rpc_lock_value;
    130 extern mutex_t  clnt_fd_lock;
    131 static cond_t   *vc_cv;
    132 #define release_fd_lock(fd, mask) {             \
    133 	mutex_lock(&clnt_fd_lock);      \
    134 	vc_fd_locks[fd] = 0;            \
    135 	mutex_unlock(&clnt_fd_lock);    \
    136 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);        \
    137 	cond_signal(&vc_cv[fd]);        \
    138 }
    139 #else
    140 #define release_fd_lock(fd,mask)
    141 #define __rpc_lock_value 0
    142 #endif
    143 
    144 
    145 /*
    146  * Create a client handle for a connection.
    147  * Default options are set, which the user can change using clnt_control()'s.
    148  * The rpc/vc package does buffering similar to stdio, so the client
    149  * must pick send and receive buffer sizes, 0 => use the default.
    150  * NB: fd is copied into a private area.
    151  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
    152  * set this something more useful.
    153  *
    154  * fd should be an open socket
    155  */
    156 CLIENT *
    157 clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
    158 	int fd;
    159 	const struct netbuf *raddr;
    160 	rpcprog_t prog;
    161 	rpcvers_t vers;
    162 	u_int sendsz;
    163 	u_int recvsz;
    164 {
    165 	CLIENT *h;
    166 	struct ct_data *ct = NULL;
    167 	struct timeval now;
    168 	struct rpc_msg call_msg;
    169 	static u_int32_t disrupt;
    170 #ifdef __REENT
    171 	sigset_t mask;
    172 #endif
    173 	sigset_t newmask;
    174 	struct sockaddr_storage ss;
    175 	socklen_t slen;
    176 	struct __rpc_sockinfo si;
    177 
    178 	if (disrupt == 0)
    179 		disrupt = (u_int32_t)(long)raddr;
    180 
    181 	h  = mem_alloc(sizeof(*h));
    182 	if (h == NULL) {
    183 		warnx("clnt_vc_create: out of memory");
    184 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    185 		rpc_createerr.cf_error.re_errno = errno;
    186 		goto fooy;
    187 	}
    188 	ct = mem_alloc(sizeof(*ct));
    189 	if (ct == NULL) {
    190 		warnx("clnt_vc_create: out of memory");
    191 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    192 		rpc_createerr.cf_error.re_errno = errno;
    193 		goto fooy;
    194 	}
    195 
    196 	sigfillset(&newmask);
    197 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    198 #ifdef __REENT
    199 	mutex_lock(&clnt_fd_lock);
    200 	if (vc_fd_locks == (int *) NULL) {
    201 		int cv_allocsz, fd_allocsz;
    202 		int dtbsize = __rpc_dtbsize();
    203 
    204 		fd_allocsz = dtbsize * sizeof (int);
    205 		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
    206 		if (vc_fd_locks == (int *) NULL) {
    207 			mutex_unlock(&clnt_fd_lock);
    208 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    209 			goto fooy;
    210 		} else
    211 			memset(vc_fd_locks, '\0', fd_allocsz);
    212 
    213 		assert(vc_cv == (cond_t *) NULL);
    214 		cv_allocsz = dtbsize * sizeof (cond_t);
    215 		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
    216 		if (vc_cv == (cond_t *) NULL) {
    217 			mem_free(vc_fd_locks, fd_allocsz);
    218 			vc_fd_locks = (int *) NULL;
    219 			mutex_unlock(&clnt_fd_lock);
    220 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    221 			goto fooy;
    222 		} else {
    223 			int i;
    224 
    225 			for (i = 0; i < dtbsize; i++)
    226 				cond_init(&vc_cv[i], 0, (void *) 0);
    227 		}
    228 	} else
    229 		assert(vc_cv != (cond_t *) NULL);
    230 #endif
    231 
    232 	/*
    233 	 * XXX - fvdl connecting while holding a mutex?
    234 	 */
    235 	slen = sizeof ss;
    236 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    237 		if (errno != ENOTCONN) {
    238 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    239 			rpc_createerr.cf_error.re_errno = errno;
    240 			mutex_unlock(&clnt_fd_lock);
    241 			goto fooy;
    242 		}
    243 		if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
    244 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    245 			rpc_createerr.cf_error.re_errno = errno;
    246 			mutex_unlock(&clnt_fd_lock);
    247 			goto fooy;
    248 		}
    249 	}
    250 	mutex_unlock(&clnt_fd_lock);
    251 	if (!__rpc_fd2sockinfo(fd, &si))
    252 		goto fooy;
    253 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    254 
    255 	ct->ct_closeit = FALSE;
    256 
    257 	/*
    258 	 * Set up private data struct
    259 	 */
    260 	ct->ct_fd = fd;
    261 	ct->ct_wait.tv_usec = 0;
    262 	ct->ct_waitset = FALSE;
    263 	ct->ct_addr.buf = malloc(raddr->maxlen);
    264 	if (ct->ct_addr.buf == NULL)
    265 		goto fooy;
    266 	memcpy(ct->ct_addr.buf, &raddr->buf, raddr->len);
    267 	ct->ct_addr.len = raddr->maxlen;
    268 	ct->ct_addr.maxlen = raddr->maxlen;
    269 
    270 	/*
    271 	 * Initialize call message
    272 	 */
    273 	(void)gettimeofday(&now, NULL);
    274 	call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
    275 	call_msg.rm_direction = CALL;
    276 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    277 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
    278 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
    279 
    280 	/*
    281 	 * pre-serialize the static part of the call msg and stash it away
    282 	 */
    283 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
    284 	    XDR_ENCODE);
    285 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
    286 		if (ct->ct_closeit) {
    287 			(void)close(fd);
    288 		}
    289 		goto fooy;
    290 	}
    291 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
    292 	XDR_DESTROY(&(ct->ct_xdrs));
    293 
    294 	/*
    295 	 * Create a client handle which uses xdrrec for serialization
    296 	 * and authnone for authentication.
    297 	 */
    298 	h->cl_ops = clnt_vc_ops();
    299 	h->cl_private = ct;
    300 	h->cl_auth = authnone_create();
    301 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
    302 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
    303 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
    304 	    h->cl_private, read_vc, write_vc);
    305 	return (h);
    306 
    307 fooy:
    308 	/*
    309 	 * Something goofed, free stuff and barf
    310 	 */
    311 	if (ct)
    312 		mem_free(ct, sizeof(struct ct_data));
    313 	if (h)
    314 		mem_free(h, sizeof(CLIENT));
    315 	return (NULL);
    316 }
    317 
    318 static enum clnt_stat
    319 clnt_vc_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
    320 	CLIENT *h;
    321 	rpcproc_t proc;
    322 	xdrproc_t xdr_args;
    323 	caddr_t args_ptr;
    324 	xdrproc_t xdr_results;
    325 	caddr_t results_ptr;
    326 	struct timeval timeout;
    327 {
    328 	struct ct_data *ct;
    329 	XDR *xdrs;
    330 	struct rpc_msg reply_msg;
    331 	u_int32_t x_id;
    332 	u_int32_t *msg_x_id;
    333 	bool_t shipnow;
    334 	int refreshes = 2;
    335 #ifdef __REENT
    336 	sigset_t mask, newmask;
    337 #endif
    338 
    339 	_DIAGASSERT(h != NULL);
    340 
    341 #ifdef __REENT
    342 	sigfillset(&newmask);
    343 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    344 	mutex_lock(&clnt_fd_lock);
    345 	while (vc_fd_locks[ct->ct_fd])
    346 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
    347 	vc_fd_locks[ct->ct_fd] = lock_value;
    348 	mutex_unlock(&clnt_fd_lock);
    349 #endif
    350 
    351 	ct = (struct ct_data *) h->cl_private;
    352 	xdrs = &(ct->ct_xdrs);
    353 	msg_x_id = &ct->ct_u.ct_mcalli;
    354 
    355 	if (!ct->ct_waitset) {
    356 		if (time_not_ok(&timeout) == FALSE)
    357 		ct->ct_wait = timeout;
    358 	}
    359 
    360 	shipnow =
    361 	    (xdr_results == NULL && timeout.tv_sec == 0
    362 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
    363 
    364 call_again:
    365 	xdrs->x_op = XDR_ENCODE;
    366 	ct->ct_error.re_status = RPC_SUCCESS;
    367 	x_id = ntohl(--(*msg_x_id));
    368 	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
    369 	    (! XDR_PUTINT32(xdrs, &proc)) ||
    370 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
    371 	    (! (*xdr_args)(xdrs, args_ptr))) {
    372 		if (ct->ct_error.re_status == RPC_SUCCESS)
    373 			ct->ct_error.re_status = RPC_CANTENCODEARGS;
    374 		(void)xdrrec_endofrecord(xdrs, TRUE);
    375 		release_fd_lock(ct->ct_fd, mask);
    376 		return (ct->ct_error.re_status);
    377 	}
    378 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
    379 		release_fd_lock(ct->ct_fd, mask);
    380 		return (ct->ct_error.re_status = RPC_CANTSEND);
    381 	}
    382 	if (! shipnow) {
    383 		release_fd_lock(ct->ct_fd, mask);
    384 		return (RPC_SUCCESS);
    385 	}
    386 	/*
    387 	 * Hack to provide rpc-based message passing
    388 	 */
    389 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
    390 		release_fd_lock(ct->ct_fd, mask);
    391 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
    392 	}
    393 
    394 
    395 	/*
    396 	 * Keep receiving until we get a valid transaction id
    397 	 */
    398 	xdrs->x_op = XDR_DECODE;
    399 	for (;;) {
    400 		reply_msg.acpted_rply.ar_verf = _null_auth;
    401 		reply_msg.acpted_rply.ar_results.where = NULL;
    402 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
    403 		if (! xdrrec_skiprecord(xdrs)) {
    404 			release_fd_lock(ct->ct_fd, mask);
    405 			return (ct->ct_error.re_status);
    406 		}
    407 		/* now decode and validate the response header */
    408 		if (! xdr_replymsg(xdrs, &reply_msg)) {
    409 			if (ct->ct_error.re_status == RPC_SUCCESS)
    410 				continue;
    411 			release_fd_lock(ct->ct_fd, mask);
    412 			return (ct->ct_error.re_status);
    413 		}
    414 		if (reply_msg.rm_xid == x_id)
    415 			break;
    416 	}
    417 
    418 	/*
    419 	 * process header
    420 	 */
    421 	_seterr_reply(&reply_msg, &(ct->ct_error));
    422 	if (ct->ct_error.re_status == RPC_SUCCESS) {
    423 		if (! AUTH_VALIDATE(h->cl_auth,
    424 		    &reply_msg.acpted_rply.ar_verf)) {
    425 			ct->ct_error.re_status = RPC_AUTHERROR;
    426 			ct->ct_error.re_why = AUTH_INVALIDRESP;
    427 		} else if (! (*xdr_results)(xdrs, results_ptr)) {
    428 			if (ct->ct_error.re_status == RPC_SUCCESS)
    429 				ct->ct_error.re_status = RPC_CANTDECODERES;
    430 		}
    431 		/* free verifier ... */
    432 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
    433 			xdrs->x_op = XDR_FREE;
    434 			(void)xdr_opaque_auth(xdrs,
    435 			    &(reply_msg.acpted_rply.ar_verf));
    436 		}
    437 	}  /* end successful completion */
    438 	else {
    439 		/* maybe our credentials need to be refreshed ... */
    440 		if (refreshes-- && AUTH_REFRESH(h->cl_auth))
    441 			goto call_again;
    442 	}  /* end of unsuccessful completion */
    443 	release_fd_lock(ct->ct_fd, mask);
    444 	return (ct->ct_error.re_status);
    445 }
    446 
    447 static void
    448 clnt_vc_geterr(h, errp)
    449 	CLIENT *h;
    450 	struct rpc_err *errp;
    451 {
    452 	struct ct_data *ct;
    453 
    454 	_DIAGASSERT(h != NULL);
    455 	_DIAGASSERT(errp != NULL);
    456 
    457 	ct = (struct ct_data *) h->cl_private;
    458 	*errp = ct->ct_error;
    459 }
    460 
    461 static bool_t
    462 clnt_vc_freeres(cl, xdr_res, res_ptr)
    463 	CLIENT *cl;
    464 	xdrproc_t xdr_res;
    465 	caddr_t res_ptr;
    466 {
    467 	struct ct_data *ct;
    468 	XDR *xdrs;
    469 	bool_t dummy;
    470 #ifdef __REENT
    471 	sigset_t mask;
    472 #endif
    473 	sigset_t newmask;
    474 
    475 	_DIAGASSERT(cl != NULL);
    476 
    477 	ct = (struct ct_data *)cl->cl_private;
    478 	xdrs = &(ct->ct_xdrs);
    479 
    480 	sigfillset(&newmask);
    481 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    482 	mutex_lock(&clnt_fd_lock);
    483 #ifdef __REENT
    484 	while (vc_fd_locks[ct->ct_fd])
    485 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
    486 #endif
    487 
    488 	xdrs->x_op = XDR_FREE;
    489 	dummy = (*xdr_res)(xdrs, res_ptr);
    490 	mutex_unlock(&clnt_fd_lock);
    491 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    492 	cond_signal(&vc_cv[ct->ct_fd]);
    493 
    494 	return dummy;
    495 }
    496 
    497 /*ARGSUSED*/
    498 static void
    499 clnt_vc_abort(cl)
    500 	CLIENT *cl;
    501 {
    502 }
    503 
    504 static bool_t
    505 clnt_vc_control(cl, request, info)
    506 	CLIENT *cl;
    507 	u_int request;
    508 	char *info;
    509 {
    510 	struct ct_data *ct;
    511 	void *infop = info;
    512 #ifdef _REENT
    513 	sigset_t mask;
    514 #endif
    515 	sigset_t newmask;
    516 
    517 	_DIAGASSERT(cl != NULL);
    518 
    519 	ct = (struct ct_data *)cl->cl_private;
    520 
    521 	sigfillset(&newmask);
    522 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    523 	mutex_lock(&clnt_fd_lock);
    524 #ifdef __REENT
    525 	while (vc_fd_locks[ct->ct_fd])
    526 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
    527 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
    528 #endif
    529 	mutex_unlock(&clnt_fd_lock);
    530 
    531 	switch (request) {
    532 	case CLSET_FD_CLOSE:
    533 		ct->ct_closeit = TRUE;
    534 		release_fd_lock(ct->ct_fd, mask);
    535 		return (TRUE);
    536 	case CLSET_FD_NCLOSE:
    537 		ct->ct_closeit = FALSE;
    538 		release_fd_lock(ct->ct_fd, mask);
    539 		return (TRUE);
    540 	default:
    541 		break;
    542 	}
    543 
    544 	/* for other requests which use info */
    545 	if (info == NULL) {
    546 		release_fd_lock(ct->ct_fd, mask);
    547 		return (FALSE);
    548 	}
    549 	switch (request) {
    550 	case CLSET_TIMEOUT:
    551 		if (time_not_ok((struct timeval *)(void *)info)) {
    552 			release_fd_lock(ct->ct_fd, mask);
    553 			return (FALSE);
    554 		}
    555 		ct->ct_wait = *(struct timeval *)infop;
    556 		ct->ct_waitset = TRUE;
    557 		break;
    558 	case CLGET_TIMEOUT:
    559 		*(struct timeval *)infop = ct->ct_wait;
    560 		break;
    561 	case CLGET_SERVER_ADDR:
    562 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
    563 		break;
    564 	case CLGET_FD:
    565 		*(int *)(void *)info = ct->ct_fd;
    566 		break;
    567 	case CLGET_SVC_ADDR:
    568 		/* The caller should not free this memory area */
    569 		*(struct netbuf *)(void *)info = ct->ct_addr;
    570 		break;
    571 	case CLSET_SVC_ADDR:		/* set to new address */
    572 		release_fd_lock(ct->ct_fd, mask);
    573 		return (FALSE);
    574 	case CLGET_XID:
    575 		/*
    576 		 * use the knowledge that xid is the
    577 		 * first element in the call structure
    578 		 * This will get the xid of the PREVIOUS call
    579 		 */
    580 		*(u_int32_t *)(void *)info =
    581 		    ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
    582 		break;
    583 	case CLSET_XID:
    584 		/* This will set the xid of the NEXT call */
    585 		*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
    586 		    htonl(*((u_int32_t *)(void *)info) + 1);
    587 		/* increment by 1 as clnt_vc_call() decrements once */
    588 		break;
    589 	case CLGET_VERS:
    590 		/*
    591 		 * This RELIES on the information that, in the call body,
    592 		 * the version number field is the fifth field from the
    593 		 * begining of the RPC header. MUST be changed if the
    594 		 * call_struct is changed
    595 		 */
    596 		*(u_int32_t *)(void *)info =
    597 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    598 		    4 * BYTES_PER_XDR_UNIT));
    599 		break;
    600 
    601 	case CLSET_VERS:
    602 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    603 		    4 * BYTES_PER_XDR_UNIT) =
    604 		    htonl(*(u_int32_t *)(void *)info);
    605 		break;
    606 
    607 	case CLGET_PROG:
    608 		/*
    609 		 * This RELIES on the information that, in the call body,
    610 		 * the program number field is the fourth field from the
    611 		 * begining of the RPC header. MUST be changed if the
    612 		 * call_struct is changed
    613 		 */
    614 		*(u_int32_t *)(void *)info =
    615 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    616 		    3 * BYTES_PER_XDR_UNIT));
    617 		break;
    618 
    619 	case CLSET_PROG:
    620 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
    621 		    3 * BYTES_PER_XDR_UNIT) =
    622 		    htonl(*(u_int32_t *)(void *)info);
    623 		break;
    624 
    625 	default:
    626 		release_fd_lock(ct->ct_fd, mask);
    627 		return (FALSE);
    628 	}
    629 	release_fd_lock(ct->ct_fd, mask);
    630 	return (TRUE);
    631 }
    632 
    633 
    634 static void
    635 clnt_vc_destroy(cl)
    636 	CLIENT *cl;
    637 {
    638 	struct ct_data *ct;
    639 #ifdef __REENT
    640 	int ct_fd = ct->ct_fd;
    641 	sigset_t mask;
    642 #endif
    643 	sigset_t newmask;
    644 
    645 	_DIAGASSERT(cl != NULL);
    646 
    647 	ct = (struct ct_data *) cl->cl_private;
    648 
    649 	sigfillset(&newmask);
    650 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    651 	mutex_lock(&clnt_fd_lock);
    652 #ifdef _REENT
    653 	while (vc_fd_locks[ct_fd])
    654 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
    655 #endif
    656 	if (ct->ct_closeit && ct->ct_fd != -1) {
    657 		(void)close(ct->ct_fd);
    658 	}
    659 	XDR_DESTROY(&(ct->ct_xdrs));
    660 	if (ct->ct_addr.buf)
    661 		free(ct->ct_addr.buf);
    662 	mem_free(ct, sizeof(struct ct_data));
    663 	mem_free(cl, sizeof(CLIENT));
    664 	mutex_unlock(&clnt_fd_lock);
    665 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    666 
    667 	cond_signal(&vc_cv[ct_fd]);
    668 }
    669 
    670 /*
    671  * Interface between xdr serializer and tcp connection.
    672  * Behaves like the system calls, read & write, but keeps some error state
    673  * around for the rpc level.
    674  */
    675 static int
    676 read_vc(ctp, buf, len)
    677 	caddr_t ctp;
    678 	caddr_t buf;
    679 	int len;
    680 {
    681 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
    682 	struct pollfd fd;
    683 	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
    684 	    (ct->ct_wait.tv_usec / 1000));
    685 
    686 	if (len == 0)
    687 		return (0);
    688 	fd.fd = ct->ct_fd;
    689 	fd.events = POLLIN;
    690 	for (;;) {
    691 		switch (poll(&fd, 1, milliseconds)) {
    692 		case 0:
    693 			ct->ct_error.re_status = RPC_TIMEDOUT;
    694 			return (-1);
    695 
    696 		case -1:
    697 			if (errno == EINTR)
    698 				continue;
    699 			ct->ct_error.re_status = RPC_CANTRECV;
    700 			ct->ct_error.re_errno = errno;
    701 			return (-1);
    702 		}
    703 		break;
    704 	}
    705 	switch (len = read(ct->ct_fd, buf, (size_t)len)) {
    706 
    707 	case 0:
    708 		/* premature eof */
    709 		ct->ct_error.re_errno = ECONNRESET;
    710 		ct->ct_error.re_status = RPC_CANTRECV;
    711 		len = -1;  /* it's really an error */
    712 		break;
    713 
    714 	case -1:
    715 		ct->ct_error.re_errno = errno;
    716 		ct->ct_error.re_status = RPC_CANTRECV;
    717 		break;
    718 	}
    719 	return (len);
    720 }
    721 
    722 static int
    723 write_vc(ctp, buf, len)
    724 	caddr_t ctp;
    725 	caddr_t buf;
    726 	int len;
    727 {
    728 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
    729 	int i, cnt;
    730 
    731 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
    732 		if ((i = write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
    733 			ct->ct_error.re_errno = errno;
    734 			ct->ct_error.re_status = RPC_CANTSEND;
    735 			return (-1);
    736 		}
    737 	}
    738 	return (len);
    739 }
    740 
    741 static struct clnt_ops *
    742 clnt_vc_ops()
    743 {
    744 	static struct clnt_ops ops;
    745 #ifdef __REENT
    746 	extern mutex_t  ops_lock;
    747 	sigset_t mask;
    748 #endif
    749 	sigset_t newmask;
    750 
    751 	/* VARIABLES PROTECTED BY ops_lock: ops */
    752 
    753 	sigfillset(&newmask);
    754 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    755 	mutex_lock(&ops_lock);
    756 	if (ops.cl_call == NULL) {
    757 		ops.cl_call = clnt_vc_call;
    758 		ops.cl_abort = clnt_vc_abort;
    759 		ops.cl_geterr = clnt_vc_geterr;
    760 		ops.cl_freeres = clnt_vc_freeres;
    761 		ops.cl_destroy = clnt_vc_destroy;
    762 		ops.cl_control = clnt_vc_control;
    763 	}
    764 	mutex_unlock(&ops_lock);
    765 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    766 	return (&ops);
    767 }
    768 
    769 /*
    770  * Make sure that the time is not garbage.   -1 value is disallowed.
    771  * Note this is different from time_not_ok in clnt_dg.c
    772  */
    773 static bool_t
    774 time_not_ok(t)
    775 	struct timeval *t;
    776 {
    777 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
    778 		t->tv_usec <= -1 || t->tv_usec > 1000000);
    779 }
    780