]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/rpc/clnt_dg.c
zfs: merge openzfs/zfs@a94860a6d
[FreeBSD/FreeBSD.git] / lib / libc / rpc / clnt_dg.c
1 /*      $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without 
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice, 
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice, 
14  *   this list of conditions and the following disclaimer in the documentation 
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, 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 "AS IS" 
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc. 
34  */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #ident  "@(#)clnt_dg.c  1.23    94/04/22 SMI"
38 static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
39 #endif
40 /*
41  * Implements a connectionless client side RPC.
42  */
43
44 #include "namespace.h"
45 #include "reentrant.h"
46 #include <sys/types.h>
47 #include <sys/event.h>
48 #include <sys/time.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <sys/tree.h>
52 #include <arpa/inet.h>
53 #include <rpc/rpc.h>
54 #include <rpc/rpcsec_gss.h>
55 #include <assert.h>
56 #include <errno.h>
57 #include <pthread.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <signal.h>
61 #include <stdbool.h>
62 #include <unistd.h>
63 #include <err.h>
64 #include "un-namespace.h"
65 #include "rpc_com.h"
66 #include "mt_misc.h"
67
68
69 #ifdef _FREEFALL_CONFIG
70 /*
71  * Disable RPC exponential back-off for FreeBSD.org systems.
72  */
73 #define RPC_MAX_BACKOFF         1 /* second */
74 #else
75 #define RPC_MAX_BACKOFF         30 /* seconds */
76 #endif
77
78
79 static struct clnt_ops *clnt_dg_ops(void);
80 static bool_t time_not_ok(struct timeval *);
81 static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
82             xdrproc_t, void *, struct timeval);
83 static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
84 static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
85 static void clnt_dg_abort(CLIENT *);
86 static bool_t clnt_dg_control(CLIENT *, u_int, void *);
87 static void clnt_dg_destroy(CLIENT *);
88
89
90
91
92 /*
93  *      This machinery implements per-fd locks for MT-safety.  It is not
94  *      sufficient to do per-CLIENT handle locks for MT-safety because a
95  *      user may create more than one CLIENT handle with the same fd behind
96  *      it.  Therefore, we allocate an associative array of flags and condition
97  *      variables (dg_fd).  The flags and the array are protected by the
98  *      clnt_fd_lock mutex.  dg_fd[fd].lock == 1 => a call is active on some
99  *      CLIENT handle created for that fd.  The current implementation holds
100  *      locks across the entire RPC and reply, including retransmissions.  Yes,
101  *      this is silly, and as soon as this code is proven to work, this should
102  *      be the first thing fixed.  One step at a time.
103  */
104 struct dg_fd {
105         RB_ENTRY(dg_fd) dg_link;
106         int fd;
107         mutex_t mtx;
108 };
109 static inline int
110 cmp_dg_fd(struct dg_fd *a, struct dg_fd *b)
111 {
112         if (a->fd > b->fd) {
113                 return (1);
114         } else if (a->fd < b->fd) {
115                 return (-1);
116         } else {
117                 return (0);
118         }
119 }
120 RB_HEAD(dg_fd_list, dg_fd);
121 RB_PROTOTYPE(dg_fd_list, dg_fd, dg_link, cmp_dg_fd);
122 RB_GENERATE(dg_fd_list, dg_fd, dg_link, cmp_dg_fd);
123 struct dg_fd_list dg_fd_head = RB_INITIALIZER(&dg_fd_head);
124
125 /*
126  * Find the lock structure for the given file descriptor, or initialize it if
127  * it does not already exist.  The clnt_fd_lock mutex must be held.
128  */
129 static struct dg_fd *
130 dg_fd_find(int fd)
131 {
132         struct dg_fd key, *elem;
133
134         key.fd = fd;
135         elem = RB_FIND(dg_fd_list, &dg_fd_head, &key);
136         if (elem == NULL) {
137                 elem = calloc(1, sizeof(*elem));
138                 elem->fd = fd;
139                 mutex_init(&elem->mtx, NULL);
140                 RB_INSERT(dg_fd_list, &dg_fd_head, elem);
141         }
142         return (elem);
143 }
144
145 static void
146 release_fd_lock(struct dg_fd *elem, sigset_t mask)
147 {
148         mutex_unlock(&elem->mtx);
149         thr_sigsetmask(SIG_SETMASK, &mask, NULL);
150 }
151
152 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
153
154 /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd */
155
156 #define MCALL_MSG_SIZE 24
157
158 /*
159  * Private data kept per client handle
160  */
161 struct cu_data {
162         int                     cu_fd;          /* connections fd */
163         bool_t                  cu_closeit;     /* opened by library */
164         struct sockaddr_storage cu_raddr;       /* remote address */
165         int                     cu_rlen;
166         struct timeval          cu_wait;        /* retransmit interval */
167         struct timeval          cu_total;       /* total time for the call */
168         struct rpc_err          cu_error;
169         XDR                     cu_outxdrs;
170         u_int                   cu_xdrpos;
171         u_int                   cu_sendsz;      /* send size */
172         char                    cu_outhdr[MCALL_MSG_SIZE];
173         char                    *cu_outbuf;
174         u_int                   cu_recvsz;      /* recv size */
175         int                     cu_async;
176         int                     cu_connect;     /* Use connect(). */
177         int                     cu_connected;   /* Have done connect(). */
178         struct kevent           cu_kin;
179         int                     cu_kq;
180         char                    cu_inbuf[1];
181 };
182
183 /*
184  * Connection less client creation returns with client handle parameters.
185  * Default options are set, which the user can change using clnt_control().
186  * fd should be open and bound.
187  * NB: The rpch->cl_auth is initialized to null authentication.
188  *      Caller may wish to set this something more useful.
189  *
190  * sendsz and recvsz are the maximum allowable packet sizes that can be
191  * sent and received. Normally they are the same, but they can be
192  * changed to improve the program efficiency and buffer allocation.
193  * If they are 0, use the transport default.
194  *
195  * If svcaddr is NULL, returns NULL.
196  *
197  * fd      - open file descriptor
198  * svcaddr - servers address
199  * program - program number
200  * version - version number
201  * sendsz  - buffer recv size
202  * recvsz  - buffer send size
203  */
204 CLIENT *
205 clnt_dg_create(int fd, const struct netbuf *svcaddr, rpcprog_t program,
206     rpcvers_t version, u_int sendsz, u_int recvsz)
207 {
208         CLIENT *cl = NULL;              /* client handle */
209         struct cu_data *cu = NULL;      /* private data */
210         struct timeval now;
211         struct rpc_msg call_msg;
212         struct __rpc_sockinfo si;
213         int one = 1;
214
215         if (svcaddr == NULL) {
216                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
217                 return (NULL);
218         }
219
220         if (!__rpc_fd2sockinfo(fd, &si)) {
221                 rpc_createerr.cf_stat = RPC_TLIERROR;
222                 rpc_createerr.cf_error.re_errno = 0;
223                 return (NULL);
224         }
225         /*
226          * Find the receive and the send size
227          */
228         sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
229         recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
230         if ((sendsz == 0) || (recvsz == 0)) {
231                 rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
232                 rpc_createerr.cf_error.re_errno = 0;
233                 return (NULL);
234         }
235
236         if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
237                 goto err1;
238         /*
239          * Should be multiple of 4 for XDR.
240          */
241         sendsz = ((sendsz + 3) / 4) * 4;
242         recvsz = ((recvsz + 3) / 4) * 4;
243         cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
244         if (cu == NULL)
245                 goto err1;
246         (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
247         cu->cu_rlen = svcaddr->len;
248         cu->cu_outbuf = &cu->cu_inbuf[recvsz];
249         /* Other values can also be set through clnt_control() */
250         cu->cu_wait.tv_sec = 15;        /* heuristically chosen */
251         cu->cu_wait.tv_usec = 0;
252         cu->cu_total.tv_sec = -1;
253         cu->cu_total.tv_usec = -1;
254         cu->cu_sendsz = sendsz;
255         cu->cu_recvsz = recvsz;
256         cu->cu_async = FALSE;
257         cu->cu_connect = FALSE;
258         cu->cu_connected = FALSE;
259         (void) gettimeofday(&now, NULL);
260         call_msg.rm_xid = __RPC_GETXID(&now);
261         call_msg.rm_call.cb_prog = program;
262         call_msg.rm_call.cb_vers = version;
263         xdrmem_create(&(cu->cu_outxdrs), cu->cu_outhdr, MCALL_MSG_SIZE,
264             XDR_ENCODE);
265         if (! xdr_callhdr(&cu->cu_outxdrs, &call_msg)) {
266                 rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
267                 rpc_createerr.cf_error.re_errno = 0;
268                 goto err2;
269         }
270         cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
271         XDR_DESTROY(&cu->cu_outxdrs);
272         xdrmem_create(&cu->cu_outxdrs, cu->cu_outbuf, sendsz, XDR_ENCODE);
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         cl->cl_ops = clnt_dg_ops();
288         cl->cl_private = (caddr_t)(void *)cu;
289         cl->cl_auth = authnone_create();
290         cl->cl_tp = NULL;
291         cl->cl_netid = NULL;
292         cu->cu_kq = -1;
293         EV_SET(&cu->cu_kin, cu->cu_fd, EVFILT_READ, EV_ADD, 0, 0, 0);
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 /*
309  * cl       - client handle
310  * proc     - procedure number
311  * xargs    - xdr routine for args
312  * argsp    - pointer to args
313  * xresults - xdr routine for results
314  * resultsp - pointer to results
315  * utimeout - seconds to wait before giving up
316  */
317 static enum clnt_stat
318 clnt_dg_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xargs, void *argsp,
319     xdrproc_t xresults, void *resultsp, struct timeval utimeout)
320 {
321         struct cu_data *cu = (struct cu_data *)cl->cl_private;
322         XDR *xdrs;
323         size_t outlen = 0;
324         struct rpc_msg reply_msg;
325         XDR reply_xdrs;
326         bool_t ok;
327         int nrefreshes = 2;             /* number of times to refresh cred */
328         int nretries = 0;               /* number of times we retransmitted */
329         struct timeval timeout;
330         struct timeval retransmit_time;
331         struct timeval next_sendtime, starttime, time_waited, tv;
332         struct timespec ts;
333         struct kevent kv;
334         struct sockaddr *sa;
335         struct dg_fd *elem;
336         sigset_t mask;
337         sigset_t newmask;
338         socklen_t salen;
339         ssize_t recvlen = 0;
340         int kin_len, n;
341         u_int32_t xid;
342
343         outlen = 0;
344         sigfillset(&newmask);
345         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
346         mutex_lock(&clnt_fd_lock);
347         elem = dg_fd_find(cu->cu_fd);
348         mutex_unlock(&clnt_fd_lock);
349         mutex_lock(&elem->mtx);
350         if (cu->cu_total.tv_usec == -1) {
351                 timeout = utimeout;     /* use supplied timeout */
352         } else {
353                 timeout = cu->cu_total; /* use default timeout */
354         }
355
356         if (cu->cu_connect && !cu->cu_connected) {
357                 if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr,
358                     cu->cu_rlen) < 0) {
359                         cu->cu_error.re_errno = errno;
360                         cu->cu_error.re_status = RPC_CANTSEND;
361                         goto out;
362                 }
363                 cu->cu_connected = 1;
364         }
365         if (cu->cu_connected) {
366                 sa = NULL;
367                 salen = 0;
368         } else {
369                 sa = (struct sockaddr *)&cu->cu_raddr;
370                 salen = cu->cu_rlen;
371         }
372         time_waited.tv_sec = 0;
373         time_waited.tv_usec = 0;
374         retransmit_time = next_sendtime = cu->cu_wait;
375         gettimeofday(&starttime, NULL);
376
377         /* Clean up in case the last call ended in a longjmp(3) call. */
378         if (cu->cu_kq >= 0)
379                 _close(cu->cu_kq);
380         if ((cu->cu_kq = kqueue()) < 0) {
381                 cu->cu_error.re_errno = errno;
382                 cu->cu_error.re_status = RPC_CANTSEND;
383                 goto out;
384         }
385         kin_len = 1;
386
387 call_again:
388         if (cu->cu_async == TRUE && xargs == NULL)
389                 goto get_reply;
390         /*
391          * the transaction is the first thing in the out buffer
392          * XXX Yes, and it's in network byte order, so we should to
393          * be careful when we increment it, shouldn't we.
394          */
395         xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr));
396         xid++;
397         *(u_int32_t *)(void *)(cu->cu_outhdr) = htonl(xid);
398 call_again_same_xid:
399         xdrs = &(cu->cu_outxdrs);
400         xdrs->x_op = XDR_ENCODE;
401         XDR_SETPOS(xdrs, 0);
402
403         if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
404                 if ((! XDR_PUTBYTES(xdrs, cu->cu_outhdr, cu->cu_xdrpos)) ||
405                     (! XDR_PUTINT32(xdrs, &proc)) ||
406                     (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
407                     (! (*xargs)(xdrs, argsp))) {
408                         cu->cu_error.re_status = RPC_CANTENCODEARGS;
409                         goto out;
410                 }
411         } else {
412                 *(uint32_t *) &cu->cu_outhdr[cu->cu_xdrpos] = htonl(proc);
413                 if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outhdr,
414                         cu->cu_xdrpos + sizeof(uint32_t),
415                         xdrs, xargs, argsp)) {
416                         cu->cu_error.re_status = RPC_CANTENCODEARGS;
417                         goto out;
418                 }
419         }
420         outlen = (size_t)XDR_GETPOS(xdrs);
421
422 send_again:
423         if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) {
424                 cu->cu_error.re_errno = errno;
425                 cu->cu_error.re_status = RPC_CANTSEND;
426                 goto out;
427         }
428
429         /*
430          * Hack to provide rpc-based message passing
431          */
432         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
433                 cu->cu_error.re_status = RPC_TIMEDOUT;
434                 goto out;
435         }
436
437 get_reply:
438
439         /*
440          * sub-optimal code appears here because we have
441          * some clock time to spare while the packets are in flight.
442          * (We assume that this is actually only executed once.)
443          */
444         reply_msg.acpted_rply.ar_verf = _null_auth;
445         if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
446                 reply_msg.acpted_rply.ar_results.where = resultsp;
447                 reply_msg.acpted_rply.ar_results.proc = xresults;
448         } else {
449                 reply_msg.acpted_rply.ar_results.where = NULL;
450                 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
451         }
452
453         for (;;) {
454                 /* Decide how long to wait. */
455                 if (timercmp(&next_sendtime, &timeout, <))
456                         timersub(&next_sendtime, &time_waited, &tv);
457                 else
458                         timersub(&timeout, &time_waited, &tv);
459                 if (tv.tv_sec < 0 || tv.tv_usec < 0)
460                         tv.tv_sec = tv.tv_usec = 0;
461                 TIMEVAL_TO_TIMESPEC(&tv, &ts);
462
463                 n = _kevent(cu->cu_kq, &cu->cu_kin, kin_len, &kv, 1, &ts);
464                 /* We don't need to register the event again. */
465                 kin_len = 0;
466
467                 if (n == 1) {
468                         if (kv.flags & EV_ERROR) {
469                                 cu->cu_error.re_errno = kv.data;
470                                 cu->cu_error.re_status = RPC_CANTRECV;
471                                 goto out;
472                         }
473                         /* We have some data now */
474                         do {
475                                 recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf,
476                                     cu->cu_recvsz, 0, NULL, NULL);
477                         } while (recvlen < 0 && errno == EINTR);
478                         if (recvlen < 0 && errno != EWOULDBLOCK) {
479                                 cu->cu_error.re_errno = errno;
480                                 cu->cu_error.re_status = RPC_CANTRECV;
481                                 goto out;
482                         }
483                         if (recvlen >= sizeof(u_int32_t) &&
484                             (cu->cu_async == TRUE ||
485                             *((u_int32_t *)(void *)(cu->cu_inbuf)) ==
486                             *((u_int32_t *)(void *)(cu->cu_outbuf)))) {
487                                 /* We now assume we have the proper reply. */
488                                 break;
489                         }
490                 }
491                 if (n == -1 && errno != EINTR) {
492                         cu->cu_error.re_errno = errno;
493                         cu->cu_error.re_status = RPC_CANTRECV;
494                         goto out;
495                 }
496                 gettimeofday(&tv, NULL);
497                 timersub(&tv, &starttime, &time_waited);
498
499                 /* Check for timeout. */
500                 if (timercmp(&time_waited, &timeout, >)) {
501                         cu->cu_error.re_status = RPC_TIMEDOUT;
502                         goto out;
503                 }
504
505                 /* Retransmit if necessary. */          
506                 if (timercmp(&time_waited, &next_sendtime, >)) {
507                         /* update retransmit_time */
508                         if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
509                                 timeradd(&retransmit_time, &retransmit_time,
510                                     &retransmit_time);
511                         timeradd(&next_sendtime, &retransmit_time,
512                             &next_sendtime);
513                         nretries++;
514
515                         /*
516                          * When retransmitting a RPCSEC_GSS message,
517                          * we must use a new sequence number (handled
518                          * by __rpc_gss_wrap above).
519                          */
520                         if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS)
521                                 goto send_again;
522                         else
523                                 goto call_again_same_xid;
524                 }
525         }
526
527         /*
528          * now decode and validate the response
529          */
530
531         xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
532         ok = xdr_replymsg(&reply_xdrs, &reply_msg);
533         /* XDR_DESTROY(&reply_xdrs);    save a few cycles on noop destroy */
534         if (ok) {
535                 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
536                         (reply_msg.acpted_rply.ar_stat == SUCCESS))
537                         cu->cu_error.re_status = RPC_SUCCESS;
538                 else
539                         _seterr_reply(&reply_msg, &(cu->cu_error));
540
541                 if (cu->cu_error.re_status == RPC_SUCCESS) {
542                         if (! AUTH_VALIDATE(cl->cl_auth,
543                                             &reply_msg.acpted_rply.ar_verf)) {
544                                 if (nretries &&
545                                     cl->cl_auth->ah_cred.oa_flavor
546                                     == RPCSEC_GSS)
547                                         /*
548                                          * If we retransmitted, its
549                                          * possible that we will
550                                          * receive a reply for one of
551                                          * the earlier transmissions
552                                          * (which will use an older
553                                          * RPCSEC_GSS sequence
554                                          * number). In this case, just
555                                          * go back and listen for a
556                                          * new reply. We could keep a
557                                          * record of all the seq
558                                          * numbers we have transmitted
559                                          * so far so that we could
560                                          * accept a reply for any of
561                                          * them here.
562                                          */
563                                         goto get_reply;
564                                 cu->cu_error.re_status = RPC_AUTHERROR;
565                                 cu->cu_error.re_why = AUTH_INVALIDRESP;
566                         } else {
567                                 if (cl->cl_auth->ah_cred.oa_flavor
568                                     == RPCSEC_GSS) {
569                                         if (!__rpc_gss_unwrap(cl->cl_auth,
570                                                 &reply_xdrs, xresults,
571                                                 resultsp))
572                                                 cu->cu_error.re_status =
573                                                         RPC_CANTDECODERES;
574                                 }
575                         }
576                         if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
577                                 xdrs->x_op = XDR_FREE;
578                                 (void) xdr_opaque_auth(xdrs,
579                                         &(reply_msg.acpted_rply.ar_verf));
580                         }
581                 }               /* end successful completion */
582                 /*
583                  * If unsuccessful AND error is an authentication error
584                  * then refresh credentials and try again, else break
585                  */
586                 else if (cu->cu_error.re_status == RPC_AUTHERROR)
587                         /* maybe our credentials need to be refreshed ... */
588                         if (nrefreshes > 0 &&
589                             AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
590                                 nrefreshes--;
591                                 goto call_again;
592                         }
593                 /* end of unsuccessful completion */
594         }       /* end of valid reply message */
595         else {
596                 cu->cu_error.re_status = RPC_CANTDECODERES;
597
598         }
599 out:
600         if (cu->cu_kq >= 0)
601                 _close(cu->cu_kq);
602         cu->cu_kq = -1;
603         release_fd_lock(elem, mask);
604         return (cu->cu_error.re_status);
605 }
606
607 static void
608 clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
609 {
610         struct cu_data *cu = (struct cu_data *)cl->cl_private;
611
612         *errp = cu->cu_error;
613 }
614
615 static bool_t
616 clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
617 {
618         struct cu_data *cu = (struct cu_data *)cl->cl_private;
619         struct dg_fd *elem;
620         XDR *xdrs = &(cu->cu_outxdrs);
621         bool_t dummy;
622         sigset_t mask;
623         sigset_t newmask;
624
625         sigfillset(&newmask);
626         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
627         mutex_lock(&clnt_fd_lock);
628         elem = dg_fd_find(cu->cu_fd);
629         mutex_lock(&elem->mtx);
630         xdrs->x_op = XDR_FREE;
631         dummy = (*xdr_res)(xdrs, res_ptr);
632         mutex_unlock(&clnt_fd_lock);
633         release_fd_lock(elem, mask);
634         return (dummy);
635 }
636
637 /*ARGSUSED*/
638 static void
639 clnt_dg_abort(CLIENT *h)
640 {
641 }
642
643 static bool_t
644 clnt_dg_control(CLIENT *cl, u_int request, void *info)
645 {
646         struct cu_data *cu = (struct cu_data *)cl->cl_private;
647         struct netbuf *addr;
648         struct dg_fd *elem;
649         sigset_t mask;
650         sigset_t newmask;
651
652         sigfillset(&newmask);
653         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
654         mutex_lock(&clnt_fd_lock);
655         elem = dg_fd_find(cu->cu_fd);
656         mutex_unlock(&clnt_fd_lock);
657         mutex_lock(&elem->mtx);
658         switch (request) {
659         case CLSET_FD_CLOSE:
660                 cu->cu_closeit = TRUE;
661                 release_fd_lock(elem, mask);
662                 return (TRUE);
663         case CLSET_FD_NCLOSE:
664                 cu->cu_closeit = FALSE;
665                 release_fd_lock(elem, mask);
666                 return (TRUE);
667         }
668
669         /* for other requests which use info */
670         if (info == NULL) {
671                 release_fd_lock(elem, mask);
672                 return (FALSE);
673         }
674         switch (request) {
675         case CLSET_TIMEOUT:
676                 if (time_not_ok((struct timeval *)info)) {
677                         release_fd_lock(elem, mask);
678                         return (FALSE);
679                 }
680                 cu->cu_total = *(struct timeval *)info;
681                 break;
682         case CLGET_TIMEOUT:
683                 *(struct timeval *)info = cu->cu_total;
684                 break;
685         case CLGET_SERVER_ADDR:         /* Give him the fd address */
686                 /* Now obsolete. Only for backward compatibility */
687                 (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
688                 break;
689         case CLSET_RETRY_TIMEOUT:
690                 if (time_not_ok((struct timeval *)info)) {
691                         release_fd_lock(elem, mask);
692                         return (FALSE);
693                 }
694                 cu->cu_wait = *(struct timeval *)info;
695                 break;
696         case CLGET_RETRY_TIMEOUT:
697                 *(struct timeval *)info = cu->cu_wait;
698                 break;
699         case CLGET_FD:
700                 *(int *)info = cu->cu_fd;
701                 break;
702         case CLGET_SVC_ADDR:
703                 addr = (struct netbuf *)info;
704                 addr->buf = &cu->cu_raddr;
705                 addr->len = cu->cu_rlen;
706                 addr->maxlen = sizeof cu->cu_raddr;
707                 break;
708         case CLSET_SVC_ADDR:            /* set to new address */
709                 addr = (struct netbuf *)info;
710                 if (addr->len < sizeof cu->cu_raddr) {
711                         release_fd_lock(elem, mask);
712                         return (FALSE);
713                 }
714                 (void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
715                 cu->cu_rlen = addr->len;
716                 break;
717         case CLGET_XID:
718                 /*
719                  * use the knowledge that xid is the
720                  * first element in the call structure *.
721                  * This will get the xid of the PREVIOUS call
722                  */
723                 *(u_int32_t *)info =
724                     ntohl(*(u_int32_t *)(void *)cu->cu_outhdr);
725                 break;
726
727         case CLSET_XID:
728                 /* This will set the xid of the NEXT call */
729                 *(u_int32_t *)(void *)cu->cu_outhdr =
730                     htonl(*(u_int32_t *)info - 1);
731                 /* decrement by 1 as clnt_dg_call() increments once */
732                 break;
733
734         case CLGET_VERS:
735                 /*
736                  * This RELIES on the information that, in the call body,
737                  * the version number field is the fifth field from the
738                  * beginning of the RPC header. MUST be changed if the
739                  * call_struct is changed
740                  */
741                 *(u_int32_t *)info =
742                     ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
743                     4 * BYTES_PER_XDR_UNIT));
744                 break;
745
746         case CLSET_VERS:
747                 *(u_int32_t *)(void *)(cu->cu_outhdr + 4 * BYTES_PER_XDR_UNIT)
748                         = htonl(*(u_int32_t *)info);
749                 break;
750
751         case CLGET_PROG:
752                 /*
753                  * This RELIES on the information that, in the call body,
754                  * the program number field is the fourth field from the
755                  * beginning of the RPC header. MUST be changed if the
756                  * call_struct is changed
757                  */
758                 *(u_int32_t *)info =
759                     ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
760                     3 * BYTES_PER_XDR_UNIT));
761                 break;
762
763         case CLSET_PROG:
764                 *(u_int32_t *)(void *)(cu->cu_outhdr + 3 * BYTES_PER_XDR_UNIT)
765                         = htonl(*(u_int32_t *)info);
766                 break;
767         case CLSET_ASYNC:
768                 cu->cu_async = *(int *)info;
769                 break;
770         case CLSET_CONNECT:
771                 cu->cu_connect = *(int *)info;
772                 break;
773         default:
774                 release_fd_lock(elem, mask);
775                 return (FALSE);
776         }
777         release_fd_lock(elem, mask);
778         return (TRUE);
779 }
780
781 static void
782 clnt_dg_destroy(CLIENT *cl)
783 {
784         struct cu_data *cu = (struct cu_data *)cl->cl_private;
785         struct dg_fd *elem;
786         int cu_fd = cu->cu_fd;
787         sigset_t mask;
788         sigset_t newmask;
789
790         sigfillset(&newmask);
791         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
792         mutex_lock(&clnt_fd_lock);
793         elem = dg_fd_find(cu_fd);
794         mutex_lock(&elem->mtx);
795         if (cu->cu_closeit)
796                 (void)_close(cu_fd);
797         if (cu->cu_kq >= 0)
798                 _close(cu->cu_kq);
799         XDR_DESTROY(&(cu->cu_outxdrs));
800         mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
801         if (cl->cl_netid && cl->cl_netid[0])
802                 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
803         if (cl->cl_tp && cl->cl_tp[0])
804                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
805         mem_free(cl, sizeof (CLIENT));
806         mutex_unlock(&clnt_fd_lock);
807         release_fd_lock(elem, mask);
808 }
809
810 static struct clnt_ops *
811 clnt_dg_ops(void)
812 {
813         static struct clnt_ops ops;
814         sigset_t mask;
815         sigset_t newmask;
816
817 /* VARIABLES PROTECTED BY ops_lock: ops */
818
819         sigfillset(&newmask);
820         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
821         mutex_lock(&ops_lock);
822         if (ops.cl_call == NULL) {
823                 ops.cl_call = clnt_dg_call;
824                 ops.cl_abort = clnt_dg_abort;
825                 ops.cl_geterr = clnt_dg_geterr;
826                 ops.cl_freeres = clnt_dg_freeres;
827                 ops.cl_destroy = clnt_dg_destroy;
828                 ops.cl_control = clnt_dg_control;
829         }
830         mutex_unlock(&ops_lock);
831         thr_sigsetmask(SIG_SETMASK, &mask, NULL);
832         return (&ops);
833 }
834
835 /*
836  * Make sure that the time is not garbage.  -1 value is allowed.
837  */
838 static bool_t
839 time_not_ok(struct timeval *t)
840 {
841         return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
842                 t->tv_usec < -1 || t->tv_usec > 1000000);
843 }
844