clnt_raw.c revision 261046
145095Ssos/* $NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 christos Exp $ */ 283728Ssos 345095Ssos/*- 445095Ssos * Copyright (c) 2009, Sun Microsystems, Inc. 545095Ssos * All rights reserved. 645095Ssos * 745095Ssos * Redistribution and use in source and binary forms, with or without 845095Ssos * modification, are permitted provided that the following conditions are met: 945095Ssos * - Redistributions of source code must retain the above copyright notice, 1045095Ssos * this list of conditions and the following disclaimer. 1145095Ssos * - Redistributions in binary form must reproduce the above copyright notice, 1245095Ssos * this list of conditions and the following disclaimer in the documentation 1345095Ssos * and/or other materials provided with the distribution. 1445095Ssos * - Neither the name of Sun Microsystems, Inc. nor the names of its 1545095Ssos * contributors may be used to endorse or promote products derived 1645095Ssos * from this software without specific prior written permission. 1745095Ssos * 1845095Ssos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1945095Ssos * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2045095Ssos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2145095Ssos * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2245095Ssos * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2345095Ssos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2445095Ssos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2545095Ssos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2645095Ssos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2745095Ssos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2850477Speter * POSSIBILITY OF SUCH DAMAGE. 2945095Ssos */ 3045095Ssos 3145150Ssos#if defined(LIBC_SCCS) && !defined(lint) 3245095Ssosstatic char *sccsid2 = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; 3345095Ssosstatic char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC"; 3474302Ssos#endif 3560041Sphk#include <sys/cdefs.h> 3645095Ssos__FBSDID("$FreeBSD: stable/10/lib/libc/rpc/clnt_raw.c 261046 2014-01-22 23:45:27Z mav $"); 3745798Ssos 3854270Ssos/* 3954270Ssos * clnt_raw.c 4051520Ssos * 4145095Ssos * Copyright (C) 1984, Sun Microsystems, Inc. 4245095Ssos * 4366106Ssos * Memory based rpc for simple testing and timing. 4472106Ssos * Interface to create an rpc client and server in the same process. 4545095Ssos * This lets us similate rpc and get round trip overhead, without 4645095Ssos * any interference from the kernel. 4752067Ssos */ 4866070Ssos 4966070Ssos#include "namespace.h" 5066070Ssos#include "reentrant.h" 5152067Ssos#include <assert.h> 5252067Ssos#include <err.h> 5345720Speter#include <stdio.h> 5445720Speter#include <stdlib.h> 5551520Ssos 5645720Speter#include <rpc/rpc.h> 5782053Ssos#include <rpc/raw.h> 5882053Ssos#include "un-namespace.h" 5982053Ssos#include "mt_misc.h" 6045720Speter 6182053Ssos#define MCALL_MSG_SIZE 24 6266070Ssos 6366070Ssos/* 6466070Ssos * This is the "network" we will be moving stuff over. 6566070Ssos */ 6666070Ssosstatic struct clntraw_private { 6766070Ssos CLIENT client_object; 6866070Ssos XDR xdr_stream; 6966070Ssos char *_raw_buf; 7066070Ssos union { 7166070Ssos struct rpc_msg mashl_rpcmsg; 7266070Ssos char mashl_callmsg[MCALL_MSG_SIZE]; 7366070Ssos } u; 7466070Ssos u_int mcnt; 7566070Ssos} *clntraw_private; 7666070Ssos 7766070Ssosstatic enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t, void *, 7856744Ssos xdrproc_t, void *, struct timeval); 7966070Ssosstatic void clnt_raw_geterr(CLIENT *, struct rpc_err *); 8066070Ssosstatic bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, void *); 8145095Ssosstatic void clnt_raw_abort(CLIENT *); 8257325Ssosstatic bool_t clnt_raw_control(CLIENT *, u_int, void *); 8367058Ssosstatic void clnt_raw_destroy(CLIENT *); 8464307Ssosstatic struct clnt_ops *clnt_raw_ops(void); 8545095Ssos 8656744Ssos/* 8756744Ssos * Create a client handle for memory based rpc. 8856744Ssos */ 8972106SsosCLIENT * 9056744Ssosclnt_raw_create(prog, vers) 9145095Ssos rpcprog_t prog; 9252067Ssos rpcvers_t vers; 9367058Ssos{ 9472106Ssos struct clntraw_private *clp; 9572106Ssos struct rpc_msg call_msg; 9672106Ssos XDR *xdrs; 9772106Ssos CLIENT *client; 9856558Ssos 9956744Ssos mutex_lock(&clntraw_lock); 10052067Ssos if ((clp = clntraw_private) == NULL) { 10152067Ssos clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); 10252067Ssos if (clp == NULL) { 10366070Ssos mutex_unlock(&clntraw_lock); 10466070Ssos return NULL; 10545095Ssos } 10685345Ssos if (__rpc_rawcombuf == NULL) 10784584Ssos __rpc_rawcombuf = 10860829Ssos (char *)calloc(UDPMSGSIZE, sizeof (char)); 10964027Ssteve clp->_raw_buf = __rpc_rawcombuf; 11060829Ssos clntraw_private = clp; 11160829Ssos } 11285345Ssos xdrs = &clp->xdr_stream; 11355333Ssos client = &clp->client_object; 11445095Ssos 11585352Ssos /* 11685352Ssos * pre-serialize the static part of the call msg and stash it away 11775553Ssos */ 11864307Ssos call_msg.rm_direction = CALL; 11964307Ssos call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 12064307Ssos /* XXX: prog and vers have been long historically :-( */ 12164307Ssos call_msg.rm_call.cb_prog = (u_int32_t)prog; 12264307Ssos call_msg.rm_call.cb_vers = (u_int32_t)vers; 12364307Ssos xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 12464307Ssos if (! xdr_callhdr(xdrs, &call_msg)) 12584584Ssos warnx("clntraw_create - Fatal header serialization error."); 12664307Ssos clp->mcnt = XDR_GETPOS(xdrs); 12764307Ssos XDR_DESTROY(xdrs); 12864307Ssos 12975553Ssos /* 13064307Ssos * Set xdrmem for client/server shared buffer 13164307Ssos */ 13264307Ssos xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE); 13364307Ssos 13464307Ssos /* 13564307Ssos * create client handle 13664307Ssos */ 13764479Ssos client->cl_ops = clnt_raw_ops(); 13864307Ssos client->cl_auth = authnone_create(); 13964307Ssos mutex_unlock(&clntraw_lock); 14064307Ssos return (client); 14164307Ssos} 14264307Ssos 14364479Ssos/* ARGSUSED */ 14464479Ssosstatic enum clnt_stat 14564479Ssosclnt_raw_call(h, proc, xargs, argsp, xresults, resultsp, timeout) 14664307Ssos CLIENT *h; 14764307Ssos rpcproc_t proc; 14857391Ssos xdrproc_t xargs; 14985352Ssos void *argsp; 15057391Ssos xdrproc_t xresults; 15157391Ssos void *resultsp; 15257391Ssos struct timeval timeout; 15357391Ssos{ 15457391Ssos struct clntraw_private *clp = clntraw_private; 15557391Ssos XDR *xdrs = &clp->xdr_stream; 15684584Ssos struct rpc_msg msg; 15757391Ssos enum clnt_stat status; 15857391Ssos struct rpc_err error; 15957391Ssos 16075553Ssos assert(h != NULL); 16175553Ssos 16257391Ssos mutex_lock(&clntraw_lock); 16357391Ssos if (clp == NULL) { 16457391Ssos mutex_unlock(&clntraw_lock); 16557391Ssos return (RPC_FAILED); 16657391Ssos } 16757391Ssos mutex_unlock(&clntraw_lock); 16857391Ssos 16957391Ssoscall_again: 17057391Ssos /* 17157391Ssos * send request 17257391Ssos */ 17357391Ssos xdrs->x_op = XDR_ENCODE; 17464479Ssos XDR_SETPOS(xdrs, 0); 17564479Ssos clp->u.mashl_rpcmsg.rm_xid ++ ; 17664479Ssos if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) || 17757391Ssos (! XDR_PUTINT32(xdrs, &proc)) || 17857391Ssos (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 17945095Ssos (! (*xargs)(xdrs, argsp))) { 18085352Ssos return (RPC_CANTENCODEARGS); 18156138Ssos } 18256138Ssos (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 18345095Ssos 18451520Ssos /* 18545095Ssos * We have to call server input routine here because this is 18684584Ssos * all going on in one process. Yuk. 18753029Ssos */ 18851520Ssos svc_getreq_common(FD_SETSIZE); 18975553Ssos 19075553Ssos /* 19153681Ssos * get results 19253681Ssos */ 19353681Ssos xdrs->x_op = XDR_DECODE; 19457325Ssos XDR_SETPOS(xdrs, 0); 19557325Ssos msg.acpted_rply.ar_verf = _null_auth; 19653681Ssos msg.acpted_rply.ar_results.where = resultsp; 19756558Ssos msg.acpted_rply.ar_results.proc = xresults; 19856744Ssos if (! xdr_replymsg(xdrs, &msg)) { 19953681Ssos /* 20045095Ssos * It's possible for xdr_replymsg() to fail partway 20164479Ssos * through its attempt to decode the result from the 20264479Ssos * server. If this happens, it will leave the reply 20364479Ssos * structure partially populated with dynamically 20445095Ssos * allocated memory. (This can happen if someone uses 20545095Ssos * clntudp_bufcreate() to create a CLIENT handle and 20645095Ssos * specifies a receive buffer size that is too small.) 20745095Ssos * This memory must be free()ed to avoid a leak. 20845095Ssos */ 20945095Ssos int op = xdrs->x_op; 21045095Ssos xdrs->x_op = XDR_FREE; 21167058Ssos xdr_replymsg(xdrs, &msg); 21257325Ssos xdrs->x_op = op; 21357325Ssos return (RPC_CANTDECODERES); 21451520Ssos } 21551520Ssos _seterr_reply(&msg, &error); 21651520Ssos status = error.re_status; 21751520Ssos 21851520Ssos if (status == RPC_SUCCESS) { 21951520Ssos if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 22051520Ssos status = RPC_AUTHERROR; 22151520Ssos } 22251520Ssos } /* end successful completion */ 22357325Ssos else { 22457325Ssos if (AUTH_REFRESH(h->cl_auth, &msg)) 22545095Ssos goto call_again; 22684584Ssos } /* end of unsuccessful completion */ 22753029Ssos 22851520Ssos if (status == RPC_SUCCESS) { 22975553Ssos if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 23075553Ssos status = RPC_AUTHERROR; 23153681Ssos } 23253681Ssos if (msg.acpted_rply.ar_verf.oa_base != NULL) { 23353681Ssos xdrs->x_op = XDR_FREE; 23453681Ssos (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 23553681Ssos } 23653681Ssos } 23753681Ssos 23853681Ssos return (status); 23953681Ssos} 24053681Ssos 24153681Ssos/*ARGSUSED*/ 24253681Ssosstatic void 24353681Ssosclnt_raw_geterr(cl, err) 24467058Ssos CLIENT *cl; 24553681Ssos struct rpc_err *err; 24653681Ssos{ 24753681Ssos} 24853681Ssos 24953681Ssos 25057325Ssos/* ARGSUSED */ 25157325Ssosstatic bool_t 25253681Ssosclnt_raw_freeres(cl, xdr_res, res_ptr) 25357325Ssos CLIENT *cl; 25457325Ssos xdrproc_t xdr_res; 25553681Ssos void *res_ptr; 25656558Ssos{ 25756744Ssos struct clntraw_private *clp = clntraw_private; 25845095Ssos XDR *xdrs = &clp->xdr_stream; 25951520Ssos bool_t rval; 26053681Ssos 26145095Ssos mutex_lock(&clntraw_lock); 26245095Ssos if (clp == NULL) { 26345095Ssos rval = (bool_t) RPC_FAILED; 26454544Ssos mutex_unlock(&clntraw_lock); 26554544Ssos return (rval); 26654544Ssos } 26757325Ssos mutex_unlock(&clntraw_lock); 26867058Ssos xdrs->x_op = XDR_FREE; 26954544Ssos return ((*xdr_res)(xdrs, res_ptr)); 27054544Ssos} 27154544Ssos 27254544Ssos/*ARGSUSED*/ 27354544Ssosstatic void 27454544Ssosclnt_raw_abort(cl) 27584584Ssos CLIENT *cl; 27654544Ssos{ 27754544Ssos} 27856558Ssos 27975553Ssos/*ARGSUSED*/ 28056558Ssosstatic bool_t 28154544Ssosclnt_raw_control(cl, ui, str) 28256558Ssos CLIENT *cl; 28356744Ssos u_int ui; 28454544Ssos void *str; 28554544Ssos{ 28645095Ssos return (FALSE); 28745095Ssos} 28852067Ssos 28975271Ssos/*ARGSUSED*/ 29075271Ssosstatic void 29175271Ssosclnt_raw_destroy(cl) 29256558Ssos CLIENT *cl; 29356558Ssos{ 29453029Ssos} 29552067Ssos 29675271Ssosstatic struct clnt_ops * 29784584Ssosclnt_raw_ops() 29875271Ssos{ 29975271Ssos static struct clnt_ops ops; 30075271Ssos 30175553Ssos /* VARIABLES PROTECTED BY ops_lock: ops */ 30275271Ssos 30375271Ssos mutex_lock(&ops_lock); 30475271Ssos if (ops.cl_call == NULL) { 30552067Ssos ops.cl_call = clnt_raw_call; 30675271Ssos ops.cl_abort = clnt_raw_abort; 30775271Ssos ops.cl_geterr = clnt_raw_geterr; 30875271Ssos ops.cl_freeres = clnt_raw_freeres; 30975271Ssos ops.cl_destroy = clnt_raw_destroy; 31075271Ssos ops.cl_control = clnt_raw_control; 31175271Ssos } 31275271Ssos mutex_unlock(&ops_lock); 31375271Ssos return (&ops); 31475271Ssos} 31575271Ssos