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