]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libc/rpc/clnt_vc.c
Merge MAP_GUARD.
[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 __inline void
507 htonlp(void *dst, const void *src, uint32_t incr)
508 {
509         /* We are aligned, so we think */
510         *(uint32_t *)dst = htonl(*(const uint32_t *)src + incr);
511 }
512
513 static __inline void
514 ntohlp(void *dst, const void *src)
515 {
516         /* We are aligned, so we think */
517         *(uint32_t *)dst = htonl(*(const uint32_t *)src);
518 }
519
520 static bool_t
521 clnt_vc_control(CLIENT *cl, u_int request, 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                 ntohlp(info, &ct->ct_u.ct_mcalli);
595                 break;
596         case CLSET_XID:
597                 /* This will set the xid of the NEXT call */
598                 /* increment by 1 as clnt_vc_call() decrements once */
599                 htonlp(&ct->ct_u.ct_mcalli, info, 1);
600                 break;
601         case CLGET_VERS:
602                 /*
603                  * This RELIES on the information that, in the call body,
604                  * the version number field is the fifth field from the
605                  * begining of the RPC header. MUST be changed if the
606                  * call_struct is changed
607                  */
608                 ntohlp(info, ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT);
609                 break;
610
611         case CLSET_VERS:
612                 htonlp(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, info, 0);
613                 break;
614
615         case CLGET_PROG:
616                 /*
617                  * This RELIES on the information that, in the call body,
618                  * the program number field is the fourth field from the
619                  * begining of the RPC header. MUST be changed if the
620                  * call_struct is changed
621                  */
622                 ntohlp(info, ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT);
623                 break;
624
625         case CLSET_PROG:
626                 htonlp(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, info, 0);
627                 break;
628
629         default:
630                 release_fd_lock(ct->ct_fd, mask);
631                 return (FALSE);
632         }
633         release_fd_lock(ct->ct_fd, mask);
634         return (TRUE);
635 }
636
637
638 static void
639 clnt_vc_destroy(CLIENT *cl)
640 {
641         struct ct_data *ct = (struct ct_data *) cl->cl_private;
642         int ct_fd = ct->ct_fd;
643         sigset_t mask;
644         sigset_t newmask;
645
646         assert(cl != NULL);
647
648         ct = (struct ct_data *) cl->cl_private;
649
650         sigfillset(&newmask);
651         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
652         mutex_lock(&clnt_fd_lock);
653         while (vc_fd_locks[ct_fd])
654                 cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
655         if (ct->ct_closeit && ct->ct_fd != -1) {
656                 (void)_close(ct->ct_fd);
657         }
658         XDR_DESTROY(&(ct->ct_xdrs));
659         free(ct->ct_addr.buf);
660         mem_free(ct, sizeof(struct ct_data));
661         if (cl->cl_netid && cl->cl_netid[0])
662                 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
663         if (cl->cl_tp && cl->cl_tp[0])
664                 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
665         mem_free(cl, sizeof(CLIENT));
666         mutex_unlock(&clnt_fd_lock);
667         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
668         cond_signal(&vc_cv[ct_fd]);
669 }
670
671 /*
672  * Interface between xdr serializer and tcp connection.
673  * Behaves like the system calls, read & write, but keeps some error state
674  * around for the rpc level.
675  */
676 static int
677 read_vc(void *ctp, void *buf, int len)
678 {
679         struct sockaddr sa;
680         socklen_t sal;
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         sal = sizeof(sa);
707         if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
708             (sa.sa_family == AF_LOCAL)) {
709                 len = __msgread(ct->ct_fd, buf, (size_t)len);
710         } else {
711                 len = _read(ct->ct_fd, buf, (size_t)len);
712         }
713
714         switch (len) {
715         case 0:
716                 /* premature eof */
717                 ct->ct_error.re_errno = ECONNRESET;
718                 ct->ct_error.re_status = RPC_CANTRECV;
719                 len = -1;  /* it's really an error */
720                 break;
721
722         case -1:
723                 ct->ct_error.re_errno = errno;
724                 ct->ct_error.re_status = RPC_CANTRECV;
725                 break;
726         }
727         return (len);
728 }
729
730 static int
731 write_vc(void *ctp, void *buf, int len)
732 {
733         struct sockaddr sa;
734         socklen_t sal;
735         struct ct_data *ct = (struct ct_data *)ctp;
736         int i, cnt;
737
738         sal = sizeof(sa);
739         if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) &&
740             (sa.sa_family == AF_LOCAL)) {
741                 for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
742                         if ((i = __msgwrite(ct->ct_fd, buf,
743                              (size_t)cnt)) == -1) {
744                                 ct->ct_error.re_errno = errno;
745                                 ct->ct_error.re_status = RPC_CANTSEND;
746                                 return (-1);
747                         }
748                 }
749         } else {
750                 for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
751                         if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
752                                 ct->ct_error.re_errno = errno;
753                                 ct->ct_error.re_status = RPC_CANTSEND;
754                                 return (-1);
755                         }
756                 }
757         }
758         return (len);
759 }
760
761 static struct clnt_ops *
762 clnt_vc_ops(void)
763 {
764         static struct clnt_ops ops;
765         sigset_t mask, newmask;
766
767         /* VARIABLES PROTECTED BY ops_lock: ops */
768
769         sigfillset(&newmask);
770         thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
771         mutex_lock(&ops_lock);
772         if (ops.cl_call == NULL) {
773                 ops.cl_call = clnt_vc_call;
774                 ops.cl_abort = clnt_vc_abort;
775                 ops.cl_geterr = clnt_vc_geterr;
776                 ops.cl_freeres = clnt_vc_freeres;
777                 ops.cl_destroy = clnt_vc_destroy;
778                 ops.cl_control = clnt_vc_control;
779         }
780         mutex_unlock(&ops_lock);
781         thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
782         return (&ops);
783 }
784
785 /*
786  * Make sure that the time is not garbage.   -1 value is disallowed.
787  * Note this is different from time_not_ok in clnt_dg.c
788  */
789 static bool_t
790 time_not_ok(struct timeval *t)
791 {
792         return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
793                 t->tv_usec <= -1 || t->tv_usec > 1000000);
794 }
795
796 static int
797 __msgread(int sock, void *buf, size_t cnt)
798 {
799         struct iovec iov[1];
800         struct msghdr msg;
801         union {
802                 struct cmsghdr cmsg;
803                 char control[CMSG_SPACE(sizeof(struct cmsgcred))];
804         } cm;
805  
806         bzero((char *)&cm, sizeof(cm));
807         iov[0].iov_base = buf;
808         iov[0].iov_len = cnt;
809  
810         msg.msg_iov = iov;
811         msg.msg_iovlen = 1;
812         msg.msg_name = NULL;
813         msg.msg_namelen = 0;
814         msg.msg_control = (caddr_t)&cm;
815         msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
816         msg.msg_flags = 0;
817  
818         return(_recvmsg(sock, &msg, 0));
819 }
820
821 static int
822 __msgwrite(int sock, void *buf, size_t cnt)
823 {
824         struct iovec iov[1];
825         struct msghdr msg;
826         union {
827                 struct cmsghdr cmsg;
828                 char control[CMSG_SPACE(sizeof(struct cmsgcred))];
829         } cm;
830  
831         bzero((char *)&cm, sizeof(cm));
832         iov[0].iov_base = buf;
833         iov[0].iov_len = cnt;
834  
835         cm.cmsg.cmsg_type = SCM_CREDS;
836         cm.cmsg.cmsg_level = SOL_SOCKET;
837         cm.cmsg.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
838  
839         msg.msg_iov = iov;
840         msg.msg_iovlen = 1;
841         msg.msg_name = NULL;
842         msg.msg_namelen = 0;
843         msg.msg_control = (caddr_t)&cm;
844         msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
845         msg.msg_flags = 0;
846
847         return(_sendmsg(sock, &msg, 0));
848 }