174462Salfred/* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */ 274462Salfred 3261057Smav/*- 4261057Smav * Copyright (c) 2009, Sun Microsystems, Inc. 5261057Smav * All rights reserved. 6261057Smav * 7261057Smav * Redistribution and use in source and binary forms, with or without 8261057Smav * modification, are permitted provided that the following conditions are met: 9261057Smav * - Redistributions of source code must retain the above copyright notice, 10261057Smav * this list of conditions and the following disclaimer. 11261057Smav * - Redistributions in binary form must reproduce the above copyright notice, 12261057Smav * this list of conditions and the following disclaimer in the documentation 13261057Smav * and/or other materials provided with the distribution. 14261057Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its 15261057Smav * contributors may be used to endorse or promote products derived 16261057Smav * from this software without specific prior written permission. 1774462Salfred * 18261057Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19261057Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20261057Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21261057Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22261057Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23261057Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24261057Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25261057Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26261057Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27261057Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28261057Smav * POSSIBILITY OF SUCH DAMAGE. 2974462Salfred */ 3074462Salfred/* 3174462Salfred * Copyright (c) 1986-1991 by Sun Microsystems Inc. 3274462Salfred */ 3374462Salfred 34136581Sobrien#if defined(LIBC_SCCS) && !defined(lint) 35136581Sobrien#ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" 3674462Salfredstatic char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro"; 3774462Salfred#endif 3892990Sobrien#include <sys/cdefs.h> 3992990Sobrien__FBSDID("$FreeBSD$"); 4074462Salfred 4174462Salfred/* 4274462Salfred * Implements a connectionless client side RPC. 4374462Salfred */ 4474462Salfred 4575094Siedowse#include "namespace.h" 4674462Salfred#include "reentrant.h" 4774462Salfred#include <sys/types.h> 48105189Siedowse#include <sys/event.h> 4974462Salfred#include <sys/time.h> 5074462Salfred#include <sys/socket.h> 5174462Salfred#include <sys/ioctl.h> 5290868Smike#include <arpa/inet.h> 5374462Salfred#include <rpc/rpc.h> 54181344Sdfr#include <rpc/rpcsec_gss.h> 5574462Salfred#include <errno.h> 5674462Salfred#include <stdlib.h> 5774462Salfred#include <string.h> 5874462Salfred#include <signal.h> 5974462Salfred#include <unistd.h> 6074462Salfred#include <err.h> 6174462Salfred#include "un-namespace.h" 6274462Salfred#include "rpc_com.h" 63156090Sdeischen#include "mt_misc.h" 6474462Salfred 6574462Salfred 66167199Ssimon#ifdef _FREEFALL_CONFIG 67167199Ssimon/* 68167199Ssimon * Disable RPC exponential back-off for FreeBSD.org systems. 69167199Ssimon */ 70167199Ssimon#define RPC_MAX_BACKOFF 1 /* second */ 71167199Ssimon#else 7274462Salfred#define RPC_MAX_BACKOFF 30 /* seconds */ 73167199Ssimon#endif 7474462Salfred 7574462Salfred 7692905Sobrienstatic struct clnt_ops *clnt_dg_ops(void); 7792905Sobrienstatic bool_t time_not_ok(struct timeval *); 7895658Sdesstatic enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *, 7995658Sdes xdrproc_t, void *, struct timeval); 8092905Sobrienstatic void clnt_dg_geterr(CLIENT *, struct rpc_err *); 8195658Sdesstatic bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *); 8292905Sobrienstatic void clnt_dg_abort(CLIENT *); 8399996Salfredstatic bool_t clnt_dg_control(CLIENT *, u_int, void *); 8492905Sobrienstatic void clnt_dg_destroy(CLIENT *); 8574462Salfred 8674462Salfred 8774462Salfred 8874462Salfred 8974462Salfred/* 9074462Salfred * This machinery implements per-fd locks for MT-safety. It is not 9174462Salfred * sufficient to do per-CLIENT handle locks for MT-safety because a 9274462Salfred * user may create more than one CLIENT handle with the same fd behind 9374462Salfred * it. Therfore, we allocate an array of flags (dg_fd_locks), protected 9474462Salfred * by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables 9574462Salfred * similarly protected. Dg_fd_lock[fd] == 1 => a call is activte on some 9674462Salfred * CLIENT handle created for that fd. 9774462Salfred * The current implementation holds locks across the entire RPC and reply, 9874462Salfred * including retransmissions. Yes, this is silly, and as soon as this 9974462Salfred * code is proven to work, this should be the first thing fixed. One step 10074462Salfred * at a time. 10174462Salfred */ 10274462Salfredstatic int *dg_fd_locks; 10374462Salfredstatic cond_t *dg_cv; 10474462Salfred#define release_fd_lock(fd, mask) { \ 10574462Salfred mutex_lock(&clnt_fd_lock); \ 10675144Siedowse dg_fd_locks[fd] = 0; \ 10774462Salfred mutex_unlock(&clnt_fd_lock); \ 108105189Siedowse thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \ 10974462Salfred cond_signal(&dg_cv[fd]); \ 11074462Salfred} 11174462Salfred 11274462Salfredstatic const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory"; 11374462Salfred 11474462Salfred/* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */ 11574462Salfred 116181344Sdfr#define MCALL_MSG_SIZE 24 117181344Sdfr 11874462Salfred/* 11974462Salfred * Private data kept per client handle 12074462Salfred */ 12174462Salfredstruct cu_data { 12274462Salfred int cu_fd; /* connections fd */ 12374462Salfred bool_t cu_closeit; /* opened by library */ 12474462Salfred struct sockaddr_storage cu_raddr; /* remote address */ 12574462Salfred int cu_rlen; 12674462Salfred struct timeval cu_wait; /* retransmit interval */ 12774462Salfred struct timeval cu_total; /* total time for the call */ 12874462Salfred struct rpc_err cu_error; 12974462Salfred XDR cu_outxdrs; 13074462Salfred u_int cu_xdrpos; 13174462Salfred u_int cu_sendsz; /* send size */ 132181344Sdfr char cu_outhdr[MCALL_MSG_SIZE]; 13374462Salfred char *cu_outbuf; 13474462Salfred u_int cu_recvsz; /* recv size */ 13574879Swpaul int cu_async; 13678678Siedowse int cu_connect; /* Use connect(). */ 13778678Siedowse int cu_connected; /* Have done connect(). */ 138105189Siedowse struct kevent cu_kin; 139105189Siedowse int cu_kq; 14074462Salfred char cu_inbuf[1]; 14174462Salfred}; 14274462Salfred 14374462Salfred/* 14474462Salfred * Connection less client creation returns with client handle parameters. 14574462Salfred * Default options are set, which the user can change using clnt_control(). 14674462Salfred * fd should be open and bound. 14774462Salfred * NB: The rpch->cl_auth is initialized to null authentication. 14874462Salfred * Caller may wish to set this something more useful. 14974462Salfred * 15074462Salfred * sendsz and recvsz are the maximum allowable packet sizes that can be 15174462Salfred * sent and received. Normally they are the same, but they can be 15274462Salfred * changed to improve the program efficiency and buffer allocation. 15374462Salfred * If they are 0, use the transport default. 15474462Salfred * 15574462Salfred * If svcaddr is NULL, returns NULL. 15674462Salfred */ 15774462SalfredCLIENT * 15874462Salfredclnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz) 15974462Salfred int fd; /* open file descriptor */ 16074462Salfred const struct netbuf *svcaddr; /* servers address */ 16174462Salfred rpcprog_t program; /* program number */ 16274462Salfred rpcvers_t version; /* version number */ 16374462Salfred u_int sendsz; /* buffer recv size */ 16474462Salfred u_int recvsz; /* buffer send size */ 16574462Salfred{ 16674462Salfred CLIENT *cl = NULL; /* client handle */ 16774462Salfred struct cu_data *cu = NULL; /* private data */ 16874462Salfred struct timeval now; 16974462Salfred struct rpc_msg call_msg; 17074462Salfred sigset_t mask; 17174462Salfred sigset_t newmask; 17274462Salfred struct __rpc_sockinfo si; 17374462Salfred int one = 1; 17474462Salfred 17574462Salfred sigfillset(&newmask); 17674462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 17774462Salfred mutex_lock(&clnt_fd_lock); 17874462Salfred if (dg_fd_locks == (int *) NULL) { 17974462Salfred int cv_allocsz; 18074462Salfred size_t fd_allocsz; 18174462Salfred int dtbsize = __rpc_dtbsize(); 18274462Salfred 18374462Salfred fd_allocsz = dtbsize * sizeof (int); 18474462Salfred dg_fd_locks = (int *) mem_alloc(fd_allocsz); 18574462Salfred if (dg_fd_locks == (int *) NULL) { 18674462Salfred mutex_unlock(&clnt_fd_lock); 18774462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 18874462Salfred goto err1; 18974462Salfred } else 19074462Salfred memset(dg_fd_locks, '\0', fd_allocsz); 19174462Salfred 19274462Salfred cv_allocsz = dtbsize * sizeof (cond_t); 19374462Salfred dg_cv = (cond_t *) mem_alloc(cv_allocsz); 19474462Salfred if (dg_cv == (cond_t *) NULL) { 19574462Salfred mem_free(dg_fd_locks, fd_allocsz); 19674462Salfred dg_fd_locks = (int *) NULL; 19774462Salfred mutex_unlock(&clnt_fd_lock); 19874462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 19974462Salfred goto err1; 20074462Salfred } else { 20174462Salfred int i; 20274462Salfred 20374462Salfred for (i = 0; i < dtbsize; i++) 20474462Salfred cond_init(&dg_cv[i], 0, (void *) 0); 20574462Salfred } 20674462Salfred } 20774462Salfred 20874462Salfred mutex_unlock(&clnt_fd_lock); 20974462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 21074462Salfred 21174462Salfred if (svcaddr == NULL) { 21274462Salfred rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 21374462Salfred return (NULL); 21474462Salfred } 21574462Salfred 21674462Salfred if (!__rpc_fd2sockinfo(fd, &si)) { 21774462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; 21874462Salfred rpc_createerr.cf_error.re_errno = 0; 21974462Salfred return (NULL); 22074462Salfred } 22174462Salfred /* 22274462Salfred * Find the receive and the send size 22374462Salfred */ 22474462Salfred sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 22574462Salfred recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 22674462Salfred if ((sendsz == 0) || (recvsz == 0)) { 22774462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */ 22874462Salfred rpc_createerr.cf_error.re_errno = 0; 22974462Salfred return (NULL); 23074462Salfred } 23174462Salfred 23274462Salfred if ((cl = mem_alloc(sizeof (CLIENT))) == NULL) 23374462Salfred goto err1; 23474462Salfred /* 23574462Salfred * Should be multiple of 4 for XDR. 23674462Salfred */ 23774462Salfred sendsz = ((sendsz + 3) / 4) * 4; 23874462Salfred recvsz = ((recvsz + 3) / 4) * 4; 23974462Salfred cu = mem_alloc(sizeof (*cu) + sendsz + recvsz); 24074462Salfred if (cu == NULL) 24174462Salfred goto err1; 24274462Salfred (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len); 24374462Salfred cu->cu_rlen = svcaddr->len; 24474462Salfred cu->cu_outbuf = &cu->cu_inbuf[recvsz]; 24574462Salfred /* Other values can also be set through clnt_control() */ 24674462Salfred cu->cu_wait.tv_sec = 15; /* heuristically chosen */ 24774462Salfred cu->cu_wait.tv_usec = 0; 24874462Salfred cu->cu_total.tv_sec = -1; 24974462Salfred cu->cu_total.tv_usec = -1; 25074462Salfred cu->cu_sendsz = sendsz; 25174462Salfred cu->cu_recvsz = recvsz; 25274879Swpaul cu->cu_async = FALSE; 25378678Siedowse cu->cu_connect = FALSE; 25478678Siedowse cu->cu_connected = FALSE; 25574462Salfred (void) gettimeofday(&now, NULL); 25674462Salfred call_msg.rm_xid = __RPC_GETXID(&now); 25774462Salfred call_msg.rm_call.cb_prog = program; 25874462Salfred call_msg.rm_call.cb_vers = version; 259181344Sdfr xdrmem_create(&(cu->cu_outxdrs), cu->cu_outhdr, MCALL_MSG_SIZE, 260181344Sdfr XDR_ENCODE); 261181344Sdfr if (! xdr_callhdr(&cu->cu_outxdrs, &call_msg)) { 26274462Salfred rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */ 26374462Salfred rpc_createerr.cf_error.re_errno = 0; 26474462Salfred goto err2; 26574462Salfred } 26674462Salfred cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs)); 267181344Sdfr XDR_DESTROY(&cu->cu_outxdrs); 268181344Sdfr xdrmem_create(&cu->cu_outxdrs, cu->cu_outbuf, sendsz, XDR_ENCODE); 26974462Salfred 27074462Salfred /* XXX fvdl - do we still want this? */ 27174462Salfred#if 0 27274462Salfred (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf); 27374462Salfred#endif 27474462Salfred _ioctl(fd, FIONBIO, (char *)(void *)&one); 27574462Salfred 27674462Salfred /* 27774462Salfred * By default, closeit is always FALSE. It is users responsibility 27874462Salfred * to do a close on it, else the user may use clnt_control 27974462Salfred * to let clnt_destroy do it for him/her. 28074462Salfred */ 28174462Salfred cu->cu_closeit = FALSE; 28274462Salfred cu->cu_fd = fd; 28374462Salfred cl->cl_ops = clnt_dg_ops(); 28474462Salfred cl->cl_private = (caddr_t)(void *)cu; 28574462Salfred cl->cl_auth = authnone_create(); 28674462Salfred cl->cl_tp = NULL; 28774462Salfred cl->cl_netid = NULL; 288105189Siedowse cu->cu_kq = -1; 289105189Siedowse EV_SET(&cu->cu_kin, cu->cu_fd, EVFILT_READ, EV_ADD, 0, 0, 0); 29074462Salfred return (cl); 29174462Salfrederr1: 29274462Salfred warnx(mem_err_clnt_dg); 29374462Salfred rpc_createerr.cf_stat = RPC_SYSTEMERROR; 29474462Salfred rpc_createerr.cf_error.re_errno = errno; 29574462Salfrederr2: 29674462Salfred if (cl) { 29774462Salfred mem_free(cl, sizeof (CLIENT)); 29874462Salfred if (cu) 29974462Salfred mem_free(cu, sizeof (*cu) + sendsz + recvsz); 30074462Salfred } 30174462Salfred return (NULL); 30274462Salfred} 30374462Salfred 30474462Salfredstatic enum clnt_stat 30574462Salfredclnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout) 30674462Salfred CLIENT *cl; /* client handle */ 30774462Salfred rpcproc_t proc; /* procedure number */ 30874462Salfred xdrproc_t xargs; /* xdr routine for args */ 30995658Sdes void *argsp; /* pointer to args */ 31074462Salfred xdrproc_t xresults; /* xdr routine for results */ 31195658Sdes void *resultsp; /* pointer to results */ 31274462Salfred struct timeval utimeout; /* seconds to wait before giving up */ 31374462Salfred{ 31474462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 31574462Salfred XDR *xdrs; 316105189Siedowse size_t outlen = 0; 31774462Salfred struct rpc_msg reply_msg; 31874462Salfred XDR reply_xdrs; 31974462Salfred bool_t ok; 32074462Salfred int nrefreshes = 2; /* number of times to refresh cred */ 321181344Sdfr int nretries = 0; /* number of times we retransmitted */ 32274462Salfred struct timeval timeout; 32374462Salfred struct timeval retransmit_time; 324105189Siedowse struct timeval next_sendtime, starttime, time_waited, tv; 325105189Siedowse struct timespec ts; 326105189Siedowse struct kevent kv; 32778678Siedowse struct sockaddr *sa; 32874462Salfred sigset_t mask; 32974462Salfred sigset_t newmask; 33078678Siedowse socklen_t inlen, salen; 33174462Salfred ssize_t recvlen = 0; 332105189Siedowse int kin_len, n, rpc_lock_value; 33374879Swpaul u_int32_t xid; 33474462Salfred 33590271Salfred outlen = 0; 33674462Salfred sigfillset(&newmask); 33774462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 33874462Salfred mutex_lock(&clnt_fd_lock); 33974462Salfred while (dg_fd_locks[cu->cu_fd]) 34074462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 34174462Salfred if (__isthreaded) 34274462Salfred rpc_lock_value = 1; 34374462Salfred else 34474462Salfred rpc_lock_value = 0; 34574462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 34674462Salfred mutex_unlock(&clnt_fd_lock); 34774462Salfred if (cu->cu_total.tv_usec == -1) { 34874462Salfred timeout = utimeout; /* use supplied timeout */ 34974462Salfred } else { 35074462Salfred timeout = cu->cu_total; /* use default timeout */ 35174462Salfred } 35274462Salfred 35378678Siedowse if (cu->cu_connect && !cu->cu_connected) { 35478678Siedowse if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr, 35578678Siedowse cu->cu_rlen) < 0) { 35678678Siedowse cu->cu_error.re_errno = errno; 357105189Siedowse cu->cu_error.re_status = RPC_CANTSEND; 358105189Siedowse goto out; 35978678Siedowse } 36078678Siedowse cu->cu_connected = 1; 36178678Siedowse } 36278678Siedowse if (cu->cu_connected) { 36378678Siedowse sa = NULL; 36478678Siedowse salen = 0; 36578678Siedowse } else { 36678678Siedowse sa = (struct sockaddr *)&cu->cu_raddr; 36778678Siedowse salen = cu->cu_rlen; 36878678Siedowse } 36974462Salfred time_waited.tv_sec = 0; 37074462Salfred time_waited.tv_usec = 0; 371105189Siedowse retransmit_time = next_sendtime = cu->cu_wait; 372105189Siedowse gettimeofday(&starttime, NULL); 37374462Salfred 374105189Siedowse /* Clean up in case the last call ended in a longjmp(3) call. */ 375105189Siedowse if (cu->cu_kq >= 0) 376105189Siedowse _close(cu->cu_kq); 377105189Siedowse if ((cu->cu_kq = kqueue()) < 0) { 378105189Siedowse cu->cu_error.re_errno = errno; 379105189Siedowse cu->cu_error.re_status = RPC_CANTSEND; 380105189Siedowse goto out; 381105189Siedowse } 382105189Siedowse kin_len = 1; 383105189Siedowse 38474462Salfredcall_again: 385183039Sdfr if (cu->cu_async == TRUE && xargs == NULL) 386183039Sdfr goto get_reply; 38774462Salfred /* 38874462Salfred * the transaction is the first thing in the out buffer 38974879Swpaul * XXX Yes, and it's in network byte order, so we should to 39074879Swpaul * be careful when we increment it, shouldn't we. 39174462Salfred */ 392181344Sdfr xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr)); 39374879Swpaul xid++; 394181344Sdfr *(u_int32_t *)(void *)(cu->cu_outhdr) = htonl(xid); 395181344Sdfrcall_again_same_xid: 396181344Sdfr xdrs = &(cu->cu_outxdrs); 397181344Sdfr xdrs->x_op = XDR_ENCODE; 398181344Sdfr XDR_SETPOS(xdrs, 0); 39974879Swpaul 400181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { 401181344Sdfr if ((! XDR_PUTBYTES(xdrs, cu->cu_outhdr, cu->cu_xdrpos)) || 402181344Sdfr (! XDR_PUTINT32(xdrs, &proc)) || 403181344Sdfr (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || 404181344Sdfr (! (*xargs)(xdrs, argsp))) { 405181344Sdfr cu->cu_error.re_status = RPC_CANTENCODEARGS; 406181344Sdfr goto out; 407181344Sdfr } 408181344Sdfr } else { 409181344Sdfr *(uint32_t *) &cu->cu_outhdr[cu->cu_xdrpos] = htonl(proc); 410181344Sdfr if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outhdr, 411181344Sdfr cu->cu_xdrpos + sizeof(uint32_t), 412181344Sdfr xdrs, xargs, argsp)) { 413181344Sdfr cu->cu_error.re_status = RPC_CANTENCODEARGS; 414181344Sdfr goto out; 415181344Sdfr } 41674462Salfred } 41774462Salfred outlen = (size_t)XDR_GETPOS(xdrs); 41874462Salfred 41974462Salfredsend_again: 42078678Siedowse if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) { 42174462Salfred cu->cu_error.re_errno = errno; 422105189Siedowse cu->cu_error.re_status = RPC_CANTSEND; 423105189Siedowse goto out; 42474462Salfred } 42574462Salfred 42674462Salfred /* 42774462Salfred * Hack to provide rpc-based message passing 42874462Salfred */ 42974462Salfred if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { 430105189Siedowse cu->cu_error.re_status = RPC_TIMEDOUT; 431105189Siedowse goto out; 43274462Salfred } 43374879Swpaul 43474879Swpaulget_reply: 43574879Swpaul 43674462Salfred /* 43774462Salfred * sub-optimal code appears here because we have 43874462Salfred * some clock time to spare while the packets are in flight. 43974462Salfred * (We assume that this is actually only executed once.) 44074462Salfred */ 44174462Salfred reply_msg.acpted_rply.ar_verf = _null_auth; 442181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { 443181344Sdfr reply_msg.acpted_rply.ar_results.where = resultsp; 444181344Sdfr reply_msg.acpted_rply.ar_results.proc = xresults; 445181344Sdfr } else { 446181344Sdfr reply_msg.acpted_rply.ar_results.where = NULL; 447181344Sdfr reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 448181344Sdfr } 44974462Salfred 45074462Salfred for (;;) { 451105189Siedowse /* Decide how long to wait. */ 452105189Siedowse if (timercmp(&next_sendtime, &timeout, <)) 453105189Siedowse timersub(&next_sendtime, &time_waited, &tv); 454105189Siedowse else 455105189Siedowse timersub(&timeout, &time_waited, &tv); 456105189Siedowse if (tv.tv_sec < 0 || tv.tv_usec < 0) 457105189Siedowse tv.tv_sec = tv.tv_usec = 0; 458105189Siedowse TIMEVAL_TO_TIMESPEC(&tv, &ts); 45974462Salfred 460105189Siedowse n = _kevent(cu->cu_kq, &cu->cu_kin, kin_len, &kv, 1, &ts); 461105189Siedowse /* We don't need to register the event again. */ 462105189Siedowse kin_len = 0; 46374462Salfred 464105189Siedowse if (n == 1) { 465105189Siedowse if (kv.flags & EV_ERROR) { 466105189Siedowse cu->cu_error.re_errno = kv.data; 467105189Siedowse cu->cu_error.re_status = RPC_CANTRECV; 468105189Siedowse goto out; 469105189Siedowse } 470105189Siedowse /* We have some data now */ 471105189Siedowse do { 472105189Siedowse recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf, 473105189Siedowse cu->cu_recvsz, 0, NULL, NULL); 474105189Siedowse } while (recvlen < 0 && errno == EINTR); 475105189Siedowse if (recvlen < 0 && errno != EWOULDBLOCK) { 47674462Salfred cu->cu_error.re_errno = errno; 477105189Siedowse cu->cu_error.re_status = RPC_CANTRECV; 478105189Siedowse goto out; 47974462Salfred } 480105189Siedowse if (recvlen >= sizeof(u_int32_t) && 481105189Siedowse (cu->cu_async == TRUE || 482105189Siedowse *((u_int32_t *)(void *)(cu->cu_inbuf)) == 483105189Siedowse *((u_int32_t *)(void *)(cu->cu_outbuf)))) { 484105189Siedowse /* We now assume we have the proper reply. */ 485105189Siedowse break; 48674462Salfred } 487105189Siedowse } 488105189Siedowse if (n == -1 && errno != EINTR) { 489105189Siedowse cu->cu_error.re_errno = errno; 49074462Salfred cu->cu_error.re_status = RPC_CANTRECV; 491105189Siedowse goto out; 49274462Salfred } 493105189Siedowse gettimeofday(&tv, NULL); 494105189Siedowse timersub(&tv, &starttime, &time_waited); 49574462Salfred 496105189Siedowse /* Check for timeout. */ 497105189Siedowse if (timercmp(&time_waited, &timeout, >)) { 498105189Siedowse cu->cu_error.re_status = RPC_TIMEDOUT; 499105189Siedowse goto out; 50074462Salfred } 501105189Siedowse 502105189Siedowse /* Retransmit if necessary. */ 503105189Siedowse if (timercmp(&time_waited, &next_sendtime, >)) { 504105189Siedowse /* update retransmit_time */ 505105189Siedowse if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) 506105189Siedowse timeradd(&retransmit_time, &retransmit_time, 507105189Siedowse &retransmit_time); 508105189Siedowse timeradd(&next_sendtime, &retransmit_time, 509105189Siedowse &next_sendtime); 510181344Sdfr nretries++; 511181344Sdfr 512181344Sdfr /* 513181344Sdfr * When retransmitting a RPCSEC_GSS message, 514181344Sdfr * we must use a new sequence number (handled 515181344Sdfr * by __rpc_gss_wrap above). 516181344Sdfr */ 517181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) 518181344Sdfr goto send_again; 519181344Sdfr else 520181344Sdfr goto call_again_same_xid; 521105189Siedowse } 52274462Salfred } 52374462Salfred inlen = (socklen_t)recvlen; 52474462Salfred 52574462Salfred /* 52674462Salfred * now decode and validate the response 52774462Salfred */ 52874462Salfred 529121654Smbr xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE); 53074462Salfred ok = xdr_replymsg(&reply_xdrs, &reply_msg); 53174462Salfred /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ 53274462Salfred if (ok) { 53374462Salfred if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 53474462Salfred (reply_msg.acpted_rply.ar_stat == SUCCESS)) 53574462Salfred cu->cu_error.re_status = RPC_SUCCESS; 53674462Salfred else 53774462Salfred _seterr_reply(&reply_msg, &(cu->cu_error)); 53874462Salfred 53974462Salfred if (cu->cu_error.re_status == RPC_SUCCESS) { 54074462Salfred if (! AUTH_VALIDATE(cl->cl_auth, 54174462Salfred &reply_msg.acpted_rply.ar_verf)) { 542181344Sdfr if (nretries && 543181344Sdfr cl->cl_auth->ah_cred.oa_flavor 544181344Sdfr == RPCSEC_GSS) 545181344Sdfr /* 546181344Sdfr * If we retransmitted, its 547181344Sdfr * possible that we will 548181344Sdfr * receive a reply for one of 549181344Sdfr * the earlier transmissions 550181344Sdfr * (which will use an older 551181344Sdfr * RPCSEC_GSS sequence 552181344Sdfr * number). In this case, just 553181344Sdfr * go back and listen for a 554181344Sdfr * new reply. We could keep a 555181344Sdfr * record of all the seq 556181344Sdfr * numbers we have transmitted 557181344Sdfr * so far so that we could 558181344Sdfr * accept a reply for any of 559181344Sdfr * them here. 560181344Sdfr */ 561181344Sdfr goto get_reply; 56274462Salfred cu->cu_error.re_status = RPC_AUTHERROR; 56374462Salfred cu->cu_error.re_why = AUTH_INVALIDRESP; 564181344Sdfr } else { 565181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor 566181344Sdfr == RPCSEC_GSS) { 567181344Sdfr if (!__rpc_gss_unwrap(cl->cl_auth, 568181344Sdfr &reply_xdrs, xresults, 569181344Sdfr resultsp)) 570181344Sdfr cu->cu_error.re_status = 571181344Sdfr RPC_CANTDECODERES; 572181344Sdfr } 57374462Salfred } 57474462Salfred if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { 57574462Salfred xdrs->x_op = XDR_FREE; 57674462Salfred (void) xdr_opaque_auth(xdrs, 57774462Salfred &(reply_msg.acpted_rply.ar_verf)); 57874462Salfred } 57974462Salfred } /* end successful completion */ 58074462Salfred /* 58174462Salfred * If unsuccesful AND error is an authentication error 58274462Salfred * then refresh credentials and try again, else break 58374462Salfred */ 58474462Salfred else if (cu->cu_error.re_status == RPC_AUTHERROR) 58574462Salfred /* maybe our credentials need to be refreshed ... */ 58674462Salfred if (nrefreshes > 0 && 58774462Salfred AUTH_REFRESH(cl->cl_auth, &reply_msg)) { 58874462Salfred nrefreshes--; 58974462Salfred goto call_again; 59074462Salfred } 59174462Salfred /* end of unsuccessful completion */ 59274462Salfred } /* end of valid reply message */ 59374462Salfred else { 59474462Salfred cu->cu_error.re_status = RPC_CANTDECODERES; 59574462Salfred 59674462Salfred } 597105189Siedowseout: 598105189Siedowse if (cu->cu_kq >= 0) 599105189Siedowse _close(cu->cu_kq); 600105189Siedowse cu->cu_kq = -1; 60174462Salfred release_fd_lock(cu->cu_fd, mask); 60274462Salfred return (cu->cu_error.re_status); 60374462Salfred} 60474462Salfred 60574462Salfredstatic void 60674462Salfredclnt_dg_geterr(cl, errp) 60774462Salfred CLIENT *cl; 60874462Salfred struct rpc_err *errp; 60974462Salfred{ 61074462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 61174462Salfred 61274462Salfred *errp = cu->cu_error; 61374462Salfred} 61474462Salfred 61574462Salfredstatic bool_t 61674462Salfredclnt_dg_freeres(cl, xdr_res, res_ptr) 61774462Salfred CLIENT *cl; 61874462Salfred xdrproc_t xdr_res; 61995658Sdes void *res_ptr; 62074462Salfred{ 62174462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 62274462Salfred XDR *xdrs = &(cu->cu_outxdrs); 62374462Salfred bool_t dummy; 62474462Salfred sigset_t mask; 62574462Salfred sigset_t newmask; 62674462Salfred 62774462Salfred sigfillset(&newmask); 62874462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 62974462Salfred mutex_lock(&clnt_fd_lock); 63074462Salfred while (dg_fd_locks[cu->cu_fd]) 63174462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 63274462Salfred xdrs->x_op = XDR_FREE; 63374462Salfred dummy = (*xdr_res)(xdrs, res_ptr); 63474462Salfred mutex_unlock(&clnt_fd_lock); 63574462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 63674462Salfred cond_signal(&dg_cv[cu->cu_fd]); 63774462Salfred return (dummy); 63874462Salfred} 63974462Salfred 64074462Salfred/*ARGSUSED*/ 64174462Salfredstatic void 64274462Salfredclnt_dg_abort(h) 64374462Salfred CLIENT *h; 64474462Salfred{ 64574462Salfred} 64674462Salfred 64774462Salfredstatic bool_t 64874462Salfredclnt_dg_control(cl, request, info) 64974462Salfred CLIENT *cl; 65074462Salfred u_int request; 65199996Salfred void *info; 65274462Salfred{ 65374462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 65474462Salfred struct netbuf *addr; 65574462Salfred sigset_t mask; 65674462Salfred sigset_t newmask; 65774462Salfred int rpc_lock_value; 65874462Salfred 65974462Salfred sigfillset(&newmask); 66074462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 66174462Salfred mutex_lock(&clnt_fd_lock); 66274462Salfred while (dg_fd_locks[cu->cu_fd]) 66374462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 66474462Salfred if (__isthreaded) 66574462Salfred rpc_lock_value = 1; 66674462Salfred else 66774462Salfred rpc_lock_value = 0; 66874462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 66974462Salfred mutex_unlock(&clnt_fd_lock); 67074462Salfred switch (request) { 67174462Salfred case CLSET_FD_CLOSE: 67274462Salfred cu->cu_closeit = TRUE; 67374462Salfred release_fd_lock(cu->cu_fd, mask); 67474462Salfred return (TRUE); 67574462Salfred case CLSET_FD_NCLOSE: 67674462Salfred cu->cu_closeit = FALSE; 67774462Salfred release_fd_lock(cu->cu_fd, mask); 67874462Salfred return (TRUE); 67974462Salfred } 68074462Salfred 68174462Salfred /* for other requests which use info */ 68274462Salfred if (info == NULL) { 68374462Salfred release_fd_lock(cu->cu_fd, mask); 68474462Salfred return (FALSE); 68574462Salfred } 68674462Salfred switch (request) { 68774462Salfred case CLSET_TIMEOUT: 68899996Salfred if (time_not_ok((struct timeval *)info)) { 68974462Salfred release_fd_lock(cu->cu_fd, mask); 69074462Salfred return (FALSE); 69174462Salfred } 69299996Salfred cu->cu_total = *(struct timeval *)info; 69374462Salfred break; 69474462Salfred case CLGET_TIMEOUT: 69599996Salfred *(struct timeval *)info = cu->cu_total; 69674462Salfred break; 69774462Salfred case CLGET_SERVER_ADDR: /* Give him the fd address */ 69874462Salfred /* Now obsolete. Only for backward compatibility */ 69974462Salfred (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen); 70074462Salfred break; 70174462Salfred case CLSET_RETRY_TIMEOUT: 70299996Salfred if (time_not_ok((struct timeval *)info)) { 70374462Salfred release_fd_lock(cu->cu_fd, mask); 70474462Salfred return (FALSE); 70574462Salfred } 70699996Salfred cu->cu_wait = *(struct timeval *)info; 70774462Salfred break; 70874462Salfred case CLGET_RETRY_TIMEOUT: 70999996Salfred *(struct timeval *)info = cu->cu_wait; 71074462Salfred break; 71174462Salfred case CLGET_FD: 71299996Salfred *(int *)info = cu->cu_fd; 71374462Salfred break; 71474462Salfred case CLGET_SVC_ADDR: 71599996Salfred addr = (struct netbuf *)info; 71674462Salfred addr->buf = &cu->cu_raddr; 71774462Salfred addr->len = cu->cu_rlen; 71874462Salfred addr->maxlen = sizeof cu->cu_raddr; 71974462Salfred break; 72074462Salfred case CLSET_SVC_ADDR: /* set to new address */ 72199996Salfred addr = (struct netbuf *)info; 72275097Siedowse if (addr->len < sizeof cu->cu_raddr) { 72375097Siedowse release_fd_lock(cu->cu_fd, mask); 72474462Salfred return (FALSE); 72575097Siedowse } 72674462Salfred (void) memcpy(&cu->cu_raddr, addr->buf, addr->len); 72774462Salfred cu->cu_rlen = addr->len; 72874462Salfred break; 72974462Salfred case CLGET_XID: 73074462Salfred /* 73174462Salfred * use the knowledge that xid is the 73274462Salfred * first element in the call structure *. 73374462Salfred * This will get the xid of the PREVIOUS call 73474462Salfred */ 73599996Salfred *(u_int32_t *)info = 736181344Sdfr ntohl(*(u_int32_t *)(void *)cu->cu_outhdr); 73774462Salfred break; 73874462Salfred 73974462Salfred case CLSET_XID: 74074462Salfred /* This will set the xid of the NEXT call */ 741181344Sdfr *(u_int32_t *)(void *)cu->cu_outhdr = 74299996Salfred htonl(*(u_int32_t *)info - 1); 74374462Salfred /* decrement by 1 as clnt_dg_call() increments once */ 74474462Salfred break; 74574462Salfred 74674462Salfred case CLGET_VERS: 74774462Salfred /* 74874462Salfred * This RELIES on the information that, in the call body, 74974462Salfred * the version number field is the fifth field from the 75074462Salfred * begining of the RPC header. MUST be changed if the 75174462Salfred * call_struct is changed 75274462Salfred */ 75399996Salfred *(u_int32_t *)info = 754181344Sdfr ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr + 75574462Salfred 4 * BYTES_PER_XDR_UNIT)); 75674462Salfred break; 75774462Salfred 75874462Salfred case CLSET_VERS: 759181344Sdfr *(u_int32_t *)(void *)(cu->cu_outhdr + 4 * BYTES_PER_XDR_UNIT) 76099996Salfred = htonl(*(u_int32_t *)info); 76174462Salfred break; 76274462Salfred 76374462Salfred case CLGET_PROG: 76474462Salfred /* 76574462Salfred * This RELIES on the information that, in the call body, 76674462Salfred * the program number field is the fourth field from the 76774462Salfred * begining of the RPC header. MUST be changed if the 76874462Salfred * call_struct is changed 76974462Salfred */ 77099996Salfred *(u_int32_t *)info = 771181344Sdfr ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr + 77274462Salfred 3 * BYTES_PER_XDR_UNIT)); 77374462Salfred break; 77474462Salfred 77574462Salfred case CLSET_PROG: 776181344Sdfr *(u_int32_t *)(void *)(cu->cu_outhdr + 3 * BYTES_PER_XDR_UNIT) 77799996Salfred = htonl(*(u_int32_t *)info); 77874462Salfred break; 77974879Swpaul case CLSET_ASYNC: 78099996Salfred cu->cu_async = *(int *)info; 78174879Swpaul break; 78278678Siedowse case CLSET_CONNECT: 78399996Salfred cu->cu_connect = *(int *)info; 78478678Siedowse break; 78574462Salfred default: 78674462Salfred release_fd_lock(cu->cu_fd, mask); 78774462Salfred return (FALSE); 78874462Salfred } 78974462Salfred release_fd_lock(cu->cu_fd, mask); 79074462Salfred return (TRUE); 79174462Salfred} 79274462Salfred 79374462Salfredstatic void 79474462Salfredclnt_dg_destroy(cl) 79574462Salfred CLIENT *cl; 79674462Salfred{ 79774462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 79874462Salfred int cu_fd = cu->cu_fd; 79974462Salfred sigset_t mask; 80074462Salfred sigset_t newmask; 80174462Salfred 80274462Salfred sigfillset(&newmask); 80374462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 80474462Salfred mutex_lock(&clnt_fd_lock); 80574462Salfred while (dg_fd_locks[cu_fd]) 80674462Salfred cond_wait(&dg_cv[cu_fd], &clnt_fd_lock); 80774462Salfred if (cu->cu_closeit) 80874462Salfred (void)_close(cu_fd); 809105189Siedowse if (cu->cu_kq >= 0) 810105189Siedowse _close(cu->cu_kq); 81174462Salfred XDR_DESTROY(&(cu->cu_outxdrs)); 81274462Salfred mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz)); 81374462Salfred if (cl->cl_netid && cl->cl_netid[0]) 81474462Salfred mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 81574462Salfred if (cl->cl_tp && cl->cl_tp[0]) 81674462Salfred mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 81774462Salfred mem_free(cl, sizeof (CLIENT)); 81874462Salfred mutex_unlock(&clnt_fd_lock); 81974462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 82074462Salfred cond_signal(&dg_cv[cu_fd]); 82174462Salfred} 82274462Salfred 82374462Salfredstatic struct clnt_ops * 82474462Salfredclnt_dg_ops() 82574462Salfred{ 82674462Salfred static struct clnt_ops ops; 82774462Salfred sigset_t mask; 82874462Salfred sigset_t newmask; 82974462Salfred 83074462Salfred/* VARIABLES PROTECTED BY ops_lock: ops */ 83174462Salfred 83274462Salfred sigfillset(&newmask); 83374462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 83474462Salfred mutex_lock(&ops_lock); 83574462Salfred if (ops.cl_call == NULL) { 83674462Salfred ops.cl_call = clnt_dg_call; 83774462Salfred ops.cl_abort = clnt_dg_abort; 83874462Salfred ops.cl_geterr = clnt_dg_geterr; 83974462Salfred ops.cl_freeres = clnt_dg_freeres; 84074462Salfred ops.cl_destroy = clnt_dg_destroy; 84174462Salfred ops.cl_control = clnt_dg_control; 84274462Salfred } 84374462Salfred mutex_unlock(&ops_lock); 84474462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 84574462Salfred return (&ops); 84674462Salfred} 84774462Salfred 84874462Salfred/* 84974462Salfred * Make sure that the time is not garbage. -1 value is allowed. 85074462Salfred */ 85174462Salfredstatic bool_t 85274462Salfredtime_not_ok(t) 85374462Salfred struct timeval *t; 85474462Salfred{ 85574462Salfred return (t->tv_sec < -1 || t->tv_sec > 100000000 || 85674462Salfred t->tv_usec < -1 || t->tv_usec > 1000000); 85774462Salfred} 85874462Salfred 859