Imported Upstream version 0.2.5
[platform/upstream/libtirpc.git] / src / clnt_vc.c
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*
30  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
31  *
32  * Copyright (C) 1984, Sun Microsystems, Inc.
33  *
34  * TCP based RPC supports 'batched calls'.
35  * A sequence of calls may be batched-up in a send buffer.  The rpc call
36  * return immediately to the client even though the call was not necessarily
37  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
38  * the rpc timeout value is zero (see clnt.h, rpc).
39  *
40  * Clients should NOT casually batch calls that in fact return results; that is,
41  * the server side should be aware that a call is batched and not produce any
42  * return message.  Batched calls that produce many result messages can
43  * deadlock (netlock) the client and the server....
44  *
45  * Now go hang yourself.
46  */
47 #include <pthread.h>
48
49 #include <reentrant.h>
50 #include <sys/types.h>
51 #include <sys/poll.h>
52 #include <sys/syslog.h>
53 #include <sys/un.h>
54 #include <sys/uio.h>
55 #include <sys/socket.h>
56 #include <arpa/inet.h>
57 #include <assert.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <netdb.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <signal.h>
66
67 #include <rpc/rpc.h>
68 #include "rpc_com.h"
69
70 #define MCALL_MSG_SIZE 24
71
72 #define CMGROUP_MAX    16
73 #define SCM_CREDS      0x03            /* process creds (struct cmsgcred) */
74
75 /*
76  * Credentials structure, used to verify the identity of a peer
77  * process that has sent us a message. This is allocated by the
78  * peer process but filled in by the kernel. This prevents the
79  * peer from lying about its identity. (Note that cmcred_groups[0]
80  * is the effective GID.)
81  */
82 struct cmsgcred {
83         pid_t   cmcred_pid;             /* PID of sending process */
84         uid_t   cmcred_uid;             /* real UID of sending process */
85         uid_t   cmcred_euid;            /* effective UID of sending process */
86         gid_t   cmcred_gid;             /* real GID of sending process */
87         short   cmcred_ngroups;         /* number or groups */
88         gid_t   cmcred_groups[CMGROUP_MAX];     /* groups */
89 };
90
91 struct cmessage {
92         struct cmsghdr cmsg;
93         struct cmsgcred cmcred;
94 };
95
96 static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
97     xdrproc_t, void *, struct timeval);
98 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
99 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
100 static void clnt_vc_abort(CLIENT *);
101 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
102 static void clnt_vc_destroy(CLIENT *);
103 static struct clnt_ops *clnt_vc_ops(void);
104 static bool_t time_not_ok(struct timeval *);
105 static int read_vc(void *, void *, int);
106 static int write_vc(void *, void *, int);
107
108 struct ct_data {
109         int             ct_fd;          /* connection's fd */
110         bool_t          ct_closeit;     /* close it on destroy */
111         struct timeval  ct_wait;        /* wait interval in milliseconds */
112         bool_t          ct_waitset;     /* wait set by clnt_control? */
113         struct netbuf   ct_addr;        /* remote addr */
114         struct rpc_err  ct_error;
115         union {
116                 char    ct_mcallc[MCALL_MSG_SIZE];      /* marshalled callmsg */
117                 u_int32_t ct_mcalli;
118         } ct_u;
119         u_int           ct_mpos;        /* pos after marshal */
120         XDR             ct_xdrs;        /* XDR stream */
121 };
122
123 /*
124  *      This machinery implements per-fd locks for MT-safety.  It is not
125  *      sufficient to do per-CLIENT handle locks for MT-safety because a
126  *      user may create more than one CLIENT handle with the same fd behind
127  *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
128  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
129  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is active on some
130  *      CLIENT handle created for that fd.
131  *      The current implementation holds locks across the entire RPC and reply.
132  *      Yes, this is silly, and as soon as this code is proven to work, this
133  *      should be the first thing fixed.  One step at a time.
134  */
135 static int      *vc_fd_locks;
136 extern pthread_mutex_t disrupt_lock;
137 extern mutex_t  clnt_fd_lock;
138 static cond_t   *vc_cv;
139 #define release_fd_lock(fd, mask) {     \
140         mutex_lock(&clnt_fd_lock);      \
141         vc_fd_locks[fd] = 0;            \
142         mutex_unlock(&clnt_fd_lock);    \
143         thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);        \
144         cond_signal(&vc_cv[fd]);        \
145 }
146
147 static const char clnt_vc_errstr[] = "%s : %s";
148 static const char clnt_vc_str[] = "clnt_vc_create";
149 static const char clnt_read_vc_str[] = "read_vc";
150 static const char __no_mem_str[] = "out of memory";
151
152 /*
153  * Create a client handle for a connection.
154  * Default options are set, which the user can change using clnt_control()'s.
155  * The rpc/vc package does buffering similar to stdio, so the client
156  * must pick send and receive buffer sizes, 0 => use the default.
157  * NB: fd is copied into a private area.
158  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
159  * set this something more useful.
160  *
161  * fd should be an open socket
162  */
163 CLIENT *
164 clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
165         int fd;                         /* open file descriptor */
166         const struct netbuf *raddr;     /* servers address */
167         const rpcprog_t prog;                   /* program number */
168         const rpcvers_t vers;                   /* version number */
169         u_int sendsz;                   /* buffer recv size */
170         u_int recvsz;                   /* buffer send size */
171 {
172         CLIENT *cl;                     /* client handle */
173         struct ct_data *ct = NULL;      /* client handle */
174         struct timeval now;
175         struct rpc_msg call_msg;
176         static u_int32_t disrupt;
177         sigset_t mask;
178         sigset_t newmask;
179         struct sockaddr_storage ss;
180         socklen_t slen;
181         struct __rpc_sockinfo si;
182
183         mutex_lock(&disrupt_lock);
184         if (disrupt == 0)
185                 disrupt = (u_int32_t)(long)raddr;
186         mutex_unlock(&disrupt_lock);
187
188         cl = (CLIENT *)mem_alloc(sizeof (*cl));
189         ct = (struct ct_data *)mem_alloc(sizeof (*ct));
190         if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) {
191                 (void) syslog(LOG_ERR, clnt_vc_errstr,
192                     clnt_vc_str, __no_mem_str);
193                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
194                 rpc_createerr.cf_error.re_errno = errno;
195                 goto err;
196         }
197         ct->ct_addr.buf = NULL;
198         sigfillset(&newmask);
199         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
200         mutex_lock(&clnt_fd_lock);
201         if (vc_fd_locks == (int *) NULL) {
202                 int cv_allocsz, fd_allocsz;
203                 int dtbsize = __rpc_dtbsize();
204
205                 fd_allocsz = dtbsize * sizeof (int);
206                 vc_fd_locks = (int *) mem_alloc(fd_allocsz);
207                 if (vc_fd_locks == (int *) NULL) {
208                         mutex_unlock(&clnt_fd_lock);
209                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
210                         goto err;
211                 } else
212                         memset(vc_fd_locks, '\0', fd_allocsz);
213
214                 assert(vc_cv == (cond_t *) NULL);
215                 cv_allocsz = dtbsize * sizeof (cond_t);
216                 vc_cv = (cond_t *) mem_alloc(cv_allocsz);
217                 if (vc_cv == (cond_t *) NULL) {
218                         mem_free(vc_fd_locks, fd_allocsz);
219                         vc_fd_locks = (int *) NULL;
220                         mutex_unlock(&clnt_fd_lock);
221                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
222                         goto err;
223                 } else {
224                         int i;
225
226                         for (i = 0; i < dtbsize; i++)
227                                 cond_init(&vc_cv[i], 0, (void *) 0);
228                 }
229         } else
230                 assert(vc_cv != (cond_t *) NULL);
231
232         /*
233          * XXX - fvdl connecting while holding a mutex?
234          */
235         slen = sizeof ss;
236         if (getpeername(fd, (struct sockaddr *)&ss, &slen) < 0) {
237                 if (errno != ENOTCONN) {
238                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
239                         rpc_createerr.cf_error.re_errno = errno;
240                         mutex_unlock(&clnt_fd_lock);
241                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
242                         goto err;
243                 }
244                 if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
245                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
246                         rpc_createerr.cf_error.re_errno = errno;
247                         mutex_unlock(&clnt_fd_lock);
248                         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
249                         goto err;
250                 }
251         }
252         mutex_unlock(&clnt_fd_lock);
253         if (!__rpc_fd2sockinfo(fd, &si))
254                 goto err;
255         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
256
257         ct->ct_closeit = FALSE;
258
259         /*
260          * Set up private data struct
261          */
262         ct->ct_fd = fd;
263         ct->ct_wait.tv_usec = 0;
264         ct->ct_waitset = FALSE;
265         ct->ct_addr.buf = malloc(raddr->maxlen);
266         if (ct->ct_addr.buf == NULL)
267                 goto err;
268         memcpy(ct->ct_addr.buf, raddr->buf, raddr->len);
269         ct->ct_addr.len = raddr->len;
270         ct->ct_addr.maxlen = raddr->maxlen;
271
272         /*
273          * Initialize call message
274          */
275         (void)gettimeofday(&now, NULL);
276         mutex_lock(&disrupt_lock);
277         call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
278         mutex_unlock(&disrupt_lock);
279         call_msg.rm_direction = CALL;
280         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
281         call_msg.rm_call.cb_prog = (u_int32_t)prog;
282         call_msg.rm_call.cb_vers = (u_int32_t)vers;
283
284         /*
285          * pre-serialize the static part of the call msg and stash it away
286          */
287         xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
288             XDR_ENCODE);
289         if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
290                 if (ct->ct_closeit) {
291                         (void)close(fd);
292                 }
293                 goto err;
294         }
295         ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
296         XDR_DESTROY(&(ct->ct_xdrs));
297
298         /*
299          * Create a client handle which uses xdrrec for serialization
300          * and authnone for authentication.
301          */
302         cl->cl_ops = clnt_vc_ops();
303         cl->cl_private = ct;
304         cl->cl_auth = authnone_create();
305         sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
306         recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
307         xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
308             cl->cl_private, read_vc, write_vc);
309         return (cl);
310
311 err:
312         if (cl) {
313                 if (ct) {
314                         if (ct->ct_addr.len)
315                                 mem_free(ct->ct_addr.buf, ct->ct_addr.len);
316                         mem_free(ct, sizeof (struct ct_data));
317                 }
318                 if (cl)
319                         mem_free(cl, sizeof (CLIENT));
320         }
321         return ((CLIENT *)NULL);
322 }
323
324 static enum clnt_stat
325 clnt_vc_call(cl, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
326         CLIENT *cl;
327         rpcproc_t proc;
328         xdrproc_t xdr_args;
329         void *args_ptr;
330         xdrproc_t xdr_results;
331         void *results_ptr;
332         struct timeval timeout;
333 {
334         struct ct_data *ct = (struct ct_data *) cl->cl_private;
335         XDR *xdrs = &(ct->ct_xdrs);
336         struct rpc_msg reply_msg;
337         u_int32_t x_id;
338         u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli;    /* yuk */
339         bool_t shipnow;
340         int refreshes = 2;
341         sigset_t mask, newmask;
342         int rpc_lock_value;
343
344         assert(cl != NULL);
345
346         sigfillset(&newmask);
347         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
348         mutex_lock(&clnt_fd_lock);
349         while (vc_fd_locks[ct->ct_fd])
350                 cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
351         rpc_lock_value = 1;
352         vc_fd_locks[ct->ct_fd] = rpc_lock_value;
353         mutex_unlock(&clnt_fd_lock);
354         if (!ct->ct_waitset) {
355                 /* If time is not within limits, we ignore it. */
356                 if (time_not_ok(&timeout) == FALSE)
357                         ct->ct_wait = timeout;
358         }
359
360         shipnow =
361             (xdr_results == NULL && timeout.tv_sec == 0
362             && timeout.tv_usec == 0) ? FALSE : TRUE;
363
364 call_again:
365         xdrs->x_op = XDR_ENCODE;
366         ct->ct_error.re_status = RPC_SUCCESS;
367         x_id = ntohl(--(*msg_x_id));
368
369         if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
370             (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
371             (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
372             (! AUTH_WRAP(cl->cl_auth, xdrs, xdr_args, args_ptr))) {
373                 if (ct->ct_error.re_status == RPC_SUCCESS)
374                         ct->ct_error.re_status = RPC_CANTENCODEARGS;
375                 (void)xdrrec_endofrecord(xdrs, TRUE);
376                 release_fd_lock(ct->ct_fd, mask);
377                 return (ct->ct_error.re_status);
378         }
379         if (! xdrrec_endofrecord(xdrs, shipnow)) {
380                 release_fd_lock(ct->ct_fd, mask);
381                 return (ct->ct_error.re_status = RPC_CANTSEND);
382         }
383         if (! shipnow) {
384                 release_fd_lock(ct->ct_fd, mask);
385                 return (RPC_SUCCESS);
386         }
387         /*
388          * Hack to provide rpc-based message passing
389          */
390         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
391                 release_fd_lock(ct->ct_fd, mask);
392                 return(ct->ct_error.re_status = RPC_TIMEDOUT);
393         }
394
395
396         /*
397          * Keep receiving until we get a valid transaction id
398          */
399         xdrs->x_op = XDR_DECODE;
400         while (TRUE) {
401                 reply_msg.acpted_rply.ar_verf = _null_auth;
402                 reply_msg.acpted_rply.ar_results.where = NULL;
403                 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
404                 if (! xdrrec_skiprecord(xdrs)) {
405                         release_fd_lock(ct->ct_fd, mask);
406                         return (ct->ct_error.re_status);
407                 }
408                 /* now decode and validate the response header */
409                 if (! xdr_replymsg(xdrs, &reply_msg)) {
410                         if (ct->ct_error.re_status == RPC_SUCCESS)
411                                 continue;
412                         release_fd_lock(ct->ct_fd, mask);
413                         return (ct->ct_error.re_status);
414                 }
415                 if (reply_msg.rm_xid == x_id)
416                         break;
417         }
418
419         /*
420          * process header
421          */
422         _seterr_reply(&reply_msg, &(ct->ct_error));
423         if (ct->ct_error.re_status == RPC_SUCCESS) {
424                 if (! AUTH_VALIDATE(cl->cl_auth,
425                     &reply_msg.acpted_rply.ar_verf)) {
426                         ct->ct_error.re_status = RPC_AUTHERROR;
427                         ct->ct_error.re_why = AUTH_INVALIDRESP;
428                 } else if (! AUTH_UNWRAP(cl->cl_auth, xdrs,
429                                          xdr_results, results_ptr)) {
430                         if (ct->ct_error.re_status == RPC_SUCCESS)
431                                 ct->ct_error.re_status = RPC_CANTDECODERES;
432                 }
433                 /* free verifier ... */
434                 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
435                         xdrs->x_op = XDR_FREE;
436                         (void)xdr_opaque_auth(xdrs,
437                             &(reply_msg.acpted_rply.ar_verf));
438                 }
439         }  /* end successful completion */
440         else {
441                 /* maybe our credentials need to be refreshed ... */
442                 if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg))
443                         goto call_again;
444         }  /* end of unsuccessful completion */
445         release_fd_lock(ct->ct_fd, mask);
446         return (ct->ct_error.re_status);
447 }
448
449 static void
450 clnt_vc_geterr(cl, errp)
451         CLIENT *cl;
452         struct rpc_err *errp;
453 {
454         struct ct_data *ct;
455
456         assert(cl != NULL);
457         assert(errp != NULL);
458
459         ct = (struct ct_data *) cl->cl_private;
460         *errp = ct->ct_error;
461 }
462
463 static bool_t
464 clnt_vc_freeres(cl, xdr_res, res_ptr)
465         CLIENT *cl;
466         xdrproc_t xdr_res;
467         void *res_ptr;
468 {
469         struct ct_data *ct;
470         XDR *xdrs;
471         bool_t dummy;
472         sigset_t mask;
473         sigset_t newmask;
474
475         assert(cl != NULL);
476
477         ct = (struct ct_data *)cl->cl_private;
478         xdrs = &(ct->ct_xdrs);
479
480         sigfillset(&newmask);
481         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
482         mutex_lock(&clnt_fd_lock);
483         while (vc_fd_locks[ct->ct_fd])
484                 cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
485         xdrs->x_op = XDR_FREE;
486         dummy = (*xdr_res)(xdrs, res_ptr);
487         mutex_unlock(&clnt_fd_lock);
488         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
489         cond_signal(&vc_cv[ct->ct_fd]);
490
491         return dummy;
492 }
493
494 /*ARGSUSED*/
495 static void
496 clnt_vc_abort(cl)
497         CLIENT *cl;
498 {
499 }
500
501 static bool_t
502 clnt_vc_control(cl, request, info)
503         CLIENT *cl;
504         u_int request;
505         void *info;
506 {
507         struct ct_data *ct;
508         void *infop = info;
509         sigset_t mask;
510         sigset_t newmask;
511         int rpc_lock_value;
512         u_int32_t tmp;
513         u_int32_t ltmp;
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         rpc_lock_value = 1;
525         vc_fd_locks[ct->ct_fd] = rpc_lock_value;
526         mutex_unlock(&clnt_fd_lock);
527
528         switch (request) {
529         case CLSET_FD_CLOSE:
530                 ct->ct_closeit = TRUE;
531                 release_fd_lock(ct->ct_fd, mask);
532                 return (TRUE);
533         case CLSET_FD_NCLOSE:
534                 ct->ct_closeit = FALSE;
535                 release_fd_lock(ct->ct_fd, mask);
536                 return (TRUE);
537         default:
538                 break;
539         }
540
541         /* for other requests which use info */
542         if (info == NULL) {
543                 release_fd_lock(ct->ct_fd, mask);
544                 return (FALSE);
545         }
546         switch (request) {
547         case CLSET_TIMEOUT:
548                 if (time_not_ok((struct timeval *)info)) {
549                         release_fd_lock(ct->ct_fd, mask);
550                         return (FALSE);
551                 }
552                 ct->ct_wait = *(struct timeval *)infop;
553                 ct->ct_waitset = TRUE;
554                 break;
555         case CLGET_TIMEOUT:
556                 *(struct timeval *)infop = ct->ct_wait;
557                 break;
558         case CLGET_SERVER_ADDR:
559                 (void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
560                 break;
561         case CLGET_FD:
562                 *(int *)info = ct->ct_fd;
563                 break;
564         case CLGET_SVC_ADDR:
565                 /* The caller should not free this memory area */
566                 *(struct netbuf *)info = ct->ct_addr;
567                 break;
568         case CLSET_SVC_ADDR:            /* set to new address */
569                 release_fd_lock(ct->ct_fd, mask);
570                 return (FALSE);
571         case CLGET_XID:
572                 /*
573                  * use the knowledge that xid is the
574                  * first element in the call structure
575                  * This will get the xid of the PREVIOUS call
576                  */
577                 *(u_int32_t *)info =
578                     ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
579                 break;
580         case CLSET_XID:
581                 /* This will set the xid of the NEXT call */
582                 *(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
583                     htonl(*((u_int32_t *)info) + 1);
584                 /* increment by 1 as clnt_vc_call() decrements once */
585                 break;
586         case CLGET_VERS:
587                 /*
588                  * This RELIES on the information that, in the call body,
589                  * the version number field is the fifth field from the
590                  * begining of the RPC header. MUST be changed if the
591                  * call_struct is changed
592                  */
593                 memcpy(&tmp, ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, sizeof (tmp));
594                 ltmp = ntohl(tmp);
595                 memcpy(info, &ltmp, sizeof (ltmp));
596                 break;
597
598         case CLSET_VERS:
599                 memcpy(&ltmp, info, sizeof (ltmp));
600                 tmp = htonl(ltmp);
601                 memcpy(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, &tmp, sizeof(tmp));
602                 break;
603
604         case CLGET_PROG:
605                 /*
606                  * This RELIES on the information that, in the call body,
607                  * the program number field is the fourth field from the
608                  * begining of the RPC header. MUST be changed if the
609                  * call_struct is changed
610                  */
611                 memcpy(&tmp, ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, sizeof (tmp));
612                 ltmp = ntohl (tmp);
613                 memcpy(info, &ltmp, sizeof (ltmp));
614                 break;
615
616         case CLSET_PROG:
617                 memcpy(&ltmp, info, sizeof (ltmp));
618                 tmp = htonl(ltmp);
619                 memcpy(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, &tmp, sizeof(tmp));
620                 break;
621
622         default:
623                 release_fd_lock(ct->ct_fd, mask);
624                 return (FALSE);
625         }
626         release_fd_lock(ct->ct_fd, mask);
627         return (TRUE);
628 }
629
630
631 static void
632 clnt_vc_destroy(cl)
633         CLIENT *cl;
634 {
635         struct ct_data *ct = (struct ct_data *) cl->cl_private;
636         int ct_fd = ct->ct_fd;
637         sigset_t mask;
638         sigset_t newmask;
639
640         assert(cl != NULL);
641
642         ct = (struct ct_data *) cl->cl_private;
643
644         sigfillset(&newmask);
645         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
646         mutex_lock(&clnt_fd_lock);
647         while (vc_fd_locks[ct_fd])
648                 cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
649         if (ct->ct_closeit && ct->ct_fd != -1) {
650                 (void)close(ct->ct_fd);
651         }
652         XDR_DESTROY(&(ct->ct_xdrs));
653         if (ct->ct_addr.buf)
654                 free(ct->ct_addr.buf);
655         mem_free(ct, sizeof(struct ct_data));
656         if (cl->cl_netid && cl->cl_netid[0])
657                 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
658         if (cl->cl_tp && cl->cl_tp[0])
659                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
660         mem_free(cl, sizeof(CLIENT));
661         mutex_unlock(&clnt_fd_lock);
662         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
663         cond_signal(&vc_cv[ct_fd]);
664 }
665
666 /*
667  * Interface between xdr serializer and tcp connection.
668  * Behaves like the system calls, read & write, but keeps some error state
669  * around for the rpc level.
670  */
671 static int
672 read_vc(ctp, buf, len)
673         void *ctp;
674         void *buf;
675         int len;
676 {
677         /*
678         struct sockaddr sa;
679         socklen_t sal;
680         */
681         struct ct_data *ct = (struct ct_data *)ctp;
682         struct pollfd fd;
683         int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
684             (ct->ct_wait.tv_usec / 1000));
685
686         if (len == 0)
687                 return (0);
688         fd.fd = ct->ct_fd;
689         fd.events = POLLIN;
690         for (;;) {
691                 switch (poll(&fd, 1, milliseconds)) {
692                 case 0:
693                         ct->ct_error.re_status = RPC_TIMEDOUT;
694                         return (-1);
695
696                 case -1:
697                         if (errno == EINTR)
698                                 continue;
699                         ct->ct_error.re_status = RPC_CANTRECV;
700                         ct->ct_error.re_errno = errno;
701                         return (-1);
702                 }
703                 break;
704         }
705
706         len = read(ct->ct_fd, buf, (size_t)len);
707
708         switch (len) {
709         case 0:
710                 /* premature eof */
711                 ct->ct_error.re_errno = ECONNRESET;
712                 ct->ct_error.re_status = RPC_CANTRECV;
713                 len = -1;  /* it's really an error */
714                 break;
715
716         case -1:
717                 ct->ct_error.re_errno = errno;
718                 ct->ct_error.re_status = RPC_CANTRECV;
719                 break;
720         }
721         return (len);
722 }
723
724 static int
725 write_vc(ctp, buf, len)
726         void *ctp;
727         void *buf;
728         int len;
729 {
730         struct ct_data *ct = (struct ct_data *)ctp;
731         int i = 0, cnt;
732
733         for (cnt = len; cnt > 0; cnt -= i, buf += i) {
734             if ((i = write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
735                 ct->ct_error.re_errno = errno;
736                 ct->ct_error.re_status = RPC_CANTSEND;
737                 return (-1);
738             }
739         }
740         return (len);
741 }
742
743 static struct clnt_ops *
744 clnt_vc_ops()
745 {
746         static struct clnt_ops ops;
747         extern mutex_t  ops_lock;
748         sigset_t mask, newmask;
749
750         /* VARIABLES PROTECTED BY ops_lock: ops */
751
752         sigfillset(&newmask);
753         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
754         mutex_lock(&ops_lock);
755         if (ops.cl_call == NULL) {
756                 ops.cl_call = clnt_vc_call;
757                 ops.cl_abort = clnt_vc_abort;
758                 ops.cl_geterr = clnt_vc_geterr;
759                 ops.cl_freeres = clnt_vc_freeres;
760                 ops.cl_destroy = clnt_vc_destroy;
761                 ops.cl_control = clnt_vc_control;
762         }
763         mutex_unlock(&ops_lock);
764         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
765         return (&ops);
766 }
767
768 /*
769  * Make sure that the time is not garbage.   -1 value is disallowed.
770  * Note this is different from time_not_ok in clnt_dg.c
771  */
772 static bool_t
773 time_not_ok(t)
774         struct timeval *t;
775 {
776         return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
777                 t->tv_usec <= -1 || t->tv_usec > 1000000);
778 }