xref: /netbsd-src/lib/libc/rpc/clnt_raw.c (revision 85f5e301f5b0e5c7695c7222d16769f8f55f6aeb)
1*85f5e301Schristos /*	$NetBSD: clnt_raw.c,v 1.34 2024/01/23 17:24:38 christos Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
447c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
647c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron  * modification, are permitted provided that the following conditions are
847c0e0c3Stron  * met:
963d7b677Scgd  *
1047c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
1147c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
1247c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
1347c0e0c3Stron  *       copyright notice, this list of conditions and the following
1447c0e0c3Stron  *       disclaimer in the documentation and/or other materials
1547c0e0c3Stron  *       provided with the distribution.
1647c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
1747c0e0c3Stron  *       contributors may be used to endorse or promote products derived
1847c0e0c3Stron  *       from this software without specific prior written permission.
1963d7b677Scgd  *
2047c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2147c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2247c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2347c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2447c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2547c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2647c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2747c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2847c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2947c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3047c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3147c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd  */
3363d7b677Scgd 
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
38c63c52b2Schristos static char *sccsid = "@(#)clnt_raw.c	2.2 88/08/01 4.0 RPCSRC";
39c63c52b2Schristos #else
40*85f5e301Schristos __RCSID("$NetBSD: clnt_raw.c,v 1.34 2024/01/23 17:24:38 christos Exp $");
41c63c52b2Schristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd 
4463d7b677Scgd /*
4563d7b677Scgd  * clnt_raw.c
4663d7b677Scgd  *
4763d7b677Scgd  * Copyright (C) 1984, Sun Microsystems, Inc.
4863d7b677Scgd  *
4963d7b677Scgd  * Memory based rpc for simple testing and timing.
5063d7b677Scgd  * Interface to create an rpc client and server in the same process.
5163d7b677Scgd  * This lets us similate rpc and get round trip overhead, without
52ed1153a3Swiz  * any interference from the kernel.
5363d7b677Scgd  */
5463d7b677Scgd 
5543fa6fe3Sjtc #include "namespace.h"
567df0ccbaSfvdl #include "reentrant.h"
57b48252f3Slukem #include <assert.h>
5846e6c5e8Slukem #include <err.h>
59c63c52b2Schristos #include <stdio.h>
602e2a3a25Scgd #include <stdlib.h>
6146e6c5e8Slukem 
6263d7b677Scgd #include <rpc/rpc.h>
637df0ccbaSfvdl #include <rpc/raw.h>
6463d7b677Scgd 
65*85f5e301Schristos #include "rpc_internal.h"
66*85f5e301Schristos 
6743fa6fe3Sjtc #ifdef __weak_alias
6860549036Smycroft __weak_alias(clntraw_create,_clntraw_create)
697df0ccbaSfvdl __weak_alias(clnt_raw_create,_clnt_raw_create)
707df0ccbaSfvdl #endif
717df0ccbaSfvdl 
7263d7b677Scgd #define MCALL_MSG_SIZE 24
7363d7b677Scgd 
7463d7b677Scgd /*
7563d7b677Scgd  * This is the "network" we will be moving stuff over.
7663d7b677Scgd  */
7763d7b677Scgd static struct clntraw_private {
7863d7b677Scgd 	CLIENT	client_object;
7963d7b677Scgd 	XDR	xdr_stream;
807df0ccbaSfvdl 	char	*_raw_buf;
816f2f908dSchristos 	union {
826f2f908dSchristos 	    struct rpc_msg	mashl_rpcmsg;
8363d7b677Scgd 	    char 		mashl_callmsg[MCALL_MSG_SIZE];
846f2f908dSchristos 	} u;
85ce147c1cSlukem 	u_int	mcnt;
8663d7b677Scgd } *clntraw_private;
8763d7b677Scgd 
88adb74221Smatt static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t,
89adb74221Smatt     const char *, xdrproc_t, caddr_t, struct timeval);
90adb74221Smatt static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
91adb74221Smatt static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, caddr_t);
92adb74221Smatt static void clnt_raw_abort(CLIENT *);
93adb74221Smatt static bool_t clnt_raw_control(CLIENT *, u_int, char *);
94adb74221Smatt static void clnt_raw_destroy(CLIENT *);
95adb74221Smatt static struct clnt_ops *clnt_raw_ops(void);
9663d7b677Scgd 
9763d7b677Scgd /*
9863d7b677Scgd  * Create a client handle for memory based rpc.
9963d7b677Scgd  */
10063d7b677Scgd CLIENT *
clnt_raw_create(rpcprog_t prog,rpcvers_t vers)101adb74221Smatt clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
10263d7b677Scgd {
10346615f7eSchristos 	struct clntraw_private *clp;
10463d7b677Scgd 	struct rpc_msg call_msg;
10546615f7eSchristos 	XDR *xdrs;
10646615f7eSchristos 	CLIENT *client;
10763d7b677Scgd 
1087df0ccbaSfvdl 	mutex_lock(&clntraw_lock);
10946615f7eSchristos 	if ((clp = clntraw_private) == NULL) {
110bd66ab4aSchristos 		clp = calloc((size_t)1, sizeof (*clp));
111c9cdc302Schristos 		if (clp == NULL)
112c9cdc302Schristos 			goto out;
1137df0ccbaSfvdl 		if (__rpc_rawcombuf == NULL)
1147df0ccbaSfvdl 			__rpc_rawcombuf =
1152d02304dSyamt 			    malloc(UDPMSGSIZE);
116c9cdc302Schristos 		if (__rpc_rawcombuf == NULL)
117c9cdc302Schristos 			goto out;
1187df0ccbaSfvdl 		clp->_raw_buf = __rpc_rawcombuf;
11963d7b677Scgd 		clntraw_private = clp;
12063d7b677Scgd 	}
12146615f7eSchristos 
12246615f7eSchristos 	xdrs = &clp->xdr_stream;
12346615f7eSchristos 	client = &clp->client_object;
12446615f7eSchristos 
12563d7b677Scgd 	/*
12646e6c5e8Slukem 	 * pre-serialize the static part of the call msg and stash it away
12763d7b677Scgd 	 */
12863d7b677Scgd 	call_msg.rm_direction = CALL;
12963d7b677Scgd 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
1306f2f908dSchristos 	/* XXX: prog and vers have been long historically :-( */
1316f2f908dSchristos 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
1326f2f908dSchristos 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
1336f2f908dSchristos 	xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
13446e6c5e8Slukem 	if (! xdr_callhdr(xdrs, &call_msg))
135e24729deSchristos 		warnx("%s: Fatal header serialization error", __func__);
13663d7b677Scgd 	clp->mcnt = XDR_GETPOS(xdrs);
13763d7b677Scgd 	XDR_DESTROY(xdrs);
13863d7b677Scgd 
13963d7b677Scgd 	/*
14063d7b677Scgd 	 * Set xdrmem for client/server shared buffer
14163d7b677Scgd 	 */
14263d7b677Scgd 	xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
14363d7b677Scgd 
14463d7b677Scgd 	/*
14563d7b677Scgd 	 * create client handle
14663d7b677Scgd 	 */
1477df0ccbaSfvdl 	client->cl_ops = clnt_raw_ops();
14863d7b677Scgd 	client->cl_auth = authnone_create();
1497df0ccbaSfvdl 	mutex_unlock(&clntraw_lock);
15063d7b677Scgd 	return (client);
151c9cdc302Schristos out:
152c9cdc302Schristos 	if (clp)
153c9cdc302Schristos 		free(clp);
154c9cdc302Schristos 	mutex_unlock(&clntraw_lock);
155c9cdc302Schristos 	return NULL;
156c9cdc302Schristos 
15763d7b677Scgd }
15863d7b677Scgd 
1596f2f908dSchristos /* ARGSUSED */
16063d7b677Scgd static enum clnt_stat
clnt_raw_call(CLIENT * h,rpcproc_t proc,xdrproc_t xargs,const char * argsp,xdrproc_t xresults,caddr_t resultsp,struct timeval timeout)161adb74221Smatt clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, const char *argsp,
162adb74221Smatt 	xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
16363d7b677Scgd {
16446e6c5e8Slukem 	struct clntraw_private *clp = clntraw_private;
16546e6c5e8Slukem 	XDR *xdrs = &clp->xdr_stream;
16663d7b677Scgd 	struct rpc_msg msg;
16763d7b677Scgd 	enum clnt_stat status;
16863d7b677Scgd 	struct rpc_err error;
16963d7b677Scgd 
170b48252f3Slukem 	_DIAGASSERT(h != NULL);
171b48252f3Slukem 
1727df0ccbaSfvdl 	mutex_lock(&clntraw_lock);
1737df0ccbaSfvdl 	if (clp == NULL) {
1747df0ccbaSfvdl 		mutex_unlock(&clntraw_lock);
17563d7b677Scgd 		return (RPC_FAILED);
1767df0ccbaSfvdl 	}
1777df0ccbaSfvdl 	mutex_unlock(&clntraw_lock);
1787df0ccbaSfvdl 
17963d7b677Scgd call_again:
18063d7b677Scgd 	/*
18163d7b677Scgd 	 * send request
18263d7b677Scgd 	 */
18363d7b677Scgd 	xdrs->x_op = XDR_ENCODE;
18463d7b677Scgd 	XDR_SETPOS(xdrs, 0);
1856f2f908dSchristos 	clp->u.mashl_rpcmsg.rm_xid ++ ;
1866f2f908dSchristos 	if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
187128bd71fSchristos 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
18863d7b677Scgd 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
189347f995cSyamt 	    (! (*xargs)(xdrs, __UNCONST(argsp)))) {
19063d7b677Scgd 		return (RPC_CANTENCODEARGS);
19163d7b677Scgd 	}
19263d7b677Scgd 	(void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
19363d7b677Scgd 
19463d7b677Scgd 	/*
19563d7b677Scgd 	 * We have to call server input routine here because this is
19663d7b677Scgd 	 * all going on in one process. Yuk.
19763d7b677Scgd 	 */
19846615f7eSchristos 	svc_getreq_common(-1);
19963d7b677Scgd 
20063d7b677Scgd 	/*
20163d7b677Scgd 	 * get results
20263d7b677Scgd 	 */
20363d7b677Scgd 	xdrs->x_op = XDR_DECODE;
20463d7b677Scgd 	XDR_SETPOS(xdrs, 0);
20563d7b677Scgd 	msg.acpted_rply.ar_verf = _null_auth;
20663d7b677Scgd 	msg.acpted_rply.ar_results.where = resultsp;
20763d7b677Scgd 	msg.acpted_rply.ar_results.proc = xresults;
20867569086Slukem 	if (! xdr_replymsg(xdrs, &msg)) {
20967569086Slukem 		/*
21067569086Slukem 		 * It's possible for xdr_replymsg() to fail partway
21167569086Slukem 		 * through its attempt to decode the result from the
21267569086Slukem 		 * server. If this happens, it will leave the reply
21367569086Slukem 		 * structure partially populated with dynamically
21467569086Slukem 		 * allocated memory. (This can happen if someone uses
21567569086Slukem 		 * clntudp_bufcreate() to create a CLIENT handle and
21667569086Slukem 		 * specifies a receive buffer size that is too small.)
21767569086Slukem 		 * This memory must be free()ed to avoid a leak.
21867569086Slukem 		 */
21967569086Slukem 		int op = xdrs->x_op;
22067569086Slukem 		xdrs->x_op = XDR_FREE;
22167569086Slukem 		xdr_replymsg(xdrs, &msg);
22267569086Slukem 		xdrs->x_op = op;
22363d7b677Scgd 		return (RPC_CANTDECODERES);
22467569086Slukem 	}
22563d7b677Scgd 	_seterr_reply(&msg, &error);
22663d7b677Scgd 	status = error.re_status;
22763d7b677Scgd 
22863d7b677Scgd 	if (status == RPC_SUCCESS) {
22963d7b677Scgd 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
23063d7b677Scgd 			status = RPC_AUTHERROR;
23163d7b677Scgd 		}
23263d7b677Scgd 	}  /* end successful completion */
23363d7b677Scgd 	else {
23463d7b677Scgd 		if (AUTH_REFRESH(h->cl_auth))
23563d7b677Scgd 			goto call_again;
23663d7b677Scgd 	}  /* end of unsuccessful completion */
23763d7b677Scgd 
23863d7b677Scgd 	if (status == RPC_SUCCESS) {
23963d7b677Scgd 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
24063d7b677Scgd 			status = RPC_AUTHERROR;
24163d7b677Scgd 		}
24263d7b677Scgd 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
24363d7b677Scgd 			xdrs->x_op = XDR_FREE;
24463d7b677Scgd 			(void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
24563d7b677Scgd 		}
24663d7b677Scgd 	}
24763d7b677Scgd 
24863d7b677Scgd 	return (status);
24963d7b677Scgd }
25063d7b677Scgd 
251c63c52b2Schristos /*ARGSUSED*/
25263d7b677Scgd static void
clnt_raw_geterr(CLIENT * cl,struct rpc_err * error)253adb74221Smatt clnt_raw_geterr(CLIENT *cl, struct rpc_err *error)
25463d7b677Scgd {
25563d7b677Scgd }
25663d7b677Scgd 
25763d7b677Scgd 
2586f2f908dSchristos /* ARGSUSED */
25963d7b677Scgd static bool_t
clnt_raw_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)260adb74221Smatt clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
26163d7b677Scgd {
26246e6c5e8Slukem 	struct clntraw_private *clp = clntraw_private;
26346e6c5e8Slukem 	XDR *xdrs = &clp->xdr_stream;
26463d7b677Scgd 	bool_t rval;
26563d7b677Scgd 
2667df0ccbaSfvdl 	mutex_lock(&clntraw_lock);
2677df0ccbaSfvdl 	if (clp == NULL) {
26863d7b677Scgd 		rval = (bool_t) RPC_FAILED;
2697df0ccbaSfvdl 		mutex_unlock(&clntraw_lock);
27063d7b677Scgd 		return (rval);
27163d7b677Scgd 	}
2727df0ccbaSfvdl 	mutex_unlock(&clntraw_lock);
27363d7b677Scgd 	xdrs->x_op = XDR_FREE;
27463d7b677Scgd 	return ((*xdr_res)(xdrs, res_ptr));
27563d7b677Scgd }
27663d7b677Scgd 
277c63c52b2Schristos /*ARGSUSED*/
27863d7b677Scgd static void
clnt_raw_abort(CLIENT * cl)279adb74221Smatt clnt_raw_abort(CLIENT *cl)
28063d7b677Scgd {
28163d7b677Scgd }
28263d7b677Scgd 
283c63c52b2Schristos /*ARGSUSED*/
28463d7b677Scgd static bool_t
clnt_raw_control(CLIENT * cl,u_int ui,char * str)285adb74221Smatt clnt_raw_control(CLIENT *cl, u_int ui, char *str)
28663d7b677Scgd {
28763d7b677Scgd 	return (FALSE);
28863d7b677Scgd }
28963d7b677Scgd 
290c63c52b2Schristos /*ARGSUSED*/
29163d7b677Scgd static void
clnt_raw_destroy(CLIENT * cl)292adb74221Smatt clnt_raw_destroy(CLIENT *cl)
29363d7b677Scgd {
29463d7b677Scgd }
2957df0ccbaSfvdl 
2967df0ccbaSfvdl static struct clnt_ops *
clnt_raw_ops(void)297adb74221Smatt clnt_raw_ops(void)
2987df0ccbaSfvdl {
2997df0ccbaSfvdl 	static struct clnt_ops ops;
3007df0ccbaSfvdl 
3017df0ccbaSfvdl 	/* VARIABLES PROTECTED BY ops_lock: ops */
3027df0ccbaSfvdl 
3037df0ccbaSfvdl 	mutex_lock(&ops_lock);
3047df0ccbaSfvdl 	if (ops.cl_call == NULL) {
3057df0ccbaSfvdl 		ops.cl_call = clnt_raw_call;
3067df0ccbaSfvdl 		ops.cl_abort = clnt_raw_abort;
3077df0ccbaSfvdl 		ops.cl_geterr = clnt_raw_geterr;
3087df0ccbaSfvdl 		ops.cl_freeres = clnt_raw_freeres;
3097df0ccbaSfvdl 		ops.cl_destroy = clnt_raw_destroy;
3107df0ccbaSfvdl 		ops.cl_control = clnt_raw_control;
3117df0ccbaSfvdl 	}
3127df0ccbaSfvdl 	mutex_unlock(&ops_lock);
3137df0ccbaSfvdl 	return (&ops);
3147df0ccbaSfvdl }
315