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