clnt_vc.c revision 92905
165793Smsmith/*	$NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $	*/
265793Smsmith/*	$FreeBSD: head/lib/libc/rpc/clnt_vc.c 92905 2002-03-21 22:49:10Z obrien $ */
381082Sscottl
465793Smsmith/*
581082Sscottl * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
665793Smsmith * unrestricted use provided that this legend is included on all tape
765793Smsmith * media and as a part of the software program in whole or part.  Users
865793Smsmith * may copy or modify Sun RPC without charge, but are not authorized
965793Smsmith * to license or distribute it to anyone else except as part of a product or
1065793Smsmith * program developed by the user.
1165793Smsmith *
1265793Smsmith * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1365793Smsmith * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1465793Smsmith * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1565793Smsmith *
1665793Smsmith * Sun RPC is provided with no support and without any obligation on the
1765793Smsmith * part of Sun Microsystems, Inc. to assist in its use, correction,
1865793Smsmith * modification or enhancement.
1965793Smsmith *
2065793Smsmith * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2165793Smsmith * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2265793Smsmith * OR ANY PART THEREOF.
2365793Smsmith *
2465793Smsmith * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2565793Smsmith * or profits or other special, indirect and consequential damages, even if
2665793Smsmith * Sun has been advised of the possibility of such damages.
2765793Smsmith *
2865793Smsmith * Sun Microsystems, Inc.
2965793Smsmith * 2550 Garcia Avenue
30119418Sobrien * Mountain View, California  94043
31119418Sobrien */
32119418Sobrien
3381151Sscottl#include <sys/cdefs.h>
3481151Sscottl#if defined(LIBC_SCCS) && !defined(lint)
3565793Smsmithstatic char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
3665793Smsmithstatic char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
3765793Smsmithstatic char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
38129879Sphk#endif
3981154Sscottl
4065793Smsmith/*
4165793Smsmith * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
4265793Smsmith *
4365793Smsmith * Copyright (C) 1984, Sun Microsystems, Inc.
4465793Smsmith *
4582527Sscottl * TCP based RPC supports 'batched calls'.
4682527Sscottl * A sequence of calls may be batched-up in a send buffer.  The rpc call
4782527Sscottl * return immediately to the client even though the call was not necessarily
4882527Sscottl * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
4965793Smsmith * the rpc timeout value is zero (see clnt.h, rpc).
5065793Smsmith *
5165793Smsmith * Clients should NOT casually batch calls that in fact return results; that is,
5265793Smsmith * the server side should be aware that a call is batched and not produce any
53138635Sscottl * return message.  Batched calls that produce many result messages can
5465793Smsmith * deadlock (netlock) the client and the server....
5565793Smsmith *
5665793Smsmith * Now go hang yourself.
5765793Smsmith */
5865793Smsmith
5965793Smsmith#include "namespace.h"
6065793Smsmith#include "reentrant.h"
6165793Smsmith#include <sys/types.h>
6265793Smsmith#include <sys/poll.h>
6365793Smsmith#include <sys/syslog.h>
6465793Smsmith#include <sys/socket.h>
6565793Smsmith#include <sys/un.h>
66111525Sscottl#include <sys/uio.h>
67111525Sscottl
68111525Sscottl#include <arpa/inet.h>
69111220Sphk#include <assert.h>
7065793Smsmith#include <err.h>
7189112Smsmith#include <errno.h>
7265793Smsmith#include <netdb.h>
7365793Smsmith#include <stdio.h>
7483114Sscottl#include <stdlib.h>
7583114Sscottl#include <string.h>
7683114Sscottl#include <unistd.h>
7783114Sscottl#include <signal.h>
7865793Smsmith
7965793Smsmith#include <rpc/rpc.h>
8065793Smsmith#include "un-namespace.h"
8183114Sscottl#include "rpc_com.h"
8283114Sscottl
8383114Sscottl#define MCALL_MSG_SIZE 24
8465793Smsmith
8565793Smsmithstruct cmessage {
8695350Sscottl        struct cmsghdr cmsg;
8795350Sscottl        struct cmsgcred cmcred;
8865793Smsmith};
8965793Smsmith
9081154Sscottlstatic enum clnt_stat clnt_vc_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t,
9195350Sscottl    xdrproc_t, caddr_t, struct timeval));
9281154Sscottlstatic void clnt_vc_geterr(CLIENT *, struct rpc_err *);
9381154Sscottlstatic bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, caddr_t);
9481154Sscottlstatic void clnt_vc_abort(CLIENT *);
95121307Ssilbystatic bool_t clnt_vc_control(CLIENT *, u_int, char *);
9682527Sscottlstatic void clnt_vc_destroy(CLIENT *);
9781154Sscottlstatic struct clnt_ops *clnt_vc_ops(void);
9883114Sscottlstatic bool_t time_not_ok(struct timeval *);
9965793Smsmithstatic int read_vc(caddr_t, caddr_t, int);
10065793Smsmithstatic int write_vc(caddr_t, caddr_t, int);
10165793Smsmithstatic int __msgwrite(int, void *, size_t);
10265793Smsmithstatic int __msgread(int, void *, size_t);
10365793Smsmith
10465793Smsmithstruct ct_data {
105111525Sscottl	int		ct_fd;		/* connection's fd */
10665793Smsmith	bool_t		ct_closeit;	/* close it on destroy */
10783114Sscottl	struct timeval	ct_wait;	/* wait interval in milliseconds */
10865793Smsmith	bool_t          ct_waitset;	/* wait set by clnt_control? */
10983114Sscottl	struct netbuf	ct_addr;	/* remote addr */
11083114Sscottl	struct rpc_err	ct_error;
111111525Sscottl	union {
11265793Smsmith		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
113109088Sscottl		u_int32_t ct_mcalli;
114109088Sscottl	} ct_u;
11583114Sscottl	u_int		ct_mpos;	/* pos after marshal */
116109088Sscottl	XDR		ct_xdrs;	/* XDR stream */
11765793Smsmith};
11883114Sscottl
119109088Sscottl/*
120109088Sscottl *      This machinery implements per-fd locks for MT-safety.  It is not
121109088Sscottl *      sufficient to do per-CLIENT handle locks for MT-safety because a
12283114Sscottl *      user may create more than one CLIENT handle with the same fd behind
123109088Sscottl *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
12465793Smsmith *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
12583114Sscottl *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
12683114Sscottl *      CLIENT handle created for that fd.
12765793Smsmith *      The current implementation holds locks across the entire RPC and reply.
12865793Smsmith *      Yes, this is silly, and as soon as this code is proven to work, this
12983114Sscottl *      should be the first thing fixed.  One step at a time.
13065793Smsmith */
13165793Smsmithstatic int      *vc_fd_locks;
13265793Smsmithextern mutex_t  clnt_fd_lock;
133111525Sscottlstatic cond_t   *vc_cv;
13465793Smsmith#define release_fd_lock(fd, mask) {	\
13583114Sscottl	mutex_lock(&clnt_fd_lock);	\
13665793Smsmith	vc_fd_locks[fd] = 0;		\
13783114Sscottl	mutex_unlock(&clnt_fd_lock);	\
13883114Sscottl	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);	\
139111525Sscottl	cond_signal(&vc_cv[fd]);	\
14065793Smsmith}
14183114Sscottl
14283114Sscottlstatic const char clnt_vc_errstr[] = "%s : %s";
14365793Smsmithstatic const char clnt_vc_str[] = "clnt_vc_create";
14483114Sscottlstatic const char clnt_read_vc_str[] = "read_vc";
14583114Sscottlstatic const char __no_mem_str[] = "out of memory";
14665793Smsmith
14765793Smsmith/*
14883114Sscottl * Create a client handle for a connection.
14965793Smsmith * Default options are set, which the user can change using clnt_control()'s.
15065793Smsmith * The rpc/vc package does buffering similar to stdio, so the client
15165793Smsmith * must pick send and receive buffer sizes, 0 => use the default.
15265793Smsmith * NB: fd is copied into a private area.
15365793Smsmith * NB: The rpch->cl_auth is set null authentication. Caller may wish to
15483114Sscottl * set this something more useful.
15565793Smsmith *
15683114Sscottl * fd should be an open socket
15765793Smsmith */
158111525SscottlCLIENT *
15965793Smsmithclnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
16083114Sscottl	int fd;				/* open file descriptor */
16183114Sscottl	const struct netbuf *raddr;	/* servers address */
16283114Sscottl	rpcprog_t prog;			/* program number */
16383114Sscottl	rpcvers_t vers;			/* version number */
16483114Sscottl	u_int sendsz;			/* buffer recv size */
16583114Sscottl	u_int recvsz;			/* buffer send size */
16683114Sscottl{
16782527Sscottl	CLIENT *cl;			/* client handle */
16883114Sscottl	struct ct_data *ct = NULL;	/* client handle */
16983114Sscottl	struct timeval now;
17083114Sscottl	struct rpc_msg call_msg;
17183114Sscottl	static u_int32_t disrupt;
17283114Sscottl	sigset_t mask;
17383114Sscottl	sigset_t newmask;
17465793Smsmith	struct sockaddr_storage ss;
17583114Sscottl	socklen_t slen;
17683114Sscottl	struct __rpc_sockinfo si;
17783114Sscottl
178133540Sscottl	if (disrupt == 0)
17983114Sscottl		disrupt = (u_int32_t)(long)raddr;
180133540Sscottl
181111532Sscottl	cl = (CLIENT *)mem_alloc(sizeof (*cl));
18283114Sscottl	ct = (struct ct_data *)mem_alloc(sizeof (*ct));
18365793Smsmith	if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) {
18465793Smsmith		(void) syslog(LOG_ERR, clnt_vc_errstr,
18583114Sscottl		    clnt_vc_str, __no_mem_str);
18695350Sscottl		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
187116553Sscottl		rpc_createerr.cf_error.re_errno = errno;
188116553Sscottl		goto err;
189116553Sscottl	}
19095350Sscottl	ct->ct_addr.buf = NULL;
19195350Sscottl	sigfillset(&newmask);
19295350Sscottl	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
19395350Sscottl	mutex_lock(&clnt_fd_lock);
19495350Sscottl	if (vc_fd_locks == (int *) NULL) {
19595350Sscottl		int cv_allocsz, fd_allocsz;
19695350Sscottl		int dtbsize = __rpc_dtbsize();
19795350Sscottl
19895350Sscottl		fd_allocsz = dtbsize * sizeof (int);
19995350Sscottl		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
20095350Sscottl		if (vc_fd_locks == (int *) NULL) {
20195350Sscottl			mutex_unlock(&clnt_fd_lock);
20295350Sscottl			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
20395350Sscottl			goto err;
20495350Sscottl		} else
20595350Sscottl			memset(vc_fd_locks, '\0', fd_allocsz);
206116553Sscottl
207116553Sscottl		assert(vc_cv == (cond_t *) NULL);
20895350Sscottl		cv_allocsz = dtbsize * sizeof (cond_t);
20995350Sscottl		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
21095350Sscottl		if (vc_cv == (cond_t *) NULL) {
21195350Sscottl			mem_free(vc_fd_locks, fd_allocsz);
21295350Sscottl			vc_fd_locks = (int *) NULL;
21395350Sscottl			mutex_unlock(&clnt_fd_lock);
21495350Sscottl			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
21595350Sscottl			goto err;
21682527Sscottl		} else {
21782527Sscottl			int i;
21895350Sscottl
21982527Sscottl			for (i = 0; i < dtbsize; i++)
22082527Sscottl				cond_init(&vc_cv[i], 0, (void *) 0);
221111220Sphk		}
22282527Sscottl	} else
22383114Sscottl		assert(vc_cv != (cond_t *) NULL);
22483114Sscottl
22595350Sscottl	/*
22695350Sscottl	 * XXX - fvdl connecting while holding a mutex?
22795350Sscottl	 */
22895350Sscottl	slen = sizeof ss;
22995350Sscottl	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
23095350Sscottl		if (errno != ENOTCONN) {
231111220Sphk			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
23282527Sscottl			rpc_createerr.cf_error.re_errno = errno;
233111220Sphk			mutex_unlock(&clnt_fd_lock);
234111525Sscottl			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
23582527Sscottl			goto err;
23683114Sscottl		}
23795350Sscottl		if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
23882527Sscottl			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
23983114Sscottl			rpc_createerr.cf_error.re_errno = errno;
24082527Sscottl			mutex_unlock(&clnt_fd_lock);
24195350Sscottl			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
24295350Sscottl			goto err;
24395350Sscottl		}
24495350Sscottl	}
24595350Sscottl	mutex_unlock(&clnt_fd_lock);
24695350Sscottl	if (!__rpc_fd2sockinfo(fd, &si))
24795350Sscottl		goto err;
24882527Sscottl	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
249130006Sscottl
250130006Sscottl	ct->ct_closeit = FALSE;
25195350Sscottl
25282527Sscottl	/*
25395350Sscottl	 * Set up private data struct
25495350Sscottl	 */
25595350Sscottl	ct->ct_fd = fd;
25695350Sscottl	ct->ct_wait.tv_usec = 0;
25795350Sscottl	ct->ct_waitset = FALSE;
25895350Sscottl	ct->ct_addr.buf = malloc(raddr->maxlen);
25995350Sscottl	if (ct->ct_addr.buf == NULL)
260116553Sscottl		goto err;
261116553Sscottl	memcpy(ct->ct_addr.buf, raddr->buf, raddr->len);
262116553Sscottl	ct->ct_addr.len = raddr->maxlen;
263116553Sscottl	ct->ct_addr.maxlen = raddr->maxlen;
264116553Sscottl
265116553Sscottl	/*
266116553Sscottl	 * Initialize call message
267116553Sscottl	 */
268116553Sscottl	(void)gettimeofday(&now, NULL);
269116553Sscottl	call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
270116553Sscottl	call_msg.rm_direction = CALL;
27195350Sscottl	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
27295350Sscottl	call_msg.rm_call.cb_prog = (u_int32_t)prog;
27382527Sscottl	call_msg.rm_call.cb_vers = (u_int32_t)vers;
27495350Sscottl
27595350Sscottl	/*
27683114Sscottl	 * pre-serialize the static part of the call msg and stash it away
27795350Sscottl	 */
278119146Sscottl	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
279119171Smux	    XDR_ENCODE);
28095350Sscottl	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
28183114Sscottl		if (ct->ct_closeit) {
282116553Sscottl			(void)_close(fd);
28395350Sscottl		}
28495350Sscottl		goto err;
285132771Skan	}
28683114Sscottl	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
28782527Sscottl	XDR_DESTROY(&(ct->ct_xdrs));
28883114Sscottl
28982527Sscottl	/*
29082527Sscottl	 * Create a client handle which uses xdrrec for serialization
29183114Sscottl	 * and authnone for authentication.
29265793Smsmith	 */
29365793Smsmith	cl->cl_ops = clnt_vc_ops();
29465793Smsmith	cl->cl_private = ct;
29570393Smsmith	cl->cl_auth = authnone_create();
29665793Smsmith	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
29783114Sscottl	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
29865793Smsmith	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
29983114Sscottl	    cl->cl_private, read_vc, write_vc);
30065793Smsmith	return (cl);
301111525Sscottl
30283114Sscottlerr:
303111691Sscottl	if (cl) {
304103675Sphk		if (ct) {
305111691Sscottl			if (ct->ct_addr.len)
30683114Sscottl				mem_free(ct->ct_addr.buf, ct->ct_addr.len);
30765793Smsmith			mem_free((caddr_t)ct, sizeof (struct ct_data));
30865793Smsmith		}
30983114Sscottl		if (cl)
31065793Smsmith			mem_free((caddr_t)cl, sizeof (CLIENT));
31165793Smsmith	}
31265793Smsmith	return ((CLIENT *)NULL);
31365793Smsmith}
31465793Smsmith
31565793Smsmithstatic enum clnt_stat
31683114Sscottlclnt_vc_call(cl, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
31765793Smsmith	CLIENT *cl;
31883114Sscottl	rpcproc_t proc;
31965793Smsmith	xdrproc_t xdr_args;
32065793Smsmith	caddr_t args_ptr;
32183114Sscottl	xdrproc_t xdr_results;
32265793Smsmith	caddr_t results_ptr;
32365793Smsmith	struct timeval timeout;
32465793Smsmith{
32565793Smsmith	struct ct_data *ct = (struct ct_data *) cl->cl_private;
32665793Smsmith	XDR *xdrs = &(ct->ct_xdrs);
32783114Sscottl	struct rpc_msg reply_msg;
32883114Sscottl	u_int32_t x_id;
32983114Sscottl	u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli;    /* yuk */
33065793Smsmith	bool_t shipnow;
33183114Sscottl	int refreshes = 2;
33265793Smsmith	sigset_t mask, newmask;
33383114Sscottl	int rpc_lock_value;
33483114Sscottl
33583114Sscottl	assert(cl != NULL);
33683114Sscottl
33783114Sscottl	sigfillset(&newmask);
33865793Smsmith	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
33983114Sscottl	mutex_lock(&clnt_fd_lock);
34083114Sscottl	while (vc_fd_locks[ct->ct_fd])
34183114Sscottl		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
34283114Sscottl	if (__isthreaded)
34383114Sscottl                rpc_lock_value = 1;
34483114Sscottl        else
34583114Sscottl                rpc_lock_value = 0;
34683114Sscottl	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
34783114Sscottl	mutex_unlock(&clnt_fd_lock);
34883114Sscottl	if (!ct->ct_waitset) {
34983114Sscottl		/* If time is not within limits, we ignore it. */
35083114Sscottl		if (time_not_ok(&timeout) == FALSE)
35183114Sscottl			ct->ct_wait = timeout;
35283114Sscottl	}
35383114Sscottl
35483114Sscottl	shipnow =
35565793Smsmith	    (xdr_results == NULL && timeout.tv_sec == 0
35683114Sscottl	    && timeout.tv_usec == 0) ? FALSE : TRUE;
35783114Sscottl
35883114Sscottlcall_again:
35965793Smsmith	xdrs->x_op = XDR_ENCODE;
36083114Sscottl	ct->ct_error.re_status = RPC_SUCCESS;
36183114Sscottl	x_id = ntohl(--(*msg_x_id));
362125975Sphk
363125975Sphk	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
364125975Sphk	    (! XDR_PUTINT32(xdrs, &proc)) ||
365125975Sphk	    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
366125975Sphk	    (! (*xdr_args)(xdrs, args_ptr))) {
367125975Sphk		if (ct->ct_error.re_status == RPC_SUCCESS)
368125975Sphk			ct->ct_error.re_status = RPC_CANTENCODEARGS;
369125975Sphk		(void)xdrrec_endofrecord(xdrs, TRUE);
370125975Sphk		release_fd_lock(ct->ct_fd, mask);
371125975Sphk		return (ct->ct_error.re_status);
372125975Sphk	}
373125975Sphk	if (! xdrrec_endofrecord(xdrs, shipnow)) {
374125975Sphk		release_fd_lock(ct->ct_fd, mask);
375125975Sphk		return (ct->ct_error.re_status = RPC_CANTSEND);
37681082Sscottl	}
37783114Sscottl	if (! shipnow) {
37865793Smsmith		release_fd_lock(ct->ct_fd, mask);
37965793Smsmith		return (RPC_SUCCESS);
38083114Sscottl	}
38165793Smsmith	/*
38265793Smsmith	 * Hack to provide rpc-based message passing
38365793Smsmith	 */
38465793Smsmith	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
38565793Smsmith		release_fd_lock(ct->ct_fd, mask);
38683114Sscottl		return(ct->ct_error.re_status = RPC_TIMEDOUT);
38765793Smsmith	}
38883114Sscottl
38965793Smsmith
39083114Sscottl	/*
39165793Smsmith	 * Keep receiving until we get a valid transaction id
39283114Sscottl	 */
39383114Sscottl	xdrs->x_op = XDR_DECODE;
39483114Sscottl	while (TRUE) {
395125975Sphk		reply_msg.acpted_rply.ar_verf = _null_auth;
39665793Smsmith		reply_msg.acpted_rply.ar_results.where = NULL;
39783114Sscottl		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
39865793Smsmith		if (! xdrrec_skiprecord(xdrs)) {
399			release_fd_lock(ct->ct_fd, mask);
400			return (ct->ct_error.re_status);
401		}
402		/* now decode and validate the response header */
403		if (! xdr_replymsg(xdrs, &reply_msg)) {
404			if (ct->ct_error.re_status == RPC_SUCCESS)
405				continue;
406			release_fd_lock(ct->ct_fd, mask);
407			return (ct->ct_error.re_status);
408		}
409		if (reply_msg.rm_xid == x_id)
410			break;
411	}
412
413	/*
414	 * process header
415	 */
416	_seterr_reply(&reply_msg, &(ct->ct_error));
417	if (ct->ct_error.re_status == RPC_SUCCESS) {
418		if (! AUTH_VALIDATE(cl->cl_auth,
419		    &reply_msg.acpted_rply.ar_verf)) {
420			ct->ct_error.re_status = RPC_AUTHERROR;
421			ct->ct_error.re_why = AUTH_INVALIDRESP;
422		} else if (! (*xdr_results)(xdrs, results_ptr)) {
423			if (ct->ct_error.re_status == RPC_SUCCESS)
424				ct->ct_error.re_status = RPC_CANTDECODERES;
425		}
426		/* free verifier ... */
427		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
428			xdrs->x_op = XDR_FREE;
429			(void)xdr_opaque_auth(xdrs,
430			    &(reply_msg.acpted_rply.ar_verf));
431		}
432	}  /* end successful completion */
433	else {
434		/* maybe our credentials need to be refreshed ... */
435		if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg))
436			goto call_again;
437	}  /* end of unsuccessful completion */
438	release_fd_lock(ct->ct_fd, mask);
439	return (ct->ct_error.re_status);
440}
441
442static void
443clnt_vc_geterr(cl, errp)
444	CLIENT *cl;
445	struct rpc_err *errp;
446{
447	struct ct_data *ct;
448
449	assert(cl != NULL);
450	assert(errp != NULL);
451
452	ct = (struct ct_data *) cl->cl_private;
453	*errp = ct->ct_error;
454}
455
456static bool_t
457clnt_vc_freeres(cl, xdr_res, res_ptr)
458	CLIENT *cl;
459	xdrproc_t xdr_res;
460	caddr_t res_ptr;
461{
462	struct ct_data *ct;
463	XDR *xdrs;
464	bool_t dummy;
465	sigset_t mask;
466	sigset_t newmask;
467
468	assert(cl != NULL);
469
470	ct = (struct ct_data *)cl->cl_private;
471	xdrs = &(ct->ct_xdrs);
472
473	sigfillset(&newmask);
474	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
475	mutex_lock(&clnt_fd_lock);
476	while (vc_fd_locks[ct->ct_fd])
477		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
478	xdrs->x_op = XDR_FREE;
479	dummy = (*xdr_res)(xdrs, res_ptr);
480	mutex_unlock(&clnt_fd_lock);
481	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
482	cond_signal(&vc_cv[ct->ct_fd]);
483
484	return dummy;
485}
486
487/*ARGSUSED*/
488static void
489clnt_vc_abort(cl)
490	CLIENT *cl;
491{
492}
493
494static bool_t
495clnt_vc_control(cl, request, info)
496	CLIENT *cl;
497	u_int request;
498	char *info;
499{
500	struct ct_data *ct;
501	void *infop = info;
502	sigset_t mask;
503	sigset_t newmask;
504	int rpc_lock_value;
505
506	assert(cl != NULL);
507
508	ct = (struct ct_data *)cl->cl_private;
509
510	sigfillset(&newmask);
511	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
512	mutex_lock(&clnt_fd_lock);
513	while (vc_fd_locks[ct->ct_fd])
514		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
515	if (__isthreaded)
516                rpc_lock_value = 1;
517        else
518                rpc_lock_value = 0;
519	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
520	mutex_unlock(&clnt_fd_lock);
521
522	switch (request) {
523	case CLSET_FD_CLOSE:
524		ct->ct_closeit = TRUE;
525		release_fd_lock(ct->ct_fd, mask);
526		return (TRUE);
527	case CLSET_FD_NCLOSE:
528		ct->ct_closeit = FALSE;
529		release_fd_lock(ct->ct_fd, mask);
530		return (TRUE);
531	default:
532		break;
533	}
534
535	/* for other requests which use info */
536	if (info == NULL) {
537		release_fd_lock(ct->ct_fd, mask);
538		return (FALSE);
539	}
540	switch (request) {
541	case CLSET_TIMEOUT:
542		if (time_not_ok((struct timeval *)(void *)info)) {
543			release_fd_lock(ct->ct_fd, mask);
544			return (FALSE);
545		}
546		ct->ct_wait = *(struct timeval *)infop;
547		ct->ct_waitset = TRUE;
548		break;
549	case CLGET_TIMEOUT:
550		*(struct timeval *)infop = ct->ct_wait;
551		break;
552	case CLGET_SERVER_ADDR:
553		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
554		break;
555	case CLGET_FD:
556		*(int *)(void *)info = ct->ct_fd;
557		break;
558	case CLGET_SVC_ADDR:
559		/* The caller should not free this memory area */
560		*(struct netbuf *)(void *)info = ct->ct_addr;
561		break;
562	case CLSET_SVC_ADDR:		/* set to new address */
563		release_fd_lock(ct->ct_fd, mask);
564		return (FALSE);
565	case CLGET_XID:
566		/*
567		 * use the knowledge that xid is the
568		 * first element in the call structure
569		 * This will get the xid of the PREVIOUS call
570		 */
571		*(u_int32_t *)(void *)info =
572		    ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
573		break;
574	case CLSET_XID:
575		/* This will set the xid of the NEXT call */
576		*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
577		    htonl(*((u_int32_t *)(void *)info) + 1);
578		/* increment by 1 as clnt_vc_call() decrements once */
579		break;
580	case CLGET_VERS:
581		/*
582		 * This RELIES on the information that, in the call body,
583		 * the version number field is the fifth field from the
584		 * begining of the RPC header. MUST be changed if the
585		 * call_struct is changed
586		 */
587		*(u_int32_t *)(void *)info =
588		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
589		    4 * BYTES_PER_XDR_UNIT));
590		break;
591
592	case CLSET_VERS:
593		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
594		    4 * BYTES_PER_XDR_UNIT) =
595		    htonl(*(u_int32_t *)(void *)info);
596		break;
597
598	case CLGET_PROG:
599		/*
600		 * This RELIES on the information that, in the call body,
601		 * the program number field is the fourth field from the
602		 * begining of the RPC header. MUST be changed if the
603		 * call_struct is changed
604		 */
605		*(u_int32_t *)(void *)info =
606		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
607		    3 * BYTES_PER_XDR_UNIT));
608		break;
609
610	case CLSET_PROG:
611		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
612		    3 * BYTES_PER_XDR_UNIT) =
613		    htonl(*(u_int32_t *)(void *)info);
614		break;
615
616	default:
617		release_fd_lock(ct->ct_fd, mask);
618		return (FALSE);
619	}
620	release_fd_lock(ct->ct_fd, mask);
621	return (TRUE);
622}
623
624
625static void
626clnt_vc_destroy(cl)
627	CLIENT *cl;
628{
629	struct ct_data *ct = (struct ct_data *) cl->cl_private;
630	int ct_fd = ct->ct_fd;
631	sigset_t mask;
632	sigset_t newmask;
633
634	assert(cl != NULL);
635
636	ct = (struct ct_data *) cl->cl_private;
637
638	sigfillset(&newmask);
639	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
640	mutex_lock(&clnt_fd_lock);
641	while (vc_fd_locks[ct_fd])
642		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
643	if (ct->ct_closeit && ct->ct_fd != -1) {
644		(void)_close(ct->ct_fd);
645	}
646	XDR_DESTROY(&(ct->ct_xdrs));
647	if (ct->ct_addr.buf)
648		free(ct->ct_addr.buf);
649	mem_free(ct, sizeof(struct ct_data));
650	mem_free(cl, sizeof(CLIENT));
651	mutex_unlock(&clnt_fd_lock);
652	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
653	cond_signal(&vc_cv[ct_fd]);
654}
655
656/*
657 * Interface between xdr serializer and tcp connection.
658 * Behaves like the system calls, read & write, but keeps some error state
659 * around for the rpc level.
660 */
661static int
662read_vc(ctp, buf, len)
663	caddr_t ctp;
664	caddr_t buf;
665	int len;
666{
667	struct sockaddr sa;
668	socklen_t sal;
669	struct ct_data *ct = (struct ct_data *)(void *)ctp;
670	struct pollfd fd;
671	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
672	    (ct->ct_wait.tv_usec / 1000));
673
674	if (len == 0)
675		return (0);
676	fd.fd = ct->ct_fd;
677	fd.events = POLLIN;
678	for (;;) {
679		switch (_poll(&fd, 1, milliseconds)) {
680		case 0:
681			ct->ct_error.re_status = RPC_TIMEDOUT;
682			return (-1);
683
684		case -1:
685			if (errno == EINTR)
686				continue;
687			ct->ct_error.re_status = RPC_CANTRECV;
688			ct->ct_error.re_errno = errno;
689			return (-1);
690		}
691		break;
692	}
693
694	sal = sizeof(sa);
695	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
696	    (sa.sa_family == AF_LOCAL)) {
697		len = __msgread(ct->ct_fd, buf, (size_t)len);
698	} else {
699		len = _read(ct->ct_fd, buf, (size_t)len);
700	}
701
702	switch (len) {
703	case 0:
704		/* premature eof */
705		ct->ct_error.re_errno = ECONNRESET;
706		ct->ct_error.re_status = RPC_CANTRECV;
707		len = -1;  /* it's really an error */
708		break;
709
710	case -1:
711		ct->ct_error.re_errno = errno;
712		ct->ct_error.re_status = RPC_CANTRECV;
713		break;
714	}
715	return (len);
716}
717
718static int
719write_vc(ctp, buf, len)
720	caddr_t ctp;
721	caddr_t buf;
722	int len;
723{
724	struct sockaddr sa;
725	socklen_t sal;
726	struct ct_data *ct = (struct ct_data *)(void *)ctp;
727	int i, cnt;
728
729	sal = sizeof(sa);
730	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
731	    (sa.sa_family == AF_LOCAL)) {
732		for (cnt = len; cnt > 0; cnt -= i, buf += i) {
733			if ((i = __msgwrite(ct->ct_fd, buf,
734			     (size_t)cnt)) == -1) {
735				ct->ct_error.re_errno = errno;
736				ct->ct_error.re_status = RPC_CANTSEND;
737				return (-1);
738			}
739		}
740	} else {
741		for (cnt = len; cnt > 0; cnt -= i, buf += i) {
742			if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
743				ct->ct_error.re_errno = errno;
744				ct->ct_error.re_status = RPC_CANTSEND;
745				return (-1);
746			}
747		}
748	}
749	return (len);
750}
751
752static struct clnt_ops *
753clnt_vc_ops()
754{
755	static struct clnt_ops ops;
756	extern mutex_t  ops_lock;
757	sigset_t mask, newmask;
758
759	/* VARIABLES PROTECTED BY ops_lock: ops */
760
761	sigfillset(&newmask);
762	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
763	mutex_lock(&ops_lock);
764	if (ops.cl_call == NULL) {
765		ops.cl_call = clnt_vc_call;
766		ops.cl_abort = clnt_vc_abort;
767		ops.cl_geterr = clnt_vc_geterr;
768		ops.cl_freeres = clnt_vc_freeres;
769		ops.cl_destroy = clnt_vc_destroy;
770		ops.cl_control = clnt_vc_control;
771	}
772	mutex_unlock(&ops_lock);
773	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
774	return (&ops);
775}
776
777/*
778 * Make sure that the time is not garbage.   -1 value is disallowed.
779 * Note this is different from time_not_ok in clnt_dg.c
780 */
781static bool_t
782time_not_ok(t)
783	struct timeval *t;
784{
785	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
786		t->tv_usec <= -1 || t->tv_usec > 1000000);
787}
788
789static int
790__msgread(sock, buf, cnt)
791	int sock;
792	void *buf;
793	size_t cnt;
794{
795	struct iovec iov[1];
796	struct msghdr msg;
797	union {
798		struct cmsghdr cmsg;
799		char control[CMSG_SPACE(sizeof(struct cmsgcred))];
800	} cm;
801
802	bzero((char *)&cm, sizeof(cm));
803	iov[0].iov_base = buf;
804	iov[0].iov_len = cnt;
805
806	msg.msg_iov = iov;
807	msg.msg_iovlen = 1;
808	msg.msg_name = NULL;
809	msg.msg_namelen = 0;
810	msg.msg_control = (caddr_t)&cm;
811	msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
812	msg.msg_flags = 0;
813
814	return(_recvmsg(sock, &msg, 0));
815}
816
817static int
818__msgwrite(sock, buf, cnt)
819	int sock;
820	void *buf;
821	size_t cnt;
822{
823	struct iovec iov[1];
824	struct msghdr msg;
825	union {
826		struct cmsghdr cmsg;
827		char control[CMSG_SPACE(sizeof(struct cmsgcred))];
828	} cm;
829
830	bzero((char *)&cm, sizeof(cm));
831	iov[0].iov_base = buf;
832	iov[0].iov_len = cnt;
833
834	cm.cmsg.cmsg_type = SCM_CREDS;
835	cm.cmsg.cmsg_level = SOL_SOCKET;
836	cm.cmsg.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
837
838	msg.msg_iov = iov;
839	msg.msg_iovlen = 1;
840	msg.msg_name = NULL;
841	msg.msg_namelen = 0;
842	msg.msg_control = (caddr_t)&cm;
843	msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
844	msg.msg_flags = 0;
845
846	return(_sendmsg(sock, &msg, 0));
847}
848