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