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