Home | History | Annotate | Line # | Download | only in rpc
clnt_raw.c revision 1.10
      1 /*	$NetBSD: clnt_raw.c,v 1.10 1998/02/12 01:57:31 lukem Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
     36 static char *sccsid = "@(#)clnt_raw.c	2.2 88/08/01 4.0 RPCSRC";
     37 #else
     38 __RCSID("$NetBSD: clnt_raw.c,v 1.10 1998/02/12 01:57:31 lukem Exp $");
     39 #endif
     40 #endif
     41 
     42 /*
     43  * clnt_raw.c
     44  *
     45  * Copyright (C) 1984, Sun Microsystems, Inc.
     46  *
     47  * Memory based rpc for simple testing and timing.
     48  * Interface to create an rpc client and server in the same process.
     49  * This lets us similate rpc and get round trip overhead, without
     50  * any interference from the kernal.
     51  */
     52 
     53 #include "namespace.h"
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <rpc/rpc.h>
     57 
     58 #ifdef __weak_alias
     59 __weak_alias(clntraw_create,_clntraw_create);
     60 #endif
     61 
     62 #define MCALL_MSG_SIZE 24
     63 
     64 /*
     65  * This is the "network" we will be moving stuff over.
     66  */
     67 static struct clntraw_private {
     68 	CLIENT	client_object;
     69 	XDR	xdr_stream;
     70 	char	_raw_buf[UDPMSGSIZE];
     71 	char	mashl_callmsg[MCALL_MSG_SIZE];
     72 	u_int	mcnt;
     73 } *clntraw_private;
     74 
     75 
     76 
     77 static enum clnt_stat clntraw_call __P((CLIENT *, u_long, xdrproc_t,
     78     caddr_t, xdrproc_t, caddr_t, struct timeval));
     79 static void clntraw_geterr __P((CLIENT *, struct rpc_err *));
     80 static bool_t clntraw_freeres __P((CLIENT *, xdrproc_t, caddr_t));
     81 static void clntraw_abort __P((CLIENT *));
     82 static bool_t clntraw_control __P((CLIENT *, u_int, char *));
     83 static void clntraw_destroy __P((CLIENT *));
     84 
     85 static struct clnt_ops client_ops = {
     86 	clntraw_call,
     87 	clntraw_abort,
     88 	clntraw_geterr,
     89 	clntraw_freeres,
     90 	clntraw_destroy,
     91 	clntraw_control
     92 };
     93 
     94 /*
     95  * Create a client handle for memory based rpc.
     96  */
     97 CLIENT *
     98 clntraw_create(prog, vers)
     99 	u_long prog;
    100 	u_long vers;
    101 {
    102 	register struct clntraw_private *clp = clntraw_private;
    103 	struct rpc_msg call_msg;
    104 	XDR *xdrs = &clp->xdr_stream;
    105 	CLIENT	*client = &clp->client_object;
    106 
    107 	if (clp == 0) {
    108 		clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
    109 		if (clp == 0)
    110 			return (0);
    111 		clntraw_private = clp;
    112 	}
    113 	/*
    114 	 * pre-serialize the staic part of the call msg and stash it away
    115 	 */
    116 	call_msg.rm_direction = CALL;
    117 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    118 	call_msg.rm_call.cb_prog = prog;
    119 	call_msg.rm_call.cb_vers = vers;
    120 	xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
    121 	if (! xdr_callhdr(xdrs, &call_msg)) {
    122 		perror("clnt_raw.c - Fatal header serialization error.");
    123 	}
    124 	clp->mcnt = XDR_GETPOS(xdrs);
    125 	XDR_DESTROY(xdrs);
    126 
    127 	/*
    128 	 * Set xdrmem for client/server shared buffer
    129 	 */
    130 	xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
    131 
    132 	/*
    133 	 * create client handle
    134 	 */
    135 	client->cl_ops = &client_ops;
    136 	client->cl_auth = authnone_create();
    137 	return (client);
    138 }
    139 
    140 static enum clnt_stat
    141 clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
    142 	CLIENT *h;
    143 	u_long proc;
    144 	xdrproc_t xargs;
    145 	caddr_t argsp;
    146 	xdrproc_t xresults;
    147 	caddr_t resultsp;
    148 	struct timeval timeout;
    149 {
    150 	register struct clntraw_private *clp = clntraw_private;
    151 	register XDR *xdrs = &clp->xdr_stream;
    152 	struct rpc_msg msg;
    153 	enum clnt_stat status;
    154 	struct rpc_err error;
    155 
    156 	if (clp == 0)
    157 		return (RPC_FAILED);
    158 call_again:
    159 	/*
    160 	 * send request
    161 	 */
    162 	xdrs->x_op = XDR_ENCODE;
    163 	XDR_SETPOS(xdrs, 0);
    164 	((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ;
    165 	if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
    166 	    (! XDR_PUTLONG(xdrs, &proc)) ||
    167 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
    168 	    (! (*xargs)(xdrs, argsp))) {
    169 		return (RPC_CANTENCODEARGS);
    170 	}
    171 	(void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
    172 
    173 	/*
    174 	 * We have to call server input routine here because this is
    175 	 * all going on in one process. Yuk.
    176 	 */
    177 	svc_getreq(1);
    178 
    179 	/*
    180 	 * get results
    181 	 */
    182 	xdrs->x_op = XDR_DECODE;
    183 	XDR_SETPOS(xdrs, 0);
    184 	msg.acpted_rply.ar_verf = _null_auth;
    185 	msg.acpted_rply.ar_results.where = resultsp;
    186 	msg.acpted_rply.ar_results.proc = xresults;
    187 	if (! xdr_replymsg(xdrs, &msg)) {
    188 		/*
    189 		 * It's possible for xdr_replymsg() to fail partway
    190 		 * through its attempt to decode the result from the
    191 		 * server. If this happens, it will leave the reply
    192 		 * structure partially populated with dynamically
    193 		 * allocated memory. (This can happen if someone uses
    194 		 * clntudp_bufcreate() to create a CLIENT handle and
    195 		 * specifies a receive buffer size that is too small.)
    196 		 * This memory must be free()ed to avoid a leak.
    197 		 */
    198 		int op = xdrs->x_op;
    199 		xdrs->x_op = XDR_FREE;
    200 		xdr_replymsg(xdrs, &msg);
    201 		xdrs->x_op = op;
    202 		return (RPC_CANTDECODERES);
    203 	}
    204 	_seterr_reply(&msg, &error);
    205 	status = error.re_status;
    206 
    207 	if (status == RPC_SUCCESS) {
    208 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
    209 			status = RPC_AUTHERROR;
    210 		}
    211 	}  /* end successful completion */
    212 	else {
    213 		if (AUTH_REFRESH(h->cl_auth))
    214 			goto call_again;
    215 	}  /* end of unsuccessful completion */
    216 
    217 	if (status == RPC_SUCCESS) {
    218 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
    219 			status = RPC_AUTHERROR;
    220 		}
    221 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
    222 			xdrs->x_op = XDR_FREE;
    223 			(void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
    224 		}
    225 	}
    226 
    227 	return (status);
    228 }
    229 
    230 /*ARGSUSED*/
    231 static void
    232 clntraw_geterr(cl, err)
    233 	CLIENT *cl;
    234 	struct rpc_err *err;
    235 {
    236 }
    237 
    238 
    239 static bool_t
    240 clntraw_freeres(cl, xdr_res, res_ptr)
    241 	CLIENT *cl;
    242 	xdrproc_t xdr_res;
    243 	caddr_t res_ptr;
    244 {
    245 	register struct clntraw_private *clp = clntraw_private;
    246 	register XDR *xdrs = &clp->xdr_stream;
    247 	bool_t rval;
    248 
    249 	if (clp == 0)
    250 	{
    251 		rval = (bool_t) RPC_FAILED;
    252 		return (rval);
    253 	}
    254 	xdrs->x_op = XDR_FREE;
    255 	return ((*xdr_res)(xdrs, res_ptr));
    256 }
    257 
    258 /*ARGSUSED*/
    259 static void
    260 clntraw_abort(cl)
    261 	CLIENT *cl;
    262 {
    263 }
    264 
    265 /*ARGSUSED*/
    266 static bool_t
    267 clntraw_control(cl, ui, str)
    268 	CLIENT *cl;
    269 	u_int ui;
    270 	char *str;
    271 {
    272 	return (FALSE);
    273 }
    274 
    275 /*ARGSUSED*/
    276 static void
    277 clntraw_destroy(cl)
    278 	CLIENT *cl;
    279 {
    280 }
    281