Home | History | Annotate | Line # | Download | only in rpc
clnt_raw.c revision 1.34
      1  1.34  christos /*	$NetBSD: clnt_raw.c,v 1.34 2024/01/23 17:24:38 christos Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*
      4  1.32      tron  * Copyright (c) 2010, Oracle America, Inc.
      5   1.1       cgd  *
      6  1.32      tron  * Redistribution and use in source and binary forms, with or without
      7  1.32      tron  * modification, are permitted provided that the following conditions are
      8  1.32      tron  * met:
      9  1.32      tron  *
     10  1.32      tron  *     * Redistributions of source code must retain the above copyright
     11  1.32      tron  *       notice, this list of conditions and the following disclaimer.
     12  1.32      tron  *     * Redistributions in binary form must reproduce the above
     13  1.32      tron  *       copyright notice, this list of conditions and the following
     14  1.32      tron  *       disclaimer in the documentation and/or other materials
     15  1.32      tron  *       provided with the distribution.
     16  1.32      tron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.32      tron  *       contributors may be used to endorse or promote products derived
     18  1.32      tron  *       from this software without specific prior written permission.
     19  1.32      tron  *
     20  1.32      tron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.32      tron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.32      tron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.32      tron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.32      tron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.32      tron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.32      tron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.32      tron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.32      tron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.32      tron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.32      tron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.32      tron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34   1.4  christos #include <sys/cdefs.h>
     35   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     36   1.4  christos #if 0
     37   1.4  christos static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
     38   1.4  christos static char *sccsid = "@(#)clnt_raw.c	2.2 88/08/01 4.0 RPCSRC";
     39   1.4  christos #else
     40  1.34  christos __RCSID("$NetBSD: clnt_raw.c,v 1.34 2024/01/23 17:24:38 christos Exp $");
     41   1.4  christos #endif
     42   1.1       cgd #endif
     43   1.1       cgd 
     44   1.1       cgd /*
     45   1.1       cgd  * clnt_raw.c
     46   1.1       cgd  *
     47   1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     48   1.1       cgd  *
     49   1.1       cgd  * Memory based rpc for simple testing and timing.
     50   1.1       cgd  * Interface to create an rpc client and server in the same process.
     51   1.1       cgd  * This lets us similate rpc and get round trip overhead, without
     52  1.27       wiz  * any interference from the kernel.
     53   1.1       cgd  */
     54   1.1       cgd 
     55   1.5       jtc #include "namespace.h"
     56  1.17      fvdl #include "reentrant.h"
     57  1.15     lukem #include <assert.h>
     58  1.11     lukem #include <err.h>
     59   1.4  christos #include <stdio.h>
     60   1.2       cgd #include <stdlib.h>
     61  1.11     lukem 
     62   1.1       cgd #include <rpc/rpc.h>
     63  1.17      fvdl #include <rpc/raw.h>
     64   1.5       jtc 
     65  1.34  christos #include "rpc_internal.h"
     66  1.34  christos 
     67   1.5       jtc #ifdef __weak_alias
     68  1.16   mycroft __weak_alias(clntraw_create,_clntraw_create)
     69  1.17      fvdl __weak_alias(clnt_raw_create,_clnt_raw_create)
     70  1.17      fvdl #endif
     71  1.17      fvdl 
     72   1.1       cgd #define MCALL_MSG_SIZE 24
     73   1.1       cgd 
     74   1.1       cgd /*
     75   1.1       cgd  * This is the "network" we will be moving stuff over.
     76   1.1       cgd  */
     77   1.1       cgd static struct clntraw_private {
     78  1.10     lukem 	CLIENT	client_object;
     79  1.10     lukem 	XDR	xdr_stream;
     80  1.17      fvdl 	char	*_raw_buf;
     81  1.13  christos 	union {
     82  1.13  christos 	    struct rpc_msg	mashl_rpcmsg;
     83  1.13  christos 	    char 		mashl_callmsg[MCALL_MSG_SIZE];
     84  1.13  christos 	} u;
     85  1.10     lukem 	u_int	mcnt;
     86   1.1       cgd } *clntraw_private;
     87   1.4  christos 
     88  1.30      matt static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t,
     89  1.30      matt     const char *, xdrproc_t, caddr_t, struct timeval);
     90  1.30      matt static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
     91  1.30      matt static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, caddr_t);
     92  1.30      matt static void clnt_raw_abort(CLIENT *);
     93  1.30      matt static bool_t clnt_raw_control(CLIENT *, u_int, char *);
     94  1.30      matt static void clnt_raw_destroy(CLIENT *);
     95  1.30      matt static struct clnt_ops *clnt_raw_ops(void);
     96   1.1       cgd 
     97   1.1       cgd /*
     98   1.1       cgd  * Create a client handle for memory based rpc.
     99   1.1       cgd  */
    100   1.1       cgd CLIENT *
    101  1.30      matt clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
    102   1.1       cgd {
    103  1.33  christos 	struct clntraw_private *clp;
    104   1.1       cgd 	struct rpc_msg call_msg;
    105  1.33  christos 	XDR *xdrs;
    106  1.33  christos 	CLIENT *client;
    107   1.1       cgd 
    108  1.17      fvdl 	mutex_lock(&clntraw_lock);
    109  1.33  christos 	if ((clp = clntraw_private) == NULL) {
    110  1.25  christos 		clp = calloc((size_t)1, sizeof (*clp));
    111  1.29  christos 		if (clp == NULL)
    112  1.29  christos 			goto out;
    113  1.17      fvdl 		if (__rpc_rawcombuf == NULL)
    114  1.17      fvdl 			__rpc_rawcombuf =
    115  1.26      yamt 			    malloc(UDPMSGSIZE);
    116  1.29  christos 		if (__rpc_rawcombuf == NULL)
    117  1.29  christos 			goto out;
    118  1.17      fvdl 		clp->_raw_buf = __rpc_rawcombuf;
    119   1.1       cgd 		clntraw_private = clp;
    120   1.1       cgd 	}
    121  1.33  christos 
    122  1.33  christos 	xdrs = &clp->xdr_stream;
    123  1.33  christos 	client = &clp->client_object;
    124  1.33  christos 
    125   1.1       cgd 	/*
    126  1.11     lukem 	 * pre-serialize the static part of the call msg and stash it away
    127   1.1       cgd 	 */
    128   1.1       cgd 	call_msg.rm_direction = CALL;
    129   1.1       cgd 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    130  1.13  christos 	/* XXX: prog and vers have been long historically :-( */
    131  1.13  christos 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
    132  1.13  christos 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
    133  1.13  christos 	xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
    134  1.11     lukem 	if (! xdr_callhdr(xdrs, &call_msg))
    135  1.31  christos 		warnx("%s: Fatal header serialization error", __func__);
    136   1.1       cgd 	clp->mcnt = XDR_GETPOS(xdrs);
    137   1.1       cgd 	XDR_DESTROY(xdrs);
    138   1.1       cgd 
    139   1.1       cgd 	/*
    140   1.1       cgd 	 * Set xdrmem for client/server shared buffer
    141   1.1       cgd 	 */
    142   1.1       cgd 	xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
    143   1.1       cgd 
    144   1.1       cgd 	/*
    145   1.1       cgd 	 * create client handle
    146   1.1       cgd 	 */
    147  1.17      fvdl 	client->cl_ops = clnt_raw_ops();
    148   1.1       cgd 	client->cl_auth = authnone_create();
    149  1.17      fvdl 	mutex_unlock(&clntraw_lock);
    150   1.1       cgd 	return (client);
    151  1.29  christos out:
    152  1.29  christos 	if (clp)
    153  1.29  christos 		free(clp);
    154  1.29  christos 	mutex_unlock(&clntraw_lock);
    155  1.29  christos 	return NULL;
    156  1.29  christos 
    157   1.1       cgd }
    158   1.1       cgd 
    159  1.13  christos /* ARGSUSED */
    160   1.1       cgd static enum clnt_stat
    161  1.30      matt clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, const char *argsp,
    162  1.30      matt 	xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
    163   1.1       cgd {
    164  1.11     lukem 	struct clntraw_private *clp = clntraw_private;
    165  1.11     lukem 	XDR *xdrs = &clp->xdr_stream;
    166   1.1       cgd 	struct rpc_msg msg;
    167   1.1       cgd 	enum clnt_stat status;
    168   1.1       cgd 	struct rpc_err error;
    169  1.15     lukem 
    170  1.15     lukem 	_DIAGASSERT(h != NULL);
    171   1.1       cgd 
    172  1.17      fvdl 	mutex_lock(&clntraw_lock);
    173  1.17      fvdl 	if (clp == NULL) {
    174  1.17      fvdl 		mutex_unlock(&clntraw_lock);
    175   1.1       cgd 		return (RPC_FAILED);
    176  1.17      fvdl 	}
    177  1.17      fvdl 	mutex_unlock(&clntraw_lock);
    178  1.17      fvdl 
    179   1.1       cgd call_again:
    180   1.1       cgd 	/*
    181   1.1       cgd 	 * send request
    182   1.1       cgd 	 */
    183   1.1       cgd 	xdrs->x_op = XDR_ENCODE;
    184   1.1       cgd 	XDR_SETPOS(xdrs, 0);
    185  1.13  christos 	clp->u.mashl_rpcmsg.rm_xid ++ ;
    186  1.13  christos 	if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
    187  1.22  christos 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
    188   1.1       cgd 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
    189  1.28      yamt 	    (! (*xargs)(xdrs, __UNCONST(argsp)))) {
    190   1.1       cgd 		return (RPC_CANTENCODEARGS);
    191   1.1       cgd 	}
    192   1.1       cgd 	(void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
    193   1.1       cgd 
    194   1.1       cgd 	/*
    195   1.1       cgd 	 * We have to call server input routine here because this is
    196   1.1       cgd 	 * all going on in one process. Yuk.
    197   1.1       cgd 	 */
    198  1.33  christos 	svc_getreq_common(-1);
    199   1.1       cgd 
    200   1.1       cgd 	/*
    201   1.1       cgd 	 * get results
    202   1.1       cgd 	 */
    203   1.1       cgd 	xdrs->x_op = XDR_DECODE;
    204   1.1       cgd 	XDR_SETPOS(xdrs, 0);
    205   1.1       cgd 	msg.acpted_rply.ar_verf = _null_auth;
    206   1.1       cgd 	msg.acpted_rply.ar_results.where = resultsp;
    207   1.1       cgd 	msg.acpted_rply.ar_results.proc = xresults;
    208   1.6     lukem 	if (! xdr_replymsg(xdrs, &msg)) {
    209   1.6     lukem 		/*
    210   1.6     lukem 		 * It's possible for xdr_replymsg() to fail partway
    211   1.6     lukem 		 * through its attempt to decode the result from the
    212   1.6     lukem 		 * server. If this happens, it will leave the reply
    213   1.6     lukem 		 * structure partially populated with dynamically
    214   1.6     lukem 		 * allocated memory. (This can happen if someone uses
    215   1.6     lukem 		 * clntudp_bufcreate() to create a CLIENT handle and
    216   1.6     lukem 		 * specifies a receive buffer size that is too small.)
    217   1.6     lukem 		 * This memory must be free()ed to avoid a leak.
    218   1.6     lukem 		 */
    219   1.6     lukem 		int op = xdrs->x_op;
    220   1.6     lukem 		xdrs->x_op = XDR_FREE;
    221   1.6     lukem 		xdr_replymsg(xdrs, &msg);
    222   1.6     lukem 		xdrs->x_op = op;
    223   1.1       cgd 		return (RPC_CANTDECODERES);
    224   1.6     lukem 	}
    225   1.1       cgd 	_seterr_reply(&msg, &error);
    226   1.1       cgd 	status = error.re_status;
    227   1.1       cgd 
    228   1.1       cgd 	if (status == RPC_SUCCESS) {
    229   1.1       cgd 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
    230   1.1       cgd 			status = RPC_AUTHERROR;
    231   1.1       cgd 		}
    232   1.1       cgd 	}  /* end successful completion */
    233   1.1       cgd 	else {
    234   1.1       cgd 		if (AUTH_REFRESH(h->cl_auth))
    235   1.1       cgd 			goto call_again;
    236   1.1       cgd 	}  /* end of unsuccessful completion */
    237   1.1       cgd 
    238   1.1       cgd 	if (status == RPC_SUCCESS) {
    239   1.1       cgd 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
    240   1.1       cgd 			status = RPC_AUTHERROR;
    241   1.1       cgd 		}
    242   1.1       cgd 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
    243   1.1       cgd 			xdrs->x_op = XDR_FREE;
    244   1.1       cgd 			(void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
    245   1.1       cgd 		}
    246   1.1       cgd 	}
    247   1.1       cgd 
    248   1.1       cgd 	return (status);
    249   1.1       cgd }
    250   1.1       cgd 
    251   1.4  christos /*ARGSUSED*/
    252   1.1       cgd static void
    253  1.30      matt clnt_raw_geterr(CLIENT *cl, struct rpc_err *error)
    254   1.1       cgd {
    255   1.1       cgd }
    256   1.1       cgd 
    257   1.1       cgd 
    258  1.13  christos /* ARGSUSED */
    259   1.1       cgd static bool_t
    260  1.30      matt clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
    261   1.1       cgd {
    262  1.11     lukem 	struct clntraw_private *clp = clntraw_private;
    263  1.11     lukem 	XDR *xdrs = &clp->xdr_stream;
    264   1.1       cgd 	bool_t rval;
    265   1.1       cgd 
    266  1.17      fvdl 	mutex_lock(&clntraw_lock);
    267  1.17      fvdl 	if (clp == NULL) {
    268   1.1       cgd 		rval = (bool_t) RPC_FAILED;
    269  1.17      fvdl 		mutex_unlock(&clntraw_lock);
    270   1.1       cgd 		return (rval);
    271   1.1       cgd 	}
    272  1.17      fvdl 	mutex_unlock(&clntraw_lock);
    273   1.1       cgd 	xdrs->x_op = XDR_FREE;
    274   1.1       cgd 	return ((*xdr_res)(xdrs, res_ptr));
    275   1.1       cgd }
    276   1.1       cgd 
    277   1.4  christos /*ARGSUSED*/
    278   1.1       cgd static void
    279  1.30      matt clnt_raw_abort(CLIENT *cl)
    280   1.1       cgd {
    281   1.1       cgd }
    282   1.1       cgd 
    283   1.4  christos /*ARGSUSED*/
    284   1.1       cgd static bool_t
    285  1.30      matt clnt_raw_control(CLIENT *cl, u_int ui, char *str)
    286   1.1       cgd {
    287   1.1       cgd 	return (FALSE);
    288   1.1       cgd }
    289   1.1       cgd 
    290   1.4  christos /*ARGSUSED*/
    291   1.1       cgd static void
    292  1.30      matt clnt_raw_destroy(CLIENT *cl)
    293   1.1       cgd {
    294  1.17      fvdl }
    295  1.17      fvdl 
    296  1.17      fvdl static struct clnt_ops *
    297  1.30      matt clnt_raw_ops(void)
    298  1.17      fvdl {
    299  1.17      fvdl 	static struct clnt_ops ops;
    300  1.17      fvdl 
    301  1.17      fvdl 	/* VARIABLES PROTECTED BY ops_lock: ops */
    302  1.17      fvdl 
    303  1.17      fvdl 	mutex_lock(&ops_lock);
    304  1.17      fvdl 	if (ops.cl_call == NULL) {
    305  1.17      fvdl 		ops.cl_call = clnt_raw_call;
    306  1.17      fvdl 		ops.cl_abort = clnt_raw_abort;
    307  1.17      fvdl 		ops.cl_geterr = clnt_raw_geterr;
    308  1.17      fvdl 		ops.cl_freeres = clnt_raw_freeres;
    309  1.17      fvdl 		ops.cl_destroy = clnt_raw_destroy;
    310  1.17      fvdl 		ops.cl_control = clnt_raw_control;
    311  1.17      fvdl 	}
    312  1.17      fvdl 	mutex_unlock(&ops_lock);
    313  1.17      fvdl 	return (&ops);
    314   1.1       cgd }
    315