svc_raw.c revision 156090
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 156090 2006-02-27 22:10:59Z deischen $");
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 */
691901Swollman	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		}
9974462Salfred		if (__rpc_rawcombuf == NULL)
10074462Salfred			__rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
10174462Salfred		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
10274462Salfred		svc_raw_private = srp;
1031901Swollman	}
10474462Salfred	srp->server.xp_fd = FD_SETSIZE;
1051901Swollman	srp->server.xp_port = 0;
10674462Salfred	srp->server.xp_p3 = NULL;
10774462Salfred	svc_raw_ops(&srp->server);
1081901Swollman	srp->server.xp_verf.oa_base = srp->verf_body;
10974462Salfred	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
11074462Salfred	xprt_register(&srp->server);
11174462Salfred	mutex_unlock(&svcraw_lock);
1121901Swollman	return (&srp->server);
1131901Swollman}
1141901Swollman
11574462Salfred/*ARGSUSED*/
1161901Swollmanstatic enum xprt_stat
11774462Salfredsvc_raw_stat(xprt)
11874462SalfredSVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
1191901Swollman{
1201901Swollman	return (XPRT_IDLE);
1211901Swollman}
1221901Swollman
12374462Salfred/*ARGSUSED*/
1241901Swollmanstatic bool_t
12574462Salfredsvc_raw_recv(xprt, msg)
1261901Swollman	SVCXPRT *xprt;
1271901Swollman	struct rpc_msg *msg;
1281901Swollman{
12974462Salfred	struct svc_raw_private *srp;
13074462Salfred	XDR *xdrs;
1311901Swollman
13274462Salfred	mutex_lock(&svcraw_lock);
13374462Salfred	srp = svc_raw_private;
13474462Salfred	if (srp == NULL) {
13574462Salfred		mutex_unlock(&svcraw_lock);
13674462Salfred		return (FALSE);
13774462Salfred	}
13874462Salfred	mutex_unlock(&svcraw_lock);
13974462Salfred
1401901Swollman	xdrs = &srp->xdr_stream;
1411901Swollman	xdrs->x_op = XDR_DECODE;
14274462Salfred	(void) XDR_SETPOS(xdrs, 0);
14374462Salfred	if (! xdr_callmsg(xdrs, msg)) {
14474462Salfred		return (FALSE);
14574462Salfred	}
1461901Swollman	return (TRUE);
1471901Swollman}
1481901Swollman
14974462Salfred/*ARGSUSED*/
1501901Swollmanstatic bool_t
15174462Salfredsvc_raw_reply(xprt, msg)
1521901Swollman	SVCXPRT *xprt;
1531901Swollman	struct rpc_msg *msg;
1541901Swollman{
15574462Salfred	struct svc_raw_private *srp;
15674462Salfred	XDR *xdrs;
1571901Swollman
15874462Salfred	mutex_lock(&svcraw_lock);
15974462Salfred	srp = svc_raw_private;
16074462Salfred	if (srp == NULL) {
16174462Salfred		mutex_unlock(&svcraw_lock);
1621901Swollman		return (FALSE);
16374462Salfred	}
16474462Salfred	mutex_unlock(&svcraw_lock);
16574462Salfred
1661901Swollman	xdrs = &srp->xdr_stream;
1671901Swollman	xdrs->x_op = XDR_ENCODE;
16874462Salfred	(void) XDR_SETPOS(xdrs, 0);
16974462Salfred	if (! xdr_replymsg(xdrs, msg)) {
17074462Salfred		return (FALSE);
17174462Salfred	}
17274462Salfred	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
1731901Swollman	return (TRUE);
1741901Swollman}
1751901Swollman
17674462Salfred/*ARGSUSED*/
1771901Swollmanstatic bool_t
17874462Salfredsvc_raw_getargs(xprt, xdr_args, args_ptr)
1791901Swollman	SVCXPRT *xprt;
1801901Swollman	xdrproc_t xdr_args;
18195658Sdes	void *args_ptr;
1821901Swollman{
18374462Salfred	struct svc_raw_private *srp;
1841901Swollman
18574462Salfred	mutex_lock(&svcraw_lock);
18674462Salfred	srp = svc_raw_private;
18774462Salfred	if (srp == NULL) {
18874462Salfred		mutex_unlock(&svcraw_lock);
1891901Swollman		return (FALSE);
19074462Salfred	}
19174462Salfred	mutex_unlock(&svcraw_lock);
19274462Salfred	return (*xdr_args)(&srp->xdr_stream, args_ptr);
1931901Swollman}
1941901Swollman
19574462Salfred/*ARGSUSED*/
1961901Swollmanstatic bool_t
19774462Salfredsvc_raw_freeargs(xprt, xdr_args, args_ptr)
1981901Swollman	SVCXPRT *xprt;
1991901Swollman	xdrproc_t xdr_args;
20095658Sdes	void *args_ptr;
2018870Srgrimes{
20274462Salfred	struct svc_raw_private *srp;
20374462Salfred	XDR *xdrs;
2041901Swollman
20574462Salfred	mutex_lock(&svcraw_lock);
20674462Salfred	srp = svc_raw_private;
20774462Salfred	if (srp == NULL) {
20874462Salfred		mutex_unlock(&svcraw_lock);
2091901Swollman		return (FALSE);
21074462Salfred	}
21174462Salfred	mutex_unlock(&svcraw_lock);
21274462Salfred
2131901Swollman	xdrs = &srp->xdr_stream;
2141901Swollman	xdrs->x_op = XDR_FREE;
21574462Salfred	return (*xdr_args)(xdrs, args_ptr);
2168870Srgrimes}
2171901Swollman
21874462Salfred/*ARGSUSED*/
2191901Swollmanstatic void
22074462Salfredsvc_raw_destroy(xprt)
22174462SalfredSVCXPRT *xprt;
2221901Swollman{
2231901Swollman}
22474462Salfred
22574462Salfred/*ARGSUSED*/
22674462Salfredstatic bool_t
22774462Salfredsvc_raw_control(xprt, rq, in)
22874462Salfred	SVCXPRT *xprt;
22974462Salfred	const u_int	rq;
23074462Salfred	void		*in;
23174462Salfred{
23274462Salfred	return (FALSE);
23374462Salfred}
23474462Salfred
23574462Salfredstatic void
23674462Salfredsvc_raw_ops(xprt)
23774462Salfred	SVCXPRT *xprt;
23874462Salfred{
23974462Salfred	static struct xp_ops ops;
24074462Salfred	static struct xp_ops2 ops2;
24174462Salfred
24274462Salfred/* VARIABLES PROTECTED BY ops_lock: ops */
24374462Salfred
24474462Salfred	mutex_lock(&ops_lock);
24574462Salfred	if (ops.xp_recv == NULL) {
24674462Salfred		ops.xp_recv = svc_raw_recv;
24774462Salfred		ops.xp_stat = svc_raw_stat;
24874462Salfred		ops.xp_getargs = svc_raw_getargs;
24974462Salfred		ops.xp_reply = svc_raw_reply;
25074462Salfred		ops.xp_freeargs = svc_raw_freeargs;
25174462Salfred		ops.xp_destroy = svc_raw_destroy;
25274462Salfred		ops2.xp_control = svc_raw_control;
25374462Salfred	}
25474462Salfred	xprt->xp_ops = &ops;
25574462Salfred	xprt->xp_ops2 = &ops2;
25674462Salfred	mutex_unlock(&ops_lock);
25774462Salfred}
258