svc_raw.c revision 234769
174462Salfred/*	$NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 christos Exp $	*/
274462Salfred
31901Swollman/*
41901Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
51901Swollman * unrestricted use provided that this legend is included on all tape
61901Swollman * media and as a part of the software program in whole or part.  Users
71901Swollman * may copy or modify Sun RPC without charge, but are not authorized
81901Swollman * to license or distribute it to anyone else except as part of a product or
91901Swollman * program developed by the user.
108870Srgrimes *
111901Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
121901Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
131901Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
148870Srgrimes *
151901Swollman * Sun RPC is provided with no support and without any obligation on the
161901Swollman * part of Sun Microsystems, Inc. to assist in its use, correction,
171901Swollman * modification or enhancement.
188870Srgrimes *
191901Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
201901Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
211901Swollman * OR ANY PART THEREOF.
228870Srgrimes *
231901Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue
241901Swollman * or profits or other special, indirect and consequential damages, even if
251901Swollman * Sun has been advised of the possibility of such damages.
268870Srgrimes *
271901Swollman * Sun Microsystems, Inc.
281901Swollman * 2550 Garcia Avenue
291901Swollman * Mountain View, California  94043
301901Swollman */
3174462Salfred/*
3274462Salfred * Copyright (c) 1986-1991 by Sun Microsystems Inc.
3374462Salfred */
341901Swollman
3574462Salfred/* #ident	"@(#)svc_raw.c	1.16	94/04/24 SMI" */
3674462Salfred
37136581Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3874462Salfredstatic char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
391901Swollman#endif
4092990Sobrien#include <sys/cdefs.h>
4192990Sobrien__FBSDID("$FreeBSD: head/lib/libc/rpc/svc_raw.c 234769 2012-04-28 18:57:27Z kib $");
421901Swollman
431901Swollman/*
441901Swollman * svc_raw.c,   This a toy for simple testing and timing.
451901Swollman * Interface to create an rpc client and server in the same UNIX process.
461901Swollman * This lets us similate rpc and get rpc (round trip) overhead, without
4785138Salfred * any interference from the kernel.
481901Swollman *
491901Swollman */
501901Swollman
5175094Siedowse#include "namespace.h"
5274462Salfred#include "reentrant.h"
531901Swollman#include <rpc/rpc.h>
5474462Salfred#include <sys/types.h>
5574462Salfred#include <rpc/raw.h>
5611666Sphk#include <stdlib.h>
5774462Salfred#include "un-namespace.h"
58156090Sdeischen#include "mt_misc.h"
591901Swollman
6074462Salfred#ifndef UDPMSGSIZE
6174462Salfred#define	UDPMSGSIZE 8800
6274462Salfred#endif
6374462Salfred
641901Swollman/*
651901Swollman * This is the "network" that we will be moving data over
661901Swollman */
6774462Salfredstatic struct svc_raw_private {
6874462Salfred	char	*raw_buf;	/* should be shared with the cl handle */
69181344Sdfr	SVCXPRT	*server;
701901Swollman	XDR	xdr_stream;
711901Swollman	char	verf_body[MAX_AUTH_BYTES];
7274462Salfred} *svc_raw_private;
731901Swollman
7492905Sobrienstatic enum xprt_stat svc_raw_stat(SVCXPRT *);
7592905Sobrienstatic bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
7692905Sobrienstatic bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
7795658Sdesstatic bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *);
7895658Sdesstatic bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *);
7992905Sobrienstatic void svc_raw_destroy(SVCXPRT *);
8092905Sobrienstatic void svc_raw_ops(SVCXPRT *);
8192905Sobrienstatic bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
821901Swollman
8374462Salfredchar *__rpc_rawcombuf = NULL;
8474462Salfred
851901SwollmanSVCXPRT *
8674462Salfredsvc_raw_create()
871901Swollman{
8874462Salfred	struct svc_raw_private *srp;
8974462Salfred/* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
901901Swollman
9174462Salfred	mutex_lock(&svcraw_lock);
9274462Salfred	srp = svc_raw_private;
9374462Salfred	if (srp == NULL) {
9474462Salfred		srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
9574462Salfred		if (srp == NULL) {
9674462Salfred			mutex_unlock(&svcraw_lock);
9774462Salfred			return (NULL);
9874462Salfred		}
99234769Skib		if (__rpc_rawcombuf == NULL) {
10074462Salfred			__rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
101234769Skib			if (__rpc_rawcombuf == NULL) {
102234769Skib				free(srp);
103234769Skib				mutex_unlock(&svcraw_lock);
104234769Skib				return (NULL);
105234769Skib			}
106234769Skib		}
10774462Salfred		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
108181344Sdfr		srp->server = svc_xprt_alloc();
109234769Skib		if (srp->server == NULL) {
110234769Skib			free(__rpc_rawcombuf);
111234769Skib			free(srp);
112234769Skib			mutex_unlock(&svcraw_lock);
113234769Skib			return (NULL);
114234769Skib		}
11574462Salfred		svc_raw_private = srp;
1161901Swollman	}
117181344Sdfr	srp->server->xp_fd = FD_SETSIZE;
118181344Sdfr	srp->server->xp_port = 0;
119181344Sdfr	svc_raw_ops(srp->server);
120181344Sdfr	srp->server->xp_verf.oa_base = srp->verf_body;
12174462Salfred	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
122181344Sdfr	xprt_register(srp->server);
12374462Salfred	mutex_unlock(&svcraw_lock);
124181344Sdfr	return (srp->server);
1251901Swollman}
1261901Swollman
12774462Salfred/*ARGSUSED*/
1281901Swollmanstatic enum xprt_stat
12974462Salfredsvc_raw_stat(xprt)
13074462SalfredSVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
1311901Swollman{
1321901Swollman	return (XPRT_IDLE);
1331901Swollman}
1341901Swollman
13574462Salfred/*ARGSUSED*/
1361901Swollmanstatic bool_t
13774462Salfredsvc_raw_recv(xprt, msg)
1381901Swollman	SVCXPRT *xprt;
1391901Swollman	struct rpc_msg *msg;
1401901Swollman{
14174462Salfred	struct svc_raw_private *srp;
14274462Salfred	XDR *xdrs;
1431901Swollman
14474462Salfred	mutex_lock(&svcraw_lock);
14574462Salfred	srp = svc_raw_private;
14674462Salfred	if (srp == NULL) {
14774462Salfred		mutex_unlock(&svcraw_lock);
14874462Salfred		return (FALSE);
14974462Salfred	}
15074462Salfred	mutex_unlock(&svcraw_lock);
15174462Salfred
1521901Swollman	xdrs = &srp->xdr_stream;
1531901Swollman	xdrs->x_op = XDR_DECODE;
15474462Salfred	(void) XDR_SETPOS(xdrs, 0);
15574462Salfred	if (! xdr_callmsg(xdrs, msg)) {
15674462Salfred		return (FALSE);
15774462Salfred	}
1581901Swollman	return (TRUE);
1591901Swollman}
1601901Swollman
16174462Salfred/*ARGSUSED*/
1621901Swollmanstatic bool_t
16374462Salfredsvc_raw_reply(xprt, msg)
1641901Swollman	SVCXPRT *xprt;
1651901Swollman	struct rpc_msg *msg;
1661901Swollman{
16774462Salfred	struct svc_raw_private *srp;
16874462Salfred	XDR *xdrs;
169181344Sdfr	bool_t stat;
170181344Sdfr	xdrproc_t xdr_proc;
171181344Sdfr	caddr_t xdr_where;
1721901Swollman
17374462Salfred	mutex_lock(&svcraw_lock);
17474462Salfred	srp = svc_raw_private;
17574462Salfred	if (srp == NULL) {
17674462Salfred		mutex_unlock(&svcraw_lock);
1771901Swollman		return (FALSE);
17874462Salfred	}
17974462Salfred	mutex_unlock(&svcraw_lock);
18074462Salfred
1811901Swollman	xdrs = &srp->xdr_stream;
1821901Swollman	xdrs->x_op = XDR_ENCODE;
18374462Salfred	(void) XDR_SETPOS(xdrs, 0);
184181344Sdfr	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
185181344Sdfr	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
186181344Sdfr		xdr_proc = msg->acpted_rply.ar_results.proc;
187181344Sdfr		xdr_where = msg->acpted_rply.ar_results.where;
188181344Sdfr		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
189181344Sdfr		msg->acpted_rply.ar_results.where = NULL;
190181344Sdfr
191199785Swollman		stat = xdr_replymsg(xdrs, msg) &&
192199785Swollman		    SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where);
193181344Sdfr	} else {
194181344Sdfr		stat = xdr_replymsg(xdrs, msg);
195181344Sdfr	}
196181344Sdfr	if (!stat) {
19774462Salfred		return (FALSE);
19874462Salfred	}
19974462Salfred	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
2001901Swollman	return (TRUE);
2011901Swollman}
2021901Swollman
20374462Salfred/*ARGSUSED*/
2041901Swollmanstatic bool_t
20574462Salfredsvc_raw_getargs(xprt, xdr_args, args_ptr)
2061901Swollman	SVCXPRT *xprt;
2071901Swollman	xdrproc_t xdr_args;
20895658Sdes	void *args_ptr;
2091901Swollman{
21074462Salfred	struct svc_raw_private *srp;
2111901Swollman
21274462Salfred	mutex_lock(&svcraw_lock);
21374462Salfred	srp = svc_raw_private;
21474462Salfred	if (srp == NULL) {
21574462Salfred		mutex_unlock(&svcraw_lock);
2161901Swollman		return (FALSE);
21774462Salfred	}
21874462Salfred	mutex_unlock(&svcraw_lock);
219181344Sdfr
220181344Sdfr	return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt), &srp->xdr_stream,
221181344Sdfr		xdr_args, args_ptr));
2221901Swollman}
2231901Swollman
22474462Salfred/*ARGSUSED*/
2251901Swollmanstatic bool_t
22674462Salfredsvc_raw_freeargs(xprt, xdr_args, args_ptr)
2271901Swollman	SVCXPRT *xprt;
2281901Swollman	xdrproc_t xdr_args;
22995658Sdes	void *args_ptr;
2308870Srgrimes{
23174462Salfred	struct svc_raw_private *srp;
23274462Salfred	XDR *xdrs;
2331901Swollman
23474462Salfred	mutex_lock(&svcraw_lock);
23574462Salfred	srp = svc_raw_private;
23674462Salfred	if (srp == NULL) {
23774462Salfred		mutex_unlock(&svcraw_lock);
2381901Swollman		return (FALSE);
23974462Salfred	}
24074462Salfred	mutex_unlock(&svcraw_lock);
24174462Salfred
2421901Swollman	xdrs = &srp->xdr_stream;
2431901Swollman	xdrs->x_op = XDR_FREE;
24474462Salfred	return (*xdr_args)(xdrs, args_ptr);
2458870Srgrimes}
2461901Swollman
24774462Salfred/*ARGSUSED*/
2481901Swollmanstatic void
24974462Salfredsvc_raw_destroy(xprt)
25074462SalfredSVCXPRT *xprt;
2511901Swollman{
2521901Swollman}
25374462Salfred
25474462Salfred/*ARGSUSED*/
25574462Salfredstatic bool_t
25674462Salfredsvc_raw_control(xprt, rq, in)
25774462Salfred	SVCXPRT *xprt;
25874462Salfred	const u_int	rq;
25974462Salfred	void		*in;
26074462Salfred{
26174462Salfred	return (FALSE);
26274462Salfred}
26374462Salfred
26474462Salfredstatic void
26574462Salfredsvc_raw_ops(xprt)
26674462Salfred	SVCXPRT *xprt;
26774462Salfred{
26874462Salfred	static struct xp_ops ops;
26974462Salfred	static struct xp_ops2 ops2;
27074462Salfred
27174462Salfred/* VARIABLES PROTECTED BY ops_lock: ops */
27274462Salfred
27374462Salfred	mutex_lock(&ops_lock);
27474462Salfred	if (ops.xp_recv == NULL) {
27574462Salfred		ops.xp_recv = svc_raw_recv;
27674462Salfred		ops.xp_stat = svc_raw_stat;
27774462Salfred		ops.xp_getargs = svc_raw_getargs;
27874462Salfred		ops.xp_reply = svc_raw_reply;
27974462Salfred		ops.xp_freeargs = svc_raw_freeargs;
28074462Salfred		ops.xp_destroy = svc_raw_destroy;
28174462Salfred		ops2.xp_control = svc_raw_control;
28274462Salfred	}
28374462Salfred	xprt->xp_ops = &ops;
28474462Salfred	xprt->xp_ops2 = &ops2;
28574462Salfred	mutex_unlock(&ops_lock);
28674462Salfred}
287