xref: /netbsd-src/lib/libc/rpc/clnt_vc.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: clnt_vc.c,v 1.2 2000/06/04 03:55:20 thorpej 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 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
37 static char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
38 #else
39 __RCSID("$NetBSD: clnt_vc.c,v 1.2 2000/06/04 03:55:20 thorpej Exp $");
40 #endif
41 #endif
42 
43 /*
44  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
45  *
46  * Copyright (C) 1984, Sun Microsystems, Inc.
47  *
48  * TCP based RPC supports 'batched calls'.
49  * A sequence of calls may be batched-up in a send buffer.  The rpc call
50  * return immediately to the client even though the call was not necessarily
51  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
52  * the rpc timeout value is zero (see clnt.h, rpc).
53  *
54  * Clients should NOT casually batch calls that in fact return results; that is,
55  * the server side should be aware that a call is batched and not produce any
56  * return message.  Batched calls that produce many result messages can
57  * deadlock (netlock) the client and the server....
58  *
59  * Now go hang yourself.
60  */
61 
62 #include "namespace.h"
63 #include "reentrant.h"
64 #include <sys/types.h>
65 #include <sys/poll.h>
66 #include <sys/socket.h>
67 
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 
80 #include "rpc_com.h"
81 
82 #ifdef __weak_alias
83 __weak_alias(clnt_vc_create,_clnt_vc_create)
84 #endif
85 
86 #define MCALL_MSG_SIZE 24
87 
88 static enum clnt_stat clnt_vc_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t,
89     xdrproc_t, caddr_t, struct timeval));
90 static void clnt_vc_geterr __P((CLIENT *, struct rpc_err *));
91 static bool_t clnt_vc_freeres __P((CLIENT *, xdrproc_t, caddr_t));
92 static void clnt_vc_abort __P((CLIENT *));
93 static bool_t clnt_vc_control __P((CLIENT *, u_int, char *));
94 static void clnt_vc_destroy __P((CLIENT *));
95 static struct clnt_ops *clnt_vc_ops __P((void));
96 static bool_t time_not_ok __P((struct timeval *));
97 static int read_vc __P((caddr_t, caddr_t, int));
98 static int write_vc __P((caddr_t, caddr_t, int));
99 
100 struct ct_data {
101 	int		ct_fd;
102 	bool_t		ct_closeit;
103 	struct timeval	ct_wait;
104 	bool_t          ct_waitset;       /* wait set by clnt_control? */
105 	struct netbuf	ct_addr;
106 	struct rpc_err	ct_error;
107 	union {
108 		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
109 		u_int32_t ct_mcalli;
110 	} ct_u;
111 	u_int		ct_mpos;			/* pos after marshal */
112 	XDR		ct_xdrs;
113 };
114 
115 /*
116  *      This machinery implements per-fd locks for MT-safety.  It is not
117  *      sufficient to do per-CLIENT handle locks for MT-safety because a
118  *      user may create more than one CLIENT handle with the same fd behind
119  *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
120  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
121  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
122  *      CLIENT handle created for that fd.
123  *      The current implementation holds locks across the entire RPC and reply.
124  *      Yes, this is silly, and as soon as this code is proven to work, this
125  *      should be the first thing fixed.  One step at a time.
126  */
127 #ifdef __REENT
128 static int      *vc_fd_locks;
129 extern int __rpc_lock_value;
130 extern mutex_t  clnt_fd_lock;
131 static cond_t   *vc_cv;
132 #define release_fd_lock(fd, mask) {             \
133 	mutex_lock(&clnt_fd_lock);      \
134 	vc_fd_locks[fd] = 0;            \
135 	mutex_unlock(&clnt_fd_lock);    \
136 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);        \
137 	cond_signal(&vc_cv[fd]);        \
138 }
139 #else
140 #define release_fd_lock(fd,mask)
141 #define __rpc_lock_value 0
142 #endif
143 
144 
145 /*
146  * Create a client handle for a connection.
147  * Default options are set, which the user can change using clnt_control()'s.
148  * The rpc/vc package does buffering similar to stdio, so the client
149  * must pick send and receive buffer sizes, 0 => use the default.
150  * NB: fd is copied into a private area.
151  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
152  * set this something more useful.
153  *
154  * fd should be an open socket
155  */
156 CLIENT *
157 clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
158 	int fd;
159 	const struct netbuf *raddr;
160 	rpcprog_t prog;
161 	rpcvers_t vers;
162 	u_int sendsz;
163 	u_int recvsz;
164 {
165 	CLIENT *h;
166 	struct ct_data *ct = NULL;
167 	struct timeval now;
168 	struct rpc_msg call_msg;
169 	static u_int32_t disrupt;
170 #ifdef __REENT
171 	sigset_t mask;
172 #endif
173 	sigset_t newmask;
174 	struct sockaddr_storage ss;
175 	socklen_t slen;
176 	struct __rpc_sockinfo si;
177 
178 	if (disrupt == 0)
179 		disrupt = (u_int32_t)(long)raddr;
180 
181 	h  = (CLIENT *)mem_alloc(sizeof(*h));
182 	if (h == NULL) {
183 		warnx("clnt_vc_create: out of memory");
184 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
185 		rpc_createerr.cf_error.re_errno = errno;
186 		goto fooy;
187 	}
188 	ct = (struct ct_data *)mem_alloc(sizeof(*ct));
189 	if (ct == NULL) {
190 		warnx("clnt_vc_create: out of memory");
191 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
192 		rpc_createerr.cf_error.re_errno = errno;
193 		goto fooy;
194 	}
195 
196 	sigfillset(&newmask);
197 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
198 #ifdef __REENT
199 	mutex_lock(&clnt_fd_lock);
200 	if (vc_fd_locks == (int *) NULL) {
201 		int cv_allocsz, fd_allocsz;
202 		int dtbsize = __rpc_dtbsize();
203 
204 		fd_allocsz = dtbsize * sizeof (int);
205 		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
206 		if (vc_fd_locks == (int *) NULL) {
207 			mutex_unlock(&clnt_fd_lock);
208 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
209 			goto fooy;
210 		} else
211 			memset(vc_fd_locks, '\0', fd_allocsz);
212 
213 		assert(vc_cv == (cond_t *) NULL);
214 		cv_allocsz = dtbsize * sizeof (cond_t);
215 		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
216 		if (vc_cv == (cond_t *) NULL) {
217 			mem_free(vc_fd_locks, fd_allocsz);
218 			vc_fd_locks = (int *) NULL;
219 			mutex_unlock(&clnt_fd_lock);
220 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
221 			goto fooy;
222 		} else {
223 			int i;
224 
225 			for (i = 0; i < dtbsize; i++)
226 				cond_init(&vc_cv[i], 0, (void *) 0);
227 		}
228 	} else
229 		assert(vc_cv != (cond_t *) NULL);
230 #endif
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 			goto fooy;
242 		}
243 		if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
244 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
245 			rpc_createerr.cf_error.re_errno = errno;
246 			mutex_unlock(&clnt_fd_lock);
247 			goto fooy;
248 		}
249 	}
250 	mutex_unlock(&clnt_fd_lock);
251 	if (!__rpc_fd2sockinfo(fd, &si))
252 		goto fooy;
253 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
254 
255 	ct->ct_closeit = FALSE;
256 
257 	/*
258 	 * Set up private data struct
259 	 */
260 	ct->ct_fd = fd;
261 	ct->ct_wait.tv_usec = 0;
262 	ct->ct_waitset = FALSE;
263 	ct->ct_addr.buf = malloc(raddr->maxlen);
264 	if (ct->ct_addr.buf == NULL)
265 		goto fooy;
266 	memcpy(ct->ct_addr.buf, &raddr->buf, raddr->len);
267 	ct->ct_addr.len = raddr->maxlen;
268 	ct->ct_addr.maxlen = raddr->maxlen;
269 
270 	/*
271 	 * Initialize call message
272 	 */
273 	(void)gettimeofday(&now, (struct timezone *)0);
274 	call_msg.rm_xid =
275 	    (u_int32_t)((++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec);
276 	call_msg.rm_direction = CALL;
277 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
278 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
279 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
280 
281 	/*
282 	 * pre-serialize the static part of the call msg and stash it away
283 	 */
284 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
285 	    XDR_ENCODE);
286 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
287 		if (ct->ct_closeit) {
288 			(void)close(fd);
289 		}
290 		goto fooy;
291 	}
292 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
293 	XDR_DESTROY(&(ct->ct_xdrs));
294 
295 	/*
296 	 * Create a client handle which uses xdrrec for serialization
297 	 * and authnone for authentication.
298 	 */
299 	h->cl_ops = clnt_vc_ops();
300 	h->cl_private = ct;
301 	h->cl_auth = authnone_create();
302 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
303 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
304 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
305 	    h->cl_private, read_vc, write_vc);
306 	return (h);
307 
308 fooy:
309 	/*
310 	 * Something goofed, free stuff and barf
311 	 */
312 	if (ct)
313 		mem_free(ct, sizeof(struct ct_data));
314 	if (h)
315 		mem_free(h, sizeof(CLIENT));
316 	return ((CLIENT *)NULL);
317 }
318 
319 static enum clnt_stat
320 clnt_vc_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
321 	CLIENT *h;
322 	rpcproc_t proc;
323 	xdrproc_t xdr_args;
324 	caddr_t args_ptr;
325 	xdrproc_t xdr_results;
326 	caddr_t results_ptr;
327 	struct timeval timeout;
328 {
329 	struct ct_data *ct;
330 	XDR *xdrs;
331 	struct rpc_msg reply_msg;
332 	u_int32_t x_id;
333 	u_int32_t *msg_x_id;
334 	bool_t shipnow;
335 	int refreshes = 2;
336 #ifdef __REENT
337 	sigset_t mask, newmask;
338 #endif
339 
340 	_DIAGASSERT(h != NULL);
341 
342 #ifdef __REENT
343 	sigfillset(&newmask);
344 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
345 	mutex_lock(&clnt_fd_lock);
346 	while (vc_fd_locks[ct->ct_fd])
347 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
348 	vc_fd_locks[ct->ct_fd] = lock_value;
349 	mutex_unlock(&clnt_fd_lock);
350 #endif
351 
352 	ct = (struct ct_data *) h->cl_private;
353 	xdrs = &(ct->ct_xdrs);
354 	msg_x_id = &ct->ct_u.ct_mcalli;
355 
356 	if (!ct->ct_waitset) {
357 		if (time_not_ok(&timeout) == FALSE)
358 		ct->ct_wait = timeout;
359 	}
360 
361 	shipnow =
362 	    (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
363 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
364 
365 call_again:
366 	xdrs->x_op = XDR_ENCODE;
367 	ct->ct_error.re_status = RPC_SUCCESS;
368 	x_id = ntohl(--(*msg_x_id));
369 	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
370 	    (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
371 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
372 	    (! (*xdr_args)(xdrs, 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 	for (;;) {
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(h->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 (! (*xdr_results)(xdrs, results_ptr)) {
429 			if (ct->ct_error.re_status == RPC_SUCCESS)
430 				ct->ct_error.re_status = RPC_CANTDECODERES;
431 		}
432 		/* free verifier ... */
433 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
434 			xdrs->x_op = XDR_FREE;
435 			(void)xdr_opaque_auth(xdrs,
436 			    &(reply_msg.acpted_rply.ar_verf));
437 		}
438 	}  /* end successful completion */
439 	else {
440 		/* maybe our credentials need to be refreshed ... */
441 		if (refreshes-- && AUTH_REFRESH(h->cl_auth))
442 			goto call_again;
443 	}  /* end of unsuccessful completion */
444 	release_fd_lock(ct->ct_fd, mask);
445 	return (ct->ct_error.re_status);
446 }
447 
448 static void
449 clnt_vc_geterr(h, errp)
450 	CLIENT *h;
451 	struct rpc_err *errp;
452 {
453 	struct ct_data *ct;
454 
455 	_DIAGASSERT(h != NULL);
456 	_DIAGASSERT(errp != NULL);
457 
458 	ct = (struct ct_data *) h->cl_private;
459 	*errp = ct->ct_error;
460 }
461 
462 static bool_t
463 clnt_vc_freeres(cl, xdr_res, res_ptr)
464 	CLIENT *cl;
465 	xdrproc_t xdr_res;
466 	caddr_t res_ptr;
467 {
468 	struct ct_data *ct;
469 	XDR *xdrs;
470 	bool_t dummy;
471 #ifdef __REENT
472 	sigset_t mask;
473 #endif
474 	sigset_t newmask;
475 
476 	_DIAGASSERT(cl != NULL);
477 
478 	ct = (struct ct_data *)cl->cl_private;
479 	xdrs = &(ct->ct_xdrs);
480 
481 	sigfillset(&newmask);
482 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
483 	mutex_lock(&clnt_fd_lock);
484 #ifdef __REENT
485 	while (vc_fd_locks[ct->ct_fd])
486 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
487 #endif
488 
489 	xdrs->x_op = XDR_FREE;
490 	dummy = (*xdr_res)(xdrs, res_ptr);
491 	mutex_unlock(&clnt_fd_lock);
492 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
493 	cond_signal(&vc_cv[ct->ct_fd]);
494 
495 	return dummy;
496 }
497 
498 /*ARGSUSED*/
499 static void
500 clnt_vc_abort(cl)
501 	CLIENT *cl;
502 {
503 }
504 
505 static bool_t
506 clnt_vc_control(cl, request, info)
507 	CLIENT *cl;
508 	u_int request;
509 	char *info;
510 {
511 	struct ct_data *ct;
512 	void *infop = info;
513 #ifdef _REENT
514 	sigset_t mask;
515 #endif
516 	sigset_t newmask;
517 
518 	_DIAGASSERT(cl != NULL);
519 
520 	ct = (struct ct_data *)cl->cl_private;
521 
522 	sigfillset(&newmask);
523 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
524 	mutex_lock(&clnt_fd_lock);
525 #ifdef __REENT
526 	while (vc_fd_locks[ct->ct_fd])
527 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
528 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
529 #endif
530 	mutex_unlock(&clnt_fd_lock);
531 
532 	switch (request) {
533 	case CLSET_FD_CLOSE:
534 		ct->ct_closeit = TRUE;
535 		release_fd_lock(ct->ct_fd, mask);
536 		return (TRUE);
537 	case CLSET_FD_NCLOSE:
538 		ct->ct_closeit = FALSE;
539 		release_fd_lock(ct->ct_fd, mask);
540 		return (TRUE);
541 	default:
542 		break;
543 	}
544 
545 	/* for other requests which use info */
546 	if (info == NULL) {
547 		release_fd_lock(ct->ct_fd, mask);
548 		return (FALSE);
549 	}
550 	switch (request) {
551 	case CLSET_TIMEOUT:
552 		if (time_not_ok((struct timeval *)info)) {
553 			release_fd_lock(ct->ct_fd, mask);
554 			return (FALSE);
555 		}
556 		ct->ct_wait = *(struct timeval *)infop;
557 		ct->ct_waitset = TRUE;
558 		break;
559 	case CLGET_TIMEOUT:
560 		*(struct timeval *)infop = ct->ct_wait;
561 		break;
562 	case CLGET_SERVER_ADDR:
563 		(void) memcpy(info, ct->ct_addr.buf, (int)ct->ct_addr.len);
564 		break;
565 	case CLGET_FD:
566 		*(int *)info = ct->ct_fd;
567 		break;
568 	case CLGET_SVC_ADDR:
569 		/* The caller should not free this memory area */
570 		*(struct netbuf *)info = ct->ct_addr;
571 		break;
572 	case CLSET_SVC_ADDR:		/* set to new address */
573 		release_fd_lock(ct->ct_fd, mask);
574 		return (FALSE);
575 	case CLGET_XID:
576 		/*
577 		 * use the knowledge that xid is the
578 		 * first element in the call structure
579 		 * This will get the xid of the PREVIOUS call
580 		 */
581 		*(u_int32_t *)info = ntohl(*(u_int32_t *)&ct->ct_u.ct_mcalli);
582 		break;
583 	case CLSET_XID:
584 		/* This will set the xid of the NEXT call */
585 		*(u_int32_t *)&ct->ct_u.ct_mcalli =
586 		    htonl(*(u_int32_t *)info + 1);
587 		/* increment by 1 as clnt_vc_call() decrements once */
588 		break;
589 	case CLGET_VERS:
590 		/*
591 		 * This RELIES on the information that, in the call body,
592 		 * the version number field is the fifth field from the
593 		 * begining of the RPC header. MUST be changed if the
594 		 * call_struct is changed
595 		 */
596 		*(u_int32_t *)info = ntohl(*(u_int32_t *)(ct->ct_u.ct_mcallc +
597 						4 * BYTES_PER_XDR_UNIT));
598 		break;
599 
600 	case CLSET_VERS:
601 		*(u_int32_t *)(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT) =
602 			htonl(*(u_int32_t *)info);
603 		break;
604 
605 	case CLGET_PROG:
606 		/*
607 		 * This RELIES on the information that, in the call body,
608 		 * the program number field is the fourth field from the
609 		 * begining of the RPC header. MUST be changed if the
610 		 * call_struct is changed
611 		 */
612 		*(u_int32_t *)info = ntohl(*(u_int32_t *)(ct->ct_u.ct_mcallc +
613 						3 * BYTES_PER_XDR_UNIT));
614 		break;
615 
616 	case CLSET_PROG:
617 		*(u_int32_t *)(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT) =
618 			htonl(*(u_int32_t *)info);
619 		break;
620 
621 	default:
622 		release_fd_lock(ct->ct_fd, mask);
623 		return (FALSE);
624 	}
625 	release_fd_lock(ct->ct_fd, mask);
626 	return (TRUE);
627 }
628 
629 
630 static void
631 clnt_vc_destroy(cl)
632 	CLIENT *cl;
633 {
634 	struct ct_data *ct;
635 #ifdef __REENT
636 	int ct_fd = ct->ct_fd;
637 	sigset_t mask;
638 #endif
639 	sigset_t newmask;
640 
641 	_DIAGASSERT(cl != NULL);
642 
643 	ct = (struct ct_data *) cl->cl_private;
644 
645 	sigfillset(&newmask);
646 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
647 	mutex_lock(&clnt_fd_lock);
648 #ifdef _REENT
649 	while (vc_fd_locks[ct_fd])
650 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
651 #endif
652 	if (ct->ct_closeit && ct->ct_fd != -1) {
653 		(void)close(ct->ct_fd);
654 	}
655 	XDR_DESTROY(&(ct->ct_xdrs));
656 	if (ct->ct_addr.buf)
657 		free(ct->ct_addr.buf);
658 	mem_free(ct, sizeof(struct ct_data));
659 	mem_free(cl, sizeof(CLIENT));
660 	mutex_unlock(&clnt_fd_lock);
661 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
662 
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 	caddr_t ctp;
674 	caddr_t buf;
675 	int len;
676 {
677 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
678 	struct pollfd fd;
679 	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
680 	    (ct->ct_wait.tv_usec / 1000));
681 
682 	if (len == 0)
683 		return (0);
684 	fd.fd = ct->ct_fd;
685 	fd.events = POLLIN;
686 	for (;;) {
687 		switch (poll(&fd, 1, milliseconds)) {
688 		case 0:
689 			ct->ct_error.re_status = RPC_TIMEDOUT;
690 			return (-1);
691 
692 		case -1:
693 			if (errno == EINTR)
694 				continue;
695 			ct->ct_error.re_status = RPC_CANTRECV;
696 			ct->ct_error.re_errno = errno;
697 			return (-1);
698 		}
699 		break;
700 	}
701 	switch (len = read(ct->ct_fd, buf, (size_t)len)) {
702 
703 	case 0:
704 		/* premature eof */
705 		ct->ct_error.re_errno = ECONNRESET;
706 		ct->ct_error.re_status = RPC_CANTRECV;
707 		len = -1;  /* it's really an error */
708 		break;
709 
710 	case -1:
711 		ct->ct_error.re_errno = errno;
712 		ct->ct_error.re_status = RPC_CANTRECV;
713 		break;
714 	}
715 	return (len);
716 }
717 
718 static int
719 write_vc(ctp, buf, len)
720 	caddr_t ctp;
721 	caddr_t buf;
722 	int len;
723 {
724 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
725 	int i, cnt;
726 
727 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
728 		if ((i = write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
729 			ct->ct_error.re_errno = errno;
730 			ct->ct_error.re_status = RPC_CANTSEND;
731 			return (-1);
732 		}
733 	}
734 	return (len);
735 }
736 
737 static struct clnt_ops *
738 clnt_vc_ops()
739 {
740 	static struct clnt_ops ops;
741 #ifdef __REENT
742 	extern mutex_t  ops_lock;
743 	sigset_t mask;
744 #endif
745 	sigset_t newmask;
746 
747 	/* VARIABLES PROTECTED BY ops_lock: ops */
748 
749 	sigfillset(&newmask);
750 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
751 	mutex_lock(&ops_lock);
752 	if (ops.cl_call == NULL) {
753 		ops.cl_call = clnt_vc_call;
754 		ops.cl_abort = clnt_vc_abort;
755 		ops.cl_geterr = clnt_vc_geterr;
756 		ops.cl_freeres = clnt_vc_freeres;
757 		ops.cl_destroy = clnt_vc_destroy;
758 		ops.cl_control = clnt_vc_control;
759 	}
760 	mutex_unlock(&ops_lock);
761 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
762 	return (&ops);
763 }
764 
765 /*
766  * Make sure that the time is not garbage.   -1 value is disallowed.
767  * Note this is different from time_not_ok in clnt_dg.c
768  */
769 static bool_t
770 time_not_ok(t)
771 	struct timeval *t;
772 {
773 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
774 		t->tv_usec <= -1 || t->tv_usec > 1000000);
775 }
776