xref: /freebsd/lib/libc/rpc/clnt_dg.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
18360efbdSAlfred Perlstein /*	$NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $	*/
28360efbdSAlfred Perlstein 
32e322d37SHiroki Sato /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
62e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato  * All rights reserved.
88360efbdSAlfred Perlstein  *
92e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
132e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
162e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato  *   from this software without specific prior written permission.
198360efbdSAlfred Perlstein  *
202e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
318360efbdSAlfred Perlstein  */
328360efbdSAlfred Perlstein /*
338360efbdSAlfred Perlstein  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
348360efbdSAlfred Perlstein  */
358360efbdSAlfred Perlstein 
368360efbdSAlfred Perlstein /*
378360efbdSAlfred Perlstein  * Implements a connectionless client side RPC.
388360efbdSAlfred Perlstein  */
398360efbdSAlfred Perlstein 
408360efbdSAlfred Perlstein #include "namespace.h"
419f5afc13SIan Dowse #include "reentrant.h"
428360efbdSAlfred Perlstein #include <sys/types.h>
43d7cd6ba3SIan Dowse #include <sys/event.h>
448360efbdSAlfred Perlstein #include <sys/time.h>
458360efbdSAlfred Perlstein #include <sys/socket.h>
468360efbdSAlfred Perlstein #include <sys/ioctl.h>
47*24938f93SAlan Somers #include <sys/tree.h>
48fd8e4ebcSMike Barcroft #include <arpa/inet.h>
498360efbdSAlfred Perlstein #include <rpc/rpc.h>
508f55a568SDoug Rabson #include <rpc/rpcsec_gss.h>
51*24938f93SAlan Somers #include <assert.h>
528360efbdSAlfred Perlstein #include <errno.h>
53*24938f93SAlan Somers #include <pthread.h>
548360efbdSAlfred Perlstein #include <stdlib.h>
558360efbdSAlfred Perlstein #include <string.h>
568360efbdSAlfred Perlstein #include <signal.h>
57*24938f93SAlan Somers #include <stdbool.h>
588360efbdSAlfred Perlstein #include <unistd.h>
598360efbdSAlfred Perlstein #include <err.h>
608360efbdSAlfred Perlstein #include "un-namespace.h"
618360efbdSAlfred Perlstein #include "rpc_com.h"
62235baf26SDaniel Eischen #include "mt_misc.h"
638360efbdSAlfred Perlstein 
648360efbdSAlfred Perlstein 
653dd425f7SSimon L. B. Nielsen #ifdef _FREEFALL_CONFIG
663dd425f7SSimon L. B. Nielsen /*
673dd425f7SSimon L. B. Nielsen  * Disable RPC exponential back-off for FreeBSD.org systems.
683dd425f7SSimon L. B. Nielsen  */
693dd425f7SSimon L. B. Nielsen #define	RPC_MAX_BACKOFF		1 /* second */
703dd425f7SSimon L. B. Nielsen #else
718360efbdSAlfred Perlstein #define	RPC_MAX_BACKOFF		30 /* seconds */
723dd425f7SSimon L. B. Nielsen #endif
738360efbdSAlfred Perlstein 
748360efbdSAlfred Perlstein 
75c05ac53bSDavid E. O'Brien static struct clnt_ops *clnt_dg_ops(void);
76c05ac53bSDavid E. O'Brien static bool_t time_not_ok(struct timeval *);
77f249dbccSDag-Erling Smørgrav static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
78f249dbccSDag-Erling Smørgrav 	    xdrproc_t, void *, struct timeval);
79c05ac53bSDavid E. O'Brien static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
80f249dbccSDag-Erling Smørgrav static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
81c05ac53bSDavid E. O'Brien static void clnt_dg_abort(CLIENT *);
82cffc0b57SAlfred Perlstein static bool_t clnt_dg_control(CLIENT *, u_int, void *);
83c05ac53bSDavid E. O'Brien static void clnt_dg_destroy(CLIENT *);
848360efbdSAlfred Perlstein 
858360efbdSAlfred Perlstein 
868360efbdSAlfred Perlstein 
878360efbdSAlfred Perlstein 
888360efbdSAlfred Perlstein /*
898360efbdSAlfred Perlstein  *	This machinery implements per-fd locks for MT-safety.  It is not
908360efbdSAlfred Perlstein  *	sufficient to do per-CLIENT handle locks for MT-safety because a
918360efbdSAlfred Perlstein  *	user may create more than one CLIENT handle with the same fd behind
92*24938f93SAlan Somers  *	it.  Therefore, we allocate an associative array of flags and condition
93*24938f93SAlan Somers  *	variables (dg_fd).  The flags and the array are protected by the
94*24938f93SAlan Somers  *	clnt_fd_lock mutex.  dg_fd[fd].lock == 1 => a call is active on some
95*24938f93SAlan Somers  *	CLIENT handle created for that fd.  The current implementation holds
96*24938f93SAlan Somers  *	locks across the entire RPC and reply, including retransmissions.  Yes,
97*24938f93SAlan Somers  *	this is silly, and as soon as this code is proven to work, this should
98*24938f93SAlan Somers  *	be the first thing fixed.  One step at a time.
998360efbdSAlfred Perlstein  */
100*24938f93SAlan Somers struct dg_fd {
101*24938f93SAlan Somers 	RB_ENTRY(dg_fd) dg_link;
102*24938f93SAlan Somers 	int fd;
103*24938f93SAlan Somers 	mutex_t mtx;
104*24938f93SAlan Somers };
105*24938f93SAlan Somers static inline int
cmp_dg_fd(struct dg_fd * a,struct dg_fd * b)106*24938f93SAlan Somers cmp_dg_fd(struct dg_fd *a, struct dg_fd *b)
107a5c2f4e9SAlan Somers {
108*24938f93SAlan Somers 	if (a->fd > b->fd) {
109*24938f93SAlan Somers 		return (1);
110*24938f93SAlan Somers 	} else if (a->fd < b->fd) {
111*24938f93SAlan Somers 		return (-1);
112*24938f93SAlan Somers 	} else {
113*24938f93SAlan Somers 		return (0);
114*24938f93SAlan Somers 	}
115*24938f93SAlan Somers }
116*24938f93SAlan Somers RB_HEAD(dg_fd_list, dg_fd);
117*24938f93SAlan Somers RB_PROTOTYPE(dg_fd_list, dg_fd, dg_link, cmp_dg_fd);
118*24938f93SAlan Somers RB_GENERATE(dg_fd_list, dg_fd, dg_link, cmp_dg_fd);
119*24938f93SAlan Somers struct dg_fd_list dg_fd_head = RB_INITIALIZER(&dg_fd_head);
120*24938f93SAlan Somers 
121*24938f93SAlan Somers /*
122*24938f93SAlan Somers  * Find the lock structure for the given file descriptor, or initialize it if
123*24938f93SAlan Somers  * it does not already exist.  The clnt_fd_lock mutex must be held.
124*24938f93SAlan Somers  */
125*24938f93SAlan Somers static struct dg_fd *
dg_fd_find(int fd)126*24938f93SAlan Somers dg_fd_find(int fd)
127*24938f93SAlan Somers {
128*24938f93SAlan Somers 	struct dg_fd key, *elem;
129*24938f93SAlan Somers 
130*24938f93SAlan Somers 	key.fd = fd;
131*24938f93SAlan Somers 	elem = RB_FIND(dg_fd_list, &dg_fd_head, &key);
132*24938f93SAlan Somers 	if (elem == NULL) {
133*24938f93SAlan Somers 		elem = calloc(1, sizeof(*elem));
134*24938f93SAlan Somers 		elem->fd = fd;
135*24938f93SAlan Somers 		mutex_init(&elem->mtx, NULL);
136*24938f93SAlan Somers 		RB_INSERT(dg_fd_list, &dg_fd_head, elem);
137*24938f93SAlan Somers 	}
138*24938f93SAlan Somers 	return (elem);
139*24938f93SAlan Somers }
140*24938f93SAlan Somers 
141*24938f93SAlan Somers static void
release_fd_lock(struct dg_fd * elem,sigset_t mask)142*24938f93SAlan Somers release_fd_lock(struct dg_fd *elem, sigset_t mask)
143*24938f93SAlan Somers {
144*24938f93SAlan Somers 	mutex_unlock(&elem->mtx);
145a5c2f4e9SAlan Somers 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1468360efbdSAlfred Perlstein }
1478360efbdSAlfred Perlstein 
1488360efbdSAlfred Perlstein static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
1498360efbdSAlfred Perlstein 
150a5c2f4e9SAlan Somers /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd */
1518360efbdSAlfred Perlstein 
1528f55a568SDoug Rabson #define	MCALL_MSG_SIZE 24
1538f55a568SDoug Rabson 
1548360efbdSAlfred Perlstein /*
1558360efbdSAlfred Perlstein  * Private data kept per client handle
1568360efbdSAlfred Perlstein  */
1578360efbdSAlfred Perlstein struct cu_data {
1588360efbdSAlfred Perlstein 	int			cu_fd;		/* connections fd */
1598360efbdSAlfred Perlstein 	bool_t			cu_closeit;	/* opened by library */
1608360efbdSAlfred Perlstein 	struct sockaddr_storage	cu_raddr;	/* remote address */
1618360efbdSAlfred Perlstein 	int			cu_rlen;
1628360efbdSAlfred Perlstein 	struct timeval		cu_wait;	/* retransmit interval */
1638360efbdSAlfred Perlstein 	struct timeval		cu_total;	/* total time for the call */
1648360efbdSAlfred Perlstein 	struct rpc_err		cu_error;
1658360efbdSAlfred Perlstein 	XDR			cu_outxdrs;
1668360efbdSAlfred Perlstein 	u_int			cu_xdrpos;
1678360efbdSAlfred Perlstein 	u_int			cu_sendsz;	/* send size */
1688f55a568SDoug Rabson 	char			cu_outhdr[MCALL_MSG_SIZE];
1698360efbdSAlfred Perlstein 	char			*cu_outbuf;
1708360efbdSAlfred Perlstein 	u_int			cu_recvsz;	/* recv size */
17165d3c627SBill Paul 	int			cu_async;
17232de72d2SIan Dowse 	int			cu_connect;	/* Use connect(). */
17332de72d2SIan Dowse 	int			cu_connected;	/* Have done connect(). */
174d7cd6ba3SIan Dowse 	struct kevent		cu_kin;
175d7cd6ba3SIan Dowse 	int			cu_kq;
1768360efbdSAlfred Perlstein 	char			cu_inbuf[1];
1778360efbdSAlfred Perlstein };
1788360efbdSAlfred Perlstein 
1798360efbdSAlfred Perlstein /*
1808360efbdSAlfred Perlstein  * Connection less client creation returns with client handle parameters.
1818360efbdSAlfred Perlstein  * Default options are set, which the user can change using clnt_control().
1828360efbdSAlfred Perlstein  * fd should be open and bound.
1838360efbdSAlfred Perlstein  * NB: The rpch->cl_auth is initialized to null authentication.
1848360efbdSAlfred Perlstein  * 	Caller may wish to set this something more useful.
1858360efbdSAlfred Perlstein  *
1868360efbdSAlfred Perlstein  * sendsz and recvsz are the maximum allowable packet sizes that can be
1878360efbdSAlfred Perlstein  * sent and received. Normally they are the same, but they can be
1888360efbdSAlfred Perlstein  * changed to improve the program efficiency and buffer allocation.
1898360efbdSAlfred Perlstein  * If they are 0, use the transport default.
1908360efbdSAlfred Perlstein  *
1918360efbdSAlfred Perlstein  * If svcaddr is NULL, returns NULL.
19268895e38SCraig Rodrigues  *
19368895e38SCraig Rodrigues  * fd      - open file descriptor
19468895e38SCraig Rodrigues  * svcaddr - servers address
19568895e38SCraig Rodrigues  * program - program number
19668895e38SCraig Rodrigues  * version - version number
19768895e38SCraig Rodrigues  * sendsz  - buffer recv size
19868895e38SCraig Rodrigues  * recvsz  - buffer send size
1998360efbdSAlfred Perlstein  */
2008360efbdSAlfred Perlstein CLIENT *
clnt_dg_create(int fd,const struct netbuf * svcaddr,rpcprog_t program,rpcvers_t version,u_int sendsz,u_int recvsz)20168895e38SCraig Rodrigues clnt_dg_create(int fd, const struct netbuf *svcaddr, rpcprog_t program,
20268895e38SCraig Rodrigues     rpcvers_t version, u_int sendsz, u_int recvsz)
2038360efbdSAlfred Perlstein {
2048360efbdSAlfred Perlstein 	CLIENT *cl = NULL;		/* client handle */
2058360efbdSAlfred Perlstein 	struct cu_data *cu = NULL;	/* private data */
2068360efbdSAlfred Perlstein 	struct timeval now;
2078360efbdSAlfred Perlstein 	struct rpc_msg call_msg;
2088360efbdSAlfred Perlstein 	struct __rpc_sockinfo si;
2098360efbdSAlfred Perlstein 	int one = 1;
2108360efbdSAlfred Perlstein 
2118360efbdSAlfred Perlstein 	if (svcaddr == NULL) {
2128360efbdSAlfred Perlstein 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
2138360efbdSAlfred Perlstein 		return (NULL);
2148360efbdSAlfred Perlstein 	}
2158360efbdSAlfred Perlstein 
2168360efbdSAlfred Perlstein 	if (!__rpc_fd2sockinfo(fd, &si)) {
2178360efbdSAlfred Perlstein 		rpc_createerr.cf_stat = RPC_TLIERROR;
2188360efbdSAlfred Perlstein 		rpc_createerr.cf_error.re_errno = 0;
2198360efbdSAlfred Perlstein 		return (NULL);
2208360efbdSAlfred Perlstein 	}
2218360efbdSAlfred Perlstein 	/*
2228360efbdSAlfred Perlstein 	 * Find the receive and the send size
2238360efbdSAlfred Perlstein 	 */
2248360efbdSAlfred Perlstein 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
2258360efbdSAlfred Perlstein 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
2268360efbdSAlfred Perlstein 	if ((sendsz == 0) || (recvsz == 0)) {
2278360efbdSAlfred Perlstein 		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
2288360efbdSAlfred Perlstein 		rpc_createerr.cf_error.re_errno = 0;
2298360efbdSAlfred Perlstein 		return (NULL);
2308360efbdSAlfred Perlstein 	}
2318360efbdSAlfred Perlstein 
2328360efbdSAlfred Perlstein 	if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
2338360efbdSAlfred Perlstein 		goto err1;
2348360efbdSAlfred Perlstein 	/*
2358360efbdSAlfred Perlstein 	 * Should be multiple of 4 for XDR.
2368360efbdSAlfred Perlstein 	 */
2378360efbdSAlfred Perlstein 	sendsz = ((sendsz + 3) / 4) * 4;
2388360efbdSAlfred Perlstein 	recvsz = ((recvsz + 3) / 4) * 4;
2398360efbdSAlfred Perlstein 	cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
2408360efbdSAlfred Perlstein 	if (cu == NULL)
2418360efbdSAlfred Perlstein 		goto err1;
2428360efbdSAlfred Perlstein 	(void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
2438360efbdSAlfred Perlstein 	cu->cu_rlen = svcaddr->len;
2448360efbdSAlfred Perlstein 	cu->cu_outbuf = &cu->cu_inbuf[recvsz];
2458360efbdSAlfred Perlstein 	/* Other values can also be set through clnt_control() */
2468360efbdSAlfred Perlstein 	cu->cu_wait.tv_sec = 15;	/* heuristically chosen */
2478360efbdSAlfred Perlstein 	cu->cu_wait.tv_usec = 0;
2488360efbdSAlfred Perlstein 	cu->cu_total.tv_sec = -1;
2498360efbdSAlfred Perlstein 	cu->cu_total.tv_usec = -1;
2508360efbdSAlfred Perlstein 	cu->cu_sendsz = sendsz;
2518360efbdSAlfred Perlstein 	cu->cu_recvsz = recvsz;
25265d3c627SBill Paul 	cu->cu_async = FALSE;
25332de72d2SIan Dowse 	cu->cu_connect = FALSE;
25432de72d2SIan Dowse 	cu->cu_connected = FALSE;
2558360efbdSAlfred Perlstein 	(void) gettimeofday(&now, NULL);
2568360efbdSAlfred Perlstein 	call_msg.rm_xid = __RPC_GETXID(&now);
2578360efbdSAlfred Perlstein 	call_msg.rm_call.cb_prog = program;
2588360efbdSAlfred Perlstein 	call_msg.rm_call.cb_vers = version;
2598f55a568SDoug Rabson 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outhdr, MCALL_MSG_SIZE,
2608f55a568SDoug Rabson 	    XDR_ENCODE);
2618f55a568SDoug Rabson 	if (! xdr_callhdr(&cu->cu_outxdrs, &call_msg)) {
2628360efbdSAlfred Perlstein 		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
2638360efbdSAlfred Perlstein 		rpc_createerr.cf_error.re_errno = 0;
2648360efbdSAlfred Perlstein 		goto err2;
2658360efbdSAlfred Perlstein 	}
2668360efbdSAlfred Perlstein 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
2678f55a568SDoug Rabson 	XDR_DESTROY(&cu->cu_outxdrs);
2688f55a568SDoug Rabson 	xdrmem_create(&cu->cu_outxdrs, cu->cu_outbuf, sendsz, XDR_ENCODE);
2698360efbdSAlfred Perlstein 
2708360efbdSAlfred Perlstein 	/* XXX fvdl - do we still want this? */
2718360efbdSAlfred Perlstein #if 0
2728360efbdSAlfred Perlstein 	(void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
2738360efbdSAlfred Perlstein #endif
2748360efbdSAlfred Perlstein 	_ioctl(fd, FIONBIO, (char *)(void *)&one);
2758360efbdSAlfred Perlstein 
2768360efbdSAlfred Perlstein 	/*
2778360efbdSAlfred Perlstein 	 * By default, closeit is always FALSE. It is users responsibility
2788360efbdSAlfred Perlstein 	 * to do a close on it, else the user may use clnt_control
2798360efbdSAlfred Perlstein 	 * to let clnt_destroy do it for him/her.
2808360efbdSAlfred Perlstein 	 */
2818360efbdSAlfred Perlstein 	cu->cu_closeit = FALSE;
2828360efbdSAlfred Perlstein 	cu->cu_fd = fd;
2838360efbdSAlfred Perlstein 	cl->cl_ops = clnt_dg_ops();
2848360efbdSAlfred Perlstein 	cl->cl_private = (caddr_t)(void *)cu;
2858360efbdSAlfred Perlstein 	cl->cl_auth = authnone_create();
2868360efbdSAlfred Perlstein 	cl->cl_tp = NULL;
2878360efbdSAlfred Perlstein 	cl->cl_netid = NULL;
288d7cd6ba3SIan Dowse 	cu->cu_kq = -1;
289d7cd6ba3SIan Dowse 	EV_SET(&cu->cu_kin, cu->cu_fd, EVFILT_READ, EV_ADD, 0, 0, 0);
2908360efbdSAlfred Perlstein 	return (cl);
2918360efbdSAlfred Perlstein err1:
2928360efbdSAlfred Perlstein 	warnx(mem_err_clnt_dg);
2938360efbdSAlfred Perlstein 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2948360efbdSAlfred Perlstein 	rpc_createerr.cf_error.re_errno = errno;
2958360efbdSAlfred Perlstein err2:
2968360efbdSAlfred Perlstein 	if (cl) {
2978360efbdSAlfred Perlstein 		mem_free(cl, sizeof (CLIENT));
2988360efbdSAlfred Perlstein 		if (cu)
2998360efbdSAlfred Perlstein 			mem_free(cu, sizeof (*cu) + sendsz + recvsz);
3008360efbdSAlfred Perlstein 	}
3018360efbdSAlfred Perlstein 	return (NULL);
3028360efbdSAlfred Perlstein }
3038360efbdSAlfred Perlstein 
30468895e38SCraig Rodrigues /*
30568895e38SCraig Rodrigues  * cl       - client handle
30668895e38SCraig Rodrigues  * proc     - procedure number
30768895e38SCraig Rodrigues  * xargs    - xdr routine for args
30868895e38SCraig Rodrigues  * argsp    - pointer to args
30968895e38SCraig Rodrigues  * xresults - xdr routine for results
31068895e38SCraig Rodrigues  * resultsp - pointer to results
31168895e38SCraig Rodrigues  * utimeout - seconds to wait before giving up
31268895e38SCraig Rodrigues  */
3138360efbdSAlfred Perlstein static enum clnt_stat
clnt_dg_call(CLIENT * cl,rpcproc_t proc,xdrproc_t xargs,void * argsp,xdrproc_t xresults,void * resultsp,struct timeval utimeout)31468895e38SCraig Rodrigues clnt_dg_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xargs, void *argsp,
31568895e38SCraig Rodrigues     xdrproc_t xresults, void *resultsp, struct timeval utimeout)
3168360efbdSAlfred Perlstein {
3178360efbdSAlfred Perlstein 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
3188360efbdSAlfred Perlstein 	XDR *xdrs;
319d7cd6ba3SIan Dowse 	size_t outlen = 0;
3208360efbdSAlfred Perlstein 	struct rpc_msg reply_msg;
3218360efbdSAlfred Perlstein 	XDR reply_xdrs;
3228360efbdSAlfred Perlstein 	bool_t ok;
3238360efbdSAlfred Perlstein 	int nrefreshes = 2;		/* number of times to refresh cred */
3248f55a568SDoug Rabson 	int nretries = 0;		/* number of times we retransmitted */
3258360efbdSAlfred Perlstein 	struct timeval timeout;
3268360efbdSAlfred Perlstein 	struct timeval retransmit_time;
327d7cd6ba3SIan Dowse 	struct timeval next_sendtime, starttime, time_waited, tv;
328d7cd6ba3SIan Dowse 	struct timespec ts;
329d7cd6ba3SIan Dowse 	struct kevent kv;
33032de72d2SIan Dowse 	struct sockaddr *sa;
331*24938f93SAlan Somers 	struct dg_fd *elem;
3328360efbdSAlfred Perlstein 	sigset_t mask;
3338360efbdSAlfred Perlstein 	sigset_t newmask;
3342b1474fdSPedro F. Giffuni 	socklen_t salen;
3358360efbdSAlfred Perlstein 	ssize_t recvlen = 0;
336*24938f93SAlan Somers 	int kin_len, n;
33765d3c627SBill Paul 	u_int32_t xid;
3388360efbdSAlfred Perlstein 
3398d630135SAlfred Perlstein 	outlen = 0;
3408360efbdSAlfred Perlstein 	sigfillset(&newmask);
3418360efbdSAlfred Perlstein 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
3428360efbdSAlfred Perlstein 	mutex_lock(&clnt_fd_lock);
343*24938f93SAlan Somers 	elem = dg_fd_find(cu->cu_fd);
3448360efbdSAlfred Perlstein 	mutex_unlock(&clnt_fd_lock);
345*24938f93SAlan Somers 	mutex_lock(&elem->mtx);
3468360efbdSAlfred Perlstein 	if (cu->cu_total.tv_usec == -1) {
3478360efbdSAlfred Perlstein 		timeout = utimeout;	/* use supplied timeout */
3488360efbdSAlfred Perlstein 	} else {
3498360efbdSAlfred Perlstein 		timeout = cu->cu_total;	/* use default timeout */
3508360efbdSAlfred Perlstein 	}
3518360efbdSAlfred Perlstein 
35232de72d2SIan Dowse 	if (cu->cu_connect && !cu->cu_connected) {
35332de72d2SIan Dowse 		if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr,
35432de72d2SIan Dowse 		    cu->cu_rlen) < 0) {
35532de72d2SIan Dowse 			cu->cu_error.re_errno = errno;
356d7cd6ba3SIan Dowse 			cu->cu_error.re_status = RPC_CANTSEND;
357d7cd6ba3SIan Dowse 			goto out;
35832de72d2SIan Dowse 		}
35932de72d2SIan Dowse 		cu->cu_connected = 1;
36032de72d2SIan Dowse 	}
36132de72d2SIan Dowse 	if (cu->cu_connected) {
36232de72d2SIan Dowse 		sa = NULL;
36332de72d2SIan Dowse 		salen = 0;
36432de72d2SIan Dowse 	} else {
36532de72d2SIan Dowse 		sa = (struct sockaddr *)&cu->cu_raddr;
36632de72d2SIan Dowse 		salen = cu->cu_rlen;
36732de72d2SIan Dowse 	}
3688360efbdSAlfred Perlstein 	time_waited.tv_sec = 0;
3698360efbdSAlfred Perlstein 	time_waited.tv_usec = 0;
370d7cd6ba3SIan Dowse 	retransmit_time = next_sendtime = cu->cu_wait;
371d7cd6ba3SIan Dowse 	gettimeofday(&starttime, NULL);
372d7cd6ba3SIan Dowse 
373d7cd6ba3SIan Dowse 	/* Clean up in case the last call ended in a longjmp(3) call. */
374d7cd6ba3SIan Dowse 	if (cu->cu_kq >= 0)
375d7cd6ba3SIan Dowse 		_close(cu->cu_kq);
376d7cd6ba3SIan Dowse 	if ((cu->cu_kq = kqueue()) < 0) {
377d7cd6ba3SIan Dowse 		cu->cu_error.re_errno = errno;
378d7cd6ba3SIan Dowse 		cu->cu_error.re_status = RPC_CANTSEND;
379d7cd6ba3SIan Dowse 		goto out;
380d7cd6ba3SIan Dowse 	}
381d7cd6ba3SIan Dowse 	kin_len = 1;
3828360efbdSAlfred Perlstein 
3838360efbdSAlfred Perlstein call_again:
3843d3ec676SDoug Rabson 	if (cu->cu_async == TRUE && xargs == NULL)
3853d3ec676SDoug Rabson 		goto get_reply;
3868360efbdSAlfred Perlstein 	/*
3878360efbdSAlfred Perlstein 	 * the transaction is the first thing in the out buffer
38865d3c627SBill Paul 	 * XXX Yes, and it's in network byte order, so we should to
38965d3c627SBill Paul 	 * be careful when we increment it, shouldn't we.
3908360efbdSAlfred Perlstein 	 */
3918f55a568SDoug Rabson 	xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr));
39265d3c627SBill Paul 	xid++;
3938f55a568SDoug Rabson 	*(u_int32_t *)(void *)(cu->cu_outhdr) = htonl(xid);
3948f55a568SDoug Rabson call_again_same_xid:
3958f55a568SDoug Rabson 	xdrs = &(cu->cu_outxdrs);
3968f55a568SDoug Rabson 	xdrs->x_op = XDR_ENCODE;
3978f55a568SDoug Rabson 	XDR_SETPOS(xdrs, 0);
39865d3c627SBill Paul 
3998f55a568SDoug Rabson 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
4008f55a568SDoug Rabson 		if ((! XDR_PUTBYTES(xdrs, cu->cu_outhdr, cu->cu_xdrpos)) ||
4018f55a568SDoug Rabson 		    (! XDR_PUTINT32(xdrs, &proc)) ||
4028360efbdSAlfred Perlstein 		    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
4038360efbdSAlfred Perlstein 		    (! (*xargs)(xdrs, argsp))) {
404d7cd6ba3SIan Dowse 			cu->cu_error.re_status = RPC_CANTENCODEARGS;
405d7cd6ba3SIan Dowse 			goto out;
4068360efbdSAlfred Perlstein 		}
4078f55a568SDoug Rabson 	} else {
4088f55a568SDoug Rabson 		*(uint32_t *) &cu->cu_outhdr[cu->cu_xdrpos] = htonl(proc);
4098f55a568SDoug Rabson 		if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outhdr,
4108f55a568SDoug Rabson 			cu->cu_xdrpos + sizeof(uint32_t),
4118f55a568SDoug Rabson 			xdrs, xargs, argsp)) {
4128f55a568SDoug Rabson 			cu->cu_error.re_status = RPC_CANTENCODEARGS;
4138f55a568SDoug Rabson 			goto out;
4148f55a568SDoug Rabson 		}
4158f55a568SDoug Rabson 	}
4168360efbdSAlfred Perlstein 	outlen = (size_t)XDR_GETPOS(xdrs);
4178360efbdSAlfred Perlstein 
4188360efbdSAlfred Perlstein send_again:
41932de72d2SIan Dowse 	if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) {
4208360efbdSAlfred Perlstein 		cu->cu_error.re_errno = errno;
421d7cd6ba3SIan Dowse 		cu->cu_error.re_status = RPC_CANTSEND;
422d7cd6ba3SIan Dowse 		goto out;
4238360efbdSAlfred Perlstein 	}
4248360efbdSAlfred Perlstein 
4258360efbdSAlfred Perlstein 	/*
4268360efbdSAlfred Perlstein 	 * Hack to provide rpc-based message passing
4278360efbdSAlfred Perlstein 	 */
4288360efbdSAlfred Perlstein 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
429d7cd6ba3SIan Dowse 		cu->cu_error.re_status = RPC_TIMEDOUT;
430d7cd6ba3SIan Dowse 		goto out;
4318360efbdSAlfred Perlstein 	}
43265d3c627SBill Paul 
43365d3c627SBill Paul get_reply:
43465d3c627SBill Paul 
4358360efbdSAlfred Perlstein 	/*
4368360efbdSAlfred Perlstein 	 * sub-optimal code appears here because we have
4378360efbdSAlfred Perlstein 	 * some clock time to spare while the packets are in flight.
4388360efbdSAlfred Perlstein 	 * (We assume that this is actually only executed once.)
4398360efbdSAlfred Perlstein 	 */
4408360efbdSAlfred Perlstein 	reply_msg.acpted_rply.ar_verf = _null_auth;
4418f55a568SDoug Rabson 	if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
4428360efbdSAlfred Perlstein 		reply_msg.acpted_rply.ar_results.where = resultsp;
4438360efbdSAlfred Perlstein 		reply_msg.acpted_rply.ar_results.proc = xresults;
4448f55a568SDoug Rabson 	} else {
4458f55a568SDoug Rabson 		reply_msg.acpted_rply.ar_results.where = NULL;
4468f55a568SDoug Rabson 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
4478f55a568SDoug Rabson 	}
4488360efbdSAlfred Perlstein 
4498360efbdSAlfred Perlstein 	for (;;) {
450d7cd6ba3SIan Dowse 		/* Decide how long to wait. */
451d7cd6ba3SIan Dowse 		if (timercmp(&next_sendtime, &timeout, <))
452d7cd6ba3SIan Dowse 			timersub(&next_sendtime, &time_waited, &tv);
453d7cd6ba3SIan Dowse 		else
454d7cd6ba3SIan Dowse 			timersub(&timeout, &time_waited, &tv);
455d7cd6ba3SIan Dowse 		if (tv.tv_sec < 0 || tv.tv_usec < 0)
456d7cd6ba3SIan Dowse 			tv.tv_sec = tv.tv_usec = 0;
457d7cd6ba3SIan Dowse 		TIMEVAL_TO_TIMESPEC(&tv, &ts);
4588360efbdSAlfred Perlstein 
459d7cd6ba3SIan Dowse 		n = _kevent(cu->cu_kq, &cu->cu_kin, kin_len, &kv, 1, &ts);
460d7cd6ba3SIan Dowse 		/* We don't need to register the event again. */
461d7cd6ba3SIan Dowse 		kin_len = 0;
4628360efbdSAlfred Perlstein 
463d7cd6ba3SIan Dowse 		if (n == 1) {
464d7cd6ba3SIan Dowse 			if (kv.flags & EV_ERROR) {
465d7cd6ba3SIan Dowse 				cu->cu_error.re_errno = kv.data;
4668360efbdSAlfred Perlstein 				cu->cu_error.re_status = RPC_CANTRECV;
467d7cd6ba3SIan Dowse 				goto out;
4688360efbdSAlfred Perlstein 			}
4698360efbdSAlfred Perlstein 			/* We have some data now */
4708360efbdSAlfred Perlstein 			do {
4718360efbdSAlfred Perlstein 				recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf,
472a9d09e93SIan Dowse 				    cu->cu_recvsz, 0, NULL, NULL);
4738360efbdSAlfred Perlstein 			} while (recvlen < 0 && errno == EINTR);
474d7cd6ba3SIan Dowse 			if (recvlen < 0 && errno != EWOULDBLOCK) {
4758360efbdSAlfred Perlstein 				cu->cu_error.re_errno = errno;
476d7cd6ba3SIan Dowse 				cu->cu_error.re_status = RPC_CANTRECV;
477d7cd6ba3SIan Dowse 				goto out;
4788360efbdSAlfred Perlstein 			}
479d7cd6ba3SIan Dowse 			if (recvlen >= sizeof(u_int32_t) &&
480d7cd6ba3SIan Dowse 			    (cu->cu_async == TRUE ||
481d7cd6ba3SIan Dowse 			    *((u_int32_t *)(void *)(cu->cu_inbuf)) ==
482d7cd6ba3SIan Dowse 			    *((u_int32_t *)(void *)(cu->cu_outbuf)))) {
483d7cd6ba3SIan Dowse 				/* We now assume we have the proper reply. */
4848360efbdSAlfred Perlstein 				break;
4858360efbdSAlfred Perlstein 			}
486d7cd6ba3SIan Dowse 		}
487d7cd6ba3SIan Dowse 		if (n == -1 && errno != EINTR) {
488d7cd6ba3SIan Dowse 			cu->cu_error.re_errno = errno;
489d7cd6ba3SIan Dowse 			cu->cu_error.re_status = RPC_CANTRECV;
490d7cd6ba3SIan Dowse 			goto out;
491d7cd6ba3SIan Dowse 		}
492d7cd6ba3SIan Dowse 		gettimeofday(&tv, NULL);
493d7cd6ba3SIan Dowse 		timersub(&tv, &starttime, &time_waited);
494d7cd6ba3SIan Dowse 
495d7cd6ba3SIan Dowse 		/* Check for timeout. */
496d7cd6ba3SIan Dowse 		if (timercmp(&time_waited, &timeout, >)) {
497d7cd6ba3SIan Dowse 			cu->cu_error.re_status = RPC_TIMEDOUT;
498d7cd6ba3SIan Dowse 			goto out;
499d7cd6ba3SIan Dowse 		}
500d7cd6ba3SIan Dowse 
501d7cd6ba3SIan Dowse 		/* Retransmit if necessary. */
502d7cd6ba3SIan Dowse 		if (timercmp(&time_waited, &next_sendtime, >)) {
503d7cd6ba3SIan Dowse 			/* update retransmit_time */
504d7cd6ba3SIan Dowse 			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
505d7cd6ba3SIan Dowse 				timeradd(&retransmit_time, &retransmit_time,
506d7cd6ba3SIan Dowse 				    &retransmit_time);
507d7cd6ba3SIan Dowse 			timeradd(&next_sendtime, &retransmit_time,
508d7cd6ba3SIan Dowse 			    &next_sendtime);
5098f55a568SDoug Rabson 			nretries++;
5108f55a568SDoug Rabson 
5118f55a568SDoug Rabson 			/*
5128f55a568SDoug Rabson 			 * When retransmitting a RPCSEC_GSS message,
5138f55a568SDoug Rabson 			 * we must use a new sequence number (handled
5148f55a568SDoug Rabson 			 * by __rpc_gss_wrap above).
5158f55a568SDoug Rabson 			 */
5168f55a568SDoug Rabson 			if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS)
517d7cd6ba3SIan Dowse 				goto send_again;
5188f55a568SDoug Rabson 			else
5198f55a568SDoug Rabson 				goto call_again_same_xid;
520d7cd6ba3SIan Dowse 		}
521d7cd6ba3SIan Dowse 	}
5228360efbdSAlfred Perlstein 
5238360efbdSAlfred Perlstein 	/*
5248360efbdSAlfred Perlstein 	 * now decode and validate the response
5258360efbdSAlfred Perlstein 	 */
5268360efbdSAlfred Perlstein 
527f4a180ccSMartin Blapp 	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
5288360efbdSAlfred Perlstein 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
5298360efbdSAlfred Perlstein 	/* XDR_DESTROY(&reply_xdrs);	save a few cycles on noop destroy */
5308360efbdSAlfred Perlstein 	if (ok) {
5318360efbdSAlfred Perlstein 		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
5328360efbdSAlfred Perlstein 			(reply_msg.acpted_rply.ar_stat == SUCCESS))
5338360efbdSAlfred Perlstein 			cu->cu_error.re_status = RPC_SUCCESS;
5348360efbdSAlfred Perlstein 		else
5358360efbdSAlfred Perlstein 			_seterr_reply(&reply_msg, &(cu->cu_error));
5368360efbdSAlfred Perlstein 
5378360efbdSAlfred Perlstein 		if (cu->cu_error.re_status == RPC_SUCCESS) {
5388360efbdSAlfred Perlstein 			if (! AUTH_VALIDATE(cl->cl_auth,
5398360efbdSAlfred Perlstein 					    &reply_msg.acpted_rply.ar_verf)) {
5408f55a568SDoug Rabson 				if (nretries &&
5418f55a568SDoug Rabson 				    cl->cl_auth->ah_cred.oa_flavor
5428f55a568SDoug Rabson 				    == RPCSEC_GSS)
5438f55a568SDoug Rabson 					/*
5448f55a568SDoug Rabson 					 * If we retransmitted, its
5458f55a568SDoug Rabson 					 * possible that we will
5468f55a568SDoug Rabson 					 * receive a reply for one of
5478f55a568SDoug Rabson 					 * the earlier transmissions
5488f55a568SDoug Rabson 					 * (which will use an older
5498f55a568SDoug Rabson 					 * RPCSEC_GSS sequence
5508f55a568SDoug Rabson 					 * number). In this case, just
5518f55a568SDoug Rabson 					 * go back and listen for a
5528f55a568SDoug Rabson 					 * new reply. We could keep a
5538f55a568SDoug Rabson 					 * record of all the seq
5548f55a568SDoug Rabson 					 * numbers we have transmitted
5558f55a568SDoug Rabson 					 * so far so that we could
5568f55a568SDoug Rabson 					 * accept a reply for any of
5578f55a568SDoug Rabson 					 * them here.
5588f55a568SDoug Rabson 					 */
5598f55a568SDoug Rabson 					goto get_reply;
5608360efbdSAlfred Perlstein 				cu->cu_error.re_status = RPC_AUTHERROR;
5618360efbdSAlfred Perlstein 				cu->cu_error.re_why = AUTH_INVALIDRESP;
5628f55a568SDoug Rabson 			} else {
5638f55a568SDoug Rabson 				if (cl->cl_auth->ah_cred.oa_flavor
5648f55a568SDoug Rabson 				    == RPCSEC_GSS) {
5658f55a568SDoug Rabson 					if (!__rpc_gss_unwrap(cl->cl_auth,
5668f55a568SDoug Rabson 						&reply_xdrs, xresults,
5678f55a568SDoug Rabson 						resultsp))
5688f55a568SDoug Rabson 						cu->cu_error.re_status =
5698f55a568SDoug Rabson 							RPC_CANTDECODERES;
5708f55a568SDoug Rabson 				}
5718360efbdSAlfred Perlstein 			}
5728360efbdSAlfred Perlstein 			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
5738360efbdSAlfred Perlstein 				xdrs->x_op = XDR_FREE;
5748360efbdSAlfred Perlstein 				(void) xdr_opaque_auth(xdrs,
5758360efbdSAlfred Perlstein 					&(reply_msg.acpted_rply.ar_verf));
5768360efbdSAlfred Perlstein 			}
5778360efbdSAlfred Perlstein 		}		/* end successful completion */
5788360efbdSAlfred Perlstein 		/*
57932223c1bSPedro F. Giffuni 		 * If unsuccessful AND error is an authentication error
5808360efbdSAlfred Perlstein 		 * then refresh credentials and try again, else break
5818360efbdSAlfred Perlstein 		 */
5828360efbdSAlfred Perlstein 		else if (cu->cu_error.re_status == RPC_AUTHERROR)
5838360efbdSAlfred Perlstein 			/* maybe our credentials need to be refreshed ... */
5848360efbdSAlfred Perlstein 			if (nrefreshes > 0 &&
5858360efbdSAlfred Perlstein 			    AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
5868360efbdSAlfred Perlstein 				nrefreshes--;
5878360efbdSAlfred Perlstein 				goto call_again;
5888360efbdSAlfred Perlstein 			}
5898360efbdSAlfred Perlstein 		/* end of unsuccessful completion */
5908360efbdSAlfred Perlstein 	}	/* end of valid reply message */
5918360efbdSAlfred Perlstein 	else {
5928360efbdSAlfred Perlstein 		cu->cu_error.re_status = RPC_CANTDECODERES;
5938360efbdSAlfred Perlstein 
5948360efbdSAlfred Perlstein 	}
595d7cd6ba3SIan Dowse out:
596d7cd6ba3SIan Dowse 	if (cu->cu_kq >= 0)
597d7cd6ba3SIan Dowse 		_close(cu->cu_kq);
598d7cd6ba3SIan Dowse 	cu->cu_kq = -1;
599*24938f93SAlan Somers 	release_fd_lock(elem, mask);
6008360efbdSAlfred Perlstein 	return (cu->cu_error.re_status);
6018360efbdSAlfred Perlstein }
6028360efbdSAlfred Perlstein 
6038360efbdSAlfred Perlstein static void
clnt_dg_geterr(CLIENT * cl,struct rpc_err * errp)60468895e38SCraig Rodrigues clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
6058360efbdSAlfred Perlstein {
6068360efbdSAlfred Perlstein 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
6078360efbdSAlfred Perlstein 
6088360efbdSAlfred Perlstein 	*errp = cu->cu_error;
6098360efbdSAlfred Perlstein }
6108360efbdSAlfred Perlstein 
6118360efbdSAlfred Perlstein static bool_t
clnt_dg_freeres(CLIENT * cl,xdrproc_t xdr_res,void * res_ptr)61268895e38SCraig Rodrigues clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
6138360efbdSAlfred Perlstein {
6148360efbdSAlfred Perlstein 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
615*24938f93SAlan Somers 	struct dg_fd *elem;
6168360efbdSAlfred Perlstein 	XDR *xdrs = &(cu->cu_outxdrs);
6178360efbdSAlfred Perlstein 	bool_t dummy;
6188360efbdSAlfred Perlstein 	sigset_t mask;
6198360efbdSAlfred Perlstein 	sigset_t newmask;
6208360efbdSAlfred Perlstein 
6218360efbdSAlfred Perlstein 	sigfillset(&newmask);
6228360efbdSAlfred Perlstein 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
6238360efbdSAlfred Perlstein 	mutex_lock(&clnt_fd_lock);
624*24938f93SAlan Somers 	elem = dg_fd_find(cu->cu_fd);
625*24938f93SAlan Somers 	mutex_lock(&elem->mtx);
6268360efbdSAlfred Perlstein 	xdrs->x_op = XDR_FREE;
6278360efbdSAlfred Perlstein 	dummy = (*xdr_res)(xdrs, res_ptr);
6288360efbdSAlfred Perlstein 	mutex_unlock(&clnt_fd_lock);
629*24938f93SAlan Somers 	release_fd_lock(elem, mask);
6308360efbdSAlfred Perlstein 	return (dummy);
6318360efbdSAlfred Perlstein }
6328360efbdSAlfred Perlstein 
6338360efbdSAlfred Perlstein /*ARGSUSED*/
6348360efbdSAlfred Perlstein static void
clnt_dg_abort(CLIENT * h)63568895e38SCraig Rodrigues clnt_dg_abort(CLIENT *h)
6368360efbdSAlfred Perlstein {
6378360efbdSAlfred Perlstein }
6388360efbdSAlfred Perlstein 
6398360efbdSAlfred Perlstein static bool_t
clnt_dg_control(CLIENT * cl,u_int request,void * info)64068895e38SCraig Rodrigues clnt_dg_control(CLIENT *cl, u_int request, void *info)
6418360efbdSAlfred Perlstein {
6428360efbdSAlfred Perlstein 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
6438360efbdSAlfred Perlstein 	struct netbuf *addr;
644*24938f93SAlan Somers 	struct dg_fd *elem;
6458360efbdSAlfred Perlstein 	sigset_t mask;
6468360efbdSAlfred Perlstein 	sigset_t newmask;
6478360efbdSAlfred Perlstein 
6488360efbdSAlfred Perlstein 	sigfillset(&newmask);
6498360efbdSAlfred Perlstein 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
6508360efbdSAlfred Perlstein 	mutex_lock(&clnt_fd_lock);
651*24938f93SAlan Somers 	elem = dg_fd_find(cu->cu_fd);
6528360efbdSAlfred Perlstein 	mutex_unlock(&clnt_fd_lock);
653*24938f93SAlan Somers 	mutex_lock(&elem->mtx);
6548360efbdSAlfred Perlstein 	switch (request) {
6558360efbdSAlfred Perlstein 	case CLSET_FD_CLOSE:
6568360efbdSAlfred Perlstein 		cu->cu_closeit = TRUE;
657*24938f93SAlan Somers 		release_fd_lock(elem, mask);
6588360efbdSAlfred Perlstein 		return (TRUE);
6598360efbdSAlfred Perlstein 	case CLSET_FD_NCLOSE:
6608360efbdSAlfred Perlstein 		cu->cu_closeit = FALSE;
661*24938f93SAlan Somers 		release_fd_lock(elem, mask);
6628360efbdSAlfred Perlstein 		return (TRUE);
6638360efbdSAlfred Perlstein 	}
6648360efbdSAlfred Perlstein 
6658360efbdSAlfred Perlstein 	/* for other requests which use info */
6668360efbdSAlfred Perlstein 	if (info == NULL) {
667*24938f93SAlan Somers 		release_fd_lock(elem, mask);
6688360efbdSAlfred Perlstein 		return (FALSE);
6698360efbdSAlfred Perlstein 	}
6708360efbdSAlfred Perlstein 	switch (request) {
6718360efbdSAlfred Perlstein 	case CLSET_TIMEOUT:
672cffc0b57SAlfred Perlstein 		if (time_not_ok((struct timeval *)info)) {
673*24938f93SAlan Somers 			release_fd_lock(elem, mask);
6748360efbdSAlfred Perlstein 			return (FALSE);
6758360efbdSAlfred Perlstein 		}
676cffc0b57SAlfred Perlstein 		cu->cu_total = *(struct timeval *)info;
6778360efbdSAlfred Perlstein 		break;
6788360efbdSAlfred Perlstein 	case CLGET_TIMEOUT:
679cffc0b57SAlfred Perlstein 		*(struct timeval *)info = cu->cu_total;
6808360efbdSAlfred Perlstein 		break;
6818360efbdSAlfred Perlstein 	case CLGET_SERVER_ADDR:		/* Give him the fd address */
6828360efbdSAlfred Perlstein 		/* Now obsolete. Only for backward compatibility */
6838360efbdSAlfred Perlstein 		(void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
6848360efbdSAlfred Perlstein 		break;
6858360efbdSAlfred Perlstein 	case CLSET_RETRY_TIMEOUT:
686cffc0b57SAlfred Perlstein 		if (time_not_ok((struct timeval *)info)) {
687*24938f93SAlan Somers 			release_fd_lock(elem, mask);
6888360efbdSAlfred Perlstein 			return (FALSE);
6898360efbdSAlfred Perlstein 		}
690cffc0b57SAlfred Perlstein 		cu->cu_wait = *(struct timeval *)info;
6918360efbdSAlfred Perlstein 		break;
6928360efbdSAlfred Perlstein 	case CLGET_RETRY_TIMEOUT:
693cffc0b57SAlfred Perlstein 		*(struct timeval *)info = cu->cu_wait;
6948360efbdSAlfred Perlstein 		break;
6958360efbdSAlfred Perlstein 	case CLGET_FD:
696cffc0b57SAlfred Perlstein 		*(int *)info = cu->cu_fd;
6978360efbdSAlfred Perlstein 		break;
6988360efbdSAlfred Perlstein 	case CLGET_SVC_ADDR:
699cffc0b57SAlfred Perlstein 		addr = (struct netbuf *)info;
7008360efbdSAlfred Perlstein 		addr->buf = &cu->cu_raddr;
7018360efbdSAlfred Perlstein 		addr->len = cu->cu_rlen;
7028360efbdSAlfred Perlstein 		addr->maxlen = sizeof cu->cu_raddr;
7038360efbdSAlfred Perlstein 		break;
7048360efbdSAlfred Perlstein 	case CLSET_SVC_ADDR:		/* set to new address */
705cffc0b57SAlfred Perlstein 		addr = (struct netbuf *)info;
706cd517469SIan Dowse 		if (addr->len < sizeof cu->cu_raddr) {
707*24938f93SAlan Somers 			release_fd_lock(elem, mask);
7088360efbdSAlfred Perlstein 			return (FALSE);
709cd517469SIan Dowse 		}
7108360efbdSAlfred Perlstein 		(void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
7118360efbdSAlfred Perlstein 		cu->cu_rlen = addr->len;
7128360efbdSAlfred Perlstein 		break;
7138360efbdSAlfred Perlstein 	case CLGET_XID:
7148360efbdSAlfred Perlstein 		/*
7158360efbdSAlfred Perlstein 		 * use the knowledge that xid is the
7168360efbdSAlfred Perlstein 		 * first element in the call structure *.
7178360efbdSAlfred Perlstein 		 * This will get the xid of the PREVIOUS call
7188360efbdSAlfred Perlstein 		 */
719cffc0b57SAlfred Perlstein 		*(u_int32_t *)info =
7208f55a568SDoug Rabson 		    ntohl(*(u_int32_t *)(void *)cu->cu_outhdr);
7218360efbdSAlfred Perlstein 		break;
7228360efbdSAlfred Perlstein 
7238360efbdSAlfred Perlstein 	case CLSET_XID:
7248360efbdSAlfred Perlstein 		/* This will set the xid of the NEXT call */
7258f55a568SDoug Rabson 		*(u_int32_t *)(void *)cu->cu_outhdr =
726cffc0b57SAlfred Perlstein 		    htonl(*(u_int32_t *)info - 1);
7278360efbdSAlfred Perlstein 		/* decrement by 1 as clnt_dg_call() increments once */
7288360efbdSAlfred Perlstein 		break;
7298360efbdSAlfred Perlstein 
7308360efbdSAlfred Perlstein 	case CLGET_VERS:
7318360efbdSAlfred Perlstein 		/*
7328360efbdSAlfred Perlstein 		 * This RELIES on the information that, in the call body,
7338360efbdSAlfred Perlstein 		 * the version number field is the fifth field from the
73432223c1bSPedro F. Giffuni 		 * beginning of the RPC header. MUST be changed if the
7358360efbdSAlfred Perlstein 		 * call_struct is changed
7368360efbdSAlfred Perlstein 		 */
737cffc0b57SAlfred Perlstein 		*(u_int32_t *)info =
7388f55a568SDoug Rabson 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
7398360efbdSAlfred Perlstein 		    4 * BYTES_PER_XDR_UNIT));
7408360efbdSAlfred Perlstein 		break;
7418360efbdSAlfred Perlstein 
7428360efbdSAlfred Perlstein 	case CLSET_VERS:
7438f55a568SDoug Rabson 		*(u_int32_t *)(void *)(cu->cu_outhdr + 4 * BYTES_PER_XDR_UNIT)
744cffc0b57SAlfred Perlstein 			= htonl(*(u_int32_t *)info);
7458360efbdSAlfred Perlstein 		break;
7468360efbdSAlfred Perlstein 
7478360efbdSAlfred Perlstein 	case CLGET_PROG:
7488360efbdSAlfred Perlstein 		/*
7498360efbdSAlfred Perlstein 		 * This RELIES on the information that, in the call body,
7508360efbdSAlfred Perlstein 		 * the program number field is the fourth field from the
75132223c1bSPedro F. Giffuni 		 * beginning of the RPC header. MUST be changed if the
7528360efbdSAlfred Perlstein 		 * call_struct is changed
7538360efbdSAlfred Perlstein 		 */
754cffc0b57SAlfred Perlstein 		*(u_int32_t *)info =
7558f55a568SDoug Rabson 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr +
7568360efbdSAlfred Perlstein 		    3 * BYTES_PER_XDR_UNIT));
7578360efbdSAlfred Perlstein 		break;
7588360efbdSAlfred Perlstein 
7598360efbdSAlfred Perlstein 	case CLSET_PROG:
7608f55a568SDoug Rabson 		*(u_int32_t *)(void *)(cu->cu_outhdr + 3 * BYTES_PER_XDR_UNIT)
761cffc0b57SAlfred Perlstein 			= htonl(*(u_int32_t *)info);
7628360efbdSAlfred Perlstein 		break;
76365d3c627SBill Paul 	case CLSET_ASYNC:
764cffc0b57SAlfred Perlstein 		cu->cu_async = *(int *)info;
76565d3c627SBill Paul 		break;
76632de72d2SIan Dowse 	case CLSET_CONNECT:
767cffc0b57SAlfred Perlstein 		cu->cu_connect = *(int *)info;
76832de72d2SIan Dowse 		break;
7698360efbdSAlfred Perlstein 	default:
770*24938f93SAlan Somers 		release_fd_lock(elem, mask);
7718360efbdSAlfred Perlstein 		return (FALSE);
7728360efbdSAlfred Perlstein 	}
773*24938f93SAlan Somers 	release_fd_lock(elem, mask);
7748360efbdSAlfred Perlstein 	return (TRUE);
7758360efbdSAlfred Perlstein }
7768360efbdSAlfred Perlstein 
7778360efbdSAlfred Perlstein static void
clnt_dg_destroy(CLIENT * cl)77868895e38SCraig Rodrigues clnt_dg_destroy(CLIENT *cl)
7798360efbdSAlfred Perlstein {
7808360efbdSAlfred Perlstein 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
781*24938f93SAlan Somers 	struct dg_fd *elem;
7828360efbdSAlfred Perlstein 	int cu_fd = cu->cu_fd;
7838360efbdSAlfred Perlstein 	sigset_t mask;
7848360efbdSAlfred Perlstein 	sigset_t newmask;
7858360efbdSAlfred Perlstein 
7868360efbdSAlfred Perlstein 	sigfillset(&newmask);
7878360efbdSAlfred Perlstein 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
7888360efbdSAlfred Perlstein 	mutex_lock(&clnt_fd_lock);
789*24938f93SAlan Somers 	elem = dg_fd_find(cu_fd);
790*24938f93SAlan Somers 	mutex_lock(&elem->mtx);
7918360efbdSAlfred Perlstein 	if (cu->cu_closeit)
7928360efbdSAlfred Perlstein 		(void)_close(cu_fd);
793d7cd6ba3SIan Dowse 	if (cu->cu_kq >= 0)
794d7cd6ba3SIan Dowse 		_close(cu->cu_kq);
7958360efbdSAlfred Perlstein 	XDR_DESTROY(&(cu->cu_outxdrs));
7968360efbdSAlfred Perlstein 	mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
7978360efbdSAlfred Perlstein 	if (cl->cl_netid && cl->cl_netid[0])
7988360efbdSAlfred Perlstein 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
7998360efbdSAlfred Perlstein 	if (cl->cl_tp && cl->cl_tp[0])
8008360efbdSAlfred Perlstein 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
8018360efbdSAlfred Perlstein 	mem_free(cl, sizeof (CLIENT));
8028360efbdSAlfred Perlstein 	mutex_unlock(&clnt_fd_lock);
803*24938f93SAlan Somers 	release_fd_lock(elem, mask);
8048360efbdSAlfred Perlstein }
8058360efbdSAlfred Perlstein 
8068360efbdSAlfred Perlstein static struct clnt_ops *
clnt_dg_ops(void)80768895e38SCraig Rodrigues clnt_dg_ops(void)
8088360efbdSAlfred Perlstein {
8098360efbdSAlfred Perlstein 	static struct clnt_ops ops;
8108360efbdSAlfred Perlstein 	sigset_t mask;
8118360efbdSAlfred Perlstein 	sigset_t newmask;
8128360efbdSAlfred Perlstein 
8138360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY ops_lock: ops */
8148360efbdSAlfred Perlstein 
8158360efbdSAlfred Perlstein 	sigfillset(&newmask);
8168360efbdSAlfred Perlstein 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
8178360efbdSAlfred Perlstein 	mutex_lock(&ops_lock);
8188360efbdSAlfred Perlstein 	if (ops.cl_call == NULL) {
8198360efbdSAlfred Perlstein 		ops.cl_call = clnt_dg_call;
8208360efbdSAlfred Perlstein 		ops.cl_abort = clnt_dg_abort;
8218360efbdSAlfred Perlstein 		ops.cl_geterr = clnt_dg_geterr;
8228360efbdSAlfred Perlstein 		ops.cl_freeres = clnt_dg_freeres;
8238360efbdSAlfred Perlstein 		ops.cl_destroy = clnt_dg_destroy;
8248360efbdSAlfred Perlstein 		ops.cl_control = clnt_dg_control;
8258360efbdSAlfred Perlstein 	}
8268360efbdSAlfred Perlstein 	mutex_unlock(&ops_lock);
8278360efbdSAlfred Perlstein 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
8288360efbdSAlfred Perlstein 	return (&ops);
8298360efbdSAlfred Perlstein }
8308360efbdSAlfred Perlstein 
8318360efbdSAlfred Perlstein /*
8328360efbdSAlfred Perlstein  * Make sure that the time is not garbage.  -1 value is allowed.
8338360efbdSAlfred Perlstein  */
8348360efbdSAlfred Perlstein static bool_t
time_not_ok(struct timeval * t)83568895e38SCraig Rodrigues time_not_ok(struct timeval *t)
8368360efbdSAlfred Perlstein {
8378360efbdSAlfred Perlstein 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
8388360efbdSAlfred Perlstein 		t->tv_usec < -1 || t->tv_usec > 1000000);
8398360efbdSAlfred Perlstein }
8408360efbdSAlfred Perlstein 
841