Home | History | Annotate | Line # | Download | only in rpc
svc_raw.c revision 1.19.18.1
      1  1.19.18.1      yamt /*	$NetBSD: svc_raw.c,v 1.19.18.1 2008/05/18 12:30:18 yamt Exp $	*/
      2        1.3       cgd 
      3        1.1       cgd /*
      4        1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5        1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6        1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7        1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8        1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9        1.1       cgd  * program developed by the user.
     10        1.1       cgd  *
     11        1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12        1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13        1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14        1.1       cgd  *
     15        1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16        1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17        1.1       cgd  * modification or enhancement.
     18        1.1       cgd  *
     19        1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20        1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21        1.1       cgd  * OR ANY PART THEREOF.
     22        1.1       cgd  *
     23        1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24        1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25        1.1       cgd  * Sun has been advised of the possibility of such damages.
     26        1.1       cgd  *
     27        1.1       cgd  * Sun Microsystems, Inc.
     28        1.1       cgd  * 2550 Garcia Avenue
     29        1.1       cgd  * Mountain View, California  94043
     30        1.1       cgd  */
     31       1.13      fvdl /*
     32       1.13      fvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33       1.13      fvdl  */
     34       1.13      fvdl 
     35       1.13      fvdl /* #ident	"@(#)svc_raw.c	1.16	94/04/24 SMI" */
     36        1.1       cgd 
     37       1.17    itojun #include <sys/cdefs.h>
     38       1.17    itojun #if defined(LIBC_SCCS) && !defined(lint)
     39        1.4  christos #if 0
     40       1.13      fvdl static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
     41       1.17    itojun #else
     42  1.19.18.1      yamt __RCSID("$NetBSD: svc_raw.c,v 1.19.18.1 2008/05/18 12:30:18 yamt Exp $");
     43        1.4  christos #endif
     44        1.1       cgd #endif
     45        1.1       cgd 
     46        1.1       cgd /*
     47        1.1       cgd  * svc_raw.c,   This a toy for simple testing and timing.
     48        1.1       cgd  * Interface to create an rpc client and server in the same UNIX process.
     49        1.1       cgd  * This lets us similate rpc and get rpc (round trip) overhead, without
     50       1.19       wiz  * any interference from the kernel.
     51        1.1       cgd  *
     52        1.1       cgd  */
     53        1.1       cgd 
     54        1.5       jtc #include "namespace.h"
     55       1.13      fvdl #include "reentrant.h"
     56       1.13      fvdl #include <rpc/rpc.h>
     57       1.13      fvdl #include <sys/types.h>
     58       1.13      fvdl #include <rpc/raw.h>
     59       1.15     lukem #include <assert.h>
     60        1.2       cgd #include <stdlib.h>
     61        1.8     lukem 
     62       1.13      fvdl #ifdef __weak_alias
     63       1.13      fvdl __weak_alias(svc_raw_create,_svc_raw_create)
     64       1.13      fvdl #endif
     65        1.1       cgd 
     66       1.13      fvdl #ifndef UDPMSGSIZE
     67       1.13      fvdl #define	UDPMSGSIZE 8800
     68        1.5       jtc #endif
     69        1.1       cgd 
     70        1.1       cgd /*
     71        1.1       cgd  * This is the "network" that we will be moving data over
     72        1.1       cgd  */
     73       1.13      fvdl static struct svc_raw_private {
     74       1.13      fvdl 	char	*raw_buf;	/* should be shared with the cl handle */
     75        1.1       cgd 	SVCXPRT	server;
     76        1.1       cgd 	XDR	xdr_stream;
     77        1.1       cgd 	char	verf_body[MAX_AUTH_BYTES];
     78       1.13      fvdl } *svc_raw_private;
     79       1.13      fvdl 
     80       1.16   thorpej #ifdef _REENTRANT
     81       1.13      fvdl extern mutex_t	svcraw_lock;
     82       1.13      fvdl #endif
     83        1.1       cgd 
     84       1.13      fvdl static enum xprt_stat svc_raw_stat __P((SVCXPRT *));
     85       1.13      fvdl static bool_t svc_raw_recv __P((SVCXPRT *, struct rpc_msg *));
     86       1.13      fvdl static bool_t svc_raw_reply __P((SVCXPRT *, struct rpc_msg *));
     87       1.13      fvdl static bool_t svc_raw_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     88       1.13      fvdl static bool_t svc_raw_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     89       1.13      fvdl static void svc_raw_destroy __P((SVCXPRT *));
     90       1.13      fvdl static void svc_raw_ops __P((SVCXPRT *));
     91       1.13      fvdl static bool_t svc_raw_control __P((SVCXPRT *, const u_int, void *));
     92       1.13      fvdl 
     93       1.13      fvdl char *__rpc_rawcombuf = NULL;
     94        1.1       cgd 
     95        1.1       cgd SVCXPRT *
     96       1.13      fvdl svc_raw_create()
     97        1.1       cgd {
     98       1.13      fvdl 	struct svc_raw_private *srp;
     99       1.13      fvdl /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
    100        1.1       cgd 
    101       1.13      fvdl 	mutex_lock(&svcraw_lock);
    102       1.13      fvdl 	srp = svc_raw_private;
    103       1.13      fvdl 	if (srp == NULL) {
    104  1.19.18.1      yamt 		srp = calloc(1, sizeof(*srp));
    105  1.19.18.1      yamt 		if (srp == NULL)
    106  1.19.18.1      yamt 			goto out;
    107       1.13      fvdl 		if (__rpc_rawcombuf == NULL)
    108       1.18      yamt 			__rpc_rawcombuf = malloc(UDPMSGSIZE);
    109  1.19.18.1      yamt 		if (__rpc_rawcombuf == NULL)
    110  1.19.18.1      yamt 			goto out;
    111       1.13      fvdl 		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
    112       1.13      fvdl 		svc_raw_private = srp;
    113        1.1       cgd 	}
    114       1.13      fvdl 	srp->server.xp_fd = FD_SETSIZE;
    115        1.1       cgd 	srp->server.xp_port = 0;
    116       1.13      fvdl 	srp->server.xp_p3 = NULL;
    117       1.13      fvdl 	svc_raw_ops(&srp->server);
    118        1.1       cgd 	srp->server.xp_verf.oa_base = srp->verf_body;
    119       1.13      fvdl 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
    120       1.13      fvdl 	xprt_register(&srp->server);
    121       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    122        1.1       cgd 	return (&srp->server);
    123  1.19.18.1      yamt out:
    124  1.19.18.1      yamt 	mutex_unlock(&svcraw_lock);
    125  1.19.18.1      yamt 	return (NULL);
    126        1.1       cgd }
    127        1.1       cgd 
    128       1.14  christos /*ARGSUSED*/
    129        1.1       cgd static enum xprt_stat
    130       1.13      fvdl svc_raw_stat(xprt)
    131       1.13      fvdl SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
    132        1.1       cgd {
    133        1.1       cgd 	return (XPRT_IDLE);
    134        1.1       cgd }
    135        1.1       cgd 
    136       1.14  christos /*ARGSUSED*/
    137        1.1       cgd static bool_t
    138       1.13      fvdl svc_raw_recv(xprt, msg)
    139        1.1       cgd 	SVCXPRT *xprt;
    140        1.1       cgd 	struct rpc_msg *msg;
    141        1.1       cgd {
    142       1.13      fvdl 	struct svc_raw_private *srp;
    143        1.8     lukem 	XDR *xdrs;
    144        1.1       cgd 
    145       1.13      fvdl 	mutex_lock(&svcraw_lock);
    146       1.13      fvdl 	srp = svc_raw_private;
    147       1.13      fvdl 	if (srp == NULL) {
    148       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    149       1.13      fvdl 		return (FALSE);
    150       1.13      fvdl 	}
    151       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    152       1.11     lukem 
    153        1.1       cgd 	xdrs = &srp->xdr_stream;
    154        1.1       cgd 	xdrs->x_op = XDR_DECODE;
    155       1.13      fvdl 	(void) XDR_SETPOS(xdrs, 0);
    156       1.13      fvdl 	if (! xdr_callmsg(xdrs, msg)) {
    157       1.13      fvdl 		return (FALSE);
    158       1.13      fvdl 	}
    159        1.1       cgd 	return (TRUE);
    160        1.1       cgd }
    161        1.1       cgd 
    162       1.14  christos /*ARGSUSED*/
    163        1.1       cgd static bool_t
    164       1.13      fvdl svc_raw_reply(xprt, msg)
    165        1.1       cgd 	SVCXPRT *xprt;
    166        1.1       cgd 	struct rpc_msg *msg;
    167        1.1       cgd {
    168       1.13      fvdl 	struct svc_raw_private *srp;
    169        1.8     lukem 	XDR *xdrs;
    170        1.1       cgd 
    171       1.13      fvdl 	mutex_lock(&svcraw_lock);
    172       1.13      fvdl 	srp = svc_raw_private;
    173       1.13      fvdl 	if (srp == NULL) {
    174       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    175       1.13      fvdl 		return (FALSE);
    176       1.13      fvdl 	}
    177       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    178       1.11     lukem 
    179        1.1       cgd 	xdrs = &srp->xdr_stream;
    180        1.1       cgd 	xdrs->x_op = XDR_ENCODE;
    181       1.13      fvdl 	(void) XDR_SETPOS(xdrs, 0);
    182       1.13      fvdl 	if (! xdr_replymsg(xdrs, msg)) {
    183       1.13      fvdl 		return (FALSE);
    184       1.13      fvdl 	}
    185       1.13      fvdl 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
    186        1.1       cgd 	return (TRUE);
    187        1.1       cgd }
    188        1.1       cgd 
    189       1.14  christos /*ARGSUSED*/
    190        1.1       cgd static bool_t
    191       1.13      fvdl svc_raw_getargs(xprt, xdr_args, args_ptr)
    192        1.1       cgd 	SVCXPRT *xprt;
    193        1.1       cgd 	xdrproc_t xdr_args;
    194        1.1       cgd 	caddr_t args_ptr;
    195        1.1       cgd {
    196       1.14  christos 	struct svc_raw_private *srp;
    197       1.11     lukem 
    198       1.13      fvdl 	mutex_lock(&svcraw_lock);
    199       1.13      fvdl 	srp = svc_raw_private;
    200       1.13      fvdl 	if (srp == NULL) {
    201       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    202        1.1       cgd 		return (FALSE);
    203       1.13      fvdl 	}
    204       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    205       1.13      fvdl 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
    206        1.1       cgd }
    207        1.1       cgd 
    208       1.14  christos /*ARGSUSED*/
    209        1.1       cgd static bool_t
    210       1.13      fvdl svc_raw_freeargs(xprt, xdr_args, args_ptr)
    211        1.1       cgd 	SVCXPRT *xprt;
    212        1.1       cgd 	xdrproc_t xdr_args;
    213        1.1       cgd 	caddr_t args_ptr;
    214       1.13      fvdl {
    215       1.13      fvdl 	struct svc_raw_private *srp;
    216        1.8     lukem 	XDR *xdrs;
    217       1.11     lukem 
    218       1.13      fvdl 	mutex_lock(&svcraw_lock);
    219       1.13      fvdl 	srp = svc_raw_private;
    220       1.13      fvdl 	if (srp == NULL) {
    221       1.13      fvdl 		mutex_unlock(&svcraw_lock);
    222       1.13      fvdl 		return (FALSE);
    223       1.13      fvdl 	}
    224       1.13      fvdl 	mutex_unlock(&svcraw_lock);
    225        1.1       cgd 
    226        1.1       cgd 	xdrs = &srp->xdr_stream;
    227        1.1       cgd 	xdrs->x_op = XDR_FREE;
    228       1.13      fvdl 	return (*xdr_args)(xdrs, args_ptr);
    229       1.13      fvdl }
    230        1.1       cgd 
    231       1.14  christos /*ARGSUSED*/
    232        1.1       cgd static void
    233       1.13      fvdl svc_raw_destroy(xprt)
    234       1.13      fvdl SVCXPRT *xprt;
    235       1.13      fvdl {
    236       1.13      fvdl }
    237       1.13      fvdl 
    238       1.14  christos /*ARGSUSED*/
    239       1.13      fvdl static bool_t
    240       1.13      fvdl svc_raw_control(xprt, rq, in)
    241       1.13      fvdl 	SVCXPRT *xprt;
    242       1.13      fvdl 	const u_int	rq;
    243       1.13      fvdl 	void		*in;
    244       1.13      fvdl {
    245       1.13      fvdl 	return (FALSE);
    246       1.13      fvdl }
    247       1.13      fvdl 
    248       1.13      fvdl static void
    249       1.13      fvdl svc_raw_ops(xprt)
    250        1.4  christos 	SVCXPRT *xprt;
    251        1.1       cgd {
    252       1.13      fvdl 	static struct xp_ops ops;
    253       1.13      fvdl 	static struct xp_ops2 ops2;
    254       1.16   thorpej #ifdef _REENTRANT
    255       1.13      fvdl 	extern mutex_t ops_lock;
    256       1.13      fvdl #endif
    257       1.15     lukem 
    258       1.15     lukem 	_DIAGASSERT(xprt != NULL);
    259       1.13      fvdl 
    260       1.13      fvdl /* VARIABLES PROTECTED BY ops_lock: ops */
    261       1.13      fvdl 
    262       1.13      fvdl 	mutex_lock(&ops_lock);
    263       1.13      fvdl 	if (ops.xp_recv == NULL) {
    264       1.13      fvdl 		ops.xp_recv = svc_raw_recv;
    265       1.13      fvdl 		ops.xp_stat = svc_raw_stat;
    266       1.13      fvdl 		ops.xp_getargs = svc_raw_getargs;
    267       1.13      fvdl 		ops.xp_reply = svc_raw_reply;
    268       1.13      fvdl 		ops.xp_freeargs = svc_raw_freeargs;
    269       1.13      fvdl 		ops.xp_destroy = svc_raw_destroy;
    270       1.13      fvdl 		ops2.xp_control = svc_raw_control;
    271       1.13      fvdl 	}
    272       1.13      fvdl 	xprt->xp_ops = &ops;
    273       1.13      fvdl 	xprt->xp_ops2 = &ops2;
    274       1.13      fvdl 	mutex_unlock(&ops_lock);
    275        1.1       cgd }
    276