18360efbdSAlfred Perlstein /* $NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 christos Exp $ */ 28360efbdSAlfred Perlstein 399064799SGarrett Wollman /* 499064799SGarrett Wollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 599064799SGarrett Wollman * unrestricted use provided that this legend is included on all tape 699064799SGarrett Wollman * media and as a part of the software program in whole or part. Users 799064799SGarrett Wollman * may copy or modify Sun RPC without charge, but are not authorized 899064799SGarrett Wollman * to license or distribute it to anyone else except as part of a product or 999064799SGarrett Wollman * program developed by the user. 1099064799SGarrett Wollman * 1199064799SGarrett Wollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1299064799SGarrett Wollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1399064799SGarrett Wollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1499064799SGarrett Wollman * 1599064799SGarrett Wollman * Sun RPC is provided with no support and without any obligation on the 1699064799SGarrett Wollman * part of Sun Microsystems, Inc. to assist in its use, correction, 1799064799SGarrett Wollman * modification or enhancement. 1899064799SGarrett Wollman * 1999064799SGarrett Wollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 2099064799SGarrett Wollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 2199064799SGarrett Wollman * OR ANY PART THEREOF. 2299064799SGarrett Wollman * 2399064799SGarrett Wollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2499064799SGarrett Wollman * or profits or other special, indirect and consequential damages, even if 2599064799SGarrett Wollman * Sun has been advised of the possibility of such damages. 2699064799SGarrett Wollman * 2799064799SGarrett Wollman * Sun Microsystems, Inc. 2899064799SGarrett Wollman * 2550 Garcia Avenue 2999064799SGarrett Wollman * Mountain View, California 94043 3099064799SGarrett Wollman */ 3199064799SGarrett Wollman 328360efbdSAlfred Perlstein #include <sys/cdefs.h> 3399064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint) 348360efbdSAlfred Perlstein static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; 358360efbdSAlfred Perlstein static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC"; 367f3dea24SPeter Wemm static char *rcsid = "$FreeBSD$"; 3799064799SGarrett Wollman #endif 3899064799SGarrett Wollman 3999064799SGarrett Wollman /* 4099064799SGarrett Wollman * clnt_raw.c 4199064799SGarrett Wollman * 4299064799SGarrett Wollman * Copyright (C) 1984, Sun Microsystems, Inc. 4399064799SGarrett Wollman * 4499064799SGarrett Wollman * Memory based rpc for simple testing and timing. 4599064799SGarrett Wollman * Interface to create an rpc client and server in the same process. 4699064799SGarrett Wollman * This lets us similate rpc and get round trip overhead, without 4799064799SGarrett Wollman * any interference from the kernal. 4899064799SGarrett Wollman */ 4999064799SGarrett Wollman 508360efbdSAlfred Perlstein #include "reentrant.h" 518360efbdSAlfred Perlstein #include "namespace.h" 528360efbdSAlfred Perlstein #include <assert.h> 538360efbdSAlfred Perlstein #include <err.h> 54c124f3bdSJames Raynard #include <stdio.h> 558360efbdSAlfred Perlstein #include <stdlib.h> 568360efbdSAlfred Perlstein 578360efbdSAlfred Perlstein #include <rpc/rpc.h> 588360efbdSAlfred Perlstein #include <rpc/raw.h> 598360efbdSAlfred Perlstein #include "un-namespace.h" 608360efbdSAlfred Perlstein 618360efbdSAlfred Perlstein extern mutex_t clntraw_lock; 6299064799SGarrett Wollman 6399064799SGarrett Wollman #define MCALL_MSG_SIZE 24 6499064799SGarrett Wollman 6599064799SGarrett Wollman /* 6699064799SGarrett Wollman * This is the "network" we will be moving stuff over. 6799064799SGarrett Wollman */ 6899064799SGarrett Wollman static struct clntraw_private { 6999064799SGarrett Wollman CLIENT client_object; 7099064799SGarrett Wollman XDR xdr_stream; 718360efbdSAlfred Perlstein char *_raw_buf; 728360efbdSAlfred Perlstein union { 738360efbdSAlfred Perlstein struct rpc_msg mashl_rpcmsg; 7499064799SGarrett Wollman char mashl_callmsg[MCALL_MSG_SIZE]; 758360efbdSAlfred Perlstein } u; 7699064799SGarrett Wollman u_int mcnt; 7799064799SGarrett Wollman } *clntraw_private; 7899064799SGarrett Wollman 798360efbdSAlfred Perlstein static enum clnt_stat clnt_raw_call __P((CLIENT *, rpcproc_t, xdrproc_t, 808360efbdSAlfred Perlstein caddr_t, xdrproc_t, caddr_t, struct timeval)); 818360efbdSAlfred Perlstein static void clnt_raw_geterr __P((CLIENT *, struct rpc_err *)); 828360efbdSAlfred Perlstein static bool_t clnt_raw_freeres __P((CLIENT *, xdrproc_t, caddr_t)); 838360efbdSAlfred Perlstein static void clnt_raw_abort __P((CLIENT *)); 848360efbdSAlfred Perlstein static bool_t clnt_raw_control __P((CLIENT *, u_int, char *)); 858360efbdSAlfred Perlstein static void clnt_raw_destroy __P((CLIENT *)); 868360efbdSAlfred Perlstein static struct clnt_ops *clnt_raw_ops __P((void)); 8799064799SGarrett Wollman 8899064799SGarrett Wollman /* 8999064799SGarrett Wollman * Create a client handle for memory based rpc. 9099064799SGarrett Wollman */ 9199064799SGarrett Wollman CLIENT * 928360efbdSAlfred Perlstein clnt_raw_create(prog, vers) 938360efbdSAlfred Perlstein rpcprog_t prog; 948360efbdSAlfred Perlstein rpcvers_t vers; 9599064799SGarrett Wollman { 968360efbdSAlfred Perlstein struct clntraw_private *clp = clntraw_private; 9799064799SGarrett Wollman struct rpc_msg call_msg; 9899064799SGarrett Wollman XDR *xdrs = &clp->xdr_stream; 9999064799SGarrett Wollman CLIENT *client = &clp->client_object; 10099064799SGarrett Wollman 1018360efbdSAlfred Perlstein mutex_lock(&clntraw_lock); 1028360efbdSAlfred Perlstein if (clp == NULL) { 10399064799SGarrett Wollman clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); 1048360efbdSAlfred Perlstein if (clp == NULL) { 1058360efbdSAlfred Perlstein mutex_unlock(&clntraw_lock); 1068360efbdSAlfred Perlstein return NULL; 1078360efbdSAlfred Perlstein } 1088360efbdSAlfred Perlstein if (__rpc_rawcombuf == NULL) 1098360efbdSAlfred Perlstein __rpc_rawcombuf = 1108360efbdSAlfred Perlstein (char *)calloc(UDPMSGSIZE, sizeof (char)); 1118360efbdSAlfred Perlstein clp->_raw_buf = __rpc_rawcombuf; 11299064799SGarrett Wollman clntraw_private = clp; 11399064799SGarrett Wollman } 11499064799SGarrett Wollman /* 1155f0d67beSAdam David * pre-serialize the static part of the call msg and stash it away 11699064799SGarrett Wollman */ 11799064799SGarrett Wollman call_msg.rm_direction = CALL; 11899064799SGarrett Wollman call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 1198360efbdSAlfred Perlstein /* XXX: prog and vers have been long historically :-( */ 1208360efbdSAlfred Perlstein call_msg.rm_call.cb_prog = (u_int32_t)prog; 1218360efbdSAlfred Perlstein call_msg.rm_call.cb_vers = (u_int32_t)vers; 1228360efbdSAlfred Perlstein xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 1238360efbdSAlfred Perlstein if (! xdr_callhdr(xdrs, &call_msg)) 1248360efbdSAlfred Perlstein warnx("clntraw_create - Fatal header serialization error."); 12599064799SGarrett Wollman clp->mcnt = XDR_GETPOS(xdrs); 12699064799SGarrett Wollman XDR_DESTROY(xdrs); 12799064799SGarrett Wollman 12899064799SGarrett Wollman /* 12999064799SGarrett Wollman * Set xdrmem for client/server shared buffer 13099064799SGarrett Wollman */ 13199064799SGarrett Wollman xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE); 13299064799SGarrett Wollman 13399064799SGarrett Wollman /* 13499064799SGarrett Wollman * create client handle 13599064799SGarrett Wollman */ 1368360efbdSAlfred Perlstein client->cl_ops = clnt_raw_ops(); 13799064799SGarrett Wollman client->cl_auth = authnone_create(); 1388360efbdSAlfred Perlstein mutex_unlock(&clntraw_lock); 13999064799SGarrett Wollman return (client); 14099064799SGarrett Wollman } 14199064799SGarrett Wollman 1428360efbdSAlfred Perlstein /* ARGSUSED */ 14399064799SGarrett Wollman static enum clnt_stat 1448360efbdSAlfred Perlstein clnt_raw_call(h, proc, xargs, argsp, xresults, resultsp, timeout) 14599064799SGarrett Wollman CLIENT *h; 1468360efbdSAlfred Perlstein rpcproc_t proc; 14799064799SGarrett Wollman xdrproc_t xargs; 14899064799SGarrett Wollman caddr_t argsp; 14999064799SGarrett Wollman xdrproc_t xresults; 15099064799SGarrett Wollman caddr_t resultsp; 15199064799SGarrett Wollman struct timeval timeout; 15299064799SGarrett Wollman { 1538360efbdSAlfred Perlstein struct clntraw_private *clp = clntraw_private; 1548360efbdSAlfred Perlstein XDR *xdrs = &clp->xdr_stream; 15599064799SGarrett Wollman struct rpc_msg msg; 15699064799SGarrett Wollman enum clnt_stat status; 15799064799SGarrett Wollman struct rpc_err error; 15899064799SGarrett Wollman 1598360efbdSAlfred Perlstein assert(h != NULL); 1608360efbdSAlfred Perlstein 1618360efbdSAlfred Perlstein mutex_lock(&clntraw_lock); 1628360efbdSAlfred Perlstein if (clp == NULL) { 1638360efbdSAlfred Perlstein mutex_unlock(&clntraw_lock); 16499064799SGarrett Wollman return (RPC_FAILED); 1658360efbdSAlfred Perlstein } 1668360efbdSAlfred Perlstein mutex_unlock(&clntraw_lock); 1678360efbdSAlfred Perlstein 16899064799SGarrett Wollman call_again: 16999064799SGarrett Wollman /* 17099064799SGarrett Wollman * send request 17199064799SGarrett Wollman */ 17299064799SGarrett Wollman xdrs->x_op = XDR_ENCODE; 17399064799SGarrett Wollman XDR_SETPOS(xdrs, 0); 1748360efbdSAlfred Perlstein clp->u.mashl_rpcmsg.rm_xid ++ ; 1758360efbdSAlfred Perlstein if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) || 1768360efbdSAlfred Perlstein (! XDR_PUTINT32(xdrs, &proc)) || 17799064799SGarrett Wollman (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 17899064799SGarrett Wollman (! (*xargs)(xdrs, argsp))) { 17999064799SGarrett Wollman return (RPC_CANTENCODEARGS); 18099064799SGarrett Wollman } 18199064799SGarrett Wollman (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 18299064799SGarrett Wollman 18399064799SGarrett Wollman /* 18499064799SGarrett Wollman * We have to call server input routine here because this is 18599064799SGarrett Wollman * all going on in one process. Yuk. 18699064799SGarrett Wollman */ 1878360efbdSAlfred Perlstein svc_getreq_common(FD_SETSIZE); 18899064799SGarrett Wollman 18999064799SGarrett Wollman /* 19099064799SGarrett Wollman * get results 19199064799SGarrett Wollman */ 19299064799SGarrett Wollman xdrs->x_op = XDR_DECODE; 19399064799SGarrett Wollman XDR_SETPOS(xdrs, 0); 19499064799SGarrett Wollman msg.acpted_rply.ar_verf = _null_auth; 19599064799SGarrett Wollman msg.acpted_rply.ar_results.where = resultsp; 19699064799SGarrett Wollman msg.acpted_rply.ar_results.proc = xresults; 1978360efbdSAlfred Perlstein if (! xdr_replymsg(xdrs, &msg)) { 1988360efbdSAlfred Perlstein /* 1998360efbdSAlfred Perlstein * It's possible for xdr_replymsg() to fail partway 2008360efbdSAlfred Perlstein * through its attempt to decode the result from the 2018360efbdSAlfred Perlstein * server. If this happens, it will leave the reply 2028360efbdSAlfred Perlstein * structure partially populated with dynamically 2038360efbdSAlfred Perlstein * allocated memory. (This can happen if someone uses 2048360efbdSAlfred Perlstein * clntudp_bufcreate() to create a CLIENT handle and 2058360efbdSAlfred Perlstein * specifies a receive buffer size that is too small.) 2068360efbdSAlfred Perlstein * This memory must be free()ed to avoid a leak. 2078360efbdSAlfred Perlstein */ 2088360efbdSAlfred Perlstein int op = xdrs->x_op; 2098360efbdSAlfred Perlstein xdrs->x_op = XDR_FREE; 2108360efbdSAlfred Perlstein xdr_replymsg(xdrs, &msg); 2118360efbdSAlfred Perlstein xdrs->x_op = op; 21299064799SGarrett Wollman return (RPC_CANTDECODERES); 2138360efbdSAlfred Perlstein } 21499064799SGarrett Wollman _seterr_reply(&msg, &error); 21599064799SGarrett Wollman status = error.re_status; 21699064799SGarrett Wollman 21799064799SGarrett Wollman if (status == RPC_SUCCESS) { 21899064799SGarrett Wollman if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 21999064799SGarrett Wollman status = RPC_AUTHERROR; 22099064799SGarrett Wollman } 22199064799SGarrett Wollman } /* end successful completion */ 22299064799SGarrett Wollman else { 2238360efbdSAlfred Perlstein if (AUTH_REFRESH(h->cl_auth, &msg)) 22499064799SGarrett Wollman goto call_again; 22599064799SGarrett Wollman } /* end of unsuccessful completion */ 22699064799SGarrett Wollman 22799064799SGarrett Wollman if (status == RPC_SUCCESS) { 22899064799SGarrett Wollman if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 22999064799SGarrett Wollman status = RPC_AUTHERROR; 23099064799SGarrett Wollman } 23199064799SGarrett Wollman if (msg.acpted_rply.ar_verf.oa_base != NULL) { 23299064799SGarrett Wollman xdrs->x_op = XDR_FREE; 23399064799SGarrett Wollman (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 23499064799SGarrett Wollman } 23599064799SGarrett Wollman } 23699064799SGarrett Wollman 23799064799SGarrett Wollman return (status); 23899064799SGarrett Wollman } 23999064799SGarrett Wollman 2408360efbdSAlfred Perlstein /*ARGSUSED*/ 24199064799SGarrett Wollman static void 2428360efbdSAlfred Perlstein clnt_raw_geterr(cl, err) 2438360efbdSAlfred Perlstein CLIENT *cl; 2448360efbdSAlfred Perlstein struct rpc_err *err; 24599064799SGarrett Wollman { 24699064799SGarrett Wollman } 24799064799SGarrett Wollman 24899064799SGarrett Wollman 2498360efbdSAlfred Perlstein /* ARGSUSED */ 25099064799SGarrett Wollman static bool_t 2518360efbdSAlfred Perlstein clnt_raw_freeres(cl, xdr_res, res_ptr) 25299064799SGarrett Wollman CLIENT *cl; 25399064799SGarrett Wollman xdrproc_t xdr_res; 25499064799SGarrett Wollman caddr_t res_ptr; 25599064799SGarrett Wollman { 2568360efbdSAlfred Perlstein struct clntraw_private *clp = clntraw_private; 2578360efbdSAlfred Perlstein XDR *xdrs = &clp->xdr_stream; 25899064799SGarrett Wollman bool_t rval; 25999064799SGarrett Wollman 2608360efbdSAlfred Perlstein mutex_lock(&clntraw_lock); 2618360efbdSAlfred Perlstein if (clp == NULL) { 26299064799SGarrett Wollman rval = (bool_t) RPC_FAILED; 2638360efbdSAlfred Perlstein mutex_unlock(&clntraw_lock); 26499064799SGarrett Wollman return (rval); 26599064799SGarrett Wollman } 2668360efbdSAlfred Perlstein mutex_unlock(&clntraw_lock); 26799064799SGarrett Wollman xdrs->x_op = XDR_FREE; 26899064799SGarrett Wollman return ((*xdr_res)(xdrs, res_ptr)); 26999064799SGarrett Wollman } 27099064799SGarrett Wollman 2718360efbdSAlfred Perlstein /*ARGSUSED*/ 27299064799SGarrett Wollman static void 2738360efbdSAlfred Perlstein clnt_raw_abort(cl) 2748360efbdSAlfred Perlstein CLIENT *cl; 27599064799SGarrett Wollman { 27699064799SGarrett Wollman } 27799064799SGarrett Wollman 2788360efbdSAlfred Perlstein /*ARGSUSED*/ 27999064799SGarrett Wollman static bool_t 2808360efbdSAlfred Perlstein clnt_raw_control(cl, ui, str) 2818360efbdSAlfred Perlstein CLIENT *cl; 2828360efbdSAlfred Perlstein u_int ui; 2838360efbdSAlfred Perlstein char *str; 28499064799SGarrett Wollman { 28599064799SGarrett Wollman return (FALSE); 28699064799SGarrett Wollman } 28799064799SGarrett Wollman 2888360efbdSAlfred Perlstein /*ARGSUSED*/ 28999064799SGarrett Wollman static void 2908360efbdSAlfred Perlstein clnt_raw_destroy(cl) 2918360efbdSAlfred Perlstein CLIENT *cl; 29299064799SGarrett Wollman { 29399064799SGarrett Wollman } 2948360efbdSAlfred Perlstein 2958360efbdSAlfred Perlstein static struct clnt_ops * 2968360efbdSAlfred Perlstein clnt_raw_ops() 2978360efbdSAlfred Perlstein { 2988360efbdSAlfred Perlstein static struct clnt_ops ops; 2998360efbdSAlfred Perlstein extern mutex_t ops_lock; 3008360efbdSAlfred Perlstein 3018360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY ops_lock: ops */ 3028360efbdSAlfred Perlstein 3038360efbdSAlfred Perlstein mutex_lock(&ops_lock); 3048360efbdSAlfred Perlstein if (ops.cl_call == NULL) { 3058360efbdSAlfred Perlstein ops.cl_call = clnt_raw_call; 3068360efbdSAlfred Perlstein ops.cl_abort = clnt_raw_abort; 3078360efbdSAlfred Perlstein ops.cl_geterr = clnt_raw_geterr; 3088360efbdSAlfred Perlstein ops.cl_freeres = clnt_raw_freeres; 3098360efbdSAlfred Perlstein ops.cl_destroy = clnt_raw_destroy; 3108360efbdSAlfred Perlstein ops.cl_control = clnt_raw_control; 3118360efbdSAlfred Perlstein } 3128360efbdSAlfred Perlstein mutex_unlock(&ops_lock); 3138360efbdSAlfred Perlstein return (&ops); 3148360efbdSAlfred Perlstein } 315