clnt_vc.c revision 75094
138465Smsmith/*	$NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $	*/
238465Smsmith/*	$FreeBSD: head/lib/libc/rpc/clnt_vc.c 75094 2001-04-02 21:41:44Z iedowse $ */
338465Smsmith
438465Smsmith/*
538465Smsmith * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
638465Smsmith * unrestricted use provided that this legend is included on all tape
738465Smsmith * media and as a part of the software program in whole or part.  Users
838465Smsmith * may copy or modify Sun RPC without charge, but are not authorized
938465Smsmith * to license or distribute it to anyone else except as part of a product or
1038465Smsmith * program developed by the user.
1138465Smsmith *
1238465Smsmith * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1338465Smsmith * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1438465Smsmith * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1538465Smsmith *
1638465Smsmith * Sun RPC is provided with no support and without any obligation on the
1738465Smsmith * part of Sun Microsystems, Inc. to assist in its use, correction,
1838465Smsmith * modification or enhancement.
1938465Smsmith *
2038465Smsmith * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2138465Smsmith * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2238465Smsmith * OR ANY PART THEREOF.
2338465Smsmith *
2438465Smsmith * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2538465Smsmith * or profits or other special, indirect and consequential damages, even if
2638465Smsmith * Sun has been advised of the possibility of such damages.
27119483Sobrien *
28119483Sobrien * Sun Microsystems, Inc.
29119483Sobrien * 2550 Garcia Avenue
3038465Smsmith * Mountain View, California  94043
3138465Smsmith */
3238465Smsmith
3338465Smsmith#include <sys/cdefs.h>
3438465Smsmith#if defined(LIBC_SCCS) && !defined(lint)
35335755Skevansstatic char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
36335755Skevansstatic char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
3738465Smsmithstatic char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
3838465Smsmith#endif
3938465Smsmith
4038465Smsmith/*
41334572Sdim * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
4238465Smsmith *
4365613Sdcs * Copyright (C) 1984, Sun Microsystems, Inc.
4438465Smsmith *
4538465Smsmith * TCP based RPC supports 'batched calls'.
4668851Smsmith * A sequence of calls may be batched-up in a send buffer.  The rpc call
4738465Smsmith * return immediately to the client even though the call was not necessarily
4840015Smsmith * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
4940015Smsmith * the rpc timeout value is zero (see clnt.h, rpc).
5038465Smsmith *
5138465Smsmith * Clients should NOT casually batch calls that in fact return results; that is,
5238465Smsmith * the server side should be aware that a call is batched and not produce any
5338465Smsmith * return message.  Batched calls that produce many result messages can
5438465Smsmith * deadlock (netlock) the client and the server....
5538465Smsmith *
5638465Smsmith * Now go hang yourself.
5738465Smsmith */
58329183Skevans
59146365Ssobomax#include "namespace.h"
60329183Skevans#include "reentrant.h"
61329183Skevans#include <sys/types.h>
62329183Skevans#include <sys/poll.h>
63329183Skevans#include <sys/syslog.h>
64146365Ssobomax#include <sys/socket.h>
65329183Skevans#include <sys/un.h>
66329183Skevans#include <sys/uio.h>
67329183Skevans
68329183Skevans#include <assert.h>
69329183Skevans#include <err.h>
70329183Skevans#include <errno.h>
71329183Skevans#include <netdb.h>
72329183Skevans#include <stdio.h>
73329183Skevans#include <stdlib.h>
74329183Skevans#include <string.h>
75329183Skevans#include <unistd.h>
76329183Skevans#include <signal.h>
7738465Smsmith
78146365Ssobomax#include <rpc/rpc.h>
79329183Skevans#include "un-namespace.h"
80329183Skevans#include "rpc_com.h"
81329183Skevans
82329183Skevans#define MCALL_MSG_SIZE 24
83329183Skevans
84329183Skevansstruct cmessage {
85329183Skevans        struct cmsghdr cmsg;
8638465Smsmith        struct cmsgcred cmcred;
87329183Skevans};
88329183Skevans
89329183Skevansstatic enum clnt_stat clnt_vc_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t,
90329183Skevans    xdrproc_t, caddr_t, struct timeval));
91329183Skevansstatic void clnt_vc_geterr __P((CLIENT *, struct rpc_err *));
92329183Skevansstatic bool_t clnt_vc_freeres __P((CLIENT *, xdrproc_t, caddr_t));
93329183Skevansstatic void clnt_vc_abort __P((CLIENT *));
9438465Smsmithstatic bool_t clnt_vc_control __P((CLIENT *, u_int, char *));
95329183Skevansstatic void clnt_vc_destroy __P((CLIENT *));
96329183Skevansstatic struct clnt_ops *clnt_vc_ops __P((void));
97329183Skevansstatic bool_t time_not_ok __P((struct timeval *));
98329183Skevansstatic int read_vc __P((caddr_t, caddr_t, int));
99329183Skevansstatic int write_vc __P((caddr_t, caddr_t, int));
100329183Skevansstatic int __msgwrite(int, void *, size_t);
101329183Skevansstatic int __msgread(int, void *, size_t);
102329183Skevans
103329183Skevansstruct ct_data {
10438465Smsmith	int		ct_fd;		/* connection's fd */
105329183Skevans	bool_t		ct_closeit;	/* close it on destroy */
106329183Skevans	struct timeval	ct_wait;	/* wait interval in milliseconds */
107329183Skevans	bool_t          ct_waitset;	/* wait set by clnt_control? */
10838465Smsmith	struct netbuf	ct_addr;	/* remote addr */
109329183Skevans	struct rpc_err	ct_error;
110329183Skevans	union {
11138465Smsmith		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
11238465Smsmith		u_int32_t ct_mcalli;
11338465Smsmith	} ct_u;
11438465Smsmith	u_int		ct_mpos;	/* pos after marshal */
11538465Smsmith	XDR		ct_xdrs;	/* XDR stream */
11638465Smsmith};
11738465Smsmith
11838465Smsmith/*
11938465Smsmith *      This machinery implements per-fd locks for MT-safety.  It is not
12038465Smsmith *      sufficient to do per-CLIENT handle locks for MT-safety because a
12138465Smsmith *      user may create more than one CLIENT handle with the same fd behind
12238465Smsmith *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
12338465Smsmith *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
124329183Skevans *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
125329183Skevans *      CLIENT handle created for that fd.
12638465Smsmith *      The current implementation holds locks across the entire RPC and reply.
127329183Skevans *      Yes, this is silly, and as soon as this code is proven to work, this
128329183Skevans *      should be the first thing fixed.  One step at a time.
129329183Skevans */
130329183Skevansstatic int      *vc_fd_locks;
131329183Skevansextern mutex_t  clnt_fd_lock;
132329183Skevansstatic cond_t   *vc_cv;
133329183Skevans#define release_fd_lock(fd, mask) {	\
134329183Skevans	mutex_lock(&clnt_fd_lock);	\
135329183Skevans	if (__isthreaded)		\
136329183Skevans		vc_fd_locks[fd] = 0;	\
137329183Skevans	mutex_unlock(&clnt_fd_lock);	\
138329183Skevans	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);	\
139329183Skevans	cond_signal(&vc_cv[fd]);	\
140329183Skevans}
141329183Skevans
142329183Skevansstatic const char clnt_vc_errstr[] = "%s : %s";
14338465Smsmithstatic const char clnt_vc_str[] = "clnt_vc_create";
144146365Ssobomaxstatic const char clnt_read_vc_str[] = "read_vc";
145329183Skevansstatic const char __no_mem_str[] = "out of memory";
146329183Skevans
14738465Smsmith/*
14838465Smsmith * Create a client handle for a connection.
14940015Smsmith * Default options are set, which the user can change using clnt_control()'s.
15040015Smsmith * The rpc/vc package does buffering similar to stdio, so the client
15140015Smsmith * must pick send and receive buffer sizes, 0 => use the default.
15240015Smsmith * NB: fd is copied into a private area.
15340015Smsmith * NB: The rpch->cl_auth is set null authentication. Caller may wish to
15440015Smsmith * set this something more useful.
15540015Smsmith *
156329183Skevans * fd should be an open socket
157146365Ssobomax */
158329183SkevansCLIENT *
159329183Skevansclnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
160329183Skevans	int fd;				/* open file descriptor */
16140015Smsmith	const struct netbuf *raddr;	/* servers address */
16240015Smsmith	rpcprog_t prog;			/* program number */
163334572Sdim	rpcvers_t vers;			/* version number */
16464187Sjhb	u_int sendsz;			/* buffer recv size */
16538465Smsmith	u_int recvsz;			/* buffer send size */
166329183Skevans{
167329183Skevans	CLIENT *cl;			/* client handle */
168329183Skevans	struct ct_data *ct = NULL;	/* client handle */
169329183Skevans	struct timeval now;
170199210Sattilio	struct rpc_msg call_msg;
171329183Skevans	static u_int32_t disrupt;
172199210Sattilio	sigset_t mask;
17338465Smsmith	sigset_t newmask;
174329183Skevans	struct sockaddr_storage ss;
17538465Smsmith	socklen_t slen;
176329183Skevans	struct __rpc_sockinfo si;
177329183Skevans
178329183Skevans	if (disrupt == 0)
179329183Skevans		disrupt = (u_int32_t)(long)raddr;
180329183Skevans
181329183Skevans	cl = (CLIENT *)mem_alloc(sizeof (*cl));
182329183Skevans	ct = (struct ct_data *)mem_alloc(sizeof (*ct));
183329183Skevans	if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) {
18440015Smsmith		(void) syslog(LOG_ERR, clnt_vc_errstr,
18540015Smsmith		    clnt_vc_str, __no_mem_str);
18665613Sdcs		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
18765613Sdcs		rpc_createerr.cf_error.re_errno = errno;
188329183Skevans		goto err;
189329183Skevans	}
190329183Skevans	ct->ct_addr.buf = NULL;
191329183Skevans	sigfillset(&newmask);
192329183Skevans	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
193329183Skevans	mutex_lock(&clnt_fd_lock);
194329183Skevans	if (vc_fd_locks == (int *) NULL) {
19565613Sdcs		int cv_allocsz, fd_allocsz;
19665613Sdcs		int dtbsize = __rpc_dtbsize();
197329183Skevans
198329183Skevans		fd_allocsz = dtbsize * sizeof (int);
199329183Skevans		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
20065881Sdcs		if (vc_fd_locks == (int *) NULL) {
201329183Skevans			mutex_unlock(&clnt_fd_lock);
20238465Smsmith			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
203199210Sattilio			goto err;
204329183Skevans		} else
205329183Skevans			memset(vc_fd_locks, '\0', fd_allocsz);
206199210Sattilio
207329183Skevans		assert(vc_cv == (cond_t *) NULL);
208199210Sattilio		cv_allocsz = dtbsize * sizeof (cond_t);
209146365Ssobomax		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
210329183Skevans		if (vc_cv == (cond_t *) NULL) {
211329183Skevans			mem_free(vc_fd_locks, fd_allocsz);
212329183Skevans			vc_fd_locks = (int *) NULL;
213199210Sattilio			mutex_unlock(&clnt_fd_lock);
214329183Skevans			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
215329183Skevans			goto err;
216329183Skevans		} else {
217329183Skevans			int i;
218329183Skevans
219329183Skevans			for (i = 0; i < dtbsize; i++)
220329183Skevans				cond_init(&vc_cv[i], 0, (void *) 0);
221199210Sattilio		}
222329183Skevans	} else
223329183Skevans		assert(vc_cv != (cond_t *) NULL);
224329183Skevans
225199210Sattilio	/*
226329183Skevans	 * XXX - fvdl connecting while holding a mutex?
227329183Skevans	 */
228329183Skevans	slen = sizeof ss;
229329183Skevans	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
230329183Skevans		if (errno != ENOTCONN) {
231329183Skevans			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
232146421Ssobomax			rpc_createerr.cf_error.re_errno = errno;
233329183Skevans			mutex_unlock(&clnt_fd_lock);
234329183Skevans			goto err;
235329183Skevans		}
236329183Skevans		if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
237329183Skevans			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
238329183Skevans			rpc_createerr.cf_error.re_errno = errno;
239329183Skevans			mutex_unlock(&clnt_fd_lock);
240329183Skevans			goto err;
241329183Skevans		}
242329183Skevans	}
243146421Ssobomax	mutex_unlock(&clnt_fd_lock);
244329183Skevans	if (!__rpc_fd2sockinfo(fd, &si))
245329183Skevans		goto err;
246329183Skevans	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
247329183Skevans
248329183Skevans	ct->ct_closeit = FALSE;
249329183Skevans
250329183Skevans	/*
251329183Skevans	 * Set up private data struct
252329183Skevans	 */
25338465Smsmith	ct->ct_fd = fd;
25438465Smsmith	ct->ct_wait.tv_usec = 0;
25538465Smsmith	ct->ct_waitset = FALSE;
25638465Smsmith	ct->ct_addr.buf = malloc(raddr->maxlen);
25738465Smsmith	if (ct->ct_addr.buf == NULL)
25838465Smsmith		goto err;
259146365Ssobomax	memcpy(ct->ct_addr.buf, &raddr->buf, raddr->len);
26038465Smsmith	ct->ct_addr.len = raddr->maxlen;
261329183Skevans	ct->ct_addr.maxlen = raddr->maxlen;
262329183Skevans
263329183Skevans	/*
264146365Ssobomax	 * Initialize call message
265329183Skevans	 */
266329183Skevans	(void)gettimeofday(&now, NULL);
267329183Skevans	call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
268329183Skevans	call_msg.rm_direction = CALL;
269329183Skevans	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
270146365Ssobomax	call_msg.rm_call.cb_prog = (u_int32_t)prog;
271329183Skevans	call_msg.rm_call.cb_vers = (u_int32_t)vers;
272329183Skevans
273329183Skevans	/*
274329183Skevans	 * pre-serialize the static part of the call msg and stash it away
275329183Skevans	 */
27638465Smsmith	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
277329183Skevans	    XDR_ENCODE);
278329183Skevans	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
279329183Skevans		if (ct->ct_closeit) {
280329183Skevans			(void)_close(fd);
281329183Skevans		}
28238465Smsmith		goto err;
283329183Skevans	}
284329183Skevans	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
285329183Skevans	XDR_DESTROY(&(ct->ct_xdrs));
286329183Skevans
287329183Skevans	/*
288329183Skevans	 * Create a client handle which uses xdrrec for serialization
289329183Skevans	 * and authnone for authentication.
290329183Skevans	 */
291329183Skevans	cl->cl_ops = clnt_vc_ops();
292329183Skevans	cl->cl_private = ct;
293329183Skevans	cl->cl_auth = authnone_create();
294329183Skevans	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
295329183Skevans	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
296329183Skevans	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
297329183Skevans	    cl->cl_private, read_vc, write_vc);
29838465Smsmith	return (cl);
29938465Smsmith
30048952Smsmitherr:
30148952Smsmith	if (cl) {
302146365Ssobomax		if (ct) {
30348952Smsmith			if (ct->ct_addr.len)
30448952Smsmith				mem_free(ct->ct_addr.buf, ct->ct_addr.len);
30548952Smsmith			mem_free((caddr_t)ct, sizeof (struct ct_data));
30648952Smsmith		}
30748952Smsmith		if (cl)
30848952Smsmith			mem_free((caddr_t)cl, sizeof (CLIENT));
30948952Smsmith	}
31048952Smsmith	return ((CLIENT *)NULL);
31148952Smsmith}
31248952Smsmith
313329183Skevansstatic enum clnt_stat
314329183Skevansclnt_vc_call(cl, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
31548952Smsmith	CLIENT *cl;
316329183Skevans	rpcproc_t proc;
317329183Skevans	xdrproc_t xdr_args;
31848952Smsmith	caddr_t args_ptr;
319329183Skevans	xdrproc_t xdr_results;
320329183Skevans	caddr_t results_ptr;
321329183Skevans	struct timeval timeout;
322329183Skevans{
32348952Smsmith	struct ct_data *ct = (struct ct_data *) cl->cl_private;
324329183Skevans	XDR *xdrs = &(ct->ct_xdrs);
325329183Skevans	struct rpc_msg reply_msg;
326329183Skevans	u_int32_t x_id;
327329183Skevans	u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli;    /* yuk */
328329183Skevans	bool_t shipnow;
329329183Skevans	int refreshes = 2;
330146365Ssobomax	sigset_t mask, newmask;
331329183Skevans	int rpc_lock_value;
332329183Skevans
333329183Skevans	assert(cl != NULL);
334329183Skevans
335329183Skevans	sigfillset(&newmask);
336329183Skevans	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
337329183Skevans	mutex_lock(&clnt_fd_lock);
338329183Skevans	while (vc_fd_locks[ct->ct_fd])
339329183Skevans		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
340146365Ssobomax	if (__isthreaded)
341329183Skevans                rpc_lock_value = 1;
342329183Skevans        else
343329183Skevans                rpc_lock_value = 0;
344329183Skevans	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
345329183Skevans	mutex_unlock(&clnt_fd_lock);
346329183Skevans	if (!ct->ct_waitset) {
347329183Skevans		/* If time is not within limits, we ignore it. */
348329183Skevans		if (time_not_ok(&timeout) == FALSE)
349329183Skevans			ct->ct_wait = timeout;
350329183Skevans	}
351329183Skevans
352329183Skevans	shipnow =
353329183Skevans	    (xdr_results == NULL && timeout.tv_sec == 0
354329183Skevans	    && timeout.tv_usec == 0) ? FALSE : TRUE;
355329183Skevans
356329183Skevanscall_again:
357329183Skevans	xdrs->x_op = XDR_ENCODE;
358329183Skevans	ct->ct_error.re_status = RPC_SUCCESS;
359329183Skevans	x_id = ntohl(--(*msg_x_id));
36048952Smsmith
361329183Skevans	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
362329183Skevans	    (! XDR_PUTINT32(xdrs, &proc)) ||
363329183Skevans	    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
364329183Skevans	    (! (*xdr_args)(xdrs, args_ptr))) {
365329183Skevans		if (ct->ct_error.re_status == RPC_SUCCESS)
366329183Skevans			ct->ct_error.re_status = RPC_CANTENCODEARGS;
367329183Skevans		(void)xdrrec_endofrecord(xdrs, TRUE);
368329183Skevans		release_fd_lock(ct->ct_fd, mask);
369329183Skevans		return (ct->ct_error.re_status);
370329183Skevans	}
371329183Skevans	if (! xdrrec_endofrecord(xdrs, shipnow)) {
372329183Skevans		release_fd_lock(ct->ct_fd, mask);
373329183Skevans		return (ct->ct_error.re_status = RPC_CANTSEND);
374329183Skevans	}
375329183Skevans	if (! shipnow) {
376329183Skevans		release_fd_lock(ct->ct_fd, mask);
377329183Skevans		return (RPC_SUCCESS);
378329183Skevans	}
379329183Skevans	/*
380329183Skevans	 * Hack to provide rpc-based message passing
381329183Skevans	 */
382329183Skevans	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
383329183Skevans		release_fd_lock(ct->ct_fd, mask);
384329183Skevans		return(ct->ct_error.re_status = RPC_TIMEDOUT);
385329183Skevans	}
386329183Skevans
38748952Smsmith
38848952Smsmith	/*
389193192Srodrigc	 * Keep receiving until we get a valid transaction id
390235330Savg	 */
391329183Skevans	xdrs->x_op = XDR_DECODE;
392329183Skevans	while (TRUE) {
393235330Savg		reply_msg.acpted_rply.ar_verf = _null_auth;
394329183Skevans		reply_msg.acpted_rply.ar_results.where = NULL;
395329183Skevans		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
396329183Skevans		if (! xdrrec_skiprecord(xdrs)) {
397329183Skevans			release_fd_lock(ct->ct_fd, mask);
398329183Skevans			return (ct->ct_error.re_status);
399329183Skevans		}
400329183Skevans		/* now decode and validate the response header */
401329183Skevans		if (! xdr_replymsg(xdrs, &reply_msg)) {
402235330Savg			if (ct->ct_error.re_status == RPC_SUCCESS)
403235330Savg				continue;
404329183Skevans			release_fd_lock(ct->ct_fd, mask);
40548952Smsmith			return (ct->ct_error.re_status);
40665613Sdcs		}
40765613Sdcs		if (reply_msg.rm_xid == x_id)
40865613Sdcs			break;
40965613Sdcs	}
410329183Skevans
41165613Sdcs	/*
41265613Sdcs	 * process header
413329183Skevans	 */
414329183Skevans	_seterr_reply(&reply_msg, &(ct->ct_error));
415329183Skevans	if (ct->ct_error.re_status == RPC_SUCCESS) {
416329183Skevans		if (! AUTH_VALIDATE(cl->cl_auth,
41765613Sdcs		    &reply_msg.acpted_rply.ar_verf)) {
41865613Sdcs			ct->ct_error.re_status = RPC_AUTHERROR;
419			ct->ct_error.re_why = AUTH_INVALIDRESP;
420		} else if (! (*xdr_results)(xdrs, results_ptr)) {
421			if (ct->ct_error.re_status == RPC_SUCCESS)
422				ct->ct_error.re_status = RPC_CANTDECODERES;
423		}
424		/* free verifier ... */
425		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
426			xdrs->x_op = XDR_FREE;
427			(void)xdr_opaque_auth(xdrs,
428			    &(reply_msg.acpted_rply.ar_verf));
429		}
430	}  /* end successful completion */
431	else {
432		/* maybe our credentials need to be refreshed ... */
433		if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg))
434			goto call_again;
435	}  /* end of unsuccessful completion */
436	release_fd_lock(ct->ct_fd, mask);
437	return (ct->ct_error.re_status);
438}
439
440static void
441clnt_vc_geterr(cl, errp)
442	CLIENT *cl;
443	struct rpc_err *errp;
444{
445	struct ct_data *ct;
446
447	assert(cl != NULL);
448	assert(errp != NULL);
449
450	ct = (struct ct_data *) cl->cl_private;
451	*errp = ct->ct_error;
452}
453
454static bool_t
455clnt_vc_freeres(cl, xdr_res, res_ptr)
456	CLIENT *cl;
457	xdrproc_t xdr_res;
458	caddr_t res_ptr;
459{
460	struct ct_data *ct;
461	XDR *xdrs;
462	bool_t dummy;
463	sigset_t mask;
464	sigset_t newmask;
465
466	assert(cl != NULL);
467
468	ct = (struct ct_data *)cl->cl_private;
469	xdrs = &(ct->ct_xdrs);
470
471	sigfillset(&newmask);
472	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
473	mutex_lock(&clnt_fd_lock);
474	while (vc_fd_locks[ct->ct_fd])
475		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
476	xdrs->x_op = XDR_FREE;
477	dummy = (*xdr_res)(xdrs, res_ptr);
478	mutex_unlock(&clnt_fd_lock);
479	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
480	cond_signal(&vc_cv[ct->ct_fd]);
481
482	return dummy;
483}
484
485/*ARGSUSED*/
486static void
487clnt_vc_abort(cl)
488	CLIENT *cl;
489{
490}
491
492static bool_t
493clnt_vc_control(cl, request, info)
494	CLIENT *cl;
495	u_int request;
496	char *info;
497{
498	struct ct_data *ct;
499	void *infop = info;
500	sigset_t mask;
501	sigset_t newmask;
502	int rpc_lock_value;
503
504	assert(cl != NULL);
505
506	ct = (struct ct_data *)cl->cl_private;
507
508	sigfillset(&newmask);
509	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
510	mutex_lock(&clnt_fd_lock);
511	while (vc_fd_locks[ct->ct_fd])
512		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
513	if (__isthreaded)
514                rpc_lock_value = 1;
515        else
516                rpc_lock_value = 0;
517	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
518	mutex_unlock(&clnt_fd_lock);
519
520	switch (request) {
521	case CLSET_FD_CLOSE:
522		ct->ct_closeit = TRUE;
523		release_fd_lock(ct->ct_fd, mask);
524		return (TRUE);
525	case CLSET_FD_NCLOSE:
526		ct->ct_closeit = FALSE;
527		release_fd_lock(ct->ct_fd, mask);
528		return (TRUE);
529	default:
530		break;
531	}
532
533	/* for other requests which use info */
534	if (info == NULL) {
535		release_fd_lock(ct->ct_fd, mask);
536		return (FALSE);
537	}
538	switch (request) {
539	case CLSET_TIMEOUT:
540		if (time_not_ok((struct timeval *)(void *)info)) {
541			release_fd_lock(ct->ct_fd, mask);
542			return (FALSE);
543		}
544		ct->ct_wait = *(struct timeval *)infop;
545		ct->ct_waitset = TRUE;
546		break;
547	case CLGET_TIMEOUT:
548		*(struct timeval *)infop = ct->ct_wait;
549		break;
550	case CLGET_SERVER_ADDR:
551		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
552		break;
553	case CLGET_FD:
554		*(int *)(void *)info = ct->ct_fd;
555		break;
556	case CLGET_SVC_ADDR:
557		/* The caller should not free this memory area */
558		*(struct netbuf *)(void *)info = ct->ct_addr;
559		break;
560	case CLSET_SVC_ADDR:		/* set to new address */
561		release_fd_lock(ct->ct_fd, mask);
562		return (FALSE);
563	case CLGET_XID:
564		/*
565		 * use the knowledge that xid is the
566		 * first element in the call structure
567		 * This will get the xid of the PREVIOUS call
568		 */
569		*(u_int32_t *)(void *)info =
570		    ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
571		break;
572	case CLSET_XID:
573		/* This will set the xid of the NEXT call */
574		*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
575		    htonl(*((u_int32_t *)(void *)info) + 1);
576		/* increment by 1 as clnt_vc_call() decrements once */
577		break;
578	case CLGET_VERS:
579		/*
580		 * This RELIES on the information that, in the call body,
581		 * the version number field is the fifth field from the
582		 * begining of the RPC header. MUST be changed if the
583		 * call_struct is changed
584		 */
585		*(u_int32_t *)(void *)info =
586		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
587		    4 * BYTES_PER_XDR_UNIT));
588		break;
589
590	case CLSET_VERS:
591		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
592		    4 * BYTES_PER_XDR_UNIT) =
593		    htonl(*(u_int32_t *)(void *)info);
594		break;
595
596	case CLGET_PROG:
597		/*
598		 * This RELIES on the information that, in the call body,
599		 * the program number field is the fourth field from the
600		 * begining of the RPC header. MUST be changed if the
601		 * call_struct is changed
602		 */
603		*(u_int32_t *)(void *)info =
604		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
605		    3 * BYTES_PER_XDR_UNIT));
606		break;
607
608	case CLSET_PROG:
609		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
610		    3 * BYTES_PER_XDR_UNIT) =
611		    htonl(*(u_int32_t *)(void *)info);
612		break;
613
614	default:
615		release_fd_lock(ct->ct_fd, mask);
616		return (FALSE);
617	}
618	release_fd_lock(ct->ct_fd, mask);
619	return (TRUE);
620}
621
622
623static void
624clnt_vc_destroy(cl)
625	CLIENT *cl;
626{
627	struct ct_data *ct = (struct ct_data *) cl->cl_private;
628	int ct_fd = ct->ct_fd;
629	sigset_t mask;
630	sigset_t newmask;
631
632	assert(cl != NULL);
633
634	ct = (struct ct_data *) cl->cl_private;
635
636	sigfillset(&newmask);
637	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
638	mutex_lock(&clnt_fd_lock);
639	while (vc_fd_locks[ct_fd])
640		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
641	if (ct->ct_closeit && ct->ct_fd != -1) {
642		(void)_close(ct->ct_fd);
643	}
644	XDR_DESTROY(&(ct->ct_xdrs));
645	if (ct->ct_addr.buf)
646		free(ct->ct_addr.buf);
647	mem_free(ct, sizeof(struct ct_data));
648	mem_free(cl, sizeof(CLIENT));
649	mutex_unlock(&clnt_fd_lock);
650	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
651	cond_signal(&vc_cv[ct_fd]);
652}
653
654/*
655 * Interface between xdr serializer and tcp connection.
656 * Behaves like the system calls, read & write, but keeps some error state
657 * around for the rpc level.
658 */
659static int
660read_vc(ctp, buf, len)
661	caddr_t ctp;
662	caddr_t buf;
663	int len;
664{
665	struct sockaddr sa;
666	socklen_t sal;
667	struct ct_data *ct = (struct ct_data *)(void *)ctp;
668	struct pollfd fd;
669	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
670	    (ct->ct_wait.tv_usec / 1000));
671
672	if (len == 0)
673		return (0);
674	fd.fd = ct->ct_fd;
675	fd.events = POLLIN;
676	for (;;) {
677		switch (_poll(&fd, 1, milliseconds)) {
678		case 0:
679			ct->ct_error.re_status = RPC_TIMEDOUT;
680			return (-1);
681
682		case -1:
683			if (errno == EINTR)
684				continue;
685			ct->ct_error.re_status = RPC_CANTRECV;
686			ct->ct_error.re_errno = errno;
687			return (-1);
688		}
689		break;
690	}
691
692	sal = sizeof(sa);
693	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
694	    (sa.sa_family == AF_LOCAL)) {
695		len = __msgread(ct->ct_fd, buf, (size_t)len);
696	} else {
697		len = _read(ct->ct_fd, buf, (size_t)len);
698	}
699
700	switch (len) {
701	case 0:
702		/* premature eof */
703		ct->ct_error.re_errno = ECONNRESET;
704		ct->ct_error.re_status = RPC_CANTRECV;
705		len = -1;  /* it's really an error */
706		break;
707
708	case -1:
709		ct->ct_error.re_errno = errno;
710		ct->ct_error.re_status = RPC_CANTRECV;
711		break;
712	}
713	return (len);
714}
715
716static int
717write_vc(ctp, buf, len)
718	caddr_t ctp;
719	caddr_t buf;
720	int len;
721{
722	struct sockaddr sa;
723	socklen_t sal;
724	struct ct_data *ct = (struct ct_data *)(void *)ctp;
725	int i, cnt;
726
727	sal = sizeof(sa);
728	if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
729	    (sa.sa_family == AF_LOCAL)) {
730		for (cnt = len; cnt > 0; cnt -= i, buf += i) {
731			if ((i = __msgwrite(ct->ct_fd, buf,
732			     (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	} else {
739		for (cnt = len; cnt > 0; cnt -= i, buf += i) {
740			if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
741				ct->ct_error.re_errno = errno;
742				ct->ct_error.re_status = RPC_CANTSEND;
743				return (-1);
744			}
745		}
746	}
747	return (len);
748}
749
750static struct clnt_ops *
751clnt_vc_ops()
752{
753	static struct clnt_ops ops;
754	extern mutex_t  ops_lock;
755	sigset_t mask, newmask;
756
757	/* VARIABLES PROTECTED BY ops_lock: ops */
758
759	sigfillset(&newmask);
760	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
761	mutex_lock(&ops_lock);
762	if (ops.cl_call == NULL) {
763		ops.cl_call = clnt_vc_call;
764		ops.cl_abort = clnt_vc_abort;
765		ops.cl_geterr = clnt_vc_geterr;
766		ops.cl_freeres = clnt_vc_freeres;
767		ops.cl_destroy = clnt_vc_destroy;
768		ops.cl_control = clnt_vc_control;
769	}
770	mutex_unlock(&ops_lock);
771	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
772	return (&ops);
773}
774
775/*
776 * Make sure that the time is not garbage.   -1 value is disallowed.
777 * Note this is different from time_not_ok in clnt_dg.c
778 */
779static bool_t
780time_not_ok(t)
781	struct timeval *t;
782{
783	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
784		t->tv_usec <= -1 || t->tv_usec > 1000000);
785}
786
787static int
788__msgread(sock, buf, cnt)
789	int sock;
790	void *buf;
791	size_t cnt;
792{
793	struct iovec iov[1];
794	struct msghdr msg;
795	struct cmessage cm;
796
797	bzero((char *)&cm, sizeof(cm));
798	iov[0].iov_base = buf;
799	iov[0].iov_len = cnt;
800
801	msg.msg_iov = iov;
802	msg.msg_iovlen = 1;
803	msg.msg_name = NULL;
804	msg.msg_namelen = 0;
805	msg.msg_control = (caddr_t)&cm;
806	msg.msg_controllen = sizeof(struct cmessage);
807	msg.msg_flags = 0;
808
809	return(_recvmsg(sock, &msg, 0));
810}
811
812static int
813__msgwrite(sock, buf, cnt)
814	int sock;
815	void *buf;
816	size_t cnt;
817{
818	struct iovec iov[1];
819	struct msghdr msg;
820	struct cmessage cm;
821
822	bzero((char *)&cm, sizeof(cm));
823	iov[0].iov_base = buf;
824	iov[0].iov_len = cnt;
825
826	cm.cmsg.cmsg_type = SCM_CREDS;
827	cm.cmsg.cmsg_level = SOL_SOCKET;
828	cm.cmsg.cmsg_len = sizeof(struct cmessage);
829
830	msg.msg_iov = iov;
831	msg.msg_iovlen = 1;
832	msg.msg_name = NULL;
833	msg.msg_namelen = 0;
834	msg.msg_control = (caddr_t)&cm;
835	msg.msg_controllen = sizeof(struct cmessage);
836	msg.msg_flags = 0;
837
838	return(_sendmsg(sock, &msg, 0));
839}
840