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