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