]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libc/rpc/clnt_vc.c
MFC r288113:
[FreeBSD/stable/10.git] / lib / libc / rpc / clnt_vc.c
1 /*      $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 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 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)clnt_tcp.c   2.2 88/08/01 4.0 RPCSRC";
34 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
35 #endif
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38  
39 /*
40  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
43  *
44  * TCP based RPC supports 'batched calls'.
45  * A sequence of calls may be batched-up in a send buffer.  The rpc call
46  * return immediately to the client even though the call was not necessarily
47  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
48  * the rpc timeout value is zero (see clnt.h, rpc).
49  *
50  * Clients should NOT casually batch calls that in fact return results; that is,
51  * the server side should be aware that a call is batched and not produce any
52  * return message.  Batched calls that produce many result messages can
53  * deadlock (netlock) the client and the server....
54  *
55  * Now go hang yourself.
56  */
57
58 #include "namespace.h"
59 #include "reentrant.h"
60 #include <sys/types.h>
61 #include <sys/poll.h>
62 #include <sys/syslog.h>
63 #include <sys/socket.h>
64 #include <sys/un.h>
65 #include <sys/uio.h>
66
67 #include <arpa/inet.h>
68 #include <assert.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <netdb.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76 #include <signal.h>
77
78 #include <rpc/rpc.h>
79 #include <rpc/rpcsec_gss.h>
80 #include "un-namespace.h"
81 #include "rpc_com.h"
82 #include "mt_misc.h"
83
84 #define MCALL_MSG_SIZE 24
85
86 struct cmessage {
87         struct cmsghdr cmsg;
88         struct cmsgcred cmcred;
89 };
90
91 static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
92     xdrproc_t, void *, struct timeval);
93 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
94 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
95 static void clnt_vc_abort(CLIENT *);
96 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
97 static void clnt_vc_destroy(CLIENT *);
98 static struct clnt_ops *clnt_vc_ops(void);
99 static bool_t time_not_ok(struct timeval *);
100 static int read_vc(void *, void *, int);
101 static int write_vc(void *, void *, int);
102 static int __msgwrite(int, void *, size_t);
103 static int __msgread(int, void *, size_t);
104
105 struct ct_data {
106         int             ct_fd;          /* connection's fd */
107         bool_t          ct_closeit;     /* close it on destroy */
108         struct timeval  ct_wait;        /* wait interval in milliseconds */
109         bool_t          ct_waitset;     /* wait set by clnt_control? */
110         struct netbuf   ct_addr;        /* remote addr */
111         struct rpc_err  ct_error;
112         union {
113                 char    ct_mcallc[MCALL_MSG_SIZE];      /* marshalled callmsg */
114                 u_int32_t ct_mcalli;
115         } ct_u;
116         u_int           ct_mpos;        /* pos after marshal */
117         XDR             ct_xdrs;        /* XDR stream */
118 };
119
120 /*
121  *      This machinery implements per-fd locks for MT-safety.  It is not
122  *      sufficient to do per-CLIENT handle locks for MT-safety because a
123  *      user may create more than one CLIENT handle with the same fd behind
124  *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
125  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
126  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
127  *      CLIENT handle created for that fd.
128  *      The current implementation holds locks across the entire RPC and reply.
129  *      Yes, this is silly, and as soon as this code is proven to work, this
130  *      should be the first thing fixed.  One step at a time.
131  */
132 static int      *vc_fd_locks;
133 static cond_t   *vc_cv;
134 #define release_fd_lock(fd, mask) {     \
135         mutex_lock(&clnt_fd_lock);      \
136         vc_fd_locks[fd] = 0;            \
137         mutex_unlock(&clnt_fd_lock);    \
138         thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);        \
139         cond_signal(&vc_cv[fd]);        \
140 }
141
142 static const char clnt_vc_errstr[] = "%s : %s";
143 static const char clnt_vc_str[] = "clnt_vc_create";
144 static const char clnt_read_vc_str[] = "read_vc";
145 static const char __no_mem_str[] = "out of memory";
146
147 /*
148  * Create a client handle for a connection.
149  * Default options are set, which the user can change using clnt_control()'s.
150  * The rpc/vc package does buffering similar to stdio, so the client
151  * must pick send and receive buffer sizes, 0 => use the default.
152  * NB: fd is copied into a private area.
153  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
154  * set this something more useful.
155  *
156  * fd should be an open socket
157  *
158  * fd - open file descriptor
159  * raddr - servers address
160  * prog  - program number
161  * vers  - version number
162  * sendsz - buffer send size
163  * recvsz - buffer recv size
164  */
165 CLIENT *
166 clnt_vc_create(int fd, const struct netbuf *raddr, const rpcprog_t prog,
167     const rpcvers_t vers, u_int sendsz, u_int recvsz)
168 {
169         CLIENT *cl;                     /* client handle */
170         struct ct_data *ct = NULL;      /* client handle */
171         struct timeval now;
172         struct rpc_msg call_msg;
173         static u_int32_t disrupt;
174         sigset_t mask;
175         sigset_t newmask;
176         struct sockaddr_storage ss;
177         socklen_t slen;
178         struct __rpc_sockinfo si;
179
180         if (disrupt == 0)
181                 disrupt = (u_int32_t)(long)raddr;
182
183         cl = (CLIENT *)mem_alloc(sizeof (*cl));
184         ct = (struct ct_data *)mem_alloc(sizeof (*ct));
185         if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) {
186                 (void) syslog(LOG_ERR, clnt_vc_errstr,
187                     clnt_vc_str, __no_mem_str);
188                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
189                 rpc_createerr.cf_error.re_errno = errno;
190                 goto err;
191         }
192         ct->ct_addr.buf = NULL;
193         sigfillset(&newmask);
194         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
195         mutex_lock(&clnt_fd_lock);
196         if (vc_fd_locks == (int *) NULL) {
197                 int cv_allocsz, fd_allocsz;
198                 int dtbsize = __rpc_dtbsize();
199
200                 fd_allocsz = dtbsize * sizeof (int);
201                 vc_fd_locks = (int *) mem_alloc(fd_allocsz);
202                 if (vc_fd_locks == (int *) NULL) {
203                         mutex_unlock(&clnt_fd_lock);
204                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
205                         goto err;
206                 } else
207                         memset(vc_fd_locks, '\0', fd_allocsz);
208
209                 assert(vc_cv == (cond_t *) NULL);
210                 cv_allocsz = dtbsize * sizeof (cond_t);
211                 vc_cv = (cond_t *) mem_alloc(cv_allocsz);
212                 if (vc_cv == (cond_t *) NULL) {
213                         mem_free(vc_fd_locks, fd_allocsz);
214                         vc_fd_locks = (int *) NULL;
215                         mutex_unlock(&clnt_fd_lock);
216                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
217                         goto err;
218                 } else {
219                         int i;
220
221                         for (i = 0; i < dtbsize; i++)
222                                 cond_init(&vc_cv[i], 0, (void *) 0);
223                 }
224         } else
225                 assert(vc_cv != (cond_t *) NULL);
226
227         /*
228          * XXX - fvdl connecting while holding a mutex?
229          */
230         slen = sizeof ss;
231         if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
232                 if (errno != ENOTCONN) {
233                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
234                         rpc_createerr.cf_error.re_errno = errno;
235                         mutex_unlock(&clnt_fd_lock);
236                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
237                         goto err;
238                 }
239                 if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
240                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
241                         rpc_createerr.cf_error.re_errno = errno;
242                         mutex_unlock(&clnt_fd_lock);
243                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
244                         goto err;
245                 }
246         }
247         mutex_unlock(&clnt_fd_lock);
248         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
249         if (!__rpc_fd2sockinfo(fd, &si))
250                 goto err;
251
252         ct->ct_closeit = FALSE;
253
254         /*
255          * Set up private data struct
256          */
257         ct->ct_fd = fd;
258         ct->ct_wait.tv_usec = 0;
259         ct->ct_waitset = FALSE;
260         ct->ct_addr.buf = malloc(raddr->maxlen);
261         if (ct->ct_addr.buf == NULL)
262                 goto err;
263         memcpy(ct->ct_addr.buf, raddr->buf, raddr->len);
264         ct->ct_addr.len = raddr->len;
265         ct->ct_addr.maxlen = raddr->maxlen;
266
267         /*
268          * Initialize call message
269          */
270         (void)gettimeofday(&now, NULL);
271         call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
272         call_msg.rm_direction = CALL;
273         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
274         call_msg.rm_call.cb_prog = (u_int32_t)prog;
275         call_msg.rm_call.cb_vers = (u_int32_t)vers;
276
277         /*
278          * pre-serialize the static part of the call msg and stash it away
279          */
280         xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
281             XDR_ENCODE);
282         if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
283                 if (ct->ct_closeit) {
284                         (void)_close(fd);
285                 }
286                 goto err;
287         }
288         ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
289         XDR_DESTROY(&(ct->ct_xdrs));
290         assert(ct->ct_mpos + sizeof(uint32_t) <= MCALL_MSG_SIZE);
291
292         /*
293          * Create a client handle which uses xdrrec for serialization
294          * and authnone for authentication.
295          */
296         cl->cl_ops = clnt_vc_ops();
297         cl->cl_private = ct;
298         cl->cl_auth = authnone_create();
299         sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
300         recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
301         xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
302             cl->cl_private, read_vc, write_vc);
303         return (cl);
304
305 err:
306         if (ct) {
307                 if (ct->ct_addr.len)
308                         mem_free(ct->ct_addr.buf, ct->ct_addr.len);
309                 mem_free(ct, sizeof (struct ct_data));
310         }
311         if (cl)
312                 mem_free(cl, sizeof (CLIENT));
313         return ((CLIENT *)NULL);
314 }
315
316 static enum clnt_stat
317 clnt_vc_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xdr_args, void *args_ptr,
318     xdrproc_t xdr_results, void *results_ptr, struct timeval timeout)
319 {
320         struct ct_data *ct = (struct ct_data *) cl->cl_private;
321         XDR *xdrs = &(ct->ct_xdrs);
322         struct rpc_msg reply_msg;
323         u_int32_t x_id;
324         u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli;    /* yuk */
325         bool_t shipnow;
326         int refreshes = 2;
327         sigset_t mask, newmask;
328         int rpc_lock_value;
329         bool_t reply_stat;
330
331         assert(cl != NULL);
332
333         sigfillset(&newmask);
334         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
335         mutex_lock(&clnt_fd_lock);
336         while (vc_fd_locks[ct->ct_fd])
337                 cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
338         if (__isthreaded)
339                 rpc_lock_value = 1;
340         else
341                 rpc_lock_value = 0;
342         vc_fd_locks[ct->ct_fd] = rpc_lock_value;
343         mutex_unlock(&clnt_fd_lock);
344         if (!ct->ct_waitset) {
345                 /* If time is not within limits, we ignore it. */
346                 if (time_not_ok(&timeout) == FALSE)
347                         ct->ct_wait = timeout;
348         }
349
350         shipnow =
351             (xdr_results == NULL && timeout.tv_sec == 0
352             && timeout.tv_usec == 0) ? FALSE : TRUE;
353
354 call_again:
355         xdrs->x_op = XDR_ENCODE;
356         ct->ct_error.re_status = RPC_SUCCESS;
357         x_id = ntohl(--(*msg_x_id));
358
359         if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
360                 if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
361                     (! XDR_PUTINT32(xdrs, &proc)) ||
362                     (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
363                     (! (*xdr_args)(xdrs, args_ptr))) {
364                         if (ct->ct_error.re_status == RPC_SUCCESS)
365                                 ct->ct_error.re_status = RPC_CANTENCODEARGS;
366                         (void)xdrrec_endofrecord(xdrs, TRUE);
367                         release_fd_lock(ct->ct_fd, mask);
368                         return (ct->ct_error.re_status);
369                 }
370         } else {
371                 *(uint32_t *) &ct->ct_u.ct_mcallc[ct->ct_mpos] = htonl(proc);
372                 if (! __rpc_gss_wrap(cl->cl_auth, ct->ct_u.ct_mcallc,
373                         ct->ct_mpos + sizeof(uint32_t),
374                         xdrs, xdr_args, args_ptr)) {
375                         if (ct->ct_error.re_status == RPC_SUCCESS)
376                                 ct->ct_error.re_status = RPC_CANTENCODEARGS;
377                         (void)xdrrec_endofrecord(xdrs, TRUE);
378                         release_fd_lock(ct->ct_fd, mask);
379                         return (ct->ct_error.re_status);
380                 }
381         }
382         if (! xdrrec_endofrecord(xdrs, shipnow)) {
383                 release_fd_lock(ct->ct_fd, mask);
384                 return (ct->ct_error.re_status = RPC_CANTSEND);
385         }
386         if (! shipnow) {
387                 release_fd_lock(ct->ct_fd, mask);
388                 return (RPC_SUCCESS);
389         }
390         /*
391          * Hack to provide rpc-based message passing
392          */
393         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
394                 release_fd_lock(ct->ct_fd, mask);
395                 return(ct->ct_error.re_status = RPC_TIMEDOUT);
396         }
397
398
399         /*
400          * Keep receiving until we get a valid transaction id
401          */
402         xdrs->x_op = XDR_DECODE;
403         while (TRUE) {
404                 reply_msg.acpted_rply.ar_verf = _null_auth;
405                 reply_msg.acpted_rply.ar_results.where = NULL;
406                 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
407                 if (! xdrrec_skiprecord(xdrs)) {
408                         release_fd_lock(ct->ct_fd, mask);
409                         return (ct->ct_error.re_status);
410                 }
411                 /* now decode and validate the response header */
412                 if (! xdr_replymsg(xdrs, &reply_msg)) {
413                         if (ct->ct_error.re_status == RPC_SUCCESS)
414                                 continue;
415                         release_fd_lock(ct->ct_fd, mask);
416                         return (ct->ct_error.re_status);
417                 }
418                 if (reply_msg.rm_xid == x_id)
419                         break;
420         }
421
422         /*
423          * process header
424          */
425         _seterr_reply(&reply_msg, &(ct->ct_error));
426         if (ct->ct_error.re_status == RPC_SUCCESS) {
427                 if (! AUTH_VALIDATE(cl->cl_auth,
428                     &reply_msg.acpted_rply.ar_verf)) {
429                         ct->ct_error.re_status = RPC_AUTHERROR;
430                         ct->ct_error.re_why = AUTH_INVALIDRESP;
431                 } else {
432                         if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
433                                 reply_stat = (*xdr_results)(xdrs, results_ptr);
434                         } else {
435                                 reply_stat = __rpc_gss_unwrap(cl->cl_auth,
436                                     xdrs, xdr_results, results_ptr);
437                         }
438                         if (! reply_stat) {
439                                 if (ct->ct_error.re_status == RPC_SUCCESS)
440                                         ct->ct_error.re_status =
441                                                 RPC_CANTDECODERES;
442                         }
443                 }
444                 /* free verifier ... */
445                 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
446                         xdrs->x_op = XDR_FREE;
447                         (void)xdr_opaque_auth(xdrs,
448                             &(reply_msg.acpted_rply.ar_verf));
449                 }
450         }  /* end successful completion */
451         else {
452                 /* maybe our credentials need to be refreshed ... */
453                 if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg))
454                         goto call_again;
455         }  /* end of unsuccessful completion */
456         release_fd_lock(ct->ct_fd, mask);
457         return (ct->ct_error.re_status);
458 }
459
460 static void
461 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
462 {
463         struct ct_data *ct;
464
465         assert(cl != NULL);
466         assert(errp != NULL);
467
468         ct = (struct ct_data *) cl->cl_private;
469         *errp = ct->ct_error;
470 }
471
472 static bool_t
473 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
474 {
475         struct ct_data *ct;
476         XDR *xdrs;
477         bool_t dummy;
478         sigset_t mask;
479         sigset_t newmask;
480
481         assert(cl != NULL);
482
483         ct = (struct ct_data *)cl->cl_private;
484         xdrs = &(ct->ct_xdrs);
485
486         sigfillset(&newmask);
487         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
488         mutex_lock(&clnt_fd_lock);
489         while (vc_fd_locks[ct->ct_fd])
490                 cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
491         xdrs->x_op = XDR_FREE;
492         dummy = (*xdr_res)(xdrs, res_ptr);
493         mutex_unlock(&clnt_fd_lock);
494         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
495         cond_signal(&vc_cv[ct->ct_fd]);
496
497         return dummy;
498 }
499
500 /*ARGSUSED*/
501 static void
502 clnt_vc_abort(CLIENT *cl)
503 {
504 }
505
506 static bool_t
507 clnt_vc_control(CLIENT *cl, u_int request, void *info)
508 {
509         struct ct_data *ct;
510         void *infop = info;
511         sigset_t mask;
512         sigset_t newmask;
513         int rpc_lock_value;
514
515         assert(cl != NULL);
516
517         ct = (struct ct_data *)cl->cl_private;
518
519         sigfillset(&newmask);
520         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
521         mutex_lock(&clnt_fd_lock);
522         while (vc_fd_locks[ct->ct_fd])
523                 cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
524         if (__isthreaded)
525                 rpc_lock_value = 1;
526         else
527                 rpc_lock_value = 0;
528         vc_fd_locks[ct->ct_fd] = rpc_lock_value;
529         mutex_unlock(&clnt_fd_lock);
530
531         switch (request) {
532         case CLSET_FD_CLOSE:
533                 ct->ct_closeit = TRUE;
534                 release_fd_lock(ct->ct_fd, mask);
535                 return (TRUE);
536         case CLSET_FD_NCLOSE:
537                 ct->ct_closeit = FALSE;
538                 release_fd_lock(ct->ct_fd, mask);
539                 return (TRUE);
540         default:
541                 break;
542         }
543
544         /* for other requests which use info */
545         if (info == NULL) {
546                 release_fd_lock(ct->ct_fd, mask);
547                 return (FALSE);
548         }
549         switch (request) {
550         case CLSET_TIMEOUT:
551                 if (time_not_ok((struct timeval *)info)) {
552                         release_fd_lock(ct->ct_fd, mask);
553                         return (FALSE);
554                 }
555                 ct->ct_wait = *(struct timeval *)infop;
556                 ct->ct_waitset = TRUE;
557                 break;
558         case CLGET_TIMEOUT:
559                 *(struct timeval *)infop = ct->ct_wait;
560                 break;
561         case CLGET_SERVER_ADDR:
562                 (void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
563                 break;
564         case CLGET_FD:
565                 *(int *)info = ct->ct_fd;
566                 break;
567         case CLGET_SVC_ADDR:
568                 /* The caller should not free this memory area */
569                 *(struct netbuf *)info = ct->ct_addr;
570                 break;
571         case CLSET_SVC_ADDR:            /* set to new address */
572                 release_fd_lock(ct->ct_fd, mask);
573                 return (FALSE);
574         case CLGET_XID:
575                 /*
576                  * use the knowledge that xid is the
577                  * first element in the call structure
578                  * This will get the xid of the PREVIOUS call
579                  */
580                 *(u_int32_t *)info =
581                     ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
582                 break;
583         case CLSET_XID:
584                 /* This will set the xid of the NEXT call */
585                 *(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
586                     htonl(*((u_int32_t *)info) + 1);
587                 /* increment by 1 as clnt_vc_call() decrements once */
588                 break;
589         case CLGET_VERS:
590                 /*
591                  * This RELIES on the information that, in the call body,
592                  * the version number field is the fifth field from the
593                  * begining of the RPC header. MUST be changed if the
594                  * call_struct is changed
595                  */
596                 *(u_int32_t *)info =
597                     ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
598                     4 * BYTES_PER_XDR_UNIT));
599                 break;
600
601         case CLSET_VERS:
602                 *(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
603                     4 * BYTES_PER_XDR_UNIT) =
604                     htonl(*(u_int32_t *)info);
605                 break;
606
607         case CLGET_PROG:
608                 /*
609                  * This RELIES on the information that, in the call body,
610                  * the program number field is the fourth field from the
611                  * begining of the RPC header. MUST be changed if the
612                  * call_struct is changed
613                  */
614                 *(u_int32_t *)info =
615                     ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
616                     3 * BYTES_PER_XDR_UNIT));
617                 break;
618
619         case CLSET_PROG:
620                 *(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
621                     3 * BYTES_PER_XDR_UNIT) =
622                     htonl(*(u_int32_t *)info);
623                 break;
624
625         default:
626                 release_fd_lock(ct->ct_fd, mask);
627                 return (FALSE);
628         }
629         release_fd_lock(ct->ct_fd, mask);
630         return (TRUE);
631 }
632
633
634 static void
635 clnt_vc_destroy(CLIENT *cl)
636 {
637         struct ct_data *ct = (struct ct_data *) cl->cl_private;
638         int ct_fd = ct->ct_fd;
639         sigset_t mask;
640         sigset_t newmask;
641
642         assert(cl != NULL);
643
644         ct = (struct ct_data *) cl->cl_private;
645
646         sigfillset(&newmask);
647         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
648         mutex_lock(&clnt_fd_lock);
649         while (vc_fd_locks[ct_fd])
650                 cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
651         if (ct->ct_closeit && ct->ct_fd != -1) {
652                 (void)_close(ct->ct_fd);
653         }
654         XDR_DESTROY(&(ct->ct_xdrs));
655         free(ct->ct_addr.buf);
656         mem_free(ct, sizeof(struct ct_data));
657         if (cl->cl_netid && cl->cl_netid[0])
658                 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
659         if (cl->cl_tp && cl->cl_tp[0])
660                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
661         mem_free(cl, sizeof(CLIENT));
662         mutex_unlock(&clnt_fd_lock);
663         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
664         cond_signal(&vc_cv[ct_fd]);
665 }
666
667 /*
668  * Interface between xdr serializer and tcp connection.
669  * Behaves like the system calls, read & write, but keeps some error state
670  * around for the rpc level.
671  */
672 static int
673 read_vc(void *ctp, void *buf, int len)
674 {
675         struct sockaddr sa;
676         socklen_t sal;
677         struct ct_data *ct = (struct ct_data *)ctp;
678         struct pollfd fd;
679         int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
680             (ct->ct_wait.tv_usec / 1000));
681
682         if (len == 0)
683                 return (0);
684         fd.fd = ct->ct_fd;
685         fd.events = POLLIN;
686         for (;;) {
687                 switch (_poll(&fd, 1, milliseconds)) {
688                 case 0:
689                         ct->ct_error.re_status = RPC_TIMEDOUT;
690                         return (-1);
691
692                 case -1:
693                         if (errno == EINTR)
694                                 continue;
695                         ct->ct_error.re_status = RPC_CANTRECV;
696                         ct->ct_error.re_errno = errno;
697                         return (-1);
698                 }
699                 break;
700         }
701
702         sal = sizeof(sa);
703         if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
704             (sa.sa_family == AF_LOCAL)) {
705                 len = __msgread(ct->ct_fd, buf, (size_t)len);
706         } else {
707                 len = _read(ct->ct_fd, buf, (size_t)len);
708         }
709
710         switch (len) {
711         case 0:
712                 /* premature eof */
713                 ct->ct_error.re_errno = ECONNRESET;
714                 ct->ct_error.re_status = RPC_CANTRECV;
715                 len = -1;  /* it's really an error */
716                 break;
717
718         case -1:
719                 ct->ct_error.re_errno = errno;
720                 ct->ct_error.re_status = RPC_CANTRECV;
721                 break;
722         }
723         return (len);
724 }
725
726 static int
727 write_vc(void *ctp, void *buf, int len)
728 {
729         struct sockaddr sa;
730         socklen_t sal;
731         struct ct_data *ct = (struct ct_data *)ctp;
732         int i, cnt;
733
734         sal = sizeof(sa);
735         if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
736             (sa.sa_family == AF_LOCAL)) {
737                 for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
738                         if ((i = __msgwrite(ct->ct_fd, buf,
739                              (size_t)cnt)) == -1) {
740                                 ct->ct_error.re_errno = errno;
741                                 ct->ct_error.re_status = RPC_CANTSEND;
742                                 return (-1);
743                         }
744                 }
745         } else {
746                 for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
747                         if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
748                                 ct->ct_error.re_errno = errno;
749                                 ct->ct_error.re_status = RPC_CANTSEND;
750                                 return (-1);
751                         }
752                 }
753         }
754         return (len);
755 }
756
757 static struct clnt_ops *
758 clnt_vc_ops(void)
759 {
760         static struct clnt_ops ops;
761         sigset_t mask, newmask;
762
763         /* VARIABLES PROTECTED BY ops_lock: ops */
764
765         sigfillset(&newmask);
766         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
767         mutex_lock(&ops_lock);
768         if (ops.cl_call == NULL) {
769                 ops.cl_call = clnt_vc_call;
770                 ops.cl_abort = clnt_vc_abort;
771                 ops.cl_geterr = clnt_vc_geterr;
772                 ops.cl_freeres = clnt_vc_freeres;
773                 ops.cl_destroy = clnt_vc_destroy;
774                 ops.cl_control = clnt_vc_control;
775         }
776         mutex_unlock(&ops_lock);
777         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
778         return (&ops);
779 }
780
781 /*
782  * Make sure that the time is not garbage.   -1 value is disallowed.
783  * Note this is different from time_not_ok in clnt_dg.c
784  */
785 static bool_t
786 time_not_ok(struct timeval *t)
787 {
788         return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
789                 t->tv_usec <= -1 || t->tv_usec > 1000000);
790 }
791
792 static int
793 __msgread(int sock, void *buf, size_t cnt)
794 {
795         struct iovec iov[1];
796         struct msghdr msg;
797         union {
798                 struct cmsghdr cmsg;
799                 char control[CMSG_SPACE(sizeof(struct cmsgcred))];
800         } cm;
801  
802         bzero((char *)&cm, sizeof(cm));
803         iov[0].iov_base = buf;
804         iov[0].iov_len = cnt;
805  
806         msg.msg_iov = iov;
807         msg.msg_iovlen = 1;
808         msg.msg_name = NULL;
809         msg.msg_namelen = 0;
810         msg.msg_control = (caddr_t)&cm;
811         msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
812         msg.msg_flags = 0;
813  
814         return(_recvmsg(sock, &msg, 0));
815 }
816
817 static int
818 __msgwrite(int sock, void *buf, size_t cnt)
819 {
820         struct iovec iov[1];
821         struct msghdr msg;
822         union {
823                 struct cmsghdr cmsg;
824                 char control[CMSG_SPACE(sizeof(struct cmsgcred))];
825         } cm;
826  
827         bzero((char *)&cm, sizeof(cm));
828         iov[0].iov_base = buf;
829         iov[0].iov_len = cnt;
830  
831         cm.cmsg.cmsg_type = SCM_CREDS;
832         cm.cmsg.cmsg_level = SOL_SOCKET;
833         cm.cmsg.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
834  
835         msg.msg_iov = iov;
836         msg.msg_iovlen = 1;
837         msg.msg_name = NULL;
838         msg.msg_namelen = 0;
839         msg.msg_control = (caddr_t)&cm;
840         msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
841         msg.msg_flags = 0;
842
843         return(_sendmsg(sock, &msg, 0));
844 }