xref: /netbsd/lib/libc/rpc/clnt_vc.c (revision bf9ec67e)
1 /*	$NetBSD: clnt_vc.c,v 1.7 2001/01/04 14:42:19 lukem 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.7 2001/01/04 14:42:19 lukem 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 	_DIAGASSERT(raddr != NULL);
179 
180 	if (disrupt == 0)
181 		disrupt = (u_int32_t)(long)raddr;
182 
183 	h  = mem_alloc(sizeof(*h));
184 	if (h == NULL) {
185 		warnx("clnt_vc_create: out of memory");
186 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
187 		rpc_createerr.cf_error.re_errno = errno;
188 		goto fooy;
189 	}
190 	ct = mem_alloc(sizeof(*ct));
191 	if (ct == NULL) {
192 		warnx("clnt_vc_create: out of memory");
193 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
194 		rpc_createerr.cf_error.re_errno = errno;
195 		goto fooy;
196 	}
197 
198 	sigfillset(&newmask);
199 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
200 #ifdef __REENT
201 	mutex_lock(&clnt_fd_lock);
202 	if (vc_fd_locks == (int *) NULL) {
203 		int cv_allocsz, fd_allocsz;
204 		int dtbsize = __rpc_dtbsize();
205 
206 		fd_allocsz = dtbsize * sizeof (int);
207 		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
208 		if (vc_fd_locks == (int *) NULL) {
209 			mutex_unlock(&clnt_fd_lock);
210 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
211 			goto fooy;
212 		} else
213 			memset(vc_fd_locks, '\0', fd_allocsz);
214 
215 		assert(vc_cv == (cond_t *) NULL);
216 		cv_allocsz = dtbsize * sizeof (cond_t);
217 		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
218 		if (vc_cv == (cond_t *) NULL) {
219 			mem_free(vc_fd_locks, fd_allocsz);
220 			vc_fd_locks = (int *) NULL;
221 			mutex_unlock(&clnt_fd_lock);
222 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
223 			goto fooy;
224 		} else {
225 			int i;
226 
227 			for (i = 0; i < dtbsize; i++)
228 				cond_init(&vc_cv[i], 0, (void *) 0);
229 		}
230 	} else
231 		assert(vc_cv != (cond_t *) NULL);
232 #endif
233 
234 	/*
235 	 * XXX - fvdl connecting while holding a mutex?
236 	 */
237 	slen = sizeof ss;
238 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
239 		if (errno != ENOTCONN) {
240 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
241 			rpc_createerr.cf_error.re_errno = errno;
242 			mutex_unlock(&clnt_fd_lock);
243 			goto fooy;
244 		}
245 		if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
246 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
247 			rpc_createerr.cf_error.re_errno = errno;
248 			mutex_unlock(&clnt_fd_lock);
249 			goto fooy;
250 		}
251 	}
252 	mutex_unlock(&clnt_fd_lock);
253 	if (!__rpc_fd2sockinfo(fd, &si))
254 		goto fooy;
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 fooy;
268 	memcpy(ct->ct_addr.buf, &raddr->buf, raddr->len);
269 	ct->ct_addr.len = raddr->maxlen;
270 	ct->ct_addr.maxlen = raddr->maxlen;
271 
272 	/*
273 	 * Initialize call message
274 	 */
275 	(void)gettimeofday(&now, NULL);
276 	call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now);
277 	call_msg.rm_direction = CALL;
278 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
279 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
280 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
281 
282 	/*
283 	 * pre-serialize the static part of the call msg and stash it away
284 	 */
285 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
286 	    XDR_ENCODE);
287 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
288 		if (ct->ct_closeit) {
289 			(void)close(fd);
290 		}
291 		goto fooy;
292 	}
293 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
294 	XDR_DESTROY(&(ct->ct_xdrs));
295 
296 	/*
297 	 * Create a client handle which uses xdrrec for serialization
298 	 * and authnone for authentication.
299 	 */
300 	h->cl_ops = clnt_vc_ops();
301 	h->cl_private = ct;
302 	h->cl_auth = authnone_create();
303 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
304 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
305 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
306 	    h->cl_private, read_vc, write_vc);
307 	return (h);
308 
309 fooy:
310 	/*
311 	 * Something goofed, free stuff and barf
312 	 */
313 	if (ct)
314 		mem_free(ct, sizeof(struct ct_data));
315 	if (h)
316 		mem_free(h, sizeof(CLIENT));
317 	return (NULL);
318 }
319 
320 static enum clnt_stat
321 clnt_vc_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
322 	CLIENT *h;
323 	rpcproc_t proc;
324 	xdrproc_t xdr_args;
325 	caddr_t args_ptr;
326 	xdrproc_t xdr_results;
327 	caddr_t results_ptr;
328 	struct timeval timeout;
329 {
330 	struct ct_data *ct;
331 	XDR *xdrs;
332 	struct rpc_msg reply_msg;
333 	u_int32_t x_id;
334 	u_int32_t *msg_x_id;
335 	bool_t shipnow;
336 	int refreshes = 2;
337 #ifdef __REENT
338 	sigset_t mask, newmask;
339 #endif
340 
341 	_DIAGASSERT(h != NULL);
342 
343 #ifdef __REENT
344 	sigfillset(&newmask);
345 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
346 	mutex_lock(&clnt_fd_lock);
347 	while (vc_fd_locks[ct->ct_fd])
348 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
349 	vc_fd_locks[ct->ct_fd] = lock_value;
350 	mutex_unlock(&clnt_fd_lock);
351 #endif
352 
353 	ct = (struct ct_data *) h->cl_private;
354 	xdrs = &(ct->ct_xdrs);
355 	msg_x_id = &ct->ct_u.ct_mcalli;
356 
357 	if (!ct->ct_waitset) {
358 		if (time_not_ok(&timeout) == FALSE)
359 		ct->ct_wait = timeout;
360 	}
361 
362 	shipnow =
363 	    (xdr_results == NULL && timeout.tv_sec == 0
364 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
365 
366 call_again:
367 	xdrs->x_op = XDR_ENCODE;
368 	ct->ct_error.re_status = RPC_SUCCESS;
369 	x_id = ntohl(--(*msg_x_id));
370 	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
371 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
372 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
373 	    (! (*xdr_args)(xdrs, args_ptr))) {
374 		if (ct->ct_error.re_status == RPC_SUCCESS)
375 			ct->ct_error.re_status = RPC_CANTENCODEARGS;
376 		(void)xdrrec_endofrecord(xdrs, TRUE);
377 		release_fd_lock(ct->ct_fd, mask);
378 		return (ct->ct_error.re_status);
379 	}
380 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
381 		release_fd_lock(ct->ct_fd, mask);
382 		return (ct->ct_error.re_status = RPC_CANTSEND);
383 	}
384 	if (! shipnow) {
385 		release_fd_lock(ct->ct_fd, mask);
386 		return (RPC_SUCCESS);
387 	}
388 	/*
389 	 * Hack to provide rpc-based message passing
390 	 */
391 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
392 		release_fd_lock(ct->ct_fd, mask);
393 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
394 	}
395 
396 
397 	/*
398 	 * Keep receiving until we get a valid transaction id
399 	 */
400 	xdrs->x_op = XDR_DECODE;
401 	for (;;) {
402 		reply_msg.acpted_rply.ar_verf = _null_auth;
403 		reply_msg.acpted_rply.ar_results.where = NULL;
404 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
405 		if (! xdrrec_skiprecord(xdrs)) {
406 			release_fd_lock(ct->ct_fd, mask);
407 			return (ct->ct_error.re_status);
408 		}
409 		/* now decode and validate the response header */
410 		if (! xdr_replymsg(xdrs, &reply_msg)) {
411 			if (ct->ct_error.re_status == RPC_SUCCESS)
412 				continue;
413 			release_fd_lock(ct->ct_fd, mask);
414 			return (ct->ct_error.re_status);
415 		}
416 		if (reply_msg.rm_xid == x_id)
417 			break;
418 	}
419 
420 	/*
421 	 * process header
422 	 */
423 	_seterr_reply(&reply_msg, &(ct->ct_error));
424 	if (ct->ct_error.re_status == RPC_SUCCESS) {
425 		if (! AUTH_VALIDATE(h->cl_auth,
426 		    &reply_msg.acpted_rply.ar_verf)) {
427 			ct->ct_error.re_status = RPC_AUTHERROR;
428 			ct->ct_error.re_why = AUTH_INVALIDRESP;
429 		} else if (! (*xdr_results)(xdrs, 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(h->cl_auth))
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(h, errp)
451 	CLIENT *h;
452 	struct rpc_err *errp;
453 {
454 	struct ct_data *ct;
455 
456 	_DIAGASSERT(h != NULL);
457 	_DIAGASSERT(errp != NULL);
458 
459 	ct = (struct ct_data *) h->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 	caddr_t res_ptr;
468 {
469 	struct ct_data *ct;
470 	XDR *xdrs;
471 	bool_t dummy;
472 #ifdef __REENT
473 	sigset_t mask;
474 #endif
475 	sigset_t newmask;
476 
477 	_DIAGASSERT(cl != NULL);
478 
479 	ct = (struct ct_data *)cl->cl_private;
480 	xdrs = &(ct->ct_xdrs);
481 
482 	sigfillset(&newmask);
483 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
484 	mutex_lock(&clnt_fd_lock);
485 #ifdef __REENT
486 	while (vc_fd_locks[ct->ct_fd])
487 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
488 #endif
489 
490 	xdrs->x_op = XDR_FREE;
491 	dummy = (*xdr_res)(xdrs, res_ptr);
492 	mutex_unlock(&clnt_fd_lock);
493 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
494 	cond_signal(&vc_cv[ct->ct_fd]);
495 
496 	return dummy;
497 }
498 
499 /*ARGSUSED*/
500 static void
501 clnt_vc_abort(cl)
502 	CLIENT *cl;
503 {
504 }
505 
506 static bool_t
507 clnt_vc_control(cl, request, info)
508 	CLIENT *cl;
509 	u_int request;
510 	char *info;
511 {
512 	struct ct_data *ct;
513 	void *infop = info;
514 #ifdef _REENT
515 	sigset_t mask;
516 #endif
517 	sigset_t newmask;
518 
519 	_DIAGASSERT(cl != NULL);
520 
521 	ct = (struct ct_data *)cl->cl_private;
522 
523 	sigfillset(&newmask);
524 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
525 	mutex_lock(&clnt_fd_lock);
526 #ifdef __REENT
527 	while (vc_fd_locks[ct->ct_fd])
528 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
529 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
530 #endif
531 	mutex_unlock(&clnt_fd_lock);
532 
533 	switch (request) {
534 	case CLSET_FD_CLOSE:
535 		ct->ct_closeit = TRUE;
536 		release_fd_lock(ct->ct_fd, mask);
537 		return (TRUE);
538 	case CLSET_FD_NCLOSE:
539 		ct->ct_closeit = FALSE;
540 		release_fd_lock(ct->ct_fd, mask);
541 		return (TRUE);
542 	default:
543 		break;
544 	}
545 
546 	/* for other requests which use info */
547 	if (info == NULL) {
548 		release_fd_lock(ct->ct_fd, mask);
549 		return (FALSE);
550 	}
551 	switch (request) {
552 	case CLSET_TIMEOUT:
553 		if (time_not_ok((struct timeval *)(void *)info)) {
554 			release_fd_lock(ct->ct_fd, mask);
555 			return (FALSE);
556 		}
557 		ct->ct_wait = *(struct timeval *)infop;
558 		ct->ct_waitset = TRUE;
559 		break;
560 	case CLGET_TIMEOUT:
561 		*(struct timeval *)infop = ct->ct_wait;
562 		break;
563 	case CLGET_SERVER_ADDR:
564 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
565 		break;
566 	case CLGET_FD:
567 		*(int *)(void *)info = ct->ct_fd;
568 		break;
569 	case CLGET_SVC_ADDR:
570 		/* The caller should not free this memory area */
571 		*(struct netbuf *)(void *)info = ct->ct_addr;
572 		break;
573 	case CLSET_SVC_ADDR:		/* set to new address */
574 		release_fd_lock(ct->ct_fd, mask);
575 		return (FALSE);
576 	case CLGET_XID:
577 		/*
578 		 * use the knowledge that xid is the
579 		 * first element in the call structure
580 		 * This will get the xid of the PREVIOUS call
581 		 */
582 		*(u_int32_t *)(void *)info =
583 		    ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
584 		break;
585 	case CLSET_XID:
586 		/* This will set the xid of the NEXT call */
587 		*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
588 		    htonl(*((u_int32_t *)(void *)info) + 1);
589 		/* increment by 1 as clnt_vc_call() decrements once */
590 		break;
591 	case CLGET_VERS:
592 		/*
593 		 * This RELIES on the information that, in the call body,
594 		 * the version number field is the fifth field from the
595 		 * begining of the RPC header. MUST be changed if the
596 		 * call_struct is changed
597 		 */
598 		*(u_int32_t *)(void *)info =
599 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
600 		    4 * BYTES_PER_XDR_UNIT));
601 		break;
602 
603 	case CLSET_VERS:
604 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
605 		    4 * BYTES_PER_XDR_UNIT) =
606 		    htonl(*(u_int32_t *)(void *)info);
607 		break;
608 
609 	case CLGET_PROG:
610 		/*
611 		 * This RELIES on the information that, in the call body,
612 		 * the program number field is the fourth field from the
613 		 * begining of the RPC header. MUST be changed if the
614 		 * call_struct is changed
615 		 */
616 		*(u_int32_t *)(void *)info =
617 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
618 		    3 * BYTES_PER_XDR_UNIT));
619 		break;
620 
621 	case CLSET_PROG:
622 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
623 		    3 * BYTES_PER_XDR_UNIT) =
624 		    htonl(*(u_int32_t *)(void *)info);
625 		break;
626 
627 	default:
628 		release_fd_lock(ct->ct_fd, mask);
629 		return (FALSE);
630 	}
631 	release_fd_lock(ct->ct_fd, mask);
632 	return (TRUE);
633 }
634 
635 
636 static void
637 clnt_vc_destroy(cl)
638 	CLIENT *cl;
639 {
640 	struct ct_data *ct;
641 #ifdef __REENT
642 	int ct_fd = ct->ct_fd;
643 	sigset_t mask;
644 #endif
645 	sigset_t newmask;
646 
647 	_DIAGASSERT(cl != NULL);
648 
649 	ct = (struct ct_data *) cl->cl_private;
650 
651 	sigfillset(&newmask);
652 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
653 	mutex_lock(&clnt_fd_lock);
654 #ifdef _REENT
655 	while (vc_fd_locks[ct_fd])
656 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
657 #endif
658 	if (ct->ct_closeit && ct->ct_fd != -1) {
659 		(void)close(ct->ct_fd);
660 	}
661 	XDR_DESTROY(&(ct->ct_xdrs));
662 	if (ct->ct_addr.buf)
663 		free(ct->ct_addr.buf);
664 	mem_free(ct, sizeof(struct ct_data));
665 	mem_free(cl, sizeof(CLIENT));
666 	mutex_unlock(&clnt_fd_lock);
667 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
668 
669 	cond_signal(&vc_cv[ct_fd]);
670 }
671 
672 /*
673  * Interface between xdr serializer and tcp connection.
674  * Behaves like the system calls, read & write, but keeps some error state
675  * around for the rpc level.
676  */
677 static int
678 read_vc(ctp, buf, len)
679 	caddr_t ctp;
680 	caddr_t buf;
681 	int len;
682 {
683 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
684 	struct pollfd fd;
685 	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
686 	    (ct->ct_wait.tv_usec / 1000));
687 
688 	if (len == 0)
689 		return (0);
690 	fd.fd = ct->ct_fd;
691 	fd.events = POLLIN;
692 	for (;;) {
693 		switch (poll(&fd, 1, milliseconds)) {
694 		case 0:
695 			ct->ct_error.re_status = RPC_TIMEDOUT;
696 			return (-1);
697 
698 		case -1:
699 			if (errno == EINTR)
700 				continue;
701 			ct->ct_error.re_status = RPC_CANTRECV;
702 			ct->ct_error.re_errno = errno;
703 			return (-1);
704 		}
705 		break;
706 	}
707 	switch (len = read(ct->ct_fd, buf, (size_t)len)) {
708 
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 	caddr_t ctp;
727 	caddr_t buf;
728 	int len;
729 {
730 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
731 	int i, 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 #ifdef __REENT
748 	extern mutex_t  ops_lock;
749 	sigset_t mask;
750 #endif
751 	sigset_t newmask;
752 
753 	/* VARIABLES PROTECTED BY ops_lock: ops */
754 
755 	sigfillset(&newmask);
756 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
757 	mutex_lock(&ops_lock);
758 	if (ops.cl_call == NULL) {
759 		ops.cl_call = clnt_vc_call;
760 		ops.cl_abort = clnt_vc_abort;
761 		ops.cl_geterr = clnt_vc_geterr;
762 		ops.cl_freeres = clnt_vc_freeres;
763 		ops.cl_destroy = clnt_vc_destroy;
764 		ops.cl_control = clnt_vc_control;
765 	}
766 	mutex_unlock(&ops_lock);
767 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
768 	return (&ops);
769 }
770 
771 /*
772  * Make sure that the time is not garbage.   -1 value is disallowed.
773  * Note this is different from time_not_ok in clnt_dg.c
774  */
775 static bool_t
776 time_not_ok(t)
777 	struct timeval *t;
778 {
779 
780 	_DIAGASSERT(t != NULL);
781 
782 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
783 		t->tv_usec <= -1 || t->tv_usec > 1000000);
784 }
785