clnt_dg.c revision 1.26 1 1.26 matt /* $NetBSD: clnt_dg.c,v 1.26 2012/03/20 17:14:50 matt Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.1 fvdl * unrestricted use provided that this legend is included on all tape
6 1.1 fvdl * media and as a part of the software program in whole or part. Users
7 1.1 fvdl * may copy or modify Sun RPC without charge, but are not authorized
8 1.1 fvdl * to license or distribute it to anyone else except as part of a product or
9 1.1 fvdl * program developed by the user.
10 1.1 fvdl *
11 1.1 fvdl * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 fvdl * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 fvdl * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.1 fvdl *
15 1.1 fvdl * Sun RPC is provided with no support and without any obligation on the
16 1.1 fvdl * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 fvdl * modification or enhancement.
18 1.1 fvdl *
19 1.1 fvdl * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 fvdl * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 fvdl * OR ANY PART THEREOF.
22 1.1 fvdl *
23 1.1 fvdl * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 fvdl * or profits or other special, indirect and consequential damages, even if
25 1.1 fvdl * Sun has been advised of the possibility of such damages.
26 1.1 fvdl *
27 1.1 fvdl * Sun Microsystems, Inc.
28 1.1 fvdl * 2550 Garcia Avenue
29 1.1 fvdl * Mountain View, California 94043
30 1.1 fvdl */
31 1.1 fvdl /*
32 1.1 fvdl * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 1.1 fvdl */
34 1.1 fvdl
35 1.1 fvdl /* #ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" */
36 1.1 fvdl
37 1.12 itojun #include <sys/cdefs.h>
38 1.12 itojun #if defined(LIBC_SCCS) && !defined(lint)
39 1.1 fvdl #if 0
40 1.1 fvdl static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
41 1.12 itojun #else
42 1.26 matt __RCSID("$NetBSD: clnt_dg.c,v 1.26 2012/03/20 17:14:50 matt Exp $");
43 1.1 fvdl #endif
44 1.1 fvdl #endif
45 1.1 fvdl
46 1.1 fvdl /*
47 1.1 fvdl * Implements a connectionless client side RPC.
48 1.1 fvdl */
49 1.1 fvdl
50 1.1 fvdl #include "namespace.h"
51 1.1 fvdl #include "reentrant.h"
52 1.21 chs #include <sys/poll.h>
53 1.1 fvdl #include <sys/types.h>
54 1.1 fvdl #include <sys/time.h>
55 1.1 fvdl #include <sys/socket.h>
56 1.1 fvdl #include <sys/ioctl.h>
57 1.1 fvdl #include <rpc/rpc.h>
58 1.7 lukem #include <assert.h>
59 1.1 fvdl #include <errno.h>
60 1.1 fvdl #include <stdlib.h>
61 1.2 thorpej #include <string.h>
62 1.1 fvdl #include <signal.h>
63 1.1 fvdl #include <unistd.h>
64 1.1 fvdl #include <err.h>
65 1.8 fvdl #include "rpc_internal.h"
66 1.1 fvdl
67 1.1 fvdl #ifdef __weak_alias
68 1.1 fvdl __weak_alias(clnt_dg_create,_clnt_dg_create)
69 1.1 fvdl #endif
70 1.1 fvdl
71 1.1 fvdl #define RPC_MAX_BACKOFF 30 /* seconds */
72 1.1 fvdl
73 1.1 fvdl
74 1.26 matt static struct clnt_ops *clnt_dg_ops(void);
75 1.26 matt static bool_t time_not_ok(struct timeval *);
76 1.26 matt static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t,
77 1.26 matt const char *, xdrproc_t, caddr_t, struct timeval);
78 1.26 matt static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
79 1.26 matt static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, caddr_t);
80 1.26 matt static void clnt_dg_abort(CLIENT *);
81 1.26 matt static bool_t clnt_dg_control(CLIENT *, u_int, char *);
82 1.26 matt static void clnt_dg_destroy(CLIENT *);
83 1.1 fvdl
84 1.1 fvdl
85 1.1 fvdl
86 1.1 fvdl
87 1.1 fvdl /*
88 1.1 fvdl * This machinery implements per-fd locks for MT-safety. It is not
89 1.1 fvdl * sufficient to do per-CLIENT handle locks for MT-safety because a
90 1.1 fvdl * user may create more than one CLIENT handle with the same fd behind
91 1.1 fvdl * it. Therfore, we allocate an array of flags (dg_fd_locks), protected
92 1.1 fvdl * by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
93 1.1 fvdl * similarly protected. Dg_fd_lock[fd] == 1 => a call is activte on some
94 1.1 fvdl * CLIENT handle created for that fd.
95 1.1 fvdl * The current implementation holds locks across the entire RPC and reply,
96 1.1 fvdl * including retransmissions. Yes, this is silly, and as soon as this
97 1.1 fvdl * code is proven to work, this should be the first thing fixed. One step
98 1.1 fvdl * at a time.
99 1.1 fvdl */
100 1.1 fvdl static int *dg_fd_locks;
101 1.9 thorpej #ifdef _REENTRANT
102 1.9 thorpej #define __rpc_lock_value __isthreaded;
103 1.1 fvdl extern mutex_t clnt_fd_lock;
104 1.1 fvdl static cond_t *dg_cv;
105 1.1 fvdl #define release_fd_lock(fd, mask) { \
106 1.1 fvdl mutex_lock(&clnt_fd_lock); \
107 1.1 fvdl dg_fd_locks[fd] = 0; \
108 1.1 fvdl mutex_unlock(&clnt_fd_lock); \
109 1.22 christos thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
110 1.1 fvdl cond_signal(&dg_cv[fd]); \
111 1.1 fvdl }
112 1.1 fvdl #else
113 1.1 fvdl #define release_fd_lock(fd,mask)
114 1.1 fvdl #define __rpc_lock_value 0
115 1.1 fvdl #endif
116 1.1 fvdl
117 1.1 fvdl static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
118 1.1 fvdl
119 1.1 fvdl /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
120 1.1 fvdl
121 1.1 fvdl /*
122 1.1 fvdl * Private data kept per client handle
123 1.1 fvdl */
124 1.1 fvdl struct cu_data {
125 1.1 fvdl int cu_fd; /* connections fd */
126 1.1 fvdl bool_t cu_closeit; /* opened by library */
127 1.1 fvdl struct sockaddr_storage cu_raddr; /* remote address */
128 1.1 fvdl int cu_rlen;
129 1.1 fvdl struct timeval cu_wait; /* retransmit interval */
130 1.1 fvdl struct timeval cu_total; /* total time for the call */
131 1.1 fvdl struct rpc_err cu_error;
132 1.1 fvdl XDR cu_outxdrs;
133 1.1 fvdl u_int cu_xdrpos;
134 1.1 fvdl u_int cu_sendsz; /* send size */
135 1.1 fvdl char *cu_outbuf;
136 1.1 fvdl u_int cu_recvsz; /* recv size */
137 1.21 chs struct pollfd cu_pfdp;
138 1.1 fvdl char cu_inbuf[1];
139 1.1 fvdl };
140 1.1 fvdl
141 1.1 fvdl /*
142 1.1 fvdl * Connection less client creation returns with client handle parameters.
143 1.1 fvdl * Default options are set, which the user can change using clnt_control().
144 1.1 fvdl * fd should be open and bound.
145 1.1 fvdl * NB: The rpch->cl_auth is initialized to null authentication.
146 1.1 fvdl * Caller may wish to set this something more useful.
147 1.1 fvdl *
148 1.1 fvdl * sendsz and recvsz are the maximum allowable packet sizes that can be
149 1.1 fvdl * sent and received. Normally they are the same, but they can be
150 1.1 fvdl * changed to improve the program efficiency and buffer allocation.
151 1.1 fvdl * If they are 0, use the transport default.
152 1.1 fvdl *
153 1.1 fvdl * If svcaddr is NULL, returns NULL.
154 1.1 fvdl */
155 1.1 fvdl CLIENT *
156 1.26 matt clnt_dg_create(
157 1.26 matt int fd, /* open file descriptor */
158 1.26 matt const struct netbuf *svcaddr, /* servers address */
159 1.26 matt rpcprog_t program, /* program number */
160 1.26 matt rpcvers_t version, /* version number */
161 1.26 matt u_int sendsz, /* buffer recv size */
162 1.26 matt u_int recvsz) /* buffer send size */
163 1.1 fvdl {
164 1.3 christos CLIENT *cl = NULL; /* client handle */
165 1.3 christos struct cu_data *cu = NULL; /* private data */
166 1.1 fvdl struct rpc_msg call_msg;
167 1.9 thorpej #ifdef _REENTRANT
168 1.1 fvdl sigset_t mask;
169 1.1 fvdl #endif
170 1.1 fvdl sigset_t newmask;
171 1.1 fvdl struct __rpc_sockinfo si;
172 1.1 fvdl int one = 1;
173 1.1 fvdl
174 1.1 fvdl sigfillset(&newmask);
175 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
176 1.1 fvdl mutex_lock(&clnt_fd_lock);
177 1.22 christos if (dg_fd_locks == NULL) {
178 1.9 thorpej #ifdef _REENTRANT
179 1.9 thorpej size_t cv_allocsz;
180 1.1 fvdl #endif
181 1.3 christos size_t fd_allocsz;
182 1.1 fvdl int dtbsize = __rpc_dtbsize();
183 1.1 fvdl
184 1.1 fvdl fd_allocsz = dtbsize * sizeof (int);
185 1.22 christos dg_fd_locks = mem_alloc(fd_allocsz);
186 1.22 christos if (dg_fd_locks == NULL) {
187 1.1 fvdl mutex_unlock(&clnt_fd_lock);
188 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
189 1.1 fvdl goto err1;
190 1.1 fvdl } else
191 1.1 fvdl memset(dg_fd_locks, '\0', fd_allocsz);
192 1.1 fvdl
193 1.9 thorpej #ifdef _REENTRANT
194 1.1 fvdl cv_allocsz = dtbsize * sizeof (cond_t);
195 1.22 christos dg_cv = mem_alloc(cv_allocsz);
196 1.22 christos if (dg_cv == NULL) {
197 1.1 fvdl mem_free(dg_fd_locks, fd_allocsz);
198 1.22 christos dg_fd_locks = NULL;
199 1.1 fvdl mutex_unlock(&clnt_fd_lock);
200 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
201 1.1 fvdl goto err1;
202 1.1 fvdl } else {
203 1.1 fvdl int i;
204 1.1 fvdl
205 1.1 fvdl for (i = 0; i < dtbsize; i++)
206 1.1 fvdl cond_init(&dg_cv[i], 0, (void *) 0);
207 1.1 fvdl }
208 1.1 fvdl #endif
209 1.1 fvdl }
210 1.1 fvdl
211 1.1 fvdl mutex_unlock(&clnt_fd_lock);
212 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
213 1.1 fvdl
214 1.3 christos if (svcaddr == NULL) {
215 1.1 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
216 1.3 christos return (NULL);
217 1.1 fvdl }
218 1.1 fvdl
219 1.1 fvdl if (!__rpc_fd2sockinfo(fd, &si)) {
220 1.1 fvdl rpc_createerr.cf_stat = RPC_TLIERROR;
221 1.1 fvdl rpc_createerr.cf_error.re_errno = 0;
222 1.3 christos return (NULL);
223 1.1 fvdl }
224 1.1 fvdl /*
225 1.1 fvdl * Find the receive and the send size
226 1.1 fvdl */
227 1.1 fvdl sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
228 1.1 fvdl recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
229 1.1 fvdl if ((sendsz == 0) || (recvsz == 0)) {
230 1.1 fvdl rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
231 1.1 fvdl rpc_createerr.cf_error.re_errno = 0;
232 1.3 christos return (NULL);
233 1.1 fvdl }
234 1.1 fvdl
235 1.3 christos if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
236 1.1 fvdl goto err1;
237 1.1 fvdl /*
238 1.1 fvdl * Should be multiple of 4 for XDR.
239 1.1 fvdl */
240 1.1 fvdl sendsz = ((sendsz + 3) / 4) * 4;
241 1.1 fvdl recvsz = ((recvsz + 3) / 4) * 4;
242 1.15 yamt cu = malloc(sizeof (*cu) + sendsz + recvsz);
243 1.3 christos if (cu == NULL)
244 1.1 fvdl goto err1;
245 1.15 yamt memset(cu, 0, sizeof(*cu));
246 1.3 christos (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
247 1.1 fvdl cu->cu_rlen = svcaddr->len;
248 1.1 fvdl cu->cu_outbuf = &cu->cu_inbuf[recvsz];
249 1.1 fvdl /* Other values can also be set through clnt_control() */
250 1.1 fvdl cu->cu_wait.tv_sec = 15; /* heuristically chosen */
251 1.1 fvdl cu->cu_wait.tv_usec = 0;
252 1.1 fvdl cu->cu_total.tv_sec = -1;
253 1.1 fvdl cu->cu_total.tv_usec = -1;
254 1.1 fvdl cu->cu_sendsz = sendsz;
255 1.1 fvdl cu->cu_recvsz = recvsz;
256 1.11 itojun call_msg.rm_xid = __RPC_GETXID();
257 1.1 fvdl call_msg.rm_call.cb_prog = program;
258 1.1 fvdl call_msg.rm_call.cb_vers = version;
259 1.1 fvdl xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
260 1.1 fvdl if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
261 1.1 fvdl rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */
262 1.1 fvdl rpc_createerr.cf_error.re_errno = 0;
263 1.1 fvdl goto err2;
264 1.1 fvdl }
265 1.1 fvdl cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
266 1.1 fvdl
267 1.1 fvdl /* XXX fvdl - do we still want this? */
268 1.1 fvdl #if 0
269 1.1 fvdl (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
270 1.1 fvdl #endif
271 1.1 fvdl ioctl(fd, FIONBIO, (char *)(void *)&one);
272 1.1 fvdl
273 1.1 fvdl /*
274 1.1 fvdl * By default, closeit is always FALSE. It is users responsibility
275 1.1 fvdl * to do a close on it, else the user may use clnt_control
276 1.1 fvdl * to let clnt_destroy do it for him/her.
277 1.1 fvdl */
278 1.1 fvdl cu->cu_closeit = FALSE;
279 1.1 fvdl cu->cu_fd = fd;
280 1.21 chs cu->cu_pfdp.fd = cu->cu_fd;
281 1.21 chs cu->cu_pfdp.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
282 1.1 fvdl cl->cl_ops = clnt_dg_ops();
283 1.3 christos cl->cl_private = (caddr_t)(void *)cu;
284 1.1 fvdl cl->cl_auth = authnone_create();
285 1.3 christos cl->cl_tp = NULL;
286 1.3 christos cl->cl_netid = NULL;
287 1.1 fvdl return (cl);
288 1.1 fvdl err1:
289 1.1 fvdl warnx(mem_err_clnt_dg);
290 1.1 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
291 1.1 fvdl rpc_createerr.cf_error.re_errno = errno;
292 1.1 fvdl err2:
293 1.1 fvdl if (cl) {
294 1.3 christos mem_free(cl, sizeof (CLIENT));
295 1.1 fvdl if (cu)
296 1.3 christos mem_free(cu, sizeof (*cu) + sendsz + recvsz);
297 1.1 fvdl }
298 1.3 christos return (NULL);
299 1.1 fvdl }
300 1.1 fvdl
301 1.1 fvdl static enum clnt_stat
302 1.26 matt clnt_dg_call(
303 1.26 matt CLIENT * cl, /* client handle */
304 1.26 matt rpcproc_t proc, /* procedure number */
305 1.26 matt xdrproc_t xargs, /* xdr routine for args */
306 1.26 matt const char * argsp, /* pointer to args */
307 1.26 matt xdrproc_t xresults, /* xdr routine for results */
308 1.26 matt caddr_t resultsp, /* pointer to results */
309 1.26 matt struct timeval utimeout) /* seconds to wait before giving up */
310 1.1 fvdl {
311 1.7 lukem struct cu_data *cu;
312 1.3 christos XDR *xdrs;
313 1.3 christos size_t outlen;
314 1.1 fvdl struct rpc_msg reply_msg;
315 1.1 fvdl XDR reply_xdrs;
316 1.1 fvdl bool_t ok;
317 1.1 fvdl int nrefreshes = 2; /* number of times to refresh cred */
318 1.1 fvdl struct timeval timeout;
319 1.1 fvdl struct timeval retransmit_time;
320 1.18 rpaulo struct timeval next_sendtime, starttime, time_waited, tv;
321 1.9 thorpej #ifdef _REENTRANT
322 1.21 chs sigset_t mask, *maskp = &mask;
323 1.21 chs #else
324 1.21 chs sigset_t *maskp = NULL;
325 1.1 fvdl #endif
326 1.1 fvdl sigset_t newmask;
327 1.3 christos ssize_t recvlen = 0;
328 1.16 christos struct timespec ts;
329 1.18 rpaulo int n;
330 1.1 fvdl
331 1.7 lukem _DIAGASSERT(cl != NULL);
332 1.7 lukem
333 1.7 lukem cu = (struct cu_data *)cl->cl_private;
334 1.7 lukem
335 1.1 fvdl sigfillset(&newmask);
336 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
337 1.1 fvdl mutex_lock(&clnt_fd_lock);
338 1.1 fvdl while (dg_fd_locks[cu->cu_fd])
339 1.1 fvdl cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
340 1.1 fvdl dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
341 1.1 fvdl mutex_unlock(&clnt_fd_lock);
342 1.1 fvdl if (cu->cu_total.tv_usec == -1) {
343 1.1 fvdl timeout = utimeout; /* use supplied timeout */
344 1.1 fvdl } else {
345 1.1 fvdl timeout = cu->cu_total; /* use default timeout */
346 1.1 fvdl }
347 1.1 fvdl
348 1.1 fvdl time_waited.tv_sec = 0;
349 1.1 fvdl time_waited.tv_usec = 0;
350 1.18 rpaulo retransmit_time = next_sendtime = cu->cu_wait;
351 1.18 rpaulo gettimeofday(&starttime, NULL);
352 1.18 rpaulo
353 1.1 fvdl call_again:
354 1.1 fvdl xdrs = &(cu->cu_outxdrs);
355 1.1 fvdl xdrs->x_op = XDR_ENCODE;
356 1.1 fvdl XDR_SETPOS(xdrs, cu->cu_xdrpos);
357 1.1 fvdl /*
358 1.1 fvdl * the transaction is the first thing in the out buffer
359 1.1 fvdl */
360 1.3 christos (*(u_int32_t *)(void *)(cu->cu_outbuf))++;
361 1.6 christos if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
362 1.1 fvdl (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
363 1.20 yamt (! (*xargs)(xdrs, __UNCONST(argsp)))) {
364 1.18 rpaulo cu->cu_error.re_status = RPC_CANTENCODEARGS;
365 1.18 rpaulo goto out;
366 1.1 fvdl }
367 1.3 christos outlen = (size_t)XDR_GETPOS(xdrs);
368 1.1 fvdl
369 1.1 fvdl send_again:
370 1.23 lukem if ((size_t)sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0,
371 1.3 christos (struct sockaddr *)(void *)&cu->cu_raddr, (socklen_t)cu->cu_rlen)
372 1.1 fvdl != outlen) {
373 1.1 fvdl cu->cu_error.re_errno = errno;
374 1.18 rpaulo cu->cu_error.re_status = RPC_CANTSEND;
375 1.18 rpaulo goto out;
376 1.1 fvdl }
377 1.1 fvdl
378 1.1 fvdl /*
379 1.1 fvdl * Hack to provide rpc-based message passing
380 1.1 fvdl */
381 1.1 fvdl if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
382 1.18 rpaulo cu->cu_error.re_status = RPC_TIMEDOUT;
383 1.18 rpaulo goto out;
384 1.1 fvdl }
385 1.1 fvdl /*
386 1.1 fvdl * sub-optimal code appears here because we have
387 1.1 fvdl * some clock time to spare while the packets are in flight.
388 1.1 fvdl * (We assume that this is actually only executed once.)
389 1.1 fvdl */
390 1.1 fvdl reply_msg.acpted_rply.ar_verf = _null_auth;
391 1.1 fvdl reply_msg.acpted_rply.ar_results.where = resultsp;
392 1.1 fvdl reply_msg.acpted_rply.ar_results.proc = xresults;
393 1.1 fvdl
394 1.1 fvdl
395 1.1 fvdl for (;;) {
396 1.18 rpaulo /* Decide how long to wait. */
397 1.18 rpaulo if (timercmp(&next_sendtime, &timeout, <))
398 1.18 rpaulo timersub(&next_sendtime, &time_waited, &tv);
399 1.18 rpaulo else
400 1.18 rpaulo timersub(&timeout, &time_waited, &tv);
401 1.18 rpaulo if (tv.tv_sec < 0 || tv.tv_usec < 0)
402 1.18 rpaulo tv.tv_sec = tv.tv_usec = 0;
403 1.18 rpaulo TIMEVAL_TO_TIMESPEC(&tv, &ts);
404 1.18 rpaulo
405 1.21 chs n = pollts(&cu->cu_pfdp, 1, &ts, maskp);
406 1.18 rpaulo if (n == 1) {
407 1.18 rpaulo /* We have some data now */
408 1.18 rpaulo do {
409 1.18 rpaulo recvlen = recvfrom(cu->cu_fd, cu->cu_inbuf,
410 1.18 rpaulo cu->cu_recvsz, 0, NULL, NULL);
411 1.18 rpaulo } while (recvlen < 0 && errno == EINTR);
412 1.18 rpaulo
413 1.18 rpaulo if (recvlen < 0 && errno != EWOULDBLOCK) {
414 1.1 fvdl cu->cu_error.re_errno = errno;
415 1.18 rpaulo cu->cu_error.re_status = RPC_CANTRECV;
416 1.18 rpaulo goto out;
417 1.1 fvdl }
418 1.25 mrg if (recvlen >= (ssize_t)sizeof(uint32_t)) {
419 1.25 mrg if (memcmp(cu->cu_inbuf, cu->cu_outbuf,
420 1.25 mrg sizeof(uint32_t)) == 0)
421 1.25 mrg /* Assume we have the proper reply. */
422 1.25 mrg break;
423 1.25 mrg }
424 1.18 rpaulo }
425 1.18 rpaulo if (n == -1) {
426 1.19 christos cu->cu_error.re_errno = errno;
427 1.19 christos cu->cu_error.re_status = RPC_CANTRECV;
428 1.19 christos goto out;
429 1.1 fvdl }
430 1.1 fvdl
431 1.18 rpaulo gettimeofday(&tv, NULL);
432 1.18 rpaulo timersub(&tv, &starttime, &time_waited);
433 1.18 rpaulo
434 1.18 rpaulo /* Check for timeout. */
435 1.18 rpaulo if (timercmp(&time_waited, &timeout, >)) {
436 1.18 rpaulo cu->cu_error.re_status = RPC_TIMEDOUT;
437 1.18 rpaulo goto out;
438 1.18 rpaulo }
439 1.18 rpaulo
440 1.18 rpaulo /* Retransmit if necessary. */
441 1.18 rpaulo if (timercmp(&time_waited, &next_sendtime, >)) {
442 1.18 rpaulo /* update retransmit_time */
443 1.18 rpaulo if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
444 1.18 rpaulo timeradd(&retransmit_time, &retransmit_time,
445 1.18 rpaulo &retransmit_time);
446 1.18 rpaulo timeradd(&next_sendtime, &retransmit_time,
447 1.18 rpaulo &next_sendtime);
448 1.18 rpaulo goto send_again;
449 1.1 fvdl }
450 1.1 fvdl }
451 1.1 fvdl
452 1.1 fvdl /*
453 1.1 fvdl * now decode and validate the response
454 1.1 fvdl */
455 1.1 fvdl
456 1.13 drochner xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
457 1.1 fvdl ok = xdr_replymsg(&reply_xdrs, &reply_msg);
458 1.1 fvdl /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
459 1.1 fvdl if (ok) {
460 1.1 fvdl if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
461 1.1 fvdl (reply_msg.acpted_rply.ar_stat == SUCCESS))
462 1.1 fvdl cu->cu_error.re_status = RPC_SUCCESS;
463 1.1 fvdl else
464 1.1 fvdl _seterr_reply(&reply_msg, &(cu->cu_error));
465 1.1 fvdl
466 1.1 fvdl if (cu->cu_error.re_status == RPC_SUCCESS) {
467 1.1 fvdl if (! AUTH_VALIDATE(cl->cl_auth,
468 1.1 fvdl &reply_msg.acpted_rply.ar_verf)) {
469 1.1 fvdl cu->cu_error.re_status = RPC_AUTHERROR;
470 1.1 fvdl cu->cu_error.re_why = AUTH_INVALIDRESP;
471 1.1 fvdl }
472 1.1 fvdl if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
473 1.1 fvdl xdrs->x_op = XDR_FREE;
474 1.1 fvdl (void) xdr_opaque_auth(xdrs,
475 1.1 fvdl &(reply_msg.acpted_rply.ar_verf));
476 1.1 fvdl }
477 1.1 fvdl } /* end successful completion */
478 1.1 fvdl /*
479 1.1 fvdl * If unsuccesful AND error is an authentication error
480 1.1 fvdl * then refresh credentials and try again, else break
481 1.1 fvdl */
482 1.1 fvdl else if (cu->cu_error.re_status == RPC_AUTHERROR)
483 1.1 fvdl /* maybe our credentials need to be refreshed ... */
484 1.1 fvdl if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
485 1.1 fvdl nrefreshes--;
486 1.1 fvdl goto call_again;
487 1.1 fvdl }
488 1.1 fvdl /* end of unsuccessful completion */
489 1.1 fvdl } /* end of valid reply message */
490 1.1 fvdl else {
491 1.1 fvdl cu->cu_error.re_status = RPC_CANTDECODERES;
492 1.1 fvdl
493 1.1 fvdl }
494 1.18 rpaulo out:
495 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
496 1.1 fvdl return (cu->cu_error.re_status);
497 1.1 fvdl }
498 1.1 fvdl
499 1.1 fvdl static void
500 1.26 matt clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
501 1.1 fvdl {
502 1.7 lukem struct cu_data *cu;
503 1.1 fvdl
504 1.7 lukem _DIAGASSERT(cl != NULL);
505 1.7 lukem _DIAGASSERT(errp != NULL);
506 1.7 lukem
507 1.7 lukem cu = (struct cu_data *)cl->cl_private;
508 1.1 fvdl *errp = cu->cu_error;
509 1.1 fvdl }
510 1.1 fvdl
511 1.1 fvdl static bool_t
512 1.26 matt clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
513 1.1 fvdl {
514 1.7 lukem struct cu_data *cu;
515 1.7 lukem XDR *xdrs;
516 1.1 fvdl bool_t dummy;
517 1.9 thorpej #ifdef _REENTRANT
518 1.1 fvdl sigset_t mask;
519 1.1 fvdl #endif
520 1.1 fvdl sigset_t newmask;
521 1.1 fvdl
522 1.7 lukem _DIAGASSERT(cl != NULL);
523 1.7 lukem cu = (struct cu_data *)cl->cl_private;
524 1.7 lukem xdrs = &(cu->cu_outxdrs);
525 1.7 lukem
526 1.1 fvdl sigfillset(&newmask);
527 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
528 1.1 fvdl mutex_lock(&clnt_fd_lock);
529 1.1 fvdl while (dg_fd_locks[cu->cu_fd])
530 1.1 fvdl cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
531 1.1 fvdl xdrs->x_op = XDR_FREE;
532 1.1 fvdl dummy = (*xdr_res)(xdrs, res_ptr);
533 1.1 fvdl mutex_unlock(&clnt_fd_lock);
534 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &mask, NULL);
535 1.1 fvdl cond_signal(&dg_cv[cu->cu_fd]);
536 1.1 fvdl return (dummy);
537 1.1 fvdl }
538 1.1 fvdl
539 1.1 fvdl /*ARGSUSED*/
540 1.1 fvdl static void
541 1.26 matt clnt_dg_abort(CLIENT *h)
542 1.1 fvdl {
543 1.1 fvdl }
544 1.1 fvdl
545 1.1 fvdl static bool_t
546 1.26 matt clnt_dg_control(CLIENT *cl, u_int request, char *info)
547 1.1 fvdl {
548 1.7 lukem struct cu_data *cu;
549 1.1 fvdl struct netbuf *addr;
550 1.9 thorpej #ifdef _REENTRANT
551 1.1 fvdl sigset_t mask;
552 1.1 fvdl #endif
553 1.1 fvdl sigset_t newmask;
554 1.1 fvdl
555 1.7 lukem _DIAGASSERT(cl != NULL);
556 1.7 lukem /* info is handled below */
557 1.7 lukem
558 1.7 lukem cu = (struct cu_data *)cl->cl_private;
559 1.7 lukem
560 1.1 fvdl sigfillset(&newmask);
561 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
562 1.1 fvdl mutex_lock(&clnt_fd_lock);
563 1.1 fvdl while (dg_fd_locks[cu->cu_fd])
564 1.1 fvdl cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
565 1.1 fvdl dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
566 1.1 fvdl mutex_unlock(&clnt_fd_lock);
567 1.1 fvdl switch (request) {
568 1.1 fvdl case CLSET_FD_CLOSE:
569 1.1 fvdl cu->cu_closeit = TRUE;
570 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
571 1.1 fvdl return (TRUE);
572 1.1 fvdl case CLSET_FD_NCLOSE:
573 1.1 fvdl cu->cu_closeit = FALSE;
574 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
575 1.1 fvdl return (TRUE);
576 1.1 fvdl }
577 1.1 fvdl
578 1.1 fvdl /* for other requests which use info */
579 1.1 fvdl if (info == NULL) {
580 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
581 1.1 fvdl return (FALSE);
582 1.1 fvdl }
583 1.1 fvdl switch (request) {
584 1.1 fvdl case CLSET_TIMEOUT:
585 1.3 christos if (time_not_ok((struct timeval *)(void *)info)) {
586 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
587 1.1 fvdl return (FALSE);
588 1.1 fvdl }
589 1.3 christos cu->cu_total = *(struct timeval *)(void *)info;
590 1.1 fvdl break;
591 1.1 fvdl case CLGET_TIMEOUT:
592 1.3 christos *(struct timeval *)(void *)info = cu->cu_total;
593 1.1 fvdl break;
594 1.1 fvdl case CLGET_SERVER_ADDR: /* Give him the fd address */
595 1.1 fvdl /* Now obsolete. Only for backward compatibility */
596 1.3 christos (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
597 1.1 fvdl break;
598 1.1 fvdl case CLSET_RETRY_TIMEOUT:
599 1.3 christos if (time_not_ok((struct timeval *)(void *)info)) {
600 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
601 1.1 fvdl return (FALSE);
602 1.1 fvdl }
603 1.3 christos cu->cu_wait = *(struct timeval *)(void *)info;
604 1.1 fvdl break;
605 1.1 fvdl case CLGET_RETRY_TIMEOUT:
606 1.3 christos *(struct timeval *)(void *)info = cu->cu_wait;
607 1.1 fvdl break;
608 1.1 fvdl case CLGET_FD:
609 1.3 christos *(int *)(void *)info = cu->cu_fd;
610 1.1 fvdl break;
611 1.1 fvdl case CLGET_SVC_ADDR:
612 1.3 christos addr = (struct netbuf *)(void *)info;
613 1.1 fvdl addr->buf = &cu->cu_raddr;
614 1.1 fvdl addr->len = cu->cu_rlen;
615 1.1 fvdl addr->maxlen = sizeof cu->cu_raddr;
616 1.1 fvdl break;
617 1.1 fvdl case CLSET_SVC_ADDR: /* set to new address */
618 1.3 christos addr = (struct netbuf *)(void *)info;
619 1.10 yamt if (addr->len < sizeof cu->cu_raddr) {
620 1.10 yamt release_fd_lock(cu->cu_fd, mask);
621 1.1 fvdl return (FALSE);
622 1.10 yamt }
623 1.14 christos (void) memcpy(&cu->cu_raddr, addr->buf, (size_t)addr->len);
624 1.1 fvdl cu->cu_rlen = addr->len;
625 1.1 fvdl break;
626 1.1 fvdl case CLGET_XID:
627 1.1 fvdl /*
628 1.1 fvdl * use the knowledge that xid is the
629 1.1 fvdl * first element in the call structure *.
630 1.1 fvdl * This will get the xid of the PREVIOUS call
631 1.1 fvdl */
632 1.3 christos *(u_int32_t *)(void *)info =
633 1.3 christos ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
634 1.1 fvdl break;
635 1.1 fvdl
636 1.1 fvdl case CLSET_XID:
637 1.1 fvdl /* This will set the xid of the NEXT call */
638 1.3 christos *(u_int32_t *)(void *)cu->cu_outbuf =
639 1.3 christos htonl(*(u_int32_t *)(void *)info - 1);
640 1.1 fvdl /* decrement by 1 as clnt_dg_call() increments once */
641 1.1 fvdl break;
642 1.1 fvdl
643 1.1 fvdl case CLGET_VERS:
644 1.1 fvdl /*
645 1.1 fvdl * This RELIES on the information that, in the call body,
646 1.1 fvdl * the version number field is the fifth field from the
647 1.1 fvdl * begining of the RPC header. MUST be changed if the
648 1.1 fvdl * call_struct is changed
649 1.1 fvdl */
650 1.3 christos *(u_int32_t *)(void *)info =
651 1.3 christos ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
652 1.3 christos 4 * BYTES_PER_XDR_UNIT));
653 1.1 fvdl break;
654 1.1 fvdl
655 1.1 fvdl case CLSET_VERS:
656 1.3 christos *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
657 1.3 christos = htonl(*(u_int32_t *)(void *)info);
658 1.1 fvdl break;
659 1.1 fvdl
660 1.1 fvdl case CLGET_PROG:
661 1.1 fvdl /*
662 1.1 fvdl * This RELIES on the information that, in the call body,
663 1.1 fvdl * the program number field is the fourth field from the
664 1.1 fvdl * begining of the RPC header. MUST be changed if the
665 1.1 fvdl * call_struct is changed
666 1.1 fvdl */
667 1.3 christos *(u_int32_t *)(void *)info =
668 1.3 christos ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
669 1.3 christos 3 * BYTES_PER_XDR_UNIT));
670 1.1 fvdl break;
671 1.1 fvdl
672 1.1 fvdl case CLSET_PROG:
673 1.3 christos *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
674 1.3 christos = htonl(*(u_int32_t *)(void *)info);
675 1.1 fvdl break;
676 1.1 fvdl
677 1.1 fvdl default:
678 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
679 1.1 fvdl return (FALSE);
680 1.1 fvdl }
681 1.1 fvdl release_fd_lock(cu->cu_fd, mask);
682 1.1 fvdl return (TRUE);
683 1.1 fvdl }
684 1.1 fvdl
685 1.1 fvdl static void
686 1.26 matt clnt_dg_destroy(CLIENT *cl)
687 1.1 fvdl {
688 1.7 lukem struct cu_data *cu;
689 1.7 lukem int cu_fd;
690 1.9 thorpej #ifdef _REENTRANT
691 1.1 fvdl sigset_t mask;
692 1.1 fvdl #endif
693 1.1 fvdl sigset_t newmask;
694 1.1 fvdl
695 1.7 lukem _DIAGASSERT(cl != NULL);
696 1.7 lukem
697 1.7 lukem cu = (struct cu_data *)cl->cl_private;
698 1.7 lukem cu_fd = cu->cu_fd;
699 1.7 lukem
700 1.1 fvdl sigfillset(&newmask);
701 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
702 1.1 fvdl mutex_lock(&clnt_fd_lock);
703 1.1 fvdl while (dg_fd_locks[cu_fd])
704 1.1 fvdl cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
705 1.1 fvdl if (cu->cu_closeit)
706 1.1 fvdl (void) close(cu_fd);
707 1.1 fvdl XDR_DESTROY(&(cu->cu_outxdrs));
708 1.3 christos mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
709 1.1 fvdl if (cl->cl_netid && cl->cl_netid[0])
710 1.1 fvdl mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
711 1.1 fvdl if (cl->cl_tp && cl->cl_tp[0])
712 1.1 fvdl mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
713 1.3 christos mem_free(cl, sizeof (CLIENT));
714 1.1 fvdl mutex_unlock(&clnt_fd_lock);
715 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &mask, NULL);
716 1.1 fvdl cond_signal(&dg_cv[cu_fd]);
717 1.1 fvdl }
718 1.1 fvdl
719 1.1 fvdl static struct clnt_ops *
720 1.26 matt clnt_dg_ops(void)
721 1.1 fvdl {
722 1.1 fvdl static struct clnt_ops ops;
723 1.9 thorpej #ifdef _REENTRANT
724 1.1 fvdl extern mutex_t ops_lock;
725 1.1 fvdl sigset_t mask;
726 1.1 fvdl #endif
727 1.1 fvdl sigset_t newmask;
728 1.1 fvdl
729 1.1 fvdl /* VARIABLES PROTECTED BY ops_lock: ops */
730 1.1 fvdl
731 1.1 fvdl sigfillset(&newmask);
732 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
733 1.1 fvdl mutex_lock(&ops_lock);
734 1.1 fvdl if (ops.cl_call == NULL) {
735 1.1 fvdl ops.cl_call = clnt_dg_call;
736 1.1 fvdl ops.cl_abort = clnt_dg_abort;
737 1.1 fvdl ops.cl_geterr = clnt_dg_geterr;
738 1.1 fvdl ops.cl_freeres = clnt_dg_freeres;
739 1.1 fvdl ops.cl_destroy = clnt_dg_destroy;
740 1.1 fvdl ops.cl_control = clnt_dg_control;
741 1.1 fvdl }
742 1.1 fvdl mutex_unlock(&ops_lock);
743 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &mask, NULL);
744 1.1 fvdl return (&ops);
745 1.1 fvdl }
746 1.1 fvdl
747 1.1 fvdl /*
748 1.1 fvdl * Make sure that the time is not garbage. -1 value is allowed.
749 1.1 fvdl */
750 1.1 fvdl static bool_t
751 1.26 matt time_not_ok(struct timeval *t)
752 1.1 fvdl {
753 1.7 lukem
754 1.7 lukem _DIAGASSERT(t != NULL);
755 1.7 lukem
756 1.1 fvdl return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
757 1.1 fvdl t->tv_usec < -1 || t->tv_usec > 1000000);
758 1.1 fvdl }
759