clnt_dg.c revision 261046
1/*	$NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $	*/
2
3/*-
4 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 *   this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 *   this list of conditions and the following disclaimer in the documentation
13 *   and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 *   contributors may be used to endorse or promote products derived
16 *   from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30/*
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35#ident	"@(#)clnt_dg.c	1.23	94/04/22 SMI"
36static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
37#endif
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: stable/10/lib/libc/rpc/clnt_dg.c 261046 2014-01-22 23:45:27Z mav $");
40
41/*
42 * Implements a connectionless client side RPC.
43 */
44
45#include "namespace.h"
46#include "reentrant.h"
47#include <sys/types.h>
48#include <sys/event.h>
49#include <sys/time.h>
50#include <sys/socket.h>
51#include <sys/ioctl.h>
52#include <arpa/inet.h>
53#include <rpc/rpc.h>
54#include <rpc/rpcsec_gss.h>
55#include <errno.h>
56#include <stdlib.h>
57#include <string.h>
58#include <signal.h>
59#include <unistd.h>
60#include <err.h>
61#include "un-namespace.h"
62#include "rpc_com.h"
63#include "mt_misc.h"
64
65
66#ifdef _FREEFALL_CONFIG
67/*
68 * Disable RPC exponential back-off for FreeBSD.org systems.
69 */
70#define	RPC_MAX_BACKOFF		1 /* second */
71#else
72#define	RPC_MAX_BACKOFF		30 /* seconds */
73#endif
74
75
76static struct clnt_ops *clnt_dg_ops(void);
77static bool_t time_not_ok(struct timeval *);
78static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
79	    xdrproc_t, void *, struct timeval);
80static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
81static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
82static void clnt_dg_abort(CLIENT *);
83static bool_t clnt_dg_control(CLIENT *, u_int, void *);
84static void clnt_dg_destroy(CLIENT *);
85
86
87
88
89/*
90 *	This machinery implements per-fd locks for MT-safety.  It is not
91 *	sufficient to do per-CLIENT handle locks for MT-safety because a
92 *	user may create more than one CLIENT handle with the same fd behind
93 *	it.  Therfore, we allocate an array of flags (dg_fd_locks), protected
94 *	by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
95 *	similarly protected.  Dg_fd_lock[fd] == 1 => a call is activte on some
96 *	CLIENT handle created for that fd.
97 *	The current implementation holds locks across the entire RPC and reply,
98 *	including retransmissions.  Yes, this is silly, and as soon as this
99 *	code is proven to work, this should be the first thing fixed.  One step
100 *	at a time.
101 */
102static int	*dg_fd_locks;
103static cond_t	*dg_cv;
104#define	release_fd_lock(fd, mask) {		\
105	mutex_lock(&clnt_fd_lock);	\
106	dg_fd_locks[fd] = 0;		\
107	mutex_unlock(&clnt_fd_lock);	\
108	thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
109	cond_signal(&dg_cv[fd]);	\
110}
111
112static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
113
114/* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
115
116#define	MCALL_MSG_SIZE 24
117
118/*
119 * Private data kept per client handle
120 */
121struct cu_data {
122	int			cu_fd;		/* connections fd */
123	bool_t			cu_closeit;	/* opened by library */
124	struct sockaddr_storage	cu_raddr;	/* remote address */
125	int			cu_rlen;
126	struct timeval		cu_wait;	/* retransmit interval */
127	struct timeval		cu_total;	/* total time for the call */
128	struct rpc_err		cu_error;
129	XDR			cu_outxdrs;
130	u_int			cu_xdrpos;
131	u_int			cu_sendsz;	/* send size */
132	char			cu_outhdr[MCALL_MSG_SIZE];
133	char			*cu_outbuf;
134	u_int			cu_recvsz;	/* recv size */
135	int			cu_async;
136	int			cu_connect;	/* Use connect(). */
137	int			cu_connected;	/* Have done connect(). */
138	struct kevent		cu_kin;
139	int			cu_kq;
140	char			cu_inbuf[1];
141};
142
143/*
144 * Connection less client creation returns with client handle parameters.
145 * Default options are set, which the user can change using clnt_control().
146 * fd should be open and bound.
147 * NB: The rpch->cl_auth is initialized to null authentication.
148 * 	Caller may wish to set this something more useful.
149 *
150 * sendsz and recvsz are the maximum allowable packet sizes that can be
151 * sent and received. Normally they are the same, but they can be
152 * changed to improve the program efficiency and buffer allocation.
153 * If they are 0, use the transport default.
154 *
155 * If svcaddr is NULL, returns NULL.
156 */
157CLIENT *
158clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
159	int fd;				/* open file descriptor */
160	const struct netbuf *svcaddr;	/* servers address */
161	rpcprog_t program;		/* program number */
162	rpcvers_t version;		/* version number */
163	u_int sendsz;			/* buffer recv size */
164	u_int recvsz;			/* buffer send size */
165{
166	CLIENT *cl = NULL;		/* client handle */
167	struct cu_data *cu = NULL;	/* private data */
168	struct timeval now;
169	struct rpc_msg call_msg;
170	sigset_t mask;
171	sigset_t newmask;
172	struct __rpc_sockinfo si;
173	int one = 1;
174
175	sigfillset(&newmask);
176	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
177	mutex_lock(&clnt_fd_lock);
178	if (dg_fd_locks == (int *) NULL) {
179		int cv_allocsz;
180		size_t fd_allocsz;
181		int dtbsize = __rpc_dtbsize();
182
183		fd_allocsz = dtbsize * sizeof (int);
184		dg_fd_locks = (int *) mem_alloc(fd_allocsz);
185		if (dg_fd_locks == (int *) NULL) {
186			mutex_unlock(&clnt_fd_lock);
187			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
188			goto err1;
189		} else
190			memset(dg_fd_locks, '\0', fd_allocsz);
191
192		cv_allocsz = dtbsize * sizeof (cond_t);
193		dg_cv = (cond_t *) mem_alloc(cv_allocsz);
194		if (dg_cv == (cond_t *) NULL) {
195			mem_free(dg_fd_locks, fd_allocsz);
196			dg_fd_locks = (int *) NULL;
197			mutex_unlock(&clnt_fd_lock);
198			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
199			goto err1;
200		} else {
201			int i;
202
203			for (i = 0; i < dtbsize; i++)
204				cond_init(&dg_cv[i], 0, (void *) 0);
205		}
206	}
207
208	mutex_unlock(&clnt_fd_lock);
209	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
210
211	if (svcaddr == NULL) {
212		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
213		return (NULL);
214	}
215
216	if (!__rpc_fd2sockinfo(fd, &si)) {
217		rpc_createerr.cf_stat = RPC_TLIERROR;
218		rpc_createerr.cf_error.re_errno = 0;
219		return (NULL);
220	}
221	/*
222	 * Find the receive and the send size
223	 */
224	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
225	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
226	if ((sendsz == 0) || (recvsz == 0)) {
227		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
228		rpc_createerr.cf_error.re_errno = 0;
229		return (NULL);
230	}
231
232	if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
233		goto err1;
234	/*
235	 * Should be multiple of 4 for XDR.
236	 */
237	sendsz = ((sendsz + 3) / 4) * 4;
238	recvsz = ((recvsz + 3) / 4) * 4;
239	cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
240	if (cu == NULL)
241		goto err1;
242	(void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
243	cu->cu_rlen = svcaddr->len;
244	cu->cu_outbuf = &cu->cu_inbuf[recvsz];
245	/* Other values can also be set through clnt_control() */
246	cu->cu_wait.tv_sec = 15;	/* heuristically chosen */
247	cu->cu_wait.tv_usec = 0;
248	cu->cu_total.tv_sec = -1;
249	cu->cu_total.tv_usec = -1;
250	cu->cu_sendsz = sendsz;
251	cu->cu_recvsz = recvsz;
252	cu->cu_async = FALSE;
253	cu->cu_connect = FALSE;
254	cu->cu_connected = FALSE;
255	(void) gettimeofday(&now, NULL);
256	call_msg.rm_xid = __RPC_GETXID(&now);
257	call_msg.rm_call.cb_prog = program;
258	call_msg.rm_call.cb_vers = version;
259	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outhdr, MCALL_MSG_SIZE,
260	    XDR_ENCODE);
261	if (! xdr_callhdr(&cu->cu_outxdrs, &call_msg)) {
262		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
263		rpc_createerr.cf_error.re_errno = 0;
264		goto err2;
265	}
266	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
267	XDR_DESTROY(&cu->cu_outxdrs);
268	xdrmem_create(&cu->cu_outxdrs, cu->cu_outbuf, sendsz, XDR_ENCODE);
269
270	/* XXX fvdl - do we still want this? */
271#if 0
272	(void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
273#endif
274	_ioctl(fd, FIONBIO, (char *)(void *)&one);
275
276	/*
277	 * By default, closeit is always FALSE. It is users responsibility
278	 * to do a close on it, else the user may use clnt_control
279	 * to let clnt_destroy do it for him/her.
280	 */
281	cu->cu_closeit = FALSE;
282	cu->cu_fd = fd;
283	cl->cl_ops = clnt_dg_ops();
284	cl->cl_private = (caddr_t)(void *)cu;
285	cl->cl_auth = authnone_create();
286	cl->cl_tp = NULL;
287	cl->cl_netid = NULL;
288	cu->cu_kq = -1;
289	EV_SET(&cu->cu_kin, cu->cu_fd, EVFILT_READ, EV_ADD, 0, 0, 0);
290	return (cl);
291err1:
292	warnx(mem_err_clnt_dg);
293	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
294	rpc_createerr.cf_error.re_errno = errno;
295err2:
296	if (cl) {
297		mem_free(cl, sizeof (CLIENT));
298		if (cu)
299			mem_free(cu, sizeof (*cu) + sendsz + recvsz);
300	}
301	return (NULL);
302}
303
304static enum clnt_stat
305clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
306	CLIENT	*cl;			/* client handle */
307	rpcproc_t	proc;		/* procedure number */
308	xdrproc_t	xargs;		/* xdr routine for args */
309	void		*argsp;		/* pointer to args */
310	xdrproc_t	xresults;	/* xdr routine for results */
311	void		*resultsp;	/* pointer to results */
312	struct timeval	utimeout;	/* seconds to wait before giving up */
313{
314	struct cu_data *cu = (struct cu_data *)cl->cl_private;
315	XDR *xdrs;
316	size_t outlen = 0;
317	struct rpc_msg reply_msg;
318	XDR reply_xdrs;
319	bool_t ok;
320	int nrefreshes = 2;		/* number of times to refresh cred */
321	int nretries = 0;		/* number of times we retransmitted */
322	struct timeval timeout;
323	struct timeval retransmit_time;
324	struct timeval next_sendtime, starttime, time_waited, tv;
325	struct timespec ts;
326	struct kevent kv;
327	struct sockaddr *sa;
328	sigset_t mask;
329	sigset_t newmask;
330	socklen_t inlen, salen;
331	ssize_t recvlen = 0;
332	int kin_len, n, rpc_lock_value;
333	u_int32_t xid;
334
335	outlen = 0;
336	sigfillset(&newmask);
337	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
338	mutex_lock(&clnt_fd_lock);
339	while (dg_fd_locks[cu->cu_fd])
340		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
341	if (__isthreaded)
342		rpc_lock_value = 1;
343	else
344		rpc_lock_value = 0;
345	dg_fd_locks[cu->cu_fd] = rpc_lock_value;
346	mutex_unlock(&clnt_fd_lock);
347	if (cu->cu_total.tv_usec == -1) {
348		timeout = utimeout;	/* use supplied timeout */
349	} else {
350		timeout = cu->cu_total;	/* use default timeout */
351	}
352
353	if (cu->cu_connect && !cu->cu_connected) {
354		if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr,
355		    cu->cu_rlen) < 0) {
356			cu->cu_error.re_errno = errno;
357			cu->cu_error.re_status = RPC_CANTSEND;
358			goto out;
359		}
360		cu->cu_connected = 1;
361	}
362	if (cu->cu_connected) {
363		sa = NULL;
364		salen = 0;
365	} else {
366		sa = (struct sockaddr *)&cu->cu_raddr;
367		salen = cu->cu_rlen;
368	}
369	time_waited.tv_sec = 0;
370	time_waited.tv_usec = 0;
371	retransmit_time = next_sendtime = cu->cu_wait;
372	gettimeofday(&starttime, NULL);
373
374	/* Clean up in case the last call ended in a longjmp(3) call. */
375	if (cu->cu_kq >= 0)
376		_close(cu->cu_kq);
377	if ((cu->cu_kq = kqueue()) < 0) {
378		cu->cu_error.re_errno = errno;
379		cu->cu_error.re_status = RPC_CANTSEND;
380		goto out;
381	}
382	kin_len = 1;
383
384call_again:
385	if (cu->cu_async == TRUE && xargs == NULL)
386		goto get_reply;
387	/*
388	 * the transaction is the first thing in the out buffer
389	 * XXX Yes, and it's in network byte order, so we should to
390	 * be careful when we increment it, shouldn't we.
391	 */
392	xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr));
393	xid++;
394	*(u_int32_t *)(void *)(cu->cu_outhdr) = htonl(xid);
395call_again_same_xid:
396	xdrs = &(cu->cu_outxdrs);
397	xdrs->x_op = XDR_ENCODE;
398	XDR_SETPOS(xdrs, 0);
399
400	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
401		if ((! XDR_PUTBYTES(xdrs, cu->cu_outhdr, cu->cu_xdrpos)) ||
402		    (! XDR_PUTINT32(xdrs, &proc)) ||
403		    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
404		    (! (*xargs)(xdrs, argsp))) {
405			cu->cu_error.re_status = RPC_CANTENCODEARGS;
406			goto out;
407		}
408	} else {
409		*(uint32_t *) &cu->cu_outhdr[cu->cu_xdrpos] = htonl(proc);
410		if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outhdr,
411			cu->cu_xdrpos + sizeof(uint32_t),
412			xdrs, xargs, argsp)) {
413			cu->cu_error.re_status = RPC_CANTENCODEARGS;
414			goto out;
415		}
416	}
417	outlen = (size_t)XDR_GETPOS(xdrs);
418
419send_again:
420	if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) {
421		cu->cu_error.re_errno = errno;
422		cu->cu_error.re_status = RPC_CANTSEND;
423		goto out;
424	}
425
426	/*
427	 * Hack to provide rpc-based message passing
428	 */
429	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
430		cu->cu_error.re_status = RPC_TIMEDOUT;
431		goto out;
432	}
433
434get_reply:
435
436	/*
437	 * sub-optimal code appears here because we have
438	 * some clock time to spare while the packets are in flight.
439	 * (We assume that this is actually only executed once.)
440	 */
441	reply_msg.acpted_rply.ar_verf = _null_auth;
442	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
443		reply_msg.acpted_rply.ar_results.where = resultsp;
444		reply_msg.acpted_rply.ar_results.proc = xresults;
445	} else {
446		reply_msg.acpted_rply.ar_results.where = NULL;
447		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
448	}
449
450	for (;;) {
451		/* Decide how long to wait. */
452		if (timercmp(&next_sendtime, &timeout, <))
453			timersub(&next_sendtime, &time_waited, &tv);
454		else
455			timersub(&timeout, &time_waited, &tv);
456		if (tv.tv_sec < 0 || tv.tv_usec < 0)
457			tv.tv_sec = tv.tv_usec = 0;
458		TIMEVAL_TO_TIMESPEC(&tv, &ts);
459
460		n = _kevent(cu->cu_kq, &cu->cu_kin, kin_len, &kv, 1, &ts);
461		/* We don't need to register the event again. */
462		kin_len = 0;
463
464		if (n == 1) {
465			if (kv.flags & EV_ERROR) {
466				cu->cu_error.re_errno = kv.data;
467				cu->cu_error.re_status = RPC_CANTRECV;
468				goto out;
469			}
470			/* We have some data now */
471			do {
472				recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf,
473				    cu->cu_recvsz, 0, NULL, NULL);
474			} while (recvlen < 0 && errno == EINTR);
475			if (recvlen < 0 && errno != EWOULDBLOCK) {
476				cu->cu_error.re_errno = errno;
477				cu->cu_error.re_status = RPC_CANTRECV;
478				goto out;
479			}
480			if (recvlen >= sizeof(u_int32_t) &&
481			    (cu->cu_async == TRUE ||
482			    *((u_int32_t *)(void *)(cu->cu_inbuf)) ==
483			    *((u_int32_t *)(void *)(cu->cu_outbuf)))) {
484				/* We now assume we have the proper reply. */
485				break;
486			}
487		}
488		if (n == -1 && errno != EINTR) {
489			cu->cu_error.re_errno = errno;
490			cu->cu_error.re_status = RPC_CANTRECV;
491			goto out;
492		}
493		gettimeofday(&tv, NULL);
494		timersub(&tv, &starttime, &time_waited);
495
496		/* Check for timeout. */
497		if (timercmp(&time_waited, &timeout, >)) {
498			cu->cu_error.re_status = RPC_TIMEDOUT;
499			goto out;
500		}
501
502		/* Retransmit if necessary. */
503		if (timercmp(&time_waited, &next_sendtime, >)) {
504			/* update retransmit_time */
505			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
506				timeradd(&retransmit_time, &retransmit_time,
507				    &retransmit_time);
508			timeradd(&next_sendtime, &retransmit_time,
509			    &next_sendtime);
510			nretries++;
511
512			/*
513			 * When retransmitting a RPCSEC_GSS message,
514			 * we must use a new sequence number (handled
515			 * by __rpc_gss_wrap above).
516			 */
517			if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS)
518				goto send_again;
519			else
520				goto call_again_same_xid;
521		}
522	}
523	inlen = (socklen_t)recvlen;
524
525	/*
526	 * now decode and validate the response
527	 */
528
529	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
530	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
531	/* XDR_DESTROY(&reply_xdrs);	save a few cycles on noop destroy */
532	if (ok) {
533		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
534			(reply_msg.acpted_rply.ar_stat == SUCCESS))
535			cu->cu_error.re_status = RPC_SUCCESS;
536		else
537			_seterr_reply(&reply_msg, &(cu->cu_error));
538
539		if (cu->cu_error.re_status == RPC_SUCCESS) {
540			if (! AUTH_VALIDATE(cl->cl_auth,
541					    &reply_msg.acpted_rply.ar_verf)) {
542				if (nretries &&
543				    cl->cl_auth->ah_cred.oa_flavor
544				    == RPCSEC_GSS)
545					/*
546					 * If we retransmitted, its
547					 * possible that we will
548					 * receive a reply for one of
549					 * the earlier transmissions
550					 * (which will use an older
551					 * RPCSEC_GSS sequence
552					 * number). In this case, just
553					 * go back and listen for a
554					 * new reply. We could keep a
555					 * record of all the seq
556					 * numbers we have transmitted
557					 * so far so that we could
558					 * accept a reply for any of
559					 * them here.
560					 */
561					goto get_reply;
562				cu->cu_error.re_status = RPC_AUTHERROR;
563				cu->cu_error.re_why = AUTH_INVALIDRESP;
564			} else {
565				if (cl->cl_auth->ah_cred.oa_flavor
566				    == RPCSEC_GSS) {
567					if (!__rpc_gss_unwrap(cl->cl_auth,
568						&reply_xdrs, xresults,
569						resultsp))
570						cu->cu_error.re_status =
571							RPC_CANTDECODERES;
572				}
573			}
574			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
575				xdrs->x_op = XDR_FREE;
576				(void) xdr_opaque_auth(xdrs,
577					&(reply_msg.acpted_rply.ar_verf));
578			}
579		}		/* end successful completion */
580		/*
581		 * If unsuccesful AND error is an authentication error
582		 * then refresh credentials and try again, else break
583		 */
584		else if (cu->cu_error.re_status == RPC_AUTHERROR)
585			/* maybe our credentials need to be refreshed ... */
586			if (nrefreshes > 0 &&
587			    AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
588				nrefreshes--;
589				goto call_again;
590			}
591		/* end of unsuccessful completion */
592	}	/* end of valid reply message */
593	else {
594		cu->cu_error.re_status = RPC_CANTDECODERES;
595
596	}
597out:
598	if (cu->cu_kq >= 0)
599		_close(cu->cu_kq);
600	cu->cu_kq = -1;
601	release_fd_lock(cu->cu_fd, mask);
602	return (cu->cu_error.re_status);
603}
604
605static void
606clnt_dg_geterr(cl, errp)
607	CLIENT *cl;
608	struct rpc_err *errp;
609{
610	struct cu_data *cu = (struct cu_data *)cl->cl_private;
611
612	*errp = cu->cu_error;
613}
614
615static bool_t
616clnt_dg_freeres(cl, xdr_res, res_ptr)
617	CLIENT *cl;
618	xdrproc_t xdr_res;
619	void *res_ptr;
620{
621	struct cu_data *cu = (struct cu_data *)cl->cl_private;
622	XDR *xdrs = &(cu->cu_outxdrs);
623	bool_t dummy;
624	sigset_t mask;
625	sigset_t newmask;
626
627	sigfillset(&newmask);
628	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
629	mutex_lock(&clnt_fd_lock);
630	while (dg_fd_locks[cu->cu_fd])
631		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
632	xdrs->x_op = XDR_FREE;
633	dummy = (*xdr_res)(xdrs, res_ptr);
634	mutex_unlock(&clnt_fd_lock);
635	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
636	cond_signal(&dg_cv[cu->cu_fd]);
637	return (dummy);
638}
639
640/*ARGSUSED*/
641static void
642clnt_dg_abort(h)
643	CLIENT *h;
644{
645}
646
647static bool_t
648clnt_dg_control(cl, request, info)
649	CLIENT *cl;
650	u_int request;
651	void *info;
652{
653	struct cu_data *cu = (struct cu_data *)cl->cl_private;
654	struct netbuf *addr;
655	sigset_t mask;
656	sigset_t newmask;
657	int rpc_lock_value;
658
659	sigfillset(&newmask);
660	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
661	mutex_lock(&clnt_fd_lock);
662	while (dg_fd_locks[cu->cu_fd])
663		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
664	if (__isthreaded)
665                rpc_lock_value = 1;
666        else
667                rpc_lock_value = 0;
668	dg_fd_locks[cu->cu_fd] = rpc_lock_value;
669	mutex_unlock(&clnt_fd_lock);
670	switch (request) {
671	case CLSET_FD_CLOSE:
672		cu->cu_closeit = TRUE;
673		release_fd_lock(cu->cu_fd, mask);
674		return (TRUE);
675	case CLSET_FD_NCLOSE:
676		cu->cu_closeit = FALSE;
677		release_fd_lock(cu->cu_fd, mask);
678		return (TRUE);
679	}
680
681	/* for other requests which use info */
682	if (info == NULL) {
683		release_fd_lock(cu->cu_fd, mask);
684		return (FALSE);
685	}
686	switch (request) {
687	case CLSET_TIMEOUT:
688		if (time_not_ok((struct timeval *)info)) {
689			release_fd_lock(cu->cu_fd, mask);
690			return (FALSE);
691		}
692		cu->cu_total = *(struct timeval *)info;
693		break;
694	case CLGET_TIMEOUT:
695		*(struct timeval *)info = cu->cu_total;
696		break;
697	case CLGET_SERVER_ADDR:		/* Give him the fd address */
698		/* Now obsolete. Only for backward compatibility */
699		(void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
700		break;
701	case CLSET_RETRY_TIMEOUT:
702		if (time_not_ok((struct timeval *)info)) {
703			release_fd_lock(cu->cu_fd, mask);
704			return (FALSE);
705		}
706		cu->cu_wait = *(struct timeval *)info;
707		break;
708	case CLGET_RETRY_TIMEOUT:
709		*(struct timeval *)info = cu->cu_wait;
710		break;
711	case CLGET_FD:
712		*(int *)info = cu->cu_fd;
713		break;
714	case CLGET_SVC_ADDR:
715		addr = (struct netbuf *)info;
716		addr->buf = &cu->cu_raddr;
717		addr->len = cu->cu_rlen;
718		addr->maxlen = sizeof cu->cu_raddr;
719		break;
720	case CLSET_SVC_ADDR:		/* set to new address */
721		addr = (struct netbuf *)info;
722		if (addr->len < sizeof cu->cu_raddr) {
723			release_fd_lock(cu->cu_fd, mask);
724			return (FALSE);
725		}
726		(void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
727		cu->cu_rlen = addr->len;
728		break;
729	case CLGET_XID:
730		/*
731		 * use the knowledge that xid is the
732		 * first element in the call structure *.
733		 * This will get the xid of the PREVIOUS call
734		 */
735		*(u_int32_t *)info =
736		    ntohl(*(u_int32_t *)(void *)cu->cu_outhdr);
737		break;
738
739	case CLSET_XID:
740		/* This will set the xid of the NEXT call */
741		*(u_int32_t *)(void *)cu->cu_outhdr =
742		    htonl(*(u_int32_t *)info - 1);
743		/* decrement by 1 as clnt_dg_call() increments once */
744		break;
745
746	case CLGET_VERS:
747		/*
748		 * This RELIES on the information that, in the call body,
749		 * the version number field is the fifth field from the
750		 * begining of the RPC header. MUST be changed if the
751		 * call_struct is changed
752		 */
753		*(u_int32_t *)info =
754		    ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
755		    4 * BYTES_PER_XDR_UNIT));
756		break;
757
758	case CLSET_VERS:
759		*(u_int32_t *)(void *)(cu->cu_outhdr + 4 * BYTES_PER_XDR_UNIT)
760			= htonl(*(u_int32_t *)info);
761		break;
762
763	case CLGET_PROG:
764		/*
765		 * This RELIES on the information that, in the call body,
766		 * the program number field is the fourth field from the
767		 * begining of the RPC header. MUST be changed if the
768		 * call_struct is changed
769		 */
770		*(u_int32_t *)info =
771		    ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
772		    3 * BYTES_PER_XDR_UNIT));
773		break;
774
775	case CLSET_PROG:
776		*(u_int32_t *)(void *)(cu->cu_outhdr + 3 * BYTES_PER_XDR_UNIT)
777			= htonl(*(u_int32_t *)info);
778		break;
779	case CLSET_ASYNC:
780		cu->cu_async = *(int *)info;
781		break;
782	case CLSET_CONNECT:
783		cu->cu_connect = *(int *)info;
784		break;
785	default:
786		release_fd_lock(cu->cu_fd, mask);
787		return (FALSE);
788	}
789	release_fd_lock(cu->cu_fd, mask);
790	return (TRUE);
791}
792
793static void
794clnt_dg_destroy(cl)
795	CLIENT *cl;
796{
797	struct cu_data *cu = (struct cu_data *)cl->cl_private;
798	int cu_fd = cu->cu_fd;
799	sigset_t mask;
800	sigset_t newmask;
801
802	sigfillset(&newmask);
803	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
804	mutex_lock(&clnt_fd_lock);
805	while (dg_fd_locks[cu_fd])
806		cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
807	if (cu->cu_closeit)
808		(void)_close(cu_fd);
809	if (cu->cu_kq >= 0)
810		_close(cu->cu_kq);
811	XDR_DESTROY(&(cu->cu_outxdrs));
812	mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
813	if (cl->cl_netid && cl->cl_netid[0])
814		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
815	if (cl->cl_tp && cl->cl_tp[0])
816		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
817	mem_free(cl, sizeof (CLIENT));
818	mutex_unlock(&clnt_fd_lock);
819	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
820	cond_signal(&dg_cv[cu_fd]);
821}
822
823static struct clnt_ops *
824clnt_dg_ops()
825{
826	static struct clnt_ops ops;
827	sigset_t mask;
828	sigset_t newmask;
829
830/* VARIABLES PROTECTED BY ops_lock: ops */
831
832	sigfillset(&newmask);
833	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
834	mutex_lock(&ops_lock);
835	if (ops.cl_call == NULL) {
836		ops.cl_call = clnt_dg_call;
837		ops.cl_abort = clnt_dg_abort;
838		ops.cl_geterr = clnt_dg_geterr;
839		ops.cl_freeres = clnt_dg_freeres;
840		ops.cl_destroy = clnt_dg_destroy;
841		ops.cl_control = clnt_dg_control;
842	}
843	mutex_unlock(&ops_lock);
844	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
845	return (&ops);
846}
847
848/*
849 * Make sure that the time is not garbage.  -1 value is allowed.
850 */
851static bool_t
852time_not_ok(t)
853	struct timeval *t;
854{
855	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
856		t->tv_usec < -1 || t->tv_usec > 1000000);
857}
858
859