174462Salfred/* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */ 274462Salfred 3258578Shrs/*- 4258578Shrs * Copyright (c) 2009, Sun Microsystems, Inc. 5258578Shrs * All rights reserved. 6258578Shrs * 7258578Shrs * Redistribution and use in source and binary forms, with or without 8258578Shrs * modification, are permitted provided that the following conditions are met: 9258578Shrs * - Redistributions of source code must retain the above copyright notice, 10258578Shrs * this list of conditions and the following disclaimer. 11258578Shrs * - Redistributions in binary form must reproduce the above copyright notice, 12258578Shrs * this list of conditions and the following disclaimer in the documentation 13258578Shrs * and/or other materials provided with the distribution. 14258578Shrs * - Neither the name of Sun Microsystems, Inc. nor the names of its 15258578Shrs * contributors may be used to endorse or promote products derived 16258578Shrs * from this software without specific prior written permission. 1774462Salfred * 18258578Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19258578Shrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20258578Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21258578Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22258578Shrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23258578Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24258578Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25258578Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26258578Shrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27258578Shrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28258578Shrs * 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: releng/11.0/lib/libc/rpc/clnt_dg.c 298830 2016-04-30 01:24:24Z pfg $"); 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. 156287341Srodrigc * 157287341Srodrigc * fd - open file descriptor 158287341Srodrigc * svcaddr - servers address 159287341Srodrigc * program - program number 160287341Srodrigc * version - version number 161287341Srodrigc * sendsz - buffer recv size 162287341Srodrigc * recvsz - buffer send size 16374462Salfred */ 16474462SalfredCLIENT * 165287341Srodrigcclnt_dg_create(int fd, const struct netbuf *svcaddr, rpcprog_t program, 166287341Srodrigc rpcvers_t version, u_int sendsz, u_int recvsz) 16774462Salfred{ 16874462Salfred CLIENT *cl = NULL; /* client handle */ 16974462Salfred struct cu_data *cu = NULL; /* private data */ 17074462Salfred struct timeval now; 17174462Salfred struct rpc_msg call_msg; 17274462Salfred sigset_t mask; 17374462Salfred sigset_t newmask; 17474462Salfred struct __rpc_sockinfo si; 17574462Salfred int one = 1; 17674462Salfred 17774462Salfred sigfillset(&newmask); 17874462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 17974462Salfred mutex_lock(&clnt_fd_lock); 18074462Salfred if (dg_fd_locks == (int *) NULL) { 18174462Salfred int cv_allocsz; 18274462Salfred size_t fd_allocsz; 18374462Salfred int dtbsize = __rpc_dtbsize(); 18474462Salfred 18574462Salfred fd_allocsz = dtbsize * sizeof (int); 18674462Salfred dg_fd_locks = (int *) mem_alloc(fd_allocsz); 18774462Salfred if (dg_fd_locks == (int *) NULL) { 18874462Salfred mutex_unlock(&clnt_fd_lock); 18974462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 19074462Salfred goto err1; 19174462Salfred } else 19274462Salfred memset(dg_fd_locks, '\0', fd_allocsz); 19374462Salfred 19474462Salfred cv_allocsz = dtbsize * sizeof (cond_t); 19574462Salfred dg_cv = (cond_t *) mem_alloc(cv_allocsz); 19674462Salfred if (dg_cv == (cond_t *) NULL) { 19774462Salfred mem_free(dg_fd_locks, fd_allocsz); 19874462Salfred dg_fd_locks = (int *) NULL; 19974462Salfred mutex_unlock(&clnt_fd_lock); 20074462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 20174462Salfred goto err1; 20274462Salfred } else { 20374462Salfred int i; 20474462Salfred 20574462Salfred for (i = 0; i < dtbsize; i++) 20674462Salfred cond_init(&dg_cv[i], 0, (void *) 0); 20774462Salfred } 20874462Salfred } 20974462Salfred 21074462Salfred mutex_unlock(&clnt_fd_lock); 21174462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 21274462Salfred 21374462Salfred if (svcaddr == NULL) { 21474462Salfred rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 21574462Salfred return (NULL); 21674462Salfred } 21774462Salfred 21874462Salfred if (!__rpc_fd2sockinfo(fd, &si)) { 21974462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; 22074462Salfred rpc_createerr.cf_error.re_errno = 0; 22174462Salfred return (NULL); 22274462Salfred } 22374462Salfred /* 22474462Salfred * Find the receive and the send size 22574462Salfred */ 22674462Salfred sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 22774462Salfred recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 22874462Salfred if ((sendsz == 0) || (recvsz == 0)) { 22974462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */ 23074462Salfred rpc_createerr.cf_error.re_errno = 0; 23174462Salfred return (NULL); 23274462Salfred } 23374462Salfred 23474462Salfred if ((cl = mem_alloc(sizeof (CLIENT))) == NULL) 23574462Salfred goto err1; 23674462Salfred /* 23774462Salfred * Should be multiple of 4 for XDR. 23874462Salfred */ 23974462Salfred sendsz = ((sendsz + 3) / 4) * 4; 24074462Salfred recvsz = ((recvsz + 3) / 4) * 4; 24174462Salfred cu = mem_alloc(sizeof (*cu) + sendsz + recvsz); 24274462Salfred if (cu == NULL) 24374462Salfred goto err1; 24474462Salfred (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len); 24574462Salfred cu->cu_rlen = svcaddr->len; 24674462Salfred cu->cu_outbuf = &cu->cu_inbuf[recvsz]; 24774462Salfred /* Other values can also be set through clnt_control() */ 24874462Salfred cu->cu_wait.tv_sec = 15; /* heuristically chosen */ 24974462Salfred cu->cu_wait.tv_usec = 0; 25074462Salfred cu->cu_total.tv_sec = -1; 25174462Salfred cu->cu_total.tv_usec = -1; 25274462Salfred cu->cu_sendsz = sendsz; 25374462Salfred cu->cu_recvsz = recvsz; 25474879Swpaul cu->cu_async = FALSE; 25578678Siedowse cu->cu_connect = FALSE; 25678678Siedowse cu->cu_connected = FALSE; 25774462Salfred (void) gettimeofday(&now, NULL); 25874462Salfred call_msg.rm_xid = __RPC_GETXID(&now); 25974462Salfred call_msg.rm_call.cb_prog = program; 26074462Salfred call_msg.rm_call.cb_vers = version; 261181344Sdfr xdrmem_create(&(cu->cu_outxdrs), cu->cu_outhdr, MCALL_MSG_SIZE, 262181344Sdfr XDR_ENCODE); 263181344Sdfr if (! xdr_callhdr(&cu->cu_outxdrs, &call_msg)) { 26474462Salfred rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */ 26574462Salfred rpc_createerr.cf_error.re_errno = 0; 26674462Salfred goto err2; 26774462Salfred } 26874462Salfred cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs)); 269181344Sdfr XDR_DESTROY(&cu->cu_outxdrs); 270181344Sdfr xdrmem_create(&cu->cu_outxdrs, cu->cu_outbuf, sendsz, XDR_ENCODE); 27174462Salfred 27274462Salfred /* XXX fvdl - do we still want this? */ 27374462Salfred#if 0 27474462Salfred (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf); 27574462Salfred#endif 27674462Salfred _ioctl(fd, FIONBIO, (char *)(void *)&one); 27774462Salfred 27874462Salfred /* 27974462Salfred * By default, closeit is always FALSE. It is users responsibility 28074462Salfred * to do a close on it, else the user may use clnt_control 28174462Salfred * to let clnt_destroy do it for him/her. 28274462Salfred */ 28374462Salfred cu->cu_closeit = FALSE; 28474462Salfred cu->cu_fd = fd; 28574462Salfred cl->cl_ops = clnt_dg_ops(); 28674462Salfred cl->cl_private = (caddr_t)(void *)cu; 28774462Salfred cl->cl_auth = authnone_create(); 28874462Salfred cl->cl_tp = NULL; 28974462Salfred cl->cl_netid = NULL; 290105189Siedowse cu->cu_kq = -1; 291105189Siedowse EV_SET(&cu->cu_kin, cu->cu_fd, EVFILT_READ, EV_ADD, 0, 0, 0); 29274462Salfred return (cl); 29374462Salfrederr1: 29474462Salfred warnx(mem_err_clnt_dg); 29574462Salfred rpc_createerr.cf_stat = RPC_SYSTEMERROR; 29674462Salfred rpc_createerr.cf_error.re_errno = errno; 29774462Salfrederr2: 29874462Salfred if (cl) { 29974462Salfred mem_free(cl, sizeof (CLIENT)); 30074462Salfred if (cu) 30174462Salfred mem_free(cu, sizeof (*cu) + sendsz + recvsz); 30274462Salfred } 30374462Salfred return (NULL); 30474462Salfred} 30574462Salfred 306287341Srodrigc/* 307287341Srodrigc * cl - client handle 308287341Srodrigc * proc - procedure number 309287341Srodrigc * xargs - xdr routine for args 310287341Srodrigc * argsp - pointer to args 311287341Srodrigc * xresults - xdr routine for results 312287341Srodrigc * resultsp - pointer to results 313287341Srodrigc * utimeout - seconds to wait before giving up 314287341Srodrigc */ 31574462Salfredstatic enum clnt_stat 316287341Srodrigcclnt_dg_call(CLIENT *cl, rpcproc_t proc, xdrproc_t xargs, void *argsp, 317287341Srodrigc xdrproc_t xresults, void *resultsp, struct timeval utimeout) 31874462Salfred{ 31974462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 32074462Salfred XDR *xdrs; 321105189Siedowse size_t outlen = 0; 32274462Salfred struct rpc_msg reply_msg; 32374462Salfred XDR reply_xdrs; 32474462Salfred bool_t ok; 32574462Salfred int nrefreshes = 2; /* number of times to refresh cred */ 326181344Sdfr int nretries = 0; /* number of times we retransmitted */ 32774462Salfred struct timeval timeout; 32874462Salfred struct timeval retransmit_time; 329105189Siedowse struct timeval next_sendtime, starttime, time_waited, tv; 330105189Siedowse struct timespec ts; 331105189Siedowse struct kevent kv; 33278678Siedowse struct sockaddr *sa; 33374462Salfred sigset_t mask; 33474462Salfred sigset_t newmask; 335278932Spfg socklen_t salen; 33674462Salfred ssize_t recvlen = 0; 337105189Siedowse int kin_len, n, rpc_lock_value; 33874879Swpaul u_int32_t xid; 33974462Salfred 34090271Salfred outlen = 0; 34174462Salfred sigfillset(&newmask); 34274462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 34374462Salfred mutex_lock(&clnt_fd_lock); 34474462Salfred while (dg_fd_locks[cu->cu_fd]) 34574462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 34674462Salfred if (__isthreaded) 34774462Salfred rpc_lock_value = 1; 34874462Salfred else 34974462Salfred rpc_lock_value = 0; 35074462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 35174462Salfred mutex_unlock(&clnt_fd_lock); 35274462Salfred if (cu->cu_total.tv_usec == -1) { 35374462Salfred timeout = utimeout; /* use supplied timeout */ 35474462Salfred } else { 35574462Salfred timeout = cu->cu_total; /* use default timeout */ 35674462Salfred } 35774462Salfred 35878678Siedowse if (cu->cu_connect && !cu->cu_connected) { 35978678Siedowse if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr, 36078678Siedowse cu->cu_rlen) < 0) { 36178678Siedowse cu->cu_error.re_errno = errno; 362105189Siedowse cu->cu_error.re_status = RPC_CANTSEND; 363105189Siedowse goto out; 36478678Siedowse } 36578678Siedowse cu->cu_connected = 1; 36678678Siedowse } 36778678Siedowse if (cu->cu_connected) { 36878678Siedowse sa = NULL; 36978678Siedowse salen = 0; 37078678Siedowse } else { 37178678Siedowse sa = (struct sockaddr *)&cu->cu_raddr; 37278678Siedowse salen = cu->cu_rlen; 37378678Siedowse } 37474462Salfred time_waited.tv_sec = 0; 37574462Salfred time_waited.tv_usec = 0; 376105189Siedowse retransmit_time = next_sendtime = cu->cu_wait; 377105189Siedowse gettimeofday(&starttime, NULL); 37874462Salfred 379105189Siedowse /* Clean up in case the last call ended in a longjmp(3) call. */ 380105189Siedowse if (cu->cu_kq >= 0) 381105189Siedowse _close(cu->cu_kq); 382105189Siedowse if ((cu->cu_kq = kqueue()) < 0) { 383105189Siedowse cu->cu_error.re_errno = errno; 384105189Siedowse cu->cu_error.re_status = RPC_CANTSEND; 385105189Siedowse goto out; 386105189Siedowse } 387105189Siedowse kin_len = 1; 388105189Siedowse 38974462Salfredcall_again: 390183039Sdfr if (cu->cu_async == TRUE && xargs == NULL) 391183039Sdfr goto get_reply; 39274462Salfred /* 39374462Salfred * the transaction is the first thing in the out buffer 39474879Swpaul * XXX Yes, and it's in network byte order, so we should to 39574879Swpaul * be careful when we increment it, shouldn't we. 39674462Salfred */ 397181344Sdfr xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr)); 39874879Swpaul xid++; 399181344Sdfr *(u_int32_t *)(void *)(cu->cu_outhdr) = htonl(xid); 400181344Sdfrcall_again_same_xid: 401181344Sdfr xdrs = &(cu->cu_outxdrs); 402181344Sdfr xdrs->x_op = XDR_ENCODE; 403181344Sdfr XDR_SETPOS(xdrs, 0); 40474879Swpaul 405181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { 406181344Sdfr if ((! XDR_PUTBYTES(xdrs, cu->cu_outhdr, cu->cu_xdrpos)) || 407181344Sdfr (! XDR_PUTINT32(xdrs, &proc)) || 408181344Sdfr (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || 409181344Sdfr (! (*xargs)(xdrs, argsp))) { 410181344Sdfr cu->cu_error.re_status = RPC_CANTENCODEARGS; 411181344Sdfr goto out; 412181344Sdfr } 413181344Sdfr } else { 414181344Sdfr *(uint32_t *) &cu->cu_outhdr[cu->cu_xdrpos] = htonl(proc); 415181344Sdfr if (!__rpc_gss_wrap(cl->cl_auth, cu->cu_outhdr, 416181344Sdfr cu->cu_xdrpos + sizeof(uint32_t), 417181344Sdfr xdrs, xargs, argsp)) { 418181344Sdfr cu->cu_error.re_status = RPC_CANTENCODEARGS; 419181344Sdfr goto out; 420181344Sdfr } 42174462Salfred } 42274462Salfred outlen = (size_t)XDR_GETPOS(xdrs); 42374462Salfred 42474462Salfredsend_again: 42578678Siedowse if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) { 42674462Salfred cu->cu_error.re_errno = errno; 427105189Siedowse cu->cu_error.re_status = RPC_CANTSEND; 428105189Siedowse goto out; 42974462Salfred } 43074462Salfred 43174462Salfred /* 43274462Salfred * Hack to provide rpc-based message passing 43374462Salfred */ 43474462Salfred if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { 435105189Siedowse cu->cu_error.re_status = RPC_TIMEDOUT; 436105189Siedowse goto out; 43774462Salfred } 43874879Swpaul 43974879Swpaulget_reply: 44074879Swpaul 44174462Salfred /* 44274462Salfred * sub-optimal code appears here because we have 44374462Salfred * some clock time to spare while the packets are in flight. 44474462Salfred * (We assume that this is actually only executed once.) 44574462Salfred */ 44674462Salfred reply_msg.acpted_rply.ar_verf = _null_auth; 447181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { 448181344Sdfr reply_msg.acpted_rply.ar_results.where = resultsp; 449181344Sdfr reply_msg.acpted_rply.ar_results.proc = xresults; 450181344Sdfr } else { 451181344Sdfr reply_msg.acpted_rply.ar_results.where = NULL; 452181344Sdfr reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 453181344Sdfr } 45474462Salfred 45574462Salfred for (;;) { 456105189Siedowse /* Decide how long to wait. */ 457105189Siedowse if (timercmp(&next_sendtime, &timeout, <)) 458105189Siedowse timersub(&next_sendtime, &time_waited, &tv); 459105189Siedowse else 460105189Siedowse timersub(&timeout, &time_waited, &tv); 461105189Siedowse if (tv.tv_sec < 0 || tv.tv_usec < 0) 462105189Siedowse tv.tv_sec = tv.tv_usec = 0; 463105189Siedowse TIMEVAL_TO_TIMESPEC(&tv, &ts); 46474462Salfred 465105189Siedowse n = _kevent(cu->cu_kq, &cu->cu_kin, kin_len, &kv, 1, &ts); 466105189Siedowse /* We don't need to register the event again. */ 467105189Siedowse kin_len = 0; 46874462Salfred 469105189Siedowse if (n == 1) { 470105189Siedowse if (kv.flags & EV_ERROR) { 471105189Siedowse cu->cu_error.re_errno = kv.data; 472105189Siedowse cu->cu_error.re_status = RPC_CANTRECV; 473105189Siedowse goto out; 474105189Siedowse } 475105189Siedowse /* We have some data now */ 476105189Siedowse do { 477105189Siedowse recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf, 478105189Siedowse cu->cu_recvsz, 0, NULL, NULL); 479105189Siedowse } while (recvlen < 0 && errno == EINTR); 480105189Siedowse if (recvlen < 0 && errno != EWOULDBLOCK) { 48174462Salfred cu->cu_error.re_errno = errno; 482105189Siedowse cu->cu_error.re_status = RPC_CANTRECV; 483105189Siedowse goto out; 48474462Salfred } 485105189Siedowse if (recvlen >= sizeof(u_int32_t) && 486105189Siedowse (cu->cu_async == TRUE || 487105189Siedowse *((u_int32_t *)(void *)(cu->cu_inbuf)) == 488105189Siedowse *((u_int32_t *)(void *)(cu->cu_outbuf)))) { 489105189Siedowse /* We now assume we have the proper reply. */ 490105189Siedowse break; 49174462Salfred } 492105189Siedowse } 493105189Siedowse if (n == -1 && errno != EINTR) { 494105189Siedowse cu->cu_error.re_errno = errno; 49574462Salfred cu->cu_error.re_status = RPC_CANTRECV; 496105189Siedowse goto out; 49774462Salfred } 498105189Siedowse gettimeofday(&tv, NULL); 499105189Siedowse timersub(&tv, &starttime, &time_waited); 50074462Salfred 501105189Siedowse /* Check for timeout. */ 502105189Siedowse if (timercmp(&time_waited, &timeout, >)) { 503105189Siedowse cu->cu_error.re_status = RPC_TIMEDOUT; 504105189Siedowse goto out; 50574462Salfred } 506105189Siedowse 507105189Siedowse /* Retransmit if necessary. */ 508105189Siedowse if (timercmp(&time_waited, &next_sendtime, >)) { 509105189Siedowse /* update retransmit_time */ 510105189Siedowse if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) 511105189Siedowse timeradd(&retransmit_time, &retransmit_time, 512105189Siedowse &retransmit_time); 513105189Siedowse timeradd(&next_sendtime, &retransmit_time, 514105189Siedowse &next_sendtime); 515181344Sdfr nretries++; 516181344Sdfr 517181344Sdfr /* 518181344Sdfr * When retransmitting a RPCSEC_GSS message, 519181344Sdfr * we must use a new sequence number (handled 520181344Sdfr * by __rpc_gss_wrap above). 521181344Sdfr */ 522181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) 523181344Sdfr goto send_again; 524181344Sdfr else 525181344Sdfr goto call_again_same_xid; 526105189Siedowse } 52774462Salfred } 52874462Salfred 52974462Salfred /* 53074462Salfred * now decode and validate the response 53174462Salfred */ 53274462Salfred 533121654Smbr xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE); 53474462Salfred ok = xdr_replymsg(&reply_xdrs, &reply_msg); 53574462Salfred /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ 53674462Salfred if (ok) { 53774462Salfred if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 53874462Salfred (reply_msg.acpted_rply.ar_stat == SUCCESS)) 53974462Salfred cu->cu_error.re_status = RPC_SUCCESS; 54074462Salfred else 54174462Salfred _seterr_reply(&reply_msg, &(cu->cu_error)); 54274462Salfred 54374462Salfred if (cu->cu_error.re_status == RPC_SUCCESS) { 54474462Salfred if (! AUTH_VALIDATE(cl->cl_auth, 54574462Salfred &reply_msg.acpted_rply.ar_verf)) { 546181344Sdfr if (nretries && 547181344Sdfr cl->cl_auth->ah_cred.oa_flavor 548181344Sdfr == RPCSEC_GSS) 549181344Sdfr /* 550181344Sdfr * If we retransmitted, its 551181344Sdfr * possible that we will 552181344Sdfr * receive a reply for one of 553181344Sdfr * the earlier transmissions 554181344Sdfr * (which will use an older 555181344Sdfr * RPCSEC_GSS sequence 556181344Sdfr * number). In this case, just 557181344Sdfr * go back and listen for a 558181344Sdfr * new reply. We could keep a 559181344Sdfr * record of all the seq 560181344Sdfr * numbers we have transmitted 561181344Sdfr * so far so that we could 562181344Sdfr * accept a reply for any of 563181344Sdfr * them here. 564181344Sdfr */ 565181344Sdfr goto get_reply; 56674462Salfred cu->cu_error.re_status = RPC_AUTHERROR; 56774462Salfred cu->cu_error.re_why = AUTH_INVALIDRESP; 568181344Sdfr } else { 569181344Sdfr if (cl->cl_auth->ah_cred.oa_flavor 570181344Sdfr == RPCSEC_GSS) { 571181344Sdfr if (!__rpc_gss_unwrap(cl->cl_auth, 572181344Sdfr &reply_xdrs, xresults, 573181344Sdfr resultsp)) 574181344Sdfr cu->cu_error.re_status = 575181344Sdfr RPC_CANTDECODERES; 576181344Sdfr } 57774462Salfred } 57874462Salfred if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { 57974462Salfred xdrs->x_op = XDR_FREE; 58074462Salfred (void) xdr_opaque_auth(xdrs, 58174462Salfred &(reply_msg.acpted_rply.ar_verf)); 58274462Salfred } 58374462Salfred } /* end successful completion */ 58474462Salfred /* 585298830Spfg * If unsuccessful AND error is an authentication error 58674462Salfred * then refresh credentials and try again, else break 58774462Salfred */ 58874462Salfred else if (cu->cu_error.re_status == RPC_AUTHERROR) 58974462Salfred /* maybe our credentials need to be refreshed ... */ 59074462Salfred if (nrefreshes > 0 && 59174462Salfred AUTH_REFRESH(cl->cl_auth, &reply_msg)) { 59274462Salfred nrefreshes--; 59374462Salfred goto call_again; 59474462Salfred } 59574462Salfred /* end of unsuccessful completion */ 59674462Salfred } /* end of valid reply message */ 59774462Salfred else { 59874462Salfred cu->cu_error.re_status = RPC_CANTDECODERES; 59974462Salfred 60074462Salfred } 601105189Siedowseout: 602105189Siedowse if (cu->cu_kq >= 0) 603105189Siedowse _close(cu->cu_kq); 604105189Siedowse cu->cu_kq = -1; 60574462Salfred release_fd_lock(cu->cu_fd, mask); 60674462Salfred return (cu->cu_error.re_status); 60774462Salfred} 60874462Salfred 60974462Salfredstatic void 610287341Srodrigcclnt_dg_geterr(CLIENT *cl, struct rpc_err *errp) 61174462Salfred{ 61274462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 61374462Salfred 61474462Salfred *errp = cu->cu_error; 61574462Salfred} 61674462Salfred 61774462Salfredstatic bool_t 618287341Srodrigcclnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr) 61974462Salfred{ 62074462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 62174462Salfred XDR *xdrs = &(cu->cu_outxdrs); 62274462Salfred bool_t dummy; 62374462Salfred sigset_t mask; 62474462Salfred sigset_t newmask; 62574462Salfred 62674462Salfred sigfillset(&newmask); 62774462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 62874462Salfred mutex_lock(&clnt_fd_lock); 62974462Salfred while (dg_fd_locks[cu->cu_fd]) 63074462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 63174462Salfred xdrs->x_op = XDR_FREE; 63274462Salfred dummy = (*xdr_res)(xdrs, res_ptr); 63374462Salfred mutex_unlock(&clnt_fd_lock); 63474462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 63574462Salfred cond_signal(&dg_cv[cu->cu_fd]); 63674462Salfred return (dummy); 63774462Salfred} 63874462Salfred 63974462Salfred/*ARGSUSED*/ 64074462Salfredstatic void 641287341Srodrigcclnt_dg_abort(CLIENT *h) 64274462Salfred{ 64374462Salfred} 64474462Salfred 64574462Salfredstatic bool_t 646287341Srodrigcclnt_dg_control(CLIENT *cl, u_int request, void *info) 64774462Salfred{ 64874462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 64974462Salfred struct netbuf *addr; 65074462Salfred sigset_t mask; 65174462Salfred sigset_t newmask; 65274462Salfred int rpc_lock_value; 65374462Salfred 65474462Salfred sigfillset(&newmask); 65574462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 65674462Salfred mutex_lock(&clnt_fd_lock); 65774462Salfred while (dg_fd_locks[cu->cu_fd]) 65874462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 65974462Salfred if (__isthreaded) 66074462Salfred rpc_lock_value = 1; 66174462Salfred else 66274462Salfred rpc_lock_value = 0; 66374462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 66474462Salfred mutex_unlock(&clnt_fd_lock); 66574462Salfred switch (request) { 66674462Salfred case CLSET_FD_CLOSE: 66774462Salfred cu->cu_closeit = TRUE; 66874462Salfred release_fd_lock(cu->cu_fd, mask); 66974462Salfred return (TRUE); 67074462Salfred case CLSET_FD_NCLOSE: 67174462Salfred cu->cu_closeit = FALSE; 67274462Salfred release_fd_lock(cu->cu_fd, mask); 67374462Salfred return (TRUE); 67474462Salfred } 67574462Salfred 67674462Salfred /* for other requests which use info */ 67774462Salfred if (info == NULL) { 67874462Salfred release_fd_lock(cu->cu_fd, mask); 67974462Salfred return (FALSE); 68074462Salfred } 68174462Salfred switch (request) { 68274462Salfred case CLSET_TIMEOUT: 68399996Salfred if (time_not_ok((struct timeval *)info)) { 68474462Salfred release_fd_lock(cu->cu_fd, mask); 68574462Salfred return (FALSE); 68674462Salfred } 68799996Salfred cu->cu_total = *(struct timeval *)info; 68874462Salfred break; 68974462Salfred case CLGET_TIMEOUT: 69099996Salfred *(struct timeval *)info = cu->cu_total; 69174462Salfred break; 69274462Salfred case CLGET_SERVER_ADDR: /* Give him the fd address */ 69374462Salfred /* Now obsolete. Only for backward compatibility */ 69474462Salfred (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen); 69574462Salfred break; 69674462Salfred case CLSET_RETRY_TIMEOUT: 69799996Salfred if (time_not_ok((struct timeval *)info)) { 69874462Salfred release_fd_lock(cu->cu_fd, mask); 69974462Salfred return (FALSE); 70074462Salfred } 70199996Salfred cu->cu_wait = *(struct timeval *)info; 70274462Salfred break; 70374462Salfred case CLGET_RETRY_TIMEOUT: 70499996Salfred *(struct timeval *)info = cu->cu_wait; 70574462Salfred break; 70674462Salfred case CLGET_FD: 70799996Salfred *(int *)info = cu->cu_fd; 70874462Salfred break; 70974462Salfred case CLGET_SVC_ADDR: 71099996Salfred addr = (struct netbuf *)info; 71174462Salfred addr->buf = &cu->cu_raddr; 71274462Salfred addr->len = cu->cu_rlen; 71374462Salfred addr->maxlen = sizeof cu->cu_raddr; 71474462Salfred break; 71574462Salfred case CLSET_SVC_ADDR: /* set to new address */ 71699996Salfred addr = (struct netbuf *)info; 71775097Siedowse if (addr->len < sizeof cu->cu_raddr) { 71875097Siedowse release_fd_lock(cu->cu_fd, mask); 71974462Salfred return (FALSE); 72075097Siedowse } 72174462Salfred (void) memcpy(&cu->cu_raddr, addr->buf, addr->len); 72274462Salfred cu->cu_rlen = addr->len; 72374462Salfred break; 72474462Salfred case CLGET_XID: 72574462Salfred /* 72674462Salfred * use the knowledge that xid is the 72774462Salfred * first element in the call structure *. 72874462Salfred * This will get the xid of the PREVIOUS call 72974462Salfred */ 73099996Salfred *(u_int32_t *)info = 731181344Sdfr ntohl(*(u_int32_t *)(void *)cu->cu_outhdr); 73274462Salfred break; 73374462Salfred 73474462Salfred case CLSET_XID: 73574462Salfred /* This will set the xid of the NEXT call */ 736181344Sdfr *(u_int32_t *)(void *)cu->cu_outhdr = 73799996Salfred htonl(*(u_int32_t *)info - 1); 73874462Salfred /* decrement by 1 as clnt_dg_call() increments once */ 73974462Salfred break; 74074462Salfred 74174462Salfred case CLGET_VERS: 74274462Salfred /* 74374462Salfred * This RELIES on the information that, in the call body, 74474462Salfred * the version number field is the fifth field from the 745298830Spfg * beginning of the RPC header. MUST be changed if the 74674462Salfred * call_struct is changed 74774462Salfred */ 74899996Salfred *(u_int32_t *)info = 749181344Sdfr ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr + 75074462Salfred 4 * BYTES_PER_XDR_UNIT)); 75174462Salfred break; 75274462Salfred 75374462Salfred case CLSET_VERS: 754181344Sdfr *(u_int32_t *)(void *)(cu->cu_outhdr + 4 * BYTES_PER_XDR_UNIT) 75599996Salfred = htonl(*(u_int32_t *)info); 75674462Salfred break; 75774462Salfred 75874462Salfred case CLGET_PROG: 75974462Salfred /* 76074462Salfred * This RELIES on the information that, in the call body, 76174462Salfred * the program number field is the fourth field from the 762298830Spfg * beginning of the RPC header. MUST be changed if the 76374462Salfred * call_struct is changed 76474462Salfred */ 76599996Salfred *(u_int32_t *)info = 766181344Sdfr ntohl(*(u_int32_t *)(void *)(cu->cu_outhdr + 76774462Salfred 3 * BYTES_PER_XDR_UNIT)); 76874462Salfred break; 76974462Salfred 77074462Salfred case CLSET_PROG: 771181344Sdfr *(u_int32_t *)(void *)(cu->cu_outhdr + 3 * BYTES_PER_XDR_UNIT) 77299996Salfred = htonl(*(u_int32_t *)info); 77374462Salfred break; 77474879Swpaul case CLSET_ASYNC: 77599996Salfred cu->cu_async = *(int *)info; 77674879Swpaul break; 77778678Siedowse case CLSET_CONNECT: 77899996Salfred cu->cu_connect = *(int *)info; 77978678Siedowse break; 78074462Salfred default: 78174462Salfred release_fd_lock(cu->cu_fd, mask); 78274462Salfred return (FALSE); 78374462Salfred } 78474462Salfred release_fd_lock(cu->cu_fd, mask); 78574462Salfred return (TRUE); 78674462Salfred} 78774462Salfred 78874462Salfredstatic void 789287341Srodrigcclnt_dg_destroy(CLIENT *cl) 79074462Salfred{ 79174462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 79274462Salfred int cu_fd = cu->cu_fd; 79374462Salfred sigset_t mask; 79474462Salfred sigset_t newmask; 79574462Salfred 79674462Salfred sigfillset(&newmask); 79774462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 79874462Salfred mutex_lock(&clnt_fd_lock); 79974462Salfred while (dg_fd_locks[cu_fd]) 80074462Salfred cond_wait(&dg_cv[cu_fd], &clnt_fd_lock); 80174462Salfred if (cu->cu_closeit) 80274462Salfred (void)_close(cu_fd); 803105189Siedowse if (cu->cu_kq >= 0) 804105189Siedowse _close(cu->cu_kq); 80574462Salfred XDR_DESTROY(&(cu->cu_outxdrs)); 80674462Salfred mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz)); 80774462Salfred if (cl->cl_netid && cl->cl_netid[0]) 80874462Salfred mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 80974462Salfred if (cl->cl_tp && cl->cl_tp[0]) 81074462Salfred mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 81174462Salfred mem_free(cl, sizeof (CLIENT)); 81274462Salfred mutex_unlock(&clnt_fd_lock); 81374462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 81474462Salfred cond_signal(&dg_cv[cu_fd]); 81574462Salfred} 81674462Salfred 81774462Salfredstatic struct clnt_ops * 818287341Srodrigcclnt_dg_ops(void) 81974462Salfred{ 82074462Salfred static struct clnt_ops ops; 82174462Salfred sigset_t mask; 82274462Salfred sigset_t newmask; 82374462Salfred 82474462Salfred/* VARIABLES PROTECTED BY ops_lock: ops */ 82574462Salfred 82674462Salfred sigfillset(&newmask); 82774462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 82874462Salfred mutex_lock(&ops_lock); 82974462Salfred if (ops.cl_call == NULL) { 83074462Salfred ops.cl_call = clnt_dg_call; 83174462Salfred ops.cl_abort = clnt_dg_abort; 83274462Salfred ops.cl_geterr = clnt_dg_geterr; 83374462Salfred ops.cl_freeres = clnt_dg_freeres; 83474462Salfred ops.cl_destroy = clnt_dg_destroy; 83574462Salfred ops.cl_control = clnt_dg_control; 83674462Salfred } 83774462Salfred mutex_unlock(&ops_lock); 83874462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 83974462Salfred return (&ops); 84074462Salfred} 84174462Salfred 84274462Salfred/* 84374462Salfred * Make sure that the time is not garbage. -1 value is allowed. 84474462Salfred */ 84574462Salfredstatic bool_t 846287341Srodrigctime_not_ok(struct timeval *t) 84774462Salfred{ 84874462Salfred return (t->tv_sec < -1 || t->tv_sec > 100000000 || 84974462Salfred t->tv_usec < -1 || t->tv_usec > 1000000); 85074462Salfred} 85174462Salfred 852