1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1984, 1986, 1987, 1988, 1989, 1996 AT&T
29*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
34*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
35*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
38*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
39*7c478bd9Sstevel@tonic-gate  * contributors.
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
50*7c478bd9Sstevel@tonic-gate #include <netdb.h>
51*7c478bd9Sstevel@tonic-gate #include <errno.h>
52*7c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
53*7c478bd9Sstevel@tonic-gate #include <rpc/clnt_soc.h>
54*7c478bd9Sstevel@tonic-gate #include <syslog.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/filio.h>
56*7c478bd9Sstevel@tonic-gate #include <malloc.h>
57*7c478bd9Sstevel@tonic-gate #include <unistd.h>
58*7c478bd9Sstevel@tonic-gate #include <stropts.h>
59*7c478bd9Sstevel@tonic-gate #include <stdio.h>
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate extern int errno;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate extern int _socket(int, int, int);
65*7c478bd9Sstevel@tonic-gate extern pid_t getpid();
66*7c478bd9Sstevel@tonic-gate extern int bindresvport(int, struct sockaddr_in *);
67*7c478bd9Sstevel@tonic-gate extern bool_t   xdr_opaque_auth(XDR *, struct opaque_auth *);
68*7c478bd9Sstevel@tonic-gate extern int _sendto(int, const char *, int, int,
69*7c478bd9Sstevel@tonic-gate 	const struct sockaddr *, int);
70*7c478bd9Sstevel@tonic-gate extern int _recvfrom(int, char *, int, int,
71*7c478bd9Sstevel@tonic-gate 	struct sockaddr *, int *);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static struct clnt_ops *clntudp_ops();
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * Private data kept per client handle
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate struct cu_data {
80*7c478bd9Sstevel@tonic-gate 	int		   cu_sock;
81*7c478bd9Sstevel@tonic-gate 	bool_t		   cu_closeit;
82*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in cu_raddr;
83*7c478bd9Sstevel@tonic-gate 	int		   cu_rlen;
84*7c478bd9Sstevel@tonic-gate 	struct timeval	   cu_wait;
85*7c478bd9Sstevel@tonic-gate 	struct timeval	   cu_total;
86*7c478bd9Sstevel@tonic-gate 	struct rpc_err	   cu_error;
87*7c478bd9Sstevel@tonic-gate 	XDR		   cu_outxdrs;
88*7c478bd9Sstevel@tonic-gate 	u_int		   cu_xdrpos;
89*7c478bd9Sstevel@tonic-gate 	u_int		   cu_sendsz;
90*7c478bd9Sstevel@tonic-gate 	char		   *cu_outbuf;
91*7c478bd9Sstevel@tonic-gate 	u_int		   cu_recvsz;
92*7c478bd9Sstevel@tonic-gate 	char		   cu_inbuf[1];
93*7c478bd9Sstevel@tonic-gate };
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /*
96*7c478bd9Sstevel@tonic-gate  * Create a UDP based client handle.
97*7c478bd9Sstevel@tonic-gate  * If *sockp<0, *sockp is set to a newly created UPD socket.
98*7c478bd9Sstevel@tonic-gate  * If raddr->sin_port is 0 a binder on the remote machine
99*7c478bd9Sstevel@tonic-gate  * is consulted for the correct port number.
100*7c478bd9Sstevel@tonic-gate  * NB: It is the clients responsibility to close *sockp.
101*7c478bd9Sstevel@tonic-gate  * NB: The rpch->cl_auth is initialized to null authentication.
102*7c478bd9Sstevel@tonic-gate  *	Caller may wish to set this something more useful.
103*7c478bd9Sstevel@tonic-gate  *
104*7c478bd9Sstevel@tonic-gate  * wait is the amount of time used between retransmitting a call if
105*7c478bd9Sstevel@tonic-gate  * no response has been heard;  retransmition occurs until the actual
106*7c478bd9Sstevel@tonic-gate  * rpc call times out.
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  * sendsz and recvsz are the maximum allowable packet sizes that can be
109*7c478bd9Sstevel@tonic-gate  * sent and received.
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate CLIENT *
clntudp_bufcreate(raddr,program,version,wait,sockp,sendsz,recvsz)112*7c478bd9Sstevel@tonic-gate clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
113*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in *raddr;
114*7c478bd9Sstevel@tonic-gate 	rpcprog_t program;
115*7c478bd9Sstevel@tonic-gate 	rpcvers_t version;
116*7c478bd9Sstevel@tonic-gate 	struct timeval wait;
117*7c478bd9Sstevel@tonic-gate 	register int *sockp;
118*7c478bd9Sstevel@tonic-gate 	u_int sendsz;
119*7c478bd9Sstevel@tonic-gate 	u_int recvsz;
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate 	CLIENT *cl;
122*7c478bd9Sstevel@tonic-gate 	register struct cu_data *cu;
123*7c478bd9Sstevel@tonic-gate 	struct timeval now;
124*7c478bd9Sstevel@tonic-gate 	struct rpc_msg call_msg;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	cl = (CLIENT *)mem_alloc(sizeof (CLIENT));
127*7c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
128*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "clntudp_create: out of memory");
129*7c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
130*7c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
131*7c478bd9Sstevel@tonic-gate 		goto fooy;
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	sendsz = ((sendsz + 3) / 4) * 4;
134*7c478bd9Sstevel@tonic-gate 	recvsz = ((recvsz + 3) / 4) * 4;
135*7c478bd9Sstevel@tonic-gate 	cu = (struct cu_data *)mem_alloc(sizeof (*cu) + sendsz + recvsz);
136*7c478bd9Sstevel@tonic-gate 	if (cu == NULL) {
137*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "clntudp_create: out of memory");
138*7c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
139*7c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
140*7c478bd9Sstevel@tonic-gate 		goto fooy;
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	cu->cu_outbuf = &cu->cu_inbuf[recvsz];
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, (struct timezone *)0);
145*7c478bd9Sstevel@tonic-gate 	if (raddr->sin_port == 0) {
146*7c478bd9Sstevel@tonic-gate 		u_short port;
147*7c478bd9Sstevel@tonic-gate 		if ((port =
148*7c478bd9Sstevel@tonic-gate 		    pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
149*7c478bd9Sstevel@tonic-gate 			goto fooy;
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 		raddr->sin_port = htons(port);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 	cl->cl_ops = clntudp_ops();
154*7c478bd9Sstevel@tonic-gate 	cl->cl_private = (caddr_t)cu;
155*7c478bd9Sstevel@tonic-gate 	cu->cu_raddr = *raddr;
156*7c478bd9Sstevel@tonic-gate 	cu->cu_rlen = sizeof (cu->cu_raddr);
157*7c478bd9Sstevel@tonic-gate 	cu->cu_wait = wait;
158*7c478bd9Sstevel@tonic-gate 	cu->cu_total.tv_sec = -1;
159*7c478bd9Sstevel@tonic-gate 	cu->cu_total.tv_usec = -1;
160*7c478bd9Sstevel@tonic-gate 	cu->cu_sendsz = sendsz;
161*7c478bd9Sstevel@tonic-gate 	cu->cu_recvsz = recvsz;
162*7c478bd9Sstevel@tonic-gate 	call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
163*7c478bd9Sstevel@tonic-gate 	call_msg.rm_direction = CALL;
164*7c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
165*7c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_prog = program;
166*7c478bd9Sstevel@tonic-gate 	call_msg.rm_call.cb_vers = version;
167*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
168*7c478bd9Sstevel@tonic-gate 	    sendsz, XDR_ENCODE);
169*7c478bd9Sstevel@tonic-gate 	if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
170*7c478bd9Sstevel@tonic-gate 		goto fooy;
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
173*7c478bd9Sstevel@tonic-gate 	if (*sockp < 0) {
174*7c478bd9Sstevel@tonic-gate 		int dontblock = 1;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 		*sockp = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
177*7c478bd9Sstevel@tonic-gate 		if (*sockp < 0) {
178*7c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
179*7c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_error.re_errno = errno;
180*7c478bd9Sstevel@tonic-gate 			goto fooy;
181*7c478bd9Sstevel@tonic-gate 		}
182*7c478bd9Sstevel@tonic-gate 		/* attempt to bind to prov port */
183*7c478bd9Sstevel@tonic-gate 		(void) bindresvport(*sockp, (struct sockaddr_in *)0);
184*7c478bd9Sstevel@tonic-gate 		/* the sockets rpc controls are non-blocking */
185*7c478bd9Sstevel@tonic-gate 		(void) ioctl(*sockp, FIONBIO, (char *) &dontblock);
186*7c478bd9Sstevel@tonic-gate 		cu->cu_closeit = TRUE;
187*7c478bd9Sstevel@tonic-gate 	} else {
188*7c478bd9Sstevel@tonic-gate 		cu->cu_closeit = FALSE;
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 	cu->cu_sock = *sockp;
191*7c478bd9Sstevel@tonic-gate 	cl->cl_auth = authnone_create();
192*7c478bd9Sstevel@tonic-gate 	return (cl);
193*7c478bd9Sstevel@tonic-gate fooy:
194*7c478bd9Sstevel@tonic-gate 	if (cu)
195*7c478bd9Sstevel@tonic-gate 		mem_free((caddr_t)cu, sizeof (*cu) + sendsz + recvsz);
196*7c478bd9Sstevel@tonic-gate 	if (cl)
197*7c478bd9Sstevel@tonic-gate 		mem_free((caddr_t)cl, sizeof (CLIENT));
198*7c478bd9Sstevel@tonic-gate 	return ((CLIENT *)NULL);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate CLIENT *
clntudp_create(raddr,program,version,wait,sockp)202*7c478bd9Sstevel@tonic-gate clntudp_create(raddr, program, version, wait, sockp)
203*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in *raddr;
204*7c478bd9Sstevel@tonic-gate 	rpcprog_t program;
205*7c478bd9Sstevel@tonic-gate 	rpcvers_t version;
206*7c478bd9Sstevel@tonic-gate 	struct timeval wait;
207*7c478bd9Sstevel@tonic-gate 	register int *sockp;
208*7c478bd9Sstevel@tonic-gate {
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	return (clntudp_bufcreate(raddr, program, version, wait, sockp,
211*7c478bd9Sstevel@tonic-gate 	    UDPMSGSIZE, UDPMSGSIZE));
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate static enum clnt_stat
clntudp_call(cl,proc,xargs,argsp,xresults,resultsp,utimeout)215*7c478bd9Sstevel@tonic-gate clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
216*7c478bd9Sstevel@tonic-gate 	register CLIENT	*cl;		/* client handle */
217*7c478bd9Sstevel@tonic-gate 	rpcproc_t	proc;		/* procedure number */
218*7c478bd9Sstevel@tonic-gate 	xdrproc_t	xargs;		/* xdr routine for args */
219*7c478bd9Sstevel@tonic-gate 	caddr_t		argsp;		/* pointer to args */
220*7c478bd9Sstevel@tonic-gate 	xdrproc_t	xresults;	/* xdr routine for results */
221*7c478bd9Sstevel@tonic-gate 	caddr_t		resultsp;	/* pointer to results */
222*7c478bd9Sstevel@tonic-gate 	struct timeval	utimeout;	/* seconds to wait before giving up */
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
225*7c478bd9Sstevel@tonic-gate 	register XDR *xdrs;
226*7c478bd9Sstevel@tonic-gate 	register int outlen;
227*7c478bd9Sstevel@tonic-gate 	register int inlen;
228*7c478bd9Sstevel@tonic-gate 	int fromlen;
229*7c478bd9Sstevel@tonic-gate 	fd_set readfds;
230*7c478bd9Sstevel@tonic-gate 	fd_set mask;
231*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in from;
232*7c478bd9Sstevel@tonic-gate 	struct rpc_msg reply_msg;
233*7c478bd9Sstevel@tonic-gate 	XDR reply_xdrs;
234*7c478bd9Sstevel@tonic-gate 	struct timeval startime, curtime;
235*7c478bd9Sstevel@tonic-gate 	int firsttimeout = 1;
236*7c478bd9Sstevel@tonic-gate 	struct timeval time_waited;
237*7c478bd9Sstevel@tonic-gate 	struct timeval retransmit_time;
238*7c478bd9Sstevel@tonic-gate 	bool_t ok;
239*7c478bd9Sstevel@tonic-gate 	int nrefreshes = 2;	/* number of times to refresh cred */
240*7c478bd9Sstevel@tonic-gate 	struct timeval timeout;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	if (cu->cu_total.tv_usec == -1) {
243*7c478bd9Sstevel@tonic-gate 		timeout = utimeout;	/* use supplied timeout */
244*7c478bd9Sstevel@tonic-gate 	} else {
245*7c478bd9Sstevel@tonic-gate 		timeout = cu->cu_total; /* use default timeout */
246*7c478bd9Sstevel@tonic-gate 	}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	time_waited.tv_sec = 0;
249*7c478bd9Sstevel@tonic-gate 	time_waited.tv_usec = 0;
250*7c478bd9Sstevel@tonic-gate 	retransmit_time = cu->cu_wait;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate call_again:
253*7c478bd9Sstevel@tonic-gate 	xdrs = &(cu->cu_outxdrs);
254*7c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_ENCODE;
255*7c478bd9Sstevel@tonic-gate 	XDR_SETPOS(xdrs, cu->cu_xdrpos);
256*7c478bd9Sstevel@tonic-gate 	/*
257*7c478bd9Sstevel@tonic-gate 	 * the transaction is the first thing in the out buffer
258*7c478bd9Sstevel@tonic-gate 	 */
259*7c478bd9Sstevel@tonic-gate 	(*(u_short *)(cu->cu_outbuf))++;
260*7c478bd9Sstevel@tonic-gate 	if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
261*7c478bd9Sstevel@tonic-gate 	    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
262*7c478bd9Sstevel@tonic-gate 	    (! (*xargs)(xdrs, argsp)))
263*7c478bd9Sstevel@tonic-gate 		return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
264*7c478bd9Sstevel@tonic-gate 	outlen = (int)XDR_GETPOS(xdrs);
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate send_again:
267*7c478bd9Sstevel@tonic-gate 	if (_sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
268*7c478bd9Sstevel@tonic-gate 	    (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
269*7c478bd9Sstevel@tonic-gate 	    != outlen) {
270*7c478bd9Sstevel@tonic-gate 		cu->cu_error.re_errno = errno;
271*7c478bd9Sstevel@tonic-gate 		return (cu->cu_error.re_status = RPC_CANTSEND);
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	/*
275*7c478bd9Sstevel@tonic-gate 	 * Hack to provide rpc-based message passing
276*7c478bd9Sstevel@tonic-gate 	 */
277*7c478bd9Sstevel@tonic-gate 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
278*7c478bd9Sstevel@tonic-gate 		return (cu->cu_error.re_status = RPC_TIMEDOUT);
279*7c478bd9Sstevel@tonic-gate 	}
280*7c478bd9Sstevel@tonic-gate 	/*
281*7c478bd9Sstevel@tonic-gate 	 * sub-optimal code appears here because we have
282*7c478bd9Sstevel@tonic-gate 	 * some clock time to spare while the packets are in flight.
283*7c478bd9Sstevel@tonic-gate 	 * (We assume that this is actually only executed once.)
284*7c478bd9Sstevel@tonic-gate 	 */
285*7c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_verf = _null_auth;
286*7c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.where = resultsp;
287*7c478bd9Sstevel@tonic-gate 	reply_msg.acpted_rply.ar_results.proc = xresults;
288*7c478bd9Sstevel@tonic-gate 	FD_ZERO(&mask);
289*7c478bd9Sstevel@tonic-gate 	FD_SET(cu->cu_sock, &mask);
290*7c478bd9Sstevel@tonic-gate 	for (;;) {
291*7c478bd9Sstevel@tonic-gate 		readfds = mask;
292*7c478bd9Sstevel@tonic-gate 		switch (select(__rpc_dtbsize(), &readfds, NULL,
293*7c478bd9Sstevel@tonic-gate 		    NULL, &(retransmit_time))) {
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 		case 0:
296*7c478bd9Sstevel@tonic-gate 			time_waited.tv_sec += retransmit_time.tv_sec;
297*7c478bd9Sstevel@tonic-gate 			time_waited.tv_usec += retransmit_time.tv_usec;
298*7c478bd9Sstevel@tonic-gate 			while (time_waited.tv_usec >= 1000000) {
299*7c478bd9Sstevel@tonic-gate 				time_waited.tv_sec++;
300*7c478bd9Sstevel@tonic-gate 				time_waited.tv_usec -= 1000000;
301*7c478bd9Sstevel@tonic-gate 			}
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 			/* update retransmit_time */
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) {
306*7c478bd9Sstevel@tonic-gate 			retransmit_time.tv_usec += retransmit_time.tv_usec;
307*7c478bd9Sstevel@tonic-gate 			retransmit_time.tv_sec += retransmit_time.tv_sec;
308*7c478bd9Sstevel@tonic-gate 			while (retransmit_time.tv_usec >= 1000000) {
309*7c478bd9Sstevel@tonic-gate 				retransmit_time.tv_sec++;
310*7c478bd9Sstevel@tonic-gate 				retransmit_time.tv_usec -= 1000000;
311*7c478bd9Sstevel@tonic-gate 				}
312*7c478bd9Sstevel@tonic-gate 			}
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 			if ((time_waited.tv_sec < timeout.tv_sec) ||
315*7c478bd9Sstevel@tonic-gate 				((time_waited.tv_sec == timeout.tv_sec) &&
316*7c478bd9Sstevel@tonic-gate 				(time_waited.tv_usec < timeout.tv_usec)))
317*7c478bd9Sstevel@tonic-gate 				goto send_again;
318*7c478bd9Sstevel@tonic-gate 			return (cu->cu_error.re_status = RPC_TIMEDOUT);
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 		/*
321*7c478bd9Sstevel@tonic-gate 		 * buggy in other cases because time_waited is not being
322*7c478bd9Sstevel@tonic-gate 		 * updated.
323*7c478bd9Sstevel@tonic-gate 		 */
324*7c478bd9Sstevel@tonic-gate 		case -1:
325*7c478bd9Sstevel@tonic-gate 			if (errno != EINTR) {
326*7c478bd9Sstevel@tonic-gate 				cu->cu_error.re_errno = errno;
327*7c478bd9Sstevel@tonic-gate 				return (cu->cu_error.re_status = RPC_CANTRECV);
328*7c478bd9Sstevel@tonic-gate 			}
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 			/* interrupted by another signal, update time_waited */
331*7c478bd9Sstevel@tonic-gate 			if (firsttimeout) {
332*7c478bd9Sstevel@tonic-gate 				/*
333*7c478bd9Sstevel@tonic-gate 				 * Could have done gettimeofday before clnt_call
334*7c478bd9Sstevel@tonic-gate 				 * but that means 1 more system call per each
335*7c478bd9Sstevel@tonic-gate 				 * clnt_call, so do it after first time out
336*7c478bd9Sstevel@tonic-gate 				 */
337*7c478bd9Sstevel@tonic-gate 				if (gettimeofday(&startime,
338*7c478bd9Sstevel@tonic-gate 					(struct timezone *) NULL) == -1) {
339*7c478bd9Sstevel@tonic-gate 					errno = 0;
340*7c478bd9Sstevel@tonic-gate 					continue;
341*7c478bd9Sstevel@tonic-gate 				}
342*7c478bd9Sstevel@tonic-gate 				firsttimeout = 0;
343*7c478bd9Sstevel@tonic-gate 				errno = 0;
344*7c478bd9Sstevel@tonic-gate 				continue;
345*7c478bd9Sstevel@tonic-gate 			};
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 			if (gettimeofday(&curtime,
348*7c478bd9Sstevel@tonic-gate 				(struct timezone *) NULL) == -1) {
349*7c478bd9Sstevel@tonic-gate 				errno = 0;
350*7c478bd9Sstevel@tonic-gate 				continue;
351*7c478bd9Sstevel@tonic-gate 			};
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 			time_waited.tv_sec += curtime.tv_sec - startime.tv_sec;
354*7c478bd9Sstevel@tonic-gate 			time_waited.tv_usec += curtime.tv_usec -
355*7c478bd9Sstevel@tonic-gate 							startime.tv_usec;
356*7c478bd9Sstevel@tonic-gate 			while (time_waited.tv_usec < 0) {
357*7c478bd9Sstevel@tonic-gate 				time_waited.tv_sec--;
358*7c478bd9Sstevel@tonic-gate 				time_waited.tv_usec += 1000000;
359*7c478bd9Sstevel@tonic-gate 			};
360*7c478bd9Sstevel@tonic-gate 			while (time_waited.tv_usec >= 1000000) {
361*7c478bd9Sstevel@tonic-gate 				time_waited.tv_sec++;
362*7c478bd9Sstevel@tonic-gate 				time_waited.tv_usec -= 1000000;
363*7c478bd9Sstevel@tonic-gate 			}
364*7c478bd9Sstevel@tonic-gate 			startime.tv_sec = curtime.tv_sec;
365*7c478bd9Sstevel@tonic-gate 			startime.tv_usec = curtime.tv_usec;
366*7c478bd9Sstevel@tonic-gate 			if ((time_waited.tv_sec > timeout.tv_sec) ||
367*7c478bd9Sstevel@tonic-gate 				((time_waited.tv_sec == timeout.tv_sec) &&
368*7c478bd9Sstevel@tonic-gate 				(time_waited.tv_usec > timeout.tv_usec))) {
369*7c478bd9Sstevel@tonic-gate 				return (cu->cu_error.re_status = RPC_TIMEDOUT);
370*7c478bd9Sstevel@tonic-gate 			}
371*7c478bd9Sstevel@tonic-gate 			errno = 0; /* reset it */
372*7c478bd9Sstevel@tonic-gate 			continue;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 		}
375*7c478bd9Sstevel@tonic-gate 		do {
376*7c478bd9Sstevel@tonic-gate 			fromlen = sizeof (struct sockaddr);
377*7c478bd9Sstevel@tonic-gate 			inlen = _recvfrom(cu->cu_sock, cu->cu_inbuf,
378*7c478bd9Sstevel@tonic-gate 				(int) cu->cu_recvsz, 0,
379*7c478bd9Sstevel@tonic-gate 				(struct sockaddr *)&from, &fromlen);
380*7c478bd9Sstevel@tonic-gate 		} while (inlen < 0 && errno == EINTR);
381*7c478bd9Sstevel@tonic-gate 		if (inlen < 0) {
382*7c478bd9Sstevel@tonic-gate 			if (errno == EWOULDBLOCK)
383*7c478bd9Sstevel@tonic-gate 				continue;
384*7c478bd9Sstevel@tonic-gate 			cu->cu_error.re_errno = errno;
385*7c478bd9Sstevel@tonic-gate 			return (cu->cu_error.re_status = RPC_CANTRECV);
386*7c478bd9Sstevel@tonic-gate 		}
387*7c478bd9Sstevel@tonic-gate 		if (inlen < sizeof (uint32_t))
388*7c478bd9Sstevel@tonic-gate 			continue;
389*7c478bd9Sstevel@tonic-gate 		/* see if reply transaction id matches sent id */
390*7c478bd9Sstevel@tonic-gate 		if (*((uint32_t *)(cu->cu_inbuf)) !=
391*7c478bd9Sstevel@tonic-gate 				*((uint32_t *)(cu->cu_outbuf)))
392*7c478bd9Sstevel@tonic-gate 			continue;
393*7c478bd9Sstevel@tonic-gate 		/* we now assume we have the proper reply */
394*7c478bd9Sstevel@tonic-gate 		break;
395*7c478bd9Sstevel@tonic-gate 	}
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	/*
398*7c478bd9Sstevel@tonic-gate 	 * now decode and validate the response
399*7c478bd9Sstevel@tonic-gate 	 */
400*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
401*7c478bd9Sstevel@tonic-gate 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
402*7c478bd9Sstevel@tonic-gate 	/* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
403*7c478bd9Sstevel@tonic-gate 	if (ok) {
404*7c478bd9Sstevel@tonic-gate 		__seterr_reply(&reply_msg, &(cu->cu_error));
405*7c478bd9Sstevel@tonic-gate 		if (cu->cu_error.re_status == RPC_SUCCESS) {
406*7c478bd9Sstevel@tonic-gate 			if (! AUTH_VALIDATE(cl->cl_auth,
407*7c478bd9Sstevel@tonic-gate 				&reply_msg.acpted_rply.ar_verf)) {
408*7c478bd9Sstevel@tonic-gate 				cu->cu_error.re_status = RPC_AUTHERROR;
409*7c478bd9Sstevel@tonic-gate 				cu->cu_error.re_why = AUTH_INVALIDRESP;
410*7c478bd9Sstevel@tonic-gate 			}
411*7c478bd9Sstevel@tonic-gate 			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
412*7c478bd9Sstevel@tonic-gate 				xdrs->x_op = XDR_FREE;
413*7c478bd9Sstevel@tonic-gate 				(void) xdr_opaque_auth(xdrs,
414*7c478bd9Sstevel@tonic-gate 				    &(reply_msg.acpted_rply.ar_verf));
415*7c478bd9Sstevel@tonic-gate 			}
416*7c478bd9Sstevel@tonic-gate 		}  /* end successful completion */
417*7c478bd9Sstevel@tonic-gate 		else {
418*7c478bd9Sstevel@tonic-gate 			/* maybe our credentials need to be refreshed ... */
419*7c478bd9Sstevel@tonic-gate 			if (nrefreshes > 0 &&
420*7c478bd9Sstevel@tonic-gate 				AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
421*7c478bd9Sstevel@tonic-gate 				nrefreshes--;
422*7c478bd9Sstevel@tonic-gate 				goto call_again;
423*7c478bd9Sstevel@tonic-gate 			}
424*7c478bd9Sstevel@tonic-gate 		}  /* end of unsuccessful completion */
425*7c478bd9Sstevel@tonic-gate 	}  /* end of valid reply message */
426*7c478bd9Sstevel@tonic-gate 	else {
427*7c478bd9Sstevel@tonic-gate 		cu->cu_error.re_status = RPC_CANTDECODERES;
428*7c478bd9Sstevel@tonic-gate 	}
429*7c478bd9Sstevel@tonic-gate 	return (cu->cu_error.re_status);
430*7c478bd9Sstevel@tonic-gate }
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate static void
clntudp_geterr(cl,errp)433*7c478bd9Sstevel@tonic-gate clntudp_geterr(cl, errp)
434*7c478bd9Sstevel@tonic-gate 	CLIENT *cl;
435*7c478bd9Sstevel@tonic-gate 	struct rpc_err *errp;
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	*errp = cu->cu_error;
440*7c478bd9Sstevel@tonic-gate }
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate static bool_t
clntudp_freeres(cl,xdr_res,res_ptr)444*7c478bd9Sstevel@tonic-gate clntudp_freeres(cl, xdr_res, res_ptr)
445*7c478bd9Sstevel@tonic-gate 	CLIENT *cl;
446*7c478bd9Sstevel@tonic-gate 	xdrproc_t xdr_res;
447*7c478bd9Sstevel@tonic-gate 	caddr_t res_ptr;
448*7c478bd9Sstevel@tonic-gate {
449*7c478bd9Sstevel@tonic-gate 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
450*7c478bd9Sstevel@tonic-gate 	register XDR *xdrs = &(cu->cu_outxdrs);
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	xdrs->x_op = XDR_FREE;
453*7c478bd9Sstevel@tonic-gate 	return ((*xdr_res)(xdrs, res_ptr));
454*7c478bd9Sstevel@tonic-gate }
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate static void
clntudp_abort()457*7c478bd9Sstevel@tonic-gate clntudp_abort()
458*7c478bd9Sstevel@tonic-gate 	/* CLIENT *h; */
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate }
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate static bool_t
clntudp_control(cl,request,info)463*7c478bd9Sstevel@tonic-gate clntudp_control(cl, request, info)
464*7c478bd9Sstevel@tonic-gate 	CLIENT *cl;
465*7c478bd9Sstevel@tonic-gate 	int request;
466*7c478bd9Sstevel@tonic-gate 	char *info;
467*7c478bd9Sstevel@tonic-gate {
468*7c478bd9Sstevel@tonic-gate 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	switch (request) {
471*7c478bd9Sstevel@tonic-gate 	case CLSET_TIMEOUT:
472*7c478bd9Sstevel@tonic-gate 		cu->cu_total = *(struct timeval *)info;
473*7c478bd9Sstevel@tonic-gate 		break;
474*7c478bd9Sstevel@tonic-gate 	case CLGET_TIMEOUT:
475*7c478bd9Sstevel@tonic-gate 		*(struct timeval *)info = cu->cu_total;
476*7c478bd9Sstevel@tonic-gate 		break;
477*7c478bd9Sstevel@tonic-gate 	case CLSET_RETRY_TIMEOUT:
478*7c478bd9Sstevel@tonic-gate 		cu->cu_wait = *(struct timeval *)info;
479*7c478bd9Sstevel@tonic-gate 		break;
480*7c478bd9Sstevel@tonic-gate 	case CLGET_RETRY_TIMEOUT:
481*7c478bd9Sstevel@tonic-gate 		*(struct timeval *)info = cu->cu_wait;
482*7c478bd9Sstevel@tonic-gate 		break;
483*7c478bd9Sstevel@tonic-gate 	case CLGET_SERVER_ADDR:
484*7c478bd9Sstevel@tonic-gate 		*(struct sockaddr_in *)info = cu->cu_raddr;
485*7c478bd9Sstevel@tonic-gate 		break;
486*7c478bd9Sstevel@tonic-gate 	case CLGET_FD:
487*7c478bd9Sstevel@tonic-gate 		*(int *)info = cu->cu_sock;
488*7c478bd9Sstevel@tonic-gate 		break;
489*7c478bd9Sstevel@tonic-gate 	case CLSET_FD_CLOSE:
490*7c478bd9Sstevel@tonic-gate 		cu->cu_closeit = TRUE;
491*7c478bd9Sstevel@tonic-gate 		break;
492*7c478bd9Sstevel@tonic-gate 	case CLSET_FD_NCLOSE:
493*7c478bd9Sstevel@tonic-gate 		cu->cu_closeit = FALSE;
494*7c478bd9Sstevel@tonic-gate 		break;
495*7c478bd9Sstevel@tonic-gate 	default:
496*7c478bd9Sstevel@tonic-gate 		return (FALSE);
497*7c478bd9Sstevel@tonic-gate 	}
498*7c478bd9Sstevel@tonic-gate 	return (TRUE);
499*7c478bd9Sstevel@tonic-gate }
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate static void
clntudp_destroy(cl)502*7c478bd9Sstevel@tonic-gate clntudp_destroy(cl)
503*7c478bd9Sstevel@tonic-gate 	CLIENT *cl;
504*7c478bd9Sstevel@tonic-gate {
505*7c478bd9Sstevel@tonic-gate 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	if (cu->cu_closeit) {
508*7c478bd9Sstevel@tonic-gate 		(void) close(cu->cu_sock);
509*7c478bd9Sstevel@tonic-gate 	}
510*7c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&(cu->cu_outxdrs));
511*7c478bd9Sstevel@tonic-gate 	mem_free((caddr_t)cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
512*7c478bd9Sstevel@tonic-gate 	mem_free((caddr_t)cl, sizeof (CLIENT));
513*7c478bd9Sstevel@tonic-gate }
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate static struct clnt_ops *
clntudp_ops()516*7c478bd9Sstevel@tonic-gate clntudp_ops()
517*7c478bd9Sstevel@tonic-gate {
518*7c478bd9Sstevel@tonic-gate 	static struct clnt_ops ops;
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 	if (ops.cl_call == NULL) {
521*7c478bd9Sstevel@tonic-gate 		ops.cl_call = clntudp_call;
522*7c478bd9Sstevel@tonic-gate 		ops.cl_abort = clntudp_abort;
523*7c478bd9Sstevel@tonic-gate 		ops.cl_geterr = clntudp_geterr;
524*7c478bd9Sstevel@tonic-gate 		ops.cl_freeres = clntudp_freeres;
525*7c478bd9Sstevel@tonic-gate 		ops.cl_destroy = clntudp_destroy;
526*7c478bd9Sstevel@tonic-gate 		ops.cl_control = clntudp_control;
527*7c478bd9Sstevel@tonic-gate 	}
528*7c478bd9Sstevel@tonic-gate 	return (&ops);
529*7c478bd9Sstevel@tonic-gate }
530