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