clnt_dg.c revision 1.25.4.1 1 /* $NetBSD: clnt_dg.c,v 1.25.4.1 2013/03/14 22:03:08 riz Exp $ */
2
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 */
36
37 /* #ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
43 #else
44 __RCSID("$NetBSD: clnt_dg.c,v 1.25.4.1 2013/03/14 22:03:08 riz Exp $");
45 #endif
46 #endif
47
48 /*
49 * Implements a connectionless client side RPC.
50 */
51
52 #include "namespace.h"
53 #include "reentrant.h"
54 #include <sys/poll.h>
55 #include <sys/types.h>
56 #include <sys/time.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59 #include <rpc/rpc.h>
60 #include <assert.h>
61 #include <errno.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <signal.h>
65 #include <unistd.h>
66 #include <err.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 __P((void));
77 static bool_t time_not_ok __P((struct timeval *));
78 static enum clnt_stat clnt_dg_call __P((CLIENT *, rpcproc_t, xdrproc_t,
79 const char *, xdrproc_t, caddr_t, struct timeval));
80 static void clnt_dg_geterr __P((CLIENT *, struct rpc_err *));
81 static bool_t clnt_dg_freeres __P((CLIENT *, xdrproc_t, caddr_t));
82 static void clnt_dg_abort __P((CLIENT *));
83 static bool_t clnt_dg_control __P((CLIENT *, u_int, char *));
84 static void clnt_dg_destroy __P((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(fd, svcaddr, program, version, sendsz, recvsz)
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 cu->cu_wait.tv_sec = 15; /* heuristically chosen */
253 cu->cu_wait.tv_usec = 0;
254 cu->cu_total.tv_sec = -1;
255 cu->cu_total.tv_usec = -1;
256 cu->cu_sendsz = sendsz;
257 cu->cu_recvsz = recvsz;
258 call_msg.rm_xid = __RPC_GETXID();
259 call_msg.rm_call.cb_prog = program;
260 call_msg.rm_call.cb_vers = version;
261 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
262 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
263 rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */
264 rpc_createerr.cf_error.re_errno = 0;
265 goto err2;
266 }
267 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
268
269 /* XXX fvdl - do we still want this? */
270 #if 0
271 (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
272 #endif
273 ioctl(fd, FIONBIO, (char *)(void *)&one);
274
275 /*
276 * By default, closeit is always FALSE. It is users responsibility
277 * to do a close on it, else the user may use clnt_control
278 * to let clnt_destroy do it for him/her.
279 */
280 cu->cu_closeit = FALSE;
281 cu->cu_fd = fd;
282 cu->cu_pfdp.fd = cu->cu_fd;
283 cu->cu_pfdp.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
284 cl->cl_ops = clnt_dg_ops();
285 cl->cl_private = (caddr_t)(void *)cu;
286 cl->cl_auth = authnone_create();
287 cl->cl_tp = NULL;
288 cl->cl_netid = NULL;
289 return (cl);
290 err1:
291 warnx(mem_err_clnt_dg);
292 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
293 rpc_createerr.cf_error.re_errno = errno;
294 err2:
295 if (cl) {
296 mem_free(cl, sizeof (CLIENT));
297 if (cu)
298 mem_free(cu, sizeof (*cu) + sendsz + recvsz);
299 }
300 return (NULL);
301 }
302
303 static enum clnt_stat
304 clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
305 CLIENT *cl; /* client handle */
306 rpcproc_t proc; /* procedure number */
307 xdrproc_t xargs; /* xdr routine for args */
308 const char * argsp; /* pointer to args */
309 xdrproc_t xresults; /* xdr routine for results */
310 caddr_t resultsp; /* pointer to results */
311 struct timeval utimeout; /* seconds to wait before giving up */
312 {
313 struct cu_data *cu;
314 XDR *xdrs;
315 size_t outlen;
316 struct rpc_msg reply_msg;
317 XDR reply_xdrs;
318 bool_t ok;
319 int nrefreshes = 2; /* number of times to refresh cred */
320 struct timeval timeout;
321 struct timeval retransmit_time;
322 struct timeval next_sendtime, starttime, time_waited, tv;
323 #ifdef _REENTRANT
324 sigset_t mask, *maskp = &mask;
325 #else
326 sigset_t *maskp = NULL;
327 #endif
328 sigset_t newmask;
329 ssize_t recvlen = 0;
330 struct timespec ts;
331 int n;
332
333 _DIAGASSERT(cl != NULL);
334
335 cu = (struct cu_data *)cl->cl_private;
336
337 sigfillset(&newmask);
338 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
339 mutex_lock(&clnt_fd_lock);
340 while (dg_fd_locks[cu->cu_fd])
341 cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
342 dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
343 mutex_unlock(&clnt_fd_lock);
344 if (cu->cu_total.tv_usec == -1) {
345 timeout = utimeout; /* use supplied timeout */
346 } else {
347 timeout = cu->cu_total; /* use default timeout */
348 }
349
350 time_waited.tv_sec = 0;
351 time_waited.tv_usec = 0;
352 retransmit_time = next_sendtime = cu->cu_wait;
353 gettimeofday(&starttime, NULL);
354
355 call_again:
356 xdrs = &(cu->cu_outxdrs);
357 xdrs->x_op = XDR_ENCODE;
358 XDR_SETPOS(xdrs, cu->cu_xdrpos);
359 /*
360 * the transaction is the first thing in the out buffer
361 */
362 (*(u_int32_t *)(void *)(cu->cu_outbuf))++;
363 if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
364 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
365 (! (*xargs)(xdrs, __UNCONST(argsp)))) {
366 cu->cu_error.re_status = RPC_CANTENCODEARGS;
367 goto out;
368 }
369 outlen = (size_t)XDR_GETPOS(xdrs);
370
371 send_again:
372 if ((size_t)sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0,
373 (struct sockaddr *)(void *)&cu->cu_raddr, (socklen_t)cu->cu_rlen)
374 != outlen) {
375 cu->cu_error.re_errno = errno;
376 cu->cu_error.re_status = RPC_CANTSEND;
377 goto out;
378 }
379
380 /*
381 * Hack to provide rpc-based message passing
382 */
383 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
384 cu->cu_error.re_status = RPC_TIMEDOUT;
385 goto out;
386 }
387 /*
388 * sub-optimal code appears here because we have
389 * some clock time to spare while the packets are in flight.
390 * (We assume that this is actually only executed once.)
391 */
392 reply_msg.acpted_rply.ar_verf = _null_auth;
393 reply_msg.acpted_rply.ar_results.where = resultsp;
394 reply_msg.acpted_rply.ar_results.proc = xresults;
395
396
397 for (;;) {
398 /* Decide how long to wait. */
399 if (timercmp(&next_sendtime, &timeout, <))
400 timersub(&next_sendtime, &time_waited, &tv);
401 else
402 timersub(&timeout, &time_waited, &tv);
403 if (tv.tv_sec < 0 || tv.tv_usec < 0)
404 tv.tv_sec = tv.tv_usec = 0;
405 TIMEVAL_TO_TIMESPEC(&tv, &ts);
406
407 n = pollts(&cu->cu_pfdp, 1, &ts, maskp);
408 if (n == 1) {
409 /* We have some data now */
410 do {
411 recvlen = recvfrom(cu->cu_fd, cu->cu_inbuf,
412 cu->cu_recvsz, 0, NULL, NULL);
413 } while (recvlen < 0 && errno == EINTR);
414
415 if (recvlen < 0 && errno != EWOULDBLOCK) {
416 cu->cu_error.re_errno = errno;
417 cu->cu_error.re_status = RPC_CANTRECV;
418 goto out;
419 }
420 if (recvlen >= (ssize_t)sizeof(uint32_t)) {
421 if (memcmp(cu->cu_inbuf, cu->cu_outbuf,
422 sizeof(uint32_t)) == 0)
423 /* Assume we have the proper reply. */
424 break;
425 }
426 }
427 if (n == -1) {
428 cu->cu_error.re_errno = errno;
429 cu->cu_error.re_status = RPC_CANTRECV;
430 goto out;
431 }
432
433 gettimeofday(&tv, NULL);
434 timersub(&tv, &starttime, &time_waited);
435
436 /* Check for timeout. */
437 if (timercmp(&time_waited, &timeout, >)) {
438 cu->cu_error.re_status = RPC_TIMEDOUT;
439 goto out;
440 }
441
442 /* Retransmit if necessary. */
443 if (timercmp(&time_waited, &next_sendtime, >)) {
444 /* update retransmit_time */
445 if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
446 timeradd(&retransmit_time, &retransmit_time,
447 &retransmit_time);
448 timeradd(&next_sendtime, &retransmit_time,
449 &next_sendtime);
450 goto send_again;
451 }
452 }
453
454 /*
455 * now decode and validate the response
456 */
457
458 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
459 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
460 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
461 if (ok) {
462 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
463 (reply_msg.acpted_rply.ar_stat == SUCCESS))
464 cu->cu_error.re_status = RPC_SUCCESS;
465 else
466 _seterr_reply(&reply_msg, &(cu->cu_error));
467
468 if (cu->cu_error.re_status == RPC_SUCCESS) {
469 if (! AUTH_VALIDATE(cl->cl_auth,
470 &reply_msg.acpted_rply.ar_verf)) {
471 cu->cu_error.re_status = RPC_AUTHERROR;
472 cu->cu_error.re_why = AUTH_INVALIDRESP;
473 }
474 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
475 xdrs->x_op = XDR_FREE;
476 (void) xdr_opaque_auth(xdrs,
477 &(reply_msg.acpted_rply.ar_verf));
478 }
479 } /* end successful completion */
480 /*
481 * If unsuccesful AND error is an authentication error
482 * then refresh credentials and try again, else break
483 */
484 else if (cu->cu_error.re_status == RPC_AUTHERROR)
485 /* maybe our credentials need to be refreshed ... */
486 if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
487 nrefreshes--;
488 goto call_again;
489 }
490 /* end of unsuccessful completion */
491 } /* end of valid reply message */
492 else {
493 cu->cu_error.re_status = RPC_CANTDECODERES;
494
495 }
496 out:
497 release_fd_lock(cu->cu_fd, mask);
498 return (cu->cu_error.re_status);
499 }
500
501 static void
502 clnt_dg_geterr(cl, errp)
503 CLIENT *cl;
504 struct rpc_err *errp;
505 {
506 struct cu_data *cu;
507
508 _DIAGASSERT(cl != NULL);
509 _DIAGASSERT(errp != NULL);
510
511 cu = (struct cu_data *)cl->cl_private;
512 *errp = cu->cu_error;
513 }
514
515 static bool_t
516 clnt_dg_freeres(cl, xdr_res, res_ptr)
517 CLIENT *cl;
518 xdrproc_t xdr_res;
519 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(h)
549 CLIENT *h;
550 {
551 }
552
553 static bool_t
554 clnt_dg_control(cl, request, info)
555 CLIENT *cl;
556 u_int request;
557 char *info;
558 {
559 struct cu_data *cu;
560 struct netbuf *addr;
561 #ifdef _REENTRANT
562 sigset_t mask;
563 #endif
564 sigset_t newmask;
565
566 _DIAGASSERT(cl != NULL);
567 /* info is handled below */
568
569 cu = (struct cu_data *)cl->cl_private;
570
571 sigfillset(&newmask);
572 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
573 mutex_lock(&clnt_fd_lock);
574 while (dg_fd_locks[cu->cu_fd])
575 cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
576 dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
577 mutex_unlock(&clnt_fd_lock);
578 switch (request) {
579 case CLSET_FD_CLOSE:
580 cu->cu_closeit = TRUE;
581 release_fd_lock(cu->cu_fd, mask);
582 return (TRUE);
583 case CLSET_FD_NCLOSE:
584 cu->cu_closeit = FALSE;
585 release_fd_lock(cu->cu_fd, mask);
586 return (TRUE);
587 }
588
589 /* for other requests which use info */
590 if (info == NULL) {
591 release_fd_lock(cu->cu_fd, mask);
592 return (FALSE);
593 }
594 switch (request) {
595 case CLSET_TIMEOUT:
596 if (time_not_ok((struct timeval *)(void *)info)) {
597 release_fd_lock(cu->cu_fd, mask);
598 return (FALSE);
599 }
600 cu->cu_total = *(struct timeval *)(void *)info;
601 break;
602 case CLGET_TIMEOUT:
603 *(struct timeval *)(void *)info = cu->cu_total;
604 break;
605 case CLGET_SERVER_ADDR: /* Give him the fd address */
606 /* Now obsolete. Only for backward compatibility */
607 (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
608 break;
609 case CLSET_RETRY_TIMEOUT:
610 if (time_not_ok((struct timeval *)(void *)info)) {
611 release_fd_lock(cu->cu_fd, mask);
612 return (FALSE);
613 }
614 cu->cu_wait = *(struct timeval *)(void *)info;
615 break;
616 case CLGET_RETRY_TIMEOUT:
617 *(struct timeval *)(void *)info = cu->cu_wait;
618 break;
619 case CLGET_FD:
620 *(int *)(void *)info = cu->cu_fd;
621 break;
622 case CLGET_SVC_ADDR:
623 addr = (struct netbuf *)(void *)info;
624 addr->buf = &cu->cu_raddr;
625 addr->len = cu->cu_rlen;
626 addr->maxlen = sizeof cu->cu_raddr;
627 break;
628 case CLSET_SVC_ADDR: /* set to new address */
629 addr = (struct netbuf *)(void *)info;
630 if (addr->len < sizeof cu->cu_raddr) {
631 release_fd_lock(cu->cu_fd, mask);
632 return (FALSE);
633 }
634 (void) memcpy(&cu->cu_raddr, addr->buf, (size_t)addr->len);
635 cu->cu_rlen = addr->len;
636 break;
637 case CLGET_XID:
638 /*
639 * use the knowledge that xid is the
640 * first element in the call structure *.
641 * This will get the xid of the PREVIOUS call
642 */
643 *(u_int32_t *)(void *)info =
644 ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
645 break;
646
647 case CLSET_XID:
648 /* This will set the xid of the NEXT call */
649 *(u_int32_t *)(void *)cu->cu_outbuf =
650 htonl(*(u_int32_t *)(void *)info - 1);
651 /* decrement by 1 as clnt_dg_call() increments once */
652 break;
653
654 case CLGET_VERS:
655 /*
656 * This RELIES on the information that, in the call body,
657 * the version number field is the fifth field from the
658 * begining of the RPC header. MUST be changed if the
659 * call_struct is changed
660 */
661 *(u_int32_t *)(void *)info =
662 ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
663 4 * BYTES_PER_XDR_UNIT));
664 break;
665
666 case CLSET_VERS:
667 *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
668 = htonl(*(u_int32_t *)(void *)info);
669 break;
670
671 case CLGET_PROG:
672 /*
673 * This RELIES on the information that, in the call body,
674 * the program number field is the fourth field from the
675 * begining of the RPC header. MUST be changed if the
676 * call_struct is changed
677 */
678 *(u_int32_t *)(void *)info =
679 ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
680 3 * BYTES_PER_XDR_UNIT));
681 break;
682
683 case CLSET_PROG:
684 *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
685 = htonl(*(u_int32_t *)(void *)info);
686 break;
687
688 default:
689 release_fd_lock(cu->cu_fd, mask);
690 return (FALSE);
691 }
692 release_fd_lock(cu->cu_fd, mask);
693 return (TRUE);
694 }
695
696 static void
697 clnt_dg_destroy(cl)
698 CLIENT *cl;
699 {
700 struct cu_data *cu;
701 int cu_fd;
702 #ifdef _REENTRANT
703 sigset_t mask;
704 #endif
705 sigset_t newmask;
706
707 _DIAGASSERT(cl != NULL);
708
709 cu = (struct cu_data *)cl->cl_private;
710 cu_fd = cu->cu_fd;
711
712 sigfillset(&newmask);
713 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
714 mutex_lock(&clnt_fd_lock);
715 while (dg_fd_locks[cu_fd])
716 cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
717 if (cu->cu_closeit)
718 (void) close(cu_fd);
719 XDR_DESTROY(&(cu->cu_outxdrs));
720 mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
721 if (cl->cl_netid && cl->cl_netid[0])
722 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
723 if (cl->cl_tp && cl->cl_tp[0])
724 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
725 mem_free(cl, sizeof (CLIENT));
726 mutex_unlock(&clnt_fd_lock);
727 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
728 cond_signal(&dg_cv[cu_fd]);
729 }
730
731 static struct clnt_ops *
732 clnt_dg_ops()
733 {
734 static struct clnt_ops ops;
735 #ifdef _REENTRANT
736 extern mutex_t ops_lock;
737 sigset_t mask;
738 #endif
739 sigset_t newmask;
740
741 /* VARIABLES PROTECTED BY ops_lock: ops */
742
743 sigfillset(&newmask);
744 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
745 mutex_lock(&ops_lock);
746 if (ops.cl_call == NULL) {
747 ops.cl_call = clnt_dg_call;
748 ops.cl_abort = clnt_dg_abort;
749 ops.cl_geterr = clnt_dg_geterr;
750 ops.cl_freeres = clnt_dg_freeres;
751 ops.cl_destroy = clnt_dg_destroy;
752 ops.cl_control = clnt_dg_control;
753 }
754 mutex_unlock(&ops_lock);
755 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
756 return (&ops);
757 }
758
759 /*
760 * Make sure that the time is not garbage. -1 value is allowed.
761 */
762 static bool_t
763 time_not_ok(t)
764 struct timeval *t;
765 {
766
767 _DIAGASSERT(t != NULL);
768
769 return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
770 t->tv_usec < -1 || t->tv_usec > 1000000);
771 }
772