xref: /minix3/lib/libc/rpc/clnt_dg.c (revision 6e5a113837816c382dd4f2a25924f16fbbb1c493)
1 /*	$NetBSD: clnt_dg.c,v 1.26 2012/03/20 17:14:50 matt 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.26 2012/03/20 17:14:50 matt 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(void);
75 static bool_t time_not_ok(struct timeval *);
76 static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t,
77     const char *, xdrproc_t, caddr_t, struct timeval);
78 static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
79 static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, caddr_t);
80 static void clnt_dg_abort(CLIENT *);
81 static bool_t clnt_dg_control(CLIENT *, u_int, char *);
82 static void clnt_dg_destroy(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(
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(
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 				if (memcmp(cu->cu_inbuf, cu->cu_outbuf,
420 				    sizeof(uint32_t)) == 0)
421 					/* 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(CLIENT *cl, struct rpc_err *errp)
501 {
502 	struct cu_data *cu;
503 
504 	_DIAGASSERT(cl != NULL);
505 	_DIAGASSERT(errp != NULL);
506 
507 	cu = (struct cu_data *)cl->cl_private;
508 	*errp = cu->cu_error;
509 }
510 
511 static bool_t
512 clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
513 {
514 	struct cu_data *cu;
515 	XDR *xdrs;
516 	bool_t dummy;
517 #ifdef _REENTRANT
518 	sigset_t mask;
519 #endif
520 	sigset_t newmask;
521 
522 	_DIAGASSERT(cl != NULL);
523 	cu = (struct cu_data *)cl->cl_private;
524 	xdrs = &(cu->cu_outxdrs);
525 
526 	sigfillset(&newmask);
527 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
528 	mutex_lock(&clnt_fd_lock);
529 	while (dg_fd_locks[cu->cu_fd])
530 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
531 	xdrs->x_op = XDR_FREE;
532 	dummy = (*xdr_res)(xdrs, res_ptr);
533 	mutex_unlock(&clnt_fd_lock);
534 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
535 	cond_signal(&dg_cv[cu->cu_fd]);
536 	return (dummy);
537 }
538 
539 /*ARGSUSED*/
540 static void
541 clnt_dg_abort(CLIENT *h)
542 {
543 }
544 
545 static bool_t
546 clnt_dg_control(CLIENT *cl, u_int request, char *info)
547 {
548 	struct cu_data *cu;
549 	struct netbuf *addr;
550 #ifdef _REENTRANT
551 	sigset_t mask;
552 #endif
553 	sigset_t newmask;
554 
555 	_DIAGASSERT(cl != NULL);
556 	/* info is handled below */
557 
558 	cu = (struct cu_data *)cl->cl_private;
559 
560 	sigfillset(&newmask);
561 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
562 	mutex_lock(&clnt_fd_lock);
563 	while (dg_fd_locks[cu->cu_fd])
564 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
565 	dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
566 	mutex_unlock(&clnt_fd_lock);
567 	switch (request) {
568 	case CLSET_FD_CLOSE:
569 		cu->cu_closeit = TRUE;
570 		release_fd_lock(cu->cu_fd, mask);
571 		return (TRUE);
572 	case CLSET_FD_NCLOSE:
573 		cu->cu_closeit = FALSE;
574 		release_fd_lock(cu->cu_fd, mask);
575 		return (TRUE);
576 	}
577 
578 	/* for other requests which use info */
579 	if (info == NULL) {
580 		release_fd_lock(cu->cu_fd, mask);
581 		return (FALSE);
582 	}
583 	switch (request) {
584 	case CLSET_TIMEOUT:
585 		if (time_not_ok((struct timeval *)(void *)info)) {
586 			release_fd_lock(cu->cu_fd, mask);
587 			return (FALSE);
588 		}
589 		cu->cu_total = *(struct timeval *)(void *)info;
590 		break;
591 	case CLGET_TIMEOUT:
592 		*(struct timeval *)(void *)info = cu->cu_total;
593 		break;
594 	case CLGET_SERVER_ADDR:		/* Give him the fd address */
595 		/* Now obsolete. Only for backward compatibility */
596 		(void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
597 		break;
598 	case CLSET_RETRY_TIMEOUT:
599 		if (time_not_ok((struct timeval *)(void *)info)) {
600 			release_fd_lock(cu->cu_fd, mask);
601 			return (FALSE);
602 		}
603 		cu->cu_wait = *(struct timeval *)(void *)info;
604 		break;
605 	case CLGET_RETRY_TIMEOUT:
606 		*(struct timeval *)(void *)info = cu->cu_wait;
607 		break;
608 	case CLGET_FD:
609 		*(int *)(void *)info = cu->cu_fd;
610 		break;
611 	case CLGET_SVC_ADDR:
612 		addr = (struct netbuf *)(void *)info;
613 		addr->buf = &cu->cu_raddr;
614 		addr->len = cu->cu_rlen;
615 		addr->maxlen = sizeof cu->cu_raddr;
616 		break;
617 	case CLSET_SVC_ADDR:		/* set to new address */
618 		addr = (struct netbuf *)(void *)info;
619 		if (addr->len < sizeof cu->cu_raddr) {
620 			release_fd_lock(cu->cu_fd, mask);
621 			return (FALSE);
622 		}
623 		(void) memcpy(&cu->cu_raddr, addr->buf, (size_t)addr->len);
624 		cu->cu_rlen = addr->len;
625 		break;
626 	case CLGET_XID:
627 		/*
628 		 * use the knowledge that xid is the
629 		 * first element in the call structure *.
630 		 * This will get the xid of the PREVIOUS call
631 		 */
632 		*(u_int32_t *)(void *)info =
633 		    ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
634 		break;
635 
636 	case CLSET_XID:
637 		/* This will set the xid of the NEXT call */
638 		*(u_int32_t *)(void *)cu->cu_outbuf =
639 		    htonl(*(u_int32_t *)(void *)info - 1);
640 		/* decrement by 1 as clnt_dg_call() increments once */
641 		break;
642 
643 	case CLGET_VERS:
644 		/*
645 		 * This RELIES on the information that, in the call body,
646 		 * the version number field is the fifth field from the
647 		 * begining of the RPC header. MUST be changed if the
648 		 * call_struct is changed
649 		 */
650 		*(u_int32_t *)(void *)info =
651 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
652 		    4 * BYTES_PER_XDR_UNIT));
653 		break;
654 
655 	case CLSET_VERS:
656 		*(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
657 			= htonl(*(u_int32_t *)(void *)info);
658 		break;
659 
660 	case CLGET_PROG:
661 		/*
662 		 * This RELIES on the information that, in the call body,
663 		 * the program number field is the fourth field from the
664 		 * begining of the RPC header. MUST be changed if the
665 		 * call_struct is changed
666 		 */
667 		*(u_int32_t *)(void *)info =
668 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
669 		    3 * BYTES_PER_XDR_UNIT));
670 		break;
671 
672 	case CLSET_PROG:
673 		*(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
674 			= htonl(*(u_int32_t *)(void *)info);
675 		break;
676 
677 	default:
678 		release_fd_lock(cu->cu_fd, mask);
679 		return (FALSE);
680 	}
681 	release_fd_lock(cu->cu_fd, mask);
682 	return (TRUE);
683 }
684 
685 static void
686 clnt_dg_destroy(CLIENT *cl)
687 {
688 	struct cu_data *cu;
689 	int cu_fd;
690 #ifdef _REENTRANT
691 	sigset_t mask;
692 #endif
693 	sigset_t newmask;
694 
695 	_DIAGASSERT(cl != NULL);
696 
697 	cu = (struct cu_data *)cl->cl_private;
698 	cu_fd = cu->cu_fd;
699 
700 	sigfillset(&newmask);
701 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
702 	mutex_lock(&clnt_fd_lock);
703 	while (dg_fd_locks[cu_fd])
704 		cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
705 	if (cu->cu_closeit)
706 		(void) close(cu_fd);
707 	XDR_DESTROY(&(cu->cu_outxdrs));
708 	mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
709 	if (cl->cl_netid && cl->cl_netid[0])
710 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
711 	if (cl->cl_tp && cl->cl_tp[0])
712 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
713 	mem_free(cl, sizeof (CLIENT));
714 	mutex_unlock(&clnt_fd_lock);
715 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
716 	cond_signal(&dg_cv[cu_fd]);
717 }
718 
719 static struct clnt_ops *
720 clnt_dg_ops(void)
721 {
722 	static struct clnt_ops ops;
723 #ifdef _REENTRANT
724 	extern mutex_t	ops_lock;
725 	sigset_t mask;
726 #endif
727 	sigset_t newmask;
728 
729 /* VARIABLES PROTECTED BY ops_lock: ops */
730 
731 	sigfillset(&newmask);
732 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
733 	mutex_lock(&ops_lock);
734 	if (ops.cl_call == NULL) {
735 		ops.cl_call = clnt_dg_call;
736 		ops.cl_abort = clnt_dg_abort;
737 		ops.cl_geterr = clnt_dg_geterr;
738 		ops.cl_freeres = clnt_dg_freeres;
739 		ops.cl_destroy = clnt_dg_destroy;
740 		ops.cl_control = clnt_dg_control;
741 	}
742 	mutex_unlock(&ops_lock);
743 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
744 	return (&ops);
745 }
746 
747 /*
748  * Make sure that the time is not garbage.  -1 value is allowed.
749  */
750 static bool_t
751 time_not_ok(struct timeval *t)
752 {
753 
754 	_DIAGASSERT(t != NULL);
755 
756 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
757 		t->tv_usec < -1 || t->tv_usec > 1000000);
758 }
759