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