174462Salfred/*	$NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 christos Exp $	*/
274462Salfred
3258578Shrs/*-
4258578Shrs * Copyright (c) 2009, Sun Microsystems, Inc.
5258578Shrs * All rights reserved.
68870Srgrimes *
7258578Shrs * Redistribution and use in source and binary forms, with or without
8258578Shrs * modification, are permitted provided that the following conditions are met:
9258578Shrs * - Redistributions of source code must retain the above copyright notice,
10258578Shrs *   this list of conditions and the following disclaimer.
11258578Shrs * - Redistributions in binary form must reproduce the above copyright notice,
12258578Shrs *   this list of conditions and the following disclaimer in the documentation
13258578Shrs *   and/or other materials provided with the distribution.
14258578Shrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
15258578Shrs *   contributors may be used to endorse or promote products derived
16258578Shrs *   from this software without specific prior written permission.
17258578Shrs *
18258578Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19258578Shrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20258578Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21258578Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22258578Shrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23258578Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24258578Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25258578Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26258578Shrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27258578Shrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28258578Shrs * POSSIBILITY OF SUCH DAMAGE.
291901Swollman */
3074462Salfred/*
3174462Salfred * Copyright (c) 1986-1991 by Sun Microsystems Inc.
3274462Salfred */
331901Swollman
3474462Salfred/* #ident	"@(#)svc_raw.c	1.16	94/04/24 SMI" */
3574462Salfred
36136581Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3774462Salfredstatic char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
381901Swollman#endif
3992990Sobrien#include <sys/cdefs.h>
4092990Sobrien__FBSDID("$FreeBSD$");
411901Swollman
421901Swollman/*
431901Swollman * svc_raw.c,   This a toy for simple testing and timing.
441901Swollman * Interface to create an rpc client and server in the same UNIX process.
451901Swollman * This lets us similate rpc and get rpc (round trip) overhead, without
4685138Salfred * any interference from the kernel.
471901Swollman *
481901Swollman */
491901Swollman
5075094Siedowse#include "namespace.h"
5174462Salfred#include "reentrant.h"
521901Swollman#include <rpc/rpc.h>
5374462Salfred#include <sys/types.h>
5474462Salfred#include <rpc/raw.h>
5511666Sphk#include <stdlib.h>
5674462Salfred#include "un-namespace.h"
57156090Sdeischen#include "mt_misc.h"
581901Swollman
5974462Salfred#ifndef UDPMSGSIZE
6074462Salfred#define	UDPMSGSIZE 8800
6174462Salfred#endif
6274462Salfred
631901Swollman/*
641901Swollman * This is the "network" that we will be moving data over
651901Swollman */
6674462Salfredstatic struct svc_raw_private {
6774462Salfred	char	*raw_buf;	/* should be shared with the cl handle */
68181344Sdfr	SVCXPRT	*server;
691901Swollman	XDR	xdr_stream;
701901Swollman	char	verf_body[MAX_AUTH_BYTES];
7174462Salfred} *svc_raw_private;
721901Swollman
7392905Sobrienstatic enum xprt_stat svc_raw_stat(SVCXPRT *);
7492905Sobrienstatic bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
7592905Sobrienstatic bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
7695658Sdesstatic bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *);
7795658Sdesstatic bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *);
7892905Sobrienstatic void svc_raw_destroy(SVCXPRT *);
7992905Sobrienstatic void svc_raw_ops(SVCXPRT *);
8092905Sobrienstatic bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
811901Swollman
8274462Salfredchar *__rpc_rawcombuf = NULL;
8374462Salfred
841901SwollmanSVCXPRT *
85288113Srodrigcsvc_raw_create(void)
861901Swollman{
8774462Salfred	struct svc_raw_private *srp;
8874462Salfred/* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
891901Swollman
9074462Salfred	mutex_lock(&svcraw_lock);
9174462Salfred	srp = svc_raw_private;
9274462Salfred	if (srp == NULL) {
9374462Salfred		srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
9474462Salfred		if (srp == NULL) {
9574462Salfred			mutex_unlock(&svcraw_lock);
9674462Salfred			return (NULL);
9774462Salfred		}
98234769Skib		if (__rpc_rawcombuf == NULL) {
9974462Salfred			__rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
100234769Skib			if (__rpc_rawcombuf == NULL) {
101234769Skib				free(srp);
102234769Skib				mutex_unlock(&svcraw_lock);
103234769Skib				return (NULL);
104234769Skib			}
105234769Skib		}
10674462Salfred		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
107181344Sdfr		srp->server = svc_xprt_alloc();
108234769Skib		if (srp->server == NULL) {
109234769Skib			free(__rpc_rawcombuf);
110234769Skib			free(srp);
111234769Skib			mutex_unlock(&svcraw_lock);
112234769Skib			return (NULL);
113234769Skib		}
11474462Salfred		svc_raw_private = srp;
1151901Swollman	}
116181344Sdfr	srp->server->xp_fd = FD_SETSIZE;
117181344Sdfr	srp->server->xp_port = 0;
118181344Sdfr	svc_raw_ops(srp->server);
119181344Sdfr	srp->server->xp_verf.oa_base = srp->verf_body;
12074462Salfred	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
121181344Sdfr	xprt_register(srp->server);
12274462Salfred	mutex_unlock(&svcraw_lock);
123181344Sdfr	return (srp->server);
1241901Swollman}
1251901Swollman
12674462Salfred/*ARGSUSED*/
1271901Swollmanstatic enum xprt_stat
128288113Srodrigcsvc_raw_stat(SVCXPRT *xprt)
1291901Swollman{
1301901Swollman	return (XPRT_IDLE);
1311901Swollman}
1321901Swollman
13374462Salfred/*ARGSUSED*/
1341901Swollmanstatic bool_t
135288113Srodrigcsvc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
1361901Swollman{
13774462Salfred	struct svc_raw_private *srp;
13874462Salfred	XDR *xdrs;
1391901Swollman
14074462Salfred	mutex_lock(&svcraw_lock);
14174462Salfred	srp = svc_raw_private;
14274462Salfred	if (srp == NULL) {
14374462Salfred		mutex_unlock(&svcraw_lock);
14474462Salfred		return (FALSE);
14574462Salfred	}
14674462Salfred	mutex_unlock(&svcraw_lock);
14774462Salfred
1481901Swollman	xdrs = &srp->xdr_stream;
1491901Swollman	xdrs->x_op = XDR_DECODE;
15074462Salfred	(void) XDR_SETPOS(xdrs, 0);
15174462Salfred	if (! xdr_callmsg(xdrs, msg)) {
15274462Salfred		return (FALSE);
15374462Salfred	}
1541901Swollman	return (TRUE);
1551901Swollman}
1561901Swollman
15774462Salfred/*ARGSUSED*/
1581901Swollmanstatic bool_t
159288113Srodrigcsvc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
1601901Swollman{
16174462Salfred	struct svc_raw_private *srp;
16274462Salfred	XDR *xdrs;
163181344Sdfr	bool_t stat;
164181344Sdfr	xdrproc_t xdr_proc;
165181344Sdfr	caddr_t xdr_where;
1661901Swollman
16774462Salfred	mutex_lock(&svcraw_lock);
16874462Salfred	srp = svc_raw_private;
16974462Salfred	if (srp == NULL) {
17074462Salfred		mutex_unlock(&svcraw_lock);
1711901Swollman		return (FALSE);
17274462Salfred	}
17374462Salfred	mutex_unlock(&svcraw_lock);
17474462Salfred
1751901Swollman	xdrs = &srp->xdr_stream;
1761901Swollman	xdrs->x_op = XDR_ENCODE;
17774462Salfred	(void) XDR_SETPOS(xdrs, 0);
178181344Sdfr	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
179181344Sdfr	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
180181344Sdfr		xdr_proc = msg->acpted_rply.ar_results.proc;
181181344Sdfr		xdr_where = msg->acpted_rply.ar_results.where;
182181344Sdfr		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
183181344Sdfr		msg->acpted_rply.ar_results.where = NULL;
184181344Sdfr
185199785Swollman		stat = xdr_replymsg(xdrs, msg) &&
186199785Swollman		    SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where);
187181344Sdfr	} else {
188181344Sdfr		stat = xdr_replymsg(xdrs, msg);
189181344Sdfr	}
190181344Sdfr	if (!stat) {
19174462Salfred		return (FALSE);
19274462Salfred	}
19374462Salfred	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
1941901Swollman	return (TRUE);
1951901Swollman}
1961901Swollman
19774462Salfred/*ARGSUSED*/
1981901Swollmanstatic bool_t
199288113Srodrigcsvc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
2001901Swollman{
20174462Salfred	struct svc_raw_private *srp;
2021901Swollman
20374462Salfred	mutex_lock(&svcraw_lock);
20474462Salfred	srp = svc_raw_private;
20574462Salfred	if (srp == NULL) {
20674462Salfred		mutex_unlock(&svcraw_lock);
2071901Swollman		return (FALSE);
20874462Salfred	}
20974462Salfred	mutex_unlock(&svcraw_lock);
210181344Sdfr
211181344Sdfr	return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt), &srp->xdr_stream,
212181344Sdfr		xdr_args, args_ptr));
2131901Swollman}
2141901Swollman
21574462Salfred/*ARGSUSED*/
2161901Swollmanstatic bool_t
217288113Srodrigcsvc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
2188870Srgrimes{
21974462Salfred	struct svc_raw_private *srp;
22074462Salfred	XDR *xdrs;
2211901Swollman
22274462Salfred	mutex_lock(&svcraw_lock);
22374462Salfred	srp = svc_raw_private;
22474462Salfred	if (srp == NULL) {
22574462Salfred		mutex_unlock(&svcraw_lock);
2261901Swollman		return (FALSE);
22774462Salfred	}
22874462Salfred	mutex_unlock(&svcraw_lock);
22974462Salfred
2301901Swollman	xdrs = &srp->xdr_stream;
2311901Swollman	xdrs->x_op = XDR_FREE;
23274462Salfred	return (*xdr_args)(xdrs, args_ptr);
2338870Srgrimes}
2341901Swollman
23574462Salfred/*ARGSUSED*/
2361901Swollmanstatic void
237288113Srodrigcsvc_raw_destroy(SVCXPRT *xprt)
2381901Swollman{
2391901Swollman}
24074462Salfred
24174462Salfred/*ARGSUSED*/
24274462Salfredstatic bool_t
243288113Srodrigcsvc_raw_control(SVCXPRT *xprt, const u_int rq, void *in)
24474462Salfred{
24574462Salfred	return (FALSE);
24674462Salfred}
24774462Salfred
24874462Salfredstatic void
249288113Srodrigcsvc_raw_ops(SVCXPRT *xprt)
25074462Salfred{
25174462Salfred	static struct xp_ops ops;
25274462Salfred	static struct xp_ops2 ops2;
25374462Salfred
25474462Salfred/* VARIABLES PROTECTED BY ops_lock: ops */
25574462Salfred
25674462Salfred	mutex_lock(&ops_lock);
25774462Salfred	if (ops.xp_recv == NULL) {
25874462Salfred		ops.xp_recv = svc_raw_recv;
25974462Salfred		ops.xp_stat = svc_raw_stat;
26074462Salfred		ops.xp_getargs = svc_raw_getargs;
26174462Salfred		ops.xp_reply = svc_raw_reply;
26274462Salfred		ops.xp_freeargs = svc_raw_freeargs;
26374462Salfred		ops.xp_destroy = svc_raw_destroy;
26474462Salfred		ops2.xp_control = svc_raw_control;
26574462Salfred	}
26674462Salfred	xprt->xp_ops = &ops;
26774462Salfred	xprt->xp_ops2 = &ops2;
26874462Salfred	mutex_unlock(&ops_lock);
26974462Salfred}
270