Home | History | Annotate | Line # | Download | only in rpc
clnt_raw.c revision 1.29.4.1
      1  1.29.4.1      yamt /*	$NetBSD: clnt_raw.c,v 1.29.4.1 2012/04/17 00:05:22 yamt Exp $	*/
      2       1.3       cgd 
      3       1.1       cgd /*
      4       1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5       1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6       1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7       1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8       1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9       1.1       cgd  * program developed by the user.
     10       1.1       cgd  *
     11       1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12       1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13       1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14       1.1       cgd  *
     15       1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16       1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17       1.1       cgd  * modification or enhancement.
     18       1.1       cgd  *
     19       1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20       1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21       1.1       cgd  * OR ANY PART THEREOF.
     22       1.1       cgd  *
     23       1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24       1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25       1.1       cgd  * Sun has been advised of the possibility of such damages.
     26       1.1       cgd  *
     27       1.1       cgd  * Sun Microsystems, Inc.
     28       1.1       cgd  * 2550 Garcia Avenue
     29       1.1       cgd  * Mountain View, California  94043
     30       1.1       cgd  */
     31       1.1       cgd 
     32       1.4  christos #include <sys/cdefs.h>
     33       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34       1.4  christos #if 0
     35       1.4  christos static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
     36       1.4  christos static char *sccsid = "@(#)clnt_raw.c	2.2 88/08/01 4.0 RPCSRC";
     37       1.4  christos #else
     38  1.29.4.1      yamt __RCSID("$NetBSD: clnt_raw.c,v 1.29.4.1 2012/04/17 00:05:22 yamt Exp $");
     39       1.4  christos #endif
     40       1.1       cgd #endif
     41       1.1       cgd 
     42       1.1       cgd /*
     43       1.1       cgd  * clnt_raw.c
     44       1.1       cgd  *
     45       1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     46       1.1       cgd  *
     47       1.1       cgd  * Memory based rpc for simple testing and timing.
     48       1.1       cgd  * Interface to create an rpc client and server in the same process.
     49       1.1       cgd  * This lets us similate rpc and get round trip overhead, without
     50      1.27       wiz  * any interference from the kernel.
     51       1.1       cgd  */
     52       1.1       cgd 
     53       1.5       jtc #include "namespace.h"
     54      1.17      fvdl #include "reentrant.h"
     55      1.15     lukem #include <assert.h>
     56      1.11     lukem #include <err.h>
     57       1.4  christos #include <stdio.h>
     58       1.2       cgd #include <stdlib.h>
     59      1.11     lukem 
     60       1.1       cgd #include <rpc/rpc.h>
     61      1.17      fvdl #include <rpc/raw.h>
     62       1.5       jtc 
     63       1.5       jtc #ifdef __weak_alias
     64      1.16   mycroft __weak_alias(clntraw_create,_clntraw_create)
     65      1.17      fvdl __weak_alias(clnt_raw_create,_clnt_raw_create)
     66      1.17      fvdl #endif
     67      1.17      fvdl 
     68      1.24   thorpej #ifdef _REENTRANT
     69      1.24   thorpej extern mutex_t clntraw_lock;
     70       1.5       jtc #endif
     71       1.1       cgd 
     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.29.4.1      yamt static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t,
     89  1.29.4.1      yamt     const char *, xdrproc_t, caddr_t, struct timeval);
     90  1.29.4.1      yamt static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
     91  1.29.4.1      yamt static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, caddr_t);
     92  1.29.4.1      yamt static void clnt_raw_abort(CLIENT *);
     93  1.29.4.1      yamt static bool_t clnt_raw_control(CLIENT *, u_int, char *);
     94  1.29.4.1      yamt static void clnt_raw_destroy(CLIENT *);
     95  1.29.4.1      yamt 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.29.4.1      yamt clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
    102       1.1       cgd {
    103      1.11     lukem 	struct clntraw_private *clp = clntraw_private;
    104       1.1       cgd 	struct rpc_msg call_msg;
    105       1.1       cgd 	XDR *xdrs = &clp->xdr_stream;
    106       1.1       cgd 	CLIENT	*client = &clp->client_object;
    107       1.1       cgd 
    108      1.17      fvdl 	mutex_lock(&clntraw_lock);
    109      1.17      fvdl 	if (clp == 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.1       cgd 	/*
    122      1.11     lukem 	 * pre-serialize the static part of the call msg and stash it away
    123       1.1       cgd 	 */
    124       1.1       cgd 	call_msg.rm_direction = CALL;
    125       1.1       cgd 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    126      1.13  christos 	/* XXX: prog and vers have been long historically :-( */
    127      1.13  christos 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
    128      1.13  christos 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
    129      1.13  christos 	xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
    130      1.11     lukem 	if (! xdr_callhdr(xdrs, &call_msg))
    131      1.11     lukem 		warnx("clntraw_create - Fatal header serialization error.");
    132       1.1       cgd 	clp->mcnt = XDR_GETPOS(xdrs);
    133       1.1       cgd 	XDR_DESTROY(xdrs);
    134       1.1       cgd 
    135       1.1       cgd 	/*
    136       1.1       cgd 	 * Set xdrmem for client/server shared buffer
    137       1.1       cgd 	 */
    138       1.1       cgd 	xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
    139       1.1       cgd 
    140       1.1       cgd 	/*
    141       1.1       cgd 	 * create client handle
    142       1.1       cgd 	 */
    143      1.17      fvdl 	client->cl_ops = clnt_raw_ops();
    144       1.1       cgd 	client->cl_auth = authnone_create();
    145      1.17      fvdl 	mutex_unlock(&clntraw_lock);
    146       1.1       cgd 	return (client);
    147      1.29  christos out:
    148      1.29  christos 	if (clp)
    149      1.29  christos 		free(clp);
    150      1.29  christos 	mutex_unlock(&clntraw_lock);
    151      1.29  christos 	return NULL;
    152      1.29  christos 
    153       1.1       cgd }
    154       1.1       cgd 
    155      1.13  christos /* ARGSUSED */
    156       1.1       cgd static enum clnt_stat
    157  1.29.4.1      yamt clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, const char *argsp,
    158  1.29.4.1      yamt 	xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
    159       1.1       cgd {
    160      1.11     lukem 	struct clntraw_private *clp = clntraw_private;
    161      1.11     lukem 	XDR *xdrs = &clp->xdr_stream;
    162       1.1       cgd 	struct rpc_msg msg;
    163       1.1       cgd 	enum clnt_stat status;
    164       1.1       cgd 	struct rpc_err error;
    165      1.15     lukem 
    166      1.15     lukem 	_DIAGASSERT(h != NULL);
    167       1.1       cgd 
    168      1.17      fvdl 	mutex_lock(&clntraw_lock);
    169      1.17      fvdl 	if (clp == NULL) {
    170      1.17      fvdl 		mutex_unlock(&clntraw_lock);
    171       1.1       cgd 		return (RPC_FAILED);
    172      1.17      fvdl 	}
    173      1.17      fvdl 	mutex_unlock(&clntraw_lock);
    174      1.17      fvdl 
    175       1.1       cgd call_again:
    176       1.1       cgd 	/*
    177       1.1       cgd 	 * send request
    178       1.1       cgd 	 */
    179       1.1       cgd 	xdrs->x_op = XDR_ENCODE;
    180       1.1       cgd 	XDR_SETPOS(xdrs, 0);
    181      1.13  christos 	clp->u.mashl_rpcmsg.rm_xid ++ ;
    182      1.13  christos 	if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
    183      1.22  christos 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
    184       1.1       cgd 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
    185      1.28      yamt 	    (! (*xargs)(xdrs, __UNCONST(argsp)))) {
    186       1.1       cgd 		return (RPC_CANTENCODEARGS);
    187       1.1       cgd 	}
    188       1.1       cgd 	(void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
    189       1.1       cgd 
    190       1.1       cgd 	/*
    191       1.1       cgd 	 * We have to call server input routine here because this is
    192       1.1       cgd 	 * all going on in one process. Yuk.
    193       1.1       cgd 	 */
    194      1.17      fvdl 	svc_getreq_common(FD_SETSIZE);
    195       1.1       cgd 
    196       1.1       cgd 	/*
    197       1.1       cgd 	 * get results
    198       1.1       cgd 	 */
    199       1.1       cgd 	xdrs->x_op = XDR_DECODE;
    200       1.1       cgd 	XDR_SETPOS(xdrs, 0);
    201       1.1       cgd 	msg.acpted_rply.ar_verf = _null_auth;
    202       1.1       cgd 	msg.acpted_rply.ar_results.where = resultsp;
    203       1.1       cgd 	msg.acpted_rply.ar_results.proc = xresults;
    204       1.6     lukem 	if (! xdr_replymsg(xdrs, &msg)) {
    205       1.6     lukem 		/*
    206       1.6     lukem 		 * It's possible for xdr_replymsg() to fail partway
    207       1.6     lukem 		 * through its attempt to decode the result from the
    208       1.6     lukem 		 * server. If this happens, it will leave the reply
    209       1.6     lukem 		 * structure partially populated with dynamically
    210       1.6     lukem 		 * allocated memory. (This can happen if someone uses
    211       1.6     lukem 		 * clntudp_bufcreate() to create a CLIENT handle and
    212       1.6     lukem 		 * specifies a receive buffer size that is too small.)
    213       1.6     lukem 		 * This memory must be free()ed to avoid a leak.
    214       1.6     lukem 		 */
    215       1.6     lukem 		int op = xdrs->x_op;
    216       1.6     lukem 		xdrs->x_op = XDR_FREE;
    217       1.6     lukem 		xdr_replymsg(xdrs, &msg);
    218       1.6     lukem 		xdrs->x_op = op;
    219       1.1       cgd 		return (RPC_CANTDECODERES);
    220       1.6     lukem 	}
    221       1.1       cgd 	_seterr_reply(&msg, &error);
    222       1.1       cgd 	status = error.re_status;
    223       1.1       cgd 
    224       1.1       cgd 	if (status == RPC_SUCCESS) {
    225       1.1       cgd 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
    226       1.1       cgd 			status = RPC_AUTHERROR;
    227       1.1       cgd 		}
    228       1.1       cgd 	}  /* end successful completion */
    229       1.1       cgd 	else {
    230       1.1       cgd 		if (AUTH_REFRESH(h->cl_auth))
    231       1.1       cgd 			goto call_again;
    232       1.1       cgd 	}  /* end of unsuccessful completion */
    233       1.1       cgd 
    234       1.1       cgd 	if (status == RPC_SUCCESS) {
    235       1.1       cgd 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
    236       1.1       cgd 			status = RPC_AUTHERROR;
    237       1.1       cgd 		}
    238       1.1       cgd 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
    239       1.1       cgd 			xdrs->x_op = XDR_FREE;
    240       1.1       cgd 			(void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
    241       1.1       cgd 		}
    242       1.1       cgd 	}
    243       1.1       cgd 
    244       1.1       cgd 	return (status);
    245       1.1       cgd }
    246       1.1       cgd 
    247       1.4  christos /*ARGSUSED*/
    248       1.1       cgd static void
    249  1.29.4.1      yamt clnt_raw_geterr(CLIENT *cl, struct rpc_err *error)
    250       1.1       cgd {
    251       1.1       cgd }
    252       1.1       cgd 
    253       1.1       cgd 
    254      1.13  christos /* ARGSUSED */
    255       1.1       cgd static bool_t
    256  1.29.4.1      yamt clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
    257       1.1       cgd {
    258      1.11     lukem 	struct clntraw_private *clp = clntraw_private;
    259      1.11     lukem 	XDR *xdrs = &clp->xdr_stream;
    260       1.1       cgd 	bool_t rval;
    261       1.1       cgd 
    262      1.17      fvdl 	mutex_lock(&clntraw_lock);
    263      1.17      fvdl 	if (clp == NULL) {
    264       1.1       cgd 		rval = (bool_t) RPC_FAILED;
    265      1.17      fvdl 		mutex_unlock(&clntraw_lock);
    266       1.1       cgd 		return (rval);
    267       1.1       cgd 	}
    268      1.17      fvdl 	mutex_unlock(&clntraw_lock);
    269       1.1       cgd 	xdrs->x_op = XDR_FREE;
    270       1.1       cgd 	return ((*xdr_res)(xdrs, res_ptr));
    271       1.1       cgd }
    272       1.1       cgd 
    273       1.4  christos /*ARGSUSED*/
    274       1.1       cgd static void
    275  1.29.4.1      yamt clnt_raw_abort(CLIENT *cl)
    276       1.1       cgd {
    277       1.1       cgd }
    278       1.1       cgd 
    279       1.4  christos /*ARGSUSED*/
    280       1.1       cgd static bool_t
    281  1.29.4.1      yamt clnt_raw_control(CLIENT *cl, u_int ui, char *str)
    282       1.1       cgd {
    283       1.1       cgd 	return (FALSE);
    284       1.1       cgd }
    285       1.1       cgd 
    286       1.4  christos /*ARGSUSED*/
    287       1.1       cgd static void
    288  1.29.4.1      yamt clnt_raw_destroy(CLIENT *cl)
    289       1.1       cgd {
    290      1.17      fvdl }
    291      1.17      fvdl 
    292      1.17      fvdl static struct clnt_ops *
    293  1.29.4.1      yamt clnt_raw_ops(void)
    294      1.17      fvdl {
    295      1.17      fvdl 	static struct clnt_ops ops;
    296      1.24   thorpej #ifdef _REENTRANT
    297      1.17      fvdl 	extern mutex_t  ops_lock;
    298      1.17      fvdl #endif
    299      1.17      fvdl 
    300      1.17      fvdl 	/* VARIABLES PROTECTED BY ops_lock: ops */
    301      1.17      fvdl 
    302      1.17      fvdl 	mutex_lock(&ops_lock);
    303      1.17      fvdl 	if (ops.cl_call == NULL) {
    304      1.17      fvdl 		ops.cl_call = clnt_raw_call;
    305      1.17      fvdl 		ops.cl_abort = clnt_raw_abort;
    306      1.17      fvdl 		ops.cl_geterr = clnt_raw_geterr;
    307      1.17      fvdl 		ops.cl_freeres = clnt_raw_freeres;
    308      1.17      fvdl 		ops.cl_destroy = clnt_raw_destroy;
    309      1.17      fvdl 		ops.cl_control = clnt_raw_control;
    310      1.17      fvdl 	}
    311      1.17      fvdl 	mutex_unlock(&ops_lock);
    312      1.17      fvdl 	return (&ops);
    313       1.1       cgd }
    314