clnt_dg.c revision 75097
174462Salfred/* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */ 274462Salfred/* $FreeBSD: head/lib/libc/rpc/clnt_dg.c 75097 2001-04-02 22:14:13Z iedowse $ */ 374462Salfred 474462Salfred/* 574462Salfred * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 674462Salfred * unrestricted use provided that this legend is included on all tape 774462Salfred * media and as a part of the software program in whole or part. Users 874462Salfred * may copy or modify Sun RPC without charge, but are not authorized 974462Salfred * to license or distribute it to anyone else except as part of a product or 1074462Salfred * program developed by the user. 1174462Salfred * 1274462Salfred * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1374462Salfred * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1474462Salfred * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1574462Salfred * 1674462Salfred * Sun RPC is provided with no support and without any obligation on the 1774462Salfred * part of Sun Microsystems, Inc. to assist in its use, correction, 1874462Salfred * modification or enhancement. 1974462Salfred * 2074462Salfred * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 2174462Salfred * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 2274462Salfred * OR ANY PART THEREOF. 2374462Salfred * 2474462Salfred * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2574462Salfred * or profits or other special, indirect and consequential damages, even if 2674462Salfred * Sun has been advised of the possibility of such damages. 2774462Salfred * 2874462Salfred * Sun Microsystems, Inc. 2974462Salfred * 2550 Garcia Avenue 3074462Salfred * Mountain View, California 94043 3174462Salfred */ 3274462Salfred/* 3374462Salfred * Copyright (c) 1986-1991 by Sun Microsystems Inc. 3474462Salfred */ 3574462Salfred 3674462Salfred/* #ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" */ 3774462Salfred 3874462Salfred#if 0 3974462Salfred#if !defined(lint) && defined(SCCSIDS) 4074462Salfredstatic char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro"; 4174462Salfred#endif 4274462Salfred#endif 4374462Salfred 4474462Salfred/* 4574462Salfred * Implements a connectionless client side RPC. 4674462Salfred */ 4774462Salfred 4875094Siedowse#include "namespace.h" 4974462Salfred#include "reentrant.h" 5074462Salfred#include <sys/poll.h> 5174462Salfred#include <sys/types.h> 5274462Salfred#include <sys/time.h> 5374462Salfred#include <sys/socket.h> 5474462Salfred#include <sys/ioctl.h> 5574462Salfred#include <rpc/rpc.h> 5674462Salfred#include <errno.h> 5774462Salfred#include <stdlib.h> 5874462Salfred#include <string.h> 5974462Salfred#include <signal.h> 6074462Salfred#include <unistd.h> 6174462Salfred#include <err.h> 6274462Salfred#include "un-namespace.h" 6374462Salfred#include "rpc_com.h" 6474462Salfred 6574462Salfred 6674462Salfred#define RPC_MAX_BACKOFF 30 /* seconds */ 6774462Salfred 6874462Salfred 6974462Salfredstatic struct clnt_ops *clnt_dg_ops __P((void)); 7074462Salfredstatic bool_t time_not_ok __P((struct timeval *)); 7174462Salfredstatic enum clnt_stat clnt_dg_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t, 7274462Salfred xdrproc_t, caddr_t, struct timeval)); 7374462Salfredstatic void clnt_dg_geterr __P((CLIENT *, struct rpc_err *)); 7474462Salfredstatic bool_t clnt_dg_freeres __P((CLIENT *, xdrproc_t, caddr_t)); 7574462Salfredstatic void clnt_dg_abort __P((CLIENT *)); 7674462Salfredstatic bool_t clnt_dg_control __P((CLIENT *, u_int, char *)); 7774462Salfredstatic void clnt_dg_destroy __P((CLIENT *)); 7874462Salfredstatic int __rpc_timeval_to_msec __P((struct timeval *)); 7974462Salfred 8074462Salfred 8174462Salfred 8274462Salfred 8374462Salfred/* 8474462Salfred * This machinery implements per-fd locks for MT-safety. It is not 8574462Salfred * sufficient to do per-CLIENT handle locks for MT-safety because a 8674462Salfred * user may create more than one CLIENT handle with the same fd behind 8774462Salfred * it. Therfore, we allocate an array of flags (dg_fd_locks), protected 8874462Salfred * by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables 8974462Salfred * similarly protected. Dg_fd_lock[fd] == 1 => a call is activte on some 9074462Salfred * CLIENT handle created for that fd. 9174462Salfred * The current implementation holds locks across the entire RPC and reply, 9274462Salfred * including retransmissions. Yes, this is silly, and as soon as this 9374462Salfred * code is proven to work, this should be the first thing fixed. One step 9474462Salfred * at a time. 9574462Salfred */ 9674462Salfredstatic int *dg_fd_locks; 9774462Salfredextern mutex_t clnt_fd_lock; 9874462Salfredstatic cond_t *dg_cv; 9974462Salfred#define release_fd_lock(fd, mask) { \ 10074462Salfred mutex_lock(&clnt_fd_lock); \ 10174462Salfred if (__isthreaded) \ 10274462Salfred dg_fd_locks[fd] = 0; \ 10374462Salfred mutex_unlock(&clnt_fd_lock); \ 10474462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL); \ 10574462Salfred cond_signal(&dg_cv[fd]); \ 10674462Salfred} 10774462Salfred 10874462Salfredstatic const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory"; 10974462Salfred 11074462Salfred/* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */ 11174462Salfred 11274462Salfred/* 11374462Salfred * Private data kept per client handle 11474462Salfred */ 11574462Salfredstruct cu_data { 11674462Salfred int cu_fd; /* connections fd */ 11774462Salfred bool_t cu_closeit; /* opened by library */ 11874462Salfred struct sockaddr_storage cu_raddr; /* remote address */ 11974462Salfred int cu_rlen; 12074462Salfred struct timeval cu_wait; /* retransmit interval */ 12174462Salfred struct timeval cu_total; /* total time for the call */ 12274462Salfred struct rpc_err cu_error; 12374462Salfred XDR cu_outxdrs; 12474462Salfred u_int cu_xdrpos; 12574462Salfred u_int cu_sendsz; /* send size */ 12674462Salfred char *cu_outbuf; 12774462Salfred u_int cu_recvsz; /* recv size */ 12874462Salfred struct pollfd pfdp; 12974879Swpaul int cu_async; 13074462Salfred char cu_inbuf[1]; 13174462Salfred}; 13274462Salfred 13374462Salfred/* 13474462Salfred * Connection less client creation returns with client handle parameters. 13574462Salfred * Default options are set, which the user can change using clnt_control(). 13674462Salfred * fd should be open and bound. 13774462Salfred * NB: The rpch->cl_auth is initialized to null authentication. 13874462Salfred * Caller may wish to set this something more useful. 13974462Salfred * 14074462Salfred * sendsz and recvsz are the maximum allowable packet sizes that can be 14174462Salfred * sent and received. Normally they are the same, but they can be 14274462Salfred * changed to improve the program efficiency and buffer allocation. 14374462Salfred * If they are 0, use the transport default. 14474462Salfred * 14574462Salfred * If svcaddr is NULL, returns NULL. 14674462Salfred */ 14774462SalfredCLIENT * 14874462Salfredclnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz) 14974462Salfred int fd; /* open file descriptor */ 15074462Salfred const struct netbuf *svcaddr; /* servers address */ 15174462Salfred rpcprog_t program; /* program number */ 15274462Salfred rpcvers_t version; /* version number */ 15374462Salfred u_int sendsz; /* buffer recv size */ 15474462Salfred u_int recvsz; /* buffer send size */ 15574462Salfred{ 15674462Salfred CLIENT *cl = NULL; /* client handle */ 15774462Salfred struct cu_data *cu = NULL; /* private data */ 15874462Salfred struct timeval now; 15974462Salfred struct rpc_msg call_msg; 16074462Salfred sigset_t mask; 16174462Salfred sigset_t newmask; 16274462Salfred struct __rpc_sockinfo si; 16374462Salfred int one = 1; 16474462Salfred 16574462Salfred sigfillset(&newmask); 16674462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 16774462Salfred mutex_lock(&clnt_fd_lock); 16874462Salfred if (dg_fd_locks == (int *) NULL) { 16974462Salfred int cv_allocsz; 17074462Salfred size_t fd_allocsz; 17174462Salfred int dtbsize = __rpc_dtbsize(); 17274462Salfred 17374462Salfred fd_allocsz = dtbsize * sizeof (int); 17474462Salfred dg_fd_locks = (int *) mem_alloc(fd_allocsz); 17574462Salfred if (dg_fd_locks == (int *) NULL) { 17674462Salfred mutex_unlock(&clnt_fd_lock); 17774462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 17874462Salfred goto err1; 17974462Salfred } else 18074462Salfred memset(dg_fd_locks, '\0', fd_allocsz); 18174462Salfred 18274462Salfred cv_allocsz = dtbsize * sizeof (cond_t); 18374462Salfred dg_cv = (cond_t *) mem_alloc(cv_allocsz); 18474462Salfred if (dg_cv == (cond_t *) NULL) { 18574462Salfred mem_free(dg_fd_locks, fd_allocsz); 18674462Salfred dg_fd_locks = (int *) NULL; 18774462Salfred mutex_unlock(&clnt_fd_lock); 18874462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 18974462Salfred goto err1; 19074462Salfred } else { 19174462Salfred int i; 19274462Salfred 19374462Salfred for (i = 0; i < dtbsize; i++) 19474462Salfred cond_init(&dg_cv[i], 0, (void *) 0); 19574462Salfred } 19674462Salfred } 19774462Salfred 19874462Salfred mutex_unlock(&clnt_fd_lock); 19974462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 20074462Salfred 20174462Salfred if (svcaddr == NULL) { 20274462Salfred rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 20374462Salfred return (NULL); 20474462Salfred } 20574462Salfred 20674462Salfred if (!__rpc_fd2sockinfo(fd, &si)) { 20774462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; 20874462Salfred rpc_createerr.cf_error.re_errno = 0; 20974462Salfred return (NULL); 21074462Salfred } 21174462Salfred /* 21274462Salfred * Find the receive and the send size 21374462Salfred */ 21474462Salfred sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 21574462Salfred recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 21674462Salfred if ((sendsz == 0) || (recvsz == 0)) { 21774462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */ 21874462Salfred rpc_createerr.cf_error.re_errno = 0; 21974462Salfred return (NULL); 22074462Salfred } 22174462Salfred 22274462Salfred if ((cl = mem_alloc(sizeof (CLIENT))) == NULL) 22374462Salfred goto err1; 22474462Salfred /* 22574462Salfred * Should be multiple of 4 for XDR. 22674462Salfred */ 22774462Salfred sendsz = ((sendsz + 3) / 4) * 4; 22874462Salfred recvsz = ((recvsz + 3) / 4) * 4; 22974462Salfred cu = mem_alloc(sizeof (*cu) + sendsz + recvsz); 23074462Salfred if (cu == NULL) 23174462Salfred goto err1; 23274462Salfred (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len); 23374462Salfred cu->cu_rlen = svcaddr->len; 23474462Salfred cu->cu_outbuf = &cu->cu_inbuf[recvsz]; 23574462Salfred /* Other values can also be set through clnt_control() */ 23674462Salfred cu->cu_wait.tv_sec = 15; /* heuristically chosen */ 23774462Salfred cu->cu_wait.tv_usec = 0; 23874462Salfred cu->cu_total.tv_sec = -1; 23974462Salfred cu->cu_total.tv_usec = -1; 24074462Salfred cu->cu_sendsz = sendsz; 24174462Salfred cu->cu_recvsz = recvsz; 24274879Swpaul cu->cu_async = FALSE; 24374462Salfred (void) gettimeofday(&now, NULL); 24474462Salfred call_msg.rm_xid = __RPC_GETXID(&now); 24574462Salfred call_msg.rm_call.cb_prog = program; 24674462Salfred call_msg.rm_call.cb_vers = version; 24774462Salfred xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE); 24874462Salfred if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) { 24974462Salfred rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */ 25074462Salfred rpc_createerr.cf_error.re_errno = 0; 25174462Salfred goto err2; 25274462Salfred } 25374462Salfred cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs)); 25474462Salfred 25574462Salfred /* XXX fvdl - do we still want this? */ 25674462Salfred#if 0 25774462Salfred (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf); 25874462Salfred#endif 25974462Salfred _ioctl(fd, FIONBIO, (char *)(void *)&one); 26074462Salfred 26174462Salfred /* 26274462Salfred * By default, closeit is always FALSE. It is users responsibility 26374462Salfred * to do a close on it, else the user may use clnt_control 26474462Salfred * to let clnt_destroy do it for him/her. 26574462Salfred */ 26674462Salfred cu->cu_closeit = FALSE; 26774462Salfred cu->cu_fd = fd; 26874462Salfred cl->cl_ops = clnt_dg_ops(); 26974462Salfred cl->cl_private = (caddr_t)(void *)cu; 27074462Salfred cl->cl_auth = authnone_create(); 27174462Salfred cl->cl_tp = NULL; 27274462Salfred cl->cl_netid = NULL; 27374462Salfred cu->pfdp.fd = cu->cu_fd; 27474462Salfred cu->pfdp.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND; 27574462Salfred return (cl); 27674462Salfrederr1: 27774462Salfred warnx(mem_err_clnt_dg); 27874462Salfred rpc_createerr.cf_stat = RPC_SYSTEMERROR; 27974462Salfred rpc_createerr.cf_error.re_errno = errno; 28074462Salfrederr2: 28174462Salfred if (cl) { 28274462Salfred mem_free(cl, sizeof (CLIENT)); 28374462Salfred if (cu) 28474462Salfred mem_free(cu, sizeof (*cu) + sendsz + recvsz); 28574462Salfred } 28674462Salfred return (NULL); 28774462Salfred} 28874462Salfred 28974462Salfredstatic enum clnt_stat 29074462Salfredclnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout) 29174462Salfred CLIENT *cl; /* client handle */ 29274462Salfred rpcproc_t proc; /* procedure number */ 29374462Salfred xdrproc_t xargs; /* xdr routine for args */ 29474462Salfred caddr_t argsp; /* pointer to args */ 29574462Salfred xdrproc_t xresults; /* xdr routine for results */ 29674462Salfred caddr_t resultsp; /* pointer to results */ 29774462Salfred struct timeval utimeout; /* seconds to wait before giving up */ 29874462Salfred{ 29974462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 30074462Salfred XDR *xdrs; 30174462Salfred size_t outlen; 30274462Salfred struct rpc_msg reply_msg; 30374462Salfred XDR reply_xdrs; 30474462Salfred struct timeval time_waited; 30574462Salfred bool_t ok; 30674462Salfred int nrefreshes = 2; /* number of times to refresh cred */ 30774462Salfred struct timeval timeout; 30874462Salfred struct timeval retransmit_time; 30974462Salfred struct timeval startime, curtime; 31074462Salfred int firsttimeout = 1; 31174462Salfred int dtbsize = __rpc_dtbsize(); 31274462Salfred sigset_t mask; 31374462Salfred sigset_t newmask; 31474462Salfred socklen_t fromlen, inlen; 31574462Salfred ssize_t recvlen = 0; 31674462Salfred int rpc_lock_value; 31774879Swpaul u_int32_t xid; 31874462Salfred 31974462Salfred sigfillset(&newmask); 32074462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 32174462Salfred mutex_lock(&clnt_fd_lock); 32274462Salfred while (dg_fd_locks[cu->cu_fd]) 32374462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 32474462Salfred if (__isthreaded) 32574462Salfred rpc_lock_value = 1; 32674462Salfred else 32774462Salfred rpc_lock_value = 0; 32874462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 32974462Salfred mutex_unlock(&clnt_fd_lock); 33074462Salfred if (cu->cu_total.tv_usec == -1) { 33174462Salfred timeout = utimeout; /* use supplied timeout */ 33274462Salfred } else { 33374462Salfred timeout = cu->cu_total; /* use default timeout */ 33474462Salfred } 33574462Salfred 33674462Salfred time_waited.tv_sec = 0; 33774462Salfred time_waited.tv_usec = 0; 33874462Salfred retransmit_time = cu->cu_wait; 33974462Salfred 34074462Salfredcall_again: 34174462Salfred xdrs = &(cu->cu_outxdrs); 34274879Swpaul if (cu->cu_async == TRUE && xargs == NULL) 34374879Swpaul goto get_reply; 34474462Salfred xdrs->x_op = XDR_ENCODE; 34574462Salfred XDR_SETPOS(xdrs, cu->cu_xdrpos); 34674462Salfred /* 34774462Salfred * the transaction is the first thing in the out buffer 34874879Swpaul * XXX Yes, and it's in network byte order, so we should to 34974879Swpaul * be careful when we increment it, shouldn't we. 35074462Salfred */ 35174879Swpaul xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf)); 35274879Swpaul xid++; 35374879Swpaul *(u_int32_t *)(void *)(cu->cu_outbuf) = htonl(xid); 35474879Swpaul 35574462Salfred if ((! XDR_PUTINT32(xdrs, &proc)) || 35674462Salfred (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || 35774462Salfred (! (*xargs)(xdrs, argsp))) { 35874462Salfred release_fd_lock(cu->cu_fd, mask); 35974462Salfred return (cu->cu_error.re_status = RPC_CANTENCODEARGS); 36074462Salfred } 36174462Salfred outlen = (size_t)XDR_GETPOS(xdrs); 36274462Salfred 36374462Salfredsend_again: 36474462Salfred if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, 36574462Salfred (struct sockaddr *)(void *)&cu->cu_raddr, (socklen_t)cu->cu_rlen) 36674462Salfred != outlen) { 36774462Salfred cu->cu_error.re_errno = errno; 36874462Salfred release_fd_lock(cu->cu_fd, mask); 36974462Salfred return (cu->cu_error.re_status = RPC_CANTSEND); 37074462Salfred } 37174462Salfred 37274462Salfred /* 37374462Salfred * Hack to provide rpc-based message passing 37474462Salfred */ 37574462Salfred if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { 37674462Salfred release_fd_lock(cu->cu_fd, mask); 37774462Salfred return (cu->cu_error.re_status = RPC_TIMEDOUT); 37874462Salfred } 37974879Swpaul 38074879Swpaulget_reply: 38174879Swpaul 38274462Salfred /* 38374462Salfred * sub-optimal code appears here because we have 38474462Salfred * some clock time to spare while the packets are in flight. 38574462Salfred * (We assume that this is actually only executed once.) 38674462Salfred */ 38774462Salfred reply_msg.acpted_rply.ar_verf = _null_auth; 38874462Salfred reply_msg.acpted_rply.ar_results.where = resultsp; 38974462Salfred reply_msg.acpted_rply.ar_results.proc = xresults; 39074462Salfred 39174462Salfred 39274462Salfred for (;;) { 39374462Salfred switch (_poll(&cu->pfdp, 1, 39474462Salfred __rpc_timeval_to_msec(&retransmit_time))) { 39574462Salfred case 0: 39674462Salfred time_waited.tv_sec += retransmit_time.tv_sec; 39774462Salfred time_waited.tv_usec += retransmit_time.tv_usec; 39874462Salfred while (time_waited.tv_usec >= 1000000) { 39974462Salfred time_waited.tv_sec++; 40074462Salfred time_waited.tv_usec -= 1000000; 40174462Salfred } 40274462Salfred /* update retransmit_time */ 40374462Salfred if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) { 40474462Salfred retransmit_time.tv_usec *= 2; 40574462Salfred retransmit_time.tv_sec *= 2; 40674462Salfred while (retransmit_time.tv_usec >= 1000000) { 40774462Salfred retransmit_time.tv_sec++; 40874462Salfred retransmit_time.tv_usec -= 1000000; 40974462Salfred } 41074462Salfred } 41174462Salfred 41274462Salfred if ((time_waited.tv_sec < timeout.tv_sec) || 41374462Salfred ((time_waited.tv_sec == timeout.tv_sec) && 41474462Salfred (time_waited.tv_usec < timeout.tv_usec))) 41574462Salfred goto send_again; 41674462Salfred release_fd_lock(cu->cu_fd, mask); 41774462Salfred return (cu->cu_error.re_status = RPC_TIMEDOUT); 41874462Salfred 41974462Salfred case -1: 42074462Salfred if (errno == EBADF) { 42174462Salfred cu->cu_error.re_errno = errno; 42274462Salfred release_fd_lock(cu->cu_fd, mask); 42374462Salfred return (cu->cu_error.re_status = RPC_CANTRECV); 42474462Salfred } 42574462Salfred if (errno != EINTR) { 42674462Salfred errno = 0; /* reset it */ 42774462Salfred continue; 42874462Salfred } 42974462Salfred /* interrupted by another signal, update time_waited */ 43074462Salfred if (firsttimeout) { 43174462Salfred /* 43274462Salfred * Could have done gettimeofday before clnt_call 43374462Salfred * but that means 1 more system call per each 43474462Salfred * clnt_call, so do it after first time out 43574462Salfred */ 43674462Salfred if (gettimeofday(&startime, 43774462Salfred (struct timezone *) NULL) == -1) { 43874462Salfred errno = 0; 43974462Salfred continue; 44074462Salfred } 44174462Salfred firsttimeout = 0; 44274462Salfred errno = 0; 44374462Salfred continue; 44474462Salfred }; 44574462Salfred if (gettimeofday(&curtime, 44674462Salfred (struct timezone *) NULL) == -1) { 44774462Salfred errno = 0; 44874462Salfred continue; 44974462Salfred }; 45074462Salfred time_waited.tv_sec += curtime.tv_sec - startime.tv_sec; 45174462Salfred time_waited.tv_usec += curtime.tv_usec - 45274462Salfred startime.tv_usec; 45374462Salfred while (time_waited.tv_usec < 0) { 45474462Salfred time_waited.tv_sec--; 45574462Salfred time_waited.tv_usec += 1000000; 45674462Salfred }; 45774462Salfred while (time_waited.tv_usec >= 1000000) { 45874462Salfred time_waited.tv_sec++; 45974462Salfred time_waited.tv_usec -= 1000000; 46074462Salfred } 46174462Salfred startime.tv_sec = curtime.tv_sec; 46274462Salfred startime.tv_usec = curtime.tv_usec; 46374462Salfred if ((time_waited.tv_sec > timeout.tv_sec) || 46474462Salfred ((time_waited.tv_sec == timeout.tv_sec) && 46574462Salfred (time_waited.tv_usec > timeout.tv_usec))) { 46674462Salfred release_fd_lock(cu->cu_fd, mask); 46774462Salfred return (cu->cu_error.re_status = RPC_TIMEDOUT); 46874462Salfred } 46974462Salfred errno = 0; /* reset it */ 47074462Salfred continue; 47174462Salfred }; 47274462Salfred 47374462Salfred if (cu->pfdp.revents & POLLNVAL || (cu->pfdp.revents == 0)) { 47474462Salfred cu->cu_error.re_status = RPC_CANTRECV; 47574462Salfred /* 47674462Salfred * Note: we're faking errno here because we 47774462Salfred * previously would have expected _poll() to 47874462Salfred * return -1 with errno EBADF. Poll(BA_OS) 47974462Salfred * returns 0 and sets the POLLNVAL revents flag 48074462Salfred * instead. 48174462Salfred */ 48274462Salfred cu->cu_error.re_errno = errno = EBADF; 48374462Salfred release_fd_lock(cu->cu_fd, mask); 48474462Salfred return (-1); 48574462Salfred } 48674462Salfred 48774462Salfred /* We have some data now */ 48874462Salfred do { 48974462Salfred if (errno == EINTR) { 49074462Salfred /* 49174462Salfred * Must make sure errno was not already 49274462Salfred * EINTR in case _recvfrom() returns -1. 49374462Salfred */ 49474462Salfred errno = 0; 49574462Salfred } 49674462Salfred fromlen = sizeof (struct sockaddr_storage); 49774462Salfred recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf, 49874462Salfred cu->cu_recvsz, 0, (struct sockaddr *)(void *)&cu->cu_raddr, 49974462Salfred &fromlen); 50074462Salfred } while (recvlen < 0 && errno == EINTR); 50174462Salfred if (recvlen < 0) { 50274462Salfred if (errno == EWOULDBLOCK) 50374462Salfred continue; 50474462Salfred cu->cu_error.re_errno = errno; 50574462Salfred release_fd_lock(cu->cu_fd, mask); 50674462Salfred return (cu->cu_error.re_status = RPC_CANTRECV); 50774462Salfred } 50874462Salfred if (recvlen < sizeof (u_int32_t)) 50974462Salfred continue; 51074462Salfred /* see if reply transaction id matches sent id */ 51174879Swpaul if (cu->cu_async == FALSE && 51274879Swpaul *((u_int32_t *)(void *)(cu->cu_inbuf)) != 51374462Salfred *((u_int32_t *)(void *)(cu->cu_outbuf))) 51474462Salfred continue; 51574462Salfred /* we now assume we have the proper reply */ 51674462Salfred break; 51774462Salfred } 51874462Salfred inlen = (socklen_t)recvlen; 51974462Salfred 52074462Salfred /* 52174462Salfred * now decode and validate the response 52274462Salfred */ 52374462Salfred 52474462Salfred xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE); 52574462Salfred ok = xdr_replymsg(&reply_xdrs, &reply_msg); 52674462Salfred /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ 52774462Salfred if (ok) { 52874462Salfred if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 52974462Salfred (reply_msg.acpted_rply.ar_stat == SUCCESS)) 53074462Salfred cu->cu_error.re_status = RPC_SUCCESS; 53174462Salfred else 53274462Salfred _seterr_reply(&reply_msg, &(cu->cu_error)); 53374462Salfred 53474462Salfred if (cu->cu_error.re_status == RPC_SUCCESS) { 53574462Salfred if (! AUTH_VALIDATE(cl->cl_auth, 53674462Salfred &reply_msg.acpted_rply.ar_verf)) { 53774462Salfred cu->cu_error.re_status = RPC_AUTHERROR; 53874462Salfred cu->cu_error.re_why = AUTH_INVALIDRESP; 53974462Salfred } 54074462Salfred if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { 54174462Salfred xdrs->x_op = XDR_FREE; 54274462Salfred (void) xdr_opaque_auth(xdrs, 54374462Salfred &(reply_msg.acpted_rply.ar_verf)); 54474462Salfred } 54574462Salfred } /* end successful completion */ 54674462Salfred /* 54774462Salfred * If unsuccesful AND error is an authentication error 54874462Salfred * then refresh credentials and try again, else break 54974462Salfred */ 55074462Salfred else if (cu->cu_error.re_status == RPC_AUTHERROR) 55174462Salfred /* maybe our credentials need to be refreshed ... */ 55274462Salfred if (nrefreshes > 0 && 55374462Salfred AUTH_REFRESH(cl->cl_auth, &reply_msg)) { 55474462Salfred nrefreshes--; 55574462Salfred goto call_again; 55674462Salfred } 55774462Salfred /* end of unsuccessful completion */ 55874462Salfred } /* end of valid reply message */ 55974462Salfred else { 56074462Salfred cu->cu_error.re_status = RPC_CANTDECODERES; 56174462Salfred 56274462Salfred } 56374462Salfred release_fd_lock(cu->cu_fd, mask); 56474462Salfred return (cu->cu_error.re_status); 56574462Salfred} 56674462Salfred 56774462Salfredstatic void 56874462Salfredclnt_dg_geterr(cl, errp) 56974462Salfred CLIENT *cl; 57074462Salfred struct rpc_err *errp; 57174462Salfred{ 57274462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 57374462Salfred 57474462Salfred *errp = cu->cu_error; 57574462Salfred} 57674462Salfred 57774462Salfredstatic bool_t 57874462Salfredclnt_dg_freeres(cl, xdr_res, res_ptr) 57974462Salfred CLIENT *cl; 58074462Salfred xdrproc_t xdr_res; 58174462Salfred caddr_t res_ptr; 58274462Salfred{ 58374462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 58474462Salfred XDR *xdrs = &(cu->cu_outxdrs); 58574462Salfred bool_t dummy; 58674462Salfred sigset_t mask; 58774462Salfred sigset_t newmask; 58874462Salfred 58974462Salfred sigfillset(&newmask); 59074462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 59174462Salfred mutex_lock(&clnt_fd_lock); 59274462Salfred while (dg_fd_locks[cu->cu_fd]) 59374462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 59474462Salfred xdrs->x_op = XDR_FREE; 59574462Salfred dummy = (*xdr_res)(xdrs, res_ptr); 59674462Salfred mutex_unlock(&clnt_fd_lock); 59774462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 59874462Salfred cond_signal(&dg_cv[cu->cu_fd]); 59974462Salfred return (dummy); 60074462Salfred} 60174462Salfred 60274462Salfred/*ARGSUSED*/ 60374462Salfredstatic void 60474462Salfredclnt_dg_abort(h) 60574462Salfred CLIENT *h; 60674462Salfred{ 60774462Salfred} 60874462Salfred 60974462Salfredstatic bool_t 61074462Salfredclnt_dg_control(cl, request, info) 61174462Salfred CLIENT *cl; 61274462Salfred u_int request; 61374462Salfred char *info; 61474462Salfred{ 61574462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 61674462Salfred struct netbuf *addr; 61774462Salfred sigset_t mask; 61874462Salfred sigset_t newmask; 61974462Salfred int rpc_lock_value; 62074462Salfred 62174462Salfred sigfillset(&newmask); 62274462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 62374462Salfred mutex_lock(&clnt_fd_lock); 62474462Salfred while (dg_fd_locks[cu->cu_fd]) 62574462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 62674462Salfred if (__isthreaded) 62774462Salfred rpc_lock_value = 1; 62874462Salfred else 62974462Salfred rpc_lock_value = 0; 63074462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 63174462Salfred mutex_unlock(&clnt_fd_lock); 63274462Salfred switch (request) { 63374462Salfred case CLSET_FD_CLOSE: 63474462Salfred cu->cu_closeit = TRUE; 63574462Salfred release_fd_lock(cu->cu_fd, mask); 63674462Salfred return (TRUE); 63774462Salfred case CLSET_FD_NCLOSE: 63874462Salfred cu->cu_closeit = FALSE; 63974462Salfred release_fd_lock(cu->cu_fd, mask); 64074462Salfred return (TRUE); 64174462Salfred } 64274462Salfred 64374462Salfred /* for other requests which use info */ 64474462Salfred if (info == NULL) { 64574462Salfred release_fd_lock(cu->cu_fd, mask); 64674462Salfred return (FALSE); 64774462Salfred } 64874462Salfred switch (request) { 64974462Salfred case CLSET_TIMEOUT: 65074462Salfred if (time_not_ok((struct timeval *)(void *)info)) { 65174462Salfred release_fd_lock(cu->cu_fd, mask); 65274462Salfred return (FALSE); 65374462Salfred } 65474462Salfred cu->cu_total = *(struct timeval *)(void *)info; 65574462Salfred break; 65674462Salfred case CLGET_TIMEOUT: 65774462Salfred *(struct timeval *)(void *)info = cu->cu_total; 65874462Salfred break; 65974462Salfred case CLGET_SERVER_ADDR: /* Give him the fd address */ 66074462Salfred /* Now obsolete. Only for backward compatibility */ 66174462Salfred (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen); 66274462Salfred break; 66374462Salfred case CLSET_RETRY_TIMEOUT: 66474462Salfred if (time_not_ok((struct timeval *)(void *)info)) { 66574462Salfred release_fd_lock(cu->cu_fd, mask); 66674462Salfred return (FALSE); 66774462Salfred } 66874462Salfred cu->cu_wait = *(struct timeval *)(void *)info; 66974462Salfred break; 67074462Salfred case CLGET_RETRY_TIMEOUT: 67174462Salfred *(struct timeval *)(void *)info = cu->cu_wait; 67274462Salfred break; 67374462Salfred case CLGET_FD: 67474462Salfred *(int *)(void *)info = cu->cu_fd; 67574462Salfred break; 67674462Salfred case CLGET_SVC_ADDR: 67774462Salfred addr = (struct netbuf *)(void *)info; 67874462Salfred addr->buf = &cu->cu_raddr; 67974462Salfred addr->len = cu->cu_rlen; 68074462Salfred addr->maxlen = sizeof cu->cu_raddr; 68174462Salfred break; 68274462Salfred case CLSET_SVC_ADDR: /* set to new address */ 68374462Salfred addr = (struct netbuf *)(void *)info; 68475097Siedowse if (addr->len < sizeof cu->cu_raddr) { 68575097Siedowse release_fd_lock(cu->cu_fd, mask); 68674462Salfred return (FALSE); 68775097Siedowse } 68874462Salfred (void) memcpy(&cu->cu_raddr, addr->buf, addr->len); 68974462Salfred cu->cu_rlen = addr->len; 69074462Salfred break; 69174462Salfred case CLGET_XID: 69274462Salfred /* 69374462Salfred * use the knowledge that xid is the 69474462Salfred * first element in the call structure *. 69574462Salfred * This will get the xid of the PREVIOUS call 69674462Salfred */ 69774462Salfred *(u_int32_t *)(void *)info = 69874462Salfred ntohl(*(u_int32_t *)(void *)cu->cu_outbuf); 69974462Salfred break; 70074462Salfred 70174462Salfred case CLSET_XID: 70274462Salfred /* This will set the xid of the NEXT call */ 70374462Salfred *(u_int32_t *)(void *)cu->cu_outbuf = 70474462Salfred htonl(*(u_int32_t *)(void *)info - 1); 70574462Salfred /* decrement by 1 as clnt_dg_call() increments once */ 70674462Salfred break; 70774462Salfred 70874462Salfred case CLGET_VERS: 70974462Salfred /* 71074462Salfred * This RELIES on the information that, in the call body, 71174462Salfred * the version number field is the fifth field from the 71274462Salfred * begining of the RPC header. MUST be changed if the 71374462Salfred * call_struct is changed 71474462Salfred */ 71574462Salfred *(u_int32_t *)(void *)info = 71674462Salfred ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf + 71774462Salfred 4 * BYTES_PER_XDR_UNIT)); 71874462Salfred break; 71974462Salfred 72074462Salfred case CLSET_VERS: 72174462Salfred *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT) 72274462Salfred = htonl(*(u_int32_t *)(void *)info); 72374462Salfred break; 72474462Salfred 72574462Salfred case CLGET_PROG: 72674462Salfred /* 72774462Salfred * This RELIES on the information that, in the call body, 72874462Salfred * the program number field is the fourth field from the 72974462Salfred * begining of the RPC header. MUST be changed if the 73074462Salfred * call_struct is changed 73174462Salfred */ 73274462Salfred *(u_int32_t *)(void *)info = 73374462Salfred ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf + 73474462Salfred 3 * BYTES_PER_XDR_UNIT)); 73574462Salfred break; 73674462Salfred 73774462Salfred case CLSET_PROG: 73874462Salfred *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT) 73974462Salfred = htonl(*(u_int32_t *)(void *)info); 74074462Salfred break; 74174879Swpaul case CLSET_ASYNC: 74274879Swpaul cu->cu_async = *(int *)(void *)info; 74374879Swpaul break; 74474462Salfred default: 74574462Salfred release_fd_lock(cu->cu_fd, mask); 74674462Salfred return (FALSE); 74774462Salfred } 74874462Salfred release_fd_lock(cu->cu_fd, mask); 74974462Salfred return (TRUE); 75074462Salfred} 75174462Salfred 75274462Salfredstatic void 75374462Salfredclnt_dg_destroy(cl) 75474462Salfred CLIENT *cl; 75574462Salfred{ 75674462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 75774462Salfred int cu_fd = cu->cu_fd; 75874462Salfred sigset_t mask; 75974462Salfred sigset_t newmask; 76074462Salfred 76174462Salfred sigfillset(&newmask); 76274462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 76374462Salfred mutex_lock(&clnt_fd_lock); 76474462Salfred while (dg_fd_locks[cu_fd]) 76574462Salfred cond_wait(&dg_cv[cu_fd], &clnt_fd_lock); 76674462Salfred if (cu->cu_closeit) 76774462Salfred (void)_close(cu_fd); 76874462Salfred XDR_DESTROY(&(cu->cu_outxdrs)); 76974462Salfred mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz)); 77074462Salfred if (cl->cl_netid && cl->cl_netid[0]) 77174462Salfred mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 77274462Salfred if (cl->cl_tp && cl->cl_tp[0]) 77374462Salfred mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 77474462Salfred mem_free(cl, sizeof (CLIENT)); 77574462Salfred mutex_unlock(&clnt_fd_lock); 77674462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 77774462Salfred cond_signal(&dg_cv[cu_fd]); 77874462Salfred} 77974462Salfred 78074462Salfredstatic struct clnt_ops * 78174462Salfredclnt_dg_ops() 78274462Salfred{ 78374462Salfred static struct clnt_ops ops; 78474462Salfred extern mutex_t ops_lock; 78574462Salfred sigset_t mask; 78674462Salfred sigset_t newmask; 78774462Salfred 78874462Salfred/* VARIABLES PROTECTED BY ops_lock: ops */ 78974462Salfred 79074462Salfred sigfillset(&newmask); 79174462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 79274462Salfred mutex_lock(&ops_lock); 79374462Salfred if (ops.cl_call == NULL) { 79474462Salfred ops.cl_call = clnt_dg_call; 79574462Salfred ops.cl_abort = clnt_dg_abort; 79674462Salfred ops.cl_geterr = clnt_dg_geterr; 79774462Salfred ops.cl_freeres = clnt_dg_freeres; 79874462Salfred ops.cl_destroy = clnt_dg_destroy; 79974462Salfred ops.cl_control = clnt_dg_control; 80074462Salfred } 80174462Salfred mutex_unlock(&ops_lock); 80274462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 80374462Salfred return (&ops); 80474462Salfred} 80574462Salfred 80674462Salfred/* 80774462Salfred * Make sure that the time is not garbage. -1 value is allowed. 80874462Salfred */ 80974462Salfredstatic bool_t 81074462Salfredtime_not_ok(t) 81174462Salfred struct timeval *t; 81274462Salfred{ 81374462Salfred return (t->tv_sec < -1 || t->tv_sec > 100000000 || 81474462Salfred t->tv_usec < -1 || t->tv_usec > 1000000); 81574462Salfred} 81674462Salfred 81774462Salfred 81874462Salfred/* 81974462Salfred * Convert from timevals (used by select) to milliseconds (used by poll). 82074462Salfred */ 82174462Salfredstatic int 82274462Salfred__rpc_timeval_to_msec(t) 82374462Salfred struct timeval *t; 82474462Salfred{ 82574462Salfred int t1, tmp; 82674462Salfred 82774462Salfred /* 82874462Salfred * We're really returning t->tv_sec * 1000 + (t->tv_usec / 1000) 82974462Salfred * but try to do so efficiently. Note: 1000 = 1024 - 16 - 8. 83074462Salfred */ 83174462Salfred tmp = (int)t->tv_sec << 3; 83274462Salfred t1 = -tmp; 83374462Salfred t1 += t1 << 1; 83474462Salfred t1 += tmp << 7; 83574462Salfred if (t->tv_usec) 83674462Salfred t1 += (int)(t->tv_usec / 1000); 83774462Salfred 83874462Salfred return (t1); 83974462Salfred} 840