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