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