xref: /freebsd/lib/libc/rpc/clnt_raw.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
18360efbdSAlfred Perlstein /*	$NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 christos Exp $	*/
28360efbdSAlfred Perlstein 
32e322d37SHiroki Sato /*-
4*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni  *
62e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato  * All rights reserved.
899064799SGarrett Wollman  *
92e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
132e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
162e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato  *   from this software without specific prior written permission.
1999064799SGarrett Wollman  *
202e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
3199064799SGarrett Wollman  */
3299064799SGarrett Wollman 
3399064799SGarrett Wollman /*
3499064799SGarrett Wollman  * clnt_raw.c
3599064799SGarrett Wollman  *
3699064799SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
3799064799SGarrett Wollman  *
3899064799SGarrett Wollman  * Memory based rpc for simple testing and timing.
3999064799SGarrett Wollman  * Interface to create an rpc client and server in the same process.
4099064799SGarrett Wollman  * This lets us similate rpc and get round trip overhead, without
415814ff29SAlfred Perlstein  * any interference from the kernel.
4299064799SGarrett Wollman  */
4399064799SGarrett Wollman 
448360efbdSAlfred Perlstein #include "namespace.h"
459f5afc13SIan Dowse #include "reentrant.h"
468360efbdSAlfred Perlstein #include <assert.h>
478360efbdSAlfred Perlstein #include <err.h>
48c124f3bdSJames Raynard #include <stdio.h>
498360efbdSAlfred Perlstein #include <stdlib.h>
508360efbdSAlfred Perlstein 
518360efbdSAlfred Perlstein #include <rpc/rpc.h>
528360efbdSAlfred Perlstein #include <rpc/raw.h>
538360efbdSAlfred Perlstein #include "un-namespace.h"
54235baf26SDaniel Eischen #include "mt_misc.h"
5599064799SGarrett Wollman 
5699064799SGarrett Wollman #define MCALL_MSG_SIZE 24
5799064799SGarrett Wollman 
5899064799SGarrett Wollman /*
5999064799SGarrett Wollman  * This is the "network" we will be moving stuff over.
6099064799SGarrett Wollman  */
6199064799SGarrett Wollman static struct clntraw_private {
6299064799SGarrett Wollman 	CLIENT	client_object;
6399064799SGarrett Wollman 	XDR	xdr_stream;
648360efbdSAlfred Perlstein 	char	*_raw_buf;
658360efbdSAlfred Perlstein 	union {
668360efbdSAlfred Perlstein 	    struct rpc_msg	mashl_rpcmsg;
6799064799SGarrett Wollman 	    char 		mashl_callmsg[MCALL_MSG_SIZE];
688360efbdSAlfred Perlstein 	} u;
6999064799SGarrett Wollman 	u_int	mcnt;
7099064799SGarrett Wollman } *clntraw_private;
7199064799SGarrett Wollman 
72f249dbccSDag-Erling Smørgrav static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
73f249dbccSDag-Erling Smørgrav 	xdrproc_t, void *, struct timeval);
74c05ac53bSDavid E. O'Brien static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
75f249dbccSDag-Erling Smørgrav static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, void *);
76c05ac53bSDavid E. O'Brien static void clnt_raw_abort(CLIENT *);
77cffc0b57SAlfred Perlstein static bool_t clnt_raw_control(CLIENT *, u_int, void *);
78c05ac53bSDavid E. O'Brien static void clnt_raw_destroy(CLIENT *);
79c05ac53bSDavid E. O'Brien static struct clnt_ops *clnt_raw_ops(void);
8099064799SGarrett Wollman 
8199064799SGarrett Wollman /*
8299064799SGarrett Wollman  * Create a client handle for memory based rpc.
8399064799SGarrett Wollman  */
8499064799SGarrett Wollman CLIENT *
clnt_raw_create(rpcprog_t prog,rpcvers_t vers)85587cf682SCraig Rodrigues clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
8699064799SGarrett Wollman {
87bedffadcSGarrett Wollman 	struct clntraw_private *clp;
8899064799SGarrett Wollman 	struct rpc_msg call_msg;
89bedffadcSGarrett Wollman 	XDR *xdrs;
90bedffadcSGarrett Wollman 	CLIENT	*client;
9199064799SGarrett Wollman 
928360efbdSAlfred Perlstein 	mutex_lock(&clntraw_lock);
93bedffadcSGarrett Wollman 	if ((clp = clntraw_private) == NULL) {
9499064799SGarrett Wollman 		clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
958360efbdSAlfred Perlstein 		if (clp == NULL) {
968360efbdSAlfred Perlstein 			mutex_unlock(&clntraw_lock);
978360efbdSAlfred Perlstein 			return NULL;
988360efbdSAlfred Perlstein 		}
998360efbdSAlfred Perlstein 		if (__rpc_rawcombuf == NULL)
1008360efbdSAlfred Perlstein 			__rpc_rawcombuf =
1018360efbdSAlfred Perlstein 			    (char *)calloc(UDPMSGSIZE, sizeof (char));
1028360efbdSAlfred Perlstein 		clp->_raw_buf = __rpc_rawcombuf;
10399064799SGarrett Wollman 		clntraw_private = clp;
10499064799SGarrett Wollman 	}
105bedffadcSGarrett Wollman 	xdrs = &clp->xdr_stream;
106bedffadcSGarrett Wollman 	client = &clp->client_object;
107bedffadcSGarrett Wollman 
10899064799SGarrett Wollman 	/*
1095f0d67beSAdam David 	 * pre-serialize the static part of the call msg and stash it away
11099064799SGarrett Wollman 	 */
11199064799SGarrett Wollman 	call_msg.rm_direction = CALL;
11299064799SGarrett Wollman 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
1138360efbdSAlfred Perlstein 	/* XXX: prog and vers have been long historically :-( */
1148360efbdSAlfred Perlstein 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
1158360efbdSAlfred Perlstein 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
1168360efbdSAlfred Perlstein 	xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
1178360efbdSAlfred Perlstein 	if (! xdr_callhdr(xdrs, &call_msg))
1188360efbdSAlfred Perlstein 		warnx("clntraw_create - Fatal header serialization error.");
11999064799SGarrett Wollman 	clp->mcnt = XDR_GETPOS(xdrs);
12099064799SGarrett Wollman 	XDR_DESTROY(xdrs);
12199064799SGarrett Wollman 
12299064799SGarrett Wollman 	/*
12399064799SGarrett Wollman 	 * Set xdrmem for client/server shared buffer
12499064799SGarrett Wollman 	 */
12599064799SGarrett Wollman 	xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
12699064799SGarrett Wollman 
12799064799SGarrett Wollman 	/*
12899064799SGarrett Wollman 	 * create client handle
12999064799SGarrett Wollman 	 */
1308360efbdSAlfred Perlstein 	client->cl_ops = clnt_raw_ops();
13199064799SGarrett Wollman 	client->cl_auth = authnone_create();
1328360efbdSAlfred Perlstein 	mutex_unlock(&clntraw_lock);
13399064799SGarrett Wollman 	return (client);
13499064799SGarrett Wollman }
13599064799SGarrett Wollman 
1368360efbdSAlfred Perlstein /* ARGSUSED */
13799064799SGarrett Wollman static enum clnt_stat
clnt_raw_call(CLIENT * h,rpcproc_t proc,xdrproc_t xargs,void * argsp,xdrproc_t xresults,void * resultsp,struct timeval timeout)138587cf682SCraig Rodrigues clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, void *argsp,
139587cf682SCraig Rodrigues     xdrproc_t xresults, void *resultsp, struct timeval timeout)
14099064799SGarrett Wollman {
1418360efbdSAlfred Perlstein 	struct clntraw_private *clp = clntraw_private;
1428360efbdSAlfred Perlstein 	XDR *xdrs = &clp->xdr_stream;
14399064799SGarrett Wollman 	struct rpc_msg msg;
14499064799SGarrett Wollman 	enum clnt_stat status;
14599064799SGarrett Wollman 	struct rpc_err error;
14699064799SGarrett Wollman 
1478360efbdSAlfred Perlstein 	assert(h != NULL);
1488360efbdSAlfred Perlstein 
1498360efbdSAlfred Perlstein 	mutex_lock(&clntraw_lock);
1508360efbdSAlfred Perlstein 	if (clp == NULL) {
1518360efbdSAlfred Perlstein 		mutex_unlock(&clntraw_lock);
15299064799SGarrett Wollman 		return (RPC_FAILED);
1538360efbdSAlfred Perlstein 	}
1548360efbdSAlfred Perlstein 	mutex_unlock(&clntraw_lock);
1558360efbdSAlfred Perlstein 
15699064799SGarrett Wollman call_again:
15799064799SGarrett Wollman 	/*
15899064799SGarrett Wollman 	 * send request
15999064799SGarrett Wollman 	 */
16099064799SGarrett Wollman 	xdrs->x_op = XDR_ENCODE;
16199064799SGarrett Wollman 	XDR_SETPOS(xdrs, 0);
1628360efbdSAlfred Perlstein 	clp->u.mashl_rpcmsg.rm_xid ++ ;
1638360efbdSAlfred Perlstein 	if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
1648360efbdSAlfred Perlstein 	    (! XDR_PUTINT32(xdrs, &proc)) ||
16599064799SGarrett Wollman 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
16699064799SGarrett Wollman 	    (! (*xargs)(xdrs, argsp))) {
16799064799SGarrett Wollman 		return (RPC_CANTENCODEARGS);
16899064799SGarrett Wollman 	}
16999064799SGarrett Wollman 	(void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
17099064799SGarrett Wollman 
17199064799SGarrett Wollman 	/*
17299064799SGarrett Wollman 	 * We have to call server input routine here because this is
17399064799SGarrett Wollman 	 * all going on in one process. Yuk.
17499064799SGarrett Wollman 	 */
1758360efbdSAlfred Perlstein 	svc_getreq_common(FD_SETSIZE);
17699064799SGarrett Wollman 
17799064799SGarrett Wollman 	/*
17899064799SGarrett Wollman 	 * get results
17999064799SGarrett Wollman 	 */
18099064799SGarrett Wollman 	xdrs->x_op = XDR_DECODE;
18199064799SGarrett Wollman 	XDR_SETPOS(xdrs, 0);
18299064799SGarrett Wollman 	msg.acpted_rply.ar_verf = _null_auth;
18399064799SGarrett Wollman 	msg.acpted_rply.ar_results.where = resultsp;
18499064799SGarrett Wollman 	msg.acpted_rply.ar_results.proc = xresults;
1858360efbdSAlfred Perlstein 	if (! xdr_replymsg(xdrs, &msg)) {
1868360efbdSAlfred Perlstein 		/*
1878360efbdSAlfred Perlstein 		 * It's possible for xdr_replymsg() to fail partway
1888360efbdSAlfred Perlstein 		 * through its attempt to decode the result from the
1898360efbdSAlfred Perlstein 		 * server. If this happens, it will leave the reply
1908360efbdSAlfred Perlstein 		 * structure partially populated with dynamically
1918360efbdSAlfred Perlstein 		 * allocated memory. (This can happen if someone uses
1928360efbdSAlfred Perlstein 		 * clntudp_bufcreate() to create a CLIENT handle and
1938360efbdSAlfred Perlstein 		 * specifies a receive buffer size that is too small.)
1948360efbdSAlfred Perlstein 		 * This memory must be free()ed to avoid a leak.
1958360efbdSAlfred Perlstein 		 */
1968360efbdSAlfred Perlstein 		int op = xdrs->x_op;
1978360efbdSAlfred Perlstein 		xdrs->x_op = XDR_FREE;
1988360efbdSAlfred Perlstein 		xdr_replymsg(xdrs, &msg);
1998360efbdSAlfred Perlstein 		xdrs->x_op = op;
20099064799SGarrett Wollman 		return (RPC_CANTDECODERES);
2018360efbdSAlfred Perlstein 	}
20299064799SGarrett Wollman 	_seterr_reply(&msg, &error);
20399064799SGarrett Wollman 	status = error.re_status;
20499064799SGarrett Wollman 
20599064799SGarrett Wollman 	if (status == RPC_SUCCESS) {
20699064799SGarrett Wollman 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
20799064799SGarrett Wollman 			status = RPC_AUTHERROR;
20899064799SGarrett Wollman 		}
20999064799SGarrett Wollman 	}  /* end successful completion */
21099064799SGarrett Wollman 	else {
2118360efbdSAlfred Perlstein 		if (AUTH_REFRESH(h->cl_auth, &msg))
21299064799SGarrett Wollman 			goto call_again;
21399064799SGarrett Wollman 	}  /* end of unsuccessful completion */
21499064799SGarrett Wollman 
21599064799SGarrett Wollman 	if (status == RPC_SUCCESS) {
21699064799SGarrett Wollman 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
21799064799SGarrett Wollman 			status = RPC_AUTHERROR;
21899064799SGarrett Wollman 		}
21999064799SGarrett Wollman 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
22099064799SGarrett Wollman 			xdrs->x_op = XDR_FREE;
22199064799SGarrett Wollman 			(void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
22299064799SGarrett Wollman 		}
22399064799SGarrett Wollman 	}
22499064799SGarrett Wollman 
22599064799SGarrett Wollman 	return (status);
22699064799SGarrett Wollman }
22799064799SGarrett Wollman 
2288360efbdSAlfred Perlstein /*ARGSUSED*/
22999064799SGarrett Wollman static void
clnt_raw_geterr(CLIENT * cl,struct rpc_err * err)230587cf682SCraig Rodrigues clnt_raw_geterr(CLIENT *cl, struct rpc_err *err)
23199064799SGarrett Wollman {
23299064799SGarrett Wollman }
23399064799SGarrett Wollman 
23499064799SGarrett Wollman 
2358360efbdSAlfred Perlstein /* ARGSUSED */
23699064799SGarrett Wollman static bool_t
clnt_raw_freeres(CLIENT * cl,xdrproc_t xdr_res,void * res_ptr)237587cf682SCraig Rodrigues clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
23899064799SGarrett Wollman {
2398360efbdSAlfred Perlstein 	struct clntraw_private *clp = clntraw_private;
2408360efbdSAlfred Perlstein 	XDR *xdrs = &clp->xdr_stream;
24199064799SGarrett Wollman 	bool_t rval;
24299064799SGarrett Wollman 
2438360efbdSAlfred Perlstein 	mutex_lock(&clntraw_lock);
2448360efbdSAlfred Perlstein 	if (clp == NULL) {
24599064799SGarrett Wollman 		rval = (bool_t) RPC_FAILED;
2468360efbdSAlfred Perlstein 		mutex_unlock(&clntraw_lock);
24799064799SGarrett Wollman 		return (rval);
24899064799SGarrett Wollman 	}
2498360efbdSAlfred Perlstein 	mutex_unlock(&clntraw_lock);
25099064799SGarrett Wollman 	xdrs->x_op = XDR_FREE;
25199064799SGarrett Wollman 	return ((*xdr_res)(xdrs, res_ptr));
25299064799SGarrett Wollman }
25399064799SGarrett Wollman 
2548360efbdSAlfred Perlstein /*ARGSUSED*/
25599064799SGarrett Wollman static void
clnt_raw_abort(CLIENT * cl)256587cf682SCraig Rodrigues clnt_raw_abort(CLIENT *cl)
25799064799SGarrett Wollman {
25899064799SGarrett Wollman }
25999064799SGarrett Wollman 
2608360efbdSAlfred Perlstein /*ARGSUSED*/
26199064799SGarrett Wollman static bool_t
clnt_raw_control(CLIENT * cl,u_int ui,void * str)262587cf682SCraig Rodrigues clnt_raw_control(CLIENT *cl, u_int ui, void *str)
26399064799SGarrett Wollman {
26499064799SGarrett Wollman 	return (FALSE);
26599064799SGarrett Wollman }
26699064799SGarrett Wollman 
2678360efbdSAlfred Perlstein /*ARGSUSED*/
26899064799SGarrett Wollman static void
clnt_raw_destroy(CLIENT * cl)269587cf682SCraig Rodrigues clnt_raw_destroy(CLIENT *cl)
27099064799SGarrett Wollman {
27199064799SGarrett Wollman }
2728360efbdSAlfred Perlstein 
2738360efbdSAlfred Perlstein static struct clnt_ops *
clnt_raw_ops(void)274587cf682SCraig Rodrigues clnt_raw_ops(void)
2758360efbdSAlfred Perlstein {
2768360efbdSAlfred Perlstein 	static struct clnt_ops ops;
2778360efbdSAlfred Perlstein 
2788360efbdSAlfred Perlstein 	/* VARIABLES PROTECTED BY ops_lock: ops */
2798360efbdSAlfred Perlstein 
2808360efbdSAlfred Perlstein 	mutex_lock(&ops_lock);
2818360efbdSAlfred Perlstein 	if (ops.cl_call == NULL) {
2828360efbdSAlfred Perlstein 		ops.cl_call = clnt_raw_call;
2838360efbdSAlfred Perlstein 		ops.cl_abort = clnt_raw_abort;
2848360efbdSAlfred Perlstein 		ops.cl_geterr = clnt_raw_geterr;
2858360efbdSAlfred Perlstein 		ops.cl_freeres = clnt_raw_freeres;
2868360efbdSAlfred Perlstein 		ops.cl_destroy = clnt_raw_destroy;
2878360efbdSAlfred Perlstein 		ops.cl_control = clnt_raw_control;
2888360efbdSAlfred Perlstein 	}
2898360efbdSAlfred Perlstein 	mutex_unlock(&ops_lock);
2908360efbdSAlfred Perlstein 	return (&ops);
2918360efbdSAlfred Perlstein }
292